public class FixedSizeBitSet
extends java.lang.Object
BitSet
. Changes are that the FixedSizeBitSet doesn't expand, so access to it
doesn't need to be synchronized, plus we only need a few methods so most methods of the old class have been removed.Modifier and Type | Field and Description |
---|---|
protected static int |
ADDRESS_BITS_PER_WORD |
protected static int |
BITS_PER_WORD |
protected int |
size |
protected static long |
WORD_MASK |
protected long[] |
words |
Constructor and Description |
---|
FixedSizeBitSet() |
FixedSizeBitSet(int size)
Creates a bit set whose initial size is the range
0 through
size-1 . |
Modifier and Type | Method and Description |
---|---|
int |
cardinality()
Returns the number of bits set to true in this bit set
|
void |
clear(int index)
Sets the bit specified by the index to
false . |
void |
clear(int from,
int to)
Sets the bits from the specified
from (inclusive) to the
specified to (inclusive) to false . |
void |
flip()
Flips all bits: 1 --> 0 and 0 --> 1
|
boolean |
get(int index)
Returns the value of the bit with the specified index.
|
int |
nextClearBit(int fromIndex)
Returns the index of the first bit that is set to
false
that occurs on or after the specified starting index. |
int |
nextSetBit(int fromIndex)
Returns the index of the first bit that is set to
true that occurs on or after
the specified starting index. |
int |
previousSetBit(int from)
Returns the index of the nearest bit that is set to
true
that occurs on or before the specified starting index. |
boolean |
set(int index)
Sets the bit at the specified index to
true . |
void |
set(int from,
int to)
Sets the bits from the specified
from (inclusive) to the
specified to (inclusive) to true . |
int |
size() |
java.lang.String |
toString()
Returns a string representation of this bit set.
|
protected static int |
wordIndex(int bitIndex) |
protected static final int ADDRESS_BITS_PER_WORD
protected static final int BITS_PER_WORD
protected static final long WORD_MASK
protected long[] words
protected int size
public FixedSizeBitSet()
public FixedSizeBitSet(int size)
0
through
size-1
. All bits are initially false
.size
- the initial size of the bit set (in bits).java.lang.NegativeArraySizeException
- if the specified initial size is negativepublic boolean set(int index)
true
.index
- a bit index.java.lang.IndexOutOfBoundsException
- if the specified index is negative.public void set(int from, int to)
from
(inclusive) to the
specified to
(inclusive) to true
.from
- index of the first bit to be setto
- index of the last bit to be setjava.lang.IndexOutOfBoundsException
- if from
is negative, or to
is negative, or from
is
larger than to
public void clear(int index)
false
.index
- the index of the bit to be cleared.java.lang.IndexOutOfBoundsException
- if the specified index is negative.public void clear(int from, int to)
from
(inclusive) to the
specified to
(inclusive) to false
.from
- index of the first bit to be clearedto
- index of the last bit to be clearedjava.lang.IndexOutOfBoundsException
- if from
is negative, or to
is negative,
or from
is larger than to
public boolean get(int index)
true
if the bit with the index index
is currently set in this bit set; otherwise, the result is false
.index
- the bit index.java.lang.IndexOutOfBoundsException
- if the specified index is negative.public int nextSetBit(int 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 BitSet
,
use the following loop:
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { // operate on index i here }
fromIndex
- the index to start checking from (inclusive).java.lang.IndexOutOfBoundsException
- if the specified index is negative.public int nextClearBit(int 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 negative.public int previousSetBit(int from)
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.from
- 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 int cardinality()
public int size()
public void flip()
public java.lang.String toString()
BitSet
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, separated by ", " (a comma and a space) and
surrounded by braces, resulting in the usual mathematical
notation for a set of integers.
Overrides the toString
method of Object
.
Example:
BitSet drPepper = new BitSet();Now
drPepper.toString()
returns "{}
".
drPepper.set(2);Now
drPepper.toString()
returns "{2}
".
drPepper.set(4); drPepper.set(10);Now
drPepper.toString()
returns "{2, 4, 10}
".toString
in class java.lang.Object
protected static int wordIndex(int bitIndex)
Copyright © 1998-2020 Red Hat. All Rights Reserved.