/rust/registry/src/index.crates.io-1949cf8c6b5b557f/rayon-1.11.0/src/iter/zip.rs
Line | Count | Source |
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 | 0 | pub(super) fn new(a: A, b: B) -> Self { |
20 | 0 | Zip { a, b } |
21 | 0 | } |
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 | 0 | fn drive_unindexed<C>(self, consumer: C) -> C::Result |
32 | 0 | where |
33 | 0 | C: UnindexedConsumer<Self::Item>, |
34 | | { |
35 | 0 | bridge(self, consumer) |
36 | 0 | } |
37 | | |
38 | 0 | fn opt_len(&self) -> Option<usize> { |
39 | 0 | Some(self.len()) |
40 | 0 | } |
41 | | } |
42 | | |
43 | | impl<A, B> IndexedParallelIterator for Zip<A, B> |
44 | | where |
45 | | A: IndexedParallelIterator, |
46 | | B: IndexedParallelIterator, |
47 | | { |
48 | 0 | fn drive<C>(self, consumer: C) -> C::Result |
49 | 0 | where |
50 | 0 | C: Consumer<Self::Item>, |
51 | | { |
52 | 0 | bridge(self, consumer) |
53 | 0 | } |
54 | | |
55 | 0 | fn len(&self) -> usize { |
56 | 0 | Ord::min(self.a.len(), self.b.len()) |
57 | 0 | } |
58 | | |
59 | 0 | fn with_producer<CB>(self, callback: CB) -> CB::Output |
60 | 0 | where |
61 | 0 | CB: ProducerCallback<Self::Item>, |
62 | | { |
63 | 0 | return self.a.with_producer(CallbackA { |
64 | 0 | callback, |
65 | 0 | b: self.b, |
66 | 0 | }); |
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 | 0 | fn callback<A>(self, a_producer: A) -> Self::Output |
81 | 0 | where |
82 | 0 | A: Producer<Item = ITEM>, |
83 | | { |
84 | 0 | self.b.with_producer(CallbackB { |
85 | 0 | a_producer, |
86 | 0 | callback: self.callback, |
87 | 0 | }) |
88 | 0 | } |
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 | 0 | fn callback<B>(self, b_producer: B) -> Self::Output |
104 | 0 | where |
105 | 0 | B: Producer<Item = ITEM>, |
106 | | { |
107 | 0 | self.callback.callback(ZipProducer { |
108 | 0 | a: self.a_producer, |
109 | 0 | b: b_producer, |
110 | 0 | }) |
111 | 0 | } |
112 | | } |
113 | 0 | } |
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 | 0 | fn into_iter(self) -> Self::IntoIter { |
128 | 0 | self.a.into_iter().zip(self.b.into_iter()) |
129 | 0 | } |
130 | | |
131 | 0 | fn min_len(&self) -> usize { |
132 | 0 | Ord::max(self.a.min_len(), self.b.min_len()) |
133 | 0 | } |
134 | | |
135 | 0 | fn max_len(&self) -> usize { |
136 | 0 | Ord::min(self.a.max_len(), self.b.max_len()) |
137 | 0 | } |
138 | | |
139 | 0 | fn split_at(self, index: usize) -> (Self, Self) { |
140 | 0 | let (a_left, a_right) = self.a.split_at(index); |
141 | 0 | let (b_left, b_right) = self.b.split_at(index); |
142 | 0 | ( |
143 | 0 | ZipProducer { |
144 | 0 | a: a_left, |
145 | 0 | b: b_left, |
146 | 0 | }, |
147 | 0 | ZipProducer { |
148 | 0 | a: a_right, |
149 | 0 | b: b_right, |
150 | 0 | }, |
151 | 0 | ) |
152 | 0 | } |
153 | | } |