For general hash table implementation:
Compute the hash of the key,
hash(key)=hashcodeMap the hashcode to the table/array.
hashcode % array_length = indexonce we get the index, we add a node (the key, value, update next pointer) in the Linked List at that index.
So, the question is, what's the difference between:
def _get_index(self, key):
# compute the hashcode
hash_code = hash(key)
array_index = hash_code & 15 # FIXME : why?
return array_index
and
array_index = hash_code % 15
For example: for INPUT:
hm =MyHashMap()
hm.put("1", "sachin")
hm.put("2", "sehwag")
hm.put("3", "ganguly")
print(hm.get("1"))
print(hm.get("2"))
print(hm.get("3"))
OUTPUT:
sachin
sehwag
ganguly
'&' operator instead of '%' that doesn't make sense to me? because it doesn't work always as % operator in computing the index, But, I have seenn developer using & in some implementations of Hashtable
Any suggestion?