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