1 19 package gnu.trove; 20 21 import java.io.IOException; 22 import java.io.ObjectInputStream; 23 import java.io.ObjectOutputStream; 24 import java.io.Serializable; 25 26 34 public class TLongShortHashMap extends TLongHash implements Serializable { 35 36 37 static final long serialVersionUID = 1L; 38 39 40 protected transient short[] _values; 41 42 46 public TLongShortHashMap() { 47 super(); 48 } 49 50 57 public TLongShortHashMap(int initialCapacity) { 58 super(initialCapacity); 59 } 60 61 69 public TLongShortHashMap(int initialCapacity, float loadFactor) { 70 super(initialCapacity, loadFactor); 71 } 72 73 78 public TLongShortHashMap(TLongHashingStrategy strategy) { 79 super(strategy); 80 } 81 82 90 public TLongShortHashMap(int initialCapacity, TLongHashingStrategy strategy) { 91 super(initialCapacity, strategy); 92 } 93 94 103 public TLongShortHashMap(int initialCapacity, float loadFactor, TLongHashingStrategy strategy) { 104 super(initialCapacity, loadFactor, strategy); 105 } 106 107 110 public Object clone() { 111 TLongShortHashMap m = (TLongShortHashMap)super.clone(); 112 m._values = (short[])this._values.clone(); 113 return m; 114 } 115 116 119 public TLongShortIterator iterator() { 120 return new TLongShortIterator(this); 121 } 122 123 130 protected int setUp(int initialCapacity) { 131 int capacity; 132 133 capacity = super.setUp(initialCapacity); 134 _values = new short[capacity]; 135 return capacity; 136 } 137 138 146 public short put(long key, short value) { 147 byte previousState; 148 short previous = (short)0; 149 int index = insertionIndex(key); 150 boolean isNewMapping = true; 151 if (index < 0) { 152 index = -index -1; 153 previous = _values[index]; 154 isNewMapping = false; 155 } 156 previousState = _states[index]; 157 _set[index] = key; 158 _states[index] = FULL; 159 _values[index] = value; 160 if (isNewMapping) { 161 postInsertHook(previousState == FREE); 162 } 163 164 return previous; 165 } 166 167 172 protected void rehash(int newCapacity) { 173 int oldCapacity = _set.length; 174 long oldKeys[] = _set; 175 short oldVals[] = _values; 176 byte oldStates[] = _states; 177 178 _set = new long[newCapacity]; 179 _values = new short[newCapacity]; 180 _states = new byte[newCapacity]; 181 182 for (int i = oldCapacity; i-- > 0;) { 183 if(oldStates[i] == FULL) { 184 long o = oldKeys[i]; 185 int index = insertionIndex(o); 186 _set[index] = o; 187 _values[index] = oldVals[i]; 188 _states[index] = FULL; 189 } 190 } 191 } 192 193 199 public short get(long key) { 200 int index = index(key); 201 return index < 0 ? (short)0 : _values[index]; 202 } 203 204 208 public void clear() { 209 super.clear(); 210 long[] keys = _set; 211 short[] vals = _values; 212 byte[] states = _states; 213 214 for (int i = keys.length; i-- > 0;) { 215 keys[i] = (long)0; 216 vals[i] = (short)0; 217 states[i] = FREE; 218 } 219 } 220 221 227 public short remove(long key) { 228 short prev = (short)0; 229 int index = index(key); 230 if (index >= 0) { 231 prev = _values[index]; 232 removeAt(index); } 234 return prev; 235 } 236 237 244 public boolean equals(Object other) { 245 if (! (other instanceof TLongShortHashMap)) { 246 return false; 247 } 248 TLongShortHashMap that = (TLongShortHashMap)other; 249 if (that.size() != this.size()) { 250 return false; 251 } 252 return forEachEntry(new EqProcedure(that)); 253 } 254 255 public int hashCode() { 256 HashProcedure p = new HashProcedure(); 257 forEachEntry(p); 258 return p.getHashCode(); 259 } 260 261 private final class HashProcedure implements TLongShortProcedure { 262 private int h = 0; 263 264 public int getHashCode() { 265 return h; 266 } 267 268 public final boolean execute(long key, short value) { 269 h += (_hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value)); 270 return true; 271 } 272 } 273 274 private static final class EqProcedure implements TLongShortProcedure { 275 private final TLongShortHashMap _otherMap; 276 277 EqProcedure(TLongShortHashMap otherMap) { 278 _otherMap = otherMap; 279 } 280 281 public final boolean execute(long key, short value) { 282 int index = _otherMap.index(key); 283 if (index >= 0 && eq(value, _otherMap.get(key))) { 284 return true; 285 } 286 return false; 287 } 288 289 292 private final boolean eq(short v1, short v2) { 293 return v1 == v2; 294 } 295 296 } 297 298 303 protected void removeAt(int index) { 304 super.removeAt(index); _values[index] = (short)0; 306 } 307 308 313 public short[] getValues() { 314 short[] vals = new short[size()]; 315 short[] v = _values; 316 byte[] states = _states; 317 318 for (int i = v.length, j = 0; i-- > 0;) { 319 if (states[i] == FULL) { 320 vals[j++] = v[i]; 321 } 322 } 323 return vals; 324 } 325 326 331 public long[] keys() { 332 long[] keys = new long[size()]; 333 long[] k = _set; 334 byte[] states = _states; 335 336 for (int i = k.length, j = 0; i-- > 0;) { 337 if (states[i] == FULL) { 338 keys[j++] = k[i]; 339 } 340 } 341 return keys; 342 } 343 344 350 public boolean containsValue(short val) { 351 byte[] states = _states; 352 short[] vals = _values; 353 354 for (int i = vals.length; i-- > 0;) { 355 if (states[i] == FULL && val == vals[i]) { 356 return true; 357 } 358 } 359 return false; 360 } 361 362 363 369 public boolean containsKey(long key) { 370 return contains(key); 371 } 372 373 380 public boolean forEachKey(TLongProcedure procedure) { 381 return forEach(procedure); 382 } 383 384 391 public boolean forEachValue(TShortProcedure procedure) { 392 byte[] states = _states; 393 short[] values = _values; 394 for (int i = values.length; i-- > 0;) { 395 if (states[i] == FULL && ! procedure.execute(values[i])) { 396 return false; 397 } 398 } 399 return true; 400 } 401 402 410 public boolean forEachEntry(TLongShortProcedure procedure) { 411 byte[] states = _states; 412 long[] keys = _set; 413 short[] values = _values; 414 for (int i = keys.length; i-- > 0;) { 415 if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) { 416 return false; 417 } 418 } 419 return true; 420 } 421 422 429 public boolean retainEntries(TLongShortProcedure procedure) { 430 boolean modified = false; 431 byte[] states = _states; 432 long[] keys = _set; 433 short[] values = _values; 434 for (int i = keys.length; i-- > 0;) { 435 if (states[i] == FULL && ! procedure.execute(keys[i],values[i])) { 436 removeAt(i); 437 modified = true; 438 } 439 } 440 return modified; 441 } 442 443 448 public void transformValues(TShortFunction function) { 449 byte[] states = _states; 450 short[] values = _values; 451 for (int i = values.length; i-- > 0;) { 452 if (states[i] == FULL) { 453 values[i] = function.execute(values[i]); 454 } 455 } 456 } 457 458 464 public boolean increment(long key) { 465 return adjustValue(key, (short)1); 466 } 467 468 475 public boolean adjustValue(long key, short amount) { 476 int index = index(key); 477 if (index < 0) { 478 return false; 479 } else { 480 _values[index] += amount; 481 return true; 482 } 483 } 484 485 486 private void writeObject(ObjectOutputStream stream) 487 throws IOException { 488 stream.defaultWriteObject(); 489 490 stream.writeInt(_size); 492 493 SerializationProcedure writeProcedure = new SerializationProcedure(stream); 494 if (! forEachEntry(writeProcedure)) { 495 throw writeProcedure.exception; 496 } 497 } 498 499 private void readObject(ObjectInputStream stream) 500 throws IOException, ClassNotFoundException { 501 stream.defaultReadObject(); 502 503 int size = stream.readInt(); 504 setUp(size); 505 while (size-- > 0) { 506 long key = stream.readLong(); 507 short val = stream.readShort(); 508 put(key, val); 509 } 510 } 511 } | Popular Tags |