1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
4 import edu.berkeley.sbp.util.*;
7 import java.lang.reflect.*;
10 * An efficient representation of a collection of trees (Tomita's
11 * shared packed parse forest).
13 public abstract class Forest<T> implements GraphViz.ToGraphViz {
15 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
16 public abstract Tree<T> expand1() throws Ambiguous;
18 /** expand this forest into a set of trees */
19 public void expand(HashSet<Tree<T>> ht) { expand(ht, new HashSet<Forest<T>>(), null); }
21 /** create a new forest */
22 public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
23 return new One<T>(loc, head, children, lift);
26 // Package-Private //////////////////////////////////////////////////////////////////////////////
28 abstract void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus);
29 abstract void gather(HashSet<Forest<T>> ignore);
30 abstract void edges(GraphViz.Node n);
31 boolean ambiguous() { return false; }
34 // One //////////////////////////////////////////////////////////////////////////////
36 /** A "single" forest with a head and child subforests */
37 private static class One<T> extends Forest<T> {
39 private final Input.Region location;
41 private final Forest<T>[] children;
43 /** if true, the last child's children are considered children of this node */
44 private final boolean lift;
46 private One(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
49 this.children = children==null ? emptyForestArray : new Forest[children.length];
50 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
51 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
55 public Tree<T> expand1() throws Ambiguous {
56 Tree<T>[] ret = new Tree[children.length];
57 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
58 return new Tree<T>(location, head, ret, lift);
61 void gather(HashSet<Forest<T>> hf) {
63 for(Forest<T> f : children) f.gather(hf);
65 void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
66 if (ignore.contains(this)) { ht.add(bogus); return; }
67 expand(0, new Tree[children.length], ht, ignore, bogus);
69 private void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
70 if (i==children.length) {
71 ht.add(new Tree<T>(location, head, ta, lift));
73 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
74 children[i].expand(ht2, ignore, bogus);
75 for(Tree<T> tc : ht2) {
77 expand(i+1, ta, ht, ignore, bogus);
83 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
85 public boolean isTransparent() { return false; }
86 public boolean isHidden() { return false; }
87 public GraphViz.Node toGraphViz(GraphViz gv) {
88 if (gv.hasNode(this)) return gv.createNode(this);
89 GraphViz.Node n = gv.createNode(this);
90 n.label = headToString()==null?"":headToString();
95 boolean edges = false; // FIXME ??
96 public void edges(GraphViz.Node n) {
99 for(int i=0; i<children.length; i++) {
100 if (i==children.length-1 && lift && !children[i].ambiguous()) {
101 children[i].edges(n);
103 n.edge(children[i], null);
108 protected String headToString() { return head==null?null:head.toString(); }
109 protected String headToJava() { return "null"; }
110 protected String left() { return "{"; }
111 protected String right() { return "}"; }
112 protected boolean ignoreSingleton() { return false; }
116 // Many //////////////////////////////////////////////////////////////////////////////
118 /** An "ambiguity node"; this is immutable once it has been "looked at" */
119 static class Many<T> extends Forest<T> {
121 HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
122 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
123 private boolean touched = false;
127 public Tree<T> expand1() throws Ambiguous {
130 HashSet<Forest<T>> hf0 = new HashSet<Forest<T>>();
131 Iterator<Forest<T>> ih = hp.iterator();
132 ih.next().gather(hf0);
133 for(Forest<T> f : hp) {
134 HashSet<Forest<T>> hf1 = new HashSet<Forest<T>>();
138 HashSet<Tree<T>> ht = new HashSet<Tree<T>>();
139 expand(ht, hf0, new Tree(null, "*"));
140 throw new Ambiguous((Forest<?>)this,
141 (HashSet<Tree<?>>)(Object)ht);
143 return hp.iterator().next().expand1();
146 void gather(HashSet<Forest<T>> ht) {
149 for(Forest<T> f : hp) f.gather(ht);
152 private void touched() {
156 FastSet<Forest<T>> f2 = new FastSet<Forest<T>>();
158 if (f instanceof Forest.One) f2.add(f);
159 else for(Forest ff : ((Forest.Many<T>)f))
164 public boolean contains(Forest f) {
166 return hp.contains(f);
168 public void merge(Forest p) {
169 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
170 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
173 boolean ambiguous() {
175 if (hp.size()==0) return false;
176 if (hp.size()==1) return hp.iterator().next().ambiguous();
180 void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
182 if (ignore.contains(this)) { ht.add(bogus); return; }
183 for (Forest<T> f : hp) f.expand(ht, ignore, bogus);
187 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
189 public boolean isTransparent() { return hp.size()==1; }
190 public boolean isHidden() { return hp.size()==0; }
191 public void edges(GraphViz.Node n) {
192 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
193 for(Forest f : hp) f.edges(n);
195 public GraphViz.Node toGraphViz(GraphViz gv) {
196 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
197 if (gv.hasNode(this)) return gv.createNode(this);
198 GraphViz.Node n = gv.createNode(this);
201 for(Forest f : hp) n.edge(f, null);
206 // Statics //////////////////////////////////////////////////////////////////////////////
208 private static Tree[] tree_hint = new Tree[0];
209 private static String[] string_hint = new String[0];
210 private static final Forest[] emptyForestArray = new Forest[0];
212 protected String headToString() { return null; }
213 protected String headToJava() { return "null"; }
214 protected String left() { return "<?"; }
215 protected String right() { return "?>"; }
216 protected boolean ignoreSingleton() { return true; }