Starts implementing a neighbour implementation

This commit is contained in:
Julien Lengrand-Lambert
2013-11-24 16:33:24 +01:00
parent 423bf21fed
commit 96d3c8ca00
2 changed files with 51 additions and 0 deletions

View File

@@ -113,3 +113,49 @@ class HMTableCollision(HashMap):
h = (h << 4) + ord(letter)
return h % self._hash_size
class HashMapNeighbourCollision():
def __init__(self, hash_size=513):
self._hash_size = hash_size
self._size = 0
self.hmap = [None] * self._hash_size
def add(self, key, value):
"""
Adds the provided value to the hashmap.
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)
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]
def size(self):
return self._size
def _hash(self, value):
"""
Generates a hash for the given value.
The input is expected to be a String, with only ASCII characters.
# hash function taken from HT3.
# We shift and add : << 4 is a *16
"""
if len(value) < 1:
raise Exception("Size of value must be greater than one")
h = 0
for letter in value:
h = (h << 4) + ord(letter)
return h % self._hash_size

View File

@@ -9,6 +9,11 @@ from hm import HMTableCollision
import unittest
class test_hash_map_neighbour_collision(unittest.TestCase):
def test_add(self):
pass
class test_hash_map_table_collision(unittest.TestCase):
def test_add(self):