big update--bringing implementation of new analysis into focus
[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
35   public static ReachState factory() {
36     ReachState out = new ReachState();
37     out = (ReachState) Canonical.makeCanonical( out );
38     return out;
39   }
40
41   public static ReachState factory( ReachTuple rt ) {
42     assert rt != null;
43     assert rt.isCanonical();
44     ReachState out = new ReachState();    
45     out.reachTuples.add( rt );
46     out = (ReachState) Canonical.makeCanonical( out );
47     return out;
48   }
49
50   private ReachState() {
51     reachTuples = new HashSet<ReachTuple>();
52   }
53
54
55   public Iterator iterator() {
56     return reachTuples.iterator();
57   }
58
59   public boolean isEmpty() {
60     return reachTuples.isEmpty();
61   }
62
63   public boolean isSubset( ReachState rsIn ) {
64     assert rsIn != null;
65     return rsIn.reachTuples.containsAll( this.reachTuples );
66   }
67
68   public boolean containsTuple( ReachTuple rt ) {
69     assert rt != null;
70     return reachTuples.contains( rt );
71   }
72
73   // this should be a hash table so we can do this by key
74   public ReachTuple containsHrnID( Integer hrnID ) {
75     assert hrnID != null;
76
77     Iterator<ReachTuple> rtItr = reachTuples.iterator();
78     while( rtItr.hasNext() ) {
79       ReachTuple rt = rtItr.next();
80       if( hrnID.equals( rt.getHrnID() ) ) {
81         return rt;
82       }
83     }
84     
85     return null;
86   }
87
88
89
90   public boolean equals( Object o ) {
91     if( o == null ) {
92       return false;
93     }
94
95     if( !(o instanceof ReachState) ) {
96       return false;
97     }
98
99     ReachState rs = (ReachState) o;
100     assert this.isCanonical();
101     assert rs.isCanonical();
102     return this == rs;
103   }
104
105
106   public int hashCodeSpecific() {
107     return reachTuples.hashCode();
108   }
109
110
111   public String toString() {
112     return reachTuples.toString();
113   }
114 }