KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > sets > SharedPointsToSet


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Ondrej Lhotak
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 package soot.jimple.spark.sets;
21 import soot.jimple.spark.*;
22 import soot.jimple.spark.internal.*;
23 import soot.jimple.spark.pag.Node;
24 import soot.jimple.spark.pag.PAG;
25 import soot.util.*;
26 import soot.Type;
27
28 /** Hybrid implementation of points-to set, which uses an explicit array for
29  * small sets, and a bit vector for large sets.
30  * @author Ondrej Lhotak
31  */

32 public final class SharedPointsToSet extends PointsToSetInternal {
33     public SharedPointsToSet( Type type, PAG pag ) {
34         super( type );
35         this.pag = pag;
36     }
37     /** Returns true if this set contains no run-time objects. */
38     public final boolean isEmpty() {
39         return empty;
40     }
41
42     private final boolean superAddAll( PointsToSetInternal other, PointsToSetInternal exclude ) {
43         boolean ret = super.addAll( other, exclude );
44         if( ret ) empty = false;
45         return ret;
46     }
47
48     private final boolean nativeAddAll( SharedPointsToSet other, SharedPointsToSet exclude ) {
49         boolean ret = false;
50         BitVector mask = null;
51         TypeManager typeManager = (TypeManager) pag.getTypeManager();
52         if( !typeManager.castNeverFails( other.getType(), this.getType() ) ) {
53             mask = (BitVector) typeManager.get( this.getType() );
54         }
55         if( other.bits != null ) {
56             convertToBits();
57             if( exclude != null ) {
58                 exclude.convertToBits();
59             }
60             SharedBitSet ebits = ( exclude==null ? null : exclude.bits );
61             ret = bits.orAndAndNot( other.bits, mask, ebits );
62         } else {
63             do {
64                 if( other.n1 == null ) break;
65                 if( exclude == null || !exclude.contains( other.n1 ) ) {
66                     ret = add( other.n1 ) | ret;
67                 }
68                 if( other.n2 == null ) break;
69                 if( exclude == null || !exclude.contains( other.n2 ) ) {
70                     ret = add( other.n2 ) | ret;
71                 }
72                 if( other.n3 == null ) break;
73                 if( exclude == null || !exclude.contains( other.n3 ) ) {
74                     ret = add( other.n3 ) | ret;
75                 }
76                 if( other.n4 == null ) break;
77                 if( exclude == null || !exclude.contains( other.n4 ) ) {
78                     ret = add( other.n4 ) | ret;
79                 }
80                 if( other.n5 == null ) break;
81                 if( exclude == null || !exclude.contains( other.n5 ) ) {
82                     ret = add( other.n5 ) | ret;
83                 }
84                 if( other.n6 == null ) break;
85                 if( exclude == null || !exclude.contains( other.n6 ) ) {
86                     ret = add( other.n6 ) | ret;
87                 }
88                 if( other.n7 == null ) break;
89                 if( exclude == null || !exclude.contains( other.n7 ) ) {
90                     ret = add( other.n7 ) | ret;
91                 }
92                 if( other.n8 == null ) break;
93                 if( exclude == null || !exclude.contains( other.n8 ) ) {
94                     ret = add( other.n8 ) | ret;
95                 }
96                 if( other.n9 == null ) break;
97                 if( exclude == null || !exclude.contains( other.n9 ) ) {
98                     ret = add( other.n9 ) | ret;
99                 }
100                 if( other.n10 == null ) break;
101                 if( exclude == null || !exclude.contains( other.n10 ) ) {
102                     ret = add( other.n10 ) | ret;
103                 }
104                 if( other.n11 == null ) break;
105                 if( exclude == null || !exclude.contains( other.n11 ) ) {
106                     ret = add( other.n11 ) | ret;
107                 }
108                 if( other.n12 == null ) break;
109                 if( exclude == null || !exclude.contains( other.n12 ) ) {
110                     ret = add( other.n12 ) | ret;
111                 }
112                 if( other.n13 == null ) break;
113                 if( exclude == null || !exclude.contains( other.n13 ) ) {
114                     ret = add( other.n13 ) | ret;
115                 }
116                 if( other.n14 == null ) break;
117                 if( exclude == null || !exclude.contains( other.n14 ) ) {
118                     ret = add( other.n14 ) | ret;
119                 }
120                 if( other.n15 == null ) break;
121                 if( exclude == null || !exclude.contains( other.n15 ) ) {
122                     ret = add( other.n15 ) | ret;
123                 }
124                 if( other.n16 == null ) break;
125                 if( exclude == null || !exclude.contains( other.n16 ) ) {
126                     ret = add( other.n16 ) | ret;
127                 }
128             } while( false );
129         }
130         if( ret ) empty = false;
131         return ret;
132     }
133  
134     /** Adds contents of other into this set, returns true if this set
135      * changed. */

136     public final boolean addAll( final PointsToSetInternal other,
137             final PointsToSetInternal exclude ) {
138         if( other != null && !(other instanceof SharedPointsToSet) )
139             return superAddAll( other, exclude );
140         if( exclude != null && !(exclude instanceof SharedPointsToSet) )
141             return superAddAll( other, exclude );
142         return nativeAddAll( (SharedPointsToSet) other, (SharedPointsToSet) exclude );
143     }
144
145     /** Calls v's visit method on all nodes in this set. */
146     public final boolean forall( P2SetVisitor v ) {
147        if( bits == null ) {
148             if( n1 == null ) return v.getReturnValue(); v.visit( n1 );
149             if( n2 == null ) return v.getReturnValue(); v.visit( n2 );
150             if( n3 == null ) return v.getReturnValue(); v.visit( n3 );
151             if( n4 == null ) return v.getReturnValue(); v.visit( n4 );
152             if( n5 == null ) return v.getReturnValue(); v.visit( n5 );
153             if( n6 == null ) return v.getReturnValue(); v.visit( n6 );
154             if( n7 == null ) return v.getReturnValue(); v.visit( n7 );
155             if( n8 == null ) return v.getReturnValue(); v.visit( n8 );
156             if( n9 == null ) return v.getReturnValue(); v.visit( n9 );
157             if( n10 == null ) return v.getReturnValue(); v.visit( n10 );
158             if( n11 == null ) return v.getReturnValue(); v.visit( n11 );
159             if( n12 == null ) return v.getReturnValue(); v.visit( n12 );
160             if( n13 == null ) return v.getReturnValue(); v.visit( n13 );
161             if( n14 == null ) return v.getReturnValue(); v.visit( n14 );
162             if( n15 == null ) return v.getReturnValue(); v.visit( n15 );
163             if( n16 == null ) return v.getReturnValue(); v.visit( n16 );
164         } else {
165             for( BitSetIterator it = bits.iterator(); it.hasNext(); ) {
166                 v.visit( (Node) pag.getAllocNodeNumberer().get( it.next() ) );
167             }
168         }
169         return v.getReturnValue();
170     }
171     /** Adds n to this set, returns true if n was not already in this set. */
172     public final boolean add( Node n ) {
173         if( pag.getTypeManager().castNeverFails( n.getType(), type ) ) {
174             return fastAdd( n );
175         }
176         return false;
177     }
178     /** Returns true iff the set contains n. */
179     public final boolean contains( Node n ) {
180         if( bits == null ) {
181             if( n1 == n ) return true;
182             if( n2 == n ) return true;
183             if( n3 == n ) return true;
184             if( n4 == n ) return true;
185             if( n5 == n ) return true;
186             if( n6 == n ) return true;
187             if( n7 == n ) return true;
188             if( n8 == n ) return true;
189             if( n9 == n ) return true;
190             if( n10 == n ) return true;
191             if( n11 == n ) return true;
192             if( n12 == n ) return true;
193             if( n13 == n ) return true;
194             if( n14 == n ) return true;
195             if( n15 == n ) return true;
196             if( n16 == n ) return true;
197             return false;
198         } else {
199             return bits.get( n.getNumber() );
200         }
201     }
202     public final static P2SetFactory getFactory() {
203         return new P2SetFactory() {
204             public final PointsToSetInternal newSet( Type type, PAG pag ) {
205                 return new SharedPointsToSet( type, pag );
206             }
207         };
208     }
209
210     /* End of public methods. */
211     /* End of package methods. */
212
213     protected final boolean fastAdd( Node n ) {
214         if( bits == null ) {
215             if( n1 == null ) { empty = false; n1 = n; return true; } if( n1 == n ) return false;
216             if( n2 == null ) { empty = false; n2 = n; return true; } if( n2 == n ) return false;
217             if( n3 == null ) { empty = false; n3 = n; return true; } if( n3 == n ) return false;
218             if( n4 == null ) { empty = false; n4 = n; return true; } if( n4 == n ) return false;
219             if( n5 == null ) { empty = false; n5 = n; return true; } if( n5 == n ) return false;
220             if( n6 == null ) { empty = false; n6 = n; return true; } if( n6 == n ) return false;
221             if( n7 == null ) { empty = false; n7 = n; return true; } if( n7 == n ) return false;
222             if( n8 == null ) { empty = false; n8 = n; return true; } if( n8 == n ) return false;
223             if( n9 == null ) { empty = false; n9 = n; return true; } if( n9 == n ) return false;
224             if( n10 == null ) { empty = false; n10 = n; return true; } if( n10 == n ) return false;
225             if( n11 == null ) { empty = false; n11 = n; return true; } if( n11 == n ) return false;
226             if( n12 == null ) { empty = false; n12 = n; return true; } if( n12 == n ) return false;
227             if( n13 == null ) { empty = false; n13 = n; return true; } if( n13 == n ) return false;
228             if( n14 == null ) { empty = false; n14 = n; return true; } if( n14 == n ) return false;
229             if( n15 == null ) { empty = false; n15 = n; return true; } if( n15 == n ) return false;
230             if( n16 == null ) { empty = false; n16 = n; return true; } if( n16 == n ) return false;
231             convertToBits();
232         }
233         boolean ret = bits.set( n.getNumber() );
234         if( ret ) empty = false;
235         return ret;
236     }
237
238     protected final void convertToBits() {
239         if( bits != null ) return;
240         bits = new SharedBitSet( pag.getAllocNodeNumberer().size() );
241         if( n1 != null ) fastAdd( n1 );
242         if( n2 != null ) fastAdd( n2 );
243         if( n3 != null ) fastAdd( n3 );
244         if( n4 != null ) fastAdd( n4 );
245         if( n5 != null ) fastAdd( n5 );
246         if( n6 != null ) fastAdd( n6 );
247         if( n7 != null ) fastAdd( n7 );
248         if( n8 != null ) fastAdd( n8 );
249         if( n9 != null ) fastAdd( n9 );
250         if( n10 != null ) fastAdd( n10 );
251         if( n11 != null ) fastAdd( n11 );
252         if( n12 != null ) fastAdd( n12 );
253         if( n13 != null ) fastAdd( n13 );
254         if( n14 != null ) fastAdd( n14 );
255         if( n15 != null ) fastAdd( n15 );
256         if( n16 != null ) fastAdd( n16 );
257     }
258
259     private Node n1 = null;
260     private Node n2 = null;
261     private Node n3 = null;
262     private Node n4 = null;
263     private Node n5 = null;
264     private Node n6 = null;
265     private Node n7 = null;
266     private Node n8 = null;
267     private Node n9 = null;
268     private Node n10 = null;
269     private Node n11 = null;
270     private Node n12 = null;
271     private Node n13 = null;
272     private Node n14 = null;
273     private Node n15 = null;
274     private Node n16 = null;
275     private SharedBitSet bits = null;
276     private PAG pag;
277     private boolean empty = true;
278 }
279
280
Popular Tags