X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2FReduction.java;h=5eb3f63197de7d91f644d1625e1fbf8443d469c2;hp=684fbf8b51424ff6492a353277ff4d2b40d1d7de;hb=33d7b8fa4974bad96108c11e5548b354cf10ecb8;hpb=0620c2d97d6df986d74dbe13160afb1435096431 diff --git a/src/edu/berkeley/sbp/Reduction.java b/src/edu/berkeley/sbp/Reduction.java index 684fbf8..5eb3f63 100644 --- a/src/edu/berkeley/sbp/Reduction.java +++ b/src/edu/berkeley/sbp/Reduction.java @@ -1,90 +1,48 @@ -// 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 java.io.*; -import java.util.*; -import java.lang.reflect.*; +import edu.berkeley.sbp.Sequence.Pos; +import edu.berkeley.sbp.Sequence.Pos; final class Reduction implements Comparable { - public Position position; - public GSS.Phase phase; - public Forest result; - public Node node; - boolean done = false; + private Pos reduction; + private GSS.Phase phase; + private Forest forest; + private Node pred; - public Reduction(Node node, Position position, Forest result, GSS.Phase target) { - this.position = position; - this.result = result; + public Reduction(Node pred, Pos reduction, Forest forest, GSS.Phase target) { + this.reduction = reduction; + this.forest = forest; this.phase = target; - this.node = node; - target.reductionQueue.add(this); - } - - public void perform() { - if (done) return; - done = true; - node.finish(position, result, phase); + this.pred = pred; + target.addReduction(this); } public int compareTo(Reduction r) { - int ret = compareTo0(r); - if (ret != 0) return -1 * ret; - return (position.ord - r.position.ord); - } - - private static boolean isRightNullable(Walk.Cache c, Position p) { - if (p.isLast()) return true; - if (!c.possiblyEpsilon(p.element())) return false; - return isRightNullable(c, p.next()); - } - - public static boolean canKill(Walk.Cache cache, Position mep, Position himp) { - if (!isRightNullable(cache, mep)) return false; - if (!isRightNullable(cache, himp)) return false; - Sequence me = mep.owner(); - Sequence him = himp.owner(); - Boolean b = me.canKill.get(him); - if (b!=null) return b; - for(Sequence killer : him.hates()) { - HashSet eq2 = new Walk.EquivalentTo(killer, cache).walk(); - if (eq2.contains(me)) { me.canKill.put(him, true); return true; } + if (pred.phase()!=null || r.pred.phase()!=null) { + if (pred.phase()==null) return 1; + if (r.pred.phase()==null) return -1; + if (pred.phase().pos < r.pred.phase().pos) return 1; + if (pred.phase().pos > r.pred.phase().pos) return -1; } - me.canKill.put(him, false); - return false; + /* + int master = Parser.mastercache.comparePositions(reduction(), r.reduction()); + if (master != 0 && master != signum(reduction.ord() - r.reduction.ord())) + throw new Error("master="+master+" and a="+reduction.ord()+" and b="+r.reduction.ord()+"\n"+reduction+"\n"+r.reduction); + */ + return reduction.compareTo(r.reduction); } - public static final int LESS_THAN = -1; - public static final int EQUAL_TO = 0; - public static final int GREATER_THAN = 1; - - public int compareTo0(Reduction n) { - if (targetPhase()==null && n.targetPhase()==null) return EQUAL_TO; - if (targetPhase()==null) return LESS_THAN; - if (n.targetPhase()==null) return GREATER_THAN; - if (targetPhase().pos < n.targetPhase().pos) return LESS_THAN; - if (targetPhase().pos > n.targetPhase().pos) return GREATER_THAN; - return 0; + private int signum(int x) { + if (x==0) return 0; + if (x<0) return -1; + return 1; } - public int pos() { return targetPhase()==null ? 0 : targetPhase().pos; } - public GSS.Phase targetPhase() { return node.phase(); } - public static boolean canNeed(Walk.Cache cache, Position mep, Position himp) { - if (!isRightNullable(cache, mep)) return false; - if (!isRightNullable(cache, himp)) return false; - Sequence me = mep.owner(); - Sequence him = himp.owner(); - Boolean b = me.canNeed.get(him); - if (b!=null) return b; - for(Sequence needer : him.needs()) { - HashSet eq2 = new Walk.EquivalentTo(needer, cache).walk(); - if (eq2.contains(me)) { me.canNeed.put(him, true); return true; } - } - me.canNeed.put(him, false); - return false; - } + public void perform() { new Result(forest, pred, reduction, phase); } + public GSS.Phase predPhase() { return pred.phase(); } + public Pos reduction() { return reduction; } + public GSS.Phase targetPhase() { return phase; } + public String toString() { return (pred.phase()==null ? 0 : pred.phase().pos) + ":"+reduction; } }