Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DoubleEndedIterator for Zip #346

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/ziptuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ macro_rules! impl_zip_iter {
$B: ExactSizeIterator,
)*
{ }

#[allow(non_snake_case)]
impl<$($B),*> DoubleEndedIterator for Zip<($($B,)*)> where
$(
$B: DoubleEndedIterator + ExactSizeIterator,
)*
{
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
let ($(ref mut $B,)*) = self.t;
let size = *[$( $B.len(), )*].into_iter().min().unwrap();

$(
if $B.len() != size {
for _ in 0..$B.len() - size { $B.next_back(); }
}
)*

match ($($B.next_back(),)*) {
($(Some($B),)*) => Some(($($B,)*)),
_ => None,
}
}
}
);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,3 +1209,31 @@ quickcheck! {
TestResult::passed()
}
}

quickcheck! {
fn test_double_ended_zip_2(a: Vec<u8>, b: Vec<u8>) -> TestResult {
let mut x =
multizip((a.clone().into_iter(), b.clone().into_iter()))
.collect_vec();
x.reverse();

let y =
multizip((a.into_iter(), b.into_iter()))
.rfold(Vec::new(), |mut vec, e| { vec.push(e); vec });

TestResult::from_bool(itertools::equal(x, y))
}

fn test_double_ended_zip_3(a: Vec<u8>, b: Vec<u8>, c: Vec<u8>) -> TestResult {
let mut x =
multizip((a.clone().into_iter(), b.clone().into_iter(), c.clone().into_iter()))
.collect_vec();
x.reverse();

let y =
multizip((a.into_iter(), b.into_iter(), c.into_iter()))
.rfold(Vec::new(), |mut vec, e| { vec.push(e); vec });

TestResult::from_bool(itertools::equal(x, y))
}
}
16 changes: 15 additions & 1 deletion tests/zip.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use itertools::Itertools;
use itertools::EitherOrBoth::{Both, Left, Right};
use itertools::free::zip_eq;
use itertools::multizip;

#[test]
fn zip_longest_fused() {
Expand Down Expand Up @@ -40,6 +41,20 @@ fn test_double_ended_zip_longest() {
assert_eq!(it.next(), None);
}

#[test]
fn test_double_ended_zip() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you formulate this as a quickcheck test?

I'd like to see that for any two inputs, multizip((a, b)).collect_vec().rev() is equal to (something like) multizip((a, b)).rfold(Vec::new(), |vec, e| { vec.push(e); vec })

let xs = [1, 2, 3, 4, 5, 6];
let ys = [1, 2, 3, 7];
let a = xs.iter().map(|&x| x);
let b = ys.iter().map(|&x| x);
let mut it = multizip((a, b));
assert_eq!(it.next_back(), Some((4, 7)));
assert_eq!(it.next_back(), Some((3, 3)));
assert_eq!(it.next_back(), Some((2, 2)));
assert_eq!(it.next_back(), Some((1, 1)));
assert_eq!(it.next_back(), None);
}


#[should_panic]
#[test]
Expand All @@ -60,4 +75,3 @@ fn zip_eq_panic2()

zip_eq(&a, &b).count();
}