Skip to content

Commit

Permalink
add BitSet.clear() (apache#12268)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbellis committed Jun 16, 2023
1 parent a5d55a5 commit c698eb1
Show file tree
Hide file tree
Showing 11 changed files with 45 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 @@ -110,6 +110,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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public void clear(int index) {
bitSet.clear(index);
}

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

@Override
public boolean get(int index) {
return bitSet.get(index);
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 @@ -1097,7 +1097,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 @@ -369,6 +369,6 @@ private void prepareScratchState(int capacity) {
}
// else GrowableBitSet knows how to grow itself safely
}
visited.clear(0, visited.length());
visited.clear();
}
}
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

0 comments on commit c698eb1

Please sign in to comment.