Skip to content

Commit 3dd0e50

Browse files
committed
---
yaml --- r: 64643 b: refs/heads/snap-stage3 c: 9e4ebdb h: refs/heads/master i: 64641: 63ec583 64639: f6daf75 v: v3
1 parent 82f5462 commit 3dd0e50

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
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: 31c180e5f5da0e27c60a3bf02da182f19c66cf8f
4+
refs/heads/snap-stage3: 9e4ebdb9d612bc8d493f448386dbd99afb856818
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
204204
pub fn iter<'a>(&'a self) -> TreeMapIterator<'a, K, V> {
205205
TreeMapIterator{stack: ~[], node: &self.root, remaining: self.length}
206206
}
207+
208+
/// Get a lazy iterator that consumes the treemap.
209+
pub fn consume_iter(self) -> TreeMapConsumeIterator<K, V> {
210+
let TreeMap { root: root, length: length } = self;
211+
let stk = match root {
212+
None => ~[],
213+
Some(~tn) => ~[tn]
214+
};
215+
TreeMapConsumeIterator {
216+
stack: stk,
217+
remaining: length
218+
}
219+
}
207220
}
208221

209222
/// Lazy forward iterator over a map
@@ -241,6 +254,56 @@ impl<'self, K, V> Iterator<(&'self K, &'self V)> for TreeMapIterator<'self, K, V
241254
}
242255
}
243256

257+
/// Lazy forward iterator over a map that consumes the map while iterating
258+
pub struct TreeMapConsumeIterator<K, V> {
259+
priv stack: ~[TreeNode<K, V>],
260+
priv remaining: uint
261+
}
262+
263+
impl<K, V> Iterator<(K, V)> for TreeMapConsumeIterator<K,V> {
264+
#[inline]
265+
fn next(&mut self) -> Option<(K, V)> {
266+
while !self.stack.is_empty() {
267+
let TreeNode {
268+
key: key,
269+
value: value,
270+
left: left,
271+
right: right,
272+
level: level
273+
} = self.stack.pop();
274+
275+
match left {
276+
Some(~left) => {
277+
let n = TreeNode {
278+
key: key,
279+
value: value,
280+
left: None,
281+
right: right,
282+
level: level
283+
};
284+
self.stack.push(n);
285+
self.stack.push(left);
286+
}
287+
None => {
288+
match right {
289+
Some(~right) => self.stack.push(right),
290+
None => ()
291+
}
292+
self.remaining -= 1;
293+
return Some((key, value))
294+
}
295+
}
296+
}
297+
None
298+
}
299+
300+
#[inline]
301+
fn size_hint(&self) -> (uint, Option<uint>) {
302+
(self.remaining, Some(self.remaining))
303+
}
304+
305+
}
306+
244307
impl<'self, T> Iterator<&'self T> for TreeSetIterator<'self, T> {
245308
/// Advance the iterator to the next node (in order). If there are no more nodes, return `None`.
246309
#[inline]

0 commit comments

Comments
 (0)