Skip to content

Commit

Permalink
add BitSet.clear() (#12268)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellis authored May 26, 2023
1 parent 367b03b commit 431dc7b
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ Other

API Changes
---------------------

* GITHUB#12268: Add BitSet.clear() without parameters for clearing the entire set
(Jonathan Ellis)

(No changes)

New Features
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private int runAutomatonOnStringChars(String s) {
}

int ngramScore(CharsRef s2) {
countedSubstrings.clear(0, countedSubstrings.length());
countedSubstrings.clear();

int score1 = 0, score2 = 0, score3 = 0; // scores for substrings of length 1, 2 and 3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ static short writeBitSet(DocIdSetIterator it, IndexOutput out, byte denseRankPow
// Flush block
flush(prevBlock, buffer, blockCardinality, denseRankPower, out);
// Reset for next block
buffer.clear(0, buffer.length());
buffer.clear();
totalCardinality += blockCardinality;
blockCardinality = 0;
}
Expand All @@ -233,7 +233,7 @@ static short writeBitSet(DocIdSetIterator it, IndexOutput out, byte denseRankPow
jumps, out.getFilePointer() - origo, totalCardinality, jumpBlockIndex, prevBlock + 1);
totalCardinality += blockCardinality;
flush(prevBlock, buffer, blockCardinality, denseRankPower, out);
buffer.clear(0, buffer.length());
buffer.clear();
prevBlock++;
}
final int lastBlock =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static short writeBitSet(DocIdSetIterator it, IndexOutput out, byte dense
// Flush block
flush(prevBlock, buffer, blockCardinality, denseRankPower, out);
// Reset for next block
buffer.clear(0, buffer.length());
buffer.clear();
totalCardinality += blockCardinality;
blockCardinality = 0;
}
Expand All @@ -234,7 +234,7 @@ public static short writeBitSet(DocIdSetIterator it, IndexOutput out, byte dense
jumps, out.getFilePointer() - origo, totalCardinality, jumpBlockIndex, prevBlock + 1);
totalCardinality += blockCardinality;
flush(prevBlock, buffer, blockCardinality, denseRankPower, out);
buffer.clear(0, buffer.length());
buffer.clear();
prevBlock++;
}
final int lastBlock =
Expand Down
10 changes: 10 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/BitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public static BitSet of(DocIdSetIterator it, int maxDoc) throws IOException {
return set;
}

/**
* Clear all the bits of the set.
*
* <p>Depending on the implementation, this may be significantly faster than clear(0, length).
*/
public void clear() {
// default implementation for compatibility
clear(0, length());
}

/** Set the bit at <code>i</code>. */
public abstract void set(int i);

Expand Down
5 changes: 5 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public FixedBitSet(long[] storedBits, int numBits) {
assert verifyGhostBitsClear();
}

@Override
public void clear() {
Arrays.fill(bits, 0L);
}

/**
* Checks if the bits past numBits are clear. Some methods rely on this implicit assumption:
* search for "Depends on the ghost bits being clear!"
Expand Down
12 changes: 12 additions & 0 deletions lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.lucene.util;

import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.search.DocIdSetIterator;

/**
Expand Down Expand Up @@ -73,6 +74,17 @@ public SparseFixedBitSet(int length) {
+ RamUsageEstimator.shallowSizeOf(bits);
}

@Override
public void clear() {
Arrays.fill(bits, null);
Arrays.fill(indices, 0L);
nonZeroLongCount = 0;
ramBytesUsed =
BASE_RAM_BYTES_USED
+ RamUsageEstimator.sizeOf(indices)
+ RamUsageEstimator.shallowSizeOf(bits);
}

@Override
public int length() {
return length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ public static String getCommonPrefix(Automaton a) {
FixedBitSet tmp = current;
current = next;
next = tmp;
next.clear(0, next.length());
next.clear();
}
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private void prepareScratchState(int capacity) {
if (visited.length() < capacity) {
visited = FixedBitSet.ensureCapacity((FixedBitSet) visited, capacity);
}
visited.clear(0, visited.length());
visited.clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void startMultiValuedDoc() {
if (multiValuedDocElementaryIntervalHits == null) {
multiValuedDocElementaryIntervalHits = new FixedBitSet(boundaries.length);
} else {
multiValuedDocElementaryIntervalHits.clear(0, multiValuedDocElementaryIntervalHits.length());
multiValuedDocElementaryIntervalHits.clear();
}
}

Expand All @@ -103,7 +103,7 @@ boolean endMultiValuedDoc() {
if (multiValuedDocRangeHits == null) {
multiValuedDocRangeHits = new FixedBitSet(rangeCount());
} else {
multiValuedDocRangeHits.clear(0, multiValuedDocRangeHits.length());
multiValuedDocRangeHits.clear();
}
elementaryIntervalUpto = 0;
rollupMultiValued(root);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ public void testClearRange() throws IOException {
}
}

/** Test the {@link BitSet#clear()} method. */
public void testClearAll() 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}) {
BitSet set1 = new JavaUtilBitSet(randomSet(numBits, percentSet), numBits);
T set2 = copyOf(set1, numBits);
final int iters = atLeast(random, 10);
for (int i = 0; i < iters; ++i) {
set1.clear();
set2.clear();
assertEquals(set1, set2, numBits);
}
}
}

private DocIdSet randomCopy(BitSet set, int numBits) throws IOException {
switch (random().nextInt(5)) {
case 0:
Expand Down Expand Up @@ -241,6 +257,11 @@ private static class JavaUtilBitSet extends BitSet {
this.numBits = numBits;
}

@Override
public void clear() {
bitSet.clear();
}

@Override
public void clear(int index) {
bitSet.clear(index);
Expand Down

0 comments on commit 431dc7b

Please sign in to comment.