mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-10-02 14:54:30 +00:00
add BinaryHeap.push_unchecked
This commit is contained in:
parent
05633f9b56
commit
a58995eafb
@ -273,12 +273,21 @@ where
|
||||
/// assert_eq!(heap.peek(), Some(&5));
|
||||
/// ```
|
||||
pub fn push(&mut self, item: T) -> Result<(), T> {
|
||||
let old_len = self.len();
|
||||
self.data.push(item)?;
|
||||
self.sift_up(0, old_len);
|
||||
if self.data.is_full() {
|
||||
return Err(item);
|
||||
}
|
||||
|
||||
unsafe { self.push_unchecked(item) }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Pushes an item onto the binary heap without first checking if it's full.
|
||||
pub unsafe fn push_unchecked(&mut self, item: T) {
|
||||
let old_len = self.len();
|
||||
self.data.push_unchecked(item);
|
||||
self.sift_up(0, old_len);
|
||||
}
|
||||
|
||||
/* Private API */
|
||||
fn sift_down_to_bottom(&mut self, mut pos: usize) {
|
||||
let end = self.len();
|
||||
|
Loading…
x
Reference in New Issue
Block a user