1 package Analysis.OwnershipAnalysis;
9 public class ReachabilitySet extends Canonical {
11 private HashSet<TokenTupleSet> possibleReachabilities;
13 public ReachabilitySet() {
14 possibleReachabilities = new HashSet<TokenTupleSet>();
17 public ReachabilitySet( TokenTupleSet tts ) {
20 possibleReachabilities.add( tts );
23 public ReachabilitySet( TokenTuple tt ) {
24 this( new TokenTupleSet( tt ).makeCanonical() );
27 public ReachabilitySet( HashSet<TokenTupleSet> possibleReachabilities ) {
28 this.possibleReachabilities = possibleReachabilities;
31 public ReachabilitySet( ReachabilitySet rs ) {
33 possibleReachabilities = (HashSet<TokenTupleSet>) rs.possibleReachabilities.clone(); // again, DEEP COPY?!
36 public ReachabilitySet makeCanonical() {
37 return (ReachabilitySet) Canonical.makeCanonical( this );
40 public boolean contains( TokenTupleSet tts ) {
42 return possibleReachabilities.contains( tts );
45 public ReachabilitySet add( TokenTupleSet tts ) {
46 ReachabilitySet rsOut = new ReachabilitySet( tts );
47 return this.union( rsOut );
50 public ReachabilitySet increaseArity( Integer token ) {
53 HashSet<TokenTupleSet> possibleReachabilitiesNew = new HashSet<TokenTupleSet>();
55 Iterator itr = iterator();
56 while( itr.hasNext() ) {
57 TokenTupleSet tts = (TokenTupleSet) itr.next();
58 possibleReachabilitiesNew.add( tts.increaseArity( token ) );
61 return new ReachabilitySet( possibleReachabilitiesNew ).makeCanonical();
64 public Iterator iterator() {
65 return possibleReachabilities.iterator();
68 public ReachabilitySet union( ReachabilitySet rsIn ) {
71 ReachabilitySet rsOut = new ReachabilitySet( this );
72 rsOut.possibleReachabilities.addAll( rsIn.possibleReachabilities );
73 return rsOut.makeCanonical();
76 public ReachabilitySet union( TokenTupleSet ttsIn ) {
79 ReachabilitySet rsOut = new ReachabilitySet( this );
80 rsOut.possibleReachabilities.add( ttsIn );
81 return rsOut.makeCanonical();
84 public ReachabilitySet intersection( ReachabilitySet rsIn ) {
87 ReachabilitySet rsOut = new ReachabilitySet();
89 Iterator i = this.iterator();
90 while( i.hasNext() ) {
91 TokenTupleSet tts = (TokenTupleSet) i.next();
92 if( rsIn.possibleReachabilities.contains( tts ) ) {
93 rsOut.possibleReachabilities.add( tts );
97 return rsOut.makeCanonical();
101 public ReachabilitySet unionUpArity( ReachabilitySet rsIn ) {
104 ReachabilitySet rsOut = new ReachabilitySet();
108 itrIn = rsIn.iterator();
109 while( itrIn.hasNext() ) {
110 TokenTupleSet ttsIn = (TokenTupleSet) itrIn.next();
112 boolean foundEqual = false;
114 itrThis = this.iterator();
115 while( itrThis.hasNext() ) {
116 TokenTupleSet ttsThis = (TokenTupleSet) itrThis.next();
118 if( ttsIn.equalWithoutArity( ttsThis ) ) {
119 rsOut.possibleReachabilities.add( ttsIn.unionUpArity( ttsThis ) );
126 rsOut.possibleReachabilities.add( ttsIn );
130 itrThis = this.iterator();
131 while( itrThis.hasNext() ) {
132 TokenTupleSet ttsThis = (TokenTupleSet) itrThis.next();
134 boolean foundEqual = false;
136 itrIn = rsIn.iterator();
137 while( itrIn.hasNext() ) {
138 TokenTupleSet ttsIn = (TokenTupleSet) itrIn.next();
140 if( ttsThis.equalWithoutArity( ttsIn ) ) {
147 rsOut.possibleReachabilities.add( ttsThis );
151 return rsOut.makeCanonical();
155 public ChangeTupleSet unionUpArityToChangeSet( ReachabilitySet rsIn ) {
158 ChangeTupleSet ctsOut = new ChangeTupleSet();
160 Iterator itrO = this.iterator();
161 while( itrO.hasNext() ) {
162 TokenTupleSet o = (TokenTupleSet) itrO.next();
164 Iterator itrR = rsIn.iterator();
165 while( itrR.hasNext() ) {
166 TokenTupleSet r = (TokenTupleSet) itrR.next();
168 TokenTupleSet theUnion = new TokenTupleSet();
170 Iterator itrRelement = r.iterator();
171 while( itrRelement.hasNext() ) {
172 TokenTuple e = (TokenTuple) itrRelement.next();
174 if( o.containsToken( e.getToken() ) ) {
175 theUnion = theUnion.union( new TokenTupleSet( e.increaseArity() ) ).makeCanonical();
177 theUnion = theUnion.union( new TokenTupleSet( e ) ).makeCanonical();
181 Iterator itrOelement = o.iterator();
182 while( itrOelement.hasNext() ) {
183 TokenTuple e = (TokenTuple) itrOelement.next();
185 if( !theUnion.containsToken( e.getToken() ) ) {
186 theUnion = theUnion.union( new TokenTupleSet( e ) ).makeCanonical();
190 if( !theUnion.isEmpty() ) {
191 ctsOut = ctsOut.union(
192 new ChangeTupleSet( new ChangeTuple( o, theUnion ) )
198 return ctsOut.makeCanonical();
202 public ReachabilitySet ageTokens( AllocationSite as ) {
205 ReachabilitySet rsOut = new ReachabilitySet();
207 Iterator itrS = this.iterator();
208 while( itrS.hasNext() ) {
209 TokenTupleSet tts = (TokenTupleSet) itrS.next();
210 rsOut.possibleReachabilities.add( tts.ageTokens( as ) );
213 return rsOut.makeCanonical();
217 public ReachabilitySet pruneBy( ReachabilitySet rsIn ) {
220 ReachabilitySet rsOut = new ReachabilitySet();
222 Iterator itrB = this.iterator();
223 while( itrB.hasNext() ) {
224 TokenTupleSet ttsB = (TokenTupleSet) itrB.next();
226 boolean subsetExists = false;
228 Iterator itrA = rsIn.iterator();
229 while( itrA.hasNext() && !subsetExists ) {
230 TokenTupleSet ttsA = (TokenTupleSet) itrA.next();
232 if( ttsA.isSubset( ttsB ) ) {
234 rsOut.possibleReachabilities.add( ttsB );
239 return rsOut.makeCanonical();
243 public boolean equals( Object o ) {
248 if( !(o instanceof ReachabilitySet) ) {
252 ReachabilitySet rs = (ReachabilitySet) o;
253 return possibleReachabilities.equals( rs.possibleReachabilities );
256 public int hashCode() {
257 return possibleReachabilities.hashCode();
261 public String toStringEscapeNewline() {
264 Iterator i = this.iterator();
265 while( i.hasNext() ) {
276 public String toString() {
279 Iterator i = this.iterator();
280 while( i.hasNext() ) {