diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-29 02:08:32 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-06-30 21:15:24 +1000 |
| commit | a890c2cbf1dd92d79ade5bd8f04142ae5511943f (patch) | |
| tree | 4cb60f44f0f4d64d87f8a09937951adea5a8479d /src/libstd | |
| parent | 45940ed988ab2d04e1dbecceef440071b04c11a9 (diff) | |
| download | rust-a890c2cbf1dd92d79ade5bd8f04142ae5511943f.tar.gz rust-a890c2cbf1dd92d79ade5bd8f04142ae5511943f.zip | |
Convert vec::{rposition, rposition_elem, position_elem, contains} to methods.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/hashmap.rs | 10 | ||||
| -rw-r--r-- | src/libstd/os.rs | 4 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 68 |
3 files changed, 31 insertions, 51 deletions
diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 35db229b65d..85dca1154bc 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -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()); diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 1fbcda12dce..6d13c662ee5 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -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] diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index a704e604aa2..e2b0c54b9a4 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -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!() } |
