Question:
How are hash table's values stored in memory such that space if efficiently used and values don't have to be relocated often?
My current understanding (could be wrong):
Let's say I have 3 objects stored in a hash table. Their hash functions generate these values:
- 0
- 10
- 20
I would presume that the pointers of these objects would not be stored at the following memory addresses because there would be huge gaps between them:
- startOfHashTable + 0
- startOfHashTable + 10
- startOfHashTable + 20
The Wikipedia article on hash tables says that the "index" is computed as such:
hash = hashfunc(key)
index = hash % array_size
So in my example, the indices would be:
- 0 % 3 = 0
- 10 % 3 = 1
- 20 % 3 = 2
This gets rid of the huge gaps that I mentioned before. Even with this modulo scheme, there's problems when you add more objects to the hash table. If I add a fourth object to the hash table, I would need to apply % 4 to get the index. Wouldn't that invalidate all the % 3's that I did in the past? Would all those previous % 3's need to be relocated to the % 4 locations?
hash % table size
should be uniformly distributed, not the hash itself. – Pwner Feb 27 '15 at 01:12hash % tableSize
when tableSize can change? The hash values of 0, 5, and 10 create many collisions when the table size is 5, but have no collisions when the table size is 20. – Pwner Feb 27 '15 at 02:44