Convert vec::{rposition, rposition_elem, position_elem, contains} to methods.

This commit is contained in:
Huon Wilson 2013-06-29 02:08:32 +10:00
parent 45940ed988
commit a890c2cbf1
11 changed files with 50 additions and 74 deletions

View File

@ -383,8 +383,6 @@ mod test_set {
use super::SmallIntSet;
use std::vec;
#[test]
fn test_disjoint() {
let mut xs = SmallIntSet::new();
@ -456,7 +454,7 @@ mod test_set {
let mut i = 0;
let expected = [3, 5, 11, 77];
for a.intersection(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
@ -479,7 +477,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 5, 11];
for a.difference(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
@ -504,7 +502,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 5, 11, 14, 22];
for a.symmetric_difference(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
@ -533,7 +531,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 3, 5, 9, 11, 13, 16, 19, 24];
for a.union(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());

View File

@ -17,7 +17,6 @@ use metadata::cstore;
use metadata::decoder;
use std::hashmap::HashMap;
use std::vec;
use extra;
use syntax::ast;
use syntax::parse::token::ident_interner;
@ -91,7 +90,7 @@ pub fn iter_crate_data(cstore: &CStore,
}
pub fn add_used_crate_file(cstore: &mut CStore, lib: &Path) {
if !vec::contains(cstore.used_crate_files, lib) {
if !cstore.used_crate_files.contains(lib) {
cstore.used_crate_files.push(copy *lib);
}
}

View File

@ -363,7 +363,7 @@ pub fn missing_ctor(cx: &MatchCheckCtxt,
for m.iter().advance |r| {
let r = pat_ctor_id(cx, r[0]);
for r.iter().advance |id| {
if !vec::contains(found, id) {
if !found.contains(id) {
found.push(/*bad*/copy *id);
}
}

View File

@ -2323,7 +2323,7 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
false
}
ty_struct(ref did, _) if vec::contains(*seen, did) => {
ty_struct(ref did, _) if seen.contains(did) => {
false
}
@ -2339,7 +2339,7 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
ts.iter().any_(|t| type_requires(cx, seen, r_ty, *t))
}
ty_enum(ref did, _) if vec::contains(*seen, did) => {
ty_enum(ref did, _) if seen.contains(did) => {
false
}
@ -3266,7 +3266,7 @@ pub fn occurs_check(tcx: ctxt, sp: span, vid: TyVid, rt: t) {
if !type_needs_infer(rt) { return; }
// Occurs check!
if vec::contains(vars_in_type(rt), &vid) {
if vars_in_type(rt).contains(&vid) {
// Maybe this should be span_err -- however, there's an
// assertion later on that the type doesn't contain
// variables, so in this case we have to be sure to die.

View File

@ -985,7 +985,7 @@ pub fn do_autoderef(fcx: @mut FnCtxt, sp: span, t: ty::t) -> (ty::t, uint) {
// concerned with this, as an error will be reported
// on the enum definition as well because the enum is
// not instantiable.
if vec::contains(enum_dids, did) {
if enum_dids.contains(did) {
return (t1, autoderefs);
}
enum_dids.push(*did);
@ -3156,7 +3156,7 @@ pub fn check_enum_variants(ccx: @mut CrateCtxt,
}
}
}
if vec::contains(*disr_vals, &*disr_val) {
if disr_vals.contains(&*disr_val) {
ccx.tcx.sess.span_err(v.span,
"discriminator value already exists");
}

View File

@ -57,7 +57,6 @@ use middle::typeck::infer::unify::{Root, UnifyInferCtxtMethods};
use util::common::{indent, indenter};
use util::ppaux::ty_to_str;
use std::vec;
use syntax::ast;
pub static resolve_nested_tvar: uint = 0b0000000001;
@ -204,7 +203,7 @@ impl ResolveState {
}
pub fn resolve_ty_var(&mut self, vid: TyVid) -> ty::t {
if vec::contains(self.v_seen, &vid) {
if self.v_seen.contains(&vid) {
self.err = Some(cyclic_ty(vid));
return ty::mk_var(self.infcx.tcx, vid);
} else {

View File

@ -596,9 +596,9 @@ fn rust_path_contents() {
let cwd = os::getcwd().push(".rust");
let parent = cwd.pop().pop().push(".rust");
let grandparent = cwd.pop().pop().pop().push(".rust");
assert!(vec::contains(p, &cwd));
assert!(vec::contains(p, &parent));
assert!(vec::contains(p, &grandparent));
assert!(p.contains(&cwd));
assert!(p.contains(&parent));
assert!(p.contains(&grandparent));
for p.iter().advance() |a_path| {
assert!(!a_path.components.is_empty());
}
@ -609,9 +609,9 @@ fn rust_path_contents() {
fn rust_path_parse() {
os::setenv("RUST_PATH", "/a/b/c:/d/e/f:/g/h/i");
let paths = rust_path();
assert!(vec::contains(paths, &Path("/g/h/i")));
assert!(vec::contains(paths, &Path("/d/e/f")));
assert!(vec::contains(paths, &Path("/a/b/c")));
assert!(paths.contains(&Path("/g/h/i")));
assert!(paths.contains(&Path("/d/e/f")));
assert!(paths.contains(&Path("/a/b/c")));
os::unsetenv("RUST_PATH");
}

View File

@ -939,7 +939,7 @@ mod test_map {
mod test_set {
use super::*;
use container::{Container, Map, Set};
use vec;
use vec::ImmutableEqVector;
use uint;
#[test]
@ -1030,7 +1030,7 @@ mod test_set {
let mut i = 0;
let expected = [3, 5, 11, 77];
for a.intersection(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
@ -1053,7 +1053,7 @@ mod test_set {
let mut i = 0;
let expected = [1, 5, 11];
for a.difference(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
@ -1079,7 +1079,7 @@ mod test_set {
let mut i = 0;
let expected = [-2, 1, 5, 11, 14, 22];
for a.symmetric_difference(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());
@ -1109,7 +1109,7 @@ mod test_set {
let mut i = 0;
let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
for a.union(&b) |x| {
assert!(vec::contains(expected, x));
assert!(expected.contains(x));
i += 1
}
assert_eq!(i, expected.len());

View File

@ -1544,10 +1544,10 @@ mod tests {
let mut e = env();
setenv(n, "VALUE");
assert!(!vec::contains(e, &(copy n, ~"VALUE")));
assert!(!e.contains(&(copy n, ~"VALUE")));
e = env();
assert!(vec::contains(e, &(n, ~"VALUE")));
assert!(e.contains(&(n, ~"VALUE")));
}
#[test]

View File

@ -220,9 +220,9 @@ pub fn splitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
match v.slice(start, ln).iter().position_(|t| f(t)) {
None => break,
Some(i) => {
result.push(v.slice(start, i).to_owned());
result.push(v.slice(start, start + i).to_owned());
// Make sure to skip the separator.
start = i + 1u;
start += i + 1u;
count -= 1u;
}
}
@ -646,36 +646,6 @@ impl<'self, T:Copy> VectorVector<T> for &'self [&'self [T]] {
}
}
/// Return true if a vector contains an element with the given value
pub fn contains<T:Eq>(v: &[T], x: &T) -> bool {
for v.iter().advance |elt| { if *x == *elt { return true; } }
false
}
/// Find the first index containing a matching value
pub fn position_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
v.iter().position_(|y| *x == *y)
}
/// Find the last index containing a matching value
pub fn rposition_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
rposition(v, |y| *x == *y)
}
/**
* Find the last index matching some predicate
*
* Apply function `f` to each element of `v` in reverse order. When function
* `f` returns true then an option containing the index is returned. If `f`
* matches no elements then none is returned.
*/
pub fn rposition<T>(v: &[T], f: &fn(t: &T) -> bool) -> Option<uint> {
for v.rev_iter().enumerate().advance |(i, t)| {
if f(t) { return Some(v.len() - i - 1); }
}
None
}
/**
* Binary search a sorted vector with a comparator function.
*
@ -1265,11 +1235,14 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
*
* Apply function `f` to each element of `v` in reverse order. When
* function `f` returns true then an option containing the index is
* returned. If `f` matches no elements then none is returned.
* returned. If `f` matches no elements then None is returned.
*/
#[inline]
fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint> {
rposition(*self, f)
for self.rev_iter().enumerate().advance |(i, t)| {
if f(t) { return Some(self.len() - i - 1); }
}
None
}
/// Apply a function to each element of a vector and return the results
@ -1327,19 +1300,26 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
pub trait ImmutableEqVector<T:Eq> {
fn position_elem(&self, t: &T) -> Option<uint>;
fn rposition_elem(&self, t: &T) -> Option<uint>;
fn contains(&self, x: &T) -> bool;
}
impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
/// Find the first index containing a matching value
#[inline]
fn position_elem(&self, x: &T) -> Option<uint> {
position_elem(*self, x)
self.iter().position_(|y| *x == *y)
}
/// Find the last index containing a matching value
#[inline]
fn rposition_elem(&self, t: &T) -> Option<uint> {
rposition_elem(*self, t)
self.rposition(|x| *x == *t)
}
/// Return true if a vector contains an element with the given value
fn contains(&self, x: &T) -> bool {
for self.iter().advance |elt| { if *x == *elt { return true; } }
false
}
}
@ -2838,13 +2818,13 @@ mod tests {
#[test]
fn test_position_elem() {
assert!(position_elem([], &1).is_none());
assert!([].position_elem(&1).is_none());
let v1 = ~[1, 2, 3, 3, 2, 5];
assert_eq!(position_elem(v1, &1), Some(0u));
assert_eq!(position_elem(v1, &2), Some(1u));
assert_eq!(position_elem(v1, &5), Some(5u));
assert!(position_elem(v1, &4).is_none());
assert_eq!(v1.position_elem(&1), Some(0u));
assert_eq!(v1.position_elem(&2), Some(1u));
assert_eq!(v1.position_elem(&5), Some(5u));
assert!(v1.position_elem(&4).is_none());
}
#[test]
@ -2853,8 +2833,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!(rposition(v, f), Some(3u));
assert!(rposition(v, g).is_none());
assert_eq!(v.rposition(f), Some(3u));
assert!(v.rposition(g).is_none());
}
#[test]
@ -3417,7 +3397,7 @@ mod tests {
fn test_rposition_fail() {
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
let mut i = 0;
do rposition(v) |_elt| {
do v.rposition |_elt| {
if i == 2 {
fail!()
}

View File

@ -341,7 +341,7 @@ fn validate(edges: ~[(node_id, node_id)],
}
else {
while parent != root {
if vec::contains(path, &parent) {
if path.contains(&parent) {
status = false;
}