DSC Engine
Loading...
Searching...
No Matches
bits_array.hpp
1#pragma once
2
3#include "DSCEngine/debug/assert.hpp"
4
5namespace DSC
6{
15 template<const int N>
17 {
18 private:
19 static constexpr int BUFFER_SIZE = (N+31)/32;
20 int buffer[BUFFER_SIZE];
21
23 static int free_bit_pos(int n, int k=32);
24 public:
27 struct __bit // not safe for Debug::log(...)
28 {
29 int& buf_stored;
30 int bit_pos;
31
32 __bit(int& buf_stored, int bit_pos) : buf_stored(buf_stored), bit_pos(bit_pos) { value(); }
33
34 bool value() const { return buf_stored & (1 << bit_pos); }
35
36 operator bool() const { return value(); }
37 operator int() const { return value(); }
38
39 void operator = (bool bit) { buf_stored&=~(1 << bit_pos), buf_stored|=(bit << bit_pos); }
40
41 bool operator == (bool bit) const { return value() == bit; }
42
43 bool operator !() const { return !value();}
44 };
45
49 BitsArray(bool default_value = false);
50
55 const bool& operator[] (int index) const;
56
77 __bit operator[] (int index);
78
83 bool at(int index) const;
84
88 int find_free_bit() const;
89
91 inline int size() const { return N; }
92
94 void clear();
95
97 void set_all();
98
100 void unset_all();
101
108 static BitsArray<N>* take_over(void* offset);
109
110 };
111}
112
113template<const int N>
115{
116 int val = -default_value; // default_value ? 0xFFFFFFFF : 0x00000000;
117 for(int i=0;i<BUFFER_SIZE;i++)
118 {
119 buffer[i] = val;
120 }
121}
122
123template<const int N>
124const bool& DSC::BitsArray<N>::operator[] (int index) const
125{
126 nds_assert(index>=0);
127 nds_assert(index<N);
128
129 return __bit(buffer[index>>5], index &31);
130}
131
132template<const int N>
134{
135 nds_assert(index>=0);
136 nds_assert(index<N);
137
138 return __bit(buffer[index>>5], index &31);
139}
140
141template<const int N>
142bool DSC::BitsArray<N>::at(int index) const
143{
144 return (*this)[index];
145}
146
147template<const int N>
149{
150 for(int i=0;i<BUFFER_SIZE;i++)
151 {
152 int p = free_bit_pos(buffer[i]);
153 if(p>=0)
154 {
155 int result = 32*i+p;
156
157 return result<N ? result : -1;
158 }
159 }
160 return -1;
161}
162
163template<const int N>
164int DSC::BitsArray<N>::free_bit_pos(int n, int k)
165{
166 if((unsigned)n==0xFFFFFFFF) return -1; // full : all bits are set
167 if(k==0) return 0;
168
169 k/=2;
170 int _0f = (1<<(k-1))-1 + (1<<(k-1)); // build bit-1 masks for the LOW
171 //int _f0 = _0f << (k/2); // and HIGH parts
172
173 if((n & _0f)==_0f) // if the LOW part is full
174 return k+free_bit_pos(n>>k, k); // look in the HIGH part
175 else
176 return free_bit_pos(n & _0f, k); // otherwise check the LOW part
177}
178
179template<const int N>
181{
182 unset_all();
183}
184
185template<const int N>
187{
188 for(int i=0;i<BUFFER_SIZE;i++)
189 buffer[i] = 0xFFFFFFFF;
190}
191
192template<const int N>
194{
195 for(int i=0;i<BUFFER_SIZE;i++)
196 buffer[i] = 0x00000000;
197}
198
199template<const int N>
201{
202 nds_assert(((int)offset)%4==0, "Bad alignment for bits array");
203 nds_assert(N%32==0);
204 return (BitsArray<N>*)offset;
205}
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