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 #
- Specialization and external word width: For fixed universe
Uand capacity boundn_maxand an admissible external machine word widthw(2 * hashBits U n_max ≤ w,Fits w (base + space n_max),Fits w V), the specialized programsubroutineCode U n_maxuses precomputed configuration literals (bucketBits,hashBits, bit masks, and layout offsets baked as instruction literals, withinitParams = Fin.elim0). - Charged body cost and oblivious adversary: The expected time bounds charge initialization and
subroutine operation bodies across an operation history
opsfixed independently of the sampled hash seed (oblivious adversary), excluding caller argument delivery, caller control/dispatch loops, and code/configuration construction. - Insert-only node pool: Because
Dictionary.spec U V n_maxis insert-only, both the linear space boundspace n_max ≤ 5 * n_max + 8and sequential bump allocation (allocCount ∈ [0, n_max]) rely on the absence of deletions; a future deletion extension would revisit node-pool slot recycling.
Design and memory layout #
Given key universe Fin U and capacity bound n_max, the data structure chooses:
- power-of-two bucket count
m = numBuckets n_max = 2 ^ MwithM = bucketBits n_max = Nat.clog 2 (max n_max 2), somax n_max 2 ≤ 2 ^ M ≤ 2 * max n_max 2and the load factor satisfiesn / m ≤ n_max / 2 ^ M ≤ 1; - multiply-shift input bit-width
w₀ = hashBits U n_max = max M (Nat.clog 2 (max U 2)), so0 < w₀,M ≤ w₀, andU ≤ 2 ^ w₀.
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:
[base, base + 4): 4-word header storing the sampled multiply-shift seedb < 2 ^ (w₀ - 1),M,w₀, and the sequential node allocation counterallocCount ∈ [0, n_max];[base + 4, base + 4 + 2 ^ M): bucket head arrayhead[0 .. 2 ^ M - 1]storing0for an empty chain andidx + 1for pool slotidx ∈ [0, allocCount)(so fresh zeroed memory already represents2 ^ Mempty buckets inO(1)instructions);[base + 4 + 2 ^ M, base + 4 + 2 ^ M + 3 * n_max): staticn_max-slot node pool split intokey[0 .. n_max - 1],val[0 .. n_max - 1], andnext[0 .. n_max - 1].
Operations and expected complexity #
initSetup: invokesMultiplyShift.initCode w₀(1Instr.randcall) to sample a uniform odd multiplier seedb ∈ Fin (2 ^ (w₀ - 1))and writes the 4-word header (16instructions total forinitFreshon zeroed memory,O(n_max)viainitClearon arbitrary dirty memory).query uandinsert u v: evaluate the bucket indexbk = multiplyShift w₀ M b (encodeKey u)viaMultiplyShift.evalCode w₀ M, traverse the singly-linked chain fromhead[bk]viafindInChain, and either returnval[idx] + 1/0(query), overwriteval[idx] ← vin place on a key hit (insert), or bump-allocate slotallocCountand prepend it tohead[bk]on a key miss (insert).- By linearity of expectation and the
(2 / 2 ^ M)-universality of multiply-shift hashing (Cslib.Probability.isUniversalHash_multiplyShift, [DHKP97] Lemma 2.4; [CLRS09], Section 11.3.3, Theorem 11.3 and Corollary 11.4), the expected number of other keys indom s \ {u}colliding withuis at most((dom s).card : ℝ≥0∞) * (2 / 2 ^ M) ≤ n_max * (2 / 2 ^ M) ≤ 2, giving worst-case expectedO(1)(chargeVal = 73) body instructions per operation with zero potential (potential = 0).
Main definitions #
Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.bucketBits,numBuckets,hashBits,space: table dimension parameters andO(n_max)memory footprint.Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.subroutineCode: relocatableSubroutineCodebundle overBasis.exact.Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.Rep: seed-coupled concrete representation invariant.Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.impl,implStandard:Cslib.DataStructure.WordRAM.Randomized.StateTransitionImplcertificates overBasis.exactandBasis.standard.
Main results #
Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.space_le: linear space boundspace n_max ≤ 5 * n_max + 8.Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.expect_collCount_le_card_mul,expect_collCount_le_two: universal-hashing expected collision bounds.Cslib.DataStructure.Dictionary.WordRAM.ChainedHash.seq_expected_amortized: expected subroutine body step bound across any admissible oblivious sequence ofqueryandinsertoperations.
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.
2. Register layout, memory offsets, and structured Word-RAM code #
Total local register count of the chained hash table (1 persistent + 21 scratch/caller).
Multiply-shift seed register (0).
Multiply-shift key register (1).
Multiply-shift bucket output register (2).
Multiply-shift scratch register 1 (3, also curReg for zeroFillCode).
Multiply-shift scratch register 2 (4).
Persistent base address register (5).
Bump-allocation frontier register (6).
Operation tag register (7: 0 = query, ≠ 0 = insert).
Input key register u.val (8).
Input value register v (9).
Output register (10: 0 = none, v + 1 = some v on query).
Constant 0 scratch register (11, also zeroReg for zeroFillCode).
Constant 1 scratch register (12, also oneReg for zeroFillCode).
Bucket head array base base + 4 (13).
Node key array base base + 4 + numBuckets n_max (14).
Node value array base base + 4 + numBuckets n_max + n_max (15).
Node next-pointer array base base + 4 + numBuckets n_max + 2 * n_max (16).
Current 1-based chain pointer (17).
Current 0-based pool slot index (18).
Key-hit indicator flag (19).
Matched 0-based pool slot index (20).
Scratch memory address / value register (21).
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].
Bump-allocate space n_max words at frontierReg, recording baseReg := frontierReg and
advancing frontierReg := baseReg + space n_max.
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.
Load the stored seed b into regB and copy key argU into regX.
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.
Traverse the singly-linked bucket chain starting at cur.
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.
Complete insert u v after findInChain.
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.
3. Concrete representation invariant and chain length bound #
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.
Invariant for a single bucket bk < numBuckets n_max with chain L : List ℕ.
Lis the chain of 0-based slot indices starting athead[bk].Keys stored along
Lare pairwise distinct.- sound (idx : ℕ) : idx ∈ L → ∃ (u : Fin U), mem (keyBase base n_max + idx) = ↑u ∧ Probability.multiplyShift (hashBits U n_max) (bucketBits n_max) b (encodeKey u) = bk ∧ s u = some (mem (valBase base n_max + idx))
Every node
idx ∈ Lstores a keyu ∈ dom shashing tobkwith valueval[idx]. - complete (u : Fin U) (v : ℕ) : Probability.multiplyShift (hashBits U n_max) (bucketBits n_max) b (encodeKey u) = bk → s u = some v → ∃ idx ∈ L, mem (keyBase base n_max + idx) = ↑u
Every key
u ∈ dom shashing tobkappears inL.
If idx₁ ∈ L₁ (in bucket bk₁) and idx₂ ∈ L₂ (in bucket bk₂) and idx₁ = idx₂, then
bk₁ = bk₂ and L₁ = L₂.
Within a single bucket bk, two slots idx₁, idx₂ ∈ L have the same stored key if and only if
idx₁ = idx₂.
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.
Seed-coupled concrete representation invariant for the chained hash table at base.
- bounded : WordRAM.Data.Bounded w x
All registers and memory words fit in
w-bit words. Persistent base-address register holds
base.Header word
base + 0stores the sampled multiply-shift seedb.val.Header word
base + 1storesbucketBits n_max.Header word
base + 2storeshashBits U n_max.Header word
base + 3storesallocCount = (dom s).card.Current key count is at most
n_max.- vals_lt : ValuesBounded V s
Every stored value in
sis strictly less thanV. Every bucket
bk < numBuckets n_maxsatisfiesBucketInv.
4. Initialization execution (allocSpan, storeHeader, initSetup) #
5. Deterministic operation execution (evalBucket, findInChain, finishOp, opCode) #
Key queried or inserted by a dictionary operation o : Dictionary.Op U.
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 #
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).
Worst-case expected instruction charge per dictionary operation (55 + 9 * 2 = 73).
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).
Chained hash table Randomized.StateTransitionImpl certificate over Basis.standard.
Obtained from impl by basis transport (Randomized.StateTransitionImpl.toStandard).
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.