Skip to content

Clean up collections::binary_heap #19967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Man... what is this code. Was this idiomatic at one point?

}

// 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