LUCENE-5277: Modify FixedBitSet copy constructor to take numBits to allow grow/shrink the new bitset

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1531627 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shai Erera 2013-10-13 08:21:32 +00:00
parent 8b35981608
commit 544d997664
3 changed files with 62 additions and 12 deletions

View File

@ -146,6 +146,10 @@ API Changes:
* LUCENE-5275: Change AttributeSource.toString() to display the current * LUCENE-5275: Change AttributeSource.toString() to display the current
state of attributes. (Robert Muir) state of attributes. (Robert Muir)
* LUCENE-5277: Modify FixedBitSet copy constructor to take an additional
numBits parameter to allow growing/shrinking the copied bitset. You can
use FixedBitSet.clone() if you only need to clone the bitset. (Shai Erera)
Optimizations Optimizations
* LUCENE-5225: The ToParentBlockJoinQuery only keeps tracks of the the child * LUCENE-5225: The ToParentBlockJoinQuery only keeps tracks of the the child

View File

@ -35,7 +35,6 @@ import org.apache.lucene.search.DocIdSetIterator;
* *
* @lucene.internal * @lucene.internal
**/ **/
public final class FixedBitSet extends DocIdSet implements Bits { public final class FixedBitSet extends DocIdSet implements Bits {
private final long[] bits; private final long[] bits;
private final int numBits; private final int numBits;
@ -63,14 +62,18 @@ public final class FixedBitSet extends DocIdSet implements Bits {
} }
this.numBits = numBits; this.numBits = numBits;
this.bits = storedBits; this.bits = storedBits;
} }
/** Makes full copy. */ /**
public FixedBitSet(FixedBitSet other) { * Makes a full copy of the bits, while allowing to expand/shrink the bitset.
bits = new long[other.wordLength]; * If {@code numBits < other.numBits}, then only the first {@code numBits}
System.arraycopy(other.bits, 0, bits, 0, other.wordLength); * are copied from other.
numBits = other.numBits; */
wordLength = other.wordLength; public FixedBitSet(FixedBitSet other, int numBits) {
wordLength = bits2words(numBits);
bits = new long[wordLength];
System.arraycopy(other.bits, 0, bits, 0, Math.min(other.wordLength, wordLength));
this.numBits = numBits;
} }
@Override @Override
@ -403,7 +406,7 @@ public final class FixedBitSet extends DocIdSet implements Bits {
@Override @Override
public FixedBitSet clone() { public FixedBitSet clone() {
return new FixedBitSet(this); return new FixedBitSet(this, numBits);
} }
/** returns true if both sets have the same bits set */ /** returns true if both sets have the same bits set */

View File

@ -328,7 +328,50 @@ public class TestFixedBitSet extends BaseDocIdSetTestCase<FixedBitSet> {
checkNextSetBitArray(new int[0], setBits.length + random().nextInt(10)); checkNextSetBitArray(new int[0], setBits.length + random().nextInt(10));
} }
public void testGrow() {
FixedBitSet bits = new FixedBitSet(5);
bits.set(1);
bits.set(4);
FixedBitSet newBits = new FixedBitSet(bits, 8); // grow within the word
assertTrue(newBits.get(1));
assertTrue(newBits.get(4));
newBits = new FixedBitSet(bits, 72); // grow beyond one word
assertTrue(newBits.get(1));
assertTrue(newBits.get(4));
}
public void testShrink() {
FixedBitSet bits = new FixedBitSet(72);
bits.set(1);
bits.set(4);
bits.set(69);
FixedBitSet newBits = new FixedBitSet(bits, 66); // shrink within the word
assertTrue(newBits.get(1));
assertTrue(newBits.get(4));
boolean hitError = true;
try {
newBits.get(69);
hitError = false;
} catch (AssertionError e) {
hitError = true;
}
assertTrue(hitError);
newBits = new FixedBitSet(bits, 8); // shrink beyond one word
assertTrue(newBits.get(1));
assertTrue(newBits.get(4));
hitError = true;
try {
newBits.get(69);
hitError = false;
} catch (AssertionError e) {
hitError = true;
}
assertTrue(hitError);
}
} }