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 {
}
/** 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;
- }
+ 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>(new Body<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;
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];
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);
return h;
}
- void addTo(FastSet<Body<T>> 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); }
}
public Forest resolve() { return this; }
}
- private static class MultiForest<T> extends Forest<T> {
- private final FastSet<Body<T>> results = new FastSet<Body<T>>();
- public MultiForest(Body<T> b) { results.add(b); }
- public Iterator<Body<T>> iterator() { return results.iterator(); }
- }
// Statics //////////////////////////////////////////////////////////////////////////////