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 static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
24 return new One<T>(loc, head, children, lift);
27 /** create a new forest */
28 public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children) {
29 return Forest.create(loc, head, children, false); }
31 // Package-Private //////////////////////////////////////////////////////////////////////////////
33 abstract void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus);
34 abstract void gather(HashSet<Forest<T>> ignore);
35 abstract void edges(GraphViz.Node n);
36 boolean ambiguous() { return false; }
39 // One //////////////////////////////////////////////////////////////////////////////
41 /** A "single" forest with a head and child subforests */
42 private static class One<T> extends Forest<T> {
44 private final Input.Region location;
46 private final Forest<T>[] children;
48 /** if true, the last child's children are considered children of this node */
49 private final boolean lift;
51 private One(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
54 this.children = children==null ? emptyForestArray : new Forest[children.length];
55 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
56 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
60 public Tree<T> expand1() throws Ambiguous {
61 Tree<T>[] ret = new Tree[children.length];
62 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
63 return new Tree<T>(location, head, ret, lift);
66 void gather(HashSet<Forest<T>> hf) {
68 for(Forest<T> f : children) f.gather(hf);
70 void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
71 if (ignore.contains(this)) { ht.add(bogus); return; }
72 expand(0, new Tree[children.length], ht, ignore, bogus);
74 private void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
75 if (i==children.length) {
76 ht.add(new Tree<T>(location, head, ta, lift));
78 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
79 children[i].expand(ht2, ignore, bogus);
80 for(Tree<T> tc : ht2) {
82 expand(i+1, ta, ht, ignore, bogus);
88 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
90 public boolean isTransparent() { return false; }
91 public boolean isHidden() { return false; }
92 public GraphViz.Node toGraphViz(GraphViz gv) {
93 if (gv.hasNode(this)) return gv.createNode(this);
94 GraphViz.Node n = gv.createNode(this);
95 n.label = headToString()==null?"":headToString();
100 boolean edges = false; // FIXME ??
101 public void edges(GraphViz.Node n) {
104 for(int i=0; i<children.length; i++) {
105 if (i==children.length-1 && lift && !children[i].ambiguous()) {
106 children[i].edges(n);
108 n.edge(children[i], null);
113 protected String headToString() { return head==null?null:head.toString(); }
114 protected String headToJava() { return "null"; }
115 protected String left() { return "{"; }
116 protected String right() { return "}"; }
117 protected boolean ignoreSingleton() { return false; }
121 // Many //////////////////////////////////////////////////////////////////////////////
123 /** An "ambiguity node"; this is immutable once it has been "looked at" */
124 static class Many<T> extends Forest<T> {
126 HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
127 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
128 private boolean touched = false;
132 public Tree<T> expand1() throws Ambiguous {
135 HashSet<Forest<T>> hf0 = new HashSet<Forest<T>>();
136 Iterator<Forest<T>> ih = hp.iterator();
137 ih.next().gather(hf0);
138 for(Forest<T> f : hp) {
139 HashSet<Forest<T>> hf1 = new HashSet<Forest<T>>();
143 HashSet<Tree<T>> ht = new HashSet<Tree<T>>();
144 expand(ht, hf0, new Tree(null, "*"));
145 throw new Ambiguous((Forest<?>)this,
146 (HashSet<Tree<?>>)(Object)ht);
148 return hp.iterator().next().expand1();
151 void gather(HashSet<Forest<T>> ht) {
154 for(Forest<T> f : hp) f.gather(ht);
157 private void touched() {
161 FastSet<Forest<T>> f2 = new FastSet<Forest<T>>();
163 if (f instanceof Forest.One) f2.add(f);
164 else for(Forest ff : ((Forest.Many<T>)f))
169 public boolean contains(Forest f) {
171 return hp.contains(f);
173 public void merge(Forest p) {
174 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
175 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
178 boolean ambiguous() {
180 if (hp.size()==0) return false;
181 if (hp.size()==1) return hp.iterator().next().ambiguous();
185 void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
187 if (ignore.contains(this)) { ht.add(bogus); return; }
188 for (Forest<T> f : hp) f.expand(ht, ignore, bogus);
192 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
194 public boolean isTransparent() { return hp.size()==1; }
195 public boolean isHidden() { return hp.size()==0; }
196 public void edges(GraphViz.Node n) {
197 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
198 for(Forest f : hp) f.edges(n);
200 public GraphViz.Node toGraphViz(GraphViz gv) {
201 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
202 if (gv.hasNode(this)) return gv.createNode(this);
203 GraphViz.Node n = gv.createNode(this);
206 for(Forest f : hp) n.edge(f, null);
211 // Statics //////////////////////////////////////////////////////////////////////////////
213 private static Tree[] tree_hint = new Tree[0];
214 private static String[] string_hint = new String[0];
215 private static final Forest[] emptyForestArray = new Forest[0];
217 protected String headToString() { return null; }
218 protected String headToJava() { return "null"; }
219 protected String left() { return "<?"; }
220 protected String right() { return "?>"; }
221 protected boolean ignoreSingleton() { return true; }