diff --git a/02_hash_maps/hm.py b/02_hash_maps/hm.py index 1e46fdd..d9175ca 100644 --- a/02_hash_maps/hm.py +++ b/02_hash_maps/hm.py @@ -114,7 +114,7 @@ class HMTableCollision(HashMap): return h % self._hash_size -class HashMapNeighbourCollision(): +class HMNeighbourCollision(): def __init__(self, hash_size=513): self._hash_size = hash_size self._size = 0 @@ -126,20 +126,39 @@ class HashMapNeighbourCollision(): Raises an Exception if a collision is detected """ my_key = self._hash(key) - if self.hmap[my_key] == None: - self.hmap[my_key] = value - self._size += 1 - else: - raise Exception("Collision detected at index %d", key) + idx = self._find_free_idx(my_key) + + self.hmap[idx] = value + self._size += 1 + + def _find_free_idx(self, key): + """ + Given an index in the current hash table, finds the nearest + element with a free value + """ + idx = key + cur_ptr = 1 + negative = True + + while(True): + if self.hmap[idx] == None: + return idx + else: + if negative: + idx = key - cur_ptr + negative = False + else: + idx = key + cur_ptr + negative = True + cur_ptr += 1 def get(self, key): """ Finds the element in the hash table that may contain the id for the string we are looking for """ - my_key = self._hash(key) - return self.hmap[my_key] - + #TODO + def size(self): return self._size diff --git a/02_hash_maps/hm_test.py b/02_hash_maps/hm_test.py index bade042..3e8b909 100644 --- a/02_hash_maps/hm_test.py +++ b/02_hash_maps/hm_test.py @@ -6,13 +6,20 @@ Unit tests for the Hash Map table implementation from hm import HashMap from hm import HMTableCollision +from hm import HMNeighbourCollision import unittest class test_hash_map_neighbour_collision(unittest.TestCase): def test_add(self): - pass + hm = HMNeighbourCollision() + + hm.add("One", "Ibiza") + self.assertEqual(hm.size(), 1) + + hm.add("One", "Ibiza2") + self.assertEqual(hm.size(), 2) class test_hash_map_table_collision(unittest.TestCase):