/// `FoldChunksWith` is an iterator that groups elements of an underlying iterator and applies a /// function over them, producing a single value for each group. /// /// This struct is created by the [`fold_chunks_with()`] method on [`IndexedParallelIterator`] /// /// [`fold_chunks_with()`]: trait.IndexedParallelIterator.html#method.fold_chunks /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Clone)] pubstruct FoldChunksWith<I, U, F> where
I: IndexedParallelIterator,
{
base: I,
chunk_size: usize,
item: U,
fold_op: F,
}
impl<I, U, F> FoldChunksWith<I, U, F> where
I: IndexedParallelIterator,
U: Send + Clone,
F: Fn(U, I::Item) -> U + Send + Sync,
{ /// Creates a new `FoldChunksWith` iterator pub(super) fn new(base: I, chunk_size: usize, item: U, fold_op: F) -> Self {
FoldChunksWith {
base,
chunk_size,
item,
fold_op,
}
}
}
impl<I, U, F> ParallelIterator for FoldChunksWith<I, U, F> where
I: IndexedParallelIterator,
U: Send + Clone,
F: Fn(U, I::Item) -> U + Send + Sync,
{ type Item = U;
fn drive_unindexed<C>(self, consumer: C) -> C::Result where
C: Consumer<U>,
{
bridge(self, consumer)
}
impl<T, U, F, CB> ProducerCallback<T> for Callback<CB, U, F> where
CB: ProducerCallback<U>,
U: Send + Clone,
F: Fn(U, T) -> U + Send + Sync,
{ type Output = CB::Output;
fn callback<P>(self, base: P) -> CB::Output where
P: Producer<Item = T>,
{ let item = self.item; let fold_op = &self.fold_op; let fold_iter = move |iter: P::IntoIter| iter.fold(item.clone(), fold_op); let producer = ChunkProducer::new(self.chunk_size, self.len, base, fold_iter); self.callback.callback(producer)
}
}
}
}
#[cfg(test)] mod test { usesuper::*; use std::ops::Add;
#[test] fn check_fold_chunks_with() { let words = "bishbashbosh!"
.chars()
.collect::<Vec<_>>()
.into_par_iter()
.fold_chunks_with(4, String::new(), |mut s, c| {
s.push(c);
s
})
.collect::<Vec<_>>();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.