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>
impl<T> SparseVec<T>
Sourcepub fn new() -> Self
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());Sourcepub fn sparse() -> Self
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());Sourcepub fn sparse_from_offsets(
vals: Vec<T>,
offsets: Vec<usize>,
) -> Result<Self, SparseVecError>
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]);Sourcepub fn get_offsets(&self) -> Option<&[usize]>
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);Sourcepub fn dense() -> Self
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());Sourcepub fn len(&self) -> usize
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);Sourcepub fn push(&mut self, val: T)where
T: PartialEq,
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);Sourcepub fn compress(&mut self)
pub fn compress(&mut self)
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));Sourcepub fn size_of(&self) -> usize
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());Sourcepub fn iter(&self) -> SparseVecIter<'_, T> ⓘ
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]);Sourcepub fn is_empty(&self) -> bool
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);Sourcepub fn is_dense(&self) -> bool
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());Sourcepub fn is_sparse(&self) -> bool
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());Sourcepub fn argsort(&self) -> Vec<usize>
pub fn argsort(&self) -> Vec<usize>
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§
impl<T: Eq> Eq for SparseVec<T>
Source§impl<T: Ord> Ord for SparseVec<T>
impl<T: Ord> Ord for SparseVec<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq> PartialEq for SparseVec<T>
impl<T: PartialEq> PartialEq for SparseVec<T>
Source§impl<T: PartialOrd> PartialOrd for SparseVec<T>
impl<T: PartialOrd> PartialOrd for SparseVec<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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