001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2003 Ondrej Lhotak
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot.util;
021:
022: public final class SharedBitSet {
023: BitVector value;
024: boolean own = true;
025:
026: public SharedBitSet(int i) {
027: value = new BitVector(i);
028: }
029:
030: public SharedBitSet() {
031: this (32);
032: }
033:
034: private void acquire() {
035: if (own)
036: return;
037: own = true;
038: value = (BitVector) value.clone();
039: }
040:
041: private void canonicalize() {
042: value = SharedBitSetCache.v().canonicalize(value);
043: own = false;
044: }
045:
046: public boolean set(int bit) {
047: acquire();
048: return value.set(bit);
049: }
050:
051: public void clear(int bit) {
052: acquire();
053: value.clear(bit);
054: }
055:
056: public boolean get(int bit) {
057: return value.get(bit);
058: }
059:
060: public void and(SharedBitSet other) {
061: if (own) {
062: value.and(other.value);
063: } else {
064: value = BitVector.and(value, other.value);
065: own = true;
066: }
067: canonicalize();
068: }
069:
070: public void or(SharedBitSet other) {
071: if (own) {
072: value.or(other.value);
073: } else {
074: value = BitVector.or(value, other.value);
075: own = true;
076: }
077: canonicalize();
078: }
079:
080: public boolean orAndAndNot(SharedBitSet orset, SharedBitSet andset,
081: SharedBitSet andnotset) {
082: acquire();
083: boolean ret = value.orAndAndNot(orset.value, andset.value,
084: andnotset.value);
085: canonicalize();
086: return ret;
087: }
088:
089: public boolean orAndAndNot(SharedBitSet orset, BitVector andset,
090: SharedBitSet andnotset) {
091: acquire();
092: boolean ret = value.orAndAndNot(orset.value, andset,
093: andnotset == null ? null : andnotset.value);
094: canonicalize();
095: return ret;
096: }
097:
098: public BitSetIterator iterator() {
099: return value.iterator();
100: }
101:
102: public String toString() {
103: StringBuffer b = new StringBuffer();
104: for (BitSetIterator it = iterator(); it.hasNext();) {
105: b.append(it.next());
106: b.append(",");
107: }
108: return b.toString();
109: }
110: }
|