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 with_default(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 entry.

There is deliberately no &mut counterpart; use set_default_value, which prunes.

Source

pub fn get_element_amount(&self) -> usize

The number of explicitly stored entries.

Source

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.

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)

Resets every explicit entry, keeping the default value.

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 entry from the left; the ordinal numbers the entries, not the positions.

§Panics

Panics when fewer than n + 1 entries are stored.

Source

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

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

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

Source§

impl SparseVector<f64>

Source

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>

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 entries, descending by index: the serialization shape described on Element.

Source

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

The explicit entries, 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 entry from the left; the ordinal numbers the entries, not the positions.

§Panics

Panics when fewer than n + 1 entries are stored.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

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.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> Self

An independent copy, default value 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<T: Default> Default for SparseVector<T>

Source§

fn default() -> Self

Returns the “default value” for a type. 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,

§

impl<T> RefUnwindSafe for SparseVector<T>

§

impl<T> Send for SparseVector<T>
where BTreeMap<i64, T>: Send, T: Send,

§

impl<T> Sync for SparseVector<T>
where BTreeMap<i64, T>: Sync, T: Sync,

§

impl<T> Unpin for SparseVector<T>
where BTreeMap<i64, T>: Unpin, T: 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.