mirror of
https://codeberg.org/andyscott/HashMaps.git
synced 2024-12-21 12:33:10 -05:00
Implemented the put, empty_buckets, table_load, and clear methods
This commit is contained in:
parent
82a1c116dc
commit
b2ffb5a579
1 changed files with 44 additions and 14 deletions
|
@ -6,7 +6,7 @@
|
||||||
# Description: HashMap implementation using Separate Chaining
|
# Description: HashMap implementation using Separate Chaining
|
||||||
|
|
||||||
|
|
||||||
from a6_include import DynamicArray, LinkedList, hash_function_1, hash_function_2
|
from a6_include import DynamicArray, LinkedList, SLNode, hash_function_1, hash_function_2
|
||||||
|
|
||||||
|
|
||||||
class HashMap:
|
class HashMap:
|
||||||
|
@ -51,28 +51,58 @@ class HashMap:
|
||||||
# ------------------------------------------------------------------ #
|
# ------------------------------------------------------------------ #
|
||||||
|
|
||||||
def put(self, key: str, value: object) -> None:
|
def put(self, key: str, value: object) -> None:
|
||||||
|
"""Adds (or updates) a key/value pair to the hash map
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
key : str
|
||||||
|
Identifier for the given value
|
||||||
|
value : object
|
||||||
|
Value to add, or update if the key is already present
|
||||||
"""
|
"""
|
||||||
TODO: Write this implementation
|
|
||||||
"""
|
hash = self._hash_function(key)
|
||||||
pass
|
index = hash % self._capacity
|
||||||
|
node = self._buckets[index].contains(key)
|
||||||
|
if node is None:
|
||||||
|
self._buckets[index].insert(key, value)
|
||||||
|
self._size += 1
|
||||||
|
else:
|
||||||
|
node.value = value
|
||||||
|
|
||||||
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 index in range(self._capacity):
|
||||||
|
if self._buckets[index].length() == 0:
|
||||||
|
count += 1
|
||||||
|
return count
|
||||||
|
|
||||||
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 clear(self) -> None:
|
def clear(self) -> None:
|
||||||
"""
|
"""Clear the contents of the hash map without changing its capacity"""
|
||||||
TODO: Write this implementation
|
for index in range(self._capacity):
|
||||||
"""
|
if self._buckets[index].length() != 0:
|
||||||
pass
|
self._buckets[index] = LinkedList()
|
||||||
|
self._size = 0
|
||||||
|
|
||||||
|
|
||||||
def resize_table(self, new_capacity: int) -> None:
|
def resize_table(self, new_capacity: int) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue