pub struct PrefixPoint<N, O>where
    O: Commutative<N>,
{ /* private fields */ }
Expand description

This data structure allows prefix queries and single element modification.

This tree allocates n * sizeof(N) bytes of memory, and can be resized.

This data structure is implemented using a Fenwick tree, which is also known as a binary indexed tree.

The similar crate prefix-sum might also be of interest.

Examples

Showcase of functionality:

use segment_tree::ops::Add;
use segment_tree::PrefixPoint;

let buf = vec![10, 5, 30, 40];

let mut pp = PrefixPoint::build(buf, Add);

// If we query, we get the sum up until the specified value.
assert_eq!(pp.query(0), 10);
assert_eq!(pp.query(1), 15);
assert_eq!(pp.query(2), 45);
assert_eq!(pp.query(3), 85);

// Add five to the second value.
pp.modify(1, 5);
assert_eq!(pp.query(0), 10);
assert_eq!(pp.query(1), 20);
assert_eq!(pp.query(2), 50);
assert_eq!(pp.query(3), 90);

// Multiply every value with 2.
pp.map(|v| *v *= 2);
assert_eq!(pp.query(0), 20);
assert_eq!(pp.query(1), 40);
assert_eq!(pp.query(2), 100);
assert_eq!(pp.query(3), 180);

// Divide with two to undo.
pp.map(|v| *v /= 2);
// Add some more values.
pp.extend(vec![0, 10].into_iter());
assert_eq!(pp.query(0), 10);
assert_eq!(pp.query(1), 20);
assert_eq!(pp.query(2), 50);
assert_eq!(pp.query(3), 90);
assert_eq!(pp.query(4), 90);
assert_eq!(pp.query(5), 100);

// Get the underlying values.
assert_eq!(pp.get(0), 10);
assert_eq!(pp.get(1), 10);
assert_eq!(pp.get(2), 30);
assert_eq!(pp.get(3), 40);
assert_eq!(pp.get(4), 0);
assert_eq!(pp.get(5), 10);

// Remove the last value
pp.truncate(5);
assert_eq!(pp.get(0), 10);
assert_eq!(pp.get(1), 10);
assert_eq!(pp.get(2), 30);
assert_eq!(pp.get(3), 40);
assert_eq!(pp.get(4), 0);

// Get back the original values.
assert_eq!(pp.unwrap(), vec![10, 10, 30, 40, 0]);

You can also use other operators:

use segment_tree::ops::Mul;
use segment_tree::PrefixPoint;

let buf = vec![10, 5, 30, 40];

let mut pp = PrefixPoint::build(buf, Mul);

assert_eq!(pp.query(0), 10);
assert_eq!(pp.query(1), 50);
assert_eq!(pp.query(2), 1500);
assert_eq!(pp.query(3), 60000);

Implementations§

Creates a PrefixPoint containing the given values. Uses O(len) time.

Returns the number of values in this tree. Uses O(1) time.

Computes a[0] * a[1] * ... * a[i]. Note that i is inclusive. Uses O(log(i)) time.

Computes a[0] * a[1] * ... * a[i]. Note that i is inclusive. Uses O(log(i)) time.

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

Truncates the PrefixPoint to the given size. If size >= len, this method does nothing. Uses O(1) time.

Calls shrink_to_fit on the interval vector.

Replace every value in the type with f(value). This function assumes that combine(f(a), f(b)) = f(combine(a, b)).

The function is applied len times.

Returns the value at i. Uses O(log(i)) time. Store your own copy of the array if you want constant time.

Change the value at the index to be the specified value. Uses O(log(i)) time.

Compute the underlying array of values. Uses O(len) time.

Compute the underlying array of values. Uses O(len) 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

Adds the given values to the PrefixPoint, increasing its size. Uses O(len) time.

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. 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.