Package org.apache.lucene.util
Class FixedBitSet
java.lang.Object
org.apache.lucene.util.BitSet
org.apache.lucene.util.FixedBitSet
- All Implemented Interfaces:
Accountable,Bits
BitSet of fixed length (numBits), backed by accessible (
getBits()) long[], accessed with
an int index, implementing Bits and DocIdSet. If you need to manage more than
2.1B bits, use LongBitSet.- NOTE: This API is for internal purposes only and might change in incompatible ways in the next release.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.lucene.util.Bits
Bits.MatchAllBits, Bits.MatchNoBits -
Field Summary
Fields inherited from interface org.apache.lucene.util.Accountable
NULL_ACCOUNTABLEFields inherited from interface org.apache.lucene.util.Bits
EMPTY_ARRAY -
Constructor Summary
ConstructorsConstructorDescriptionFixedBitSet(int numBits) Creates a new FixedBitSet.FixedBitSet(long[] storedBits, int numBits) Creates a new FixedBitSet using the provided long[] array as backing store. -
Method Summary
Modifier and TypeMethodDescriptionvoidand(FixedBitSet other) this = this AND othervoidandNot(DocIdSetIterator iter) voidandNot(FixedBitSet other) this = this AND NOT otherstatic longReturns the popcount or cardinality of "a and not b" or "intersection(a, not(b))".static voidandRange(FixedBitSet source, int sourceFrom, FixedBitSet dest, int destFrom, int length) Andlengthbits starting atsourceFromfromsourceintodeststarting atdestFrom.voidapplyMask(FixedBitSet bitSet, int offset) intReturn an approximation of the cardinality of this set.Convert this instance to read-onlyBits.static intbits2words(int numBits) returns the number of 64 bit words it would take to hold numBitsintReturns number of set bits.intcardinality(int from, int to) Return the number of set bits between indexesfrominclusive andtoexclusive.voidclear()Clear all the bits of the set.voidclear(int index) Clear the bit ati.voidclear(int startIndex, int endIndex) Clears a range of bits.clone()static FixedBitSetMake a copy of the given bits.static FixedBitSetensureCapacity(FixedBitSet bits, int numBits) If the givenFixedBitSetis large enough to holdnumBits+1, returns the given bits, otherwise returns a newFixedBitSetwhich can hold the requested number of bits.booleanvoidflip(int index) Flip the bit at the provided index.voidflip(int startIndex, int endIndex) Flips a range of bitsvoidforEach(int from, int to, int base, CheckedIntConsumer<IOException> consumer) For each set bit fromfrominclusive totoexclusive, addbaseto the bit index and callconsumeron it.booleanget(int index) Returns the value of the bit with the specifiedindex.booleangetAndClear(int index) booleangetAndSet(int index) Set the bit ati, returningtrueif it was previously set.long[]getBits()Expert.inthashCode()static longReturns the popcount or cardinality of the intersection of the two sets.booleanintersects(FixedBitSet other) returns true if the sets have any elements in commonintlength()Returns the number of bits in this setintnextSetBit(int index) Returns the index of the first set bit starting at the index specified.intnextSetBit(int start, int upperBound) Returns the index of the first set bit from start (inclusive) until end (exclusive).voidor(DocIdSetIterator iter) Does in-place OR of the bits provided by the iterator.voidor(FixedBitSet other) this = this OR otherstatic voidorRange(FixedBitSet source, int sourceFrom, FixedBitSet dest, int destFrom, int length) Orlengthbits starting atsourceFromfromsourceintodeststarting atdestFrom.intprevSetBit(int index) Returns the index of the last set bit before or on the index specified.longReturn the memory usage of this object in bytes.booleanScans the backing store to check if all bits are clear.voidset(int index) Set the bit ati.voidset(int startIndex, int endIndex) Sets a range of bitsstatic longunionCount(FixedBitSet a, FixedBitSet b) Returns the popcount or cardinality of the union of the two sets.voidxor(DocIdSetIterator iter) Does in-place XOR of the bits provided by the iterator.voidxor(FixedBitSet other) this = this XOR otherMethods inherited from class org.apache.lucene.util.BitSet
checkUnpositioned, ofMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.lucene.util.Accountable
getChildResources
-
Constructor Details
-
FixedBitSet
public FixedBitSet(int numBits) Creates a new FixedBitSet. The internally allocated long array will be exactly the size needed to accommodate the numBits specified.- Parameters:
numBits- the number of bits needed
-
FixedBitSet
public FixedBitSet(long[] storedBits, int numBits) Creates a new FixedBitSet using the provided long[] array as backing store. The storedBits array must be large enough to accommodate the numBits specified, but may be larger. In that case the 'extra' or 'ghost' bits must be clear (or they may provoke spurious side-effects)- Parameters:
storedBits- the array to use as backing storenumBits- the number of bits actually needed
-
-
Method Details
-
ensureCapacity
If the givenFixedBitSetis large enough to holdnumBits+1, returns the given bits, otherwise returns a newFixedBitSetwhich can hold the requested number of bits.NOTE: the returned bitset reuses the underlying
long[]of the givenbitsif possible. Also, callinglength()on the returned bits may return a value greater thannumBits. -
bits2words
public static int bits2words(int numBits) returns the number of 64 bit words it would take to hold numBits -
intersectionCount
Returns the popcount or cardinality of the intersection of the two sets. Neither set is modified. -
unionCount
Returns the popcount or cardinality of the union of the two sets. Neither set is modified. -
andNotCount
Returns the popcount or cardinality of "a and not b" or "intersection(a, not(b))". Neither set is modified. -
clear
public void clear()Description copied from class:BitSetClear all the bits of the set.Depending on the implementation, this may be significantly faster than clear(0, length).
-
length
public int length()Description copied from interface:BitsReturns the number of bits in this set -
ramBytesUsed
public long ramBytesUsed()Description copied from interface:AccountableReturn the memory usage of this object in bytes. Negative values are illegal. -
getBits
public long[] getBits()Expert. -
cardinality
public int cardinality()Returns number of set bits. NOTE: this visits every long in the backing bits array, and the result is not internally cached!- Specified by:
cardinalityin classBitSet
-
cardinality
public int cardinality(int from, int to) Return the number of set bits between indexesfrominclusive andtoexclusive. -
approximateCardinality
public int approximateCardinality()Description copied from class:BitSetReturn an approximation of the cardinality of this set. Some implementations may trade accuracy for speed if they have the ability to estimate the cardinality of the set without iterating over all the data. The default implementation returnsBitSet.cardinality().- Specified by:
approximateCardinalityin classBitSet
-
get
public boolean get(int index) Description copied from interface:BitsReturns the value of the bit with the specifiedindex.- Parameters:
index- index, should be non-negative and <Bits.length(). The result of passing negative or out of bounds values is undefined by this interface, just don't do it!- Returns:
trueif the bit is set,falseotherwise.
-
set
public void set(int index) Description copied from class:BitSetSet the bit ati. -
getAndSet
public boolean getAndSet(int index) Description copied from class:BitSetSet the bit ati, returningtrueif it was previously set. -
clear
public void clear(int index) Description copied from class:BitSetClear the bit ati. -
getAndClear
public boolean getAndClear(int index) -
nextSetBit
public int nextSetBit(int index) Description copied from class:BitSetReturns the index of the first set bit starting at the index specified.DocIdSetIterator.NO_MORE_DOCSis returned if there are no more set bits.- Overrides:
nextSetBitin classBitSet
-
nextSetBit
public int nextSetBit(int start, int upperBound) Description copied from class:BitSetReturns the index of the first set bit from start (inclusive) until end (exclusive).DocIdSetIterator.NO_MORE_DOCSis returned if there are no more set bits.- Specified by:
nextSetBitin classBitSet
-
prevSetBit
public int prevSetBit(int index) Description copied from class:BitSetReturns the index of the last set bit before or on the index specified. -1 is returned if there are no more set bits.- Specified by:
prevSetBitin classBitSet
-
or
Description copied from class:BitSetDoes in-place OR of the bits provided by the iterator. The state of the iterator after this operation terminates is undefined.- Overrides:
orin classBitSet- Throws:
IOException
-
orRange
public static void orRange(FixedBitSet source, int sourceFrom, FixedBitSet dest, int destFrom, int length) Orlengthbits starting atsourceFromfromsourceintodeststarting atdestFrom. -
andRange
public static void andRange(FixedBitSet source, int sourceFrom, FixedBitSet dest, int destFrom, int length) Andlengthbits starting atsourceFromfromsourceintodeststarting atdestFrom. -
or
this = this OR other -
xor
this = this XOR other -
xor
Does in-place XOR of the bits provided by the iterator.- Throws:
IOException
-
intersects
returns true if the sets have any elements in common -
and
this = this AND other -
andNot
- Throws:
IOException
-
andNot
this = this AND NOT other -
scanIsEmpty
public boolean scanIsEmpty()Scans the backing store to check if all bits are clear. The method is deliberately not called "isEmpty" to emphasize it is not low cost (as isEmpty usually is).- Returns:
- true if all bits are clear.
-
flip
public void flip(int startIndex, int endIndex) Flips a range of bits- Parameters:
startIndex- lower indexendIndex- one-past the last bit to flip
-
flip
public void flip(int index) Flip the bit at the provided index. -
set
public void set(int startIndex, int endIndex) Sets a range of bits- Parameters:
startIndex- lower indexendIndex- one-past the last bit to set
-
clear
public void clear(int startIndex, int endIndex) Description copied from class:BitSetClears a range of bits. -
clone
-
equals
-
hashCode
public int hashCode() -
copyOf
Make a copy of the given bits. -
asReadOnlyBits
Convert this instance to read-onlyBits. This is useful in the case that thisFixedBitSetis returned as aBitsinstance, to make sure that consumers may not get write access back by casting to aFixedBitSet. NOTE: Changes to thisFixedBitSetwill be reflected on the returnedBits. -
applyMask
Description copied from interface:BitsApply thisBitsinstance to the givenFixedBitSet, which starts at the givenoffset.This should behave the same way as the default implementation, which does the following:
for (int i = bitSet.nextSetBit(0); i != DocIdSetIterator.NO_MORE_DOCS; i = i + 1 >= bitSet.length() ? DocIdSetIterator.NO_MORE_DOCS : bitSet.nextSetBit(i + 1)) { if (get(offset + i) == false) { bitSet.clear(i); } } -
forEach
public void forEach(int from, int to, int base, CheckedIntConsumer<IOException> consumer) throws IOException For each set bit fromfrominclusive totoexclusive, addbaseto the bit index and callconsumeron it. This is internally used by queries that use bit sets as intermediate representations of their matches.- Throws:
IOException
-