/rust/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.12.1/src/ziptuple.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use super::size_hint; |
2 | | |
3 | | /// See [`multizip`] for more information. |
4 | | #[derive(Clone, Debug)] |
5 | | #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
6 | | pub struct Zip<T> { |
7 | | t: T, |
8 | | } |
9 | | |
10 | | /// An iterator that generalizes *.zip()* and allows running multiple iterators in lockstep. |
11 | | /// |
12 | | /// The iterator `Zip<(I, J, ..., M)>` is formed from a tuple of iterators (or values that |
13 | | /// implement [`IntoIterator`]) and yields elements |
14 | | /// until any of the subiterators yields `None`. |
15 | | /// |
16 | | /// The iterator element type is a tuple like like `(A, B, ..., E)` where `A` to `E` are the |
17 | | /// element types of the subiterator. |
18 | | /// |
19 | | /// **Note:** The result of this macro is a value of a named type (`Zip<(I, J, |
20 | | /// ..)>` of each component iterator `I, J, ...`) if each component iterator is |
21 | | /// nameable. |
22 | | /// |
23 | | /// Prefer [`izip!()`] over `multizip` for the performance benefits of using the |
24 | | /// standard library `.zip()`. Prefer `multizip` if a nameable type is needed. |
25 | | /// |
26 | | /// ``` |
27 | | /// use itertools::multizip; |
28 | | /// |
29 | | /// // iterate over three sequences side-by-side |
30 | | /// let mut results = [0, 0, 0, 0]; |
31 | | /// let inputs = [3, 7, 9, 6]; |
32 | | /// |
33 | | /// for (r, index, input) in multizip((&mut results, 0..10, &inputs)) { |
34 | | /// *r = index * 10 + input; |
35 | | /// } |
36 | | /// |
37 | | /// assert_eq!(results, [0 + 3, 10 + 7, 29, 36]); |
38 | | /// ``` |
39 | | /// [`izip!()`]: crate::izip |
40 | 0 | pub fn multizip<T, U>(t: U) -> Zip<T> |
41 | 0 | where |
42 | 0 | Zip<T>: From<U> + Iterator, |
43 | 0 | { |
44 | 0 | Zip::from(t) |
45 | 0 | } |
46 | | |
47 | | macro_rules! impl_zip_iter { |
48 | | ($($B:ident),*) => ( |
49 | | #[allow(non_snake_case)] |
50 | | impl<$($B: IntoIterator),*> From<($($B,)*)> for Zip<($($B::IntoIter,)*)> { |
51 | 0 | fn from(t: ($($B,)*)) -> Self { |
52 | 0 | let ($($B,)*) = t; |
53 | 0 | Zip { t: ($($B.into_iter(),)*) } |
54 | 0 | } Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter,)> as core::convert::From<(_,)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _, _, _, _, _, _)>>::from Unexecuted instantiation: <itertools::ziptuple::Zip<(<_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter, <_ as core::iter::traits::collect::IntoIterator>::IntoIter)> as core::convert::From<(_, _, _, _, _, _, _, _, _, _, _, _)>>::from |
55 | | } |
56 | | |
57 | | #[allow(non_snake_case)] |
58 | | #[allow(unused_assignments)] |
59 | | impl<$($B),*> Iterator for Zip<($($B,)*)> |
60 | | where |
61 | | $( |
62 | | $B: Iterator, |
63 | | )* |
64 | | { |
65 | | type Item = ($($B::Item,)*); |
66 | | |
67 | 0 | fn next(&mut self) -> Option<Self::Item> |
68 | 0 | { |
69 | 0 | let ($(ref mut $B,)*) = self.t; |
70 | | |
71 | | // NOTE: Just like iter::Zip, we check the iterators |
72 | | // for None in order. We may finish unevenly (some |
73 | | // iterators gave n + 1 elements, some only n). |
74 | | $( |
75 | 0 | let $B = match $B.next() { |
76 | 0 | None => return None, |
77 | 0 | Some(elt) => elt |
78 | 0 | }; |
79 | 0 | )* |
80 | 0 | Some(($($B,)*)) |
81 | 0 | } Unexecuted instantiation: <itertools::ziptuple::Zip<(_,)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::next |
82 | | |
83 | 0 | fn size_hint(&self) -> (usize, Option<usize>) |
84 | 0 | { |
85 | 0 | let sh = (::std::usize::MAX, None); |
86 | 0 | let ($(ref $B,)*) = self.t; |
87 | 0 | $( |
88 | 0 | let sh = size_hint::min($B.size_hint(), sh); |
89 | 0 | )* |
90 | 0 | sh |
91 | 0 | } Unexecuted instantiation: <itertools::ziptuple::Zip<(_,)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _, _, _)> as core::iter::traits::iterator::Iterator>::size_hint |
92 | | } |
93 | | |
94 | | #[allow(non_snake_case)] |
95 | | impl<$($B),*> ExactSizeIterator for Zip<($($B,)*)> where |
96 | | $( |
97 | | $B: ExactSizeIterator, |
98 | | )* |
99 | | { } |
100 | | |
101 | | #[allow(non_snake_case)] |
102 | | impl<$($B),*> DoubleEndedIterator for Zip<($($B,)*)> where |
103 | | $( |
104 | | $B: DoubleEndedIterator + ExactSizeIterator, |
105 | | )* |
106 | | { |
107 | | #[inline] |
108 | 0 | fn next_back(&mut self) -> Option<Self::Item> { |
109 | 0 | let ($(ref mut $B,)*) = self.t; |
110 | 0 | let size = *[$( $B.len(), )*].iter().min().unwrap(); |
111 | 0 |
|
112 | 0 | $( |
113 | 0 | if $B.len() != size { |
114 | 0 | for _ in 0..$B.len() - size { $B.next_back(); } |
115 | 0 | } |
116 | | )* |
117 | | |
118 | 0 | match ($($B.next_back(),)*) { |
119 | 0 | ($(Some($B),)*) => Some(($($B,)*)), |
120 | 0 | _ => None, |
121 | | } |
122 | 0 | } Unexecuted instantiation: <itertools::ziptuple::Zip<(_,)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back Unexecuted instantiation: <itertools::ziptuple::Zip<(_, _, _, _, _, _, _, _, _, _, _, _)> as core::iter::traits::double_ended::DoubleEndedIterator>::next_back |
123 | | } |
124 | | ); |
125 | | } |
126 | | |
127 | | impl_zip_iter!(A); |
128 | | impl_zip_iter!(A, B); |
129 | | impl_zip_iter!(A, B, C); |
130 | | impl_zip_iter!(A, B, C, D); |
131 | | impl_zip_iter!(A, B, C, D, E); |
132 | | impl_zip_iter!(A, B, C, D, E, F); |
133 | | impl_zip_iter!(A, B, C, D, E, F, G); |
134 | | impl_zip_iter!(A, B, C, D, E, F, G, H); |
135 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I); |
136 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I, J); |
137 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K); |
138 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K, L); |