Starts implementing get method of the HashMap

This commit is contained in:
Julien Lengrand-Lambert
2013-11-21 19:54:16 +01:00
parent 77ad11ea74
commit 7f8736c9ca
2 changed files with 18 additions and 2 deletions

View File

@@ -8,7 +8,7 @@ class HashMap():
def __init__(self, hash_size=513):
self._hash_size = hash_size
self._size = 0
self.hmap = [0] * self._hash_size
self.hmap = [None] * self._hash_size
def add(self, value):
"""
@@ -16,7 +16,7 @@ class HashMap():
Raises an Exception if a collision is detected
"""
key = self._hash(value)
if self.hmap[key] == 0:
if self.hmap[key] == None:
self.hmap[key] = value
self._size += 1
else:
@@ -24,6 +24,16 @@ class HashMap():
# TODO: Keep implementing
def get(self, value):
"""
Finds the element in the hash table that may contain the id for
the string ze are looking for
"""
key = self._hash(value)
return self.hmap[key]
def size(self):
return self._size

View File

@@ -45,6 +45,12 @@ class test_hash_map(unittest.TestCase):
self.assertRaises(Exception, lambda x : hm.add(a))
self.assertEqual(hm.size(), 1)
def test_get(self):
hm = HashMap()
value = ""
self.assertRaises(Exception, lambda x : hm.get(value))
if __name__ == "__main__":
unittest.main()