[][src]Struct flatk::Sparse

pub struct Sparse<S, T = RangeTo<usize>, I = Vec<usize>> {
    pub selection: Select<T, I>,
    pub source: S,
}

A Sparse data set S where the sparsity pattern is given by I as select indices into a larger range.

Fields

selection: Select<T, I>source: S

Methods

impl<S, I> Sparse<S, RangeTo<usize>, I> where
    S: Set,
    I: AsRef<[usize]>, 
[src]

pub fn from_dim(indices: I, dim: usize, values: S) -> Self[src]

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());

impl<S, T, I> Sparse<S, T, I> where
    S: Set,
    T: Set,
    I: AsRef<[usize]>, 
[src]

pub fn new(selection: Select<T, I>, source: S) -> Self[src]

The most general constructor for a sparse collection taking a selection of values and their corresponding data.

Panics

This function will panic if selection and source have different sizes.

impl<S, T, I> Sparse<S, T, I>[src]

pub fn extend_pruned<S2, T2, I2, B>(
    &mut self,
    other: Sparse<S2, T2, I2>,
    combine: impl FnMut(&mut B::Owned, B),
    keep: impl FnMut(usize, &B::Owned) -> bool
) where
    S2: IntoIterator<Item = B>,
    I2: AsRef<[usize]>,
    B: IntoOwned,
    Self: Push<(usize, B::Owned)>, 
[src]

Extend the current sparse collection with a pruned and compressed version of the given sparse collection, other.

Example

use flatk::*;
let v = vec![1,2,3,4,5,6];
let sparse = Sparse::from_dim(vec![0,2,2,2,0,3], 4, v.as_slice());
let mut compressed = Sparse::from_dim(Vec::new(), 4, Vec::new());
compressed.extend_pruned(sparse, |a, b| *a += *b, |_, _| true);
let mut iter = compressed.iter(); // Returns (position, source, target) pairs
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());

impl<'a, S, T, I> Sparse<S, T, I>[src]

pub fn source(&self) -> &S[src]

Get a reference to the underlying source data.

pub fn source_mut(&mut self) -> &mut S[src]

Get a mutable reference to the underlying source data.

pub fn selection(&self) -> &Select<T, I>[src]

Get a reference to the underlying selection.

pub fn selection_mut(&mut self) -> &mut Select<T, I>[src]

pub fn indices(&self) -> &I[src]

Get a reference to the underlying indices.

pub fn indices_mut(&mut self) -> &mut I[src]

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: AsRef<[usize]>, 
[src]

pub fn iter(
    &'a self
) -> impl Iterator<Item = (usize, <<S as View<'a>>::Type as IntoIterator>::Item, <T as Get<'a, usize>>::Output)>
[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    S: View<'a>,
    <S as View<'a>>::Type: Set + IntoIterator
[src]

pub fn source_iter(
    &'a self
) -> <<S as View<'a>>::Type as IntoIterator>::IntoIter
[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    I: AsRef<[usize]>, 
[src]

pub fn index_iter(&'a self) -> Cloned<Iter<'a, usize>>[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    S: View<'a>,
    <S as View<'a>>::Type: Set + IntoIterator,
    I: AsRef<[usize]>, 
[src]

pub fn indexed_source_iter(
    &'a self
) -> Zip<Cloned<Iter<'a, usize>>, <<S as View<'a>>::Type as IntoIterator>::IntoIter>
[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    S: ViewMut<'a>,
    <S as ViewMut<'a>>::Type: Set + IntoIterator
[src]

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
[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    S: ViewMut<'a>,
    <S as ViewMut<'a>>::Type: Set + IntoIterator,
    I: AsRef<[usize]>, 
[src]

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)>
[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    S: ViewMut<'a>,
    <S as ViewMut<'a>>::Type: Set + IntoIterator,
    I: AsMut<[usize]>, 
[src]

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 iter_mut(
    &'a mut self
) -> Zip<IterMut<'a, usize>, <<S as ViewMut<'a>>::Type as IntoIterator>::IntoIter>
[src]

impl<'a, S, T, I> Sparse<S, T, I> where
    S: View<'a>,
    <S as View<'a>>::Type: Set + IntoIterator,
    I: AsMut<[usize]>, 
[src]

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)>
[src]

impl<S, O, T, I> Sparse<Chunked<S, O>, T, I> where
    S: Set + Truncate,
    O: AsRef<[usize]> + Truncate + Set,
    I: Truncate
[src]

pub fn trim(&mut self) -> usize[src]

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

impl<'a, S, T, I> AtomIterator<'a> for Sparse<S, T, I> where
    S: AtomIterator<'a>, 
[src]

type Item = S::Item

type Iter = S::Iter

impl<'a, S, T, I> AtomMutIterator<'a> for Sparse<S, T, I> where
    S: AtomMutIterator<'a>, 
[src]

type Item = S::Item

type Iter = S::Iter

impl<S: ChunkSize, T, I> ChunkSize for Sparse<S, T, I>[src]

impl<S: Clear, T, I: Clear> Clear for Sparse<S, T, I>[src]

impl<S: Clone, T: Clone, I: Clone> Clone for Sparse<S, T, I>[src]

impl<T: Clone, S: CloneWithStorage<U>, I: Clone, U> CloneWithStorage<U> for Sparse<S, T, I>[src]

type CloneType = Sparse<S::CloneType, T, I>

impl<S: Copy, T: Copy, I: Copy> Copy for Sparse<S, T, I>[src]

impl<S: Debug, T: Debug, I: Debug> Debug for Sparse<S, T, I>[src]

impl<S: Dummy, T: Dummy, I: Dummy> Dummy for Sparse<S, T, I>[src]

impl<S, T> Extend<(usize, <S as Set>::Elem)> for Sparse<S, T> where
    S: Set + Extend<<S as Set>::Elem>, 
[src]

impl<'a, S, T, I> GetIndex<'a, Sparse<S, T, I>> for usize where
    I: AsRef<[usize]>,
    S: Get<'a, usize>, 
[src]

type Output = (usize, <S as Get<'a, usize>>::Output)

impl<S: IntoFlat, T, I> IntoFlat for Sparse<S, T, I>[src]

type FlatType = S::FlatType

fn into_flat(self) -> Self::FlatType[src]

Convert the sparse set into its raw storage representation.

impl<S: IntoOwned, T: IntoOwned, I: IntoOwned> IntoOwned for Sparse<S, T, I>[src]

type Owned = Sparse<S::Owned, T::Owned, I::Owned>

impl<S, T, I> IntoOwnedData for Sparse<S, T, I> where
    S: IntoOwnedData
[src]

type OwnedData = Sparse<S::OwnedData, T, I>

impl<S, T, I> IsolateIndex<Sparse<S, T, I>> for usize where
    I: Isolate<usize>,
    <I as Isolate<usize>>::Output: Borrow<usize>,
    S: Isolate<usize>,
    T: Isolate<usize>, 
[src]

type Output = (<I as Isolate<usize>>::Output, <S as Isolate<usize>>::Output, <T as Isolate<usize>>::Output)

impl<S, T, I> IsolateIndex<Sparse<S, T, I>> for Range<usize> where
    S: Isolate<Range<usize>>,
    I: Isolate<Range<usize>>, 
[src]

type Output = Sparse<S::Output, T, I::Output>

impl<S, T, I, N: Unsigned> IsolateIndex<Sparse<S, T, I>> for StaticRange<N> where
    Range<usize>: IsolateIndex<Sparse<S, T, I>>, 
[src]

type Output = <Range<usize> as IsolateIndex<Sparse<S, T, I>>>::Output

impl<S: PartialEq, T: PartialEq, I: PartialEq> PartialEq<Sparse<S, T, I>> for Sparse<S, T, I>[src]

impl<S: PermuteInPlace, T, I: PermuteInPlace> PermuteInPlace for Sparse<S, T, I>[src]

impl<S, T, I, A> Push<(usize, A)> for Sparse<S, T, I> where
    S: Set<Elem = A> + Push<A>,
    I: Push<usize>, 
[src]

impl<S: RemovePrefix, T, I: RemovePrefix> RemovePrefix for Sparse<S, T, I>[src]

impl<S: Reserve, T, I: Reserve> Reserve for Sparse<S, T, I>[src]

impl<S: Set, T, I> Set for Sparse<S, T, I>[src]

type Elem = (usize, S::Elem)

Owned element of the set.

type Atom = S::Atom

The most basic element contained by this collection. If this collection contains other collections, this type should be different than Elem. Read more

fn len(&self) -> usize[src]

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());

impl<S, T, I> SplitAt for Sparse<S, T, I> where
    S: Set + SplitAt,
    T: Set + Clone,
    I: SplitAt
[src]

impl<S: Storage, T, I> Storage for Sparse<S, T, I>[src]

type Storage = S::Storage

fn storage(&self) -> &Self::Storage[src]

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);

impl<S: StorageInto<U>, T, I, U> StorageInto<U> for Sparse<S, T, I>[src]

Pass through the conversion for structure type Subset.

type Output = Sparse<S::Output, T, I>

impl<S: StorageMut, T, I> StorageMut for Sparse<S, T, I>[src]

fn storage_mut(&mut self) -> &mut Self::Storage[src]

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);

impl<'a, S: StorageView<'a>, T, I> StorageView<'a> for Sparse<S, T, I>[src]

type StorageView = S::StorageView

fn storage_view(&'a self) -> Self::StorageView[src]

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());

impl<S, T, I> StructuralPartialEq for Sparse<S, T, I>[src]

impl<S: Truncate, T, I: Truncate> Truncate for Sparse<S, T, I>[src]

impl<S, T, I, M> UniChunkable<M> for Sparse<S, T, I>[src]

type Chunk = Sparse<S, T, I>

impl<S, T, I> ValueType for Sparse<S, T, I>[src]

impl<'a, S, T, I> View<'a> for Sparse<S, T, I> where
    S: View<'a>,
    T: View<'a>,
    I: AsRef<[usize]>, 
[src]

type Type = Sparse<S::Type, T::Type, &'a [usize]>

impl<'a, S, T, I> ViewIterator<'a> for Sparse<S, T, I> where
    S: View<'a>,
    <S as View<'a>>::Type: Set + IntoIterator
[src]

type Item = <<S as View<'a>>::Type as IntoIterator>::Item

type Iter = <<S as View<'a>>::Type as IntoIterator>::IntoIter

impl<'a, S, T, I> ViewMut<'a> for Sparse<S, T, I> where
    S: Set + ViewMut<'a>,
    T: Set + View<'a>,
    I: AsMut<[usize]>, 
[src]

type Type = Sparse<S::Type, T::Type, &'a mut [usize]>

impl<'a, S, T, I> ViewMutIterator<'a> for Sparse<S, T, I> where
    S: ViewMut<'a>,
    <S as ViewMut<'a>>::Type: Set + IntoIterator
[src]

type Item = <<S as ViewMut<'a>>::Type as IntoIterator>::Item

type Iter = <<S as ViewMut<'a>>::Type as IntoIterator>::IntoIter

impl<S: Viewed, T: Viewed, I: Viewed> Viewed for Sparse<S, T, I>[src]

Auto Trait Implementations

impl<S, T, I> RefUnwindSafe for Sparse<S, T, I> where
    I: RefUnwindSafe,
    S: RefUnwindSafe,
    T: RefUnwindSafe

impl<S, T, I> Send for Sparse<S, T, I> where
    I: Send,
    S: Send,
    T: Send

impl<S, T, I> Sync for Sparse<S, T, I> where
    I: Sync,
    S: Sync,
    T: Sync

impl<S, T, I> Unpin for Sparse<S, T, I> where
    I: Unpin,
    S: Unpin,
    T: Unpin

impl<S, T, I> UnwindSafe for Sparse<S, T, I> where
    I: UnwindSafe,
    S: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> AsSlice<T> for T[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<'a, S, I> Get<'a, I> for S where
    I: GetIndex<'a, S>, 
[src]

type Output = <I as GetIndex<'a, S>>::Output

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<S> IntoChunkIterator for S where
    S: Set + SplitAt + Dummy
[src]

type Item = S

type IterType = ChunkedNIter<S>

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<S, I> Isolate<I> for S where
    I: IsolateIndex<S>, 
[src]

type Output = <I as IsolateIndex<S>>::Output

impl<T> Same<T> for T[src]

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.