diff options
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter.rs | 48 | ||||
| -rw-r--r-- | src/libcore/slice.rs | 3 | ||||
| -rw-r--r-- | src/libcore/str.rs | 3 |
3 files changed, 25 insertions, 29 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 875c852d8ae..64c53b658ef 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -529,11 +529,11 @@ pub trait Iterator<A> { /// ```rust /// let a = [1, 2, 3, 4, 5]; /// let mut it = a.iter(); - /// assert!(it.len() == 5); - /// assert!(it.len() == 0); + /// assert!(it.count() == 5); + /// assert!(it.count() == 0); /// ``` #[inline] - fn len(&mut self) -> uint { + fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) } @@ -591,16 +591,6 @@ pub trait Iterator<A> { None } - /// Count the number of elements satisfying the specified predicate - #[inline] - fn count(&mut self, predicate: |A| -> bool) -> uint { - let mut i = 0; - for x in *self { - if predicate(x) { i += 1 } - } - i - } - /// Return the element that gives the maximum value from the /// specified function. /// @@ -738,6 +728,14 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> { } None } + + #[inline] + /// Return the exact length of the iterator. + fn len(&self) -> uint { + let (lower, upper) = self.size_hint(); + assert!(upper == Some(lower)); + lower + } } // All adaptors that preserve the size of the wrapped iterator are fine @@ -2594,9 +2592,9 @@ mod tests { #[test] fn test_iterator_len() { let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - 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); + 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); } #[test] @@ -2712,9 +2710,9 @@ mod tests { #[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); + assert_eq!(xs.iter().filter(|x| **x == 2).count(), 3); + assert_eq!(xs.iter().filter(|x| **x == 5).count(), 1); + assert_eq!(xs.iter().filter(|x| **x == 95).count(), 0); } #[test] @@ -3044,10 +3042,10 @@ mod tests { assert!(range(-10i, -1).collect::<Vec<int>>() == vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]); assert!(range(0i, 5).rev().collect::<Vec<int>>() == vec![4, 3, 2, 1, 0]); - assert_eq!(range(200, -5).len(), 0); - assert_eq!(range(200, -5).rev().len(), 0); - assert_eq!(range(200, 200).len(), 0); - assert_eq!(range(200, 200).rev().len(), 0); + assert_eq!(range(200, -5).count(), 0); + assert_eq!(range(200, -5).rev().count(), 0); + assert_eq!(range(200, 200).count(), 0); + assert_eq!(range(200, 200).rev().count(), 0); assert_eq!(range(0i, 100).size_hint(), (100, Some(100))); // this test is only meaningful when sizeof uint < sizeof u64 @@ -3062,8 +3060,8 @@ mod tests { vec![0i, 1, 2, 3, 4, 5]); assert!(range_inclusive(0i, 5).rev().collect::<Vec<int>>() == vec![5i, 4, 3, 2, 1, 0]); - assert_eq!(range_inclusive(200, -5).len(), 0); - assert_eq!(range_inclusive(200, -5).rev().len(), 0); + assert_eq!(range_inclusive(200, -5).count(), 0); + assert_eq!(range_inclusive(200, -5).rev().count(), 0); assert!(range_inclusive(200, 200).collect::<Vec<int>>() == vec![200]); assert!(range_inclusive(200, 200).rev().collect::<Vec<int>>() == vec![200]); } diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 0257911e8c0..4dea1fd75a4 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -252,7 +252,7 @@ pub mod traits { use super::*; use cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering, Equiv}; - use iter::{order, Iterator}; + use iter::order; use container::Container; impl<'a,T:PartialEq> PartialEq for &'a [T] { @@ -1141,7 +1141,6 @@ impl<'a, T:Clone> MutableCloneableVector<T> for &'a mut [T] { /// Unsafe operations pub mod raw { use mem::transmute; - use iter::Iterator; use ptr::RawPtr; use raw::Slice; use option::{None, Option, Some}; diff --git a/src/libcore/str.rs b/src/libcore/str.rs index c08f30152d5..936b698d4b1 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -867,7 +867,6 @@ static TAG_CONT_U8: u8 = 128u8; pub mod raw { use mem; use container::Container; - use iter::Iterator; use ptr::RawPtr; use raw::Slice; use slice::{ImmutableVector}; @@ -1725,7 +1724,7 @@ impl<'a> StrSlice<'a> for &'a str { fn is_alphanumeric(&self) -> bool { self.chars().all(char::is_alphanumeric) } #[inline] - fn char_len(&self) -> uint { self.chars().len() } + fn char_len(&self) -> uint { self.chars().count() } #[inline] fn slice(&self, begin: uint, end: uint) -> &'a str { |
