1 8 package Acme; 9 10 import java.util.Dictionary ; 11 import java.util.Enumeration ; 12 import java.util.NoSuchElementException ; 13 14 24 public class IntHashtable extends Dictionary implements Cloneable { 25 private IntHashtableEntry table[]; 27 28 private int count; 30 31 private int threshold; 33 34 private float loadFactor; 36 37 public IntHashtable(int initialCapacity, float loadFactor) { 48 if (initialCapacity <= 0 || loadFactor <= 0.0) 49 throw new IllegalArgumentException (); 50 this.loadFactor = loadFactor; 51 table = new IntHashtableEntry[initialCapacity]; 52 threshold = (int) (initialCapacity * loadFactor); 53 } 54 55 public IntHashtable(int initialCapacity) { 59 this(initialCapacity, 0.75f); 60 } 61 62 public IntHashtable() { 66 this(101, 0.75f); 67 } 68 69 public int size() { 71 return count; 72 } 73 74 public boolean isEmpty() { 76 return count == 0; 77 } 78 79 public synchronized Enumeration keys() { 82 return new IntHashtableEnumerator(table, true); 83 } 84 85 public synchronized Enumeration elements() { 89 return new IntHashtableEnumerator(table, false); 90 } 91 92 public synchronized boolean contains(Object value) { 99 if (value == null) 100 throw new NullPointerException (); 101 IntHashtableEntry tab[] = table; 102 for (int i = tab.length; i-- > 0;) { 103 for (IntHashtableEntry e = tab[i]; e != null; e = e.next) { 104 if (e.value.equals(value)) 105 return true; 106 } 107 } 108 return false; 109 } 110 111 public synchronized boolean containsKey(int key) { 115 IntHashtableEntry tab[] = table; 116 int hash = key; 117 int index = (hash & 0x7FFFFFFF) % tab.length; 118 for (IntHashtableEntry e = tab[index]; e != null; e = e.next) { 119 if (e.hash == hash && e.key == key) 120 return true; 121 } 122 return false; 123 } 124 125 public synchronized Object get(int key) { 132 IntHashtableEntry tab[] = table; 133 int hash = key; 134 int index = (hash & 0x7FFFFFFF) % tab.length; 135 for (IntHashtableEntry e = tab[index]; e != null; e = e.next) { 136 if (e.hash == hash && e.key == key) 137 return e.value; 138 } 139 return null; 140 } 141 142 public Object get(Object okey) { 145 if (!(okey instanceof Integer )) 146 throw new InternalError ("key is not an Integer"); 147 Integer ikey = (Integer ) okey; 148 int key = ikey.intValue(); 149 return get(key); 150 } 151 152 protected void rehash() { 156 int oldCapacity = table.length; 157 IntHashtableEntry oldTable[] = table; 158 159 int newCapacity = oldCapacity * 2 + 1; 160 IntHashtableEntry newTable[] = new IntHashtableEntry[newCapacity]; 161 162 threshold = (int) (newCapacity * loadFactor); 163 table = newTable; 164 165 for (int i = oldCapacity; i-- > 0;) { 166 for (IntHashtableEntry old = oldTable[i]; old != null;) { 167 IntHashtableEntry e = old; 168 old = old.next; 169 170 int index = (e.hash & 0x7FFFFFFF) % newCapacity; 171 e.next = newTable[index]; 172 newTable[index] = e; 173 } 174 } 175 } 176 177 public synchronized Object put(int key, Object value) { 187 if (value == null) 189 throw new NullPointerException (); 190 191 IntHashtableEntry tab[] = table; 193 int hash = key; 194 int index = (hash & 0x7FFFFFFF) % tab.length; 195 for (IntHashtableEntry e = tab[index]; e != null; e = e.next) { 196 if (e.hash == hash && e.key == key) { 197 Object old = e.value; 198 e.value = value; 199 return old; 200 } 201 } 202 203 if (count >= threshold) { 204 rehash(); 206 return put(key, value); 207 } 208 209 IntHashtableEntry e = new IntHashtableEntry(); 211 e.hash = hash; 212 e.key = key; 213 e.value = value; 214 e.next = tab[index]; 215 tab[index] = e; 216 ++count; 217 return null; 218 } 219 220 public Object put(Object okey, Object value) { 223 if (!(okey instanceof Integer )) 224 throw new InternalError ("key is not an Integer"); 225 Integer ikey = (Integer ) okey; 226 int key = ikey.intValue(); 227 return put(key, value); 228 } 229 230 public synchronized Object remove(int key) { 235 IntHashtableEntry tab[] = table; 236 int hash = key; 237 int index = (hash & 0x7FFFFFFF) % tab.length; 238 for (IntHashtableEntry e = tab[index], prev = null; e != null; prev = e, e = e.next) { 239 if (e.hash == hash && e.key == key) { 240 if (prev != null) 241 prev.next = e.next; 242 else 243 tab[index] = e.next; 244 --count; 245 return e.value; 246 } 247 } 248 return null; 249 } 250 251 public Object remove(Object okey) { 254 if (!(okey instanceof Integer )) 255 throw new InternalError ("key is not an Integer"); 256 Integer ikey = (Integer ) okey; 257 int key = ikey.intValue(); 258 return remove(key); 259 } 260 261 public synchronized void clear() { 263 IntHashtableEntry tab[] = table; 264 for (int index = tab.length; --index >= 0;) 265 tab[index] = null; 266 count = 0; 267 } 268 269 public synchronized Object clone() { 273 try { 274 IntHashtable t = (IntHashtable) super.clone(); 275 t.table = new IntHashtableEntry[table.length]; 276 for (int i = table.length; i-- > 0;) 277 t.table[i] = (table[i] != null) ? 278 (IntHashtableEntry) table[i].clone() : null; 279 return t; 280 } catch (CloneNotSupportedException e) { 281 throw new InternalError (); 283 } 284 } 285 286 public synchronized String toString() { 288 int max = size() - 1; 289 StringBuffer buf = new StringBuffer (); 290 Enumeration k = keys(); 291 Enumeration e = elements(); 292 buf.append("{"); 293 294 for (int i = 0; i <= max; ++i) { 295 String s1 = k.nextElement().toString(); 296 String s2 = e.nextElement().toString(); 297 buf.append(s1 + "=" + s2); 298 if (i < max) 299 buf.append(", "); 300 } 301 buf.append("}"); 302 return buf.toString(); 303 } 304 } 305 306 307 class IntHashtableEntry { 308 int hash; 309 int key; 310 Object value; 311 IntHashtableEntry next; 312 313 protected Object clone() { 314 IntHashtableEntry entry = new IntHashtableEntry(); 315 entry.hash = hash; 316 entry.key = key; 317 entry.value = value; 318 entry.next = (next != null) ? (IntHashtableEntry) next.clone() : null; 319 return entry; 320 } 321 } 322 323 324 class IntHashtableEnumerator implements Enumeration { 325 boolean keys; 326 int index; 327 IntHashtableEntry table[]; 328 IntHashtableEntry entry; 329 330 IntHashtableEnumerator(IntHashtableEntry table[], boolean keys) { 331 this.table = table; 332 this.keys = keys; 333 this.index = table.length; 334 } 335 336 public boolean hasMoreElements() { 337 if (entry != null) 338 return true; 339 while (index-- > 0) 340 if ((entry = table[index]) != null) 341 return true; 342 return false; 343 } 344 345 public Object nextElement() { 346 if (entry == null) 347 while ((index-- > 0) && ((entry = table[index]) == null)) 348 ; 349 if (entry != null) { 350 IntHashtableEntry e = entry; 351 entry = e.next; 352 return keys ? new Integer (e.key) : e.value; 353 } 354 throw new NoSuchElementException ("IntHashtableEnumerator"); 355 } 356 } 357 | Popular Tags |