@@ -337,46 +337,6 @@ pub fn rsplitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
337337 result
338338}
339339
340- /**
341- * Partitions a vector into two new vectors: those that satisfies the
342- * predicate, and those that do not.
343- */
344- pub fn partition < T > ( v : ~[ T ] , f : & fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
345- let mut lefts = ~[ ] ;
346- let mut rights = ~[ ] ;
347-
348- // FIXME (#4355 maybe): using v.consume here crashes
349- // do v.consume |_, elt| {
350- do consume( v) |_, elt| {
351- if f ( & elt) {
352- lefts. push ( elt) ;
353- } else {
354- rights. push ( elt) ;
355- }
356- }
357-
358- ( lefts, rights)
359- }
360-
361- /**
362- * Partitions a vector into two new vectors: those that satisfies the
363- * predicate, and those that do not.
364- */
365- pub fn partitioned < T : Copy > ( v : & [ T ] , f : & fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
366- let mut lefts = ~[ ] ;
367- let mut rights = ~[ ] ;
368-
369- for v. iter( ) . advance |elt| {
370- if f( elt) {
371- lefts. push( copy * elt) ;
372- } else {
373- rights. push( copy * elt) ;
374- }
375- }
376-
377- ( lefts, rights)
378- }
379-
380340/// Consumes all elements, in a vector, moving them out into the / closure
381341/// provided. The vector is traversed from the start to the end.
382342///
@@ -1572,7 +1532,18 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
15721532 */
15731533 #[ inline]
15741534 fn partitioned( & self , f : & fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
1575- partitioned ( * self , f)
1535+ let mut lefts = ~[ ] ;
1536+ let mut rights = ~[ ] ;
1537+
1538+ for self . iter( ) . advance |elt| {
1539+ if f( elt) {
1540+ lefts. push( copy * elt) ;
1541+ } else {
1542+ rights. push( copy * elt) ;
1543+ }
1544+ }
1545+
1546+ ( lefts, rights)
15761547 }
15771548
15781549 /// Returns the element at the given index, without doing bounds checking.
@@ -1842,7 +1813,18 @@ impl<T> OwnedVector<T> for ~[T] {
18421813 */
18431814 #[ inline]
18441815 fn partition ( self , f : & fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
1845- partition ( self , f)
1816+ let mut lefts = ~[ ] ;
1817+ let mut rights = ~[ ] ;
1818+
1819+ do self. consume |_, elt| {
1820+ if f ( & elt) {
1821+ lefts. push ( elt) ;
1822+ } else {
1823+ rights. push ( elt) ;
1824+ }
1825+ }
1826+
1827+ ( lefts, rights)
18461828 }
18471829
18481830 #[ inline]
@@ -3228,11 +3210,10 @@ mod tests {
32283210
32293211 #[ test]
32303212 fn test_partition ( ) {
3231- // FIXME (#4355 maybe): using v.partition here crashes
3232- assert_eq ! ( partition( ~[ ] , |x: & int| * x < 3 ) , ( ~[ ] , ~[ ] ) ) ;
3233- assert_eq ! ( partition( ~[ 1 , 2 , 3 ] , |x: & int| * x < 4 ) , ( ~[ 1 , 2 , 3 ] , ~[ ] ) ) ;
3234- assert_eq ! ( partition( ~[ 1 , 2 , 3 ] , |x: & int| * x < 2 ) , ( ~[ 1 ] , ~[ 2 , 3 ] ) ) ;
3235- assert_eq ! ( partition( ~[ 1 , 2 , 3 ] , |x: & int| * x < 0 ) , ( ~[ ] , ~[ 1 , 2 , 3 ] ) ) ;
3213+ assert_eq ! ( ( ~[ ] ) . partition( |x: & int| * x < 3 ) , ( ~[ ] , ~[ ] ) ) ;
3214+ assert_eq ! ( ( ~[ 1 , 2 , 3 ] ) . partition( |x: & int| * x < 4 ) , ( ~[ 1 , 2 , 3 ] , ~[ ] ) ) ;
3215+ assert_eq ! ( ( ~[ 1 , 2 , 3 ] ) . partition( |x: & int| * x < 2 ) , ( ~[ 1 ] , ~[ 2 , 3 ] ) ) ;
3216+ assert_eq ! ( ( ~[ 1 , 2 , 3 ] ) . partition( |x: & int| * x < 0 ) , ( ~[ ] , ~[ 1 , 2 , 3 ] ) ) ;
32363217 }
32373218
32383219 #[ test]
0 commit comments