keep track of which reduction created each result (if any)
[sbp.git] / src / edu / berkeley / sbp / Result.java
index 95baeba..97f467f 100644 (file)
@@ -1,61 +1,64 @@
-// 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.util.*;
-import edu.berkeley.sbp.Sequence.Position;
+import edu.berkeley.sbp.Sequence.Pos;
+import edu.berkeley.sbp.Sequence.Pos;
 import java.util.*;
 
 final class Result implements GraphViz.ToGraphViz {
 
     private Forest f;
-    private Node parent;
-    private HashSet<Node> children = new HashSet<Node>();
+    private Node pred;
+    private HashSet<Node> successors = new HashSet<Node>();
     private boolean destroyed = false;
     private int usedByNonDoomedNode = 0;
+    private Pos reduction;
 
-    public GSS.Phase phase() { return parent==null ? null : parent.phase(); }
+    public GSS.Phase phase() { return pred==null ? null : pred.phase(); }
     public Forest getForest() { return f; }
-    public Node parent() { return parent; }
-    public void addChild(Node child) {
-        children.add(child);
-        usedByNonDoomedNode += child.state().doomed ? 0 : 1;
+    public Node pred() { return pred; }
+    public void addSucc(Node succ) {
+        successors.add(succ);
+        usedByNonDoomedNode += succ.state().doomed ? 0 : 1;
     }
-    public void removeChild(Node child) {
-        if (!children.contains(child)) return;
-        children.remove(child);
-        usedByNonDoomedNode -= child.state().doomed ? 0 : 1;
+    public void removeSucc(Node succ) {
+        if (!successors.contains(succ)) return;
+        successors.remove(succ);
+        usedByNonDoomedNode -= succ.state().doomed ? 0 : 1;
         check();
     }
 
-    public boolean usedByAnyNode() { return children.size() > 0; }
+    public boolean usedByAnyNode() { return successors.size() > 0; }
     public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; }
 
-    public String toString() { return super.toString()+"->"+parent(); }
+    public String toString() { return super.toString()+"->"+pred(); }
 
-    public void check() { if (children.size()==0) destroy(); }
+    public void check() { if (successors.size()==0) destroy(); }
     public void destroy() {
         if (destroyed) return;
-        if (parent==null) return;  // never destroy the "primordeal" result
+        if (pred==null) return;  // never destroy the "primordeal" result
         destroyed = true;
-        parent.removeChild(this);
-        while(children.size() > 0)
-            for(Node n : children) {
-                removeChild(n);
+        pred.removeSucc(this);
+        while(successors.size() > 0)
+            for(Node n : successors) {
+                removeSucc(n);
                 n.removeResult(this);
                 break;
             }
     }
 
-    public Result(Forest f, Node parent, Position reduction) {
-        this(f, parent, reduction, null);
+    public static void newResult(Forest f, Node pred, Pos reduction, GSS.Phase target) {
+        Result r = new Result(f, pred, reduction);
+        if (reduction == null) return;
+        Parser.Table.State state0 = (Parser.Table.State)pred.state().gotoSetNonTerminals.get(reduction);
+        target.newNodeFromReduction(r, state0, reduction);
     }
-    public Result(Forest f, Node parent, Position reduction, GSS.Phase target) {
+    public Result(Forest f, Node pred, Pos reduction) {
         this.f = f;
-        this.parent = parent;
-        if (parent != null) parent.addChild(this);
-        if (reduction == null) return;
-        Parser.Table.State state0 = (Parser.Table.State)parent.state().gotoSetNonTerminals.get(reduction.owner());
-        target.newNodeFromReduction(this, state0, reduction);
+        this.pred = pred;
+        this.reduction = reduction;
+        if (pred != null) pred.addSucc(this);
     }
 
     // GraphViz //////////////////////////////////////////////////////////////////////////////
@@ -65,7 +68,7 @@ final class Result implements GraphViz.ToGraphViz {
         GraphViz.Node n = gv.createNode(this);
         n.label = ""+f;
         n.shape = "rectangle";
-        if (parent()!=null) n.edge(parent, "");
+        if (pred()!=null) n.edge(pred, "");
         n.color = "blue";
         if (phase() != null)
             ((GraphViz.Group)phase().toGraphViz(gv)).add(n);