Package org.jgroups.util
Class FixedSizeBitSet
- java.lang.Object
-
- org.jgroups.util.FixedSizeBitSet
-
- Direct Known Subclasses:
SeqnoList
public class FixedSizeBitSet extends java.lang.Object
Class copied fromBitSet
. 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.- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description FixedSizeBitSet()
FixedSizeBitSet(int size)
Creates a bit set whose initial size is the range0
throughsize-1
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
cardinality()
Returns the number of bits set to true in this bit setvoid
clear(int index)
Sets the bit specified by the index tofalse
.void
clear(int from, int to)
Sets the bits from the specifiedfrom
(inclusive) to the specifiedto
(inclusive) tofalse
.void
flip()
Flips all bits: 1 --> 0 and 0 --> 1boolean
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 tofalse
that occurs on or after the specified starting index.int
nextSetBit(int fromIndex)
Returns the index of the first bit that is set totrue
that occurs on or after the specified starting index.int
previousSetBit(int from)
Returns the index of the nearest bit that is set totrue
that occurs on or before the specified starting index.boolean
set(int index)
Sets the bit at the specified index totrue
.void
set(int from, int to)
Sets the bits from the specifiedfrom
(inclusive) to the specifiedto
(inclusive) totrue
.int
size()
java.lang.String
toString()
Returns a string representation of this bit set.protected static int
wordIndex(int bitIndex)
-
-
-
Field Detail
-
ADDRESS_BITS_PER_WORD
protected static final int ADDRESS_BITS_PER_WORD
- See Also:
- Constant Field Values
-
BITS_PER_WORD
protected static final int BITS_PER_WORD
- See Also:
- Constant Field Values
-
WORD_MASK
protected static final long WORD_MASK
- See Also:
- Constant Field Values
-
words
protected long[] words
-
size
protected int size
-
-
Constructor Detail
-
FixedSizeBitSet
public FixedSizeBitSet()
-
FixedSizeBitSet
public FixedSizeBitSet(int size)
Creates a bit set whose initial size is the range0
throughsize-1
. All bits are initiallyfalse
.- Parameters:
size
- the initial size of the bit set (in bits).- Throws:
java.lang.NegativeArraySizeException
- if the specified initial size is negative
-
-
Method Detail
-
set
public boolean set(int index)
Sets the bit at the specified index totrue
.- Parameters:
index
- a bit index.- Returns:
- true if the bit was 0 before, false otherwise
- Throws:
java.lang.IndexOutOfBoundsException
- if the specified index is negative.
-
set
public void set(int from, int to)
Sets the bits from the specifiedfrom
(inclusive) to the specifiedto
(inclusive) totrue
.- Parameters:
from
- index of the first bit to be setto
- index of the last bit to be set- Throws:
java.lang.IndexOutOfBoundsException
- iffrom
is negative, orto
is negative, orfrom
is larger thanto
-
clear
public void clear(int index)
Sets the bit specified by the index tofalse
.- Parameters:
index
- the index of the bit to be cleared.- Throws:
java.lang.IndexOutOfBoundsException
- if the specified index is negative.
-
clear
public void clear(int from, int to)
Sets the bits from the specifiedfrom
(inclusive) to the specifiedto
(inclusive) tofalse
.- Parameters:
from
- index of the first bit to be clearedto
- index of the last bit to be cleared- Throws:
java.lang.IndexOutOfBoundsException
- iffrom
is negative, orto
is negative, orfrom
is larger thanto
-
get
public boolean get(int index)
Returns the value of the bit with the specified index. The value istrue
if the bit with the indexindex
is currently set in this bit set; otherwise, the result isfalse
.- Parameters:
index
- the bit index.- Returns:
- the value of the bit with the specified index.
- Throws:
java.lang.IndexOutOfBoundsException
- if the specified index is negative.
-
nextSetBit
public int nextSetBit(int fromIndex)
Returns the index of the first bit that is set totrue
that occurs on or after the specified starting index. If no such bit exists then -1 is returned. To iterate over thetrue
bits in aBitSet
, use the following loop:for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { // operate on index i here }
- Parameters:
fromIndex
- the index to start checking from (inclusive).- Returns:
- the index of the next set bit.
- Throws:
java.lang.IndexOutOfBoundsException
- if the specified index is negative.
-
nextClearBit
public int nextClearBit(int fromIndex)
Returns the index of the first bit that is set tofalse
that occurs on or after the specified starting index.- Parameters:
fromIndex
- the index to start checking from (inclusive).- Returns:
- the index of the next clear bit.
- Throws:
java.lang.IndexOutOfBoundsException
- if the specified index is negative.
-
previousSetBit
public int previousSetBit(int from)
Returns the index of the nearest bit that is set totrue
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.- Parameters:
from
- the index to start checking from (inclusive)- Returns:
- the index of the previous set bit, or
-1
if there is no such bit - Throws:
java.lang.IndexOutOfBoundsException
- if the specified index is less than-1
-
cardinality
public int cardinality()
Returns the number of bits set to true in this bit set- Returns:
- the number of bits set to true in this bit set
-
size
public int size()
-
flip
public void flip()
Flips all bits: 1 --> 0 and 0 --> 1
-
toString
public java.lang.String toString()
Returns a string representation of this bit set. For every index for which thisBitSet
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 ofObject
.Example:
BitSet drPepper = new BitSet();
NowdrPepper.toString()
returns "{}
".drPepper.set(2);
NowdrPepper.toString()
returns "{2}
".drPepper.set(4); drPepper.set(10);
NowdrPepper.toString()
returns "{2, 4, 10}
".- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation of this bit set.
-
wordIndex
protected static int wordIndex(int bitIndex)
-
-