Skip to content

Commit

Permalink
rollup merge of rust-lang#19330: csouth3/binaryheap-iter
Browse files Browse the repository at this point in the history
There's no reason that BinaryHeap's iterator can't implement DoubleEnded and ExactSize, so add these implementations.
  • Loading branch information
alexcrichton committed Nov 27, 2014
2 parents 3a1d538 + d48886c commit 8f94ea0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,13 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
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() }
}

impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}

/// An iterator that moves out of a `BinaryHeap`.
pub struct MoveItems<T> {
iter: vec::MoveItems<T>,
Expand Down Expand Up @@ -625,6 +632,16 @@ mod tests {
}
}

#[test]
fn test_iterator_reverse() {
let data = vec!(5i, 9, 3);
let iterout = vec!(3i, 5, 9);
let pq = BinaryHeap::from_vec(data);

let v: Vec<int> = pq.iter().rev().map(|&x| x).collect();
assert_eq!(v, iterout);
}

#[test]
fn test_move_iter() {
let data = vec!(5i, 9, 3);
Expand Down

0 comments on commit 8f94ea0

Please sign in to comment.