Struct PointSegment

Source
pub struct PointSegment<N, O>
where O: Commutative<N> + Identity<N>,
{ /* private fields */ }
Expand description

This data structure allows range modification and single element queries.

This tree allocates 2n * sizeof(N) bytes of memory.

This tree is implemented using a binary tree, where each node contains the changes that need to be propogated to its children.

§Examples

Quickly add something to every value in some interval.

use segment_tree::PointSegment;
use segment_tree::ops::Add;

use std::iter::repeat;

// make a giant tree of zeroes
let mut tree = PointSegment::build(repeat(0).take(1_000_000).collect(), Add);

// add one to every value between 200 and 500000
tree.modify(200, 500_000, 1);
assert_eq!(tree.query(100), 0);
assert_eq!(tree.query(200), 1);
assert_eq!(tree.query(500), 1);
assert_eq!(tree.query(499_999), 1);
assert_eq!(tree.query(500_000), 0);

// add five to every value between 0 and 1000
tree.modify(0, 1000, 5);
assert_eq!(tree.query(10), 5);
assert_eq!(tree.query(500), 6);
assert_eq!(tree.query(10_000), 1);
assert_eq!(tree.query(600_000), 0);

Implementations§

Source§

impl<N, O: Commutative<N> + Identity<N>> PointSegment<N, O>

Source

pub fn build(buf: Vec<N>, op: O) -> PointSegment<N, O>

Builds a tree using the given buffer. If the given buffer is less than half full, this function allocates. Uses O(len) time.

Source

pub fn build_slice(buf: &[N], op: O) -> PointSegment<N, O>
where N: Clone,

Allocate a new buffer and build the tree using the values in the slice. Uses O(len) time.

Source

pub fn build_iter<I: ExactSizeIterator<Item = N>>( iter: I, op: O, ) -> PointSegment<N, O>

Allocate a new buffer and build the tree using the values in the iterator. Uses O(len) time.

Source

pub fn query(&self, p: usize) -> N

Computes the value at p. Uses O(log(len)) time.

Source

pub fn modify(&mut self, l: usize, r: usize, delta: N)

Combine every value in the interval with delta. Uses O(log(len)) time.

Source

pub fn propogate(&mut self) -> &mut [N]

Propogate all changes to the leaves in the tree and return a mutable slice containing the leaves.

Uses O(len) time.

Source

pub fn len(&self) -> usize

Returns the number of values in the underlying array. Uses O(1) time.

Trait Implementations§

Source§

impl<N: Clone, O: Identity<N> + Commutative<N> + Clone> Clone for PointSegment<N, O>

Source§

fn clone(&self) -> PointSegment<N, O>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<N, O: Identity<N> + Commutative<N> + Default> Default for PointSegment<N, O>

Source§

fn default() -> PointSegment<N, O>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<N, O> Freeze for PointSegment<N, O>
where O: Freeze,

§

impl<N, O> RefUnwindSafe for PointSegment<N, O>

§

impl<N, O> Send for PointSegment<N, O>
where O: Send, N: Send,

§

impl<N, O> Sync for PointSegment<N, O>
where O: Sync, N: Sync,

§

impl<N, O> Unpin for PointSegment<N, O>
where O: Unpin, N: Unpin,

§

impl<N, O> UnwindSafe for PointSegment<N, O>
where O: UnwindSafe, N: 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> 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.