checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index df7a3ea..4f4baac 100644 (file)
@@ -7,19 +7,7 @@ import java.util.*;
 import java.lang.reflect.*;
 
 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
-public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/ implements Visitable<Forest.Body<T>> {
-
-    public abstract <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
-    private static class TreeMaker<T> extends HashSet<Tree<T>> implements Invokable<Forest.Body<T>,Boolean,Integer> {
-        public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
-        public void invoke(Forest.Body<T> bod, Boolean toss, Integer i) {
-            if (i==null) {
-                addAll(bod.expand(toss, 0, new TreeMaker<T>()));
-            } else {
-                bod.expand(toss, i, this);
-            }
-        }
-    }
+public abstract class Forest<T> /*extends PrintableTree<Forest.MyBody<T>>*/ implements Visitable<Forest.Body<T>> {
 
     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
     public final Tree<T> expand1() throws Ambiguous, ParseFailed {
@@ -54,7 +42,6 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/ implem
         public <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
             ivbc.invoke(this, b, c);
         }
-        public Iterator<Body<T>> iterator() { return new SingletonIterator<Body<T>>(this); }
 
         private final Input.Location    location;
         private final T                 tag;
@@ -123,6 +110,20 @@ public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/ implem
         public Forest resolve() { return this; }
     }
 
+    public abstract <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
+    private static class TreeMaker<T> extends HashSet<Tree<T>> implements Invokable<Forest.Body<T>,Boolean,Integer> {
+        public ArrayList<Tree<T>> toks = new ArrayList<Tree<T>>();
+        public void invoke(Forest.Body<T> bod, Boolean toss, Integer i) {
+            if (i==null) {
+                ArrayList<Tree<T>> toks = this.toks;
+                this.toks = new ArrayList<Tree<T>>();
+                bod.expand(toss, 0, this);
+                this.toks = toks;
+            } else {
+                bod.expand(toss, i, this);
+            }
+        }
+    }
 
     // Statics //////////////////////////////////////////////////////////////////////////////