-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement into_iter() for BinaryHeap. #19236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,9 +160,7 @@ use core::mem::{zeroed, replace, swap}; | |
use core::ptr; | ||
|
||
use slice; | ||
use vec::Vec; | ||
|
||
// FIXME(conventions): implement into_iter | ||
use vec::{mod, Vec}; | ||
|
||
/// A priority queue implemented with a binary heap. | ||
/// | ||
|
@@ -243,6 +241,27 @@ impl<T: Ord> BinaryHeap<T> { | |
Items { iter: self.data.iter() } | ||
} | ||
|
||
/// Creates a consuming iterator, that is, one that moves each value out of | ||
/// the binary heap in arbitrary order. The binary heap cannot be used | ||
/// after calling this. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use std::collections::BinaryHeap; | ||
/// let pq = BinaryHeap::from_vec(vec![1i, 2, 3, 4]); | ||
/// | ||
/// // Print 1, 2, 3, 4 in arbitrary order | ||
/// for x in pq.into_iter() { | ||
/// // x has type int, not &int | ||
/// println!("{}", x); | ||
/// } | ||
/// ``` | ||
#[unstable = "matches collection reform specification, waiting for dust to settle"] | ||
pub fn into_iter(self) -> MoveItems<T> { | ||
MoveItems { iter: self.data.into_iter() } | ||
} | ||
|
||
/// Returns the greatest item in a queue, or `None` if it is empty. | ||
/// | ||
/// # Example | ||
|
@@ -548,6 +567,26 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> { | |
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() } | ||
} | ||
|
||
/// An iterator that moves out of a `BinaryHeap`. | ||
pub struct MoveItems<T> { | ||
iter: vec::MoveItems<T>, | ||
} | ||
|
||
impl<T> Iterator<T> for MoveItems<T> { | ||
#[inline] | ||
fn next(&mut self) -> Option<T> { self.iter.next() } | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() } | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you can/should also be able to implement DoubleEndedIterator and ExactSize. |
||
impl<T> DoubleEndedIterator<T> for MoveItems<T> { | ||
#[inline] | ||
fn next_back(&mut self) -> Option<T> { self.iter.next_back() } | ||
} | ||
|
||
impl<T> ExactSize<T> for MoveItems<T> {} | ||
|
||
impl<T: Ord> FromIterator<T> for BinaryHeap<T> { | ||
fn from_iter<Iter: Iterator<T>>(mut iter: Iter) -> BinaryHeap<T> { | ||
let vec: Vec<T> = iter.collect(); | ||
|
@@ -586,6 +625,43 @@ mod tests { | |
} | ||
} | ||
|
||
#[test] | ||
fn test_move_iter() { | ||
let data = vec!(5i, 9, 3); | ||
let iterout = vec!(9i, 5, 3); | ||
let pq = BinaryHeap::from_vec(data); | ||
|
||
let v: Vec<int> = pq.into_iter().collect(); | ||
assert_eq!(v, iterout); | ||
} | ||
|
||
#[test] | ||
fn test_move_iter_size_hint() { | ||
let data = vec!(5i, 9); | ||
let pq = BinaryHeap::from_vec(data); | ||
|
||
let mut it = pq.into_iter(); | ||
|
||
assert_eq!(it.size_hint(), (2, Some(2))); | ||
assert_eq!(it.next(), Some(9i)); | ||
|
||
assert_eq!(it.size_hint(), (1, Some(1))); | ||
assert_eq!(it.next(), Some(5i)); | ||
|
||
assert_eq!(it.size_hint(), (0, Some(0))); | ||
assert_eq!(it.next(), None); | ||
} | ||
|
||
#[test] | ||
fn test_move_iter_reverse() { | ||
let data = vec!(5i, 9, 3); | ||
let iterout = vec!(3i, 5, 9); | ||
let pq = BinaryHeap::from_vec(data); | ||
|
||
let v: Vec<int> = pq.into_iter().rev().collect(); | ||
assert_eq!(v, iterout); | ||
} | ||
|
||
#[test] | ||
fn test_top_and_pop() { | ||
let data = vec!(2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I wonder if we need to explicitly say what the last sentence is saying. The sentence is true for every method that takes self by value and I wonder if there is a more systematic way of conveying this across the entire doc suite (perhaps having it somehow annotated automatically by rustdoc).
Not to block this PR on it, of course, just wondering...
cc @steveklabnik