| 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 #e# keys and Object values. |
| 27 |
* |
| 28 |
* Created: Sun Nov 4 08:52:45 2001 |
| 29 |
* |
| 30 |
* @author Eric D. Friedman |
| 31 |
*/ |
| 32 |
public class T#E#ObjectHashMap<V> extends THash implements T#E#HashingStrategy { |
| 33 |
|
| 34 |
/** the values of the map */ |
| 35 |
protected transient V[] _values; |
| 36 |
|
| 37 |
/** the set of #e#s */ |
| 38 |
protected transient #e#[] _set; |
| 39 |
|
| 40 |
/** strategy used to hash values in this collection */ |
| 41 |
protected final T#E#HashingStrategy _hashingStrategy; |
| 42 |
|
| 43 |
/** |
| 44 |
* Creates a new <code>T#E#ObjectHashMap</code> instance with the default |
| 45 |
* capacity and load factor. |
| 46 |
*/ |
| 47 |
public T#E#ObjectHashMap() { |
| 48 |
_hashingStrategy = this; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Creates a new <code>T#E#ObjectHashMap</code> instance with a prime |
| 53 |
* capacity equal to or greater than <tt>initialCapacity</tt> and |
| 54 |
* with the default load factor. |
| 55 |
* |
| 56 |
* @param initialCapacity an <code>int</code> value |
| 57 |
*/ |
| 58 |
public T#E#ObjectHashMap(int initialCapacity) { |
| 59 |
super(initialCapacity); |
| 60 |
_hashingStrategy = this; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Creates a new <code>T#E#ObjectHashMap</code> instance with a prime |
| 65 |
* capacity equal to or greater than <tt>initialCapacity</tt> and |
| 66 |
* with the specified load factor. |
| 67 |
* |
| 68 |
* @param initialCapacity an <code>int</code> value |
| 69 |
* @param loadFactor a <code>float</code> value |
| 70 |
*/ |
| 71 |
public T#E#ObjectHashMap(int initialCapacity, float loadFactor) { |
| 72 |
super(initialCapacity, loadFactor); |
| 73 |
_hashingStrategy = this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Creates a new <code>T#E#ObjectHashMap</code> instance with the default |
| 78 |
* capacity and load factor. |
| 79 |
* @param strategy used to compute hash codes and to compare keys. |
| 80 |
*/ |
| 81 |
public T#E#ObjectHashMap(T#E#HashingStrategy strategy) { |
| 82 |
_hashingStrategy = strategy; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Creates a new <code>T#E#ObjectHashMap</code> instance whose capacity |
| 87 |
* is the next highest prime above <tt>initialCapacity + 1</tt> |
| 88 |
* unless that value is already prime. |
| 89 |
* |
| 90 |
* @param initialCapacity an <code>int</code> value |
| 91 |
* @param strategy used to compute hash codes and to compare keys. |
| 92 |
*/ |
| 93 |
public T#E#ObjectHashMap(int initialCapacity, T#E#HashingStrategy strategy) { |
| 94 |
super(initialCapacity); |
| 95 |
_hashingStrategy = strategy; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Creates a new <code>T#E#ObjectHashMap</code> instance with a prime |
| 100 |
* value at or near the specified capacity and load factor. |
| 101 |
* |
| 102 |
* @param initialCapacity used to find a prime capacity for the table. |
| 103 |
* @param loadFactor used to calculate the threshold over which |
| 104 |
* rehashing takes place. |
| 105 |
* @param strategy used to compute hash codes and to compare keys. |
| 106 |
*/ |
| 107 |
public T#E#ObjectHashMap(int initialCapacity, float loadFactor, T#E#HashingStrategy strategy) { |
| 108 |
super(initialCapacity, loadFactor); |
| 109 |
_hashingStrategy = strategy; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* @return a deep clone of this collection |
| 114 |
*/ |
| 115 |
@Override |
| 116 |
public T#E#ObjectHashMap<V> clone() { |
| 117 |
T#E#ObjectHashMap<V> m = (T#E#ObjectHashMap<V>)super.clone(); |
| 118 |
m._values = _values == EMPTY_OBJECT_ARRAY ? (V[])EMPTY_OBJECT_ARRAY : _values.clone(); |
| 119 |
m._set = _values == EMPTY_OBJECT_ARRAY ? null : _set.clone(); |
| 120 |
return m; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @return a T#E#ObjectIterator with access to this map's keys and values |
| 125 |
*/ |
| 126 |
public T#E#ObjectIterator<V> iterator() { |
| 127 |
return new T#E#ObjectIterator<V>(this); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* initializes the hashtable to a prime capacity which is at least |
| 132 |
* <tt>initialCapacity + 1</tt>. |
| 133 |
* |
| 134 |
* @param initialCapacity an <code>int</code> value |
| 135 |
* @return the actual capacity chosen |
| 136 |
*/ |
| 137 |
@Override |
| 138 |
protected int setUp(int initialCapacity) { |
| 139 |
int capacity = super.setUp(initialCapacity); |
| 140 |
_values = (V[]) (initialCapacity == JUST_CREATED_CAPACITY ? EMPTY_OBJECT_ARRAY : new Object[capacity]); |
| 141 |
_set = initialCapacity == JUST_CREATED_CAPACITY ? null : new #e#[capacity]; |
| 142 |
|
| 143 |
return capacity; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Searches the set for <tt>val</tt> |
| 148 |
* |
| 149 |
* @param val an <code>#e#</code> value |
| 150 |
* @return a <code>boolean</code> value |
| 151 |
*/ |
| 152 |
public boolean contains(#e# val) { |
| 153 |
return index(val) >= 0; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Returns the capacity of the hash table. This is the true |
| 159 |
* physical capacity, without adjusting for the load factor. |
| 160 |
* |
| 161 |
* @return the physical capacity of the hash table. |
| 162 |
*/ |
| 163 |
@Override |
| 164 |
protected int capacity() { |
| 165 |
return _values.length; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Executes <tt>procedure</tt> for each element in the set. |
| 170 |
* |
| 171 |
* @param procedure a <code>T#E#Procedure</code> value |
| 172 |
* @return false if the loop over the set terminated because |
| 173 |
* the procedure returned false for some value. |
| 174 |
*/ |
| 175 |
public boolean forEach(T#E#Procedure procedure) { |
| 176 |
#e#[] set = _set; |
| 177 |
Object[] values = _values; |
| 178 |
for (int i = values.length; i-- > 0;) { |
| 179 |
if (isFull(values, i) && ! procedure.execute(set[i])) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
} |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Inserts a key/value pair into the map. |
| 188 |
* |
| 189 |
* @param key an <code>#e#</code> value |
| 190 |
* @param value an <code>Object</code> value |
| 191 |
* @return the previous value associated with <tt>key</tt>, |
| 192 |
* or null if none was found. |
| 193 |
*/ |
| 194 |
public V put(#e# key, V value) { |
| 195 |
boolean wasFree = false; |
| 196 |
V previous = null; |
| 197 |
int index = insertionIndex(key); |
| 198 |
boolean isNewMapping = true; |
| 199 |
if (index < 0) { |
| 200 |
index = -index -1; |
| 201 |
previous = unwrapNull(_values[index]); |
| 202 |
isNewMapping = false; |
| 203 |
} |
| 204 |
else { |
| 205 |
wasFree = isFree(_values, index); |
| 206 |
} |
| 207 |
_set[index] = key; |
| 208 |
_values[index] = wrapNull(value); |
| 209 |
if (isNewMapping) { |
| 210 |
postInsertHook(wasFree); |
| 211 |
} |
| 212 |
|
| 213 |
return previous; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* rehashes the map to the new capacity. |
| 218 |
* |
| 219 |
* @param newCapacity an <code>int</code> value |
| 220 |
*/ |
| 221 |
@Override |
| 222 |
protected void rehash(int newCapacity) { |
| 223 |
int oldCapacity = capacity(); |
| 224 |
#e#[] oldKeys = _set; |
| 225 |
V[] oldVals = _values; |
| 226 |
|
| 227 |
_set = new #e#[newCapacity]; |
| 228 |
_values = (V[]) new Object[newCapacity]; |
| 229 |
|
| 230 |
for (int i = oldCapacity; i-- > 0;) { |
| 231 |
if(isFull(oldVals, i)) { |
| 232 |
#e# o = oldKeys[i]; |
| 233 |
int index = insertionIndex(o); |
| 234 |
_set[index] = o; |
| 235 |
_values[index] = oldVals[i]; |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* retrieves the value for <tt>key</tt> |
| 242 |
* |
| 243 |
* @param key an <code>#e#</code> value |
| 244 |
* @return the value of <tt>key</tt> or null if no such mapping exists. |
| 245 |
*/ |
| 246 |
public V get(#e# key) { |
| 247 |
int index = index(key); |
| 248 |
return index < 0 ? null : unwrapNull(_values[index]); |
| 249 |
} |
| 250 |
|
| 251 |
private static <V> V unwrapNull(V value) { |
| 252 |
return value == TObjectHash.NULL ? null : value; |
| 253 |
} |
| 254 |
private static <V> V wrapNull(V value) { |
| 255 |
return value == null ? (V)TObjectHash.NULL : value; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Empties the map. |
| 260 |
* |
| 261 |
*/ |
| 262 |
@Override |
| 263 |
public void clear() { |
| 264 |
super.clear(); |
| 265 |
#e#[] keys = _set; |
| 266 |
Object[] values = _values; |
| 267 |
|
| 268 |
for (int i = values.length; i-- > 0;) { |
| 269 |
keys[i] = (#e#)0; |
| 270 |
values[i] = null; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Deletes a key/value pair from the map. |
| 276 |
* |
| 277 |
* @param key an <code>#e#</code> value |
| 278 |
* @return an <code>Object</code> value |
| 279 |
*/ |
| 280 |
public V remove(#e# key) { |
| 281 |
V prev = null; |
| 282 |
int index = index(key); |
| 283 |
if (index >= 0) { |
| 284 |
prev = unwrapNull(_values[index]); |
| 285 |
removeAt(index); // clear key,set; adjust size |
| 286 |
} |
| 287 |
return prev; |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Locates the index of <tt>val</tt>. |
| 292 |
* |
| 293 |
* @param val an <code>#e#</code> value |
| 294 |
* @return the index of <tt>val</tt> or -1 if it isn't in the set. |
| 295 |
*/ |
| 296 |
protected int index(#e# val) { |
| 297 |
#e#[] set = _set; |
| 298 |
Object[] values = _values; |
| 299 |
if (values == EMPTY_OBJECT_ARRAY) return -1; |
| 300 |
int length = set.length; |
| 301 |
int hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff; |
| 302 |
int index = hash % length; |
| 303 |
|
| 304 |
if (!isFree(values, index) && |
| 305 |
(isRemoved(values, index) || set[index] != val)) { |
| 306 |
// see Knuth, p. 529 |
| 307 |
int probe = 1 + (hash % (length - 2)); |
| 308 |
|
| 309 |
do { |
| 310 |
index -= probe; |
| 311 |
if (index < 0) { |
| 312 |
index += length; |
| 313 |
} |
| 314 |
} while (!isFree(values, index) && |
| 315 |
(isRemoved(values, index) || set[index] != val)); |
| 316 |
} |
| 317 |
|
| 318 |
return isFree(values, index) ? -1 : index; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Locates the index at which <tt>val</tt> can be inserted. if |
| 323 |
* there is already a value equal()ing <tt>val</tt> in the set, |
| 324 |
* returns that value as a negative integer. |
| 325 |
* |
| 326 |
* @param val an <code>#e#</code> value |
| 327 |
* @return an <code>int</code> value |
| 328 |
*/ |
| 329 |
protected int insertionIndex(#e# val) { |
| 330 |
if (_values == EMPTY_OBJECT_ARRAY) { |
| 331 |
setUp((int) (DEFAULT_INITIAL_CAPACITY / DEFAULT_LOAD_FACTOR + 1)); |
| 332 |
} |
| 333 |
Object[] values = _values; |
| 334 |
#e#[] set = _set; |
| 335 |
int length = set.length; |
| 336 |
int hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff; |
| 337 |
int index = hash % length; |
| 338 |
|
| 339 |
if (isFree(values, index)) { |
| 340 |
return index; // empty, all done |
| 341 |
} |
| 342 |
if (isFull(values, index) && set[index] == val) { |
| 343 |
return -index -1; // already stored |
| 344 |
} |
| 345 |
|
| 346 |
// already FULL or REMOVED, must probe |
| 347 |
// compute the double hash |
| 348 |
int probe = 1 + (hash % (length - 2)); |
| 349 |
// starting at the natural offset, probe until we find an |
| 350 |
// offset that isn't full. |
| 351 |
|
| 352 |
// keep track of the first removed cell. it's the natural candidate for re-insertion |
| 353 |
int firstRemoved = isRemoved(values, index) ? index : -1; |
| 354 |
|
| 355 |
do { |
| 356 |
index -= probe; |
| 357 |
if (index < 0) { |
| 358 |
index += length; |
| 359 |
} |
| 360 |
if (firstRemoved == -1 && isRemoved(values, index)) { |
| 361 |
firstRemoved = index; |
| 362 |
} |
| 363 |
} |
| 364 |
while (isFull(values, index) && set[index] != val); |
| 365 |
|
| 366 |
// if the index we found was removed: continue probing until we |
| 367 |
// locate a free location or an element which equal()s the |
| 368 |
// one we have. |
| 369 |
if (isRemoved(values, index)) { |
| 370 |
while (!isFree(values, index) && |
| 371 |
(isRemoved(values, index) || set[index] != val)) { |
| 372 |
index -= probe; |
| 373 |
if (index < 0) { |
| 374 |
index += length; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
// if it's full, the key is already stored |
| 379 |
if (isFull(values, index)) { |
| 380 |
return -index -1; |
| 381 |
} |
| 382 |
|
| 383 |
return firstRemoved == -1 ? index : firstRemoved; |
| 384 |
} |
| 385 |
|
| 386 |
static boolean isFull(Object[] values, int index) { |
| 387 |
Object value = values[index]; |
| 388 |
return value != null && value != TObjectHash.REMOVED; |
| 389 |
} |
| 390 |
|
| 391 |
private static boolean isRemoved(Object[] values, int index) { |
| 392 |
return values[index] == TObjectHash.REMOVED; |
| 393 |
} |
| 394 |
|
| 395 |
private static boolean isFree(Object[] values, int index) { |
| 396 |
return values[index] == null; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Compares this map with another map for equality of their stored |
| 401 |
* entries. |
| 402 |
* |
| 403 |
* @param other an <code>Object</code> value |
| 404 |
* @return a <code>boolean</code> value |
| 405 |
*/ |
| 406 |
@Override |
| 407 |
public boolean equals(Object other) { |
| 408 |
if (! (other instanceof T#E#ObjectHashMap)) { |
| 409 |
return false; |
| 410 |
} |
| 411 |
T#E#ObjectHashMap that = (T#E#ObjectHashMap)other; |
| 412 |
if (that.size() != size()) { |
| 413 |
return false; |
| 414 |
} |
| 415 |
return forEachEntry(new EqProcedure<V>(that)); |
| 416 |
} |
| 417 |
|
| 418 |
@Override |
| 419 |
public int hashCode() { |
| 420 |
HashProcedure p = new HashProcedure(); |
| 421 |
forEachEntry(p); |
| 422 |
return p.getHashCode(); |
| 423 |
} |
| 424 |
|
| 425 |
private final class HashProcedure implements T#E#ObjectProcedure<V> { |
| 426 |
private int h; |
| 427 |
|
| 428 |
HashProcedure() { |
| 429 |
} |
| 430 |
|
| 431 |
public int getHashCode() { |
| 432 |
return h; |
| 433 |
} |
| 434 |
|
| 435 |
public final boolean execute(#e# key, V value) { |
| 436 |
h += _hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value); |
| 437 |
return true; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
private static final class EqProcedure<V> implements T#E#ObjectProcedure<V> { |
| 442 |
private final T#E#ObjectHashMap<V> _otherMap; |
| 443 |
|
| 444 |
EqProcedure(T#E#ObjectHashMap<V> otherMap) { |
| 445 |
_otherMap = otherMap; |
| 446 |
} |
| 447 |
|
| 448 |
public final boolean execute(#e# key, V value) { |
| 449 |
int index = _otherMap.index(key); |
| 450 |
return index >= 0 && eq(value, _otherMap.get(key)); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Compare two objects for equality. |
| 455 |
*/ |
| 456 |
private static boolean eq(Object o1, Object o2) { |
| 457 |
return o1 == o2 || o1 != null && o1.equals(o2); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* removes the mapping at <tt>index</tt> from the map. |
| 463 |
* |
| 464 |
* @param index an <code>int</code> value |
| 465 |
*/ |
| 466 |
@Override |
| 467 |
protected void removeAt(int index) { |
| 468 |
_values[index] = (V)TObjectHash.REMOVED; |
| 469 |
super.removeAt(index); // clear key, set; adjust size |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Returns the values of the map. |
| 474 |
* |
| 475 |
* @return a value <code>array</code> |
| 476 |
*/ |
| 477 |
public Object[] getValues() { |
| 478 |
Object[] vals = new Object[size()]; |
| 479 |
V[] values = _values; |
| 480 |
|
| 481 |
for (int i = values.length, j = 0; i-- > 0;) { |
| 482 |
if (isFull(values, i)) { |
| 483 |
vals[j++] = unwrapNull(values[i]); |
| 484 |
} |
| 485 |
} |
| 486 |
return vals; |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* returns the keys of the map. |
| 491 |
* |
| 492 |
* @return a <code>Set</code> value |
| 493 |
*/ |
| 494 |
public #e#[] keys() { |
| 495 |
#e#[] keys = new #e#[size()]; |
| 496 |
#e#[] k = _set; |
| 497 |
Object[] values = _values; |
| 498 |
|
| 499 |
for (int i = values.length, j = 0; i-- > 0;) { |
| 500 |
if (isFull(values, i)) { |
| 501 |
keys[j++] = k[i]; |
| 502 |
} |
| 503 |
} |
| 504 |
return keys; |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* checks for the presence of <tt>val</tt> in the values of the map. |
| 509 |
* |
| 510 |
* @param val an <code>Object</code> value |
| 511 |
* @return a <code>boolean</code> value |
| 512 |
*/ |
| 513 |
public boolean containsValue(V val) { |
| 514 |
V[] values = _values; |
| 515 |
|
| 516 |
// special case null values so that we don't have to |
| 517 |
// perform null checks before every call to equals() |
| 518 |
if (null == val) { |
| 519 |
for (int i = values.length; i-- > 0;) { |
| 520 |
if (TObjectHash.NULL == values[i]) { |
| 521 |
return true; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
else { |
| 526 |
for (int i = values.length; i-- > 0;) { |
| 527 |
V value = unwrapNull(values[i]); |
| 528 |
if (isFull(values, i) && (val == value || val.equals(value))) { |
| 529 |
return true; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
return false; |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* checks for the present of <tt>key</tt> in the keys of the map. |
| 538 |
* |
| 539 |
* @param key an <code>#e#</code> value |
| 540 |
* @return a <code>boolean</code> value |
| 541 |
*/ |
| 542 |
public boolean containsKey(#e# key) { |
| 543 |
return contains(key); |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Executes <tt>procedure</tt> for each key in the map. |
| 548 |
* |
| 549 |
* @param procedure a <code>T#E#Procedure</code> value |
| 550 |
* @return false if the loop over the keys terminated because |
| 551 |
* the procedure returned false for some key. |
| 552 |
*/ |
| 553 |
public boolean forEachKey(T#E#Procedure procedure) { |
| 554 |
return forEach(procedure); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Executes <tt>procedure</tt> for each value in the map. |
| 559 |
* |
| 560 |
* @param procedure a <code>TObjectProcedure</code> value |
| 561 |
* @return false if the loop over the values terminated because |
| 562 |
* the procedure returned false for some value. |
| 563 |
*/ |
| 564 |
public boolean forEachValue(TObjectProcedure<V> procedure) { |
| 565 |
V[] values = _values; |
| 566 |
for (int i = values.length; i-- > 0;) { |
| 567 |
if (isFull(values, i) && ! procedure.execute(unwrapNull(values[i]))) { |
| 568 |
return false; |
| 569 |
} |
| 570 |
} |
| 571 |
return true; |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* Executes <tt>procedure</tt> for each key/value entry in the |
| 576 |
* map. |
| 577 |
* |
| 578 |
* @param procedure a <code>TO#E#ObjectProcedure</code> value |
| 579 |
* @return false if the loop over the entries terminated because |
| 580 |
* the procedure returned false for some entry. |
| 581 |
*/ |
| 582 |
public boolean forEachEntry(T#E#ObjectProcedure<V> procedure) { |
| 583 |
#e#[] keys = _set; |
| 584 |
V[] values = _values; |
| 585 |
for (int i = values.length; i-- > 0;) { |
| 586 |
if (isFull(values, i) && ! procedure.execute(keys[i],unwrapNull(values[i]))) { |
| 587 |
return false; |
| 588 |
} |
| 589 |
} |
| 590 |
return true; |
| 591 |
} |
| 592 |
|
| 593 |
/** |
| 594 |
* Retains only those entries in the map for which the procedure |
| 595 |
* returns a true value. |
| 596 |
* |
| 597 |
* @param procedure determines which entries to keep |
| 598 |
* @return true if the map was modified. |
| 599 |
*/ |
| 600 |
public boolean retainEntries(T#E#ObjectProcedure<V> procedure) { |
| 601 |
boolean modified = false; |
| 602 |
#e#[] keys = _set; |
| 603 |
V[] values = _values; |
| 604 |
stopCompactingOnRemove(); |
| 605 |
try { |
| 606 |
for (int i = values.length; i-- > 0;) { |
| 607 |
if (isFull(values, i) && ! procedure.execute(keys[i],unwrapNull(values[i]))) { |
| 608 |
removeAt(i); |
| 609 |
modified = true; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
finally { |
| 614 |
startCompactingOnRemove(modified); |
| 615 |
} |
| 616 |
return modified; |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Transform the values in this map using <tt>function</tt>. |
| 621 |
* |
| 622 |
* @param function a <code>TObjectFunction</code> value |
| 623 |
*/ |
| 624 |
public void transformValues(TObjectFunction<V,V> function) { |
| 625 |
V[] values = _values; |
| 626 |
for (int i = values.length; i-- > 0;) { |
| 627 |
if (isFull(values, i)) { |
| 628 |
values[i] = wrapNull(function.execute(unwrapNull(values[i]))); |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
private void writeObject(ObjectOutputStream stream) |
| 636 |
throws IOException { |
| 637 |
stream.defaultWriteObject(); |
| 638 |
|
| 639 |
// number of entries |
| 640 |
stream.writeInt(_size); |
| 641 |
|
| 642 |
SerializationProcedure writeProcedure = new SerializationProcedure(stream); |
| 643 |
if (! forEachEntry(writeProcedure)) { |
| 644 |
throw writeProcedure.exception; |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
private void readObject(ObjectInputStream stream) |
| 649 |
throws IOException, ClassNotFoundException { |
| 650 |
stream.defaultReadObject(); |
| 651 |
|
| 652 |
int size = stream.readInt(); |
| 653 |
setUp(size); |
| 654 |
while (size-- > 0) { |
| 655 |
#e# key = stream.read#E#(); |
| 656 |
V val = (V) stream.readObject(); |
| 657 |
put(key, val); |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
/** |
| 662 |
* Default implementation of T#E#HashingStrategy: |
| 663 |
* delegates hashing to HashFunctions.hash(#e#). |
| 664 |
* |
| 665 |
* @param val the value to hash |
| 666 |
* @return the hashcode. |
| 667 |
*/ |
| 668 |
public final int computeHashCode(#e# val) { |
| 669 |
return HashFunctions.hash(val); |
| 670 |
} |
| 671 |
|
| 672 |
@Override |
| 673 |
public String toString() { |
| 674 |
final StringBuilder sb = new StringBuilder(); |
| 675 |
forEachEntry(new T#E#ObjectProcedure<V>() { |
| 676 |
public boolean execute(#e# key, V value) { |
| 677 |
if (sb.length() != 0) { |
| 678 |
sb.append(',').append(' '); |
| 679 |
} |
| 680 |
sb.append(key); |
| 681 |
sb.append('='); |
| 682 |
sb.append(value == this ? "(this Map)" : value); |
| 683 |
return true; |
| 684 |
} |
| 685 |
}); |
| 686 |
sb.append('}'); |
| 687 |
sb.insert(0, '{'); |
| 688 |
return sb.toString(); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
} // T#E#ObjectHashMap |