Skip to content

Commit 01aa4ca

Browse files
committed
Clean up collections::binary_heap
1 parent 22a9f25 commit 01aa4ca

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/libcollections/binary_heap.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl<T: Ord> BinaryHeap<T> {
241241
/// }
242242
/// ```
243243
#[unstable = "matches collection reform specification, waiting for dust to settle"]
244-
pub fn iter<'a>(&'a self) -> Items<'a, T> {
244+
pub fn iter(&self) -> Items<T> {
245245
Items { iter: self.data.iter() }
246246
}
247247

@@ -282,8 +282,8 @@ impl<T: Ord> BinaryHeap<T> {
282282
/// assert_eq!(heap.top(), Some(&5i));
283283
///
284284
/// ```
285-
pub fn top<'a>(&'a self) -> Option<&'a T> {
286-
if self.is_empty() { None } else { Some(&self.data[0]) }
285+
pub fn top(&self) -> Option<&T> {
286+
self.data.get(0)
287287
}
288288

289289
/// Returns the number of elements the queue can hold without reallocating.
@@ -394,9 +394,9 @@ impl<T: Ord> BinaryHeap<T> {
394394
/// ```
395395
#[unstable = "matches collection reform specification, waiting for dust to settle"]
396396
pub fn push(&mut self, item: T) {
397+
let old_len = self.len();
397398
self.data.push(item);
398-
let new_len = self.len() - 1;
399-
self.siftup(0, new_len);
399+
self.siftup(0, old_len);
400400
}
401401

402402
/// Pushes an item onto a queue then pops the greatest item off the queue in
@@ -417,10 +417,16 @@ impl<T: Ord> BinaryHeap<T> {
417417
/// assert_eq!(heap.top(), Some(&3i));
418418
/// ```
419419
pub fn push_pop(&mut self, mut item: T) -> T {
420-
if !self.is_empty() && *self.top().unwrap() > item {
421-
swap(&mut item, &mut self.data[0]);
422-
self.siftdown(0);
420+
match self.data.get_mut(0) {
421+
None => return item,
422+
Some(top) => if *top > item {
423+
swap(&mut item, top);
424+
} else {
425+
return item;
426+
},
423427
}
428+
429+
self.siftdown(0);
424430
item
425431
}
426432

@@ -467,7 +473,7 @@ impl<T: Ord> BinaryHeap<T> {
467473
/// println!("{}", x);
468474
/// }
469475
/// ```
470-
pub fn into_vec(self) -> Vec<T> { let BinaryHeap{data: v} = self; v }
476+
pub fn into_vec(self) -> Vec<T> { self.data }
471477

472478
/// Consumes the `BinaryHeap` and returns a vector in sorted
473479
/// (ascending) order.
@@ -484,15 +490,14 @@ impl<T: Ord> BinaryHeap<T> {
484490
/// let vec = heap.into_sorted_vec();
485491
/// assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]);
486492
/// ```
487-
pub fn into_sorted_vec(self) -> Vec<T> {
488-
let mut q = self;
489-
let mut end = q.len();
493+
pub fn into_sorted_vec(mut self) -> Vec<T> {
494+
let mut end = self.len();
490495
while end > 1 {
491496
end -= 1;
492-
q.data.swap(0, end);
493-
q.siftdown_range(0, end)
497+
self.data.swap(0, end);
498+
self.siftdown_range(0, end)
494499
}
495-
q.into_vec()
500+
self.into_vec()
496501
}
497502

498503
// The implementations of siftup and siftdown use unsafe blocks in
@@ -559,21 +564,21 @@ impl<T: Ord> BinaryHeap<T> {
559564
}
560565

561566
/// `BinaryHeap` iterator.
562-
pub struct Items <'a, T:'a> {
567+
pub struct Items<'a, T: 'a> {
563568
iter: slice::Items<'a, T>,
564569
}
565570

566571
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
567572
#[inline]
568-
fn next(&mut self) -> Option<(&'a T)> { self.iter.next() }
573+
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
569574

570575
#[inline]
571576
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
572577
}
573578

574579
impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
575580
#[inline]
576-
fn next_back(&mut self) -> Option<(&'a T)> { self.iter.next_back() }
581+
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
577582
}
578583

579584
impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {}
@@ -600,8 +605,7 @@ impl<T> ExactSizeIterator<T> for MoveItems<T> {}
600605

601606
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
602607
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BinaryHeap<T> {
603-
let vec: Vec<T> = iter.collect();
604-
BinaryHeap::from_vec(vec)
608+
BinaryHeap::from_vec(iter.collect())
605609
}
606610
}
607611

@@ -796,20 +800,20 @@ mod tests {
796800

797801
#[test]
798802
fn test_empty_pop() {
799-
let mut heap: BinaryHeap<int> = BinaryHeap::new();
803+
let mut heap = BinaryHeap::<int>::new();
800804
assert!(heap.pop().is_none());
801805
}
802806

803807
#[test]
804808
fn test_empty_top() {
805-
let empty: BinaryHeap<int> = BinaryHeap::new();
809+
let empty = BinaryHeap::<int>::new();
806810
assert!(empty.top().is_none());
807811
}
808812

809813
#[test]
810814
fn test_empty_replace() {
811-
let mut heap: BinaryHeap<int> = BinaryHeap::new();
812-
heap.replace(5).is_none();
815+
let mut heap = BinaryHeap::<int>::new();
816+
assert!(heap.replace(5).is_none());
813817
}
814818

815819
#[test]

0 commit comments

Comments
 (0)