Class BitVector
- All Implemented Interfaces:
Serializable
,Cloneable
Individual indexed bits can be examined, set, or cleared.
Subranges can quickly be extracted, copied and replaced.
Quick iteration over subranges is provided by optimized internal iterators (forEach() methods).
One BitVector
may be used to modify the contents of another
BitVector
through logical AND, OR, XOR and other similar operations.
All operations consider the bits 0..size()-1 and nothing else. Operations involving two bitvectors (like AND, OR, XOR, etc.) will throw an IllegalArgumentException if the secondary bit vector has a size smaller than the receiver.
A BitVector is never automatically resized, but it can manually be grown or shrinked via setSize(...).
For use cases that need to store several bits per information entity, quick accessors are provided that interpret subranges as 64 bit long integers.
Why this class? Fist, boolean[] take one byte per stored bit. This class takes one bit per stored bit. Second, many applications find the semantics of java.util.BitSet not particularly helpful for their needs. Third, operations working on all bits of a bitvector are extremely quick. For example, on NT, Pentium Pro 200 Mhz, SunJDK1.2.2, java -classic, for two bitvectors A,B (both much larger than processor cache), the following results are obtained.
- A.and(B) i.e. A = A invalid input: '&' B --> runs at about 35 MB/sec
- A.cardinality(), i.e. determining the selectivity, the number of bits in state "true" --> runs at about 80 MB/sec
- Similar performance for or, xor, andNot, not, copy, replace, partFromTo, indexOf, clear etc.
Note that this implementation is not synchronized.
- Version:
- 1.01, 11/10/99
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected long[]
The bits of this object.protected int
Fields inherited from class cern.colt.PersistentObject
serialVersionUID
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Performs a logical AND of the receiver with another bit vector (A = A invalid input: '&' B).void
Clears all of the bits in receiver whose corresponding bit is set in the other bitvector (A = A \ B).int
Returns the number of bits currently in the true state.protected static void
checkRangeFromTo
(int from, int to, int theSize) Checks if the given range is within the contained array's bounds.protected void
Sanity check for operations requiring another bitvector with at least the same size.void
clear()
Clears all bits of the receiver.void
clear
(int bitIndex) Changes the bit with index bitIndex to the "clear" (false) state.clone()
Cloning thisBitVector
produces a newBitVector
that is equal to it.copy()
Returns a deep copy of the receiver; callsclone()
and casts the result.long[]
elements()
You normally need not use this method.void
elements
(long[] bits, int size) You normally need not use this method.boolean
Compares this object against the specified object.boolean
forEachIndexFromToInState
(int from, int to, boolean state, IntProcedure procedure) Applies a procedure to each bit index within the specified range that holds a bit in the given state.boolean
get
(int bitIndex) Returns from the bitvector the value of the bit with the specified index.long
getLongFromTo
(int from, int to) Returns a long value representing bits of the receiver from index from to index to.boolean
getQuick
(int bitIndex) Returns from the bitvector the value of the bit with the specified index; WARNING: Does not check preconditions.int
hashCode()
Returns a hash code value for the receiver.int
indexOfFromTo
(int from, int to, boolean state) Returns the index of the first occurrence of the specified state.void
not()
Performs a logical NOT on the bits of the receiver (A = ~A).protected int
Returns the number of bits used in the trailing PARTIAL unit.protected int
Returns the number of units that are FULL (not PARTIAL).void
Performs a logical OR of the receiver with another bit vector (A = A | B).partFromTo
(int from, int to) Constructs and returns a new bit vector which is a copy of the given range.void
put
(int bitIndex, boolean value) Sets the bit with index bitIndex to the state specified by value.void
putLongFromTo
(long value, int from, int to) Sets bits of the receiver from indexfrom
to indexto
to the bits ofvalue
.void
putQuick
(int bitIndex, boolean value) Sets the bit with index bitIndex to the state specified by value; WARNING: Does not check preconditions.void
replaceFromToWith
(int from, int to, boolean value) Sets the bits in the given range to the state specified by value.void
replaceFromToWith
(int from, int to, BitVector source, int sourceFrom) Replaces the bits of the receiver in the given range with the bits of another bit vector.void
set
(int bitIndex) Changes the bit with index bitIndex to the "set" (true) state.void
setSize
(int newSize) Shrinks or expands the receiver so that it holds newSize bits.int
size()
Returns the size of the receiver.toString()
Returns a string representation of the receiver.void
Performs a logical XOR of the receiver with another bit vector (A = A ^ B).
-
Field Details
-
bits
protected long[] bitsThe bits of this object. The ith bit is stored in bits[i/64] at bit position i % 64 (where bit position 0 refers to the least significant bit and 63 refers to the most significant bit). -
nbits
protected int nbits
-
-
Constructor Details
-
BitVector
public BitVector(long[] bits, int size) You normally need not use this method. Use this method only if performance is critical. Constructs a bit vector with the given backing bits and size. WARNING: For efficiency reasons and to keep memory usage low, the array is not copied. So if subsequently you modify the specified array directly via the [] operator, be sure you know what you're doing.A bitvector is modelled as a long array, i.e. long[] bits holds bits of a bitvector. Each long value holds 64 bits. The i-th bit is stored in bits[i/64] at bit position i % 64 (where bit position 0 refers to the least significant bit and 63 refers to the most significant bit).
- Throws:
IllegalArgumentException
- if size < 0 || size > bits.length*64.
-
BitVector
public BitVector(int size) Constructs a bit vector that holds size bits. All bits are initially false.- Parameters:
size
- the number of bits the bit vector shall have.- Throws:
IllegalArgumentException
- if size < 0.
-
-
Method Details
-
and
Performs a logical AND of the receiver with another bit vector (A = A invalid input: '&' B). The receiver is modified so that a bit in it has the valuetrue
if and only if it already had the valuetrue
and the corresponding bit in the other bit vector argument has the valuetrue
.- Parameters:
other
- a bit vector.- Throws:
IllegalArgumentException
- if size() > other.size().
-
andNot
Clears all of the bits in receiver whose corresponding bit is set in the other bitvector (A = A \ B). In other words, determines the difference (A=A\B) between two bitvectors.- Parameters:
other
- a bitvector with which to mask the receiver.- Throws:
IllegalArgumentException
- if size() > other.size().
-
cardinality
public int cardinality()Returns the number of bits currently in the true state. Optimized for speed. Particularly quick if the receiver is either sparse or dense. -
checkRangeFromTo
protected static void checkRangeFromTo(int from, int to, int theSize) Checks if the given range is within the contained array's bounds. -
checkSize
Sanity check for operations requiring another bitvector with at least the same size. -
clear
public void clear()Clears all bits of the receiver. -
clear
public void clear(int bitIndex) Changes the bit with index bitIndex to the "clear" (false) state.- Parameters:
bitIndex
- the index of the bit to be cleared.- Throws:
IndexOutOfBoundsException
- if bitIndex<0 || bitIndex>=size()
-
clone
Cloning thisBitVector
produces a newBitVector
that is equal to it. The clone of the bit vector is another bit vector that has exactly the same bits set totrue
as this bit vector and the same current size, but independent state.- Overrides:
clone
in classPersistentObject
- Returns:
- a deep copy of this bit vector.
-
copy
Returns a deep copy of the receiver; callsclone()
and casts the result.- Returns:
- a deep copy of the receiver.
-
elements
public long[] elements()You normally need not use this method. Use this method only if performance is critical. Returns the bit vector's backing bits. WARNING: For efficiency reasons and to keep memory usage low, the array is not copied. So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing.A bitvector is modelled as a long array, i.e. long[] bits holds bits of a bitvector. Each long value holds 64 bits. The i-th bit is stored in bits[i/64] at bit position i % 64 (where bit position 0 refers to the least significant bit and 63 refers to the most significant bit).
-
elements
public void elements(long[] bits, int size) You normally need not use this method. Use this method only if performance is critical. Sets the bit vector's backing bits and size. WARNING: For efficiency reasons and to keep memory usage low, the array is not copied. So if subsequently you modify the specified array directly via the [] operator, be sure you know what you're doing.A bitvector is modelled as a long array, i.e. long[] bits holds bits of a bitvector. Each long value holds 64 bits. The i-th bit is stored in bits[i/64] at bit position i % 64 (where bit position 0 refers to the least significant bit and 63 refers to the most significant bit).
- Parameters:
bits
- the backing bits of the bit vector.size
- the number of bits the bit vector shall hold.- Throws:
IllegalArgumentException
- if size < 0 || size > bits.length*64.
-
equals
Compares this object against the specified object. The result istrue
if and only if the argument is notnull
and is aBitVector
object that has the same size as the receiver and the same bits set totrue
as the receiver. That is, for every nonnegativeint
indexk
,((BitVector)obj).get(k) == this.get(k)
must be true. -
forEachIndexFromToInState
Applies a procedure to each bit index within the specified range that holds a bit in the given state. Starts at index from, moves rightwards to to. Useful, for example, if you want to copy bits into an image or somewhere else.Optimized for speed. Particularly quick if one of the following conditions holds
- state==true and the receiver is sparse (cardinality() is small compared to size()).
- state==false and the receiver is dense (cardinality() is large compared to size()).
- Parameters:
from
- the leftmost search position, inclusive.to
- the rightmost search position, inclusive.state
- element to search for.procedure
- a procedure object taking as argument the current bit index. Stops iteration if the procedure returns false, otherwise continues.- Returns:
- false if the procedure stopped before all elements where iterated over, true otherwise.
- Throws:
IndexOutOfBoundsException
- if (size()>0 invalid input: '&'invalid input: '&' (from<0 || from>to || to>=size())).
-
get
public boolean get(int bitIndex) Returns from the bitvector the value of the bit with the specified index. The value is true if the bit with the index bitIndex is currently set; otherwise, returns false.- Parameters:
bitIndex
- the bit index.- Returns:
- the value of the bit with the specified index.
- Throws:
IndexOutOfBoundsException
- if bitIndex<0 || bitIndex>=size()
-
getLongFromTo
public long getLongFromTo(int from, int to) Returns a long value representing bits of the receiver from index from to index to. Bits are returned as a long value with the return value having bit 0 set to bitfrom
, ..., bitto-from
set to bitto
. All other bits of the return value are set to 0. If to-from+1==0 then returns zero (0L).- Parameters:
from
- index of start bit (inclusive).to
- index of end bit (inclusive).- Returns:
- the specified bits as long value.
- Throws:
IndexOutOfBoundsException
- if from<0 || from>=size() || to<0 || to>=size() || to-from+1invalid input: '<'0 || to-from+1>64
-
getQuick
public boolean getQuick(int bitIndex) Returns from the bitvector the value of the bit with the specified index; WARNING: Does not check preconditions. The value is true if the bit with the index bitIndex is currently set; otherwise, returns false.Provided with invalid parameters this method may return invalid values without throwing any exception. You should only use this method when you are absolutely sure that the index is within bounds. Precondition (unchecked): bitIndex >= 0 invalid input: '&'invalid input: '&' bitIndex < size().
- Parameters:
bitIndex
- the bit index.- Returns:
- the value of the bit with the specified index.
-
hashCode
public int hashCode()Returns a hash code value for the receiver. The hash code depends only on which bits have been set within the receiver. The algorithm used to compute it may be described as follows.Suppose the bits in the receiver were to be stored in an array of
long
integers called, say,bits
, in such a manner that bitk
is set in the receiver (for nonnegative values ofk
) if and only if the expression((k>>6) < bits.length) invalid input: '&'invalid input: '&' ((bits[k>>6] invalid input: '&' (1L << (bit invalid input: '&' 0x3F))) != 0)
is true. Then the following definition of thehashCode
method would be a correct implementation of the actual algorithm:public int hashCode() { long h = 1234; for (int i = bits.length; --i >= 0; ) { h ^= bits[i] * (i + 1); } return (int)((h >> 32) ^ h); }
Note that the hash code values change if the set of bits is altered. -
indexOfFromTo
public int indexOfFromTo(int from, int to, boolean state) Returns the index of the first occurrence of the specified state. Returns-1
if the receiver does not contain this state. Searches betweenfrom
, inclusive andto
, inclusive.Optimized for speed. Preliminary performance (200Mhz Pentium Pro, JDK 1.2, NT): size=10^6, from=0, to=size-1, receiver contains matching state in the very end --> 0.002 seconds elapsed time.
- Parameters:
from
- the leftmost search position, inclusive.to
- the rightmost search position, inclusive.state
- state to search for.- Returns:
- the index of the first occurrence of the element in the receiver; returns
-1
if the element is not found. - Throws:
IndexOutOfBoundsException
- if (size()>0 invalid input: '&'invalid input: '&' (from<0 || from>to || to>=size())).
-
not
public void not()Performs a logical NOT on the bits of the receiver (A = ~A). -
numberOfBitsInPartialUnit
protected int numberOfBitsInPartialUnit()Returns the number of bits used in the trailing PARTIAL unit. Returns zero if there is no such trailing partial unit. -
numberOfFullUnits
protected int numberOfFullUnits()Returns the number of units that are FULL (not PARTIAL). -
or
Performs a logical OR of the receiver with another bit vector (A = A | B). The receiver is modified so that a bit in it has the valuetrue
if and only if it either already had the valuetrue
or the corresponding bit in the other bit vector argument has the valuetrue
.- Parameters:
other
- a bit vector.- Throws:
IllegalArgumentException
- if size() > other.size().
-
partFromTo
Constructs and returns a new bit vector which is a copy of the given range. The new bitvector has size()==to-from+1.- Parameters:
from
- the start index within the receiver, inclusive.to
- the end index within the receiver, inclusive.- Throws:
IndexOutOfBoundsException
- if size()>0 invalid input: '&'invalid input: '&' (from<0 || from>to || to>=size())).
-
put
public void put(int bitIndex, boolean value) Sets the bit with index bitIndex to the state specified by value.- Parameters:
bitIndex
- the index of the bit to be changed.value
- the value to be stored in the bit.- Throws:
IndexOutOfBoundsException
- if bitIndex<0 || bitIndex>=size()
-
putLongFromTo
public void putLongFromTo(long value, int from, int to) Sets bits of the receiver from indexfrom
to indexto
to the bits ofvalue
. Bitfrom
is set to bit 0 ofvalue
, ..., bitto
is set to bitto-from
ofvalue
. All other bits stay unaffected. If to-from+1==0 then does nothing.- Parameters:
value
- the value to be copied into the receiver.from
- index of start bit (inclusive).to
- index of end bit (inclusive).- Throws:
IndexOutOfBoundsException
- if from<0 || from>=size() || to<0 || to>=size() || to-from+1invalid input: '<'0 || to-from+1>64.
-
putQuick
public void putQuick(int bitIndex, boolean value) Sets the bit with index bitIndex to the state specified by value; WARNING: Does not check preconditions.Provided with invalid parameters this method may set invalid values without throwing any exception. You should only use this method when you are absolutely sure that the index is within bounds. Precondition (unchecked): bitIndex >= 0 invalid input: '&'invalid input: '&' bitIndex < size().
- Parameters:
bitIndex
- the index of the bit to be changed.value
- the value to be stored in the bit.
-
replaceFromToWith
Replaces the bits of the receiver in the given range with the bits of another bit vector. Replaces the range [from,to] with the contents of the range [sourceFrom,sourceFrom+to-from], all inclusive. If source==this and the source and destination range intersect in an ambiguous way, then replaces as if using an intermediate auxiliary copy of the receiver.Optimized for speed. Preliminary performance (200Mhz Pentium Pro, JDK 1.2, NT): replace 10^6 ill aligned bits --> 0.02 seconds elapsed time.
- Parameters:
from
- the start index within the receiver, inclusive.to
- the end index within the receiver, inclusive.source
- the source bitvector to copy from.sourceFrom
- the start index within source, inclusive.- Throws:
IndexOutOfBoundsException
- if size()>0 invalid input: '&'invalid input: '&' (from<0 || from>to || to>=size() || sourceFrominvalid input: '<'0 || sourceFrom+to-from+1>source.size())).
-
replaceFromToWith
public void replaceFromToWith(int from, int to, boolean value) Sets the bits in the given range to the state specified by value.Optimized for speed. Preliminary performance (200Mhz Pentium Pro, JDK 1.2, NT): replace 10^6 ill aligned bits --> 0.002 seconds elapsed time.
- Parameters:
from
- the start index, inclusive.to
- the end index, inclusive.value
- the value to be stored in the bits of the range.- Throws:
IndexOutOfBoundsException
- if size()>0 invalid input: '&'invalid input: '&' (from<0 || from>to || to>=size()).
-
set
public void set(int bitIndex) Changes the bit with index bitIndex to the "set" (true) state.- Parameters:
bitIndex
- the index of the bit to be set.- Throws:
IndexOutOfBoundsException
- if bitIndex<0 || bitIndex>=size()
-
setSize
public void setSize(int newSize) Shrinks or expands the receiver so that it holds newSize bits. If the receiver is expanded, additional false bits are added to the end. If the receiver is shrinked, all bits between the old size and the new size are lost; their memory is subject to garbage collection. (This method introduces a new backing array of elements. WARNING: if you have more than one BitVector or BitMatrix sharing identical backing elements, be sure you know what you are doing.)- Parameters:
newSize
- the number of bits the bit vector shall have.- Throws:
IllegalArgumentException
- if size < 0.
-
size
public int size()Returns the size of the receiver. -
toString
Returns a string representation of the receiver. For every index for which the receiver contains a bit in the "set" (true) state, the decimal representation of that index is included in the result. Such indeces are listed in order from lowest to highest, separated by ", " (a comma and a space) and surrounded by braces. -
xor
Performs a logical XOR of the receiver with another bit vector (A = A ^ B). The receiver is modified so that a bit in it has the valuetrue
if and only if one of the following statements holds:- The bit initially has the value
true
, and the corresponding bit in the argument has the valuefalse
. - The bit initially has the value
false
, and the corresponding bit in the argument has the valuetrue
.
- Parameters:
other
- a bit vector.- Throws:
IllegalArgumentException
- if size() > other.size().
- The bit initially has the value
-