Finishes implementing the bare simple HM ersion using items. Now going to modify the version handling collisions

This commit is contained in:
julien lengrand-lambert
2013-11-26 18:59:33 +01:00
parent 7297cec59b
commit b5d8de4faf
2 changed files with 5 additions and 3 deletions

View File

@@ -145,9 +145,12 @@ class HashMapWithItem():
Adds the provided value to the hashmap.
Raises an Exception if a collision is detected
"""
# item to be saved in the HM
item = _HashMapItem(key, value)
my_key = self._hash(key)
if self.hmap[my_key] == None:
self.hmap[my_key] = value
self.hmap[my_key] = item
self._size += 1
else:
raise Exception("Collision detected at index %d", key)
@@ -158,7 +161,7 @@ class HashMapWithItem():
the string we are looking for
"""
my_key = self._hash(key)
return self.hmap[my_key]
return self.hmap[my_key].v # returns the value
def size(self):
return self._size

View File

@@ -80,7 +80,6 @@ class test_hash_map_table_collision(unittest.TestCase):
self.assertEqual(hm.get("Five"), None)
self.assertEqual(hm.get(key), [value, value3])
class test_hash_map_with_item(unittest.TestCase):
def test_hash_size(self):