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<NodeType> implements GraphViz.ToGraphViz {
17 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
18 public abstract Tree<NodeType> expand1() throws Ambiguous;
20 /** expand this forest into a set of trees */
21 public void expand(HashSet<Tree<NodeType>> ht) { expand(ht, new HashSet<Forest<NodeType>>(), null); }
23 static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
24 if (loc == null) throw new Error();
25 return new One<NodeType>(loc, head, children, lift);
28 /** create a new forest */
29 public static <NodeType> Forest<NodeType> create(Input.Region loc, NodeType head, Forest<NodeType>[] children) {
30 return Forest.create(loc, head, children, false); }
32 // Package-Private //////////////////////////////////////////////////////////////////////////////
34 abstract void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus);
35 abstract void gather(HashSet<Forest<NodeType>> ignore);
36 abstract void edges(GraphViz.Node n);
37 boolean ambiguous() { return false; }
39 abstract Input.Region getRegion();
41 // One //////////////////////////////////////////////////////////////////////////////
43 /** A "single" forest with a head and child subforests */
44 private static class One<NodeType> extends Forest<NodeType> {
46 private final Input.Region location;
47 private final NodeType head;
48 private final Forest<NodeType>[] children;
50 /** if true, the last child's children are considered children of this node */
51 private final boolean lift;
53 Input.Region getRegion() { return location; }
55 private One(Input.Region loc, NodeType head, Forest<NodeType>[] children, boolean lift) {
58 this.children = children==null ? emptyForestArray : new Forest[children.length];
59 if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
60 if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
64 public Tree<NodeType> expand1() throws Ambiguous {
65 Tree<NodeType>[] ret = new Tree[children.length];
66 for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
67 return new Tree<NodeType>(location, head, ret, lift);
70 void gather(HashSet<Forest<NodeType>> hf) {
72 for(Forest<NodeType> f : children) f.gather(hf);
74 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
75 if (ignore.contains(this)) { ht.add(bogus); return; }
76 expand(0, new Tree[children.length], ht, ignore, bogus);
78 private void expand(final int i, Tree<NodeType>[] ta, HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
79 if (i==children.length) {
80 ht.add(new Tree<NodeType>(location, head, ta, lift));
82 HashSet<Tree<NodeType>> ht2 = new HashSet<Tree<NodeType>>();
83 children[i].expand(ht2, ignore, bogus);
84 for(Tree<NodeType> tc : ht2) {
86 expand(i+1, ta, ht, ignore, bogus);
92 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
94 public boolean isTransparent() { return false; }
95 public boolean isHidden() { return false; }
96 public GraphViz.Node toGraphViz(GraphViz gv) {
97 if (gv.hasNode(this)) return gv.createNode(this);
98 GraphViz.Node n = gv.createNode(this);
99 n.label = headToString()==null?"":headToString();
104 boolean edges = false; // FIXME ??
105 public void edges(GraphViz.Node n) {
108 for(int i=0; i<children.length; i++) {
109 if (i==children.length-1 && lift && !children[i].ambiguous()) {
110 children[i].edges(n);
112 n.edge(children[i], null);
117 protected String headToString() { return head==null?null:head.toString(); }
118 protected String headToJava() { return "null"; }
119 protected String left() { return "{"; }
120 protected String right() { return "}"; }
121 protected boolean ignoreSingleton() { return false; }
125 // Many //////////////////////////////////////////////////////////////////////////////
127 /** An "ambiguity node"; this is immutable once it has been "looked at" */
128 static class Many<NodeType> extends Forest<NodeType> {
130 HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
131 private FastSet<Forest<NodeType>> hp = new FastSet<Forest<NodeType>>();
132 private boolean touched = false;
136 Input.Region getRegion() { return hp.iterator().next().getRegion(); } // all should be identical
138 public Tree<NodeType> expand1() throws Ambiguous {
141 HashSet<Forest<NodeType>> hf0 = new HashSet<Forest<NodeType>>();
142 Iterator<Forest<NodeType>> ih = hp.iterator();
143 ih.next().gather(hf0);
144 for(Forest<NodeType> f : hp) {
145 HashSet<Forest<NodeType>> hf1 = new HashSet<Forest<NodeType>>();
149 HashSet<Tree<NodeType>> ht = new HashSet<Tree<NodeType>>();
150 expand(ht, hf0, new Tree(null, "*"));
151 throw new Ambiguous((Forest<?>)this,
152 (HashSet<Tree<?>>)(Object)ht);
154 return hp.iterator().next().expand1();
157 void gather(HashSet<Forest<NodeType>> ht) {
160 for(Forest<NodeType> f : hp) f.gather(ht);
163 private void touched() {
167 FastSet<Forest<NodeType>> f2 = new FastSet<Forest<NodeType>>();
169 if (f instanceof Forest.One) f2.add(f);
170 else for(Forest ff : ((Forest.Many<NodeType>)f))
175 public boolean contains(Forest f) {
177 return hp.contains(f);
179 public void merge(Forest p) {
180 if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
181 if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
184 boolean ambiguous() {
186 if (hp.size()==0) return false;
187 if (hp.size()==1) return hp.iterator().next().ambiguous();
191 void expand(HashSet<Tree<NodeType>> ht, HashSet<Forest<NodeType>> ignore, Tree<NodeType> bogus) {
193 if (ignore.contains(this)) { ht.add(bogus); return; }
194 for (Forest<NodeType> f : hp) f.expand(ht, ignore, bogus);
198 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
200 public boolean isTransparent() { return hp.size()==1; }
201 public boolean isHidden() { return hp.size()==0; }
202 public void edges(GraphViz.Node n) {
203 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
204 for(Forest f : hp) f.edges(n);
206 public GraphViz.Node toGraphViz(GraphViz gv) {
207 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
208 if (gv.hasNode(this)) return gv.createNode(this);
209 GraphViz.Node n = gv.createNode(this);
212 for(Forest f : hp) n.edge(f, null);
217 // Statics //////////////////////////////////////////////////////////////////////////////
219 private static Tree[] tree_hint = new Tree[0];
220 private static String[] string_hint = new String[0];
221 private static final Forest[] emptyForestArray = new Forest[0];
223 protected String headToString() { return null; }
224 protected String headToJava() { return "null"; }
225 protected String left() { return "<?"; }
226 protected String right() { return "?>"; }
227 protected boolean ignoreSingleton() { return true; }