Skip to content

Apache Lucene open-source search software

Add method SparseFixedBitSet#set(int startIndex, int endIndex) (#16107)

Ignacio Vera53538823ced8
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index ed1fed222a5..a003c49717e 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -427,6 +427,8 @@ Optimizations
* GITHUB#16097: Implement DocIdSetIterator#intoBitSet in dense implememntatios of the
Lucene90DocValuesProducer. (Ignacio Vera)
+* GITHUB#16107: Add method SparseFixedBitSet#set(int startIndex, int endIndex). (Ignacio Vera)
+
Bug Fixes
---------------------
* GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequent
diff --git a/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java b/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
index e59dc919076..ef32a635d8f 100644
--- a/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
+++ b/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
@@ -214,6 +214,10 @@ public class SparseFixedBitSet extends BitSet {
}
private void insertLong(int i4096, long i64bit, int i, long index) {
+ insertLongValue(i4096, i64bit, 1L << i, index);
+ }
+
+ private void insertLongValue(int i4096, long i64bit, long value, long index) {
indices[i4096] |= i64bit;
// we count the number of bits that are set on the right of i64
// this gives us the index at which to perform the insertion
@@ -223,13 +227,13 @@ public class SparseFixedBitSet extends BitSet {
// since we only store non-zero longs, if the last value is 0, it means
// that we already have extra space, make use of it
System.arraycopy(bitArray, o, bitArray, o + 1, bitArray.length - o - 1);
- bitArray[o] = 1L << i;
+ bitArray[o] = value;
} else {
// we don't have extra space so we need to resize to insert the new long
final int newSize = oversize(bitArray.length + 1);
final long[] newBitArray = new long[newSize];
System.arraycopy(bitArray, 0, newBitArray, 0, o);
- newBitArray[o] = 1L << i;
+ newBitArray[o] = value;
System.arraycopy(bitArray, o, newBitArray, o + 1, bitArray.length - o);
bits[i4096] = newBitArray;
// we may slightly overestimate size here, but keep it cheap
@@ -238,6 +242,32 @@ public class SparseFixedBitSet extends BitSet {
++nonZeroLongCount;
}
+ /**
+ * Sets a range of bits
+ *
+ * @param startIndex lower index
+ * @param endIndex one-past the last bit to set
+ */
+ public void set(int startIndex, int endIndex) {
+ assert startIndex >= 0 && startIndex < length
+ : "startIndex=" + startIndex + ", length=" + length;
+ assert endIndex >= 0 && endIndex <= length : "endIndex=" + endIndex + ", length=" + length;
+ if (endIndex <= startIndex) {
+ return;
+ }
+ final int firstBlock = startIndex >>> 12;
+ final int lastBlock = (endIndex - 1) >>> 12;
+ if (firstBlock == lastBlock) {
+ setWithinBlock(firstBlock, startIndex & MASK_4096, (endIndex - 1) & MASK_4096);
+ } else {
+ setWithinBlock(firstBlock, startIndex & MASK_4096, MASK_4096);
+ for (int i = firstBlock + 1; i < lastBlock; ++i) {
+ setWithinBlock(i, 0, MASK_4096);
+ }
+ setWithinBlock(lastBlock, 0, (endIndex - 1) & MASK_4096);
+ }
+ }
+
/** Clear the bit at index <code>i</code>. */
@Override
public void clear(int i) {
@@ -261,6 +291,26 @@ public class SparseFixedBitSet extends BitSet {
}
}
+ private void orLong(int i4096, int i64, long newBits) {
+ if (newBits == 0) {
+ return;
+ }
+ final long index = indices[i4096];
+ final long i64bit = 1L << i64;
+ if ((index & i64bit) != 0) {
+ final int o = Long.bitCount(index & (i64bit - 1));
+ this.bits[i4096][o] |= newBits;
+ } else if (index == 0) {
+ indices[i4096] = i64bit;
+ assert bits[i4096] == null;
+ bits[i4096] = new long[] {newBits};
+ ++nonZeroLongCount;
+ ramBytesUsed += SINGLE_ELEMENT_ARRAY_BYTES_USED;
+ } else {
+ insertLongValue(i4096, i64bit, newBits, index);
+ }
+ }
+
private void removeLong(int i4096, int i64, long index, int o) {
index &= ~(1L << i64);
indices[i4096] = index;
@@ -319,6 +369,22 @@ public class SparseFixedBitSet extends BitSet {
}
}
+ private void setWithinBlock(int i4096, int from, int to) {
+ int firstLong = from >>> 6;
+ int lastLong = to >>> 6;
+
+ if (firstLong == lastLong) {
+ orLong(i4096, firstLong, mask(from, to));
+ } else {
+ assert firstLong < lastLong;
+ orLong(i4096, lastLong, mask(0, to));
+ for (int i = firstLong + 1; i <= lastLong - 1; ++i) {
+ orLong(i4096, i, -1L);
+ }
+ orLong(i4096, firstLong, mask(from, 63));
+ }
+ }
+
/** Return the first document that occurs on or after the provided block index. */
private int firstDoc(int i4096, int i4096upper) {
assert i4096upper <= indices.length
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java b/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java
index 65458b08b45..2bdefb1e526 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestSparseFixedBitSet.java
@@ -116,6 +116,27 @@ public class TestSparseFixedBitSet extends BaseBitSetTestCase<SparseFixedBitSet>
return DocIdSetIterator.NO_MORE_DOCS;
}
+ public void testSetRange() throws IOException {
+ Random random = random();
+ final int numBits = 1 + random.nextInt(100000);
+ for (float percentSet : new float[] {0, 0.01f, 0.1f, 0.5f, 0.9f, 0.99f, 1f}) {
+ FixedBitSet expected = new FixedBitSet(numBits);
+ final int numInitial = (int) (percentSet * numBits);
+ for (int j = 0; j < numInitial; j++) {
+ expected.set(random.nextInt(numBits));
+ }
+ SparseFixedBitSet set = copyOf(expected, numBits);
+ final int iters = atLeast(random, 10);
+ for (int i = 0; i < iters; ++i) {
+ final int from = random.nextInt(numBits);
+ final int to = random.nextInt(numBits + 1);
+ expected.set(from, to);
+ set.set(from, to);
+ assertEquals(expected, set, numBits);
+ }
+ }
+ }
+
public void testNextClearBit() {
Random rand = random();
final int outer = TEST_NIGHTLY ? 300 : 60;