Skip to content

libcore: Simplify Enumerate, Zip::next_back #16728

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

Closed
wants to merge 1 commit into from
Closed
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
34 changes: 18 additions & 16 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,10 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
/// Return the exact length of the iterator.
fn len(&self) -> uint {
let (lower, upper) = self.size_hint();
// Note: This assertion is overly defensive, but it checks the invariant
// guaranteed by the trait. If this trait were rust-internal,
// we could use debug_assert!; assert_eq! will check all Rust user
// implementations too.
assert_eq!(upper, Some(lower));
lower
}
Expand Down Expand Up @@ -1195,21 +1199,20 @@ impl<A, B, T: ExactSize<A>, U: ExactSize<B>> DoubleEndedIterator<(A, B)>
for Zip<T, U> {
#[inline]
fn next_back(&mut self) -> Option<(A, B)> {
let (a_sz, a_upper) = self.a.size_hint();
let (b_sz, b_upper) = self.b.size_hint();
assert!(a_upper == Some(a_sz));
assert!(b_upper == Some(b_sz));
if a_sz < b_sz {
for _ in range(0, b_sz - a_sz) { self.b.next_back(); }
} else if a_sz > b_sz {
for _ in range(0, a_sz - b_sz) { self.a.next_back(); }
}
let (a_sz, _) = self.a.size_hint();
let (b_sz, _) = self.b.size_hint();
assert!(a_sz == b_sz);
let a_sz = self.a.len();
let b_sz = self.b.len();
if a_sz != b_sz {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than two checks here, couldn't you just do one cmp?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, strictly speaking you can just have the a_sz > b_sz check, because in the else branch if a_sz = b_sz, then the range is empty and the loop doesn't run.

Not sure what's the most efficient/readable here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the input, I don't know exactly either. But the evening out step should only happen once, and that's why I wanted a single comparison to exclude it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, good call!

// Adjust a, b to equal length
if a_sz > b_sz {
for _ in range(0, a_sz - b_sz) { self.a.next_back(); }
} else {
for _ in range(0, b_sz - a_sz) { self.b.next_back(); }
}
}
match (self.a.next_back(), self.b.next_back()) {
(Some(x), Some(y)) => Some((x, y)),
_ => None
(None, None) => None,
_ => unreachable!(),
}
}
}
Expand Down Expand Up @@ -1395,9 +1398,8 @@ impl<A, T: ExactSize<A>> DoubleEndedIterator<(uint, A)> for Enumerate<T> {
fn next_back(&mut self) -> Option<(uint, A)> {
match self.iter.next_back() {
Some(a) => {
let (lower, upper) = self.iter.size_hint();
assert!(upper == Some(lower));
Some((self.count + lower, a))
let len = self.iter.len();
Some((self.count + len, a))
}
_ => None
}
Expand Down