-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_map_oa.py
314 lines (283 loc) · 10 KB
/
hash_map_oa.py
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
# Name: Christian Castro
# OSU Email: [email protected]
# Course: CS261 - Data Structures
# Assignment: 6
# Due Date: 03/11/2022
# Description: Hash Map ADT implementation using open addressing.
from a6_include import *
class HashEntry:
def __init__(self, key: str, value: object):
"""
Initializes an entry for use in a hash map
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.key = key
self.value = value
self.is_tombstone = False
def __str__(self):
"""
Overrides object's string method
Return content of hash map t in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
return f"K: {self.key} V: {self.value} TS: {self.is_tombstone}"
def hash_function_1(key: str) -> int:
"""
Sample Hash function #1 to be used with HashMap implementation
DO NOT CHANGE THIS FUNCTION IN ANY WAY
"""
hash = 0
for letter in key:
hash += ord(letter)
return hash
def hash_function_2(key: str) -> int:
"""
Sample Hash function #2 to be used with HashMap implementation
DO NOT CHANGE THIS FUNCTION IN ANY WAY
"""
hash, index = 0, 0
index = 0
for letter in key:
hash += (index + 1) * ord(letter)
index += 1
return hash
class HashMap:
def __init__(self, capacity: int, function) -> None:
"""
Initialize new HashMap that uses Quadratic Probing for collision resolution
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.buckets = DynamicArray()
for _ in range(capacity):
self.buckets.append(None)
self.capacity = capacity
self.hash_function = function
self.size = 0
def __str__(self) -> str:
"""
Overrides object's string method
Return content of hash map in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = ''
for i in range(self.buckets.length()):
out += str(i) + ': ' + str(self.buckets[i]) + '\n'
return out
def clear(self) -> None:
"""
Class HashMap: This class method is used to clear
a HashMap's key-value pairs without changing the
underlying storage capacity.
:return: {None}
"""
# simply iterate through the length of the
# bucket array and set to None.
for i in range(self.buckets.length()):
self.buckets[i] = None
self.size = 0
return None
def get(self, key: str) -> object:
"""
Class HashMap: This class method is used to get the
value associated with the input parameter "key".
:param key: {str}
:return: {object}
"""
# calculate hashed index
hash = self.hash_function(key)
length = self.buckets.length()
index = hash % length
# if no key-val found, returns None
if self.buckets[index] is None:
return None
else:
# else we have a key-val pair at index and now
# we check that index for matching key. If key
# found at index does not match, we search for
# the key using quadratic probing.
j = 0
i = (index + j**2) % length
while self.buckets[i] is not None:
if self.buckets[i].key == key and not self.buckets[i].is_tombstone:
return self.buckets[i].value
j += 1
i = (index + j**2) % length
return None
def put(self, key: str, value: object) -> None:
"""
Class HashMap: This class method is used to put
a new key-value pair into the HashMap.
:param key: {str}
:param value: {object}
:return: {None}
"""
# resizes table if load factor is too large.
if self.table_load() >= 0.5:
self.resize_table(self.capacity*2)
# computes hashed index for key
hash = self.hash_function(key)
length = self.buckets.length()
index = hash % length
# if no pre-existing key-value pair is found, we
# set bucket[index] to new Hash entry.
if self.buckets[index] is None:
self.size += 1
self.buckets[index] = HashEntry(key, value)
return None
else:
# else we have a collision so we use quadratic
# probing until we find appropriate placement.
j = 0
i = (index + j**2) % length
while self.buckets[i] is not None:
if self.buckets[i].is_tombstone:
self.size += 1
self.buckets[i] = HashEntry(key, value)
return None
elif self.buckets[i].key == key:
self.buckets[i].value = value
return None
j += 1
i = (index + j**2) % length
self.size += 1
self.buckets[i] = HashEntry(key, value)
return None
def remove(self, key: str) -> None:
"""
Class HashMap: This class method is used to
remove a key-value pair from the HashMap.
:param key: {str}
:return: {None}
"""
# computes hashed index
hash = self.hash_function(key)
length = self.buckets.length()
index = hash % length
# if no key-val pair is found, we return None
if self.buckets[index] is None:
return None
else:
# else, we begin searching for a key match so
# we can delete. (Using quadratic probing)
j = 0
i = (index + j**2) % length
while self.buckets[i] is not None:
if self.buckets[i].key == key:
if not self.buckets[i].is_tombstone:
self.buckets[i].is_tombstone = True
self.size -= 1
return None
j += 1
i = (index + j**2) % length
return None
def contains_key(self, key: str) -> bool:
"""
Class HashMap: This class method is used to
determine whether the HashMap contains the
key-value pair.
:param key: {str}
:return: {bool}
"""
if self.size < 1:
return False
# computes hashed index for key
hash = self.hash_function(key)
length = self.buckets.length()
index = hash % length
# returns false if no key-val pair is found at index
if self.buckets[index] is None:
return False
else:
# else, we begin searching for a key match
# (Using quadratic probing)
j = 0
i = (index + j**2) % length
while self.buckets[i] is not None:
if self.buckets[i].key == key:
if not self.buckets[i].is_tombstone:
return True
else:
return False
j += 1
i = (index + j**2) % length
return False
def empty_buckets(self) -> int:
"""
Class HashMap: This class method is used to
determine the number of empty buckets in the
HashMap.
:return empty_count: {int}
"""
# iterates through the array and counts empty
# buckets and tombstone key-val pairs.
empty_count = 0
for i in range(self.buckets.length()):
if self.buckets[i] is None:
empty_count += 1
elif self.buckets[i] is not None:
if self.buckets[i].is_tombstone:
empty_count += 1
return empty_count
def table_load(self) -> float:
"""
Class HashMap: This class method is used to
determine the load factor of the HashMap table.
:return: {float}
"""
# returns load factor
return self.size / self.buckets.length()
def resize_table(self, new_capacity: int) -> None:
"""
Class HashMap: This class method is used to
resize a Hash table.
:param new_capacity: {int}
:return: {None}
"""
if new_capacity < 1 or new_capacity < self.size:
return None
# using a helper function, we can rehash our key-value pairs into
# a new hash map then copy the desired attributes over to self.
new_hash = rehash(self.buckets, new_capacity, self.hash_function)
self.buckets = new_hash.buckets
self.capacity = new_hash.capacity
return None
def get_keys(self) -> DynamicArray:
"""
Class HashMap: This class method is used to
get the keys which are contained in the Hash
table.
:return keys: {DynamicArray}
"""
# iterates through the array and appends non-tombstone
# and non-none keys to return DA.
keys = DynamicArray()
for i in range(self.buckets.length()):
if self.buckets[i] is not None:
if not self.buckets[i].is_tombstone:
keys.append(self.buckets[i].key)
return keys
def rehash(old_buckets, new_capacity, hash_func):
"""
Helper Function For resize(): this function is used
to rehash keys to a new HashMap.
:param old_buckets: {DynamicArray}
:param new_capacity: {int}
:hash_func: {function}
"""
# creates new hashmap with desired capacity and then
# iterates through the input hash table to rehash keys
hash_map = HashMap(new_capacity, hash_func)
for i in range(old_buckets.length()):
if old_buckets[i] is not None:
if not old_buckets[i].is_tombstone:
hash_map.put(old_buckets[i].key, old_buckets[i].value)
return hash_map
def peek_array(HashMap: object) -> None:
"""
Helper method for debugging: Prints out the
key-value pairs in human readable form through
iteration.
:param HashMap: {HashMap}
"""
for i in range(HashMap.buckets.length()):
print(HashMap.buckets[i].__str__())