libcommonc++  0.7
BitSet Class Reference

A compact representation of an array of bits (boolean values). More...

#include <BitSet.h++>

Classes

class  Bit
 A "reference" to a bit within a BitSet. More...
 

Public Member Functions

 BitSet (uint_t size)
 Construct a new BitSet of the given size. More...
 
 BitSet (const BitSet &other)
 Copy constructor. More...
 
 BitSet (const String &bits)
 Construct a BitSet from a String. More...
 
virtual ~BitSet ()
 Destructor. More...
 
BitSetset (uint_t pos, bool value=true)
 Set (or clear) a bit at the given position. More...
 
BitSetsetRange (uint_t startPos, uint_t endPos, bool value=true)
 Set (or clear) the bits in the given (inclusive) range. More...
 
BitSetsetAll (bool value=true)
 Set (or clear) all of the bits in the BitSet. More...
 
BitSetclear (uint_t pos)
 Clear the bit at the given position. More...
 
BitSetclearRange (uint_t startPos, uint_t endPos)
 Clear the bits in the given (inclusive) range. More...
 
BitSetclearAll ()
 Clear all of the bits in the BitSet. More...
 
bool isSet (int pos) const
 Test if a bit at the given position is set. More...
 
bool get (uint_t pos) const
 Get the value of the bit at the given position. More...
 
bool isClear (uint_t pos) const
 Test if a bit at the given position is clear. More...
 
bool isAnySet () const
 Test if at least one bit is set in the BitSet. More...
 
bool isAllSet () const
 Test if all of the bits are set in the BitSet. More...
 
bool isAllClear () const
 Test if all of the bits are clear in the BitSet. More...
 
int nextSetBit (uint_t startPos) const
 Find the index of the next bit that is set, beginning at the given position. More...
 
int firstSetBit () const
 Find the index of the first bit that is set. More...
 
int nextClearBit (uint_t startPos) const
 Find the index of the next bit that is clear, beginning at the given position. More...
 
int firstClearBit () const
 Find the index of the first bit that is clear. More...
 
bool operator! () const
 Test if all of the bits in the BitSet are cleared. More...
 
BitSetflip ()
 Flip (toggle) all of the bits in the BitSet. More...
 
BitSetflip (uint_t pos)
 Flip (toggle) the bit at the given position. More...
 
void swap (BitSet &other)
 Swap the bits of this BitSet with those of another. More...
 
Bit operator[] (int pos)
 Obtain a "reference" to a bit at the given position. More...
 
bool operator[] (int pos) const
 Get the value of a bit at the given position. More...
 
uint_t getSize () const
 Get the size of the BitSet, in bits. More...
 
uint_t getWordCount () const
 Get the number of words in the BitSet. More...
 
uint32_t getWord (uint_t index) const
 Get the word at the specified index in the BitSet. More...
 
uint32_t getWordDiff (uint_t index, uint32_t other) const
 "Diff" the word at the specificed index in the BitSet against another word. More...
 
void setWord (uint_t index, uint32_t word)
 Set the word at the specified index in the BitSet. More...
 
String toString () const
 Get a String representation of the BitSet. More...
 
void write (std::ostream &stream) const
 Write a string representation of the BitSet to a stream. More...
 
bool operator== (const BitSet &other) const
 Equality operator. More...
 
bool operator!= (const BitSet &other) const
 Inequality operator. More...
 
BitSetoperator= (const BitSet &other)
 Assignment operator. More...
 
BitSetoperator|= (const BitSet &other)
 Bitwise OR assignment operator. More...
 
BitSetoperator &= (const BitSet &other)
 Bitwise AND assignment operator. More...
 
BitSetoperator^= (const BitSet &other)
 Bitwise XOR assignment operator. More...
 
BitSetoperator>>= (uint_t n)
 Bitwise right-shift assignment operator. More...
 
BitSetoperator<<= (uint_t n)
 Bitwise left-shift assignment operator. More...
 
BitSet operator>> (uint_t n) const
 Bitwise right-shift operator. More...
 
BitSet operator<< (uint_t n) const
 Bitwise left-shift operator. More...
 
BitSet operator~ () const
 Bitwise NOT operator. More...
 

Detailed Description

A compact representation of an array of bits (boolean values).

The bits are stored internally in an array of words, each word being a 32-bit unsigned integer that represents 32 boolean values. Complete words can be fetched if the caller needs efficient access to multiple bits at a time.

Author
Mark Lindner

Constructor & Destructor Documentation

◆ BitSet() [1/3]

BitSet ( uint_t  size)

Construct a new BitSet of the given size.

Parameters
sizeThe number of bits the BitSet will hold.

◆ BitSet() [2/3]

BitSet ( const BitSet other)

Copy constructor.

◆ BitSet() [3/3]

BitSet ( const String bits)

Construct a BitSet from a String.

Parameters
bitsA String consisting of only the characters '0' and '1'; any other characters will be interpreted as 0.

◆ ~BitSet()

~BitSet ( )
virtual

Destructor.

Member Function Documentation

◆ clear()

BitSet& clear ( uint_t  pos)
inline

Clear the bit at the given position.

Parameters
posThe position.

◆ clearAll()

BitSet& clearAll ( )
inline

Clear all of the bits in the BitSet.

◆ clearRange()

BitSet& clearRange ( uint_t  startPos,
uint_t  endPos 
)
inline

Clear the bits in the given (inclusive) range.

Parameters
startPosThe start position of the range.
endPosThe end position of the range.

◆ firstClearBit()

int firstClearBit ( ) const
inline

Find the index of the first bit that is clear.

Returns
The index of the bit, or -1 if there are no clear bits in the BitSet.

◆ firstSetBit()

int firstSetBit ( ) const
inline

Find the index of the first bit that is set.

Returns
The index of the bit, or -1 if there are no set bits in the BitSet.

◆ flip() [1/2]

BitSet & flip ( )

Flip (toggle) all of the bits in the BitSet.

◆ flip() [2/2]

BitSet & flip ( uint_t  pos)

Flip (toggle) the bit at the given position.

Exceptions
OutOfBoundsExceptionIf the position is invalid.

◆ get()

bool get ( uint_t  pos) const
inline

Get the value of the bit at the given position.

◆ getSize()

uint_t getSize ( ) const
inline

Get the size of the BitSet, in bits.

◆ getWord()

uint32_t getWord ( uint_t  index) const
inline

Get the word at the specified index in the BitSet.

Parameters
indexThe index. If the index is out of range, a value of 0 is returned.
Returns
The word.

◆ getWordCount()

uint_t getWordCount ( ) const
inline

Get the number of words in the BitSet.

(Each word is a 32-bit integer.)

◆ getWordDiff()

uint32_t getWordDiff ( uint_t  index,
uint32_t  other 
) const
inline

"Diff" the word at the specificed index in the BitSet against another word.

Essentially, this function simply returns a bitwise XOR of the two words.

Parameters
indexThe index. If the index is out of range, a value of 0 is returned.
otherThe other word to "diff" against.
Returns
The "diff" of the two words.

◆ isAllClear()

bool isAllClear ( ) const
inline

Test if all of the bits are clear in the BitSet.

◆ isAllSet()

bool isAllSet ( ) const

Test if all of the bits are set in the BitSet.

◆ isAnySet()

bool isAnySet ( ) const

Test if at least one bit is set in the BitSet.

◆ isClear()

bool isClear ( uint_t  pos) const
inline

Test if a bit at the given position is clear.

◆ isSet()

bool isSet ( int  pos) const

Test if a bit at the given position is set.

◆ nextClearBit()

int nextClearBit ( uint_t  startPos) const
inline

Find the index of the next bit that is clear, beginning at the given position.

Parameters
startPosThe starting bit position to search from.
Returns
The index of the bit, or -1 if there are no clear bits between the start position and the end of the BitSet, inclusive.

◆ nextSetBit()

int nextSetBit ( uint_t  startPos) const
inline

Find the index of the next bit that is set, beginning at the given position.

Parameters
startPosThe starting bit position to search from.
Returns
The index of the bit, or -1 if there are no set bits between the start position and the end of the BitSet, inclusive.

◆ operator &=()

BitSet& operator&= ( const BitSet other)

Bitwise AND assignment operator.

Exceptions
OutOfBoundsExceptionIf the two BitSets are not of equal size.

◆ operator!()

bool operator! ( ) const
inline

Test if all of the bits in the BitSet are cleared.

Equivalent to isAllClear().

◆ operator!=()

bool operator!= ( const BitSet other) const
inline

Inequality operator.

◆ operator<<()

BitSet operator<< ( uint_t  n) const
inline

Bitwise left-shift operator.

Parameters
nThe number of bit positions by which to shift the BitSet.
Returns
A new BitSet that is a bit-shifted version of the original.

◆ operator<<=()

BitSet & operator<<= ( uint_t  n)

Bitwise left-shift assignment operator.

Parameters
nThe number of bit positions by which to shift the BitSet.

◆ operator=()

BitSet & operator= ( const BitSet other)

Assignment operator.

◆ operator==()

bool operator== ( const BitSet other) const

Equality operator.

◆ operator>>()

BitSet operator>> ( uint_t  n) const
inline

Bitwise right-shift operator.

Parameters
nThe number of bit positions by which to shift the BitSet.
Returns
A new BitSet that is a bit-shifted version of the original.

◆ operator>>=()

BitSet & operator>>= ( uint_t  n)

Bitwise right-shift assignment operator.

Parameters
nThe number of bit positions by which to shift the BitSet.

◆ operator[]() [1/2]

BitSet::Bit operator[] ( int  pos)

Obtain a "reference" to a bit at the given position.

Parameters
posThe position.
Returns
The bit.
Exceptions
OutOfBoundsExceptionIf the position is invalid.

◆ operator[]() [2/2]

bool operator[] ( int  pos) const
inline

Get the value of a bit at the given position.

Parameters
posThe position.
Returns
The value of the bit.
Exceptions
OutOfBoundsExceptionIf the position is invalid.

◆ operator^=()

BitSet & operator^= ( const BitSet other)

Bitwise XOR assignment operator.

Exceptions
OutOfBoundsExceptionIf the two BitSets are not of equal size.

◆ operator|=()

BitSet & operator|= ( const BitSet other)

Bitwise OR assignment operator.

Exceptions
OutOfBoundsExceptionIf the two BitSets are not of equal size.

◆ operator~()

BitSet operator~ ( ) const
inline

Bitwise NOT operator.

Returns
A new BitSet that is a bit-flipped version of the original.

◆ set()

BitSet & set ( uint_t  pos,
bool  value = true 
)

Set (or clear) a bit at the given position.

Parameters
posThe position of the bit.
valueThe value to set the bit to.

◆ setAll()

BitSet & setAll ( bool  value = true)

Set (or clear) all of the bits in the BitSet.

Parameters
valueThe value to set all of the bits to.

◆ setRange()

BitSet & setRange ( uint_t  startPos,
uint_t  endPos,
bool  value = true 
)

Set (or clear) the bits in the given (inclusive) range.

Parameters
startPosThe start position of the range.
endPosThe end position of the range.
valueThe value to set the bits to.

◆ setWord()

void setWord ( uint_t  index,
uint32_t  word 
)

Set the word at the specified index in the BitSet.

Parameters
indexThe index. If the index is out of range, the method has no effect.
wordThe word.

◆ swap()

void swap ( BitSet other)

Swap the bits of this BitSet with those of another.

Exceptions
OutOfBoundsExceptionIf both BitSets are not of equal size.

◆ toString()

String toString ( ) const

Get a String representation of the BitSet.

◆ write()

void write ( std::ostream &  stream) const

Write a string representation of the BitSet to a stream.


The documentation for this class was generated from the following files: