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

Produce a parallel iterator over elements (borrowed slices) of a Sparse.

Produce a parallel iterator over elements (borrowed slices) of a Sparse.

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

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.

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 reference to the underlying source data.

Get a mutable reference to the underlying source data.

Get a reference to the underlying selection.

Get a reference to the underlying indices.

A mutable iterator over the source elements in S

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.

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.

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

Remove all elements from the current set without necessarily deallocating the space previously used. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Constructs a potentially invalid instance of a type. Read more

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Gets the value in the set at this index.

Gets the value in the set at this index. Read more

Gets the value in the set at this index.

Gets the value in the set at this index. Read more

The type of item that the parallel iterator will produce.

The parallel iterator type that will be created.

Converts self into a parallel iterator. Read more

Convert the sparse set into its raw storage representation.

Attempts to isolate a value in the given set at this index. Read more

Attempts to isolate a value in the given set at this index. Read more

Attempts to isolate a value in the given set at this index. Read more

Attempts to isolate a value in the given set at this index. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

Owned element of the set.

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

Split self into two sets at the given midpoint. This function is analogous to <[T]>::split_at. 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());

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Return a value at the given index. This is provided as the checked version of get that will panic if the equivalent get call is None, which typically means that the given index is out of bounds. Read more

Return a value at the given index. Read more

Performs the conversion.

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

Unchecked version of isolate. Read more

Return a value at the given index. This is provided as the checked version of try_isolate that will panic if the equivalent try_isolate call is None, which typically means that the given index is out of bounds. Read more

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.