Documentation

CslibFrontier.DataStructure.Dictionary.WordRAM.CuckooHashing

Pagh–Rodler Cuckoo Hashing on the Randomized Word RAM #

This module packages Pagh and Rodler's two-table cuckoo hashing dictionary [PR04] on the randomized Word RAM (Cslib.WordRAM.Basis.exact and Cslib.WordRAM.Basis.standard), realizing the bounded-capacity lookup-and-insert specification Cslib.DataStructure.Dictionary.spec U V n_max via a composable, assumption-free Cslib.DataStructure.WordRAM.Randomized.StateTransitionImpl certificate (seedSlack := 2, potential := 0) and an expected-amortized sequence theorem cuckoo_seq_expected_amortized.

Design overview #

  1. Memory layout, lookup, and bounded displacement (CslibFrontier.DataStructure.Dictionary.WordRAM.CuckooHashing.Evict):

    • Contiguous [base, base + space c_exp n_max) block of O_{c_exp}(n_max + 1) words (space_le) storing a 20-word header, the regime-switched hash pair (KWise below doubleTabThreshold c_exp, Thorup's DoubleTabulation [Tho13] at or above it), two sub-tables T₁, T₂ of size tableSize n_max ≥ 32 * max n_max 4, and insertion-order arrays insKeys, insVals.
    • Deterministic two-probe lookup queryCode (queryCode_execWithin) and bounded displacement loop evictLoopCode (evictLoopCode_execWithin).
  2. Las Vegas rehashing and insertion (CslibFrontier.DataStructure.Dictionary.WordRAM.CuckooHashing.Rehash):

    • rehashLoopCode resamples (h₁, h₂), clears T₁, T₂, and replays insKeys in insertion order until all stored keys settle, yielding an exact conditional-uniform exit law over survivingSeeds (rehashLoopCode_execCodeDist).
    • insertCode updates present keys in place and inserts absent keys via bounded displacement followed by Las Vegas rehashing on overflow (insertCode_execCodeDist).
  3. Unconditional O_{c_exp}(1) expected-amortized bounds (seedSlack = 2, potential = 0):

    • Because Dictionary.spec has no deletion operation and rehashLoopCode replays insKeys in exact insertion order, the surviving-seed set shrinks monotonically across operations, so keeping surviving seeds and rejection-sampling uniformly on overflow maintains the exact conditional uniform distribution (unifOn_rehash_eq), bounded by 2 * seedLaw.
    • Whenever U ≤ max n_max 4 ^ c_exp, isCuckooHashFamily_tableBits discharges IsCuckooHashFamily c_exp U n_max (tableBits n_max) (maxEvictions n_max) 3 4 unconditionally via isCuckooHashPair_provableFamily [PaghRodler2004, Thorup2013], giving O_{c_exp}(1) worst-case lookup and O_{c_exp}(1) expected-amortized insertion (expectedOpCharge c_exp) independent of n_max and U.

Main definitions #

Main results #

References #

Register encoding, opCode, and amortized charge definitions #

Standalone register encoding for Dictionary.Op U:

  • query u sets reg 2 = 0 (selector) and reg 3 = u.val.
  • insert u v sets reg 2 = 1 (selector), reg 3 = u.val, and reg 4 = v.

Partial argument-matching predicate for inline callers on Fin 19 → ℕ.

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

Output decoder reading Option ((Dictionary.spec U V n_max).Result o) from final registers.

Combined operation body (opCode): dispatches on reg 2 = 0 (queryCode) vs reg 2 ≠ 0 (insertCode).

def Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.charge (c_exp n_max U C_walk C_reh : ℕ) (_s : State U) :
Op U → ℕ

Per-operation amortized instruction charge for Dictionary.Op U.

State-independent expected step bound for a single opCode invocation under an arbitrary input distribution (used in the standalone Randomized.StateTransitionImpl certificate, while cuckoo_seq_expected_amortized establishes the tighter charge / expectedOpCharge bound along valid trajectories from initFresh).

cuckooCode, impl, and expected-amortized sequence theorems #

Relocatable SubroutineCode bundle for Pagh–Rodler cuckoo hashing over Basis.exact.

Unconditional Pagh–Rodler cuckoo hash family certificate at tableBits n_max (C_walk = 3, C_reh = 4): whenever U ≤ max n_max 4 ^ c_exp, the regime-switched Word-RAM cuckoo hash pair (KWise below doubleTabThreshold c_exp, DoubleTabulation at or above doubleTabThreshold c_exp) satisfies IsCuckooHashFamily c_exp U n_max (tableBits n_max) (maxEvictions n_max) 3 4 for every capacity n_max : ℕ ([PaghRodler2004], [Thorup2013]).

O(c_exp⁴) = O_{c_exp}(1) worst-case instruction bound for query via opCode c_exp n_max.

O(c_exp⁴) = O_{c_exp}(1) expected amortized instruction bound for insert (C_walk = 3, C_reh = 4), independent of n_max and U whenever U ≤ max n_max 4 ^ c_exp.

O(c_exp⁴) = O_{c_exp}(1) expected amortized per-operation instruction charge for Pagh–Rodler cuckoo hashing, completely independent of n_max and U whenever U ≤ max n_max 4 ^ c_exp.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.charge_le_expectedOpCharge {c_exp n_max U : ℕ} (hU : U ≤ max n_max 4 ^ c_exp) (s : State U) (o : Op U) :
charge c_exp n_max U 3 4 s o ≤ expectedOpCharge c_exp s o
noncomputable def Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.impl (w base c_exp n_max U V : ℕ) (hU : U ≤ max n_max 4 ^ c_exp) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_ev : WordRAM.Fits w (CuckooHashing.maxEvictions n_max)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_V : WordRAM.Fits w V) :
WordRAM.Randomized.StateTransitionImpl (cuckooCode c_exp n_max) (spec U V n_max) w base ![n_max, U] (empty U)

Composable randomized Word-RAM StateTransitionImpl certificate for Pagh–Rodler cuckoo hashing over Basis.exact, unconditional whenever U ≤ max n_max 4 ^ c_exp.

@[reducible, inline]
noncomputable abbrev Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.implStandard (w base c_exp n_max U V : ℕ) (hU : U ≤ max n_max 4 ^ c_exp) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_ev : WordRAM.Fits w (CuckooHashing.maxEvictions n_max)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_V : WordRAM.Fits w V) :
WordRAM.Randomized.StateTransitionImpl (cuckooCode c_exp n_max).toStandard (spec U V n_max) w base ![n_max, U] (empty U)

Standard-basis (Basis.standard) randomized StateTransitionImpl certificate for Pagh–Rodler cuckoo hashing, unconditional whenever U ≤ max n_max 4 ^ c_exp.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.cuckoo_steps_expected_amortized (w base c_exp n_max U V : ℕ) (hU : U ≤ max n_max 4 ^ c_exp) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_ev : WordRAM.Fits w (CuckooHashing.maxEvictions n_max)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_V : WordRAM.Fits w V) (ops : List (Op U)) {ks : List (Fin U)} {s : State U} (ν : PMF (Seed c_exp n_max U (CuckooHashing.tableBits n_max) × WordRAM.Data 19)) (hv : (spec U V n_max).ValidHistory s ops) (hE : (CuckooHashing.survivingSeeds evalHash1 evalHash2 (CuckooHashing.maxEvictions n_max) ks).Nonempty) (hseed : PMF.map Prod.fst ν = Probability.PMF.uniformOfFinset (CuckooHashing.survivingSeeds evalHash1 evalHash2 (CuckooHashing.maxEvictions n_max) ks) hE) (hsupp : ∀ p ∈ ν.support, p.1 = extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) p.2.mem ∧ RepWithKeys w base c_exp n_max U V ks s p.2) :
let C := impl w base c_exp n_max U V hU hw hfit_end hfit_ev hfit_U hfit_V; ∃ (ν_final : PMF (Seed c_exp n_max U (CuckooHashing.tableBits n_max) × WordRAM.Data 19)) (T_ops : ENNReal), C.ExecStepsOfFn s ops ν ν_final T_ops ∧ (∀ (θ : Seed c_exp n_max U (CuckooHashing.tableBits n_max)), (PMF.map Prod.fst ν_final) θ ≤ 2 * (seedLaw c_exp n_max U (CuckooHashing.tableBits n_max)) θ) ∧ (∀ p ∈ ν_final.support, Rep w base c_exp n_max U V ((spec U V n_max).run s ops).1 p.2) ∧ T_ops ≤ ↑((spec U V n_max).totalCost (expectedOpCharge c_exp) s ops)

Coupled multi-step execution telescope for Pagh–Rodler cuckoo hashing maintaining the exact conditional-uniform in-memory seed marginal PMF.uniformOfFinset (survivingSeeds evalHash1 evalHash2 (maxEvictions n_max) ks) hE across an operation sequence ops.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.cuckoo_seq_expected_amortized (w base c_exp n_max U V : ℕ) (hU : U ≤ max n_max 4 ^ c_exp) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_ev : WordRAM.Fits w (CuckooHashing.maxEvictions n_max)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_V : WordRAM.Fits w V) {x : WordRAM.Data 19} (ops : List (Op U)) (hx : WordRAM.Data.Bounded w x) (hfr : x.regs 1 = base) (h3 : x.regs 3 = n_max) (h4 : x.regs 4 = U) (hz : ∀ a ∈ { base := base, length := space c_exp n_max }.footprint, x.mem a = 0) (hv : (spec U V n_max).ValidHistory (empty U) ops) :
let C := impl w base c_exp n_max U V hU hw hfit_end hfit_ev hfit_U hfit_V; ∃ (ν₀ : PMF (Seed c_exp n_max U (CuckooHashing.tableBits n_max) × WordRAM.Data 19)) (ν_final : PMF (Seed c_exp n_max U (CuckooHashing.tableBits n_max) × WordRAM.Data 19)) (T_ops : ENNReal), WordRAM.ExecCodeDist w (cuckooCode c_exp n_max).initFresh x (PMF.map Prod.snd ν₀) C.initTime ∧ C.ExecStepsOfFn (empty U) ops ν₀ ν_final T_ops ∧ (∀ (θ : Seed c_exp n_max U (CuckooHashing.tableBits n_max)), (PMF.map Prod.fst ν_final) θ ≤ 2 * (seedLaw c_exp n_max U (CuckooHashing.tableBits n_max)) θ) ∧ (∀ p ∈ ν_final.support, Rep w base c_exp n_max U V ((spec U V n_max).run (empty U) ops).1 p.2) ∧ C.initTime + T_ops ≤ C.initTime + ↑((spec U V n_max).totalCost (expectedOpCharge c_exp) (empty U) ops)

Unconditional end-to-end expected-amortized theorem for Pagh–Rodler cuckoo hashing. Whenever U ≤ max n_max 4 ^ c_exp, starting from zeroed memory in [base, base + space c_exp n_max), initializing cuckooCode c_exp n_max and executing any valid sequence ops : List (Dictionary.Op U) of query and insert operations takes expected instruction cost at most spanSteps + setupSteps c_exp n_max U + (Dictionary.spec U V n_max).totalCost (expectedOpCharge c_exp) (Dictionary.empty U) ops, where expectedOpCharge c_exp charges O(c_exp⁴) = O_{c_exp}(1) instructions per query and insert operation (completely independent of n_max and U!) and space c_exp n_max ≤ spaceFactor c_exp * (n_max + 1) uses O_{c_exp}(n_max + 1) words of memory across every capacity n_max : ℕ ([PaghRodler2004], [Thorup2013]).