| 1 |
use super::plumbing::*; |
| 2 |
use super::*; |
| 3 |
use std::iter; |
| 4 |
|
| 5 |
/// `Zip` is an iterator that zips up `a` and `b` into a single iterator |
| 6 |
/// of pairs. This struct is created by the [`zip()`] method on |
| 7 |
/// [`IndexedParallelIterator`] |
| 8 |
/// |
| 9 |
/// [`zip()`]: IndexedParallelIterator::zip() |
| 10 |
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 11 |
#[derive(Debug, Clone)] |
| 12 |
pub struct Zip<A, B> { |
| 13 |
a: A, |
| 14 |
b: B, |
| 15 |
} |
| 16 |
|
| 17 |
impl<A, B> Zip<A, B> { |
| 18 |
/// Creates a new `Zip` iterator. |
| 19 |
pub(super) fn new(a: A, b: B) -> Self { |
| 20 |
Zip { a, b } |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
impl<A, B> ParallelIterator for Zip<A, B> |
| 25 |
where |
| 26 |
A: IndexedParallelIterator, |
| 27 |
B: IndexedParallelIterator, |
| 28 |
{ |
| 29 |
type Item = (A::Item, B::Item); |
| 30 |
|
| 31 |
fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 32 |
where |
| 33 |
C: UnindexedConsumer<Self::Item>, |
| 34 |
{ |
| 35 |
bridge(self, consumer) |
| 36 |
} |
| 37 |
|
| 38 |
fn opt_len(&self) -> Option<usize> { |
| 39 |
Some(self.len()) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl<A, B> IndexedParallelIterator for Zip<A, B> |
| 44 |
where |
| 45 |
A: IndexedParallelIterator, |
| 46 |
B: IndexedParallelIterator, |
| 47 |
{ |
| 48 |
fn drive<C>(self, consumer: C) -> C::Result |
| 49 |
where |
| 50 |
C: Consumer<Self::Item>, |
| 51 |
{ |
| 52 |
bridge(self, consumer) |
| 53 |
} |
| 54 |
|
| 55 |
fn len(&self) -> usize { |
| 56 |
Ord::min(self.a.len(), self.b.len()) |
| 57 |
} |
| 58 |
|
| 59 |
fn with_producer<CB>(self, callback: CB) -> CB::Output |
| 60 |
where |
| 61 |
CB: ProducerCallback<Self::Item>, |
| 62 |
{ |
| 63 |
return self.a.with_producer(CallbackA { |
| 64 |
callback, |
| 65 |
b: self.b, |
| 66 |
}); |
| 67 |
|
| 68 |
struct CallbackA<CB, B> { |
| 69 |
callback: CB, |
| 70 |
b: B, |
| 71 |
} |
| 72 |
|
| 73 |
impl<CB, ITEM, B> ProducerCallback<ITEM> for CallbackA<CB, B> |
| 74 |
where |
| 75 |
B: IndexedParallelIterator, |
| 76 |
CB: ProducerCallback<(ITEM, B::Item)>, |
| 77 |
{ |
| 78 |
type Output = CB::Output; |
| 79 |
|
| 80 |
fn callback<A>(self, a_producer: A) -> Self::Output |
| 81 |
where |
| 82 |
A: Producer<Item = ITEM>, |
| 83 |
{ |
| 84 |
self.b.with_producer(CallbackB { |
| 85 |
a_producer, |
| 86 |
callback: self.callback, |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
struct CallbackB<CB, A> { |
| 92 |
a_producer: A, |
| 93 |
callback: CB, |
| 94 |
} |
| 95 |
|
| 96 |
impl<CB, A, ITEM> ProducerCallback<ITEM> for CallbackB<CB, A> |
| 97 |
where |
| 98 |
A: Producer, |
| 99 |
CB: ProducerCallback<(A::Item, ITEM)>, |
| 100 |
{ |
| 101 |
type Output = CB::Output; |
| 102 |
|
| 103 |
fn callback<B>(self, b_producer: B) -> Self::Output |
| 104 |
where |
| 105 |
B: Producer<Item = ITEM>, |
| 106 |
{ |
| 107 |
self.callback.callback(ZipProducer { |
| 108 |
a: self.a_producer, |
| 109 |
b: b_producer, |
| 110 |
}) |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// //////////////////////////////////////////////////////////////////////// |
| 117 |
|
| 118 |
struct ZipProducer<A: Producer, B: Producer> { |
| 119 |
a: A, |
| 120 |
b: B, |
| 121 |
} |
| 122 |
|
| 123 |
impl<A: Producer, B: Producer> Producer for ZipProducer<A, B> { |
| 124 |
type Item = (A::Item, B::Item); |
| 125 |
type IntoIter = iter::Zip<A::IntoIter, B::IntoIter>; |
| 126 |
|
| 127 |
fn into_iter(self) -> Self::IntoIter { |
| 128 |
self.a.into_iter().zip(self.b.into_iter()) |
| 129 |
} |
| 130 |
|
| 131 |
fn min_len(&self) -> usize { |
| 132 |
Ord::max(self.a.min_len(), self.b.min_len()) |
| 133 |
} |
| 134 |
|
| 135 |
fn max_len(&self) -> usize { |
| 136 |
Ord::min(self.a.max_len(), self.b.max_len()) |
| 137 |
} |
| 138 |
|
| 139 |
fn split_at(self, index: usize) -> (Self, Self) { |
| 140 |
let (a_left, a_right) = self.a.split_at(index); |
| 141 |
let (b_left, b_right) = self.b.split_at(index); |
| 142 |
( |
| 143 |
ZipProducer { |
| 144 |
a: a_left, |
| 145 |
b: b_left, |
| 146 |
}, |
| 147 |
ZipProducer { |
| 148 |
a: a_right, |
| 149 |
b: b_right, |
| 150 |
}, |
| 151 |
) |
| 152 |
} |
| 153 |
} |