-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Mutable access to the highest-priority element in a BinaryHeap #1626
Comments
Some discussion on #rust-libs: https://botbot.me/mozilla/rust-libs/2016-05-21/?msg=66470212&page=1 This is obviously implementable via pop-modify-push, but the idea here is to avoid the extra work involved in fixing up the heap after popping the element. A closure-based API for this kind of thing is somewhat nontraditional. I would personally lean towards something like: pub fn peek_mut<'a>(&'a mut self) -> Option<PeekMut<'a, T>> { ... }
pub struct PeekMut<'a, T> { ... }
impl<'a, T> Drop for PeekMut<'a, T> { ... }
impl<'a, T> Deref for PeekMut<'a, T> { ... }
impl<'a, T> DerefMut for PeekMut<'a, T> { ... } where the cc @rust-lang/libs |
The follow PR shows how much this algorithm can improve something implemented on top of it: rust-itertools/itertools#101 (and where we chose to hand roll the heap instead of using BinaryHeap, exactly for missing this API). The Deref/Drop proposal works out well for this use case, if the PeekMut has an additional method that allows the modified element to be dequeued if needed: impl<'a, T> PeekTop<'a, T> where T: Ord {
fn remove(self) -> T {
self.heap.swap_remove(0)
}
} Removing the top element also uses the sift down in drop afterwards, to put the swapped element in the right place. |
…binaryheap, r=alexcrichton Mutable access to the top element of a BinaryHeap An implementation of the library change discussed here: rust-lang/rfcs#1626
This has been implemented and stabilized. |
Yay! |
The current implementation of
BinaryHeap
makes it difficult to write efficient I/O schedulers. I propose the addition of a function toBinaryHeap
(inexact form):that would allow the caller to mutate the highest-priority element in the heap, then restore the heap property after the closure completes.
This functionality makes it much easier to build efficient priority-queue-based schedulers.
The text was updated successfully, but these errors were encountered: