// Note: this implementation is very similar to `SplitProducer`. impl<T, Slice, Pred> UnindexedProducer for ChunkByProducer<'_, T, Slice, Pred> where
Slice: ChunkBySlice<T>,
Pred: Fn(&T, &T) -> bool + Send + Sync,
{ type Item = Slice;
// Look forward for the separator, and failing that look backward. let mid = self.tail / 2; let index = matchself.slice.find(self.pred, mid, self.tail) {
Some(i) => Some(mid + i),
None => self.slice.rfind(self.pred, mid + 1),
};
iflet Some(index) = index { let (left, right) = self.slice.split(index);
let (left_tail, right_tail) = if index <= mid { // If we scanned backwards to find the separator, everything in // the right side is exhausted, with no separators left to find.
(index, 0)
} else {
(mid + 1, self.tail - index)
};
// Create the left split before the separator. let left = Self {
slice: left,
tail: left_tail,
..self
};
// Create the right split following the separator. let right = Self {
slice: right,
tail: right_tail,
..self
};
(left, Some(right))
} else { // The search is exhausted, no more separators...
(Self { tail: 0, ..self }, None)
}
}
fn fold_with<F>(self, mut folder: F) -> F where
F: Folder<Self::Item>,
{ letSelf {
slice, pred, tail, ..
} = self;
let (slice, tail) = if tail == slice.as_ref().len() { // No tail section, so just let `consume_iter` do it all.
(Some(slice), None)
} elseiflet Some(index) = slice.rfind(pred, tail) { // We found the last separator to complete the tail, so // end with that slice after `consume_iter` finds the rest. let (left, right) = slice.split(index);
(Some(left), Some(right))
} else { // We know there are no separators at all, so it's all "tail".
(None, Some(slice))
};
iflet Some(mut slice) = slice { // TODO (MSRV 1.77) use either: // folder.consume_iter(slice.chunk_by(pred)) // folder.consume_iter(slice.chunk_by_mut(pred))
folder = folder.consume_iter(std::iter::from_fn(move || { let len = slice.as_ref().len(); if len > 0 { let i = slice.find(pred, 0, len).unwrap_or(len); let (head, tail) = mem::take(&mut slice).split(i);
slice = tail;
Some(head)
} else {
None
}
}));
}
/// Parallel iterator over slice in (non-overlapping) chunks separated by a predicate. /// /// This struct is created by the [`par_chunk_by`] method on `&[T]`. /// /// [`par_chunk_by`]: trait.ParallelSlice.html#method.par_chunk_by pubstruct ChunkBy<'data, T, P> {
pred: P,
slice: &'data [T],
}
/// Parallel iterator over slice in (non-overlapping) mutable chunks /// separated by a predicate. /// /// This struct is created by the [`par_chunk_by_mut`] method on `&mut [T]`. /// /// [`par_chunk_by_mut`]: trait.ParallelSliceMut.html#method.par_chunk_by_mut pubstruct ChunkByMut<'data, T, P> {
pred: P,
slice: &'data mut [T],
}
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.