Skip to content

Implement ExactSizeIterator for remaining core Iterators where applicable #21453

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

Merged
merged 3 commits into from
Jan 23, 2015
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
15 changes: 15 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,9 @@ impl<'a, I> DoubleEndedIterator for ByRef<'a, I> where I: 'a + DoubleEndedIterat
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next_back() }
}

#[stable]
impl<'a, I> ExactSizeIterator for ByRef<'a, I> where I: 'a + ExactSizeIterator {}

/// A trait for iterators over elements which can be added together
#[unstable = "needs to be re-evaluated as part of numerics reform"]
pub trait AdditiveIterator<A> {
Expand Down Expand Up @@ -1790,6 +1793,9 @@ impl<T, I> Iterator for Peekable<T, I> where I: Iterator<Item=T> {
}
}

#[stable]
impl<T, I> ExactSizeIterator for Peekable<T, I> where I: ExactSizeIterator<Item = T> {}
Copy link
Member

Choose a reason for hiding this comment

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

(copying over from previous comment to continue discussion)

Sorry, that was supposed to be (uint::MAX, Some(uint::MAX)) (copy and paste error).

My basic point is that that if the number of items in an ExactSizeIterator once fit in a uint (required by the spec), it must always fit in a uint regardless of where those items are (moving one from self.iter to self.peeked doesn't change the total number of items).

Ah right, good point! So if the underlying iterator returns (MAX, Some(MAX)) then the only time we'll add 1 is if we've already peeked in which case the underlying iterator will return (MAX-1, Some(MAX-1)).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly.


#[stable]
impl<T, I> Peekable<T, I> where I: Iterator<Item=T> {
/// Return a reference to the next element of the iterator with out advancing it,
Expand Down Expand Up @@ -1982,6 +1988,9 @@ impl<I> RandomAccessIterator for Skip<I> where I: RandomAccessIterator{
}
}

#[stable]
impl<I> ExactSizeIterator for Skip<I> where I: ExactSizeIterator {}

/// An iterator that only iterates over the first `n` iterations of `iter`.
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
Expand Down Expand Up @@ -2037,6 +2046,9 @@ impl<I> RandomAccessIterator for Take<I> where I: RandomAccessIterator{
}
}

#[stable]
impl<I> ExactSizeIterator for Take<I> where I: ExactSizeIterator {}


/// An iterator to maintain state while iterating another iterator
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
Expand Down Expand Up @@ -2246,6 +2258,9 @@ impl<I> RandomAccessIterator for Fuse<I> where I: RandomAccessIterator {
}
}

#[stable]
impl<I> ExactSizeIterator for Fuse<I> where I: ExactSizeIterator {}

impl<I> Fuse<I> {
/// Resets the fuse such that the next call to .next() or .next_back() will
/// call the underlying iterator again even if it previously returned None.
Expand Down
61 changes: 57 additions & 4 deletions src/libcoretest/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,32 @@ fn test_iterator_enumerate() {
fn test_iterator_peekable() {
let xs = vec![0u, 1, 2, 3, 4, 5];
let mut it = xs.iter().map(|&x|x).peekable();

assert_eq!(it.len(), 6);
assert_eq!(it.peek().unwrap(), &0);
assert_eq!(it.len(), 6);
assert_eq!(it.next().unwrap(), 0);
assert_eq!(it.len(), 5);
assert_eq!(it.next().unwrap(), 1);
assert_eq!(it.len(), 4);
assert_eq!(it.next().unwrap(), 2);
assert_eq!(it.len(), 3);
assert_eq!(it.peek().unwrap(), &3);
assert_eq!(it.len(), 3);
assert_eq!(it.peek().unwrap(), &3);
assert_eq!(it.len(), 3);
assert_eq!(it.next().unwrap(), 3);
assert_eq!(it.len(), 2);
assert_eq!(it.next().unwrap(), 4);
assert_eq!(it.len(), 1);
assert_eq!(it.peek().unwrap(), &5);
assert_eq!(it.len(), 1);
assert_eq!(it.next().unwrap(), 5);
assert_eq!(it.len(), 0);
assert!(it.peek().is_none());
assert_eq!(it.len(), 0);
assert!(it.next().is_none());
assert_eq!(it.len(), 0);
}

#[test]
Expand Down Expand Up @@ -166,24 +180,45 @@ fn test_iterator_skip() {
let ys = [13, 15, 16, 17, 19, 20, 30];
let mut it = xs.iter().skip(5);
let mut i = 0;
for &x in it {
while let Some(&x) = it.next() {
assert_eq!(x, ys[i]);
i += 1;
assert_eq!(it.len(), xs.len()-5-i);
}
assert_eq!(i, ys.len());
assert_eq!(it.len(), 0);
}

#[test]
fn test_iterator_take() {
let xs = [0u, 1, 2, 3, 5, 13, 15, 16, 17, 19];
let ys = [0u, 1, 2, 3, 5];
let xs = [0us, 1, 2, 3, 5, 13, 15, 16, 17, 19];
let ys = [0us, 1, 2, 3, 5];
let mut it = xs.iter().take(5);
let mut i = 0;
for &x in it {
assert_eq!(it.len(), 5);
while let Some(&x) = it.next() {
assert_eq!(x, ys[i]);
i += 1;
assert_eq!(it.len(), 5-i);
}
assert_eq!(i, ys.len());
assert_eq!(it.len(), 0);
}

#[test]
fn test_iterator_take_short() {
let xs = [0us, 1, 2, 3];
let ys = [0us, 1, 2, 3];
let mut it = xs.iter().take(5);
let mut i = 0;
assert_eq!(it.len(), 4);
while let Some(&x) = it.next() {
assert_eq!(x, ys[i]);
i += 1;
assert_eq!(it.len(), 4-i);
}
assert_eq!(i, ys.len());
assert_eq!(it.len(), 0);
}

#[test]
Expand Down Expand Up @@ -828,6 +863,24 @@ fn test_repeat() {
assert_eq!(it.next(), Some(42u));
}

#[test]
fn test_fuse() {
let mut it = 0us..3;
assert_eq!(it.len(), 3);
assert_eq!(it.next(), Some(0us));
assert_eq!(it.len(), 2);
assert_eq!(it.next(), Some(1us));
assert_eq!(it.len(), 1);
assert_eq!(it.next(), Some(2us));
assert_eq!(it.len(), 0);
assert_eq!(it.next(), None);
assert_eq!(it.len(), 0);
assert_eq!(it.next(), None);
assert_eq!(it.len(), 0);
assert_eq!(it.next(), None);
assert_eq!(it.len(), 0);
}

#[bench]
fn bench_rposition(b: &mut Bencher) {
let it: Vec<uint> = range(0u, 300).collect();
Expand Down