Self-Contained Open-Addressed Permutation-Probing Dictionary on the Word-RAM #
This file implements a self-contained open-addressed permutation-probing dictionary realizing
Cslib.DataStructure.Dictionary.spec U V n_max (query and insert up to n_max distinct keys)
on the Word-RAM (Cslib.WordRAM.Basis.exact and Cslib.WordRAM.Basis.standard), combining the
power-of-two odd-stride double-hashing construction of
Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, third edition (MIT Press,
2009), Section 11.4, "Double hashing", pp. 269–277, the k-wise independence double
hashing analysis of J. P. Schmidt and A. Siegel, The Analysis of Closed Hashing under Limited
Randomness, STOC 1990, pp. 224–234; extended NYU Courant Technical Report TR1995-687, May 1995,
pp. 1–2, 4–7, 37–39, and Mikkel Thorup's Double Tabulation
(M. Thorup, Simple Tabulation, Fast Expanders, Double Tabulation, and High Independence, FOCS
2013, pp. 90–99; arXiv:1311.3121v2, pp. 4–5, 11–13, Theorem 1(a) and Lemma 2).
Design and reuse of Cslib.DataStructure.Dictionary.UniformProbing and DoubleHashing #
In the ideal uniform-probing model (CslibFrontier/DataStructure/Dictionary/UniformProbing.lean),
each key
u : Fin U is assigned an independent random permutation π u : Equiv.Perm (Fin m). Storing an
explicit table of U permutations requires Ω(U) words when U = poly(n_max) ≫ n_max.
To achieve a self-contained O_{c_exp}(n_max + 1)-word data structure with assumption-free
O_{c_exp}(1) expected operation time whenever U ≤ max n_max 4 ^ c_exp, we construct for each key
u : Fin U a full-cycle affine permutation of m = 2 ^ M slots
(M = bucketBits n_max = Nat.clog 2 (32 * max n_max 4)):
probePerm θ u i = (h₁(u) + i * (2 * h₂(u) + 1)) mod 2 ^ M, where h₁(u) ∈ Fin (2 ^ M) and
h₂(u) ∈ Fin (2 ^ (M - 1)) are independent instances of Mikkel Thorup's Double Tabulation
(Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation, with c = 2 * max c_exp 1, d = 8 * c,
and b = (1 + Nat.clog 2 (max n_max 4)) / 2).
Because 2 * h₂(u) + 1 is always odd and therefore coprime to 2 ^ M, probePerm θ u is a
bijection on Fin (2 ^ M) for every seed θ and key u
(DoubleHashing.bijective_oddStrideSeq).
We maintain DoubleHashing.OrderedTableRep (probePerm θ) on the concrete Word-RAM slot arrays and
bound the expected probe count by probeBound c_exp n_max across all capacities n_max : ℕ:
at most 3 probes once doubleTabThreshold c_exp ≤ n_max
(DoubleHashing.expect_probeCount_oddStride_doubleTab_le) and at most doubleTabThreshold c_exp
below it (DoubleHashing.expect_probeCount_oddStride_unconditional_le). This yields an
assumption-free expected operation charge charge c_exp n_max, which is at most the
O_{c_exp}(1) bound expectedOpCharge c_exp independent of n_max and U, and equals the
threshold-free hashEvalSteps c_exp + 47 above the threshold, together with
space c_exp n_max ≤ spaceFactor c_exp * (n_max + 1) = O_{c_exp}(n_max + 1) words of memory.
Main definitions #
Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.bucketBits,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.numBuckets,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.numChars,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.tupleLen,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.charBits,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.singleTabSpace,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.slotOffset,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.space,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.spaceFactor: table dimension parameters and memory footprint.Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.Seed,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.seedLaw,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.primaryHash,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.rawStrideHash,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.strideHash,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.probePerm: Double Tabulation seed space, uniform seed law, primary and odd-stride hashes, and full-cycle probe permutation.Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.extractSlots,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.Rep: slot-array extractor and Word-RAM representation invariant (DoubleHashing.OrderedTableRep).Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.allocSpan,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.initSetup,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.opCode,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.subroutineCode,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.probeBound,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.charge,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.expectedOpCharge,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.impl,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.implStandard: structured Word-RAM programs and assumption-freeRandomized.StateTransitionImplcertificates overBasis.exactandBasis.standard.
Main results #
Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.space_le:space c_exp n_max ≤ spaceFactor c_exp * (n_max + 1) = O_{c_exp}(n_max + 1)across alln_max.Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.setup_exec_spec: randomized seed and table initialization insetupSteps c_exp n_max = O_{c_exp}(n_max + 1)instructions.Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.opCode_execWithin: everyqueryorinsertoperation on keyuexecutes in at mostopPathSteps c_exp θ T u = 13 * probeCount T u (probePerm θ u) + hashEvalSteps c_exp + 8instructions.Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.seq_expected_amortized: assumption-free expected operation timecharge c_exp n_maxacross any admissible sequenceops : List (Dictionary.Op U)wheneverU ≤ max n_max 4 ^ c_exp.Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.charge_le_expectedOpCharge,Cslib.DataStructure.Dictionary.WordRAM.UniformProbing.charge_of_le: the charge is at most theO_{c_exp}(1)boundexpectedOpCharge c_expfor everyn_max, and equalshashEvalSteps c_exp + 47(at most3expected probes) oncedoubleTabThreshold c_exp ≤ n_max.
References #
- T. H. Cormen, C. E. Leiserson, R. L. Rivest, and C. Stein, Introduction to Algorithms, third edition (MIT Press, 2009), Section 11.4, pp. 269–277
- J. P. Schmidt and A. Siegel, The Analysis of Closed Hashing under Limited
Randomness (STOC 1990, pp. 224–234; NYU Courant Technical Report
TR1995-687, May 1995, pp. 1–2, 4–7, 37–39) - M. Thorup, Simple Tabulation, Fast Expanders, Double Tabulation, and High
Independence (FOCS 2013, pp. 90–99;
arXiv:1311.3121v2, pp. 4–5, 11–13)
Section 1: Table Parameters, Seed Space, and Full-Cycle Probe Permutations #
Log₂ of the power-of-2 table size: M = ⌈log₂ (32 * max n_max 4)⌉ ≥ 7.
Power-of-2 number of open-addressed table slots m = 2 ^ bucketBits n_max ≥ 32 * max n_max 4.
Number of base-2 ^ b characters c = 2 * max c_exp 1 for Double Tabulation under
U ≤ max n_max 4 ^ c_exp.
Intermediate tuple length d = 8 * numChars c_exp = 16 * max c_exp 1 for Double Tabulation.
Character bit-width b = (1 + Nat.clog 2 (max n_max 4)) / 2 for Double Tabulation.
Offset 2 * singleTabSpace c_exp n_max of the open-addressed slot arrays relative to base
(after the primary hash table at [base, base + singleTabSpace c_exp n_max) and the stride hash
table at [base + singleTabSpace c_exp n_max, base + 2 * singleTabSpace c_exp n_max)).
Total memory footprint slotOffset c_exp n_max + 3 * numBuckets n_max words in
[base, base + space c_exp n_max).
Explicit O_{c_exp}(1) linear-space factor bounding space c_exp n_max by
spaceFactor c_exp * (n_max + 1) across every n_max : ℕ.
All-n_max linear space bound (O_{c_exp}(n_max + 1)):
for every c_exp, n_max : ℕ, space c_exp n_max ≤ spaceFactor c_exp * (n_max + 1).
Combined Double Tabulation seed DoubleHashSeed c_exp n_max (bucketBits n_max) consisting of
the primary hash seed (T₁, T₂) and the half-range stride hash seed (S₁, S₂).
Double Tabulation primary slot h₁(u) ∈ Fin (numBuckets n_max).
Double Tabulation half-range stride index h₂(u) ∈ Fin (2 ^ (bucketBits n_max - 1)).
Odd probe stride 2 * (rawStrideHash θ u).val + 1 ∈ Fin (numBuckets n_max).
Full-cycle double-hashing probe permutation
probePerm θ u : Equiv.Perm (Fin (numBuckets n_max)) sending probe index i to
(h₁(u) + i * (2 * h₂(u) + 1)) mod 2 ^ M.
Section 2: Concrete Slot Extraction and Representation Invariant #
Well-formedness of the flat open-addressed slot arrays at
[base + slotOffset c_exp n_max, base + slotOffset c_exp n_max + 3 * numBuckets n_max):
each used[j] word is 0 or 1, and whenever used[j] = 1, key[j] < U.
Extract the abstract open-addressed table UniformProbing.Table U (numBuckets n_max) from
Word-RAM memory mem.
Coupled representation invariant Rep w base c_exp U V n_max θ s x on Data 16:
x.Bounded w;x.regs 0 = base;DoubleTabulation.SeedRepholds forθ.1at[base, base + singleTabSpace c_exp n_max)and forθ.2at[base + singleTabSpace c_exp n_max, base + 2 * singleTabSpace c_exp n_max);SlotArrayWF base c_exp U n_max x.memandDoubleHashing.OrderedTableRep (probePerm θ) s (extractSlots base c_exp U n_max x.mem)hold on the slot arrays;(Dictionary.dom s).card ≤ n_maxandDictionary.ValuesBounded V s.
- bounded : WordRAM.Data.Bounded w x
- primary_rep : HashFamily.WordRAM.DoubleTabulation.SeedRep base (numChars c_exp) (tupleLen c_exp) (charBits n_max) (bucketBits n_max) θ.1 x.mem
- stride_rep : HashFamily.WordRAM.DoubleTabulation.SeedRep (base + singleTabSpace c_exp n_max) (numChars c_exp) (tupleLen c_exp) (charBits n_max) (bucketBits n_max - 1) θ.2 x.mem
- slot_wf : SlotArrayWF base c_exp U n_max x.mem
- vals_bounded : ValuesBounded V s
Section 3: Register Layout, Allocation, and Randomized Initialization #
Register 0 (baseReg, persistent): holds the base memory address base.
Register 1 (curReg): loop condition / Double Tabulation key input.
Register 2 (zeroReg): memory address / Double Tabulation output.
Register 3 (oneReg): current probe slot slot ∈ Fin (numBuckets n_max).
Register 4: slot array base address base + slotOffset c_exp n_max.
Register 5: bucket mask numBuckets n_max - 1.
Register 6: odd probe stride strideHash θ u.
Register 7: table size numBuckets n_max.
Register 8: constant 0 during probing / general scratch.
Register 12 (output): operation tag (0 for query, 1 for insert) on input; decoded
return value (0 for none/(), v + 1 for some v) on output.
Register 13: input key u.val < U.
Register 14 (frontierReg): input value v < V for insert / allocation frontier.
Register 15: temporary register saving primaryHash θ u across the second hash evaluation.
Deterministic 2-instruction glue advancing regBase by singleTabSpace c_exp n_max.
Deterministic 2-instruction glue restoring regBase from base + singleTabSpace c_exp n_max
back to base.
Complete randomized setup program initSetup c_exp n_max: resamples the primary Double
Tabulation seed at [base, base + singleTabSpace c_exp n_max) and the stride Double Tabulation
seed at [base + singleTabSpace c_exp n_max, base + 2 * singleTabSpace c_exp n_max).
Total instruction count
2 * resampleSteps (numChars c_exp) (tupleLen c_exp) (charBits n_max) + 4 of
initSetup c_exp n_max.
Full Randomized.StateTransitionImpl.setup_exec certificate for initSetup c_exp n_max.
Probe inspection snippet probeCheckCode (≤ 9 instructions): checks whether the slot in
regSlot belongs to otherOccupiedSlots (extractSlots base c_exp U n_max d.mem) u (i.e.,
used[slot] = 1 and key[slot] ≠ u.val), storing 1 in regCond if so and 0 otherwise.
One iteration of the probe loop (≤ 11 instructions): advances regSlot by regStride modulo
numBuckets n_max (regMaskM) and runs probeCheckCode.
Probe loop: continues advancing regSlot while 0 < regCond (i.e., while regSlot is occupied
by a key other than u).
Deterministic 3-instruction glue between the primary and stride DoubleTabulation.evalCode
calls: saves (primaryHash θ u).val from regAddr into regSave and advances regBase by
singleTabSpace c_exp n_max.
Deterministic 11-instruction glue after the stride DoubleTabulation.evalCode call:
restores regBase = base, forms regStride = 2 * regAddr + 1, restores regSlot = regSave,
and initializes regSlotBase, regMaskM, regM, and regZero.
Hash evaluation and probe-register setup opEvalHashesCode c_exp n_max: evaluates
primaryHash θ u into regSlot, strideHash θ u into regStride, and initializes
regSlotBase, regMaskM, regM, and regZero.
Exact deterministic instruction count
2 * DoubleTabulation.evalSteps (numChars c_exp) (tupleLen c_exp) + 15 = O_{c_exp}(1) of
opEvalHashesCode c_exp n_max (independent of n_max and U).
Lookup completion program finishQueryCode (≤ 9 instructions): inspects used[regSlot];
if 0, writes 0 (none) into regOp, and if 1, loads val[regSlot] and writes
val[regSlot] + 1 (some v) into regOp.
Insertion completion program finishInsertCode (8 instructions): writes used[regSlot] := 1,
key[regSlot] := regU, val[regSlot] := regV, and sets regOp := 0.
Complete operation body opCode c_exp n_max for both query and insert.
Exact pathwise instruction budget
13 * probeCount T u (probePerm θ u) + hashEvalSteps c_exp + 8 for opCode c_exp n_max on table
T and key u.
Worst-case instruction bound 13 * numBuckets n_max + hashEvalSteps c_exp + 8 for
opCode.
Key extracted from a Dictionary.Op U.
Operation tag (0 for query, 1 for insert).
Pathwise correctness and complexity of opCode c_exp n_max: for any seed θ, dictionary
state s, machine state x satisfying Rep w base c_exp U V n_max θ s x, and admissible operation
op, opCode c_exp n_max halts deterministically in at most
opPathSteps c_exp U n_max θ (extractSlots base c_exp U n_max x.mem) (opKey op)
≤ opSteps c_exp n_max instructions, preserving Rep for
((Dictionary.spec U V n_max).stepFn s op).1, writing opOutWord s op into regOp, and modifying
memory only within [base, base + space c_exp n_max).
Section 5: SubroutineCode, StateTransitionImpl, and Expected Probe / Collision Bounds #
Relocatable SubroutineCode over Basis.exact (16 registers, 0 runtime init parameters)
for the open-addressed permutation-probing dictionary.
Expected per-operation instruction charge
13 * doubleTabThreshold c_exp + hashEvalSteps c_exp + 8 = O_{c_exp}(1) for both query and
insert (independent of n_max and U). It bounds charge c_exp n_max at every capacity
(charge_le_expectedOpCharge).
Expected probe-count bound at capacity n_max: 3 once doubleTabThreshold c_exp ≤ n_max
(DoubleHashing.expect_probeCount_oddStride_doubleTab_le), and below the threshold the cap
doubleTabThreshold c_exp on the deterministic probe count n_max + 1.
Expected per-operation instruction charge
13 * probeBound c_exp n_max + hashEvalSteps c_exp + 8 at capacity n_max, for both query and
insert. It is at most expectedOpCharge c_exp for every n_max, and equals the threshold-free
hashEvalSteps c_exp + 47 once doubleTabThreshold c_exp ≤ n_max (charge_of_le).
Above the Double Tabulation threshold the per-operation charge is
13 * 3 + hashEvalSteps c_exp + 8, independent of doubleTabThreshold c_exp.
Composable randomized Word-RAM StateTransitionImpl certificate over Basis.exact for the
open-addressed odd-stride double-hashing dictionary realizing Dictionary.spec U V n_max with
$O_{c_{\exp}}(1)$ expected operation time (charge c_exp n_max ≤ expectedOpCharge c_exp, and
hashEvalSteps c_exp + 47 once doubleTabThreshold c_exp ≤ n_max) and
$O_{c_{\exp}}(n_{\max} + 1)$ space (space c_exp n_max) for every n_max : ℕ whenever
U ≤ max n_max 4 ^ c_exp.
Standard-basis (Basis.standard) randomized StateTransitionImpl certificate for the
open-addressed
odd-stride double-hashing dictionary.
End-to-end multi-operation expected-time theorem (seq_expected_amortized):
from fresh zeroed memory [base, base + space c_exp n_max), initializing the odd-stride
double-hashing dictionary (initFresh) and executing the subroutine operation bodies
(ExecStepsOfFn) across any admissible operation history ops : List (Dictionary.Op U) fixed
independently of the sampled seed (oblivious adversary) takes at most
3 + setupSteps c_exp n_max + ops.length * charge c_exp n_max expected Word-RAM instructions
whenever U ≤ max n_max 4 ^ c_exp. The per-operation charge is at most the $O_{c_{\exp}}(1)$ bound
expectedOpCharge c_exp for every n_max : ℕ (charge_le_expectedOpCharge) and equals
hashEvalSteps c_exp + 47 once doubleTabThreshold c_exp ≤ n_max (charge_of_le).
Ideal uniform-probing expected-step corollary (≤ hashEvalSteps c_exp + 34):
when the probe permutation π is drawn uniformly from Equiv.Perm (Fin (numBuckets n_max)) on a
table T with at most n_max other occupied slots ((otherOccupiedSlots T u).card ≤ n_max), the
expected Word-RAM step count 13 * probeCount T u π + hashEvalSteps c_exp + 8 is at most
hashEvalSteps c_exp + 34 (combining expect_probeLinear_le with B = 2 from
UniformProbing.expect_unsuccessfulProbes_le_two).