842098d10d09d1fdd401090a3a0c8c731c4e8ea1
[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     }
16
17     public ReachabilitySet( TokenTupleSet tts ) {
18         assert tts != null;
19         possibleReachabilities = new HashSet<TokenTupleSet>();
20         possibleReachabilities.add( tts );
21     }
22
23     public ReachabilitySet( TokenTuple tt ) {
24         this( new TokenTupleSet( tt ) );
25     }
26
27     public ReachabilitySet( ReachabilitySet rs ) {
28         assert rs != null;
29         possibleReachabilities = (HashSet<TokenTupleSet>) rs.possibleReachabilities.clone(); // again, DEEP COPY?!
30     }
31
32     public ReachabilitySet makeCanonical() {
33         return (ReachabilitySet) Canonical.makeCanonical( this );
34     }
35
36     public boolean contains( TokenTupleSet tts ) {
37         assert tts != null;
38         return possibleReachabilities.contains( tts );
39     }
40
41     public Iterator iterator() {
42         return possibleReachabilities.iterator();
43     }
44
45     public ReachabilitySet union( ReachabilitySet rsIn ) {
46         assert rsIn != null;
47
48         ReachabilitySet rsOut = new ReachabilitySet( this );
49         rsOut.possibleReachabilities.addAll( rsIn.possibleReachabilities );
50         return rsOut.makeCanonical();
51     }
52
53     public ReachabilitySet union( TokenTupleSet ttsIn ) {
54         assert ttsIn != null;
55
56         ReachabilitySet rsOut = new ReachabilitySet( this );
57         rsOut.possibleReachabilities.add( ttsIn );
58         return rsOut.makeCanonical();
59     }
60
61     public ReachabilitySet intersection( ReachabilitySet rsIn ) {
62         assert rsIn != null;
63
64         ReachabilitySet rsOut = new ReachabilitySet();
65
66         Iterator i = this.iterator();
67         while( i.hasNext() ) {
68             TokenTupleSet tts = (TokenTupleSet) i.next();
69             if( rsIn.possibleReachabilities.contains( tts ) ) {
70                 rsOut.possibleReachabilities.add( tts );
71             }
72         }
73
74         return rsOut.makeCanonical();
75     }
76
77     public ChangeTupleSet unionUpArity( ReachabilitySet rsIn ) {
78         assert rsIn != null;
79
80         ChangeTupleSet ctsOut = new ChangeTupleSet();
81
82         Iterator itrO = this.iterator();
83         while( itrO.hasNext() ) {
84             TokenTupleSet o = (TokenTupleSet) itrO.next();
85
86             Iterator itrR = rsIn.iterator();
87             while( itrR.hasNext() ) {
88                 TokenTupleSet r = (TokenTupleSet) itrR.next();
89
90                 TokenTupleSet theUnion = new TokenTupleSet();
91
92                 Iterator itrRelement = r.iterator();
93                 while( itrRelement.hasNext() ) {
94                     TokenTuple e = (TokenTuple) itrRelement.next();
95
96                     if( o.containsToken( e.getToken() ) ) {
97                         theUnion = theUnion.union( new TokenTupleSet( e.increaseArity() ) ).makeCanonical();
98                     } else {
99                         theUnion = theUnion.union( new TokenTupleSet( e                 ) ).makeCanonical();
100                     }
101                 }
102
103                 Iterator itrOelement = o.iterator();
104                 while( itrOelement.hasNext() ) {
105                     TokenTuple e = (TokenTuple) itrOelement.next();
106
107                     if( !theUnion.containsToken( e.getToken() ) ) {
108                         theUnion = theUnion.union( new TokenTupleSet( e ) ).makeCanonical();
109                     }
110                 }
111
112                 if( !theUnion.isEmpty() ) {
113                     ctsOut = ctsOut.union( 
114                       new ChangeTupleSet( new ChangeTuple( o, theUnion ) )
115                                           );
116                 }
117             }
118         }
119
120         return ctsOut.makeCanonical();
121     }
122
123
124     public boolean equals( Object o ) {
125         if( !(o instanceof ReachabilitySet) ) {
126             return false;
127         }
128
129         ReachabilitySet rs = (ReachabilitySet) o;
130         return possibleReachabilities.equals( rs.possibleReachabilities );
131     }
132
133     public int hashCode() {
134         return possibleReachabilities.hashCode();
135     }
136
137
138     public String toStringEscapeNewline() {
139         String s = "[";
140
141         Iterator i = this.iterator();
142         while( i.hasNext() ) {
143             s += "\\n  "+i.next();
144         }
145
146         s += "]";
147
148         return s;       
149     }
150
151     public String toString() {
152         String s = "[";
153
154         Iterator i = this.iterator();
155         while( i.hasNext() ) {
156             s += "\n  "+i.next();
157         }
158
159         s += "\n]";
160
161         return s;       
162     }
163 }