1 package antlr.collections.impl; 2 3 9 10 import antlr.CharFormatter; 11 12 27 public class BitSet implements Cloneable { 28 protected final static int BITS = 64; protected final static int NIBBLE = 4; 30 protected final static int LOG_BITS = 6; 32 37 protected final static int MOD_MASK = BITS - 1; 38 39 40 protected long bits[]; 41 42 43 public BitSet() { 44 this(BITS); 45 } 46 47 48 public BitSet(long[] bits_) { 49 bits = bits_; 50 } 51 52 55 public BitSet(int nbits) { 56 bits = new long[((nbits - 1) >> LOG_BITS) + 1]; 57 } 58 59 60 public void add(int el) { 61 int n = wordNumber(el); 63 if (n >= bits.length) { 66 growToInclude(el); 67 } 68 bits[n] |= bitMask(el); 69 } 70 71 public BitSet and(BitSet a) { 72 BitSet s = (BitSet)this.clone(); 73 s.andInPlace(a); 74 return s; 75 } 76 77 public void andInPlace(BitSet a) { 78 int min = Math.min(bits.length, a.bits.length); 79 for (int i = min - 1; i >= 0; i--) { 80 bits[i] &= a.bits[i]; 81 } 82 for (int i = min; i < bits.length; i++) { 84 bits[i] = 0; 85 } 86 } 87 88 private final static long bitMask(int bitNumber) { 89 int bitPosition = bitNumber & MOD_MASK; return 1L << bitPosition; 91 } 92 93 public void clear() { 94 for (int i = bits.length - 1; i >= 0; i--) { 95 bits[i] = 0; 96 } 97 } 98 99 public void clear(int el) { 100 int n = wordNumber(el); 101 if (n >= bits.length) { growToInclude(el); 103 } 104 bits[n] &= ~bitMask(el); 105 } 106 107 public Object clone() { 108 BitSet s; 109 try { 110 s = (BitSet)super.clone(); 111 s.bits = new long[bits.length]; 112 System.arraycopy(bits, 0, s.bits, 0, bits.length); 113 } 114 catch (CloneNotSupportedException e) { 115 throw new InternalError (); 116 } 117 return s; 118 } 119 120 public int degree() { 121 int deg = 0; 122 for (int i = bits.length - 1; i >= 0; i--) { 123 long word = bits[i]; 124 if (word != 0L) { 125 for (int bit = BITS - 1; bit >= 0; bit--) { 126 if ((word & (1L << bit)) != 0) { 127 deg++; 128 } 129 } 130 } 131 } 132 return deg; 133 } 134 135 136 public boolean equals(Object obj) { 137 if ((obj != null) && (obj instanceof BitSet)) { 138 BitSet set = (BitSet)obj; 139 140 int n = Math.min(bits.length, set.bits.length); 141 for (int i = n; i-- > 0;) { 142 if (bits[i] != set.bits[i]) { 143 return false; 144 } 145 } 146 if (bits.length > n) { 147 for (int i = bits.length; i-- > n;) { 148 if (bits[i] != 0) { 149 return false; 150 } 151 } 152 } 153 else if (set.bits.length > n) { 154 for (int i = set.bits.length; i-- > n;) { 155 if (set.bits[i] != 0) { 156 return false; 157 } 158 } 159 } 160 return true; 161 } 162 return false; 163 } 164 165 169 public static Vector getRanges(int[] elems) { 170 if (elems.length == 0) { 171 return null; 172 } 173 int begin = elems[0]; 174 int end = elems[elems.length - 1]; 175 if (elems.length <= 2) { 176 return null; 178 } 179 180 Vector ranges = new Vector(5); 181 for (int i = 0; i < elems.length - 2; i++) { 183 int lastInRange; 184 lastInRange = elems.length - 1; 185 for (int j = i + 1; j < elems.length; j++) { 186 if (elems[j] != elems[j - 1] + 1) { 187 lastInRange = j - 1; 188 break; 189 } 190 } 191 if (lastInRange - i > 2) { 193 ranges.appendElement(new IntRange(elems[i], elems[lastInRange])); 194 } 195 } 196 return ranges; 197 } 198 199 203 public void growToInclude(int bit) { 204 int newSize = Math.max(bits.length << 1, numWordsToHold(bit)); 205 long newbits[] = new long[newSize]; 206 System.arraycopy(bits, 0, newbits, 0, bits.length); 207 bits = newbits; 208 } 209 210 public boolean member(int el) { 211 int n = wordNumber(el); 212 if (n >= bits.length) return false; 213 return (bits[n] & bitMask(el)) != 0; 214 } 215 216 public boolean nil() { 217 for (int i = bits.length - 1; i >= 0; i--) { 218 if (bits[i] != 0) return false; 219 } 220 return true; 221 } 222 223 public BitSet not() { 224 BitSet s = (BitSet)this.clone(); 225 s.notInPlace(); 226 return s; 227 } 228 229 public void notInPlace() { 230 for (int i = bits.length - 1; i >= 0; i--) { 231 bits[i] = ~bits[i]; 232 } 233 } 234 235 236 public void notInPlace(int maxBit) { 237 notInPlace(0, maxBit); 238 } 239 240 241 public void notInPlace(int minBit, int maxBit) { 242 growToInclude(maxBit); 244 for (int i = minBit; i <= maxBit; i++) { 245 int n = wordNumber(i); 246 bits[n] ^= bitMask(i); 247 } 248 } 249 250 private final int numWordsToHold(int el) { 251 return (el >> LOG_BITS) + 1; 252 } 253 254 public static BitSet of(int el) { 255 BitSet s = new BitSet(el + 1); 256 s.add(el); 257 return s; 258 } 259 260 261 public BitSet or(BitSet a) { 262 BitSet s = (BitSet)this.clone(); 263 s.orInPlace(a); 264 return s; 265 } 266 267 public void orInPlace(BitSet a) { 268 if (a.bits.length > bits.length) { 270 setSize(a.bits.length); 271 } 272 int min = Math.min(bits.length, a.bits.length); 273 for (int i = min - 1; i >= 0; i--) { 274 bits[i] |= a.bits[i]; 275 } 276 } 277 278 public void remove(int el) { 280 int n = wordNumber(el); 281 if (n >= bits.length) { 282 growToInclude(el); 283 } 284 bits[n] &= ~bitMask(el); 285 } 286 287 291 private void setSize(int nwords) { 292 long newbits[] = new long[nwords]; 293 int n = Math.min(nwords, bits.length); 294 System.arraycopy(bits, 0, newbits, 0, n); 295 bits = newbits; 296 } 297 298 public int size() { 299 return bits.length << LOG_BITS; } 301 302 305 public int lengthInLongWords() { 306 return bits.length; 307 } 308 309 310 public boolean subset(BitSet a) { 311 if (a == null || !(a instanceof BitSet)) return false; 312 return this.and(a).equals(this); 313 } 314 315 318 public void subtractInPlace(BitSet a) { 319 if (a == null) return; 320 for (int i = 0; i < bits.length && i < a.bits.length; i++) { 322 bits[i] &= ~a.bits[i]; 323 } 324 } 325 326 public int[] toArray() { 327 int[] elems = new int[degree()]; 328 int en = 0; 329 for (int i = 0; i < (bits.length << LOG_BITS); i++) { 330 if (member(i)) { 331 elems[en++] = i; 332 } 333 } 334 return elems; 335 } 336 337 public long[] toPackedArray() { 338 return bits; 339 } 340 341 public String toString() { 342 return toString(","); 343 } 344 345 349 public String toString(String separator) { 350 String str = ""; 351 for (int i = 0; i < (bits.length << LOG_BITS); i++) { 352 if (member(i)) { 353 if (str.length() > 0) { 354 str += separator; 355 } 356 str = str + i; 357 } 358 } 359 return str; 360 } 361 362 367 public String toString(String separator, CharFormatter formatter) { 368 String str = ""; 369 370 for (int i = 0; i < (bits.length << LOG_BITS); i++) { 371 if (member(i)) { 372 if (str.length() > 0) { 373 str += separator; 374 } 375 str = str + formatter.literalChar(i); 376 } 377 } 378 return str; 379 } 380 381 387 public String toString(String separator, Vector vocabulary) { 388 if (vocabulary == null) { 389 return toString(separator); 390 } 391 String str = ""; 392 for (int i = 0; i < (bits.length << LOG_BITS); i++) { 393 if (member(i)) { 394 if (str.length() > 0) { 395 str += separator; 396 } 397 if (i >= vocabulary.size()) { 398 str += "<bad element " + i + ">"; 399 } 400 else if (vocabulary.elementAt(i) == null) { 401 str += "<" + i + ">"; 402 } 403 else { 404 str += (String )vocabulary.elementAt(i); 405 } 406 } 407 } 408 return str; 409 } 410 411 416 public String toStringOfHalfWords() { 417 String s = new String (); 418 for (int i = 0; i < bits.length; i++) { 419 if (i != 0) s += ", "; 420 long tmp = bits[i]; 421 tmp &= 0xFFFFFFFFL; 422 s += (tmp + "UL"); 423 s += ", "; 424 tmp = bits[i] >>> 32; 425 tmp &= 0xFFFFFFFFL; 426 s += (tmp + "UL"); 427 } 428 return s; 429 } 430 431 435 public String toStringOfWords() { 436 String s = new String (); 437 for (int i = 0; i < bits.length; i++) { 438 if (i != 0) s += ", "; 439 s += (bits[i] + "L"); 440 } 441 return s; 442 } 443 444 445 public String toStringWithRanges(String separator, CharFormatter formatter) { 446 String str = ""; 447 int[] elems = this.toArray(); 448 if (elems.length == 0) { 449 return ""; 450 } 451 int i = 0; 453 while (i < elems.length) { 454 int lastInRange; 455 lastInRange = 0; 456 for (int j = i + 1; j < elems.length; j++) { 457 if (elems[j] != elems[j - 1] + 1) { 458 break; 459 } 460 lastInRange = j; 461 } 462 if (str.length() > 0) { 464 str += separator; 465 } 466 if (lastInRange - i >= 2) { 467 str += formatter.literalChar(elems[i]); 468 str += ".."; 469 str += formatter.literalChar(elems[lastInRange]); 470 i = lastInRange; } 472 else { str += formatter.literalChar(elems[i]); 474 } 475 i++; 476 } 477 return str; 478 } 479 480 private final static int wordNumber(int bit) { 481 return bit >> LOG_BITS; } 483 } 484 | Popular Tags |