Struct flatk::Subset[][src]

pub struct Subset<S, I = Box<[usize]>> { /* fields omitted */ }
Expand description

A Set that is a non-contiguous subset of some larger collection. B can be any borrowed collection type that implements the Set, and RemovePrefix traits. For iteration of subsets, the underlying type must also implement SplitFirst and SplitAt traits.

Example

The following example shows how to create a Subset from a standard Vec.

use flatk::*;
let v = vec![1,2,3,4,5];
let subset = Subset::from_indices(vec![0,2,4], v.as_slice());
let mut subset_iter = subset.iter();
assert_eq!(Some(&1), subset_iter.next());
assert_eq!(Some(&3), subset_iter.next());
assert_eq!(Some(&5), subset_iter.next());
assert_eq!(None, subset_iter.next());

The next example shows how to create a Subset from a UniChunked collection.

use flatk::*;
let mut v = Chunked3::from_flat(vec![1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]);
let mut subset = Subset::from_indices(vec![0,2,4], v.view_mut());
{
    let subset_view = subset.view();
    let mut subset_iter = subset_view.iter();
    assert_eq!(Some(&[1,2,3]), subset_iter.next());
    assert_eq!(Some(&[7,8,9]), subset_iter.next());
    assert_eq!(Some(&[13,14,15]), subset_iter.next());
    assert_eq!(None, subset_iter.next());
}
*subset.view_mut().isolate(1) = [0; 3];
assert_eq!(&[0,0,0], subset.view().at(1));

Translation independence

This struct is very similar to Chunked, with the main difference being that each index corresponds to a single element instead of a chunk starting point.

Translation independence refers to the need to ensure that indices remain valid after splitting. When the indices are owned or mutably borrowed, we could simply modify the indices when we split the subset, but when the indices are a borrowed slice, this is not possible. To resolve this, we chop the part of data below the first index to ensure that the first index serves as an offset to the rest of the indices, making the entire index array translation independent.

Because Subsets modify the underlying data storage, it can often be misleading when querying the underlying data at any given time using one of Storage, StorageMut or StorageView traits.

For a more transparent data structure that preserves the original data set, use Select. To expose any characteristics of the contained data type, use a trait. See ChunkSize for an example.

Implementations

Convert this subset into its internal representation.

Construct a subset from a set of indices and a data set.

Note that independent of the value of the indices, the first element in the subset will be the first element in data, and all subsequent elements are taken from data[index - first] for each index in indices where first is the first index appearing in indices.

Safety

Constructing an invalid subset using this function isn’t itself unsafe, however calling various functions (except for Subset::validate) may be unsafe.

The given indices must be unique and in accending sorted order. All indices (minus the first) must be strictly less than the number of elements in data.

The Subset can be validated explicitly after creation using Subset::validate.

Create a subset of elements from the original set given at the specified indices.

Example
use flatk::*;
let v = vec![1,2,3];
let subset = Subset::from_indices(vec![0,2], v.as_slice());
assert_eq!(1, subset[0]);
assert_eq!(3, subset[1]);

Create a subset of elements from the original collection corresponding to the given indices.

In contrast to Subset::from_indices, this function expects the indices to be unique and in sorted order, instead of manully making it so.

Panics

This function panics when given a collection of unsorted indices. It also panics when indices are repeated.

Example
use flatk::*;
let v = vec![0,1,2,3];
let indices = vec![1,3];

let subset_view = Subset::from_unique_ordered_indices(indices.as_slice(), v.as_slice());
assert_eq!(1, subset_view[0]);
assert_eq!(3, subset_view[1]);

let subset = Subset::from_unique_ordered_indices(indices, v.as_slice());
assert_eq!(1, subset[0]);
assert_eq!(3, subset[1]);

Create a subset with all elements from the original set.

Example
use flatk::*;
let subset = Subset::all::<Box<_>>(vec![1,2,3]);
let subset_view = subset.view();
let mut subset_iter = subset_view.iter();
assert_eq!(Some(&1), subset_iter.next());
assert_eq!(Some(&2), subset_iter.next());
assert_eq!(Some(&3), subset_iter.next());
assert_eq!(None, subset_iter.next());

Create a subset with all elements from the original set.

This version of all creates the Subset type with the default index type, since it cannot be determined from the function arguments. In other words this function doesn’t require an additional generic parameter to be specified when the return type is ambiguous.

Example
use flatk::*;
let subset = Subset::all_def(vec![1,2,3]);
let subset_view = subset.view();
let mut subset_iter = subset_view.iter();
assert_eq!(Some(&1), subset_iter.next());
assert_eq!(Some(&2), subset_iter.next());
assert_eq!(Some(&3), subset_iter.next());
assert_eq!(None, subset_iter.next());

Find an element in the subset by its index in the superset. Return the index of the element in the subset if found. Since subset indices are always in sorted order, this function performs a binary search.

Examples

In the following simple example the element 3 is found at superset index 2 which is located at index 1 in the subset.

use flatk::*;
let superset =  vec![1,2,3,4,5,6];
let subset = Subset::from_unique_ordered_indices(vec![1,2,5], superset);
assert_eq!(Some(1), subset.find_by_index(2));
assert_eq!(None, subset.find_by_index(3));

Note that the superset index refers to the indices with which the subset was created. This means that even after we have split the subset, the input indices are expected to refer to the original subset. The following example demonstrates this by splitting the original subset in the pervious example.

use flatk::*;
let superset =  vec![1,2,3,4,5,6];
let subset = Subset::from_unique_ordered_indices(vec![1,2,5], superset);
let (_, r) = subset.view().split_at(1);
assert_eq!(Some(0), r.find_by_index(2));
assert_eq!(None, r.find_by_index(3));

Get a references to the underlying indices. If None is returned, then this subset spans the entire domain data.

Return the superset of this Subset. This is just the set it was created with.

The typical way to use this function is to clone from a SubsetView into an owned S type.

Panics

This function panics if other has a length unequal to self.len().

Example
use flatk::*;
let v = vec![1,2,3,4,5];
let indices = vec![0,2,4];
let subset = Subset::from_unique_ordered_indices(indices.as_slice(), v.as_slice());
let mut owned = vec![0; 4];
subset.clone_into_other(&mut owned[..3]); // Need 3 elements to avoid panics.
let mut iter_owned = owned.iter();
assert_eq!(owned, vec![1,3,5,0]);

Immutably iterate over a borrowed subset.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
let mut iter = subset.iter();
assert_eq!(Some(&1), iter.next());
assert_eq!(Some(&3), iter.next());
assert_eq!(Some(&5), iter.next());
assert_eq!(None, iter.next());

Immutably iterate over the indices stored by this subset.

The returned indices point to the superset from which this subset was created.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
let mut iter = subset.index_iter();
assert_eq!(Some(0), iter.next());
assert_eq!(Some(2), iter.next());
assert_eq!(Some(4), iter.next());
assert_eq!(None, iter.next());

This also works if the subset is entire:

use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::all_def(v.as_slice());
let mut iter = subset.index_iter();
assert_eq!(Some(0), iter.next());
assert_eq!(Some(3), iter.nth(2));
assert_eq!(Some(4), iter.next());
assert_eq!(None, iter.next());

Mutably iterate over a borrowed subset.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
for i in subset.iter_mut() {
    *i += 1;
}
assert_eq!(v, vec![2,2,4,4,6]);

Trait Implementations

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

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

Immutably index the subset.

Panics

This function panics if the index is out of bounds or if the subset is empty.

Example
use flatk::*;
let c = Chunked2::from_flat((1..=12).collect::<Vec<_>>());
let subset = Subset::from_indices(vec![0,2,4], c.view());
assert_eq!([1,2], subset[0]);
assert_eq!([5,6], subset[1]);
assert_eq!([9,10], subset[2]);

The returned type after indexing.

Immutably index the subset.

Panics

This function panics if the index is out of bounds or if the subset is empty.

Example
use flatk::*;
let v = vec![1,2,3,4,5];
let subset = Subset::from_indices(vec![0,2,4], v.as_slice());
assert_eq!(3, subset[1]);

The returned type after indexing.

Immutably index the subset.

Panics

This function panics if the index is out of bounds or if the subset is empty.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
assert_eq!(3, subset[1]);

The returned type after indexing.

Mutably index the subset.

Panics

This function panics if the index is out of bounds or if the subset is empty.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
assert_eq!(subset[1], 3);
subset[1] = 100;
assert_eq!(subset[0], 1);
assert_eq!(subset[1], 100);
assert_eq!(subset[2], 5);

Mutably index the subset.

Panics

This function panics if the index is out of bounds or if the subset is empty.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
assert_eq!(subset[1], 3);
subset[1] = 100;
assert_eq!(subset[0], 1);
assert_eq!(subset[1], 100);
assert_eq!(subset[2], 5);

Convert a Subset into an iterator.

Example
use flatk::*;
let mut s = Subset::from_unique_ordered_indices(vec![1,3,5], vec![1,2,3,4,5,6]);
let mut iter = s.view().into_iter();
assert_eq!(Some(&2), iter.next());
assert_eq!(Some(&4), iter.next());
assert_eq!(Some(&6), iter.next());
assert_eq!(None, iter.next());

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

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 !=.

This function will panic if n is larger than self.len().

Required for Chunked and UniChunked subsets.

Get the length of this subset.

Example
use flatk::*;
let v = vec![1,2,3,4,5];
let subset = Subset::from_indices(vec![0,2,4], v.as_slice());
assert_eq!(3, subset.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

This impl enables Subsets of Subsets

Split the first element of this subset.

Split off the first element without checking if one exists. Read more

Return an immutable reference to the underlying storage type.

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 = Subset::from_indices(vec![0, 2, 3], s0.clone());
assert_eq!(s1.storage(), &v);

Pass through the conversion for structure type Subset.

Return a mutable reference to the underlying storage type.

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 = Subset::from_indices(vec![0, 2, 3], s0.clone());
assert_eq!(s1.storage_mut(), &mut v);

Return a view to the underlying storage type.

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 = Subset::from_indices(vec![0, 2, 3], s0.clone());
assert_eq!(s1.storage_view(), v.as_slice());

Required for Chunked and UniChunked subsets.

Create a mutable view into this subset.

Example
use flatk::*;
let mut v = vec![1,2,3,4,5];
let mut subset = Subset::from_indices(vec![0,2,4], v.as_mut_slice());
let mut view = subset.view_mut();
for i in view.iter_mut() {
    *i += 1;
}
assert_eq!(v, vec![2,2,4,4,6]);

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.