|
1 | 1 | use super::plumbing::*;
|
2 | 2 | use super::*;
|
3 | 3 |
|
4 |
| -use std::cmp; |
5 |
| - |
6 | 4 | /// `MultiZip` is an iterator that zips up a tuple of parallel iterators to
|
7 | 5 | /// produce tuples of their items.
|
8 | 6 | ///
|
@@ -92,44 +90,36 @@ pub struct MultiZip<T> {
|
92 | 90 | //
|
93 | 91 | // But if we ever increase to 13, we would want to split 8,5 rather than 4,9.
|
94 | 92 |
|
95 |
| -macro_rules! zip { |
96 |
| - ($a:expr, $b:expr, $c:expr, $d:expr, $( $x:expr, )+) => { |
97 |
| - zip!(zip!($a, $b, $c, $d,), zip!($( $x, )+),) |
98 |
| - }; |
99 |
| - ($a:expr, $b:expr, $( $x:expr, )+) => { |
100 |
| - zip!(zip!($a, $b,), zip!($( $x, )+),) |
101 |
| - }; |
102 |
| - ($a:expr, $( $x:expr, )*) => { |
103 |
| - $a $( .zip($x) )* |
104 |
| - }; |
105 |
| -} |
106 |
| - |
107 |
| -macro_rules! min { |
108 |
| - ($a:expr, $b:expr, $c:expr, $d:expr, $( $x:expr, )+) => { |
109 |
| - min!(min!($a, $b, $c, $d,), min!($( $x, )+),) |
| 93 | +macro_rules! reduce { |
| 94 | + ($a:expr, $b:expr, $c:expr, $d:expr, $( $x:expr ),+ => $fn:path) => { |
| 95 | + reduce!(reduce!($a, $b, $c, $d => $fn), |
| 96 | + reduce!($( $x ),+ => $fn) |
| 97 | + => $fn) |
110 | 98 | };
|
111 |
| - ($a:expr, $b:expr, $( $x:expr, )+) => { |
112 |
| - min!(min!($a, $b,), min!($( $x, )+),) |
| 99 | + ($a:expr, $b:expr, $( $x:expr ),+ => $fn:path) => { |
| 100 | + reduce!(reduce!($a, $b => $fn), |
| 101 | + reduce!($( $x ),+ => $fn) |
| 102 | + => $fn) |
113 | 103 | };
|
114 |
| - ($a:expr, $b:expr,) => { cmp::min($a, $b) }; |
115 |
| - ($a:expr,) => { $a }; |
| 104 | + ($a:expr, $b:expr => $fn:path) => { $fn($a, $b) }; |
| 105 | + ($a:expr => $fn:path) => { $a }; |
116 | 106 | }
|
117 | 107 |
|
118 | 108 | macro_rules! nest {
|
119 |
| - ($A:tt, $B:tt, $C:tt, $D:tt, $( $X:tt, )+) => { |
120 |
| - (nest!($A, $B, $C, $D,), nest!($( $X, )+)) |
| 109 | + ($A:tt, $B:tt, $C:tt, $D:tt, $( $X:tt ),+) => { |
| 110 | + (nest!($A, $B, $C, $D), nest!($( $X ),+)) |
121 | 111 | };
|
122 |
| - ($A:tt, $B:tt, $( $X:tt, )+) => { |
123 |
| - (($A, $B), nest!($( $X, )+)) |
| 112 | + ($A:tt, $B:tt, $( $X:tt ),+) => { |
| 113 | + (($A, $B), nest!($( $X ),+)) |
124 | 114 | };
|
125 |
| - ($A:tt, $B:tt,) => { ($A, $B) }; |
126 |
| - ($A:tt,) => { $A }; |
| 115 | + ($A:tt, $B:tt) => { ($A, $B) }; |
| 116 | + ($A:tt) => { $A }; |
127 | 117 | }
|
128 | 118 |
|
129 | 119 | macro_rules! flatten {
|
130 |
| - ($( $T:ident, )+) => {{ |
| 120 | + ($( $T:ident ),+) => {{ |
131 | 121 | #[allow(non_snake_case)]
|
132 |
| - fn flatten<$( $T ),+>(nest!($( $T, )+) : nest!($( $T, )+)) -> ($( $T, )+) { |
| 122 | + fn flatten<$( $T ),+>(nest!($( $T ),+) : nest!($( $T ),+)) -> ($( $T, )+) { |
133 | 123 | ($( $T, )+)
|
134 | 124 | }
|
135 | 125 | flatten
|
@@ -220,21 +210,21 @@ macro_rules! multizip_impls {
|
220 | 210 | where
|
221 | 211 | CONSUMER: Consumer<Self::Item>,
|
222 | 212 | {
|
223 |
| - zip!($( self.tuple.$idx, )+) |
224 |
| - .map(flatten!($( $T, )+)) |
| 213 | + reduce!($( self.tuple.$idx ),+ => IndexedParallelIterator::zip) |
| 214 | + .map(flatten!($( $T ),+)) |
225 | 215 | .drive(consumer)
|
226 | 216 | }
|
227 | 217 |
|
228 | 218 | fn len(&self) -> usize {
|
229 |
| - min!($( self.tuple.$idx.len(), )+) |
| 219 | + reduce!($( self.tuple.$idx.len() ),+ => Ord::min) |
230 | 220 | }
|
231 | 221 |
|
232 | 222 | fn with_producer<CB>(self, callback: CB) -> CB::Output
|
233 | 223 | where
|
234 | 224 | CB: ProducerCallback<Self::Item>,
|
235 | 225 | {
|
236 |
| - zip!($( self.tuple.$idx, )+) |
237 |
| - .map(flatten!($( $T, )+)) |
| 226 | + reduce!($( self.tuple.$idx ),+ => IndexedParallelIterator::zip) |
| 227 | + .map(flatten!($( $T ),+)) |
238 | 228 | .with_producer(callback)
|
239 | 229 | }
|
240 | 230 | }
|
|
0 commit comments