com.googlecode.javaewah.datastructure
Class BitSet

java.lang.Object
  extended by com.googlecode.javaewah.datastructure.BitSet
All Implemented Interfaces:
Externalizable, Serializable, Cloneable, Iterable<Integer>

public class BitSet
extends Object
implements Cloneable, Iterable<Integer>, Externalizable

This is an optimized version of Java's BitSet. In many cases, it can be used as a drop-in replacement. It differs from the basic Java BitSet class in the following ways:

Since:
0.8.0
Author:
Daniel Lemire
See Also:
Serialized Form

Constructor Summary
BitSet(int sizeinbits)
          Construct a bitset with the specified number of bits (initially all false).
 
Method Summary
 void and(BitSet bs)
          Compute bitwise AND.
 int andcardinality(BitSet bs)
          Compute cardinality of bitwise AND.
 void andNot(BitSet bs)
          Compute bitwise AND NOT.
 int andNotcardinality(BitSet bs)
          Compute cardinality of bitwise AND NOT.
 int cardinality()
          Compute the number of bits set to 1
 void clear()
          Reset all bits to false.
 BitSet clone()
           
 boolean empty()
          Check whether a bitset contains a set bit.
 boolean equals(Object o)
           
 boolean get(int i)
           
 int hashCode()
           
 boolean intersects(BitSet bs)
          Checks whether two bitsets intersect.
 IntIterator intIterator()
          Iterate over the set bits
 Iterator<Integer> iterator()
           
 int nextSetBit(int i)
          Usage: for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) { operate on index i here }
 int nextUnsetBit(int i)
          Usage: for(int i=bs.nextUnsetBit(0); i>=0; i=bs.nextUnsetBit(i+1)) { operate on index i here }
 void or(BitSet bs)
          Compute bitwise OR.
 int orcardinality(BitSet bs)
          Compute cardinality of bitwise OR.
 void readExternal(ObjectInput in)
           
 void resize(int sizeinbits)
          Resize the bitset
 void set(int i)
          Set to true
 void set(int i, boolean b)
           
 int size()
          Query the size
 void trim()
          Recovers wasted memory
 void unset(int i)
          Set to false
 IntIterator unsetIntIterator()
          Iterate over the unset bits
 void writeExternal(ObjectOutput out)
           
 void xor(BitSet bs)
          Compute bitwise XOR.
 int xorcardinality(BitSet bs)
          Compute cardinality of bitwise XOR.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BitSet

public BitSet(int sizeinbits)
Construct a bitset with the specified number of bits (initially all false). The number of bits is rounded up to the nearest multiple of 64.

Parameters:
sizeinbits - the size in bits
Method Detail

and

public void and(BitSet bs)
Compute bitwise AND.

Parameters:
bs - other bitset

andcardinality

public int andcardinality(BitSet bs)
Compute cardinality of bitwise AND. The current bitmap is modified. Consider calling trim() to recover wasted memory afterward.

Parameters:
bs - other bitset
Returns:
cardinality

andNot

public void andNot(BitSet bs)
Compute bitwise AND NOT. The current bitmap is modified. Consider calling trim() to recover wasted memory afterward.

Parameters:
bs - other bitset

andNotcardinality

public int andNotcardinality(BitSet bs)
Compute cardinality of bitwise AND NOT.

Parameters:
bs - other bitset
Returns:
cardinality

cardinality

public int cardinality()
Compute the number of bits set to 1

Returns:
the number of bits

clear

public void clear()
Reset all bits to false. This might be wasteful: a better approach is to create a new empty bitmap.


clone

public BitSet clone()
Overrides:
clone in class Object

equals

public boolean equals(Object o)
Overrides:
equals in class Object

empty

public boolean empty()
Check whether a bitset contains a set bit.

Returns:
true if no set bit is found

get

public boolean get(int i)
Parameters:
i - index
Returns:
value of the bit

hashCode

public int hashCode()
Overrides:
hashCode in class Object

intIterator

public IntIterator intIterator()
Iterate over the set bits

Returns:
an iterator

iterator

public Iterator<Integer> iterator()
Specified by:
iterator in interface Iterable<Integer>

intersects

public boolean intersects(BitSet bs)
Checks whether two bitsets intersect.

Parameters:
bs - other bitset
Returns:
true if they have a non-empty intersection (result of AND)

nextSetBit

public int nextSetBit(int i)
Usage: for(int i=bs.nextSetBit(0); i>=0; i=bs.nextSetBit(i+1)) { operate on index i here }

Parameters:
i - current set bit
Returns:
next set bit or -1

nextUnsetBit

public int nextUnsetBit(int i)
Usage: for(int i=bs.nextUnsetBit(0); i>=0; i=bs.nextUnsetBit(i+1)) { operate on index i here }

Parameters:
i - current unset bit
Returns:
next unset bit or -1

or

public void or(BitSet bs)
Compute bitwise OR. The current bitmap is modified. Consider calling trim() to recover wasted memory afterward.

Parameters:
bs - other bitset

orcardinality

public int orcardinality(BitSet bs)
Compute cardinality of bitwise OR. BitSets are not modified.

Parameters:
bs - other bitset
Returns:
cardinality

readExternal

public void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException
Specified by:
readExternal in interface Externalizable
Throws:
IOException
ClassNotFoundException

resize

public void resize(int sizeinbits)
Resize the bitset

Parameters:
sizeinbits - new number of bits

set

public void set(int i)
Set to true

Parameters:
i - index of the bit

set

public void set(int i,
                boolean b)
Parameters:
i - index
b - value of the bit

size

public int size()
Query the size

Returns:
the size in bits.

trim

public void trim()
Recovers wasted memory


unset

public void unset(int i)
Set to false

Parameters:
i - index of the bit

unsetIntIterator

public IntIterator unsetIntIterator()
Iterate over the unset bits

Returns:
an iterator

writeExternal

public void writeExternal(ObjectOutput out)
                   throws IOException
Specified by:
writeExternal in interface Externalizable
Throws:
IOException

xor

public void xor(BitSet bs)
Compute bitwise XOR. The current bitmap is modified. Consider calling trim() to recover wasted memory afterward.

Parameters:
bs - other bitset

xorcardinality

public int xorcardinality(BitSet bs)
Compute cardinality of bitwise XOR. BitSets are not modified.

Parameters:
bs - other bitset
Returns:
cardinality


Copyright © 2014. All Rights Reserved.