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.
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>.
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.