1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 16 public final class HashtableOfObjectToIntArray implements Cloneable { 17 18 public Object [] keyTable; 20 public int[][] valueTable; 21 22 public int elementSize; int threshold; 24 25 public HashtableOfObjectToIntArray() { 26 this(13); 27 } 28 29 public HashtableOfObjectToIntArray(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 int[extraRoom][]; 38 } 39 40 public Object clone() throws CloneNotSupportedException { 41 HashtableOfObjectToIntArray result = (HashtableOfObjectToIntArray) 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 int[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 index = (key.hashCode()& 0x7FFFFFFF) % length; 58 Object currentKey; 59 while ((currentKey = this.keyTable[index]) != null) { 60 if (currentKey.equals(key)) 61 return true; 62 if (++index == length) { 63 index = 0; 64 } 65 } 66 return false; 67 } 68 69 public int[] get(Object key) { 70 int length = this.keyTable.length, 71 index = (key.hashCode()& 0x7FFFFFFF) % length; 72 Object currentKey; 73 while ((currentKey = this.keyTable[index]) != null) { 74 if (currentKey.equals(key)) 75 return this.valueTable[index]; 76 if (++index == length) { 77 index = 0; 78 } 79 } 80 return null; 81 } 82 83 public void keysToArray(Object [] array) { 84 int index = 0; 85 for (int i=0, length=this.keyTable.length; i<length; i++) { 86 if (this.keyTable[i] != null) 87 array[index++] = this.keyTable[i]; 88 } 89 } 90 91 public int[] put(Object key, int[] value) { 92 int length = this.keyTable.length, 93 index = (key.hashCode()& 0x7FFFFFFF) % length; 94 Object currentKey; 95 while ((currentKey = this.keyTable[index]) != null) { 96 if (currentKey.equals(key)) 97 return this.valueTable[index] = value; 98 if (++index == length) { 99 index = 0; 100 } 101 } 102 this.keyTable[index] = key; 103 this.valueTable[index] = value; 104 105 if (++elementSize > threshold) 107 rehash(); 108 return value; 109 } 110 111 public int[] removeKey(Object key) { 112 int length = this.keyTable.length, 113 index = (key.hashCode()& 0x7FFFFFFF) % length; 114 Object currentKey; 115 while ((currentKey = this.keyTable[index]) != null) { 116 if (currentKey.equals(key)) { 117 int[] value = this.valueTable[index]; 118 elementSize--; 119 this.keyTable[index] = null; 120 rehash(); 121 return value; 122 } 123 if (++index == length) { 124 index = 0; 125 } 126 } 127 return null; 128 } 129 130 private void rehash() { 131 132 HashtableOfObjectToIntArray newHashtable = new HashtableOfObjectToIntArray(elementSize * 2); Object currentKey; 134 for (int i = this.keyTable.length; --i >= 0;) 135 if ((currentKey = this.keyTable[i]) != null) 136 newHashtable.put(currentKey, this.valueTable[i]); 137 138 this.keyTable = newHashtable.keyTable; 139 this.valueTable = newHashtable.valueTable; 140 this.threshold = newHashtable.threshold; 141 } 142 143 public int size() { 144 return elementSize; 145 } 146 147 public String toString() { 148 String s = ""; Object key; 150 for (int i = 0, length = this.keyTable.length; i < length; i++) 151 if ((key = this.keyTable[i]) != null) 152 s += key + " -> " + this.valueTable[i] + "\n"; return s; 154 } 155 } 156 | Popular Tags |