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
O(log n) complexity for both updates and prefix sum queries.
Storage-efficient using a mapping(uint256 => int256), instead of an array.
Only supports lengths that are exact powers of two (2^k), which simplifies internal logic and allows future extensions via extend().
Invariants and Constraints
The tree must be initialized with a power-of-two length > 0 via initialize(...).
Index bounds are enforced — access beyond the current capacity reverts with IndexOutOfBounds().
To support dynamic resizing, extend() can double the current tree length (up to a safe limit).
Use of negative values is supported in modify(...), allowing decrement operations.
Data Structures
structTree{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.
Reverts with InvalidLength() if length_ == 0 or not a power of two.
Only callable once (re-initialization is not allowed).