diff options
| author | bors <bors@rust-lang.org> | 2013-06-26 23:07:41 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-06-26 23:07:41 -0700 |
| commit | f1e09d6f1faa6e4d7d4d19123d1633fce370e145 (patch) | |
| tree | 660935f34a057fbf3178197e5cec40e8de89d309 /src/libextra/priority_queue.rs | |
| parent | eda5e40b79f7aaa51765f59c21a76fe033c937b1 (diff) | |
| parent | ab428b648001030a0ea71c61dd89a531109c0cdd (diff) | |
| download | rust-f1e09d6f1faa6e4d7d4d19123d1633fce370e145.tar.gz rust-f1e09d6f1faa6e4d7d4d19123d1633fce370e145.zip | |
auto merge of #7420 : mozilla/rust/rollup, r=thestinger
Diffstat (limited to 'src/libextra/priority_queue.rs')
| -rw-r--r-- | src/libextra/priority_queue.rs | 31 |
1 files changed, 27 insertions, 4 deletions
diff --git a/src/libextra/priority_queue.rs b/src/libextra/priority_queue.rs index 4e201a6538b..af891edf9e5 100644 --- a/src/libextra/priority_queue.rs +++ b/src/libextra/priority_queue.rs @@ -37,10 +37,11 @@ impl<T:Ord> Mutable for PriorityQueue<T> { } impl<T:Ord> PriorityQueue<T> { - /// Visit all values in the underlying vector. - /// - /// The values are **not** visited in order. - pub fn each(&self, f: &fn(&T) -> bool) -> bool { self.data.iter().advance(f) } + /// An iterator visiting all values in underlying vector, in + /// arbitrary order. + pub fn iter<'a>(&'a self) -> PriorityQueueIterator<'a, T> { + PriorityQueueIterator { iter: self.data.iter() } + } /// Returns the greatest item in the queue - fails if empty pub fn top<'a>(&'a self) -> &'a T { &self.data[0] } @@ -178,12 +179,34 @@ impl<T:Ord> PriorityQueue<T> { } } +/// PriorityQueue iterator +pub struct PriorityQueueIterator <'self, T> { + priv iter: vec::VecIterator<'self, T>, +} + +impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> { + #[inline] + fn next(&mut self) -> Option<(&'self T)> { self.iter.next() } +} + #[cfg(test)] mod tests { use sort::merge_sort; use priority_queue::PriorityQueue; #[test] + fn test_iterator() { + let data = ~[5, 9, 3]; + let iterout = ~[9, 5, 3]; + let pq = PriorityQueue::from_vec(data); + let mut i = 0; + for pq.iter().advance |el| { + assert_eq!(*el, iterout[i]); + i += 1; + } + } + + #[test] fn test_top_and_pop() { let data = ~[2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]; let mut sorted = merge_sort(data, |x, y| x.le(y)); |
