Skip to main content

SumTree

Struct SumTree 

Source
pub struct SumTree { /* private fields */ }
Expand description

A binary sum tree over capacity leaves. The capacity is rounded up to a power of two so the tree stays complete.

Implementations§

Source§

impl SumTree

Source

pub fn new(capacity: usize) -> Self

Create a tree whose capacity is capacity rounded up to a power of two (at least one).

Source

pub fn capacity(&self) -> usize

Number of leaves the tree can hold.

Source

pub fn len(&self) -> usize

Number of leaves currently written.

Source

pub fn is_empty(&self) -> bool

Source

pub fn is_full(&self) -> bool

Source

pub fn total(&self) -> f32

Sum of every priority in the tree.

Source

pub fn priority(&self, index: usize) -> f32

Read the priority stored at index.

Source

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.

Source

pub fn rebuild(&mut self)

Recompute every internal sum from the leaves, clearing any drift left by repeated SumTree::update calls.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for SumTree

Source§

fn clone(&self) -> SumTree

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SumTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.