about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorEric Holk <eholk@mozilla.com>2011-08-26 10:50:02 -0700
committerEric Holk <eholk@mozilla.com>2011-08-26 18:03:32 -0700
commit2fab948e01477a9862142993be486bca36aa8152 (patch)
tree289e692dc4467dce5836415bf9b14873b3029839 /src/lib
parentcd913b454d1974967536c99c2e0b744b548536c5 (diff)
downloadrust-2fab948e01477a9862142993be486bca36aa8152.tar.gz
rust-2fab948e01477a9862142993be486bca36aa8152.zip
stdlib: Added a treemap traversal function.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/treemap.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/lib/treemap.rs b/src/lib/treemap.rs
index 97ab1353c6b..6f73df36ef2 100644
--- a/src/lib/treemap.rs
+++ b/src/lib/treemap.rs
@@ -15,6 +15,7 @@ export treemap;
 export init;
 export insert;
 export find;
+export traverse;
 
 tag tree_node<@K, @V> {
     empty;
@@ -74,3 +75,25 @@ fn find<@K, @V>(m : &treemap<K, V>, k : &K) -> option<V> {
     }
   }
 }
+
+// Performs an in-order traversal
+fn traverse<@K, @V>(m : &treemap<K, V>, f : fn(&K, &V)) {
+  alt *m {
+    empty. { }
+    node(@k, @v, _, _) {
+      // copy v to make aliases work out
+      let v1 = v;
+      alt *m {
+        node(_, _, left, _) {
+          traverse(left, f);
+        }
+      }
+      f(k, v1);
+      alt *m {
+        node(_, _, _, right) {
+          traverse(right, f);
+        }
+      }
+    }
+  }
+}