Xyris  0.5
Bitset< S > Class Template Reference

#include <Bitset.hpp>

Public Member Functions

 Bitset ()
 
 Bitset (size_t val)
 
size_t Size ()
 
void Set (size_t pos)
 
void Reset (size_t pos)
 
void Flip (size_t pos)
 
bool Test (size_t pos)
 
bool operator[] (size_t pos)
 
size_t FindFirstBit (bool isSet)
 
size_t FindFirstRange (size_t count, bool isSet)
 

Private Member Functions

size_t TypeSize ()
 
size_t Index (size_t bit)
 
size_t Offset (size_t bit)
 

Private Attributes

size_t map [S]
 

Detailed Description

template<size_t S>
class Bitset< S >

Definition at line 21 of file Bitset.hpp.

Constructor & Destructor Documentation

◆ Bitset() [1/2]

template<size_t S>
Bitset< S >::Bitset ( )
inline

Construct a new Bitset object and initalize the bitmap to zero.

Definition at line 28 of file Bitset.hpp.

29  {
30  memset(&map, 0, S);
31  }

◆ Bitset() [2/2]

template<size_t S>
Bitset< S >::Bitset ( size_t  val)
inline

Construct a new Bitset object and initialize the bitmap with the desired value.

Parameters
valInitialization value

Definition at line 39 of file Bitset.hpp.

40  {
41  for (size_t i = 0; i < S; i++)
42  map[i] = val;
43  }

Member Function Documentation

◆ FindFirstBit()

template<size_t S>
size_t Bitset< S >::FindFirstBit ( bool  isSet)
inline

Finds and returns the position of the first clear bit.

Parameters
isSetIf true, find the first bit that is set.
Returns
size_t Position of the first bit with desired polarity. If all bits are polarized, SIZE_MAX is returned.

Definition at line 116 of file Bitset.hpp.

117  {
118  for (size_t i = 0; i < S; i++) {
119  if (Test(i) == isSet)
120  return i;
121  }
122  return SIZE_MAX;
123  }
+ Here is the caller graph for this function:

◆ FindFirstRange()

template<size_t S>
size_t Bitset< S >::FindFirstRange ( size_t  count,
bool  isSet 
)
inline

Finds a range of count clear bits and returns the starting position.

Parameters
countNumber of clear bits desired
isSetIf true, find the first range of count bits that are set.
Returns
size_t Position of the first bit with desired range and polarity. If all bits are polarized, SIZE_MAX is returned.

Definition at line 133 of file Bitset.hpp.

134  {
135  size_t checkLow, checkHigh, check, idx, offset;
136  size_t mask = ((size_t)1 << count) - (size_t)1;
137  for (size_t i = 0UL; i < S - count; i++) {
138  idx = Index(i);
139  offset = Offset(i);
140  checkLow = map[idx] >> offset;
141  checkHigh = offset ? map[idx + 1] << (TypeSize() - offset) : 0;
142  check = checkLow | checkHigh;
143 
144  if ((check & mask) == isSet)
145  return i;
146  }
147  return SIZE_MAX;
148  }

◆ Flip()

template<size_t S>
void Bitset< S >::Flip ( size_t  pos)
inline

Flip the bit at the given position.

Parameters
posTarget bit to be flipped

Definition at line 80 of file Bitset.hpp.

81  {
82  Test(pos) ? Reset(pos) : Set(pos);
83  }

◆ Index()

template<size_t S>
size_t Bitset< S >::Index ( size_t  bit)
inlineprivate

Definition at line 157 of file Bitset.hpp.

158  {
159  return bit / TypeSize();
160  }
+ Here is the caller graph for this function:

◆ Offset()

template<size_t S>
size_t Bitset< S >::Offset ( size_t  bit)
inlineprivate

Definition at line 162 of file Bitset.hpp.

163  {
164  return bit % TypeSize();
165  }
+ Here is the caller graph for this function:

◆ operator[]()

template<size_t S>
bool Bitset< S >::operator[] ( size_t  pos)
inline

Return the value of the bit at the given position Same functionality as Test() as an operator This operator cannot be used to set a bit, only test.

Parameters
posPosition to be tested
Returns
bool Bit value

Definition at line 104 of file Bitset.hpp.

105  {
106  return Test(pos);
107  }

◆ Reset()

template<size_t S>
void Bitset< S >::Reset ( size_t  pos)
inline

Reset (clear) the bit at the given position.

Parameters
posTarget bit to be reset

Definition at line 70 of file Bitset.hpp.

71  {
72  map[Index(pos)] &= ~(1UL << Offset(pos));
73  }
+ Here is the caller graph for this function:

◆ Set()

template<size_t S>
void Bitset< S >::Set ( size_t  pos)
inline

Set the bit for a given position.

Parameters
posTarget bit to be set

Definition at line 60 of file Bitset.hpp.

61  {
62  map[Index(pos)] |= 1UL << Offset(pos);
63  }
+ Here is the caller graph for this function:

◆ Size()

template<size_t S>
size_t Bitset< S >::Size ( )
inline

Returns the size of the bitset in bytes.

Returns
size_t Size of the bitset in bytes

Definition at line 50 of file Bitset.hpp.

51  {
52  return S;
53  }

◆ Test()

template<size_t S>
bool Bitset< S >::Test ( size_t  pos)
inline

Return the value of the bit at the given position.

Parameters
posPosition to be tested
Returns
bool Bit value

Definition at line 91 of file Bitset.hpp.

92  {
93  return map[Index(pos)] >> Offset(pos) & 1;
94  }
+ Here is the caller graph for this function:

◆ TypeSize()

template<size_t S>
size_t Bitset< S >::TypeSize ( )
inlineprivate

Definition at line 152 of file Bitset.hpp.

153  {
154  return sizeof(size_t) * CHAR_BIT;
155  }
+ Here is the caller graph for this function:

Field Documentation

◆ map

template<size_t S>
size_t Bitset< S >::map[S]
private

Definition at line 151 of file Bitset.hpp.


The documentation for this class was generated from the following file:
Bitset::TypeSize
size_t TypeSize()
Definition: Bitset.hpp:152
Bitset::Test
bool Test(size_t pos)
Return the value of the bit at the given position.
Definition: Bitset.hpp:91
Bitset::Index
size_t Index(size_t bit)
Definition: Bitset.hpp:157
Bitset::Set
void Set(size_t pos)
Set the bit for a given position.
Definition: Bitset.hpp:60
Bitset::Offset
size_t Offset(size_t bit)
Definition: Bitset.hpp:162
Bitset::map
size_t map[S]
Definition: Bitset.hpp:151
memset
void * memset(void *bufptr, int value, size_t size)
Sets the number of bytes in memory at ptr to the value.
Definition: string.cpp:106
Bitset::Reset
void Reset(size_t pos)
Reset (clear) the bit at the given position.
Definition: Bitset.hpp:70