diff options
| author | bors <bors@rust-lang.org> | 2013-07-14 12:01:22 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-07-14 12:01:22 -0700 |
| commit | 0cb1ac0f9f7bf98ea8ab5ccbd6ef319decc41a72 (patch) | |
| tree | 273268a2048fe1c4c1e41750f8fac87d1d7810f1 /src/libextra/priority_queue.rs | |
| parent | 1c35ab322ff2f26962a3550fffc2fa4154224b64 (diff) | |
| parent | bbe03da9c6bad23d8e09077461c1616872e1aca0 (diff) | |
| download | rust-0cb1ac0f9f7bf98ea8ab5ccbd6ef319decc41a72.tar.gz rust-0cb1ac0f9f7bf98ea8ab5ccbd6ef319decc41a72.zip | |
auto merge of #7788 : MarkJr94/rust/from_iter, r=cmr
Added Iterators for HashMap/Set, TreeMap/Set, TrieMap/Set, and PriorityQueue as per Issue #7626
Diffstat (limited to 'src/libextra/priority_queue.rs')
| -rw-r--r-- | src/libextra/priority_queue.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs index 1f7ba9f6530..58bf4ba9247 100644 --- a/src/libextra/priority_queue.rs +++ b/src/libextra/priority_queue.rs @@ -16,6 +16,7 @@ use std::unstable::intrinsics::{move_val_init, init}; use std::util::{replace, swap}; use std::vec; +use std::iterator::FromIterator; /// A priority queue implemented with a binary heap pub struct PriorityQueue<T> { @@ -191,6 +192,21 @@ impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> { fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() } } +impl<T: Ord, Iter: Iterator<T>> FromIterator<T, Iter> for PriorityQueue<T> { + pub fn from_iterator(iter: &mut Iter) -> PriorityQueue<T> { + let (lower, _) = iter.size_hint(); + + let mut q = PriorityQueue::new(); + q.reserve_at_least(lower); + + for iter.advance |elem| { + q.push(elem); + } + + q + } +} + #[cfg(test)] mod tests { use sort::merge_sort; @@ -341,4 +357,15 @@ mod tests { #[should_fail] #[ignore(cfg(windows))] fn test_empty_replace() { let mut heap = PriorityQueue::new(); heap.replace(5); } + + #[test] + fn test_from_iter() { + let xs = ~[9u, 8, 7, 6, 5, 4, 3, 2, 1]; + + let mut q: PriorityQueue<uint> = xs.rev_iter().transform(|&x| x).collect(); + + for xs.iter().advance |&x| { + assert_eq!(q.pop(), x); + } + } } |
