]> git.lizzy.rs Git - rust.git/commitdiff
Make BinaryHeap's Items iterator implement DoubleEnded and ExactSize
authorChase Southwood <chase.southwood@gmail.com>
Wed, 26 Nov 2014 02:57:32 +0000 (20:57 -0600)
committerChase Southwood <chase.southwood@gmail.com>
Wed, 26 Nov 2014 03:41:23 +0000 (21:41 -0600)
src/libcollections/binary_heap.rs

index 8efc4cd50c1a67a20c24997edb9d124a747c9140..4abe555b0eaea066e01c9704a5f4006a02edace8 100644 (file)
@@ -567,6 +567,13 @@ fn next(&mut self) -> Option<(&'a T)> { self.iter.next() }
     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>,
@@ -625,6 +632,16 @@ fn test_iterator() {
         }
     }
 
+    #[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);