keep track of which reduction created each result (if any)
[sbp.git] / src / edu / berkeley / sbp / Node.java
index 53782f7..740d8a6 100644 (file)
-// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
+// Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
 
 package edu.berkeley.sbp;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.util.*;
 import edu.berkeley.sbp.Parser.Table.*;
-import edu.berkeley.sbp.Sequence.Position;
+import edu.berkeley.sbp.Sequence.Pos;
+import edu.berkeley.sbp.Sequence.Pos;
 import java.io.*;
 import java.util.*;
 import java.lang.reflect.*;
 
 /** a node in the GSS */
-final class Node implements Invokable<Position, Node, Node>, IntegerMappable, GraphViz.ToGraphViz {
-
-    public static int node_idx = 0;
-
-    private final GSS.Phase phase;
-
-    public FastSet<Node> setx = new FastSet<Node>();
-    public FastSet<Node> childs = new FastSet<Node>();
-
-    private boolean allqueued = false;
-
-    /** what state this node is in */
-    public final Parser.Table.State state;
+final class Node
+    implements Invokable<Pos, Result>,
+               IntegerMappable,
+               GraphViz.ToGraphViz,
+               Iterable<Result> {
+
+    /** which GSS.Phase this Node belongs to */
+    public GSS.Phase phase() { return phase; }
+    public Iterator<Result> iterator() { return results.iterator(); }
+    public Parser.Table.State state() { return state; }
 
-    /** which GSS.Phase this Node belongs to (node that Node is also a non-static inner class of GSS.Phase) */
-    public  GSS.Phase phase() { return phase; }
+    public int toInt() { return idx; }
 
-    private HashSet<Forest.Many> resultMap = new HashSet<Forest.Many>();
-    public Iterable<Forest.Many> results() { return resultMap; }
-    public Iterable<Node> parents() { return setx; }
-    public void addParent(Node n, boolean b) {
-        if (n==null) return;
-        setx.add(n, b);
-        n.childs.add(this, true);
-        //else
-        //System.out.println("************ evilstate: " + this);
-    }
-    public boolean merge(Node parent, Forest result) {
-        // FIXME: inefficient!
-        for(Forest.Many f : results()) {
-            if (f.parents.contains(parent) /* UGLY: */ && f.parents.size()==1) {
-                f.merge(result);
-                return true;
+    boolean destroyed = false;
+
+    public void check() {
+        if (destroyed) return;
+        boolean dead = true;
+        // - all nodes must have a predecessor
+        // - non-doomed nodes must either:
+        //      - be on the frontier or
+        //      - have a non-doomed node closer to the frontier than themself
+        if (phase.isFrontier()) dead = false;
+        else for(Result r : successors)
+                 if (state.doomed) { if (r.usedByAnyNode()) { dead = false; break; } }
+                 else              { if (r.usedByNonDoomedNode()) { dead = false; break; } }
+        dead |= results.size()==0;
+        if (!dead) return;
+        destroyed = true;
+        while(successors.size()>0)
+            for(Result r : successors) {
+                successors.remove(r);
+                r.destroy();
+                break;
             }
-        }
-        Forest.Many f = new Forest.Many();
-        f.parents.add(parent);
-        f.merge(result);
-        resultMap.add(f);
-        addParent(parent, true);
-        return false;
-    }
-
-    public void performReductions() {
-        if (allqueued) return;
-        allqueued = true;
-        state.invokeReductions(phase().token(), this, this, null);
+        while(results.size()>0)
+            for(Result r : results) {
+                results.remove(r);
+                r.removeSucc(this);
+                break;
+            }
+        results = null;
+        successors = null;
     }
 
-    public void performReductions(Node n2) {
-        if (!allqueued) phase().reductionQueue.add(this);//performReductions();
-        else            state.invokeReductions(phase().token(), this, this, n2);
-    }
+    //////////////////////////////////////////////////////////////////////
 
-    public Parser.Table.State state() { return state; }
+    private static int node_idx = 0;
+    private final int idx = node_idx++;
 
-    public void performEmptyReductions() { state.invokeReductions(phase().token, this, null, null); }
-    public final void invoke(Position r, Node n, Node n2) {
-        //reductions++;
-        //if (r.pos==1) single_newnode++;
-        //if (r.pos>1) multi_newnode++;
-        if (n==null || n2==null || r.pos==0) {
-            if (r.pos==0) {
-                if (n==null) n = this;
-                else return;
-            }
-            if (n==null) return;
-            Forest[] holder = new Forest[r.pos];
-            if (r.pos==0) n.finish(r, r.zero(n.phase().getLocation().createRegion(n.phase().getLocation())), n.phase());
-            else          n.reduce(r, r.pos-1,  n.phase(), null);
-        } else {
-            if (r.pos<=0) throw new Error("called wrong form of reduce()");
-            int pos = r.pos-1;
-            n.reduce(r, pos, n.phase(), n2);
+    private final GSS.Phase phase;
+    private final Parser.Table.State state;
+    private final boolean fromEmptyReduction;
+    //private       FastSet<Result> results = new FastSet<Result>();
+    private       HashSet<Result> results = new HashSet<Result>();
+    private       HashSet<Result> successors = new HashSet<Result>();
+
+    public final void invoke(Pos r, Result only) {
+        boolean emptyProductions = only==null;
+        if (emptyProductions != (r.numPops()==0)) return;
+        if (r.numPops()!=0)  reduce(r, r.numPops()-1, phase(), only);
+        else {
+            Input.Region region = phase().getLocation().createRegion(phase().getLocation());
+            Result.newResult(r.rewrite(region), this, r, phase());
         }
     }
 
-    public void reduce(Position r, int pos, GSS.Phase target, Node only) {
+    private void reduce(Pos r, int pos, GSS.Phase target, Result only) {
         Forest[] holder = r.holder;
         Forest old = holder[pos];
-                
-        //toplevel_reductions++;
-        HashSet<Forest> rr = new HashSet<Forest>();
-        for(Forest result : results()) {
-            rr.add(result);
-        }
-        //System.out.println(r);
-        for(Forest result : rr) {
-            for(Node child : ((Forest.Many<?>)result).parents) {
-                if (only != null && child!=only) continue;
-                holder[pos] = result;
-                if (pos==0) child.finish(r, r.rewrite(child.phase().getLocation().createRegion(target.getLocation())), target);
-                else        child.reduce(r, pos-1, target, null);
+        if (results==null) return;   // FIXME: this should not happen
+        for(Result res : results)
+            if (only == null || res == only) {
+                Node pred = res.pred();
+                holder[pos] = res.getForest();
+                if (pos>0)  pred.reduce(r, pos-1, target, null);
+                else {
+                    Input.Region region = pred.phase().getLocation().createRegion(target.getLocation());
+                    new Reduction(pred, r, r.rewrite(region), target);
+                }
             }
-        }
         holder[pos] = old;
     }
 
-    public void finish(Position r, Forest result, GSS.Phase target) {
-        Parser.Table.State state0 = (Parser.Table.State)state.gotoSetNonTerminals.get(r.owner());
-        if (result==null) throw new Error();
-        if (state0!=null)
-            target.newNode(this, result, state0, r.pos<=0, r);
-    }
-
-    Node(GSS.Phase phase, Node parent, Forest pending, State state) {
+    Node(GSS.Phase phase, Result pred, State state, boolean fromEmptyReduction) {
         this.phase = phase;
         this.state = state;
-        this.merge(parent, pending);
-        GSS.Phase start = parent==null ? null : parent.phase();
-        if (parent != null) addParent(parent, true);
-        if (phase.hash.get(state, start) != null) throw new Error("severe problem!");
-        phase.hash.put(state, start, this);
+        this.fromEmptyReduction = fromEmptyReduction;
+        if (phase.hash.get(state, pred.phase()) != null) throw new Error("severe problem!");
+        phase.hash.put(state, pred.phase(), this);
+        addResult(pred);
+        state.invokeEpsilonReductions(phase().token, this);
+    }
+
+    // Add/Remove Successors/Results //////////////////////////////////////////////////////////////////////////////
+
+    public void removeSucc(Result succ)   {
+        if (successors==null) return;
+        successors.remove(succ);
+        check();
+    }
+    public void removeResult(Result result) {
+        if (results==null) return;
+        results.remove(result);
+        check();
+    }
+    public void addSucc(Result succ)      {
+        if (successors==null) return;   // FIXME: this should not happen
+        successors.add(succ);
+    }
+    public void addResult(Result r) {
+        if (results.contains(r)) return;
+        results.add(r);
+        r.addSucc(this);
+        if (!fromEmptyReduction) state.invokeReductions(phase().getToken(), this, r);
     }
-    public int toInt() { return idx; }
-    private final int idx = node_idx++;
 
     // GraphViz //////////////////////////////////////////////////////////////////////////////
 
     public GraphViz.Node toGraphViz(GraphViz gv) {
+        if (results.size()==0) return null;
         if (gv.hasNode(this)) return gv.createNode(this);
         GraphViz.Node n = gv.createNode(this);
-        n.label = ""+state.toStringx();
+        n.label = "state["+state.toInt()+"]";
         n.shape = "rectangle";
-        boolean hasparents = false;
-        for(Node parent : parents()) { hasparents = true; n.edge(parent, ""); }
-        for(Forest result : results()) n.edge(result, "");
-        n.color = !hasparents ? "blue" : /*state.evil ? "red" :*/ "green";
+        boolean haspreds = false;
+        for(Result r : results) n.edge(r, "");
+        n.color = state.doomed ? "red" : "green";
         ((GraphViz.Group)phase().toGraphViz(gv)).add(n);
         return n;
     }