pub struct SumTree { /* private fields */ }Expand description
A binary sum tree holding exactly capacity leaves.
The backing tree is padded up to a power of two internally; the extra
padding leaves stay at zero weight and are never returned by
SumTree::get. The exposed SumTree::capacity is exactly what you
requested.
Implementations§
Source§impl SumTree
impl SumTree
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Create a tree that holds exactly capacity leaves (at least one).
pub fn is_empty(&self) -> bool
pub fn is_full(&self) -> bool
Sourcepub fn update(&mut self, index: usize, priority: f32)
pub fn update(&mut self, index: usize, priority: f32)
Set the priority at index and repair the sums up to the root.
Ancestors are adjusted by the delta rather than recomputed from both
children, halving the memory traffic per level. Over very many updates
this can accumulate floating-point drift; call SumTree::rebuild to
reset it.
Sourcepub fn rebuild(&mut self)
pub fn rebuild(&mut self)
Recompute every internal sum from the leaves, clearing any drift left by
repeated SumTree::update calls.
Sourcepub fn push(&mut self, priority: f32) -> usize
pub fn push(&mut self, priority: f32) -> usize
Append a priority at the next ring position, overwriting the oldest leaf when full. Returns the leaf index that was written.
Sourcepub fn get(&self, s: f32) -> Option<(usize, f32)>
pub fn get(&self, s: f32) -> Option<(usize, f32)>
Find the leaf whose cumulative-weight interval contains s, returning
its index and priority. s is clamped to [0, total]. Returns None
when the tree is empty or its total weight is zero.
The returned index is always in [0, len), so it is safe to use it in a
side buffer sized to SumTree::capacity.