diff options
| author | bors <bors@rust-lang.org> | 2013-06-21 01:49:50 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-06-21 01:49:50 -0700 |
| commit | b0e3ffd3805203420846296bc7d5a3c13dfd8b3a (patch) | |
| tree | 791c56423464c90ff6f14a56992ae62f621d4665 /src/libstd/iterator.rs | |
| parent | ba05af7b1cbb5ccf25226ca5cd8d84f872426466 (diff) | |
| parent | 06bec77fafb1c30052b9c69a3fb17b2835cb608f (diff) | |
| download | rust-b0e3ffd3805203420846296bc7d5a3c13dfd8b3a.tar.gz rust-b0e3ffd3805203420846296bc7d5a3c13dfd8b3a.zip | |
auto merge of #7263 : thestinger/rust/iterator, r=graydon
Diffstat (limited to 'src/libstd/iterator.rs')
| -rw-r--r-- | src/libstd/iterator.rs | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index eefad1a03dc..394066f1d4c 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -273,6 +273,7 @@ pub trait IteratorUtil<A> { /// ~~~ fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B; + // FIXME: #5898: should be called len /// Counts the number of elements in this iterator. /// /// # Example @@ -280,10 +281,10 @@ pub trait IteratorUtil<A> { /// ~~~ {.rust} /// let a = [1, 2, 3, 4, 5]; /// let mut it = a.iter(); - /// assert!(it.count() == 5); - /// assert!(it.count() == 0); + /// assert!(it.len_() == 5); + /// assert!(it.len_() == 0); /// ~~~ - fn count(&mut self) -> uint; + fn len_(&mut self) -> uint; /// Tests whether the predicate holds true for all elements in the iterator. /// @@ -314,6 +315,9 @@ pub trait IteratorUtil<A> { /// Return the index of the first element satisfying the specified predicate fn position_(&mut self, predicate: &fn(A) -> bool) -> Option<uint>; + + /// Count the number of elements satisfying the specified predicate + fn count(&mut self, predicate: &fn(A) -> bool) -> uint; } /// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also @@ -432,7 +436,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { /// Count the number of items yielded by an iterator #[inline] - fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) } + fn len_(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) } #[inline] fn all(&mut self, f: &fn(A) -> bool) -> bool { @@ -467,6 +471,15 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { } None } + + #[inline] + fn count(&mut self, predicate: &fn(A) -> bool) -> uint { + let mut i = 0; + for self.advance |x| { + if predicate(x) { i += 1 } + } + i + } } /// A trait for iterators over elements which can be added together @@ -1020,11 +1033,11 @@ mod tests { } #[test] - fn test_iterator_count() { + fn test_iterator_len() { let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - assert_eq!(v.slice(0, 4).iter().count(), 4); - assert_eq!(v.slice(0, 10).iter().count(), 10); - assert_eq!(v.slice(0, 0).iter().count(), 0); + assert_eq!(v.slice(0, 4).iter().len_(), 4); + assert_eq!(v.slice(0, 10).iter().len_(), 10); + assert_eq!(v.slice(0, 0).iter().len_(), 0); } #[test] @@ -1099,4 +1112,12 @@ mod tests { assert_eq!(v.iter().position_(|x| *x % 3 == 0).unwrap(), 1); assert!(v.iter().position_(|x| *x % 12 == 0).is_none()); } + + #[test] + fn test_count() { + let xs = &[1, 2, 2, 1, 5, 9, 0, 2]; + assert_eq!(xs.iter().count(|x| *x == 2), 3); + assert_eq!(xs.iter().count(|x| *x == 5), 1); + assert_eq!(xs.iter().count(|x| *x == 95), 0); + } } |
