1
Fork 0

vec: replace position with iter().position_

This commit is contained in:
Daniel Micay 2013-06-18 22:59:48 -04:00
parent 49c74524e2
commit 883c966d5c
8 changed files with 13 additions and 54 deletions

View file

@ -177,7 +177,7 @@ fn name_str(nm: &Name) -> ~str {
} }
fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> { fn find_opt(opts: &[Opt], nm: Name) -> Option<uint> {
vec::position(opts, |opt| opt.name == nm) opts.iter().position_(|opt| opt.name == nm)
} }
/** /**

View file

@ -950,7 +950,7 @@ impl serialize::Decoder for Decoder {
} }
ref json => fail!("invalid variant: %?", *json), 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, Some(idx) => idx,
None => fail!("Unknown variant name: %?", name), None => fail!("Unknown variant name: %?", name),
}; };

View file

@ -468,8 +468,7 @@ pub fn add_clean_free(cx: block, ptr: ValueRef, heap: heap) {
pub fn revoke_clean(cx: block, val: ValueRef) { pub fn revoke_clean(cx: block, val: ValueRef) {
do in_scope_cx(cx) |scope_info| { do in_scope_cx(cx) |scope_info| {
let scope_info = &mut *scope_info; // FIXME(#5074) workaround borrowck let scope_info = &mut *scope_info; // FIXME(#5074) workaround borrowck
let cleanup_pos = vec::position( let cleanup_pos = scope_info.cleanups.iter().position_(
scope_info.cleanups,
|cu| match *cu { |cu| match *cu {
clean_temp(v, _, _) if v == val => true, clean_temp(v, _, _) if v == val => true,
_ => false _ => false

View file

@ -1152,8 +1152,7 @@ fn trans_rec_or_struct(bcx: block,
let mut need_base = vec::from_elem(field_tys.len(), true); let mut need_base = vec::from_elem(field_tys.len(), true);
let numbered_fields = do fields.map |field| { let numbered_fields = do fields.map |field| {
let opt_pos = vec::position(field_tys, |field_ty| let opt_pos = field_tys.iter().position_(|field_ty| field_ty.ident == field.node.ident);
field_ty.ident == field.node.ident);
match opt_pos { match opt_pos {
Some(i) => { Some(i) => {
need_base[i] = false; need_base[i] = false;

View file

@ -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<uint> { pub fn method_idx(id: ast::ident, meths: &[@Method]) -> Option<uint> {
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 /// Returns a vector containing the indices of all type parameters that appear

View file

@ -420,7 +420,7 @@ impl<'self> LookupContext<'self> {
let tcx = self.tcx(); let tcx = self.tcx();
let ms = ty::trait_methods(tcx, did); 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, Some(i) => i,
None => { return; } // no method with the right name None => { return; } // no method with the right name
}; };
@ -474,7 +474,7 @@ impl<'self> LookupContext<'self> {
// First, try self methods // First, try self methods
let mut method_info: Option<MethodInfo> = None; let mut method_info: Option<MethodInfo> = None;
let methods = ty::trait_methods(tcx, did); 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) => { Some(i) => {
method_info = Some(MethodInfo { method_info = Some(MethodInfo {
method_ty: methods[i], method_ty: methods[i],
@ -489,8 +489,7 @@ impl<'self> LookupContext<'self> {
for ty::trait_supertraits(tcx, did).each() |trait_ref| { for ty::trait_supertraits(tcx, did).each() |trait_ref| {
let supertrait_methods = let supertrait_methods =
ty::trait_methods(tcx, trait_ref.def_id); ty::trait_methods(tcx, trait_ref.def_id);
match vec::position(*supertrait_methods, match supertrait_methods.iter().position_(|m| m.ident == self.m_name) {
|m| m.ident == self.m_name) {
Some(i) => { Some(i) => {
method_info = Some(MethodInfo { method_info = Some(MethodInfo {
method_ty: supertrait_methods[i], method_ty: supertrait_methods[i],

View file

@ -22,7 +22,7 @@ use iter::{FromIter, Times};
use num::{Zero, One}; use num::{Zero, One};
use option::{Option, Some, None}; use option::{Option, Some, None};
use ops::{Add, Mul}; use ops::{Add, Mul};
use cmp::{Ord, Eq}; use cmp::Ord;
use clone::Clone; use clone::Clone;
/// An interface for dealing with "external iterators". These types of iterators /// An interface for dealing with "external iterators". These types of iterators

View file

@ -19,7 +19,7 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use clone::Clone; use clone::Clone;
use old_iter::BaseIter; use old_iter::BaseIter;
use old_iter; use old_iter;
use iterator::{Iterator}; use iterator::{Iterator, IteratorUtil};
use iter::FromIter; use iter::FromIter;
use kinds::Copy; use kinds::Copy;
use libc; use libc;
@ -1107,18 +1107,7 @@ pub fn rfind_between<T:Copy>(v: &[T],
/// Find the first index containing a matching value /// Find the first index containing a matching value
pub fn position_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> { pub fn position_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
position(v, |y| *x == *y) v.iter().position_(|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<T>(v: &[T], f: &fn(t: &T) -> bool) -> Option<uint> {
position_between(v, 0u, v.len(), f)
} }
/** /**
@ -3146,18 +3135,6 @@ mod tests {
assert!(position_elem(v1, &4).is_none()); 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] #[test]
fn test_position_between() { fn test_position_between() {
assert!(position_between([], 0u, 0u, f).is_none()); 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' } fn g(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'd' }
let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')]; let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
assert_eq!(position(v, f), Some(1u)); assert_eq!(rposition(v, f), Some(3u));
assert!(position(v, g).is_none()); assert!(rposition(v, g).is_none());
} }
#[test] #[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] #[test]
#[ignore(windows)] #[ignore(windows)]
#[should_fail] #[should_fail]