/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/positions.rs
Line | Count | Source |
1 | | use super::plumbing::*; |
2 | | use super::*; |
3 | | |
4 | | use 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)] |
14 | | pub struct Positions<I, P> { |
15 | | base: I, |
16 | | predicate: P, |
17 | | } |
18 | | |
19 | | impl<I: Debug, P> Debug for Positions<I, P> { |
20 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
21 | 0 | f.debug_struct("Positions") |
22 | 0 | .field("base", &self.base) |
23 | 0 | .finish() |
24 | 0 | } |
25 | | } |
26 | | |
27 | | impl<I, P> Positions<I, P> { |
28 | | /// Create a new `Positions` iterator. |
29 | 0 | pub(super) fn new(base: I, predicate: P) -> Self { |
30 | 0 | Positions { base, predicate } |
31 | 0 | } |
32 | | } |
33 | | |
34 | | impl<I, P> ParallelIterator for Positions<I, P> |
35 | | where |
36 | | I: IndexedParallelIterator, |
37 | | P: Fn(I::Item) -> bool + Sync + Send, |
38 | | { |
39 | | type Item = usize; |
40 | | |
41 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
42 | 0 | where |
43 | 0 | C: UnindexedConsumer<Self::Item>, |
44 | | { |
45 | 0 | let consumer1 = PositionsConsumer::new(consumer, &self.predicate, 0); |
46 | 0 | self.base.drive(consumer1) |
47 | 0 | } |
48 | | } |
49 | | |
50 | | // //////////////////////////////////////////////////////////////////////// |
51 | | // Consumer implementation |
52 | | |
53 | | struct PositionsConsumer<'p, C, P> { |
54 | | base: C, |
55 | | predicate: &'p P, |
56 | | offset: usize, |
57 | | } |
58 | | |
59 | | impl<'p, C, P> PositionsConsumer<'p, C, P> { |
60 | 0 | fn new(base: C, predicate: &'p P, offset: usize) -> Self { |
61 | 0 | PositionsConsumer { |
62 | 0 | base, |
63 | 0 | predicate, |
64 | 0 | offset, |
65 | 0 | } |
66 | 0 | } |
67 | | } |
68 | | |
69 | | impl<'p, T, C, P> Consumer<T> for PositionsConsumer<'p, C, P> |
70 | | where |
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 | 0 | fn split_at(self, index: usize) -> (Self, Self, C::Reducer) { |
79 | 0 | let (left, right, reducer) = self.base.split_at(index); |
80 | 0 | ( |
81 | 0 | PositionsConsumer::new(left, self.predicate, self.offset), |
82 | 0 | PositionsConsumer::new(right, self.predicate, self.offset + index), |
83 | 0 | reducer, |
84 | 0 | ) |
85 | 0 | } |
86 | | |
87 | 0 | fn into_folder(self) -> Self::Folder { |
88 | 0 | PositionsFolder { |
89 | 0 | base: self.base.into_folder(), |
90 | 0 | predicate: self.predicate, |
91 | 0 | offset: self.offset, |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | 0 | fn full(&self) -> bool { |
96 | 0 | self.base.full() |
97 | 0 | } |
98 | | } |
99 | | |
100 | | struct PositionsFolder<'p, F, P> { |
101 | | base: F, |
102 | | predicate: &'p P, |
103 | | offset: usize, |
104 | | } |
105 | | |
106 | | impl<F, P, T> Folder<T> for PositionsFolder<'_, F, P> |
107 | | where |
108 | | F: Folder<usize>, |
109 | | P: Fn(T) -> bool, |
110 | | { |
111 | | type Result = F::Result; |
112 | | |
113 | 0 | fn consume(mut self, item: T) -> Self { |
114 | 0 | let index = self.offset; |
115 | 0 | self.offset += 1; |
116 | 0 | if (self.predicate)(item) { |
117 | 0 | self.base = self.base.consume(index); |
118 | 0 | } |
119 | 0 | self |
120 | 0 | } |
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 | 0 | fn complete(self) -> Self::Result { |
127 | 0 | self.base.complete() |
128 | 0 | } |
129 | | |
130 | 0 | fn full(&self) -> bool { |
131 | 0 | self.base.full() |
132 | 0 | } |
133 | | } |