Added some functionality to reachability classes that is apparently
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / ReachabilitySet.java
1 package Analysis.OwnershipAnalysis;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8
9 public class ReachabilitySet extends Canonical {
10
11     private HashSet<TokenTupleSet> possibleReachabilities;
12
13     public ReachabilitySet() {
14         possibleReachabilities = new HashSet<TokenTupleSet>();
15         //TokenTupleSet ttsEmpty = new TokenTupleSet().makeCanonical();
16         //possibleReachabilities.add( ttsEmpty );       
17     }
18
19     public ReachabilitySet( TokenTupleSet tts ) {
20         this();
21         assert tts != null;
22         possibleReachabilities.add( tts );
23     }
24
25     public ReachabilitySet( TokenTuple tt ) {
26         this( new TokenTupleSet( tt ).makeCanonical() );
27     }
28
29     public ReachabilitySet( HashSet<TokenTupleSet> possibleReachabilities ) {
30         this.possibleReachabilities = possibleReachabilities;
31     }
32
33     public ReachabilitySet( ReachabilitySet rs ) {
34         assert rs != null;
35         possibleReachabilities = (HashSet<TokenTupleSet>) rs.possibleReachabilities.clone(); // again, DEEP COPY?!
36     }
37
38     public ReachabilitySet makeCanonical() {
39         return (ReachabilitySet) Canonical.makeCanonical( this );
40     }
41
42     public boolean contains( TokenTupleSet tts ) {
43         assert tts != null;
44         return possibleReachabilities.contains( tts );
45     }
46
47     public ReachabilitySet add( TokenTupleSet tts ) {
48         ReachabilitySet rsOut = new ReachabilitySet( tts );
49         return this.union( rsOut );
50     }
51
52     public ReachabilitySet increaseArity( Integer token ) {
53         assert token != null;
54
55         HashSet<TokenTupleSet> possibleReachabilitiesNew = new HashSet<TokenTupleSet>();
56
57         Iterator itr = iterator();
58         while( itr.hasNext() ) {
59             TokenTupleSet tts = (TokenTupleSet) itr.next();
60             possibleReachabilitiesNew.add( tts.increaseArity( token ) );
61         }
62
63         return new ReachabilitySet( possibleReachabilitiesNew ).makeCanonical(); 
64     }
65
66     public Iterator iterator() {
67         return possibleReachabilities.iterator();
68     }
69
70     public ReachabilitySet union( ReachabilitySet rsIn ) {
71         assert rsIn != null;
72
73         ReachabilitySet rsOut = new ReachabilitySet( this );
74         rsOut.possibleReachabilities.addAll( rsIn.possibleReachabilities );
75         return rsOut.makeCanonical();
76     }
77
78     public ReachabilitySet union( TokenTupleSet ttsIn ) {
79         assert ttsIn != null;
80
81         ReachabilitySet rsOut = new ReachabilitySet( this );
82         rsOut.possibleReachabilities.add( ttsIn );
83         return rsOut.makeCanonical();
84     }
85
86     public ReachabilitySet intersection( ReachabilitySet rsIn ) {
87         assert rsIn != null;
88
89         ReachabilitySet rsOut = new ReachabilitySet();
90
91         Iterator i = this.iterator();
92         while( i.hasNext() ) {
93             TokenTupleSet tts = (TokenTupleSet) i.next();
94             if( rsIn.possibleReachabilities.contains( tts ) ) {
95                 rsOut.possibleReachabilities.add( tts );
96             }
97         }
98
99         return rsOut.makeCanonical();
100     }
101     
102     /*
103     public ReachabilitySet unionUpArity( ReachabilitySet rsIn ) {
104         assert rsIn != null;
105
106         ReachabilitySet rsOut = new ReachabilitySet();
107         Iterator itrIn;
108         Iterator itrThis;       
109
110         itrIn = rsIn.iterator();
111         while( itrIn.hasNext() ) {
112             TokenTupleSet ttsIn = (TokenTupleSet) itrIn.next();
113
114             boolean foundEqual = false;
115
116             itrThis = this.iterator();
117             while( itrThis.hasNext() ) {
118                 TokenTupleSet ttsThis = (TokenTupleSet) itrThis.next();
119
120                 if( ttsIn.equalWithoutArity( ttsThis ) ) {
121                     rsOut.possibleReachabilities.add( ttsIn.unionUpArity( ttsThis ) );
122                     foundEqual = true;
123                     continue;
124                 }
125             }
126
127             if( !foundEqual ) {
128                 rsOut.possibleReachabilities.add( ttsIn );
129             }
130         }
131
132         itrThis = this.iterator();
133         while( itrThis.hasNext() ) {
134             TokenTupleSet ttsThis = (TokenTupleSet) itrThis.next();
135
136             boolean foundEqual = false;
137
138             itrIn = rsIn.iterator();
139             while( itrIn.hasNext() ) {
140                 TokenTupleSet ttsIn = (TokenTupleSet) itrIn.next();
141
142                 if( ttsThis.equalWithoutArity( ttsIn ) ) {
143                     foundEqual = true;
144                     continue;
145                 }
146             }
147
148             if( !foundEqual ) {
149                 rsOut.possibleReachabilities.add( ttsThis );
150             }
151         }
152
153         return rsOut.makeCanonical();
154     }  
155     */
156
157     public ChangeTupleSet unionUpArityToChangeSet( ReachabilitySet rsIn ) {
158         assert rsIn != null;
159
160         ChangeTupleSet ctsOut = new ChangeTupleSet();
161
162         Iterator itrO = this.iterator();
163         while( itrO.hasNext() ) {
164             TokenTupleSet o = (TokenTupleSet) itrO.next();
165
166             Iterator itrR = rsIn.iterator();
167             while( itrR.hasNext() ) {
168                 TokenTupleSet r = (TokenTupleSet) itrR.next();
169
170                 TokenTupleSet theUnion = new TokenTupleSet();
171
172                 Iterator itrRelement = r.iterator();
173                 while( itrRelement.hasNext() ) {
174                     TokenTuple e = (TokenTuple) itrRelement.next();
175
176                     if( o.containsToken( e.getToken() ) ) {
177                         theUnion = theUnion.union( new TokenTupleSet( e.increaseArity() ) ).makeCanonical();
178                     } else {
179                         theUnion = theUnion.union( new TokenTupleSet( e                 ) ).makeCanonical();
180                     }
181                 }
182
183                 Iterator itrOelement = o.iterator();
184                 while( itrOelement.hasNext() ) {
185                     TokenTuple e = (TokenTuple) itrOelement.next();
186
187                     if( !theUnion.containsToken( e.getToken() ) ) {
188                         theUnion = theUnion.union( new TokenTupleSet( e ) ).makeCanonical();
189                     }
190                 }
191
192                 if( !theUnion.isEmpty() ) {
193                     ctsOut = ctsOut.union( 
194                       new ChangeTupleSet( new ChangeTuple( o, theUnion ) )
195                                           );
196                 }
197             }
198         }
199
200         return ctsOut.makeCanonical();
201     }
202
203
204     public boolean equals( Object o ) {
205         if( !(o instanceof ReachabilitySet) ) {
206             return false;
207         }
208
209         ReachabilitySet rs = (ReachabilitySet) o;
210         return possibleReachabilities.equals( rs.possibleReachabilities );
211     }
212
213     public int hashCode() {
214         return possibleReachabilities.hashCode();
215     }
216
217
218     public String toStringEscapeNewline() {
219         String s = "[";
220
221         Iterator i = this.iterator();
222         while( i.hasNext() ) {
223             s += i.next();
224             if( i.hasNext() ) {
225                 s += "\\n";
226             }
227         }
228
229         s += "]";
230         return s;       
231     }
232
233     public String toString() {
234         String s = "[";
235
236         Iterator i = this.iterator();
237         while( i.hasNext() ) {
238             if( possibleReachabilities.size() > 1 ) {
239                 s += "\n";
240             }
241             s += " "+i.next();
242         }
243         
244         if( possibleReachabilities.size() > 1 ) {
245             s += "\n";
246         }
247
248         s += " ]";
249         return s;       
250     }
251 }