@@ -337,46 +337,6 @@ pub fn rsplitn<T:Copy>(v: &[T], n: uint, f: &fn(t: &T) -> bool) -> ~[~[T]] {
337
337
result
338
338
}
339
339
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
-
380
340
/// Consumes all elements, in a vector, moving them out into the / closure
381
341
/// provided. The vector is traversed from the start to the end.
382
342
///
@@ -1572,7 +1532,18 @@ impl<'self,T:Copy> ImmutableCopyableVector<T> for &'self [T] {
1572
1532
*/
1573
1533
#[ inline]
1574
1534
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)
1576
1547
}
1577
1548
1578
1549
/// Returns the element at the given index, without doing bounds checking.
@@ -1842,7 +1813,18 @@ impl<T> OwnedVector<T> for ~[T] {
1842
1813
*/
1843
1814
#[ inline]
1844
1815
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)
1846
1828
}
1847
1829
1848
1830
#[ inline]
@@ -3228,11 +3210,10 @@ mod tests {
3228
3210
3229
3211
#[ test]
3230
3212
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 ] ) ) ;
3236
3217
}
3237
3218
3238
3219
#[ test]
0 commit comments