Skip to content

Commit 66e7608

Browse files
committed
---
yaml --- r: 38356 b: refs/heads/try c: 9a7e261 h: refs/heads/master v: v3
1 parent 37cdd94 commit 66e7608

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 09bb07bed9166105ea961a42b5fff7739ae0d2e9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
5-
refs/heads/try: c55787d00905dced9dd5fd9c5b8c92928a8c2eaa
5+
refs/heads/try: 9a7e26156259560ac546a35dd285abe44728b1f5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: a810c03263670238bccd64cabb12a23a46e3a278

branches/try/src/libcore/vec.rs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,46 @@ pub fn rsplitn<T: Copy>(v: &[T], n: uint, f: fn(t: &T) -> bool) -> ~[~[T]] {
405405
result
406406
}
407407

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+
408448
// Mutators
409449

410450
/// Removes the first element from a vector and return it
@@ -1664,7 +1704,6 @@ impl<T> &[T]: ImmutableVector<T> {
16641704
pure fn filter_map<U: Copy>(&self, f: fn(t: &T) -> Option<U>) -> ~[U] {
16651705
filter_map(*self, f)
16661706
}
1667-
16681707
}
16691708

16701709
pub trait ImmutableEqVector<T: Eq> {
@@ -1714,8 +1753,8 @@ impl<T: Eq> &[T]: ImmutableEqVector<T> {
17141753

17151754
pub trait ImmutableCopyableVector<T> {
17161755
pure fn filter(&self, f: fn(t: &T) -> bool) -> ~[T];
1717-
17181756
pure fn rfind(&self, f: fn(t: &T) -> bool) -> Option<T>;
1757+
pure fn partitioned(&self, f: fn(&T) -> bool) -> (~[T], ~[T]);
17191758
}
17201759

17211760
/// Extension methods for vectors
@@ -1743,6 +1782,15 @@ impl<T: Copy> &[T]: ImmutableCopyableVector<T> {
17431782
pure fn rfind(&self, f: fn(t: &T) -> bool) -> Option<T> {
17441783
rfind(*self, f)
17451784
}
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+
}
17461794
}
17471795

17481796
pub trait OwnedVector<T> {
@@ -1757,6 +1805,7 @@ pub trait OwnedVector<T> {
17571805
fn truncate(&mut self, newlen: uint);
17581806
fn retain(&mut self, f: pure fn(t: &T) -> bool);
17591807
fn consume(self, f: fn(uint, v: T));
1808+
fn partition(self, f: pure fn(&T) -> bool) -> (~[T], ~[T]);
17601809
}
17611810

17621811
impl<T> ~[T]: OwnedVector<T> {
@@ -1814,6 +1863,15 @@ impl<T> ~[T]: OwnedVector<T> {
18141863
fn consume(self, f: fn(uint, v: T)) {
18151864
consume(self, f)
18161865
}
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+
}
18171875
}
18181876

18191877
pub trait OwnedCopyableVector<T: Copy> {
@@ -3105,6 +3163,25 @@ mod tests {
31053163
~[~[1, 2, 3, 4], ~[5]];
31063164
}
31073165

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+
31083185
#[test]
31093186
#[should_fail]
31103187
#[ignore(cfg(windows))]

0 commit comments

Comments
 (0)