This library implements a 0-indexed Fenwick Tree for tracking cumulative values over a dynamic array. It is suitable for systems where frequent prefix sum queries and point updates are required, such as time-based accounting, queuing systems, or share tracking.

Design Characteristics

Invariants and Constraints

Data Structures

struct Tree {
    mapping(uint256 index => int256) _values; // Internal Fenwick Tree nodes.
    uint256 _length;                          // Capacity of the tree (must be power of two).
}

Functions

initialize(Tree storage tree, uint256 length_)

Initializes the tree with the specified length.

length(Tree storage tree) → uint256

Returns the current capacity of the tree.

extend(Tree storage tree)