Update BitArray to automatically grow whenever setting/toggling bits beyond the end

develop
Quietust 2012-01-15 18:08:35 -06:00
parent e84904f5c5
commit 97bae19c13
1 changed files with 16 additions and 2 deletions

@ -50,6 +50,18 @@ namespace DFHack
if(bits)
memset(bits, 0, size);
}
void extend (T index)
{
uint32_t newsize = (index / 8) + 1;
if (newsize <= size)
return;
uint8_t *newbits = new uint8_t[newsize];
memset(newbits, 0, newsize);
memcpy(newbits, bits, size);
delete[] bits;
bits = newbits;
size = newsize;
}
void set (T index, bool value = true)
{
if(!value)
@ -58,7 +70,8 @@ namespace DFHack
return;
}
uint32_t byte = index / 8;
if(byte < size)
extend(index);
//if(byte < size)
{
uint8_t bit = 1 << (index % 8);
bits[byte] |= bit;
@ -76,7 +89,8 @@ namespace DFHack
void toggle (T index)
{
uint32_t byte = index / 8;
if(byte < size)
extend(index);
//if(byte < size)
{
uint8_t bit = 1 << (index % 8);
bits[byte] ^= bit;