Struct flatk::Chunked [−][src]
pub struct Chunked<S, O = Offsets> {
pub chunks: O,
pub data: S,
}
Expand description
A partitioning of the collection S
into distinct chunks.
Fields
chunks: O
This can be either offsets of a uniform chunk size, if chunk size is specified at compile time.
data: S
Implementations
pub fn par_iter_mut(
&'a mut self
) -> ChunkedParIter<<<O as View<'a>>::Type as IntoParOffsetValuesAndSizes>::ParIter, <S as ViewMut<'a>>::Type>
pub fn par_iter_mut(
&'a mut self
) -> ChunkedParIter<<<O as View<'a>>::Type as IntoParOffsetValuesAndSizes>::ParIter, <S as ViewMut<'a>>::Type>
Produce a parallel iterator over elements (borrowed slices) of a Chunked
.
Sort each sparse chunk by the source index.
Example
use flatk::*;
let sparse = Sparse::from_dim(vec![0,2,1,2,0], 4, vec![1,2,3,4,5]);
let mut chunked = Chunked::from_sizes(vec![3,2], sparse);
chunked.sort_chunks_by_index();
assert_eq!(chunked.storage(), &[1,3,2,5,4]);
assert_eq!(chunked.data().indices(), &[0,1,2,0,2]);
Combine elements in each sorted chunk with the same index using the given function.
Assuming that the chunks are sorted by index, this function will combine adjacent elements with the same index into one element.
Example
use flatk::*;
let sparse = Sparse::from_dim(vec![0,2,1,1,2,0,2], 4, vec![1,2,3,4,5,6,7]);
let mut chunked = Chunked::from_sizes(vec![4,3], sparse);
chunked.sort_chunks_by_index();
let compressed = chunked.compressed(|a, b| *a += *b);
assert_eq!(compressed.view().offsets().into_inner(), &[0,3,5]);
assert_eq!(compressed.view().storage(), &[1,7,2,6,12]);
assert_eq!(compressed.view().data().indices(), &[0,1,2,0,2]);
pub fn pruned<E>(
&'a self,
combine: impl FnMut(&mut E::Owned, E),
keep: impl FnMut(usize, usize, &E::Owned) -> bool,
map: impl FnMut(usize, usize)
) -> Chunked<Sparse<S::Owned, T>, Offsets> where
<S as View<'a>>::Type: IntoIterator<Item = E>,
E: IntoOwned,
S::Owned: Set<Elem = E::Owned> + Default + Reserve + Push<E::Owned>,
pub fn pruned<E>(
&'a self,
combine: impl FnMut(&mut E::Owned, E),
keep: impl FnMut(usize, usize, &E::Owned) -> bool,
map: impl FnMut(usize, usize)
) -> Chunked<Sparse<S::Owned, T>, Offsets> where
<S as View<'a>>::Type: IntoIterator<Item = E>,
E: IntoOwned,
S::Owned: Set<Elem = E::Owned> + Default + Reserve + Push<E::Owned>,
Prune elements according to a given predicate and combine them in each sorted chunk with the same index.
Assuming that the chunks are sorted by index, this function will combine adjacent elements with the same index into one element.
This is a more general version of compressed
that allows you to prune unwanted elements.
In addition the combine
and keep
functions get an additional target
index where each element will be written to.
Examples
A simple example.
use flatk::*;
let sparse = Sparse::from_dim(vec![0,2,1,1,2,0,2], 4, vec![1.0, 2.0, 0.1, 0.01, 5.0, 0.001, 7.0]);
let mut chunked = Chunked::from_sizes(vec![4,3], sparse);
chunked.sort_chunks_by_index();
let pruned = chunked.pruned(|a, b| *a += *b, |_, _, &val| val > 0.01, |_,_| {});
assert_eq!(pruned.view().offsets().into_inner(), &[0,3,4]);
assert_eq!(pruned.view().storage(), &[1.0, 0.11, 2.0, 12.0]); // 0.001 is pruned.
assert_eq!(pruned.view().data().indices(), &[0,1,2,2]);
The following example extends on the previous example but shows how one may construct a mapping from original elements to the pruned output.
use flatk::*;
let indices = vec![0, 2, 1, 1, 2, 0, 2];
let num_indices = indices.len();
let sparse = Sparse::from_dim(indices, 4, vec![1.0, 2.0, 0.1, 0.01, 5.0, 0.001, 7.0]);
let mut chunked = Chunked::from_sizes(vec![4,3], sparse);
chunked.sort_chunks_by_index();
let mut mapping = vec![None; num_indices];
let pruned = chunked.pruned(|a, b| {
*a += *b
}, |_, _, &val| {
val > 0.01
}, |src, dst| mapping[src] = Some(dst));
// As before, the resulting structure is pruned.
assert_eq!(pruned.view().offsets().into_inner(), &[0,3,4]);
assert_eq!(pruned.view().storage(), &[1.0, 0.11, 2.0, 12.0]); // 0.001 is pruned.
assert_eq!(pruned.view().data().indices(), &[0,1,2,2]);
assert_eq!(mapping, vec![Some(0), Some(1), Some(1), Some(2), None, Some(3), Some(3)]);
Get a immutable reference to the underlying data.
Example
use flatk::*;
let v = vec![1,2,3,4,5,6];
let s = Chunked::from_offsets(vec![0,3,4,6], v.clone());
assert_eq!(&v, s.data());
Construct a Chunked
collection of elements from a set of sizes
that
determine the number of elements in each chunk. The sum of the sizes
must be equal to the length of the given data
.
Panics
This function will panic if the sum of all given sizes is greater than
data.len()
.
Example
use flatk::*;
let s = Chunked::from_sizes(vec![3,1,2], vec![1,2,3,4,5,6]);
let mut iter = s.iter();
assert_eq!(vec![1,2,3], iter.next().unwrap().to_vec());
assert_eq!(vec![4], iter.next().unwrap().to_vec());
assert_eq!(vec![5,6], iter.next().unwrap().to_vec());
assert_eq!(None, iter.next());
Constructs a Clumped
collection of elements from a set of sizes
and counts
sizes
and counts
determine the number of elements in each chunk. The
length of sizes
must be equal to the the length of counts
. Each
element in sizes
corresponds to chunk size, while the corresponding
element in counts
tells how many times this chunk size is repeated.
The dot product between sizes
and counts
must be equal to the length of the given
data
.
Panics
This function will panic if sizes
and counts
have different lengths or
if the dot product between sizes
and counts
is not equal to data.len()
.
Example
use flatk::*;
let s = Clumped::from_sizes_and_counts(vec![3,2], vec![2,1], vec![1,2,3,4,5,6,7,8]);
let mut iter = s.iter();
assert_eq!(&[1, 2, 3][..], iter.next().unwrap());
assert_eq!(&[4, 5, 6][..], iter.next().unwrap());
assert_eq!(&[7, 8][..], iter.next().unwrap());
assert_eq!(None, iter.next());
Construct a Chunked
collection of elements given a collection of
offsets into S
. This is the most efficient constructor for creating
variable sized chunks, however it is also the most error prone.
Panics
The absolute value of offsets
is not significant, however their
relative quantities are. More specifically, if x
is the first offset,
then the last element of offsets must always be data.len() + x
.
This also implies that offsets
cannot be empty. This function panics
if any one of these invariants isn’t satisfied.
Example
use flatk::*;
let s = Chunked::from_offsets(vec![0,3,4,6], vec![1,2,3,4,5,6]);
let mut iter = s.iter();
assert_eq!(vec![1,2,3], iter.next().unwrap().to_vec());
assert_eq!(vec![4], iter.next().unwrap().to_vec());
assert_eq!(vec![5,6], iter.next().unwrap().to_vec());
assert_eq!(None, iter.next());
Construct a Clumped
collection of elements given a collection of
“clumped” offsets into S
. This is the most efficient constructor for creating Clumped
types, however it is also the most error prone.
chunk_offsets
, identify the offsets into a conceptually “chunked” version of S
.
offsets
is a corresponding the collection of offsets into S
itself.
In theory, these should specify the places where the chunk size (or stride) changes within
S
, however this is not always necessary.
Panics
The absolute value of offsets (this applies to both offsets
as well as chunk_offsets
)
is not significant, however their relative quantities are. More specifically, for
offsets
, if x
is the first offset, then the last element of offsets must always be
data.len() + x
. For chunk_offsets
the same holds but data
is substituted by the
conceptual collection of chunks stored in data
. This also implies that offsets cannot be
empty. This function panics if any one of these invariants isn’t satisfied.
This function will also panic if offsets
and chunk_offsets
have different lengths.
Although the validity of offsets
is easily checked, the same is not true for
chunk_offsets
, since the implied stride must divide into the size of each clump, and
checking this at run time is expensive. As such a malformed Clumped
may cause panics
somewhere down the line. For ensuring a valid construction, use the
Self::from_sizes_and_counts
constructor.
Example
use flatk::*;
let v = vec![1,2, 3,4, 5,6, 7,8,9];
// The following splits v intos 3 pairs and a triplet.
let s = Clumped::from_clumped_offsets(vec![0,3,4], vec![0,6,9], v);
let mut iter = s.iter();
assert_eq!(&[1,2][..], iter.next().unwrap());
assert_eq!(&[3,4][..], iter.next().unwrap());
assert_eq!(&[5,6][..], iter.next().unwrap());
assert_eq!(&[7,8,9][..], iter.next().unwrap());
assert_eq!(None, iter.next());
Convert this Chunked
into its inner representation, which consists of
a collection of offsets (first output) along with the underlying data
storage type (second output).
Example
use flatk::*;
let data = vec![1,2,3,4,5,6];
let offsets = vec![0,3,4,6];
let s = Chunked::from_offsets(offsets.clone(), data.clone());
assert_eq!(s.into_inner(), (Offsets::new(offsets), data));
Return the offset into data
of the element at the given index.
This function returns the total length of data
if index
is equal to
self.len()
.
Panics
This function panics if index
is larger than self.len()
.
Example
use flatk::*;
let s = Chunked::from_offsets(vec![2,5,6,8], vec![1,2,3,4,5,6]);
assert_eq!(0, s.offset(0));
assert_eq!(3, s.offset(1));
assert_eq!(4, s.offset(2));
Return the raw offset value of the element at the given index.
Panics
This function panics if index
is larger than self.len()
.
Example
use flatk::*;
let s = Chunked::from_offsets(vec![2,5,6,8], vec![1,2,3,4,5,6]);
assert_eq!(2, s.offset_value(0));
assert_eq!(5, s.offset_value(1));
assert_eq!(6, s.offset_value(2));
Move a number of elements from a chunk at the given index to the following chunk. If the last chunk is selected, then the transferred elements are effectively removed.
This operation is efficient and only performs one write on a single
element in an array of usize
.
Example
use flatk::*;
let v = vec![1,2,3,4,5];
let mut c = Chunked::from_sizes(vec![3,2], v);
let mut c_iter = c.iter();
assert_eq!(Some(&[1,2,3][..]), c_iter.next());
assert_eq!(Some(&[4,5][..]), c_iter.next());
assert_eq!(None, c_iter.next());
// Transfer 2 elements from the first chunk to the next.
c.transfer_forward(0, 2);
let mut c_iter = c.iter();
assert_eq!(Some(&[1][..]), c_iter.next());
assert_eq!(Some(&[2,3,4,5][..]), c_iter.next());
assert_eq!(None, c_iter.next());
Like transfer_forward
but specify the number of elements to keep
instead of the number of elements to transfer in the chunk at
chunk_index
.
Move a number of elements from a chunk at the given index to the previous chunk.
If the first chunk is selected, then the transferred elements are explicitly removed, which may cause reallocation if the underlying storage type manages memory.
This operation is efficient and only performs one write on a single
element in an array of usize
, unless a reallocation is triggered.
Example
use flatk::*;
let v = vec![1,2,3,4,5];
let mut c = Chunked::from_sizes(vec![3,2], v);
let mut c_iter = c.iter();
assert_eq!(Some(&[1,2,3][..]), c_iter.next());
assert_eq!(Some(&[4,5][..]), c_iter.next());
assert_eq!(None, c_iter.next());
// Transfer 1 element from the second chunk to the previous.
c.transfer_backward(1, 1);
let mut c_iter = c.iter();
assert_eq!(Some(&[1,2,3,4][..]), c_iter.next());
assert_eq!(Some(&[5][..]), c_iter.next());
assert_eq!(None, c_iter.next());
Construct a Chunked
Vec
from a nested Vec
.
Example
use flatk::*;
let s = Chunked::<Vec<_>>::from_nested_vec(vec![vec![1,2,3],vec![4],vec![5,6]]);
let mut iter = s.iter();
assert_eq!(vec![1,2,3], iter.next().unwrap().to_vec());
assert_eq!(vec![4], iter.next().unwrap().to_vec());
assert_eq!(vec![5,6], iter.next().unwrap().to_vec());
assert_eq!(None, iter.next());
Remove any unused data past the last offset. Return the number of elements removed.
Example
use flatk::*;
let mut s = 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 a zero sized chunk at the end.
s.transfer_forward(2, 2);
assert_eq!(6, s.data().len());
assert_eq!(3, s.len());
s.trim_data(); // Remove unindexed elements.
assert_eq!(4, s.data().len());
Remove any empty chunks at the end of the collection and any unindexed data past the last offset. Return the number of chunks removed.
Example
use flatk::*;
let mut s = 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.transfer_forward(2, 2);
assert_eq!(6, s.data().len());
assert_eq!(3, s.len());
s.trim(); // Remove unindexed elements.
assert_eq!(4, s.data().len());
Push a slice of elements onto this Chunked
.
This can be more efficient than pushing from an iterator.
Example
use flatk::*;
let mut s = Chunked::from_offsets(vec![0,3,5], vec![1,2,3,4,5]);
assert_eq!(2, s.len());
s.push_slice(&[1,2]);
assert_eq!(3, s.len());
Push a chunk using an iterator over chunk elements.
Example
use flatk::*;
let mut s = Chunked::from_offsets(vec![0,3,5], vec![1,2,3,4,5]);
assert_eq!(2, s.len());
s.push_iter(std::iter::repeat(100).take(4));
assert_eq!(3, s.len());
assert_eq!(&[100; 4][..], s.view().at(2));
Extend the last chunk with the given iterator.
Example
use flatk::*;
let mut s = Chunked::from_offsets(vec![0,3,5], vec![1,2,3,4,5]);
assert_eq!(2, s.len());
s.extend_last(std::iter::repeat(100).take(2));
assert_eq!(2, s.len());
assert_eq!(&[4, 5, 100, 100][..], s.view().at(1));
pub fn iter(
&'a self
) -> ChunkedIter<<<O as View<'a>>::Type as IntoOffsetValuesAndSizes>::Iter, <S as View<'a>>::Type>ⓘNotable traits for ChunkedIter<I, V>impl<I, V> Iterator for ChunkedIter<I, V> where
V: SplitAt + Set + Dummy,
I: ExactSizeIterator<Item = (usize, usize)>, type Item = V;
pub fn iter(
&'a self
) -> ChunkedIter<<<O as View<'a>>::Type as IntoOffsetValuesAndSizes>::Iter, <S as View<'a>>::Type>ⓘNotable traits for ChunkedIter<I, V>impl<I, V> Iterator for ChunkedIter<I, V> where
V: SplitAt + Set + Dummy,
I: ExactSizeIterator<Item = (usize, usize)>, type Item = V;
impl<I, V> Iterator for ChunkedIter<I, V> where
V: SplitAt + Set + Dummy,
I: ExactSizeIterator<Item = (usize, usize)>, type Item = V;
Produce an iterator over elements (borrowed slices) of a Chunked
.
Examples
The following simple example demonstrates how to iterate over a Chunked
of integers stored in a flat Vec
.
use flatk::*;
let s = Chunked::from_offsets(vec![0,3,4,6], vec![1,2,3,4,5,6]);
let mut iter = s.iter();
let mut e0_iter = iter.next().unwrap().iter();
assert_eq!(Some(&1), e0_iter.next());
assert_eq!(Some(&2), e0_iter.next());
assert_eq!(Some(&3), e0_iter.next());
assert_eq!(None, e0_iter.next());
assert_eq!(Some(&[4][..]), iter.next());
assert_eq!(Some(&[5,6][..]), iter.next());
assert_eq!(None, iter.next());
Nested Chunked
s can also be used to create more complex data organization:
use flatk::*;
let s0 = Chunked::from_offsets(vec![0,3,4,6,9,11], vec![1,2,3,4,5,6,7,8,9,10,11]);
let s1 = Chunked::from_offsets(vec![0,1,4,5], s0);
let mut iter1 = s1.iter();
let v0 = iter1.next().unwrap();
let mut iter0 = v0.iter();
assert_eq!(Some(&[1,2,3][..]), iter0.next());
assert_eq!(None, iter0.next());
let v0 = iter1.next().unwrap();
let mut iter0 = v0.iter();
assert_eq!(Some(&[4][..]), iter0.next());
assert_eq!(Some(&[5,6][..]), iter0.next());
assert_eq!(Some(&[7,8,9][..]), iter0.next());
assert_eq!(None, iter0.next());
let v0 = iter1.next().unwrap();
let mut iter0 = v0.iter();
assert_eq!(Some(&[10,11][..]), iter0.next());
assert_eq!(None, iter0.next());
pub fn iter_mut(
&'a mut self
) -> ChunkedIter<<<O as View<'a>>::Type as IntoOffsetValuesAndSizes>::Iter, <S as ViewMut<'a>>::Type>ⓘNotable traits for ChunkedIter<I, V>impl<I, V> Iterator for ChunkedIter<I, V> where
V: SplitAt + Set + Dummy,
I: ExactSizeIterator<Item = (usize, usize)>, type Item = V;
pub fn iter_mut(
&'a mut self
) -> ChunkedIter<<<O as View<'a>>::Type as IntoOffsetValuesAndSizes>::Iter, <S as ViewMut<'a>>::Type>ⓘNotable traits for ChunkedIter<I, V>impl<I, V> Iterator for ChunkedIter<I, V> where
V: SplitAt + Set + Dummy,
I: ExactSizeIterator<Item = (usize, usize)>, type Item = V;
impl<I, V> Iterator for ChunkedIter<I, V> where
V: SplitAt + Set + Dummy,
I: ExactSizeIterator<Item = (usize, usize)>, type Item = V;
Produce a mutable iterator over elements (borrowed slices) of a
Chunked
.
Example
use flatk::*;
let mut s = Chunked::from_offsets(vec![0,3,4,6], vec![1,2,3,4,5,6]);
for i in s.view_mut().iter_mut() {
for j in i.iter_mut() {
*j += 1;
}
}
let mut iter = s.iter();
assert_eq!(vec![2,3,4], iter.next().unwrap().to_vec());
assert_eq!(vec![5], iter.next().unwrap().to_vec());
assert_eq!(vec![6,7], iter.next().unwrap().to_vec());
assert_eq!(None, iter.next());
Nested Chunked
s can also be used to create more complex data organization:
use flatk::*;
let mut s0 = Chunked::from_offsets(vec![0,3,4,6,9,11], vec![0,1,2,3,4,5,6,7,8,9,10]);
let mut s1 = Chunked::from_offsets(vec![0,1,4,5], s0);
for mut v0 in s1.view_mut().iter_mut() {
for i in v0.iter_mut() {
for j in i.iter_mut() {
*j += 1;
}
}
}
let v1 = s1.view();
let mut iter1 = v1.iter();
let v0 = iter1.next().unwrap();
let mut iter0 = v0.iter();
assert_eq!(Some(&[1,2,3][..]), iter0.next());
assert_eq!(None, iter0.next());
let v0 = iter1.next().unwrap();
let mut iter0 = v0.iter();
assert_eq!(Some(&[4][..]), iter0.next());
assert_eq!(Some(&[5,6][..]), iter0.next());
assert_eq!(Some(&[7,8,9][..]), iter0.next());
assert_eq!(None, iter0.next());
let v0 = iter1.next().unwrap();
let mut iter0 = v0.iter();
assert_eq!(Some(&[10,11][..]), iter0.next());
assert_eq!(None, iter0.next());
Trait Implementations
Construct a Chunked
collection from an iterator over immutable slices.
Example
use flatk::*;
let v = [&[1,2,3][..], &[4][..], &[5,6][..]];
let s: Chunked::<Vec<_>> = v.iter().cloned().collect();
let mut iter = s.iter();
assert_eq!(Some(&[1,2,3][..]), iter.next());
assert_eq!(Some(&[4][..]), iter.next());
assert_eq!(Some(&[5,6][..]), iter.next());
assert_eq!(None, iter.next());
Construct a Chunked
from an iterator over Vec
types.
Example
use flatk::*;
use std::iter::FromIterator;
let s = Chunked::<Vec<_>>::from_iter(vec![vec![1,2,3],vec![4],vec![5,6]].into_iter());
let mut iter = s.iter();
assert_eq!(vec![1,2,3], iter.next().unwrap().to_vec());
assert_eq!(vec![4], iter.next().unwrap().to_vec());
assert_eq!(vec![5,6], iter.next().unwrap().to_vec());
assert_eq!(None, iter.next());
Get an element of the given Chunked
collection.
Example
use flatk::*;
let v = vec![0, 1, 4, 6];
let data = (1..=6).collect::<Vec<_>>();
let s = Chunked::from_offsets(v.as_slice(), data.view());
assert_eq!(Some(&[1][..]), s.get(0));
assert_eq!(Some(&[2,3,4][..]), s.get(1));
assert_eq!(Some(&[5,6][..]), s.get(2));
Get an element of the given Chunked
collection.
Example
use flatk::*;
let v = vec![0, 1, 4, 6];
let data = (1..=6).collect::<Vec<_>>();
let s = Chunked::from_offsets(v.as_slice(), data.view());
assert_eq!(Some(&[1][..]), s.get(&0));
assert_eq!(Some(&[2,3,4][..]), s.get(&1));
assert_eq!(Some(&[5,6][..]), s.get(&2));
Get a [begin..end)
subview of the given Chunked
collection.
Example
use flatk::*;
let data = (1..=6).collect::<Vec<_>>();
let offsets = vec![1, 2, 5, 7]; // Offsets don't have to start at 0
let s = Chunked::from_offsets(offsets.as_slice(), data.view());
let v = s.get(1..3).unwrap();
assert_eq!(Some(&[2,3,4][..]), v.get(0));
assert_eq!(Some(&[5,6][..]), v.get(1));
Get reference to a chunk at the given index.
Note that this works for Chunked
collections that are themselves NOT Chunked
, since a
chunk of a doubly Chunked
collection is itself Chunked
, which cannot be represented by
a single borrow. For more complex indexing use the get
method provided by the Get
trait.
Example
use flatk::*;
let v = vec![1,2,3,4,5,6,7,8,9,10,11];
let s = Chunked::from_offsets(vec![0,3,4,6,9,11], v.clone());
assert_eq!(2, (&s[2]).len());
assert_eq!(&[5,6], &s[2]);
Immutably index the Chunked
borrowed slice by usize
.
Note that this works for chunked collections that are themselves not chunked, since the
item at the index of a doubly chunked collection is itself chunked, which cannot be
represented by a single borrow. For more complex indexing use the get
method provided by
the Get
trait.
Example
use flatk::*;
let v = vec![1,2,3,4,5,6,7,8,9,10,11];
let s = Chunked::from_offsets(vec![0,3,4,6,9,11], v.as_slice());
assert_eq!(&[5,6], &s[2]);
Immutably index the Chunked
mutably borrowed slice by usize
.
Note that this works for chunked collections that are themselves not chunked, since the
item at the index of a doubly chunked collection is itself chunked, which cannot be
represented by a single borrow. For more complex indexing use the get
method provided by
the Get
trait.
Example
use flatk::*;
let mut v = vec![1,2,3,4,5,6,7,8,9,10,11];
let s = Chunked::from_offsets(vec![0,3,4,6,9,11], v.as_mut_slice());
assert_eq!(&[5,6], &s[2]);
Mutably index the Chunked
Vec
by usize
.
Note that this works for chunked collections that are themselves not chunked, since the
item at the index of a doubly chunked collection is itself chunked, which cannot be
represented by a single borrow. For more complex indexing use the get
method provided by
the Get
trait.
Example
use flatk::*;
let mut v = vec![1,2,3,4,0,0,7,8,9,10,11];
let mut s = Chunked::from_offsets(vec![0,3,4,6,9,11], v.clone());
s[2].copy_from_slice(&[5,6]);
assert_eq!(vec![1,2,3,4,5,6,7,8,9,10,11], s.into_storage());
Mutably index the Chunked
mutably borrowed slice by usize
.
Note that this works for chunked collections that are themselves not
chunked, since the item at the index of a doubly chunked collection is
itself chunked, which cannot be represented by a single borrow. For more
complex indexing use the get
method provided by the Get
trait.
Example
use flatk::*;
let mut v = vec![1,2,3,4,0,0,7,8,9,10,11];
let mut s = Chunked::from_offsets(vec![0,3,4,6,9,11], v.as_mut_slice());
s[2].copy_from_slice(&[5,6]);
assert_eq!(vec![1,2,3,4,5,6,7,8,9,10,11], v);
impl<'a, S, O> IntoIterator for Chunked<S, O> where
O: IntoOffsetValuesAndSizes + GetOffset,
S: SplitAt + Set + Dummy,
O::Iter: ExactSizeIterator,
impl<'a, S, O> IntoIterator for Chunked<S, O> where
O: IntoOffsetValuesAndSizes + GetOffset,
S: SplitAt + Set + Dummy,
O::Iter: ExactSizeIterator,
Strip all organizational information from this set, returning the underlying storage type.
Example
use flatk::*;
let v = vec![1,2,3,4,5,6,7,8,9,10,11];
let s0 = Chunked::from_offsets(vec![0,3,4,6,9,11], v.clone());
let s1 = Chunked::from_offsets(vec![0,1,4,5], s0.clone());
assert_eq!(s1.into_storage(), v);
assert_eq!(s0.into_storage(), v);
type StorageType = S::StorageType
impl<S, O> IsolateIndex<Chunked<S, O>> for usize where
S: Set + Isolate<Range<usize>>,
O: IndexRange,
impl<S, O> IsolateIndex<Chunked<S, O>> for usize where
S: Set + Isolate<Range<usize>>,
O: IndexRange,
Isolate a [begin..end)
range of the given Chunked
collection.
Attempts to isolate a value in the given set at this index. Read more
Push a slice of elements onto this Chunked
.
Examples
use flatk::*;
let mut s = Chunked::<Vec<usize>>::from_offsets(vec![0,1,4], vec![0,1,2,3]);
s.push(vec![4,5]);
let v1 = s.view();
let mut view1_iter = v1.into_iter();
assert_eq!(Some(&[0][..]), view1_iter.next());
assert_eq!(Some(&[1,2,3][..]), view1_iter.next());
assert_eq!(Some(&[4,5][..]), view1_iter.next());
assert_eq!(None, view1_iter.next());
use flatk::*;
let mut s = Chunked::from_offsets(vec![0,3,5], vec![1,2,3,4,5]);
assert_eq!(2, s.len());
s.push(&[1,2]);
assert_eq!(3, s.len());
Required for subsets of chunked collections.
Remove a prefix of size n
from a chunked collection.
Example
use flatk::*;
let mut s = Chunked::<Vec<usize>>::from_offsets(vec![0,1,4,6], vec![0,1,2,3,4,5]);
s.remove_prefix(2);
let mut iter = s.iter();
assert_eq!(Some(&[4,5][..]), iter.next());
assert_eq!(None, iter.next());
Get the number of elements in a Chunked
.
Example
use flatk::*;
let s = Chunked::from_offsets(vec![0,3,4,6], vec![1,2,3,4,5,6]);
assert_eq!(3, s.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, O> SplitFirst for Chunked<S, O> where
S: Viewed + Set + SplitAt,
O: GetOffset + SplitOffsetsAt,
impl<S, O> SplitFirst for Chunked<S, O> where
S: Viewed + Set + SplitAt,
O: GetOffset + SplitOffsetsAt,
impl<S, O, N> SplitPrefix<N> for Chunked<S, O> where
S: Viewed + Set + SplitAt,
N: Unsigned,
O: GetOffset + SplitOffsetsAt,
impl<S, O, N> SplitPrefix<N> for Chunked<S, O> where
S: Viewed + Set + SplitAt,
N: Unsigned,
O: GetOffset + SplitOffsetsAt,
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];
let s0 = Chunked::from_offsets(vec![0,3,4,6,9,11], v.clone());
let s1 = Chunked::from_offsets(vec![0,1,4,5], s0.clone());
assert_eq!(s1.storage(), &v);
assert_eq!(s0.storage(), &v);
Pass through the conversion for structure type Chunked
.
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];
let mut s0 = Chunked::from_offsets(vec![0,3,4,6,9,11], v.clone());
let mut s1 = Chunked::from_offsets(vec![0,1,4,5], s0.clone());
assert_eq!(s1.storage_mut(), &mut v);
assert_eq!(s0.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];
let s0 = Chunked::from_offsets(vec![0,3,4,6,9,11], v.clone());
let s1 = Chunked::from_offsets(vec![0,1,4,5], s0.clone());
assert_eq!(s1.storage_view(), v.as_slice());
assert_eq!(s0.storage_view(), v.as_slice());
type StorageView = S::StorageView
Create a contiguous immutable (shareable) view into this set.
Example
use flatk::*;
let s = Chunked::<Vec<usize>>::from_offsets(vec![0,1,4,6], vec![0,1,2,3,4,5]);
let v1 = s.view();
let v2 = v1.clone();
let mut view1_iter = v1.clone().into_iter();
assert_eq!(Some(&[0][..]), view1_iter.next());
assert_eq!(Some(&[1,2,3][..]), view1_iter.next());
assert_eq!(Some(&[4,5][..]), view1_iter.next());
assert_eq!(None, view1_iter.next());
for (a,b) in v1.into_iter().zip(v2.into_iter()) {
assert_eq!(a,b);
}
Create a contiguous mutable (unique) view into this set.
Example
use flatk::*;
let mut s = Chunked::<Vec<usize>>::from_offsets(vec![0,1,4,6], vec![0,1,2,3,4,5]);
let mut v1 = s.view_mut();
v1.iter_mut().next().unwrap()[0] = 100;
let mut view1_iter = v1.iter();
assert_eq!(Some(&[100][..]), view1_iter.next());
assert_eq!(Some(&[1,2,3][..]), view1_iter.next());
assert_eq!(Some(&[4,5][..]), view1_iter.next());
assert_eq!(None, view1_iter.next());
type Iter = ChunkedIter<OffsetValuesAndSizes<'a>, <S as ViewMut<'a>>::Type>
Auto Trait Implementations
impl<S, O> RefUnwindSafe for Chunked<S, O> where
O: RefUnwindSafe,
S: RefUnwindSafe,
impl<S, O> UnwindSafe for Chunked<S, O> where
O: 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