KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > scalar > ToppedSet


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.jimple.toolkits.scalar;
28
29 import soot.*;
30 import soot.util.*;
31 import soot.toolkits.scalar.*;
32 import java.util.*;
33
34 /** Represents information for flow analysis, adding a top element to a lattice.
35  * A FlowSet is an element of a lattice; this lattice might be described by a FlowUniverse.
36  * If add, remove, size, isEmpty, toList and contains are implemented, the lattice must be the powerset of some set.
37  *
38  */

39 public class ToppedSet extends AbstractFlowSet
40 {
41     FlowSet underlyingSet;
42     boolean isTop;
43
44     public void setTop(boolean top) { isTop = top; }
45     public boolean isTop() { return isTop; }
46
47     public ToppedSet(FlowSet under)
48     {
49         underlyingSet = under;
50     }
51
52     public Object JavaDoc clone()
53     {
54         ToppedSet newSet = new ToppedSet((FlowSet)underlyingSet.clone());
55         newSet.setTop(isTop());
56         return newSet;
57     }
58
59     public void copy(FlowSet d)
60     {
61         ToppedSet dest = (ToppedSet)d;
62         if (!isTop())
63         {
64             underlyingSet.copy(dest.underlyingSet);
65             dest.setTop(false);
66             return;
67         }
68
69         dest.setTop(true);
70     }
71
72     public Object JavaDoc emptySet()
73     {
74         return new ToppedSet((FlowSet)underlyingSet.emptySet());
75     }
76
77     public void clear()
78     {
79         isTop = false;
80         underlyingSet.clear();
81     }
82
83     public void union(FlowSet o, FlowSet d)
84     {
85       if (o instanceof ToppedSet &&
86           d instanceof ToppedSet) {
87         ToppedSet other = (ToppedSet)o;
88         ToppedSet dest = (ToppedSet)d;
89
90         if (isTop())
91         {
92             copy(dest);
93             return;
94         }
95
96         if (other.isTop())
97             other.copy(dest);
98         else
99         {
100             underlyingSet.union(other.underlyingSet,
101                                 dest.underlyingSet);
102             dest.setTop(false);
103         }
104       } else
105         super.union(o, d);
106     }
107
108     public void intersection(FlowSet o, FlowSet d)
109     {
110         if (isTop())
111         {
112             o.copy(d);
113             return;
114         }
115
116         ToppedSet other = (ToppedSet)o, dest = (ToppedSet)d;
117
118         if (other.isTop())
119         {
120             copy(dest);
121             return;
122         }
123         else
124         {
125             underlyingSet.intersection(other.underlyingSet,
126                                        dest.underlyingSet);
127             dest.setTop(false);
128         }
129     }
130
131     public void difference(FlowSet o, FlowSet d)
132     {
133       ToppedSet other = (ToppedSet)o, dest = (ToppedSet)d;
134
135       if (isTop()) {
136         if (other.isTop())
137           dest.clear();
138         else if (other.underlyingSet instanceof BoundedFlowSet)
139           ((BoundedFlowSet)other.underlyingSet).complement(dest);
140         else
141           throw new RuntimeException JavaDoc("can't take difference!");
142       } else {
143         if (other.isTop())
144           dest.clear();
145         else
146           underlyingSet.difference(other.underlyingSet, dest.underlyingSet);
147       }
148         /* not very helpful...
149         if (isTop() && other.isTop())
150           dest.clear();
151         else if (isTop())
152         if (!(other.underlyingSet instanceof BoundedFlowSet) &&
153             othe
154         if (other.underlyingSet instanceof BoundedFlowSet)
155         {
156             FlowSet temp = (FlowSet)other.underlyingSet.clone();
157             ((BoundedFlowSet)other.underlyingSet).complement(temp);
158             temp.union(temp, dest.underlyingSet);
159             return;
160         }
161         throw new RuntimeException("can't take difference!");
162         */

163     }
164
165     public boolean isEmpty()
166     {
167         if (isTop()) return false;
168         return underlyingSet.isEmpty();
169     }
170
171     public int size()
172     {
173         if (isTop()) throw new UnsupportedOperationException JavaDoc();
174         return underlyingSet.size();
175     }
176
177     public void add(Object JavaDoc obj)
178     {
179         if (isTop()) return;
180         underlyingSet.add(obj);
181     }
182
183     public void remove(Object JavaDoc obj)
184     {
185         if (isTop()) return;
186         underlyingSet.remove(obj);
187     }
188
189     public boolean contains(Object JavaDoc obj)
190     {
191         if (isTop()) return true;
192         return underlyingSet.contains(obj);
193     }
194
195     public List toList()
196     {
197         if (isTop()) throw new UnsupportedOperationException JavaDoc();
198         return underlyingSet.toList();
199     }
200
201     public boolean equals(Object JavaDoc o)
202     {
203         if (!(o instanceof ToppedSet))
204             return false;
205
206         ToppedSet other = (ToppedSet)o;
207         if (other.isTop() != isTop())
208             return false;
209         return underlyingSet.equals(other.underlyingSet);
210     }
211
212     public String JavaDoc toString()
213     {
214         if (isTop()) return "{TOP}"; else return underlyingSet.toString();
215     }
216 }
217
218
Popular Tags