1
2
3
4
5
6
7
8 package org.jdtaus.mojo.resource.model.impl.runtime;
9
10
11
12
13
14
15
16
17 final class IdentityHashSet {
18
19 private Object table[];
20
21
22 private int count;
23
24
25
26
27
28 private int threshold;
29
30
31 private static final float loadFactor = 0.3f;
32 private static final int initialCapacity = 191;
33
34
35 public IdentityHashSet() {
36 table = new Object[initialCapacity];
37 threshold = (int) (initialCapacity * loadFactor);
38 }
39
40 public boolean contains(Object key) {
41 Object tab[] = table;
42 int index = (System.identityHashCode(key) & 0x7FFFFFFF) % tab.length;
43
44 while (true) {
45 final Object e = tab[index];
46 if (e == null)
47 return false;
48 if (e==key)
49 return true;
50 index = (index + 1) % tab.length;
51 }
52 }
53
54
55
56
57
58
59
60
61 private void rehash() {
62
63
64 int oldCapacity = table.length;
65 Object oldMap[] = table;
66
67 int newCapacity = oldCapacity * 2 + 1;
68 Object newMap[] = new Object[newCapacity];
69
70 for (int i = oldCapacity; i-- > 0;)
71 if (oldMap[i] != null) {
72 int index = (System.identityHashCode(oldMap[i]) & 0x7FFFFFFF) % newMap.length;
73 while (newMap[index] != null)
74 index = (index + 1) % newMap.length;
75 newMap[index] = oldMap[i];
76 }
77
78
79 threshold = (int) (newCapacity * loadFactor);
80
81 table = newMap;
82 }
83
84 public boolean add(Object newObj) {
85 if (count >= threshold)
86 rehash();
87
88 Object tab[] = table;
89 int index = (System.identityHashCode(newObj) & 0x7FFFFFFF) % tab.length;
90
91 Object existing;
92
93 while ((existing=tab[index]) != null) {
94 if(existing==newObj) return false;
95 index = (index + 1) % tab.length;
96 }
97 tab[index] = newObj;
98
99 count++;
100
101 return true;
102 }
103 }