# Hash map

Implement an in-memory **hash map**: an unordered map from 64-bit signed
integer keys to 64-bit signed integer values.

This is a plain **open-addressing hash table with linear probing**, **not**
a chained table, a Robin Hood or cuckoo table, or a SwissTable-style table
that probes in groups.

## Structure

- **One flat table.** All entries live in a single table of `capacity` slots,
  where `capacity` is a power of two. An entry occupies exactly one slot. The
  table may be one array of slots or a few parallel arrays indexed by slot
  number (for example keys, values, and a per-slot state byte).
- **Nothing else stores entries.** No buckets, chains, or per-entry heap
  allocations; no second table, overflow area, side index, or cache of entries
  or lookups.
- **Fixed hash function.** In a table of `capacity = 2^b` slots, the home slot
  of key `k` is the top `b` bits of a wrapping 64-bit multiplication:

  ```
  home(k) = ((k as unsigned 64-bit) * 0x9E3779B97F4A7C15 mod 2^64) >> (64 - b)
  ```

  Do not use any other hash function, and do not add a second one.
- **Linear probing.** The probe sequence of a key is its home slot, then the
  next slot, then the next, wrapping from the last slot to slot 0. A lookup
  examines those slots in that order and stops at the key or at the first
  empty slot (a slot marked as deleted is not empty). An insert of a new key
  places it in a free slot on that same sequence. Entries are not reordered
  to equalise probe lengths.
- **Deletion is your choice**: either mark the slot as deleted (a tombstone)
  or close the gap by shifting later entries back (backward-shift deletion).
  Either way, every remaining key must stay reachable by the lookup rule
  above.
- **Capacity and growth.**
  - A new map has `capacity = 16`.
  - **Maximum load factor 7/8**: `len * 8 <= capacity * 7` at all times.
  - When inserting a new key would break that, the table grows by doubling
    its capacity and re-inserting every entry.
  - Capacity never decreases.
  - Capacity stays bounded: it is never more than twice the smallest capacity
    that satisfies the load factor for the largest number of entries the map
    has ever held. (If you use tombstones, clean them up by rebuilding the
    table at its current capacity, not by growing without limit.)
- Keys are unique. **Any `i64` value is a valid key and any `i64` value is a
  valid value**, including `0`, `-1`, and the minimum and maximum `i64`. If
  you reserve a key value to mark empty or deleted slots, the map must still
  work correctly when that value is used as a key, and the entry with that
  key must itself occupy a slot of the table like any other entry (you may
  remember which slot), not a separate field outside it.

These invariants must hold after every operation returns.

## Operations

| Operation | Behaviour |
| --- | --- |
| `new()` | Create an empty map with capacity 16. |
| `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`. |
| `scan() -> entries` | Produce every entry exactly once, in any order. |
| `len() -> count` | Number of entries in the map. |
| `capacity() -> slots` | Current number of slots in the table. |

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

- `insert`, `get`, `remove`: expected `O(1)`; an insert that grows the table
  is `O(n)`, amortised `O(1)`.
- `scan`: `O(capacity)`. It walks the table directly.
- `len`, `capacity`: `O(1)`.

A map that has been emptied by `remove` must be fully usable again
(`len() == 0`, lookups of absent keys terminate, and later inserts work).

## Rules

- Write the hash table yourself. Do not use or wrap a hash map, hash set,
  dictionary, or other associative container from a library
  (`std::unordered_map`, `HashMap`, `map`, `dict`, ...).
- 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 and fills, bit operations, and similar.
- Memory use must stay proportional to the capacity, and everything is
  released when the map 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 map through ten workloads
   at several sizes up to 100,000 entries, and compares every result
   against a reference. `capacity()` is checked against the rules above.
2. **Speed.** The same ten workloads are then timed on a map of 2,000,000
   entries, 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 hash map of this design you can write in this language.
