diff options
author | Rich Hickey <richhickey@gmail.com> | 2006-06-09 15:16:52 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2006-06-09 15:16:52 +0000 |
commit | 4b2bb1e90da439d7bed1527959baa0235b0b1bd7 (patch) | |
tree | 381ac65978722e5b4f0f5fdbabec4ce48f4043e6 /src/cli/runtime/PersistentTree.cs | |
parent | 0b9144199c7ce0c05cb99227252106ab313a99e4 (diff) |
made ISequential
Diffstat (limited to 'src/cli/runtime/PersistentTree.cs')
-rw-r--r-- | src/cli/runtime/PersistentTree.cs | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/src/cli/runtime/PersistentTree.cs b/src/cli/runtime/PersistentTree.cs index b4686264..cfdb05cb 100644 --- a/src/cli/runtime/PersistentTree.cs +++ b/src/cli/runtime/PersistentTree.cs @@ -25,7 +25,7 @@ namespace org.clojure.runtime * See Okasaki, Kahrs, Larsen et al */ -public class PersistentTree : IPersistentMap{ +public class PersistentTree : IPersistentMap, ISequential{ public readonly IComparer comp; public readonly Node tree; @@ -84,6 +84,17 @@ public IPersistentMap remove(Object key){ return new PersistentTree(comp, t.blacken(), _count - 1); } +public ISeq seq() {
+ if(_count > 0)
+ return Seq.create(tree, true);
+ return null;
+} + +public ISeq rseq() {
+ if(_count > 0)
+ return Seq.create(tree, false);
+ return null;
+} public IEnumerator GetEnumerator(){ return new NodeIEnumerator(tree, true); @@ -618,6 +629,44 @@ class RedBranchVal : RedBranch{ } } +public class Seq : ISeq{
+ readonly ISeq stack;
+ readonly bool asc;
+
+ Seq(ISeq stack, bool asc) {
+ this.stack = stack;
+ this.asc = asc;
+ }
+
+ internal static Seq create(Node t, bool asc){
+ return new Seq(push(t,asc,null),asc);
+ }
+
+ static ISeq push(Node t, bool asc, ISeq stack){
+ while(t != null)
+ {
+ stack = RT.cons(t,stack);
+ t = asc ? t.left() : t.right();
+ }
+ return stack;
+ }
+
+ public Object first() {
+ return stack.first();
+ }
+
+ public ISeq rest() {
+ Node t = (Node)stack.first();
+ ISeq nextstack = push(asc ? t.right() : t.left(),asc,stack.rest());
+ if(nextstack != null)
+ {
+ return new Seq(nextstack,asc);
+ }
+ return null;
+ }
+}
+ + public class NodeIEnumerator : IEnumerator{ Stack stack = new Stack(); bool asc;
|