@@ -405,6 +405,46 @@ pub fn rsplitn<T: Copy>(v: &[T], n: uint, f: fn(t: &T) -> bool) -> ~[~[T]] {
405
405
result
406
406
}
407
407
408
+ /**
409
+ * Partitions a vector into two new vectors: those that satisfies the
410
+ * predicate, and those that do not.
411
+ */
412
+ pub fn partition < T > ( v : ~[ T ] , f : fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
413
+ let mut lefts = ~[ ] ;
414
+ let mut rights = ~[ ] ;
415
+
416
+ do v. consume |_, elt| {
417
+ if f ( & elt) {
418
+ lefts. push ( elt) ;
419
+ } else {
420
+ rights. push ( elt) ;
421
+ }
422
+ }
423
+
424
+ ( lefts, rights)
425
+ }
426
+
427
+ /**
428
+ * Partitions a vector into two new vectors: those that satisfies the
429
+ * predicate, and those that do not.
430
+ */
431
+ pub pure fn partitioned < T : Copy > ( v : & [ T ] , f : fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
432
+ let mut lefts = ~[ ] ;
433
+ let mut rights = ~[ ] ;
434
+
435
+ for each( v) |elt| {
436
+ unsafe {
437
+ if f ( elt) {
438
+ lefts. push ( * elt) ;
439
+ } else {
440
+ rights. push ( * elt) ;
441
+ }
442
+ }
443
+ }
444
+
445
+ ( lefts, rights)
446
+ }
447
+
408
448
// Mutators
409
449
410
450
/// Removes the first element from a vector and return it
@@ -1664,7 +1704,6 @@ impl<T> &[T]: ImmutableVector<T> {
1664
1704
pure fn filter_map < U : Copy > ( & self , f : fn ( t : & T ) -> Option < U > ) -> ~[ U ] {
1665
1705
filter_map ( * self , f)
1666
1706
}
1667
-
1668
1707
}
1669
1708
1670
1709
pub trait ImmutableEqVector < T : Eq > {
@@ -1714,8 +1753,8 @@ impl<T: Eq> &[T]: ImmutableEqVector<T> {
1714
1753
1715
1754
pub trait ImmutableCopyableVector < T > {
1716
1755
pure fn filter ( & self , f : fn ( t : & T ) -> bool ) -> ~[ T ] ;
1717
-
1718
1756
pure fn rfind ( & self , f : fn ( t : & T ) -> bool ) -> Option < T > ;
1757
+ pure fn partitioned ( & self , f : fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) ;
1719
1758
}
1720
1759
1721
1760
/// Extension methods for vectors
@@ -1743,6 +1782,15 @@ impl<T: Copy> &[T]: ImmutableCopyableVector<T> {
1743
1782
pure fn rfind ( & self , f : fn ( t : & T ) -> bool ) -> Option < T > {
1744
1783
rfind ( * self , f)
1745
1784
}
1785
+
1786
+ /**
1787
+ * Partitions the vector into those that satisfies the predicate, and
1788
+ * those that do not.
1789
+ */
1790
+ #[ inline]
1791
+ pure fn partitioned ( & self , f : fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
1792
+ partitioned ( * self , f)
1793
+ }
1746
1794
}
1747
1795
1748
1796
pub trait OwnedVector < T > {
@@ -1757,6 +1805,7 @@ pub trait OwnedVector<T> {
1757
1805
fn truncate ( & mut self , newlen : uint ) ;
1758
1806
fn retain ( & mut self , f : pure fn( t : & T ) -> bool ) ;
1759
1807
fn consume ( self , f : fn ( uint , v : T ) ) ;
1808
+ fn partition ( self , f : pure fn( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) ;
1760
1809
}
1761
1810
1762
1811
impl < T > ~[ T ] : OwnedVector < T > {
@@ -1814,6 +1863,15 @@ impl<T> ~[T]: OwnedVector<T> {
1814
1863
fn consume ( self , f : fn ( uint , v : T ) ) {
1815
1864
consume ( self , f)
1816
1865
}
1866
+
1867
+ /**
1868
+ * Partitions the vector into those that satisfies the predicate, and
1869
+ * those that do not.
1870
+ */
1871
+ #[ inline]
1872
+ fn partition ( self , f : fn ( & T ) -> bool ) -> ( ~[ T ] , ~[ T ] ) {
1873
+ partition ( self , f)
1874
+ }
1817
1875
}
1818
1876
1819
1877
pub trait OwnedCopyableVector < T : Copy > {
@@ -3105,6 +3163,25 @@ mod tests {
3105
3163
~[ ~[ 1 , 2 , 3 , 4 ] , ~[ 5 ] ] ;
3106
3164
}
3107
3165
3166
+ #[ test]
3167
+ fn test_partition ( ) {
3168
+ assert ( ~[ ] ) . partition ( |x : & int | * x < 3 ) == ( ~[ ] , ~[ ] ) ;
3169
+ assert ( ~[ 1 , 2 , 3 ] ) . partition ( |x : & int | * x < 4 ) == ( ~[ 1 , 2 , 3 ] , ~[ ] ) ;
3170
+ assert ( ~[ 1 , 2 , 3 ] ) . partition ( |x : & int | * x < 2 ) == ( ~[ 1 ] , ~[ 2 , 3 ] ) ;
3171
+ assert ( ~[ 1 , 2 , 3 ] ) . partition ( |x : & int | * x < 0 ) == ( ~[ ] , ~[ 1 , 2 , 3 ] ) ;
3172
+ }
3173
+
3174
+ #[ test]
3175
+ fn test_partitioned ( ) {
3176
+ assert ( ~[ ] ) . partitioned ( |x : & int | * x < 3 ) == ( ~[ ] , ~[ ] ) ;
3177
+ assert ( ~[ 1 , 2 , 3 ] ) . partitioned ( |x : & int | * x < 4 ) ==
3178
+ ( ~[ 1 , 2 , 3 ] , ~[ ] ) ;
3179
+ assert ( ~[ 1 , 2 , 3 ] ) . partitioned ( |x : & int | * x < 2 ) ==
3180
+ ( ~[ 1 ] , ~[ 2 , 3 ] ) ;
3181
+ assert ( ~[ 1 , 2 , 3 ] ) . partitioned ( |x : & int | * x < 0 ) ==
3182
+ ( ~[ ] , ~[ 1 , 2 , 3 ] ) ;
3183
+ }
3184
+
3108
3185
#[ test]
3109
3186
#[ should_fail]
3110
3187
#[ ignore( cfg( windows) ) ]
0 commit comments