@@ -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 + 1 u;
225+ start + = i + 1 u;
226226 count -= 1 u;
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] {
13271300pub 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
13321306impl < ' 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 ( 0 u) ) ;
2845- assert_eq ! ( position_elem( v1 , & 2 ) , Some ( 1 u) ) ;
2846- assert_eq ! ( position_elem( v1 , & 5 ) , Some ( 5 u) ) ;
2847- assert ! ( position_elem( v1 , & 4 ) . is_none( ) ) ;
2824+ assert_eq ! ( v1 . position_elem( & 1 ) , Some ( 0 u) ) ;
2825+ assert_eq ! ( v1 . position_elem( & 2 ) , Some ( 1 u) ) ;
2826+ assert_eq ! ( v1 . position_elem( & 5 ) , Some ( 5 u) ) ;
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 ( 3 u) ) ;
2857- assert ! ( rposition( v , g) . is_none( ) ) ;
2836+ assert_eq ! ( v . rposition( f) , Some ( 3 u) ) ;
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 }
0 commit comments