Documentation

CslibFrontier.DataStructure.Dictionary.WordRAM.ChainedHash

Chained Hash Table on the Randomized Word RAM #

This file implements a textbook chained hash table ([CLRS09], Sections 11.2 and 11.3.3, Theorem 11.3 and Corollary 11.4) realizing the bounded-capacity insert-only dictionary specification Cslib.DataStructure.Dictionary.spec U V n_max (query and insert up to n_max distinct keys, with deletion deferred to a follow-up) on the randomized Word RAM over Cslib.WordRAM.Basis.exact (and lifted to Basis.standard via StateTransitionImpl.toStandard).

Model, specialization, and cost scope #

Design and memory layout #

Given key universe Fin U and capacity bound n_max, the data structure chooses:

All state resides in a single contiguous memory block [base, base + space n_max) of space n_max = 4 + 2 ^ M + 3 * n_max ≤ 5 * n_max + 8 = O(n_max) words:

Operations and expected complexity #

Main definitions #

Main results #

References #

1. Table dimension parameters and arithmetic bounds #

Logarithmic bucket bit-width M = ⌈log₂(max n_max 2)⌉.

Power-of-two bucket count m = 2 ^ M.

Multiply-shift input bit-width w₀ = max M ⌈log₂(max U 2)⌉.

Total contiguous memory footprint 4 + 2 ^ M + 3 * n_max words.

The power-of-two bucket count numBuckets n_max is at most 2 * n_max + 4.

Linear O(n_max) space bound: space n_max ≤ 5 * n_max + 8.

Canonical injection of keys Fin U into the multiply-shift domain Fin (2 ^ hashBits U n_max).

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.multiplyShift_lt_two_pow {w₀ M : ℕ} (hM : M ≤ w₀) (b : Fin (2 ^ (w₀ - 1))) (x : Fin (2 ^ w₀)) :

Every multiply-shift hash value lies strictly below 2 ^ M.

2. Register layout, memory offsets, and structured Word-RAM code #

@[reducible, inline]

Total local register count of the chained hash table (1 persistent + 21 scratch/caller).

@[reducible, inline]

Multiply-shift seed register (0).

@[reducible, inline]

Multiply-shift key register (1).

@[reducible, inline]

Multiply-shift bucket output register (2).

@[reducible, inline]

Multiply-shift scratch register 1 (3, also curReg for zeroFillCode).

@[reducible, inline]

Multiply-shift scratch register 2 (4).

@[reducible, inline]

Persistent base address register (5).

@[reducible, inline]

Bump-allocation frontier register (6).

@[reducible, inline]

Operation tag register (7: 0 = query, ≠ 0 = insert).

@[reducible, inline]

Input key register u.val (8).

@[reducible, inline]

Input value register v (9).

@[reducible, inline]

Output register (10: 0 = none, v + 1 = some v on query).

@[reducible, inline]

Constant 0 scratch register (11, also zeroReg for zeroFillCode).

@[reducible, inline]

Constant 1 scratch register (12, also oneReg for zeroFillCode).

@[reducible, inline]

Bucket head array base base + 4 (13).

@[reducible, inline]

Node key array base base + 4 + numBuckets n_max (14).

@[reducible, inline]

Node value array base base + 4 + numBuckets n_max + n_max (15).

@[reducible, inline]

Node next-pointer array base base + 4 + numBuckets n_max + 2 * n_max (16).

@[reducible, inline]

Current 1-based chain pointer (17).

@[reducible, inline]

Current 0-based pool slot index (18).

@[reducible, inline]

Key-hit indicator flag (19).

@[reducible, inline]

Matched 0-based pool slot index (20).

@[reducible, inline]

Scratch memory address / value register (21).

Embedding of the 5 multiply-shift registers (Fin 5) into Fin rHash (0..4).

Register classification: only baseReg (5) is persistent (false); all other registers are caller-overwritable (true).

Memory address of the bucket head array head[0 .. numBuckets n_max - 1].

Memory address of the node key array key[0 .. n_max - 1].

Memory address of the node value array val[0 .. n_max - 1].

Memory address of the node next-pointer array next[0 .. n_max - 1].

Store the sampled multiply-shift seed regB, bucketBits n_max, hashBits U n_max, and initial allocation count 0 into the 4-word header [base, base + 4).

Self-contained hash-table initialization: sample a uniform multiply-shift seed via MultiplyShift.initCode and write the 4-word header.

Compute array base pointers and load cur := head[regOut], clearing found and matchIdx.

Evaluate the bucket index regOut = multiplyShift w₀ M b (encodeKey u) and load head[regOut] into cur.

Single step of chain traversal: inspect slot idx = cur - 1; if key[idx] = argU, record found := 1, matchIdx := idx and terminate the loop (cur := 0), else advance cur := next[idx].

Complete query u after findInChain: return val[matchIdx] + 1 on a hit, or 0 on a miss.

Complete insert u v on a key hit: overwrite val[matchIdx] ← argV in place.

Complete insert u v on a key miss: allocate slot idx = allocCount, increment allocCount, write key[idx] := argU, val[idx] := argV, next[idx] := head[regOut], and head[regOut] := idx + 1.

Dispatch post-traversal completion between query (tagReg = 0) and insert (tagReg ≠ 0).

Unified operation dispatcher for query and insert.

Relocatable SubroutineCode bundle for the chained hash table over Basis.exact.

Standalone register encoding for Dictionary.Op U.

Partial argument-matching predicate for inline dictionary calls.

def Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.decode {U V n_max : ℕ} (o : (spec U V n_max).Op) :
(Fin rHash → ℕ) → Option ((spec U V n_max).Result o)

Output decoder for Dictionary.spec U V n_max (0 = none, v + 1 = some v on query).

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.arguments_fit {U V n_max w base : ℕ} (hw : 2 * hashBits U n_max ≤ w) (hS : WordRAM.Fits w (base + space n_max)) (hV : WordRAM.Fits w V) {s : State U} (o : Op U) (hv : (spec U V n_max).valid s o) (i : Fin rHash) :

3. Concrete representation invariant and chain length bound #

inductive Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.IsChain (mem : ℕ → ℕ) (nextBase sz : ℕ) :
ℕ → List ℕ → Prop

Inductive linked-list predicate for a bucket chain in mem: IsChain mem nextBase sz ptr L states that following 1-based pointers from ptr through next[idx] (mem (nextBase + idx)) visits the sequence L of 0-based pool indices < sz before reaching 0.

  • nil {mem : ℕ → ℕ} {nextBase sz : ℕ} : IsChain mem nextBase sz 0 []
  • cons {mem : ℕ → ℕ} {nextBase sz idx nextPtr : ℕ} {rest : List ℕ} : idx < sz → mem (nextBase + idx) = nextPtr → IsChain mem nextBase sz nextPtr rest → IsChain mem nextBase sz (idx + 1) (idx :: rest)
theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.IsChain.lt_sz {mem : ℕ → ℕ} {nextBase sz ptr : ℕ} {L : List ℕ} (h : IsChain mem nextBase sz ptr L) (idx : ℕ) :
idx ∈ L → idx < sz
theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.IsChain.unique {mem : ℕ → ℕ} {nextBase sz₁ sz₂ ptr : ℕ} {L₁ L₂ : List ℕ} (h₁ : IsChain mem nextBase sz₁ ptr L₁) (h₂ : IsChain mem nextBase sz₂ ptr L₂) :
L₁ = L₂
theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.IsChain.congr_mem {mem mem' : ℕ → ℕ} {nextBase sz ptr : ℕ} {L : List ℕ} (h : IsChain mem nextBase sz ptr L) (hmem : ∀ idx ∈ L, mem' (nextBase + idx) = mem (nextBase + idx)) :
IsChain mem' nextBase sz ptr L
theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.IsChain.mono_sz {mem : ℕ → ℕ} {nextBase sz sz' ptr : ℕ} {L : List ℕ} (h : IsChain mem nextBase sz ptr L) (hsz : sz ≤ sz') :
IsChain mem nextBase sz' ptr L
structure Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.BucketInv (U n_max base : ℕ) (b : Fin (2 ^ (hashBits U n_max - 1))) (s : State U) (mem : ℕ → ℕ) (bk : ℕ) (L : List ℕ) :

Invariant for a single bucket bk < numBuckets n_max with chain L : List ℕ.

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.BucketInv.congr_mem {U n_max base : ℕ} {b : Fin (2 ^ (hashBits U n_max - 1))} {s : State U} {mem mem' : ℕ → ℕ} {bk : ℕ} {L : List ℕ} (hb : BucketInv U n_max base b s mem bk L) (hbk : bk < numBuckets n_max) (hcard : (dom s).card ≤ n_max) (hmem : ∀ a ∈ { base := base, length := space n_max }.footprint, mem' a = mem a) :
BucketInv U n_max base b s mem' bk L
theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.BucketInv.eq_of_mem_eq {U n_max base : ℕ} {b : Fin (2 ^ (hashBits U n_max - 1))} {s : State U} {mem : ℕ → ℕ} {bk₁ bk₂ idx : ℕ} {L₁ L₂ : List ℕ} (hb₁ : BucketInv U n_max base b s mem bk₁ L₁) (hb₂ : BucketInv U n_max base b s mem bk₂ L₂) (h₁ : idx ∈ L₁) (h₂ : idx ∈ L₂) :
bk₁ = bk₂ ∧ L₁ = L₂

If idx₁ ∈ L₁ (in bucket bk₁) and idx₂ ∈ L₂ (in bucket bk₂) and idx₁ = idx₂, then bk₁ = bk₂ and L₁ = L₂.

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.BucketInv.idx_eq_of_key_eq {U n_max base : ℕ} {b : Fin (2 ^ (hashBits U n_max - 1))} {s : State U} {mem : ℕ → ℕ} {bk idx₁ idx₂ : ℕ} {L : List ℕ} (hb : BucketInv U n_max base b s mem bk L) (h₁ : idx₁ ∈ L) (h₂ : idx₂ ∈ L) (hk : mem (keyBase base n_max + idx₁) = mem (keyBase base n_max + idx₂)) :
idx₁ = idx₂

Within a single bucket bk, two slots idx₁, idx₂ ∈ L have the same stored key if and only if idx₁ = idx₂.

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.BucketInv.length_le {U n_max base : ℕ} {b : Fin (2 ^ (hashBits U n_max - 1))} {s : State U} {mem : ℕ → ℕ} {L : List ℕ} (u : Fin U) (hb : BucketInv U n_max base b s mem (Probability.multiplyShift (hashBits U n_max) (bucketBits n_max) b (encodeKey u)) L) :

Chain length bound: for any query key u : Fin U, the length of the chain L in bucket bk = multiplyShift w₀ M b (encodeKey u) is at most 1 plus the number of other keys u' ∈ (dom s).erase u that collide with u under multiplyShift w₀ M b.

structure Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.Rep (U V n_max w base : ℕ) (b : Fin (2 ^ (hashBits U n_max - 1))) (s : State U) (x : WordRAM.Data rHash) :

Seed-coupled concrete representation invariant for the chained hash table at base.

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.Rep.unique {U V n_max w base : ℕ} {b : Fin (2 ^ (hashBits U n_max - 1))} {s₁ s₂ : State U} {x : WordRAM.Data rHash} (hr₁ : Rep U V n_max w base b s₁ x) (hr₂ : Rep U V n_max w base b s₂ x) :
s₁ = s₂
theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.Rep.supported {U V n_max w base : ℕ} {b : Fin (2 ^ (hashBits U n_max - 1))} {s : State U} {x x' : WordRAM.Data rHash} (hreg : ∀ (i : Fin rHash), inputMask i = false → x'.regs i = x.regs i) (hmem : ∀ a ∈ { base := base, length := space n_max }.footprint, x'.mem a = x.mem a) (hbx' : WordRAM.Data.Bounded w x') (hr : Rep U V n_max w base b s x) :
Rep U V n_max w base b s x'

4. Initialization execution (allocSpan, storeHeader, initSetup) #

5. Deterministic operation execution (evalBucket, findInChain, finishOp, opCode) #

def Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.collCount {U : ℕ} (n_max : ℕ) (s : State U) (u : Fin U) (b : Fin (2 ^ (hashBits U n_max - 1))) :

Number of keys u' ∈ (dom s).erase u that collide with u under multiplyShift w₀ M b.

Key queried or inserted by a dictionary operation o : Dictionary.Op U.

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.opCode_spec {U V n_max w base : ℕ} (hw : 2 * hashBits U n_max ≤ w) (hS : WordRAM.Fits w (base + space n_max)) (hV : WordRAM.Fits w V) {b : Fin (2 ^ (hashBits U n_max - 1))} {s : State U} {x : WordRAM.Data rHash} (hr : Rep U V n_max w base b s x) (o : Op U) (hv : (spec U V n_max).valid s o) (harg : argMatch o x.regs) :
∃ (z : WordRAM.Data rHash), WordRAM.ExecWithin w (opCode U n_max) x z (55 + 9 * collCount n_max s (opKey o) b) ∧ decode o z.regs = some ((spec U V n_max).stepFn s o).2 ∧ Rep U V n_max w base b ((spec U V n_max).stepFn s o).1 z ∧ WordRAM.Modifies { base := base, length := space n_max }.footprint x.mem z.mem

Deterministic execution of opCode U n_max for a fixed seed b: executes in at most 55 + 9 * collCount n_max s (opKey o) b instructions, preserves Rep, decodes the abstract result, and modifies memory only within [base, base + space n_max).

6. Expected collision bounds and Randomized.StateTransitionImpl certificate #

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.expect_collCount_le_card_mul (U n_max : ℕ) (s : State U) (u : Fin U) :
let Seed := Fin (2 ^ (hashBits U n_max - 1)); have seedLaw := Probability.PMF.uniformOfFintype Seed; ∑' (b : Seed), seedLaw b * ↑(collCount n_max s u b) ≤ ↑((dom s).erase u).card * (2 / 2 ^ bucketBits n_max)

By linearity of expectation and (2 / 2 ^ M)-universality of multiply-shift hashing (isUniversalHash_multiplyShift), the expected number of keys in (dom s).erase u colliding with u under a uniform seed b ∈ Fin (2 ^ (hashBits U n_max - 1)) is at most ((dom s).erase u).card * (2 / 2 ^ bucketBits n_max).

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.expect_collCount_le_two {U n_max : ℕ} {s : State U} (hcard : (dom s).card ≤ n_max) (u : Fin U) :
let Seed := Fin (2 ^ (hashBits U n_max - 1)); have seedLaw := Probability.PMF.uniformOfFintype Seed; ∑' (b : Seed), seedLaw b * ↑(collCount n_max s u b) ≤ 2

When (dom s).card ≤ n_max, the expected collision count is at most 2.

Worst-case expected instruction charge per dictionary operation (55 + 9 * 2 = 73).

def Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.charge (U V n_max : ℕ) :
(spec U V n_max).State → (spec U V n_max).Op → ℕ

Constant O(1) expected instruction charge for Dictionary.spec U V n_max.

noncomputable def Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.impl (U V n_max w base : ℕ) (hw : 2 * hashBits U n_max ≤ w) (hS : WordRAM.Fits w (base + space n_max)) (hV : WordRAM.Fits w V) :

Chained hash table Randomized.StateTransitionImpl certificate over Basis.exact. Realizes Dictionary.spec U V n_max in space n_max = 4 + 2 ^ M + 3 * n_max = O(n_max) words of memory, O(1) (16-instruction) initialization on zeroed memory (O(n_max) via initClear on dirty memory), and chargeVal = 73 = O(1) worst-case expected instructions per query or insert operation with zero potential (potential = 0).

noncomputable def Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.implStandard (U V n_max w base : ℕ) (hw : 2 * hashBits U n_max ≤ w) (hS : WordRAM.Fits w (base + space n_max)) (hV : WordRAM.Fits w V) :

Chained hash table Randomized.StateTransitionImpl certificate over Basis.standard. Obtained from impl by basis transport (Randomized.StateTransitionImpl.toStandard).

theorem Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.seq_expected_amortized (U V n_max w base : ℕ) (hw : 2 * hashBits U n_max ≤ w) (hS : WordRAM.Fits w (base + space n_max)) (hV : WordRAM.Fits w V) {x : WordRAM.Data rHash} (ops : List (Op U)) (hx : WordRAM.Data.Bounded w x) (hfr : x.regs Reg.frontierReg = base) (hz : ∀ a ∈ { base := base, length := space n_max }.footprint, x.mem a = 0) (hv : (spec U V n_max).ValidHistory (empty U) ops) :
have C := impl U V n_max w base hw hS hV; ∃ (ν₀ : PMF (C.Seed × WordRAM.Data rHash)) (ν_final : PMF (C.Seed × WordRAM.Data rHash)) (T_ops : ENNReal), WordRAM.ExecCodeDist w (subroutineCode U n_max).initFresh x (PMF.map Prod.snd ν₀) C.initTime ∧ C.ExecStepsOfFn (empty U) ops ν₀ ν_final T_ops ∧ (∀ (θ : C.Seed), (PMF.map Prod.fst ν_final) θ ≤ C.seedSlack * C.seedLaw θ) ∧ (∀ p ∈ ν_final.support, C.rep p.1 ((spec U V n_max).run (empty U) ops).1 p.2) ∧ C.initTime + T_ops ≤ 16 + ↑(chargeVal * ops.length)

Expected subroutine body time across an oblivious operation sequence (seq_expected_amortized). For fixed U, n_max and admissible word width w, subroutineCode U n_max uses precomputed configuration literals (initParams = Fin.elim0). For a valid operation history ops : List (Dictionary.Op U) fixed independently of the sampled seed (oblivious adversary), running initFresh (16 body instructions on zeroed [base, base + space n_max)) and C.ExecSteps across ops (which receives each operation's prepared arguments and charges subroutine operation bodies rather than a complete client/dispatch driver or configuration construction) has total expected body cost 16 + T_ops ≤ 16 + 73 * ops.length.