Struct fenwick_tree::FenwickTree[][src]

pub struct FenwickTree<I> where
    I: Default + Copy + AddAssign + SubAssign
{ /* fields omitted */ }
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

impl<I> FenwickTree<I> where
    I: Default + Copy + AddAssign + SubAssign
[src]

pub fn with_len(len: usize) -> Self[src]

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.

pub fn len(&self) -> usize[src]

A length of the backing vector of the tree.

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

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

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

Updates the value at i by delta.

Complexity: O(log n).

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.