-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyHashedSet.java
260 lines (222 loc) · 4.3 KB
/
MyHashedSet.java
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
public class MyHashedSet<E> implements MySet <E>
{
private static int DEFAULT_INITIAL_CAPACITY = 4;
private static int MAXIMUM_CAPACITY = 1 << 30;
private int capacity;
private static float DEFAULT_MAX_LOAD_FACTOR = 0.75f;
private float loadFactorThreshold;
private int size = 0;
private LinkedList<E>[] table;
public MyHashedSet()
{
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_LOAD_FACTOR);
}
public MyHashedSet(int initialCapacity)
{
this(initialCapacity, DEFAULT_MAX_LOAD_FACTOR);
}
public MyHashedSet(List<E> lst)
{
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_LOAD_FACTOR);
for(E e: lst)
this.add(e);
}
public MyHashedSet(int initialCapacity, float loadFactorThreshold)
{
if(initialCapacity > MAXIMUM_CAPACITY)
{
this.capacity = MAXIMUM_CAPACITY;
}
else
{
this.capacity = trimToPowerOf2(initialCapacity);
}
this.loadFactorThreshold = loadFactorThreshold;
table = new LinkedList[capacity];
}
private int trimToPowerOf2(int initialCapacity)
{
int capacity = 1;
while(capacity < initialCapacity)
{
capacity <<=1; // it is similar to *=2 but more efficient.
}
return capacity;
}
public Iterator<E> iterator()
{
return new MyHashedSetIterator(this);
}
private class MyHashedSetIterator implements java.util.Iterator<E>
{
private java.util.ArrayList<E> list;
private int current = 0;
private MyHashedSet<E> set;
public MyHashedSetIterator(MyHashedSet<E> set)
{
this.set = set;
list = setToList();
}
public boolean hasNext()
{
if(current < list.size())
{
return true;
}
return false;
}
public E next()
{
return list.get(current++);
}
public void remove()
{
set.remove(list.get(current));
list.remove(current);
}
}
private java.util.ArrayList<E> setToList()
{
java.util.ArrayList<E> list = new java.util.ArrayList<E>();
for(int i = 0; i < capacity; i++)
{
if(table[i] != null)
{
for(E e: table[i])
{
list.add(e);
}
}
}
return list;
}
public void clear()
{
size = 0;
removeElements();
}
private void removeElements()
{
for(int i = 0; i < capacity; i++)
{
if(table[i] != null)
{
table[i].clear();
}
}
}
public boolean contains(E e)
{
int bucketIndex = hash(e.hashCode());
if(table[bucketIndex] != null)
{
LinkedList<E> bucket = table[bucketIndex];
for(E element: bucket)
{
if(element.equals(e))
{
return true;
}
}
}
return false;
}
private int hash(int hashCode)
{
return supplementalHash(hashCode) & (capacity - 1);
}
private int supplementalHash(int h)
{
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
public boolean add(E e)
{
if(contains(e))
{
return false;
}
if(size + 1 > capacity * loadFactorThreshold)
{
if(capacity == MAXIMUM_CAPACITY)
{
throw new RuntimeException("Exceeding maximum capacity");
}
rehash();
}
int bucketIndex = hash(e.hashCode());
if(table[bucketIndex] == null)
{
table[bucketIndex] = new LinkedList<E>();
}
table[bucketIndex].add(e);
size++;
return true;
}
private void rehash()
{
java.util.ArrayList<E> list = setToList();
capacity <<= 1; // It is similar to *= 2. <= is more efficient
table = new LinkedList[capacity];
size = 0;
for(E element: list)
{
add(element);
}
}
public boolean remove(E e)
{
if(!contains(e))
{
return false;
}
int bucketIndex = hash(e.hashCode());
if(table[bucketIndex] != null)
{
LinkedList<E> bucket = table[bucketIndex];
for(E element: bucket)
{
if(e.equals(element))
{
bucket.remove(element);
break;
}
}
}
size--;
return true;
}
public boolean isEmpty()
{
return size == 0;
}
public int size()
{
return size;
}
public String toString()
{
java.util.ArrayList<E> list = setToList();
StringBuilder builder = new StringBuilder("[");
for(int i = 0; i < list.size() - 1; i++)
{
builder.append(list.get(i) + ", ");
}
if(list.size() == 0)
{
builder.append("]");
}
else
{
builder.append(list.get(list.size() - 1) + "]");
}
return builder.toString();
}
public String[] split(String string) {
// TODO Auto-generated method stub
return null;
}
}