diff options
| author | Felix S. Klock II <pnkfelix@pnkfx.org> | 2014-03-06 23:33:46 +0100 |
|---|---|---|
| committer | Felix S. Klock II <pnkfelix@pnkfx.org> | 2014-03-12 07:51:49 +0100 |
| commit | da19563dbce08e2a78720010fe458007baa3703a (patch) | |
| tree | 556c984d636e83eef92bdcd6c818831d10493005 /src/libstd | |
| parent | 8a32ee7444f9d9e3b8ea38ead0814cf13dd6e7cc (diff) | |
| download | rust-da19563dbce08e2a78720010fe458007baa3703a.tar.gz rust-da19563dbce08e2a78720010fe458007baa3703a.zip | |
Port partition method from ~[T] to Vec<T>, for use early-late lifetime code.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/vec_ng.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/libstd/vec_ng.rs b/src/libstd/vec_ng.rs index 76fd68a5265..eed5143b7cf 100644 --- a/src/libstd/vec_ng.rs +++ b/src/libstd/vec_ng.rs @@ -64,6 +64,26 @@ impl<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> { |
