Skip to content

Commit

Permalink
Add finite iterator specialisations for Cycle
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Jan 12, 2018
1 parent 6710669 commit d6e2867
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
37 changes: 36 additions & 1 deletion src/libcore/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use cmp;
use cmp::{self, Ordering};
use fmt;
use iter_private::TrustedRandomAccess;
use ops::Try;
Expand Down Expand Up @@ -643,6 +643,41 @@ impl<I> Iterator for Cycle<I> where I: Clone + Iterator {
_ => (usize::MAX, None)
}
}

#[inline]
fn all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool { self.orig.clone().all(f) }

#[inline]
fn max(self) -> Option<Self::Item> where Self::Item: cmp::Ord { self.orig.clone().max() }

#[inline]
fn min(self) -> Option<Self::Item> where Self::Item: cmp::Ord {
cmp::min(self.iter.min(), self.orig.clone().min())
}

#[inline]
fn max_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> B {
self.orig.clone().max_by_key(f)
}

#[inline]
fn max_by<F>(self, f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item, &Self::Item) -> Ordering {
self.orig.clone().max_by(f)
}

#[inline]
fn min_by_key<B: Ord, F>(self, f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item) -> B {
self.iter.chain(self.orig.clone()).min_by_key(f)
}

#[inline]
fn min_by<F>(self, f: F) -> Option<Self::Item>
where F: FnMut(&Self::Item, &Self::Item) -> Ordering {
self.iter.chain(self.orig.clone()).min_by(f)
}
}

#[unstable(feature = "fused", issue = "35602")]
Expand Down
18 changes: 16 additions & 2 deletions src/libcore/tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,20 @@ fn test_cycle() {
assert_eq!(it.next(), None);
}

#[test]
fn test_cycle_iterator() {
let a = 1;
let b = 5;
let mut it = (a..b).cycle();
assert_eq!(it.all(|x| x <= b), true);
assert_eq!((a..b).cycle().max(), Some(b - 1));
assert_eq!((a..b).cycle().min(), Some(a));
assert_eq!((a..b).cycle().max_by_key(|x| -x), Some(a));
assert_eq!((a..b).cycle().max_by(|x, y| y.cmp(x)), Some(a));
assert_eq!((a..b).cycle().min_by_key(|x| -x), Some(b - 1));
assert_eq!((a..b).cycle().min_by(|x, y| y.cmp(x)), Some(b - 1));
}

#[test]
fn test_iterator_nth() {
let v: &[_] = &[0, 1, 2, 3, 4];
Expand Down Expand Up @@ -1414,9 +1428,9 @@ fn test_repeat_iterator() {
assert_eq!(repeat(42).max(), Some(42));
assert_eq!(repeat(42).min(), Some(42));
assert_eq!(repeat(42).max_by_key(|_| 0), Some(42));
assert_eq!(repeat(42).max_by_key(|_| Ordering::Greater), Some(42));
assert_eq!(repeat(42).max_by(|_, _| Ordering::Equal), Some(42));
assert_eq!(repeat(42).min_by_key(|_| 0), Some(42));
assert_eq!(repeat(42).min_by_key(|_| Ordering::Greater), Some(42));
assert_eq!(repeat(42).min_by(|_, _| Ordering::Equal), Some(42));
}

#[test]
Expand Down

0 comments on commit d6e2867

Please sign in to comment.