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