aboutsummaryrefslogtreecommitdiff
path: root/src/iter/flat_map.rs
diff options
context:
space:
mode:
authorJakub Kotur <qtr@google.com>2021-03-16 20:22:30 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-03-16 20:22:30 +0000
commit4c0b8e0ef78bcd80a4facc34c6a2158d83f96802 (patch)
tree13641ab7afc7aa43b586606c18d53084dedf7ace /src/iter/flat_map.rs
parenta679e9b8b7e4ae27a19b81f216e375ea8a9cdb8e (diff)
parent7522a9ba008ac88224e0990932bdd298a43a2336 (diff)
downloadrayon-4c0b8e0ef78bcd80a4facc34c6a2158d83f96802.tar.gz
Initial import of rayon-1.5.0. am: 041839ceab am: d836dd6404 am: 7522a9ba00
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/rayon/+/1622436 Change-Id: I94b0e06d9da131d6f98db436ad1a178dfff2ec90
Diffstat (limited to 'src/iter/flat_map.rs')
-rw-r--r--src/iter/flat_map.rs154
1 files changed, 154 insertions, 0 deletions
diff --git a/src/iter/flat_map.rs b/src/iter/flat_map.rs
new file mode 100644
index 0000000..f264e1e
--- /dev/null
+++ b/src/iter/flat_map.rs
@@ -0,0 +1,154 @@
+use super::plumbing::*;
+use super::*;
+
+use std::fmt::{self, Debug};
+
+/// `FlatMap` maps each element to a parallel iterator, then flattens these iterators together.
+/// This struct is created by the [`flat_map()`] method on [`ParallelIterator`]
+///
+/// [`flat_map()`]: trait.ParallelIterator.html#method.flat_map
+/// [`ParallelIterator`]: trait.ParallelIterator.html
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+#[derive(Clone)]
+pub struct FlatMap<I: ParallelIterator, F> {
+ base: I,
+ map_op: F,
+}
+
+impl<I: ParallelIterator + Debug, F> Debug for FlatMap<I, F> {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ f.debug_struct("FlatMap").field("base", &self.base).finish()
+ }
+}
+
+impl<I: ParallelIterator, F> FlatMap<I, F> {
+ /// Creates a new `FlatMap` iterator.
+ pub(super) fn new(base: I, map_op: F) -> Self {
+ FlatMap { base, map_op }
+ }
+}
+
+impl<I, F, PI> ParallelIterator for FlatMap<I, F>
+where
+ I: ParallelIterator,
+ F: Fn(I::Item) -> PI + Sync + Send,
+ PI: IntoParallelIterator,
+{
+ type Item = PI::Item;
+
+ fn drive_unindexed<C>(self, consumer: C) -> C::Result
+ where
+ C: UnindexedConsumer<Self::Item>,
+ {
+ let consumer = FlatMapConsumer::new(consumer, &self.map_op);
+ self.base.drive_unindexed(consumer)
+ }
+}
+
+/// ////////////////////////////////////////////////////////////////////////
+/// Consumer implementation
+
+struct FlatMapConsumer<'f, C, F> {
+ base: C,
+ map_op: &'f F,
+}
+
+impl<'f, C, F> FlatMapConsumer<'f, C, F> {
+ fn new(base: C, map_op: &'f F) -> Self {
+ FlatMapConsumer { base, map_op }
+ }
+}
+
+impl<'f, T, U, C, F> Consumer<T> for FlatMapConsumer<'f, C, F>
+where
+ C: UnindexedConsumer<U::Item>,
+ F: Fn(T) -> U + Sync,
+ U: IntoParallelIterator,
+{
+ type Folder = FlatMapFolder<'f, C, F, C::Result>;
+ type Reducer = C::Reducer;
+ type Result = C::Result;
+
+ fn split_at(self, index: usize) -> (Self, Self, C::Reducer) {
+ let (left, right, reducer) = self.base.split_at(index);
+ (
+ FlatMapConsumer::new(left, self.map_op),
+ FlatMapConsumer::new(right, self.map_op),
+ reducer,
+ )
+ }
+
+ fn into_folder(self) -> Self::Folder {
+ FlatMapFolder {
+ base: self.base,
+ map_op: self.map_op,
+ previous: None,
+ }
+ }
+
+ fn full(&self) -> bool {
+ self.base.full()
+ }
+}
+
+impl<'f, T, U, C, F> UnindexedConsumer<T> for FlatMapConsumer<'f, C, F>
+where
+ C: UnindexedConsumer<U::Item>,
+ F: Fn(T) -> U + Sync,
+ U: IntoParallelIterator,
+{
+ fn split_off_left(&self) -> Self {
+ FlatMapConsumer::new(self.base.split_off_left(), self.map_op)
+ }
+
+ fn to_reducer(&self) -> Self::Reducer {
+ self.base.to_reducer()
+ }
+}
+
+struct FlatMapFolder<'f, C, F, R> {
+ base: C,
+ map_op: &'f F,
+ previous: Option<R>,
+}
+
+impl<'f, T, U, C, F> Folder<T> for FlatMapFolder<'f, C, F, C::Result>
+where
+ C: UnindexedConsumer<U::Item>,
+ F: Fn(T) -> U + Sync,
+ U: IntoParallelIterator,
+{
+ type Result = C::Result;
+
+ fn consume(self, item: T) -> Self {
+ let map_op = self.map_op;
+ let par_iter = map_op(item).into_par_iter();
+ let consumer = self.base.split_off_left();
+ let result = par_iter.drive_unindexed(consumer);
+
+ let previous = match self.previous {
+ None => Some(result),
+ Some(previous) => {
+ let reducer = self.base.to_reducer();
+ Some(reducer.reduce(previous, result))
+ }
+ };
+
+ FlatMapFolder {
+ base: self.base,
+ map_op,
+ previous,
+ }
+ }
+
+ fn complete(self) -> Self::Result {
+ match self.previous {
+ Some(previous) => previous,
+ None => self.base.into_folder().complete(),
+ }
+ }
+
+ fn full(&self) -> bool {
+ self.base.full()
+ }
+}