-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add a range argument to vec.extract_if #133265
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
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 |
---|---|---|
|
@@ -3615,12 +3615,15 @@ impl<T, A: Allocator> Vec<T, A> { | |
Splice { drain: self.drain(range), replace_with: replace_with.into_iter() } | ||
} | ||
|
||
/// Creates an iterator which uses a closure to determine if an element should be removed. | ||
/// Creates an iterator which uses a closure to determine if element in the range should be removed. | ||
/// | ||
/// If the closure returns true, then the element is removed and yielded. | ||
/// If the closure returns false, the element will remain in the vector and will not be yielded | ||
/// by the iterator. | ||
/// | ||
/// Only elements that fall in the provided range are considered for extraction, but any elements | ||
/// after the range will still have to be moved if any element has been extracted. | ||
/// | ||
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating | ||
/// or the iteration short-circuits, then the remaining elements will be retained. | ||
/// Use [`retain`] with a negated predicate if you do not need the returned iterator. | ||
|
@@ -3630,10 +3633,12 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// Using this method is equivalent to the following code: | ||
/// | ||
/// ``` | ||
/// # use std::cmp::min; | ||
/// # let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 6 }; | ||
/// # let mut vec = vec![1, 2, 3, 4, 5, 6]; | ||
Contributor
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. Apologies for the late comment, but I wanted to point out that this sample code doesn't behave the same way as the real Fortunately, the real let some_predicate = |x: &mut i32| { *x == 2 || *x == 3 || *x == 5 };
let mut vec = vec![0, 1, 2, 3, 4, 5, 6];
let range = 1..5;
let mut i = range.start;
let mut end = range.end;
while i < min(vec.len(), end) {
if some_predicate(&mut vec[i]) {
let _val = vec.remove(i);
end -= 1;
// your code here
} else {
i += 1;
}
}
assert_eq!(vec, vec![0, 1, 4, 5, 6]); 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. If you want to propose a documentation improvement you can just open a PR. |
||
/// let mut i = 0; | ||
/// while i < vec.len() { | ||
/// # let range = 1..4; | ||
/// let mut i = range.start; | ||
/// while i < min(vec.len(), range.end) { | ||
/// if some_predicate(&mut vec[i]) { | ||
/// let val = vec.remove(i); | ||
/// // your code here | ||
|
@@ -3648,8 +3653,12 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// But `extract_if` is easier to use. `extract_if` is also more efficient, | ||
/// because it can backshift the elements of the array in bulk. | ||
/// | ||
/// Note that `extract_if` also lets you mutate every element in the filter closure, | ||
/// regardless of whether you choose to keep or remove it. | ||
/// Note that `extract_if` also lets you mutate the elements passed to the filter closure, | ||
/// regardless of whether you choose to keep or remove them. | ||
/// | ||
/// # Panics | ||
/// | ||
/// If `range` is out of bounds. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -3659,25 +3668,29 @@ impl<T, A: Allocator> Vec<T, A> { | |
/// #![feature(extract_if)] | ||
/// let mut numbers = vec![1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15]; | ||
/// | ||
/// let evens = numbers.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>(); | ||
/// let evens = numbers.extract_if(.., |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]); | ||
/// ``` | ||
/// | ||
/// Using the range argument to only process a part of the vector: | ||
/// | ||
/// ``` | ||
/// #![feature(extract_if)] | ||
/// let mut items = vec![0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 2, 1, 2]; | ||
/// let ones = items.extract_if(7.., |x| *x == 1).collect::<Vec<_>>(); | ||
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. Maybe change one of the first few 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. Hrm, I wanted to show the "I have a prefix that I know doesn't contain those things, so I can skip them" use. |
||
/// assert_eq!(items, vec![0, 0, 0, 0, 0, 0, 0, 2, 2, 2]); | ||
/// assert_eq!(ones.len(), 3); | ||
/// ``` | ||
#[unstable(feature = "extract_if", reason = "recently added", issue = "43244")] | ||
pub fn extract_if<F>(&mut self, filter: F) -> ExtractIf<'_, T, F, A> | ||
pub fn extract_if<F, R>(&mut self, range: R, filter: F) -> ExtractIf<'_, T, F, A> | ||
where | ||
F: FnMut(&mut T) -> bool, | ||
R: RangeBounds<usize>, | ||
{ | ||
let old_len = self.len(); | ||
|
||
// Guard against us getting leaked (leak amplification) | ||
unsafe { | ||
self.set_len(0); | ||
} | ||
|
||
ExtractIf { vec: self, idx: 0, del: 0, old_len, pred: filter } | ||
ExtractIf::new(self, filter, range) | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.