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.*;
11 * An efficient representation of a collection of trees (Tomita's
12 * shared packed parse forest).
15 public abstract class Forest<T> implements GraphViz.ToGraphViz {
17 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
18 public abstract Tree<T> expand1() throws Ambiguous;
20 /** expand this forest into a set of trees */
21 public void expand(HashSet<Tree<T>> ht) { expand(ht, new HashSet<Forest<T>>(), null); }
23 /** create a new forest */
24 public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
25 return new One<T>(loc, head, children, lift);
28 // Package-Private //////////////////////////////////////////////////////////////////////////////
30 abstract void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus);
31 abstract void gather(HashSet<Forest<T>> ignore);
32 abstract void edges(GraphViz.Node n);
33 boolean ambiguous() { return false; }
36 // One //////////////////////////////////////////////////////////////////////////////
38 /** A "single" forest with a head and child subforests */
39 private static class One<T> extends Forest<T> {
41 private final Input.Region location;
43 private final Forest<T>[] children;
45 /** if true, the last child's children are considered children of this node */
46 private final boolean lift;
48 private One(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
51 this.children = children==null ? emptyForestArray : new Forest[children.length];
52 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
53 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
57 public Tree<T> expand1() throws Ambiguous {
58 Tree<T>[] ret = new Tree[children.length];
59 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
60 return new Tree<T>(location, head, ret, lift);
63 void gather(HashSet<Forest<T>> hf) {
65 for(Forest<T> f : children) f.gather(hf);
67 void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
68 if (ignore.contains(this)) { ht.add(bogus); return; }
69 expand(0, new Tree[children.length], ht, ignore, bogus);
71 private void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
72 if (i==children.length) {
73 ht.add(new Tree<T>(location, head, ta, lift));
75 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
76 children[i].expand(ht2, ignore, bogus);
77 for(Tree<T> tc : ht2) {
79 expand(i+1, ta, ht, ignore, bogus);
85 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
87 public boolean isTransparent() { return false; }
88 public boolean isHidden() { return false; }
89 public GraphViz.Node toGraphViz(GraphViz gv) {
90 if (gv.hasNode(this)) return gv.createNode(this);
91 GraphViz.Node n = gv.createNode(this);
92 n.label = headToString()==null?"":headToString();
97 boolean edges = false; // FIXME ??
98 public void edges(GraphViz.Node n) {
101 for(int i=0; i<children.length; i++) {
102 if (i==children.length-1 && lift && !children[i].ambiguous()) {
103 children[i].edges(n);
105 n.edge(children[i], null);
110 protected String headToString() { return head==null?null:head.toString(); }
111 protected String headToJava() { return "null"; }
112 protected String left() { return "{"; }
113 protected String right() { return "}"; }
114 protected boolean ignoreSingleton() { return false; }
118 // Many //////////////////////////////////////////////////////////////////////////////
120 /** An "ambiguity node"; this is immutable once it has been "looked at" */
121 static class Many<T> extends Forest<T> {
123 HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
124 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
125 private boolean touched = false;
129 public Tree<T> expand1() throws Ambiguous {
132 HashSet<Forest<T>> hf0 = new HashSet<Forest<T>>();
133 Iterator<Forest<T>> ih = hp.iterator();
134 ih.next().gather(hf0);
135 for(Forest<T> f : hp) {
136 HashSet<Forest<T>> hf1 = new HashSet<Forest<T>>();
140 HashSet<Tree<T>> ht = new HashSet<Tree<T>>();
141 expand(ht, hf0, new Tree(null, "*"));
142 throw new Ambiguous((Forest<?>)this,
143 (HashSet<Tree<?>>)(Object)ht);
145 return hp.iterator().next().expand1();
148 void gather(HashSet<Forest<T>> ht) {
151 for(Forest<T> f : hp) f.gather(ht);
154 private void touched() {
158 FastSet<Forest<T>> f2 = new FastSet<Forest<T>>();
160 if (f instanceof Forest.One) f2.add(f);
161 else for(Forest ff : ((Forest.Many<T>)f))
166 public boolean contains(Forest f) {
168 return hp.contains(f);
170 public void merge(Forest p) {
171 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
172 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
175 boolean ambiguous() {
177 if (hp.size()==0) return false;
178 if (hp.size()==1) return hp.iterator().next().ambiguous();
182 void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
184 if (ignore.contains(this)) { ht.add(bogus); return; }
185 for (Forest<T> f : hp) f.expand(ht, ignore, bogus);
189 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
191 public boolean isTransparent() { return hp.size()==1; }
192 public boolean isHidden() { return hp.size()==0; }
193 public void edges(GraphViz.Node n) {
194 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
195 for(Forest f : hp) f.edges(n);
197 public GraphViz.Node toGraphViz(GraphViz gv) {
198 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
199 if (gv.hasNode(this)) return gv.createNode(this);
200 GraphViz.Node n = gv.createNode(this);
203 for(Forest f : hp) n.edge(f, null);
208 // Statics //////////////////////////////////////////////////////////////////////////////
210 private static Tree[] tree_hint = new Tree[0];
211 private static String[] string_hint = new String[0];
212 private static final Forest[] emptyForestArray = new Forest[0];
214 protected String headToString() { return null; }
215 protected String headToJava() { return "null"; }
216 protected String left() { return "<?"; }
217 protected String right() { return "?>"; }
218 protected boolean ignoreSingleton() { return true; }