Documentation

CslibFrontier.DataStructure.FusionTree.WordRAM.Rank

Packed sketch ranks #

Guard extraction and multiplication count all simultaneous comparisons. Complementing the sketches reverses the comparison to obtain inclusive ranks. None of these operations assumes a most-significant-bit primitive. Node constants must be precomputed and loaded separately.

Main definitions #

Main results #

References #

One guard bit per field, with all other bits zero.

Move each comparison's guard to the lowest bit of its radix field.

theorem Cslib.DataStructure.FusionTree.WordRAM.guardFlags_eq {b n q : ℕ} (s : ℕ → ℕ) (hs : ∀ i < n, s i < 2 ^ b) (hq : q < 2 ^ b) :
guardFlags b n s q = BitString.packRadix (2 ^ (b + 1)) n fun (i : ℕ) => (decide (q ≤ s i)).toNat

Guard extraction returns exactly the packed zero-or-one comparison digits.

Count all set comparison guards through the middle field of one product.

theorem Cslib.DataStructure.FusionTree.WordRAM.countGuards_eq {b n q : ℕ} (s : ℕ → ℕ) (hn : 0 < n) (hb : n < 2 ^ (b + 1)) (hs : ∀ i < n, s i < 2 ^ b) (hq : q < 2 ^ b) :
countGuards b n s q = ∑ i ∈ Finset.range n, (decide (q ≤ s i)).toNat

countGuards b n s q counts the indices i < n with q ≤ s i.

theorem Cslib.DataStructure.FusionTree.WordRAM.countGuards_fits {w b n q : ℕ} (s : ℕ → ℕ) (hs : ∀ i < n, s i < 2 ^ b) (hq : q < 2 ^ b) (hw : 2 * ((b + 1) * n) ≤ w) :

Every intermediate word of the eight-instruction guard count fits when the packed node occupies at most half the word: the replicated query, the packed difference, its guards, the extracted flags, the count product, its middle digit, and the count.

An inclusive sketch rank, obtained by complementing both sides of each comparison.

theorem Cslib.DataStructure.FusionTree.WordRAM.upperRankExpr_eq_card {b n q : ℕ} (s : ℕ → ℕ) (hn : 0 < n) (hb : n < 2 ^ (b + 1)) (hs : ∀ i < n, s i < 2 ^ b) (hq : q < 2 ^ b) :
upperRankExpr b n s q = {i ∈ Finset.range n | s i ≤ q}.card

The expression returns the number of sketches at or below the query. Sortedness is unnecessary for the arithmetic identity; a tree uses sorted sketches to interpret the rank.

@[reducible, inline]

Query sketch.

@[reducible, inline]

Packed guarded complements of the stored sketches.

@[reducible, inline]

Replication multiplier.

@[reducible, inline]

Mask selecting all guard bits.

@[reducible, inline]

Number of payload bits per field.

@[reducible, inline]

Power selecting the count product's middle digit.

@[reducible, inline]

Field radix.

@[reducible, inline]

Largest possible payload.

@[reducible, inline]

Replicated complemented query.

@[reducible, inline]

Scratch register and final inclusive rank.

Eight standard instructions compute the complete inclusive sketch rank. All eight input registers and all memory are preserved. The precomputed constants are supplied by the caller.

The exact final data, retaining the intermediate temporary register.

theorem Cslib.DataStructure.FusionTree.WordRAM.RankProgram.exec {w b n q : ℕ} (s : ℕ → ℕ) (x : WordRAM.Data 10) (hn : 0 < n) (hb : n < 2 ^ (b + 1)) (hs : ∀ i < n, s i < 2 ^ b) (hq : q < 2 ^ b) (hw : 2 * ((b + 1) * n) ≤ w) (hxq : x.regs query = q) (hxn : x.regs node = packedNode b n fun (i : ℕ) => 2 ^ b - 1 - s i) (hxc : x.regs copies = queryMultiplier b n) (hxg : x.regs guards = guardMask b n) (hxb : x.regs bits = b) (hxm : x.regs middle = 2 ^ ((b + 1) * (n - 1))) (hxr : x.regs base = 2 ^ (b + 1)) (hxmax : x.regs maximum = 2 ^ b - 1) :
WordRAM.Exec w code x (result x b n s q) 8 ∧ (result x b n s q).regs out = {i ∈ Finset.range n | s i ≤ q}.card ∧ (result x b n s q).mem = x.mem

Checked execution of the full inclusive rank in eight instructions. The packed word occupies at most half the physical width, making the count multiplication legal too. Construction/loading of the node constants and compressed query remain separate.