checkpoint
[sbp.git] / src / edu / berkeley / sbp / Forest.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.Sequence.Position;
4 import edu.berkeley.sbp.util.*;
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /**
10  *   An efficient representation of a collection of trees (Tomita's
11  *   shared packed parse forest).
12  */
13 public abstract class Forest<T> implements GraphViz.ToGraphViz {
14
15     /** assume that this forest contains exactly one tree and return it; otherwise throw an exception */
16     public abstract Tree<T> expand1() throws Ambiguous;
17
18     /** expand this forest into a set of trees */
19     public void expand(HashSet<Tree<T>> ht) { expand(ht, new HashSet<Forest<T>>(), null); }
20
21     /** create a new forest */
22     public static <T> Forest<T> create(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
23         return new One<T>(loc, head, children, lift);
24     }
25
26     // Package-Private //////////////////////////////////////////////////////////////////////////////
27
28     abstract void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus);
29     abstract void gather(HashSet<Forest<T>> ignore);
30     abstract void edges(GraphViz.Node n);
31     boolean ambiguous() { return false; }
32
33
34     // One //////////////////////////////////////////////////////////////////////////////
35
36     /** A "single" forest with a head and child subforests */    
37     private static class One<T> extends Forest<T> {
38
39         private final Input.Region      location;
40         private final T                 head;
41         private final Forest<T>[]       children;
42
43         /** if true, the last child's children are considered children of this node */
44         private final boolean           lift;
45
46         private One(Input.Region loc, T head, Forest<T>[] children, boolean lift) {
47             this.location = loc;
48             this.head = head;
49             this.children = children==null ? emptyForestArray : new Forest[children.length];
50             if (children != null) System.arraycopy(children, 0, this.children, 0, children.length);
51             if (children != null) for(int i=0; i<children.length; i++) if (children[i]==null) throw new Error(i+"");
52             this.lift = lift;
53         }
54
55         public Tree<T> expand1() throws Ambiguous {
56             Tree<T>[] ret = new Tree[children.length];
57             for(int i=0; i<children.length; i++) ret[i] = children[i].expand1();
58             return new Tree<T>(location, head, ret, lift);
59         }
60
61         void gather(HashSet<Forest<T>> hf) {
62             hf.add(this);
63             for(Forest<T> f : children) f.gather(hf);
64         }
65         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[children.length], ht, ignore, bogus);
68         }
69         private void expand(final int i, Tree<T>[] ta, HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
70             if (i==children.length) {
71                 ht.add(new Tree<T>(location, head, ta, lift));
72             } else {
73                 HashSet<Tree<T>> ht2 = new HashSet<Tree<T>>();
74                 children[i].expand(ht2, ignore, bogus);
75                 for(Tree<T> tc : ht2) {
76                     ta[i] = tc;
77                     expand(i+1, ta, ht, ignore, bogus);
78                     ta[i] = null;
79                 }
80             }
81         }
82
83         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
84
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();
91             n.directed = true;
92             edges(n);
93             return n;
94         }
95         boolean edges = false; // FIXME ??
96         public void edges(GraphViz.Node n) {
97             if (edges) return;
98             edges = true;
99             for(int i=0; i<children.length; i++) {
100                 if (i==children.length-1 && lift && !children[i].ambiguous()) {
101                     children[i].edges(n);
102                 } else {
103                     n.edge(children[i], null);
104                 }
105             }
106         }
107
108         protected String  headToString()         { return head==null?null:head.toString(); }
109         protected String  headToJava()           { return "null"; }
110         protected String  left()                 { return "{"; }
111         protected String  right()                { return "}"; }
112         protected boolean ignoreSingleton()      { return false; }
113     }
114
115
116     // Many //////////////////////////////////////////////////////////////////////////////
117
118     /** An "ambiguity node"; this is immutable once it has been "looked at" */
119     static class Many<T> extends Forest<T> {
120
121         HashSet<GSS.Phase.Node> parents = new HashSet<GSS.Phase.Node>();
122         private FastSet<Forest<T>> hp = new FastSet<Forest<T>>();
123         private boolean touched = false;
124
125         public Many() { }
126
127         public Tree<T> expand1() throws Ambiguous {
128             touched();
129             if (hp.size() > 1) {
130                 HashSet<Forest<T>> hf0 = new HashSet<Forest<T>>();
131                 Iterator<Forest<T>> ih = hp.iterator();
132                 ih.next().gather(hf0);
133                 for(Forest<T> f : hp) {
134                     HashSet<Forest<T>> hf1 = new HashSet<Forest<T>>();
135                     f.gather(hf1);
136                     hf0.retainAll(hf1);
137                 }
138                 HashSet<Tree<T>> ht = new HashSet<Tree<T>>();
139                 expand(ht, hf0, new Tree(null, "*"));
140                 throw new Ambiguous((Forest<?>)this,
141                                     (HashSet<Tree<?>>)(Object)ht);
142             }
143             return hp.iterator().next().expand1();
144         }
145         
146         void gather(HashSet<Forest<T>> ht) {
147             touched();
148             ht.add(this);
149             for(Forest<T> f : hp) f.gather(ht);
150         }
151
152         private void touched() {
153             if (touched) return;
154             touched = true;
155             /*
156             FastSet<Forest<T>> f2 = new FastSet<Forest<T>>();
157             for(Forest f : hp)
158                 if (f instanceof Forest.One) f2.add(f);
159                 else for(Forest ff : ((Forest.Many<T>)f))
160                     f2.add(ff);
161             hp = f2;
162             */
163         }
164         public boolean contains(Forest f) {
165             touched();
166             return hp.contains(f);
167         }
168         public void merge(Forest p) { 
169             if (touched) throw new RuntimeException("attempt to merge() on a Forest.Many that has already been examined");
170             if (p==this) throw new RuntimeException("attempt to merge() a Forest.Many to itself!");
171             hp.add(p, true);
172         }
173         boolean ambiguous() {
174             touched();
175             if (hp.size()==0) return false;
176             if (hp.size()==1) return hp.iterator().next().ambiguous();
177             return true;
178         }
179
180         void expand(HashSet<Tree<T>> ht, HashSet<Forest<T>> ignore, Tree<T> bogus) {
181             touched();
182             if (ignore.contains(this)) { ht.add(bogus); return; }
183             for (Forest<T> f : hp) f.expand(ht, ignore, bogus);
184         }
185
186
187         // GraphViz, ToInt //////////////////////////////////////////////////////////////////////////////
188
189         public boolean isTransparent() { return hp.size()==1; }
190         public boolean isHidden() { return hp.size()==0; }
191         public void edges(GraphViz.Node n) {
192             if (hp.size()==1) { hp.iterator().next().edges(n); return; }
193             for(Forest f : hp) f.edges(n);
194         }
195         public GraphViz.Node toGraphViz(GraphViz gv) {
196             if (hp.size()==1) return hp.iterator().next().toGraphViz(gv);
197             if (gv.hasNode(this)) return gv.createNode(this);
198             GraphViz.Node n = gv.createNode(this);
199             n.label = "?";
200             n.color = "red";
201             for(Forest f : hp) n.edge(f, null);
202             return n;
203         }
204     }
205
206     // Statics //////////////////////////////////////////////////////////////////////////////
207
208     private static Tree[] tree_hint = new Tree[0];
209     private static String[] string_hint = new String[0];
210     private static final Forest[] emptyForestArray = new Forest[0];
211
212     protected String  headToString()    { return null; }
213     protected String  headToJava()      { return "null"; }
214     protected String  left()            { return "<?"; }
215     protected String  right()           { return "?>"; }
216     protected boolean ignoreSingleton() { return true; }
217 }