Documentation

CslibFrontier.DataStructure.Dictionary.CuckooHashing.Basic

Pure cuckoo tables, displacement, and replay #

This module defines the pure two-table cuckoo model used by the graph analysis and the Word-RAM implementation. It has no dependency on a machine model. The table scale and eviction cap are the parameters used by the quantitative analysis; SlotEntry, PureTable, WellPlaced, pureEvict, and pureReplay describe table contents and insertion-order replay independently of memory layout.

GoodForSet requires successful replay for every duplicate-free insertion order in a set. cuckooWalkRounds is the supremum of the displacement rounds over every ValidPlacement of that set and every allocation index. These are original strengthened sufficient notions inspired by Pagh and Rodler [PR04], Section 2.3 (8 December 2003 manuscript, pp. 6–10): Lemma 1 on p. 7 concerns the distinct-subsequence structure of displacement walks, Eq. (3) on p. 9 bounds expected displacement iterations, and pp. 9–10 analyze rehashing. The chosen sub-table size 2 ^ tableBits n_max ≥ 32 * max n_max 4 is a stronger load condition than their m ≥ (1 + ε) n.

Main definitions #

Main results #

References #

Section 1: Table scale and eviction cap parameters #

Sub-table bit-width M = ⌈log₂ (32 * max n_max 4)⌉, ensuring each of the two cuckoo sub-tables has size m = 2 ^ M ≥ 32 * max n_max 4.

Sub-table capacity m = 2 ^ tableBits n_max.

Maximum number of two-table cuckoo displacement rounds 6 * (⌈log₂ max(n_max, 2)⌉ + 2) before triggering a full-table rehash.

Independence parameter k = 2 * maxEvictions n_max + 2 required by the two-table cuckoo hashing analysis on eviction walks of length up to maxEvictions n_max.

Section 2: Pure two-table cuckoo displacement and replay model #

A stored slot entry in one of the two cuckoo sub-tables, recording a key key : Fin U and its allocation index idx : ℕ in the insertion-order arrays insKeys / insVals.

  • key : Fin U

    Stored dictionary key.

  • idx : ℕ

    Index into insKeys and insVals.

Instances For

    Pure state of the two cuckoo sub-tables T₁ and T₂, each of size m.

    The empty cuckoo table pair in which every slot of T₁ and T₂ is none.

    Entry e occupies some slot of T₁ or T₂.

    Key k is present in some slot of T₁ or T₂.

    structure Cslib.DataStructure.Dictionary.CuckooHashing.WellPlaced {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (tbl : PureTable U m) :

    Well-placed cuckoo table invariant under hash pair (h₁, h₂): every entry in T₁ sits at its h₁-home slot, every entry in T₂ sits at its h₂-home slot, and no key appears simultaneously in both T₁ and T₂.

    def Cslib.DataStructure.Dictionary.CuckooHashing.lookupEntry {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (tbl : PureTable U m) (u : Fin U) :

    Two-probe cuckoo lookup for key u: probes T₁[h₁ u] first, then T₂[h₂ u].

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.WellPlaced.entryIn_iff_home {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} (hwp : WellPlaced h₁ h₂ tbl) (e : SlotEntry U) :
    EntryIn tbl e ↔ tbl.t1 (h₁ e.key) = some e ∨ tbl.t2 (h₂ e.key) = some e
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.WellPlaced.entry_unique_of_key_eq {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} (hwp : WellPlaced h₁ h₂ tbl) {e₁ e₂ : SlotEntry U} (h₁in : EntryIn tbl e₁) (h₂in : EntryIn tbl e₂) (hk : e₁.key = e₂.key) :
    e₁ = e₂
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.WellPlaced.lookupEntry_eq_some_iff {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} (hwp : WellPlaced h₁ h₂ tbl) (u : Fin U) (e : SlotEntry U) :
    lookupEntry h₁ h₂ tbl u = some e ↔ EntryIn tbl e ∧ e.key = u
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.WellPlaced.lookupEntry_eq_none_iff {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} (hwp : WellPlaced h₁ h₂ tbl) (u : Fin U) :
    lookupEntry h₁ h₂ tbl u = none ↔ ¬HasKey tbl u

    Place entry e into T₁[h₁ e.key], returning the updated table and any displaced entry.

    Place entry e into T₂[h₂ e.key], returning the updated table and any displaced entry.

    One two-table displacement round: places e in T₁[h₁ e.key]; if that slot was occupied by e₁, places e₁ in T₂[h₂ e₁.key] and returns any entry displaced from T₂.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.putT1_wellPlaced {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} {e : SlotEntry U} (hwp : WellPlaced h₁ h₂ tbl) (hno : ¬HasKey tbl e.key) :
    WellPlaced h₁ h₂ (putT1 h₁ tbl e).1
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.putT1_entryIn_none {U m : ℕ} {h₁ : Fin U → Fin m} {tbl : PureTable U m} {e : SlotEntry U} (hold : (putT1 h₁ tbl e).2 = none) (d : SlotEntry U) :
    EntryIn (putT1 h₁ tbl e).1 d ↔ EntryIn tbl d ∨ d = e
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.putT1_some_spec {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} {e e₁ : SlotEntry U} (hwp : WellPlaced h₁ h₂ tbl) (hno : ¬HasKey tbl e.key) (hold : (putT1 h₁ tbl e).2 = some e₁) :
    ¬HasKey (putT1 h₁ tbl e).1 e₁.key ∧ ∀ (d : SlotEntry U), EntryIn (putT1 h₁ tbl e).1 d ∨ d = e₁ ↔ EntryIn tbl d ∨ d = e
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.putT2_wellPlaced {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} {e : SlotEntry U} (hwp : WellPlaced h₁ h₂ tbl) (hno : ¬HasKey tbl e.key) :
    WellPlaced h₁ h₂ (putT2 h₂ tbl e).1
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.putT2_entryIn_none {U m : ℕ} {h₂ : Fin U → Fin m} {tbl : PureTable U m} {e : SlotEntry U} (hold : (putT2 h₂ tbl e).2 = none) (d : SlotEntry U) :
    EntryIn (putT2 h₂ tbl e).1 d ↔ EntryIn tbl d ∨ d = e
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.putT2_some_spec {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {tbl : PureTable U m} {e e₂ : SlotEntry U} (hwp : WellPlaced h₁ h₂ tbl) (hno : ¬HasKey tbl e.key) (hold : (putT2 h₂ tbl e).2 = some e₂) :
    ¬HasKey (putT2 h₂ tbl e).1 e₂.key ∧ ∀ (d : SlotEntry U), EntryIn (putT2 h₂ tbl e).1 d ∨ d = e₂ ↔ EntryIn tbl d ∨ d = e
    def Cslib.DataStructure.Dictionary.CuckooHashing.pureEvict {U m : ℕ} (h₁ h₂ : Fin U → Fin m) :
    ℕ → PureTable U m → SlotEntry U → Option (PureTable U m)

    Run up to fuel two-table cuckoo displacement rounds starting with displaced entry e.

    def Cslib.DataStructure.Dictionary.CuckooHashing.evictRounds {U m : ℕ} (h₁ h₂ : Fin U → Fin m) :
    ℕ → PureTable U m → SlotEntry U → ℕ

    Number of displacement rounds executed by pureEvict before either settling or exhausting fuel.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.evictRounds_le {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (fuel : ℕ) (tbl : PureTable U m) (e : SlotEntry U) :
    evictRounds h₁ h₂ fuel tbl e ≤ fuel
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureEvict_spec {U m : ℕ} {h₁ h₂ : Fin U → Fin m} (fuel : ℕ) {tbl tbl' : PureTable U m} {e : SlotEntry U} :
    WellPlaced h₁ h₂ tbl → ¬HasKey tbl e.key → pureEvict h₁ h₂ fuel tbl e = some tbl' → WellPlaced h₁ h₂ tbl' ∧ ∀ (d : SlotEntry U), EntryIn tbl' d ↔ EntryIn tbl d ∨ d = e
    def Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) (ks : List (Fin U)) :

    Replay a list of distinct inserted keys ks in exact insertion order starting from PureTable.empty.

    @[simp]
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_nil {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) :
    pureReplay h₁ h₂ maxLoop [] = some (PureTable.empty U m)
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_append_singleton {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) (ks : List (Fin U)) (u : Fin U) :
    pureReplay h₁ h₂ maxLoop (ks ++ [u]) = (pureReplay h₁ h₂ maxLoop ks).bind fun (tbl : PureTable U m) => pureEvict h₁ h₂ maxLoop tbl { key := u, idx := ks.length }
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_spec {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {tbl : PureTable U m} (hnodup : ks.Nodup) (hrep : pureReplay h₁ h₂ maxLoop ks = some tbl) :
    WellPlaced h₁ h₂ tbl ∧ ∀ (d : SlotEntry U), EntryIn tbl d ↔ (d.key, d.idx) ∈ ks.zipIdx
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_hasKey_iff {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {tbl : PureTable U m} (hnodup : ks.Nodup) (hrep : pureReplay h₁ h₂ maxLoop ks = some tbl) (u : Fin U) :
    HasKey tbl u ↔ u ∈ ks
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_lookupEntry_of_mem {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {tbl : PureTable U m} (hnodup : ks.Nodup) (hrep : pureReplay h₁ h₂ maxLoop ks = some tbl) {idx : ℕ} (hlt : idx < ks.length) :
    lookupEntry h₁ h₂ tbl ks[idx] = some { key := ks[idx], idx := idx }
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_lookupEntry_of_notMem {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {tbl : PureTable U m} (hnodup : ks.Nodup) (hrep : pureReplay h₁ h₂ maxLoop ks = some tbl) {u : Fin U} (hu : u ∉ ks) :
    lookupEntry h₁ h₂ tbl u = none

    Section 3: Set-monotone cuckoo goodness, walk bounds, and conditional-uniform invariance #

    def Cslib.DataStructure.Dictionary.CuckooHashing.GoodForSet {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) (S : Finset (Fin U)) :

    Original strengthened sufficient goodness predicate: a hash pair (h₁, h₂) is good for key set S with cap maxLoop if replaying every duplicate-free (Nodup) key list ks with ks ⊆ S succeeds within maxLoop displacement rounds per insertion.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.GoodForSet.mono {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {S S' : Finset (Fin U)} (hsub : S ⊆ S') (hgood : GoodForSet h₁ h₂ maxLoop S') :
    GoodForSet h₁ h₂ maxLoop S
    @[simp]
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.goodForSet_empty {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) :
    GoodForSet h₁ h₂ maxLoop ∅
    def Cslib.DataStructure.Dictionary.CuckooHashing.ValidPlacement {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (tbl : PureTable U m) :

    Pure predicate that tbl is well-placed under (h₁, h₂) and every key in tbl belongs to S.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_validPlacement {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {S : Finset (Fin U)} {tbl : PureTable U m} (hnodup : ks.Nodup) (hsub : ∀ u ∈ ks, u ∈ S) (hrep : pureReplay h₁ h₂ maxLoop ks = some tbl) :
    ValidPlacement h₁ h₂ S tbl
    noncomputable def Cslib.DataStructure.Dictionary.CuckooHashing.cuckooWalkRounds {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) (S : Finset (Fin U)) (u : Fin U) :

    Original strengthened worst-case displacement round count: supremum (sSup) of evictRounds to insert u over all ValidPlacements of S under (h₁, h₂) with cap maxLoop.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.evictRounds_le_cuckooWalkRounds {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {S : Finset (Fin U)} {tbl : PureTable U m} (hvp : ValidPlacement h₁ h₂ S tbl) (u : Fin U) (idx : ℕ) :
    evictRounds h₁ h₂ maxLoop tbl { key := u, idx := idx } ≤ cuckooWalkRounds h₁ h₂ maxLoop S u
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.cuckooWalkRounds_le_maxLoop {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) (S : Finset (Fin U)) (u : Fin U) :
    cuckooWalkRounds h₁ h₂ maxLoop S u ≤ maxLoop
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.t2_eq_none_of_validPlacement_card_le_one {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {S : Finset (Fin U)} {tbl : PureTable U m} (hvp : ValidPlacement h₁ h₂ S tbl) (hcard : S.card ≤ 1) {i₁ : Fin m} {e₁ : SlotEntry U} (ht1 : tbl.t1 i₁ = some e₁) (i₂ : Fin m) :
    tbl.t2 i₂ = none

    In any valid placement of a key set S of cardinality at most 1, if T₁ contains an entry e₁, then every slot of T₂ is empty (by key disjointness between T₁ and T₂).

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureEvictRound_snd_eq_none_of_card_le_one {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {S : Finset (Fin U)} {tbl : PureTable U m} (hvp : ValidPlacement h₁ h₂ S tbl) (hcard : S.card ≤ 1) (e : SlotEntry U) :
    (pureEvictRound h₁ h₂ tbl e).2 = none

    In any valid placement of a key set S of cardinality at most 1, a single two-table displacement round pureEvictRound always terminates without displacing an entry from T₂.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.cuckooWalkRounds_le_one_of_card_le_one {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) {S : Finset (Fin U)} (hcard : S.card ≤ 1) (u : Fin U) :
    cuckooWalkRounds h₁ h₂ maxLoop S u ≤ 1

    Whenever S.card ≤ 1, inserting any key u into any valid placement of S completes in at most 1 displacement round for every hash pair (h₁, h₂).

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.cuckooWalkRounds_empty {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (maxLoop : ℕ) (u : Fin U) :
    cuckooWalkRounds h₁ h₂ maxLoop ∅ u ≤ 1

    Inserting into a valid placement of the empty set takes at most one displacement round.

    noncomputable def Cslib.DataStructure.Dictionary.CuckooHashing.survivingSeeds {Θ : Type u_1} [Fintype Θ] {U m : ℕ} (h₁ h₂ : Θ → Fin U → Fin m) (maxLoop : ℕ) (ks : List (Fin U)) :

    Finset of hash seeds θ : Θ under which insertion-order replay pureReplay (h₁ θ) (h₂ θ) maxLoop ks succeeds without exhausting the displacement cap maxLoop.

    @[simp]
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.mem_survivingSeeds {Θ : Type u_1} [Fintype Θ] {U m : ℕ} {h₁ h₂ : Θ → Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {θ : Θ} :
    θ ∈ survivingSeeds h₁ h₂ maxLoop ks ↔ (pureReplay (h₁ θ) (h₂ θ) maxLoop ks).isSome = true
    @[simp]
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.survivingSeeds_nil {Θ : Type u_1} [Fintype Θ] {U m : ℕ} (h₁ h₂ : Θ → Fin U → Fin m) (maxLoop : ℕ) :
    survivingSeeds h₁ h₂ maxLoop [] = Finset.univ
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.survivingSeeds_append_subset {Θ : Type u_1} [Fintype Θ] {U m : ℕ} (h₁ h₂ : Θ → Fin U → Fin m) (maxLoop : ℕ) (ks : List (Fin U)) (u : Fin U) :
    survivingSeeds h₁ h₂ maxLoop (ks ++ [u]) ⊆ survivingSeeds h₁ h₂ maxLoop ks
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.mem_survivingSeeds_of_goodForSet {Θ : Type u_1} [Fintype Θ] {U m : ℕ} {h₁ h₂ : Θ → Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {S : Finset (Fin U)} {θ : Θ} (hnodup : ks.Nodup) (hsub : ∀ u ∈ ks, u ∈ S) (hgood : GoodForSet (h₁ θ) (h₂ θ) maxLoop S) :
    θ ∈ survivingSeeds h₁ h₂ maxLoop ks
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_isSome_of_length_le_one {U m : ℕ} (h₁ h₂ : Fin U → Fin m) {maxLoop : ℕ} (hloop : 0 < maxLoop) {ks : List (Fin U)} (hlen : ks.length ≤ 1) :
    (pureReplay h₁ h₂ maxLoop ks).isSome = true

    When ks has length at most 1 and 0 < maxLoop, pureReplay succeeds for every hash pair (h₁, h₂) in a single probe into T₁.

    theorem Cslib.DataStructure.Dictionary.CuckooHashing.pureReplay_isSome_of_length_le_two {U m : ℕ} (h₁ h₂ : Fin U → Fin m) {maxLoop : ℕ} (hloop : 0 < maxLoop) {ks : List (Fin U)} (hlen : ks.length ≤ 2) :
    (pureReplay h₁ h₂ maxLoop ks).isSome = true

    When ks has length at most 2 and 0 < maxLoop, pureReplay succeeds for every hash pair (h₁, h₂) in at most 1 displacement round per key: the first key lands in T₁, and even if the second key collides with the first in T₁, the displaced first key lands in T₂, which is empty.