1 29 30 package com.lowagie.text.pdf; 31 32 import java.util.Arrays ; 33 import java.util.Iterator ; 34 import java.util.NoSuchElementException ; 35 36 49 public class IntHashtable implements Cloneable { 50 51 54 private transient Entry table[]; 55 56 59 private transient int count; 60 61 67 private int threshold; 68 69 74 private float loadFactor; 75 76 80 public IntHashtable() { 81 this(150, 0.75f); 82 } 83 84 92 public IntHashtable(int initialCapacity) { 93 this(initialCapacity, 0.75f); 94 } 95 96 105 public IntHashtable(int initialCapacity, float loadFactor) { 106 super(); 107 if (initialCapacity < 0) { 108 throw new IllegalArgumentException ("Illegal Capacity: " + initialCapacity); 109 } 110 if (loadFactor <= 0) { 111 throw new IllegalArgumentException ("Illegal Load: " + loadFactor); 112 } 113 if (initialCapacity == 0) { 114 initialCapacity = 1; 115 } 116 this.loadFactor = loadFactor; 117 table = new Entry[initialCapacity]; 118 threshold = (int) (initialCapacity * loadFactor); 119 } 120 121 126 public int size() { 127 return count; 128 } 129 130 136 public boolean isEmpty() { 137 return count == 0; 138 } 139 140 158 public boolean contains(int value) { 159 160 Entry tab[] = table; 161 for (int i = tab.length; i-- > 0;) { 162 for (Entry e = tab[i]; e != null; e = e.next) { 163 if (e.value == value) { 164 return true; 165 } 166 } 167 } 168 return false; 169 } 170 171 183 public boolean containsValue(int value) { 184 return contains(value); 185 } 186 187 196 public boolean containsKey(int key) { 197 Entry tab[] = table; 198 int hash = key; 199 int index = (hash & 0x7FFFFFFF) % tab.length; 200 for (Entry e = tab[index]; e != null; e = e.next) { 201 if (e.hash == hash && e.key == key) { 202 return true; 203 } 204 } 205 return false; 206 } 207 208 217 public int get(int key) { 218 Entry tab[] = table; 219 int hash = key; 220 int index = (hash & 0x7FFFFFFF) % tab.length; 221 for (Entry e = tab[index]; e != null; e = e.next) { 222 if (e.hash == hash && e.key == key) { 223 return e.value; 224 } 225 } 226 return 0; 227 } 228 229 238 protected void rehash() { 239 int oldCapacity = table.length; 240 Entry oldMap[] = table; 241 242 int newCapacity = oldCapacity * 2 + 1; 243 Entry newMap[] = new Entry[newCapacity]; 244 245 threshold = (int) (newCapacity * loadFactor); 246 table = newMap; 247 248 for (int i = oldCapacity; i-- > 0;) { 249 for (Entry old = oldMap[i]; old != null;) { 250 Entry e = old; 251 old = old.next; 252 253 int index = (e.hash & 0x7FFFFFFF) % newCapacity; 254 e.next = newMap[index]; 255 newMap[index] = e; 256 } 257 } 258 } 259 260 275 public int put(int key, int value) { 276 Entry tab[] = table; 278 int hash = key; 279 int index = (hash & 0x7FFFFFFF) % tab.length; 280 for (Entry e = tab[index]; e != null; e = e.next) { 281 if (e.hash == hash && e.key == key) { 282 int old = e.value; 283 e.value = value; 284 return old; 285 } 286 } 287 288 if (count >= threshold) { 289 rehash(); 291 292 tab = table; 293 index = (hash & 0x7FFFFFFF) % tab.length; 294 } 295 296 Entry e = new Entry(hash, key, value, tab[index]); 298 tab[index] = e; 299 count++; 300 return 0; 301 } 302 303 314 public int remove(int key) { 315 Entry tab[] = table; 316 int hash = key; 317 int index = (hash & 0x7FFFFFFF) % tab.length; 318 for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.next) { 319 if (e.hash == hash && e.key == key) { 320 if (prev != null) { 321 prev.next = e.next; 322 } else { 323 tab[index] = e.next; 324 } 325 count--; 326 int oldValue = e.value; 327 e.value = 0; 328 return oldValue; 329 } 330 } 331 return 0; 332 } 333 334 337 public synchronized void clear() { 338 Entry tab[] = table; 339 for (int index = tab.length; --index >= 0;) { 340 tab[index] = null; 341 } 342 count = 0; 343 } 344 345 349 static class Entry { 350 int hash; 351 int key; 352 int value; 353 Entry next; 354 355 363 protected Entry(int hash, int key, int value, Entry next) { 364 this.hash = hash; 365 this.key = key; 366 this.value = value; 367 this.next = next; 368 } 369 370 public int getKey() { 372 return key; 373 } 374 public int getValue() { 375 return value; 376 } 377 protected Object clone() { 378 Entry entry = new Entry(hash, key, value, (next != null) ? (Entry)next.clone() : null); 379 return entry; 380 } 381 } 382 383 static class IntHashtableIterator implements Iterator { 385 int index; 386 Entry table[]; 387 Entry entry; 388 389 IntHashtableIterator(Entry table[]) { 390 this.table = table; 391 this.index = table.length; 392 } 393 public boolean hasNext() { 394 if (entry != null) { 395 return true; 396 } 397 while (index-- > 0) { 398 if ((entry = table[index]) != null) { 399 return true; 400 } 401 } 402 return false; 403 } 404 405 public Object next() { 406 if (entry == null) { 407 while ((index-- > 0) && ((entry = table[index]) == null)); 408 } 409 if (entry != null) { 410 Entry e = entry; 411 entry = e.next; 412 return e; 413 } 414 throw new NoSuchElementException ("IntHashtableIterator"); 415 } 416 public void remove() { 417 throw new UnsupportedOperationException ("remove() not supported."); 418 } 419 } 420 421 423 public Iterator getEntryIterator() { 424 return new IntHashtableIterator(table); 425 } 426 427 public int[] toOrderedKeys() { 428 int res[] = getKeys(); 429 Arrays.sort(res); 430 return res; 431 } 432 433 public int[] getKeys() { 434 int res[] = new int[count]; 435 int ptr = 0; 436 int index = table.length; 437 Entry entry = null; 438 while (true) { 439 if (entry == null) 440 while ((index-- > 0) && ((entry = table[index]) == null)); 441 if (entry == null) 442 break; 443 Entry e = entry; 444 entry = e.next; 445 res[ptr++] = e.key; 446 } 447 return res; 448 } 449 450 public int getOneKey() { 451 if (count == 0) 452 return 0; 453 int index = table.length; 454 Entry entry = null; 455 while ((index-- > 0) && ((entry = table[index]) == null)); 456 if (entry == null) 457 return 0; 458 return entry.key; 459 } 460 461 public Object clone() { 462 try { 463 IntHashtable t = (IntHashtable)super.clone(); 464 t.table = new Entry[table.length]; 465 for (int i = table.length ; i-- > 0 ; ) { 466 t.table[i] = (table[i] != null) 467 ? (Entry)table[i].clone() : null; 468 } 469 return t; 470 } catch (CloneNotSupportedException e) { 471 throw new InternalError (); 473 } 474 } 475 } | Popular Tags |