Debian Codegraph @

debian/additionalSrc/trove4j/util/src/gnu/trove/generate/TKVHashMap.template

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 ///////////////////////////////////////////////////////////////////////////////
18 // THIS FILE IS AUTOGENERATED, PLEASE DO NOT EDIT OR ELSE
19 package gnu.trove;
20
21 import java.io.IOException;
22 import java.io.ObjectInputStream;
23 import java.io.ObjectOutputStream;
24
25 /**
26 * An open addressed Map implementation for #k# keys and #v# values.
27 *
28 * Created: Sun Nov 4 08:52:45 2001
29 *
30 * @author Eric D. Friedman
31 */
32 public class T#K##V#HashMap extends T#K#Hash {
33
34 /** the values of the map */
35 protected transient #v#[] _values;
36
37 /**
38 * Creates a new <code>T#K##V#HashMap</code> instance with the default
39 * capacity and load factor.
40 */
41 public T#K##V#HashMap() {
42 }
43
44 /**
45 * Creates a new <code>T#K##V#HashMap</code> instance with a prime
46 * capacity equal to or greater than <tt>initialCapacity</tt> and
47 * with the default load factor.
48 *
49 * @param initialCapacity an <code>int</code> value
50 */
51 public T#K##V#HashMap(int initialCapacity) {
52 super(initialCapacity);
53 }
54
55 /**
56 * Creates a new <code>T#K##V#HashMap</code> instance with a prime
57 * capacity equal to or greater than <tt>initialCapacity</tt> and
58 * with the specified load factor.
59 *
60 * @param initialCapacity an <code>int</code> value
61 * @param loadFactor a <code>float</code> value
62 */
63 public T#K##V#HashMap(int initialCapacity, float loadFactor) {
64 super(initialCapacity, loadFactor);
65 }
66
67 /**
68 * Creates a new <code>T#K##V#HashMap</code> instance with the default
69 * capacity and load factor.
70 * @param strategy used to compute hash codes and to compare keys.
71 */
72 public T#K##V#HashMap(T#K#HashingStrategy strategy) {
73 super(strategy);
74 }
75
76 /**
77 * Creates a new <code>T#K##V#HashMap</code> instance whose capacity
78 * is the next highest prime above <tt>initialCapacity + 1</tt>
79 * unless that value is already prime.
80 *
81 * @param initialCapacity an <code>int</code> value
82 * @param strategy used to compute hash codes and to compare keys.
83 */
84 public T#K##V#HashMap(int initialCapacity, T#K#HashingStrategy strategy) {
85 super(initialCapacity, strategy);
86 }
87
88 /**
89 * Creates a new <code>T#K##V#HashMap</code> instance with a prime
90 * value at or near the specified capacity and load factor.
91 *
92 * @param initialCapacity used to find a prime capacity for the table.
93 * @param loadFactor used to calculate the threshold over which
94 * rehashing takes place.
95 * @param strategy used to compute hash codes and to compare keys.
96 */
97 public T#K##V#HashMap(int initialCapacity, float loadFactor, T#K#HashingStrategy strategy) {
98 super(initialCapacity, loadFactor, strategy);
99 }
100
101 /**
102 * @return a deep clone of this collection
103 */
104 @Override
105 public Object clone() {
106 T#K##V#HashMap m = (T#K##V#HashMap)super.clone();
107 m._values = _values == null ? null : _values.clone();
108 return m;
109 }
110
111 /**
112 * @return a T#K##V#Iterator with access to this map's keys and values
113 */
114 public T#K##V#Iterator iterator() {
115 return new T#K##V#Iterator(this);
116 }
117
118 /**
119 * initializes the hashtable to a prime capacity which is at least
120 * <tt>initialCapacity + 1</tt>.
121 *
122 * @param initialCapacity an <code>int</code> value
123 * @return the actual capacity chosen
124 */
125 @Override
126 protected int setUp(int initialCapacity) {
127 int capacity = super.setUp(initialCapacity);
128 _values = initialCapacity == JUST_CREATED_CAPACITY ? null : new #v#[capacity];
129 return capacity;
130 }
131
132 /**
133 * Inserts a key/value pair into the map.
134 *
135 * @param key an <code>#k#</code> value
136 * @param value an <code>#v#</code> value
137 * @return the previous value associated with <tt>key</tt>,
138 * or null if none was found.
139 */
140 public #v# put(#k# key, #v# value) {
141 #v# previous = 0;
142 int index = insertionIndex(key);
143 boolean isNewMapping = true;
144 if (index < 0) {
145 index = -index -1;
146 previous = _values[index];
147 isNewMapping = false;
148 }
149 byte previousState = _states[index];
150 _set[index] = key;
151 _states[index] = FULL;
152 _values[index] = value;
153 if (isNewMapping) {
154 postInsertHook(previousState == FREE);
155 }
156
157 return previous;
158 }
159
160 /**
161 * rehashes the map to the new capacity.
162 *
163 * @param newCapacity an <code>int</code> value
164 */
165 @Override
166 protected void rehash(int newCapacity) {
167 int oldCapacity = capacity();
168 #k#[] oldKeys = _set;
169 #v#[] oldVals = _values;
170 byte[] oldStates = _states;
171
172 _set = new #k#[newCapacity];
173 _values = new #v#[newCapacity];
174 _states = new byte[newCapacity];
175
176 for (int i = oldCapacity; i-- > 0;) {
177 if(oldStates[i] == FULL) {
178 #k# o = oldKeys[i];
179 int index = insertionIndex(o);
180 _set[index] = o;
181 _values[index] = oldVals[i];
182 _states[index] = FULL;
183 }
184 }
185 }
186
187 /**
188 * retrieves the value for <tt>key</tt>
189 *
190 * @param key an <code>#k#</code> value
191 * @return the value of <tt>key</tt> or null if no such mapping exists.
192 */
193 public #v# get(#k# key) {
194 int index = index(key);
195 return index < 0 ? 0 : _values[index];
196 }
197
198 /**
199 * Empties the map.
200 *
201 */
202 @Override
203 public void clear() {
204 super.clear();
205 #k#[] keys = _set;
206 #v#[] vals = _values;
207 if (vals == null) return;
208 byte[] states = _states;
209
210 for (int i = keys.length; i-- > 0;) {
211 keys[i] = 0;
212 vals[i] = 0;
213 states[i] = FREE;
214 }
215 }
216
217 /**
218 * Deletes a key/value pair from the map.
219 *
220 * @param key an <code>#k#</code> value
221 * @return an <code>#v#</code> value
222 */
223 public #v# remove(#k# key) {
224 #v# prev = 0;
225 int index = index(key);
226 if (index >= 0) {
227 prev = _values[index];
228 removeAt(index); // clear key,state; adjust size
229 }
230 return prev;
231 }
232
233 /**
234 * Compares this map with another map for equality of their stored
235 * entries.
236 *
237 * @param other an <code>Object</code> value
238 * @return a <code>boolean</code> value
239 */
240 @Override
241 public boolean equals(Object other) {
242 if (! (other instanceof T#K##V#HashMap)) {
243 return false;
244 }
245 T#K##V#HashMap that = (T#K##V#HashMap)other;
246 if (that.size() != size()) {
247 return false;
248 }
249 return forEachEntry(new EqProcedure(that));
250 }
251
252 @Override
253 public int hashCode() {
254 HashProcedure p = new HashProcedure();
255 forEachEntry(p);
256 return p.getHashCode();
257 }
258
259 private final class HashProcedure implements T#K##V#Procedure {
260 private int h;
261
262 HashProcedure() {
263 }
264
265 public int getHashCode() {
266 return h;
267 }
268
269 public final boolean execute(#k# key, #v# value) {
270 h += _hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value);
271 return true;
272 }
273 }
274
275 private static final class EqProcedure implements T#K##V#Procedure {
276 private final T#K##V#HashMap _otherMap;
277
278 EqProcedure(T#K##V#HashMap otherMap) {
279 _otherMap = otherMap;
280 }
281
282 public final boolean execute(#k# key, #v# value) {
283 int index = _otherMap.index(key);
284 return index >= 0 && eq(value, _otherMap.get(key));
285 }
286
287 /**
288 * Compare two #v#s for equality.
289 */
290 private static boolean eq(#v# v1, #v# v2) {
291 return v1 == v2;
292 }
293
294 }
295
296 /**
297 * removes the mapping at <tt>index</tt> from the map.
298 *
299 * @param index an <code>int</code> value
300 */
301 @Override
302 protected void removeAt(int index) {
303 _values[index] = 0;
304 super.removeAt(index); // clear key, state; adjust size
305 }
306
307 /**
308 * Returns the values of the map.
309 *
310 * @return a <code>Collection</code> value
311 */
312 public #v#[] getValues() {
313 #v#[] vals = new #v#[size()];
314 #v#[] v = _values;
315 byte[] states = _states;
316
317 if (states != null) {
318 for (int i = states.length, j = 0; i-- > 0;) {
319 if (states[i] == FULL) {
320 vals[j++] = v[i];
321 }
322 }
323 }
324 return vals;
325 }
326
327 /**
328 * returns the keys of the map.
329 *
330 * @return a <code>Set</code> value
331 */
332 public #k#[] keys() {
333 #k#[] keys = new #k#[size()];
334 #k#[] k = _set;
335 byte[] states = _states;
336
337 if (states != null) {
338 for (int i = states.length, j = 0; i-- > 0;) {
339 if (states[i] == FULL) {
340 keys[j++] = k[i];
341 }
342 }
343 }
344 return keys;
345 }
346
347 /**
348 * checks for the presence of <tt>val</tt> in the values of the map.
349 *
350 * @param val an <code>#v#</code> value
351 * @return a <code>boolean</code> value
352 */
353 public boolean containsValue(#v# val) {
354 byte[] states = _states;
355 #v#[] vals = _values;
356 if (states != null) {
357 for (int i = states.length; i-- > 0;) {
358 if (states[i] == FULL && val == vals[i]) {
359 return true;
360 }
361 }
362 }
363 return false;
364 }
365
366
367 /**
368 * checks for the present of <tt>key</tt> in the keys of the map.
369 *
370 * @param key an <code>#k#</code> value
371 * @return a <code>boolean</code> value
372 */
373 public boolean containsKey(#k# key) {
374 return contains(key);
375 }
376
377 /**
378 * Executes <tt>procedure</tt> for each key in the map.
379 *
380 * @param procedure a <code>T#K#Procedure</code> value
381 * @return false if the loop over the keys terminated because
382 * the procedure returned false for some key.
383 */
384 public boolean forEachKey(T#K#Procedure procedure) {
385 return forEach(procedure);
386 }
387
388 /**
389 * Executes <tt>procedure</tt> for each value in the map.
390 *
391 * @param procedure a <code>T#V#Procedure</code> value
392 * @return false if the loop over the values terminated because
393 * the procedure returned false for some value.
394 */
395 public boolean forEachValue(T#V#Procedure procedure) {
396 byte[] states = _states;
397 #v#[] values = _values;
398 if (states != null) {
399 for (int i = states.length; i-- > 0;) {
400 if (states[i] == FULL && ! procedure.execute(values[i])) {
401 return false;
402 }
403 }
404 }
405 return true;
406 }
407
408 /**
409 * Executes <tt>procedure</tt> for each key/value entry in the
410 * map.
411 *
412 * @param procedure a <code>T#K##V#Procedure</code> value
413 * @return false if the loop over the entries terminated because
414 * the procedure returned false for some entry.
415 */
416 public boolean forEachEntry(T#K##V#Procedure procedure) {
417 byte[] states = _states;
418 #k#[] keys = _set;
419 #v#[] values = _values;
420 if (states != null) {
421 for (int i = states.length; i-- > 0;) {
422 if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) {
423 return false;
424 }
425 }
426 }
427 return true;
428 }
429
430 /**
431 * Retains only those entries in the map for which the procedure
432 * returns a true value.
433 *
434 * @param procedure determines which entries to keep
435 * @return true if the map was modified.
436 */
437 public boolean retainEntries(T#K##V#Procedure procedure) {
438 boolean modified = false;
439 byte[] states = _states;
440 #k#[] keys = _set;
441 #v#[] values = _values;
442 if (states != null) {
443 for (int i = states.length; i-- > 0;) {
444 if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) {
445 removeAt(i);
446 modified = true;
447 }
448 }
449 }
450 return modified;
451 }
452
453 /**
454 * Transform the values in this map using <tt>function</tt>.
455 *
456 * @param function a <code>T#V#Function</code> value
457 */
458 public void transformValues(T#V#Function function) {
459 byte[] states = _states;
460 #v#[] values = _values;
461 if (states != null) {
462 for (int i = states.length; i-- > 0;) {
463 if (states[i] == FULL) {
464 values[i] = function.execute(values[i]);
465 }
466 }
467 }
468 }
469
470 /**
471 * Increments the primitive value mapped to key by 1
472 *
473 * @param key the key of the value to increment
474 * @return true if a mapping was found and modified.
475 */
476 public boolean increment(#k# key) {
477 return adjustValue(key, (#v#) 1);
478 }
479
480 /**
481 * Adjusts the primitive value mapped to key.
482 *
483 * @param key the key of the value to increment
484 * @param amount the amount to adjust the value by.
485 * @return true if a mapping was found and modified.
486 */
487 public boolean adjustValue(#k# key, #v# amount) {
488 int index = index(key);
489 if (index < 0) {
490 return false;
491 } else {
492 _values[index] += amount;
493 return true;
494 }
495 }
496
497
498 private void writeObject(ObjectOutputStream stream)
499 throws IOException {
500 stream.defaultWriteObject();
501
502 // number of entries
503 stream.writeInt(_size);
504
505 SerializationProcedure writeProcedure = new SerializationProcedure(stream);
506 if (! forEachEntry(writeProcedure)) {
507 throw writeProcedure.exception;
508 }
509 }
510
511 private void readObject(ObjectInputStream stream)
512 throws IOException, ClassNotFoundException {
513 stream.defaultReadObject();
514
515 int size = stream.readInt();
516 setUp(size);
517 while (size-- > 0) {
518 #k# key = stream.read#K#();
519 #v# val = stream.read#V#();
520 put(key, val);
521 }
522 }
523
524 @Override
525 public String toString() {
526 final StringBuilder sb = new StringBuilder();
527 forEachEntry(new T#K##V#Procedure() {
528 public boolean execute(#k# key, #v# value) {
529 if (sb.length() != 0) {
530 sb.append(',').append(' ');
531 }
532 sb.append(key);
533 sb.append('=');
534 sb.append(value);
535 return true;
536 }
537 });
538 sb.append('}');
539 sb.insert(0, '{');
540 return sb.toString();
541 }
542
543 } // T#K##V#HashMap