slight code improvements
[IRC.git] / Robust / src / Analysis / Disjoint / ReachState.java
1 package Analysis.Disjoint;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8 ///////////////////////////////////////////
9 //  IMPORTANT
10 //  This class is an immutable Canonical, so
11 //
12 //  0) construct them with a factory pattern
13 //  to ensure only canonical versions escape
14 //
15 //  1) any operation that modifies a Canonical
16 //  is a static method in the Canonical class
17 //
18 //  2) operations that just read this object
19 //  should be defined here
20 //
21 //  3) every Canonical subclass hashCode should
22 //  throw an error if the hash ever changes
23 //
24 ///////////////////////////////////////////
25
26 // a reach state is a set of reach tuples
27 // such that any heap region ID in a tuple
28 // appears at most once in the state
29
30 public class ReachState extends Canonical {
31
32   protected HashSet<ReachTuple> reachTuples;
33
34   // existance predicates must be true in a caller
35   // context for this state to transfer from this
36   // callee to that context
37   protected ExistPredSet preds;
38
39
40   public static ReachState factory() {
41     ReachState out = new ReachState();
42     out = (ReachState) Canonical.makeCanonical(out);
43     return out;
44   }
45
46   public static ReachState factory(ReachTuple... rts) {
47     ReachState out = new ReachState();
48     for( ReachTuple rt : rts ) {
49       assert rt != null;
50       assert rt.isCanonical();
51       out.reachTuples.add(rt);
52     }
53     out = (ReachState) Canonical.makeCanonical(out);
54     return out;
55   }
56
57   public static ReachState factory(HashSet<ReachTuple> reachTuples,
58                                    ExistPredSet preds
59                                    ) {
60     assert reachTuples != null;
61     assert preds != null;
62     assert preds.isCanonical();
63     ReachState out = new ReachState();
64     out.reachTuples.addAll(reachTuples);
65     out.preds = preds;
66     out = (ReachState) Canonical.makeCanonical(out);
67     return out;
68   }
69
70   protected ReachState() {
71     reachTuples = new HashSet<ReachTuple>();
72     preds       = ExistPredSet.factory();
73   }
74
75
76   public Iterator<ReachTuple> iterator() {
77     return reachTuples.iterator();
78   }
79
80   public boolean isEmpty() {
81     return reachTuples.isEmpty();
82   }
83
84   public boolean isSubset(ReachState rsIn) {
85     assert rsIn != null;
86     return rsIn.reachTuples.containsAll(this.reachTuples);
87   }
88
89   public boolean containsTuple(ReachTuple rt) {
90     assert rt != null;
91     return reachTuples.contains(rt);
92   }
93
94   // this should be a hash table so we can do this by key
95   public ReachTuple containsHrnID(Integer hrnID,
96                                   boolean isOutOfContext) {
97     assert hrnID != null;
98
99     Iterator<ReachTuple> rtItr = reachTuples.iterator();
100     while( rtItr.hasNext() ) {
101       ReachTuple rt = rtItr.next();
102       if( hrnID.equals(rt.getHrnID() ) &&
103           isOutOfContext == rt.isOutOfContext()
104           ) {
105         return rt;
106       }
107     }
108
109     return null;
110   }
111
112   public ExistPredSet getPreds() {
113     return preds;
114   }
115
116
117   public boolean equalsSpecific(Object o) {
118     if( o == null ) {
119       return false;
120     }
121
122     if( !(o instanceof ReachState) ) {
123       return false;
124     }
125
126     ReachState rs = (ReachState) o;
127     return
128       reachTuples.equals(rs.reachTuples) &&
129       preds.equals(rs.preds);
130   }
131
132
133   public int hashCodeSpecific() {
134     return
135       reachTuples.hashCode() ^
136       preds.hashCode();
137   }
138
139
140   public boolean equalsIgnorePreds(Object o) {
141     if( o == null ) {
142       return false;
143     }
144
145     if( !(o instanceof ReachState) ) {
146       return false;
147     }
148
149     ReachState rs = (ReachState) o;
150     return
151       reachTuples.equals(rs.reachTuples);
152   }
153
154
155
156   public String toString() {
157     return reachTuples.toString();
158     //return reachTuples+":"+preds;
159   }
160
161   public String toStringPreds() {
162     return reachTuples+":"+preds;
163   }
164
165   public String toString( boolean hidePreds ) {
166     if( hidePreds ) {
167       return toString();
168     }
169     return toStringPreds();
170   }
171 }