v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
Classes | Public Member Functions | Static Public Member Functions | List of all members
Sifteo::BitArray< tSize > Class Template Reference

A fixed-size array of bits, with compact storage and fast iteration. More...

#include <sifteo/array.h>

Classes

struct  iterator
 An STL-style iterator for the BitArray. More...
 

Public Member Functions

iterator begin () const
 Return an STL-style iterator for this array.
 
 BitArray ()
 Create a new empty BitArray.
 
 BitArray (unsigned index)
 Create a new BitArray with a single bit marked.
 
 BitArray (unsigned begin, unsigned end)
 Create a new BitArray with a range of bits marked. More...
 
void clear (unsigned index)
 Clear (set to 0) a single bit.
 
void clear ()
 Clear (set to 0) all bits in the array.
 
bool clearFirst (unsigned &index)
 Find and clear the lowest marked bit. More...
 
bool clearN (unsigned &index, unsigned n)
 Find and clear the Nth lowest marked bit. More...
 
unsigned count () const
 How many bits are marked in this vector?
 
bool empty () const
 Is every bit in this array set to zero?
 
iterator end () const
 Return an STL-style iterator for this array.
 
bool findFirst (unsigned &index) const
 Find the lowest index where there's a marked (1) bit. More...
 
void mark (unsigned index)
 Mark (set to 1) a single bit.
 
void mark ()
 Mark (set to 1) all bits in the array.
 
BitArray< tSize > operator& (const BitArray< tSize > &other) const
 Bitwise AND of two BitArrays of the same size.
 
BitArray< tSize > operator^ (const BitArray< tSize > &other) const
 Bitwise XOR of two BitArrays of the same size.
 
BitArray< tSize > operator| (const BitArray< tSize > &other) const
 Bitwise OR of two BitArrays of the same size.
 
BitArray< tSize > operator~ () const
 Negate a BitArray, returning a new array in which each bit is inverted.
 
bool test (unsigned index) const
 Is a particular bit marked?
 

Static Public Member Functions

static unsigned size ()
 Retrieve the size of this array in bits, always constant at compile-time.
 

Detailed Description

template<unsigned tSize>
class Sifteo::BitArray< tSize >

A fixed-size array of bits, with compact storage and fast iteration.

Supports arrays with any fixed-size number of bits. For sizes <= 32 bits, this is just as efficient as using a bare uint32_t.

This is a Plain Old Data type, with no constructor.

The default value of the bits in a BitArray is undefined. Initialize the BitArray prior to use, for example by calling mark() or clear().

BitArrays support iteration, through explicit method calls or via C++11 range-based for loops.

Constructor & Destructor Documentation

template<unsigned tSize>
Sifteo::BitArray< tSize >::BitArray ( unsigned  begin,
unsigned  end 
)
inlineexplicit

Create a new BitArray with a range of bits marked.

This is a half-open interval. All bits >= 'begin' and < 'end' are marked.

Member Function Documentation

template<unsigned tSize>
bool Sifteo::BitArray< tSize >::clearFirst ( unsigned &  index)
inline

Find and clear the lowest marked bit.

If we find any marked bits, returns true, sets "index" to that bit's index, and clears the bit. This can be used as part of an iteration pattern:

  unsigned index;
  while (vec.clearFirst(index)) {
      doStuff(index);
  }

This is functionally equivalent to findFirst() followed by clear(), but it's a tiny bit more efficient.

template<unsigned tSize>
bool Sifteo::BitArray< tSize >::clearN ( unsigned &  index,
unsigned  n 
)
inline

Find and clear the Nth lowest marked bit.

For n=0, this is equivalent to clearFirst(). For n=1, we skip the first marked bit and clear the second. And so on. If there is no Nth marked bit, returns false.

template<unsigned tSize>
bool Sifteo::BitArray< tSize >::findFirst ( unsigned &  index) const
inline

Find the lowest index where there's a marked (1) bit.

If any marked bits exist, returns true and puts the bit's index in "index". Iff the entire array is zero, returns false.


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