I'm following this formula from wikipedia:
H(i, k) = (H1(k) + i*H2(k)) % size
and my H1
is Python's built-in hash()
function.
H2
is:
PRIME - (H1(k) % PRIME)
Unfortunately it randomly sticks in an infinite loop after a couple of execution. It cannot traverse all the slots in my table.
Here is my code but you have to set PYTHONHASHSEED=12
in order to reproduce this bug. (I deliberately removed many details so that the implementation would be minimal)
EMPTY = object()
class DoubleHashingHashMap:
def __init__(self):
self.prime = 7
self.size = 15
self.slots = [EMPTY] * self.size
def __setitem__(self, key, value):
for idx in self.probing_squence(key):
slot = self.slots[idx]
if slot is EMPTY:
self.slots[idx] = (key, value)
break
elif isinstance(slot, tuple):
k, v = slot
if k == key:
self.slots[idx] = (key, value)
break
def probing_squence(self, key):
h1 = self.hash_func1(key) % self.size
h2 = self.hash_func2(key) % self.size
i = 1
while True:
yield (h1 + i*h2) % self.size
i += 1
def hash_func1(self, item):
return hash(item)
def hash_func2(self, item):
return self.prime - (self.hash_func1(item) % self.prime)
hashmap = DoubleHashingHashMap()
for i in range(8):
hashmap[str(i)] = i
print("8 items added.")
print("Going into the infinite loop when adding 9th item(which is 8)...")
hashmap["8"] = 8
print("This line can't be reached.")
I would appreciate if you tell me what's wrong with my math.