Skip to content

Commit f1f5f89

Browse files
committed
HashTable: Made sure put halts if the table is full (throws ValueError)
1 parent ea92ba4 commit f1f5f89

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

map/hashtable.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, size=11):
2020
self._values = [self._empty] * size # values
2121

2222
def put(self, key, value):
23-
hash_ = self.hash(key)
23+
initial_hash = hash_ = self.hash(key)
2424

2525
while True:
2626
if self._keys[hash_] is self._empty or self._keys[hash_] is self._deleted:
@@ -36,6 +36,10 @@ def put(self, key, value):
3636

3737
hash_ = self.rehash(hash_)
3838

39+
if initial_hash == hash_:
40+
# table is full
41+
raise ValueError("Table is full")
42+
3943
def get(self, key):
4044
initial_hash = hash_ = self.hash(key)
4145
while True:
@@ -126,3 +130,11 @@ def test_delete_key_and_reassign(self):
126130
m.del_(1)
127131
m.put(1, 2)
128132
self.assertEqual(2, m.get(1))
133+
134+
def test_assigning_to_full_table_throws_error(self):
135+
m = HashTable(3)
136+
m.put(1, 1)
137+
m.put(2, 2)
138+
m.put(3, 3)
139+
with self.assertRaises(ValueError):
140+
m.put(4, 4)

0 commit comments

Comments
 (0)