Skip to content

Commit 2209baf

Browse files
artagnonemberian
authored andcommitted
---
yaml --- r: 63717 b: refs/heads/snap-stage3 c: f4621ca h: refs/heads/master i: 63715: 141ce96 v: v3
1 parent 029dd81 commit 2209baf

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 2b17e4775c816b59d92e166b8a8db039aaa7ba85
4+
refs/heads/snap-stage3: f4621cab6859ee0f8ddacea0ece98e4a4c00d5a6
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libextra/priority_queue.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ impl<T:Ord> Mutable for PriorityQueue<T> {
3737
}
3838

3939
impl<T:Ord> PriorityQueue<T> {
40-
/// Visit all values in the underlying vector.
41-
///
42-
/// The values are **not** visited in order.
43-
pub fn each(&self, f: &fn(&T) -> bool) -> bool { self.data.iter().advance(f) }
40+
/// An iterator visiting all values in underlying vector, in
41+
/// arbitrary order.
42+
pub fn iter<'a>(&'a self) -> PriorityQueueIterator<'a, T> {
43+
PriorityQueueIterator { iter: self.data.iter() }
44+
}
4445

4546
/// Returns the greatest item in the queue - fails if empty
4647
pub fn top<'a>(&'a self) -> &'a T { &self.data[0] }
@@ -178,11 +179,33 @@ impl<T:Ord> PriorityQueue<T> {
178179
}
179180
}
180181

182+
/// PriorityQueue iterator
183+
pub struct PriorityQueueIterator <'self, T> {
184+
priv iter: vec::VecIterator<'self, T>,
185+
}
186+
187+
impl<'self, T> Iterator<&'self T> for PriorityQueueIterator<'self, T> {
188+
#[inline]
189+
fn next(&mut self) -> Option<(&'self T)> { self.iter.next() }
190+
}
191+
181192
#[cfg(test)]
182193
mod tests {
183194
use sort::merge_sort;
184195
use priority_queue::PriorityQueue;
185196

197+
#[test]
198+
fn test_iterator() {
199+
let data = ~[5, 9, 3];
200+
let iterout = ~[9, 5, 3];
201+
let pq = PriorityQueue::from_vec(data);
202+
let mut i = 0;
203+
for pq.iter().advance |el| {
204+
assert_eq!(*el, iterout[i]);
205+
i += 1;
206+
}
207+
}
208+
186209
#[test]
187210
fn test_top_and_pop() {
188211
let data = ~[2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];

0 commit comments

Comments
 (0)