Struct BIT

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

Binary Indexed Tree (0-indexed)

This data structure supports these two queries in O(log n)

  1. add w to v[at]
  2. the sum of v[begin], v[begin+1], .., v[end-1]

Implementations§

Source§

impl BIT

Source

pub fn new(len: usize) -> BIT

Constructs a new BIT of length len. All values are initialized zero.

§Examples
use algonium::data_structure::BIT;
let mut bit = BIT::new(100);
Source

pub fn len(&self) -> usize

Returns the number of elements in the BIT.

§Examples
use algonium::data_structure::BIT;
let mut bit = BIT::new(100);
assert_eq!(bit.len(), 100);
Source

pub fn add(&mut self, index: usize, value: i64)

Add a value value to a element of index index. v[index] += value

§Panics

Panics if index > len

§Examples
use algonium::data_structure::BIT;
let mut bit = BIT::new(10);
bit.add(5, 100);
assert_eq!(bit.get(3, 6), 100);
bit.add(5, 10);
assert_eq!(bit.get(3, 6), 110);
Source

pub fn get(&self, begin: usize, end: usize) -> i64

sum of v[idx] such that begin <= idx < end

Auto Trait Implementations§

§

impl Freeze for BIT

§

impl RefUnwindSafe for BIT

§

impl Send for BIT

§

impl Sync for BIT

§

impl Unpin for BIT

§

impl UnwindSafe for BIT

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.