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.*;
9 /** an efficient representation of a collection of trees (Tomita's shared packed parse forest) */
10 public abstract class Forest<T> /*extends PrintableTree<Forest.Body<T>>*/
11 implements IntegerMappable,
14 private static int master_idx = 0;
15 private final int idx = master_idx++;
16 public int toInt() { return idx; }
18 /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
19 public abstract Tree<T> expand1() throws Ambiguous;
21 /** expand this forest into a set of trees */
22 public void expand(HashSet<Tree<T>> ht) { expand(ht, new HashSet<Forest<T>>(), null); }
23 public abstract void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus);
24 public abstract void gather(HashSet<Forest<T>> ignore);
25 public HashSet<Tree<T>> expand(boolean toss) {
26 HashSet<Tree<T>> ret = new HashSet<Tree<T>>();
31 public static <T> Forest<T> create(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap) {
32 return new Body<T>(loc, tag, tokens, unwrap);
34 public abstract void edges(GraphViz.Node n);
35 public boolean ambiguous() { return false; }
37 // Body //////////////////////////////////////////////////////////////////////////////
39 public /*protected*/ static class Body<T> extends Forest<T> /* extends PrintableTree<Forest<T>> implements */ {
41 private final Input.Region location;
43 private final Forest<T>[] tokens;
44 private final boolean unwrap;
46 private Body(Input.Region loc, T tag, Forest<T>[] tokens, boolean unwrap) {
49 this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length];
50 if (tokens != null) System.arraycopy(tokens, 0, this.tokens, 0, tokens.length);
51 if (tokens != null) for(int i=0; i<tokens.length; i++) if (tokens[i]==null) throw new Error(i+"");
55 public Tree<T> expand1() throws Ambiguous {
56 Tree<T>[] ret = new Tree[tokens.length];
57 for(int i=0; i<tokens.length; i++)
58 ret[i] = tokens[i].expand1();
59 return new Tree<T>(location, tag, ret, unwrap);
61 public void gather(HashSet<Forest<T>> hf) {
63 for(Forest<T> f : tokens) f.gather(hf);
65 public 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[tokens.length], ht, ignore, bogus);
69 public void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
70 if (i==tokens.length) {
71 ht.add(new Tree<T>(location, tag, ta, unwrap));
73 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
74 tokens[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;
96 public void edges(GraphViz.Node n) {
99 for(int i=0; i<tokens.length; i++) {
100 if (i==tokens.length-1 && unwrap && !tokens[i].ambiguous()) {
103 n.edge(tokens[i], null);
108 protected String headToString() { return tag==null?null:tag.toString(); }
109 protected String headToJava() { return "null"; }
110 protected String left() { return "{"; }
111 protected String right() { return "}"; }
112 protected boolean ignoreSingleton() { return false; }
116 // Ref //////////////////////////////////////////////////////////////////////////////
119 * This class represents a partially complete collection of
120 * forests to be viewed as a forest at some later date; once
121 * viewed, it becomes immutable
123 static class Ref<T> extends Forest<T> {
124 public HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
125 private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
129 public Tree<T> expand1() throws Ambiguous {
130 if (hp.size() > 1) throw new Ambiguous(this);
131 return hp.iterator().next().expand1();
134 public void gather(HashSet<Forest<T>> ht) {
136 for(Forest<T> f : hp) f.gather(ht);
139 public Forest resolve() { return this; }
140 public void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
141 if (ignore.contains(this)) { ht.add(bogus); return; }
142 for (Forest<T> f : hp) f.expand(ht, ignore, bogus);
144 public boolean contains(Forest f) { return hp.contains(f); }
145 public void merge(Forest p) { if (p!=this) hp.add(p, true); }
146 public boolean ambiguous() {
147 if (hp.size()==0) return false;
148 if (hp.size()==1) return hp.iterator().next().ambiguous();
152 // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
155 if (hp.size()==1) return hp.iterator().next().toInt();
156 return super.toInt();
158 public boolean isTransparent() { return hp.size()==1; }
159 public boolean isHidden() { return hp.size()==0; }
160 public void edges(GraphViz.Node n) {
161 if (hp.size()==1) { hp.iterator().next().edges(n); return; }
162 for(Forest f : hp) f.edges(n);
164 public GraphViz.Node toGraphViz(GraphViz gv) {
165 //if (hp.size()==0) return null;
166 if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
167 if (gv.hasNode(this)) return gv.createNode(this);
168 GraphViz.Node n = gv.createNode(this);
171 for(Forest f : hp) n.edge(f, null);
176 // Statics //////////////////////////////////////////////////////////////////////////////
178 private static Tree[] tree_hint = new Tree[0];
179 private static String[] string_hint = new String[0];
180 private static final Forest[] emptyForestArray = new Forest[0];
182 protected String headToString() { return null; }
183 protected String headToJava() { return "null"; }
184 protected String left() { return "<?"; }
185 protected String right() { return "?>"; }
186 protected boolean ignoreSingleton() { return true; }