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