33: add BinaryHeap.push_unchecked r=japaric a=japaric



Co-authored-by: Jorge Aparicio <jorge@japaric.io>
This commit is contained in:
bors[bot] 2018-04-28 03:49:40 +00:00
commit 7836224ed9
2 changed files with 14 additions and 5 deletions

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();
@ -335,7 +344,7 @@ impl<'a, T> Hole<'a, T> {
#[inline] #[inline]
unsafe fn new(data: &'a mut [T], pos: usize) -> Self { unsafe fn new(data: &'a mut [T], pos: usize) -> Self {
debug_assert!(pos < data.len()); debug_assert!(pos < data.len());
let elt = ptr::read(&data[pos]); let elt = ptr::read(data.get_unchecked(pos));
Hole { Hole {
data, data,
elt: Some(elt), elt: Some(elt),

View File

@ -64,8 +64,8 @@ extern crate hash32;
extern crate std; extern crate std;
pub use binary_heap::BinaryHeap; pub use binary_heap::BinaryHeap;
pub use generic_array::ArrayLength;
pub use generic_array::typenum::consts; pub use generic_array::typenum::consts;
pub use generic_array::ArrayLength;
pub use indexmap::{FnvIndexMap, IndexMap}; pub use indexmap::{FnvIndexMap, IndexMap};
pub use indexset::{FnvIndexSet, IndexSet}; pub use indexset::{FnvIndexSet, IndexSet};
pub use linear_map::LinearMap; pub use linear_map::LinearMap;