-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableMap.java
More file actions
257 lines (219 loc) · 5.82 KB
/
HashTableMap.java
File metadata and controls
257 lines (219 loc) · 5.82 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
import java.util.NoSuchElementException;
// --== CS400 File Header Information ==--
// Name: Christopher Hall
// Email: cthall2@wisc.edu
// Team: Blue
// Group: JB
// TA: Xinyi
// Lecturer: Florian
// Notes to Grader:
public class HashTableMap<KeyType, ValueType> implements MapADT<KeyType, ValueType> {
private HashBST<KeyType, ValueType>[] pairs;
private int capacity1;
private int size;
/**
*
* @param capacity
*/
public HashTableMap(int capacity) {
// If capacity is negative
if (capacity < 0) {
throw new IllegalArgumentException();
}
this.pairs = new HashBST[capacity];
this.capacity1 = capacity;
this.size = 0;
}
public HashTableMap() {
this.pairs = new HashBST[10];
this.capacity1 = 10;
this.size = 0;
}
/**
* Return true if the value was successfully added to the array, false otherwise
*/
@Override
public boolean put(KeyType key, ValueType value) {
// If key or value are null, return false
if (key == null || containsKey(key)) {
return false;
}
HashBST<KeyType, ValueType> table = new HashBST<KeyType, ValueType>(key, value);
int index = Math.abs(key.hashCode()) % this.capacity1;
// Check if the index is occupied
if (this.pairs[index] == null) {
this.pairs[index] = table;
this.size++;
grow();
return true;
}
// Set table to head and set what was there to next
else {
HashBST<KeyType, ValueType> temp = this.pairs[index];
this.pairs[index] = table;
table.setNext(temp);
this.size++;
grow();
return true;
}
}
@Override
public ValueType get(KeyType key) throws NoSuchElementException {
// If table doesn't have the key
if (!containsKey(key)) {
throw new NoSuchElementException();
}
if (key == null) {
return null;
}
// Find the index
int index = Math.abs(key.hashCode()) % this.capacity1;
// Check if null
if (this.pairs[index] == null) {
return null;
}
// Current object
HashBST<KeyType, ValueType> current = this.pairs[index];
// If current equals key, return it
if (current.getKey().equals(key)) {
return current.getValue();
}
// Else, check the next objects in the list to see if key matches them
while (current.hasNext()) {
current = current.getNext();
if (current.getKey().equals(key)) {
return current.getValue();
}
}
return null;
}
@Override
public int size() {
return this.size;
}
/**
* If the table reaches 85% capacity or more, double the capacity size
*
* @return the new capacity of the table
*/
private void grow() {
// If the length is zero
if (!((double) size / this.capacity1 >= 0.85)) {
return;
}
HashBST<KeyType, ValueType>[] currentHash = new HashBST[this.size];
int counter = 0;
for (int i = 0; i < this.capacity1; ++i) {
if (this.pairs[i] != null) {
HashBST<KeyType, ValueType> currentObj = this.pairs[i];
currentHash[counter] = currentObj;
++counter;
while (currentObj.getNext() != null) {
currentObj = currentObj.getNext();
currentHash[counter] = currentObj;
++counter;
}
}
continue;
}
clear();
this.capacity1 *= 2;
this.pairs = new HashBST[this.capacity1];
for (int i = 0; i < currentHash.length; ++i) {
if (currentHash[i] == null) {
continue;
}
HashBST<KeyType, ValueType> newObjLoc = currentHash[i];
put(newObjLoc.getKey(), newObjLoc.getValue());
while (newObjLoc.getNext() != null) {
newObjLoc = newObjLoc.getNext();
put(newObjLoc.getKey(), newObjLoc.getValue());
}
}
int currCapacity = this.pairs.length;
double loadFactor = this.size / currCapacity;
// If size is equal to or greater than 85% of the capacity, double the capacity
if (loadFactor >= 0.85) {
currCapacity = this.pairs.length * 2;
this.capacity1 = currCapacity;
}
// Else, do nothing
}
/**
* Checks if the table contains the designated key value
*/
@Override
public boolean containsKey(KeyType key) {
// Find index
int index = Math.abs(key.hashCode()) % this.capacity1;
// Check if null
if (this.pairs[index] == null) {
return false;
}
// Current object
HashBST<KeyType, ValueType> current = this.pairs[index];
// If current equals key, return it
if (current.getKey().equals(key)) {
return true;
}
// Else, check the next objects in the list to see if key matches them
while (current.hasNext()) {
current = current.getNext();
if (current.getKey().equals(key)) {
return true;
}
}
return false;
}
/**
* Removes a key from the table. If the key is not found, return null.
*/
@Override
public ValueType remove(KeyType key) {
// If size is zero
if (this.size == 0) {
return null;
}
if (key.equals(null)) {
return null;
}
// If the table does not have the key
if (!containsKey(key)) {
return null;
}
// Find the index
int index = Math.abs(key.hashCode()) % this.capacity1;
// Check if null
if (this.pairs[index] == null) {
return null;
}
// Current object
HashBST<KeyType, ValueType> current = this.pairs[index];
// If current equals key, return it
if (current.getKey().equals(key)) {
this.pairs[index] = current.getNext();
--this.size;
return current.getValue();
}
while (current.getNext().hasNext()) {
if (current.getNext().getKey().equals(key)) {
HashBST<KeyType, ValueType> temp = current.getNext();
current.setNext(temp.getNext());
--this.size;
return temp.getValue();
}
current = current.getNext();
}
return null;
}
/**
* Empties the table and sets the size to zero
*/
@Override
public void clear() {
for (int i = 0; i < this.capacity1; ++i) {
this.pairs[i] = null;
}
this.size = 0;
}
}