1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.util.*;
7 import java.lang.reflect.*;
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> extends PrintableTree<Forest.Body<T>> implements Iterable<Forest.Body<T>> {
12 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13 public final Tree<T> expand1() throws Ambiguous, ParseFailed {
14 Iterator<Tree<T>> it = expand(true).iterator();
15 if (!it.hasNext()) throw new ParseFailed();
19 /** expand this forest into a set of trees */
20 //public abstract HashSet<Tree<T>> expand(boolean toss);
21 public HashSet<Tree<T>> expand(boolean toss) {
22 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
24 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
25 if (toss && ret.size() > 1) throw new Ambiguous(this);
29 static <T> Forest<T> singleton(Input.Location loc) { return create(loc, null, new Forest[] { }, false, true); }
30 static <T> Forest<T> singleton(Input.Location loc, Forest<T> body) { return create(loc, null, new Forest[] { body }, false, true); }
31 static <T> Forest<T> leaf(Input.Location loc, T tag) { return create(loc, tag, null, false, false); }
32 public static <T> Forest<T> create(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
33 return new MultiForest<T>(new Body<T>(loc, tag, tokens, unwrap, singleton));
36 // Body //////////////////////////////////////////////////////////////////////////////
38 protected static class Body<T> extends PrintableTree<Forest<T>> implements Iterable<Forest<T>> {
40 private final Input.Location location;
42 private final Forest<T>[] tokens;
43 private final boolean unwrap;
44 private final boolean singleton;
46 private Body(Input.Location loc, T tag, Forest<T>[] tokens, boolean unwrap, boolean singleton) {
49 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
50 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
51 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
53 this.singleton = singleton;
56 private HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
58 for(Body<T> b : tokens[0]) b.expand(toss, toks, i, h);
60 } else if (i==tokens.length) {
61 h.add(new Tree<T>(null, tag, toks.toArray(tree_hint)));
63 } else if (unwrap && i==tokens.length-1) {
64 if (tokens[i] != null)
65 for(Body b : tokens[i])
66 b.expand(toss, toks, 0, h);
70 for(Tree<T> r : tokens[i].expand(toss)) {
72 int old = toks.size();
74 expand(toss, toks, i+1, h);
75 while(toks.size() > old) toks.remove(toks.size()-1);
77 //if (!hit) throw new Error();
82 void addTo(FastSet<Body<T>> h) {
83 /*if (!singleton)*/ h.add(this, true);
84 //else for(Body b : tokens[0]) b.addTo(h);
87 protected String headToString() { return null; }
88 protected String headToJava() { return null; }
89 protected String left() { return "{"; }
90 protected String right() { return "}"; }
91 protected boolean ignoreSingleton() { return false; }
92 public Iterator<Forest<T>> iterator() { return new ArrayIterator<Forest<T>>(tokens); }
96 // Ref //////////////////////////////////////////////////////////////////////////////
99 * This class represents a partially complete collection of
100 * forests to be viewed as a forest at some later date; once
101 * viewed, it becomes immutable
103 static class Ref<T> extends Forest<T> {
104 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
106 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
107 public Iterator<Body<T>> iterator() {
108 final Iterator<Forest<T>> ift = hp==null ? null : hp.iterator();
109 return new Iterator<Body<T>>() {
110 Iterator<Body<T>> ibt = ift==null ? null : ift.hasNext() ? ift.next().iterator() : null;
111 public void remove() { throw new RuntimeException("not supported"); }
112 public boolean hasNext() {
113 if (ibt==null) return false;
114 if (ibt.hasNext()) return true;
115 ibt = ift.hasNext() ? ift.next().iterator() : null;
118 public Body<T> next() {
124 public Forest resolve() { return this; }
127 private static class MultiForest<T> extends Forest<T> {
128 private final FastSet<Body<T>> results = new FastSet<Body<T>>();
129 public MultiForest(Body<T> b) { results.add(b); }
130 public Iterator<Body<T>> iterator() { return results.iterator(); }
133 // Statics //////////////////////////////////////////////////////////////////////////////
135 private static Tree[] tree_hint = new Tree[0];
136 private static final Forest[] emptyForestArray = new Forest[0];
138 protected String headToString() { return null; }
139 protected String headToJava() { return null; }
140 protected String left() { return "<?"; }
141 protected String right() { return "?>"; }
142 protected boolean ignoreSingleton() { return true; }