Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ boolean isEmpty() {

abstract void setBit(final long index);

// Word index of a bit. Shift before the narrowing cast: `(int) index >>> 6` casts first, so every
// index at or above 2^32 collapses into the low 32 bits and aliases a different bit.
static int wordIndex(final long bitIndex) {
return (int) (bitIndex >>> 6);
}

// Byte index of a bit. Kept as a long so an index at or above 2^32 does not wrap to byte 0.
static long byteOffset(final long bitIndex) {
return bitIndex >>> 3;
}

abstract long getNumBitsSet();

abstract void reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected boolean isDirty() {

@Override
boolean getBit(final long index) { //index a bit in an array of bytes
return (wseg_.get(JAVA_BYTE, DATA_OFFSET + ((int) index >>> 3)) & (1 << (index & 0x7))) != 0;
return (wseg_.get(JAVA_BYTE, DATA_OFFSET + byteOffset(index)) & (1 << (index & 0x7))) != 0;
}

@Override
Expand All @@ -130,15 +130,15 @@ void reset() {

@Override
void setBit(final long index) { //index a bit in an array of bytes
final long segmentOffset = DATA_OFFSET + ((int) index >>> 3);
final long segmentOffset = DATA_OFFSET + byteOffset(index);
final byte val = wseg_.get(JAVA_BYTE, segmentOffset);
setBits(wseg_, segmentOffset, (byte) (val | (1 << (index & 0x07))));
setNumBitsSet(-1); // mark dirty
}

@Override
boolean getAndSetBit(final long index) { //index a bit in an array of bytes
final long segmentOffset = DATA_OFFSET + ((int) index >>> 3);
final long segmentOffset = DATA_OFFSET + byteOffset(index);
final byte mask = (byte) (1 << (index & 0x07));
final byte val = wseg_.get(JAVA_BYTE, segmentOffset);
if ((val & mask) != 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int getArrayLength() {
@Override
boolean getBit(final long index) { //index into an array of bytes
if (isEmpty()) { return false; }
return (wseg_.get(JAVA_BYTE, DATA_OFFSET + ((int) index >>> 3)) & (1 << (index & 0x7))) != 0;
return (wseg_.get(JAVA_BYTE, DATA_OFFSET + byteOffset(index)) & (1 << (index & 0x7))) != 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ public boolean isOffHeap() {
// queries a single bit in the array of longs
@Override
boolean getBit(final long index) {
return ((data_[(int) index >>> 6] & (1L << index)) != 0);
return ((data_[wordIndex(index)] & (1L << index)) != 0);
}

// sets a single bit in the array of longs without querying, meaning the method
// cannot properly track the number of bits set, so set isDirty = true
@Override
void setBit(final long index) {
data_[(int) index >>> 6] |= 1L << index;
data_[wordIndex(index)] |= 1L << index;
isDirty_ = true;
}

// returns existing value of bit in an array of longs
@Override
boolean getAndSetBit(final long index) {
final int offset = (int) index >>> 6;
final int offset = wordIndex(index);
final long mask = 1L << index;
if ((data_[offset] & mask) != 0) {
return true; // already seen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ public void initializeTooSmallTest() {
assertThrows(SketchesArgumentException.class, () -> DirectBitArray.initialize(-5, wseg));
}

@Test
public void bitAtOrAbove2To32DoesNotWrapToTheFirstByte() {
final byte[] bytes = new byte[56];
final MemorySegment wseg = MemorySegment.ofArray(bytes);
final DirectBitArray dba = DirectBitArray.initialize(192, wseg);
assertThrows(IndexOutOfBoundsException.class, () -> dba.getBit(1L << 32));
assertThrows(IndexOutOfBoundsException.class, () -> dba.setBit(1L << 32));
assertThrows(IndexOutOfBoundsException.class, () -> dba.getAndSetBit((1L << 32) + 9));
assertFalse(dba.getBit(0));
assertFalse(dba.getBit(9));
assertEquals(dba.getNumBitsSet(), 0);

dba.setBit(1);
final BitArray readOnly = BitArray.wrap(wseg, false);
assertTrue(readOnly.getBit(1));
assertThrows(IndexOutOfBoundsException.class, () -> readOnly.getBit(1L << 32));
}

@Test
public void basicInitializeOperationsTest() {
final byte[] bytes = new byte[56];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import org.apache.datasketches.common.SketchesArgumentException;
import org.apache.datasketches.common.positional.PositionalSegment;
import org.testng.SkipException;
import org.testng.annotations.Test;

public class HeapBitArrayTest {
Expand Down Expand Up @@ -90,6 +91,42 @@ public void bitAddresOutOfBoundsTest() {
assertThrows(ArrayIndexOutOfBoundsException.class, () -> ba.getAndSetBit(8192));
}

@Test
public void wordIndexKeepsBitsAtOrAbove2To32Distinct() {
assertEquals(BitArray.wordIndex(0), 0);
assertEquals(BitArray.wordIndex(63), 0);
assertEquals(BitArray.wordIndex(64), 1);
// 2^32 bits is word 2^26. The old `(int) index >>> 6` cast first and returned 0.
assertEquals(BitArray.wordIndex(1L << 32), 1 << 26);
assertEquals(BitArray.wordIndex((1L << 32) + 70), (1 << 26) + 1);
assertEquals(BitArray.byteOffset(1L << 32), 1L << 29);
assertEquals(BitArray.byteOffset((1L << 32) + 3), 1L << 29);
assertEquals(BitArray.byteOffset((1L << 32) + 8), (1L << 29) + 1);
}

@Test
public void bitAtOrAbove2To32DoesNotAliasLowBits() {
final HeapBitArray small = new HeapBitArray(128);
assertThrows(ArrayIndexOutOfBoundsException.class, () -> small.getBit(1L << 32));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> small.setBit(1L << 32));
assertThrows(ArrayIndexOutOfBoundsException.class, () -> small.getAndSetBit((1L << 32) + 3));
assertFalse(small.getBit(0));
assertFalse(small.getBit(3));
assertEquals(small.getNumBitsSet(), 0);

// In-range bit past 2^32. The array is 512MiB plus one word; skip when the heap cannot hold it.
if (Runtime.getRuntime().maxMemory() < 1_200_000_000L) {
throw new SkipException("heap is too small to allocate a filter past 2^32 bits");
}
final long numBits = (1L << 32) + 128;
final HeapBitArray large = new HeapBitArray(numBits);
final long high = (1L << 32) + 5;
assertFalse(large.getAndSetBit(high));
assertTrue(large.getBit(high));
assertFalse(large.getBit(5));
assertEquals(large.getNumBitsSet(), 1);
}

@Test
public void inversionTest() {
final int numBits = 1024;
Expand Down