Hash table with open addressing
how it works
The idea in a few sentences and three pictures, then one example, moment by moment.
A hash table with open addressing keeps every number in the table's own slots, one number a slot, with no chains of numbers, and each number once. This one has 7 slots, numbered 0 to 6. A number's home is its remainder when divided by 7, the table's hash: 38 is 5 sevens and 3 over, so home 3. To put a number in, you look at its home, then the next slots, round from slot 6 to slot 0, until a free one: linear probing. Each slot looked at is a probe. A take walks the same way to the number and leaves a marker. A search stops at an empty slot, since a put would have filled it, so the marker keeps it walking. A put that passes a marker walks on to a free slot, or round all 7, to be sure the number is not further along. If it is not, it goes in the marker's slot. The example after these pictures puts in 17, 24, 9 and 31, takes 24 and 31 out, then puts in 38.
step 9 of 13
31 leaves 3 when divided by 7, so its home is slot 3.
slots looked at: 0
step 10 of 13
Slot 3 holds 17, not 31.
slots looked at: 1
step 11 of 13
Slot 4 holds 24, not 31.
slots looked at: 2
step 12 of 13
Slot 5 is free, so put 31 there.
slots looked at: 3
| put | number | home | its walk | probes |
|---|---|---|---|---|
| 1 | 7 | 0 | 0 | 1 |
| 2 | 14 | 0 | 0, 1 | 2 |
| 3 | 21 | 0 | 0, 1, 2 | 3 |
| 4 | 28 | 0 | 0, 1, 2, 3 | 4 |
| the table | slot 4 | slot 5 | slot 6 | a search for 39 | a put of 39 |
|---|---|---|---|---|---|
| 25, 32, 39 put in | 25 | 32 | 39 | found, 3 probes | already in |
| 32 taken, a marker left | 25 | × | 39 | walks on: found, 3 probes | walks past ×: already in |
| 32 taken, slot emptied | 25 | empty | 39 | stops: missing, 2 probes | stops: 39 twice |
| a put that stopped at × | 25 | × | 39 | puts 39 in slot 5: twice |
step 3 of 27
Slot 3 is free, so put 17 there.
what it remembers to know where it is
- doing operation
- 1
- number
- 17
- slot
- 3
step 12 of 27
Slot 5 is free, so put 31 there.
what it remembers to know where it is
- doing operation
- 4
- number
- 31
- slot
- 5
step 16 of 27
Take 24 out of slot 4, and leave a marker so later searches walk past it.
what it remembers to know where it is
- doing operation
- 5
- number
- 24
- slot
- 4
step 19 of 27
Slot 4 holds a marker left by a number taken out, not 31.
what it remembers to know where it is
- doing operation
- 6
- number
- 31
- slot
- 4
step 21 of 27
Take 31 out of slot 5, and leave a marker so later searches walk past it.
what it remembers to know where it is
- doing operation
- 6
- number
- 31
- slot
- 5
step 24 of 27
Slot 4 holds a marker left by a number taken out. Note it: 38 can go here if it is nowhere further along.
what it remembers to know where it is
- doing operation
- 7
- number
- 38
- slot
- 4
step 25 of 27
Slot 5 holds a marker left by a number taken out, not 38.
what it remembers to know where it is
- doing operation
- 7
- number
- 38
- slot
- 5
step 26 of 27
38 is nowhere further along, so put it in slot 4, where the first marker was.
what it remembers to know where it is
- doing operation
- 7
- number
- 38
- slot
- 4
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- start with 7 empty slotsPython line 12
- a number's home is the remainder it leaves when divided by 7Python line 14
- look at its home slot, then the next ones, round to the startPython lines 17, 18 and 19
- to put it in, stop at a free slot or use the first marker passed; a repeat, or no room, is turned awayPython lines 15, 22, 23, 24, 25, 26, 27 and 28
- to take it out, stop at it and leave a markerPython lines 29 and 30
- an empty slot, or a whole round, ends the search, and the number is not therePython lines 19, 20 and 21
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
class Marker:
"""What a take leaves in its slot: no number, yet not empty."""
def hash_table_open_addressing(operations):
"""Run a list of operations on a hash table of 7 slots, one number each.
Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
out. Returns the numbers held, slot by slot. Each probe looks at one
slot and compares what it holds with n.
"""
slots = [None] * 7 # 7 empty slots, numbered 0 to 6
for action, number in operations:
home = number % 7 # its remainder when divided by 7
marker = None # the first marker a put passes
stop = None # the slot that ends the search
for i in range(7): # home, then the next slots, round to the start
s = (home + i) % 7
if slots[s] == number or slots[s] is None: # one probe
stop = s
break
if isinstance(slots[s], Marker) and marker is None:
marker = s
if action == 'put':
if stop is not None and slots[stop] is None: # not there yet
slots[stop if marker is None else marker] = number
elif stop is None and marker is not None: # no empty slot
slots[marker] = number
elif stop is not None and slots[stop] is not None: # found it
slots[stop] = Marker() # take it out, leave a marker
return [x for x in slots if x is not None and not isinstance(x, Marker)]
how it scales
How the probes grow, from ten numbers to a million, each put into an empty table of 7 slots one at a time. Three ways to pick them: homes all different while there is room (1, 2, 3 and on), homes at random, and one home (7, 14, 21 and on). The plot and tables call a probe a slot looked at, as the watch page does.
Each step across or up is ten times the one before, a log-log plot. From 100 numbers on, all three lines climb one step up for each step across: ten times the numbers, a little over ten times the probes. The 7 slots are full by the 7th number, and from then on every put looks at all 7 and is turned away. So the lines nearly meet: 658, about 665 and 679 probes at 100 numbers. What differs is how the table fills, which the grid in section 3 shows.
slots looked at, ten times more at each mark up
, how many numbers are in the table, ten times more at each mark to the right
- one home, 7, 14, and on up to solid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
- each number's home at random, every slot as likelypoints only, each measured by a real run, since no single formula for the slots looked at of this row is worked out here
- homes all different while there is room, 1 to solid where measured by running it; dashed where its formula carries it on to ten million
| slots looked at | time | |
|---|---|---|
| 1,000 | 6,979 | about 7 microseconds |
| 100,000 | 699,979 | about 700 microseconds |
| 10,000,000 | 69,999,979 | about 70 milliseconds |
The slots looked at are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count a hash table's probes yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each size n it puts n different
numbers into an empty table of 7 slots, one at a time, and prints the
probes for 1 to n (each its own home while there is room, the cheapest),
the average over 20 tables whose numbers each have a home picked at
random, and the probes for 7, 14, up to 7n (every home 0, the dearest).
Past 7 numbers the table is full, and each put probes all 7 slots. A probe
is one slot looked at. The counts are the same on any computer; only the
time differs.
"""
import random
class Counted:
"""A number that counts the probes made with it: one a slot looked at."""
probes = 0
def __init__(self, value):
self.value = value
def __eq__(self, other):
Counted.probes += 1
return isinstance(other, Counted) and self.value == other.value
def __mod__(self, other):
return self.value % other # finding the home is no probe
class Marker:
"""What a take leaves in its slot: no number, yet not empty."""
def hash_table_open_addressing(operations):
"""Run a list of operations on a hash table of 7 slots, one number each.
Each operation is a pair: ('put', n) puts n in, and ('take', n) takes n
out. Returns the numbers held, slot by slot. Each probe looks at one
slot and compares what it holds with n.
"""
slots = [None] * 7 # 7 empty slots, numbered 0 to 6
for action, number in operations:
home = number % 7 # its remainder when divided by 7
marker = None # the first marker a put passes
stop = None # the slot that ends the search
for i in range(7): # home, then the next slots, round to the start
s = (home + i) % 7
if slots[s] == number or slots[s] is None: # one probe
stop = s
break
if isinstance(slots[s], Marker) and marker is None:
marker = s
if action == 'put':
if stop is not None and slots[stop] is None: # not there yet
slots[stop if marker is None else marker] = number
elif stop is None and marker is not None: # no empty slot
slots[marker] = number
elif stop is not None and slots[stop] is not None: # found it
slots[stop] = Marker() # take it out, leave a marker
return [x for x in slots if x is not None and not isinstance(x, Marker)]
def cheapest(n):
"""1 to n put in: homes 1, 2, 3, 4, 5, 6, 0, then the table is full."""
return [('put', number) for number in range(1, n + 1)]
def count(operations):
"""How many probes the table makes on this list of operations."""
Counted.probes = 0
hash_table_open_addressing([(action, Counted(number)) for action, number in operations])
return Counted.probes
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>6} {'own homes':>10} {'at random':>10} {'one home':>9}")
for n in [10, 100, 1000]:
tables = []
for _ in range(20):
row = [7 * i + random.randrange(7) for i in range(1, n + 1)]
tables.append(count([('put', number) for number in row]))
dearest = [('put', 7 * i) for i in range(1, n + 1)]
print(f"{n:>6} {count(cheapest(n)):>10} {sum(tables) / 20:>10.1f} {count(dearest):>9}")
best, worst and average
A put probes from its home to the first free slot. So the homes decide the cost while the table fills, and after that every put costs 7.
A case is one kind of input, the thing the method is given, such as numbers that all have the same home, however many. A bound says how fast that case's cost grows as more numbers go in.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| besthomes all different while there is room, 1 to | averageeach number's home at random, every slot as likely | worstone home, 7, 14, and on up to | |
|---|---|---|---|
| slots looked at | in all up to 7 numbers, then | between the two, about 665 at 100 | from 7 numbers on |
| how the slots looked at grow in each case | |||
| tight, | |||
| ceiling, | |||
| floor, | |||
extra memory: 7 slots, nothing else
Extra memory is the room it needs on top of the input itself, while it runs.
| put | one home | homes all different | at random, on average |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 2 | 2 | 1 | about 1.14 |
| 3 | 3 | 1 | about 1.35 |
| 4 | 4 | 1 | about 1.65 |
| 5 | 5 | 1 | about 2.10 |
| 6 | 6 | 1 | about 2.82 |
| 7 | 7 | 1 | 4 |
| 8th on | 7 | 7 | 7 |
- one home, from 7 on
- homes all different, from 7 on
where the formula comes from
Each count above, worked out line by line. Each reason names the line it uses, and each 'where does this come from?' box opens a fact from another page.
the move: price one put
the line:step 11 of 16
28 leaves 0 when divided by 7, so its home is slot 0.
slots looked at: 0
step 12 of 16
Slot 0 holds 7, not 28.
slots looked at: 1
step 13 of 16
Slot 1 holds 14, not 28.
slots looked at: 2
step 14 of 16
Slot 2 holds 21, not 28.
slots looked at: 3
step 15 of 16
Slot 3 is free, so put 28 there.
slots looked at: 4
Operation 4 puts 28 in, after 7, 14 and 21, all with home 0. 28 walks past all three to slot 3. 4 probes. why it is allowed: Count the puts from 1. The numbers before it, one fewer than , fill a run from its home, so it looks at each, then at the free slot after them.
the move: the table is full
the line:why it is allowed: The 7 slots hold 7 numbers, so the walk goes all the way round and finds no free slot.
guess it, thenthe move: no numbers cost more
the line:why it is allowed: Its walk passes only slots holding an earlier number, at most one fewer than , then stops at a free slot: the 4th put passes at most 3. And a walk never looks at more than the 7 slots.
guess it, thenthe move: add the puts
the line:why it is allowed: is the probes of numbers with one home, from 7 numbers on. By lines 1 and 2, puts 1 to 7 make 1 up to 7, and each later put 7. For 8, 28 + 7.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is 7: 7 times 8, halved, is 28.
guess it, thenthe move: tidy
the line:why it is allowed: In line 5, 7 times 8, halved, is 28, and is 7 times , less 49. 28 take away 49 is minus 21. For 8, 56 less 21 is 35.
guess it, thenthe move: a ceiling
the line:why it is allowed: Line 6 takes 21 away from . For 7, 28 is at most 49.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, with the fixed multiple 7.
guess it, thenthe move: a fact about n
the line:why it is allowed: For a floor, swap the 21 for a multiple of . 3 times 7 is 21, and only grows. For 8, 21 is at most 24.
guess it, thenthe move: a floor
the line:why it is allowed: In line 6, less 21 is at least less , by line 9, which is . For 7, 28 is at least 28.
guess it, thenthe move: read it as a bound
the line:- the ceiling,
- , one home
- the floor,
From 7 numbers on, stays between and , two lines of the same growth. At 20, 119 lies between 80 and 140. why it is allowed: By line 10, from 7 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 8 and 11 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: price the first 7
the line:step 6 of 8
3 leaves 3 when divided by 7, so its home is slot 3.
slots looked at: 0
step 7 of 8
Slot 3 is free, so put 3 there.
slots looked at: 1
Operation 3 puts 3 in, after 1 and 2. Its home, slot 3, is free. 1 probe. why it is allowed: 1 to 7 have homes 1, 2, 3, 4, 5, 6 and 0, so each home is free when its number arrives.
the move: no numbers cost less
the line:why it is allowed: A put looks at its home at least. Any 7 different numbers fill all 7 slots, as each walk can go round all 7 to a free one. Then, by line 2 of the one-home question, every later put probes all 7, whatever the homes.
guess it, thenthe move: add the puts
the line:why it is allowed: is the probes of 1 to put in, from 7 numbers on. By lines 1 and 2, 7 puts of 1, then 7 each. For 8, 7 + 7 is 14.
guess it, thenthe move: tidy
the line:why it is allowed: In line 3, is 7 times , less 49, and 7 take away 49 is minus 42. For 8, 56 less 42 is 14.
guess it, thenthe move: a ceiling
the line:why it is allowed: Line 4 takes 42 away from . For 7, 7 is at most 49.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5, with the fixed multiple 7.
guess it, thenthe move: a fact about n
the line:why it is allowed: For a floor, swap the 42 for a multiple of . 6 times 7 is 42, and only grows. For 8, 42 is at most 48.
guess it, thenthe move: a floor
the line:why it is allowed: In line 4, less 42 is at least less , by line 7, which is . For 7, 7 is at least 7.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 8, from 7 numbers on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 6 and 9 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: walk to the free slot
the line:home its walk probes 5 5 1 4 4, 5 2 3 3, 4, 5 3 2 2 to 5 4 1 1 to 5 5 0 0 to 5 6 6 6, 0 to 5 7 7 slots, only slot 5 free. Each home gives a different number of probes, 1 to 7. why it is allowed: It looks at its home, then the next slots, round to the start, and stops at the one free slot. Its home is any of the slots, each as likely, and each home is a different distance away.
the move: average over the homes
the line:why it is allowed: is the probes averaged over the homes. By line 1, the homes give 1 up to , one each. For 7, 28 over 7.
guess it, thenthe move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line shows that 1 up to a last number adds to that number times one more, halved. Here the last number is . For 7, 7 times 8, halved, is 28.
guess it, thenthe move: halve and divide at once
the line:why it is allowed: Halved, then over , is over twice . For 7, 7 times 8 over 14 is 4.
guess it, thenthe move: tidy
the line:why it is allowed: The on top and below cancel. For 7 slots, 8 over 2 is 4, section 3's average for the 7th put, which finds 6 numbers in 7 slots.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: no fewer than the cheapest
the line:numbers one home at random, on average homes all different 7 28 about 14.06 7 100 679 about 665 658 At 7 numbers, the average over all 823,543 choices of homes. It is section 3's column at random, added up: 1 + 1.14 + 1.35 + 1.65 + 2.10 + 2.82 + 4. At 100, the average of 200 tables picked at random. why it is allowed: is the probes averaged over every choice of homes. No choice costs fewer than homes all different while there is room, by lines 2 and 4 of the question on the fewest probes, so neither does the average.
the move: read it as a bound
the line:why it is allowed: By line 1, as is at least from 7 numbers on, line 8 of the question on the fewest probes.
guess it, thenthe move: no more than the dearest
the line:why it is allowed: No choice costs more than one home, by lines 3 and 6 of the one-home question, so neither does the average.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 3, as is at most , line 7 of the one-home question, the fixed multiple 7.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 2 and 4 bound by the same growth, .
guess it, then
try it on small inputs: over every choice of home for each of 7 numbers, 823,543 in all, the average is about 14.06
count it yourself
Count every probe as 7, 14, 21, 28, 35, 42 and 49 go into an empty table. All seven have home 0.
| step | the slots, numbers only | what happens | slots looked at |
|---|---|---|---|
| 1 | Start with 7 empty slots, and 7 operations to do. | 0 | |
| 2 | 7 leaves 0 when divided by 7, so its home is slot 0. | 0 | |
| 3 | 7 | Slot 0 is free, so put 7 there.7 | 1 |
| 4 | 7 | 14 leaves 0 when divided by 7, so its home is slot 0.7 | 1 |
| 5 | 7 | Slot 0 holds 7, not 14.7 | 2 |
| 6 | 7, 14 | Slot 1 is free, so put 14 there.7, 14 | 3 |
| 7 | 7, 14 | 21 leaves 0 when divided by 7, so its home is slot 0.7, 14 | 3 |
| 8 | 7, 14 | Slot 0 holds 7, not 21.7, 14 | 4 |
| 9 | 7, 14 | Slot 1 holds 14, not 21.7, 14 | 5 |
| 10 | 7, 14, 21 | Slot 2 is free, so put 21 there.7, 14, 21 | 6 |
| 11 | 7, 14, 21 | 28 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21 | 6 |
| 12 | 7, 14, 21 | Slot 0 holds 7, not 28.7, 14, 21 | 7 |
| 13 | 7, 14, 21 | Slot 1 holds 14, not 28.7, 14, 21 | 8 |
| 14 | 7, 14, 21 | Slot 2 holds 21, not 28.7, 14, 21 | 9 |
| 15 | 7, 14, 21, 28 | Slot 3 is free, so put 28 there.7, 14, 21, 28 | 10 |
| 16 | 7, 14, 21, 28 | 35 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21, 28 | 10 |
| 17 | 7, 14, 21, 28 | Slot 0 holds 7, not 35.7, 14, 21, 28 | 11 |
| 18 | 7, 14, 21, 28 | Slot 1 holds 14, not 35.7, 14, 21, 28 | 12 |
| 19 | 7, 14, 21, 28 | Slot 2 holds 21, not 35.7, 14, 21, 28 | 13 |
| 20 | 7, 14, 21, 28 | Slot 3 holds 28, not 35.7, 14, 21, 28 | 14 |
| 21 | 7, 14, 21, 28, 35 | Slot 4 is free, so put 35 there.7, 14, 21, 28, 35 | 15 |
| 22 | 7, 14, 21, 28, 35 | 42 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21, 28, 35 | 15 |
| 23 | 7, 14, 21, 28, 35 | Slot 0 holds 7, not 42.7, 14, 21, 28, 35 | 16 |
| 24 | 7, 14, 21, 28, 35 | Slot 1 holds 14, not 42.7, 14, 21, 28, 35 | 17 |
| 25 | 7, 14, 21, 28, 35 | Slot 2 holds 21, not 42.7, 14, 21, 28, 35 | 18 |
| 26 | 7, 14, 21, 28, 35 | Slot 3 holds 28, not 42.7, 14, 21, 28, 35 | 19 |
| 27 | 7, 14, 21, 28, 35 | Slot 4 holds 35, not 42.7, 14, 21, 28, 35 | 20 |
| 28 | 7, 14, 21, 28, 35, 42 | Slot 5 is free, so put 42 there.7, 14, 21, 28, 35, 42 | 21 |
| 29 | 7, 14, 21, 28, 35, 42 | 49 leaves 0 when divided by 7, so its home is slot 0.7, 14, 21, 28, 35, 42 | 21 |
| 30 | 7, 14, 21, 28, 35, 42 | Slot 0 holds 7, not 49.7, 14, 21, 28, 35, 42 | 22 |
| 31 | 7, 14, 21, 28, 35, 42 | Slot 1 holds 14, not 49.7, 14, 21, 28, 35, 42 | 23 |
| 32 | 7, 14, 21, 28, 35, 42 | Slot 2 holds 21, not 49.7, 14, 21, 28, 35, 42 | 24 |
| 33 | 7, 14, 21, 28, 35, 42 | Slot 3 holds 28, not 49.7, 14, 21, 28, 35, 42 | 25 |
| 34 | 7, 14, 21, 28, 35, 42 | Slot 4 holds 35, not 49.7, 14, 21, 28, 35, 42 | 26 |
| 35 | 7, 14, 21, 28, 35, 42 | Slot 5 holds 42, not 49.7, 14, 21, 28, 35, 42 | 27 |
| 36 | 7, 14, 21, 28, 35, 42, 49 | Slot 6 is free, so put 49 there.7, 14, 21, 28, 35, 42, 49 | 28 |
| 37 | 7, 14, 21, 28, 35, 42, 49 | Done. The table holds 7 numbers.7, 14, 21, 28, 35, 42, 49 | 28 |
In all, 28 slots looked at. The formula from section 4, , gives at : the same.
where you meet it
Open addressing is how Python's dict and many other tables are built.
Python's dict
dict, Python's own table, keeps its keys, the things it looks up, by open addressing. A deleted key leaves a dummy, its marker, and the table is rebuilt once two thirds of its slots are used, dummies counted, so it is never full.
source: CPython's Objects/dictobject.c, USABLE_FRACTION and DKIX_DUMMY
Java's IdentityHashMap
Java's IdentityHashMap is a linear-probe table, walking to the next slot as this page does. It leaves no marker: taking a key out empties its slot, then moves back into the gap each later key whose walk the gap would cut short.
source: Java's documentation, java.util.IdentityHashMap (opens in a new tab); OpenJDK's source, java/util/IdentityHashMap.java in its java.base module, closeDeletion
| dict's slots | keys it holds before growing |
|---|---|
| 8 | 5 |
| 16 | 10 |
| 32 | 21 |
what is used instead
Java's HashMap and C++'s unordered_map chain instead, as the hash table with chaining does. Sebastian Bourges' books give the rule: 'Reach for open addressing when the keys and values are small and are copied cheaply' (volume two, chapter 18).
how it compares
The two hash tables of this site side by side.
| way | where a number goes | past full | taking out | reach for it when |
|---|---|---|---|---|
| open addressing, 7 slots | its home, or the first marker its walk passed, or the next free slot | no room: turned away after 7 probes | leaves a marker | numbers are small and few are taken out, as markers stay and slow every walk |
| chaining, 5 buckets | the end of its bucket's chain | chains grow longer, still working | taken out of its chain, no marker | many are taken out, or the table may overfill |