edd9edccdd105dc098c693038fc346cee253ea77
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / TokenTupleSet.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 TokenTupleSet extends Canonical {
10
11     private HashSet<TokenTuple> tokenTuples;
12
13     public TokenTupleSet() {
14         tokenTuples = new HashSet<TokenTuple>();
15     }
16
17     public TokenTupleSet( TokenTuple tt ) {
18         this();
19         tokenTuples.add( tt );
20     }
21
22     public TokenTupleSet( TokenTupleSet tts ) {
23         tokenTuples = (HashSet<TokenTuple>) tts.tokenTuples.clone(); //COPY?!
24     }
25
26     public TokenTupleSet makeCanonical() {
27         return (TokenTupleSet) Canonical.makeCanonical( this );
28     }
29
30     public Iterator iterator() {
31         return tokenTuples.iterator();
32     }
33
34     /*
35     public TokenTupleSet add( TokenTuple tt ) {
36         TokenTupleSet ttsOut = new TokenTupleSet( tt );
37         return this.union( ttsOut );
38     }
39     */
40
41     public TokenTupleSet union( TokenTupleSet ttsIn ) {
42         TokenTupleSet ttsOut = new TokenTupleSet( this );
43         ttsOut.tokenTuples.addAll( ttsIn.tokenTuples );
44         return ttsOut.makeCanonical();
45     }
46
47     /*
48     public TokenTupleSet unionUpArity( TokenTupleSet ttsIn ) {
49         TokenTupleSet ttsOut = new TokenTupleSet();
50         
51         Iterator itrIn = ttsIn.iterator();
52         while( itrIn.hasNext() ) {
53             TokenTuple ttIn = (TokenTuple) itrIn.next();
54
55             if( this.containsToken( ttIn.getToken() ) ) {       
56                 ttsOut.tokenTuples.add( ttIn.increaseArity() );
57             } else {
58                 ttsOut.tokenTuples.add( ttIn );
59             }
60         }
61
62         Iterator itrThis = this.iterator();
63         while( itrThis.hasNext() ) {
64             TokenTuple ttThis = (TokenTuple) itrThis.next();
65
66             if( !ttsIn.containsToken( ttThis.getToken() ) ) {
67                 ttsOut.tokenTuples.add( ttThis );
68             }
69         }
70         
71         return ttsOut.makeCanonical();
72     }
73     */
74
75     public boolean isEmpty() {
76         return tokenTuples.isEmpty();
77     }
78
79     public boolean containsTuple( TokenTuple tt ) {
80         return tokenTuples.contains( tt );
81     }
82
83     // only needs to be done if newSummary is true?  RIGHT?
84     public TokenTupleSet increaseArity( Integer token ) {
85         TokenTuple tt 
86             = new TokenTuple( token, true, TokenTuple.ARITY_ONE ).makeCanonical();
87         if( tokenTuples.contains( tt ) ) {
88             tokenTuples.remove( tt );
89             tokenTuples.add( 
90               new TokenTuple( token, true, TokenTuple.ARITY_MANY ).makeCanonical()
91                              );
92         }
93         
94         return makeCanonical();
95     }
96
97     public boolean equals( Object o ) {
98         if( !(o instanceof TokenTupleSet) ) {
99             return false;
100         }
101
102         TokenTupleSet tts = (TokenTupleSet) o;
103         return tokenTuples.equals( tts.tokenTuples );
104     }
105
106     public int hashCode() {
107         return tokenTuples.hashCode();
108     }
109
110     /*
111     public boolean equalWithoutArity( TokenTupleSet ttsIn ) {
112         Iterator itrIn = ttsIn.iterator();
113         while( itrIn.hasNext() ) {
114             TokenTuple ttIn = (TokenTuple) itrIn.next();
115
116             if( !this.containsToken( ttIn.getToken() ) )
117             {
118                 return false;
119             }
120         }
121
122         Iterator itrThis = this.iterator();
123         while( itrThis.hasNext() ) {
124             TokenTuple ttThis = (TokenTuple) itrThis.next();
125
126             if( !ttsIn.containsToken( ttThis.getToken() ) )
127             {
128                 return false;
129             }
130         }
131         
132         return true;
133     }
134     */
135
136     // this should be a hash table so we can do this by key
137     public boolean containsToken( Integer token ) {
138         Iterator itr = tokenTuples.iterator();
139         while( itr.hasNext() ) {
140             TokenTuple tt = (TokenTuple) itr.next();
141             if( token.equals( tt.getToken() ) ) {
142                 return true;
143             }
144         }
145         return false;
146     }
147
148     public TokenTupleSet ageTokens( AllocationSite as ) {
149         TokenTupleSet ttsOut = new TokenTupleSet();
150
151         TokenTuple ttSummary = null;
152         boolean foundOldest  = false;
153
154         Iterator itrT = this.iterator();
155         while( itrT.hasNext() ) {
156             TokenTuple tt = (TokenTuple) itrT.next();
157
158             Integer token = tt.getToken();
159             int age = as.getAge( token );
160
161             // summary tokens and tokens not associated with
162             // the site should be left alone
163             if( age == AllocationSite.AGE_notInThisSite ) {
164                 ttsOut.tokenTuples.add( tt );
165
166             } else {
167                 if( age == AllocationSite.AGE_summary ) {
168                     // remember the summary tuple, but don't add it
169                     // we may combine it with the oldest tuple
170                     ttSummary = tt;
171
172                 } else if( age == AllocationSite.AGE_oldest ) {
173                     // found an oldest token, again just remember
174                     // for later
175                     foundOldest = true;
176
177                 } else {
178                     // otherwise, we change this token to the
179                     // next older token
180                     Integer tokenToChangeTo = as.getIthOldest( age + 1 );                  
181                     TokenTuple ttAged       = tt.changeTokenTo( tokenToChangeTo );
182                     ttsOut.tokenTuples.add( ttAged );
183                 }
184
185             }
186         }
187
188         // there are four cases to consider here
189         // 1. we found a summary tuple and no oldest tuple
190         //    Here we just pass the summary unchanged
191         // 2. we found an oldest tuple, no summary
192         //    Make a new, arity-one summary tuple
193         // 3. we found both a summary and an oldest
194         //    Merge them by increasing arity of summary
195         // 4. (not handled) we found neither, do nothing
196         if       ( ttSummary != null && !foundOldest ) {
197             ttsOut.tokenTuples.add( ttSummary );
198
199         } else if( ttSummary == null &&  foundOldest ) {
200             ttsOut.tokenTuples.add( new TokenTuple( as.getSummary(),
201                                         true,
202                                         TokenTuple.ARITY_ONE ).makeCanonical() );          
203         
204         } else if( ttSummary != null &&  foundOldest ) {
205             ttsOut.tokenTuples.add( ttSummary.increaseArity() );
206         }
207
208         return ttsOut.makeCanonical();
209     }
210
211     public String toString() {
212         return tokenTuples.toString();
213     }
214 }