use std::cmp::min; use super::plumbing::*; use super::*; use crate::math::div_round_up; /// `Chunks` is an iterator that groups elements of an underlying iterator. /// /// This struct is created by the [`chunks()`] method on [`IndexedParallelIterator`] /// /// [`chunks()`]: trait.IndexedParallelIterator.html#method.chunks /// [`IndexedParallelIterator`]: trait.IndexedParallelIterator.html #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] #[derive(Debug, Clone)] pub struct Chunks where I: IndexedParallelIterator, { size: usize, i: I, } impl Chunks where I: IndexedParallelIterator, { /// Creates a new `Chunks` iterator pub(super) fn new(i: I, size: usize) -> Self { Chunks { i, size } } } impl ParallelIterator for Chunks where I: IndexedParallelIterator, { type Item = Vec; fn drive_unindexed(self, consumer: C) -> C::Result where C: Consumer>, { bridge(self, consumer) } fn opt_len(&self) -> Option { Some(self.len()) } } impl IndexedParallelIterator for Chunks where I: IndexedParallelIterator, { fn drive(self, consumer: C) -> C::Result where C: Consumer, { bridge(self, consumer) } fn len(&self) -> usize { div_round_up(self.i.len(), self.size) } fn with_producer(self, callback: CB) -> CB::Output where CB: ProducerCallback, { let len = self.i.len(); return self.i.with_producer(Callback { size: self.size, len, callback, }); struct Callback { size: usize, len: usize, callback: CB, } impl ProducerCallback for Callback where CB: ProducerCallback>, { type Output = CB::Output; fn callback

(self, base: P) -> CB::Output where P: Producer, { let producer = ChunkProducer::new(self.size, self.len, base, Vec::from_iter); self.callback.callback(producer) } } } } pub(super) struct ChunkProducer { chunk_size: usize, len: usize, base: P, map: F, } impl ChunkProducer { pub(super) fn new(chunk_size: usize, len: usize, base: P, map: F) -> Self { Self { chunk_size, len, base, map, } } } impl Producer for ChunkProducer where P: Producer, F: Fn(P::IntoIter) -> T + Send + Clone, { type Item = T; type IntoIter = std::iter::Map, F>; fn into_iter(self) -> Self::IntoIter { let chunks = ChunkSeq { chunk_size: self.chunk_size, len: self.len, inner: if self.len > 0 { Some(self.base) } else { None }, }; chunks.map(self.map) } fn split_at(self, index: usize) -> (Self, Self) { let elem_index = min(index * self.chunk_size, self.len); let (left, right) = self.base.split_at(elem_index); ( ChunkProducer { chunk_size: self.chunk_size, len: elem_index, base: left, map: self.map.clone(), }, ChunkProducer { chunk_size: self.chunk_size, len: self.len - elem_index, base: right, map: self.map, }, ) } fn min_len(&self) -> usize { div_round_up(self.base.min_len(), self.chunk_size) } fn max_len(&self) -> usize { self.base.max_len() / self.chunk_size } } pub(super) struct ChunkSeq

{ chunk_size: usize, len: usize, inner: Option

, } impl

Iterator for ChunkSeq

where P: Producer, { type Item = P::IntoIter; fn next(&mut self) -> Option { let producer = self.inner.take()?; if self.len > self.chunk_size { let (left, right) = producer.split_at(self.chunk_size); self.inner = Some(right); self.len -= self.chunk_size; Some(left.into_iter()) } else { debug_assert!(self.len > 0); self.len = 0; Some(producer.into_iter()) } } fn size_hint(&self) -> (usize, Option) { let len = self.len(); (len, Some(len)) } } impl

ExactSizeIterator for ChunkSeq

where P: Producer, { #[inline] fn len(&self) -> usize { div_round_up(self.len, self.chunk_size) } } impl

DoubleEndedIterator for ChunkSeq

where P: Producer, { fn next_back(&mut self) -> Option { let producer = self.inner.take()?; if self.len > self.chunk_size { let mut size = self.len % self.chunk_size; if size == 0 { size = self.chunk_size; } let (left, right) = producer.split_at(self.len - size); self.inner = Some(left); self.len -= size; Some(right.into_iter()) } else { debug_assert!(self.len > 0); self.len = 0; Some(producer.into_iter()) } } }