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§

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

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

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

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

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

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

Uses O(len) time.

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

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.