1 11 package org.eclipse.core.internal.runtime; 12 13 import org.eclipse.core.internal.preferences.StringPool; 14 15 25 public final class HashMapOfString { 26 29 private int elementSize; 30 33 private String [] keyTable; 34 35 private int threshold; 36 37 private String [] valueTable; 38 private static final float LOAD_FACTOR = 0.45f; 39 40 public HashMapOfString() { 41 this(16); 42 } 43 44 public HashMapOfString(int size) { 45 this.elementSize = 0; 46 int tableLen = 1; 48 while (tableLen < size) 49 tableLen *= 2; 50 this.keyTable = new String [tableLen]; 51 this.valueTable = new String [tableLen]; 52 this.threshold = (int) (tableLen * LOAD_FACTOR); 53 } 54 55 public String get(String key) { 56 int lengthMask = keyTable.length - 1; 57 int index = key.hashCode() & lengthMask; 58 String currentKey; 59 while ((currentKey = keyTable[index]) != null) { 60 if (currentKey.equals(key)) 61 return valueTable[index]; 62 index = (index+1) & lengthMask; 63 } 64 return null; 65 } 66 67 public boolean isEmpty() { 68 return elementSize == 0; 69 } 70 71 74 public String [] keys() { 75 String [] result = new String [elementSize]; 76 int next = 0; 77 for (int i = 0; i < keyTable.length; i++) 78 if (keyTable[i] != null) 79 result[next++] = keyTable[i]; 80 return result; 81 } 82 83 public String put(String key, String value) { 84 int lengthMask = keyTable.length - 1; 85 int index = key.hashCode() & lengthMask; 86 String currentKey; 87 while ((currentKey = keyTable[index]) != null) { 88 if (currentKey.equals(key)) 89 return valueTable[index] = value; 90 index = (index+1) & lengthMask; 91 } 92 keyTable[index] = key; 93 valueTable[index] = value; 94 95 if (++elementSize > threshold) 97 rehash(keyTable.length * 2); 98 return value; 99 } 100 101 private void rehash(int newLen) { 102 HashMapOfString newHashtable = new HashMapOfString(newLen); 103 String currentKey; 104 int oldLen = keyTable.length; 105 for (int i = oldLen; --i >= 0;) 106 if ((currentKey = keyTable[i]) != null) 107 newHashtable.put(currentKey, valueTable[i]); 108 this.keyTable = newHashtable.keyTable; 109 this.valueTable = newHashtable.valueTable; 110 this.threshold = newHashtable.threshold; 111 } 112 113 public String removeKey(String key) { 114 int lengthMask = keyTable.length - 1; 115 int index = key.hashCode() & lengthMask; 116 String currentKey; 117 while ((currentKey = keyTable[index]) != null) { 118 if (currentKey.equals(key)) { 119 String value = valueTable[index]; 120 elementSize--; 121 keyTable[index] = null; 122 valueTable[index] = null; 123 rehash((int) (elementSize / LOAD_FACTOR)); 124 return value; 125 } 126 index = (index+1) & lengthMask; 127 } 128 return null; 129 } 130 131 134 public void shareStrings(StringPool set) { 135 String [] array = keyTable; 137 if (array == null) 138 return; 139 for (int i = 0; i < array.length; i++) { 140 String o = array[i]; 141 if (o != null) 142 array[i] = set.add(o); 143 } 144 array = valueTable; 145 if (array == null) 146 return; 147 for (int i = 0; i < array.length; i++) { 148 String o = array[i]; 149 if (o != null) 150 array[i] = set.add(o); 151 } 152 } 153 154 public int size() { 155 return elementSize; 156 } 157 158 public String toString() { 159 String s = ""; String value; 161 for (int i = 0, length = valueTable.length; i < length; i++) 162 if ((value = valueTable[i]) != null) 163 s += keyTable[i] + " -> " + value.toString() + "\n"; return s; 165 } 166 } | Popular Tags |