fenwick_tree

Struct FenwickTree

Source
pub struct FenwickTree<I>{ /* private fields */ }
Expand description

An implementation of the binary indexed tree (Fenwick tree) data structure.

The tree is backed by a simple vec of the fixed size where each item is responsible for storing cumulative sum of some range, allowing to perform queries and updates in O(log n) time.

Implementations§

Source§

impl<I> FenwickTree<I>

Source

pub fn with_len(len: usize) -> Self

Constructs a new Fenwick tree with the specified len with each element set as I::default().

The vector is initialized with vec![I::default(); len].

§Panics

Vector initialization may panic if len is too big.

Source

pub fn len(&self) -> usize

A length of the backing vector of the tree.

Source

pub fn sum(&self, bounds: impl RangeBounds<usize>) -> Result<I, SumError>

A partial sum of the specified bounds.

Complexity: O(log n).

Note that sum for empty range (a range (i..j) where i >= j) is 0.

Also, bounds are converted into a pair (start, end) that represents [start, end) range. This means that boundary case tree.sum(i..=usize::MAX) fallbacks to tree.sum(0..usize::MAX). However, in practice, it’s not possible to construct such a big tree (Vec panics with capacity overflow).

Source

pub fn add(&mut self, i: usize, delta: I) -> Result<(), AddError>

Updates the value at i by delta.

Complexity: O(log n).

Auto Trait Implementations§

§

impl<I> Freeze for FenwickTree<I>

§

impl<I> RefUnwindSafe for FenwickTree<I>
where I: RefUnwindSafe,

§

impl<I> Send for FenwickTree<I>
where I: Send,

§

impl<I> Sync for FenwickTree<I>
where I: Sync,

§

impl<I> Unpin for FenwickTree<I>
where I: Unpin,

§

impl<I> UnwindSafe for FenwickTree<I>
where I: UnwindSafe,

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> 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, 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.