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