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 TByteByteHashMap extends TByteHash implements Serializable { 35 36 37 static final long serialVersionUID = 1L; 38 39 40 protected transient byte[] _values; 41 42 46 public TByteByteHashMap() { 47 super(); 48 } 49 50 57 public TByteByteHashMap(int initialCapacity) { 58 super(initialCapacity); 59 } 60 61 69 public TByteByteHashMap(int initialCapacity, float loadFactor) { 70 super(initialCapacity, loadFactor); 71 } 72 73 78 public TByteByteHashMap(TByteHashingStrategy strategy) { 79 super(strategy); 80 } 81 82 90 public TByteByteHashMap(int initialCapacity, TByteHashingStrategy strategy) { 91 super(initialCapacity, strategy); 92 } 93 94 103 public TByteByteHashMap(int initialCapacity, float loadFactor, TByteHashingStrategy strategy) { 104 super(initialCapacity, loadFactor, strategy); 105 } 106 107 110 public Object clone() { 111 TByteByteHashMap m = (TByteByteHashMap)super.clone(); 112 m._values = (byte[])this._values.clone(); 113 return m; 114 } 115 116 119 public TByteByteIterator iterator() { 120 return new TByteByteIterator(this); 121 } 122 123 130 protected int setUp(int initialCapacity) { 131 int capacity; 132 133 capacity = super.setUp(initialCapacity); 134 _values = new byte[capacity]; 135 return capacity; 136 } 137 138 146 public byte put(byte key, byte value) { 147 byte previousState; 148 byte previous = (byte)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 byte oldKeys[] = _set; 175 byte oldVals[] = _values; 176 byte oldStates[] = _states; 177 178 _set = new byte[newCapacity]; 179 _values = new byte[newCapacity]; 180 _states = new byte[newCapacity]; 181 182 for (int i = oldCapacity; i-- > 0;) { 183 if(oldStates[i] == FULL) { 184 byte 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 byte get(byte key) { 200 int index = index(key); 201 return index < 0 ? (byte)0 : _values[index]; 202 } 203 204 208 public void clear() { 209 super.clear(); 210 byte[] keys = _set; 211 byte[] vals = _values; 212 byte[] states = _states; 213 214 for (int i = keys.length; i-- > 0;) { 215 keys[i] = (byte)0; 216 vals[i] = (byte)0; 217 states[i] = FREE; 218 } 219 } 220 221 227 public byte remove(byte key) { 228 byte prev = (byte)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 TByteByteHashMap)) { 246 return false; 247 } 248 TByteByteHashMap that = (TByteByteHashMap)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 TByteByteProcedure { 262 private int h = 0; 263 264 public int getHashCode() { 265 return h; 266 } 267 268 public final boolean execute(byte key, byte value) { 269 h += (_hashingStrategy.computeHashCode(key) ^ HashFunctions.hash(value)); 270 return true; 271 } 272 } 273 274 private static final class EqProcedure implements TByteByteProcedure { 275 private final TByteByteHashMap _otherMap; 276 277 EqProcedure(TByteByteHashMap otherMap) { 278 _otherMap = otherMap; 279 } 280 281 public final boolean execute(byte key, byte 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(byte v1, byte v2) { 293 return v1 == v2; 294 } 295 296 } 297 298 303 protected void removeAt(int index) { 304 super.removeAt(index); _values[index] = (byte)0; 306 } 307 308 313 public byte[] getValues() { 314 byte[] vals = new byte[size()]; 315 byte[] 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 byte[] keys() { 332 byte[] keys = new byte[size()]; 333 byte[] 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(byte val) { 351 byte[] states = _states; 352 byte[] 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(byte key) { 370 return contains(key); 371 } 372 373 380 public boolean forEachKey(TByteProcedure procedure) { 381 return forEach(procedure); 382 } 383 384 391 public boolean forEachValue(TByteProcedure procedure) { 392 byte[] states = _states; 393 byte[] 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(TByteByteProcedure procedure) { 411 byte[] states = _states; 412 byte[] keys = _set; 413 byte[] 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(TByteByteProcedure procedure) { 430 boolean modified = false; 431 byte[] states = _states; 432 byte[] keys = _set; 433 byte[] 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(TByteFunction function) { 449 byte[] states = _states; 450 byte[] 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(byte key) { 465 return adjustValue(key, (byte)1); 466 } 467 468 475 public boolean adjustValue(byte key, byte 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 byte key = stream.readByte(); 507 byte val = stream.readByte(); 508 put(key, val); 509 } 510 } 511 } | Popular Tags |