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

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2820
7f6d6b80a58b
parent 2702
9ca8d8713094
permissions
-rw-r--r--

merge

aoqi@0 1 /*
vromero@2820 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.util;
aoqi@0 27
aoqi@0 28 import java.util.Arrays;
aoqi@0 29
aoqi@0 30 /** A class for extensible, mutable bit sets.
aoqi@0 31 *
aoqi@0 32 * <p><b>This is NOT part of any supported API.
aoqi@0 33 * If you write code that depends on this, you do so at your own risk.
aoqi@0 34 * This code and its internal interfaces are subject to change or
aoqi@0 35 * deletion without notice.</b>
aoqi@0 36 */
aoqi@0 37 public class Bits {
aoqi@0 38
aoqi@0 39 // ____________ reset _________
aoqi@0 40 // / UNKNOWN \ <-------- / UNINIT \
aoqi@0 41 // \____________/ | \_________/
aoqi@0 42 // | | |
aoqi@0 43 // |assign | | any
aoqi@0 44 // | ___________ |
aoqi@0 45 // ------> / NORMAL \ <----
aoqi@0 46 // \___________/ |
aoqi@0 47 // | |
aoqi@0 48 // | |
aoqi@0 49 // -----------
aoqi@0 50 // any
aoqi@0 51 protected enum BitsState {
aoqi@0 52 /* A Bits instance is in UNKNOWN state if it has been explicitly reset.
aoqi@0 53 * It is possible to get to this state from any other by calling the
aoqi@0 54 * reset method. An instance in the UNKNOWN state can pass to the
aoqi@0 55 * NORMAL state after being assigned another Bits instance.
aoqi@0 56 *
aoqi@0 57 * Bits instances are final fields in Flow so the UNKNOWN state models
aoqi@0 58 * the null assignment.
aoqi@0 59 */
aoqi@0 60 UNKNOWN,
aoqi@0 61 /* A Bits instance is in UNINIT when it is created with the default
aoqi@0 62 * constructor but it isn't explicitly reset. The main objective of this
aoqi@0 63 * internal state is to save some memory.
aoqi@0 64 */
aoqi@0 65 UNINIT,
aoqi@0 66 /* The normal state is reached after creating a Bits instance from an
aoqi@0 67 * existing one or after applying any operation to an instance on UNINIT
aoqi@0 68 * or NORMAL state. From this state a bits instance can pass to the
aoqi@0 69 * UNKNOWN state by calling the reset method.
aoqi@0 70 */
aoqi@0 71 NORMAL;
aoqi@0 72
aoqi@0 73 static BitsState getState(int[] someBits, boolean reset) {
aoqi@0 74 if (reset) {
aoqi@0 75 return UNKNOWN;
aoqi@0 76 } else {
aoqi@0 77 if (someBits != unassignedBits) {
aoqi@0 78 return NORMAL;
aoqi@0 79 } else {
aoqi@0 80 return UNINIT;
aoqi@0 81 }
aoqi@0 82 }
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 private final static int wordlen = 32;
aoqi@0 88 private final static int wordshift = 5;
aoqi@0 89 private final static int wordmask = wordlen - 1;
aoqi@0 90
aoqi@0 91 public int[] bits = null;
aoqi@0 92 // This field will store last version of bits after every change.
aoqi@0 93 private static final int[] unassignedBits = new int[0];
aoqi@0 94
aoqi@0 95 protected BitsState currentState;
aoqi@0 96
aoqi@0 97 /** Construct an initially empty set.
aoqi@0 98 */
aoqi@0 99 public Bits() {
aoqi@0 100 this(false);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public Bits(Bits someBits) {
aoqi@0 104 this(someBits.dup().bits, BitsState.getState(someBits.bits, false));
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 public Bits(boolean reset) {
aoqi@0 108 this(unassignedBits, BitsState.getState(unassignedBits, reset));
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 /** Construct a set consisting initially of given bit vector.
aoqi@0 112 */
aoqi@0 113 protected Bits(int[] bits, BitsState initState) {
aoqi@0 114 this.bits = bits;
aoqi@0 115 this.currentState = initState;
aoqi@0 116 switch (initState) {
aoqi@0 117 case UNKNOWN:
aoqi@0 118 this.bits = null;
aoqi@0 119 break;
aoqi@0 120 case NORMAL:
aoqi@0 121 Assert.check(bits != unassignedBits);
aoqi@0 122 break;
aoqi@0 123 }
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 protected void sizeTo(int len) {
aoqi@0 127 if (bits.length < len) {
aoqi@0 128 bits = Arrays.copyOf(bits, len);
aoqi@0 129 }
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 /** This set = {}.
aoqi@0 133 */
aoqi@0 134 public void clear() {
aoqi@0 135 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 136 for (int i = 0; i < bits.length; i++) {
aoqi@0 137 bits[i] = 0;
aoqi@0 138 }
aoqi@0 139 currentState = BitsState.NORMAL;
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public void reset() {
aoqi@0 143 internalReset();
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 protected void internalReset() {
aoqi@0 147 bits = null;
aoqi@0 148 currentState = BitsState.UNKNOWN;
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 public boolean isReset() {
aoqi@0 152 return currentState == BitsState.UNKNOWN;
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 public Bits assign(Bits someBits) {
aoqi@0 156 bits = someBits.dup().bits;
aoqi@0 157 currentState = BitsState.NORMAL;
aoqi@0 158 return this;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 /** Return a copy of this set.
aoqi@0 162 */
aoqi@0 163 public Bits dup() {
aoqi@0 164 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 165 Bits tmp = new Bits();
aoqi@0 166 tmp.bits = dupBits();
aoqi@0 167 currentState = BitsState.NORMAL;
aoqi@0 168 return tmp;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 protected int[] dupBits() {
aoqi@0 172 int [] result;
aoqi@0 173 if (currentState != BitsState.NORMAL) {
aoqi@0 174 result = bits;
aoqi@0 175 } else {
aoqi@0 176 result = new int[bits.length];
aoqi@0 177 System.arraycopy(bits, 0, result, 0, bits.length);
aoqi@0 178 }
aoqi@0 179 return result;
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 /** Include x in this set.
aoqi@0 183 */
aoqi@0 184 public void incl(int x) {
aoqi@0 185 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 186 Assert.check(x >= 0, "Value of x " + x);
aoqi@0 187 sizeTo((x >>> wordshift) + 1);
aoqi@0 188 bits[x >>> wordshift] = bits[x >>> wordshift] |
aoqi@0 189 (1 << (x & wordmask));
aoqi@0 190 currentState = BitsState.NORMAL;
aoqi@0 191 }
aoqi@0 192
aoqi@0 193
aoqi@0 194 /** Include [start..limit) in this set.
aoqi@0 195 */
aoqi@0 196 public void inclRange(int start, int limit) {
aoqi@0 197 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 198 sizeTo((limit >>> wordshift) + 1);
aoqi@0 199 for (int x = start; x < limit; x++) {
aoqi@0 200 bits[x >>> wordshift] = bits[x >>> wordshift] |
aoqi@0 201 (1 << (x & wordmask));
aoqi@0 202 }
aoqi@0 203 currentState = BitsState.NORMAL;
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 /** Exclude [start...end] from this set.
aoqi@0 207 */
aoqi@0 208 public void excludeFrom(int start) {
aoqi@0 209 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 210 Bits temp = new Bits();
aoqi@0 211 temp.sizeTo(bits.length);
aoqi@0 212 temp.inclRange(0, start);
aoqi@0 213 internalAndSet(temp);
aoqi@0 214 currentState = BitsState.NORMAL;
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 /** Exclude x from this set.
aoqi@0 218 */
aoqi@0 219 public void excl(int x) {
aoqi@0 220 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 221 Assert.check(x >= 0);
aoqi@0 222 sizeTo((x >>> wordshift) + 1);
aoqi@0 223 bits[x >>> wordshift] = bits[x >>> wordshift] &
aoqi@0 224 ~(1 << (x & wordmask));
aoqi@0 225 currentState = BitsState.NORMAL;
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 /** Is x an element of this set?
aoqi@0 229 */
aoqi@0 230 public boolean isMember(int x) {
aoqi@0 231 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 232 return
aoqi@0 233 0 <= x && x < (bits.length << wordshift) &&
aoqi@0 234 (bits[x >>> wordshift] & (1 << (x & wordmask))) != 0;
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 /** {@literal this set = this set & xs}.
aoqi@0 238 */
aoqi@0 239 public Bits andSet(Bits xs) {
aoqi@0 240 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 241 internalAndSet(xs);
aoqi@0 242 currentState = BitsState.NORMAL;
aoqi@0 243 return this;
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 protected void internalAndSet(Bits xs) {
aoqi@0 247 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 248 sizeTo(xs.bits.length);
aoqi@0 249 for (int i = 0; i < xs.bits.length; i++) {
aoqi@0 250 bits[i] = bits[i] & xs.bits[i];
aoqi@0 251 }
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 /** this set = this set | xs.
aoqi@0 255 */
aoqi@0 256 public Bits orSet(Bits xs) {
aoqi@0 257 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 258 sizeTo(xs.bits.length);
aoqi@0 259 for (int i = 0; i < xs.bits.length; i++) {
aoqi@0 260 bits[i] = bits[i] | xs.bits[i];
aoqi@0 261 }
aoqi@0 262 currentState = BitsState.NORMAL;
aoqi@0 263 return this;
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 /** this set = this set \ xs.
aoqi@0 267 */
aoqi@0 268 public Bits diffSet(Bits xs) {
aoqi@0 269 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 270 for (int i = 0; i < bits.length; i++) {
aoqi@0 271 if (i < xs.bits.length) {
aoqi@0 272 bits[i] = bits[i] & ~xs.bits[i];
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275 currentState = BitsState.NORMAL;
aoqi@0 276 return this;
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 /** this set = this set ^ xs.
aoqi@0 280 */
aoqi@0 281 public Bits xorSet(Bits xs) {
aoqi@0 282 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 283 sizeTo(xs.bits.length);
aoqi@0 284 for (int i = 0; i < xs.bits.length; i++) {
aoqi@0 285 bits[i] = bits[i] ^ xs.bits[i];
aoqi@0 286 }
aoqi@0 287 currentState = BitsState.NORMAL;
aoqi@0 288 return this;
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 /** Count trailing zero bits in an int. Algorithm from "Hacker's
aoqi@0 292 * Delight" by Henry S. Warren Jr. (figure 5-13)
aoqi@0 293 */
aoqi@0 294 private static int trailingZeroBits(int x) {
aoqi@0 295 Assert.check(wordlen == 32);
aoqi@0 296 if (x == 0) {
aoqi@0 297 return 32;
aoqi@0 298 }
aoqi@0 299 int n = 1;
aoqi@0 300 if ((x & 0xffff) == 0) { n += 16; x >>>= 16; }
aoqi@0 301 if ((x & 0x00ff) == 0) { n += 8; x >>>= 8; }
aoqi@0 302 if ((x & 0x000f) == 0) { n += 4; x >>>= 4; }
aoqi@0 303 if ((x & 0x0003) == 0) { n += 2; x >>>= 2; }
aoqi@0 304 return n - (x&1);
aoqi@0 305 }
aoqi@0 306
aoqi@0 307 /** Return the index of the least bit position &ge; x that is set.
aoqi@0 308 * If none are set, returns -1. This provides a nice way to iterate
aoqi@0 309 * over the members of a bit set:
aoqi@0 310 * <pre>{@code
aoqi@0 311 * for (int i = bits.nextBit(0); i>=0; i = bits.nextBit(i+1)) ...
aoqi@0 312 * }</pre>
aoqi@0 313 */
aoqi@0 314 public int nextBit(int x) {
aoqi@0 315 Assert.check(currentState != BitsState.UNKNOWN);
aoqi@0 316 int windex = x >>> wordshift;
aoqi@0 317 if (windex >= bits.length) {
aoqi@0 318 return -1;
aoqi@0 319 }
aoqi@0 320 int word = bits[windex] & ~((1 << (x & wordmask))-1);
aoqi@0 321 while (true) {
aoqi@0 322 if (word != 0) {
aoqi@0 323 return (windex << wordshift) + trailingZeroBits(word);
aoqi@0 324 }
aoqi@0 325 windex++;
aoqi@0 326 if (windex >= bits.length) {
aoqi@0 327 return -1;
aoqi@0 328 }
aoqi@0 329 word = bits[windex];
aoqi@0 330 }
aoqi@0 331 }
aoqi@0 332
aoqi@0 333 /** a string representation of this set.
aoqi@0 334 */
aoqi@0 335 @Override
aoqi@0 336 public String toString() {
aoqi@0 337 if (bits != null && bits.length > 0) {
aoqi@0 338 char[] digits = new char[bits.length * wordlen];
aoqi@0 339 for (int i = 0; i < bits.length * wordlen; i++) {
aoqi@0 340 digits[i] = isMember(i) ? '1' : '0';
aoqi@0 341 }
aoqi@0 342 return new String(digits);
aoqi@0 343 } else {
aoqi@0 344 return "[]";
aoqi@0 345 }
aoqi@0 346 }
aoqi@0 347
aoqi@0 348 /** Test Bits.nextBit(int). */
aoqi@0 349 public static void main(String[] args) {
aoqi@0 350 java.util.Random r = new java.util.Random();
aoqi@0 351 Bits bits = new Bits();
aoqi@0 352 for (int i=0; i<125; i++) {
aoqi@0 353 int k;
aoqi@0 354 do {
aoqi@0 355 k = r.nextInt(250);
aoqi@0 356 } while (bits.isMember(k));
aoqi@0 357 System.out.println("adding " + k);
aoqi@0 358 bits.incl(k);
aoqi@0 359 }
aoqi@0 360 int count = 0;
aoqi@0 361 for (int i = bits.nextBit(0); i >= 0; i = bits.nextBit(i+1)) {
aoqi@0 362 System.out.println("found " + i);
aoqi@0 363 count ++;
aoqi@0 364 }
aoqi@0 365 if (count != 125) {
aoqi@0 366 throw new Error();
aoqi@0 367 }
aoqi@0 368 }
aoqi@0 369 }

mercurial