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 import java.util.Arrays; 26 27 35 public class TObjectIntHashMap extends TObjectHash implements Serializable { 36 37 38 static final long serialVersionUID = 1L; 39 40 41 protected transient int[] _values; 42 43 47 public TObjectIntHashMap() { 48 super(); 49 } 50 51 58 public TObjectIntHashMap(int initialCapacity) { 59 super(initialCapacity); 60 } 61 62 70 public TObjectIntHashMap(int initialCapacity, float loadFactor) { 71 super(initialCapacity, loadFactor); 72 } 73 74 79 public TObjectIntHashMap(TObjectHashingStrategy strategy) { 80 super(strategy); 81 } 82 83 91 public TObjectIntHashMap(int initialCapacity, TObjectHashingStrategy strategy) { 92 super(initialCapacity, strategy); 93 } 94 95 104 public TObjectIntHashMap(int initialCapacity, float loadFactor, TObjectHashingStrategy strategy) { 105 super(initialCapacity, loadFactor, strategy); 106 } 107 108 111 public TObjectIntIterator iterator() { 112 return new TObjectIntIterator(this); 113 } 114 115 122 protected int setUp(int initialCapacity) { 123 int capacity; 124 125 capacity = super.setUp(initialCapacity); 126 _values = new int[capacity]; 127 return capacity; 128 } 129 130 138 public int put(Object key, int value) { 139 int previous = (int)0; 140 int index = insertionIndex(key); 141 boolean isNewMapping = true; 142 if (index < 0) { 143 index = -index -1; 144 previous = _values[index]; 145 isNewMapping = false; 146 } 147 Object oldKey = _set[index]; 148 _set[index] = key; 149 _values[index] = value; 150 151 if (isNewMapping) { 152 postInsertHook(oldKey == FREE); 153 } 154 return previous; 155 } 156 157 162 protected void rehash(int newCapacity) { 163 int oldCapacity = _set.length; 164 Object oldKeys[] = _set; 165 int oldVals[] = _values; 166 167 _set = new Object[newCapacity]; 168 Arrays.fill(_set, FREE); 169 _values = new int[newCapacity]; 170 171 for (int i = oldCapacity; i-- > 0;) { 172 if(oldKeys[i] != FREE && oldKeys[i] != REMOVED) { 173 Object o = oldKeys[i]; 174 int index = insertionIndex(o); 175 if (index < 0) { 176 throwObjectContractViolation(_set[(-index -1)], o); 177 } 178 _set[index] = o; 179 _values[index] = oldVals[i]; 180 } 181 } 182 } 183 184 190 public int get(Object key) { 191 int index = index(key); 192 return index < 0 ? (int)0 : _values[index]; 193 } 194 195 199 public void clear() { 200 super.clear(); 201 Object[] keys = _set; 202 int[] vals = _values; 203 204 for (int i = keys.length; i-- > 0;) { 205 keys[i] = FREE; 206 vals[i] = (int)0; 207 } 208 } 209 210 216 public int remove(Object key) { 217 int prev = (int)0; 218 int index = index(key); 219 if (index >= 0) { 220 prev = _values[index]; 221 removeAt(index); } 223 return prev; 224 } 225 226 233 public boolean equals(Object other) { 234 if (! (other instanceof TObjectIntHashMap)) { 235 return false; 236 } 237 TObjectIntHashMap that = (TObjectIntHashMap)other; 238 if (that.size() != this.size()) { 239 return false; 240 } 241 return forEachEntry(new EqProcedure(that)); 242 } 243 244 private static final class EqProcedure implements TObjectIntProcedure { 245 private final TObjectIntHashMap _otherMap; 246 247 EqProcedure(TObjectIntHashMap otherMap) { 248 _otherMap = otherMap; 249 } 250 251 public final boolean execute(Object key, int value) { 252 int index = _otherMap.index(key); 253 if (index >= 0 && eq(value, _otherMap.get(key))) { 254 return true; 255 } 256 return false; 257 } 258 259 262 private final boolean eq(int v1, int v2) { 263 return v1 == v2; 264 } 265 266 } 267 268 273 protected void removeAt(int index) { 274 super.removeAt(index); _values[index] = (int)0; 276 } 277 278 283 public int[] getValues() { 284 int[] vals = new int[size()]; 285 int[] v = _values; 286 Object[] keys = _set; 287 288 for (int i = v.length, j = 0; i-- > 0;) { 289 if (keys[i] != FREE && keys[i] != REMOVED) { 290 vals[j++] = v[i]; 291 } 292 } 293 return vals; 294 } 295 296 301 public Object[] keys() { 302 Object[] keys = new Object[size()]; 303 Object[] k = _set; 304 305 for (int i = k.length, j = 0; i-- > 0;) { 306 if (k[i] != FREE && k[i] != REMOVED) { 307 keys[j++] = k[i]; 308 } 309 } 310 return keys; 311 } 312 313 319 public boolean containsValue(int val) { 320 Object[] keys = _set; 321 int[] vals = _values; 322 323 for (int i = vals.length; i-- > 0;) { 324 if (keys[i] != FREE && keys[i] != REMOVED && val == vals[i]) { 325 return true; 326 } 327 } 328 return false; 329 } 330 331 332 338 public boolean containsKey(Object key) { 339 return contains(key); 340 } 341 342 349 public boolean forEachKey(TObjectProcedure procedure) { 350 return forEach(procedure); 351 } 352 353 360 public boolean forEachValue(TIntProcedure procedure) { 361 Object[] keys = _set; 362 int[] values = _values; 363 for (int i = values.length; i-- > 0;) { 364 if (keys[i] != FREE && keys[i] != REMOVED 365 && ! procedure.execute(values[i])) { 366 return false; 367 } 368 } 369 return true; 370 } 371 372 380 public boolean forEachEntry(TObjectIntProcedure procedure) { 381 Object[] keys = _set; 382 int[] values = _values; 383 for (int i = keys.length; i-- > 0;) { 384 if (keys[i] != FREE 385 && keys[i] != REMOVED 386 && ! procedure.execute(keys[i],values[i])) { 387 return false; 388 } 389 } 390 return true; 391 } 392 393 400 public boolean retainEntries(TObjectIntProcedure procedure) { 401 boolean modified = false; 402 Object[] keys = _set; 403 int[] values = _values; 404 for (int i = keys.length; i-- > 0;) { 405 if (keys[i] != FREE 406 && keys[i] != REMOVED 407 && ! procedure.execute(keys[i],values[i])) { 408 removeAt(i); 409 modified = true; 410 } 411 } 412 return modified; 413 } 414 415 420 public void transformValues(TIntFunction function) { 421 Object[] keys = _set; 422 int[] values = _values; 423 for (int i = values.length; i-- > 0;) { 424 if (keys[i] != FREE && keys[i] != REMOVED) { 425 values[i] = function.execute(values[i]); 426 } 427 } 428 } 429 430 436 public boolean increment(Object key) { 437 return adjustValue(key, (int)1); 438 } 439 440 447 public boolean adjustValue(Object key, int amount) { 448 int index = index(key); 449 if (index < 0) { 450 return false; 451 } else { 452 _values[index] += amount; 453 return true; 454 } 455 } 456 457 458 private void writeObject(ObjectOutputStream stream) 459 throws IOException { 460 stream.defaultWriteObject(); 461 462 stream.writeInt(_size); 464 465 SerializationProcedure writeProcedure = new SerializationProcedure(stream); 466 if (! forEachEntry(writeProcedure)) { 467 throw writeProcedure.exception; 468 } 469 } 470 471 private void readObject(ObjectInputStream stream) 472 throws IOException, ClassNotFoundException { 473 stream.defaultReadObject(); 474 475 int size = stream.readInt(); 476 setUp(size); 477 while (size-- > 0) { 478 Object key = stream.readObject(); 479 int val = stream.readInt(); 480 put(key, val); 481 } 482 } 483 } | Popular Tags |