1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 18 public final class SimpleLookupTable implements Cloneable { 19 20 public Object [] keyTable; 22 public Object [] valueTable; 23 public int elementSize; public int threshold; 25 26 public SimpleLookupTable() { 27 this(13); 28 } 29 30 public SimpleLookupTable(int size) { 31 this.elementSize = 0; 32 this.threshold = size; int extraRoom = (int) (size * 1.5f); 34 if (this.threshold == extraRoom) 35 extraRoom++; 36 this.keyTable = new Object [extraRoom]; 37 this.valueTable = new Object [extraRoom]; 38 } 39 40 public Object clone() throws CloneNotSupportedException { 41 SimpleLookupTable result = (SimpleLookupTable) super.clone(); 42 result.elementSize = this.elementSize; 43 result.threshold = this.threshold; 44 45 int length = this.keyTable.length; 46 result.keyTable = new Object [length]; 47 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length); 48 49 length = this.valueTable.length; 50 result.valueTable = new Object [length]; 51 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length); 52 return result; 53 } 54 55 public boolean containsKey(Object key) { 56 int length = keyTable.length; 57 int index = (key.hashCode() & 0x7FFFFFFF) % length; 58 Object currentKey; 59 while ((currentKey = keyTable[index]) != null) { 60 if (currentKey.equals(key)) return true; 61 if (++index == length) index = 0; 62 } 63 return false; 64 } 65 66 public Object get(Object key) { 67 int length = keyTable.length; 68 int index = (key.hashCode() & 0x7FFFFFFF) % length; 69 Object currentKey; 70 while ((currentKey = keyTable[index]) != null) { 71 if (currentKey.equals(key)) return valueTable[index]; 72 if (++index == length) index = 0; 73 } 74 return null; 75 } 76 77 public Object getKey(Object key) { 78 int length = keyTable.length; 79 int index = (key.hashCode() & 0x7FFFFFFF) % length; 80 Object currentKey; 81 while ((currentKey = keyTable[index]) != null) { 82 if (currentKey.equals(key)) return currentKey; 83 if (++index == length) index = 0; 84 } 85 return key; 86 } 87 88 public Object keyForValue(Object valueToMatch) { 89 if (valueToMatch != null) 90 for (int i = 0, l = keyTable.length; i < l; i++) 91 if (keyTable[i] != null && valueToMatch.equals(valueTable[i])) 92 return keyTable[i]; 93 return null; 94 } 95 96 public Object put(Object key, Object value) { 97 int length = keyTable.length; 98 int index = (key.hashCode() & 0x7FFFFFFF) % length; 99 Object currentKey; 100 while ((currentKey = keyTable[index]) != null) { 101 if (currentKey.equals(key)) return valueTable[index] = value; 102 if (++index == length) index = 0; 103 } 104 keyTable[index] = key; 105 valueTable[index] = value; 106 107 if (++elementSize > threshold) rehash(); 109 return value; 110 } 111 112 public Object removeKey(Object key) { 113 int length = keyTable.length; 114 int index = (key.hashCode() & 0x7FFFFFFF) % length; 115 Object currentKey; 116 while ((currentKey = keyTable[index]) != null) { 117 if (currentKey.equals(key)) { 118 elementSize--; 119 Object oldValue = valueTable[index]; 120 keyTable[index] = null; 121 valueTable[index] = null; 122 if (keyTable[index + 1 == length ? 0 : index + 1] != null) 123 rehash(); return oldValue; 125 } 126 if (++index == length) index = 0; 127 } 128 return null; 129 } 130 131 public void removeValue(Object valueToRemove) { 132 boolean rehash = false; 133 for (int i = 0, l = valueTable.length; i < l; i++) { 134 Object value = valueTable[i]; 135 if (value != null && value.equals(valueToRemove)) { 136 elementSize--; 137 keyTable[i] = null; 138 valueTable[i] = null; 139 if (!rehash && keyTable[i + 1 == l ? 0 : i + 1] != null) 140 rehash = true; } 142 } 143 if (rehash) rehash(); 144 } 145 146 private void rehash() { 147 SimpleLookupTable newLookupTable = new SimpleLookupTable(elementSize * 2); Object currentKey; 149 for (int i = keyTable.length; --i >= 0;) 150 if ((currentKey = keyTable[i]) != null) 151 newLookupTable.put(currentKey, valueTable[i]); 152 153 this.keyTable = newLookupTable.keyTable; 154 this.valueTable = newLookupTable.valueTable; 155 this.elementSize = newLookupTable.elementSize; 156 this.threshold = newLookupTable.threshold; 157 } 158 159 public String toString() { 160 String s = ""; Object object; 162 for (int i = 0, l = valueTable.length; i < l; i++) 163 if ((object = valueTable[i]) != null) 164 s += keyTable[i].toString() + " -> " + object.toString() + "\n"; return s; 166 } 167 } 168 | Popular Tags |