KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > SharedBitSet


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 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.util;
21
22 public final class SharedBitSet {
23     BitVector value;
24     boolean own = true;
25     public SharedBitSet(int i) {
26         value = new BitVector( i );
27     }
28     public SharedBitSet() {
29         this(32);
30     }
31     private void acquire() {
32         if( own ) return;
33         own = true;
34         value = (BitVector) value.clone();
35     }
36     private void canonicalize() {
37         value = SharedBitSetCache.v().canonicalize( value );
38         own = false;
39     }
40     public boolean set(int bit) {
41         acquire();
42         return value.set(bit);
43     }
44     public void clear(int bit) {
45         acquire();
46         value.clear(bit);
47     }
48     public boolean get(int bit) {
49         return value.get(bit);
50     }
51     public void and( SharedBitSet other ) {
52         if( own ) {
53             value.and( other.value );
54         } else {
55             value = BitVector.and( value, other.value );
56             own = true;
57         }
58         canonicalize();
59     }
60     public void or( SharedBitSet other ) {
61         if( own ) {
62             value.or( other.value );
63         } else {
64             value = BitVector.or( value, other.value );
65             own = true;
66         }
67         canonicalize();
68     }
69     public boolean orAndAndNot( SharedBitSet orset, SharedBitSet andset, SharedBitSet andnotset ) {
70         acquire();
71         boolean ret = value.orAndAndNot( orset.value, andset.value, andnotset.value );
72         canonicalize();
73         return ret;
74     }
75     public boolean orAndAndNot( SharedBitSet orset, BitVector andset, SharedBitSet andnotset ) {
76         acquire();
77         boolean ret = value.orAndAndNot( orset.value, andset,
78                 andnotset == null ? null : andnotset.value );
79         canonicalize();
80         return ret;
81     }
82     public BitSetIterator iterator() {
83         return value.iterator();
84     }
85     public String JavaDoc toString() {
86         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
87         for( BitSetIterator it = iterator(); it.hasNext(); ) {
88             b.append( it.next() );
89             b.append( "," );
90         }
91         return b.toString();
92     }
93 }
94
Popular Tags