forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapCuckooHashing.java
More file actions
269 lines (242 loc) · 8.62 KB
/
HashMapCuckooHashing.java
File metadata and controls
269 lines (242 loc) · 8.62 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
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 com.thealgorithms.datastructures.hashmap.hashing;
import java.util.Objects;
/**
* This class implements a hash table using Cuckoo Hashing.
* Cuckoo hashing is a type of open-addressing hash table that resolves collisions
* by relocating existing keys. It utilizes two hash functions to minimize collisions
* and automatically resizes the table when the load factor exceeds 0.7.
*
* For more information on cuckoo hashing, refer to
* <a href="https://en.wikipedia.org/wiki/Cuckoo_hashing">this Wikipedia page</a>.
*/
public class HashMapCuckooHashing {
private int tableSize; // Size of the hash table
private Integer[] buckets; // Array representing the hash table
private final Integer emptySlot; // Placeholder for deleted slots
private int size; // Number of elements in the hash table
private int thresh; // Threshold for detecting infinite loops during insertion
/**
* Constructs a HashMapCuckooHashing object with the specified initial table size.
*
* @param tableSize the initial size of the hash map
*/
public HashMapCuckooHashing(int tableSize) {
this.buckets = new Integer[tableSize];
this.tableSize = tableSize;
this.emptySlot = Integer.MIN_VALUE;
this.size = 0;
this.thresh = (int) (Math.log(tableSize) / Math.log(2)) + 2;
}
/**
* Computes the first hash index for a given key using the modulo operation.
*
* @param key the key for which the hash index is computed
* @return an integer index corresponding to the key
*/
public int hashFunction1(int key) {
int hash = key % tableSize;
if (hash < 0) {
hash += tableSize;
}
return hash;
}
/**
* Computes the second hash index for a given key using integer division.
*
* @param key the key for which the hash index is computed
* @return an integer index corresponding to the key
*/
public int hashFunction2(int key) {
int hash = key / tableSize;
hash %= tableSize;
if (hash < 0) {
hash += tableSize;
}
return hash;
}
/**
* Inserts a key into the hash table using cuckoo hashing.
* If the target bucket is occupied, it relocates the existing key and attempts to insert
* it into its alternate location. If the insertion process exceeds the threshold,
* the table is resized.
*
* @param key the key to be inserted into the hash table
* @throws IllegalArgumentException if the key already exists in the table
*/
public void insertKey2HashTable(int key) {
Integer wrappedInt = key;
Integer temp;
int hash;
int loopCounter = 0;
if (isFull()) {
System.out.println("Hash table is full, lengthening & rehashing table");
reHashTableIncreasesTableSize();
}
if (checkTableContainsKey(key)) {
throw new IllegalArgumentException("Key already exists; duplicates are not allowed.");
}
while (loopCounter <= thresh) {
loopCounter++;
hash = hashFunction1(key);
if ((buckets[hash] == null) || Objects.equals(buckets[hash], emptySlot)) {
buckets[hash] = wrappedInt;
size++;
checkLoadFactor();
return;
}
temp = buckets[hash];
buckets[hash] = wrappedInt;
wrappedInt = temp;
hash = hashFunction2(temp);
if (Objects.equals(buckets[hash], emptySlot)) {
buckets[hash] = wrappedInt;
size++;
checkLoadFactor();
return;
} else if (buckets[hash] == null) {
buckets[hash] = wrappedInt;
size++;
checkLoadFactor();
return;
}
temp = buckets[hash];
buckets[hash] = wrappedInt;
wrappedInt = temp;
}
System.out.println("Infinite loop occurred, lengthening & rehashing table");
reHashTableIncreasesTableSize();
insertKey2HashTable(key);
}
/**
* Rehashes the current table to a new size (double the current size) and reinserts existing keys.
*/
public void reHashTableIncreasesTableSize() {
HashMapCuckooHashing newT = new HashMapCuckooHashing(tableSize * 2);
for (int i = 0; i < tableSize; i++) {
if (buckets[i] != null && !Objects.equals(buckets[i], emptySlot)) {
newT.insertKey2HashTable(this.buckets[i]);
}
}
this.tableSize *= 2;
this.buckets = newT.buckets;
this.thresh = (int) (Math.log(tableSize) / Math.log(2)) + 2;
}
/**
* Deletes a key from the hash table, marking its position as available.
*
* @param key the key to be deleted from the hash table
* @throws IllegalArgumentException if the table is empty or if the key is not found
*/
public void deleteKeyFromHashTable(int key) {
Integer wrappedInt = key;
int hash = hashFunction1(key);
if (isEmpty()) {
throw new IllegalArgumentException("Table is empty, cannot delete.");
}
if (Objects.equals(buckets[hash], wrappedInt)) {
buckets[hash] = emptySlot;
size--;
return;
}
hash = hashFunction2(key);
if (Objects.equals(buckets[hash], wrappedInt)) {
buckets[hash] = emptySlot;
size--;
return;
}
throw new IllegalArgumentException("Key " + key + " not found in the table.");
}
/**
* Displays the hash table contents, bucket by bucket.
*/
public void displayHashtable() {
for (int i = 0; i < tableSize; i++) {
if ((buckets[i] == null) || Objects.equals(buckets[i], emptySlot)) {
System.out.println("Bucket " + i + ": Empty");
} else {
System.out.println("Bucket " + i + ": " + buckets[i].toString());
}
}
System.out.println();
}
/**
* Finds the index of a given key in the hash table.
*
* @param key the key to be found
* @return the index where the key is located
* @throws IllegalArgumentException if the table is empty or the key is not found
*/
public int findKeyInTable(int key) {
Integer wrappedInt = key;
int hash = hashFunction1(key);
if (isEmpty()) {
throw new IllegalArgumentException("Table is empty; cannot find keys.");
}
if (Objects.equals(buckets[hash], wrappedInt)) {
return hash;
}
hash = hashFunction2(key);
if (!Objects.equals(buckets[hash], wrappedInt)) {
throw new IllegalArgumentException("Key " + key + " not found in the table.");
} else {
return hash;
}
}
/**
* Checks if the given key is present in the hash table.
*
* @param key the key to be checked
* @return true if the key exists, false otherwise
*/
public boolean checkTableContainsKey(int key) {
return ((buckets[hashFunction1(key)] != null && buckets[hashFunction1(key)].equals(key)) || (buckets[hashFunction2(key)] != null && buckets[hashFunction2(key)].equals(key)));
}
/**
* Checks the load factor of the hash table. If the load factor exceeds 0.7,
* the table is resized to prevent further collisions.
*
* @return the current load factor of the hash table
*/
public double checkLoadFactor() {
double factor = (double) size / tableSize;
if (factor > .7) {
System.out.printf("Load factor is %.2f, rehashing table.%n", factor);
reHashTableIncreasesTableSize();
}
return factor;
}
/**
* Checks if the hash map is full.
*
* @return true if the hash map is full, false otherwise
*/
public boolean isFull() {
for (int i = 0; i < tableSize; i++) {
if (buckets[i] == null || Objects.equals(buckets[i], emptySlot)) {
return false;
}
}
return true;
}
/**
* Checks if the hash map is empty.
*
* @return true if the hash map is empty, false otherwise
*/
public boolean isEmpty() {
for (int i = 0; i < tableSize; i++) {
if (buckets[i] != null) {
return false;
}
}
return true;
}
/**
* Returns the current number of keys in the hash table.
*
* @return the number of keys present in the hash table
*/
public int getNumberOfKeysInTable() {
return size;
}
}