diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-06-17 19:43:22 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-06-21 03:20:22 -0400 |
| commit | 49c74524e2c5a2a81ce4cbe2c50a507c0be9f24e (patch) | |
| tree | 441c718864c414bd4f7750c2435edc100e4a1841 /src/libstd | |
| parent | cbad1da3db7eda0911e988fb6255ac5c16961aa7 (diff) | |
| download | rust-49c74524e2c5a2a81ce4cbe2c50a507c0be9f24e.tar.gz rust-49c74524e2c5a2a81ce4cbe2c50a507c0be9f24e.zip | |
vec: rm old_iter implementations, except BaseIter
The removed test for issue #2611 is well covered by the `std::iterator` module itself. This adds the `count` method to `IteratorUtil` to replace `EqIter`.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io.rs | 4 | ||||
| -rw-r--r-- | src/libstd/iterator.rs | 39 | ||||
| -rw-r--r-- | src/libstd/old_iter.rs | 2 | ||||
| -rw-r--r-- | src/libstd/str.rs | 2 | ||||
| -rw-r--r-- | src/libstd/task/local_data_priv.rs | 4 | ||||
| -rw-r--r-- | src/libstd/unstable/extfmt.rs | 2 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 187 |
7 files changed, 49 insertions, 191 deletions
diff --git a/src/libstd/io.rs b/src/libstd/io.rs index e5e8a4cb601..715f2fdabd3 100644 --- a/src/libstd/io.rs +++ b/src/libstd/io.rs @@ -65,7 +65,7 @@ use str::StrSlice; use to_str::ToStr; use uint; use vec; -use vec::{OwnedVector, OwnedCopyableVector}; +use vec::{OwnedVector, OwnedCopyableVector, CopyableVector}; #[allow(non_camel_case_types)] // not sure what to do about this pub type fd_t = c_int; @@ -698,7 +698,7 @@ impl<T:Reader> ReaderUtil for T { // over-read by reading 1-byte per char needed nbread = if ncreq > nbreq { ncreq } else { nbreq }; if nbread > 0 { - bytes = vec::slice(bytes, offset, bytes.len()).to_vec(); + bytes = vec::slice(bytes, offset, bytes.len()).to_owned(); } } chars diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index eefad1a03dc..57f23d8e657 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -22,7 +22,7 @@ use iter::{FromIter, Times}; use num::{Zero, One}; use option::{Option, Some, None}; use ops::{Add, Mul}; -use cmp::Ord; +use cmp::{Ord, Eq}; use clone::Clone; /// An interface for dealing with "external iterators". These types of iterators @@ -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); + } } diff --git a/src/libstd/old_iter.rs b/src/libstd/old_iter.rs index 83bb9eb0908..9b87d76a309 100644 --- a/src/libstd/old_iter.rs +++ b/src/libstd/old_iter.rs @@ -16,7 +16,7 @@ #[allow(missing_doc)]; -use cmp::{Eq}; +use cmp::Eq; use kinds::Copy; use option::{None, Option, Some}; use vec; diff --git a/src/libstd/str.rs b/src/libstd/str.rs index 04f5247782b..fd11b49a00a 100644 --- a/src/libstd/str.rs +++ b/src/libstd/str.rs @@ -1317,7 +1317,7 @@ impl<'self> StrSlice<'self> for &'self str { } /// Returns the number of characters that a string holds #[inline] - fn char_len(&self) -> uint { self.iter().count() } + fn char_len(&self) -> uint { self.iter().len_() } /** * Returns a slice of the given string from the byte range diff --git a/src/libstd/task/local_data_priv.rs b/src/libstd/task/local_data_priv.rs index f6b14a51539..0956b76c970 100644 --- a/src/libstd/task/local_data_priv.rs +++ b/src/libstd/task/local_data_priv.rs @@ -142,7 +142,7 @@ unsafe fn local_data_lookup<T: 'static>( -> Option<(uint, *libc::c_void)> { let key_value = key_to_key_value(key); - let map_pos = (*map).position(|entry| + let map_pos = (*map).iter().position_(|entry| match *entry { Some((k,_,_)) => k == key_value, None => false @@ -215,7 +215,7 @@ pub unsafe fn local_set<T: 'static>( } None => { // Find an empty slot. If not, grow the vector. - match (*map).position(|x| x.is_none()) { + match (*map).iter().position_(|x| x.is_none()) { Some(empty_index) => { map[empty_index] = new_entry; } None => { map.push(new_entry); } } diff --git a/src/libstd/unstable/extfmt.rs b/src/libstd/unstable/extfmt.rs index d466488ce5e..87bd25bdad3 100644 --- a/src/libstd/unstable/extfmt.rs +++ b/src/libstd/unstable/extfmt.rs @@ -350,7 +350,7 @@ pub mod ct { #[test] fn test_parse_flags() { fn pack(fs: &[Flag]) -> uint { - fs.foldl(0, |&p, &f| p | (1 << f as uint)) + fs.iter().fold(0, |p, &f| p | (1 << f as uint)) } fn test(s: &str, flags: &[Flag], next: uint) { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 0d5a84a4a76..ad03d8fc4a0 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -24,7 +24,6 @@ use iter::FromIter; use kinds::Copy; use libc; use num::Zero; -use old_iter::CopyableIter; use option::{None, Option, Some}; use ptr::to_unsafe_ptr; use ptr; @@ -324,12 +323,12 @@ pub fn split<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { match position_between(v, start, ln, f) { None => break, Some(i) => { - result.push(slice(v, start, i).to_vec()); + result.push(slice(v, start, i).to_owned()); start = i + 1u; } } } - result.push(slice(v, start, ln).to_vec()); + result.push(slice(v, start, ln).to_owned()); result } @@ -348,14 +347,14 @@ pub fn splitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { match position_between(v, start, ln, f) { None => break, Some(i) => { - result.push(slice(v, start, i).to_vec()); + result.push(slice(v, start, i).to_owned()); // Make sure to skip the separator. start = i + 1u; count -= 1u; } } } - result.push(slice(v, start, ln).to_vec()); + result.push(slice(v, start, ln).to_owned()); result } @@ -373,12 +372,12 @@ pub fn rsplit<T:Copy>(v: &[T], f: &fn(t: &T) -> bool) -> ~[~[T]] { match rposition_between(v, 0, end, f) { None => break, Some(i) => { - result.push(slice(v, i + 1, end).to_vec()); + result.push(slice(v, i + 1, end).to_owned()); end = i; } } } - result.push(slice(v, 0u, end).to_vec()); + result.push(slice(v, 0u, end).to_owned()); reverse(result); result } @@ -398,14 +397,14 @@ pub fn rsplitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] { match rposition_between(v, 0u, end, f) { None => break, Some(i) => { - result.push(slice(v, i + 1u, end).to_vec()); + result.push(slice(v, i + 1u, end).to_owned()); // Make sure to skip the separator. end = i; count -= 1u; } } } - result.push(slice(v, 0u, end).to_vec()); + result.push(slice(v, 0u, end).to_owned()); reverse(result); result } @@ -1792,7 +1791,6 @@ pub trait ImmutableVector<'self, T> { fn initn(&self, n: uint) -> &'self [T]; fn last(&self) -> &'self T; fn last_opt(&self) -> Option<&'self T>; - fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint>; fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint>; fn map<U>(&self, f: &fn(t: &T) -> U) -> ~[U]; fn mapi<U>(&self, f: &fn(uint, t: &T) -> U) -> ~[U]; @@ -1861,18 +1859,6 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] { fn last_opt(&self) -> Option<&'self T> { last_opt(*self) } /** - * Find the first index matching some predicate - * - * Apply function `f` to each element of `v`. When function `f` returns - * true then an option containing the index is returned. If `f` matches no - * elements then none is returned. - */ - #[inline] - fn position(&self, f: &fn(t: &T) -> bool) -> Option<uint> { - position(*self, f) - } - - /** * Find the last index matching some predicate * * Apply function `f` to each element of `v` in reverse order. When @@ -2434,9 +2420,6 @@ pub mod bytes { } } -// ___________________________________________________________________________ -// ITERATION TRAIT METHODS - impl<'self,A> old_iter::BaseIter<A> for &'self [A] { #[inline] fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) -> bool { @@ -2446,152 +2429,6 @@ impl<'self,A> old_iter::BaseIter<A> for &'self [A] { fn size_hint(&self) -> Option<uint> { Some(self.len()) } } -// FIXME(#4148): This should be redundant -impl<A> old_iter::BaseIter<A> for ~[A] { - #[inline] - fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) -> bool { - each(*self, blk) - } - #[inline] - fn size_hint(&self) -> Option<uint> { Some(self.len()) } -} - -// FIXME(#4148): This should be redundant -impl<A> old_iter::BaseIter<A> for @[A] { - #[inline] - fn each<'a>(&'a self, blk: &fn(v: &'a A) -> bool) -> bool { - each(*self, blk) - } - #[inline] - fn size_hint(&self) -> Option<uint> { Some(self.len()) } -} - -impl<'self,A> old_iter::ExtendedIter<A> for &'self [A] { - pub fn eachi(&self, blk: &fn(uint, v: &A) -> bool) -> bool { - old_iter::eachi(self, blk) - } - pub fn all(&self, blk: &fn(&A) -> bool) -> bool { - old_iter::all(self, blk) - } - pub fn any(&self, blk: &fn(&A) -> bool) -> bool { - old_iter::any(self, blk) - } - pub fn foldl<B>(&self, b0: B, blk: &fn(&B, &A) -> B) -> B { - old_iter::foldl(self, b0, blk) - } - pub fn position(&self, f: &fn(&A) -> bool) -> Option<uint> { - old_iter::position(self, f) - } - fn map_to_vec<B>(&self, op: &fn(&A) -> B) -> ~[B] { - old_iter::map_to_vec(self, op) - } - fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: &fn(&A) -> IB) - -> ~[B] { - old_iter::flat_map_to_vec(self, op) - } -} - -// FIXME(#4148): This should be redundant -impl<A> old_iter::ExtendedIter<A> for ~[A] { - pub fn eachi(&self, blk: &fn(uint, v: &A) -> bool) -> bool { - old_iter::eachi(self, blk) - } - pub fn all(&self, blk: &fn(&A) -> bool) -> bool { - old_iter::all(self, blk) - } - pub fn any(&self, blk: &fn(&A) -> bool) -> bool { - old_iter::any(self, blk) - } - pub fn foldl<B>(&self, b0: B, blk: &fn(&B, &A) -> B) -> B { - old_iter::foldl(self, b0, blk) - } - pub fn position(&self, f: &fn(&A) -> bool) -> Option<uint> { - old_iter::position(self, f) - } - fn map_to_vec<B>(&self, op: &fn(&A) -> B) -> ~[B] { - old_iter::map_to_vec(self, op) - } - fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: &fn(&A) -> IB) - -> ~[B] { - old_iter::flat_map_to_vec(self, op) - } -} - -// FIXME(#4148): This should be redundant -impl<A> old_iter::ExtendedIter<A> for @[A] { - pub fn eachi(&self, blk: &fn(uint, v: &A) -> bool) -> bool { - old_iter::eachi(self, blk) - } - pub fn all(&self, blk: &fn(&A) -> bool) -> bool { - old_iter::all(self, blk) - } - pub fn any(&self, blk: &fn(&A) -> bool) -> bool { - old_iter::any(self, blk) - } - pub fn foldl<B>(&self, b0: B, blk: &fn(&B, &A) -> B) -> B { - old_iter::foldl(self, b0, blk) - } - pub fn position(&self, f: &fn(&A) -> bool) -> Option<uint> { - old_iter::position(self, f) - } - fn map_to_vec<B>(&self, op: &fn(&A) -> B) -> ~[B] { - old_iter::map_to_vec(self, op) - } - fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: &fn(&A) -> IB) - -> ~[B] { - old_iter::flat_map_to_vec(self, op) - } -} - -impl<'self,A:Eq> old_iter::EqIter<A> for &'self [A] { - pub fn contains(&self, x: &A) -> bool { old_iter::contains(self, x) } - pub fn count(&self, x: &A) -> uint { old_iter::count(self, x) } -} - -// FIXME(#4148): This should be redundant -impl<A:Eq> old_iter::EqIter<A> for ~[A] { - pub fn contains(&self, x: &A) -> bool { old_iter::contains(self, x) } - pub fn count(&self, x: &A) -> uint { old_iter::count(self, x) } -} - -// FIXME(#4148): This should be redundant -impl<A:Eq> old_iter::EqIter<A> for @[A] { - pub fn contains(&self, x: &A) -> bool { old_iter::contains(self, x) } - pub fn count(&self, x: &A) -> uint { old_iter::count(self, x) } -} - -impl<'self,A:Copy> old_iter::CopyableIter<A> for &'self [A] { - fn filter_to_vec(&self, pred: &fn(&A) -> bool) -> ~[A] { - old_iter::filter_to_vec(self, pred) - } - fn to_vec(&self) -> ~[A] { old_iter::to_vec(self) } - pub fn find(&self, f: &fn(&A) -> bool) -> Option<A> { - old_iter::find(self, f) - } -} - -// FIXME(#4148): This should be redundant -impl<A:Copy> old_iter::CopyableIter<A> for ~[A] { - fn filter_to_vec(&self, pred: &fn(&A) -> bool) -> ~[A] { - old_iter::filter_to_vec(self, pred) - } - fn to_vec(&self) -> ~[A] { old_iter::to_vec(self) } - pub fn find(&self, f: &fn(&A) -> bool) -> Option<A> { - old_iter::find(self, f) - } -} - -// FIXME(#4148): This should be redundant -impl<A:Copy> old_iter::CopyableIter<A> for @[A] { - fn filter_to_vec(&self, pred: &fn(&A) -> bool) -> ~[A] { - old_iter::filter_to_vec(self, pred) - } - fn to_vec(&self) -> ~[A] { old_iter::to_vec(self) } - pub fn find(&self, f: &fn(&A) -> bool) -> Option<A> { - old_iter::find(self, f) - } -} - impl<A:Clone> Clone for ~[A] { #[inline] fn clone(&self) -> ~[A] { @@ -2916,7 +2753,7 @@ mod tests { fn test_slice() { // Test fixed length vector. let vec_fixed = [1, 2, 3, 4]; - let v_a = slice(vec_fixed, 1u, vec_fixed.len()).to_vec(); + let v_a = slice(vec_fixed, 1u, vec_fixed.len()).to_owned(); assert_eq!(v_a.len(), 3u); assert_eq!(v_a[0], 2); assert_eq!(v_a[1], 3); @@ -2924,14 +2761,14 @@ mod tests { // Test on stack. let vec_stack = &[1, 2, 3]; - let v_b = slice(vec_stack, 1u, 3u).to_vec(); + let v_b = slice(vec_stack, 1u, 3u).to_owned(); assert_eq!(v_b.len(), 2u); assert_eq!(v_b[0], 2); assert_eq!(v_b[1], 3); // Test on managed heap. let vec_managed = @[1, 2, 3, 4, 5]; - let v_c = slice(vec_managed, 0u, 3u).to_vec(); + let v_c = slice(vec_managed, 0u, 3u).to_owned(); assert_eq!(v_c.len(), 3u); assert_eq!(v_c[0], 1); assert_eq!(v_c[1], 2); @@ -2939,7 +2776,7 @@ mod tests { // Test on exchange heap. let vec_unique = ~[1, 2, 3, 4, 5, 6]; - let v_d = slice(vec_unique, 1u, 6u).to_vec(); + let v_d = slice(vec_unique, 1u, 6u).to_owned(); assert_eq!(v_d.len(), 5u); assert_eq!(v_d[0], 2); assert_eq!(v_d[1], 3); |
