-
Notifications
You must be signed in to change notification settings - Fork 3
/
DoubleHashing.java
269 lines (227 loc) · 6.26 KB
/
DoubleHashing.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
package rsn170330.sp07;
import java.util.Iterator;
/**
* CS 5V81.001: Implementation of Data Structures and Algorithms
* Short Project SP07: Double Hashing Implementation
* Team SP43:
* @author Rahul Nalawade (rsn170330)
*
* Date: October 21, 2018
*/
public class DoubleHashing<T> {
private int size;
Entry<T>[] table;
private double loadFactor = 0.5; // default for open-addressing
int capacity;
static class Entry<T>{
T element;
boolean isDeleted;
public Entry(T x){
this.element = x;
this.isDeleted = false;
}
}
public DoubleHashing() {
capacity = 1024;
table = new Entry[1024];
size = 0;
}
public DoubleHashing(int initialSize) {
capacity = initialSize;
table = new Entry[initialSize];
size = 0;
}
/**
* TODO: This might look to give random sequence
* but, we will give in the sequence of our table
*/
public Iterator<T> iterator() {
return null;
}
/**
* Code extracted from Java HashMap.
* 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).
* @param h input number
* @return the hash value
*/
static int hash(int h){
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ ( h >>> 7) ^ (h >>> 4);
}
/**
* Returns appropriate index where key is to be stored
* @param h a (hash?) value
* @param length
* @return index in hash table
*/
static int indexFor(int h, int length) {
// length = table.length is a power of 2
return h & (length - 1);
}
/*
* hashCode(): implemented by converting the internal address of
* the object into an integer
* Key is stored at table[hash( x.hashCode() ) & ( table.length − 1 ) ]
*/
private int h(T x) {
return indexFor(hash(x.hashCode()), table.length);
}
/**
* Returns single digit non-zero number
* @param x the input
* @return the hashDigit
*/
private int h2(T x) {
return 1 + x.hashCode() % 9;
}
/**
* Search for x and return index of x.
* If x is not found, return index where x can be added.
* @param x the element
* @return index of x
*/
private int find(T x) {
int index = 0;
int k = 0;
while (true) {
// update index as per Double Hashing algorithm
index = (h(x) + k * h2(x)) % table.length;
// When the index is freshly available or there is already x at that index
if (table[index] == null || x.equals(table[index].element))
return index;
// When the index is available because of some other element was removed
else if (table[index].isDeleted)
break;
// When the index is unavailable
else
k++;
}
// We know we got an previously occupied place
int xSpot = index;
/* What if our x is in further probe sequence?
* So, finding if x is in next probe sequences,
* If present return that index.
* If freshly unoccupied spot found, x was never there.
*/
while (table[index] != null) {
// updating index for next index in probe sequence
k++;
index = (h(x) + k * h2(x)) % table.length;
// When we found freshly available index
if (table[index] == null)
return xSpot;
// When x was there, return that index
if (x.equals(table[index].element))
return index;
}
// avoiding NPE**
return xSpot; // so returning xSpot
}
/**
* If x is there is the Collection.
* @param x the input element
* @return true if present, false otherwise
*/
public boolean contains(T x) {
int location = find(x);
// When that location is not NULL AND the elements match AND it is not deleted
return (table[location] != null && x.equals(table[location].element) && !table[location].isDeleted);
}
/**
* 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) {
int location = find(x);
// When that location is not NULL AND the elements match AND it is not deleted
if (table[location] != null && x.equals(table[location].element) && !table[location].isDeleted)
return false;
// Now we need to actually add x.
else {
// We will create a new Entry for x.
Entry<T> newEntry = new Entry(x);
table[location] = newEntry;
size++;
if (size > loadFactor * table.length) {
resize();
}
return true;
}
}
/**
* 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 location = find(x);
// When that location is not NULL AND the elements match AND it is not deleted
if (table[location] != null && x.equals(table[location].element) && !table[location].isDeleted) {
//T result = table[location].element;
table[location].isDeleted = true;
size--;
return true;
}
return false;
}
// Returns current size
public int size() {
return size;
}
// Returns true if this set contains no elements.
public boolean isEmpty() {
return size() == 0;
}
/**
* Re-builds Hash Table with new length
* as twice the previous length
*/
private void resize() {
Entry<T>[] temp = table;
capacity = capacity * 2;
table = new Entry[capacity];
size = 0;
for (Entry<T> e: temp) {
if (e != null && !e.isDeleted) {
this.add( e.element);
}
}
}
// Debugging
private void printer(){
if (size == 0) {
System.out.println("HashTable is empty");
}
else {
for (int i = 0; i < table.length; i++) {
if(table[i] != null && !table[i].isDeleted){
System.out.print(i + ":" + table[i].element + " ");
}
}
}
}
// Debugging
private void printTable() {
System.out.print("Table: ");
for (int j = 0; j < table.length; j++) {
if (table[j] != null && table[j].isDeleted == false)
System.out.print(table[j].element + " ");
else
System.out.print("* ");
}
System.out.println();
}
/**
* 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){
DoubleHashing<T> dist = new DoubleHashing<>();
for (T e : arr) { dist.add(e); }
return dist.size();
}
}