Sub topic 5
From hash value to bucket index
This chapter assumes you can read Python, use the % and & operators, and know that a hash table stores items in an array of buckets. It also assumes you know what a hash function returns for a key.
The last step of a hash lookup
A hash function returns a large integer, such as a 32-bit or 64-bit value. A table with 16 buckets needs an index from 0 to 15. The table therefore applies one more step to shrink the hash into the valid index range.
Two reductions are common.
- Modulo:
index = h % n. It works for any bucket countn. - Mask:
index = h & (n - 1). It works only whennis a power of two. It keeps the low bits of the hash and is faster than division.
For a power of two, both give the same answer:
h = 0xDEADBEEF
print(h % 16, h & 15) # both print 15Why the reduction matters
The mask keeps only the low bits, so any pattern in those bits goes straight into the index. Suppose the keys are memory addresses or IDs that are multiples of 8, and the hash passes them through unchanged. The three low bits are always zero.
hashes = [i * 8 for i in range(1000)]
def bucket_mask(h, n):
return h & (n - 1)
def bucket_mod(h, n):
return h % n
print(len({bucket_mask(h, 16) for h in hashes})) # 2 buckets used
print(len({bucket_mod(h, 17) for h in hashes})) # 17 buckets usedWith 16 buckets and a mask, 1000 keys fall into only 2 buckets. A prime bucket count such as 17 breaks the pattern, because 8 and 17 share no factor.
There are two ways to avoid this problem.
- Use a prime bucket count with modulo. The cost is a slower division.
- Use a power-of-two count with a mask, and make sure the hash mixes its bits well. A hash with good avalanche behavior spreads patterns across all bits, so the low bits are as good as any others.
Many real tables take the second route. Some also apply an extra mixing step to the hash before masking, so a weak hash does less harm.
Negative values and bias
Python's % always returns a result between 0 and n - 1 for a positive n. In C, C++, and Java, % can return a negative number when the hash is negative. The usual fixes are to use an unsigned type, to mask the sign bit, or to use Math.floorMod in Java.
Modulo also has a small bias. If the hash range is not a multiple of n, some buckets receive slightly more values than others. For a 32-bit hash and a few thousand buckets the bias is tiny. It matters only when n is close to the size of the hash range.
Resizing changes every index
When a table grows from 16 to 32 buckets, the same hash gives a different index. Items must be reinserted, or at least re-examined. Tables store the full hash with each item so that resizing does not need to hash the keys again.
Exercise
A table has 64 buckets and uses h & 63. Every key's hash is a multiple of 4. How many buckets can ever be used, and what is one way to fix it?
Show answer
Only 16 buckets can be used, because the two low bits are always zero and 64 / 4 = 16. Fix it by using a hash that mixes its bits well, or by switching to modulo with a prime bucket count such as 61.