pub struct SparseVector<T = f64> { /* private fields */ }Expand description
A sparse vector: a mapping from integer index to arbitrary value, in which only the positions that differ from a default value are stored.
Because only non-default positions are kept, a vector occupies O(k) memory for k stored entries no matter how far apart the indices are, and whether they are large or negative. Indices need not be contiguous, and an index holding no explicit entry simply reports the default value.
Implementations§
Source§impl<T> SparseVector<T>
impl<T> SparseVector<T>
Sourcepub fn with_default(default: T) -> Self
pub fn with_default(default: T) -> Self
Creates a vector whose empty positions read back as default.
Sourcepub fn default_value(&self) -> &T
pub fn default_value(&self) -> &T
The value reported for every position that has no explicit entry.
There is deliberately no &mut counterpart: mutating the default has to
prune, which set_default_value does.
Sourcepub fn contains_key(&self, index: i64) -> bool
pub fn contains_key(&self, index: i64) -> bool
Whether index has an explicitly stored entry.
Sourcepub fn element_index(&self, n: usize) -> i64
pub fn element_index(&self, n: usize) -> i64
Sourcepub fn last_element_index(&self, n: usize) -> i64
pub fn last_element_index(&self, n: usize) -> i64
Source§impl SparseVector<f64>
impl SparseVector<f64>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a vector whose empty positions read back as 0.0.
This exists only for f64, where the 0 the shared semantics name is a
value of the right type; for any other T use
with_default.
Source§impl<T: Clone> SparseVector<T>
impl<T: Clone> SparseVector<T>
Sourcepub fn get(&self, index: i64) -> T
pub fn get(&self, index: i64) -> T
The value at index, or the default value when nothing is stored there.
Reading is total: every i64, negative, huge or far outside anything ever
written, returns a value rather than failing, so this returns T and not
Option<T>.
Sourcepub fn elements(&self) -> Vec<Element<T>>
pub fn elements(&self) -> Vec<Element<T>>
The explicit entries, in ascending index order.
This is the serialization shape described on Element.
Sourcepub fn element_value(&self, n: usize) -> T
pub fn element_value(&self, n: usize) -> T
The value of the entry at 0-based ordinal n, counting from the left.
§Panics
Panics when fewer than n + 1 entries are stored.
Sourcepub fn last_element(&self, n: usize) -> Element<T>
pub fn last_element(&self, n: usize) -> Element<T>
Sourcepub fn last_element_value(&self, n: usize) -> T
pub fn last_element_value(&self, n: usize) -> T
The value of the entry at 0-based ordinal n, counting from the right.
§Panics
Panics when fewer than n + 1 entries are stored.
Sourcepub fn left_significant_value(&self, n: i64) -> T
pub fn left_significant_value(&self, n: i64) -> T
Treating the first stored entry as the first significant digit, the value
n positions to its right.
Positions without an entry in between count, the way the zeros inside a
number count towards its sign. n may be negative, which walks left of
that first entry instead; the result is then the default value.
§Panics
Panics when nothing is stored, so there is no position to measure from, or
when the position leaves the i64 range.
Sourcepub fn right_significant_value(&self, n: i64) -> T
pub fn right_significant_value(&self, n: i64) -> T
Treating the last stored entry as the first significant digit counting
from the right, the value n positions to its left.
n may be negative, which walks right of that last entry instead.
§Panics
Panics when nothing is stored, so there is no position to measure from, or
when the position leaves the i64 range.
Source§impl<T: PartialEq> SparseVector<T>
impl<T: PartialEq> SparseVector<T>
Sourcepub fn set(&mut self, index: i64, value: T) -> &mut Self
pub fn set(&mut self, index: i64, value: T) -> &mut Self
Inserts or updates value at index, returning &mut self so calls chain.
Writing a value equal to the default removes whatever was there instead of storing it: that is what keeps the structure sparse.
Sourcepub fn set_default_value(&mut self, next: T)
pub fn set_default_value(&mut self, next: T)
Replaces the default value, immediately dropping every entry equal to it.
Pruning is one-way: the previous default is not remembered, so positions dropped here never come back.
Sourcepub fn remove(&mut self, index: i64) -> Option<T>
pub fn remove(&mut self, index: i64) -> Option<T>
Removes the explicit entry at index and returns it, if there was one.
The position subsequently reads back as the default value. Returned as an
Option rather than a bool so the removed value is available;
.is_some() recovers the boolean.
Sourcepub fn from_elements<I>(elements: I, default: T) -> Selfwhere
I: IntoIterator<Item = Element<T>>,
pub fn from_elements<I>(elements: I, default: T) -> Selfwhere
I: IntoIterator<Item = Element<T>>,
Builds a vector from a sequence of elements.
Entries equal to the default are dropped, and for a repeated index the
last element wins, both because this goes through set.