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));
/// ```
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),

View File

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