From 6c6be8663c893b6239e453b543abfb35cc2fef0e Mon Sep 17 00:00:00 2001 From: julien lengrand-lambert Date: Thu, 5 Dec 2013 07:21:52 +0100 Subject: [PATCH] Starts implementing get method for neighbour based hashmap --- 02_hash_maps/hm.py | 24 +++++++++++++++++++++--- 02_hash_maps/hm_test.py | 4 +--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/02_hash_maps/hm.py b/02_hash_maps/hm.py index d44d952..429fb08 100644 --- a/02_hash_maps/hm.py +++ b/02_hash_maps/hm.py @@ -168,8 +168,11 @@ class HMTableCollision(HashMap): elif len(items) == 1: return items[0].v # Returns correct value else: - # TODO: Change this. Only return the correct result - return items # we return all the results if there are several + if [key in item.k for item in items]: + return item.v # result found + else: + return None # result not found + # TODO: Test this def size(self): return self._size @@ -235,7 +238,22 @@ class HMNeighbourCollision(): Finds the element in the hash table that may contain the id for the string we are looking for """ - #TODO + my_key = self._hash(key) + items = self.hmap[my_key] + + if items is None: + return items # Nothing found + else: + # We think the key is here. + # Test all the possible indexes for the key + # Stop when we reach the limits of the hasmaps + # or we find a free index + idx = my_key + cur_ptr = 1 + negative = True + while(idx > 0 and idx < self._hash_size): + pass + # TODO: IMplement def size(self): return self._size diff --git a/02_hash_maps/hm_test.py b/02_hash_maps/hm_test.py index aebb12a..bc55116 100644 --- a/02_hash_maps/hm_test.py +++ b/02_hash_maps/hm_test.py @@ -77,9 +77,7 @@ class test_hash_map_table_collision(unittest.TestCase): self.assertEqual(hm.get(key2), value2) self.assertEqual(hm.get("Five"), None) - # Two tests to be done : - # Exact same key already exists : We return an error - # Different keys, but collision anyway, we return the correct value + # TODO: Different keys, but collision anyway, we return the correct value #self.assertEqual(hm.get(key), ) class test_hash_map_with_item(unittest.TestCase):