PrefixSumVec

Struct PrefixSumVec 

Source
pub struct PrefixSumVec<T, Idx = usize> { /* private fields */ }
Expand description

A data structure for space-efficient storage of repeating sequences.

Much like Vec, this is a growable array-like type. This data structure is capaable of storing a contiguous sequence of repeating values only once, thus “compressing” the representation of the sequence. Conversely index lookups are O(log n), rather than O(1) as provided by Vec.

Note, however, that this data structure can become less space-efficient than a plain Vec if the data is incompressible.

§Examples

let mut prefix_sum_vec = prefix_sum_vec::PrefixSumVec::new();

prefix_sum_vec.try_push_many(12, "strings").expect("could not push");
assert_eq!(prefix_sum_vec.max_index(), Some(&11));

assert_eq!(prefix_sum_vec[0], "strings");
assert_eq!(prefix_sum_vec[11], "strings");

Implementations§

Source§

impl<T, Idx> PrefixSumVec<T, Idx>

Source

pub fn new() -> Self

Create a new, empty PrefixSumVec.

Does not allocate.

Source

pub fn clear(&mut self)

Clears the data structure, removing all contained values.

This method has no effect on the allocated capacity. Due to the compressed representation, a repeating value in a contiguous sequence is dropped only once.

Source

pub fn max_index(&self) -> Option<&Idx>

Get the current maximum index that can be used for indexing this data structure.

Will return None while this data structure contains no elements. In practice this is the number of elements contained within data structure minus one.

Complexity: O(1)

Source§

impl<T, Idx: Ord> PrefixSumVec<T, Idx>

Source

pub fn get(&self, index: &Idx) -> Option<&T>

Find the value by an index.

If the value at this index is not present in the data structure, None is returned.

Complexity: O(log n)

Source§

impl<T, Idx: Index> PrefixSumVec<T, Idx>

Source

pub fn try_push_many( &mut self, count: Idx, value: T, ) -> Result<(), TryPushError>

Append count copies of a value to the back of the collection.

This method will not inspect the values currently stored in the data structure in search for further compression opportunities. Instead the provided value will be stored compressed as-if a sequence of count repeating values were provided.

Complexity: O(1) amortized.

Source§

impl<T: PartialEq, Idx: Index> PrefixSumVec<T, Idx>

Source

pub fn try_push_more( &mut self, count: Idx, value: T, ) -> Result<(), TryPushError>

Append count copies of a value to the back of the collection, attempting compression.

This method will not inspect the values currently stored in the data structure in search for further compression opportunities. Instead the provided value will be stored compressed as-if a sequence of count repeating values were provided.

Complexity: O(1) amortized.

Trait Implementations§

Source§

impl<T: Debug, Idx: Debug> Debug for PrefixSumVec<T, Idx>

Source§

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

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

impl<K, Idx: Ord> Index<&Idx> for PrefixSumVec<K, Idx>

Source§

type Output = K

The returned type after indexing.
Source§

fn index(&self, index: &Idx) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<K, Idx: Ord> Index<Idx> for PrefixSumVec<K, Idx>

Source§

type Output = K

The returned type after indexing.
Source§

fn index(&self, index: Idx) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, T, Idx> IntoIterator for &'a PrefixSumVec<T, Idx>

Source§

type Item = (&'a Idx, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = CompressedIter<'a, T, Idx>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T, Idx> Freeze for PrefixSumVec<T, Idx>

§

impl<T, Idx> RefUnwindSafe for PrefixSumVec<T, Idx>

§

impl<T, Idx> Send for PrefixSumVec<T, Idx>
where Idx: Send, T: Send,

§

impl<T, Idx> Sync for PrefixSumVec<T, Idx>
where Idx: Sync, T: Sync,

§

impl<T, Idx> Unpin for PrefixSumVec<T, Idx>
where Idx: Unpin, T: Unpin,

§

impl<T, Idx> UnwindSafe for PrefixSumVec<T, Idx>
where Idx: UnwindSafe, T: 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> 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, 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.