Word-RAM Fenwick Trees (Binary Indexed Trees) #
This module implements Fenwick trees ([Fenwick1994], pp. 328–332) as fixed bounded-word machine
programs over Cslib.WordRAM.Basis.exact (lifted to Basis.standard via
StateTransitionImpl.toStandard), realizing dynamic prefix sums (PrefixSums.Op ℕ,
PrefixSums.step) in exact n words of memory [base, base + n) for any length n:
- Bitwise lowest-set-bit stepping (
and_pred_eq_sub_lowbit,sub_and_pred_eq_lowbit): on unsigned naturals,m &&& (m - 1) = m - lowbit mclears the lowest set bit without word-width masks, andm - (m &&& (m - 1)) = lowbit misolates it inO(1)exact-basis instructions with every intermediate value≤ m. - Overflow-free parent bounds: upward updates and immediate-parent construction compare
n - m < lowbit mbefore computingm + lowbit m, requiring onlyFits w (base + n)rather thanFits w (base + 2 * n). - Logarithmic operations (
opCode,impl,implStandard,certificate): a single fixed 12-registerSubroutineCode .exact 12 1serves both point increments (add pos δ) and prefix queries (sum len, clamped ton) within13 * Nat.clog 2 (s.length + 1) + 10body instructions (+ 2with canonical result recoding andhaltinDynamicCertificate). - Linear initialization (
initClear_exec,buildCode,buildCode_spec): dirty-memory zero initialization runs in4 * n + 9instructions viaSubroutineBase.initClear_exec(3instructions on fresh zeroed memory viainitFresh_exec), and in-place immediate-parent propagationbuildCodeconverts raw array entriessin[base, base + s.length)into a valid Fenwick tree (ofList s) in at most17 * s.length + 5exact-basis instructions wheneverFits w s.sum.
Main definitions #
Cslib.DataStructure.FenwickTree.WordRAM.spec: dynamic prefix-sumStateTransitionSpeconList ℕwith word-width admissibilityvalid w.Cslib.DataStructure.FenwickTree.WordRAM.treeOfMem,Rep: view of[base, base + n)as aFenwickTree ℕand bounded machine representation invariant.Cslib.DataStructure.FenwickTree.WordRAM.queryLoop,updateLoop,opCode,subroutineCode: exact-basis structured loops, dispatcher, and relocatableSubroutineCode .exact 12 1.Cslib.DataStructure.FenwickTree.WordRAM.buildCode:O(n)in-place immediate-parent Fenwick tree construction overBasis.exact.Cslib.DataStructure.FenwickTree.WordRAM.impl,implStandard,recoding,certificate:WordRAM.StateTransitionImplcertificates overBasis.exactandBasis.standardand standaloneDynamicCertificate.
Main results #
Cslib.DataStructure.FenwickTree.and_pred_eq_sub_lowbit,sub_and_pred_eq_lowbit: unsigned bitwise identitiesm &&& (m - 1) = m - lowbit mandm - (m &&& (m - 1)) = lowbit m.Cslib.DataStructure.FenwickTree.WordRAM.queryLoop_spec,updateLoop_spec,opCode_spec: logarithmic instruction bounds and memory framing for prefix queries and point increments.Cslib.DataStructure.FenwickTree.WordRAM.buildCode_spec: in-place immediate-parent construction establishesRepfors(matchingofList s) in at most17 * s.length + 5instructions.Cslib.DataStructure.FenwickTree.WordRAM.instruction_bound,totalCost_charge,client_spec:13 * Nat.clog 2 (s.length + 1) + 12per-operationDynamicPrograminstruction bound and adaptive-client execution theorem.
References #
Unsigned bitwise lowbit identities #
Word-bounded dynamic prefix-sum specification #
Word-fit admissibility for PrefixSums.Op ℕ at word width w: operation arguments fit in a
w-bit word, and an in-bounds increment add pos δ keeps the total sum below 2 ^ w.
Dynamic prefix sums on List ℕ restricted to w-bit fitting operations (PrefixSums.step).
Memory view as a FenwickTree ℕ #
Extract the n-word region [base, base + n) of Word-RAM memory as a FenwickTree ℕ.
Register assignment and machine programs #
Persistent bump-pointer frontier register (base + n after initialization).
Persistent base address of [base, base + n).
Persistent array length n.
Operation tag (0 for add, nonzero for sum).
Operation pos (for add) or len (for sum).
Operation increment δ (for add).
Query accumulator / output register (curReg during initClear).
Traversal index register (1-based node m or 0-based counter in buildCode).
Constant 0 scratch (zeroReg during initClear).
Constant 1 scratch (oneReg during initClear).
Bitwise / address scratch 1.
Address / value scratch 2.
Concrete Fenwick-tree representation invariant at [base, base + n) and word width w.
- bounded : WordRAM.Data.Bounded w x
All registers and memory words fit in
wbits. Persistent base register holds
b.Persistent size register holds
n.- sum_fits : WordRAM.Fits w s.sum
Total sum of
sfits inwbits (so no prefix or Fenwick interval sum overflows). [b, b + n)forms the unique valid Fenwick tree fors.
Downward Fenwick prefix-query loop (while 0 < rIdx).
queryLoop accumulates (s.take m).sum into rAns in 7 * (downChain m).length + 2
instructions (≤ 7 * Nat.clog 2 (n + 1) + 2), preserving memory and persistent registers.
Upward point-update loop (updateLoop) #
Upward Fenwick point-update loop (while 0 < rIdx).
updateLoop performs addLoop n m δ in-place on [b, b + n) in at most
13 * (upChain n m).length + 2 instructions (≤ 13 * Nat.clog 2 (n + 1) + 2), preserving
outside memory and persistent registers.
Operation dispatcher and StateTransitionImpl #
Constant-register initialization prefix of opCode.
Trivial post-zeroed scalar setup.
Relocatable SubroutineCode over Basis.exact for Fenwick-tree dynamic prefix sums.
Decode the typed result of PrefixSums.Op ℕ from rAns.
Representation-independent per-operation instruction budget 13 * ⌈log₂ (|s| + 1)⌉ + 10.
Per-operation execution theorem for opCode over Basis.exact.
Unified WordRAM.StateTransitionImpl certificate over Basis.exact for Fenwick-tree dynamic
prefix sums of length n starting from List.replicate n 0.
Standard-basis Fenwick-tree WordRAM.StateTransitionImpl, lifted from Basis.exact.
Canonical native result format for PrefixSums.Op.Result ℕ.
Paid 1-instruction canonical result recoding for standalone DynamicProgram projection.
Standalone DynamicCertificate over Basis.standard for Fenwick-tree dynamic prefix sums.
Worst-case instruction bound 13 * Nat.clog 2 (s.length + 1) + 12 per DynamicProgram call
(including 1-instruction canonical recoding and final halt).
Every admissible adaptive client executes on the Word-RAM Fenwick tree from any valid
representation Rep b n w s x (whether zero-initialized or constructed in O(n) instructions by
buildCode_spec), returning (spec w).clientResult s p in at most
history.length * (13 * Nat.clog 2 (s.length + 1) + 12) total instructions.
In-place O(n) Fenwick-tree construction over Basis.exact: transforms an array of n raw
values in [rBase, rBase + n) into its Fenwick tree (ofList s).
In-place immediate-parent propagation buildCode transforms raw entries s in
[b, b + s.length) into a valid Rep b s.length w s y (whose memory equals ofList s) within
17 * s.length + 5 exact-basis instructions, preserving outside memory and persistent registers.