a start on reachability, not fully functioning yet
[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   protected 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                                    boolean isOutOfContext ) {
76     assert hrnID != null;
77
78     Iterator<ReachTuple> rtItr = reachTuples.iterator();
79     while( rtItr.hasNext() ) {
80       ReachTuple rt = rtItr.next();
81       if( hrnID.equals( rt.getHrnID() ) &&
82           isOutOfContext == rt.isOutOfContext()
83           ) {
84         return rt;
85       }
86     }
87     
88     return null;
89   }
90
91
92
93   public boolean equals( Object o ) {
94     if( o == null ) {
95       return false;
96     }
97
98     if( !(o instanceof ReachState) ) {
99       return false;
100     }
101
102     ReachState rs = (ReachState) o;
103     return reachTuples.equals( rs.reachTuples );
104   }
105
106
107   public int hashCodeSpecific() {
108     return reachTuples.hashCode();
109   }
110
111
112   public String toString() {
113     return reachTuples.toString();
114   }
115 }