-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Vec::drain_filter #43245
Add Vec::drain_filter #43245
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1960,6 +1960,65 @@ impl<T> Vec<T> { | |
} | ||
} | ||
|
||
/// Creates an iterator which uses a closure to determine if an element should be removed. | ||
/// | ||
/// If the closure returns true, then the element is removed and yielded. | ||
/// If the closure returns false, it will try again, and call the closure | ||
/// on the next element, seeing if it passes the test. | ||
/// | ||
/// Using this method is equivalent to the following code: | ||
/// | ||
/// ``` | ||
/// # let some_predicate = |x: &mut i32| { *x == 2 }; | ||
/// # let mut vec = vec![1, 2, 3, 4, 5]; | ||
/// let mut i = 0; | ||
/// while i != vec.len() { | ||
/// if some_predicate(&mut vec[i]) { | ||
/// let val = vec.remove(i); | ||
/// // your code here | ||
/// } | ||
/// i += 1; | ||
/// } | ||
/// ``` | ||
/// | ||
/// But `drain_filter` is easier to use. `drain_filter` is also more efficient, | ||
/// because it can backshift the elements of the array in bulk. | ||
/// | ||
/// Note that `drain_filter` also lets you mutate every element in the filter closure, | ||
/// regardless of whether you choose to keep or remove it. | ||
/// | ||
/// | ||
/// # Examples | ||
/// | ||
/// Splitting an array into evens and odds, reusing the original allocation: | ||
/// | ||
/// ``` | ||
/// #![feature(drain_filter)] | ||
/// let mut numbers = vec![1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]; | ||
/// | ||
/// let evens = numbers.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>(); | ||
/// let odds = numbers; | ||
/// | ||
/// assert_eq!(evens, vec![2, 4, 6, 8, 14]); | ||
/// assert_eq!(odds, vec![1, 3, 5, 9, 11, 13, 15]); | ||
/// ``` | ||
#[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] | ||
pub fn drain_filter<F>(&mut self, filter: F) -> DrainFilter<T, F> | ||
where F: FnMut(&mut T) -> bool, | ||
{ | ||
let old_len = self.len(); | ||
|
||
// Guard against us getting leaked (leak amplification) | ||
unsafe { self.set_len(0); } | ||
|
||
DrainFilter { | ||
vec: self, | ||
idx: 0, | ||
del: 0, | ||
old_len, | ||
pred: filter, | ||
} | ||
} | ||
} | ||
|
||
#[stable(feature = "extend_ref", since = "1.2.0")] | ||
|
@@ -2596,3 +2655,57 @@ impl<'a, T> Drain<'a, T> { | |
self.tail_start = new_tail_start; | ||
} | ||
} | ||
|
||
/// An iterator produced by calling `drain_filter` on Vec. | ||
#[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] | ||
#[derive(Debug)] | ||
pub struct DrainFilter<'a, T: 'a, F> | ||
where F: FnMut(&mut T) -> bool, | ||
{ | ||
vec: &'a mut Vec<T>, | ||
idx: usize, | ||
del: usize, | ||
old_len: usize, | ||
pred: F, | ||
} | ||
|
||
#[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] | ||
impl<'a, T, F> Iterator for DrainFilter<'a, T, F> | ||
where F: FnMut(&mut T) -> bool, | ||
{ | ||
type Item = T; | ||
|
||
fn next(&mut self) -> Option<T> { | ||
unsafe { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would personally not make the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's also needed for the ptr::read. At that point might as well just say the whole thing is unsafe and call it a day (this matches many parts of this file). |
||
while self.idx != self.old_len { | ||
let i = self.idx; | ||
self.idx += 1; | ||
let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len); | ||
if (self.pred)(&mut v[i]) { | ||
self.del += 1; | ||
return Some(ptr::read(&v[i])); | ||
} else if self.del > 0 { | ||
v.swap(i - self.del, i); | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
(0, Some(self.old_len - self.idx)) | ||
} | ||
} | ||
|
||
#[unstable(feature = "drain_filter", reason = "recently added", issue = "43244")] | ||
impl<'a, T, F> Drop for DrainFilter<'a, T, F> | ||
where F: FnMut(&mut T) -> bool, | ||
{ | ||
fn drop(&mut self) { | ||
for _ in self.by_ref() { } | ||
|
||
unsafe { | ||
self.vec.set_len(self.old_len - self.del); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would skip every item after a removed item.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, haha! Need to put the +=1 in an else