1 4 5 9 10 11 54 55 56 package org.openlaszlo.utils; 57 58 import java.util.ConcurrentModificationException ; 59 import java.util.Enumeration ; 60 import java.util.NoSuchElementException ; 61 62 66 73 public final class HashIntTable 74 { 75 78 Entry[] table = null; 79 80 84 private int defaultValue; 86 89 private int numberOfEntries = 0; 90 91 94 private int modificationCount = 0; 95 96 97 101 public HashIntTable() 102 { 103 this(101, 0); 104 } 105 106 107 113 public HashIntTable(int size, int defaultValue) 114 { 115 if (size <= 0) { 116 throw new IllegalArgumentException ("The argument 'size' is not greater than 0."); 117 } 118 119 this.table = new Entry[size]; 120 this.defaultValue = defaultValue; 121 } 122 123 128 public int size() 129 { 130 return numberOfEntries; 131 } 132 133 134 145 public Enumeration keys() 146 { 147 return new KeysEnumeration(modificationCount, table, this); 148 } 149 150 157 public int get(Object key) 158 { 159 if (null == key) { 160 throw new IllegalArgumentException ("The arguments 'key' is null."); 161 } 162 163 int hashCode = key.hashCode(); 165 Entry entry = table[(hashCode & 0x7FFFFFFF) % table.length]; 167 168 while (null != entry) { 170 if ((entry.hashCode == hashCode) && 171 (entry.key.equals(key))) { 172 return entry.value; 173 } 174 entry = entry.next; 176 } 177 return defaultValue; 179 } 180 181 187 public boolean containsKey(Object key) 188 { 189 if (null == key) { 190 throw new IllegalArgumentException ("The arguments 'key' is null."); 191 } 192 193 int hashCode = key.hashCode(); 195 Entry entry = table[(hashCode & 0x7FFFFFFF) % table.length]; 197 198 while (null != entry) { 200 if ((entry.hashCode == hashCode) && 201 (entry.key.equals(key))) { 202 return true; 203 } 204 entry = entry.next; 206 } 207 return false; 209 } 210 211 212 213 220 public int remove(Object key) 221 { 222 if (null == key) { 223 throw new IllegalArgumentException ("The arguments 'key' is null."); 224 } 225 226 int hashCode = key.hashCode(); 228 int index = (hashCode & 0x7FFFFFFF) % table.length; 230 Entry entry = table[index]; 232 Entry previousEntry = null; 234 235 while (null != entry) { 237 if ((entry.hashCode == hashCode) && 238 (entry.key.equals(key))) { 239 if (null == previousEntry) { 241 table[index] = entry.next; 242 } 243 else { 244 previousEntry.next = entry.next; 245 } 246 --numberOfEntries; 248 ++modificationCount; 250 return entry.value; 251 } 252 previousEntry = entry; 254 entry = entry.next; 256 } 257 return defaultValue; 259 } 260 261 262 272 public int put(Object key, int value) 273 { 274 if (null == key) { 275 throw new IllegalArgumentException ("The arguments 'key' is null."); 276 } 277 278 int hashCode = key.hashCode(); 280 int index = (hashCode & 0x7FFFFFFF) % table.length; 282 Entry entry = table[index]; 284 Entry previousEntry = null; 286 287 while (null != entry) { 289 if ((entry.hashCode == hashCode) && 290 (entry.key.equals(key))) { 291 int oldValue = entry.value; 293 entry.value = value; 295 return oldValue; 296 } 297 previousEntry = entry; 299 entry = entry.next; 301 } 302 303 if (null == previousEntry) { 305 table[index] = new Entry(key, hashCode, value); 306 } 307 else { 308 previousEntry.next= new Entry(key, hashCode, value); 309 } 310 311 ++numberOfEntries; 313 ++modificationCount; 315 return value; 317 } 318 319 320 330 public int increment(Object key, int increment) 331 { 332 if (null == key) { 333 throw new IllegalArgumentException ("The arguments 'key' is null."); 334 } 335 336 int hashCode = key.hashCode(); 338 int index = (hashCode & 0x7FFFFFFF) % table.length; 340 Entry entry = table[index]; 342 Entry previousEntry = null; 344 345 while (null != entry) { 347 if ((entry.hashCode == hashCode) && 348 (entry.key.equals(key))) { 349 entry.value += increment; 351 return entry.value; 352 } 353 previousEntry = entry; 355 entry = entry.next; 357 } 358 359 int value = defaultValue + increment; 361 362 if (null == previousEntry) { 364 table[index] = new Entry(key, hashCode, value); 365 } 366 else { 367 previousEntry.next= new Entry(key, hashCode, value); 368 } 369 370 ++numberOfEntries; 372 ++modificationCount; 374 return value; 376 } 377 378 379 public static void main (String args[]) { 380 HashIntTable table = new HashIntTable(); 381 Object [] keys = new Object [4]; 382 383 for (int i = keys.length; --i >= 0;) { 384 final String name = "Key" + i; 385 keys[i] = new Object (){public String toString(){return name;}}; 386 } 387 388 System.out.println("size " + table.size()); 389 System.out.println("get " + keys[0]); 390 System.out.println("= " + table.get(keys[0])); 391 392 for (int i = keys.length; --i >= 0;) { 393 table.put(keys[i], i); 394 } 395 396 System.out.println("size " + table.size()); 397 398 for (int i = keys.length; --i >= 0;) { 399 System.out.println("get " + keys[i]); 400 System.out.println("= " + table.get(keys[i])); 401 } 402 403 for (int i = keys.length; --i >= 0;) { 404 table.increment(keys[i], 100); 405 } 406 407 System.out.println("size " + table.size()); 408 409 for (int i = keys.length; --i >= 0;) { 410 System.out.println("get " + keys[i]); 411 System.out.println("= " + table.get(keys[i])); 412 } 413 414 for (int i = keys.length; --i >= 0;) { 415 System.out.println("remove " + keys[i]); 416 System.out.println("= " + table.remove(keys[i])); 417 } 418 419 System.out.println("size " + table.size()); 420 421 for (int i = keys.length; --i >= 0;) { 422 System.out.println("get " + keys[i]); 423 System.out.println("= " + table.get(keys[i])); 424 } 425 426 for (int i = keys.length; --i >= 0;) { 427 System.out.println("remove " + keys[i]); 428 System.out.println("= " + table.remove(keys[i])); 429 } 430 431 System.out.println("size " + table.size()); 432 433 for (int i = keys.length; --i >= 0;) { 434 System.out.println("get " + keys[i]); 435 System.out.println("= " + table.get(keys[i])); 436 } 437 438 for (int i = keys.length; --i >= 0;) { 439 table.increment(keys[i], 100); 440 } 441 442 System.out.println("size " + table.size()); 443 444 for (int i = keys.length; --i >= 0;) { 445 System.out.println("get " + keys[i]); 446 System.out.println("= " + table.get(keys[i])); 447 } 448 449 for (int i = keys.length; --i >= 0;) { 450 table.put(keys[i], i); 451 } 452 453 System.out.println("size " + table.size()); 454 455 for (int i = keys.length; --i >= 0;) { 456 System.out.println("get " + keys[i]); 457 System.out.println("= " + table.get(keys[i])); 458 } 459 460 for (Enumeration e = table.keys(); e.hasMoreElements();) { 461 System.out.println("key " + e.nextElement()); 462 } 463 } 464 465 } 466 467 470 class KeysEnumeration 471 implements Enumeration { 472 476 final int expectedModificationCount; 477 478 479 483 private int index = -1; 484 485 486 489 Entry entry = null; 490 491 492 Entry[] table; 493 HashIntTable ht; 494 495 502 KeysEnumeration(int modificationCount, Entry[] table, HashIntTable ht) 503 { 504 this.expectedModificationCount = modificationCount; 505 this.table = table; 506 this.ht = ht; 507 } 508 509 510 517 public boolean hasMoreElements() 518 { 519 if (null == entry) { 520 for (; ++index < table.length;) { 521 entry = table[index]; 522 523 if (null != entry) { 524 return true; 525 } 526 } 527 } 528 529 return null != entry; 530 } 531 532 539 public Object nextElement() 540 { 541 if (!hasMoreElements()) { 542 throw new NoSuchElementException ("No more elements in exception."); 543 } 544 545 Object key = entry.key; 547 entry = entry.next; 549 550 return key; 551 } 552 } 553 554 558 class Entry { 559 562 final Object key; 563 564 567 final int hashCode; 568 569 572 int value; 573 574 577 Entry next = null; 578 579 586 Entry(Object key, int hashCode, int value) 587 { 588 this.key = key; 589 this.hashCode = hashCode; 590 this.value = value; 591 } 592 } 593 594 595 596 | Popular Tags |