pub struct FenwickTree { /* private fields */ }Expand description
Fenwick tree (Binary Indexed Tree) for prefix sum queries over u32 values.
Designed for virtualized list height layout: each entry i stores the height
of item i, and prefix(i) gives the y-offset of item i+1.
Implementations§
Source§impl FenwickTree
impl FenwickTree
Sourcepub fn from_values(values: &[u32]) -> Self
pub fn from_values(values: &[u32]) -> Self
Create a Fenwick tree from an initial array of values in O(n).
This is faster than calling update n times (which would be O(n log n)).
Sourcepub fn set(&mut self, i: usize, value: u32)
pub fn set(&mut self, i: usize, value: u32)
Set element at position i to value (0-indexed). O(log n).
Sourcepub fn get(&self, i: usize) -> u32
pub fn get(&self, i: usize) -> u32
Get the value at position i (0-indexed). O(log n).
Computed as prefix(i) - prefix(i-1).
Sourcepub fn range(&self, left: usize, right: usize) -> u32
pub fn range(&self, left: usize, right: usize) -> u32
Range sum of elements [left..=right] (0-indexed). O(log n).
§Panics
Panics if left > right or right >= n.
Sourcepub fn batch_update(&mut self, deltas: &[(usize, i32)])
pub fn batch_update(&mut self, deltas: &[(usize, i32)])
Apply multiple updates in a single pass. O(m log n), zero alloc.
Each (index, delta) pair is applied sequentially. This produces
identical results to calling update for each pair individually.
Sourcepub fn rebuild(&mut self, values: &[u32])
pub fn rebuild(&mut self, values: &[u32])
Rebuild the tree from a fresh array of values in O(n).
Requires values.len() == self.n.
§Panics
Panics if values.len() != self.n.
Sourcepub fn find_prefix(&self, target: u32) -> Option<usize>
pub fn find_prefix(&self, target: u32) -> Option<usize>
Find the largest index i such that prefix(i) <= target.
Returns None if all prefix sums exceed target (i.e., values[0] > target).
This is useful for binary-search-by-offset in virtualized lists.
O(log n), zero alloc.
Trait Implementations§
Source§impl Clone for FenwickTree
impl Clone for FenwickTree
Source§fn clone(&self) -> FenwickTree
fn clone(&self) -> FenwickTree
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more