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 {
16 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
17 public abstract Tree<T> expand1() throws Ambiguous;
19 /** expand this forest into a set of trees */
20 public void expand(HashSet<Tree<T>> ht) { expand(ht, new HashSet<Forest<T>>(), null); }
22 /** create a new forest */
23 public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean unwrap) {
24 return new One<T>(loc, head, children, unwrap);
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 boolean ambiguous() { return false; }
34 public abstract void edges(GraphViz.Node n);
37 // One //////////////////////////////////////////////////////////////////////////////
39 /** A "single" forest with a head and child subforests */
40 public static class One<T> extends Forest<T> {
42 private final Input.Region location;
44 private final Forest<T>[] children;
46 /** if true, the last child's children are considered children of this node */
47 private final boolean unwrap;
49 private One(Input.Region loc, T head, Forest<T>[] children, boolean unwrap) {
52 this.children = children==null ? emptyForestArray : new Forest[children.length];
53 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
54 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
58 public Tree<T> expand1() throws Ambiguous {
59 Tree<T>[] ret = new Tree[children.length];
60 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
61 return new Tree<T>(location, head, ret, unwrap);
64 public void gather(HashSet<Forest<T>> hf) {
66 for(Forest<T> f : children) f.gather(hf);
68 public void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
69 if (ignore.contains(this)) { ht.add(bogus); return; }
70 expand(0, new Tree[children.length], ht, ignore, bogus);
72 private void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
73 if (i==children.length) {
74 ht.add(new Tree<T>(location, head, ta, unwrap));
76 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
77 children[i].expand(ht2, ignore, bogus);
78 for(Tree<T> tc : ht2) {
80 expand(i+1, ta, ht, ignore, bogus);
86 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
88 public boolean isTransparent() { return false; }
89 public boolean isHidden() { return false; }
90 public GraphViz.Node toGraphViz(GraphViz gv) {
91 if (gv.hasNode(this)) return gv.createNode(this);
92 GraphViz.Node n = gv.createNode(this);
93 n.label = headToString()==null?"":headToString();
98 boolean edges = false;
99 public void edges(GraphViz.Node n) {
102 for(int i=0; i<children.length; i++) {
103 if (i==children.length-1 && unwrap && !children[i].ambiguous()) {
104 children[i].edges(n);
106 n.edge(children[i], null);
111 protected String headToString() { return head==null?null:head.toString(); }
112 protected String headToJava() { return "null"; }
113 protected String left() { return "{"; }
114 protected String right() { return "}"; }
115 protected boolean ignoreSingleton() { return false; }
119 // Many //////////////////////////////////////////////////////////////////////////////
121 /** An "ambiguity node"; this is immutable once it has been "looked at" */
122 public static class Many<T> extends Forest<T> implements Iterable<One<T>> {
124 HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
125 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
126 private boolean touched = false;
130 public Iterator<One<T>> iterator() {
132 return (Iterator<One<T>>)(Object)hp.iterator(); // FIXME: fastset's iterator is not safe!
135 public Tree<T> expand1() throws Ambiguous {
138 HashSet<Forest<T>> hf0 = new HashSet<Forest<T>>();
139 Iterator<Forest<T>> ih = hp.iterator();
140 ih.next().gather(hf0);
141 for(Forest<T> f : hp) {
142 HashSet<Forest<T>> hf1 = new HashSet<Forest<T>>();
146 HashSet<Tree<T>> ht = new HashSet<Tree<T>>();
147 expand(ht, hf0, new Tree(null, "*"));
148 throw new Ambiguous((Forest<?>)this,
149 (HashSet<Tree<?>>)(Object)ht);
151 return hp.iterator().next().expand1();
154 public void gather(HashSet<Forest<T>> ht) {
157 for(Forest<T> f : hp) f.gather(ht);
160 private void touched() {
163 FastSet<Forest<T>> f2 = new FastSet<Forest<T>>();
165 if (f instanceof Forest.One) f2.add(f);
166 else for(Forest ff : ((Forest.Many<T>)f))
170 public boolean contains(Forest f) {
172 return hp.contains(f);
174 public void merge(Forest p) {
175 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
176 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
179 boolean ambiguous() {
181 if (hp.size()==0) return false;
182 if (hp.size()==1) return hp.iterator().next().ambiguous();
186 public void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
188 if (ignore.contains(this)) { ht.add(bogus); return; }
189 for (Forest<T> f : hp) f.expand(ht, ignore, bogus);
193 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
195 public boolean isTransparent() { return hp.size()==1; }
196 public boolean isHidden() { return hp.size()==0; }
197 public void edges(GraphViz.Node n) {
198 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
199 for(Forest f : hp) f.edges(n);
201 public GraphViz.Node toGraphViz(GraphViz gv) {
202 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
203 if (gv.hasNode(this)) return gv.createNode(this);
204 GraphViz.Node n = gv.createNode(this);
207 for(Forest f : hp) n.edge(f, null);
212 // Statics //////////////////////////////////////////////////////////////////////////////
214 private static Tree[] tree_hint = new Tree[0];
215 private static String[] string_hint = new String[0];
216 private static final Forest[] emptyForestArray = new Forest[0];
218 protected String headToString() { return null; }
219 protected String headToJava() { return "null"; }
220 protected String left() { return "<?"; }
221 protected String right() { return "?>"; }
222 protected boolean ignoreSingleton() { return true; }