Corrects the implementation of add. It is not possible any more to create use twice the same key.

This commit is contained in:
julien lengrand-lambert
2013-11-26 19:10:08 +01:00
parent 0267a97f33
commit b7364b40da
2 changed files with 7 additions and 6 deletions

View File

@@ -149,6 +149,9 @@ class HMTableCollision(HashMap):
if self.hmap[my_key] == None:
self.hmap[my_key] = [item]
else:
# we check if same key already exists
if [key in item.k for item in self.hmap[my_key]]:
raise Exception("This key already exists!")
self.hmap[my_key].append(item)
self._size += 1

View File

@@ -51,7 +51,9 @@ class test_hash_map_table_collision(unittest.TestCase):
hm.add("a", "Ibiza")
self.assertEqual(hm.size(), 1)
hm.add("a", "Ibiza2")
self.assertRaises(Exception, lambda x : hm.add("a", "Ibiza"))
hm.add("a2", "Ibiza2")
self.assertEqual(hm.size(), 2)
# TODO: Should return exception. Not allow twice the same key!
def test_get(self):
@@ -70,11 +72,7 @@ class test_hash_map_table_collision(unittest.TestCase):
hm.add("Three", "Berlin")
hm.add("Four", "Chicago")
value3 = "Ibiza2"
hm.add(key, value3)
self.assertEqual(hm.size(), 5)
self.assertEqual(hm.size(), 4)
self.assertEqual(hm.get(key2), value2)
self.assertEqual(hm.get("Five"), None)