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 get_default_value(&self) -> &T
pub fn get_default_value(&self) -> &T
The value reported for every position with no explicit element.
There is deliberately no &mut counterpart; use
set_default_value, which prunes.
Sourcepub fn get_equality(&self) -> Option<&Equality<T>>
pub fn get_equality(&self) -> Option<&Equality<T>>
The predicate replacing PartialEq, or None when PartialEq decides.
Sourcepub fn get_element_amount(&self) -> usize
pub fn get_element_amount(&self) -> usize
The number of explicitly stored elements.
Sourcepub fn get_significant_dimension(&self) -> i64
pub fn get_significant_dimension(&self) -> i64
The distance between the leftmost and rightmost elements, 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) -> bool
pub fn reset_vector(&mut self) -> bool
Resets every explicit element, keeping the default value, and returns whether there was any.
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 element from the left; the ordinal numbers the elements, not the positions.
§Panics
Panics when fewer than n + 1 elements 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 element from the right.
§Panics
Panics when fewer than n + 1 elements are stored.
Source§impl<T: Default> SparseVector<T>
impl<T: Default> SparseVector<T>
Sourcepub fn default_new() -> Self
pub fn default_new() -> Self
Creates a vector whose empty positions read back as T::default(), the default left
out. For f64 that is 0.0.
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 elements, 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 elements, 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 element from the left; the ordinal numbers the elements, not the positions.
§Panics
Panics when fewer than n + 1 elements 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 element from the left.
§Panics
Panics when fewer than n + 1 elements are stored.
Sourcepub fn inverted_element(&self, n: usize) -> Element<T>
pub fn inverted_element(&self, n: usize) -> Element<T>
The (n+1)-th element from the right, inverted_element(0) being the rightmost.
§Panics
Panics when fewer than n + 1 elements 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 element from the right.
§Panics
Panics when fewer than n + 1 elements 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 element, 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 element; 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 element at index, returning &mut self so calls chain;
writing the default removes instead.
Sourcepub fn set_default_value(&mut self, next: T) -> bool
pub fn set_default_value(&mut self, next: T) -> bool
Replaces the default value, immediately dropping every element equal to it and returning whether anything was dropped. Pruning is one-way: dropped positions never come back.
Sourcepub fn set_equality(&mut self, equality: Option<Equality<T>>) -> bool
pub fn set_equality(&mut self, equality: Option<Equality<T>>) -> bool
Replaces the predicate, immediately dropping every element it calls equal to the default
and returning whether anything was dropped. None restores PartialEq.
Sourcepub fn reset_value(&mut self, index: i64) -> bool
pub fn reset_value(&mut self, index: i64) -> bool
Resets the element at index, returning whether there was one. It then reads as the
default.
Sourcepub fn is_equal_to(&self, other: &Self) -> bool
pub fn is_equal_to(&self, other: &Self) -> bool
Whether other holds the same values as self, every comparison made by this vector’s
predicate: the two default values first, then each index either vector stores an element
at, a position holding nothing contributing its own vector’s default.
Never panics. Two NaN defaults compare unequal, so a vector holding one is not equal to
itself. The three properties of equality hold only as far as the predicate does — an
asymmetric or non-reflexive predicate gives an asymmetric or non-reflexive answer, the
library never correcting it.
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.
Source§impl<T: Clone + PartialEq> SparseVector<T>
impl<T: Clone + PartialEq> SparseVector<T>
Sourcepub fn differences(&self, other: &Self) -> Vec<Element<T>>
pub fn differences(&self, other: &Self) -> Vec<Element<T>>
The elements of self that differ from other, descending by index, each carrying this
vector’s value at that index — the default value itself wherever nothing is stored there.
Not a symmetric difference, and not guaranteed to be empty for a.differences(a): a stored
NaN differs from itself, since NaN != NaN.
§Panics
Panics when the two default values do not compare equal under this vector’s predicate, so there is no sense in which the vectors can be compared.