cleanups, reorg, and commenting
[sbp.git] / src / edu / berkeley / sbp / Forest.java
index 1730f60..cf64ed2 100644 (file)
@@ -1,12 +1,9 @@
 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp;
 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp;
-import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.Sequence.Position;
 import edu.berkeley.sbp.util.*;
 import java.io.*;
 import java.util.*;
 import edu.berkeley.sbp.util.*;
 import java.io.*;
 import java.util.*;
-import java.lang.reflect.*;
 
 /**
  *   <font color=blue>
 
 /**
  *   <font color=blue>
@@ -20,26 +17,31 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
     public abstract Tree<NodeType> expand1() throws Ambiguous;
 
     /** expand this forest into a set of trees */
     public abstract Tree<NodeType> expand1() throws Ambiguous;
 
     /** expand this forest into a set of trees */
-    public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
-
-    static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
-        if (loc == null) throw new RuntimeException("invoked Forest.create(null,,,,) -- this should never happen");
-        return new One<NodeType>(loc, head, children, lift);
+    public Iterable<Tree<NodeType>> expand() {
+        HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
+        expand(ht, new HashSet<Forest<NodeType>>(), null);
+        return ht;
     }
 
     }
 
-    /** create a new forest */
-    public static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children) {
-        return Forest.create(loc, head, children, false); }
+    /** returns the input Region which this Forest was parsed from */
+    public abstract Input.Region getRegion();
 
     // Package-Private //////////////////////////////////////////////////////////////////////////////
 
 
     // Package-Private //////////////////////////////////////////////////////////////////////////////
 
+    static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children, boolean lift) {
+        if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen");
+        return new One<NodeType>(region, head, children, lift);
+    }
+
+    /** create a new forest */
+    public static <NodeType> Forest<NodeType> create(Input.Region region, NodeType head, Forest<NodeType>[] children) {
+        return Forest.create(region, head, children, false); }
+
     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
     abstract void gather(HashSet<Forest<NodeType>> ignore);
     abstract void edges(GraphViz.Node n);
     boolean ambiguous() { return false; }
     
     abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
     abstract void gather(HashSet<Forest<NodeType>> ignore);
     abstract void edges(GraphViz.Node n);
     boolean ambiguous() { return false; }
     
-    abstract Input.Region getRegion();
-
     // One //////////////////////////////////////////////////////////////////////////////
 
     /** A "single" forest with a head and child subforests */    
     // One //////////////////////////////////////////////////////////////////////////////
 
     /** A "single" forest with a head and child subforests */    
@@ -52,7 +54,7 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
         /** if true, the last child's children are considered children of this node */
         private final boolean           lift;
 
         /** if true, the last child's children are considered children of this node */
         private final boolean           lift;
 
-        Input.Region getRegion() { return location; }
+        public Input.Region getRegion() { return location; }
 
         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
             this.location = loc;
 
         private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
             this.location = loc;
@@ -78,7 +80,8 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
             if (ignore.contains(this)) { ht.add(bogus); return; }
             expand(0, new Tree[children.length], ht, ignore, bogus);
         }
             if (ignore.contains(this)) { ht.add(bogus); return; }
             expand(0, new Tree[children.length], ht, ignore, bogus);
         }
-        private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
+        private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore,
+                            Tree<NodeType> bogus) {
             if (i==children.length) {
                 ht.add(new Tree<NodeType>(location, head, ta, lift));
             } else {
             if (i==children.length) {
                 ht.add(new Tree<NodeType>(location, head, ta, lift));
             } else {
@@ -135,7 +138,7 @@ public abstract class Forest<NodeType> implements GraphViz.ToGraphViz {
 
         public Many() { }
 
 
         public Many() { }
 
-        Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
+        public Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
 
         public Tree<NodeType> expand1() throws Ambiguous {
             touched();
 
         public Tree<NodeType> expand1() throws Ambiguous {
             touched();