org.apache.cassandra.utils.obs
Class OpenBitSet

java.lang.Object
  extended by org.apache.cassandra.utils.obs.OpenBitSet
All Implemented Interfaces:
java.io.Closeable, IBitSet

public class OpenBitSet
extends java.lang.Object
implements IBitSet

An "open" BitSet implementation that allows direct access to the arrays of words storing the bits. Derived from Lucene's OpenBitSet, but with a paged backing array (see bits delaration, below).

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.

OpenBitSet is faster than java.util.BitSet in 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 of OpenBitSet are 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, use java.util.BitSet.


Constructor Summary
OpenBitSet()
           
OpenBitSet(long numBits)
          Constructs an OpenBitSet large enough to hold numBits.
 
Method Summary
 void and(OpenBitSet other)
           
static int bits2words(long numBits)
          returns the number of 64 bit words it would take to hold numBits
 long capacity()
          Returns the current capacity in bits (1 greater than the index of the last bit)
 long cardinality()
           
 void clear()
           
 void clear(int index)
          clears a bit.
 void clear(int startIndex, int endIndex)
          Clears a range of bits.
 void clear(long index)
          clears a bit.
 void clear(long startIndex, long endIndex)
          Clears a range of bits.
 void close()
           
static OpenBitSet deserialize(java.io.DataInput dis)
           
 boolean equals(java.lang.Object o)
          returns true if both sets have the same bits set
 boolean get(int index)
          Returns true or false for the specified bit index.
 boolean get(long index)
          Returns true or false for the specified bit index.
 int getNumWords()
          Expert: gets the number of longs in the array that are in use
 long[] getPage(int pageIdx)
           
 int getPageCount()
           
 int getPageSize()
           
 int hashCode()
           
 void intersect(OpenBitSet other)
          this = this AND other
 boolean isEmpty()
          Returns true if there are no set bits
 long length()
           
 void serialize(java.io.DataOutput dos)
           
 long serializedSize(TypeSizes type)
           
 void set(int index)
          Sets the bit at the specified index.
 void set(long index)
          Sets the bit at the specified index.
 long size()
          Returns the current capacity of this set.
 void trimTrailingZeros()
          Lowers numWords, the number of words in use, by checking for trailing zero words.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OpenBitSet

public OpenBitSet(long numBits)
Constructs an OpenBitSet large enough to hold numBits.

Parameters:
numBits -

OpenBitSet

public OpenBitSet()
Method Detail

getPageSize

public int getPageSize()
Returns:
the pageSize

getPageCount

public int getPageCount()

getPage

public long[] getPage(int pageIdx)

capacity

public long capacity()
Returns the current capacity in bits (1 greater than the index of the last bit)

Specified by:
capacity in interface IBitSet

size

public long size()
Returns the current capacity of this set. Included for compatibility. This is *not* equal to cardinality()


length

public long length()

isEmpty

public boolean isEmpty()
Returns true if there are no set 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. The index should be less than the OpenBitSet size


get

public boolean get(long index)
Returns true or false for the specified bit index. The index should be less than the OpenBitSet size.

Specified by:
get in interface IBitSet

set

public void set(long index)
Sets the bit at the specified index. The index should be less than the OpenBitSet size.

Specified by:
set in interface IBitSet

set

public void set(int index)
Sets the bit at the specified index. The index should be less than the OpenBitSet size.


clear

public void clear(int index)
clears a bit. The index should be less than the OpenBitSet size.


clear

public void clear(long index)
clears a bit. The index should be less than the OpenBitSet size.

Specified by:
clear in interface IBitSet

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 index
endIndex - 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 index
endIndex - one-past the last bit to clear

cardinality

public long cardinality()
Returns:
the number of set bits

intersect

public void intersect(OpenBitSet other)
this = this AND other


and

public void and(OpenBitSet other)

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:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Throws:
java.io.IOException

serialize

public void serialize(java.io.DataOutput dos)
               throws java.io.IOException
Specified by:
serialize in interface IBitSet
Throws:
java.io.IOException

serializedSize

public long serializedSize(TypeSizes type)
Specified by:
serializedSize in interface IBitSet

clear

public void clear()
Specified by:
clear in interface IBitSet

deserialize

public static OpenBitSet deserialize(java.io.DataInput dis)
                              throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2013 The Apache Software Foundation