Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

Generational Handle Pool

Prerequisites: Container Invalidation and Stable Handles, Dangling References, string_view, and span, Raw Pointers, unique_ptr, shared_ptr, and weak_ptr

Build the pool that holds the order book's resting AAPL orders. The engine refers to an order by handle, not pointer. When a filled or cancelled order's slot is reused, an old handle must report the order gone instead of reading the new one.

  • Slot: one entry of the pool's slot array, numbered from 0.
  • Generation: a per-slot counter. It is 0 when the slot is created and goes up by 1 each time the slot's object is destroyed.
  • Handle: an object's slot plus that slot's generation when the object was created. Written index:generation, two decimal integers joined by a colon with no spaces: 3:2 is slot 3 at generation 2.
  • Live handle: its index is less than the number of slots, that slot holds an object, and the slot's generation equals the handle's.
  • Stale handle: any other handle. Its object was destroyed, or no object ever had that handle.

What to implement

  • process_commands(capacity, max_generation, commands) returns the printed lines in order. Each command is one line and prints exactly one line.
  • capacity and max_generation are the pool's limits (Part 2). Part 1's tests set them too high to matter.
  • An object's value is an integer from 0 to 109: the order's remaining quantity in shares.
  • On stdin, the first two lines are capacity and max_generation. The third is the number of commands, and each remaining line is one command.
  • There are three parts, each adding to the previous ones. One solution must pass all ten tests.
CommandEffectPrints
CREATE valueStore a new objectits handle, or FULL (Part 2)
GET handleRead an objectits value, or STALE
SET handle valueReplace an object's valueOK, or STALE
DESTROY handleRemove an objectOK, RETIRED (Part 2), or STALE
STATSReport the pool's sizeSTATS live slots free
SCANList the live objects (Part 3)SCAN and one handle=value per object, or SCAN NONE

Part 1: Create, read, destroy, and reuse

  1. CREATE value places a new object in a slot and prints its handle.
  2. If a slot is free, CREATE reuses the most recently freed one: the free slots form a free list used as a stack. A reused slot keeps the generation it received when freed.
  3. If no slot is free, the pool appends a new slot. Its index is the number of slots before it, and its generation is 0.
  4. GET handle prints the value. SET handle value replaces it and prints OK.
  5. DESTROY handle removes the object, adds 1 to its slot's generation, pushes the slot onto the free list, and prints OK.
  6. GET, SET, and DESTROY with a stale handle print STALE and change nothing. So a second destroy of the same object prints STALE and does not free its slot again.
  7. The slot array never shrinks.

The new generation makes every copy of the old handle stale, so it can't reach the slot's next occupant. That is the fix for the ABA problem from Container Invalidation and Stable Handles.

A free slot's generation can equal a handle's. After DESTROY 1:0, slot 1 is free at generation 1, and 1:1 stays stale until a CREATE reuses slot 1, because the slot holds no object.

Output of STATS:

STATS live slots free
  • live: the number of objects in the pool.
  • slots: the length of the slot array.
  • free: the length of the free list.

Limits and guarantees

  • At most 105 commands. capacity is 1 to 105, and max_generation is 0 to 109.
  • Every command is well formed. Every handle has the index:generation form, both numbers from 0 to 109.
  • A handle need not be one the pool ever printed: a component can hold one long after its object is gone, and a corrupted message can carry one that never existed.
  • The SCAN lines print at most 106 objects in total.

Tests 1–3 cover this part.

Part 2: Fixed capacity and retired slots

A real pool is sized once at startup, and its generation lives in a fixed number of bits. Wrapping back to 0 would make a long-ago handle live again and read a different object, so this pool never wraps.

  1. The slot array holds at most capacity slots. A CREATE that finds the free list empty and the slot array already capacity slots long prints FULL and changes nothing.
  2. A DESTROY whose slot's generation is already max_generation retires the slot and prints RETIRED instead of OK. The slot stays empty at generation max_generation, is not pushed onto the free list, and no later CREATE reuses it.
  3. A retired slot stays in the slot array and counts toward capacity. A pool whose slots have all retired prints FULL even when it holds no objects.
  4. STATS counts a retired slot in slots but not in free, so slots - live - free is the number of retired slots.
  5. With max_generation equal to 0, every destroy retires its slot.

Tests 4–7 cover this part.

Part 3: Iterating the live objects

Repricing every resting order after a price move visits every live object, but the slot array also holds free and retired slots. So the pool keeps a dense array: the values of exactly the live objects, packed with no gaps, each with its owning slot.

  1. CREATE appends the new object to the end of the dense array.
  2. DESTROY, whether it prints OK or RETIRED, does a swap-remove: the object at the end of the dense array moves into the destroyed object's position, and the array shrinks by one. If the destroyed object is already at the end, nothing moves.
  3. The moved object's handle does not change; only its position does.
  4. SET changes a value in place.
  5. FULL and every command that prints STALE leave the dense array unchanged.

Output of SCAN:

SCAN handle=value handle=value ...
  • One handle=value entry per live object, in dense-array order, separated by single spaces. For example: SCAN 0:0=10 3:0=40 2:0=30.
  • With no live objects it prints SCAN NONE.

Tests 8–10 cover this part.

Example
Input
1000
1000000
12
CREATE 300
CREATE 150
CREATE 500
GET 1:0
DESTROY 1:0
GET 1:0
CREATE 200
GET 1:0
GET 1:1
SET 0:0 250
GET 0:0
STATS
Output
0:0
1:0
2:0
150
OK
STALE
1:1
STALE
200
OK
250
STATS 3 3 0
Explanation

The first two lines are capacity and max_generation, both too large to matter here. Three orders fill slots 0, 1, and 2 at generation 0.

Destroying 1:0 frees slot 1 and raises its generation to 1, so GET 1:0 is stale. The next CREATE reuses slot 1 and prints 1:1.

The old handle 1:0 still reads STALE, not the new order's 200, while 1:1 reads 200. At the end the pool holds three objects in three slots, and the free list is empty.