about summary refs log tree commit diff
path: root/src/libextra
diff options
context:
space:
mode:
authorGraydon Hoare <graydon@mozilla.com>2013-07-18 12:37:18 -0700
committerGraydon Hoare <graydon@mozilla.com>2013-07-23 15:23:02 -0700
commit9e4ebdb9d612bc8d493f448386dbd99afb856818 (patch)
treefa1f48576962cb2417b38c704b07ac2916fb435e /src/libextra
parent31c180e5f5da0e27c60a3bf02da182f19c66cf8f (diff)
downloadrust-9e4ebdb9d612bc8d493f448386dbd99afb856818.tar.gz
rust-9e4ebdb9d612bc8d493f448386dbd99afb856818.zip
extra: add consume iter to treemap.
Diffstat (limited to 'src/libextra')
-rw-r--r--src/libextra/treemap.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/libextra/treemap.rs b/src/libextra/treemap.rs
index f9b2c8429cf..4ad1f56a9ae 100644
--- a/src/libextra/treemap.rs
+++ b/src/libextra/treemap.rs
@@ -204,6 +204,19 @@ impl<K: TotalOrd, V> TreeMap<K, V> {
     pub fn iter<'a>(&'a self) -> TreeMapIterator<'a, K, V> {
         TreeMapIterator{stack: ~[], node: &self.root, remaining: self.length}
     }
+
+    /// Get a lazy iterator that consumes the treemap.
+    pub fn consume_iter(self) -> TreeMapConsumeIterator<K, V> {
+        let TreeMap { root: root, length: length } = self;
+        let stk = match root {
+            None => ~[],
+            Some(~tn) => ~[tn]
+        };
+        TreeMapConsumeIterator {
+            stack: stk,
+            remaining: length
+        }
+    }
 }
 
 /// Lazy forward iterator over a map
@@ -241,6 +254,56 @@ impl<'self, K, V> Iterator<(&'self K, &'self V)> for TreeMapIterator<'self, K, V
     }
 }
 
+/// Lazy forward iterator over a map that consumes the map while iterating
+pub struct TreeMapConsumeIterator<K, V> {
+    priv stack: ~[TreeNode<K, V>],
+    priv remaining: uint
+}
+
+impl<K, V> Iterator<(K, V)> for TreeMapConsumeIterator<K,V> {
+    #[inline]
+    fn next(&mut self) -> Option<(K, V)> {
+        while !self.stack.is_empty() {
+            let TreeNode {
+                key: key,
+                value: value,
+                left: left,
+                right: right,
+                level: level
+            } = self.stack.pop();
+
+            match left {
+                Some(~left) => {
+                    let n = TreeNode {
+                        key: key,
+                        value: value,
+                        left: None,
+                        right: right,
+                        level: level
+                    };
+                    self.stack.push(n);
+                    self.stack.push(left);
+                }
+                None => {
+                    match right {
+                        Some(~right) => self.stack.push(right),
+                        None => ()
+                    }
+                    self.remaining -= 1;
+                    return Some((key, value))
+                }
+            }
+        }
+        None
+    }
+
+    #[inline]
+    fn size_hint(&self) -> (uint, Option<uint>) {
+        (self.remaining, Some(self.remaining))
+    }
+
+}
+
 impl<'self, T> Iterator<&'self T> for TreeSetIterator<'self, T> {
     /// Advance the iterator to the next node (in order). If there are no more nodes, return `None`.
     #[inline]