Skip to main content

SparseVector

Struct SparseVector 

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

Source

pub fn new(default: T) -> Self

Creates a vector whose empty positions read back as default.

Source

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.

Source

pub fn get_equality(&self) -> Option<&Equality<T>>

The predicate replacing PartialEq, or None when PartialEq decides.

Source

pub fn get_element_amount(&self) -> usize

The number of explicitly stored elements.

Source

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.

Source

pub fn get_plus_dimension(&self) -> i64

How far the vector reaches above zero: the leftmost index itself, or zero.

Source

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.

Source

pub fn reset_vector(&mut self) -> bool

Resets every explicit element, keeping the default value, and returns whether there was any.

Source

pub fn indexes(&self) -> Vec<i64>

The stored indices, descending.

Source

pub fn inverted_indexes(&self) -> Vec<i64>

The stored indices, ascending.

Source

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.

Source

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

pub fn iter(&self) -> Iter<'_, T> ⓘ

Iterates the elements descending by index, borrowing them, without allocating.

Source§

impl<T: Default> SparseVector<T>

Source

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>

Source

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

Source

pub fn elements(&self) -> Vec<Element<T>>

The explicit elements, descending by index: the serialization shape described on Element.

Source

pub fn inverted_elements(&self) -> Vec<Element<T>>

The explicit elements, ascending by index.

Source

pub fn values(&self) -> Vec<T>

The stored values, descending by index.

Source

pub fn inverted_values(&self) -> Vec<T>

The stored values, ascending by index.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn from_elements<I>(elements: I, default: T) -> Self
where 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>

Source

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.

Trait Implementations§

Source§

impl<T: Clone> Clone for SparseVector<T>

Source§

fn clone(&self) -> Self

An independent copy, default value and equality predicate included.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for SparseVector<T>

Source§

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

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

impl<'a, T> IntoIterator for &'a SparseVector<T>

Source§

type Item = Element<&'a T>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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> Freeze for SparseVector<T>
where BTreeMap<i64, T>: Freeze, T: Freeze, Option<Arc<dyn Fn(&T, &T) -> bool + RefUnwindSafe + Sync + Send>>: Freeze,

§

impl<T> RefUnwindSafe for SparseVector<T>

§

impl<T> Send for SparseVector<T>
where BTreeMap<i64, T>: Send, T: Send, Option<Arc<dyn Fn(&T, &T) -> bool + RefUnwindSafe + Sync + Send>>: Send,

§

impl<T> Sync for SparseVector<T>
where BTreeMap<i64, T>: Sync, T: Sync, Option<Arc<dyn Fn(&T, &T) -> bool + RefUnwindSafe + Sync + Send>>: Sync,

§

impl<T> Unpin for SparseVector<T>
where BTreeMap<i64, T>: Unpin, T: Unpin, Option<Arc<dyn Fn(&T, &T) -> bool + RefUnwindSafe + Sync + Send>>: Unpin,

§

impl<T> UnsafeUnpin for SparseVector<T>

§

impl<T> UnwindSafe for SparseVector<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.