add BitSet.clear() (#12268)

This commit is contained in:
Jonathan Ellis 2023-05-26 09:13:16 -07:00 committed by GitHub
parent 367b03bfc2
commit 431dc7b415
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 61 additions and 9 deletions

View File

@ -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

View File

@ -79,7 +79,7 @@ class TrigramAutomaton {
}
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

View File

@ -219,7 +219,7 @@ final class IndexedDISI extends DocIdSetIterator {
// Flush block
flush(prevBlock, buffer, blockCardinality, denseRankPower, out);
// Reset for next block
buffer.clear(0, buffer.length());
buffer.clear();
totalCardinality += blockCardinality;
blockCardinality = 0;
}
@ -233,7 +233,7 @@ final class IndexedDISI extends DocIdSetIterator {
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 =

View File

@ -220,7 +220,7 @@ public final class IndexedDISI extends DocIdSetIterator {
// Flush block
flush(prevBlock, buffer, blockCardinality, denseRankPower, out);
// Reset for next block
buffer.clear(0, buffer.length());
buffer.clear();
totalCardinality += blockCardinality;
blockCardinality = 0;
}
@ -234,7 +234,7 @@ public final class IndexedDISI extends DocIdSetIterator {
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 =

View File

@ -43,6 +43,16 @@ public abstract class BitSet implements Bits, Accountable {
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);

View File

@ -147,6 +147,11 @@ public final class FixedBitSet extends BitSet {
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!"

View File

@ -17,6 +17,7 @@
package org.apache.lucene.util;
import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.search.DocIdSetIterator;
/**
@ -73,6 +74,17 @@ public class SparseFixedBitSet extends BitSet {
+ 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;

View File

@ -1094,7 +1094,7 @@ public final class Operations {
FixedBitSet tmp = current;
current = next;
next = tmp;
next.clear(0, next.length());
next.clear();
}
return builder.toString();
}

View File

@ -339,7 +339,7 @@ public class HnswGraphSearcher<T> {
if (visited.length() < capacity) {
visited = FixedBitSet.ensureCapacity((FixedBitSet) visited, capacity);
}
visited.clear(0, visited.length());
visited.clear();
}
/**

View File

@ -84,7 +84,7 @@ class OverlappingLongRangeCounter extends LongRangeCounter {
if (multiValuedDocElementaryIntervalHits == null) {
multiValuedDocElementaryIntervalHits = new FixedBitSet(boundaries.length);
} else {
multiValuedDocElementaryIntervalHits.clear(0, multiValuedDocElementaryIntervalHits.length());
multiValuedDocElementaryIntervalHits.clear();
}
}
@ -103,7 +103,7 @@ class OverlappingLongRangeCounter extends LongRangeCounter {
if (multiValuedDocRangeHits == null) {
multiValuedDocRangeHits = new FixedBitSet(rangeCount());
} else {
multiValuedDocRangeHits.clear(0, multiValuedDocRangeHits.length());
multiValuedDocRangeHits.clear();
}
elementaryIntervalUpto = 0;
rollupMultiValued(root);

View File

@ -170,6 +170,22 @@ public abstract class BaseBitSetTestCase<T extends BitSet> extends LuceneTestCas
}
}
/** 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:
@ -241,6 +257,11 @@ public abstract class BaseBitSetTestCase<T extends BitSet> extends LuceneTestCas
this.numBits = numBits;
}
@Override
public void clear() {
bitSet.clear();
}
@Override
public void clear(int index) {
bitSet.clear(index);