Documentation

CslibFrontier.DataStructure.Dictionary.WordRAM.LinearProbing

Open-Addressed Linear Probing on the Randomized Word RAM #

This file implements a self-contained open-addressed linear-probing hash table realizing Cslib.DataStructure.Dictionary.spec U V n_max on the randomized Word RAM (Cslib.DataStructure.WordRAM.Randomized.StateTransitionImpl), adapting Pagh, Pagh and Ružić, Linear Probing with Constant Independence, SIAM J. Comput. 39(3):1107–1120, 2009, Section 2.3 (Lemma 2.1, pp. 1110–1111) and Section 4 (Lemma 4.1, Theorem 4.3, and Theorem 4.5, pp. 1114–1118); Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, Section 11.4.

Design #

  1. Power-of-$2$ open-addressed layout ($O(n_{\max})$ words, zero tombstones): For capacity bound $n_{\max}$, we set $M = \mathrm{tableLog}(n_{\max}) = \lceil \log_2 \max(8 n_{\max}, 8) \rceil$ and table size $m = 2^M$ (tableSize n_max), so that $8 n_{\max} \le 2^M < 16 \max(n_{\max}, 1)$ (load factor $\alpha \le 1/8$). Memory at base consists of:

    • [base, base + KWise.space n_max 5): embedded $5$-wise independent trinomial-field hash state (Cslib.DataStructure.HashFamily.WordRAM.KWise);
    • [usedBase, usedBase + 2^M): occupancy flags used[0 .. 2^M - 1] (0 = empty, 1 = occupied, so zero-initialized memory is immediately a valid empty table);
    • [keyBase, keyBase + 2^M): stored keys key[0 .. 2^M - 1];
    • [valBase, valBase + 2^M): stored values val[0 .. 2^M - 1]. Total footprint is space n_max = KWise.space n_max 5 + 3 * tableSize n_max = O(n_max) words.
  2. Self-contained operations (initSetup, query, insert):

    • initSetup: runs KWise.initCode 5 ($O(n_{\max})$ deterministic Four-Russians carryless multiplication table setup plus $5$ Code.rand calls, with zero caller-supplied primes).
    • query u and insert u v: evaluate $b = h_\theta(u) \in \mathrm{Fin}(2^M)$ via KWise.evalCode 5 in $O(L^2 + 1)$ deterministic steps ($L = \mathrm{limbCount}(n_{\max}, U, M) = O(1)$ when $U \le n_{\max}^c$), then scan $(b + d) \bmod 2^M$ until reaching the first slot p where used[p] = 0 or key[p] = u. Because at most $n_{\max} \le 2^M / 8$ slots are occupied, at least $7/8$ of all slots are empty and the probe loop terminates deterministically in $\le n_{\max} < 2^M$ iterations on every seed.
  3. Expected $O(1)$ probe bound under $5$-wise independence: If probing for $u$ from $b = h_\theta(u)$ steps over $t \ge 1$ slots occupied by keys in $S = \mathrm{dom}(s) \setminus \{u\}$, walking backwards from $b$ to the nearest preceding empty slot yields a contiguous cyclic interval cyclicInterval M b a ℓ of length $\ell = a + t \in [t, \#S]$ whose $\ell$ stored keys in $S$ all hash into cyclicInterval M b a ℓ (TableInv.hasClusterWitness_probeLen, adapting the occupied-run argument in the proof of Lemma 4.1, p. 1114 of [PaghPaghRuzic2009]). Applying tsum_uniform_probe_le_of_cluster_witness from CslibFrontier.DataStructure.Dictionary.WordRAM.LinearProbing.FiveWise bounds the expected number of skipped occupied slots TableInv.probeLen by $288 = O(1)$ (tsum_probeLen_le).

Source mapping, specialization, and cost scope #

In [PaghPaghRuzic2009], Section 4 (Theorems 4.3 and 4.5, pp. 1115–1118, built on Lemma 2.1, pp. 1110–1111, and Lemma 4.1, p. 1114) proves that for any sequence of insertions, deletions, and lookups in a linear-probing hash table of size $r$ using a hash function chosen uniformly at random from a $5$-wise independent family $\mathcal{H} : U \to [r]$ with maximum load $\bar{\alpha} < 1$, the expected number of probes made during an operation is $O((1 - \bar{\alpha})^{-5/2})$ (Theorem 4.3) and $O((1 - \bar{\alpha})^{-13/6})$ (Theorem 4.5), which is $O(1)$ for bounded load factor. The declarations in this module and LinearProbing.FiveWise map to [PaghPaghRuzic2009] as follows:

References #

Section 1: Parameters, memory layout, and cyclic probe geometry #

@[reducible, inline]

Latent seed type for the embedded $5$-wise independent trinomial-field hash family: $5$ coefficients in $\mathrm{Fin}(2^{\mathrm{trinomialDeg}(U, \mathrm{tableLog}(n_{\max}))})$.

Uniform reference distribution over Seed U n_max.

noncomputable def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.hashFn (U n_max : ℕ) (θ : Seed U n_max) (u : Fin U) :
Fin (2 ^ tableLog n_max)

The $5$-wise independent hash function $h_\theta : \mathrm{Fin}\,U \to \mathrm{Fin}(2^M)$ associated with seed $\theta$.

Base address of the occupancy array used[0 .. 2^M - 1].

Base address of the key array key[0 .. 2^M - 1].

Base address of the value array val[0 .. 2^M - 1].

Total contiguous memory footprint of the linear-probing dictionary (in words): KWise.space n_max 5 + 3 * tableSize n_max = O(n_max).

space n_max is $O(n_{\max})$: specifically space n_max ≤ 49 * max n_max 4 + 13.

Section 2: Open-addressed table invariant, termination, and cluster witness #

Occupancy flag used[p] at slot p : Fin (2 ^ tableLog n_max).

def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.keyAt (base n_max : ℕ) (mem : ℕ → ℕ) (p : Fin (2 ^ tableLog n_max)) :

Stored key key[p] at slot p : Fin (2 ^ tableLog n_max).

def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.valAt (base n_max : ℕ) (mem : ℕ → ℕ) (p : Fin (2 ^ tableLog n_max)) :

Stored value val[p] at slot p : Fin (2 ^ tableLog n_max).

structure Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv (U V n_max base : ℕ) (θ : Seed U n_max) (s : State U) (mem : ℕ → ℕ) :

Linear-probing table invariant at (base, n_max) coupling abstract state s : Dictionary.State U and seed θ : Seed U n_max with concrete memory mem:

  1. (Dictionary.dom s).card ≤ n_max and Dictionary.ValuesBounded V s;
  2. every slot p has usedAt p ∈ {0, 1};
  3. every occupied slot p (usedAt p = 1) stores a valid key keyAt p < U and value valAt p < V matching s ⟨keyAt p, _⟩ = some (valAt p);
  4. occupied slots store pairwise distinct keys;
  5. every present key u with s u = some v resides at some probe offset k < 2 ^ M from hashFn U n_max θ u such that all preceding probe offsets j < k are occupied (usedAt = 1);
  6. conversely, every occupied slot p (usedAt p = 1) is reachable from its key's home bucket hashFn U n_max θ ⟨keyAt p, _⟩ at some offset k < 2 ^ M along an entirely occupied prefix (∀ j < k, usedAt = 1).
  • card_dom_le : (dom s).card ≤ n_max

    Number of stored keys is at most n_max.

  • values_bounded : ValuesBounded V s

    Every stored value in s is strictly less than V.

  • used_bit (p : Fin (2 ^ tableLog n_max)) : usedAt base n_max mem p = 0 ∨ usedAt base n_max mem p = 1

    Every occupancy entry is 0 or 1.

  • occupied_key_lt (p : Fin (2 ^ tableLog n_max)) : usedAt base n_max mem p = 1 → keyAt base n_max mem p < U

    Every occupied slot holds a key < U.

  • occupied_state (p : Fin (2 ^ tableLog n_max)) (u : Fin U) : usedAt base n_max mem p = 1 → keyAt base n_max mem p = ↑u → s u = some (valAt base n_max mem p)

    Every occupied slot agrees with the abstract dictionary s.

  • occupied_inj (p₁ p₂ : Fin (2 ^ tableLog n_max)) : usedAt base n_max mem p₁ = 1 → usedAt base n_max mem p₂ = 1 → keyAt base n_max mem p₁ = keyAt base n_max mem p₂ → p₁ = p₂

    Distinct occupied slots hold distinct keys.

  • present_slot (u : Fin U) (v : ℕ) : s u = some v → ∃ k < 2 ^ tableLog n_max, let p := probeSlot (tableLog n_max) (hashFn U n_max θ u) k; usedAt base n_max mem p = 1 ∧ keyAt base n_max mem p = ↑u ∧ valAt base n_max mem p = v ∧ ∀ j < k, usedAt base n_max mem (probeSlot (tableLog n_max) (hashFn U n_max θ u) j) = 1

    Every present key u with s u = some v is stored at some offset k < 2 ^ M from its hash bucket with all intermediate probe slots j < k occupied.

  • occupied_prefix (p : Fin (2 ^ tableLog n_max)) (u : Fin U) : usedAt base n_max mem p = 1 → keyAt base n_max mem p = ↑u → ∃ k < 2 ^ tableLog n_max, p = probeSlot (tableLog n_max) (hashFn U n_max θ u) k ∧ ∀ j < k, usedAt base n_max mem (probeSlot (tableLog n_max) (hashFn U n_max θ u) j) = 1

    Every occupied slot p has a contiguous occupied probe prefix from its key's hash bucket.

theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.card_slots_le {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (A : Finset (Fin (2 ^ tableLog n_max))) (K : Finset (Fin U)) (hused : ∀ p ∈ A, usedAt base n_max mem p = 1) (hkey : ∀ (p : Fin (2 ^ tableLog n_max)) (hp : p ∈ A), ⟨keyAt base n_max mem p, ⋯⟩ ∈ K) :

Any finset of occupied slots A whose keys belong to K : Finset (Fin U) has cardinality #A ≤ #K.

theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.key_mem_erase_of_ne {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) {p : Fin (2 ^ tableLog n_max)} (hp : usedAt base n_max mem p = 1) (u : Fin U) (hne : keyAt base n_max mem p ≠ ↑u) :
⟨keyAt base n_max mem p, ⋯⟩ ∈ (dom s).erase u

Every occupied slot p with keyAt p ≠ u.val maps to a key in (Dictionary.dom s).erase u.

def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.probeContinue (base n_max : ℕ) (mem : ℕ → ℕ) (u : ℕ) (b : Fin (2 ^ tableLog n_max)) (d : ℕ) :

Probing for key u from bucket b continues past offset d when slot probeSlot M b d is occupied (used = 1) by a key different from u.

Instances For
    @[instance_reducible]
    instance Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.instDecidableProbeContinue (base n_max : ℕ) (mem : ℕ → ℕ) (u : ℕ) (b : Fin (2 ^ tableLog n_max)) (d : ℕ) :
    Decidable (probeContinue base n_max mem u b d)
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeContinue_prefix_le {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) {d : ℕ} (hd : d ≤ 2 ^ tableLog n_max) (hcont : ∀ i < d, probeContinue base n_max mem (↑u) b i) :
    d ≤ ((dom s).erase u).card

    If probing for u : Fin U from b continues across all i < d (d ≤ 2 ^ M), then d ≤ #((Dictionary.dom s).erase u).

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.exists_not_probeContinue {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :
    ∃ d ≤ ((dom s).erase u).card, ¬probeContinue base n_max mem (↑u) b d

    Under TableInv, probing for any u : Fin U from any b stops within #((Dictionary.dom s).erase u) steps.

    noncomputable def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeLen {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :

    Number of probe steps from b before reaching an empty slot or key u.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeLen_congr {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem₁ mem₂ : ℕ → ℕ} (h₁ : TableInv U V n_max base θ s mem₁) (h₂ : TableInv U V n_max base θ s mem₂) (hmem : mem₁ = mem₂) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :
    h₁.probeLen u b = h₂.probeLen u b
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeLen_le_card_erase {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :
    hinv.probeLen u b ≤ ((dom s).erase u).card
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeLen_le_n_max {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :
    hinv.probeLen u b ≤ n_max
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeLen_lt_tableSize {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :
    hinv.probeLen u b < 2 ^ tableLog n_max
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.not_probeContinue_probeLen {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) :
    ¬probeContinue base n_max mem (↑u) b (hinv.probeLen u b)
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeContinue_of_lt_probeLen {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (b : Fin (2 ^ tableLog n_max)) {d : ℕ} (hd : d < hinv.probeLen u b) :
    probeContinue base n_max mem (↑u) b d
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.probeStop_spec {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) :
    have b := hashFn U n_max θ u; have t := hinv.probeLen u b; have p := probeSlot (tableLog n_max) b t; usedAt base n_max mem p = 0 ∧ s u = none ∨ usedAt base n_max mem p = 1 ∧ keyAt base n_max mem p = ↑u ∧ s u = some (valAt base n_max mem p)

    Exact characterization of the stopping slot p = probeSlot M (hashFn U n_max θ u) t where t = hinv.probeLen u (hashFn U n_max θ u):

    • if usedAt p = 0, then s u = none;
    • if usedAt p = 1, then keyAt p = u.val and s u = some (valAt p).
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.eq_probeStop_of_key_eq {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) {p : Fin (2 ^ tableLog n_max)} (hp1 : usedAt base n_max mem p = 1) (hkey : keyAt base n_max mem p = ↑u) :
    p = probeSlot (tableLog n_max) (hashFn U n_max θ u) (hinv.probeLen u (hashFn U n_max θ u))

    If a slot p has usedAt p = 1 and keyAt p = u.val, then p is the stopping slot probeSlot M (hashFn U n_max θ u) (hinv.probeLen u (hashFn U n_max θ u)).

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.tableInv_empty (U V n_max base : ℕ) (θ : Seed U n_max) {mem : ℕ → ℕ} (hzero : ∀ (p : Fin (2 ^ tableLog n_max)), usedAt base n_max mem p = 0) :
    TableInv U V n_max base θ (empty U) mem

    Zeroed occupancy array ∀ p, usedAt base n_max mem p = 0 satisfies TableInv for Dictionary.empty U.

    def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.insertMem (base n_max : ℕ) (mem : ℕ → ℕ) (p : Fin (2 ^ tableLog n_max)) (u v : ℕ) :
    ℕ → ℕ

    Memory update performed by insert u v once the stopping slot p has been located: writes used[p] ← 1, key[p] ← u, and val[p] ← v.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.usedAt_insertMem (base n_max : ℕ) (mem : ℕ → ℕ) (p q : Fin (2 ^ tableLog n_max)) (u v : ℕ) :
    usedAt base n_max (insertMem base n_max mem p u v) q = if q = p then 1 else usedAt base n_max mem q
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.keyAt_insertMem (base n_max : ℕ) (mem : ℕ → ℕ) (p q : Fin (2 ^ tableLog n_max)) (u v : ℕ) :
    keyAt base n_max (insertMem base n_max mem p u v) q = if q = p then u else keyAt base n_max mem q
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.valAt_insertMem (base n_max : ℕ) (mem : ℕ → ℕ) (p q : Fin (2 ^ tableLog n_max)) (u v : ℕ) :
    valAt base n_max (insertMem base n_max mem p u v) q = if q = p then v else valAt base n_max mem q
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.insert_mem {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (v : ℕ) (hvalid : (spec U V n_max).valid s (Op.insert u v)) :
    have b := hashFn U n_max θ u; have t := hinv.probeLen u b; have p := probeSlot (tableLog n_max) b t; TableInv U V n_max base θ (Function.update s u (some v)) (insertMem base n_max mem p (↑u) v)

    Writing (1, u.val, v) at the stopping slot p = probeSlot M (hashFn U n_max θ u) t preserves TableInv for Function.update s u (some v) whenever .insert u v is admissible.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.TableInv.hasClusterWitness_probeLen {U V n_max base : ℕ} {θ : Seed U n_max} {s : State U} {mem : ℕ → ℕ} (hinv : TableInv U V n_max base θ s mem) (u : Fin U) (ht : 1 ≤ hinv.probeLen u (hashFn U n_max θ u)) :
    ∃ (a : ℕ) (ℓ : ℕ), a < ℓ ∧ hinv.probeLen u (hashFn U n_max θ u) ≤ ℓ ∧ ℓ ≤ ((dom s).erase u).card ∧ ℓ ≤ {x ∈ (dom s).erase u | hashFn U n_max θ x ∈ cyclicInterval (tableLog n_max) (hashFn U n_max θ u) a ℓ}.card

    Left-anchored cluster witness theorem: if probing key u from b = hashFn U n_max θ u takes t = hinv.probeLen u b ≥ 1 steps, there is a contiguous cyclic interval cyclicInterval M b a ℓ (a < ℓ, t ≤ ℓ ≤ #((Dictionary.dom s).erase u)) into which at least ℓ keys of (Dictionary.dom s).erase u hash.

    @[instance_reducible]
    noncomputable instance Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.instDecidablePredHasClusterWitness (U n_max : ℕ) (S : Finset (Fin U)) (u : Fin U) (θ : Seed U n_max) :
    noncomputable def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.maxWitnessProbe (U n_max : ℕ) (S : Finset (Fin U)) (u : Fin U) (θ : Seed U n_max) :

    Maximum t ≤ #S satisfying HasClusterWitness (hashFn U n_max) S u θ t (depends only on θ).

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.le_maxWitnessProbe {U n_max : ℕ} {S : Finset (Fin U)} {u : Fin U} {θ : Seed U n_max} {t : ℕ} (ht_le : t ≤ S.card) (hwit : HasClusterWitness (hashFn U n_max) S u θ t) :
    t ≤ maxWitnessProbe U n_max S u θ
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.tsum_probeLen_le {α : Type u_1} {U V n_max base : ℕ} {s : State U} (u : Fin U) (ν : PMF (Seed U n_max × α)) (hseed : ∀ (θ : Seed U n_max), (PMF.map Prod.fst ν) θ ≤ (seedLaw U n_max) θ) (T_probe : Seed U n_max × α → ℕ) (hT : ∀ p ∈ ν.support, ∃ (mem : ℕ → ℕ) (hinv : TableInv U V n_max base p.1 s mem), T_probe p ≤ hinv.probeLen u (hashFn U n_max p.1 u)) :
    ∑' (p : Seed U n_max × α), ν p * ↑(T_probe p) ≤ 288

    For any coupled state distribution ν : PMF (Seed U n_max × α) whose seed marginal is bounded by seedLaw U n_max, any step counter T_probe : Seed U n_max × α → ℕ bounded by hinv.probeLen on ν.support has expected value at most 288.

    Section 3: Word-RAM register layout, programs, and operational execution #

    @[reducible, inline]

    Total number of registers used by the linear-probing subroutine (0 .. 17 for KWise and probe scratch, 18 .. 23 for allocation, zero-fill, and operation arguments).

    Register embedding Fin 18 → Fin 24 mapping KWise registers 0 .. 17 to 0 .. 17.

    Allocation span program: copies frontierReg (18) to baseReg (0) and advances frontierReg by space n_max.

    18-register initialization program setting (n_max, U, tableLog n_max) in registers 1, 2, 3 and invoking KWise.initCode 5.

    Single-slot probe inspection program: given current slot pos in reg 2, loads used[pos] into reg 9 (cellReg) and sets reg 7 (contReg) to 1 if used[pos] = 1 ∧ key[pos] ≠ u.val, or 0 otherwise.

    Probe setup program: initializes maskReg (3), usedBaseReg (4), keyBaseReg (5), valBaseReg (6), and cOneReg (10), then inspects the home slot pos = (h u).val (reg 2).

    Probe loop body: advances pos ← (pos + 1) &&& (2 ^ M - 1) in reg 2 and inspects the new slot via probeCheckCode.

    Insertion suffix after probeLoop halts at slot pos (reg 2): stores used[pos] ← 1, key[pos] ← u.val (reg 1), val[pos] ← v (reg 23), and sets outReg (reg 2) to 0.

    Lookup suffix after probeLoop halts at slot pos (reg 2) with used[pos] in reg 9: if reg 9 == 1, loads val[pos] and writes val[pos] + 1 into reg 2; otherwise writes 0 into reg 2.

    Unified operation program for query u (opReg = 0) and insert u v (opReg = 1).

    Standalone argument encoding for Dictionary.Op U.

    Argument-matching predicate for Dictionary.Op U.

    def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.decodeOp {U V n_max : ℕ} (o : (spec U V n_max).Op) (regs : Fin rLP → ℕ) :
    Option ((spec U V n_max).Result o)

    Output decoder for Dictionary.Op U: for query u, 0 decodes to none and k + 1 decodes to some k; for insert u v, decodes to some ().

    Expected instruction count for initSetup U n_max: KWise.initTableSteps n_max U (tableLog n_max) + 38 = O(n_max + log U).

    Expected instruction charge per dictionary operation: KWise.evalSteps n_max U (tableLog n_max) 5 + 3483 = O(L ^ 2 + 1).

    structure Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.Rep (U V n_max w base : ℕ) (θ : Seed U n_max) (s : State U) (x : WordRAM.Data rLP) :

    Full seed-coupled representation invariant Rep U V n_max w base θ s x for linear probing.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.Rep.unique {U V n_max w base : ℕ} {θ : Seed U n_max} {s₁ s₂ : State U} {x : WordRAM.Data rLP} (h₁ : Rep U V n_max w base θ s₁ x) (h₂ : Rep U V n_max w base θ s₂ x) :
    s₁ = s₂
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.Rep.supported {U V n_max w base : ℕ} {θ : Seed U n_max} {s : State U} {x x' : WordRAM.Data rLP} (hregs : ∀ (i : Fin rLP), (subroutineCode U n_max).input i = false → x'.regs i = x.regs i) (hmem : ∀ a ∈ { base := base, length := space n_max }.footprint, x'.mem a = x.mem a) (hbdd : WordRAM.Data.Bounded w x') (hrep : Rep U V n_max w base θ s x) :
    Rep U V n_max w base θ s x'
    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.allocSpan_execWithin {w base n_max : ℕ} {x : WordRAM.Data rLP} (hfr : x.regs 18 = base) (hfit : WordRAM.Fits w (base + space n_max)) :
    ∃ (x₁ : WordRAM.Data rLP), WordRAM.ExecWithin w (allocSpan n_max) x x₁ 3 ∧ x₁.mem = x.mem ∧ x₁.regs 0 = base ∧ x₁.regs 18 = base + space n_max

    allocSpan n_max executes in 3 steps, copying frontierReg (18) to baseReg (0) and advancing frontierReg to base + space n_max.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.initSetup_exec {U V n_max w base : ℕ} {x₁ : WordRAM.Data rLP} (hw : 2 * Probability.trinomialDeg U (tableLog n_max) ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max)) (hx₁ : WordRAM.Data.Bounded w x₁) (hb : x₁.regs 0 = base) (hfr : x₁.regs 18 = base + space n_max) (hz : ∀ a ∈ { base := base, length := space n_max }.footprint, x₁.mem a = 0) :
    ∃ (ν₀ : PMF (Seed U n_max × WordRAM.Data rLP)), WordRAM.ExecCodeDist w (initSetup U n_max) x₁ (PMF.map Prod.snd ν₀) ↑(setupTime U n_max) ∧ (∀ (θ : Seed U n_max), (PMF.map Prod.fst ν₀) θ ≤ 1 * (seedLaw U n_max) θ) ∧ ∀ p ∈ ν₀.support, p.2.regs 18 = base + space n_max ∧ Rep U V n_max w base p.1 (empty U) p.2 ∧ 0 ≤ 0 ∧ WordRAM.Modifies { base := base, length := space n_max }.footprint x₁.mem p.2.mem

    initSetup U n_max initializes the 5-wise independent hash table and leaves the zeroed linear-probing arrays representing the empty dictionary in at most setupTime U n_max expected instructions.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.probeCheckCode_execWithin {U w base n_max : ℕ} (u : Fin U) (pos : Fin (2 ^ tableLog n_max)) {x : WordRAM.Data rLP} (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (h2 : x.regs 2 = ↑pos) (h3 : x.regs 3 = tableSize n_max - 1) (h4 : x.regs 4 = usedBase base n_max) (h5 : x.regs 5 = keyBase base n_max) (h6 : x.regs 6 = valBase base n_max) (h10 : x.regs 10 = 1) (hfit_space : WordRAM.Fits w (base + space n_max)) :
    ∃ (y : WordRAM.Data rLP), WordRAM.ExecWithin w probeCheckCode x y 8 ∧ y.mem = x.mem ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑pos ∧ y.regs 3 = tableSize n_max - 1 ∧ y.regs 4 = usedBase base n_max ∧ y.regs 5 = keyBase base n_max ∧ y.regs 6 = valBase base n_max ∧ (y.regs 7 = if usedAt base n_max x.mem pos = 1 ∧ keyAt base n_max x.mem pos ≠ ↑u then 1 else 0) ∧ y.regs 9 = usedAt base n_max x.mem pos ∧ y.regs 10 = 1 ∧ y.regs 22 = x.regs 22 ∧ y.regs 23 = x.regs 23

    Single-slot inspection probeCheckCode executes in at most 8 deterministic steps, loading usedAt base n_max x.mem pos into reg 9 and setting reg 7 to 1 iff usedAt base n_max x.mem pos = 1 ∧ keyAt base n_max x.mem pos ≠ u.val.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.probeSetupCode_execWithin {U w base n_max : ℕ} (u : Fin U) (pos : Fin (2 ^ tableLog n_max)) {x : WordRAM.Data rLP} (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (h2 : x.regs 2 = ↑pos) (hfit_space : WordRAM.Fits w (base + space n_max)) :
    ∃ (y : WordRAM.Data rLP), WordRAM.ExecWithin w (probeSetupCode n_max) x y 16 ∧ y.mem = x.mem ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑pos ∧ y.regs 3 = tableSize n_max - 1 ∧ y.regs 4 = usedBase base n_max ∧ y.regs 5 = keyBase base n_max ∧ y.regs 6 = valBase base n_max ∧ (y.regs 7 = if usedAt base n_max x.mem pos = 1 ∧ keyAt base n_max x.mem pos ≠ ↑u then 1 else 0) ∧ y.regs 9 = usedAt base n_max x.mem pos ∧ y.regs 10 = 1 ∧ y.regs 22 = x.regs 22 ∧ y.regs 23 = x.regs 23

    probeSetupCode n_max executes in at most 16 deterministic steps, initializing registers 3, 4, 5, 6, 10 and running probeCheckCode on the initial hash bucket pos.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.probeBody_execWithin {U w base n_max : ℕ} (u : Fin U) (pos : Fin (2 ^ tableLog n_max)) {x : WordRAM.Data rLP} (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (h2 : x.regs 2 = ↑pos) (h3 : x.regs 3 = tableSize n_max - 1) (h4 : x.regs 4 = usedBase base n_max) (h5 : x.regs 5 = keyBase base n_max) (h6 : x.regs 6 = valBase base n_max) (h10 : x.regs 10 = 1) (hfit_space : WordRAM.Fits w (base + space n_max)) :
    have pos' := probeSlot (tableLog n_max) pos 1; ∃ (y : WordRAM.Data rLP), WordRAM.ExecWithin w probeBody x y 10 ∧ y.mem = x.mem ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑pos' ∧ y.regs 3 = tableSize n_max - 1 ∧ y.regs 4 = usedBase base n_max ∧ y.regs 5 = keyBase base n_max ∧ y.regs 6 = valBase base n_max ∧ (y.regs 7 = if usedAt base n_max x.mem pos' = 1 ∧ keyAt base n_max x.mem pos' ≠ ↑u then 1 else 0) ∧ y.regs 9 = usedAt base n_max x.mem pos' ∧ y.regs 10 = 1 ∧ y.regs 22 = x.regs 22 ∧ y.regs 23 = x.regs 23

    One iteration of probeBody advances pos to probeSlot (tableLog n_max) pos 1 and inspects the new slot in at most 10 deterministic steps.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.probeLoop_execWithin {U V w base n_max : ℕ} {θ : Seed U n_max} {s : State U} {x : WordRAM.Data rLP} (hinv : TableInv U V n_max base θ s x.mem) (u : Fin U) (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (h2 : x.regs 2 = ↑(hashFn U n_max θ u)) (h3 : x.regs 3 = tableSize n_max - 1) (h4 : x.regs 4 = usedBase base n_max) (h5 : x.regs 5 = keyBase base n_max) (h6 : x.regs 6 = valBase base n_max) (h7 : x.regs 7 = if usedAt base n_max x.mem (hashFn U n_max θ u) = 1 ∧ keyAt base n_max x.mem (hashFn U n_max θ u) ≠ ↑u then 1 else 0) (h9 : x.regs 9 = usedAt base n_max x.mem (hashFn U n_max θ u)) (h10 : x.regs 10 = 1) (hfit_space : WordRAM.Fits w (base + space n_max)) :
    have b := hashFn U n_max θ u; have t := hinv.probeLen u b; have p := probeSlot (tableLog n_max) b t; ∃ (y : WordRAM.Data rLP), WordRAM.ExecWithin w probeLoop x y (12 * t + 2) ∧ y.mem = x.mem ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑p ∧ y.regs 4 = usedBase base n_max ∧ y.regs 5 = keyBase base n_max ∧ y.regs 6 = valBase base n_max ∧ y.regs 9 = usedAt base n_max x.mem p ∧ y.regs 10 = 1 ∧ y.regs 22 = x.regs 22 ∧ y.regs 23 = x.regs 23

    probeLoop halts deterministically at the stopping slot p = probeSlot (tableLog n_max) b (hinv.probeLen u b) in at most 12 * (hinv.probeLen u b) + 2 instructions.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.insertFinishCode_execWithin {U w base n_max : ℕ} (u : Fin U) (v : ℕ) (p : Fin (2 ^ tableLog n_max)) {x : WordRAM.Data rLP} (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (h2 : x.regs 2 = ↑p) (h4 : x.regs 4 = usedBase base n_max) (h5 : x.regs 5 = keyBase base n_max) (h6 : x.regs 6 = valBase base n_max) (h10 : x.regs 10 = 1) (h23 : x.regs 23 = v) (hfit_space : WordRAM.Fits w (base + space n_max)) :
    ∃ (y : WordRAM.Data rLP), WordRAM.ExecWithin w insertFinishCode x y 7 ∧ y.mem = insertMem base n_max x.mem p (↑u) v ∧ y.regs 0 = base ∧ y.regs 2 = 0

    insertFinishCode executes in 7 deterministic steps, writing (1, u.val, v) at slot p (insertMem base n_max x.mem p u.val v) and setting reg 2 (outReg) to 0.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.queryFinishCode_execWithin {U V w base n_max : ℕ} {θ : Seed U n_max} {s : State U} {x : WordRAM.Data rLP} (hinv : TableInv U V n_max base θ s x.mem) (u : Fin U) (h0 : x.regs 0 = base) (h2 : x.regs 2 = ↑(probeSlot (tableLog n_max) (hashFn U n_max θ u) (hinv.probeLen u (hashFn U n_max θ u)))) (h6 : x.regs 6 = valBase base n_max) (h9 : x.regs 9 = usedAt base n_max x.mem (probeSlot (tableLog n_max) (hashFn U n_max θ u) (hinv.probeLen u (hashFn U n_max θ u)))) (h10 : x.regs 10 = 1) (hfit_space : WordRAM.Fits w (base + space n_max)) (hfit_V : WordRAM.Fits w V) :

    queryFinishCode executes in at most 4 deterministic steps, writing val[p] + 1 into reg 2 when s u = some (val[p]) (used[p] = 1) and 0 into reg 2 when s u = none (used[p] = 0).

    The key queried or updated by dictionary operation o.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.opCode_execWithin {U V w base n_max : ℕ} {θ : Seed U n_max} {s : State U} {x : WordRAM.Data rLP} (hw : 2 * Probability.trinomialDeg U (tableLog n_max) ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max)) (hfit_V : WordRAM.Fits w V) (hrep : Rep U V n_max w base θ s x) (o : (spec U V n_max).Op) (hvalid : (spec U V n_max).valid s o) (harg : argMatchOp o x.regs) :
    have u := opKey o; have b := hashFn U n_max θ u; have t := ⋯.probeLen u b; ∃ (y : WordRAM.Data rLP), WordRAM.ExecWithin w (opCode n_max) x y (HashFamily.WordRAM.KWise.evalSteps n_max U (tableLog n_max) 5 + 12 * t + 27) ∧ decodeOp o y.regs = some ((spec U V n_max).stepFn s o).2 ∧ Rep U V n_max w base θ ((spec U V n_max).stepFn s o).1 y ∧ WordRAM.Modifies { base := base, length := space n_max }.footprint x.mem y.mem

    opCode n_max executes any admissible operation o from Rep U V n_max w base θ s x and argMatchOp o x.regs in at most KWise.evalSteps n_max U (tableLog n_max) 5 + 12 * t + 27 deterministic instructions (where t = hrep.table_inv.probeLen (opKey o) (hashFn U n_max θ (opKey o))), preserving Rep for ((Dictionary.spec U V n_max).step s o).1, decoding to some ((Dictionary.spec U V n_max).step s o).2, and modifying only (Block.mk base (space n_max)).footprint.

    Section 4: Randomized.StateTransitionImpl certificate and expected-time sequence theorem #

    noncomputable def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.impl (U V n_max w base : ℕ) (hw : 2 * Probability.trinomialDeg U (tableLog n_max) ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max)) (hfit_V : WordRAM.Fits w V) :

    StateTransitionImpl certificate for open-addressed linear probing on the randomized Word RAM over Cslib.WordRAM.Basis.exact:

    • Space: space n_max = KWise.space n_max 5 + 3 * 2 ^ (tableLog n_max) = O(n_max) words;
    • Initialization time: setupTime U n_max = KWise.initTableSteps n_max U (tableLog n_max) + 38 = O(n_max + log U) expected instructions;
    • Operation charge: charge U n_max = KWise.evalSteps n_max U (tableLog n_max) 5 + 3483 = O(L ^ 2 + 1) expected instructions per query or insert, which is O(1) whenever U ≤ n_max ^ c for any constant c;
    • Potential: 0 (seedSlack := 1).
    noncomputable def Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.implStandard (U V n_max w base : ℕ) (hw : 2 * Probability.trinomialDeg U (tableLog n_max) ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max)) (hfit_V : WordRAM.Fits w V) :

    Standard-basis (Basis.standard) StateTransitionImpl certificate for linear probing, transported from impl via Randomized.StateTransitionImpl.toStandard.

    theorem Cslib.DataStructure.Dictionary.WordRAM.LinearProbing.seq_expected_amortized (U V n_max w base : ℕ) (hw : 2 * Probability.trinomialDeg U (tableLog n_max) ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max)) (hfit_V : WordRAM.Fits w V) {x₀ : WordRAM.Data rLP} (hx₀ : WordRAM.Data.Bounded w x₀) (hfr : x₀.regs 18 = base) (hz : ∀ a ∈ { base := base, length := space n_max }.footprint, x₀.mem a = 0) (ops : List (Op U)) (hvalid : (spec U V n_max).ValidHistory (empty U) ops) :
    ∃ (ν₀ : PMF (Seed U n_max × WordRAM.Data rLP)) (ν_f : PMF (Seed U n_max × WordRAM.Data rLP)) (T_ops : ENNReal), WordRAM.ExecCodeDist w (subroutineCode U n_max).initFresh x₀ (PMF.map Prod.snd ν₀) ↑(3 + setupTime U n_max) ∧ (impl U V n_max w base hw hfit_space hfit_V).ExecStepsOfFn (empty U) ops ν₀ ν_f T_ops ∧ ↑(3 + setupTime U n_max) + T_ops ≤ ↑(3 + setupTime U n_max + ops.length * charge U n_max)

    Expected amortized multi-operation theorem for linear probing: for any admissible sequence ops : List (Dictionary.Op U) starting from the empty dictionary on fresh zeroed memory and fixed independently of the sampled hash seed (oblivious adversary), initFresh followed by the subroutine operation bodies (StateTransitionImpl.ExecSteps) executes with total expected instruction count at most 3 + setupTime U n_max + ops.length * charge U n_max (excluding caller argument delivery, caller dispatch loops, and host-level code specialization).