pub struct SparseVector<T = f64> { /* private fields */ }Expand description
A sparse vector: a mapping from integer index to arbitrary value that stores only the positions differing from the default value. Entries are ordered largest index leftmost.
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 get_default_value(&self) -> &T
pub fn get_default_value(&self) -> &T
The value reported for every position with no explicit entry.
There is deliberately no &mut counterpart; use
set_default_value, which prunes.
Sourcepub fn get_element_amount(&self) -> usize
pub fn get_element_amount(&self) -> usize
The number of explicitly stored entries.
Sourcepub fn get_significant_dimension(&self) -> i64
pub fn get_significant_dimension(&self) -> i64
The distance between the leftmost and rightmost entries, both included; zero when empty.
§Panics
Panics when the span leaves the i64 range.
Sourcepub fn get_plus_dimension(&self) -> i64
pub fn get_plus_dimension(&self) -> i64
How far the vector reaches above zero: the leftmost index itself, or zero.
Sourcepub fn get_minus_dimension(&self) -> i64
pub fn get_minus_dimension(&self) -> i64
How far the vector reaches below zero: the rightmost index negated, or zero.
§Panics
Panics when the negation leaves the i64 range.
Sourcepub fn reset_vector(&mut self)
pub fn reset_vector(&mut self)
Resets every explicit entry, keeping the default value.
Sourcepub fn inverted_indexes(&self) -> Vec<i64>
pub fn inverted_indexes(&self) -> Vec<i64>
The stored indices, ascending.
Sourcepub fn element_index(&self, n: usize) -> i64
pub fn element_index(&self, n: usize) -> i64
The index of the (n+1)-th entry from the left; the ordinal numbers the entries, not the positions.
§Panics
Panics when fewer than n + 1 entries are stored.
Sourcepub fn inverted_element_index(&self, n: usize) -> i64
pub fn inverted_element_index(&self, n: usize) -> i64
The index of the (n+1)-th entry from the right.
§Panics
Panics when fewer than n + 1 entries are stored.
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; 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, so it returns T and not Option<T>.
Sourcepub fn elements(&self) -> Vec<Element<T>>
pub fn elements(&self) -> Vec<Element<T>>
The explicit entries, descending by index: the serialization shape described on
Element.
Sourcepub fn inverted_elements(&self) -> Vec<Element<T>>
pub fn inverted_elements(&self) -> Vec<Element<T>>
The explicit entries, ascending by index.
Sourcepub fn inverted_values(&self) -> Vec<T>
pub fn inverted_values(&self) -> Vec<T>
The stored values, ascending by index.
Sourcepub fn element(&self, n: usize) -> Element<T>
pub fn element(&self, n: usize) -> Element<T>
The (n+1)-th entry from the left; the ordinal numbers the entries, not the positions.
§Panics
Panics when fewer than n + 1 entries are stored.
Sourcepub fn element_value(&self, n: usize) -> T
pub fn element_value(&self, n: usize) -> T
The value of the (n+1)-th entry from the left.
§Panics
Panics when fewer than n + 1 entries are stored.
Sourcepub fn inverted_element(&self, n: usize) -> Element<T>
pub fn inverted_element(&self, n: usize) -> Element<T>
The (n+1)-th entry from the right, inverted_element(0) being the rightmost.
§Panics
Panics when fewer than n + 1 entries are stored.
Sourcepub fn inverted_element_value(&self, n: usize) -> T
pub fn inverted_element_value(&self, n: usize) -> T
The value of the (n+1)-th entry 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
The value n positions right of the leftmost entry, empty positions counting; a
negative n walks left of it, into the default value.
§Panics
Panics when nothing is stored, or the position leaves the i64 range.
Sourcepub fn right_significant_value(&self, n: i64) -> T
pub fn right_significant_value(&self, n: i64) -> T
The value n positions left of the rightmost entry; a negative n walks right of it.
§Panics
Panics when nothing is stored, or 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 the entry at index, returning &mut self so calls chain;
writing the default removes instead.
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: dropped positions never come back.
Sourcepub fn reset_value(&mut self, index: i64) -> Option<T>
pub fn reset_value(&mut self, index: i64) -> Option<T>
Resets the entry at index and returns it if there was one. Returned as an Option
rather than a bool, so .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 from any iterator of elements, dropping those equal to the default and keeping the last of a repeated index.