mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-28 13:00:26 +00:00
Merge #33
33: add BinaryHeap.push_unchecked r=japaric a=japaric Co-authored-by: Jorge Aparicio <jorge@japaric.io>
This commit is contained in:
commit
7836224ed9
@ -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();
|
||||
@ -335,7 +344,7 @@ impl<'a, T> Hole<'a, T> {
|
||||
#[inline]
|
||||
unsafe fn new(data: &'a mut [T], pos: usize) -> Self {
|
||||
debug_assert!(pos < data.len());
|
||||
let elt = ptr::read(&data[pos]);
|
||||
let elt = ptr::read(data.get_unchecked(pos));
|
||||
Hole {
|
||||
data,
|
||||
elt: Some(elt),
|
||||
|
@ -64,8 +64,8 @@ extern crate hash32;
|
||||
extern crate std;
|
||||
|
||||
pub use binary_heap::BinaryHeap;
|
||||
pub use generic_array::ArrayLength;
|
||||
pub use generic_array::typenum::consts;
|
||||
pub use generic_array::ArrayLength;
|
||||
pub use indexmap::{FnvIndexMap, IndexMap};
|
||||
pub use indexset::{FnvIndexSet, IndexSet};
|
||||
pub use linear_map::LinearMap;
|
||||
|
Loading…
x
Reference in New Issue
Block a user