Skip to content

Commit

Permalink
HashTable: Made sure put halts if the table is full (throws ValueError)
Browse files Browse the repository at this point in the history
  • Loading branch information
orenovadia committed May 5, 2017
1 parent ea92ba4 commit f1f5f89
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion map/hashtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, size=11):
self._values = [self._empty] * size # values

def put(self, key, value):
hash_ = self.hash(key)
initial_hash = hash_ = self.hash(key)

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

hash_ = self.rehash(hash_)

if initial_hash == hash_:
# table is full
raise ValueError("Table is full")

def get(self, key):
initial_hash = hash_ = self.hash(key)
while True:
Expand Down Expand Up @@ -126,3 +130,11 @@ def test_delete_key_and_reassign(self):
m.del_(1)
m.put(1, 2)
self.assertEqual(2, m.get(1))

def test_assigning_to_full_table_throws_error(self):
m = HashTable(3)
m.put(1, 1)
m.put(2, 2)
m.put(3, 3)
with self.assertRaises(ValueError):
m.put(4, 4)

0 comments on commit f1f5f89

Please sign in to comment.