Starts implementing add method to fill the hash map

This commit is contained in:
Julien Lengrand-Lambert
2013-11-21 11:58:50 +01:00
parent bcadb29f5f
commit 20fe41493b
2 changed files with 14 additions and 3 deletions

View File

@@ -8,14 +8,19 @@ class HashMap():
def __init__(self, hash_size=513):
self._hash_size = hash_size
self._size = 0
self.hmap = [] * self._hash_size
self.hmap = [0] * self._hash_size
def add(self, value):
"""
Adds the provided value to the hashmap
Adds the provided value to the hashmap.
Raises an Exception if a collision is detected
"""
key = self._hash(value)
self.hmap[key] = value
if self.hmap[key] == 0:
self.hmap[key] = value
self._size += 1
else:
raise Exception("Collision detected at index %d", key)
# TODO: Keep implementing

View File

@@ -35,5 +35,11 @@ class test_hash_map(unittest.TestCase):
value = ""
self.assertRaises(Exception, lambda x : hm._hash(value))
def test_add(self):
hm = HashMap()
hm.add("a")
self.assertEqual(hm.size(), 1)
if __name__ == "__main__":
unittest.main()