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