Skip to content

Commit

Permalink
Clean up collections::binary_heap
Browse files Browse the repository at this point in the history
  • Loading branch information
apasel422 committed Dec 18, 2014
1 parent 22a9f25 commit 01aa4ca
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/libcollections/binary_heap.rs
Expand Up @@ -241,7 +241,7 @@ impl<T: Ord> BinaryHeap<T> {
/// }
/// ```
#[unstable = "matches collection reform specification, waiting for dust to settle"]
pub fn iter<'a>(&'a self) -> Items<'a, T> {
pub fn iter(&self) -> Items<T> {
Items { iter: self.data.iter() }
}

Expand Down Expand Up @@ -282,8 +282,8 @@ impl<T: Ord> BinaryHeap<T> {
/// 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.
Expand Down Expand Up @@ -394,9 +394,9 @@ impl<T: Ord> BinaryHeap<T> {
/// ```
#[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
Expand All @@ -417,10 +417,16 @@ impl<T: Ord> BinaryHeap<T> {
/// 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
}

Expand Down Expand Up @@ -467,7 +473,7 @@ impl<T: Ord> BinaryHeap<T> {
/// println!("{}", x);
/// }
/// ```
pub fn into_vec(self) -> Vec<T> { let BinaryHeap{data: v} = self; v }
pub fn into_vec(self) -> Vec<T> { self.data }

/// Consumes the `BinaryHeap` and returns a vector in sorted
/// (ascending) order.
Expand All @@ -484,15 +490,14 @@ impl<T: Ord> BinaryHeap<T> {
/// let vec = heap.into_sorted_vec();
/// assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]);
/// ```
pub fn into_sorted_vec(self) -> Vec<T> {
let mut q = self;
let mut end = q.len();
pub fn into_sorted_vec(mut self) -> Vec<T> {
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
Expand Down Expand Up @@ -559,21 +564,21 @@ impl<T: Ord> BinaryHeap<T> {
}

/// `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<uint>) { self.iter.size_hint() }
}

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> {}
Expand All @@ -600,8 +605,7 @@ impl<T> ExactSizeIterator<T> for MoveItems<T> {}

impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BinaryHeap<T> {
let vec: Vec<T> = iter.collect();
BinaryHeap::from_vec(vec)
BinaryHeap::from_vec(iter.collect())
}
}

Expand Down Expand Up @@ -796,20 +800,20 @@ mod tests {

#[test]
fn test_empty_pop() {
let mut heap: BinaryHeap<int> = BinaryHeap::new();
let mut heap = BinaryHeap::<int>::new();
assert!(heap.pop().is_none());
}

#[test]
fn test_empty_top() {
let empty: BinaryHeap<int> = BinaryHeap::new();
let empty = BinaryHeap::<int>::new();
assert!(empty.top().is_none());
}

#[test]
fn test_empty_replace() {
let mut heap: BinaryHeap<int> = BinaryHeap::new();
heap.replace(5).is_none();
let mut heap = BinaryHeap::<int>::new();
assert!(heap.replace(5).is_none());
}

#[test]
Expand Down

0 comments on commit 01aa4ca

Please sign in to comment.