Package org.apache.lucene.util
Class OpenBitSet
- java.lang.Object
 - 
- org.apache.lucene.search.DocIdSet
 - 
- org.apache.lucene.util.OpenBitSet
 
 
 
- 
- All Implemented Interfaces:
 java.lang.Cloneable,Bits
- Direct Known Subclasses:
 OpenBitSetDISI
public class OpenBitSet extends DocIdSet implements Bits, java.lang.Cloneable
An "open" BitSet implementation that allows direct access to the array of words storing the bits. Unlike java.util.bitset, the fact that bits are packed into an array of longs is part of the interface. This allows efficient implementation of other algorithms by someone other than the author. It also allows one to efficiently implement alternate serialization or interchange formats.OpenBitSetis faster thanjava.util.BitSetin most operations and *much* faster at calculating cardinality of sets and results of set operations. It can also handle sets of larger cardinality (up to 64 * 2**32-1) The goals ofOpenBitSetare the fastest implementation possible, and maximum code reuse. Extra safety and encapsulation may always be built on top, but if that's built in, the cost can never be removed (and hence people re-implement their own version in order to get better performance). If you want a "safe", totally encapsulated (and slower and limited) BitSet class, usejava.util.BitSet.Performance Results
Test system: Pentium 4, Sun Java 1.5_06 -server -Xbatch -Xmx64M
BitSet size = 1,000,000
Results are java.util.BitSet time divided by OpenBitSet time.cardinality intersect_count union nextSetBit get iterator 50% full 3.36 3.96 1.44 1.46 1.99 1.58 1% full 3.31 3.90 1.04 0.99 
Test system: AMD Opteron, 64 bit linux, Sun Java 1.5_06 -server -Xbatch -Xmx64M
BitSet size = 1,000,000
Results are java.util.BitSet time divided by OpenBitSet time.cardinality intersect_count union nextSetBit get iterator 50% full 2.50 3.50 1.00 1.03 1.12 1.25 1% full 2.51 3.49 1.00 1.02  
- 
- 
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.Bits
EMPTY_ARRAY 
 - 
 
- 
Constructor Summary
Constructors Constructor Description OpenBitSet()Constructor: allocates enough space for 64 bits.OpenBitSet(long numBits)Constructs an OpenBitSet large enough to holdnumBits.OpenBitSet(long[] bits, int numWords)Constructs an OpenBitSet from an existing long[]. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidand(OpenBitSet other)voidandNot(OpenBitSet other)static longandNotCount(OpenBitSet a, OpenBitSet b)Returns the popcount or cardinality of "a and not b" or "intersection(a, not(b))".Bitsbits()Optionally provides aBitsinterface for random access to matching documents.static intbits2words(long numBits)returns the number of 64 bit words it would take to hold numBitslongcapacity()Returns the current capacity in bits (1 greater than the index of the last bit)longcardinality()voidclear(int startIndex, int endIndex)Clears a range of bits.voidclear(long index)clears a bit, allowing access beyond the current set size without changing the size.voidclear(long startIndex, long endIndex)Clears a range of bits.OpenBitSetclone()voidensureCapacity(long numBits)Ensure that the long[] is big enough to hold numBits, expanding it if necessary.voidensureCapacityWords(int numWords)Expand the long[] with the size given as a number of words (64 bit longs).booleanequals(java.lang.Object o)returns true if both sets have the same bits setvoidfastClear(int index)clears a bit.voidfastClear(long index)clears a bit.voidfastFlip(int index)flips a bit.voidfastFlip(long index)flips a bit.booleanfastGet(int index)Returns true or false for the specified bit index.booleanfastGet(long index)Returns true or false for the specified bit index.voidfastSet(int index)Sets the bit at the specified index.voidfastSet(long index)Sets the bit at the specified index.voidflip(long index)flips a bit, expanding the set size if necessaryvoidflip(long startIndex, long endIndex)Flips a range of bits, expanding the set size if necessarybooleanflipAndGet(int index)flips a bit and returns the resulting bit value.booleanflipAndGet(long index)flips a bit and returns the resulting bit value.booleanget(int index)Returns true or false for the specified bit index.booleanget(long index)Returns true or false for the specified bit indexbooleangetAndSet(int index)Sets a bit and returns the previous value.booleangetAndSet(long index)Sets a bit and returns the previous value.intgetBit(int index)returns 1 if the bit is set, 0 if not.long[]getBits()Expert: returns the long[] storing the bitsintgetNumWords()Expert: gets the number of longs in the array that are in useinthashCode()voidintersect(OpenBitSet other)this = this AND otherstatic longintersectionCount(OpenBitSet a, OpenBitSet b)Returns the popcount or cardinality of the intersection of the two sets.booleanintersects(OpenBitSet other)returns true if the sets have any elements in commonbooleanisCacheable()This DocIdSet implementation is cacheable.booleanisEmpty()Returns true if there are no set bitsDocIdSetIteratoriterator()Provides aDocIdSetIteratorto access the set.intlength()Returns the number of bits in this setintnextSetBit(int index)Returns the index of the first set bit starting at the index specified.longnextSetBit(long index)Returns the index of the first set bit starting at the index specified.voidor(OpenBitSet other)intprevSetBit(int index)Returns the index of the first set bit starting downwards at the index specified.longprevSetBit(long index)Returns the index of the first set bit starting downwards at the index specified.voidremove(OpenBitSet other)Remove all elements set in other.voidset(long index)sets a bit, expanding the set size if necessaryvoidset(long startIndex, long endIndex)Sets a range of bits, expanding the set size if necessarylongsize()Returns the current capacity of this set.voidtrimTrailingZeros()Lowers numWords, the number of words in use, by checking for trailing zero words.voidunion(OpenBitSet other)this = this OR otherstatic longunionCount(OpenBitSet a, OpenBitSet b)Returns the popcount or cardinality of the union of the two sets.voidxor(OpenBitSet other)this = this XOR otherstatic longxorCount(OpenBitSet a, OpenBitSet b)Returns the popcount or cardinality of the exclusive-or of the two sets. 
 - 
 
- 
- 
Constructor Detail
- 
OpenBitSet
public OpenBitSet(long numBits)
Constructs an OpenBitSet large enough to holdnumBits. 
- 
OpenBitSet
public OpenBitSet()
Constructor: allocates enough space for 64 bits. 
- 
OpenBitSet
public OpenBitSet(long[] bits, int numWords)Constructs an OpenBitSet from an existing long[].The first 64 bits are in long[0], with bit index 0 at the least significant bit, and bit index 63 at the most significant. Given a bit index, the word containing it is long[index/64], and it is at bit number index%64 within that word.
numWords are the number of elements in the array that contain set bits (non-zero longs). numWords should be <= bits.length, and any existing words in the array at position >= numWords should be zero.
 
 - 
 
- 
Method Detail
- 
iterator
public DocIdSetIterator iterator()
Description copied from class:DocIdSetProvides aDocIdSetIteratorto access the set. This implementation can returnnullif there are no docs that match. 
- 
bits
public Bits bits()
Description copied from class:DocIdSetOptionally provides aBitsinterface for random access to matching documents.- Overrides:
 bitsin classDocIdSet- Returns:
 null, if thisDocIdSetdoes not support random access. In contrast toDocIdSet.iterator(), a return value ofnulldoes not imply that no documents match the filter! The default implementation does not provide random access, so you only need to implement this method if your DocIdSet can guarantee random access to every docid in O(1) time without external disk access (asBitsinterface cannot throwIOException). This is generally true for bit sets likeFixedBitSet, which return itself if they are used asDocIdSet.
 
- 
isCacheable
public boolean isCacheable()
This DocIdSet implementation is cacheable.- Overrides:
 isCacheablein classDocIdSet
 
- 
capacity
public long capacity()
Returns the current capacity in bits (1 greater than the index of the last bit) 
- 
size
public long size()
Returns the current capacity of this set. Included for compatibility. This is *not* equal tocardinality() 
- 
length
public int length()
Description copied from interface:BitsReturns the number of bits in this set 
- 
isEmpty
public boolean isEmpty()
Returns true if there are no set bits 
- 
getBits
public long[] getBits()
Expert: returns the long[] storing the bits 
- 
getNumWords
public int getNumWords()
Expert: gets the number of longs in the array that are in use 
- 
get
public boolean get(int index)
Returns true or false for the specified bit index.- Specified by:
 getin interfaceBits- 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.
 
- 
fastGet
public boolean fastGet(int index)
Returns true or false for the specified bit index. The index should be less than the OpenBitSet size 
- 
get
public boolean get(long index)
Returns true or false for the specified bit index 
- 
fastGet
public boolean fastGet(long index)
Returns true or false for the specified bit index. The index should be less than the OpenBitSet size. 
- 
getBit
public int getBit(int index)
returns 1 if the bit is set, 0 if not. The index should be less than the OpenBitSet size 
- 
set
public void set(long index)
sets a bit, expanding the set size if necessary 
- 
fastSet
public void fastSet(int index)
Sets the bit at the specified index. The index should be less than the OpenBitSet size. 
- 
fastSet
public void fastSet(long index)
Sets the bit at the specified index. The index should be less than the OpenBitSet size. 
- 
set
public void set(long startIndex, long endIndex)Sets a range of bits, expanding the set size if necessary- Parameters:
 startIndex- lower indexendIndex- one-past the last bit to set
 
- 
fastClear
public void fastClear(int index)
clears a bit. The index should be less than the OpenBitSet size. 
- 
fastClear
public void fastClear(long index)
clears a bit. The index should be less than the OpenBitSet size. 
- 
clear
public void clear(long index)
clears a bit, allowing access beyond the current set size without changing the size. 
- 
clear
public void clear(int startIndex, int endIndex)Clears a range of bits. Clearing past the end does not change the size of the set.- Parameters:
 startIndex- lower indexendIndex- one-past the last bit to clear
 
- 
clear
public void clear(long startIndex, long endIndex)Clears a range of bits. Clearing past the end does not change the size of the set.- Parameters:
 startIndex- lower indexendIndex- one-past the last bit to clear
 
- 
getAndSet
public boolean getAndSet(int index)
Sets a bit and returns the previous value. The index should be less than the OpenBitSet size. 
- 
getAndSet
public boolean getAndSet(long index)
Sets a bit and returns the previous value. The index should be less than the OpenBitSet size. 
- 
fastFlip
public void fastFlip(int index)
flips a bit. The index should be less than the OpenBitSet size. 
- 
fastFlip
public void fastFlip(long index)
flips a bit. The index should be less than the OpenBitSet size. 
- 
flip
public void flip(long index)
flips a bit, expanding the set size if necessary 
- 
flipAndGet
public boolean flipAndGet(int index)
flips a bit and returns the resulting bit value. The index should be less than the OpenBitSet size. 
- 
flipAndGet
public boolean flipAndGet(long index)
flips a bit and returns the resulting bit value. The index should be less than the OpenBitSet size. 
- 
flip
public void flip(long startIndex, long endIndex)Flips a range of bits, expanding the set size if necessary- Parameters:
 startIndex- lower indexendIndex- one-past the last bit to flip
 
- 
cardinality
public long cardinality()
- Returns:
 - the number of set bits
 
 
- 
intersectionCount
public static long intersectionCount(OpenBitSet a, OpenBitSet b)
Returns the popcount or cardinality of the intersection of the two sets. Neither set is modified. 
- 
unionCount
public static long unionCount(OpenBitSet a, OpenBitSet b)
Returns the popcount or cardinality of the union of the two sets. Neither set is modified. 
- 
andNotCount
public static long andNotCount(OpenBitSet a, OpenBitSet b)
Returns the popcount or cardinality of "a and not b" or "intersection(a, not(b))". Neither set is modified. 
- 
xorCount
public static long xorCount(OpenBitSet a, OpenBitSet b)
Returns the popcount or cardinality of the exclusive-or of the two sets. Neither set is modified. 
- 
nextSetBit
public int nextSetBit(int index)
Returns the index of the first set bit starting at the index specified. -1 is returned if there are no more set bits. 
- 
nextSetBit
public long nextSetBit(long index)
Returns the index of the first set bit starting at the index specified. -1 is returned if there are no more set bits. 
- 
prevSetBit
public int prevSetBit(int index)
Returns the index of the first set bit starting downwards at the index specified. -1 is returned if there are no more set bits. 
- 
prevSetBit
public long prevSetBit(long index)
Returns the index of the first set bit starting downwards at the index specified. -1 is returned if there are no more set bits. 
- 
clone
public OpenBitSet clone()
 
- 
intersect
public void intersect(OpenBitSet other)
this = this AND other 
- 
union
public void union(OpenBitSet other)
this = this OR other 
- 
remove
public void remove(OpenBitSet other)
Remove all elements set in other. this = this AND_NOT other 
- 
xor
public void xor(OpenBitSet other)
this = this XOR other 
- 
and
public void and(OpenBitSet other)
 
- 
or
public void or(OpenBitSet other)
 
- 
andNot
public void andNot(OpenBitSet other)
 
- 
intersects
public boolean intersects(OpenBitSet other)
returns true if the sets have any elements in common 
- 
ensureCapacityWords
public void ensureCapacityWords(int numWords)
Expand the long[] with the size given as a number of words (64 bit longs). 
- 
ensureCapacity
public void ensureCapacity(long numBits)
Ensure that the long[] is big enough to hold numBits, expanding it if necessary. 
- 
trimTrailingZeros
public void trimTrailingZeros()
Lowers numWords, the number of words in use, by checking for trailing zero words. 
- 
bits2words
public static int bits2words(long numBits)
returns the number of 64 bit words it would take to hold numBits 
- 
equals
public boolean equals(java.lang.Object o)
returns true if both sets have the same bits set- Overrides:
 equalsin classjava.lang.Object
 
- 
hashCode
public int hashCode()
- Overrides:
 hashCodein classjava.lang.Object
 
 - 
 
 -