KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > scalar > AbstractFlowSet


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Florian Loitsch
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.toolkits.scalar;
28
29 import soot.*;
30 import soot.util.*;
31 import java.util.*;
32
33 /**
34  * provides functional code for most of the methods. Subclasses are invited to
35  * provide a more efficient version. Most often this will be done in the
36  * following way:<br>
37  * <pre>
38  * public void yyy(FlowSet dest) {
39  * if (dest instanceof xxx) {
40  * blahblah;
41  * } else
42  * super.yyy(dest)
43  * }
44  * </pre>
45  */

46 public abstract class AbstractFlowSet implements FlowSet {
47   public abstract Object JavaDoc clone();
48
49   /**
50    * implemented, but inefficient.
51    */

52   public Object JavaDoc emptySet() {
53     FlowSet t = (FlowSet)clone();
54     t.clear();
55     return t;
56   }
57
58   public void copy(FlowSet dest) {
59     List elements = toList();
60     Iterator it = elements.iterator();
61     dest.clear();
62     while (it.hasNext())
63       dest.add(it.next());
64   }
65
66   /**
67    * implemented, but *very* inefficient.
68    */

69   public void clear() {
70     Iterator it = toList().iterator();
71     while (it.hasNext())
72       remove(it.next());
73   }
74
75   public void union(FlowSet other) {
76     union(other, this);
77   }
78
79   public void union(FlowSet other, FlowSet dest) {
80     if (dest != this && dest != other)
81       dest.clear();
82
83     if (dest != this) {
84       Iterator thisIt = toList().iterator();
85       while (thisIt.hasNext())
86         dest.add(thisIt.next());
87     }
88
89     if (dest != other) {
90       Iterator otherIt = other.toList().iterator();
91       while (otherIt.hasNext())
92         dest.add(otherIt.next());
93     }
94   }
95
96   public void intersection(FlowSet other) {
97     intersection(other, this);
98   }
99
100   public void intersection(FlowSet other, FlowSet dest) {
101     if (dest == this && dest == other) return;
102     List elements = null;
103     FlowSet flowSet = null;
104     if (dest == this) {
105       /* makes automaticly a copy of <code>this</code>, as it will be cleared */
106       elements = toList();
107       flowSet = other;
108     } else {
109       /* makes a copy o <code>other</code>, as it might be cleared */
110       elements = other.toList();
111       flowSet = this;
112     }
113     dest.clear();
114     Iterator it = elements.iterator();
115     while (it.hasNext()) {
116       Object JavaDoc o = it.next();
117       if (flowSet.contains(o))
118         dest.add(o);
119     }
120   }
121
122   public void difference(FlowSet other) {
123     difference(other, this);
124   }
125
126   public void difference(FlowSet other, FlowSet dest) {
127     if (dest == this && dest == other) {
128       dest.clear();
129       return;
130     }
131
132     Iterator it = this.toList().iterator();
133     FlowSet flowSet = (other == dest)? (FlowSet)other.clone(): other;
134     dest.clear(); // now safe, since we have copies of this & other
135

136     while (it.hasNext()) {
137       Object JavaDoc o = it.next();
138       if (!flowSet.contains(o))
139         dest.add(o);
140     }
141   }
142
143   public abstract boolean isEmpty();
144
145   public abstract int size();
146
147   public abstract void add(Object JavaDoc obj);
148
149   public void add(Object JavaDoc obj, FlowSet dest) {
150     if (dest != this)
151       copy(dest);
152     dest.add(obj);
153   }
154   
155   public abstract void remove(Object JavaDoc obj);
156
157   public void remove(Object JavaDoc obj, FlowSet dest) {
158     if (dest != this)
159       copy(dest);
160     dest.remove(obj);
161   }
162
163   public abstract boolean contains(Object JavaDoc obj);
164
165   public Iterator iterator() {
166     return toList().iterator();
167   }
168
169   public abstract List toList();
170
171   public boolean equals(Object JavaDoc o) {
172     if (!(o instanceof FlowSet)) return false;
173     FlowSet other = (FlowSet)o;
174     if (size() != other.size()) return false;
175     Iterator it = toList().iterator();
176     while (it.hasNext())
177       if (!other.contains(it.next())) return false;
178     return true;
179   }
180
181   public String JavaDoc toString() {
182     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("{");
183     Iterator it = toList().iterator();
184     if (it.hasNext()) {
185       buffer.append(it.next());
186
187       while(it.hasNext())
188         buffer.append(", " + it.next());
189     }
190     buffer.append("}");
191     return buffer.toString();
192   }
193 }
194
195
Popular Tags