src/share/classes/com/sun/tools/javac/util/Bits.java

changeset 1
9a66ca7c79fa
child 554
9d9f26857129
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Bits.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,222 @@
     1.4 +/*
     1.5 + * Copyright 1999-2005 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javac.util;
    1.30 +
    1.31 +/** A class for extensible, mutable bit sets.
    1.32 + *
    1.33 + *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.34 + *  you write code that depends on this, you do so at your own risk.
    1.35 + *  This code and its internal interfaces are subject to change or
    1.36 + *  deletion without notice.</b>
    1.37 + */
    1.38 +public class Bits {
    1.39 +
    1.40 +
    1.41 +    private final static int wordlen = 32;
    1.42 +    private final static int wordshift = 5;
    1.43 +    private final static int wordmask = wordlen - 1;
    1.44 +
    1.45 +    private int[] bits;
    1.46 +
    1.47 +    /** Construct an initially empty set.
    1.48 +     */
    1.49 +    public Bits() {
    1.50 +        this(new int[1]);
    1.51 +    }
    1.52 +
    1.53 +    /** Construct a set consisting initially of given bit vector.
    1.54 +     */
    1.55 +    public Bits(int[] bits) {
    1.56 +        this.bits = bits;
    1.57 +    }
    1.58 +
    1.59 +    /** Construct a set consisting initially of given range.
    1.60 +     */
    1.61 +    public Bits(int start, int limit) {
    1.62 +        this();
    1.63 +        inclRange(start, limit);
    1.64 +    }
    1.65 +
    1.66 +    private void sizeTo(int len) {
    1.67 +        if (bits.length < len) {
    1.68 +            int[] newbits = new int[len];
    1.69 +            System.arraycopy(bits, 0, newbits, 0, bits.length);
    1.70 +            bits = newbits;
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +    /** This set = {}.
    1.75 +     */
    1.76 +    public void clear() {
    1.77 +        for (int i = 0; i < bits.length; i++) bits[i] = 0;
    1.78 +    }
    1.79 +
    1.80 +    /** Return a copy of this set.
    1.81 +     */
    1.82 +    public Bits dup() {
    1.83 +        int[] newbits = new int[bits.length];
    1.84 +        System.arraycopy(bits, 0, newbits, 0, bits.length);
    1.85 +        return new Bits(newbits);
    1.86 +    }
    1.87 +
    1.88 +    /** Include x in this set.
    1.89 +     */
    1.90 +    public void incl(int x) {
    1.91 +        assert x >= 0;
    1.92 +        sizeTo((x >>> wordshift) + 1);
    1.93 +        bits[x >>> wordshift] = bits[x >>> wordshift] |
    1.94 +            (1 << (x & wordmask));
    1.95 +    }
    1.96 +
    1.97 +
    1.98 +    /** Include [start..limit) in this set.
    1.99 +     */
   1.100 +    public void inclRange(int start, int limit) {
   1.101 +        sizeTo((limit >>> wordshift) + 1);
   1.102 +        for (int x = start; x < limit; x++)
   1.103 +            bits[x >>> wordshift] = bits[x >>> wordshift] |
   1.104 +                (1 << (x & wordmask));
   1.105 +    }
   1.106 +
   1.107 +    /** Exclude x from this set.
   1.108 +     */
   1.109 +    public void excl(int x) {
   1.110 +        assert x >= 0;
   1.111 +        sizeTo((x >>> wordshift) + 1);
   1.112 +        bits[x >>> wordshift] = bits[x >>> wordshift] &
   1.113 +            ~(1 << (x & wordmask));
   1.114 +    }
   1.115 +
   1.116 +    /** Is x an element of this set?
   1.117 +     */
   1.118 +    public boolean isMember(int x) {
   1.119 +        return
   1.120 +            0 <= x && x < (bits.length << wordshift) &&
   1.121 +            (bits[x >>> wordshift] & (1 << (x & wordmask))) != 0;
   1.122 +    }
   1.123 +
   1.124 +    /** this set = this set & xs.
   1.125 +     */
   1.126 +    public Bits andSet(Bits xs) {
   1.127 +        sizeTo(xs.bits.length);
   1.128 +        for (int i = 0; i < xs.bits.length; i++)
   1.129 +            bits[i] = bits[i] & xs.bits[i];
   1.130 +        return this;
   1.131 +    }
   1.132 +
   1.133 +    /** this set = this set | xs.
   1.134 +     */
   1.135 +    public Bits orSet(Bits xs) {
   1.136 +        sizeTo(xs.bits.length);
   1.137 +        for (int i = 0; i < xs.bits.length; i++)
   1.138 +            bits[i] = bits[i] | xs.bits[i];
   1.139 +        return this;
   1.140 +    }
   1.141 +
   1.142 +    /** this set = this set \ xs.
   1.143 +     */
   1.144 +    public Bits diffSet(Bits xs) {
   1.145 +        for (int i = 0; i < bits.length; i++) {
   1.146 +            if (i < xs.bits.length) {
   1.147 +                bits[i] = bits[i] & ~xs.bits[i];
   1.148 +            }
   1.149 +        }
   1.150 +        return this;
   1.151 +    }
   1.152 +
   1.153 +    /** this set = this set ^ xs.
   1.154 +     */
   1.155 +    public Bits xorSet(Bits xs) {
   1.156 +        sizeTo(xs.bits.length);
   1.157 +        for (int i = 0; i < xs.bits.length; i++)
   1.158 +            bits[i] = bits[i] ^ xs.bits[i];
   1.159 +        return this;
   1.160 +    }
   1.161 +
   1.162 +    /** Count trailing zero bits in an int. Algorithm from "Hacker's
   1.163 +     *  Delight" by Henry S. Warren Jr. (figure 5-13)
   1.164 +     */
   1.165 +    private static int trailingZeroBits(int x) {
   1.166 +        assert wordlen == 32;
   1.167 +        if (x == 0) return 32;
   1.168 +        int n = 1;
   1.169 +        if ((x & 0xffff) == 0) { n += 16; x >>>= 16; }
   1.170 +        if ((x & 0x00ff) == 0) { n +=  8; x >>>=  8; }
   1.171 +        if ((x & 0x000f) == 0) { n +=  4; x >>>=  4; }
   1.172 +        if ((x & 0x0003) == 0) { n +=  2; x >>>=  2; }
   1.173 +        return n - (x&1);
   1.174 +    }
   1.175 +
   1.176 +    /** Return the index of the least bit position >= x that is set.
   1.177 +     *  If none are set, returns -1.  This provides a nice way to iterate
   1.178 +     *  over the members of a bit set:
   1.179 +     *  <pre>
   1.180 +     *  for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ...
   1.181 +     *  </pre>
   1.182 +     */
   1.183 +    public int nextBit(int x) {
   1.184 +        int windex = x >>> wordshift;
   1.185 +        if (windex >= bits.length) return -1;
   1.186 +        int word = bits[windex] & ~((1 << (x & wordmask))-1);
   1.187 +        while (true) {
   1.188 +            if (word != 0)
   1.189 +                return (windex << wordshift) + trailingZeroBits(word);
   1.190 +            windex++;
   1.191 +            if (windex >= bits.length) return -1;
   1.192 +            word = bits[windex];
   1.193 +        }
   1.194 +    }
   1.195 +
   1.196 +    /** a string representation of this set.
   1.197 +     */
   1.198 +    public String toString() {
   1.199 +        char[] digits = new char[bits.length * wordlen];
   1.200 +        for (int i = 0; i < bits.length * wordlen; i++)
   1.201 +            digits[i] = isMember(i) ? '1' : '0';
   1.202 +        return new String(digits);
   1.203 +    }
   1.204 +
   1.205 +    /** Test Bits.nextBit(int). */
   1.206 +    public static void main(String[] args) {
   1.207 +        java.util.Random r = new java.util.Random();
   1.208 +        Bits bits = new Bits();
   1.209 +        int dupCount = 0;
   1.210 +        for (int i=0; i<125; i++) {
   1.211 +            int k;
   1.212 +            do {
   1.213 +                k = r.nextInt(250);
   1.214 +            } while (bits.isMember(k));
   1.215 +            System.out.println("adding " + k);
   1.216 +            bits.incl(k);
   1.217 +        }
   1.218 +        int count = 0;
   1.219 +        for (int i = bits.nextBit(0); i >= 0; i = bits.nextBit(i+1)) {
   1.220 +            System.out.println("found " + i);
   1.221 +            count ++;
   1.222 +        }
   1.223 +        if (count != 125) throw new Error();
   1.224 +    }
   1.225 +}

mercurial