1 package com.sun.tools.xjc.runtime; 2 3 10 final class IdentityHashSet { 11 12 private Object table[]; 13 14 15 private int count; 16 17 21 private int threshold; 22 23 24 private static final float loadFactor = 0.3f; 25 private static final int initialCapacity = 191; 26 27 28 public IdentityHashSet() { 29 table = new Object [initialCapacity]; 30 threshold = (int) (initialCapacity * loadFactor); 31 } 32 33 public boolean contains(Object key) { 34 Object tab[] = table; 35 int index = (System.identityHashCode(key) & 0x7FFFFFFF) % tab.length; 36 37 while (true) { 38 final Object e = tab[index]; 39 if (e == null) 40 return false; 41 if (e==key) 42 return true; 43 index = (index + 1) % tab.length; 44 } 45 } 46 47 54 private void rehash() { 55 int oldCapacity = table.length; 58 Object oldMap[] = table; 59 60 int newCapacity = oldCapacity * 2 + 1; 61 Object newMap[] = new Object [newCapacity]; 62 63 for (int i = oldCapacity; i-- > 0;) 64 if (oldMap[i] != null) { 65 int index = (System.identityHashCode(oldMap[i]) & 0x7FFFFFFF) % newMap.length; 66 while (newMap[index] != null) 67 index = (index + 1) % newMap.length; 68 newMap[index] = oldMap[i]; 69 } 70 71 threshold = (int) (newCapacity * loadFactor); 73 table = newMap; 75 } 76 77 public boolean add(Object newObj) { 78 if (count >= threshold) 79 rehash(); 80 81 Object tab[] = table; 82 int index = (System.identityHashCode(newObj) & 0x7FFFFFFF) % tab.length; 83 84 Object existing; 85 86 while ((existing=tab[index]) != null) { 87 if(existing==newObj) return false; 88 index = (index + 1) % tab.length; 89 } 90 tab[index] = newObj; 91 92 count++; 93 94 return true; 95 } 96 } 97 | Popular Tags |