X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FForest.java;h=cf64ed272a57322dd12473acf5c44049a07bf920;hp=04e06be6d80646a17e87494cf363a586a5bf6a5d;hb=dc9bb3a45ed306e2e35549076842b3e74efecb48;hpb=a8478f5ddfbfbc8d910d09f27163cbd55752d3b6 diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 04e06be..cf64ed2 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -1,12 +1,9 @@ // 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 java.lang.reflect.*; /** * @@ -20,26 +17,31 @@ public abstract class Forest implements GraphViz.ToGraphViz { public abstract Tree expand1() throws Ambiguous; /** expand this forest into a set of trees */ - public void expand(HashSet> ht) { expand(ht, new HashSet>(), null); } - - static Forest create(Input.Region loc, NodeType head, Forest[] children, boolean lift) { - if (loc == null) throw new Error(); - return new One(loc, head, children, lift); + public Iterable> expand() { + HashSet> ht = new HashSet>(); + expand(ht, new HashSet>(), null); + return ht; } - /** create a new forest */ - public static Forest create(Input.Region loc, NodeType head, Forest[] 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 ////////////////////////////////////////////////////////////////////////////// + static Forest create(Input.Region region, NodeType head, Forest[] children, boolean lift) { + if (region == null) throw new RuntimeException("invoked Forest.create(region=null) -- this should never happen"); + return new One(region, head, children, lift); + } + + /** create a new forest */ + public static Forest create(Input.Region region, NodeType head, Forest[] children) { + return Forest.create(region, head, children, false); } + abstract void expand(HashSet> ht, HashSet> ignore, Tree bogus); abstract void gather(HashSet> 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 */ @@ -52,11 +54,12 @@ public abstract class Forest implements GraphViz.ToGraphViz { /** 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[] children, boolean lift) { this.location = loc; this.head = head; + if (head==null) throw new RuntimeException("invoked Forest.create(,null,,,) -- this should never happen"); this.children = children==null ? emptyForestArray : new Forest[children.length]; if (children != null) System.arraycopy(children, 0, this.children, 0, children.length); if (children != null) for(int i=0; i implements GraphViz.ToGraphViz { if (ignore.contains(this)) { ht.add(bogus); return; } expand(0, new Tree[children.length], ht, ignore, bogus); } - private void expand(final int i, Tree[] ta, HashSet> ht, HashSet> ignore, Tree bogus) { + private void expand(final int i, Tree[] ta, HashSet> ht, HashSet> ignore, + Tree bogus) { if (i==children.length) { ht.add(new Tree(location, head, ta, lift)); } else { @@ -129,13 +133,12 @@ public abstract class Forest implements GraphViz.ToGraphViz { /** An "ambiguity node"; this is immutable once it has been "looked at" */ static class Many extends Forest { - HashSet parents = new HashSet(); private FastSet> hp = new FastSet>(); private boolean touched = false; 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 expand1() throws Ambiguous { touched();