add BinaryHeap.push_unchecked

This commit is contained in:
Jorge Aparicio 2018-04-28 05:48:58 +02:00
parent 05633f9b56
commit a58995eafb

View File

@ -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();