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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 773
5fb14e67c371
child 816
7c537f4298fb
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

duke@1 1 /*
ohair@798 2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
duke@1 28 /** A class for extensible, mutable bit sets.
duke@1 29 *
jjg@581 30 * <p><b>This is NOT part of any supported API.
jjg@581 31 * If you write code that depends on this, you do so at your own risk.
duke@1 32 * This code and its internal interfaces are subject to change or
duke@1 33 * deletion without notice.</b>
duke@1 34 */
duke@1 35 public class Bits {
duke@1 36
duke@1 37
duke@1 38 private final static int wordlen = 32;
duke@1 39 private final static int wordshift = 5;
duke@1 40 private final static int wordmask = wordlen - 1;
duke@1 41
duke@1 42 private int[] bits;
duke@1 43
duke@1 44 /** Construct an initially empty set.
duke@1 45 */
duke@1 46 public Bits() {
duke@1 47 this(new int[1]);
duke@1 48 }
duke@1 49
duke@1 50 /** Construct a set consisting initially of given bit vector.
duke@1 51 */
duke@1 52 public Bits(int[] bits) {
duke@1 53 this.bits = bits;
duke@1 54 }
duke@1 55
duke@1 56 /** Construct a set consisting initially of given range.
duke@1 57 */
duke@1 58 public Bits(int start, int limit) {
duke@1 59 this();
duke@1 60 inclRange(start, limit);
duke@1 61 }
duke@1 62
duke@1 63 private void sizeTo(int len) {
duke@1 64 if (bits.length < len) {
duke@1 65 int[] newbits = new int[len];
duke@1 66 System.arraycopy(bits, 0, newbits, 0, bits.length);
duke@1 67 bits = newbits;
duke@1 68 }
duke@1 69 }
duke@1 70
duke@1 71 /** This set = {}.
duke@1 72 */
duke@1 73 public void clear() {
duke@1 74 for (int i = 0; i < bits.length; i++) bits[i] = 0;
duke@1 75 }
duke@1 76
duke@1 77 /** Return a copy of this set.
duke@1 78 */
duke@1 79 public Bits dup() {
duke@1 80 int[] newbits = new int[bits.length];
duke@1 81 System.arraycopy(bits, 0, newbits, 0, bits.length);
duke@1 82 return new Bits(newbits);
duke@1 83 }
duke@1 84
duke@1 85 /** Include x in this set.
duke@1 86 */
duke@1 87 public void incl(int x) {
duke@1 88 assert x >= 0;
duke@1 89 sizeTo((x >>> wordshift) + 1);
duke@1 90 bits[x >>> wordshift] = bits[x >>> wordshift] |
duke@1 91 (1 << (x & wordmask));
duke@1 92 }
duke@1 93
duke@1 94
duke@1 95 /** Include [start..limit) in this set.
duke@1 96 */
duke@1 97 public void inclRange(int start, int limit) {
duke@1 98 sizeTo((limit >>> wordshift) + 1);
duke@1 99 for (int x = start; x < limit; x++)
duke@1 100 bits[x >>> wordshift] = bits[x >>> wordshift] |
duke@1 101 (1 << (x & wordmask));
duke@1 102 }
duke@1 103
mcimadamore@773 104 /** Exclude [start...end] from this set.
mcimadamore@773 105 */
mcimadamore@773 106 public void excludeFrom(int start) {
mcimadamore@773 107 Bits temp = new Bits();
mcimadamore@773 108 temp.sizeTo(bits.length);
mcimadamore@773 109 temp.inclRange(0, start);
mcimadamore@773 110 andSet(temp);
mcimadamore@773 111 }
mcimadamore@773 112
duke@1 113 /** Exclude x from this set.
duke@1 114 */
duke@1 115 public void excl(int x) {
duke@1 116 assert x >= 0;
duke@1 117 sizeTo((x >>> wordshift) + 1);
duke@1 118 bits[x >>> wordshift] = bits[x >>> wordshift] &
duke@1 119 ~(1 << (x & wordmask));
duke@1 120 }
duke@1 121
duke@1 122 /** Is x an element of this set?
duke@1 123 */
duke@1 124 public boolean isMember(int x) {
duke@1 125 return
duke@1 126 0 <= x && x < (bits.length << wordshift) &&
duke@1 127 (bits[x >>> wordshift] & (1 << (x & wordmask))) != 0;
duke@1 128 }
duke@1 129
duke@1 130 /** this set = this set & xs.
duke@1 131 */
duke@1 132 public Bits andSet(Bits xs) {
duke@1 133 sizeTo(xs.bits.length);
duke@1 134 for (int i = 0; i < xs.bits.length; i++)
duke@1 135 bits[i] = bits[i] & xs.bits[i];
duke@1 136 return this;
duke@1 137 }
duke@1 138
duke@1 139 /** this set = this set | xs.
duke@1 140 */
duke@1 141 public Bits orSet(Bits xs) {
duke@1 142 sizeTo(xs.bits.length);
duke@1 143 for (int i = 0; i < xs.bits.length; i++)
duke@1 144 bits[i] = bits[i] | xs.bits[i];
duke@1 145 return this;
duke@1 146 }
duke@1 147
duke@1 148 /** this set = this set \ xs.
duke@1 149 */
duke@1 150 public Bits diffSet(Bits xs) {
duke@1 151 for (int i = 0; i < bits.length; i++) {
duke@1 152 if (i < xs.bits.length) {
duke@1 153 bits[i] = bits[i] & ~xs.bits[i];
duke@1 154 }
duke@1 155 }
duke@1 156 return this;
duke@1 157 }
duke@1 158
duke@1 159 /** this set = this set ^ xs.
duke@1 160 */
duke@1 161 public Bits xorSet(Bits xs) {
duke@1 162 sizeTo(xs.bits.length);
duke@1 163 for (int i = 0; i < xs.bits.length; i++)
duke@1 164 bits[i] = bits[i] ^ xs.bits[i];
duke@1 165 return this;
duke@1 166 }
duke@1 167
duke@1 168 /** Count trailing zero bits in an int. Algorithm from "Hacker's
duke@1 169 * Delight" by Henry S. Warren Jr. (figure 5-13)
duke@1 170 */
duke@1 171 private static int trailingZeroBits(int x) {
duke@1 172 assert wordlen == 32;
duke@1 173 if (x == 0) return 32;
duke@1 174 int n = 1;
duke@1 175 if ((x & 0xffff) == 0) { n += 16; x >>>= 16; }
duke@1 176 if ((x & 0x00ff) == 0) { n += 8; x >>>= 8; }
duke@1 177 if ((x & 0x000f) == 0) { n += 4; x >>>= 4; }
duke@1 178 if ((x & 0x0003) == 0) { n += 2; x >>>= 2; }
duke@1 179 return n - (x&1);
duke@1 180 }
duke@1 181
duke@1 182 /** Return the index of the least bit position >= x that is set.
duke@1 183 * If none are set, returns -1. This provides a nice way to iterate
duke@1 184 * over the members of a bit set:
duke@1 185 * <pre>
duke@1 186 * for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ...
duke@1 187 * </pre>
duke@1 188 */
duke@1 189 public int nextBit(int x) {
duke@1 190 int windex = x >>> wordshift;
duke@1 191 if (windex >= bits.length) return -1;
duke@1 192 int word = bits[windex] & ~((1 << (x & wordmask))-1);
duke@1 193 while (true) {
duke@1 194 if (word != 0)
duke@1 195 return (windex << wordshift) + trailingZeroBits(word);
duke@1 196 windex++;
duke@1 197 if (windex >= bits.length) return -1;
duke@1 198 word = bits[windex];
duke@1 199 }
duke@1 200 }
duke@1 201
duke@1 202 /** a string representation of this set.
duke@1 203 */
duke@1 204 public String toString() {
duke@1 205 char[] digits = new char[bits.length * wordlen];
duke@1 206 for (int i = 0; i < bits.length * wordlen; i++)
duke@1 207 digits[i] = isMember(i) ? '1' : '0';
duke@1 208 return new String(digits);
duke@1 209 }
duke@1 210
duke@1 211 /** Test Bits.nextBit(int). */
duke@1 212 public static void main(String[] args) {
duke@1 213 java.util.Random r = new java.util.Random();
duke@1 214 Bits bits = new Bits();
duke@1 215 int dupCount = 0;
duke@1 216 for (int i=0; i<125; i++) {
duke@1 217 int k;
duke@1 218 do {
duke@1 219 k = r.nextInt(250);
duke@1 220 } while (bits.isMember(k));
duke@1 221 System.out.println("adding " + k);
duke@1 222 bits.incl(k);
duke@1 223 }
duke@1 224 int count = 0;
duke@1 225 for (int i = bits.nextBit(0); i >= 0; i = bits.nextBit(i+1)) {
duke@1 226 System.out.println("found " + i);
duke@1 227 count ++;
duke@1 228 }
duke@1 229 if (count != 125) throw new Error();
duke@1 230 }
duke@1 231 }

mercurial