/rust/registry/src/index.crates.io-1949cf8c6b5b557f/itertools-0.14.0/src/ziptuple.rs
Line | Count | Source |
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 function 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!()`](crate::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 | 0 | pub fn multizip<T, U>(t: U) -> Zip<T> |
40 | 0 | where |
41 | 0 | Zip<T>: From<U> + Iterator, |
42 | | { |
43 | 0 | Zip::from(t) |
44 | 0 | } |
45 | | |
46 | | macro_rules! impl_zip_iter { |
47 | | ($($B:ident),*) => ( |
48 | | #[allow(non_snake_case)] |
49 | | impl<$($B: IntoIterator),*> From<($($B,)*)> for Zip<($($B::IntoIter,)*)> { |
50 | 0 | fn from(t: ($($B,)*)) -> Self { |
51 | 0 | let ($($B,)*) = t; |
52 | 0 | Zip { t: ($($B.into_iter(),)*) } |
53 | 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::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 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 |
54 | | } |
55 | | |
56 | | #[allow(non_snake_case)] |
57 | | #[allow(unused_assignments)] |
58 | | impl<$($B),*> Iterator for Zip<($($B,)*)> |
59 | | where |
60 | | $( |
61 | | $B: Iterator, |
62 | | )* |
63 | | { |
64 | | type Item = ($($B::Item,)*); |
65 | | |
66 | 0 | fn next(&mut self) -> Option<Self::Item> |
67 | | { |
68 | 0 | let ($(ref mut $B,)*) = self.t; |
69 | | |
70 | | // NOTE: Just like iter::Zip, we check the iterators |
71 | | // for None in order. We may finish unevenly (some |
72 | | // iterators gave n + 1 elements, some only n). |
73 | | $( |
74 | 0 | let $B = match $B.next() { |
75 | 0 | None => return None, |
76 | 0 | Some(elt) => elt |
77 | | }; |
78 | | )* |
79 | 0 | Some(($($B,)*)) |
80 | 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 |
81 | | |
82 | 0 | fn size_hint(&self) -> (usize, Option<usize>) |
83 | | { |
84 | 0 | let sh = (usize::MAX, None); |
85 | 0 | let ($(ref $B,)*) = self.t; |
86 | | $( |
87 | 0 | let sh = size_hint::min($B.size_hint(), sh); |
88 | | )* |
89 | 0 | sh |
90 | 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 |
91 | | } |
92 | | |
93 | | #[allow(non_snake_case)] |
94 | | impl<$($B),*> ExactSizeIterator for Zip<($($B,)*)> where |
95 | | $( |
96 | | $B: ExactSizeIterator, |
97 | | )* |
98 | | { } |
99 | | |
100 | | #[allow(non_snake_case)] |
101 | | impl<$($B),*> DoubleEndedIterator for Zip<($($B,)*)> where |
102 | | $( |
103 | | $B: DoubleEndedIterator + ExactSizeIterator, |
104 | | )* |
105 | | { |
106 | | #[inline] |
107 | 0 | fn next_back(&mut self) -> Option<Self::Item> { |
108 | 0 | let ($(ref mut $B,)*) = self.t; |
109 | 0 | let size = *[$( $B.len(), )*].iter().min().unwrap(); |
110 | | |
111 | | $( |
112 | 0 | if $B.len() != size { |
113 | 0 | for _ in 0..$B.len() - size { $B.next_back(); } |
114 | 0 | } |
115 | | )* |
116 | | |
117 | 0 | match ($($B.next_back(),)*) { |
118 | 0 | ($(Some($B),)*) => Some(($($B,)*)), |
119 | 0 | _ => None, |
120 | | } |
121 | 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 |
122 | | } |
123 | | ); |
124 | | } |
125 | | |
126 | | impl_zip_iter!(A); |
127 | | impl_zip_iter!(A, B); |
128 | | impl_zip_iter!(A, B, C); |
129 | | impl_zip_iter!(A, B, C, D); |
130 | | impl_zip_iter!(A, B, C, D, E); |
131 | | impl_zip_iter!(A, B, C, D, E, F); |
132 | | impl_zip_iter!(A, B, C, D, E, F, G); |
133 | | impl_zip_iter!(A, B, C, D, E, F, G, H); |
134 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I); |
135 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I, J); |
136 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K); |
137 | | impl_zip_iter!(A, B, C, D, E, F, G, H, I, J, K, L); |