Sub topic 4
From hash value to bucket index
This chapter assumes you know array indexing, the modulo operator %, and the idea of a hash function that turns a key into an integer. Here we cover the step after hashing: turning that integer into a valid position in the array.
The size mismatch
A hash function returns a large integer, often 32 or 64 bits wide. The table's array might have only 8 slots, called buckets. You can't use the hash value as an index directly, because it could be far larger than the array, or negative.
The fix is to reduce the hash value to the range 0 to size - 1. The usual way is the modulo operator.
Computing the index
The formula is index = hash(key) % size. The same key always gives the same hash, and the size is fixed between resizes. So the same key always lands in the same bucket.
size = 8
for key in [10, 18, -3]:
h = hash(key)
print(key, h, h % size, h & (size - 1))This prints:
10 10 2 2
18 18 2 2
-3 -3 5 5Python integers hash to themselves, except that -1 hashes to -2. Python's % returns a non-negative result when the divisor is positive, so negative hash values still produce valid indexes. Many other languages return negative remainders, and implementations there must fix the sign first, for example by masking.
Keys 10 and 18 both land in bucket 2. That is a collision, which the next chapter covers.
Two ways to reduce
The expression h & (size - 1) is a bitwise AND. It gives the same result as h % size, but only when size is a power of two. It is faster, and many implementations use it with power-of-two capacities.
The cost is that only the low bits of the hash choose the bucket. If the hash function leaves those bits patterned, many keys pile into a few buckets.
keys = [0, 8, 16, 24, 32]
print([k % 8 for k in keys])
print([k % 7 for k in keys])This prints:
[0, 0, 0, 0, 0]
[0, 1, 2, 3, 4]With size 8, every multiple of 8 shares its low three bits, so all five keys collide. A prime size such as 7 spreads them out. The two designs respond differently:
| Capacity | Reduction | Advantage | Risk |
|---|---|---|---|
| Prime | `h % size` | Spreads patterned hashes well | Division is slower |
| Power of two | `h & (size - 1)` | Fast, and doubling is simple | Needs a hash that mixes its low bits well |
Libraries that use power-of-two sizes usually mix the hash bits first, so the low bits depend on the whole value.
The index depends on the size
The bucket index is computed from the current table size. When the table grows from 8 to 16 buckets, the same key can map to a different index. A key with hash 18 sits in bucket 2 when the size is 8, and in bucket 2 again when the size is 16. A key with hash 10 moves from bucket 2 to bucket 10.
For this reason, a resize can't copy buckets across unchanged. It must recompute the index of every entry, which is why the _resize method in a hash table reinserts each key.
Exercise
A key's hash value is 1234567 and the table has 16 buckets. Which bucket index does the key get? Show that % and the bitwise AND agree.
Show answer
1234567 % 16 = 7, because 16 × 77160 = 1234560 and the remainder is 7. Since 16 is a power of two, 1234567 & 15 also equals 7, because the low four bits of 1234567 are 0111.