Apache Solr open-source search software
LUCENE-6017: Specialize FixedBitSet.xor in case of a FixedBitSetIterator.
Adrien Grand62bf86a2e17b
diff --git a/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java b/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.javaindex e155a70d954..d97f121a18b 100644--- a/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java+++ b/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java@@ -338,20 +338,31 @@ public final class FixedBitSet extends DocIdSet implements MutableBits {/** this = this XOR other */public void xor(FixedBitSet other) {- assert other.numWords <= numWords : "numWords=" + numWords + ", other.numWords=" + other.numWords;- final long[] thisBits = this.bits;- final long[] otherBits = other.bits;- int pos = Math.min(numWords, other.numWords);- while (--pos >= 0) {- thisBits[pos] ^= otherBits[pos];- }+ xor(other.bits, other.numWords);}/** Does in-place XOR of the bits provided by the iterator. */public void xor(DocIdSetIterator iter) throws IOException {- int doc;- while ((doc = iter.nextDoc()) < numBits) {- flip(doc, doc + 1);+ if (iter instanceof FixedBitSetIterator && iter.docID() == -1) {+ final FixedBitSetIterator fbs = (FixedBitSetIterator) iter;+ xor(fbs.bits, fbs.numWords);+ // advance after last doc that would be accepted if standard+ // iteration is used (to exhaust it):+ fbs.advance(numBits);+ } else {+ int doc;+ while ((doc = iter.nextDoc()) < numBits) {+ flip(doc);+ }+ }+ }++ private void xor(long[] otherBits, int otherNumWords) {+ assert otherNumWords <= numWords : "numWords=" + numWords + ", other.numWords=" + otherNumWords;+ final long[] thisBits = this.bits;+ int pos = Math.min(numWords, otherNumWords);+ while (--pos >= 0) {+ thisBits[pos] ^= otherBits[pos];}}@@ -476,6 +487,15 @@ public final class FixedBitSet extends DocIdSet implements MutableBits {bits[endWord] ^= endmask;}+ /** Flip the bit at the provided index. */+ public void flip(int index) {+ assert index >= 0 && index < numBits: "index=" + index + " numBits=" + numBits;+ int wordNum = index >> 6; // div 64+ int bit = index & 0x3f; // mod 64+ long bitmask = 1L << bit;+ bits[wordNum] ^= bitmask;+ }+/** Sets a range of bits** @param startIndex lower indexdiff --git a/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java b/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.javaindex 9f31e301a5e..6fa84271a49 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestFixedBitSet.java@@ -123,12 +123,12 @@ public class TestFixedBitSet extends BaseDocIdSetTestCase<FixedBitSet> {b.clear(idx);idx = random().nextInt(sz);- a.flip(idx);+ a.flip(idx, idx+1);b.flip(idx, idx+1);idx = random().nextInt(sz);a.flip(idx);- b.flip(idx, idx+1);+ b.flip(idx);boolean val2 = b.get(idx);boolean val = b.getAndSet(idx);@@ -201,7 +201,7 @@ public class TestFixedBitSet extends BaseDocIdSetTestCase<FixedBitSet> {b0=b;}}-+// large enough to flush obvious bugs, small enough to run in <.5 sec as part of a// larger testsuite.public void testSmall() throws IOException {