Skip to content

Commit da19563

Browse files
committed
Port partition method from ~[T] to Vec<T>, for use early-late lifetime code.
1 parent 8a32ee7 commit da19563

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/libstd/vec_ng.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ impl<T> Vec<T> {
6464
xs
6565
}
6666
}
67+
68+
/**
69+
* Partitions the vector into two vectors `(A,B)`, where all
70+
* elements of `A` satisfy `f` and all elements of `B` do not.
71+
*/
72+
#[inline]
73+
pub fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) {
74+
let mut lefts = Vec::new();
75+
let mut rights = Vec::new();
76+
77+
for elt in self.move_iter() {
78+
if f(&elt) {
79+
lefts.push(elt);
80+
} else {
81+
rights.push(elt);
82+
}
83+
}
84+
85+
(lefts, rights)
86+
}
6787
}
6888

6989
impl<T: Clone> Vec<T> {

0 commit comments

Comments
 (0)