Struct flatk::Subset [−][src]
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 Subset
s 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
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));
pub fn clone_into_other<V>(&'a self, other: &'a mut V) where
V: Set + ViewMutIterator<'a> + ?Sized,
<Self as ViewIterator<'a>>::Item: CloneIntoOther<<V as ViewMutIterator<'a>>::Item>,
pub fn clone_into_other<V>(&'a self, other: &'a mut V) where
V: Set + ViewMutIterator<'a> + ?Sized,
<Self as ViewIterator<'a>>::Item: CloneIntoOther<<V as ViewMutIterator<'a>>::Item>,
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]);
pub fn iter(&'a self) -> SubsetIter<S::Type, &'a [usize]>ⓘNotable traits for SubsetIter<S, I>impl<S, I> Iterator for SubsetIter<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>, type Item = S::First;
pub fn iter(&'a self) -> SubsetIter<S::Type, &'a [usize]>ⓘNotable traits for SubsetIter<S, I>impl<S, I> Iterator for SubsetIter<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>, type Item = S::First;
impl<S, I> Iterator for SubsetIter<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>, type Item = S::First;
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());
pub fn index_iter(&'a self) -> SubsetIndexIter<'a>ⓘNotable traits for SubsetIndexIter<'a>impl<'a> Iterator for SubsetIndexIter<'a> type Item = usize;
pub fn index_iter(&'a self) -> SubsetIndexIter<'a>ⓘNotable traits for SubsetIndexIter<'a>impl<'a> Iterator for SubsetIndexIter<'a> type Item = usize;
impl<'a> Iterator for SubsetIndexIter<'a> type Item = usize;
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());
pub fn iter_mut(
&'a mut self
) -> SubsetIter<<S as ViewMut<'a>>::Type, &'a [usize]>ⓘNotable traits for SubsetIter<S, I>impl<S, I> Iterator for SubsetIter<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>, type Item = S::First;
pub fn iter_mut(
&'a mut self
) -> SubsetIter<<S as ViewMut<'a>>::Type, &'a [usize]>ⓘNotable traits for SubsetIter<S, I>impl<S, I> Iterator for SubsetIter<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>, type Item = S::First;
impl<S, I> Iterator for SubsetIter<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>, type Item = S::First;
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
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]);
type Output = T
type Output = T
The returned type after indexing.
type Output = T
type Output = T
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);
impl<S, I> IntoIterator for Subset<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>,
impl<S, I> IntoIterator for Subset<S, I> where
S: SplitAt + SplitFirst + Set + Dummy,
I: SplitFirst + Clone,
<I as SplitFirst>::First: Borrow<usize>,
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());
type IntoIter = SubsetIter<S, I>
type IntoIter = SubsetIter<S, I>
Which kind of iterator are we turning this into?
Attempts to isolate a value in the given set at this index. Read more
impl<S, O, N: Unsigned> IsolateIndex<Subset<S, O>> for StaticRange<N> where
Range<usize>: IsolateIndex<Subset<S, O>>,
impl<S, O, N: Unsigned> IsolateIndex<Subset<S, O>> for StaticRange<N> where
Range<usize>: IsolateIndex<Subset<S, O>>,
Attempts to isolate a value in the given set at this index. Read more
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());
The most basic element contained by this collection.
If this collection contains other collections, this type should be
different than Elem
. Read more
impl<S, I> SplitFirst for Subset<S, I> where
I: SplitFirst + AsRef<[usize]>,
<I as SplitFirst>::First: Borrow<usize>,
S: Set + SplitAt + SplitFirst,
impl<S, I> SplitFirst for Subset<S, I> where
I: SplitFirst + AsRef<[usize]>,
<I as SplitFirst>::First: Borrow<usize>,
S: Set + SplitAt + SplitFirst,
This impl enables Subset
s of Subset
s
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());
type StorageView = S::StorageView
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]);
type Item = <<S as ViewMut<'a>>::Type as SplitFirst>::First
Auto Trait Implementations
impl<S, I> RefUnwindSafe for Subset<S, I> where
I: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, I> UnwindSafe for Subset<S, I> where
I: UnwindSafe,
S: 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