Open-Addressing Map with Tombstones
Prerequisites: unordered_map Buckets, Load Factor, and Rehashing, Cache Lines, Locality, and Pointer Chasing, STL Container Choice
The rehashing article ended on the layout that removes std::unordered_map's pointer chase. Build that map for integer keys and values with the simplest probing rule. A cancel that looks up an AAPL order by order_id then reads a few adjacent slots, not scattered nodes.
- Open addressing: every entry is stored inside one contiguous array of slots. When a key's slot is taken, the map tries other slots of the same array instead of growing a chain.
- Home slot:
key mod capacity, the remainder ofkey / capacitytaken in the range0tocapacity - 1. - Linear probing: a walk from the home slot to the next slot, then the one after. The
i-th slot visited is(home + i) mod capacity, wrapping from the last slot back to slot 0. - Probe: one slot a walk looks at. The slots in order form the walk's probe path.
What to implement
process_commands(capacity, commands)returns the printed lines in order. Each command is one line.- The map starts with
capacityslots, andcapacityis a power of two. - Store the entries in your own slot array, without
std::unordered_map,std::map, or Python'sdictandset. - On stdin, the first line is
capacity, the second is the number of commands, and each remaining line is one command. - There are three parts, each adding behavior to the previous one. One solution must pass all ten tests.
| Command | Meaning | Prints |
|---|---|---|
PUT key value | Store value for key, replacing any value already stored | nothing |
GET key | Look up key | its value, or NOT_FOUND |
REMOVE key | Delete key if it is present (Part 2) | nothing |
PROBES | Report the probes of the most recent PUT, GET, or REMOVE | the count |
SLOTS | Show the slot array | one token per slot |
PROBESprints how many slots the most recentPUT,GET, orREMOVElooked at, counting the slot where its walk stopped. Before the first such command it prints0.PROBESandSLOTSnever change the count.SLOTSprints one token per slot, from slot 0 to the last slot, separated by single spaces. The tokens are in the table below.
Slots and home slots
Each slot is in one of three states, and every slot starts EMPTY.
| State | Meaning | SLOTS token |
|---|---|---|
| EMPTY | has held nothing since its array was allocated | . |
| LIVE | holds one key and its value | the key |
| TOMBSTONE | held a key that was removed (Part 2) | T |
- Because
capacityis a power of two,key mod capacityequalskey & (capacity - 1), which keeps the key's low bits. This holds in both C++ and Python, and for negative keys too. - With 8 slots, key
-1has home slot 7.
Part 1: Insert and look up
GET keywalks from the key's home slot until it reaches a LIVE slot holdingkeyor an EMPTY slot.- A LIVE slot holding a different key does not stop the walk, because a colliding key may have been stored further along.
- If the walk finds the key,
GETprints its value. If it reaches an EMPTY slot first, the key is absent andGETprintsNOT_FOUND. PUT key valuemakes the same walk. If it finds the key, it replaces the value in that slot. If it reaches an EMPTY slot, it stores the key and value there.- So a new key lands in the first EMPTY slot of its probe path, and a later
GETretraces that path to find it.
Limits
- Keys and values are signed 32-bit integers.
capacityis a power of two between 1 and 1024. There are at most 2 × 105 commands.SLOTSappears only while the array has at most 64 slots.
Tests 1–3 cover this part. They include keys that share a home slot, a walk that wraps from the last slot to slot 0, replaced values, negative keys, and the 32-bit extremes.
These tests never fill more than three quarters of the slots, so Part 3's growth never happens in them and an EMPTY slot always remains to end a walk.
Part 2: Delete with tombstones
REMOVE keywalks likeGET. If the walk finds the key, its slot becomes a tombstone: the slot no longer holds an entry, but unlike an EMPTY slot it does not end a walk.- If the key is absent,
REMOVEchanges nothing. Either way, its walk sets thePROBEScount. GETandREMOVEwalk past tombstones and stop only at the key or at an EMPTY slot.PUTalso walks until it finds the key or reaches an EMPTY slot, since the key may be stored past a tombstone. It remembers the first tombstone it passes.- If
PUTfinds the key, it replaces the value in the key's slot. The key does not move to an earlier tombstone. - If
PUTreaches an EMPTY slot, the key is absent. The new entry goes into the first tombstone on the walk, or into the EMPTY slot when the walk passed no tombstone. PROBEScounts the whole walk, up to the key or the EMPTY slot, even when the entry lands in an earlier tombstone.
Marking a removed slot EMPTY instead would break lookups. A key that collided with the removed one and was stored further along would become unreachable, because its GET would stop at the new EMPTY slot.
Tests 4–6 cover this part. Like tests 1–3, they never let LIVE slots and tombstones together fill more than three quarters of the array.
Part 3: Grow the array
Only an EMPTY slot ends a walk, so tombstones lengthen walks just as LIVE slots do. The map therefore counts its used slots: the LIVE slots plus the tombstones.
- Right after a
PUTstores a new key, if4 * used > 3 * capacity, meaning more than three quarters of the slots are used, the map grows. - Growing allocates
2 * capacityEMPTY slots and reinserts every LIVE entry with its value. Tombstones are not copied, so the new array has none. - The reinsertion scans the old array from slot 0 to its last slot. It puts each key in the first EMPTY slot of its probe path in the new array, whose home slots use the new capacity.
- A
PUTthat replaces a value or reuses a tombstone leavesusedunchanged, so it never grows the map.REMOVEnever shrinks it. - After a
PUTthat grew the map,PROBESreports thatPUT's walk in the old array; the reinsertion is not counted.
One doubling is always enough. Before the PUT, at most three quarters of the old slots were used, so the live keys fill at most half of the new array.
Growth never changes what GET prints, but it changes the layout, which SLOTS and PROBES can see.
Tests 7–10 cover this part.
8 9 PUT 3 30 PUT 11 110 PUT 19 190 PUT 4 40 PROBES GET 27 PROBES GET 11 SLOTS
3 NOT_FOUND 5 110 . . . 3 11 19 4 .
Keys 3, 11, and 19 all have home slot 3, because each leaves remainder 3 when divided by 8. Key 11 finds slot 3 taken and lands in slot 4, and 19 lands in slot 5.
Key 4's home slot is 4, which 11 already holds, so its walk looks at slots 4, 5, and 6 and stores it in slot 6. PROBES prints 3.
GET 27 also starts at slot 3 and passes four LIVE slots before it reaches EMPTY slot 7, so 27 is absent after 5 probes.