remove Node from GSS.hash when destroyed
[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>,
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.destroy();
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 Parser.Table.State state;
68     private final boolean fromEmptyReduction;
69     //private       FastSet<Result> results = new FastSet<Result>();
70     private       HashSet<Result> results = new HashSet<Result>();
71     private       HashSet<Result> successors = new HashSet<Result>();
72
73     public final void invoke(Pos r, Result only) {
74         boolean emptyProductions = only==null;
75         if (emptyProductions != (r.numPops()==0)) return;
76         if (r.numPops()!=0)  reduce(r, r.numPops()-1, phase(), only);
77         else {
78             Input.Region region = phase().getLocation().createRegion(phase().getLocation());
79             Result.newResult(r.rewrite(region), this, r, phase());
80         }
81     }
82
83     private void reduce(Pos r, int pos, GSS.Phase target, Result only) {
84         Forest[] holder = r.holder;
85         Forest old = holder[pos];
86         if (results==null) return;   // FIXME: this should not happen
87         for(Result res : results)
88             if (only == null || res == only) {
89                 Node pred = res.pred();
90                 holder[pos] = res.getForest();
91                 if (pos>0)  pred.reduce(r, pos-1, target, null);
92                 else {
93                     Input.Region region = pred.phase().getLocation().createRegion(target.getLocation());
94                     new Reduction(pred, r, r.rewrite(region), target);
95                 }
96             }
97         holder[pos] = old;
98     }
99
100     Node(GSS.Phase phase, Result pred, State state, boolean fromEmptyReduction) {
101         this.phase = phase;
102         this.state = state;
103         this.fromEmptyReduction = fromEmptyReduction;
104         if (phase.hash.get(state, pred.phase()) != null) throw new Error("severe problem!");
105         phase.hash.put(state, pred.phase(), this);
106         addResult(pred);
107         state.invokeEpsilonReductions(phase().token, this);
108     }
109
110     // Add/Remove Successors/Results //////////////////////////////////////////////////////////////////////////////
111
112     public void removeSucc(Result succ)   {
113         if (successors==null) return;
114         successors.remove(succ);
115         check();
116     }
117     public void removeResult(Result result) {
118         if (results==null) return;
119         results.remove(result);
120         check();
121     }
122     public void addSucc(Result succ)      {
123         if (successors==null) return;   // FIXME: this should not happen
124         successors.add(succ);
125     }
126     public void addResult(Result r) {
127         if (results.contains(r)) return;
128         results.add(r);
129         r.addSucc(this);
130         if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
131     }
132
133     // GraphViz //////////////////////////////////////////////////////////////////////////////
134
135     public GraphViz.Node toGraphViz(GraphViz gv) {
136         if (results.size()==0) return null;
137         if (gv.hasNode(this)) return gv.createNode(this);
138         GraphViz.Node n = gv.createNode(this);
139         n.label = "state["+state.toInt()+"]";
140         n.shape = "rectangle";
141         boolean haspreds = false;
142         for(Result r : results) n.edge(r, "");
143         n.color = state.doomed ? "red" : "green";
144         ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
145         return n;
146     }
147     public boolean isTransparent() { return false; }
148     public boolean isHidden() { return false; }
149
150 }