Skip to content

Commit 8f94ea0

Browse files
committed
rollup merge of rust-lang#19330: csouth3/binaryheap-iter
There's no reason that BinaryHeap's iterator can't implement DoubleEnded and ExactSize, so add these implementations.
2 parents 3a1d538 + d48886c commit 8f94ea0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libcollections/binary_heap.rs

+17
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
567567
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
568568
}
569569

570+
impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
571+
#[inline]
572+
fn next_back(&mut self) -> Option<(&'a T)> { self.iter.next_back() }
573+
}
574+
575+
impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}
576+
570577
/// An iterator that moves out of a `BinaryHeap`.
571578
pub struct MoveItems<T> {
572579
iter: vec::MoveItems<T>,
@@ -625,6 +632,16 @@ mod tests {
625632
}
626633
}
627634

635+
#[test]
636+
fn test_iterator_reverse() {
637+
let data = vec!(5i, 9, 3);
638+
let iterout = vec!(3i, 5, 9);
639+
let pq = BinaryHeap::from_vec(data);
640+
641+
let v: Vec<int> = pq.iter().rev().map(|&x| x).collect();
642+
assert_eq!(v, iterout);
643+
}
644+
628645
#[test]
629646
fn test_move_iter() {
630647
let data = vec!(5i, 9, 3);

0 commit comments

Comments
 (0)