Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public boolean isSet(int index) {
public int nextSetBit(int fromIndex) {
return BitSetMethods.nextSetBit(baseObject, baseOffset, fromIndex, numWords);
}

/*
* @return whether any bit in the BitSet is set
*/
public boolean anySet() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should match BitSetMethods's description:

/**
    * Returns {@code true} if any bit is set.
    */

return BitSetMethods.anySet(baseObject, baseOffset, numWords);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public final class BitSetMethods {

private static final long WORD_SIZE = 8;
static final long WORD_SIZE = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remain private.


private BitSetMethods() {
// Make the default constructor private, since this only holds static methods.
Expand Down Expand Up @@ -70,9 +70,10 @@ public static boolean isSet(Object baseObject, long baseOffset, int index) {
/**
* Returns {@code true} if any bit is set.
*/
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) {
for (int i = 0; i <= bitSetWidthInBytes; i++) {
if (PlatformDependent.UNSAFE.getByte(baseObject, baseOffset + i) != 0) {
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInWords) {
long addr = baseOffset;
for (int i = 0; i < bitSetWidthInWords; i++, addr += WORD_SIZE) {
if (PlatformDependent.UNSAFE.getLong(baseObject, addr) != 0) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void basicOps() {
for (int i = 0; i < bs.capacity(); i++) {
Assert.assertFalse(bs.isSet(i));
}
// another form of asserting that the bit set is empty
Assert.assertFalse(bs.anySet());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think that we should write a proper unit test for anySet that will create a new bitset spanning multiple words, then set a single in each word, then check that anySet() returns true. This test isn't sufficient because it wouldn't have caught any of the bugs in earlier iterations of this PR.


// Set every bit and check it.
for (int i = 0; i < bs.capacity(); i++) {
Expand All @@ -52,6 +54,11 @@ public void basicOps() {
bs.unset(i);
Assert.assertFalse(bs.isSet(i));
}

// Make sure anySet() can detect any set bit
bs = createBitSet(256);
bs.set(64);
Assert.assertTrue(bs.anySet());
}

@Test
Expand Down