Skip to content

Commit b51dcaf

Browse files
committed
Add unit test in BitSetSuite for BitSet#anySet()
1 parent 83f9f87 commit b51dcaf

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

unsafe/src/main/java/org/apache/spark/unsafe/bitset/BitSet.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,12 @@ public boolean isSet(int index) {
102102
public int nextSetBit(int fromIndex) {
103103
return BitSetMethods.nextSetBit(baseObject, baseOffset, fromIndex, numWords);
104104
}
105+
106+
/*
107+
* @return whether any bit in the BitSet is set
108+
*/
109+
public boolean anySet() {
110+
return BitSetMethods.anySet(baseObject, baseOffset, numWords*BitSetMethods.WORD_SIZE);
111+
}
112+
105113
}

unsafe/src/main/java/org/apache/spark/unsafe/bitset/BitSetMethods.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@
2828
*/
2929
public final class BitSetMethods {
3030

31-
private static final long WORD_SIZE = 8;
32-
private static final int SIZE_OF_LONG = Long.SIZE;
31+
static final long WORD_SIZE = 8;
3332

3433
private BitSetMethods() {
3534
// Make the default constructor private, since this only holds static methods.
@@ -72,10 +71,10 @@ public static boolean isSet(Object baseObject, long baseOffset, int index) {
7271
* Returns {@code true} if any bit is set.
7372
*/
7473
public static boolean anySet(Object baseObject, long baseOffset, long bitSetWidthInBytes) {
75-
assert bitSetWidthInBytes % SIZE_OF_LONG == 0;
76-
int widthInLong = (int)(bitSetWidthInBytes / SIZE_OF_LONG);
74+
assert bitSetWidthInBytes % WORD_SIZE == 0;
75+
int widthInLong = (int)(bitSetWidthInBytes / WORD_SIZE);
7776
long addr = baseOffset;
78-
for (int i = 0; i <= widthInLong; i++, addr += SIZE_OF_LONG) {
77+
for (int i = 0; i <= widthInLong; i++, addr += WORD_SIZE) {
7978
if (PlatformDependent.UNSAFE.getLong(baseObject, addr) != 0) {
8079
return true;
8180
}

unsafe/src/test/java/org/apache/spark/unsafe/bitset/BitSetSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void basicOps() {
3939
for (int i = 0; i < bs.capacity(); i++) {
4040
Assert.assertFalse(bs.isSet(i));
4141
}
42+
Assert.assertFalse(bs.anySet());
4243

4344
// Set every bit and check it.
4445
for (int i = 0; i < bs.capacity(); i++) {

0 commit comments

Comments
 (0)