return (bits[word] & (1L << (index & 0x3F))) != 0L;\r
}\r
\r
+ /** Returns the bit at the given index and clears it in one go.\r
+ * @param index the index of the bit\r
+ * @return whether the bit was set before invocation\r
+ * @throws ArrayIndexOutOfBoundsException if index < 0 */\r
+ public boolean getAndClear (int index) {\r
+ final int word = index >>> 6;\r
+ if (word >= bits.length) return false;\r
+ long oldBits = bits[word];\r
+ bits[word] &= ~(1L << (index & 0x3F));\r
+ return bits[word] != oldBits;\r
+ }\r
+\r
+ /** Returns the bit at the given index and sets it in one go.\r
+ * @param index the index of the bit\r
+ * @return whether the bit was set before invocation\r
+ * @throws ArrayIndexOutOfBoundsException if index < 0 */\r
+ public boolean getAndSet (int index) {\r
+ final int word = index >>> 6;\r
+ checkCapacity(word);\r
+ long oldBits = bits[word];\r
+ bits[word] |= 1L << (index & 0x3F);\r
+ return bits[word] == oldBits;\r
+ }\r
+\r
/** @param index the index of the bit to set\r
* @throws ArrayIndexOutOfBoundsException if index < 0 */\r
public void set (int index) {\r
\r
/** Clears the entire bitset */\r
public void clear () {\r
+ long[] bits = this.bits;\r
int length = bits.length;\r
for (int i = 0; i < length; i++) {\r
bits[i] = 0L;\r
* \r
* @return the logical size of this bitset */\r
public int length () {\r
+ long[] bits = this.bits;\r
for (int word = bits.length - 1; word >= 0; --word) {\r
- if (bits[word] != 0) {\r
+ long bitsAtWord = bits[word]; \r
+ if (bitsAtWord != 0) {\r
for (int bit = 63; bit >= 0; --bit) {\r
- if ((bits[word] & (1L << (bit & 0x3F))) != 0L) {\r
+ if ((bitsAtWord & (1L << (bit & 0x3F))) != 0L) {\r
return (word << 6) + bit;\r
}\r
}\r
\r
/** @return true if this bitset contains no bits that are set to true */\r
public boolean isEmpty () {\r
+ long[] bits = this.bits;\r
int length = bits.length;\r
for (int i = 0; i < length; i++) {\r
if (bits[i] != 0L) {\r
/** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. If no such bit\r
* exists then -1 is returned. */\r
public int nextSetBit (int fromIndex) {\r
+ long[] bits = this.bits;\r
int word = fromIndex >>> 6;\r
- if (word >= bits.length) return -1;\r
+ int bitsLength = bits.length;\r
+ if (word >= bitsLength) return -1;\r
+ long bitsAtWord = bits[word];\r
for (int i = fromIndex & 0x3f; i < 64; i++) {\r
- if ((bits[word] & (1L << (i & 0x3F))) != 0L) {\r
+ if ((bitsAtWord & (1L << (i & 0x3F))) != 0L) {\r
return (word << 6) + i;\r
}\r
}\r
- for (word++; word < bits.length; word++) {\r
+ for (word++; word < bitsLength; word++) {\r
if (word != 0) {\r
+ bitsAtWord = bits[word];\r
for (int i = 0; i < 64; i++) {\r
- if ((bits[word] & (1L << (i & 0x3F))) != 0L) {\r
+ if ((bitsAtWord & (1L << (i & 0x3F))) != 0L) {\r
return (word << 6) + i;\r
}\r
}\r
/** Returns the index of the first bit that is set to false that occurs on or after the specified starting index. If no such bit\r
* exists then -1 is returned. */\r
public int nextClearBit (int fromIndex) {\r
+ long[] bits = this.bits;\r
int word = fromIndex >>> 6;\r
- if (word >= bits.length) return -1;\r
+ int bitsLength = bits.length;\r
+ if (word >= bitsLength) return -1;\r
+ long bitsAtWord = bits[word];\r
for (int i = fromIndex & 0x3f; i < 64; i++) {\r
- if ((bits[word] & (1L << (i & 0x3F))) == 0L) {\r
+ if ((bitsAtWord & (1L << (i & 0x3F))) == 0L) {\r
return (word << 6) + i;\r
}\r
}\r
- for (word++; word < bits.length; word++) {\r
+ for (word++; word < bitsLength; word++) {\r
if (word == 0) {\r
return word << 6;\r
}\r
+ bitsAtWord = bits[word];\r
for (int i = 0; i < 64; i++) {\r
- if ((bits[word] & (1L << (i & 0x3F))) == 0L) {\r
+ if ((bitsAtWord & (1L << (i & 0x3F))) == 0L) {\r
return (word << 6) + i;\r
}\r
}\r
* @return boolean indicating whether this bit set intersects the specified bit set\r
*/\r
public boolean intersects (Bits other) {\r
- for (int i = 0, j = bits.length, k = other.bits.length; i < j && i < k; i++) {\r
- if ((bits[i] & other.bits[i]) != 0) {\r
+ long[] bits = this.bits;\r
+ long[] otherBits = other.bits;\r
+ for (int i = Math.min(bits.length, otherBits.length) - 1; i >= 0; i--) {\r
+ if ((bits[i] & otherBits[i]) != 0) {\r
return true;\r
}\r
}\r
return false;\r
}\r
\r
+ /** Returns true if this bit set is a super set of the specified set, i.e. it has all bits set to true that are also set to true\r
+ * in the specified BitSet.\r
+ * \r
+ * @param other a bit set\r
+ * @return boolean indicating whether this bit set is a super set of the specified set */\r
+ public boolean containsAll (Bits other) {\r
+ long[] bits = this.bits;\r
+ long[] otherBits = other.bits;\r
+ int otherBitsLength = otherBits.length;\r
+ int bitsLength = bits.length;\r
+\r
+ for (int i = bitsLength; i < otherBitsLength; i++) {\r
+ if (otherBits[i] != 0) {\r
+ return false;\r
+ }\r
+ }\r
+ for (int i = Math.min(bitsLength, otherBitsLength) - 1; i >= 0; i--) {\r
+ if ((bits[i] & otherBits[i]) != otherBits[i]) {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
}
\ No newline at end of file