X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FForest.java;h=786ffad6ff14c4c0f18d596809cfe511772d2461;hb=d93afe731bf19f7dbf6be40f916672d9c200b947;hp=748c4357bfbd84d837755ba18c06a00dfd785968;hpb=3998af3d6daba1f9dd81e0b1a42955a501968606;p=sbp.git diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 748c435..786ffad 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -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 /*extends PrintableTree>*/ implements Visitable>, Iterable> { - - public abstract void invoke(Invokable,B,C> ivbc, B b, C c); - private static class TreeMaker extends HashSet> implements Invokable,Boolean,Integer> { - public ArrayList> toks = new ArrayList>(); - public void invoke(Forest.Body bod, Boolean toss, Integer i) { - if (i==null) { - addAll(bod.expand(toss, 0, new TreeMaker())); - } else { - bod.expand(toss, i, this); - } - } - } +public abstract class Forest /*extends PrintableTree>*/ implements Visitable> { /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */ public final Tree expand1() throws Ambiguous, ParseFailed { @@ -31,7 +19,7 @@ public abstract class Forest /*extends PrintableTree>*/ implem /** expand this forest into a set of trees */ public HashSet> expand(boolean toss) { TreeMaker ret = new TreeMaker(); - invoke(ret, new Boolean(toss), null); + visit(ret, new Boolean(toss), null); if (toss && ret.size() > 1) throw new Ambiguous(this); return ret; } @@ -49,12 +37,14 @@ public abstract class Forest /*extends PrintableTree>*/ implem TreeMaker expand(boolean toss, int i, TreeMaker h); } - protected static class MyBody extends Forest implements Body /* extends PrintableTree> implements */,Iterable> { + protected static class MyBody extends Forest implements Body /* extends PrintableTree> implements *//*, Invokable>*/ { - public void invoke(Invokable,B,C> ivbc, B b, C c) { + public void visit(Invokable,B,C> ivbc, B b, C c) { ivbc.invoke(this, b, c); } - public Iterator> iterator() { return new SingletonIterator>(this); } + public void invoke(Tree ivbc, B b, C c) { + //ivbc.invoke(this, b, c); + } private final Input.Location location; private final T tag; @@ -74,17 +64,18 @@ public abstract class Forest /*extends PrintableTree>*/ implem public TreeMaker expand(boolean toss, int i, TreeMaker h) { if (singleton) { - tokens[0].invoke(h, toss, i); + tokens[0].visit(h, toss, i); } else if (i==tokens.length) { h.add(new Tree(null, tag, h.toks.toArray(tree_hint))); } else if (unwrap && i==tokens.length-1) { if (tokens[i] != null) - tokens[i].invoke(h, toss, 0); + tokens[i].visit(h, toss, 0); } else { boolean hit = false; + //tokens[i].visit(this, for(Tree r : tokens[i].expand(toss)) { hit = true; int old = h.toks.size(); @@ -112,34 +103,31 @@ public abstract class Forest /*extends PrintableTree>*/ implem * forests to be viewed as a forest at some later date; once * viewed, it becomes immutable */ - static class Ref extends Forest implements Iterable> { + static class Ref extends Forest { private FastSet> hp = new FastSet>(); public Ref() { } public void merge(Forest p) { if (p!=this) hp.add(p, true); } - public void invoke(Invokable,B,C> ivbc, B b, C c) { - for(Forest.Body bod : this) - ivbc.invoke(bod, b, c); - } - public Iterator> iterator() { - final Iterator> ift = hp==null ? null : hp.iterator(); - return new Iterator>() { - Iterator> 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 next() { - return ibt.next(); - } - }; - + public void visit(Invokable,B,C> ivbc, B b, C c) { + if (hp==null) return; + for(Forest f : hp) f.visit(ivbc, b, c); } public Forest resolve() { return this; } } + public abstract void visit(Invokable,B,C> ivbc, B b, C c); + private static class TreeMaker extends HashSet> implements Invokable,Boolean,Integer> { + public ArrayList> toks = new ArrayList>(); + public void invoke(Forest.Body bod, Boolean toss, Integer i) { + if (i==null) { + ArrayList> toks = this.toks; + this.toks = new ArrayList>(); + bod.expand(toss, 0, this); + this.toks = toks; + } else { + bod.expand(toss, i, this); + } + } + } // Statics //////////////////////////////////////////////////////////////////////////////