1 package Analysis.Disjoint;
8 ///////////////////////////////////////////
10 // This class is an immutable Canonical, so
12 // 0) construct them with a factory pattern
13 // to ensure only canonical versions escape
15 // 1) any operation that modifies a Canonical
16 // is a static method in the Canonical class
18 // 2) operations that just read this object
19 // should be defined here
21 // 3) every Canonical subclass hashCode should
22 // throw an error if the hash ever changes
24 ///////////////////////////////////////////
26 // a reach set is a set of reach states
28 public class ReachSet extends Canonical {
30 protected HashSet<ReachState> reachStates;
33 public static ReachSet factory() {
34 ReachSet out = new ReachSet();
35 out = (ReachSet) Canonical.makeCanonical( out );
39 public static ReachSet factory( ReachState state ) {
41 assert state.isCanonical();
42 ReachSet out = new ReachSet();
43 out.reachStates.add( state );
44 out = (ReachSet) Canonical.makeCanonical( out );
48 protected ReachSet() {
49 reachStates = new HashSet<ReachState>();
53 public Iterator<ReachState> iterator() {
54 return reachStates.iterator();
58 return reachStates.size();
61 public boolean isEmpty() {
62 return reachStates.isEmpty();
65 // this should be a hash table so we can do this by key
66 public ReachState containsIgnorePreds( ReachState state ) {
69 Iterator<ReachState> stateItr = this.reachStates.iterator();
70 while( stateItr.hasNext() ) {
71 ReachState stateThis = stateItr.next();
72 if( stateThis.equalsIgnorePreds( state ) ) {
81 public boolean containsWithZeroes( ReachState state ) {
84 if( reachStates.contains( state ) ) {
88 Iterator<ReachState> itr = iterator();
89 while( itr.hasNext() ) {
90 ReachState stateThis = itr.next();
91 if( stateThis.containsWithZeroes( state ) ) {
100 public boolean containsSuperSet( ReachState state ) {
101 return containsSuperSet( state, false );
104 public boolean containsStrictSuperSet( ReachState state ) {
105 return containsSuperSet( state, true );
108 public boolean containsSuperSet( ReachState state,
110 assert state != null;
112 if( !strict && reachStates.contains( state ) ) {
116 Iterator<ReachState> itr = iterator();
117 while( itr.hasNext() ) {
118 ReachState stateThis = itr.next();
120 if( !state.equals( stateThis ) &&
121 state.isSubset( stateThis ) ) {
125 if( state.isSubset( stateThis ) ) {
135 public boolean containsTuple( ReachTuple rt ) {
136 Iterator<ReachState> itr = iterator();
137 while( itr.hasNext() ) {
138 ReachState state = itr.next();
139 if( state.containsTuple( rt ) ) {
146 public ReachSet getStatesWithBoth( ReachTuple rt1,
149 ReachSet out = new ReachSet();
151 Iterator<ReachState> itr = iterator();
152 while( itr.hasNext() ) {
153 ReachState state = itr.next();
154 if( state.containsTuple( rt1 ) &&
155 state.containsTuple( rt2 ) ) {
156 out.reachStates.add( state );
160 out = (ReachSet) Canonical.makeCanonical( out );
164 // used to assert each state in the set is
166 public boolean containsNoDuplicates() {
167 Vector<ReachState> v = new Vector( reachStates );
168 for( int i = 0; i < v.size(); ++i ) {
169 ReachState s1 = v.get( i );
170 for( int j = i+1; j < v.size(); ++j ) {
171 ReachState s2 = v.get( j );
172 if( s1.equals( s2 ) ) {
173 assert s1.isCanonical();
174 assert s2.isCanonical();
183 public boolean equals( Object o ) {
188 if( !(o instanceof ReachSet) ) {
192 ReachSet rs = (ReachSet) o;
193 return reachStates.equals( rs.reachStates );
197 public int hashCodeSpecific() {
198 return reachStates.hashCode();
202 public String toStringEscNewline( boolean hideSubsetReachability ) {
205 Iterator<ReachState> i = this.iterator();
206 while( i.hasNext() ) {
207 ReachState state = i.next();
209 // skip this if there is a superset already
210 if( hideSubsetReachability &&
211 containsStrictSuperSet( state ) ) {
226 public String toString() {
227 return toString( false );
230 public String toString( boolean hideSubsetReachability ) {
233 Iterator<ReachState> i = this.iterator();
234 while( i.hasNext() ) {
235 ReachState state = i.next();
237 // skip this if there is a superset already
238 if( hideSubsetReachability &&
239 containsStrictSuperSet( state ) ) {