special union of reachability sets works correctly now
[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 {
10
11     private HashSet<TokenTupleSet> possibleReachabilities;
12
13     public ReachabilitySet() {
14         possibleReachabilities = new HashSet<TokenTupleSet>();
15     }
16
17     public ReachabilitySet( TokenTupleSet tts ) {
18         possibleReachabilities = new HashSet<TokenTupleSet>();
19         possibleReachabilities.add( tts );
20     }
21
22     public ReachabilitySet( ReachabilitySet rs ) {
23         possibleReachabilities = (HashSet<TokenTupleSet>) rs.possibleReachabilities.clone(); // again, DEEP COPY?!
24     }
25
26     public Iterator iterator() {
27         return possibleReachabilities.iterator();
28     }
29
30     public ReachabilitySet union( ReachabilitySet rsIn ) {
31         ReachabilitySet rsOut = new ReachabilitySet( this );
32         rsOut.possibleReachabilities.addAll( rsIn.possibleReachabilities );
33         return rsOut;
34     }
35
36     public ReachabilitySet intersection( ReachabilitySet rsIn ) {
37         ReachabilitySet rsOut = new ReachabilitySet();
38
39         Iterator i = this.iterator();
40         while( i.hasNext() ) {
41             TokenTupleSet tts = (TokenTupleSet) i.next();
42             if( rsIn.possibleReachabilities.contains( tts ) ) {
43                 rsOut.possibleReachabilities.add( tts );
44             }
45         }
46
47         return rsOut;
48     }
49
50     public ChangeTupleSet unionUpArity( ReachabilitySet rsIn ) {
51         ChangeTupleSet ctsOut = new ChangeTupleSet();
52
53         Iterator itrO = this.iterator();
54         while( itrO.hasNext() ) {
55             TokenTupleSet o = (TokenTupleSet) itrO.next();
56
57             Iterator itrR = rsIn.iterator();
58             while( itrR.hasNext() ) {
59                 TokenTupleSet r = (TokenTupleSet) itrR.next();
60
61                 TokenTupleSet theUnion = new TokenTupleSet();
62
63                 Iterator itrRelement = r.iterator();
64                 while( itrRelement.hasNext() ) {
65                     TokenTuple e = (TokenTuple) itrRelement.next();
66
67                     if( o.containsToken( e.getToken() ) ) {
68                         theUnion = theUnion.union( new TokenTupleSet( e.increaseArity() ) );
69                     } else {
70                         theUnion = theUnion.union( new TokenTupleSet( e ) );
71                     }
72                 }
73
74                 Iterator itrOelement = o.iterator();
75                 while( itrOelement.hasNext() ) {
76                     TokenTuple e = (TokenTuple) itrOelement.next();
77
78                     if( !theUnion.containsToken( e.getToken() ) ) {
79                         theUnion = theUnion.union( new TokenTupleSet( e ) );
80                     }
81                 }
82
83                 if( !theUnion.isEmpty() ) {
84                     ctsOut = ctsOut.union( 
85                                new ChangeTupleSet( 
86                                  new ChangeTuple( o, theUnion )
87                                                   )
88                                           );
89                 }
90             }
91         }
92
93         return ctsOut;
94     }
95
96     public String toString() {
97         String s = "[";
98
99         Iterator i = this.iterator();
100         while( i.hasNext() ) {
101             s += "\n  "+i.next();
102         }
103
104         s += "\n]";
105
106         return s;       
107     }
108 }