X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FForest.java;h=7d872adda98152a53979ae8ac47c438dda3615fd;hp=e53bfcb10e32111f87c3038254d3a82390a2b4c9;hb=4b5b35b9dbcf29da663c72d12aaf6ac3d2083d27;hpb=5ae7d4fd7c53dab6df9aae2e42579efabf66f49e diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index e53bfcb..7d872ad 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -7,71 +7,66 @@ 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 /*extends PrintableTree>*/ - implements IntegerMappable, - GraphViz.ToGraphViz { +public abstract class Forest implements GraphViz.ToGraphViz { - private static int master_idx = 0; - private final int idx = master_idx++; - public int toInt() { return idx; } /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */ public abstract Tree expand1() throws Ambiguous; /** expand this forest into a set of trees */ public void expand(HashSet> ht) { expand(ht, new HashSet>(), null); } - public abstract void expand(HashSet> ht, HashSet> ignore, Tree bogus); - public abstract void gather(HashSet> ignore); - public HashSet> expand(boolean toss) { - HashSet> ret = new HashSet>(); - expand(ret); - return ret; - } - public static Forest create(Input.Region loc, T tag, Forest[] tokens, boolean unwrap) { - return new Body(loc, tag, tokens, unwrap); + /** create a new forest node */ + public static Forest create(Input.Region loc, T head, Forest[] children, boolean unwrap) { + return new Body(loc, head, children, unwrap); } + + // Package-Private ////////////////////////////////////////////////////////////////////////////// + + abstract void expand(HashSet> ht, HashSet> ignore, Tree bogus); + abstract void gather(HashSet> ignore); + public abstract void edges(GraphViz.Node n); public boolean ambiguous() { return false; } // Body ////////////////////////////////////////////////////////////////////////////// - public /*protected*/ static class Body extends Forest /* extends PrintableTree> implements */ { + private static class Body extends Forest /* extends PrintableTree> implements */ { private final Input.Region location; - private final T tag; - private final Forest[] tokens; + private final T head; + private final Forest[] children; private final boolean unwrap; - private Body(Input.Region loc, T tag, Forest[] tokens, boolean unwrap) { + private Body(Input.Region loc, T head, Forest[] children, boolean unwrap) { this.location = loc; - this.tag = tag; - this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length]; - if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length); - if (tokens != null) for(int i=0; i expand1() throws Ambiguous { - Tree[] ret = new Tree[tokens.length]; - for(int i=0; i(location, tag, ret, unwrap); + Tree[] ret = new Tree[children.length]; + for(int i=0; i(location, head, ret, unwrap); } public void gather(HashSet> hf) { hf.add(this); - for(Forest f : tokens) f.gather(hf); + for(Forest f : children) f.gather(hf); } public void expand(HashSet> ht, HashSet> ignore, Tree bogus) { if (ignore.contains(this)) { ht.add(bogus); return; } - expand(0, new Tree[tokens.length], ht, ignore, bogus); + expand(0, new Tree[children.length], ht, ignore, bogus); } public void expand(final int i, Tree[] ta, HashSet> ht, HashSet> ignore, Tree bogus) { - if (i==tokens.length) { - ht.add(new Tree(location, tag, ta, unwrap)); + if (i==children.length) { + ht.add(new Tree(location, head, ta, unwrap)); } else { HashSet> ht2 = new HashSet>(); - tokens[i].expand(ht2, ignore, bogus); + children[i].expand(ht2, ignore, bogus); for(Tree tc : ht2) { ta[i] = tc; expand(i+1, ta, ht, ignore, bogus); @@ -96,16 +91,16 @@ public abstract class Forest /*extends PrintableTree>*/ public void edges(GraphViz.Node n) { if (edges) return; edges = true; - for(int i=0; i /*extends PrintableTree>*/ } // GraphViz, ToInt ////////////////////////////////////////////////////////////////////////////// - + /* public int toInt() { if (hp.size()==1) return hp.iterator().next().toInt(); return super.toInt(); } + */ public boolean isTransparent() { return hp.size()==1; } public boolean isHidden() { return hp.size()==0; } public void edges(GraphViz.Node n) {