add ImportResolver mechanism
[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.Position;
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 parent;
13     
14     public Reduction(Node parent, Pos reduction, Forest forest, GSS.Phase target) {
15         this.reduction = reduction;
16         this.forest = forest;
17         this.phase = target;
18         this.parent = parent;
19         target.addReduction(this);
20     }
21
22     public int compareTo(Reduction r) {
23         if (parent.phase()!=null || r.parent.phase()!=null) {
24             if (parent.phase()==null) return 1;
25             if (r.parent.phase()==null) return -1;
26             if (parent.phase().pos < r.parent.phase().pos) return 1;
27             if (parent.phase().pos > r.parent.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() { new Result(forest, parent, reduction, phase); }
44     public GSS.Phase parentPhase() { return parent.phase(); }
45     public Pos reduction() { return reduction; }
46     public GSS.Phase targetPhase() { return phase; }
47     public String toString() { return (parent.phase()==null ? 0 : parent.phase().pos) + ":"+reduction; }
48 }