X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FResult.java;h=10a9b9434b3b81bbe52ea7c8fefe367ce1a4b18b;hb=61539aaf02d0537fd1df08b5d5bd03189992cf1e;hp=8eccf96eb4d9cfbee7e8b374267cf840ac5818e0;hpb=e84029a8b861075d6d0ed5040f919b2e4da4c98f;p=sbp.git diff --git a/src/edu/berkeley/sbp/Result.java b/src/edu/berkeley/sbp/Result.java index 8eccf96..10a9b94 100644 --- a/src/edu/berkeley/sbp/Result.java +++ b/src/edu/berkeley/sbp/Result.java @@ -9,23 +9,76 @@ import java.io.*; import java.util.*; import java.lang.reflect.*; -class Result { +final class Result implements GraphViz.ToGraphViz { private Forest f; private Node parent; private GSS.Phase phase; private Position reduction; + private HashSet children = new HashSet(); + private boolean destroyed = false; + private int usedByNonDoomedNode = 0; public Position reduction() { return reduction; } public GSS.Phase phase() { return 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 void removeChild(Node child) { + children.remove(child); + usedByNonDoomedNode -= child.state().doomed ? 0 : 1; + } + + public boolean usedByAnyNode() { return children.size() > 0; } + public boolean usedByNonDoomedNode() { return usedByNonDoomedNode > 0; } + + public String toString() { return super.toString()+"->"+parent(); } + + public void check() { if (children.size()==0) destroy(); } + public void destroy() { + if (destroyed) return; + if (parent==null) return; // never destroy the "primordeal" result + destroyed = true; + if (parent() != null) { + parent().removeChild(this); + parent().check(); + } + OUTER: while(true) { + for(Node n : children) { + children.remove(n); + n.removeResult(this); + n.check(); + continue OUTER; + } + break; + } + } public Result(Forest f, Node parent, Position reduction) { this.f = f; this.reduction = reduction; this.parent = parent; + if (parent != null) parent.addChild(this); if (parent != null) phase = parent.phase(); } + // GraphViz ////////////////////////////////////////////////////////////////////////////// + + public GraphViz.Node toGraphViz(GraphViz gv) { + if (gv.hasNode(this)) return gv.createNode(this); + GraphViz.Node n = gv.createNode(this); + n.label = ""+f; + n.shape = "rectangle"; + if (parent()!=null) n.edge(parent, ""); + n.color = "blue"; + if (phase() != null) + ((GraphViz.Group)phase().toGraphViz(gv)).add(n); + return n; + } + public boolean isTransparent() { return false; } + public boolean isHidden() { return false; } + } \ No newline at end of file