I want to provide [] for my bitmap, but it is stored as an array of uint32_t's, therfore a reference to a bool is out of the question. Is there a solution, or am I stuck with the ol' get/set interface?
Just for the heck of it:
Code:
#include <stddef.h>
#include <stdint.h>
template <size_t bits, bool default_value = false>
class bitmap
{
public:
bitmap()
{
blocks = bits / 32 + (bits % 32 == 0 ? 0 : 1);
reset_all();
}
bool get(size_t index)
{
if(index >= bits)
return default_value;
return data[index / 32] & ~(1 << index % 32) != 0;
}
void reset(size_t index)
{
set(index, default_value);
}
void reset_all()
{
set_all(default_value);
}
void set(size_t index, bool value)
{
if(index >= bits)
return;
if(value)
data[index / 32] |= 1 << index % 32;
else
data[index / 32] &= ~(1 << index % 32);
}
void set_all(bool value)
{
for(size_t i = 0;i < blocks;i++)
data[i] = value ? ~(uint32_t)0 : (uint32_t)0;
}
private:
size_t blocks;
uint32_t data[bits / 32 + (bits % 32 == 0 ? 0 : 1)];
};