major overhaul to institute optimal GSS sharing
[sbp.git] / src / edu / berkeley / sbp / Reduction.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.Sequence.Pos;
5 import edu.berkeley.sbp.Sequence.Pos;
6
7 final class Reduction implements Comparable<Reduction> {
8
9     private Pos reduction;
10     private GSS.Phase phase;
11     private Forest forest;
12     private Node pred;
13     
14     public Reduction(Node pred, Pos reduction, Forest forest, GSS.Phase target) {
15         this.reduction = reduction;
16         this.forest = forest;
17         this.phase = target;
18         this.pred = pred;
19         target.addReduction(this);
20     }
21
22     public int compareTo(Reduction r) {
23         if (pred.phase()!=null || r.pred.phase()!=null) {
24             if (pred.phase()==null) return 1;
25             if (r.pred.phase()==null) return -1;
26             if (pred.phase().pos < r.pred.phase().pos) return 1;
27             if (pred.phase().pos > r.pred.phase().pos) return -1;
28         }
29         /*
30         int master = Parser.mastercache.comparePositions(reduction(), r.reduction());
31         if (master != 0 && master != signum(reduction.ord() - r.reduction.ord()))
32             throw new Error("master="+master+" and a="+reduction.ord()+" and b="+r.reduction.ord()+"\n"+reduction+"\n"+r.reduction);
33         */
34         return reduction.compareTo(r.reduction);
35     }
36
37     private int signum(int x) {
38         if (x==0) return 0;
39         if (x<0) return -1;
40         return 1;
41     }
42
43     public void perform() {
44         if (reduction==null) return;
45         phase.newNodeFromReduction(forest, reduction, pred);
46     }
47     public GSS.Phase predPhase() { return pred.phase(); }
48     public Pos reduction() { return reduction; }
49     public GSS.Phase targetPhase() { return phase; }
50     public String toString() { return (pred.phase()==null ? 0 : pred.phase().pos) + ":"+reduction; }
51 }