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 Visitable<Forest.Body<T>>, Iterable<Forest.Body<T>> {
+public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/ implements Visitable<Forest.Body<T>> {
public abstract <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c);
private static class TreeMaker<T> extends HashSet<Tree<T>> implements Invokable<Forest.Body<T>,Boolean,Integer> {
TreeMaker<T> expand(boolean toss, int i, TreeMaker<T> h);
}
- protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */,Iterable<Forest.Body<T>> {
+ protected static class MyBody<T> extends Forest<T> implements Body<T> /* extends PrintableTree<Forest<T>> implements */ {
public <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
ivbc.invoke(this, b, c);
* forests to be viewed as a forest at some later date; once
* viewed, it becomes immutable
*/
- static class Ref<T> extends Forest<T> implements Iterable<Forest.Body<T>> {
+ static class Ref<T> extends Forest<T> {
private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
public Ref() { }
public void merge(Forest p) { if (p!=this) hp.add(p, true); }
public <B,C> void invoke(Invokable<Forest.Body<T>,B,C> ivbc, B b, C c) {
- for(Forest.Body<T> bod : this)
- ivbc.invoke(bod, b, c);
- }
- public Iterator<Body<T>> iterator() {
- final Iterator<Forest<T>> ift = hp==null ? null : hp.iterator();
- return new Iterator<Body<T>>() {
- Iterator<Body<T>> 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<T> next() {
- return ibt.next();
- }
- };
-
+ if (hp==null) return;
+ for(Forest<T> f : hp) f.invoke(ivbc, b, c);
}
public Forest resolve() { return this; }
}