-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cuckoo.java
315 lines (275 loc) · 8.75 KB
/
Cuckoo.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
package rsn170330.sp07;
/**
* CS 5V81.001: Implementation of Data Structures and Algorithms
* Short Project SP07: Cuckoo Hashing Implementation
* @author Rahul Nalawade (rsn170330)
*
* Date: January 05, 2018
*/
public class Cuckoo<T> {
int k; // Number of Hash functions
int capacity; // = length of the Hash Table = hashTable.length
Entry<T>[][] hashTable; // Version with 1 table and k hash functions
// NOTE: each location (row) can have k spots/ cells (columns)
int size; // Number of actual elements in Hash Table
double loadFactor = 0.5; // open-addressing default
int threshold; // limit for no of replacements on collision
// Entry corresponding to an element in Hash Table
class Entry<E> {
E element;
public Entry(E element) {
this.element = element;
}
}
// Default Constructor
public Cuckoo() {
size = 0;
k = 3;
capacity = 1024;
hashTable = new Entry[capacity][k];
threshold = (int) Math.log((double) capacity);
}
// Code extracted from Java HashMap:
static int hash(int h) {
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
static int indexFor(int h, int length) {
return h & (length - 1);
}
/**
* Generates a Hash value for an i in {1, .., k}
*
* @param i the number of hash function from k hash functions.
* @param x the element for which hash value is to be computed
* @return hash value as an integer
*/
private int hashFunction(int i, T x) {
switch (i) {
case 1:
// Hash Function 1
return indexFor(hash(x.hashCode()), hashTable.length);
default:
// Hash Function 2, for i > 1
return (hashFunction(1,x) + i * (1 + x.hashCode() % 9)) & (hashTable.length - 1);
}
}
/**
* Adds the specified element to this set if it is not already present.
* @param x the element to be added
* @return true if successful insertion, false otherwise
*/
public boolean add(T x) {
double fraction = 0;
// Reject Duplicates
if (contains(x)) { return false; }
int i = 1;
int cell = i - 1;
int location = hashFunction(i, x);
// While we are able to find a free spot/ cell among k spots meant for x.
// just like finding/waiting for a full-time job from all applied job applications :P
while (i <= k) {
cell = i - 1;
location = hashFunction(i++, x);
// When the spot/ cell is free
if (hashTable[location][cell] == null) {
hashTable[location][cell] = new Entry<T>(x);
size++;
// Is load-factor reached?
fraction = (double) size / capacity;
if (loadFactor < fraction) {
rehash();
}
return true;
}
}
// When all k spots are occupied, replace our x with one of them and
// stop until everyone is inserted i.e. until no collision.
i = 1;
int count = 0;
while (count < threshold) {
count++; // threshold = log (capacity)
cell = i - 1;
location = hashFunction(i, x);
// When the spot is free
if (hashTable[location][cell] == null) {
hashTable[location][cell] = new Entry<T>(x);
size++;
// Is load-factor reached?
fraction = (double) size / capacity;
if (loadFactor < fraction) {
rehash();
}
return true;
}
// When you cannot insert x, replace it with it's place holder
else {
T temp = (T) hashTable[location][cell].element;
hashTable[location][cell].element = x;
x = temp; // Now try to insert new x (it's place holder)
}
i = (i == k) ? 1 : (i + 1);
}
// Too many steps (possible infinite loop).
// Rebuild hash table with new hash functions.
fraction = (double) size / capacity;
if (loadFactor < fraction) {
rehash();
}
return false;
}
/**
* If x is there is the Collection.
* @param x the input element
* @return true if present, false otherwise
*/
public boolean contains(T x) {
int i = 1;
int cell = 0;
int location = hashFunction(1, x);
while (i <= k) {
cell = i - 1;
location = hashFunction(i++, x);
if ((hashTable[location][cell] != null) &&
(x.equals(hashTable[location][cell].element))) {
return true;
}
}
return false;
}
/**
* Removes the specified element from this set if it is present.
* @param x the element to be removed
* @return true, if successfully removed, false otherwise
*/
public boolean remove(T x) {
int i = 1;
int cell = 0;
int location = hashFunction(1, x);
while (i <= k) {
cell = i - 1;
location = hashFunction(i++, x);
if ((hashTable[location][cell] != null) &&
(x.equals(hashTable[location][cell].element))) {
hashTable[location][cell] = null;
size--;
return true;
}
}
return false;
}
// Returns the number of elements in the table.
public int size() {
return size;
}
// Rehashing will double the table size, re-inserting the elements
private void rehash() {
Entry<T>[][] temp = hashTable;
size = 0; // as a new hash table is to be created
capacity = capacity * 2;
hashTable = new Entry[capacity][k];
threshold = (int) Math.log((double) capacity);
int location = 0;
int cell = 0;
Entry<T> e = null;
while (location < temp.length) {
cell = 0;
while (cell < k) {
e = temp[location][cell++];
if (e != null) {
add(e.element);
}
}
location++;
}
}
// Prints the Hash Table for Cuckoo Hashing with k = 2.
public void printHashTable2() {
System.out.println("\nHash Table: ");
System.out.format("%40s", "+--------------------------------------+\n");
System.out.format("%-11s%-14s%-13s%-2s", "| Location", "| Cell 1", "| Cell 2", " |\n");
System.out.println("|--------------------------------------|");
int location = 0;
while (location < hashTable.length) {
Entry<T> c1 = hashTable[location][0];
Entry<T> c2 = hashTable[location][1];
Integer nothing = null;
if (c1 != null) {
if (c2 != null) {
System.out.format("%-11s%-14s%-13s%-2s", "| "+ location, "| "
+ c1.element, "| " + c2.element, " |\n");
}
else {
System.out.format("%-11s%-14s%-13s%-2s", "| "+ location, "| "
+ c1.element, "| " + nothing, " |\n");
}
}
else {
if (c2 != null) {
System.out.format("%-11s%-14s%-13s%-2s", "| "+ location, "| "
+ nothing, "| " + c2.element, " |\n");
}
else {
System.out.format("%-11s%-14s%-13s%-2s", "| "+ location, "| "
+ nothing, "| " + nothing, " |\n");
}
}
location++;
}
System.out.format("%40s","+--------------------------------------+\n\n");
System.out.println("Size = " + size + " Capacity = " + capacity);
}
/**
* Calculate distinct elements in an array
* @param arr: Array of Integers which may or may not have duplicates.
* @return: returns the count of distinct elements in the provided array.
*/
public static<T> int distinctElements(T[] arr){
Cuckoo<T> dist = new Cuckoo<>();
for (T e : arr) { dist.add(e); }
return dist.size();
}
//-------------------------- MAIN METHOD ----------------------------------
public static void main(String[] args) {
Cuckoo<Integer> ch = new Cuckoo<>();
int N = 18;
//int[] num = {24, 20, 53, 1, 12, 0, 3, 24, 0, 45, 42, 30, 12, 50, 24, 49, 26, 17};
//int[] operation = {59, 33, 6, 11, 54, 2, 6, 97, 25, 73, 32, 18, 79, 19, 97, 22, 36, 60};
int[] num = {187, 121, 62, 166, 35, 43, 3, 24, 0, 45, 42, 30, 12, 50, 24, 49, 26, 17};
int[] operation = {20, 33, 6, 11, 54, 2, 6, 97, 25, 73, 32, 18, 79, 19, 97, 22, 36, 60};
System.out.println("Key \th1(x) \th2(x)");
for (int i = 0; i < N; i++) {
System.out.println(num[i] + "\t" + ch.hashFunction(1, num[i]) + "\t"
+ ch.hashFunction(2, num[i]));
}
System.out.println("\nOperations Timeline: ");
for (int i = 0; i < N; i++) {
/*
if (i == 5) {
ch.printHashTable2();
}
*/
if (operation[i] < 67) {
System.out.println("Add "+num[i]+" \t\t" + ch.add(num[i]) + " "
+ ch.size() + " " + ch.capacity);
}
else if (operation[i] < 84) {
System.out.println("Contains "+num[i]+" \t" + ch.contains(num[i]) + " "
+ ch.size() + " " + ch.capacity);
}
else {
System.out.println("Remove "+num[i]+" \t" + ch.remove(num[i]) + " "
+ ch.size() + " " + ch.capacity);
}
}
System.out.println("\n\nKey \th1(x) \th2(x)");
for (int i = 0; i < N; i++) {
System.out.println(num[i] + "\t" + ch.hashFunction(1, num[i]) + "\t"
+ ch.hashFunction(2, num[i]));
}
ch.printHashTable2();
}
}