5e59a4e241668499d8172a97febf32e61230e406
[sbp.git] / src / edu / berkeley / sbp / Reduction.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.Sequence.Position;
5
6 final class Reduction implements Comparable<Reduction> {
7
8     private Position reduction;
9     private GSS.Phase phase;
10     private Forest forest;
11     private Node parent;
12     
13     public Reduction(Node parent, Position reduction, Forest forest, GSS.Phase target) {
14         this.reduction = reduction;
15         this.forest = forest;
16         this.phase = target;
17         this.parent = parent;
18         target.addReduction(this);
19     }
20
21     public int compareTo(Reduction r) {
22         if (parent.phase()!=null || r.parent.phase()!=null) {
23             if (parent.phase()==null) return 1;
24             if (r.parent.phase()==null) return -1;
25             if (parent.phase().pos < r.parent.phase().pos) return 1;
26             if (parent.phase().pos > r.parent.phase().pos) return -1;
27         }
28         /*
29         int master = Parser.mastercache.comparePositions(reduction(), r.reduction());
30         if (master != 0 && master != signum(reduction.ord - r.reduction.ord))
31             throw new Error("master="+master+" and a="+reduction.ord+" and b="+r.reduction.ord+"\n"+reduction+"\n"+r.reduction);
32         */
33         return reduction.ord - r.reduction.ord;
34     }
35
36     private int signum(int x) {
37         if (x==0) return 0;
38         if (x<0) return -1;
39         return 1;
40     }
41
42     public void perform() { new Result(forest, parent, reduction, phase); }
43     public GSS.Phase parentPhase() { return parent.phase(); }
44     public Position reduction() { return reduction; }
45     public GSS.Phase targetPhase() { return phase; }
46     public String toString() { return (parent.phase()==null ? 0 : parent.phase().pos) + ":"+reduction.ord+":"+ reduction+" "+reduction.owner(); }
47 }