-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTables.java
522 lines (456 loc) · 13.2 KB
/
HashTables.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
package com.company;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Main {
public static void main(String[] args) {
Cuckoo<Integer, String> ht = new Cuckoo<>();
Random rnd = new Random();
RandomStr rndStr = new RandomStr();
for(int i = 0; i < 100; ++i){
int next = rnd.nextInt(100);
System.out.println("\n\ninserting : " + next);
ht.put(next, rndStr.next(20));
ht.print();
}
System.out.println("initial size: " + ht.size());
while(!ht.isEmpty()){
int next = rnd.nextInt(100);
System.out.println("removing: " + next);
ht.remove(next);
System.out.println("size is " + ht.size());
}
}
}
class RandomStr{
private Random rnd;
public RandomStr(long seed){
rnd = new Random(seed);
}
public RandomStr(){
rnd = new Random();
}
public String next(int bound){
StringBuilder sb = new StringBuilder(bound);
for(int i = 0; i < bound; ++i){
sb.append(rnd.nextInt(Character.MAX_VALUE + 1));
}
return sb.toString();
}
}
class Cuckoo<Key, Value> implements ST<Key, Value>{
private Key[] keysOne;
private Value[] valuesOne;
private Key[] keysTwo;
private Value[] valuesTwo;
private int N;
private int M;
private final int maxLoop = 10;
private int hasher = 0;
private int timesRehashed = 0;
private Random rnd = new Random(System.currentTimeMillis());
public Cuckoo(){
M = 11;
keysOne = (Key[]) new Object[M];
keysTwo = (Key[]) new Object[M];
valuesOne = (Value[]) new Object[M];
valuesTwo = (Value[]) new Object[M];
}
@Override
public void put(Key key, Value val){
if(containsOne(key)) valuesOne[hashOne(key)] = val;
else if(containsTwo(key)) valuesTwo[hashTwo(key)] = val;
else{
int index = hashOne(key);
for(int i = 0; i < maxLoop; ++i){
if(keysOne[index] == null){
keysOne[index] = key;
valuesOne[index] = val;
N++;
return;
} else{
Key oldKey = keysOne[index];
Value oldVal = valuesOne[index];
keysOne[index] = key;
valuesOne[index] = val;
key = oldKey;
val = oldVal;
int tempIndex = hashTwo(key);
if(keysTwo[tempIndex] == null){
keysTwo[tempIndex] = key;
valuesTwo[tempIndex] = val;
N++;
return;
} else{
oldKey = keysTwo[tempIndex];
oldVal = valuesTwo[tempIndex];
keysTwo[tempIndex] = key;
valuesTwo[tempIndex] = val;
key = oldKey;
val = oldVal;
}
}
}
rehash(); put(key, val);
}
}
public void print(){
System.out.println("\nOne:\n");
for(int i = 0; i < keysOne.length; ++i){
if(keysOne[i] != null){
System.out.print(keysOne[i] + ",");
}
}
System.out.println("\nTwo:\n");
for(int i = 0; i < keysTwo.length; ++i) {
if (keysTwo[i] != null){
System.out.print(keysTwo[i] + ", ");
}
}
System.out.println("\nTimes rehashed: " + timesRehashed);
}
@Override
public void remove(Key key) {
if(containsOne(key)){
keysOne[hashOne(key)] = null;
valuesOne[hashOne(key)] = null;
} else if(containsTwo(key)){
keysTwo[hashTwo(key)] = null;
valuesTwo[hashTwo(key)] = null;
} else return;
N--;
if(N <= M / 4) resize(M / 2);
}
private void resize(int size){
Key[] oldKeysOne = keysOne;
Key[] oldKeysTwo = keysTwo;
Value[] oldValsOne = valuesOne;
Value[] oldValsTwo = valuesTwo;
M = size;
keysOne = (Key[]) new Object[size];
valuesOne = (Value[]) new Object[size];
keysTwo = (Key[]) new Object[size];
valuesTwo = (Value[]) new Object[size];
for(int i = 0; i < oldKeysOne.length; ++i){
if(oldKeysOne[i] != null){
N--;
put(oldKeysOne[i], oldValsOne[i]);
}
}
for(int i = 0; i < oldKeysTwo.length; ++i){
if(oldKeysTwo[i] != null){
N--;
put(oldKeysTwo[i], oldValsTwo[i]);
}
}
}
@Override
public Value get(Key key) {
int index1 = hashOne(key);
if(key.equals(keysOne[index1])) return valuesOne[index1];
int index2 = hashTwo(key);
if(key.equals(keysTwo[index2])) return valuesTwo[index2];
return null;
}
@Override
public int size() {
return N;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean contains(Key key) {
if(containsOne(key) || containsTwo(key)) return true;
return false;
}
private int hashOne(Key key){
return ((31 + hasher) * (key.hashCode() & 0x7fffffff)) % M;
}
private int hashTwo(Key key){
return ((13 + hasher) * (key.hashCode() & 0x7fffffff)) % M;
}
private boolean containsOne(Key key){
int index = hashOne(key);
if(key.equals(keysOne[index])) return true;
return false;
}
private boolean containsTwo(Key key){
int index = hashTwo(key);
if(key.equals(keysTwo[index])) return true;
return false;
}
private void rehash(){
hasher = (hasher + 13) % 1000;
resize(2 * M);
timesRehashed++;
}
}
interface ST<Key, Value>{
void put(Key key, Value val);
void remove(Key key);
Value get(Key key);
int size();
boolean isEmpty();
boolean contains(Key key);
}
class LinearProbingST<Key, Value> implements ST<Key, Value>{
private int N;
private int M;
private Key[] keys;
private Value[] values;
public void printNM(){
System.out.println("N = " + N + ", M = " + M);
}
public LinearProbingST(){
keys = (Key[]) new Object[13];
values = (Value[]) new Object[13];
M = 13;
}
public LinearProbingST(int cap){
keys = (Key[]) new Object[cap];
values = (Value[]) new Object[cap];
M = cap;
}
@Override
public void put(Key key, Value val) {
if(N >= M / 2) resize(M * 2);
int i;
for(i = hash(key); keys[i] != null; i = (i + 1) % M){
if(key.equals(keys[i])){
values[i] = val; return;
}
}
keys[i] = key;
values[i] = val;
N++;
}
@Override
public void remove(Key key) {
if(!contains(key)) return;
int i = hash(key);
while(!keys[i].equals(key)) i = (i + 1) % M;
keys[i] = null; values[i] = null;
i = (i + 1) % M;
while(keys[i] != null){
Key tempKey = keys[i];
Value tempVal = values[i];
keys[i] = null;
values[i] = null;
i = (i + 1) % M;
N--;
put(tempKey, tempVal);
//System.out.println("Index : " + i);
}
N--;
if(N > 0 && N <= M / 8) resize(M / 2);
}
@Override
public Value get(Key key) {
for(int i = hash(key); keys[i] != null; i = (i + 1) % M){
if(key.equals(keys[i])) return values[i];
}
return null;
}
@Override
public int size() {
return N;
}
@Override
public boolean isEmpty() {
return N == 0;
}
@Override
public boolean contains(Key key) {
for(int i = hash(key); keys[i] != null; i = (i + 1) % M){
if(key.equals(keys[i])) return true;
}
return false;
}
private void resize(int size){
LinearProbingST<Key, Value> temp = new LinearProbingST<>(size);
for(int i = 0; i < M; ++i){
if(keys[i] != null) temp.put(keys[i], values[i]);
}
keys = temp.keys;
values = temp.values;
M = temp.M;
}
private int hash(Key key){
return (key.hashCode() & 0x7fffffff) % M;
}
}
class SeparateChainingST<Key, Value> implements ST<Key, Value>{
private LinkedList<Key, Value>[] table;
private int N;
private int M;
public SeparateChainingST(int cap){
M = cap;
table = (LinkedList<Key, Value>[]) new LinkedList[M];
for(int i = 0; i < M; ++i){
table[i] = new LinkedList<>();
}
}
public SeparateChainingST(){
M = 13;
table = (LinkedList<Key, Value>[]) new LinkedList[M];
for(int i = 0; i < M; ++i){
table[i] = new LinkedList<>();
}
}
@Override
public void put(Key key, Value val) {
if(N >= M/2) resize(M*2);
int index = hash(key);
int oldSize = table[index].size();
table[index].put(key, val);
int newSize = table[index].size();
if(oldSize != newSize) N++;
}
@Override
public void remove(Key key) {
int index = hash(key);
int oldSize = table[index].size();
table[index].remove(key);
int newSize = table[index].size();
if(oldSize != newSize) N--;
if(N > 0 && N == M/8) resize(M/2);
}
@Override
public Value get(Key key) {
int index = hash(key);
Value val = table[index].get(key);
if(val != null) return val;
return null;
}
@Override
public int size() {
return N;
}
@Override
public boolean isEmpty() {
return N == 0;
}
@Override
public boolean contains(Key key) {
int index = hash(key);
if(table[index].contains(key)) return true;
return false;
}
private int hash(Key key){
return (key.hashCode() & 0x7fffffff) % M;
}
private void resize(int cap) {
SeparateChainingST<Key, Value> temp = new SeparateChainingST<>(cap);
for(int i = 0; i < M; ++i){
if(!table[i].isEmpty()){
for(LinkedList<Key, Value>.Entry e : table[i]){
temp.put(e.key, e.val);
}
}
}
M = cap;
table = temp.table;
}
}
class LinkedList<Key, Value> implements ST<Key, Value>, Iterable<LinkedList<Key, Value>.Entry>{
@Override
public Iterator<LinkedList<Key, Value>.Entry> iterator() {
return new LLIterator();
}
private class LLIterator implements Iterator<LinkedList<Key, Value>.Entry>{
private Node it = first;
@Override
public boolean hasNext() {
return it != null;
}
@Override
public Entry next() {
Node oldNode = it;
it = it.next;
return new Entry(oldNode.key, oldNode.val);
}
}
private class Node{
Node next, prev;
Key key;
Value val;
public Node(Key key, Value val, Node next, Node prev){
this.key = key; this.val = val; this.next = next; this.prev = prev;
}
}
public class Entry{
Key key;
Value val;
public Entry(Key key, Value val){
this.key = key;
this.val = val;
}
}
private Node first, last;
private int count;
public LinkedList(){
}
public void put(Key key, Value val){
if(count == 0){
first = new Node(key, val, null, null);
last = first;
count++;
} else{
Node x = getNode(key);
if(x != null){
x.val = val;
} else{
Node oldLast = last;
last = new Node(key, val, null, oldLast);
oldLast.next = last;
count++;
}
}
}
public void remove(Key key) {
Node x = getNode(key);
if(x != null){
if(count == 1){
first = last = null;
} else if(x == first){
first = first.next;
first.prev = null;
} else if(x == last){
last = last.prev;
last.next = null;
} else{
Node prev = x.prev;
Node next = x.next;
prev.next = next;
next.prev = prev;
}
count--;
}
}
private Node getNode(Key key){
for(Node it = first; it != null; it = it.next){
if(it.key.equals(key)) return it;
}
return null;
}
public Value get(Key key){
Node x = getNode(key);
if(x != null) return x.val;
return null;
}
public boolean isEmpty(){
return count == 0;
}
@Override
public boolean contains(Key key) {
Node x = getNode(key);
if(x != null) return true;
return false;
}
public int size(){
return count;
}
}