Skip to content

Commit ae2f185

Browse files
committed
Convert vec::{partition, partitioned} to methods.
1 parent 206d4f0 commit ae2f185

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

src/libextra/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ fn run_tests(opts: &TestOpts,
431431
callback(TeFiltered(filtered_descs));
432432
433433
let (filtered_tests, filtered_benchs) =
434-
do vec::partition(filtered_tests) |e| {
434+
do filtered_tests.partition |e| {
435435
match e.testfn {
436436
StaticTestFn(_) | DynTestFn(_) => true,
437437
StaticBenchFn(_) | DynBenchFn(_) => false

src/libstd/vec.rs

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)