changes on the definite clearance for shared locations analysis
[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   public SharedStatus() {
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
35     Set<Descriptor> dSet = getStatus(loc).getFirst();
36     boolean isClared = getStatus(loc).getSecond().booleanValue();
37     dSet.remove(d);
38
39 //    if (dSet.isEmpty() && !isClared) {
40       // if status has empty descriptor set and 'false' status, remove it!
41 //      mapLocation2Status.remove(loc);
42 //    }
43
44   }
45
46   public String toString() {
47     return mapLocation2Status.toString();
48   }
49
50   public Set<Location> getLocationSet() {
51     return mapLocation2Status.keySet();
52   }
53
54   public void merge(SharedStatus inState) {
55     Set<Location> keySet = inState.getLocationSet();
56     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
57       Location inLoc = (Location) iterator.next();
58
59       Pair<Set<Descriptor>, Boolean> inPair = inState.getStatus(inLoc);
60       Pair<Set<Descriptor>, Boolean> currPair = mapLocation2Status.get(inLoc);
61
62       if (currPair == null) {
63         currPair =
64             new Pair<Set<Descriptor>, Boolean>(new HashSet<Descriptor>(), new Boolean(false));
65         mapLocation2Status.put(inLoc, currPair);
66       }
67       mergeSet(currPair.getFirst(), inPair.getFirst());
68     }
69   }
70
71   public void mergeSet(Set<Descriptor> curr, Set<Descriptor> in) {
72     if (curr.isEmpty()) {
73       // Varset has a special initial value which covers all possible
74       // elements
75       // For the first time of intersection, we can take all previous set
76       curr.addAll(in);
77     } else {
78       curr.retainAll(in);
79     }
80   }
81
82   public int hashCode() {
83     return mapLocation2Status.hashCode();
84   }
85
86   public Hashtable<Location, Pair<Set<Descriptor>, Boolean>> getMap() {
87     return mapLocation2Status;
88   }
89
90   public boolean equals(Object o) {
91     if (!(o instanceof SharedStatus)) {
92       return false;
93     }
94     SharedStatus in = (SharedStatus) o;
95     return in.getMap().equals(mapLocation2Status);
96   }
97
98   public Set<Descriptor> getVarSet(Location loc) {
99     return mapLocation2Status.get(loc).getFirst();
100   }
101
102   public void updateFlag(Location loc, boolean b) {
103     Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
104     if (pair.getSecond() != b) {
105       mapLocation2Status.put(loc,
106           new Pair<Set<Descriptor>, Boolean>(pair.getFirst(), Boolean.valueOf(b)));
107     }
108   }
109
110   public void updateFlag(boolean b) {
111     Set<Location> locKeySet = mapLocation2Status.keySet();
112     for (Iterator iterator = locKeySet.iterator(); iterator.hasNext();) {
113       Location loc = (Location) iterator.next();
114       Pair<Set<Descriptor>, Boolean> pair = mapLocation2Status.get(loc);
115       mapLocation2Status.put(loc,
116           new Pair<Set<Descriptor>, Boolean>(pair.getFirst(), Boolean.valueOf(b)));
117     }
118
119   }
120
121   public boolean getFlag(Location loc) {
122     return mapLocation2Status.get(loc).getSecond().booleanValue();
123   }
124
125   public SharedStatus clone() {
126     SharedStatus newState = new SharedStatus();
127     newState.mapLocation2Status =
128         (Hashtable<Location, Pair<Set<Descriptor>, Boolean>>) mapLocation2Status.clone();
129     return newState;
130   }
131 }