major overhaul to institute optimal GSS sharing
[sbp.git] / src / edu / berkeley / sbp / Node.java
1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Pos;
8 import edu.berkeley.sbp.Sequence.Pos;
9 import java.io.*;
10 import java.util.*;
11 import java.lang.reflect.*;
12
13 /** a node in the GSS */
14 final class Node
15     implements Invokable<Pos, Result, Object>,
16                IntegerMappable,
17                GraphViz.ToGraphViz,
18                Iterable<Result> {
19
20     /** which GSS.Phase this Node belongs to */
21     public GSS.Phase phase() { return phase; }
22     public Iterator<Result> iterator() { return results.iterator(); }
23     public Parser.Table.State state() { return state; }
24
25     public int toInt() { return idx; }
26
27     boolean destroyed = false;
28
29     public void check() {
30         if (destroyed) return;
31         boolean dead = true;
32         // - all nodes must have a predecessor
33         // - non-doomed nodes must either:
34         //      - be on the frontier or
35         //      - have a non-doomed node closer to the frontier than themself
36         if (phase.isFrontier()) dead = false;
37         else for(Result r : successors)
38                  if (state.doomed) { if (r.usedByAnyNode()) { dead = false; break; } }
39                  else              { if (r.usedByNonDoomedNode()) { dead = false; break; } }
40         dead |= results.size()==0;
41         if (!dead) return;
42         destroyed = true;
43         if (phase() != null && phase().hash != null)
44             phase().hash.remove(state, predPhase);
45         while(successors.size()>0)
46             for(Result r : successors) {
47                 successors.remove(r);
48                 r.removePred(this);
49                 break;
50             }
51         while(results.size()>0)
52             for(Result r : results) {
53                 results.remove(r);
54                 r.removeSucc(this);
55                 break;
56             }
57         results = null;
58         successors = null;
59     }
60
61     //////////////////////////////////////////////////////////////////////
62
63     private static int node_idx = 0;
64     private final int idx = node_idx++;
65
66     private final GSS.Phase phase;
67     private final GSS.Phase predPhase;
68     private final Parser.Table.State state;
69     private  boolean fromEmptyReduction;
70     private       FastSet<Result> results = new FastSet<Result>();
71     private       FastSet<Result> successors = new FastSet<Result>();
72     //private       HashSet<Result> results = new HashSet<Result>();
73     //private       HashSet<Result> successors = new HashSet<Result>();
74
75     public final void invoke(Pos r, Result only, Object o) {
76         boolean emptyProductions = only==null;
77         if (emptyProductions != (r.numPops()==0)) return;
78         if (r.numPops()!=0)  reduce(r, r.numPops()-1, phase(), only);
79         else {
80             Input.Region region = phase().getLocation().createRegion(phase().getLocation());
81             phase().newNodeFromReduction(r.rewrite(region), r, this);
82         }
83     }
84
85     private void reduce(Pos r, int pos, GSS.Phase target, Result only) {
86         for(Result res : results)
87             if (only == null || res == only)
88                 for(Node pred : res.getPreds())
89                     reduce2(r, pos, target, pred, res.getForest());
90     }
91
92     void reduce2(Pos r, int pos, GSS.Phase target, Node pred, Forest f) {
93         Forest[] holder = r.holder;
94         Forest old = pos >= holder.length ? null : holder[pos];
95         if (pos < holder.length) holder[pos] = f;
96         if (pos>0)  pred.reduce(r, pos-1, target, null);
97         else {
98             Input.Region region = pred.phase().getLocation().createRegion(target.getLocation());
99             new Reduction(pred, r, r.rewrite(region), target);
100         }
101         if (pos < holder.length) holder[pos] = old;
102     }
103
104     Node(GSS.Phase phase, Forest f, Pos reduction, Node pred, State state, boolean fromEmptyReduction) {
105         this(phase, new Result(f, reduction, pred), state, fromEmptyReduction);
106     }
107     Node(GSS.Phase phase, Result pred, State state, boolean fromEmptyReduction) {
108         this.phase = phase;
109         this.state = state;
110         this.fromEmptyReduction = fromEmptyReduction;
111         if (phase.hash.get(state, pred.phase()) != null) throw new Error("severe problem!");
112         this.predPhase = pred.phase();
113         phase.hash.put(state, pred.phase(), this);
114
115         results.add(pred);
116         pred.addSucc(this);
117         if (!fromEmptyReduction)
118             state.invokeReductions(phase().getToken(), this, pred);
119
120         state.invokeEpsilonReductions(phase().token, this);
121     }
122
123     // Add/Remove Successors/Results //////////////////////////////////////////////////////////////////////////////
124
125     public void removeSucc(Result succ)   {
126         successors.remove(succ);
127         check();
128     }
129     public void removeResult(Result result) {
130         results.remove(result);
131         check();
132     }
133     public void addSucc(Result succ)      {
134         successors.add(succ);
135     }
136     public void addResult(Forest f, Pos reduction, Node pred) {
137         for(Result r : results)
138             if (r.predecessorsContains(pred)) {
139                 r.merge(f);
140                 return;
141             }
142         Result result = new Result(f, reduction, pred);
143         results.add(result);
144         result.addSucc(this);
145         if (!this.fromEmptyReduction) state.invokeReductions(phase().getToken(), this, result);
146     }
147
148     // GraphViz //////////////////////////////////////////////////////////////////////////////
149
150     public GraphViz.Node toGraphViz(GraphViz gv) {
151         if (results.size()==0) return null;
152         if (gv.hasNode(this)) return gv.createNode(this);
153         GraphViz.Node n = gv.createNode(this);
154         n.label = "state["+state.toInt()+"]";
155         n.shape = "rectangle";
156         boolean haspreds = false;
157         for(Result r : results) n.edge(r, "");
158         n.color = state.doomed ? "red" : "green";
159         ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
160         return n;
161     }
162     public boolean isTransparent() { return false; }
163     public boolean isHidden() { return false; }
164
165 }