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