Skip to main content

SparseVec

Struct SparseVec 

Source
pub struct SparseVec<T> { /* private fields */ }
Expand description

A vector that can be stored in either dense or sparse format.

Useful for compressing repeated values.

Implementations§

Source§

impl<T> SparseVec<T>

Source

pub fn new() -> Self

Creates a new empty sparse vector in dense format.

§Examples
use timsrust_utils::vec::SparseVec;
let sv: SparseVec<u32> = SparseVec::new();
assert_eq!(sv.len(), 0);
assert!(sv.is_dense());
Source

pub fn sparse() -> Self

Creates a new empty sparse vector in sparse format.

§Examples
use timsrust_utils::vec::SparseVec;
let sv: SparseVec<u32> = SparseVec::sparse();
assert_eq!(sv.len(), 0);
assert!(sv.is_sparse());
Source

pub fn sparse_from_offsets( vals: Vec<T>, offsets: Vec<usize>, ) -> Result<Self, SparseVecError>

Creates a new sparse vector from values and offsets.

The vals vector contains the unique values, and offsets specifies the start of each run. The length of offsets must be one more than the length of vals, and must be non-decreasing.

§Examples
use timsrust_utils::vec::SparseVec;
let vals = vec![1, 2];
let offsets = vec![0, 2, 5];
let sv = SparseVec::sparse_from_offsets(vals, offsets).unwrap();
assert_eq!(sv.iter().collect::<Vec<_>>(), vec![1, 1, 2, 2, 2]);
Source

pub fn get_offsets(&self) -> Option<&[usize]>

Returns the offsets array if the vector is in sparse format, or None if dense.

§Examples
use timsrust_utils::vec::SparseVec;
let vals = vec![1, 2];
let offsets = vec![0, 2, 5];
let sv = SparseVec::sparse_from_offsets(vals, offsets.clone()).unwrap();
assert_eq!(sv.get_offsets(), Some(&offsets[..]));

let dv: SparseVec<u32> = SparseVec::dense();
assert_eq!(dv.get_offsets(), None);
Source

pub fn dense() -> Self

Creates a new empty sparse vector in dense format.

§Examples
use timsrust_utils::vec::SparseVec;
let sv: SparseVec<u32> = SparseVec::dense();
assert_eq!(sv.len(), 0);
assert!(sv.is_dense());
Source

pub fn len(&self) -> usize

Returns the total number of elements in the vector.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::dense();
sv.push(1);
sv.push(2);
assert_eq!(sv.len(), 2);
Source

pub fn push(&mut self, val: T)
where T: PartialEq,

Appends an element to the vector.

In sparse format, consecutive equal values are compressed.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::sparse();
sv.push(1);
sv.push(1);
sv.push(2);
assert_eq!(sv.len(), 3);
Source

pub fn compress(&mut self)
where T: PartialEq + Copy,

Compresses the vector from dense to sparse format if it saves memory.

Only compresses if the sparse representation uses less memory than the dense one.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::new();
for i in 0..100 {
    sv.push(i / 25);
}
assert_eq!(sv.len(), 100);
assert!(sv.is_dense());
let mut sv2 = sv.clone();
sv2.compress();
assert!(sv2.is_sparse());
assert_eq!(Vec::from(sv), Vec::from(sv2));
Source

pub fn size_of(&self) -> usize

Returns the memory usage in bytes.

§Examples
use timsrust_utils::vec::SparseVec;
let mut dv: SparseVec<u32> = SparseVec::dense();
for i in 0..100 {
    dv.push(i / 25);
}
let mut sv = dv.clone();
sv.compress();
assert!(sv.size_of() < dv.size_of());
Source

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

Returns an iterator over all elements in the vector.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::dense();
sv.push(1);
sv.push(2);
let v: Vec<_> = sv.iter().collect();
assert_eq!(v, vec![1, 2]);
Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

§Examples
use timsrust_utils::vec::SparseVec;
let sv: SparseVec<u32> = SparseVec::sparse();
assert_eq!(sv.len(), 0);
Source

pub fn is_dense(&self) -> bool

Returns true if the vector is in dense format.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv: SparseVec<u32> = SparseVec::dense();
assert!(sv.is_dense());
Source

pub fn is_sparse(&self) -> bool

Returns true if the vector is in sparse format.

§Examples
use timsrust_utils::vec::SparseVec;
let sv: SparseVec<u32> = SparseVec::sparse();
assert!(sv.is_sparse());
Source

pub fn argsort(&self) -> Vec<usize>
where T: Ord + Copy,

Returns the indices that would sort the vector.

The returned vector contains the indices of the elements in sorted order.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::dense();
sv.push(3);
sv.push(1);
sv.push(2);
let sorted_indices = sv.argsort();
assert_eq!(sorted_indices, vec![1, 2, 0]);

let mut sv = SparseVec::sparse();
sv.push(2);
sv.push(2);
sv.push(1);
sv.push(1);
sv.push(1);
let sorted_indices = sv.argsort();
assert_eq!(sorted_indices, vec![2, 3, 0, 1, 2]);

Trait Implementations§

Source§

impl<T: Clone> Clone for SparseVec<T>

Source§

fn clone(&self) -> SparseVec<T>

Returns a duplicate of the value. Read more
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 SparseVec<T>

Source§

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

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

impl<T: Default> Default for SparseVec<T>

Source§

fn default() -> SparseVec<T>

Returns the “default value” for a type. Read more
Source§

impl<T: Eq> Eq for SparseVec<T>

Source§

impl<T: Copy> From<&SparseVec<T>> for Vec<T>

Source§

fn from(sparse: &SparseVec<T>) -> Self

Converts a SparseVec into a regular Vec, expanding any compressed values.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::sparse();
sv.push(1);
sv.push(1);
sv.push(2);
let v: Vec<_> = Vec::from(sv);
assert_eq!(v, vec![1, 1, 2]);
Source§

impl<T: Copy> From<SparseVec<T>> for Vec<T>

Source§

fn from(sparse: SparseVec<T>) -> Self

Converts a SparseVec into a regular Vec, expanding any compressed values.

§Examples
use timsrust_utils::vec::SparseVec;
let mut sv = SparseVec::sparse();
sv.push(1);
sv.push(1);
sv.push(2);
let v: Vec<_> = Vec::from(sv);
assert_eq!(v, vec![1, 1, 2]);
Source§

impl<T: Copy + PartialEq> From<Vec<T>> for SparseVec<T>

Source§

fn from(vec: Vec<T>) -> Self

Converts a regular Vec into a compressed SparseVec.

§Examples
use timsrust_utils::vec::SparseVec;
let mut v = Vec::new();
for i in 0..100 {
    v.push(i / 25);
}
let sv: SparseVec<_> = v.clone().into();
assert_eq!(sv.iter().collect::<Vec<_>>(), v);
Source§

impl<T: Hash> Hash for SparseVec<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Ord> Ord for SparseVec<T>

Source§

fn cmp(&self, other: &SparseVec<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for SparseVec<T>

Source§

fn eq(&self, other: &SparseVec<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for SparseVec<T>

Source§

fn partial_cmp(&self, other: &SparseVec<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> StructuralPartialEq for SparseVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for SparseVec<T>

§

impl<T> RefUnwindSafe for SparseVec<T>
where T: RefUnwindSafe,

§

impl<T> Send for SparseVec<T>
where T: Send,

§

impl<T> Sync for SparseVec<T>
where T: Sync,

§

impl<T> Unpin for SparseVec<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for SparseVec<T>

§

impl<T> UnwindSafe for SparseVec<T>
where T: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.