From 49c74524e2c5a2a81ce4cbe2c50a507c0be9f24e Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 17 Jun 2013 19:43:22 -0400 Subject: 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`. --- src/libstd/iterator.rs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'src/libstd/iterator.rs') 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 { /// ~~~ fn fold(&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 { /// ~~~ {.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 { /// Return the index of the first element satisfying the specified predicate fn position_(&mut self, predicate: &fn(A) -> bool) -> Option; + + /// 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> IteratorUtil 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> IteratorUtil 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); + } } -- cgit 1.4.1-3-g733a5 From 883c966d5c6ba93bf8834543500b01f644ab362f Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 18 Jun 2013 22:59:48 -0400 Subject: vec: replace `position` with `iter().position_` --- src/libextra/getopts.rs | 2 +- src/libextra/json.rs | 2 +- src/librustc/middle/trans/common.rs | 3 +- src/librustc/middle/trans/expr.rs | 3 +- src/librustc/middle/ty.rs | 2 +- src/librustc/middle/typeck/check/method.rs | 7 ++--- src/libstd/iterator.rs | 2 +- src/libstd/vec.rs | 46 +++--------------------------- 8 files changed, 13 insertions(+), 54 deletions(-) (limited to 'src/libstd/iterator.rs') diff --git a/src/libextra/getopts.rs b/src/libextra/getopts.rs index 05649104c31..5980bd7c9e4 100644 --- a/src/libextra/getopts.rs +++ b/src/libextra/getopts.rs @@ -177,7 +177,7 @@ fn name_str(nm: &Name) -> ~str { } fn find_opt(opts: &[Opt], nm: Name) -> Option { - vec::position(opts, |opt| opt.name == nm) + opts.iter().position_(|opt| opt.name == nm) } /** diff --git a/src/libextra/json.rs b/src/libextra/json.rs index db95327f0aa..0bbccd4df4a 100644 --- a/src/libextra/json.rs +++ b/src/libextra/json.rs @@ -950,7 +950,7 @@ impl serialize::Decoder for Decoder { } ref json => fail!("invalid variant: %?", *json), }; - let idx = match vec::position(names, |n| str::eq_slice(*n, name)) { + let idx = match names.iter().position_(|n| str::eq_slice(*n, name)) { Some(idx) => idx, None => fail!("Unknown variant name: %?", name), }; diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index fc79c8c23d4..e9a0d42f29b 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -468,8 +468,7 @@ pub fn add_clean_free(cx: block, ptr: ValueRef, heap: heap) { pub fn revoke_clean(cx: block, val: ValueRef) { do in_scope_cx(cx) |scope_info| { let scope_info = &mut *scope_info; // FIXME(#5074) workaround borrowck - let cleanup_pos = vec::position( - scope_info.cleanups, + let cleanup_pos = scope_info.cleanups.iter().position_( |cu| match *cu { clean_temp(v, _, _) if v == val => true, _ => false diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index 3cbbd61aed6..d1467edd94d 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -1152,8 +1152,7 @@ fn trans_rec_or_struct(bcx: block, let mut need_base = vec::from_elem(field_tys.len(), true); let numbered_fields = do fields.map |field| { - let opt_pos = vec::position(field_tys, |field_ty| - field_ty.ident == field.node.ident); + let opt_pos = field_tys.iter().position_(|field_ty| field_ty.ident == field.node.ident); match opt_pos { Some(i) => { need_base[i] = false; diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index a4702808746..2340ffc76b3 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3372,7 +3372,7 @@ pub fn field_idx_strict(tcx: ty::ctxt, id: ast::ident, fields: &[field]) } pub fn method_idx(id: ast::ident, meths: &[@Method]) -> Option { - vec::position(meths, |m| m.ident == id) + meths.iter().position_(|m| m.ident == id) } /// Returns a vector containing the indices of all type parameters that appear diff --git a/src/librustc/middle/typeck/check/method.rs b/src/librustc/middle/typeck/check/method.rs index bdf88cb6df4..2b7f7d0f77c 100644 --- a/src/librustc/middle/typeck/check/method.rs +++ b/src/librustc/middle/typeck/check/method.rs @@ -420,7 +420,7 @@ impl<'self> LookupContext<'self> { let tcx = self.tcx(); let ms = ty::trait_methods(tcx, did); - let index = match vec::position(*ms, |m| m.ident == self.m_name) { + let index = match ms.iter().position_(|m| m.ident == self.m_name) { Some(i) => i, None => { return; } // no method with the right name }; @@ -474,7 +474,7 @@ impl<'self> LookupContext<'self> { // First, try self methods let mut method_info: Option = None; let methods = ty::trait_methods(tcx, did); - match vec::position(*methods, |m| m.ident == self.m_name) { + match methods.iter().position_(|m| m.ident == self.m_name) { Some(i) => { method_info = Some(MethodInfo { method_ty: methods[i], @@ -489,8 +489,7 @@ impl<'self> LookupContext<'self> { for ty::trait_supertraits(tcx, did).each() |trait_ref| { let supertrait_methods = ty::trait_methods(tcx, trait_ref.def_id); - match vec::position(*supertrait_methods, - |m| m.ident == self.m_name) { + match supertrait_methods.iter().position_(|m| m.ident == self.m_name) { Some(i) => { method_info = Some(MethodInfo { method_ty: supertrait_methods[i], diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 57f23d8e657..394066f1d4c 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, Eq}; +use cmp::Ord; use clone::Clone; /// An interface for dealing with "external iterators". These types of iterators diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index ad03d8fc4a0..ce5662c6d4b 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -19,7 +19,7 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater}; use clone::Clone; use old_iter::BaseIter; use old_iter; -use iterator::{Iterator}; +use iterator::{Iterator, IteratorUtil}; use iter::FromIter; use kinds::Copy; use libc; @@ -1107,18 +1107,7 @@ pub fn rfind_between(v: &[T], /// Find the first index containing a matching value pub fn position_elem(v: &[T], x: &T) -> Option { - position(v, |y| *x == *y) -} - -/** - * 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. - */ -pub fn position(v: &[T], f: &fn(t: &T) -> bool) -> Option { - position_between(v, 0u, v.len(), f) + v.iter().position_(|y| *x == *y) } /** @@ -3146,18 +3135,6 @@ mod tests { assert!(position_elem(v1, &4).is_none()); } - #[test] - fn test_position() { - fn less_than_three(i: &int) -> bool { *i < 3 } - fn is_eighteen(i: &int) -> bool { *i == 18 } - - assert!(position([], less_than_three).is_none()); - - let v1 = ~[5, 4, 3, 2, 1]; - assert_eq!(position(v1, less_than_three), Some(3u)); - assert!(position(v1, is_eighteen).is_none()); - } - #[test] fn test_position_between() { assert!(position_between([], 0u, 0u, f).is_none()); @@ -3234,8 +3211,8 @@ mod tests { fn g(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'd' } let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')]; - assert_eq!(position(v, f), Some(1u)); - assert!(position(v, g).is_none()); + assert_eq!(rposition(v, f), Some(3u)); + assert!(rposition(v, g).is_none()); } #[test] @@ -3877,21 +3854,6 @@ mod tests { }; } - #[test] - #[ignore(windows)] - #[should_fail] - fn test_position_fail() { - let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)]; - let mut i = 0; - do position(v) |_elt| { - if i == 2 { - fail!() - } - i += 0; - false - }; - } - #[test] #[ignore(windows)] #[should_fail] -- cgit 1.4.1-3-g733a5