1 11 package org.eclipse.jdt.internal.core.util; 12 13 16 public final class HashtableOfArrayToObject implements Cloneable { 17 18 public Object [][] keyTable; 20 public Object [] valueTable; 21 22 public int elementSize; int threshold; 24 25 public HashtableOfArrayToObject() { 26 this(13); 27 } 28 29 public HashtableOfArrayToObject(int size) { 30 31 this.elementSize = 0; 32 this.threshold = size; int extraRoom = (int) (size * 1.75f); 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 HashtableOfArrayToObject result = (HashtableOfArrayToObject) 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 = this.keyTable.length; 57 int index = hashCode(key) % length; 58 int keyLength = key.length; 59 Object [] currentKey; 60 while ((currentKey = this.keyTable[index]) != null) { 61 if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key)) 62 return true; 63 if (++index == length) { 64 index = 0; 65 } 66 } 67 return false; 68 } 69 70 public Object get(Object [] key) { 71 int length = this.keyTable.length; 72 int index = hashCode(key) % length; 73 int keyLength = key.length; 74 Object [] currentKey; 75 while ((currentKey = this.keyTable[index]) != null) { 76 if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key)) 77 return this.valueTable[index]; 78 if (++index == length) { 79 index = 0; 80 } 81 } 82 return null; 83 } 84 85 public Object [] getKey(Object [] key, int keyLength) { 86 int length = this.keyTable.length; 87 int index = hashCode(key, keyLength) % length; 88 Object [] currentKey; 89 while ((currentKey = this.keyTable[index]) != null) { 90 if (currentKey.length == keyLength && Util.equalArrays(currentKey, key, keyLength)) 91 return currentKey; 92 if (++index == length) { 93 index = 0; 94 } 95 } 96 return null; 97 } 98 99 private int hashCode(Object [] element) { 100 return hashCode(element, element.length); 101 } 102 103 private int hashCode(Object [] element, int length) { 104 int hash = 0; 105 for (int i = length-1; i >= 0; i--) 106 hash = Util.combineHashCodes(hash, element[i].hashCode()); 107 return hash & 0x7FFFFFFF; 108 } 109 110 public Object put(Object [] key, Object value) { 111 int length = this.keyTable.length; 112 int index = hashCode(key) % length; 113 int keyLength = key.length; 114 Object [] currentKey; 115 while ((currentKey = this.keyTable[index]) != null) { 116 if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key)) 117 return this.valueTable[index] = value; 118 if (++index == length) { 119 index = 0; 120 } 121 } 122 this.keyTable[index] = key; 123 this.valueTable[index] = value; 124 125 if (++this.elementSize > threshold) 127 rehash(); 128 return value; 129 } 130 131 public Object removeKey(Object [] key) { 132 int length = this.keyTable.length; 133 int index = hashCode(key) % length; 134 int keyLength = key.length; 135 Object [] currentKey; 136 while ((currentKey = this.keyTable[index]) != null) { 137 if (currentKey.length == keyLength && Util.equalArraysOrNull(currentKey, key)) { 138 Object value = this.valueTable[index]; 139 this.elementSize--; 140 this.keyTable[index] = null; 141 this.valueTable[index] = null; 142 rehash(); 143 return value; 144 } 145 if (++index == length) { 146 index = 0; 147 } 148 } 149 return null; 150 } 151 152 private void rehash() { 153 154 HashtableOfArrayToObject newHashtable = new HashtableOfArrayToObject(elementSize * 2); Object [] currentKey; 156 for (int i = this.keyTable.length; --i >= 0;) 157 if ((currentKey = this.keyTable[i]) != null) 158 newHashtable.put(currentKey, this.valueTable[i]); 159 160 this.keyTable = newHashtable.keyTable; 161 this.valueTable = newHashtable.valueTable; 162 this.threshold = newHashtable.threshold; 163 } 164 165 public int size() { 166 return elementSize; 167 } 168 169 public String toString() { 170 StringBuffer buffer = new StringBuffer (); 171 Object [] element; 172 for (int i = 0, length = this.keyTable.length; i < length; i++) 173 if ((element = this.keyTable[i]) != null) { 174 buffer.append('{'); 175 for (int j = 0, length2 = element.length; j < length2; j++) { 176 buffer.append(element[j]); 177 if (j != length2-1) 178 buffer.append(", "); } 180 buffer.append("} -> "); buffer.append(this.valueTable[i]); 182 if (i != length-1) 183 buffer.append('\n'); 184 } 185 return buffer.toString(); 186 } 187 } 188 | Popular Tags |