# B-tree

Implement an in-memory **B-tree**: an ordered map from 64-bit signed integer
keys to 64-bit signed integer values.

This is the classic B-tree (Bayer & McCreight; CLRS chapter 18), **not** a
B+ tree or any other variant.

## Structure

- The tree has **minimum degree `t = 16`**, fixed at compile time.
  - Every node holds at most `2t - 1 = 31` keys.
  - Every node except the root holds at least `t - 1 = 15` keys.
  - The root of a non-empty tree holds at least 1 key.
  - An internal node holding `k` keys has exactly `k + 1` children.
  - All leaves are at the same depth.
- Keys within a node are stored in strictly ascending order. For an internal
  node, every key in child `i` is less than key `i`, and every key in child
  `i + 1` is greater than key `i`.
- **Entries live in every node.** Each key is stored exactly once, together
  with its value, in either an internal node or a leaf. Internal nodes are not
  just routing nodes.
- **No neighbor pointers.** A node holds only its keys, its values, its child
  pointers, and small bookkeeping fields (such as a key count or a leaf flag).
  There are no sibling pointers, no parent pointers, and no linked list of
  leaves.
- The B-tree is the only structure that stores entries. No side index, hash
  table, or cache of entries or lookups.
- Keys are unique. Keys are compared as signed 64-bit integers; any `i64`
  value is a valid key and any `i64` value is a valid value.

These invariants must hold after every operation returns.

## Operations

| Operation | Behaviour |
| --- | --- |
| `new()` | Create an empty tree. |
| `insert(key, value) -> bool` | If `key` is absent, add the entry and return `true`. If `key` is present, replace its value and return `false`. |
| `get(key) -> value or absent` | Return the value stored for `key`, or report that the key is absent. |
| `remove(key) -> bool` | If `key` is present, delete its entry and return `true`. Otherwise change nothing and return `false`. |
| `range(lo, hi, limit) -> entries` | Produce the entries with `lo <= key <= hi` (both bounds inclusive) in ascending key order, stopping after `limit` entries. If `lo > hi` or `limit` is 0 the result is empty. `lo` and `hi` need not be keys in the tree. |
| `len() -> count` | Number of entries in the tree. |
| `height() -> levels` | Number of levels: 0 for an empty tree, 1 when the root is a leaf, and so on. |

Complexity requirements, for a tree of `n` entries:

- `insert`, `get`, `remove`: `O(log n)`.
- `range`: `O(log n + m)` where `m` is the number of entries produced. It
  must walk the tree directly; it must not be built from repeated `get`s or
  from a scan that starts at the smallest key.
- `len`, `height`: `O(1)` or `O(log n)`.

A tree that has been emptied by `remove` must be fully usable again
(`len() == 0`, `height() == 0`, and later inserts work).

## Rules

- Write the B-tree yourself. Do not use or wrap an ordered container, tree, or
  sorted-map from a library (`std::map`, `BTreeMap`, `TreeMap`,
  `sortedcontainers`, ...).
- 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, binary search over an array, and similar.
- Memory use must stay proportional to the largest number of entries the tree
  has held, and everything is released when the tree 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 tree through ten workloads
   at several sizes up to 100,000 entries, and compares every result
   against a reference. Tree height is checked against the bounds implied by
   `t = 16`.
2. **Speed.** The same ten workloads are then timed. Only the time spent
   inside your operations is measured, so write for performance: the goal is
   the fastest correct B-tree you can write in this language.
