diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index 9421159269896..7af40dea0586c 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -241,7 +241,7 @@ impl BinaryHeap { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter<'a>(&'a self) -> Items<'a, T> { + pub fn iter(&self) -> Items { Items { iter: self.data.iter() } } @@ -282,8 +282,8 @@ impl BinaryHeap { /// assert_eq!(heap.top(), Some(&5i)); /// /// ``` - pub fn top<'a>(&'a self) -> Option<&'a T> { - if self.is_empty() { None } else { Some(&self.data[0]) } + pub fn top(&self) -> Option<&T> { + self.data.get(0) } /// Returns the number of elements the queue can hold without reallocating. @@ -394,9 +394,9 @@ impl BinaryHeap { /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn push(&mut self, item: T) { + let old_len = self.len(); self.data.push(item); - let new_len = self.len() - 1; - self.siftup(0, new_len); + self.siftup(0, old_len); } /// Pushes an item onto a queue then pops the greatest item off the queue in @@ -417,10 +417,16 @@ impl BinaryHeap { /// assert_eq!(heap.top(), Some(&3i)); /// ``` pub fn push_pop(&mut self, mut item: T) -> T { - if !self.is_empty() && *self.top().unwrap() > item { - swap(&mut item, &mut self.data[0]); - self.siftdown(0); + match self.data.get_mut(0) { + None => return item, + Some(top) => if *top > item { + swap(&mut item, top); + } else { + return item; + }, } + + self.siftdown(0); item } @@ -467,7 +473,7 @@ impl BinaryHeap { /// println!("{}", x); /// } /// ``` - pub fn into_vec(self) -> Vec { let BinaryHeap{data: v} = self; v } + pub fn into_vec(self) -> Vec { self.data } /// Consumes the `BinaryHeap` and returns a vector in sorted /// (ascending) order. @@ -484,15 +490,14 @@ impl BinaryHeap { /// let vec = heap.into_sorted_vec(); /// assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]); /// ``` - pub fn into_sorted_vec(self) -> Vec { - let mut q = self; - let mut end = q.len(); + pub fn into_sorted_vec(mut self) -> Vec { + let mut end = self.len(); while end > 1 { end -= 1; - q.data.swap(0, end); - q.siftdown_range(0, end) + self.data.swap(0, end); + self.siftdown_range(0, end) } - q.into_vec() + self.into_vec() } // The implementations of siftup and siftdown use unsafe blocks in @@ -559,13 +564,13 @@ impl BinaryHeap { } /// `BinaryHeap` iterator. -pub struct Items <'a, T:'a> { +pub struct Items<'a, T: 'a> { iter: slice::Items<'a, T>, } impl<'a, T> Iterator<&'a T> for Items<'a, T> { #[inline] - fn next(&mut self) -> Option<(&'a T)> { self.iter.next() } + fn next(&mut self) -> Option<&'a T> { self.iter.next() } #[inline] fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } @@ -573,7 +578,7 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> { impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { #[inline] - fn next_back(&mut self) -> Option<(&'a T)> { self.iter.next_back() } + fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() } } impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {} @@ -600,8 +605,7 @@ impl ExactSizeIterator for MoveItems {} impl FromIterator for BinaryHeap { fn from_iter>(iter: Iter) -> BinaryHeap { - let vec: Vec = iter.collect(); - BinaryHeap::from_vec(vec) + BinaryHeap::from_vec(iter.collect()) } } @@ -796,20 +800,20 @@ mod tests { #[test] fn test_empty_pop() { - let mut heap: BinaryHeap = BinaryHeap::new(); + let mut heap = BinaryHeap::::new(); assert!(heap.pop().is_none()); } #[test] fn test_empty_top() { - let empty: BinaryHeap = BinaryHeap::new(); + let empty = BinaryHeap::::new(); assert!(empty.top().is_none()); } #[test] fn test_empty_replace() { - let mut heap: BinaryHeap = BinaryHeap::new(); - heap.replace(5).is_none(); + let mut heap = BinaryHeap::::new(); + assert!(heap.replace(5).is_none()); } #[test]