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 TObjectDoubleHashMap extends TObjectHash implements Serializable { 36 37 38 static final long serialVersionUID = 1L; 39 40 41 protected transient double[] _values; 42 43 47 public TObjectDoubleHashMap() { 48 super(); 49 } 50 51 58 public TObjectDoubleHashMap(int initialCapacity) { 59 super(initialCapacity); 60 } 61 62 70 public TObjectDoubleHashMap(int initialCapacity, float loadFactor) { 71 super(initialCapacity, loadFactor); 72 } 73 74 79 public TObjectDoubleHashMap(TObjectHashingStrategy strategy) { 80 super(strategy); 81 } 82 83 91 public TObjectDoubleHashMap(int initialCapacity, TObjectHashingStrategy strategy) { 92 super(initialCapacity, strategy); 93 } 94 95 104 public TObjectDoubleHashMap(int initialCapacity, float loadFactor, TObjectHashingStrategy strategy) { 105 super(initialCapacity, loadFactor, strategy); 106 } 107 108 111 public TObjectDoubleIterator iterator() { 112 return new TObjectDoubleIterator(this); 113 } 114 115 122 protected int setUp(int initialCapacity) { 123 int capacity; 124 125 capacity = super.setUp(initialCapacity); 126 _values = new double[capacity]; 127 return capacity; 128 } 129 130 138 public double put(Object key, double value) { 139 double previous = (double)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 double oldVals[] = _values; 166 167 _set = new Object[newCapacity]; 168 Arrays.fill(_set, FREE); 169 _values = new double[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 double get(Object key) { 191 int index = index(key); 192 return index < 0 ? (double)0 : _values[index]; 193 } 194 195 199 public void clear() { 200 super.clear(); 201 Object[] keys = _set; 202 double[] vals = _values; 203 204 for (int i = keys.length; i-- > 0;) { 205 keys[i] = FREE; 206 vals[i] = (double)0; 207 } 208 } 209 210 216 public double remove(Object key) { 217 double prev = (double)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 TObjectDoubleHashMap)) { 235 return false; 236 } 237 TObjectDoubleHashMap that = (TObjectDoubleHashMap)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 TObjectDoubleProcedure { 245 private final TObjectDoubleHashMap _otherMap; 246 247 EqProcedure(TObjectDoubleHashMap otherMap) { 248 _otherMap = otherMap; 249 } 250 251 public final boolean execute(Object key, double 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(double v1, double v2) { 263 return v1 == v2; 264 } 265 266 } 267 268 273 protected void removeAt(int index) { 274 super.removeAt(index); _values[index] = (double)0; 276 } 277 278 283 public double[] getValues() { 284 double[] vals = new double[size()]; 285 double[] 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(double val) { 320 Object[] keys = _set; 321 double[] 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(TDoubleProcedure procedure) { 361 Object[] keys = _set; 362 double[] 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(TObjectDoubleProcedure procedure) { 381 Object[] keys = _set; 382 double[] 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(TObjectDoubleProcedure procedure) { 401 boolean modified = false; 402 Object[] keys = _set; 403 double[] 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(TDoubleFunction function) { 421 Object[] keys = _set; 422 double[] 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, (double)1); 438 } 439 440 447 public boolean adjustValue(Object key, double 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 double val = stream.readDouble(); 480 put(key, val); 481 } 482 } 483 } | Popular Tags |