28 lines
1.4 KiB
Python
28 lines
1.4 KiB
Python
from MyLinkedList import DerekLinkedList
|
|
|
|
class DerekHashTable:
|
|
def __init__(self) -> None:
|
|
self.backingStore = [DerekLinkedList() for _ in range(36)] # Create a linked list backing for the hash table
|
|
self.count = 0
|
|
|
|
def __iter__(self):
|
|
for linkedList in self.backingStore: # Look at each linked list
|
|
for data in linkedList: # Look at each item in the linked list
|
|
yield data # Return the data from each cell
|
|
|
|
def _calcHash(self, key) -> int: # Use the built in hash function
|
|
return hash(key) % 36 # Modulo the size of the hash array
|
|
|
|
def addItem(self, key, value):
|
|
i = self._calcHash(key) # Calculate a hash/modulo and put in the correct list
|
|
self.backingStore[i].append(key, value)
|
|
self.count += 1
|
|
|
|
def getItem(self, key):
|
|
i = self._calcHash(key) # Calculate the hash/modulo to get the data from the linked list
|
|
return self.backingStore[i].get(key)
|
|
|
|
def remItem(self, key):
|
|
i = self._calcHash(key) # Calculate the hash/modulo to get the list
|
|
self.backingStore[i].remove(key) # Remove the item from the list
|
|
self.count -= 1 |