mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-17 05:25:37 +03:00
Port partition method from ~[T] to Vec<T>, for use early-late lifetime code.
This commit is contained in:
@@ -64,6 +64,26 @@ pub fn from_fn(length: uint, op: |uint| -> T) -> Vec<T> {
|
||||
xs
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions the vector into two vectors `(A,B)`, where all
|
||||
* elements of `A` satisfy `f` and all elements of `B` do not.
|
||||
*/
|
||||
#[inline]
|
||||
pub fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>) {
|
||||
let mut lefts = Vec::new();
|
||||
let mut rights = Vec::new();
|
||||
|
||||
for elt in self.move_iter() {
|
||||
if f(&elt) {
|
||||
lefts.push(elt);
|
||||
} else {
|
||||
rights.push(elt);
|
||||
}
|
||||
}
|
||||
|
||||
(lefts, rights)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Vec<T> {
|
||||
|
||||
Reference in New Issue
Block a user