FenwickTree Module
A purely functional Fenwick tree (binary indexed tree) parameterized by an associative combining operation. The tree stores partial aggregates over an array of n values, supporting: - point update : O(log n) - prefix query : O(log n) - range query : via prefix difference when the combine has an inverse The index sets used by the Bravyi-Kitaev encoding (update, parity, occupation, remainder) all fall out naturally from the Fenwick tree structure, making this a good foundation for the BK transform.
Types
| Type | Description |
|
A Fenwick tree over values of type 'a. 'combine' must be associative: combine (combine a b) c = combine a (combine b c) 'identity' must be a left/right identity for combine. |
Functions and values
| Function or value |
Description
|
Full Usage:
ancestors n k
Parameters:
int
k : int
Returns: int seq
|
Walk from k toward the root by adding the lowest set bit. Returns the sequence of 1-based ancestor indices (excluding k itself).
|
Full Usage:
build combine identity n f
Parameters:
'a -> 'a -> 'a
identity : 'a
n : int
f : int -> 'a
Returns: FenwickTree<'a>
|
Build a Fenwick tree of size n from a point-value function f, where f returns the raw value at 0-based index i.
|
Full Usage:
descendants k
Parameters:
int
Returns: int seq
|
Walk from k toward the leaves by clearing the lowest set bit. Returns the sequence of 1-based descendant indices (excluding k itself), stopping at the "left wall" of k's subtree.
|
Full Usage:
empty combine identity n
Parameters:
'a -> 'a -> 'a
identity : 'a
n : int
Returns: FenwickTree<'a>
|
Create an empty (all-identity) Fenwick tree of size n.
|
Full Usage:
lsb k
Parameters:
^a
Returns: ^a
Modifiers: inline Type parameters: ^a |
Lowest set bit of k (e.g. lsb 6 = 2, lsb 8 = 8).
|
|
Occupation set Occ(j): the set of indices in j's subtree that together determine element j's value. { j } ∪ { descendants of j+1 in 1-based Fenwick } Returns 0-based indices.
|
Full Usage:
ofArray combine identity values
Parameters:
'a -> 'a -> 'a
identity : 'a
values : 'a array
Returns: FenwickTree<'a>
|
Build a Fenwick tree from an array of values.
|
|
Parity set P(j): the set of tree indices whose stored values contribute to the prefix aggregate of elements 0 .. (j-1). Returns 0-based indices.
|
|
Retrieve the single-element value at 0-based index j. Requires that 'combine' has a notion of "undo" or that we reconstruct from the tree structure. For a XOR / symmetric-difference tree this is exact. For a general tree, this walks the Fenwick descendants.
|
Full Usage:
prefixIndices k
Parameters:
int
Returns: int seq
|
Walk by clearing the lowest set bit starting from k itself. Returns the 1-based prefix-contributing indices.
|
|
Compute the prefix aggregate of elements 0 .. j (0-based, inclusive).
|
|
|
|
|
|
|
Full Usage:
update tree j delta
Parameters:
FenwickTree<'a>
j : int
delta : 'a
Returns: FenwickTree<'a>
|
Return a new Fenwick tree with the value at 0-based index j combined with delta: tree[j] <- combine tree[j] delta
|
|
Update set U(j): the set of ancestor indices that store partial aggregates including element j. These are the nodes that need updating when element j changes. Returns 0-based index set, excluding j itself.
|