-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hashing.java
328 lines (268 loc) · 8.41 KB
/
Hashing.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
package sample;
// Sukhamrit Singh
// Hashing
/*
The program has a new concrete class that implements MyMap (Listing 27.1)
using open addressing with quadratic probing. For simplicity, I used
f(key) = key % size as the hash function, where size is the hash-table size.
Initially, the hash-table size is 4. The table size is doubled whenever
the load factor exceeds the threshold (0.5). I wrote a driver program
to test your class.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Hashing {
public static void main(String[] args) {
// Creating a map and putting keys and values
MyMap<String, Integer> map = new MyHashMap<>();
map.put("Sukhamrit", 25);
map.put("Bob", 27);
map.put("Joe", 20);
map.put("Josh", 22);
map.put("Messi", 21);
map.put("Virgil", 20);
map.put("Andre", 30);
map.put("Luis", 21);
map.put("Felix", 54);
map.put("Angela", 97);
// Printing out the number of entries in the map
System.out.println("Entries in map: " + map);
// Printing value for Messi
System.out.println("The age of Messi is " +
map.get("Messi"));
// Checking is alex is contained in map
System.out.println("Is Alex in the map? " +
map.containsKey("Alex"));
// Checking if any keys have value of 29
System.out.println("Is age 29 in the map? " +
map.containsValue(29));
// Printing out all keys in map
System.out.print("Keys in map: ");
for (String key : map.keySet()) {
System.out.print(key + " ");
}
System.out.println();
// Printing out all values in map
System.out.print("Values in map: ");
for (int value : map.values()) {
System.out.print(value + " ");
}
System.out.println();
// Removing key Luis
map.remove("Luis");
System.out.println("Entries in map " + map);
}
}
interface MyMap<K, V> {
/** Remove all of the entries from this map */
public void clear();
/** Return true if the specified key is in the map */
public boolean containsKey(K key);
/** Return true if this map contains the specified value */
public boolean containsValue(V value);
/** Return a set of entries in the map */
public java.util.Set<Entry<K, V>> entrySet();
/** Return the first value that matches the specified key */
public V get(K key);
/** Return true if this map contains no entries */
public boolean isEmpty();
/** Return a set consisting of the keys in this map */
public java.util.Set<K> keySet();
/** Add an entry (key, value) into the map */
public V put(K key, V value);
/** Remove the entries for the specified key */
public Entry<K, V> remove(K key);
/** Return the number of mappings in this map */
public int size();
/** Return a set consisting of the values in this map */
public java.util.Set<V> values();
/** Define inner class for Entry */
public static class Entry<K, V> {
K key;
V value;
public Entry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
@Override
public String toString() {
return "[" + key + ", " + value + "]";
}
}
}
class MyHashMap<K, V> implements MyMap<K, V> {
// Define the default hash-table size. Must be a power of 2
private static int DEFAULT_INITIAL_CAPACITY = 4;
private static int MAXIMUM_CAPACITY = 1 << 30;
private static float DEFAULT_MAX_LOAD_FACTOR = 0.5f;
Entry<K, V>[] table;
private int capacity;
private float loadFactorThreshold;
private int size = 0;
public MyHashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_MAX_LOAD_FACTOR);
}
public MyHashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_MAX_LOAD_FACTOR);
}
public MyHashMap(int initialCapacity, float loadFactorThreshold) {
if (initialCapacity > MAXIMUM_CAPACITY)
this.capacity = MAXIMUM_CAPACITY;
else
this.capacity = trimToPowerOf2(initialCapacity);
this.loadFactorThreshold = loadFactorThreshold;
table = new Entry[capacity];
}
private static int supplementalHash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
/**
* Return a power of 2 for initialCapacity
*/
private int trimToPowerOf2(int initialCapacity) {
int capacity = 1;
while (capacity < initialCapacity) {
capacity <<= 1; // Same as capacity *= 2. <= is more efficient
}
return capacity;
}
@Override
public void clear() {
size = 0;
for (int i = 0; i < capacity; i++) {
if (table[i] != null) {
table[i] = null;
}
}
}
@Override
/** Return true if this map contains the value */
public boolean containsKey(K key) {
return get(key) != null;
}
@Override
public boolean containsValue(V value) {
for (int i = 0; i < capacity; i++) {
if (table[i] != null) {
if (table[i].getValue().equals(value)) {
return true;
}
}
}
return false;
}
@Override
public Set<Entry<K, V>> entrySet() {
HashSet<Entry<K, V>> set = new HashSet<>();
for (Entry<K, V> entry : table) {
if (entry != null) {
set.add(entry);
}
}
return set;
}
/**
* Hash function
*/
private int hash(int hashCode) {
return supplementalHash(hashCode) & (capacity - 1);
}
@Override
public V get(K key) {
int hash = hash(key.hashCode()) % capacity;
int n = 0;
while (table[hash] != null && !table[hash].getKey().equals(key)) {
hash = (hash + n * n) % capacity;
n++;
}
if (table[hash] == null) {
return null;
}
return table[hash].getValue();
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Set<K> keySet() {
HashSet<K> set = new HashSet<>();
for (Entry<K, V> entry : table) {
if (entry != null) {
set.add(entry.key);
}
}
return set;
}
@Override
public V put(K key, V value) {
if (((double) size) / capacity >= loadFactorThreshold) {
ArrayList<Entry<K, V>> items = new ArrayList<>();
for (K key_ : keySet()) {
Entry<K, V> entry = new Entry<>(key_, get(key_));
items.add(entry);
remove(key);
}
capacity *= 2;
table = new Entry[capacity];
for (Entry<K, V> item : items) {
put(item.getKey(), item.getValue());
}
}
int hash = hash(key.hashCode()) % capacity;
int n = 0;
while (table[hash] != null && table[hash].getKey() != key) {
hash = (hash + n * n) % capacity;
n++;
}
table[hash] = new Entry<>(key, value);
size++;
return table[hash].getValue();
}
@Override
public Entry<K, V> remove(K key) {
int hash = hash(key.hashCode()) % capacity;
int n = 0;
while (table[hash] != null && !table[hash].getKey().equals(key)) {
hash = (hash + n * n) % capacity;
n++;
}
Entry<K, V> item = table[hash];
table[hash] = null;
size--;
return item;
}
@Override
public int size() {
return size;
}
@Override
public Set<V> values() {
HashSet<V> set = new HashSet<>();
for (Entry<K, V> entry : table) {
if (entry != null) {
set.add(entry.value);
}
}
return set;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < capacity; i++) {
if (table[i] != null) {
builder.append(table[i]);
builder.append(" ");
}
}
return builder.toString();
}
}