Skip to content

Commit

Permalink
Make BinaryHeap's Items iterator implement DoubleEnded and ExactSize
Browse files Browse the repository at this point in the history
  • Loading branch information
csouth3 committed Nov 26, 2014
1 parent eedfc07 commit d48886c
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 d48886c

Please sign in to comment.