mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-30 22:10:37 +00:00
add BinaryHeap.pop_unchecked
This commit is contained in:
parent
8121700a76
commit
af5d6d1480
@ -250,13 +250,23 @@ where
|
|||||||
/// assert_eq!(heap.pop(), None);
|
/// assert_eq!(heap.pop(), None);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn pop(&mut self) -> Option<T> {
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
self.data.pop().map(|mut item| {
|
if self.is_empty() {
|
||||||
if !self.is_empty() {
|
None
|
||||||
mem::swap(&mut item, &mut self.data[0]);
|
} else {
|
||||||
self.sift_down_to_bottom(0);
|
Some(unsafe { self.pop_unchecked() })
|
||||||
}
|
}
|
||||||
item
|
}
|
||||||
})
|
|
||||||
|
/// Removes the *top* (greatest if max-heap, smallest if min-heap) item from the binary heap and
|
||||||
|
/// returns it, without checking if the binary heap is empty.
|
||||||
|
pub unsafe fn pop_unchecked(&mut self) -> T {
|
||||||
|
let mut item = self.data.pop_unchecked();
|
||||||
|
|
||||||
|
if !self.is_empty() {
|
||||||
|
mem::swap(&mut item, &mut self.data[0]);
|
||||||
|
self.sift_down_to_bottom(0);
|
||||||
|
}
|
||||||
|
item
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pushes an item onto the binary heap.
|
/// Pushes an item onto the binary heap.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user