public class BiggerBitSet
extends java.lang.Object
implements java.lang.Cloneable
BitSet
class in
a few ways:
1.) BiggerBitSet uses long
instead of int
to address bits
in its API which greatly increases the size of bits addresable. The
implementation of BitSet allows a maximum number of pixels of (2^31 - 1)
or 2147483647, which is about 2 billion pixels. If 3-D printing at a
resolution of 0.1 mm/pixel, this gives a maximum size of about 5.07 inches
on a side.
((2^31-1))^(1/3) 0.1 mm -> in
This is not quite big enough to fit a modern 3-D printer's bed.
With this class's API, the number of pixels is increased by a factor of
64, giving over 137 billion pixels. The calculation is:
((2^31-1) * 64)^(1/3) 0.1 mm -> in
which gives a cube of about 20 inches on a side. This is enough of an
improvement to warrant the class.
2.) BiggerBitSet does not allow itself to be dynamically resized; this
should give a performance improvement over BitSet. BitSet takes a lot of
effort to validate invariants and resize with every set.
3.) BiggerBitSet is constant across Java releases and implements functions
(especially those to set a range of values quickly and efficiently) which
did not exist in earlier versions of Java's BitSet.Modifier and Type | Field and Description |
---|---|
private static int |
ADDRESS_BITS_PER_WORD |
private static int |
BIT_INDEX_MASK |
private static int |
BITS_PER_WORD |
private static long |
WORD_MASK |
private long[] |
words
The internal field corresponding to the serialField "bits".
|
Constructor and Description |
---|
BiggerBitSet(long nbits)
Creates a bit set whose initial size is large enough to explicitly
represent bits with indices in the range
0 through
nbits-1 . |
Modifier and Type | Method and Description |
---|---|
void |
and(BiggerBitSet set)
Performs a logical AND of this target bit set with the
argument bit set.
|
void |
andNot(BiggerBitSet set)
Clears all of the bits in this
BiggerBitSet whose corresponding
bit is set in the specified BiggerBitSet . |
static int |
bitCountLong(long i)
Returns the number of one-bits in the two's complement binary
representation of the specified
long value. |
long |
cardinality()
Returns the number of bits set to
true in this BiggerBitSet . |
private static void |
checkRange(long fromIndex,
long toIndex)
Checks that fromIndex ...
|
void |
clear()
Sets all of the bits in this BiggerBitSet to
false . |
void |
clear(long bitIndex)
Sets the bit specified by the index to
false . |
void |
clearRange(long fromIndex,
long toIndex)
Sets the bits from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to false . |
java.lang.Object |
clone()
Cloning this
BiggerBitSet produces a new BiggerBitSet
that is equal to it. |
boolean |
equals(java.lang.Object obj)
Compares this object against the specified object.
|
void |
flip(long bitIndex)
Sets the bit at the specified index to the complement of its
current value.
|
void |
flip(long fromIndex,
long toIndex)
Sets each bit from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to the complement of its current
value. |
boolean |
get(long bitIndex)
Returns the value of the bit with the specified index.
|
int |
hashCode()
Returns the hash code value for this bit set.
|
private void |
initWords(long nbits)
Create the array of words.
|
boolean |
intersects(BiggerBitSet set)
Returns true if the specified
BiggerBitSet has any bits set to
true that are also set to true in this BiggerBitSet . |
boolean |
isEmpty()
Returns true if this
BiggerBitSet contains no bits that are set
to true . |
long |
length()
Returns the "logical size" of this
BiggerBitSet : the index of
the highest set bit in the BiggerBitSet plus one. |
long |
nextClearBit(long fromIndex)
Returns the index of the first bit that is set to
false
that occurs on or after the specified starting index. |
long |
nextSetBit(long fromIndex)
Returns the index of the first bit that is set to
true
that occurs on or after the specified starting index. |
private long |
nextSetBit(long fromIndex,
long toWordIndex)
Returns the index of the first bit that is set to
true
that occurs on or after the specified starting index and up to and
including the specified word index
If no such bit exists then -1 is returned. |
static int |
numberOfLeadingZerosInt(int i)
Returns the number of zero bits preceding the highest-order
("leftmost") one-bit in the two's complement binary representation
of the specified
int value. |
static int |
numberOfLeadingZerosLong(long i)
Returns the number of zero bits preceding the highest-order
("leftmost") one-bit in the two's complement binary representation
of the specified
long value. |
static int |
numberOfTrailingZerosInt(int i)
Returns the number of zero bits following the lowest-order ("rightmost")
one-bit in the two's complement binary representation of the specified
int value. |
static int |
numberOfTrailingZerosLong(long i)
Returns the number of zero bits following the lowest-order ("rightmost")
one-bit in the two's complement binary representation of the specified
long value. |
void |
or(BiggerBitSet set)
Performs a logical OR of this bit set with the bit set
argument.
|
long |
previousClearBit(long fromIndex)
Returns the index of the nearest bit that is set to
false
that occurs on or before the specified starting index. |
long |
previousSetBit(long fromIndex)
Returns the index of the nearest bit that is set to
true
that occurs on or before the specified starting index. |
void |
set(long bitIndex)
Sets the bit at the specified index to
true . |
void |
set(long bitIndex,
boolean value)
Sets the bit at the specified index to the specified value.
|
void |
setRange(long fromIndex,
long toIndex)
Sets the bits from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to true . |
void |
setRange(long fromIndex,
long toIndex,
boolean value)
Sets the bits from the specified
fromIndex (inclusive) to the
specified toIndex (exclusive) to the specified value. |
long |
size()
Returns the number of bits of space actually in use by this
BiggerBitSet to represent bit values. |
private static int |
wordIndex(long bitIndex)
Given a bit index, return word index containing it.
|
void |
xor(BiggerBitSet set)
Performs a logical XOR of this bit set with the bit set
argument.
|
private static final int ADDRESS_BITS_PER_WORD
private static final int BITS_PER_WORD
private static final int BIT_INDEX_MASK
private static final long WORD_MASK
private long[] words
public BiggerBitSet(long nbits)
0
through
nbits-1
. All bits are initially false
.nbits
- the size of the bit setjava.lang.NegativeArraySizeException
- if the specified initial size
is negativeprivate static int wordIndex(long bitIndex)
private void initWords(long nbits)
private static void checkRange(long fromIndex, long toIndex)
public void flip(long bitIndex)
bitIndex
- the index of the bit to flipjava.lang.IndexOutOfBoundsException
- if the specified index is negativepublic void flip(long fromIndex, long toIndex)
fromIndex
(inclusive) to the
specified toIndex
(exclusive) to the complement of its current
value.fromIndex
- index of the first bit to fliptoIndex
- index after the last bit to flipjava.lang.IndexOutOfBoundsException
- if fromIndex
is negative,
or toIndex
is negative, or fromIndex
is
larger than toIndex
public void set(long bitIndex)
true
.bitIndex
- a bit indexjava.lang.IndexOutOfBoundsException
- if the specified index is negativepublic void set(long bitIndex, boolean value)
bitIndex
- a bit indexvalue
- a boolean value to setjava.lang.IndexOutOfBoundsException
- if the specified index is negativepublic void setRange(long fromIndex, long toIndex)
fromIndex
(inclusive) to the
specified toIndex
(exclusive) to true
.fromIndex
- index of the first bit to be settoIndex
- index after the last bit to be setjava.lang.IndexOutOfBoundsException
- if fromIndex
is negative,
or toIndex
is negative, or fromIndex
is
larger than toIndex
public void setRange(long fromIndex, long toIndex, boolean value)
fromIndex
(inclusive) to the
specified toIndex
(exclusive) to the specified value.fromIndex
- index of the first bit to be settoIndex
- index after the last bit to be setvalue
- value to set the selected bits tojava.lang.IndexOutOfBoundsException
- if fromIndex
is negative,
or toIndex
is negative, or fromIndex
is
larger than toIndex
public void clear(long bitIndex)
false
.bitIndex
- the index of the bit to be clearedjava.lang.IndexOutOfBoundsException
- if the specified index is negativepublic void clearRange(long fromIndex, long toIndex)
fromIndex
(inclusive) to the
specified toIndex
(exclusive) to false
.fromIndex
- index of the first bit to be clearedtoIndex
- index after the last bit to be clearedjava.lang.IndexOutOfBoundsException
- if fromIndex
is negative,
or toIndex
is negative, or fromIndex
is
larger than toIndex
public void clear()
false
.public boolean get(long bitIndex)
true
if the bit with the index bitIndex
is currently set in this BiggerBitSet
; otherwise, the result
is false
.bitIndex
- the bit indexjava.lang.IndexOutOfBoundsException
- if the specified index is negativepublic long nextSetBit(long fromIndex)
true
that occurs on or after the specified starting index. If no such
bit exists then -1
is returned.
To iterate over the true
bits in a BiggerBitSet
,
use the following loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
// operate on index i here
if (i == Integer.MAX_VALUE) {
break; // or (i+1) would overflow
}
}
fromIndex
- the index to start checking from (inclusive)-1
if there
is no such bitjava.lang.IndexOutOfBoundsException
- if the specified index is negativepublic long nextClearBit(long fromIndex)
false
that occurs on or after the specified starting index.fromIndex
- the index to start checking from (inclusive)java.lang.IndexOutOfBoundsException
- if the specified index is negativepublic long previousSetBit(long fromIndex)
true
that occurs on or before the specified starting index.
If no such bit exists, or if a negative index is given as the
starting index, then -1
is returned.
To iterate over the true
bits in a BiggerBitSet
,
use the following loop:
for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
// operate on index i here
}
fromIndex
- the index to start checking from (inclusive)-1
if there
is no such bitjava.lang.IndexOutOfBoundsException
- if the specified index is less
than -1
public long previousClearBit(long fromIndex)
false
that occurs on or before the specified starting index.
If no such bit exists, or if -1
is given as the
starting index, then -1
is returned.fromIndex
- the index to start checking from (inclusive)-1
if there
is no such bitjava.lang.IndexOutOfBoundsException
- if the specified index is less
than -1
public long length()
BiggerBitSet
: the index of
the highest set bit in the BiggerBitSet
plus one. Returns zero
if the BiggerBitSet
contains no set bits.BiggerBitSet
public boolean isEmpty()
BiggerBitSet
contains no bits that are set
to true
.BiggerBitSet
is emptypublic boolean intersects(BiggerBitSet set)
BiggerBitSet
has any bits set to
true
that are also set to true
in this BiggerBitSet
.set
- BiggerBitSet
to intersect withBiggerBitSet
intersects
the specified BiggerBitSet
public long cardinality()
true
in this BiggerBitSet
.true
in this
BiggerBitSet
public void and(BiggerBitSet set)
true
if and only if it both initially
had the value true
and the corresponding bit in the
bit set argument also had the value true
.set
- a bit setpublic void or(BiggerBitSet set)
true
if and only if it either already had the
value true
or the corresponding bit in the bit set
argument has the value true
.set
- a bit setpublic void xor(BiggerBitSet set)
true
if and only if one of the following
statements holds:
true
, and the
corresponding bit in the argument has the value false
.
false
, and the
corresponding bit in the argument has the value true
.
set
- a bit setpublic void andNot(BiggerBitSet set)
BiggerBitSet
whose corresponding
bit is set in the specified BiggerBitSet
.set
- the BiggerBitSet
with which to mask this
BiggerBitSet
public int hashCode()
BiggerBitSet
.
The hash code is defined to be the result of the following calculation:
public int hashCode() {
long h = 1234;
long[] words = toLongArray();
for (int i = words.length; --i >= 0; )
h ^= words[i] * (i + 1);
return (int)((h >> 32) ^ h);
}
Note that the hash code changes if the set of bits is altered.hashCode
in class java.lang.Object
public long size()
BiggerBitSet
to represent bit values.
The maximum element in the set is the size - 1st element.public boolean equals(java.lang.Object obj)
true
if and only if the argument is
not null
and is a Bitset
object that has
exactly the same set of bits set to true
as this bit
set. That is, for every nonnegative int
index k
,
((BiggerBitSet)obj).get(k) == this.get(k)must be true. The current sizes of the two bit sets are not compared.
equals
in class java.lang.Object
obj
- the object to compare withtrue
if the objects are the same;
false
otherwisesize()
public java.lang.Object clone()
BiggerBitSet
produces a new BiggerBitSet
that is equal to it.
The clone of the bit set is another bit set that has exactly the
same bits set to true
as this bit set.clone
in class java.lang.Object
size()
private long nextSetBit(long fromIndex, long toWordIndex)
true
that occurs on or after the specified starting index and up to and
including the specified word index
If no such bit exists then -1
is returned.fromIndex
- the index to start checking from (inclusive)toWordIndex
- the last word index to check (inclusive)-1
if there
is no such bitpublic static int numberOfLeadingZerosInt(int i)
int
value. Returns 32 if the
specified value has no one-bits in its two's complement representation,
in other words if it is equal to zero.
Note that this method is closely related to the logarithm base 2.
For all positive int
values x:
31 - numberOfLeadingZeros(x)
32 - numberOfLeadingZeros(x - 1)
i
- the value whose number of leading zeros is to be computedint
value, or 32 if the value
is equal to zero.
TODO: Replace this with Integer.numberOfTrailingZeros when migrating to
Java 1.5 or later.public static int numberOfTrailingZerosInt(int i)
int
value. Returns 32 if the specified value has no
one-bits in its two's complement representation, in other words if it is
equal to zero.i
- the value whose number of trailing zeros is to be computedint
value, or 32 if the value is equal
to zero.
TODO: Replace this with Integer.numberOfTrailingZeros when migrating to
Java 1.5 or later.public static int numberOfLeadingZerosLong(long i)
long
value. Returns 64 if the
specified value has no one-bits in its two's complement representation,
in other words if it is equal to zero.
Note that this method is closely related to the logarithm base 2.
For all positive long
values x:
63 - numberOfLeadingZeros(x)
64 - numberOfLeadingZeros(x - 1)
i
- the value whose number of leading zeros is to be computedlong
value, or 64 if the value
is equal to zero.
TODO: Replace this with Long.numberOfLeadingZeros when migrating to
Java 1.5 or later.public static int numberOfTrailingZerosLong(long i)
long
value. Returns 64 if the specified value has no
one-bits in its two's complement representation, in other words if it is
equal to zero.i
- the value whose number of trailing zeros is to be computedlong
value, or 64 if the value is equal
to zero.
TODO: Replace this with Long.numberOfTrailingZeros when migrating to
Java 1.5 or later.public static int bitCountLong(long i)
long
value. This function is
sometimes referred to as the population count.i
- the value whose bits are to be countedlong
value.
TODO: Replace this with Long.bitCount when migrating to
Java 1.5 or later.