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