rayon/iter/
positions.rs

1use super::plumbing::*;
2use super::*;
3
4use std::fmt::{self, Debug};
5
6/// `Positions` takes a predicate `predicate` and filters out elements that match,
7/// yielding their indices.
8///
9/// This struct is created by the [`positions()`] method on [`IndexedParallelIterator`]
10///
11/// [`positions()`]: IndexedParallelIterator::positions()
12#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
13#[derive(Clone)]
14pub struct Positions<I, P> {
15    base: I,
16    predicate: P,
17}
18
19impl<I: Debug, P> Debug for Positions<I, P> {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        f.debug_struct("Positions")
22            .field("base", &self.base)
23            .finish()
24    }
25}
26
27impl<I, P> Positions<I, P> {
28    /// Create a new `Positions` iterator.
29    pub(super) fn new(base: I, predicate: P) -> Self {
30        Positions { base, predicate }
31    }
32}
33
34impl<I, P> ParallelIterator for Positions<I, P>
35where
36    I: IndexedParallelIterator,
37    P: Fn(I::Item) -> bool + Sync + Send,
38{
39    type Item = usize;
40
41    fn drive_unindexed<C>(self, consumer: C) -> C::Result
42    where
43        C: UnindexedConsumer<Self::Item>,
44    {
45        let consumer1 = PositionsConsumer::new(consumer, &self.predicate, 0);
46        self.base.drive(consumer1)
47    }
48}
49
50// ////////////////////////////////////////////////////////////////////////
51// Consumer implementation
52
53struct PositionsConsumer<'p, C, P> {
54    base: C,
55    predicate: &'p P,
56    offset: usize,
57}
58
59impl<'p, C, P> PositionsConsumer<'p, C, P> {
60    fn new(base: C, predicate: &'p P, offset: usize) -> Self {
61        PositionsConsumer {
62            base,
63            predicate,
64            offset,
65        }
66    }
67}
68
69impl<'p, T, C, P> Consumer<T> for PositionsConsumer<'p, C, P>
70where
71    C: Consumer<usize>,
72    P: Fn(T) -> bool + Sync,
73{
74    type Folder = PositionsFolder<'p, C::Folder, P>;
75    type Reducer = C::Reducer;
76    type Result = C::Result;
77
78    fn split_at(self, index: usize) -> (Self, Self, C::Reducer) {
79        let (left, right, reducer) = self.base.split_at(index);
80        (
81            PositionsConsumer::new(left, self.predicate, self.offset),
82            PositionsConsumer::new(right, self.predicate, self.offset + index),
83            reducer,
84        )
85    }
86
87    fn into_folder(self) -> Self::Folder {
88        PositionsFolder {
89            base: self.base.into_folder(),
90            predicate: self.predicate,
91            offset: self.offset,
92        }
93    }
94
95    fn full(&self) -> bool {
96        self.base.full()
97    }
98}
99
100struct PositionsFolder<'p, F, P> {
101    base: F,
102    predicate: &'p P,
103    offset: usize,
104}
105
106impl<F, P, T> Folder<T> for PositionsFolder<'_, F, P>
107where
108    F: Folder<usize>,
109    P: Fn(T) -> bool,
110{
111    type Result = F::Result;
112
113    fn consume(mut self, item: T) -> Self {
114        let index = self.offset;
115        self.offset += 1;
116        if (self.predicate)(item) {
117            self.base = self.base.consume(index);
118        }
119        self
120    }
121
122    // This cannot easily specialize `consume_iter` to be better than
123    // the default, because that requires checking `self.base.full()`
124    // during a call to `self.base.consume_iter()`. (#632)
125
126    fn complete(self) -> Self::Result {
127        self.base.complete()
128    }
129
130    fn full(&self) -> bool {
131        self.base.full()
132    }
133}