changes.
[IRC.git] / Robust / src / Analysis / SSJava / SharedStatus.java
1 package Analysis.SSJava;
2
3 import java.util.HashSet;
4 import java.util.Hashtable;
5 import java.util.Iterator;
6 import java.util.Set;
7
8 import IR.Descriptor;
9 import Util.Pair;
10
11 public class SharedStatus {
12
13   // maps location to its current writing var set and flag
14   Hashtable<Location, Pair<Set<Descriptor>, Boolean>> mapLocation2Status;
15
16   // set of location having write effects
17   public HashSet<Location> writeLocSet;
18
19   public SharedStatus() {
20     mapLocation2Status = new Hashtable<Location, Pair<Set<Descriptor>, Boolean>>();
21     writeLocSet = new HashSet<Location>();
22   }
23
24   private Pair<Set<Descriptor>, Boolean> getStatus(Location loc) {
25     Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
26     if (pair == null) {
27       pair = new Pair<Set<Descriptor>, Boolean>(new HashSet<Descriptor>(), new Boolean(false));
28       mapLocation2Status.put(loc, pair);
29     }
30     return pair;
31   }
32
33   public void addVar(Location loc, Descriptor d) {
34     getStatus(loc).getFirst().add(d);
35   }
36
37   public void setWriteEffect(Location loc) {
38     writeLocSet.add(loc);
39   }
40
41   public void removeVar(Location loc, Descriptor d) {
42
43     Set<Descriptor> dSet = getStatus(loc).getFirst();
44     dSet.remove(d);
45
46   }
47
48   public String toString() {
49     return mapLocation2Status.toString();
50   }
51
52   public Set<Location> getLocationSet() {
53     return mapLocation2Status.keySet();
54   }
55
56   public void merge(SharedStatus inState) {
57     Set<Location> keySet = inState.getLocationSet();
58     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
59       Location inLoc = (Location) iterator.next();
60
61       Pair<Set<Descriptor>, Boolean> inPair = inState.getStatus(inLoc);
62       Pair<Set<Descriptor>, Boolean> currPair = mapLocation2Status.get(inLoc);
63
64       if (currPair == null) {
65         currPair =
66             new Pair<Set<Descriptor>, Boolean>(new HashSet<Descriptor>(), new Boolean(false));
67         mapLocation2Status.put(inLoc, currPair);
68       }
69       mergeSet(currPair.getFirst(), inPair.getFirst());
70     }
71
72     writeLocSet.addAll(inState.getWriteLocSet());
73   }
74
75   public boolean haveWriteEffect(Location loc) {
76     return writeLocSet.contains(loc);
77   }
78
79   public Set<Location> getWriteLocSet() {
80     return writeLocSet;
81   }
82
83   public void mergeSet(Set<Descriptor> curr, Set<Descriptor> in) {
84     if (curr.isEmpty()) {
85       // Varset has a special initial value which covers all possible
86       // elements
87       // For the first time of intersection, we can take all previous set
88       curr.addAll(in);
89     } else {
90       curr.retainAll(in);
91     }
92   }
93
94   public int hashCode() {
95     return mapLocation2Status.hashCode();
96   }
97
98   public Hashtable<Location, Pair<Set<Descriptor>, Boolean>> getMap() {
99     return mapLocation2Status;
100   }
101
102   public boolean equals(Object o) {
103     if (!(o instanceof SharedStatus)) {
104       return false;
105     }
106     SharedStatus in = (SharedStatus) o;
107     return in.getMap().equals(mapLocation2Status);
108   }
109
110   public Set<Descriptor> getVarSet(Location loc) {
111     return mapLocation2Status.get(loc).getFirst();
112   }
113
114   public void updateFlag(Location loc, boolean b) {
115     Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
116     if (pair.getSecond() != b) {
117       mapLocation2Status.put(loc,
118           new Pair<Set<Descriptor>, Boolean>(pair.getFirst(), Boolean.valueOf(b)));
119     }
120   }
121
122   public void updateFlag(boolean b) {
123     Set<Location> locKeySet = mapLocation2Status.keySet();
124     for (Iterator iterator = locKeySet.iterator(); iterator.hasNext();) {
125       Location loc = (Location) iterator.next();
126       Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
127       mapLocation2Status.put(loc,
128           new Pair<Set<Descriptor>, Boolean>(pair.getFirst(), Boolean.valueOf(b)));
129     }
130
131   }
132
133   public boolean getFlag(Location loc) {
134     return mapLocation2Status.get(loc).getSecond().booleanValue();
135   }
136
137   public SharedStatus clone() {
138     SharedStatus newState = new SharedStatus();
139     newState.mapLocation2Status =
140         (Hashtable<Location, Pair<Set<Descriptor>, Boolean>>) mapLocation2Status.clone();
141     newState.writeLocSet=(HashSet<Location>) writeLocSet.clone();
142     return newState;
143   }
144 }