Struct SegmentPoint

Source
pub struct SegmentPoint<N, O>
where O: Operation<N>,
{ /* private fields */ }
Expand description

This data structure allows range queries and single element modification.

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

This tree is implemented using a segment tree. A segment tree is a binary tree where each node contains the combination of the children under the operation.

§Examples

This example solves the range minimum query problem.

use segment_tree::SegmentPoint;
use segment_tree::ops::Min;

// Let's solve the range minimum query on this array.
let mut tree = SegmentPoint::build(
    vec![10i32, 5, 6, 4, 12, 8, 9, 3, 2, 1, 5], Min
); //        0  1  2  3   4  5  6  7  8  9 10  - indices

// Find the minimum value in a few intervals. Note that the second argument is
// exclusive.
assert_eq!(tree.query(0, 2), 5);
assert_eq!(tree.query(4, 8), 3);
assert_eq!(tree.query(3, 11), 1);
assert_eq!(tree.query(0, 11), 1);

// query returns the identity if given an invalid interval
// The identity of min is the MAX.
assert_eq!(tree.query(4, 2), std::i32::MAX);

// We can change individual values in the array as well.
tree.modify(2, 0);
assert_eq!(tree.query(0, 3), 0);
assert_eq!(tree.query(3, 6), 4);

// We can view the values currently stored at any time.
assert_eq!(tree.view(), &[10, 5, 0, 4, 12, 8, 9, 3, 2, 1, 5]);

We can also use a SegmentPoint to find the sum of any interval, by changing the operator to Add.

use segment_tree::SegmentPoint;
use segment_tree::ops::Add;

let mut tree = SegmentPoint::build(
    vec![10, 5, 6, 4, 12, 8, 9, 3, 2, 1, 5], Add
); //     0  1  2  3   4  5  6  7  8  9 10  - indices

assert_eq!(tree.query(4, 8), 12 + 8 + 9 + 3);
assert_eq!(tree.query(1, 3), 5 + 6);

// we can still modify values in the tree
tree.modify(2, 4);
assert_eq!(tree.query(1, 3), 5 + 4);
assert_eq!(tree.query(4, 8), 12 + 8 + 9 + 3);

assert_eq!(tree.view(), &[10, 5, 4, 4, 12, 8, 9, 3, 2, 1, 5]);

Implementations§

Source§

impl<N, O: Operation<N>> SegmentPoint<N, O>

Source

pub fn build(buf: Vec<N>, op: O) -> SegmentPoint<N, O>
where N: Clone,

Builds a tree using the given buffer. If the given buffer is less than half full, this function allocates. This function clones every value in the input array. Uses O(len) time.

See also the function build_noalloc.

Source

pub fn modify(&mut self, p: usize, value: N) -> N

Set the value at the specified index and return the old value. Uses O(log(len)) time.

Source

pub fn query(&self, l: usize, r: usize) -> N
where O: Commutative<N> + Identity<N>,

Computes a[l] * a[l+1] * ... * a[r-1]. Uses O(log(len)) time.

If l >= r, this method returns the identity.

See query_noiden or query_noclone for a version that works with non-commutative operations.

Source

pub fn compose(&mut self, p: usize, delta: &N)
where O: Commutative<N>,

Combine the value at p with delta. Uses O(log(len)) time.

Source

pub fn compose_left(&mut self, p: usize, delta: &N)

Combine the value at p with delta, such that delta is the left argument. Uses O(log(len)) time.

Source

pub fn compose_right(&mut self, p: usize, delta: &N)

Combine the value at p with delta, such that delta is the right argument. Uses O(log(len)) time.

Source

pub fn view(&self) -> &[N]

View the values in this segment tree using a slice. Uses O(1) time.

Source

pub fn len(&self) -> usize

The number of elements stored in this segment tree. Uses O(1) time.

Source

pub fn build_noalloc(buf: Vec<N>, op: O) -> SegmentPoint<N, O>

Builds a tree using the given buffer. The buffer must be even in size. The first n values have no effect on the resulting tree, and the remaining n values contains the array to build the tree on. Uses O(len) time.

This function panics if the size of the buffer is odd.

§Example
use segment_tree::SegmentPoint;
use segment_tree::ops::Min;

// make a segment point using the other build method
let mut tree = SegmentPoint::build(
    vec![1, 2, 3, 4], Min
);
// make a segment point using the build_noalloc method:
let mut tree1 = SegmentPoint::build_noalloc(
    // the first half of the values are ignored
    vec![3282, 210, 0, 245, 1, 2, 3, 4], Min
);
assert_eq!(tree, tree1);

let mut tree2 = SegmentPoint::build_noalloc(
    // we can also try some other first few values
    vec![0, 0, 0, 0, 1, 2, 3, 4], Min
);
assert_eq!(tree1, tree2);
assert_eq!(tree, tree2);
Source§

impl<N, O: Operation<N>> SegmentPoint<N, O>

Source

pub fn query_noiden(&self, l: usize, r: usize) -> N
where N: Clone,

Like query, except it doesn’t require the operation to be commutative, nor to have any identity.

Computes a[l] * a[l+1] * ... * a[r-1].

This method panics if l >= r.

This method clones at most twice and runs in O(log(len)) time. See query_noclone for a version that doesn’t clone.

Source

pub fn query_noclone<'a>(&'a self, l: usize, r: usize) -> MaybeOwned<'a, N>

Like query_noiden, except it doesn’t clone.

Computes a[l] * a[l+1] * ... * a[r-1].

This method panics if l >= r.

Uses O(log(len)) time. See also query and query_commut.

Trait Implementations§

Source§

impl<N: Clone, O: Operation<N> + Clone> Clone for SegmentPoint<N, O>

Source§

fn clone(&self) -> SegmentPoint<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: Debug, O: Operation<N>> Debug for SegmentPoint<N, O>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<N, O: Operation<N> + Default> Default for SegmentPoint<N, O>

Source§

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

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

impl<'a, N: 'a + Hash, O: Operation<N>> Hash for SegmentPoint<N, O>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<N: PartialEq, O: Operation<N> + PartialEq> PartialEq for SegmentPoint<N, O>

Source§

fn eq(&self, other: &SegmentPoint<N, O>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

fn ne(&self, other: &SegmentPoint<N, O>) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<N: Eq, O: Operation<N> + Eq> Eq for SegmentPoint<N, O>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<N, O> UnwindSafe for SegmentPoint<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.