orx_parallel/generic_iterator/
reduce.rs

1use super::iter::GenericIterator;
2use crate::ParIter;
3use core::cmp::Ordering;
4
5impl<T, S, R, O> GenericIterator<T, S, R, O>
6where
7    T: Send + Sync,
8    S: Iterator<Item = T>,
9    R: rayon::iter::ParallelIterator<Item = T>,
10    O: ParIter<Item = T>,
11{
12    /// Reduction for the generic iterator.
13    ///
14    /// See [`reduce`] for details.
15    ///
16    /// [`reduce`]: crate::ParIter::reduce
17    pub fn reduce<Reduce>(self, reduce: Reduce) -> Option<T>
18    where
19        Reduce: Fn(T, T) -> T + Send + Sync,
20    {
21        match self {
22            GenericIterator::Sequential(x) => x.reduce(reduce),
23            GenericIterator::Rayon(x) => x.reduce_with(reduce),
24            GenericIterator::Orx(x) => x.reduce(reduce),
25        }
26    }
27
28    /// Count reduction for the generic iterator.
29    ///
30    /// See [`count`] for details.
31    ///
32    /// [`count`]: crate::ParIter::count
33    pub fn count(self) -> usize {
34        match self {
35            GenericIterator::Sequential(x) => x.count(),
36            GenericIterator::Rayon(x) => x.count(),
37            GenericIterator::Orx(x) => x.count(),
38        }
39    }
40
41    /// For-each iteration for the generic iterator.
42    ///
43    /// See [`for_each`] for details.
44    ///
45    /// [`for_each`]: crate::ParIter::for_each
46    pub fn for_each<Operation>(self, operation: Operation)
47    where
48        Operation: Fn(T) + Sync + Send,
49    {
50        match self {
51            GenericIterator::Sequential(x) => x.for_each(operation),
52            GenericIterator::Rayon(x) => x.for_each(operation),
53            GenericIterator::Orx(x) => x.for_each(operation),
54        }
55    }
56
57    /// Max reduction for the generic iterator.
58    ///
59    /// See [`max`] for details.
60    ///
61    /// [`max`]: crate::ParIter::max
62    pub fn max(self) -> Option<T>
63    where
64        T: Ord,
65    {
66        match self {
67            GenericIterator::Sequential(x) => x.max(),
68            GenericIterator::Rayon(x) => x.max(),
69            GenericIterator::Orx(x) => x.max(),
70        }
71    }
72
73    /// Max-by reduction for the generic iterator.
74    ///
75    /// See [`max_by`] for details.
76    ///
77    /// [`max_by`]: crate::ParIter::max_by
78    pub fn max_by<Compare>(self, compare: Compare) -> Option<T>
79    where
80        Compare: Fn(&T, &T) -> Ordering + Sync + Send,
81    {
82        match self {
83            GenericIterator::Sequential(x) => x.max_by(compare),
84            GenericIterator::Rayon(x) => x.max_by(compare),
85            GenericIterator::Orx(x) => x.max_by(compare),
86        }
87    }
88
89    /// Max-by-key reduction for the generic iterator.
90    ///
91    /// See [`max_by_key`] for details.
92    ///
93    /// [`max_by_key`]: crate::ParIter::max_by_key
94    pub fn max_by_key<Key, GetKey>(self, key: GetKey) -> Option<T>
95    where
96        Key: Ord + Send,
97        GetKey: Fn(&T) -> Key + Sync + Send,
98    {
99        match self {
100            GenericIterator::Sequential(x) => x.max_by_key(key),
101            GenericIterator::Rayon(x) => x.max_by_key(key),
102            GenericIterator::Orx(x) => x.max_by_key(key),
103        }
104    }
105
106    /// Min reduction for the generic iterator.
107    ///
108    /// See [`min`] for details.
109    ///
110    /// [`min`]: crate::ParIter::min
111    pub fn min(self) -> Option<T>
112    where
113        T: Ord,
114    {
115        match self {
116            GenericIterator::Sequential(x) => x.min(),
117            GenericIterator::Rayon(x) => x.min(),
118            GenericIterator::Orx(x) => x.min(),
119        }
120    }
121
122    /// Min-by reduction for the generic iterator.
123    ///
124    /// See [`min_by`] for details.
125    ///
126    /// [`min_by`]: crate::ParIter::min_by
127    pub fn min_by<Compare>(self, compare: Compare) -> Option<T>
128    where
129        Compare: Fn(&T, &T) -> Ordering + Sync + Send,
130    {
131        match self {
132            GenericIterator::Sequential(x) => x.min_by(compare),
133            GenericIterator::Rayon(x) => x.min_by(compare),
134            GenericIterator::Orx(x) => x.min_by(compare),
135        }
136    }
137
138    /// Min-by-key reduction for the generic iterator.
139    ///
140    /// See [`min_by_key`] for details.
141    ///
142    /// [`min_by_key`]: crate::ParIter::min_by_key
143    pub fn min_by_key<Key, GetKey>(self, key: GetKey) -> Option<T>
144    where
145        Key: Ord + Send,
146        GetKey: Fn(&T) -> Key + Sync + Send,
147    {
148        match self {
149            GenericIterator::Sequential(x) => x.min_by_key(key),
150            GenericIterator::Rayon(x) => x.min_by_key(key),
151            GenericIterator::Orx(x) => x.min_by_key(key),
152        }
153    }
154
155    /// Sum reduction for the generic iterator.
156    ///
157    /// See [`sum`] for details.
158    ///
159    /// [`sum`]: crate::ParIter::sum
160    pub fn sum(self) -> T
161    where
162        T: crate::special_type_sets::Sum<T> + core::iter::Sum<T>,
163    {
164        match self {
165            GenericIterator::Sequential(x) => x.sum(),
166            GenericIterator::Rayon(x) => x.sum(),
167            GenericIterator::Orx(x) => x.sum(),
168        }
169    }
170}