-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashedDictionary.java
More file actions
383 lines (314 loc) · 9.7 KB
/
HashedDictionary.java
File metadata and controls
383 lines (314 loc) · 9.7 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
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
import java.util.Iterator;
import java.util.NoSuchElementException;
public class HashedDictionary<K,V> implements DictionaryInterface<K,V> {
private int numberOfEntries;
private int collisionCount;
private static final int DEFAULT_CAPACITY = 444;
private static final int MAX_CAPACITY = 10000;
private int tableSize;
private Entry<K,V>[] hashTable;
private boolean integrityOK = false;
protected final Entry<K,V> AVAILABLE = new Entry<K,V>(null, null);
public HashedDictionary() {
this(DEFAULT_CAPACITY);
}
public HashedDictionary(int initialCapacity) {
checkCapacity(initialCapacity);
numberOfEntries = 0;
collisionCount = 0;
tableSize = initialCapacity;
@SuppressWarnings("unchecked")
Entry<K,V>[] temp =(Entry<K,V>[]) new Entry[tableSize];
hashTable = temp;
integrityOK = true;
}
public int getCollisionCount() {
return collisionCount;
}
/**
* Retrieves the value associated with a given key.
*
* @param key The key to search for.
* @return The value associated with the key,
* or null if the key is not in the dictionary.
*/
@Override
public V getValue(K key) {
checkIntegrity();
if (key == null) {
throw new IllegalArgumentException("Cannot get value for null key");
}
int index = getHashIndex(key);
for (int counter = 0; counter < hashTable.length; counter++) {
if (hashTable[index] == null) {
return null;
}
if (hashTable[index] != AVAILABLE && key.equals(hashTable[index].getKey())) {
return hashTable[index].getValue();
}
index = (index + 1) % hashTable.length;
}
return null;
}
/**
* Adds a new entry to this dictionary. If the given key already exists,
* replaces the corresponding value and returns the original value.
*
* @param key The key of the entry to be added.
* @param value The value associated with the key.
* @return The value that was associated with the key before the new value
* was added, or null if the key was not in the dictionary.
*/
@Override
public V add(K key, V value) {
if ((key == null) || (value == null)) {
throw new IllegalArgumentException("'key' or 'value' is null. Not allowed!'");
}
int hashIndex = getHashIndex(key);
V oldValue = null;
boolean duplicateFound = false;
for (int index = getHashIndex(key); index < hashTable.length && !duplicateFound; index++) {
if (hashTable[index] != null && key.equals(hashTable[index].getKey())) {
duplicateFound = true;
}
}
// Check for collision and handle with linear probing if needed
if (hashTable[hashIndex] != null && ((!key.equals(hashTable[hashIndex].getKey()))) && !duplicateFound) {
hashIndex = linearProbe(hashIndex, key);
collisionCount++;
}
// Add new entry or update existing one
if ((hashTable[hashIndex] == null) && !duplicateFound) {
hashTable[hashIndex] = new Entry<K,V>(key, value);
numberOfEntries++;
} else {
oldValue = hashTable[hashIndex].getValue();
hashTable[hashIndex].setValue(value);
}
if (numberOfEntries >= hashTable.length * 0.75) {
enlargeHashTable();
}
return oldValue;
}
private int linearProbe(int index, K key) {
boolean found = false;
int availableStateIndex = -1; // Index of first element in the available state
while (!found && (hashTable[index] != null)) { // Search while we haven't found something or the hashTable hasn't come across an open spot
if (!hashTable[index].equals(AVAILABLE)) {
if (key.equals(hashTable[index].getKey())) {
found = true;
} else {
index = (index + 1) % hashTable.length;
}
} else {
if (availableStateIndex == -1) {
availableStateIndex = index;
}
index = (index + 1) % hashTable.length;
}
}
if (found || (availableStateIndex == -1)) {
return index; // Index of either key or null
} else {
return availableStateIndex; // Index of an available index
}
}
/**
* Creates an iterator that traverses all keys in this dictionary.
*
* @return An iterator that provides sequential access to the keys in the dictionary.
*/
@Override
public Iterator<K> getKeyIterator() {
return new KeyIterator();
}
/**
* Creates an iterator that traverses all values in this dictionary.
*
* @return An iterator that provides sequential access to the values in the dictionary.
*/
@Override
public Iterator<V> getValueIterator() {
return new ValueIterator();
}
/**
* Removes a specific entry from this dictionary.
*
* @param key The key of the entry to be removed.
* @return The value that was associated with the key,
* or null if the key was not in the dictionary.
*/
@Override
public V remove(K key) {
throw new UnsupportedOperationException("remove() method is not a supported operation.");
}
/**
* Determines whether a given key is in this dictionary.
*
* @param key The key to search for.
* @return True if the key is in the dictionary, false otherwise.
*/
@Override
public boolean contains(K key) {
throw new UnsupportedOperationException("contains() method is not a supported operation.");
}
/**
* Determines whether this dictionary is empty.
*
* @return True if the dictionary is empty, false otherwise.
*/
@Override
public boolean isEmpty() {
throw new UnsupportedOperationException("isEmpty() method is not a supported operation.");
}
/**
* Gets the number of entries in this dictionary.
*
* @return The number of entries in the dictionary.
*/
@Override
public int getSize() {
throw new UnsupportedOperationException("getSize() method is not a supported operation.");
}
/**
* Removes all entries from this dictionary.
*/
@Override
public void clear() {
throw new UnsupportedOperationException("clear() method is not a supported operation.");
}
private int getHashIndex(K key) {
int hashIndex = key.hashCode() % hashTable.length;
if (hashIndex < 0) {
hashIndex = hashIndex + hashTable.length;
}
return hashIndex;
}
private boolean isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i = i + 6) {
if (num % i == 0 || num % (i + 2) == 0)
return false;
}
return true;
}
private int getNextPrime(int num) {
if (num <= 1) {
return 2;
}
int prime = num;
boolean found = false;
while (!found) {
prime++;
if (isPrime(prime)) {
found = true;
}
}
return prime;
}
private void enlargeHashTable() {
Entry<K, V>[] oldTable = hashTable;
int oldSize = hashTable.length;
int newSize = getNextPrime(oldSize + oldSize);
checkCapacity(newSize);
// The cast is safe because the new array contains null entries.
@SuppressWarnings("unchecked")
Entry<K, V>[] temp = (Entry<K, V>[])new Entry[newSize];
hashTable = temp;
numberOfEntries = 0; // Reset number of dictionary entries, siunce it will be incremented by add during rehash
// Rehash dictionary entries from old array to the new and bigger array;
// skip elements that contain null or AVAILABLE
for (int index = 0; index < oldSize; index++) {
if ((oldTable[index] != null) && oldTable[index] != AVAILABLE) {
add(oldTable[index].getKey(), oldTable[index].getValue());
}
} // end for
} // end enlargeHashTable
private void checkCapacity(int initialCapacity) {
if (initialCapacity > MAX_CAPACITY) {
throw new IllegalStateException("Attemp to create a hash dictionary whoses capacity exceeds allowed maximum capacity of " + MAX_CAPACITY);
}
}
private void checkIntegrity(){
if(!integrityOK){
throw new SecurityException("bag is corrupted");
}
}
private class ValueIterator implements Iterator<V> {
private int currentIndex;
private int numberLeft;
private ValueIterator() {
currentIndex = 0;
numberLeft = numberOfEntries;
}
public boolean hasNext() {
return numberLeft > 0;
}
public V next() {
V result = null;
if (hasNext()) {
// Loop until we find occupied hash table entry
while (hashTable[currentIndex] == null || hashTable[currentIndex] == AVAILABLE) {
currentIndex++;
}
result = hashTable[currentIndex].getValue();
numberLeft--;
currentIndex++;
} else {
throw new NoSuchElementException();
}
return result;
}
public void remove() {
throw new UnsupportedOperationException("remove is not supported by this iterator");
}
}
private class KeyIterator implements Iterator<K> {
private int currentIndex;
private int numberLeft;
private KeyIterator() {
currentIndex = 0;
numberLeft = numberOfEntries;
}
public boolean hasNext() {
return numberLeft > 0;
}
public K next() {
K result = null;
if (hasNext()) {
// Loop until we find occupied hash table entry
while (hashTable[currentIndex] == null || hashTable[currentIndex] == AVAILABLE) {
currentIndex++;
}
result = hashTable[currentIndex].getKey();
numberLeft--;
currentIndex++;
} else {
throw new NoSuchElementException();
}
return result;
}
public void remove() {
throw new UnsupportedOperationException("remove is not supported by this iterator");
}
}
protected final class Entry<KE,VE> {
private KE key;
private VE value;
public Entry(KE key, VE value) {
this.key = key;
this.value = value;
}
public KE getKey() {
return key;
}
public VE getValue() {
return value;
}
public void setValue(VE value) {
this.value = value;
}
}
}