Skip to content

Commit f5b92b4

Browse files
committed
auto merge of #19236 : csouth3/rust/master, r=Gankro
Whilst browsing the source for BinaryHeap, I saw a FIXME for implementing into_iter. I think, since the BinaryHeap is represented internally using just a Vec, just calling into_iter() on the BinaryHeap's data should be sufficient to do what we want here. If this actually isn't the right approach (e.g., I should write a struct MoveItems and appropriate implementation for BinaryHeap instead), let me know and I'll happily rework this. Both of the tests that I have added pass. This is my first contribution to Rust, so please let me know any ways I can improve this PR!
2 parents c637cab + 3f8e269 commit f5b92b4

File tree

1 file changed

+79
-3
lines changed

1 file changed

+79
-3
lines changed

src/libcollections/binary_heap.rs

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ use core::mem::{zeroed, replace, swap};
160160
use core::ptr;
161161

162162
use slice;
163-
use vec::Vec;
164-
165-
// FIXME(conventions): implement into_iter
163+
use vec::{mod, Vec};
166164

167165
/// A priority queue implemented with a binary heap.
168166
///
@@ -243,6 +241,27 @@ impl<T: Ord> BinaryHeap<T> {
243241
Items { iter: self.data.iter() }
244242
}
245243

244+
/// Creates a consuming iterator, that is, one that moves each value out of
245+
/// the binary heap in arbitrary order. The binary heap cannot be used
246+
/// after calling this.
247+
///
248+
/// # Example
249+
///
250+
/// ```
251+
/// use std::collections::BinaryHeap;
252+
/// let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4]);
253+
///
254+
/// // Print 1, 2, 3, 4 in arbitrary order
255+
/// for x in pq.into_iter() {
256+
/// // x has type int, not &int
257+
/// println!("{}", x);
258+
/// }
259+
/// ```
260+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
261+
pub fn into_iter(self) -> MoveItems<T> {
262+
MoveItems { iter: self.data.into_iter() }
263+
}
264+
246265
/// Returns the greatest item in a queue, or `None` if it is empty.
247266
///
248267
/// # Example
@@ -548,6 +567,26 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
548567
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
549568
}
550569

570+
/// An iterator that moves out of a `BinaryHeap`.
571+
pub struct MoveItems<T> {
572+
iter: vec::MoveItems<T>,
573+
}
574+
575+
impl<T> Iterator<T> for MoveItems<T> {
576+
#[inline]
577+
fn next(&mut self) -> Option<T> { self.iter.next() }
578+
579+
#[inline]
580+
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
581+
}
582+
583+
impl<T> DoubleEndedIterator<T> for MoveItems<T> {
584+
#[inline]
585+
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
586+
}
587+
588+
impl<T> ExactSize<T> for MoveItems<T> {}
589+
551590
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
552591
fn from_iter<Iter: Iterator<T>>(mut iter: Iter) -> BinaryHeap<T> {
553592
let vec: Vec<T> = iter.collect();
@@ -586,6 +625,43 @@ mod tests {
586625
}
587626
}
588627

628+
#[test]
629+
fn test_move_iter() {
630+
let data = vec!(5i, 9, 3);
631+
let iterout = vec!(9i, 5, 3);
632+
let pq = BinaryHeap::from_vec(data);
633+
634+
let v: Vec<int> = pq.into_iter().collect();
635+
assert_eq!(v, iterout);
636+
}
637+
638+
#[test]
639+
fn test_move_iter_size_hint() {
640+
let data = vec!(5i, 9);
641+
let pq = BinaryHeap::from_vec(data);
642+
643+
let mut it = pq.into_iter();
644+
645+
assert_eq!(it.size_hint(), (2, Some(2)));
646+
assert_eq!(it.next(), Some(9i));
647+
648+
assert_eq!(it.size_hint(), (1, Some(1)));
649+
assert_eq!(it.next(), Some(5i));
650+
651+
assert_eq!(it.size_hint(), (0, Some(0)));
652+
assert_eq!(it.next(), None);
653+
}
654+
655+
#[test]
656+
fn test_move_iter_reverse() {
657+
let data = vec!(5i, 9, 3);
658+
let iterout = vec!(3i, 5, 9);
659+
let pq = BinaryHeap::from_vec(data);
660+
661+
let v: Vec<int> = pq.into_iter().rev().collect();
662+
assert_eq!(v, iterout);
663+
}
664+
589665
#[test]
590666
fn test_top_and_pop() {
591667
let data = vec!(2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1);

0 commit comments

Comments
 (0)