3#include "DSCEngine/debug/assert.hpp"
19 static constexpr int BUFFER_SIZE = (N+31)/32;
20 int buffer[BUFFER_SIZE];
23 static int free_bit_pos(
int n,
int k=32);
32 __bit(
int& buf_stored,
int bit_pos) : buf_stored(buf_stored), bit_pos(bit_pos) { value(); }
34 bool value()
const {
return buf_stored & (1 << bit_pos); }
36 operator bool()
const {
return value(); }
37 operator int()
const {
return value(); }
39 void operator = (
bool bit) { buf_stored&=~(1 << bit_pos), buf_stored|=(bit << bit_pos); }
41 bool operator == (
bool bit)
const {
return value() == bit; }
43 bool operator !()
const {
return !value();}
83 bool at(
int index)
const;
91 inline int size()
const {
return N; }
116 int val = -default_value;
117 for(
int i=0;i<BUFFER_SIZE;i++)
126 nds_assert(index>=0);
129 return __bit(buffer[index>>5], index &31);
135 nds_assert(index>=0);
138 return __bit(buffer[index>>5], index &31);
144 return (*
this)[index];
150 for(
int i=0;i<BUFFER_SIZE;i++)
152 int p = free_bit_pos(buffer[i]);
157 return result<N ? result : -1;
166 if((
unsigned)n==0xFFFFFFFF)
return -1;
170 int _0f = (1<<(k-1))-1 + (1<<(k-1));
174 return k+free_bit_pos(n>>k, k);
176 return free_bit_pos(n & _0f, k);
188 for(
int i=0;i<BUFFER_SIZE;i++)
189 buffer[i] = 0xFFFFFFFF;
195 for(
int i=0;i<BUFFER_SIZE;i++)
196 buffer[i] = 0x00000000;
202 nds_assert(((
int)offset)%4==0,
"Bad alignment for bits array");
Specialized class for handling arrays of bits.
Definition: bits_array.hpp:17
const bool & operator[](int index) const
random access iterator in const contexts
Definition: bits_array.hpp:124
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
Definition: bits_array.hpp:200
void set_all()
sets all bits of the array to 1
Definition: bits_array.hpp:186
int find_free_bit() const
finds a position of the first unset bit in the array
Definition: bits_array.hpp:148
BitsArray(bool default_value=false)
creates a new prefilled BitsArray instance
Definition: bits_array.hpp:114
bool at(int index) const
alternative to random access iterator
Definition: bits_array.hpp:142
void unset_all()
sets all bits of the array to 0
Definition: bits_array.hpp:193
void clear()
clears all bits of the array (sets them all to 0)
Definition: bits_array.hpp:180
int size() const
Definition: bits_array.hpp:91
proxy bit access
Definition: bits_array.hpp:28