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)); /// assert_eq!(heap.peek(), Some(&5));
/// ``` /// ```
pub fn push(&mut self, item: T) -> Result<(), T> { pub fn push(&mut self, item: T) -> Result<(), T> {
let old_len = self.len(); if self.data.is_full() {
self.data.push(item)?; return Err(item);
self.sift_up(0, old_len); }
unsafe { self.push_unchecked(item) }
Ok(()) 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 */ /* Private API */
fn sift_down_to_bottom(&mut self, mut pos: usize) { fn sift_down_to_bottom(&mut self, mut pos: usize) {
let end = self.len(); let end = self.len();