mirror of
https://codeberg.org/andyscott/HashMaps.git
synced 2024-12-21 12:33:10 -05:00
Added initial code for put, table_load, empty_buckets, resize_table
This commit is contained in:
parent
d0caad7862
commit
9a53e5f27f
1 changed files with 63 additions and 11 deletions
|
@ -57,26 +57,78 @@ class HashMap:
|
||||||
"""
|
"""
|
||||||
# remember, if the load factor is greater than or equal to 0.5,
|
# remember, if the load factor is greater than or equal to 0.5,
|
||||||
# resize the table before putting the new key/value pair
|
# resize the table before putting the new key/value pair
|
||||||
pass
|
|
||||||
|
if self.table_load() >= 0.5:
|
||||||
|
self.resize_table(self._capacity * 2)
|
||||||
|
|
||||||
|
|
||||||
|
hash = self._hash_function(key)
|
||||||
|
index = hash % self._capacity
|
||||||
|
if self._buckets[index] is None:
|
||||||
|
self._buckets[index] = HashEntry(key, value)
|
||||||
|
|
||||||
|
elif self._buckets[index].key == key:
|
||||||
|
self._buckets[index].value = value
|
||||||
|
self._buckets[index].is_tombstone = False
|
||||||
|
|
||||||
|
self._size += 1
|
||||||
|
|
||||||
def table_load(self) -> float:
|
def table_load(self) -> float:
|
||||||
|
"""Get the current hash table load factor
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
float
|
||||||
|
The load factor
|
||||||
"""
|
"""
|
||||||
TODO: Write this implementation
|
|
||||||
"""
|
return self._size / self._capacity
|
||||||
pass
|
|
||||||
|
|
||||||
def empty_buckets(self) -> int:
|
def empty_buckets(self) -> int:
|
||||||
|
"""Gets the number of empty buckets in the hash table
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
int
|
||||||
|
Number of empty buckets
|
||||||
"""
|
"""
|
||||||
TODO: Write this implementation
|
|
||||||
"""
|
count = 0
|
||||||
pass
|
for i in range(self._capacity):
|
||||||
|
entry = self._buckets[i]
|
||||||
|
if entry is None or entry.is_tombstone:
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
|
|
||||||
|
|
||||||
def resize_table(self, new_capacity: int) -> None:
|
def resize_table(self, new_capacity: int) -> None:
|
||||||
|
"""Changes the capacity of the hash table
|
||||||
|
|
||||||
|
All existing key/value pairs remain and are rehashed. Does nothing if
|
||||||
|
the new capacity is less than 1.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
new_capacity : int
|
||||||
|
New capacity for the hash table
|
||||||
"""
|
"""
|
||||||
TODO: Write this implementation
|
|
||||||
"""
|
# immediately return if new_capacity is less than 1
|
||||||
# remember to rehash non-deleted entries into new table
|
if new_capacity < 1:
|
||||||
pass
|
return
|
||||||
|
# create new hash table if new_capacity is 1 or greater
|
||||||
|
new_table = DynamicArray()
|
||||||
|
for i in range(new_capacity):
|
||||||
|
new_table.append(None)
|
||||||
|
# rehash and move values from current to new hash table
|
||||||
|
for i in range(self._capacity):
|
||||||
|
if self._buckets[i] is not None:
|
||||||
|
hash = self._hash_function(self._buckets[i].key)
|
||||||
|
index = hash % new_capacity
|
||||||
|
new_table[index] = self._buckets[i]
|
||||||
|
# assign the new table and capacity to the existing HashMap object
|
||||||
|
self._buckets = new_table
|
||||||
|
self._capacity = new_capacity
|
||||||
|
|
||||||
def get(self, key: str) -> object:
|
def get(self, key: str) -> object:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue