|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.rim.device.api.util.BitSet
public class BitSet
Maintains a collection of bits.
Behaviour
For efficiency purposes, this set maintains an internal cache of the
number of bits that are set. Any normal operation affecting a bit's state
updates this cache value to keep it current.
However, some operations ignore the value of this cache, specifically, operations which could set bits beyond your declared size of the set. Typically, this occurs if you NOT the contents of a set: since the set is actually stored as a collection of 32 bit ints, any bits beyond your size mod 32 will be affected by a NOT operation, and this will automatically increase the size of the bitset (as bits beyond the current size will now be set). This can also happen if you OR or AND your set with another, larger set: if bits in your set beyond its size are set on, then the size of your set (and the number of set bits) must increase accordingly.
Alternatively, you can save this time by using the fast set and clear
methods, which ignore the cache; however, the cache then becomes unsuable and
calls to BitSet.getNumSet()
must dive into the bit set itself (and reset the
cache) rather than simply retrieving the value out of the cache.
Constructor Summary | ||
---|---|---|
BitSet()
Constructs a new BitSet instance with enough room for 128 bits. |
||
BitSet(int size)
Constructs a new BitSet instance of provided size. |
||
BitSet(BitSet srcBitSet)
Constructs a new BitSet instance using another as a template. |
Method Summary | ||
---|---|---|
void |
and(BitSet mask)
Performs bitwise AND on this set with provided mask. |
|
void |
clear(int index)
Clears bit by index. |
|
boolean |
equals(Object obj)
Compares this bit set to provided object. |
|
void |
fastClear(int index)
Clears bit by index, ignoring cache. |
|
void |
fastSet(int index)
Sets bit by index, ignoring cache. |
|
int |
getFirstSet()
Retrieves first set bit in this set. |
|
int |
getLastSet()
Retrieves last set bit in this set. |
|
int |
getNextSet(int index)
Retrieves the first bit set starting forward from provided position. |
|
int |
getNumSet()
Retrieves number of set bits. |
|
int |
getPreviousSet(int index)
Retrieves the first bit set starting backwards from provided position. |
|
boolean |
isSet(int index)
Determines bit state by index. |
|
void |
not()
Performs logical NOT on this set. |
|
void |
or(BitSet mask)
Performs binary OR on this set with provided mask. |
|
void |
reset()
Clears all bits. |
|
void |
set(int index)
Sets bit by index. |
|
void |
xor(BitSet mask)
Performs binary XOR on this set with provided mask. |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public BitSet()
public BitSet(int size)
size
- Number of bits this set must accomodate.public BitSet(BitSet srcBitSet)
This builds a bit set using another's size and contents as initial values.
srcBitSet
- Source bit set.Method Detail |
---|
public void fastSet(int index)
This method doesn't bother to keep the cache of the number of set bits up to date.
index
- Position of bit to set.public void set(int index)
Unless the cache of number of set bits has been made unusable, this method keeps it up to date. If the cache is unusable, this method doesn't bother fixing it.
index
- Position of bit to set.public void fastClear(int index)
This method doesn't bother to keep the cache of the number of set bits up to date.
index
- Position of bit to clear.public void clear(int index)
Unless the cache of number of set bits has been made unusable, this method keeps it up to date. If the cache is unusable, this method doesn't bother fixing it.
index
- Position of bit to clear.public void reset()
public boolean isSet(int index)
index
- Position of bit to test.
public int getNumSet()
If the cache of number of set bits is usable, this method returns its value; otherwise, this method walks through the set to determine the state of each bit in turn. In the latter case, this method also resets the cache, making it usable again.
public boolean equals(Object obj)
Note that this method does require that both objects be bit sets for equality; however, the two bit sets do not need to be of equal length. This method tests for equality only in the positions that both sets provide. Thus if this set is only four bits long, while the obj parameter is a set that's 20 bits long, this method tests only the first four bits of both sets for equality.
equals
in class Object
obj
- Object to compare with this bit set.
Boolean.hashCode()
,
Hashtable
public int getNextSet(int index)
This method starts at the provided position and checks forward towards the end of the bit set.
index
- First bit to check.
public int getPreviousSet(int index)
This method starts at the provided position and checks backwards towards the start of the bit set.
index
- First bit to check.
public int getFirstSet()
public int getLastSet()
public void and(BitSet mask)
mask
- Bit mask to use; if the mask is shorter than this set, then
invoking this method has the effect of clearing those "extra" bits in
this set.public void or(BitSet mask)
mask
- Bit mask to use; if the mask is longer than this set, then
this method first pads this set with zeros to make it the same length as
the mask, and then applies the mask.public void not()
public void xor(BitSet mask)
mask
- Bit mask to use; if the mask is longer than this set, then
this method first pads this set with zeros to make it the same length as
the mask, and then applies the mask.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright 1999-2011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Java is a trademark of Oracle America Inc. in the US and other countries.
Legal