Documentation

CslibFrontier.DataStructure.ApproximateMembership.WordRAM.QuotientFilter.SlotArray

Bit-Packed Slot Arrays for the Word-RAM Quotient Filter #

This file defines the compact bit-packed memory layout and extraction/update laws for the two-stage quotient filter:

  1. Generic power-of-2 bit-packed array (slotPackShift, packedWords, extractPacked, writePackedMem): For 2 ^ b slots of d-bit entries (0 < d, 2 * d ≤ w), each word packs 2 ^ p entries where p = slotPackShift b d w = min b (Nat.log 2 (w / (2 * d))). Because p ≤ b and 2 ^ p * d ≤ w / 2 < w, the array occupies packedWords b d w = 2 ^ (b - p) ≤ 4 * (2 ^ b * d) / w + 1 words, no entry ever crosses a word boundary, and every intermediate bit-shift mask fits strictly inside a w-bit word.
  2. Quotient-filter memory layout and total state extraction (extractFilterState, space_le): Packs ReducedKWise (5-wise quotient hash seed and bit-packed table), the 1-word remainder seed γ : RemSeed w₀, three 1-bit slot arrays (isFull, isOcc, isRunStart), and one f-bit remainder array (remAt, f = remBits w₀ k = min w₀ (k + 1)) into space w w₀ n k ≤ 640 * ((n * k + w - 1) / w + 1) words.
  3. Slot modular arithmetic and memory layout offsets (add_and_eq_probeSlot_val, add_sub_and_eq_backSlot_val, remSeedOff, fullOff, occOff, runOff, remOff): Relates bitwise slot masks to probeSlot / backSlot and base-relative offsets to physical array addresses.

Section 1: Generic power-of-2 bit-packed slot array arithmetic #

Log₂ of the number of d-bit entries packed per w-bit word in a 2 ^ b-slot array: min b (Nat.log 2 (w / (2 * d))).

Number of w-bit words 2 ^ (b - slotPackShift b d w) used by a 2 ^ b-slot packed array of d-bit entries.

Whenever 0 < d and 2 * d ≤ w, the total bits 2 ^ (slotPackShift b d w) * d used per word is at most w / 2 (and hence strictly less than w).

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.packedWords_le {b d w : ℕ} (hd : 0 < d) (hdw : 2 * d ≤ w) :
packedWords b d w ≤ 4 * (2 ^ b * d) / w + 1

Word-count upper bound: whenever 0 < d and 2 * d ≤ w, packedWords b d w is at most 4 * (2 ^ b * d) / w + 1.

def Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractPacked (arrBase b d w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) :
Fin (2 ^ d)

Extract the d-bit value at slot q : Fin (2 ^ b) from a packed array starting at arrBase.

def Cslib.DataStructure.ApproximateMembership.QuotientFilter.writePackedMem (arrBase b d w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) (val : ℕ) :
ℕ → ℕ

Update the d-bit entry at slot q : Fin (2 ^ b) in a packed array starting at arrBase to val.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.writePackedMem_outside (arrBase b d w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) (val : ℕ) {a : ℕ} (ha : a < arrBase ∨ arrBase + packedWords b d w ≤ a) :
writePackedMem arrBase b d w mem q val a = mem a
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractPacked_writePackedMem_self (arrBase b d w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) (val : Fin (2 ^ d)) :
extractPacked arrBase b d w (writePackedMem arrBase b d w mem q ↑val) q = val
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractPacked_writePackedMem_of_ne (arrBase b d w : ℕ) (mem : ℕ → ℕ) (q q' : Fin (2 ^ b)) (val : Fin (2 ^ d)) (hne : q' ≠ q) :
extractPacked arrBase b d w (writePackedMem arrBase b d w mem q ↑val) q' = extractPacked arrBase b d w mem q'
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractPacked_writePackedMem (arrBase b d w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) (val : Fin (2 ^ d)) :
extractPacked arrBase b d w (writePackedMem arrBase b d w mem q ↑val) = Function.update (extractPacked arrBase b d w mem) q val

Extract the boolean flag at slot q : Fin (2 ^ b) from a 1-bit packed array at arrBase.

def Cslib.DataStructure.ApproximateMembership.QuotientFilter.writeBitMem (arrBase b w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) (bval : Bool) :
ℕ → ℕ

Update the boolean flag at slot q : Fin (2 ^ b) in a 1-bit packed array at arrBase.

@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractBit_writeBitMem (arrBase b w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) (bval : Bool) :
extractBit arrBase b w (writeBitMem arrBase b w mem q bval) = Function.update (extractBit arrBase b w mem) q bval
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractPacked_zero (arrBase b d w : ℕ) (q : Fin (2 ^ b)) :
extractPacked arrBase b d w (fun (x : ℕ) => 0) q = ⟨0, ⋯⟩
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractBit_zero (arrBase b w : ℕ) (q : Fin (2 ^ b)) :
extractBit arrBase b w (fun (x : ℕ) => 0) q = false

Section 2: Quotient-filter memory layout, space_le, and extractFilterState #

Address base + ReducedKWise.space w w₀ (quotBits w₀ n) (quotBits w₀ n) 5 storing the multiply-shift remainder seed γ : RemSeed w₀.

Base address of the isFull 1-bit packed array (packedWords (quotBits w₀ n) 1 w words).

Base address of the isOcc 1-bit packed array (packedWords (quotBits w₀ n) 1 w words).

Base address of the isRunStart 1-bit packed array (packedWords (quotBits w₀ n) 1 w words).

Base address of the remAt remBits w₀ k-bit packed array (packedWords (quotBits w₀ n) (remBits w₀ k) w words).

Total contiguous memory footprint space w w₀ n k of the two-stage quotient filter in words.

Because effCap w₀ n = min n (2 ^ w₀) ≤ 2 ^ w₀, the quotient bit-width quotBits w₀ n is always at most w₀ + 3.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.space_le {w w₀ n k : ℕ} (hw₀ : 0 < w₀) (hw : 2 * w₀ ≤ w) (hb_w : quotBits w₀ n ≤ w) (hn : 1 ≤ n) (hk : 1 ≤ k) :
space w w₀ n k ≤ 640 * ((n * k + w - 1) / w + 1)

Compact space bound: for all n ≥ 1, k ≥ 1, 0 < w₀, 2 * w₀ ≤ w, and quotBits w₀ n ≤ w, the total Word-RAM footprint space w w₀ n k is at most 640 * ((n * k + w - 1) / w + 1) words.

Extract the remainder seed γ : RemSeed w₀ from mem (remSeedAddr base w w₀ n).

Extract the full seed (θ, γ) : FilterSeed w₀ n from memory at base.

Extract the pure FilterState (quotBits w₀ n) (remBits w₀ k) from the 4 bit-packed slot arrays in mem.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractFilterState_congr {base w w₀ n k : ℕ} {mem₁ mem₂ : ℕ → ℕ} (h : ∀ (a : ℕ), fullBase base w w₀ n ≤ a → a < base + space w w₀ n k → mem₁ a = mem₂ a) :
extractFilterState base w w₀ n k mem₁ = extractFilterState base w w₀ n k mem₂
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractFilterState_of_zero {base w w₀ n k : ℕ} {mem : ℕ → ℕ} (hz : ∀ (a : ℕ), fullBase base w w₀ n ≤ a → a < base + space w w₀ n k → mem a = 0) :
extractFilterState base w w₀ n k mem = FilterState.empty (quotBits w₀ n) (remBits w₀ k)

Zeroed slot arrays [fullBase, base + space) represent FilterState.empty.

Each slot-array write changes its own field; the other three arrays are disjoint from it.

@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractFilterState_writeFull (base w w₀ n k : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ quotBits w₀ n)) (bval : Bool) :
extractFilterState base w w₀ n k (writeBitMem (fullBase base w w₀ n) (quotBits w₀ n) w mem q bval) = (extractFilterState base w w₀ n k mem).setFull q bval
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractFilterState_writeOcc (base w w₀ n k : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ quotBits w₀ n)) (bval : Bool) :
extractFilterState base w w₀ n k (writeBitMem (occBase base w w₀ n) (quotBits w₀ n) w mem q bval) = (extractFilterState base w w₀ n k mem).setOcc q bval
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractFilterState_writeRunStart (base w w₀ n k : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ quotBits w₀ n)) (bval : Bool) :
extractFilterState base w w₀ n k (writeBitMem (runBase base w w₀ n) (quotBits w₀ n) w mem q bval) = (extractFilterState base w w₀ n k mem).setRunStart q bval
@[simp]
theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractFilterState_writeRem (base w w₀ n k : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ quotBits w₀ n)) (r : Fin (2 ^ remBits w₀ k)) :
extractFilterState base w w₀ n k (writePackedMem (remBase base w w₀ n) (quotBits w₀ n) (remBits w₀ k) w mem q ↑r) = (extractFilterState base w w₀ n k mem).setRem q r

Section 3: Slot modular arithmetic and memory layout offsets #

Bitwise identity (o.val + d) &&& (2 ^ b - 1) = (probeSlot b o d).val.

Bitwise identity (q.val + 2 ^ b - a) &&& (2 ^ b - 1) = (backSlot b q a).val for a ≤ 2 ^ b.

theorem Cslib.DataStructure.ApproximateMembership.QuotientFilter.extractPacked_one_val_eq_toNat (arrBase b w : ℕ) (mem : ℕ → ℕ) (q : Fin (2 ^ b)) :
↑(extractPacked arrBase b 1 w mem q) = (extractBit arrBase b w mem q).toNat