duke@1: /* ohair@798: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: /** A class for extensible, mutable bit sets. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Bits { duke@1: duke@1: duke@1: private final static int wordlen = 32; duke@1: private final static int wordshift = 5; duke@1: private final static int wordmask = wordlen - 1; duke@1: duke@1: private int[] bits; duke@1: duke@1: /** Construct an initially empty set. duke@1: */ duke@1: public Bits() { duke@1: this(new int[1]); duke@1: } duke@1: duke@1: /** Construct a set consisting initially of given bit vector. duke@1: */ duke@1: public Bits(int[] bits) { duke@1: this.bits = bits; duke@1: } duke@1: duke@1: /** Construct a set consisting initially of given range. duke@1: */ duke@1: public Bits(int start, int limit) { duke@1: this(); duke@1: inclRange(start, limit); duke@1: } duke@1: duke@1: private void sizeTo(int len) { duke@1: if (bits.length < len) { duke@1: int[] newbits = new int[len]; duke@1: System.arraycopy(bits, 0, newbits, 0, bits.length); duke@1: bits = newbits; duke@1: } duke@1: } duke@1: duke@1: /** This set = {}. duke@1: */ duke@1: public void clear() { duke@1: for (int i = 0; i < bits.length; i++) bits[i] = 0; duke@1: } duke@1: duke@1: /** Return a copy of this set. duke@1: */ duke@1: public Bits dup() { duke@1: int[] newbits = new int[bits.length]; duke@1: System.arraycopy(bits, 0, newbits, 0, bits.length); duke@1: return new Bits(newbits); duke@1: } duke@1: duke@1: /** Include x in this set. duke@1: */ duke@1: public void incl(int x) { duke@1: assert x >= 0; duke@1: sizeTo((x >>> wordshift) + 1); duke@1: bits[x >>> wordshift] = bits[x >>> wordshift] | duke@1: (1 << (x & wordmask)); duke@1: } duke@1: duke@1: duke@1: /** Include [start..limit) in this set. duke@1: */ duke@1: public void inclRange(int start, int limit) { duke@1: sizeTo((limit >>> wordshift) + 1); duke@1: for (int x = start; x < limit; x++) duke@1: bits[x >>> wordshift] = bits[x >>> wordshift] | duke@1: (1 << (x & wordmask)); duke@1: } duke@1: mcimadamore@773: /** Exclude [start...end] from this set. mcimadamore@773: */ mcimadamore@773: public void excludeFrom(int start) { mcimadamore@773: Bits temp = new Bits(); mcimadamore@773: temp.sizeTo(bits.length); mcimadamore@773: temp.inclRange(0, start); mcimadamore@773: andSet(temp); mcimadamore@773: } mcimadamore@773: duke@1: /** Exclude x from this set. duke@1: */ duke@1: public void excl(int x) { duke@1: assert x >= 0; duke@1: sizeTo((x >>> wordshift) + 1); duke@1: bits[x >>> wordshift] = bits[x >>> wordshift] & duke@1: ~(1 << (x & wordmask)); duke@1: } duke@1: duke@1: /** Is x an element of this set? duke@1: */ duke@1: public boolean isMember(int x) { duke@1: return duke@1: 0 <= x && x < (bits.length << wordshift) && duke@1: (bits[x >>> wordshift] & (1 << (x & wordmask))) != 0; duke@1: } duke@1: duke@1: /** this set = this set & xs. duke@1: */ duke@1: public Bits andSet(Bits xs) { duke@1: sizeTo(xs.bits.length); duke@1: for (int i = 0; i < xs.bits.length; i++) duke@1: bits[i] = bits[i] & xs.bits[i]; duke@1: return this; duke@1: } duke@1: duke@1: /** this set = this set | xs. duke@1: */ duke@1: public Bits orSet(Bits xs) { duke@1: sizeTo(xs.bits.length); duke@1: for (int i = 0; i < xs.bits.length; i++) duke@1: bits[i] = bits[i] | xs.bits[i]; duke@1: return this; duke@1: } duke@1: duke@1: /** this set = this set \ xs. duke@1: */ duke@1: public Bits diffSet(Bits xs) { duke@1: for (int i = 0; i < bits.length; i++) { duke@1: if (i < xs.bits.length) { duke@1: bits[i] = bits[i] & ~xs.bits[i]; duke@1: } duke@1: } duke@1: return this; duke@1: } duke@1: duke@1: /** this set = this set ^ xs. duke@1: */ duke@1: public Bits xorSet(Bits xs) { duke@1: sizeTo(xs.bits.length); duke@1: for (int i = 0; i < xs.bits.length; i++) duke@1: bits[i] = bits[i] ^ xs.bits[i]; duke@1: return this; duke@1: } duke@1: duke@1: /** Count trailing zero bits in an int. Algorithm from "Hacker's duke@1: * Delight" by Henry S. Warren Jr. (figure 5-13) duke@1: */ duke@1: private static int trailingZeroBits(int x) { duke@1: assert wordlen == 32; duke@1: if (x == 0) return 32; duke@1: int n = 1; duke@1: if ((x & 0xffff) == 0) { n += 16; x >>>= 16; } duke@1: if ((x & 0x00ff) == 0) { n += 8; x >>>= 8; } duke@1: if ((x & 0x000f) == 0) { n += 4; x >>>= 4; } duke@1: if ((x & 0x0003) == 0) { n += 2; x >>>= 2; } duke@1: return n - (x&1); duke@1: } duke@1: duke@1: /** Return the index of the least bit position >= x that is set. duke@1: * If none are set, returns -1. This provides a nice way to iterate duke@1: * over the members of a bit set: duke@1: *

duke@1:      *  for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ...
duke@1:      *  
duke@1: */ duke@1: public int nextBit(int x) { duke@1: int windex = x >>> wordshift; duke@1: if (windex >= bits.length) return -1; duke@1: int word = bits[windex] & ~((1 << (x & wordmask))-1); duke@1: while (true) { duke@1: if (word != 0) duke@1: return (windex << wordshift) + trailingZeroBits(word); duke@1: windex++; duke@1: if (windex >= bits.length) return -1; duke@1: word = bits[windex]; duke@1: } duke@1: } duke@1: duke@1: /** a string representation of this set. duke@1: */ duke@1: public String toString() { duke@1: char[] digits = new char[bits.length * wordlen]; duke@1: for (int i = 0; i < bits.length * wordlen; i++) duke@1: digits[i] = isMember(i) ? '1' : '0'; duke@1: return new String(digits); duke@1: } duke@1: duke@1: /** Test Bits.nextBit(int). */ duke@1: public static void main(String[] args) { duke@1: java.util.Random r = new java.util.Random(); duke@1: Bits bits = new Bits(); duke@1: int dupCount = 0; duke@1: for (int i=0; i<125; i++) { duke@1: int k; duke@1: do { duke@1: k = r.nextInt(250); duke@1: } while (bits.isMember(k)); duke@1: System.out.println("adding " + k); duke@1: bits.incl(k); duke@1: } duke@1: int count = 0; duke@1: for (int i = bits.nextBit(0); i >= 0; i = bits.nextBit(i+1)) { duke@1: System.out.println("found " + i); duke@1: count ++; duke@1: } duke@1: if (count != 125) throw new Error(); duke@1: } duke@1: }