-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Implement extract_if for BinaryHeap #154317
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
Open
Nokel81
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
Nokel81:extract-if-binary-heap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+170
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| use core::fmt; | ||
| use core::iter::FusedIterator; | ||
| use core::mem::ManuallyDrop; | ||
|
|
||
| use super::BinaryHeap; | ||
| use crate::alloc::{Allocator, Global}; | ||
|
|
||
| /// An iterator which uses a closure to determine if an element should be removed. | ||
| /// | ||
| /// This struct is created by [`BinaryHeap::extract_if`]. | ||
| /// See its documentation for more. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// #![feature(binary_heap_extract_if)] | ||
| /// use crate::alloc::collections::BinaryHeap; | ||
| /// | ||
| /// let mut heap: BinaryHeap<u32> = (0..128).collect(); | ||
| /// let iter: Vec<u32> = heap.extract_if(|x| *x % 2 == 0).collect(); | ||
| #[unstable(feature = "binary_heap_extract_if", issue = "154721")] | ||
| #[must_use = "iterators are lazy and do nothing unless consumed; \ | ||
| use `retain_mut` or `extract_if().for_each(drop)` to remove and discard elements"] | ||
| pub struct ExtractIf< | ||
| 'a, | ||
| T: Ord, | ||
| F, | ||
| #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global, | ||
| > { | ||
| heap_ptr: *mut BinaryHeap<T, A>, | ||
| extract_if: ManuallyDrop<super::vec::ExtractIf<'a, T, F, A>>, | ||
| } | ||
|
|
||
| impl<T: Ord, F, A: Allocator> ExtractIf<'_, T, F, A> | ||
| where | ||
| F: FnMut(&mut T) -> bool, | ||
| { | ||
| pub(super) fn new<'a>(heap: &'a mut BinaryHeap<T, A>, predicate: F) -> ExtractIf<'a, T, F, A> { | ||
| // We need to keep a reference around to the heap so that we can | ||
| let heap_ptr: *mut BinaryHeap<T, A> = heap; | ||
| let extract_if = ManuallyDrop::new(heap.data.extract_if(.., predicate)); | ||
|
|
||
| ExtractIf { heap_ptr, extract_if } | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "binary_heap_extract_if", issue = "154721")] | ||
| impl<T: Ord, F, A: Allocator> Iterator for ExtractIf<'_, T, F, A> | ||
| where | ||
| F: FnMut(&mut T) -> bool, | ||
| { | ||
| type Item = T; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.extract_if.next() | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "binary_heap_extract_if", issue = "154721")] | ||
| impl<'a, T: Ord, F, A: Allocator> Drop for ExtractIf<'a, T, F, A> { | ||
| fn drop(&mut self) { | ||
| // SAFETY: We need to drop this before we rebuild the heap so that its descructor resets the vec info | ||
| // We also are only calling this hear during the drop of ExtractIf and then never using it again | ||
| unsafe { | ||
| ManuallyDrop::drop(&mut self.extract_if); | ||
| } | ||
|
|
||
| // SAFETY: We only generate this ptr from a reference so we know that it is never null | ||
| let heap = unsafe { self.heap_ptr.as_mut_unchecked() }; | ||
|
|
||
| // Removing some items from the heap almost certainly has invalidated its invariants, we need to fix this up here | ||
| heap.rebuild(); | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "binary_heap_extract_if", issue = "154721")] | ||
| impl<T: Ord, F, A: Allocator> FusedIterator for ExtractIf<'_, T, F, A> where F: FnMut(&mut T) -> bool | ||
| {} | ||
|
|
||
| #[unstable(feature = "binary_heap_extract_if", issue = "154721")] | ||
| impl<T: Ord, F, A> fmt::Debug for ExtractIf<'_, T, F, A> | ||
| where | ||
| T: fmt::Debug, | ||
| A: Allocator, | ||
| { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("ExtractIf").finish_non_exhaustive() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.