Skip to content

Commit a890c2c

Browse files
committed
Convert vec::{rposition, rposition_elem, position_elem, contains} to methods.
1 parent 45940ed commit a890c2c

File tree

11 files changed

+50
-74
lines changed

11 files changed

+50
-74
lines changed

src/libextra/smallintmap.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,6 @@ mod test_set {
383383

384384
use super::SmallIntSet;
385385

386-
use std::vec;
387-
388386
#[test]
389387
fn test_disjoint() {
390388
let mut xs = SmallIntSet::new();
@@ -456,7 +454,7 @@ mod test_set {
456454
let mut i = 0;
457455
let expected = [3, 5, 11, 77];
458456
for a.intersection(&b) |x| {
459-
assert!(vec::contains(expected, x));
457+
assert!(expected.contains(x));
460458
i += 1
461459
}
462460
assert_eq!(i, expected.len());
@@ -479,7 +477,7 @@ mod test_set {
479477
let mut i = 0;
480478
let expected = [1, 5, 11];
481479
for a.difference(&b) |x| {
482-
assert!(vec::contains(expected, x));
480+
assert!(expected.contains(x));
483481
i += 1
484482
}
485483
assert_eq!(i, expected.len());
@@ -504,7 +502,7 @@ mod test_set {
504502
let mut i = 0;
505503
let expected = [1, 5, 11, 14, 22];
506504
for a.symmetric_difference(&b) |x| {
507-
assert!(vec::contains(expected, x));
505+
assert!(expected.contains(x));
508506
i += 1
509507
}
510508
assert_eq!(i, expected.len());
@@ -533,7 +531,7 @@ mod test_set {
533531
let mut i = 0;
534532
let expected = [1, 3, 5, 9, 11, 13, 16, 19, 24];
535533
for a.union(&b) |x| {
536-
assert!(vec::contains(expected, x));
534+
assert!(expected.contains(x));
537535
i += 1
538536
}
539537
assert_eq!(i, expected.len());

src/librustc/metadata/cstore.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use metadata::cstore;
1717
use metadata::decoder;
1818

1919
use std::hashmap::HashMap;
20-
use std::vec;
2120
use extra;
2221
use syntax::ast;
2322
use syntax::parse::token::ident_interner;
@@ -91,7 +90,7 @@ pub fn iter_crate_data(cstore: &CStore,
9190
}
9291

9392
pub fn add_used_crate_file(cstore: &mut CStore, lib: &Path) {
94-
if !vec::contains(cstore.used_crate_files, lib) {
93+
if !cstore.used_crate_files.contains(lib) {
9594
cstore.used_crate_files.push(copy *lib);
9695
}
9796
}

src/librustc/middle/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ pub fn missing_ctor(cx: &MatchCheckCtxt,
363363
for m.iter().advance |r| {
364364
let r = pat_ctor_id(cx, r[0]);
365365
for r.iter().advance |id| {
366-
if !vec::contains(found, id) {
366+
if !found.contains(id) {
367367
found.push(/*bad*/copy *id);
368368
}
369369
}

src/librustc/middle/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,7 +2323,7 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
23232323
false
23242324
}
23252325

2326-
ty_struct(ref did, _) if vec::contains(*seen, did) => {
2326+
ty_struct(ref did, _) if seen.contains(did) => {
23272327
false
23282328
}
23292329

@@ -2339,7 +2339,7 @@ pub fn is_instantiable(cx: ctxt, r_ty: t) -> bool {
23392339
ts.iter().any_(|t| type_requires(cx, seen, r_ty, *t))
23402340
}
23412341

2342-
ty_enum(ref did, _) if vec::contains(*seen, did) => {
2342+
ty_enum(ref did, _) if seen.contains(did) => {
23432343
false
23442344
}
23452345

@@ -3266,7 +3266,7 @@ pub fn occurs_check(tcx: ctxt, sp: span, vid: TyVid, rt: t) {
32663266
if !type_needs_infer(rt) { return; }
32673267

32683268
// Occurs check!
3269-
if vec::contains(vars_in_type(rt), &vid) {
3269+
if vars_in_type(rt).contains(&vid) {
32703270
// Maybe this should be span_err -- however, there's an
32713271
// assertion later on that the type doesn't contain
32723272
// variables, so in this case we have to be sure to die.

src/librustc/middle/typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ pub fn do_autoderef(fcx: @mut FnCtxt, sp: span, t: ty::t) -> (ty::t, uint) {
985985
// concerned with this, as an error will be reported
986986
// on the enum definition as well because the enum is
987987
// not instantiable.
988-
if vec::contains(enum_dids, did) {
988+
if enum_dids.contains(did) {
989989
return (t1, autoderefs);
990990
}
991991
enum_dids.push(*did);
@@ -3156,7 +3156,7 @@ pub fn check_enum_variants(ccx: @mut CrateCtxt,
31563156
}
31573157
}
31583158
}
3159-
if vec::contains(*disr_vals, &*disr_val) {
3159+
if disr_vals.contains(&*disr_val) {
31603160
ccx.tcx.sess.span_err(v.span,
31613161
"discriminator value already exists");
31623162
}

src/librustc/middle/typeck/infer/resolve.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ use middle::typeck::infer::unify::{Root, UnifyInferCtxtMethods};
5757
use util::common::{indent, indenter};
5858
use util::ppaux::ty_to_str;
5959

60-
use std::vec;
6160
use syntax::ast;
6261

6362
pub static resolve_nested_tvar: uint = 0b0000000001;
@@ -204,7 +203,7 @@ impl ResolveState {
204203
}
205204

206205
pub fn resolve_ty_var(&mut self, vid: TyVid) -> ty::t {
207-
if vec::contains(self.v_seen, &vid) {
206+
if self.v_seen.contains(&vid) {
208207
self.err = Some(cyclic_ty(vid));
209208
return ty::mk_var(self.infcx.tcx, vid);
210209
} else {

src/librustpkg/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ fn rust_path_contents() {
596596
let cwd = os::getcwd().push(".rust");
597597
let parent = cwd.pop().pop().push(".rust");
598598
let grandparent = cwd.pop().pop().pop().push(".rust");
599-
assert!(vec::contains(p, &cwd));
600-
assert!(vec::contains(p, &parent));
601-
assert!(vec::contains(p, &grandparent));
599+
assert!(p.contains(&cwd));
600+
assert!(p.contains(&parent));
601+
assert!(p.contains(&grandparent));
602602
for p.iter().advance() |a_path| {
603603
assert!(!a_path.components.is_empty());
604604
}
@@ -609,9 +609,9 @@ fn rust_path_contents() {
609609
fn rust_path_parse() {
610610
os::setenv("RUST_PATH", "/a/b/c:/d/e/f:/g/h/i");
611611
let paths = rust_path();
612-
assert!(vec::contains(paths, &Path("/g/h/i")));
613-
assert!(vec::contains(paths, &Path("/d/e/f")));
614-
assert!(vec::contains(paths, &Path("/a/b/c")));
612+
assert!(paths.contains(&Path("/g/h/i")));
613+
assert!(paths.contains(&Path("/d/e/f")));
614+
assert!(paths.contains(&Path("/a/b/c")));
615615
os::unsetenv("RUST_PATH");
616616
}
617617

src/libstd/hashmap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ mod test_map {
939939
mod test_set {
940940
use super::*;
941941
use container::{Container, Map, Set};
942-
use vec;
942+
use vec::ImmutableEqVector;
943943
use uint;
944944

945945
#[test]
@@ -1030,7 +1030,7 @@ mod test_set {
10301030
let mut i = 0;
10311031
let expected = [3, 5, 11, 77];
10321032
for a.intersection(&b) |x| {
1033-
assert!(vec::contains(expected, x));
1033+
assert!(expected.contains(x));
10341034
i += 1
10351035
}
10361036
assert_eq!(i, expected.len());
@@ -1053,7 +1053,7 @@ mod test_set {
10531053
let mut i = 0;
10541054
let expected = [1, 5, 11];
10551055
for a.difference(&b) |x| {
1056-
assert!(vec::contains(expected, x));
1056+
assert!(expected.contains(x));
10571057
i += 1
10581058
}
10591059
assert_eq!(i, expected.len());
@@ -1079,7 +1079,7 @@ mod test_set {
10791079
let mut i = 0;
10801080
let expected = [-2, 1, 5, 11, 14, 22];
10811081
for a.symmetric_difference(&b) |x| {
1082-
assert!(vec::contains(expected, x));
1082+
assert!(expected.contains(x));
10831083
i += 1
10841084
}
10851085
assert_eq!(i, expected.len());
@@ -1109,7 +1109,7 @@ mod test_set {
11091109
let mut i = 0;
11101110
let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
11111111
for a.union(&b) |x| {
1112-
assert!(vec::contains(expected, x));
1112+
assert!(expected.contains(x));
11131113
i += 1
11141114
}
11151115
assert_eq!(i, expected.len());

src/libstd/os.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,10 +1544,10 @@ mod tests {
15441544
15451545
let mut e = env();
15461546
setenv(n, "VALUE");
1547-
assert!(!vec::contains(e, &(copy n, ~"VALUE")));
1547+
assert!(!e.contains(&(copy n, ~"VALUE")));
15481548
15491549
e = env();
1550-
assert!(vec::contains(e, &(n, ~"VALUE")));
1550+
assert!(e.contains(&(n, ~"VALUE")));
15511551
}
15521552
15531553
#[test]

src/libstd/vec.rs

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ pub fn splitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
220220
match v.slice(start, ln).iter().position_(|t| f(t)) {
221221
None => break,
222222
Some(i) => {
223-
result.push(v.slice(start, i).to_owned());
223+
result.push(v.slice(start, start + i).to_owned());
224224
// Make sure to skip the separator.
225-
start = i + 1u;
225+
start += i + 1u;
226226
count -= 1u;
227227
}
228228
}
@@ -646,36 +646,6 @@ impl<'self, T:Copy> VectorVector<T> for &'self [&'self [T]] {
646646
}
647647
}
648648

649-
/// Return true if a vector contains an element with the given value
650-
pub fn contains<T:Eq>(v: &[T], x: &T) -> bool {
651-
for v.iter().advance |elt| { if *x == *elt { return true; } }
652-
false
653-
}
654-
655-
/// Find the first index containing a matching value
656-
pub fn position_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
657-
v.iter().position_(|y| *x == *y)
658-
}
659-
660-
/// Find the last index containing a matching value
661-
pub fn rposition_elem<T:Eq>(v: &[T], x: &T) -> Option<uint> {
662-
rposition(v, |y| *x == *y)
663-
}
664-
665-
/**
666-
* Find the last index matching some predicate
667-
*
668-
* Apply function `f` to each element of `v` in reverse order. When function
669-
* `f` returns true then an option containing the index is returned. If `f`
670-
* matches no elements then none is returned.
671-
*/
672-
pub fn rposition<T>(v: &[T], f: &fn(t: &T) -> bool) -> Option<uint> {
673-
for v.rev_iter().enumerate().advance |(i, t)| {
674-
if f(t) { return Some(v.len() - i - 1); }
675-
}
676-
None
677-
}
678-
679649
/**
680650
* Binary search a sorted vector with a comparator function.
681651
*
@@ -1265,11 +1235,14 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
12651235
*
12661236
* Apply function `f` to each element of `v` in reverse order. When
12671237
* function `f` returns true then an option containing the index is
1268-
* returned. If `f` matches no elements then none is returned.
1238+
* returned. If `f` matches no elements then None is returned.
12691239
*/
12701240
#[inline]
12711241
fn rposition(&self, f: &fn(t: &T) -> bool) -> Option<uint> {
1272-
rposition(*self, f)
1242+
for self.rev_iter().enumerate().advance |(i, t)| {
1243+
if f(t) { return Some(self.len() - i - 1); }
1244+
}
1245+
None
12731246
}
12741247

12751248
/// 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] {
13271300
pub trait ImmutableEqVector<T:Eq> {
13281301
fn position_elem(&self, t: &T) -> Option<uint>;
13291302
fn rposition_elem(&self, t: &T) -> Option<uint>;
1303+
fn contains(&self, x: &T) -> bool;
13301304
}
13311305

13321306
impl<'self,T:Eq> ImmutableEqVector<T> for &'self [T] {
13331307
/// Find the first index containing a matching value
13341308
#[inline]
13351309
fn position_elem(&self, x: &T) -> Option<uint> {
1336-
position_elem(*self, x)
1310+
self.iter().position_(|y| *x == *y)
13371311
}
13381312

13391313
/// Find the last index containing a matching value
13401314
#[inline]
13411315
fn rposition_elem(&self, t: &T) -> Option<uint> {
1342-
rposition_elem(*self, t)
1316+
self.rposition(|x| *x == *t)
1317+
}
1318+
1319+
/// Return true if a vector contains an element with the given value
1320+
fn contains(&self, x: &T) -> bool {
1321+
for self.iter().advance |elt| { if *x == *elt { return true; } }
1322+
false
13431323
}
13441324
}
13451325

@@ -2838,13 +2818,13 @@ mod tests {
28382818

28392819
#[test]
28402820
fn test_position_elem() {
2841-
assert!(position_elem([], &1).is_none());
2821+
assert!([].position_elem(&1).is_none());
28422822

28432823
let v1 = ~[1, 2, 3, 3, 2, 5];
2844-
assert_eq!(position_elem(v1, &1), Some(0u));
2845-
assert_eq!(position_elem(v1, &2), Some(1u));
2846-
assert_eq!(position_elem(v1, &5), Some(5u));
2847-
assert!(position_elem(v1, &4).is_none());
2824+
assert_eq!(v1.position_elem(&1), Some(0u));
2825+
assert_eq!(v1.position_elem(&2), Some(1u));
2826+
assert_eq!(v1.position_elem(&5), Some(5u));
2827+
assert!(v1.position_elem(&4).is_none());
28482828
}
28492829

28502830
#[test]
@@ -2853,8 +2833,8 @@ mod tests {
28532833
fn g(xy: &(int, char)) -> bool { let (_x, y) = *xy; y == 'd' }
28542834
let v = ~[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'b')];
28552835

2856-
assert_eq!(rposition(v, f), Some(3u));
2857-
assert!(rposition(v, g).is_none());
2836+
assert_eq!(v.rposition(f), Some(3u));
2837+
assert!(v.rposition(g).is_none());
28582838
}
28592839

28602840
#[test]
@@ -3417,7 +3397,7 @@ mod tests {
34173397
fn test_rposition_fail() {
34183398
let v = [(~0, @0), (~0, @0), (~0, @0), (~0, @0)];
34193399
let mut i = 0;
3420-
do rposition(v) |_elt| {
3400+
do v.rposition |_elt| {
34213401
if i == 2 {
34223402
fail!()
34233403
}

src/test/bench/graph500-bfs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fn validate(edges: ~[(node_id, node_id)],
341341
}
342342
else {
343343
while parent != root {
344-
if vec::contains(path, &parent) {
344+
if path.contains(&parent) {
345345
status = false;
346346
}
347347

0 commit comments

Comments
 (0)