/
HashTableOpenAddressing.java
315 lines (263 loc) · 11.3 KB
/
HashTableOpenAddressing.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 cmsc256;
import java.util.Iterator;
import java.util.NoSuchElementException;
public abstract class HashTableOpenAddressing<K, V> implements MapInterface<K, V> {
protected int numEntries;
protected static final int DEFAULT_CAPACITY = 27;
protected static final int MAX_CAPACITY = 10000;
protected Entry<K, V>[] table;
protected double loadFactor;
protected static final double DEFAULT_LOAD_FACTOR = 0.5;
//****************************TableEntry**************************
public static class Entry<K, V> {
private K key;
private V value;
private States state; // Flag for the state of Entry in the table
private enum States {CURRENT, REMOVED} // Possible values of state, null is empty
public Entry(K key, V value) {
this.key = key;
this.value = value;
state = States.CURRENT;
}
protected K getKey() {
return key;
}
protected V getValue() {
return value;
}
protected void setValue(V newValue) {
value = newValue;
}
// Returns true if this entry is currently in the hash table.
protected boolean isIn() {
return state == States.CURRENT;
}
// Returns true if this entry has been removed from the hash table.
protected boolean isRemoved() {
return state == States.REMOVED;
}
// Sets the state of this entry to removed.
protected void setToRemoved() {
// Entry not in use, deleted from table and set State to REMOVED
state = States.REMOVED;
}
public String toString() {
return "Key-" + key + ": Value-" + value;
}
}
//****************************TableEntry**************************
// Constructors
public HashTableOpenAddressing() {
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR);
}
public HashTableOpenAddressing(int initialCapacity, double loadFactorIn) {
numEntries = 0;
if (loadFactorIn <= 0 || initialCapacity <= 0) {
throw new IllegalArgumentException("Initial capacity and load " +
"factor must be greater than 0");
}
else if (initialCapacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a dictionary " +
"whose capacity is larger than " + MAX_CAPACITY);
loadFactor = loadFactorIn;
// Set up hash table:
// Initial size of hash table is same as initialCapacity if it is prime;
// otherwise increase it until it is prime size
int tableSize = getNextPrime(initialCapacity);
@SuppressWarnings("unchecked")
Entry<K, V>[] temp = (Entry<K, V>[]) new Entry[tableSize];
table = temp;
}
// Method to expand the hash table array as needed
protected void enlargeHashTable() {
Entry<K, V>[] oldTable = table;
int capacity = getNextPrime(oldTable.length * 2);
// The case is safe because the new array contains null entries
@SuppressWarnings("unchecked")
Entry<K, V>[] temp = (Entry<K, V>[]) new Entry[capacity];
table = temp;
numEntries = 0;
// Rehash dictionary entries from old array to the new
for (int index = 0; index < oldTable.length; index++) {
if ((oldTable[index] != null) && oldTable[index].isIn())
put(oldTable[index].getKey(), oldTable[index].getValue());
}
}
// Returns a prime integer that is >= the given integer.
private int getNextPrime(int integer) {
// if even, add 1 to make odd
if (integer % 2 == 0) {
integer++;
}
// test odd integers
while (!isPrime(integer)) {
integer = integer + 2;
}
return integer;
}
// Returns true if the given integer is prime.
private boolean isPrime(int integer) {
boolean result;
boolean done = false;
// 1 and even numbers are not prime
if ((integer == 1) || (integer % 2 == 0)) {
result = false;
}
// 2 and 3 are prime
else if ((integer == 2) || (integer == 3)) {
result = true;
}
else { // integer is odd and >= 5
result = true; // assume prime
for (int divisor = 3; !done && (divisor * divisor <= integer); divisor = divisor + 2) {
if (integer % divisor == 0) {
result = false; // divisible; not prime
done = true;
}
}
}
return result;
}
/** Task: Gets the size of the dictionary.
* @return the number of entries (key-value pairs) currently
* in the dictionary
*/
@Override
public int getSize() {
return numEntries;
}
/** Task: Sees whether the dictionary is empty.
* @return true if the dictionary is empty
*/
@Override
public boolean isEmpty() {
return numEntries == 0;
}
/** Task: Sees whether the dictionary is full.
* @return true if the dictionary the number of elements
* stored in the hash table is greater than the load factor will
* allow for this hash table
*/
@Override
public boolean isFull() {
return numEntries > table.length * loadFactor;
}
/** Task: Removes all entries from the dictionary. */
@Override
public void clear() {
@SuppressWarnings("unchecked")
Entry<K, V>[] temp = (Entry<K, V>[]) new Entry[table.length];
table = temp;
numEntries = 0;
}
protected int getHashIndex(K key) {
int hashIndex = Math.abs(key.hashCode() % 10) % table.length;
return hashIndex;
}
public String toString() {
String result = "";
for(int i = 0; i < table.length; i++) {
result += i + " ";
if(table[i] == null)
result += "null\n";
else{
if(table[i].isRemoved() )
result += "has been set to \"removed\"\n";
else
result += table[i].getKey() + " " + table[i].getValue() + "\n";
}
}
return result;
}
@Override
public Iterator<K> getKeyIterator() {
return new KeyIterator();
}
@Override
public Iterator<V> getValueIterator() {
return new ValueIterator();
}
//****************************KeyIterator**************************
private class KeyIterator implements Iterator<K>{
private int currentIndex; // Current position in hash table
private int numberLeft; // Number of entries left in iteration
private KeyIterator() {
currentIndex = 0;
numberLeft = numEntries;
}
public boolean hasNext() {
return numberLeft > 0;
}
public K next() {
K result = null;
if (hasNext()) {
// Skip table locations that do not contain a current entry
while ((table[currentIndex] == null)
|| table[currentIndex].isRemoved()){
currentIndex++;
}
result = table[currentIndex].getKey();
numberLeft--;
currentIndex++;
}
else
throw new NoSuchElementException();
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
//****************************ValueIterator**************************
private class ValueIterator implements Iterator<V> {
private int currentIndex; // Current position in hash table
private int numberLeft; // Number of entries left in iteration
private ValueIterator() {
currentIndex = 0;
numberLeft = numEntries;
}
public boolean hasNext() {
return numberLeft > 0;
}
public V next() {
V result = null;
if (hasNext()) {
// Skip table locations that do not contain a current entry
while ((table[currentIndex] == null)
|| table[currentIndex].isRemoved()){
currentIndex++;
}
result = table[currentIndex].getValue();
numberLeft--;
currentIndex++;
}
else
throw new NoSuchElementException();
return result;
}
}
/** Task: Adds a new entry to the dictionary. If the given search
* key already exists in the dictionary, replaces the
* corresponding value.
* @param key an object search key of the new entry
* @param value an object associated with the search key
* @return either null if the new entry was added to the dictionary
* or the value that was associated with key if that value
* was replaced*/
public abstract V put(final K key, final V value);
/** Task: Removes a specific entry from the dictionary.
* @param key an object search key of the entry to be removed
* @return either the value that was associated with the search key
* or null if no such object exists*/
public abstract V remove(final K key);
/** Task: Retrieves the value associated with a given search key.
* @param key an object search key of the entry to be retrieved
* @return either the value that is associated with the search key
* or null if no such object exists */
public abstract V getValue(final K key);
/** Task: Sees whether a specific entry is in the dictionary.
* @param key an object search key of the desired entry
* @return true if key is associated with an entry in the
* dictionary */
public abstract boolean contains(final K key);
}