# Doubly linked list

Implement an in-memory **doubly linked list**: a sequence of 64-bit signed
integer values in which every element knows its successor and its
predecessor, so that an element can be inserted or removed anywhere in
constant time.

This is the classic doubly linked list, **not** a dynamic array, a deque of
blocks, an unrolled list that keeps several values per node, an XOR-linked
list, a skip list, or a tree.

## Structure

- **One node per value.** Every value in the list lives in its own node. A
  node holds exactly three things: its value, a link to the next node, and a
  link to the previous node. A list of `n` values has exactly `n` nodes
  (plus, if you want them, a fixed number of sentinel nodes).
- **A link identifies one node directly**, as a pointer, a reference, or an
  index into a pool of nodes. Following a link is a single step: no search
  and no lookup in another structure. The two links are stored separately
  (not combined, as in an XOR list).
- **The order of the list exists only in the links.** There is no array of
  the elements in list order, no index by position, no cache of positions,
  and no extra links that skip over nodes.
- **Handles.** Every operation that adds a value returns a *handle* for the
  new node: an unsigned 64-bit integer of your choosing (for example the
  node's address, or its index plus one) that the caller later passes back
  to name that node.
  - A handle is never `0`. `0` means "no node".
  - A handle stays valid, and keeps naming the same node, for as long as its
    node is in the list, whatever else happens to the list. Nodes are never
    moved in a way that changes their handles.
  - After its node is removed, a handle is dead, and the same number may be
    given to a node added later.
  - Turning a handle into its node is a constant-time computation (a cast or
    an array index), not a lookup in a table or map.
- Where nodes are allocated is up to you: individually, in blocks, or in one
  pool. Whatever you choose, the memory of removed nodes must be reused for
  nodes added later.
- Any `i64` is a valid value. Values need not be distinct.

These invariants must hold after every operation returns.

## Operations

| Operation | Behaviour |
| --- | --- |
| `new()` | Create an empty list. |
| `push_front(value) -> handle` | Add a value as the new first element. |
| `push_back(value) -> handle` | Add a value as the new last element. |
| `insert_before(node, value) -> handle` | Add a value immediately before the given node. |
| `insert_after(node, value) -> handle` | Add a value immediately after the given node. |
| `remove(node) -> value` | Unlink the given node and return its value. |
| `pop_front() -> value or absent` | Remove the first element and return its value. Absent if the list is empty. |
| `pop_back() -> value or absent` | Remove the last element and return its value. Absent if the list is empty. |
| `move_to_front(node)` | Make the given node the first element, keeping its handle and value. Nothing changes if it already is the first. |
| `front() -> handle` | The first node, or `0` if the list is empty. |
| `back() -> handle` | The last node, or `0` if the list is empty. |
| `next(node) -> handle` | The node after the given one, or `0` if it is the last. |
| `prev(node) -> handle` | The node before the given one, or `0` if it is the first. |
| `get(node) -> value` | The value of the given node. |
| `len() -> count` | Number of elements in the list. |

Wherever an operation takes a `node`, the caller passes the handle of a node
that is currently in the list. The driver never passes `0` or a dead handle,
and the list need not detect it.

**Every operation is `O(1)`**, apart from the occasional growth of a node
pool, which must be amortised `O(1)` per added value.

A list that has been emptied must be fully usable again.

## Rules

- Write the linked list yourself. Do not use or wrap a list, deque, or other
  sequence container from a library to hold the elements or their order
  (`std::list`, `LinkedList`, `container/list`, `collections.deque`, ...).
  A plain growable array used as a pool of nodes is fine.
- Use only the language's standard library. No third-party packages and no
  code in another language (no C extensions, FFI, JNI, cgo, or inline assembly).
- No architecture-specific SIMD intrinsics (`<arm_neon.h>`, `std::arch`,
  `System.Runtime.Intrinsics`, and the like). Portable features of the
  language and its compiler are fine: branch hints, prefetch hints, and
  portable vector types.
- Single-threaded: do not start threads or processes.
- Do not change process-wide runtime settings (garbage collector tuning,
  JIT flags, recursion limits, allocator replacement, and so on).
- Low-level utilities from the standard library are fine: arrays, block
  copies, bit operations, and similar.
- Memory use must stay proportional to the largest number of elements the
  list has held at one time, and everything is released when the list is
  destroyed in languages with manual memory management.
- Language features that trade safety for speed (for example `unsafe` in
  Rust) are permitted.

The exact names, signatures, and file names for your language are given in
the language binding that accompanies this specification. The benchmark
driver calls exactly that interface.

## How it is judged

1. **Correctness.** A fixed test suite drives the list through ten workloads
   at several sizes up to 100,000 elements, and compares every result
   against a reference. After each workload the whole list is walked in both
   directions and every link is checked against its neighbour's.
2. **Speed.** The same ten workloads are then timed on a list of 3,000,000
   elements, which is far larger than the CPU caches. Only the time spent
   inside your operations is measured, so write for performance: the goal is
   the fastest correct doubly linked list you can write in this language.
