pub struct Sparse<S, T = RangeTo<usize>, I = Vec<usize>> {
pub selection: Select<T, I>,
pub source: S,
}Expand description
A Sparse data set S where the sparsity pattern is given by I as select
indices into a larger range.
For example we can represent a sparse vector by assigning values to a selection of indices:
use flatk::{Sparse, Get, View};
let values = vec![1.0, 2.0, 3.0, 4.0];
let sparse_vector = Sparse::from_dim(vec![0,5,10,100], 1000, values);
let sparse_vector_view = sparse_vector.view();
assert_eq!(sparse_vector_view.at(0), (0, &1.0));
assert_eq!(sparse_vector_view.at(1), (5, &2.0));
assert_eq!(sparse_vector_view.at(2), (10, &3.0));
assert_eq!(sparse_vector_view.at(3), (100, &4.0));
assert_eq!(sparse_vector_view.selection.target, ..1000);In this scenario, the target set is just the range 0..1000, however in general this can be any
data set, which makes Sparse an implementation of a one-to-one mapping or a directed graph
with disjoint source and target node sets.
Fields§
§selection: Select<T, I>§source: SImplementations§
Source§impl<'a, S, T, I> Sparse<S, T, I>where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoParallelIterator,
<<S as View<'a>>::Type as IntoParallelIterator>::Iter: IndexedParallelIterator,
T: Set + Get<'a, usize> + View<'a> + Sync,
T::Output: Send,
I: AsIndexSlice + Sync,
impl<'a, S, T, I> Sparse<S, T, I>where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoParallelIterator,
<<S as View<'a>>::Type as IntoParallelIterator>::Iter: IndexedParallelIterator,
T: Set + Get<'a, usize> + View<'a> + Sync,
T::Output: Send,
I: AsIndexSlice + Sync,
Source§impl<'a, S, T, I> Sparse<S, T, I>where
S: ViewMut<'a>,
<S as ViewMut<'a>>::Type: Set + IntoParallelIterator,
<<S as ViewMut<'a>>::Type as IntoParallelIterator>::Iter: IndexedParallelIterator,
I: AsMut<[usize]>,
impl<'a, S, T, I> Sparse<S, T, I>where
S: ViewMut<'a>,
<S as ViewMut<'a>>::Type: Set + IntoParallelIterator,
<<S as ViewMut<'a>>::Type as IntoParallelIterator>::Iter: IndexedParallelIterator,
I: AsMut<[usize]>,
Sourcepub fn par_iter_mut(
&'a mut self,
) -> Zip<IterMut<'a, usize>, <<S as ViewMut<'a>>::Type as IntoParallelIterator>::Iter>
pub fn par_iter_mut( &'a mut self, ) -> Zip<IterMut<'a, usize>, <<S as ViewMut<'a>>::Type as IntoParallelIterator>::Iter>
Produce a parallel iterator over elements (borrowed slices) of a Sparse.
Source§impl<S, I> Sparse<S, RangeTo<usize>, I>where
S: Set,
I: AsIndexSlice,
impl<S, I> Sparse<S, RangeTo<usize>, I>where
S: Set,
I: AsIndexSlice,
Sourcepub fn from_dim(indices: I, dim: usize, values: S) -> Self
pub fn from_dim(indices: I, dim: usize, values: S) -> Self
Create a sparse collection from the given set of indices, a
dimension and a set of values.
The corresponding sparse collection will represent a collection
of size dim which stores only the given values at the specified
indices. Note that dim may be smaller than values.len(), in
which case a position in the sparse data structure may contain multiple
values.
§Example
use flatk::*;
let v = vec![1,2,3,4,5,6];
let sparse = Sparse::from_dim(vec![0,2,0,2,0,3], 4, v.as_slice());
// The iterator traverses only non-vacant elements.
let mut iter = sparse.iter(); // Returns (position, source, target) triplets
assert_eq!(Some((0, &1, 0)), iter.next());
assert_eq!(Some((2, &2, 2)), iter.next());
assert_eq!(Some((0, &3, 0)), iter.next());
assert_eq!(Some((2, &4, 2)), iter.next());
assert_eq!(Some((0, &5, 0)), iter.next());
assert_eq!(Some((3, &6, 3)), iter.next());
assert_eq!(None, iter.next());Source§impl<S: Set, T, I> Sparse<S, T, I>
impl<S: Set, T, I> Sparse<S, T, I>
Sourcepub fn extend_pruned<S2, T2, I2, B>(
&mut self,
other: Sparse<S2, T2, I2>,
combine: impl FnMut(usize, &mut B::Owned, B),
keep: impl FnMut(usize, &B::Owned) -> bool,
map: impl FnMut(usize, usize),
)
pub fn extend_pruned<S2, T2, I2, B>( &mut self, other: Sparse<S2, T2, I2>, combine: impl FnMut(usize, &mut B::Owned, B), keep: impl FnMut(usize, &B::Owned) -> bool, map: impl FnMut(usize, usize), )
Extend the current sparse collection with a pruned and compressed version of the given
sparse collection, other.
Each element is in the original sparse collection is guaranteed to be
passed to exactly one of keep or combine.
The keep function will get the combined value of all consecutively
overlapping elements, and the source index of the first element in the
consecutive group.
The map function allows the caller to map between original other
sparse collection and the newly populated collection self. The first
parameter passed into map is the index in the output sparse array where
each element is being considered for insertion. The second parameter is
the position of each element in the output sparse structure. Note that map
is not called on pruned elements.
§Example
use flatk::*;
let v = vec![1,2,3,4,5,6];
// Create a sparse vector with overlapping elements:
// [0] -> 1 [0] -> 5
// [1] -> [1] ->
// [2] -> 2 + 3 + 4 [2] ->
// [3] -> [3] -> 6
let sparse = Sparse::from_dim(vec![0,2,2,2,0,3], 4, v.as_slice());
// Create an empty sparse vector.
let mut compressed = Sparse::from_dim(Vec::new(), 4, Vec::new());
// Transfer the elements from `sparse` into `compressed` while resolving the consecutive overlaps:
compressed.extend_pruned(sparse, |_pos, a, b| *a += *b, |_pos, _val| true, |_src_idx, _dst_idx, | {});
let mut iter = compressed.iter(); // Returns (position, source, target) triplets.
assert_eq!(Some((0, &1, 0)), iter.next());
assert_eq!(Some((2, &9, 2)), iter.next());
assert_eq!(Some((0, &5, 0)), iter.next());
assert_eq!(Some((3, &6, 3)), iter.next());
assert_eq!(None, iter.next());
// Note: 1 and 5 are not merged because they are not consecutive.Source§impl<'a, S, T, I> Sparse<S, T, I>
impl<'a, S, T, I> Sparse<S, T, I>
Sourcepub fn source_mut(&mut self) -> &mut S
pub fn source_mut(&mut self) -> &mut S
Get a mutable reference to the underlying source data.
pub fn selection_mut(&mut self) -> &mut Select<T, I>
pub fn indices_mut(&mut self) -> &mut I
Source§impl<'a, S, T, I> Sparse<S, T, I>where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoIterator,
T: Set + Get<'a, usize> + View<'a>,
I: AsIndexSlice,
impl<'a, S, T, I> Sparse<S, T, I>where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoIterator,
T: Set + Get<'a, usize> + View<'a>,
I: AsIndexSlice,
Source§impl<'a, S, T, I> Sparse<S, T, I>
impl<'a, S, T, I> Sparse<S, T, I>
pub fn source_iter( &'a self, ) -> <<S as View<'a>>::Type as IntoIterator>::IntoIter
Source§impl<'a, S, T, I> Sparse<S, T, I>where
I: AsIndexSlice,
impl<'a, S, T, I> Sparse<S, T, I>where
I: AsIndexSlice,
pub fn index_iter(&'a self) -> Cloned<Iter<'a, usize>>
Source§impl<'a, S, T, I> Sparse<S, T, I>
impl<'a, S, T, I> Sparse<S, T, I>
pub fn indexed_source_iter( &'a self, ) -> Zip<Cloned<Iter<'a, usize>>, <<S as View<'a>>::Type as IntoIterator>::IntoIter>
Source§impl<'a, S, T, I> Sparse<S, T, I>
A mutable iterator over the source elements in S
impl<'a, S, T, I> Sparse<S, T, I>
A mutable iterator over the source elements in S
pub fn source_iter_mut( &'a mut self, ) -> <<S as ViewMut<'a>>::Type as IntoIterator>::IntoIter
Source§impl<'a, S, T, I> Sparse<S, T, I>
A mutable iterator can only iterate over the source elements in S and not
the target elements in T since we would need scheduling to modify
potentially overlapping mutable references.
impl<'a, S, T, I> Sparse<S, T, I>
A mutable iterator can only iterate over the source elements in S and not
the target elements in T since we would need scheduling to modify
potentially overlapping mutable references.
pub fn indexed_source_iter_mut( &'a mut self, ) -> impl Iterator<Item = (usize, <<S as ViewMut<'a>>::Type as IntoIterator>::Item)>
Source§impl<'a, S, T, I> Sparse<S, T, I>
A mutable iterator can only iterate over the source elements in S and not
the target elements in T since we would need scheduling to modify
potentially overlapping mutable references.
impl<'a, S, T, I> Sparse<S, T, I>
A mutable iterator can only iterate over the source elements in S and not
the target elements in T since we would need scheduling to modify
potentially overlapping mutable references.
Source§impl<'a, S, T, I> Sparse<S, T, I>
Mutably iterate over the selected indices.
impl<'a, S, T, I> Sparse<S, T, I>
Mutably iterate over the selected indices.
pub fn index_iter_mut( &'a mut self, ) -> impl Iterator<Item = (&'a mut usize, <<S as View<'a>>::Type as IntoIterator>::Item)>
Source§impl<S, O, T, I> Sparse<Chunked<S, O>, T, I>
impl<S, O, T, I> Sparse<Chunked<S, O>, T, I>
Sourcepub fn trim(&mut self) -> usize
pub fn trim(&mut self) -> usize
Remove any empty elements (indexed chunks) at the end of the collection and any unindexed data past the last offset. Return the number of elements removed.
§Example
use flatk::*;
let mut s = Sparse::from_dim(vec![0,1,2], 3, Chunked::from_sizes(vec![1,3,2], vec![1,2,3,4,5,6]));
assert_eq!(3, s.len());
// Transferring the last two elements past the indexed stack.
// This creates an empty chunk at the end.
s.source_mut().transfer_forward(2, 2);
assert_eq!(6, s.storage().len());
assert_eq!(3, s.len());
s.trim(); // remove unindexed elements.
assert_eq!(4, s.storage().len());Trait Implementations§
Source§impl<'a, S, T, I> AtomIterator<'a> for Sparse<S, T, I>where
S: AtomIterator<'a>,
impl<'a, S, T, I> AtomIterator<'a> for Sparse<S, T, I>where
S: AtomIterator<'a>,
Source§impl<'a, S, T, I> AtomMutIterator<'a> for Sparse<S, T, I>where
S: AtomMutIterator<'a>,
impl<'a, S, T, I> AtomMutIterator<'a> for Sparse<S, T, I>where
S: AtomMutIterator<'a>,
type Item = <S as AtomMutIterator<'a>>::Item
type Iter = <S as AtomMutIterator<'a>>::Iter
fn atom_mut_iter(&'a mut self) -> Self::Iter
Source§impl<T: Clone, S: CloneWithStorage<U>, I: Clone, U> CloneWithStorage<U> for Sparse<S, T, I>
impl<T: Clone, S: CloneWithStorage<U>, I: Clone, U> CloneWithStorage<U> for Sparse<S, T, I>
type CloneType = Sparse<<S as CloneWithStorage<U>>::CloneType, T, I>
fn clone_with_storage(&self, storage: U) -> Self::CloneType
Source§impl<S, T> Extend<(usize, <S as Set>::Elem)> for Sparse<S, T>
impl<S, T> Extend<(usize, <S as Set>::Elem)> for Sparse<S, T>
Source§fn extend<It: IntoIterator<Item = (usize, <S as Set>::Elem)>>(
&mut self,
iter: It,
)
fn extend<It: IntoIterator<Item = (usize, <S as Set>::Elem)>>( &mut self, iter: It, )
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<S, T, I> IntoOwnedData for Sparse<S, T, I>where
S: IntoOwnedData,
impl<S, T, I> IntoOwnedData for Sparse<S, T, I>where
S: IntoOwnedData,
type OwnedData = Sparse<<S as IntoOwnedData>::OwnedData, T, I>
fn into_owned_data(self) -> Self::OwnedData
fn clone_into(self, target: &mut Self::OwnedData)
Source§impl<S, T, I> IntoParallelIterator for Sparse<S, T, I>where
S: Send + SplitAt + SplitFirst + Set + Dummy,
S::First: Send,
I: Send + IndexedParallelIterator + Producer<Item = usize>,
I::IntoIter: ExactSizeIterator<Item = usize>,
impl<S, T, I> IntoParallelIterator for Sparse<S, T, I>where
S: Send + SplitAt + SplitFirst + Set + Dummy,
S::First: Send,
I: Send + IndexedParallelIterator + Producer<Item = usize>,
I::IntoIter: ExactSizeIterator<Item = usize>,
Source§impl<S: IntoStorage, T, I> IntoStorage for Sparse<S, T, I>
impl<S: IntoStorage, T, I> IntoStorage for Sparse<S, T, I>
Source§fn into_storage(self) -> Self::StorageType
fn into_storage(self) -> Self::StorageType
Convert the sparse set into its raw storage representation.
type StorageType = <S as IntoStorage>::StorageType
Source§impl<S, T, I> IsolateIndex<Sparse<S, T, I>> for Range<usize>
impl<S, T, I> IsolateIndex<Sparse<S, T, I>> for Range<usize>
Source§impl<S, T, I> IsolateIndex<Sparse<S, T, I>> for usize
impl<S, T, I> IsolateIndex<Sparse<S, T, I>> for usize
Source§impl<S: MapStorage<Out>, T, I, Out> MapStorage<Out> for Sparse<S, T, I>
impl<S: MapStorage<Out>, T, I, Out> MapStorage<Out> for Sparse<S, T, I>
Source§impl<S: PermuteInPlace, T, I: PermuteInPlace> PermuteInPlace for Sparse<S, T, I>
impl<S: PermuteInPlace, T, I: PermuteInPlace> PermuteInPlace for Sparse<S, T, I>
fn permute_in_place(&mut self, permutation: &[usize], seen: &mut [bool])
Source§impl<S: RemovePrefix, T, I: RemovePrefix> RemovePrefix for Sparse<S, T, I>
impl<S: RemovePrefix, T, I: RemovePrefix> RemovePrefix for Sparse<S, T, I>
Source§fn remove_prefix(&mut self, n: usize)
fn remove_prefix(&mut self, n: usize)
n elements from the beginning.Source§impl<S: Set, T, I> Set for Sparse<S, T, I>
impl<S: Set, T, I> Set for Sparse<S, T, I>
Source§fn len(&self) -> usize
fn len(&self) -> usize
Get the length of this sparse collection.
§Example
use flatk::*;
let v = vec![1,2,3,4,5];
let sparse = Sparse::from_dim(vec![0,2,2,1,1], 3, v.as_slice());
assert_eq!(5, sparse.len());Source§type Atom = <S as Set>::Atom
type Atom = <S as Set>::Atom
Elem.fn is_empty(&self) -> bool
Source§impl<S: Storage, T, I> Storage for Sparse<S, T, I>
impl<S: Storage, T, I> Storage for Sparse<S, T, I>
Source§fn storage(&self) -> &Self::Storage
fn storage(&self) -> &Self::Storage
Return an immutable reference to the underlying storage type of source data.
§Example
use flatk::*;
let v = vec![1,2,3,4,5,6,7,8,9,10,11,12];
let s0 = Chunked3::from_flat(v.clone());
let s1 = Sparse::from_dim(vec![0, 2, 2, 0], 4, s0.clone());
assert_eq!(s1.storage(), &v);type Storage = <S as Storage>::Storage
Source§impl<S: StorageInto<U>, T, I, U> StorageInto<U> for Sparse<S, T, I>
Pass through the conversion for structure type Subset.
impl<S: StorageInto<U>, T, I, U> StorageInto<U> for Sparse<S, T, I>
Pass through the conversion for structure type Subset.
type Output = Sparse<<S as StorageInto<U>>::Output, T, I>
fn storage_into(self) -> Self::Output
Source§impl<S: StorageMut, T, I> StorageMut for Sparse<S, T, I>
impl<S: StorageMut, T, I> StorageMut for Sparse<S, T, I>
Source§fn storage_mut(&mut self) -> &mut Self::Storage
fn storage_mut(&mut self) -> &mut Self::Storage
Return a mutable reference to the underlying storage type of source data.
§Example
use flatk::*;
let mut v = vec![1,2,3,4,5,6,7,8,9,10,11,12];
let mut s0 = Chunked3::from_flat(v.clone());
let mut s1 = Sparse::from_dim(vec![0, 2, 2, 0], 4, s0.clone());
assert_eq!(s1.storage_mut(), &mut v);Source§impl<'a, S: StorageView<'a>, T, I> StorageView<'a> for Sparse<S, T, I>
impl<'a, S: StorageView<'a>, T, I> StorageView<'a> for Sparse<S, T, I>
Source§fn storage_view(&'a self) -> Self::StorageView
fn storage_view(&'a self) -> Self::StorageView
Return a view to the underlying storage type of source data.
§Example
use flatk::*;
let v = vec![1,2,3,4,5,6,7,8,9,10,11,12];
let s0 = Chunked3::from_flat(v.clone());
let s1 = Sparse::from_dim(vec![0, 2, 2, 0], 4, s0.clone());
assert_eq!(s1.storage_view(), v.as_slice());type StorageView = <S as StorageView<'a>>::StorageView
Source§impl<'a, S, T, I> ViewIterator<'a> for Sparse<S, T, I>
impl<'a, S, T, I> ViewIterator<'a> for Sparse<S, T, I>
Source§impl<'a, S, T, I> ViewMutIterator<'a> for Sparse<S, T, I>
impl<'a, S, T, I> ViewMutIterator<'a> for Sparse<S, T, I>
impl<S: Copy, T: Copy, I: Copy> Copy for Sparse<S, T, I>
impl<S, T, I> DynamicRangeIndexType for Sparse<S, T, I>
impl<S, T, I> StructuralPartialEq for Sparse<S, T, I>
impl<S, T, I> ValueType for Sparse<S, T, I>
impl<S: Viewed, T: Viewed, I: Viewed> Viewed for Sparse<S, T, I>
Auto Trait Implementations§
impl<S, T, I> Freeze for Sparse<S, T, I>
impl<S, T, I> RefUnwindSafe for Sparse<S, T, I>
impl<S, T, I> Send for Sparse<S, T, I>
impl<S, T, I> Sync for Sparse<S, T, I>
impl<S, T, I> Unpin for Sparse<S, T, I>
impl<S, T, I> UnwindSafe for Sparse<S, T, I>
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<'a, S, I> Get<'a, I> for Swhere
I: GetIndex<'a, S>,
impl<'a, S, I> Get<'a, I> for Swhere
I: GetIndex<'a, S>,
type Output = <I as GetIndex<'a, S>>::Output
fn get(&self, idx: I) -> Option<<I as GetIndex<'a, S>>::Output>
Source§fn at(&self, idx: I) -> Self::Output
fn at(&self, idx: I) -> Self::Output
get that will panic if the equivalent get call is None,
which typically means that the given index is out of bounds. Read moreSource§unsafe fn at_unchecked(&self, idx: I) -> Self::Output
unsafe fn at_unchecked(&self, idx: I) -> Self::Output
Source§impl<S> IntoChunkIterator for S
impl<S> IntoChunkIterator for S
type Item = S
type IterType = ChunkedNIter<S>
Source§fn into_chunk_iter(
self,
chunk_size: usize,
) -> <S as IntoChunkIterator>::IterType
fn into_chunk_iter( self, chunk_size: usize, ) -> <S as IntoChunkIterator>::IterType
chunk_size.
One notable difference between this trait and chunks* methods on slices is that
chunks_iter should panic when the underlying data cannot split into chunk_size sized
chunks exactly.