DSC Engine
All Classes Files Functions Variables Typedefs
Classes | Public Member Functions | Static Public Member Functions | List of all members
DSC::BitsArray< N > Class Template Reference

Specialized class for handling arrays of bits. More...

#include <bits_array.hpp>

Classes

struct  __bit
 proxy bit access More...
 

Public Member Functions

 BitsArray (bool default_value=false)
 creates a new prefilled BitsArray instance More...
 
const bool & operator[] (int index) const
 random access iterator in const contexts More...
 
__bit operator[] (int index)
 random access iterator for both l-value and r-value contexts More...
 
bool at (int index) const
 alternative to random access iterator More...
 
int find_free_bit () const
 finds a position of the first unset bit in the array More...
 
int size () const
 
void clear ()
 clears all bits of the array (sets them all to 0)
 
void set_all ()
 sets all bits of the array to 1
 
void unset_all ()
 sets all bits of the array to 0
 

Static Public Member Functions

static BitsArray< N > * take_over (void *offset)
 Makes the memory at a certain offset available as a bits array and provides direct bit access to it
More...
 

Detailed Description

template<const int N>
class DSC::BitsArray< N >

Specialized class for handling arrays of bits.

Template Parameters
Nthe size of the bits array

The bits are packed into full words in a contiguous memory zone, so the size of the structure BitsArray<N> is the size of the minimum number of 32-bit integers required to contain all the N bits.

Actually, sizeof(BitsArray<N>) == 4*((N+31) / 32).

Constructor & Destructor Documentation

◆ BitsArray()

template<const int N>
DSC::BitsArray< N >::BitsArray ( bool  default_value = false)

creates a new prefilled BitsArray instance

Parameters
default_valuethe initial value of all bits

Member Function Documentation

◆ at()

template<const int N>
bool DSC::BitsArray< N >::at ( int  index) const

alternative to random access iterator

Parameters
indexthe position of the bit 0..N-1
Returns
the value of the bit at the specified index

◆ find_free_bit()

template<const int N>
int DSC::BitsArray< N >::find_free_bit

finds a position of the first unset bit in the array

Returns
position of the unset bit (0..N-1), or -1 if no free bit was found

◆ operator[]() [1/2]

template<const int N>
DSC::BitsArray< N >::__bit DSC::BitsArray< N >::operator[] ( int  index)

random access iterator for both l-value and r-value contexts

Parameters
indexthe position of the bit 0..N-1
Returns
a reference to the value of the bit at the specified index
Warning
Do not use the reference as a substitute for the bit's value! The value of a bit is extracted from DSC::BitsArray::__bit by conversion to bool. This conversion is done implicitely wherever the compiler thinks it's necessary. However, if unsure whetehr the conversion takes place or not, use the explicit (bool)(...) conversion, or use DSC::BitsArray::at().
bits[0] = true; // ok
bool b = bits[0]; // ok
if(bits[0]) {...} // ok
if(!bits[1]) {...} // ok
DSC::Debug::log("%i", bits[0]); // wrong, will display the int constructed from the first 4 bytes of _the reference_ pushed to stack
DSC::Debug::log("%i", (bool)bits[0]); // ok, the actual bit value will be pushed onto stack
Specialized class for handling arrays of bits.
Definition: bits_array.hpp:17

◆ operator[]() [2/2]

template<const int N>
const bool & DSC::BitsArray< N >::operator[] ( int  index) const

random access iterator in const contexts

Parameters
indexthe position of the bit 0..N-1
Returns
the value of the bit at the specified index

◆ size()

template<const int N>
int DSC::BitsArray< N >::size ( ) const
inline
Returns
the number of bits in the array

◆ take_over()

template<const int N>
DSC::BitsArray< N > * DSC::BitsArray< N >::take_over ( void *  offset)
static

Makes the memory at a certain offset available as a bits array and provides direct bit access to it

Parameters
offsetthe start of the bits array (must be word-aligned)
Template Parameters
Nthe size of the bits array (must be multiple of 32)
Returns
pointer to BitsArray instance over the specified offset

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