Skip to content

Commit 180c4e0

Browse files
bors[bot]Sven Knoblochjswrenn
authored
Merge #346
346: Implement DoubleEndedIterator for Zip r=jswrenn a=svenknobloch Implements the DoubleEndedIterator trait for Zip. Based on the standard library's implementation. Co-authored-by: Sven Knobloch <[email protected]> Co-authored-by: Jack Wrenn <[email protected]>
2 parents c48da7b + da719ad commit 180c4e0

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/ziptuple.rs

+24
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,30 @@ macro_rules! impl_zip_iter {
9898
$B: ExactSizeIterator,
9999
)*
100100
{ }
101+
102+
#[allow(non_snake_case)]
103+
impl<$($B),*> DoubleEndedIterator for Zip<($($B,)*)> where
104+
$(
105+
$B: DoubleEndedIterator + ExactSizeIterator,
106+
)*
107+
{
108+
#[inline]
109+
fn next_back(&mut self) -> Option<Self::Item> {
110+
let ($(ref mut $B,)*) = self.t;
111+
let size = *[$( $B.len(), )*].into_iter().min().unwrap();
112+
113+
$(
114+
if $B.len() != size {
115+
for _ in 0..$B.len() - size { $B.next_back(); }
116+
}
117+
)*
118+
119+
match ($($B.next_back(),)*) {
120+
($(Some($B),)*) => Some(($($B,)*)),
121+
_ => None,
122+
}
123+
}
124+
}
101125
);
102126
}
103127

tests/quick.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1209,3 +1209,31 @@ quickcheck! {
12091209
TestResult::passed()
12101210
}
12111211
}
1212+
1213+
quickcheck! {
1214+
fn test_double_ended_zip_2(a: Vec<u8>, b: Vec<u8>) -> TestResult {
1215+
let mut x =
1216+
multizip((a.clone().into_iter(), b.clone().into_iter()))
1217+
.collect_vec();
1218+
x.reverse();
1219+
1220+
let y =
1221+
multizip((a.into_iter(), b.into_iter()))
1222+
.rfold(Vec::new(), |mut vec, e| { vec.push(e); vec });
1223+
1224+
TestResult::from_bool(itertools::equal(x, y))
1225+
}
1226+
1227+
fn test_double_ended_zip_3(a: Vec<u8>, b: Vec<u8>, c: Vec<u8>) -> TestResult {
1228+
let mut x =
1229+
multizip((a.clone().into_iter(), b.clone().into_iter(), c.clone().into_iter()))
1230+
.collect_vec();
1231+
x.reverse();
1232+
1233+
let y =
1234+
multizip((a.into_iter(), b.into_iter(), c.into_iter()))
1235+
.rfold(Vec::new(), |mut vec, e| { vec.push(e); vec });
1236+
1237+
TestResult::from_bool(itertools::equal(x, y))
1238+
}
1239+
}

tests/zip.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use itertools::Itertools;
22
use itertools::EitherOrBoth::{Both, Left, Right};
33
use itertools::free::zip_eq;
4+
use itertools::multizip;
45

56
#[test]
67
fn zip_longest_fused() {
@@ -40,6 +41,20 @@ fn test_double_ended_zip_longest() {
4041
assert_eq!(it.next(), None);
4142
}
4243

44+
#[test]
45+
fn test_double_ended_zip() {
46+
let xs = [1, 2, 3, 4, 5, 6];
47+
let ys = [1, 2, 3, 7];
48+
let a = xs.iter().map(|&x| x);
49+
let b = ys.iter().map(|&x| x);
50+
let mut it = multizip((a, b));
51+
assert_eq!(it.next_back(), Some((4, 7)));
52+
assert_eq!(it.next_back(), Some((3, 3)));
53+
assert_eq!(it.next_back(), Some((2, 2)));
54+
assert_eq!(it.next_back(), Some((1, 1)));
55+
assert_eq!(it.next_back(), None);
56+
}
57+
4358

4459
#[should_panic]
4560
#[test]
@@ -60,4 +75,3 @@ fn zip_eq_panic2()
6075

6176
zip_eq(&a, &b).count();
6277
}
63-

0 commit comments

Comments
 (0)