📄 bitset.java
字号:
return this.complement(); } return set.subtract(this); } public void notInPlace() { for (int i = bits.length - 1; i >= 0; i--) { bits[i] = ~bits[i]; } } /** complement bits in the range 0..maxBit. */ public void notInPlace(int maxBit) { notInPlace(0, maxBit); } /** complement bits in the range minBit..maxBit.*/ public void notInPlace(int minBit, int maxBit) { // make sure that we have room for maxBit growToInclude(maxBit); for (int i = minBit; i <= maxBit; i++) { int n = wordNumber(i); bits[n] ^= bitMask(i); } } private final int numWordsToHold(int el) { return (el >> LOG_BITS) + 1; } public static BitSet of(int el) { BitSet s = new BitSet(el + 1); s.add(el); return s; } public static BitSet of(Collection elements) { BitSet s = new BitSet(); Iterator iter = elements.iterator(); while (iter.hasNext()) { Integer el = (Integer) iter.next(); s.add(el.intValue()); } return s; } public static BitSet of(IntSet set) { if ( set==null ) { return null; } if ( set instanceof BitSet ) { return (BitSet)set; } if ( set instanceof IntervalSet ) { BitSet s = new BitSet(); s.addAll(set); return s; } throw new IllegalArgumentException("can't create BitSet from "+set.getClass().getName()); } public static BitSet of(Map elements) { return BitSet.of(elements.keySet()); } public static BitSet range(int a, int b) { BitSet s = new BitSet(b + 1); for (int i = a; i <= b; i++) { int n = wordNumber(i); s.bits[n] |= bitMask(i); } return s; } /** return this | a in a new set */ public IntSet or(IntSet a) { if ( a==null ) { return this; } BitSet s = (BitSet)this.clone(); s.orInPlace((BitSet)a); return s; } public void orInPlace(BitSet a) { if ( a==null ) { return; } // If this is smaller than a, grow this first if (a.bits.length > bits.length) { setSize(a.bits.length); } int min = Math.min(bits.length, a.bits.length); for (int i = min - 1; i >= 0; i--) { bits[i] |= a.bits[i]; } } // remove this element from this set public void remove(int el) { int n = wordNumber(el); if (n >= bits.length) { growToInclude(el); } bits[n] &= ~bitMask(el); } /** * Sets the size of a set. * @param nwords how many words the new set should be */ private void setSize(int nwords) { long newbits[] = new long[nwords]; int n = Math.min(nwords, bits.length); System.arraycopy(bits, 0, newbits, 0, n); bits = newbits; } public int numBits() { return bits.length << LOG_BITS; // num words * bits per word } /** return how much space is being used by the bits array not * how many actually have member bits on. */ public int lengthInLongWords() { return bits.length; } /**Is this contained within a? */ public boolean subset(BitSet a) { if (a == null) return false; return this.and(a).equals(this); } /**Subtract the elements of 'a' from 'this' in-place. * Basically, just turn off all bits of 'this' that are in 'a'. */ public void subtractInPlace(BitSet a) { if (a == null) return; // for all words of 'a', turn off corresponding bits of 'this' for (int i = 0; i < bits.length && i < a.bits.length; i++) { bits[i] &= ~a.bits[i]; } } public IntSet subtract(IntSet a) { if (a == null || !(a instanceof BitSet)) return null; BitSet s = (BitSet)this.clone(); s.subtractInPlace((BitSet)a); return s; } public List toList() { throw new NoSuchMethodError("BitSet.toList() unimplemented"); } public int[] toArray() { int[] elems = new int[size()]; int en = 0; for (int i = 0; i < (bits.length << LOG_BITS); i++) { if (member(i)) { elems[en++] = i; } } return elems; } public long[] toPackedArray() { return bits; } public String toString() { return toString(null); } /** Transform a bit set into a string by formatting each element as an integer * separator The string to put in between elements * @return A commma-separated list of values */ public String toString(Grammar g) { StringBuffer buf = new StringBuffer(); String separator = ","; boolean havePrintedAnElement = false; buf.append('{'); for (int i = 0; i < (bits.length << LOG_BITS); i++) { if (member(i)) { if (i > 0 && havePrintedAnElement ) { buf.append(separator); } if ( g!=null ) { buf.append(g.getTokenDisplayName(i)); } else { buf.append(i); } havePrintedAnElement = true; } } buf.append('}'); return buf.toString(); } /**Create a string representation where instead of integer elements, the * ith element of vocabulary is displayed instead. Vocabulary is a Vector * of Strings. * separator The string to put in between elements * @return A commma-separated list of character constants. */ public String toString(String separator, List vocabulary) { if (vocabulary == null) { return toString(null); } String str = ""; for (int i = 0; i < (bits.length << LOG_BITS); i++) { if (member(i)) { if (str.length() > 0) { str += separator; } if (i >= vocabulary.size()) { str += "'" + (char)i + "'"; } else if (vocabulary.get(i) == null) { str += "'" + (char)i + "'"; } else { str += (String)vocabulary.get(i); } } } return str; } /** * Dump a comma-separated list of the words making up the bit set. * Split each 64 bit number into two more manageable 32 bit numbers. * This generates a comma-separated list of C++-like unsigned long constants. */ public String toStringOfHalfWords() { StringBuffer s = new StringBuffer(); for (int i = 0; i < bits.length; i++) { if (i != 0) s.append(", "); long tmp = bits[i]; tmp &= 0xFFFFFFFFL; s.append(tmp); s.append("UL"); s.append(", "); tmp = bits[i] >>> 32; tmp &= 0xFFFFFFFFL; s.append(tmp); s.append("UL"); } return s.toString(); } /** * Dump a comma-separated list of the words making up the bit set. * This generates a comma-separated list of Java-like long int constants. */ public String toStringOfWords() { StringBuffer s = new StringBuffer(); for (int i = 0; i < bits.length; i++) { if (i != 0) s.append(", "); s.append(bits[i]); s.append("L"); } return s.toString(); } public String toStringWithRanges() { return toString(); } private final static int wordNumber(int bit) { return bit >> LOG_BITS; // bit / BITS }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -