1
Fork 0

Stripped trailing spaces; Implemented FromIterator for TreeMap and PriorityQueue

This commit is contained in:
=Mark Sinclair 2013-07-14 13:18:50 -04:00
parent bb6615d43a
commit bbe03da9c6
4 changed files with 80 additions and 6 deletions

View file

@ -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);
}
}
}