public class SparseBitSet extends Object implements Cloneable, Serializable
boolean value. The values of a
SparseBitSet are indexed by non-negative integers.
Individual indexed values may be examined, set, cleared, or modified by
logical operations. One SparseBitSet or logical value may be
used to modify the contents of (another) SparseBitSet through
logical AND, logical inclusive OR, logical exclusive
OR, and And NOT operations over all or part of the bit sets.
All values in a bit set initially have the value false.
Every bit set has a current size, which is the number of bits of space nominally in use by the bit set from the first set bit to just after the last set bit. The length of the bit set effectively tells the position available after the last bit of the SparseBitSet.
The maximum cardinality of a SparseBitSet is
Integer.MAX_VALUE, which means the bits of a
SparseBitSet are labelled
0 .. Integer.MAX_VALUE − 1.
After the last set bit of a SparseBitSet, any attempt to find
a subsequent bit (nextSetBit()), will return an value of −1.
If an attempt is made to use nextClearBit(), and all the bits are
set from the starting position of the search to the bit labelled
Integer.MAX_VALUE − 1, then similarly −1
will be returned.
Unless otherwise noted, passing a null parameter to any of the methods in
a SparseBitSet will result in a
NullPointerException.
A SparseBitSet is not safe for multi-threaded use without
external synchronization.
| Modifier and Type | Class and Description |
|---|---|
static class |
SparseBitSet.Statistics
These enumeration values are used as labels for the values in the String
created by the statistics() method.
|
| Constructor and Description |
|---|
SparseBitSet()
Constructs an empty bit set with the default initial size.
|
SparseBitSet(int nbits)
Creates a bit set whose initial size is large enough to efficiently
represent bits with indices in the range
0 through
at least nbits-1. |
| Modifier and Type | Method and Description |
|---|---|
void |
and(int i,
boolean value)
Performs a logical AND of the addressed target bit with the argument
value.
|
void |
and(int i,
int j,
SparseBitSet b)
Performs a logical AND of this target bit set with the argument bit
set within the given range of bits.
|
void |
and(SparseBitSet b)
Performs a logical AND of this target bit set with the argument bit
set.
|
static SparseBitSet |
and(SparseBitSet a,
SparseBitSet b)
Performs a logical AND of the two given
SparseBitSets. |
void |
andNot(int i,
boolean value)
Performs a logical AndNOT of the addressed target bit with the
argument value.
|
void |
andNot(int i,
int j,
SparseBitSet b)
Performs a logical AndNOT of this target bit set with the argument
bit set within the given range of bits.
|
void |
andNot(SparseBitSet b)
Performs a logical AndNOT of this target bit set with the argument
bit set.
|
static SparseBitSet |
andNot(SparseBitSet a,
SparseBitSet b)
Creates a bit set from thie first
SparseBitSet whose
corresponding bits are cleared by the set bits of the second
SparseBitSet. |
int |
cardinality()
Returns the number of bits set to
true in this
SparseBitSet. |
void |
clear()
Sets all of the bits in this
SparseBitSet to
false. |
void |
clear(int i)
Sets the bit at the specified index to
false. |
void |
clear(int i,
int j)
Sets the bits from the specified
i (inclusive) to the
specified j (exclusive) to false. |
SparseBitSet |
clone()
Cloning this
SparseBitSet produces a new
SparseBitSet that is equal() to it. |
boolean |
equals(Object obj)
Compares this object against the specified object.
|
void |
flip(int i)
Sets the bit at the specified index to the complement of its current value.
|
void |
flip(int i,
int j)
Sets each bit from the specified
i (inclusive) to the
specified j (exclusive) to the complement of its current
value. |
boolean |
get(int i)
Returns the value of the bit with the specified index.
|
SparseBitSet |
get(int i,
int j)
Returns a new
SparseBitSet composed of bits from this
SparseBitSet from i (inclusive) to j
(exclusive). |
int |
hashCode()
Returns a hash code value for this bit set.
|
boolean |
intersects(int i,
int j,
SparseBitSet b)
Returns true if the specified
SparseBitSet has any bits
within the given range i (inclusive) to j
(exclusive) set to true that are also set to true
in the same range of this SparseBitSet. |
boolean |
intersects(SparseBitSet b)
Returns true if the specified
SparseBitSet has any bits set to
true that are also set to true in this
SparseBitSet. |
boolean |
isEmpty()
Returns true if this
SparseBitSet contains no bits that are
set to true. |
int |
length()
Returns the "logical length" of this
SparseBitSet: the index
of the highest set bit in the SparseBitSet plus one. |
int |
nextClearBit(int i)
Returns the index of the first bit that is set to
false that
occurs on or after the specified starting index. |
int |
nextSetBit(int i)
Returns the index of the first bit that is set to
true that
occurs on or after the specified starting index. |
void |
or(int i,
boolean value)
Performs a logical OR of the addressed target bit with the
argument value.
|
void |
or(int i,
int j,
SparseBitSet b)
Performs a logical OR of the addressed target bit with the
argument value within the given range.
|
void |
or(SparseBitSet b)
Performs a logical OR of this bit set with the bit set argument.
|
static SparseBitSet |
or(SparseBitSet a,
SparseBitSet b)
Performs a logical OR of the two given
SparseBitSets. |
int |
previousClearBit(int i)
Returns the index of the nearest bit that is set to
false
that occurs on or before the specified starting index. |
int |
previousSetBit(int i)
Returns the index of the nearest bit that is set to
true
that occurs on or before the specified starting index. |
void |
set(int i)
Sets the bit at the specified index.
|
void |
set(int i,
boolean value)
Sets the bit at the specified index to the specified value.
|
void |
set(int i,
int j)
Sets the bits from the specified
i (inclusive) to the specified
j (exclusive) to true. |
void |
set(int i,
int j,
boolean value)
Sets the bits from the specified
i (inclusive) to the specified
j (exclusive) to the specified value. |
int |
size()
Returns the number of bits of space nominally in use by this
SparseBitSet to represent bit values. |
String |
statistics()
Convenience method for statistics if the individual results are not needed.
|
String |
statistics(String[] values)
Determine, and create a String with the bit set statistics.
|
String |
toString()
Returns a string representation of this bit set.
|
void |
toStringCompaction(boolean change)
If change is
true, the current value of the
toStringCompaction() value is made the default value for all
SparseBitSets created from this point onward in this JVM. |
void |
toStringCompaction(int count)
Sequences of set bits longer than this value are shown by
toString() as a "sub-sequence," in the form a..b. |
void |
xor(int i,
boolean value)
Performs a logical XOR of the addressed target bit with the
argument value.
|
void |
xor(int i,
int j,
SparseBitSet b)
Performs a logical XOR of this bit set with the bit set argument
within the given range.
|
void |
xor(SparseBitSet b)
Performs a logical XOR of this bit set with the bit set argument.
|
static SparseBitSet |
xor(SparseBitSet a,
SparseBitSet b)
Performs a logical XOR of the two given
SparseBitSets. |
public SparseBitSet()
false.public SparseBitSet(int nbits)
throws NegativeArraySizeException
0 through
at least nbits-1. Initially all bits are effectively
false.
No guarantees are given for how large or small the actual object will be. The setting of bits above the given range is permitted (and will perhaps eventually cause resizing).
nbits - the initial provisional length of the SparseBitSetNegativeArraySizeException - if the specified initial
length is negativeSparseBitSet()public void and(int i,
boolean value)
throws IndexOutOfBoundsException
true if and only if it both initially had the value
true and the argument value is also true.i - a bit indexvalue - a boolean value to AND with that bitIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic void and(int i,
int j,
SparseBitSet b)
throws IndexOutOfBoundsException
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. Outside
the range, this set is not changed.i - index of the first bit to be included in the operationj - index after the last bit to included in the operationb - a SparseBitSetIndexOutOfBoundsException - if i is negative or
equal to Integer.MAX_VALUE, or j is negative,
or i is larger than jpublic void and(SparseBitSet b)
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.b - a SparseBitSetpublic static SparseBitSet and(SparseBitSet a, SparseBitSet b)
SparseBitSets.
The returned SparseBitSet is created so that each bit in it
has the value true if and only if both the given sets
initially had the corresponding bits true, otherwise
false.a - a SparseBitSetb - another SparseBitSetpublic void andNot(int i,
boolean value)
true if and only if it both initially had the value
true and the argument value is false.i - a bit indexvalue - a boolean value to AndNOT with that bitIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic void andNot(int i,
int j,
SparseBitSet b)
throws IndexOutOfBoundsException
true if and only
if it both initially had the value true and the corresponding
bit in the bit set argument has the value false. Outside
the range, this set is not changed.i - index of the first bit to be included in the operationj - index after the last bit to included in the operationb - the SparseBitSet with which to mask this SparseBitSetIndexOutOfBoundsException - if i is negative or
equal to Integer.MAX_VALUE, or j is negative,
or i is larger than jpublic void andNot(SparseBitSet b)
true if and only if it both initially had the value
true and the corresponding bit in the bit set argument has
the value false.b - the SparseBitSet with which to mask this SparseBitSetpublic static SparseBitSet andNot(SparseBitSet a, SparseBitSet b)
SparseBitSet whose
corresponding bits are cleared by the set bits of the second
SparseBitSet. The resulting bit set is created so that a bit
in it has the value true if and only if the corresponding bit
in the SparseBitSet of the first is set, and that same
corresponding bit is not set in the SparseBitSet of the second
argument.a - a SparseBitSetb - another SparseBitSetpublic int cardinality()
true in this
SparseBitSet.public void clear(int i)
false.i - a bit index.IndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUE.public void clear(int i,
int j)
throws IndexOutOfBoundsException
i (inclusive) to the
specified j (exclusive) to false.i - index of the first bit to be clearedj - index after the last bit to be clearedIndexOutOfBoundsException - if i is negative or
equal to Integer.MAX_VALUE, or j is negative,
or i is larger than jpublic void clear()
SparseBitSet to
false.public SparseBitSet clone()
SparseBitSet produces a new
SparseBitSet 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.
Note: the actual space allocated to the clone tries to minimise the actual
amount of storage allocated to hold the bits, while still trying to
keep access to the bits being a rapid as possible. Since the space
allocated to a SparseBitSet is not normally decreased,
replacing a bit set by its clone may be a way of both managing memory
consumption and improving the rapidity of access.
public boolean equals(Object obj)
true if and only if the argument is not null
and is a SparseBitSet object that has exactly the same bits
set to true as this bit set. That is, for every nonnegative
i indexing a bit in the set,
((SparseBitSet)obj).get(i) == this.get(i)must be true.
public void flip(int i)
i - the index of the bit to flipIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic void flip(int i,
int j)
throws IndexOutOfBoundsException
i (inclusive) to the
specified j (exclusive) to the complement of its current
value.i - index of the first bit to flipj - index after the last bit to flipIndexOutOfBoundsException - if i is negative or is
equal to Integer.MAX_VALUE, or j is negative, or
i is larger than jpublic boolean get(int i)
true if the bit with the index i is currently set
in this SparseBitSet; otherwise, the result is
false.i - the bit indexIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic SparseBitSet get(int i, int j) throws IndexOutOfBoundsException
SparseBitSet composed of bits from this
SparseBitSet from i (inclusive) to j
(exclusive).i - index of the first bit to includej - index after the last bit to includeIndexOutOfBoundsException - if i is negative or is
equal to Integer.MAX_VALUE, or j is negative, or
i is larger than jpublic int hashCode()
SparseBitSet. The
algorithm used to compute it may be described as follows.
Suppose the bits in the SparseBitSet were to be stored in an
array of long integers called, say, bits, in such
a manner that bit i is set in the SparseBitSet
(for nonnegative values of i) if and only if the expression
((i>>6) < bits.length) && ((bits[i>>6] & (1L << (bit & 0x3F))) != 0)is true. Then the following definition of the
hashCode method
would be a correct implementation of the actual algorithm:
public int hashCode()
{
long hash = 1234L;
for( int i = bits.length; --i >= 0; )
hash ^= bits[i] * (i + 1);
return (int)((h >> 32) ^ h);
}
Note that the hash code values change if the set of bits is altered.hashCode in class ObjectObject.equals(Object),
Hashtablepublic boolean intersects(int i,
int j,
SparseBitSet b)
throws IndexOutOfBoundsException
SparseBitSet has any bits
within the given range i (inclusive) to j
(exclusive) set to true that are also set to true
in the same range of this SparseBitSet.i - index of the first bit to includej - index after the last bit to includeb - the SparseBitSet with which to intersectIndexOutOfBoundsException - if i is negative or
equal to Integer.MAX_VALUE, or j is negative,
or i is larger than jpublic boolean intersects(SparseBitSet b)
SparseBitSet has any bits set to
true that are also set to true in this
SparseBitSet.b - a SparseBitSet with which to intersectpublic boolean isEmpty()
SparseBitSet contains no bits that are
set to true.public int length()
SparseBitSet: the index
of the highest set bit in the SparseBitSet plus one. Returns
zero if the SparseBitSet contains no set bits.public int nextClearBit(int i)
false that
occurs on or after the specified starting index.i - the index to start checking from (inclusive)IndexOutOfBoundsException - if the specified index is negativepublic int nextSetBit(int i)
true that
occurs on or after the specified starting index. If no such it exists then
-1 is returned.
To iterate over the true bits in a SparseBitSet
sbs, use the following loop:
for( int i = sbbits.nextSetBit(0); i >= 0; i = sbbits.nextSetBit(i+1) )
{
// operate on index i here
}i - the index to start checking from (inclusive)IndexOutOfBoundsException - if the specified index is negativepublic int previousClearBit(int i)
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.i - the index to start checking from (inclusive)-1 if there
is no such bitIndexOutOfBoundsException - if the specified index is less
than -1BitSet.previousClearBit(int)public int previousSetBit(int i)
true
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.i - the index to start checking from (inclusive)-1 if there
is no such bitIndexOutOfBoundsException - if the specified index is less
than -1BitSet.previousSetBit(int)public void or(int i,
boolean value)
true if and only if it both initially had the value
true or the argument value is true.i - a bit indexvalue - a boolean value to OR with that bitIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic void or(int i,
int j,
SparseBitSet b)
throws IndexOutOfBoundsException
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. Outside the range
this set is not changed.i - index of the first bit to be included in the operationj - index after the last bit to included in the operationb - the SparseBitSet with which to perform the OR
operation with this SparseBitSetIndexOutOfBoundsException - if i is negative or
equal to Integer.MAX_VALUE, or j is negative,
or i is larger than jpublic void or(SparseBitSet b)
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.b - the SparseBitSet with which to perform the OR
operation with this SparseBitSetpublic static SparseBitSet or(SparseBitSet a, SparseBitSet b)
SparseBitSets.
The returned SparseBitSet is created so that a bit in it has
the value true if and only if it either had the value
true in the set given by the first arguemetn or had the value
true in the second argument, otherwise false.a - a SparseBitSetb - another SparseBitSetpublic void set(int i)
i - a bit indexIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic void set(int i,
boolean value)
i - a bit indexvalue - a boolean value to setIndexOutOfBoundsException - if the specified index is negative
or equal to Integer.MAX_VALUEpublic void set(int i,
int j)
throws IndexOutOfBoundsException
i (inclusive) to the specified
j (exclusive) to true.i - index of the first bit to be setj - index after the last bit to be seIndexOutOfBoundsException - if i is negative or is
equal to Integer.MAX_INT, or j is negative, or
i is larger than j.public void set(int i,
int j,
boolean value)
i (inclusive) to the specified
j (exclusive) to the specified value.i - index of the first bit to be setj - index after the last bit to be setvalue - to which to set the selected bitsIndexOutOfBoundsException - if i is negative or is
equal to Integer.MAX_VALUE, or j is negative, or
i is larger than jpublic int size()
SparseBitSet to represent bit values. The count of bits in
the set is the (label of the last set bit) + 1 - (the label of the first
set bit).public String statistics()
statistics(String[])public String statistics(String[] values)
This method is intended for diagnostic use (as it is relatively expensive
in time), but can be useful in understanding an application's use of a
SparseBitSet.
values - an array for the individual results (if not null)public String toString()
SparseBitSet contains a bit in the set state, the decimal
representation of that index is included in the result. Such indices are
listed in order from lowest to highest. If there is a subsequence of set
bits longer than the value given by toStringCompaction, the subsequence
is represented by the value for the first and the last values, with ".."
between them. The individual bits, or the representation of sub-sequences
are separated by ", " (a comma and a space) and surrounded by braces,
resulting in a compact string showing (a variant of) the usual mathematical
notation for a set of integers.
SparseBitSet drPepper = new SparseBitSet();
Now drPepper.toString() returns "{}".
drPepper.set(2);
Now drPepper.toString() returns "{2}".
drPepper.set(3, 4);
drPepper.set(10);
Now drPepper.toString() returns "{2..4, 10}".
SparseBitSet.toString in class ObjecttoStringCompaction(int length)public void toStringCompaction(int count)
toString() as a "sub-sequence," in the form a..b.
Setting this value to zero causes each set bit to be listed individually.
The default default value is 2 (which means sequences of three or more
bits set are shown as a subsequence, and all other set bits are listed
individually).
Note: this value will be passed to SparseBitSets that
may be created within or as a result of the operations on this bit set,
or, for static methods, from the value belonging to the first parameter.
count - the maximum count of a run of bits that are shown as
individual entries in a toString() conversion.
If 0, all bits are shown individually.toString()public void toStringCompaction(boolean change)
true, the current value of the
toStringCompaction() value is made the default value for all
SparseBitSets created from this point onward in this JVM.change - if true, change the default valuepublic void xor(int i,
boolean value)
true if and only one of the following statements holds:
true, and the
value of the argument is false.
false, and the
value of the argument is true.
i - a bit indexvalue - a boolean value to XOR with that bitIndexOutOfBoundsException - if the specified index
is negative
or equal to Integer.MAX_VALUEpublic void xor(int i,
int j,
SparseBitSet b)
throws IndexOutOfBoundsException
true if and only if one
of the following statements holds:
true, and the
corresponding bit in the argument set has the value false.
false, and the
corresponding bit in the argument set has the value true.
i - index of the first bit to be included in the operationj - index after the last bit to included in the operationb - the SparseBitSet with which to perform the XOR
operation with this SparseBitSetIndexOutOfBoundsException - if i is negative or
equal to Integer.MAX_VALUE, or j is negative,
or i is larger than jpublic void xor(SparseBitSet b)
true if and only if one of the following statements holds:
true, and the
corresponding bit in the argument set has the value false.
false, and the
corresponding bit in the argument set has the value true.
b - the SparseBitSet with which to perform the XOR
operation with thisSparseBitSetpublic static SparseBitSet xor(SparseBitSet a, SparseBitSet b)
SparseBitSets.
The resulting bit set is created so that a bit in it has the value
true if and only if one of the following statements holds:
true, and the
corresponding bit in the second argument has the value
false.false, and the
corresponding bit in the second argument has the value
true.a - a SparseBitSetb - another SparseBitSetCopyright © 2018 Zaxxer.com. All rights reserved.