orx_parallel/generic_iterator/
reduce.rs1use 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 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 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 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 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 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 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 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 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 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 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}