Struct prefix_sum::PrefixSum

source ·
pub struct PrefixSum<T: Summable> { /* private fields */ }
Expand description

Data type allowing O(1) interval modifications to an array.

Example

use prefix_sum::PrefixSum;

let mut sum = PrefixSum::new(6);
sum[2..=4] += 2;
sum[1..3] += 3;

sum[ ..3] -= 1;
sum[4.. ] += 10;

assert_eq!(vec![-1, 2, 4, 2, 12, 10], sum.build());

Implementations§

Builds a new PrefixSum filled with zeroes.

This runs in linear time in the length.

Returns a PrefixSum, such that calling build on the result would return the input.

This allocates if vec.len() == vec.capacity(). This runs in linear time in the length.

Examples
use prefix_sum::PrefixSum;

let sum = PrefixSum::from_vec(vec![1, 2, 3, 4]);
assert_eq!(sum.build(), vec![1, 2, 3, 4]);

When resized, new items will be zero.

use prefix_sum::PrefixSum;

let mut sum = PrefixSum::from_vec(vec![1, 2, 3, 4]);
sum.resize(5);
assert_eq!(sum.build(), vec![1, 2, 3, 4, 0]);

Returns the number of items in this prefix sum.

This runs in constant time.

Resize the prefix sum. Any changes done using intervals with no upper bound will affect the newly created values.

If the size is decreased, this is constant time. If the size is increased, this runs in amortized linear time in the number of items added.

Example
use prefix_sum::PrefixSum;

let mut sum = PrefixSum::new(3);
sum[ ..] += 2;
sum[1..] += 3;

sum[2] += 1;
assert_eq!(vec![2, 5, 6], sum.clone().build());

sum.resize(4);

assert_eq!(vec![2, 5, 6, 5], sum.build());

Build the vector containing the computed sums. This is linear time in the length.

Create an iterator through the sums in this PrefixSum.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
Which kind of iterator are we turning this into?
The type of the elements being iterated over.
Creates an iterator from a value. Read more
Which kind of iterator are we turning this into?
The type of the elements being iterated over.
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. 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.