checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index 0a20228..34759e3 100644 (file)
@@ -7,7 +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 Iterable<Forest.Body<T>> {
+public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/ implements Iterable<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 {
@@ -17,18 +17,29 @@ public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements
     }
 
     /** expand this forest into a set of trees */
-    public abstract HashSet<Tree<T>>  expand(boolean toss);
+    public HashSet<Tree<T>> expand(boolean toss) {
+        HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
+        for(Body<T> b : this)
+            ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
+        if (toss && ret.size() > 1) throw new Ambiguous(this);
+        return ret;
+    }
 
     static        <T> Forest<T> singleton(Input.Location loc)                       { return create(loc, null, new Forest[] { }, false, true); }
     static        <T> Forest<T> singleton(Input.Location loc, Forest<T> body)       { return create(loc, null, new Forest[] { body },  false, true); }
     static        <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
     public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
-        return new MultiForest<T>(loc, tag, tokens, unwrap, singleton);
+        return new MyBody<T>(loc, tag, tokens, unwrap, singleton);
     }
 
     // Body //////////////////////////////////////////////////////////////////////////////
 
-    protected static class Body<T> extends PrintableTree<Forest<T>> implements Iterable<Forest<T>> {
+    protected static interface Body<T> {
+         HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h);
+    }
+
+    protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements Iterable<Forest.Body<T>>*/ {
+        public Iterator<Body<T>> iterator() { return new SingletonIterator<Body<T>>(this); }
 
         private final Input.Location    location;
         private final T                 tag;
@@ -36,7 +47,7 @@ public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements
         private final boolean           unwrap;
         private final boolean           singleton;
 
-        private Body(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
+        private MyBody(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
             this.location = loc;
             this.tag = tag;
             this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
@@ -46,7 +57,7 @@ public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements
             this.singleton = singleton;
         }
 
-        private HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
+        public HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
             if (singleton) {
                 for(Body<T> b : tokens[0]) b.expand(toss, toks, i, h);
 
@@ -72,17 +83,11 @@ public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements
             return h;
         }
 
-        void addTo(FastSet<Body> h) {
-            if (!singleton) h.add(this, true);
-            else for(Body b : tokens[0]) b.addTo(h);
-        }
-
         protected String  headToString()         { return null; }
         protected String  headToJava()           { return null; }
         protected String  left()                 { return "{"; }
         protected String  right()                { return "}"; }
         protected boolean ignoreSingleton()      { return false; }
-        public    Iterator<Forest<T>> iterator() { return new ArrayIterator<Forest<T>>(tokens); }
     }
 
 
@@ -94,46 +99,30 @@ public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements
      *  viewed, it becomes immutable
      */
     static class Ref<T> extends Forest<T> {
-        private FastSet<Forest> hp = new FastSet<Forest>();
-        private Forest res = null;
+        private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
         public Ref() { }
-        public void merge(Forest p) {
-            if (res != null) throw new Error("already resolved!");
-            if (p==null) throw new Error();
-            if (p!=this) hp.add(p, true);
-        }
-        public Iterator<Body<T>> iterator() { return ((Forest<T>)resolve()).iterator(); }
-        public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
-        public Forest resolve() {
-            if (hp==null) return res;
-            FastSet<Body> nh      = new FastSet<Body>();
-            for(Forest<?> p : hp)
-                for(Body<?> b : (Forest<?>)p)
-                    b.addTo(nh);
-            res = new MultiForest(nh);
-            hp = null;
-            return res;
-        }
-    }
-
-    // Implementations //////////////////////////////////////////////////////////////////////////////
+        public void merge(Forest p) { if (p!=this) hp.add(p, true); }
+        public Iterator<Body<T>> iterator() {
+            final Iterator<Forest<T>> ift = hp==null ? null : hp.iterator();
+            return new Iterator<Body<T>>() {
+                Iterator<Body<T>> ibt = ift==null ? null : ift.hasNext() ? ift.next().iterator() : null;
+                public void remove() { throw new RuntimeException("not supported"); }
+                public boolean hasNext() {
+                    if (ibt==null) return false;
+                    if (ibt.hasNext()) return true;
+                    ibt = ift.hasNext() ? ift.next().iterator() : null;
+                    return hasNext();
+                }
+                public Body<T> next() {
+                    return ibt.next();
+                }
+            };
 
-    private static class MultiForest<T> extends Forest<T> {
-        private final FastSet<Body<T>> results;
-        private MultiForest(FastSet<Body<T>> results) { this.results = results; }
-        public MultiForest(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
-            this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, unwrap, singleton));
-        }
-        public Iterator<Body<T>> iterator() { return results.iterator(); }
-        public HashSet<Tree<T>> expand(boolean toss) {
-            HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
-            for(Body<T> b : results)
-                ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
-            if (toss && ret.size() > 1) throw new Ambiguous(this);
-            return ret;
         }
+        public Forest resolve() { return this; }
     }
 
+
     // Statics //////////////////////////////////////////////////////////////////////////////
 
     private static Tree[] tree_hint = new Tree[0];