129
|
1 /**
|
|
2 * Copyright: Copyright (c) 2008 Jacob Carlborg. All rights reserved.
|
|
3 * Authors: Jacob Carlborg
|
|
4 * Version: Initial created: 2008
|
|
5 * License: $(LINK2 http://opensource.org/licenses/bsd-license.php, BSD Style)
|
|
6 *
|
|
7 */
|
|
8 module dwt.dwthelper.associativearray;
|
|
9
|
|
10 /**
|
|
11 * Returns the value to which the specified key is mapped,
|
|
12 * or ($D_CODE null) if this associative array contains no mapping for the key.
|
|
13 *
|
|
14 * $(P More formally, if the specified associative array contains a mapping from a key
|
|
15 * $(D_CODE k) to a value $(D_CODE v) such that $(D_CODE (key==null ? k==null :
|
|
16 * key.equals(k))), then this method returns $(D_CODE v); otherwise
|
|
17 * it returns $(D_CODE null). (There can be at most one such mapping.))
|
|
18 *
|
|
19 * Params:
|
|
20 * aa = the associative array to get the value from
|
|
21 * key = the key whose associated value is to be returned
|
|
22 *
|
|
23 *
|
|
24 * Returns: the value to which the specified key is mapped, or
|
|
25 * $(D_CODE null) if this map contains no mapping for the key
|
|
26 *
|
|
27 * Throws: AssertException if any paramter is invalid
|
|
28 */
|
|
29 V get (K, V) (V[K] aa, K key)
|
|
30 in
|
|
31 {
|
|
32 assert(aa.length > 0);
|
|
33 }
|
|
34 body
|
|
35 {
|
|
36 return aa[key];
|
|
37 }
|
|
38
|
|
39 /**
|
|
40 * Associates the specified value with the specified key in the specified
|
|
41 * associative array. If the associative array previously contained a mapping for
|
|
42 * the key, the old value is replaced by the specified value. (An associative array
|
|
43 * <tt>aa</tt> is said to contain a mapping for a key <tt>k</tt> if and only
|
|
44 * if $(LINK2 #containsKey(Object), m.containsKey(k)) would return
|
|
45 * <tt>true</tt>.)
|
|
46 *
|
|
47 * Params:
|
|
48 * aa = the associative array to add the key/value pair to
|
|
49 * key = key with which the specified value is to be associated
|
|
50 * value = value to be associated with the specified key
|
|
51 *
|
|
52 * Returns: the previous value associated with <tt>key</tt>, or
|
|
53 * <tt>null</tt> if there was no mapping for <tt>key</tt>.
|
|
54 * (A <tt>null</tt> return can also indicate that the
|
|
55 * associative array previously associated <tt>null</tt>
|
|
56 * with <tt>key</tt>.)
|
|
57 */
|
|
58 V put (K, V) (V[K] aa, K key, V value)
|
|
59 {
|
|
60 return aa[key] = value;
|
|
61 }
|
|
62
|
|
63 /**
|
|
64 * Removes the mapping for a key from the specified
|
|
65 * associative array if it is present. More formally,
|
|
66 * if the associative array contains a mapping
|
|
67 * from key <tt>k</tt> to value <tt>v</tt> such that
|
|
68 * $(D_CODE (key==null ? k==null : key.equals(k))), that mapping
|
|
69 * is removed. (The associative array can contain at most one such mapping.)
|
|
70 *
|
|
71 * $(P Returns the value to which the associative array previously associated the key,
|
|
72 * or <tt>null</tt> if the map contained no mapping for the key.)
|
|
73 *
|
|
74 * Params:
|
|
75 * aa = the associative array to remove the key/value pair from
|
|
76 * key = key whose mapping is to be removed from the associative array
|
|
77 *
|
|
78 * Returns:
|
|
79 */
|
|
80 V remove (K, V) (V[K] aa, K key)
|
|
81 {
|
|
82 V v = aa[key];
|
|
83 aa.remove(k);
|
|
84
|
|
85 return v;
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 * Returns <tt>true</tt> if the specified
|
|
90 * associative array contains no key-value mappings.
|
|
91 *
|
|
92 * Params:
|
|
93 * aa = the associative array to check if it's empty
|
|
94 *
|
|
95 * Returns: <tt>true</tt> if the specified
|
|
96 * associative array contains no key-value mappings
|
|
97 */
|
|
98 bool isEmpty (K, V) (V[K] aa)
|
|
99 {
|
|
100 return aa.length == 0;
|
|
101 }
|
|
102
|
|
103
|
|
104 /**
|
|
105 * Returns a array of the values contained in the
|
|
106 * specifed associative array. The array is backed by
|
|
107 * the associative array(if it contains classes or pointers),
|
|
108 * so changes to the associative array are reflected in
|
|
109 * the array, and vice-versa. If the associative array is
|
|
110 * modified while an iteration over the collection is in progress
|
|
111 * (except through the iterator's own <tt>remove</tt> operation),
|
|
112 * the results of the iteration are undefined. The collection
|
|
113 * supports element removal, which removes the corresponding
|
|
114 * mapping from the map, via the <tt>Iterator.remove</tt>,
|
|
115 * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
|
|
116 * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
|
|
117 * support the <tt>add</tt> or <tt>addAll</tt> operations.
|
|
118 *
|
|
119 * Params:
|
|
120 * aa = the associative array to get the values from
|
|
121 *
|
|
122 * Returns: a collection view of the values contained in this map
|
|
123 */
|
|
124 V[] values (K, V) (V[K] aa)
|
|
125 {
|
|
126 return aa.values;
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * Removes all mappings from this map
|
|
131 */
|
|
132 void clear (K, V) (V[K] aa)
|
|
133 {
|
|
134 foreach (k, v ; aa)
|
|
135 aa.remove(k);
|
|
136 }
|
|
137
|
|
138 /**
|
|
139 * Returns the number of key-value mappings in
|
|
140 * the specifed associative array
|
|
141 *
|
|
142 * Params:
|
|
143 * aa = the associative array to get the number of key-value mappings from
|
|
144 *
|
|
145 * Returns: the number of key-value mappings in the associative array
|
|
146 */
|
|
147 /*int size (K, V) (V[K] aa)
|
|
148 {
|
|
149 aa.length;
|
|
150 }*/ |