1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use std::iter::Zip;

/*
 * zip2
 */
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Zip2<A, B> {
    subzip: Zip<A, B>
}
impl<A, B> Iterator for Zip2<A, B> where
A: Iterator,
B: Iterator
{
    type Item = (A::Item, B::Item);

    #[inline]
    fn next(&mut self) -> Option<(A::Item, B::Item)> {
        self.subzip.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.subzip.size_hint()
    }
}
pub fn zip2<U, V>(u: U, v: V) -> Zip2<U::IntoIter, V::IntoIter> where
    U: IntoIterator,
    V: IntoIterator,
{
    Zip2 {
        subzip: u.into_iter().zip(v.into_iter())
    }
}

/*
 * The generating macro is honestly hard to read, so if you feel like you really
 * need to dive into it, here is an example of what it generates. This is the
 * implementation of zip4.
 *
 * As you can see, it is based on a struct Zip4<A, B, C, D> which acts as the
 * Zip struct in Rust's stdlib. It recursively builds on the previous version,
 * (here: Zip3) and uses Zip to store both a Zip3 + another element, which makes
 * 4 of them in total.
 */
// #[derive(Clone)]
// #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
// pub struct Zip4<A, B, C, D> {
//     subzip: Zip<Zip3<A, B, C>, D>
// }
// impl<A, B, C, D> Iterator for Zip4<A, B, C, D> where
//     A: Iterator,
//     B: Iterator,
//     C: Iterator,
//     D: Iterator,
// {
//     type Item = (A::Item, B::Item, C::Item, D::Item);
//
//     #[inline]
//     fn next(&mut self) -> Option<(A::Item, B::Item, C::Item, D::Item)> {
//         self.subzip.next().map(|((a, b, c), d)| (a, b, c, d))
//     }
//
//     #[inline]
//     fn size_hint(&self) -> (usize, Option<usize>) {
//         self.subzip.size_hint()
//     }
// }
//
// pub fn zip4<IA, IB, IC, ID>(ia: IA, ib: IB, ic: IC, id: ID)
//     -> Zip4<IA::IntoIter, IB::IntoIter, IC::IntoIter, ID::IntoIter> where
//     IA: IntoIterator,
//     IB: IntoIterator,
//     IC: IntoIterator,
//     ID: IntoIterator,
// {
//     Zip4 {
//         subzip: zip3(ia, ib, ic).zip(id.into_iter())
//     }
// }

// This macro will create the function `$zipn`, building on the previous stage
// `$zipnprev`. It needs a lot of arguments which are:
//   * $ZipN: the current ZipN struct
//   * $zipn: the current zipn function
//   * $ZipNPrev: the previous ZipN struct (NPrev == N-1)
//   * $zipnprev: the previous zipn function (nprev = n-1)
//   * $A: loops over A, B, C... type of the item zipped over
//   * $a: loops over a, b, c... name of a variable for the type A, B, C...
//   * $IA: loops over IA, IB, IC... type of the Iterator for A
//   * $ia: loops over ia, ib, ic... name of a variable for the type IA, IB, IC...
//   * $ALast: like $A but the last one. For example, it is D in Zip4<A, B, C, D>
//   * $alast: name of a variable for the type $ALast (ex: d if $ALast is D)
//   * $IALast: like $IA but the last one. For example, it is ID in Zip4.
//   * $ialast: name of a variable for the type $IALast (ex: id if $IALast is ID)
//
macro_rules! impl_zipn {
    ($ZipN:ident $zipn:ident $ZipNPrev:ident $zipnprev:ident ( $($A:ident $a:ident $IA:ident $ia:ident)+ ) $ALast:ident $alast:ident $IALast:ident $ialast:ident) => (
        #[derive(Clone)]
        #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
        pub struct $ZipN<$($A),*, $ALast> {
            subzip: Zip<$ZipNPrev<$($A),*>, $ALast>
        }
        impl<$($A),*, $ALast> Iterator for $ZipN<$($A),*, $ALast> where
            $($A: Iterator),*,
            $ALast: Iterator
        {
            type Item = ($($A::Item),*, $ALast::Item);

            #[inline]
            fn next(&mut self) -> Option<($($A::Item),*, $ALast::Item)> {
                self.subzip.next().map(|(( $($a),* ), $alast)| ($($a),*, $alast))
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                self.subzip.size_hint()
            }
        }
        pub fn $zipn<$($IA),*, $IALast>($($ia: $IA),*, $ialast: $IALast)
           -> $ZipN<$($IA::IntoIter),*, $IALast::IntoIter>
        where
            $($IA: IntoIterator),*,
            $IALast: IntoIterator
        {
            $ZipN {
                subzip: $zipnprev($($ia),*).zip($ialast.into_iter())
            }
        }
    );
}

impl_zipn!(Zip3 zip3 Zip2 zip2
           (A a IA ia
            B b IB ib)
           C c IC ic);
impl_zipn!(Zip4 zip4 Zip3 zip3
           (A a IA ia
            B b IB ib
            C c IC ic)
           D d ID id);
impl_zipn!(Zip5 zip5 Zip4 zip4
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id)
           E e IE ie);
impl_zipn!(Zip6 zip6 Zip5 zip5
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie)
           F f IF iff);
impl_zipn!(Zip7 zip7 Zip6 zip6
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie
            F f IF iff)
           G g IG ig);
impl_zipn!(Zip8 zip8 Zip7 zip7
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie
            F f IF iff
            G g IG ig)
           H h IH ih);
impl_zipn!(Zip9 zip9 Zip8 zip8
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie
            F f IF iff
            G g IG ig
            H h IH ih)
           I i II ii);
impl_zipn!(Zip10 zip10 Zip9 zip9
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie
            F f IF iff
            G g IG ig
            H h IH ih
            I i II ii)
           J j IJ ij);
impl_zipn!(Zip11 zip11 Zip10 zip10
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie
            F f IF iff
            G g IG ig
            H h IH ih
            I i II ii
            J j IJ ij)
           K k IK ik);
impl_zipn!(Zip12 zip12 Zip11 zip11
           (A a IA ia
            B b IB ib
            C c IC ic
            D d ID id
            E e IE ie
            F f IF iff
            G g IG ig
            H h IH ih
            I i II ii
            J j IJ ij
            K k IK ik)
           L l IL il);



#[cfg(test)]
mod tests {
    #![allow(non_upper_case_globals)]
    use super::{zip2, zip3, zip4, zip5, zip6, zip7, zip8, zip9, zip10, zip11,
                zip12};
    const a: [i8; 3] = [0, 1, 2];
    const b: [i8; 3] = [3, 4, 5];
    const c: [i8; 3] = [6, 7, 8];
    const d: [i8; 3] = [9, 10, 11];
    const e: [i8; 3] = [12, 13, 14];
    const f: [i8; 3] = [15, 16, 17];
    const g: [i8; 3] = [18, 19, 20];
    const h: [i8; 3] = [21, 22, 23];
    const i: [i8; 3] = [24, 25, 26];
    const j: [i8; 3] = [27, 28, 29];
    const k: [i8; 3] = [30, 31, 32];
    const l: [i8; 3] = [33, 34, 35];

    #[test]
    fn test_zip2() {
        let ab = zip2(a.iter(),
                      b.iter())
                 .map(|(&aa, &bb)| (aa, bb))
                 .collect::<Vec<_>>();
        assert_eq!(ab, vec![(0, 3),
                            (1, 4),
                            (2, 5)]);
    }

    #[test]
    fn test_zip3() {
        let abc = zip3(a.iter(),
                       b.iter(),
                       c.iter())
                 .map(|(&aa, &bb, &cc)| (aa, bb, cc))
                 .collect::<Vec<_>>();
        assert_eq!(abc, vec![(0, 3, 6),
                             (1, 4, 7),
                             (2, 5, 8)]);
    }

    #[test]
    fn test_zip4() {
        let abcd = zip4(a.iter(),
                        b.iter(),
                        c.iter(),
                        d.iter())
                 .map(|(&aa, &bb, &cc, &dd)| (aa, bb, cc, dd))
                 .collect::<Vec<_>>();
        assert_eq!(abcd, vec![(0, 3, 6, 9),
                              (1, 4, 7, 10),
                              (2, 5, 8, 11)]);
    }

    #[test]
    fn test_zip5() {
        let abcde = zip5(a.iter(),
                        b.iter(),
                        c.iter(),
                        d.iter(),
                        e.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee)| (aa, bb, cc, dd, ee))
                 .collect::<Vec<_>>();
        assert_eq!(abcde, vec![(0, 3, 6, 9,  12),
                               (1, 4, 7, 10, 13),
                               (2, 5, 8, 11, 14)]);
    }

    #[test]
    fn test_zip6() {
        let abcdef = zip6(a.iter(),
                          b.iter(),
                          c.iter(),
                          d.iter(),
                          e.iter(),
                          f.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff)| (aa, bb, cc, dd, ee, ff))
                 .collect::<Vec<_>>();
        assert_eq!(abcdef, vec![(0, 3, 6, 9,  12, 15),
                                (1, 4, 7, 10, 13, 16),
                                (2, 5, 8, 11, 14, 17)]);
    }

    #[test]
    fn test_zip7() {
        let abcdefg = zip7(a.iter(),
                           b.iter(),
                           c.iter(),
                           d.iter(),
                           e.iter(),
                           f.iter(),
                           g.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff, &gg)|
                       ( aa,  bb,  cc,  dd,  ee,  ff,  gg))
                 .collect::<Vec<_>>();
        assert_eq!(abcdefg, vec![(0, 3, 6, 9,  12, 15, 18),
                                 (1, 4, 7, 10, 13, 16, 19),
                                 (2, 5, 8, 11, 14, 17, 20)]);
    }

    #[test]
    fn test_zip8() {
        let abcdefgh = zip8(a.iter(),
                            b.iter(),
                            c.iter(),
                            d.iter(),
                            e.iter(),
                            f.iter(),
                            g.iter(),
                            h.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff, &gg, &hh)|
                       ( aa,  bb,  cc,  dd,  ee,  ff,  gg,  hh))
                 .collect::<Vec<_>>();
        assert_eq!(abcdefgh, vec![(0, 3, 6, 9,  12, 15, 18, 21),
                                   (1, 4, 7, 10, 13, 16, 19, 22),
                                   (2, 5, 8, 11, 14, 17, 20, 23)]);
    }

    #[test]
    fn test_zip9() {
        let abcdefghi = zip9(a.iter(),
                             b.iter(),
                             c.iter(),
                             d.iter(),
                             e.iter(),
                             f.iter(),
                             g.iter(),
                             h.iter(),
                             i.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff, &gg, &hh, &ii)|
                       ( aa,  bb,  cc,  dd,  ee,  ff,  gg,  hh,  ii))
                 .collect::<Vec<_>>();
        assert_eq!(abcdefghi, vec![(0, 3, 6, 9,  12, 15, 18, 21, 24),
                                    (1, 4, 7, 10, 13, 16, 19, 22, 25),
                                    (2, 5, 8, 11, 14, 17, 20, 23, 26)]);
    }

    #[test]
    fn test_zip10() {
        let abcdefghij = zip10(a.iter(),
                              b.iter(),
                              c.iter(),
                              d.iter(),
                              e.iter(),
                              f.iter(),
                              g.iter(),
                              h.iter(),
                              i.iter(),
                              j.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff, &gg, &hh, &ii, &jj)|
                       ( aa,  bb,  cc,  dd,  ee,  ff,  gg,  hh,  ii,  jj))
                 .collect::<Vec<_>>();
        assert_eq!(abcdefghij, vec![(0, 3, 6, 9,  12, 15, 18, 21, 24, 27),
                                     (1, 4, 7, 10, 13, 16, 19, 22, 25, 28),
                                     (2, 5, 8, 11, 14, 17, 20, 23, 26, 29)]);
    }

    #[test]
    fn test_zip11() {
        let abcdefghijk = zip11(a.iter(),
                                b.iter(),
                                c.iter(),
                                d.iter(),
                                e.iter(),
                                f.iter(),
                                g.iter(),
                                h.iter(),
                                i.iter(),
                                j.iter(),
                                k.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff, &gg, &hh, &ii, &jj, &kk)|
                       ( aa,  bb,  cc,  dd,  ee,  ff,  gg,  hh,  ii,  jj,  kk))
                 .collect::<Vec<_>>();
        assert_eq!(abcdefghijk, vec![(0, 3, 6, 9,  12, 15, 18, 21, 24, 27, 30),
                                      (1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31),
                                      (2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32)]);
    }

    #[test]
    fn test_zip12() {
        let abcdefghijkl = zip12(a.iter(),
                                 b.iter(),
                                 c.iter(),
                                 d.iter(),
                                 e.iter(),
                                 f.iter(),
                                 g.iter(),
                                 h.iter(),
                                 i.iter(),
                                 j.iter(),
                                 k.iter(),
                                 l.iter())
                 .map(|(&aa, &bb, &cc, &dd, &ee, &ff, &gg, &hh, &ii, &jj, &kk, &ll)|
                       ( aa,  bb,  cc,  dd,  ee,  ff,  gg,  hh,  ii,  jj,  kk,  ll))
                 .collect::<Vec<_>>();
        assert_eq!(abcdefghijkl, vec![(0, 3, 6, 9,  12, 15, 18, 21, 24, 27, 30, 33),
                                      (1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34),
                                      (2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35)]);
    }
}