annotated
[IRC.git] / Robust / src / Analysis / SSJava / SharedLocState.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 SharedLocState {
12
13   // maps location to its current writing var set and flag
14   Hashtable<Location, Pair<Set<Descriptor>, Boolean>> mapLocation2Status;
15
16   public SharedLocState() {
17     mapLocation2Status = new Hashtable<Location, Pair<Set<Descriptor>, Boolean>>();
18   }
19
20   private Pair<Set<Descriptor>, Boolean> getStatus(Location loc) {
21     Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
22     if (pair == null) {
23       pair = new Pair<Set<Descriptor>, Boolean>(new HashSet<Descriptor>(), new Boolean(false));
24       mapLocation2Status.put(loc, pair);
25     }
26     return pair;
27   }
28
29   public void addVar(Location loc, Descriptor d) {
30     getStatus(loc).getFirst().add(d);
31   }
32
33   public void removeVar(Location loc, Descriptor d) {
34     getStatus(loc).getFirst().remove(d);
35   }
36
37   public String toString() {
38     return mapLocation2Status.toString();
39   }
40
41   public Set<Location> getLocationSet() {
42     return mapLocation2Status.keySet();
43   }
44
45   public void merge(SharedLocState inState) {
46     Set<Location> keySet = inState.getLocationSet();
47     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
48       Location inLoc = (Location) iterator.next();
49
50       Pair<Set<Descriptor>, Boolean> inPair = inState.getStatus(inLoc);
51       Pair<Set<Descriptor>, Boolean> currPair = mapLocation2Status.get(inLoc);
52
53       if (currPair == null) {
54         currPair =
55             new Pair<Set<Descriptor>, Boolean>(new HashSet<Descriptor>(), new Boolean(false));
56         mapLocation2Status.put(inLoc, currPair);
57       }
58       mergeSet(currPair.getFirst(), inPair.getFirst());
59     }
60   }
61
62   public void mergeSet(Set<Descriptor> curr, Set<Descriptor> in) {
63     if (curr.isEmpty()) {
64       // Varset has a special initial value which covers all possible
65       // elements
66       // For the first time of intersection, we can take all previous set
67       curr.addAll(in);
68     } else {
69       curr.retainAll(in);
70     }
71   }
72
73   public int hashCode() {
74     return mapLocation2Status.hashCode();
75   }
76
77   public Hashtable<Location, Pair<Set<Descriptor>, Boolean>> getMap() {
78     return mapLocation2Status;
79   }
80
81   public boolean equals(Object o) {
82     if (!(o instanceof SharedLocState)) {
83       return false;
84     }
85     SharedLocState in = (SharedLocState) o;
86     return in.getMap().equals(mapLocation2Status);
87   }
88
89   public Set<Descriptor> getVarSet(Location loc) {
90     return mapLocation2Status.get(loc).getFirst();
91   }
92
93   public void updateFlag(Location loc, boolean b) {
94     Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
95     if (pair.getSecond() != b) {
96       mapLocation2Status.put(loc,
97           new Pair<Set<Descriptor>, Boolean>(pair.getFirst(), Boolean.valueOf(b)));
98     }
99   }
100
101   public boolean getFlag(Location loc) {
102     return mapLocation2Status.get(loc).getSecond().booleanValue();
103   }
104
105   public SharedLocState clone() {
106     SharedLocState newState = new SharedLocState();
107     newState.mapLocation2Status =
108         (Hashtable<Location, Pair<Set<Descriptor>, Boolean>>) mapLocation2Status.clone();
109     return newState;
110   }
111 }