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> {
12 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
13 public final Tree<T> expand1() throws Parser.Ambiguous, Parser.Failed {
14 Iterator<Tree<T>> it = expand(true).iterator();
15 if (!it.hasNext()) throw new Parser.Failed();
19 /** expand this forest into a set of trees */
20 public abstract HashSet<Tree<T>> expand(boolean toss);
22 abstract boolean valid();
24 static <T> Forest<T> singleton(Token.Location loc, Sequence creator) { return create(loc, null, new Forest[] { }, creator, false, true); }
25 static <T> Forest<T> singleton(Token.Location loc, Forest<T> body, Sequence creator) { return create(loc, null, new Forest[] { body }, creator, false, true); }
26 static <T> Forest<T> leaf(Token.Location loc, T tag, Sequence creator) { return create(loc, tag, null, creator, false, false); }
27 public static <T> Forest<T> create(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
28 return new MultiForest<T>(loc, tag, tokens, creator, unwrap, singleton);
31 // Body //////////////////////////////////////////////////////////////////////////////
33 protected static class Body<T> {
35 private final Token.Location location;
37 private final Forest<T>[] tokens;
38 private final Sequence creator;
39 private final boolean unwrap;
40 private final boolean singleton;
42 private Body(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
45 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
46 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
47 this.creator = creator;
49 this.singleton = singleton;
52 private HashSet<Tree<T>> expand(boolean toss, ArrayList<Tree<T>> toks, int i, HashSet<Tree<T>> h) {
54 for(Body<T> b : (IterableForest<T>)tokens[0]) b.expand(toss, toks, i, h);
56 } else if (i==tokens.length) {
57 h.add(new Tree<T>(null, tag, toks.toArray(tree_hint)));
59 } else if (unwrap && i==tokens.length-1) {
60 if (tokens[i] != null)
61 for(Body b : (IterableForest<T>)tokens[i])
62 b.expand(toss, toks, 0, h);
65 if (tokens[i]!=null) {
66 HashSet<Tree<T>> exp = tokens[i].expand(toss);
68 for(Tree<T> r : exp) {
69 int old = toks.size();
71 expand(toss, toks, i+1, h);
72 while(toks.size() > old) toks.remove(toks.size()-1);
78 void addTo(HashSet<Body> h) {
79 if (!singleton) h.add(this);
80 else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
82 void addTo(FastSet<Body> h) {
83 if (!singleton) h.add(this, true);
84 else for(Body b : (IterableForest<T>)tokens[0]) b.addTo(h);
87 private boolean kcache = false;
88 private boolean keep = false;
89 public boolean keep() {
90 if (kcache) return keep;
92 for(Forest<T> token : tokens) if (!token.valid()) return keep = false;
93 return keep = creator==null || (creator.needs.size()==0 && creator.hates.size()==0);
95 public boolean keep(Iterable<Body<T>> h) {
96 if (keep()) return true;
97 for(Forest<T> token : tokens) if (!token.valid()) return false;
100 if (creator.hates.contains(b.creator) && b.keep(h)) return false;
101 if (creator.needs.contains(b.creator) && b.keep(h)) needs--;
103 return needs <= -1 * creator.needs.size();
107 public String toString() {
108 StringBuffer ret = new StringBuffer();
109 for(int i=0; i<tokens.length; i++) {
110 String q = tokens[i]==null ? "null" : tokens[i].toString();
111 if (q.length() > 0) {
116 String tail = ret.toString().trim();
117 String head = (tag!=null && !tag.toString().equals("")) ? (tail.length() > 0 ? tag+":" : tag+"") : "";
118 if (tail.length() > 0) tail = "{" + tail + "}";
124 // Ref //////////////////////////////////////////////////////////////////////////////
126 static abstract class IterableForest<T> extends Forest<T> implements Iterable<Forest.Body<T>> {
127 public abstract Iterator<Forest.Body<T>> iterator();
131 * This class represents a partially complete collection of
132 * forests to be viewed as a forest at some later date; once
133 * viewed, it becomes immutable
135 static class Ref<T> extends IterableForest<T> {
136 private FastSet<Forest> hp = new FastSet<Forest>();
137 private Forest res = null;
138 public boolean valid = false;
140 public void merge(Forest p) {
141 if (res != null) throw new Error("already resolved!");
142 if (p==null) throw new Error();
143 if (p!=this) hp.add(p);
145 public Iterator<Body<T>> iterator() { return ((IterableForest<T>)resolve()).iterator(); }
146 public HashSet<Tree<T>> expand(boolean toss) { return resolve().expand(toss); }
147 public boolean valid() { if (valid) return true; resolve(); return valid; }
148 public String toString() { return resolve().toString(); }
149 public Forest resolve() {
150 if (hp==null) return res;
151 HashSet<Body> results = null;
152 FastSet<Body> nh = new FastSet<Body>();
153 for(Forest<?> p : hp)
154 for(Body<?> b : (IterableForest<?>)p) {
155 if (b.keep() && (b.creator==null || !b.creator.lame)) { valid = true; b.addTo(nh); }
156 else results = new HashSet<Body>();
158 if (results != null) {
159 for(Forest<?> p : hp)
160 for(Body<?> b : (IterableForest<?>)p)
162 for(Body b : results) {
163 if (b.keep() && (b.creator==null || !b.creator.lame)) continue;
164 if (b.creator!=null && b.creator.lame) continue;
165 if (!b.keep(results)) continue;
171 return res = new MultiForest(nh, valid);
175 // Implementations //////////////////////////////////////////////////////////////////////////////
177 private static class MultiForest<T> extends IterableForest<T> {
178 private final FastSet<Body<T>> results;
179 private boolean valid;
180 public boolean valid() { return valid; }
181 private MultiForest(FastSet<Body<T>> results, boolean valid) { this.results = results; this.valid = valid; }
182 public MultiForest(Token.Location loc, T tag, Forest<T>[] tokens, Sequence creator, boolean unwrap, boolean singleton) {
183 this.results = new FastSet<Body<T>>(new Body(loc, tag, tokens, creator, unwrap, singleton));
186 public Iterator<Body<T>> iterator() { return results.iterator(); }
188 public HashSet<Tree<T>> expand(boolean toss) {
189 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
190 for(Body<T> b : results)
191 ret.addAll(b.expand(toss, new ArrayList<Tree<T>>(), 0, new HashSet<Tree<T>>()));
192 if (toss && ret.size() > 1) throw new Parser.Ambiguous(this);
196 // Display //////////////////////////////////////////////////////////////////////////////
198 private String toString = null;
199 public String toString() {
200 if (toString != null) return toString;
201 StringBuffer ret = new StringBuffer();
202 if (results.size()==1) {
203 for(Forest.Body<T> r : results)
205 return toString = ret.toString();
208 boolean first = true;
209 for(Forest.Body<T> r : results) {
210 if (!first) ret.append(' ');
215 return toString = ret.toString();
219 // Statics //////////////////////////////////////////////////////////////////////////////
221 private static Tree[] tree_hint = new Tree[0];
222 private static Body[] body_hint = new Body[0];
223 private static final Forest[] emptyForestArray = new Forest[0];