Struct flatk::Sparse [−][src]
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: S
Implementations
pub 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
.
Create a sparse collection from the given set of indices
, a
dim
ension 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());
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.
Get a mutable reference to the underlying source data.
impl<'a, S, T, I> Sparse<S, T, I> where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoIterator,
I: AsIndexSlice,
impl<'a, S, T, I> Sparse<S, T, I> where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoIterator,
I: AsIndexSlice,
pub fn indexed_source_iter(
&'a self
) -> Zip<Cloned<Iter<'a, usize>>, <<S as View<'a>>::Type as IntoIterator>::IntoIter>
A mutable iterator over the source elements in S
impl<'a, S, T, I> Sparse<S, T, I> where
S: ViewMut<'a>,
<S as ViewMut<'a>>::Type: Set + IntoIterator,
I: AsIndexSlice,
impl<'a, S, T, I> Sparse<S, T, I> where
S: ViewMut<'a>,
<S as ViewMut<'a>>::Type: Set + IntoIterator,
I: AsIndexSlice,
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)>
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.
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)>
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
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
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>,
Convert the sparse set into its raw storage representation.
type StorageType = S::StorageType
Attempts to isolate a value in the given set at this index. Read more
Remove n
elements from the beginning.
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());
The most basic element contained by this collection.
If this collection contains other collections, this type should be
different than Elem
. Read more
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);
Pass through the conversion for structure type Subset
.
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);
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::StorageView
impl<'a, S, T, I> ViewIterator<'a> for Sparse<S, T, I> where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoIterator,
impl<'a, S, T, I> ViewIterator<'a> for Sparse<S, T, I> where
S: View<'a>,
<S as View<'a>>::Type: Set + IntoIterator,
impl<'a, S, T, I> ViewMutIterator<'a> for Sparse<S, T, I> where
S: ViewMut<'a>,
<S as ViewMut<'a>>::Type: Set + IntoIterator,
impl<'a, S, T, I> ViewMutIterator<'a> for Sparse<S, T, I> where
S: ViewMut<'a>,
<S as ViewMut<'a>>::Type: Set + IntoIterator,
type Item = <<S as ViewMut<'a>>::Type as IntoIterator>::Item
type Iter = <<S as ViewMut<'a>>::Type as IntoIterator>::IntoIter
Auto Trait Implementations
impl<S, T, I> RefUnwindSafe for Sparse<S, T, I> where
I: RefUnwindSafe,
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<S, T, I> UnwindSafe for Sparse<S, T, I> where
I: UnwindSafe,
S: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
type Item = S
type IterType = ChunkedNIter<S>
Produce a chunk iterator with the given stride 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. Read more
type Output = <I as IsolateIndex<S>>::Output
Unchecked version of isolate
. Read more