1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 16 17 public final class HashtableOfLong { 18 public long[] keyTable; 20 public Object [] valueTable; 21 22 public int elementSize; int threshold; 24 public HashtableOfLong() { 25 this(13); 26 } 27 public HashtableOfLong(int size) { 28 this.elementSize = 0; 29 this.threshold = size; int extraRoom = (int) (size * 1.75f); 31 if (this.threshold == extraRoom) 32 extraRoom++; 33 this.keyTable = new long[extraRoom]; 34 this.valueTable = new Object [extraRoom]; 35 } 36 public boolean containsKey(long key) { 37 int length = keyTable.length, 38 index = ((int)(key >>> 32)) % length; 39 long currentKey; 40 while ((currentKey = keyTable[index]) != 0) { 41 if (currentKey == key) 42 return true; 43 if (++index == length) { 44 index = 0; 45 } 46 } 47 return false; 48 } 49 public Object get(long key) { 50 int length = keyTable.length, 51 index = ((int)(key >>> 32)) % length; 52 long currentKey; 53 while ((currentKey = keyTable[index]) != 0) { 54 if (currentKey == key) return valueTable[index]; 55 if (++index == length) { 56 index = 0; 57 } 58 } 59 return null; 60 } 61 public Object put(long key, Object value) { 62 int length = keyTable.length, 63 index = ((int)(key >>> 32)) % length; 64 long currentKey; 65 while ((currentKey = keyTable[index]) != 0) { 66 if (currentKey == key) return valueTable[index] = value; 67 if (++index == length) { 68 index = 0; 69 } 70 } 71 keyTable[index] = key; 72 valueTable[index] = value; 73 74 if (++elementSize > threshold) 76 rehash(); 77 return value; 78 } 79 private void rehash() { 80 HashtableOfLong newHashtable = new HashtableOfLong(elementSize * 2); long currentKey; 82 for (int i = keyTable.length; --i >= 0;) 83 if ((currentKey = keyTable[i]) != 0) 84 newHashtable.put(currentKey, valueTable[i]); 85 86 this.keyTable = newHashtable.keyTable; 87 this.valueTable = newHashtable.valueTable; 88 this.threshold = newHashtable.threshold; 89 } 90 public int size() { 91 return elementSize; 92 } 93 public String toString() { 94 String s = ""; Object object; 96 for (int i = 0, length = valueTable.length; i < length; i++) 97 if ((object = valueTable[i]) != null) 98 s += keyTable[i] + " -> " + object.toString() + "\n"; return s; 100 } 101 } 102 | Popular Tags |