-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.java
More file actions
245 lines (222 loc) · 4.31 KB
/
HashTable.java
File metadata and controls
245 lines (222 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.AbstractCollection;
/**
* This class implements hashing with chaining using multiplicative hashing
* @author morin
*
* @param <T>
*/
public class HashTable<T> extends AbstractCollection<T> {
/**
* The hash table
*/
List<T>[] t;
/**
* The "dimension" of the table (table.length = 2^d)
*/
int d;
/**
* The number of elements in the hash table
*/
int n;
/**
* The multiplier
*/
int z;
/**
* The number of bits in an int
*/
protected static final int w = 32;
/**
* Create a new empty hash table
*/
public HashTable() {
d = 1;
t = allocTable(1<<d);
Random r = new Random();
z = r.nextInt() | 1; // is a random odd integer
if( z < 0 ) z = -z;
}
/**
* Allocate and initialize a new empty table
* @param s
* @return
*/
@SuppressWarnings({"unchecked"})
protected List<T>[] allocTable(int s) {
List<T>[] tab = new ArrayList[s];
for (int i = 0; i < s; i++) {
tab[i] = new ArrayList<T>();
}
return tab;
}
/**
* Resize the table so that it has size 2^d
*/
protected void resize(int d) {
this.d = d;
List<T>[] oldTable = t;
t = allocTable(1<<d);
for (int i = 0; i < oldTable.length; i++) {
for (T x : oldTable[i]) {
t[hash(x)].add(x);
//add(x); // note that this increases n!!
}
}
}
/**
* Double the size of the table
*/
protected void grow() {
resize(d+1);
}
/**
* Halve the size of the table
*/
protected void shrink() {
resize(d-1);
}
/**
* Return the number of elements stored in this hash table
*/
public int size() {
return n;
}
/**
* Compute the table location for object x
* @param x
* @return ((x.hashCode() * z) mod 2^w) div 2^(w-d)
*/
protected final int hash(Object x) {
// avoid overflow by using longs
long h = x.hashCode() * (long)z;
return (int)(h) >>> (w-d);
}
/**
* Add the element x to the hashtable if it is not
* already present
*/
public boolean add(T x) {
if (n+1 > t.length)
grow();
t[hash(x)].add(x);
n++;
return true;
}
/**
* Remove the element x from the hashtable if it exists
* @param x
* @return
*/
public int removeAll(Object x) {
int r = 0;
Iterator<T> it = t[hash(x)].iterator();
while (it.hasNext()) {
T y = it.next();
if (y.equals(x)) {
it.remove();
n--;
r++;
}
}
return r;
}
public T removeOne(Object x) {
Iterator<T> it = t[hash(x)].iterator();
while (it.hasNext()) {
T y = it.next();
if (y.equals(x)) {
it.remove();
n--;
return y;
}
}
return null;
}
public boolean remove(Object x) {
return removeOne(x) != null;
}
/**
* Get the copy of x stored in this table.
* @param x - the item to get
* @return - the element y stored in this table such that x.equals(y)
* is true, or null if no such element y exists
*/
public T find(Object x) {
for (T y : t[hash(x)])
if (y.equals(x))
return y;
return null;
}
/**
* Return a list of all the elements y in this hash table such that
* x.equals(y) is true
* @param x the value to search for
* @return a list of all elements y such that x.equals(y) is true
*/
public List<T> findAll(Object x) {
List<T> l = new LinkedList<T>();
int i = hash(x);
for (T y : t[i]) {
if (y.equals(x)) {
l.add(y);
}
}
return l;
}
public Iterator<T> iterator() {
class IT implements Iterator<T> {
int i, j;
int ilast, jlast;
IT() {
i = 0;
j = 0;
while (i < t.length && t[i].isEmpty())
i++;
}
protected void jumpToNext() {
while (i < t.length && j + 1 > t[i].size()) {
j = 0;
i++;
}
}
public boolean hasNext() {
return i < t.length;
}
public T next() {
ilast = i;
jlast = j;
T x = t[i].get(j);
j++;
jumpToNext();
return x;
}
public void remove() {
HashTable.this.remove(t[ilast].get(jlast));
}
}
return new IT();
}
/**
* @param args
*/
public static void main(String[] args) {
int n = 100000;
HashTable<Integer> t = new HashTable<Integer>();
for (int i = 0; i < n; i++) {
t.add(i*2);
}
for (int i = 0; i < 2*n; i++) {
Integer x = t.find(i);
if (i % 2 == 0) {
assert(x.intValue() == i);
} else {
assert(x == null);
}
}
}
}