Nebula
Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
18#include "core/types.h"
19
20//------------------------------------------------------------------------------
21namespace Util
22{
23
24// Template magic to automatically use the smallest type possible
25template <size_t S> struct BitFieldBitType
26{
27 using T = uint8_t;
28};
29template <> struct BitFieldBitType<16>
30{
31 using T = uint16_t;
32};
33template <> struct BitFieldBitType<32>
34{
35 using T = uint32_t;
36};
37template <> struct BitFieldBitType<64>
38{
39 using T = uint64_t;
40};
41
42template <unsigned int NUMBITS> class BitField
43{
44 static_assert(NUMBITS > 0);
45
46public:
50 constexpr BitField(std::initializer_list<unsigned int> list);
52 BitField(const BitField<NUMBITS>& rhs) = default;
54 BitField(const uint64_t mask);
55
57 constexpr bool operator==(const BitField<NUMBITS>& rhs) const;
59 constexpr bool operator!=(const BitField<NUMBITS>& rhs) const;
60
62 constexpr bool IsSet(const uint64_t bitIndex) const;
64 template <uint64_t bitIndex> constexpr bool IsSet() const;
66 void Clear();
68 bool IsNull() const;
70 constexpr void SetBit(const uint64_t bitIndex);
72 constexpr void SetBitIf(const uint64_t bitIndex, uint64_t cond);
74 template <uint64_t bitIndex> constexpr void SetBit();
76 void ClearBit(const uint64_t bitIndex);
77
83 bool SectionIsNull(uint64_t section) const;
84
86 static constexpr BitField<NUMBITS> Or(const BitField<NUMBITS>& b0, const BitField<NUMBITS>& b1);
88 static constexpr BitField<NUMBITS> And(const BitField<NUMBITS>& b0, const BitField<NUMBITS>& b1);
89
90private:
91 static constexpr uint64_t BASE = NUMBITS > 32 ? 64 : NUMBITS > 16 ? 32 : NUMBITS > 8 ? 16 : 8;
92 static const int size = ((NUMBITS + BASE - 1) / BASE);
93
95
97
98public:
99 typename BitFieldBitType<BASE>::T GetBits(size_t index) const
100 {
101 n_assert(index < size);
102 return this->bits[index];
103 }
104
105 void SetBits(size_t index, typename BitFieldBitType<BASE>::T value)
106 {
107 n_assert(index < size);
108 this->bits[index] = value;
109 }
110};
111
112//------------------------------------------------------------------------------
115template <unsigned int NUMBITS> BitField<NUMBITS>::BitField()
116{
117 IndexT i;
118 for (i = 0; i < size; i++)
119 {
120 this->bits[i] = 0;
121 }
122}
123
124//------------------------------------------------------------------------------
127template <unsigned int NUMBITS> constexpr BitField<NUMBITS>::BitField(std::initializer_list<unsigned int> list)
128{
129 for (IndexT i = 0; i < size; i++)
130 {
131 this->bits[i] = 0;
132 }
133
134 for (auto bit : list)
135 {
136 this->SetBit(bit);
137 }
138}
139
140//------------------------------------------------------------------------------
143template<unsigned int NUMBITS>
144inline BitField<NUMBITS>::BitField(const uint64_t mask)
145{
146 for (IndexT i = 0; i < NUMBITS; i++)
147 {
148 if (mask & (1ull << i))
149 {
150 this->SetBit(i);
151 }
152 }
153}
154
155//------------------------------------------------------------------------------
158template <unsigned int NUMBITS>
159constexpr bool
161{
162 for (IndexT i = 0; i < size; i++)
163 {
164 if (this->bits[i] != rhs.bits[i])
165 {
166 return false;
167 }
168 }
169 return true;
170}
171
172//------------------------------------------------------------------------------
175template <unsigned int NUMBITS>
176constexpr bool
178{
179 return !(*this == rhs);
180}
181
182//------------------------------------------------------------------------------
185template <unsigned int NUMBITS>
186constexpr bool
187BitField<NUMBITS>::IsSet(const uint64_t bitIndex) const
188{
189 n_assert(bitIndex < NUMBITS);
190 const TYPE i = (1ull << (bitIndex % BASE));
191 const TYPE index = bitIndex / BASE;
192 return (this->bits[index] & i) == i;
193}
194
195//------------------------------------------------------------------------------
198template <unsigned int NUMBITS>
199template <uint64_t bitIndex>
200constexpr bool
202{
203 static_assert(bitIndex < NUMBITS);
204 constexpr TYPE i = (1ull << (bitIndex % BASE));
205 constexpr TYPE index = bitIndex / BASE;
206 return (this->bits[index] & i) == i;
207}
208
209//------------------------------------------------------------------------------
212template <unsigned int NUMBITS>
213void
215{
216 IndexT i;
217 for (i = 0; i < size; i++)
218 {
219 this->bits[i] = 0;
220 }
221}
222
223//------------------------------------------------------------------------------
226template <unsigned int NUMBITS>
227bool
229{
230 IndexT i;
231 for (i = 0; i < size; i++)
232 {
233 if (this->bits[i] != 0)
234 {
235 return false;
236 }
237 }
238 return true;
239}
240
241//------------------------------------------------------------------------------
244template <unsigned int NUMBITS>
245constexpr void
247{
248 n_assert(i < NUMBITS);
249 const TYPE index = i / BASE;
250 const TYPE bit = (1ull << (i % BASE));
251 this->bits[index] |= bit;
252}
253
254//------------------------------------------------------------------------------
257template <unsigned int NUMBITS>
258constexpr void
259BitField<NUMBITS>::SetBitIf(const uint64_t i, uint64_t cond)
260{
261 n_assert(i < NUMBITS);
262 const TYPE index = i / BASE;
263 const TYPE bit = (1ull << (i % BASE));
264 this->bits[index] |= bit * cond;
265}
266
267//------------------------------------------------------------------------------
270template <unsigned int NUMBITS>
271template <uint64_t i>
272constexpr void
274{
275 static_assert(i < NUMBITS);
276 constexpr TYPE index = i / BASE;
277 constexpr TYPE bit = (1ull << (i % BASE));
278 this->bits[index] |= bit;
279}
280
281//------------------------------------------------------------------------------
284template <unsigned int NUMBITS>
285void
287{
288 n_assert(i < NUMBITS);
289 const TYPE index = i / BASE;
290 const TYPE bit = ~(1ull << (i % BASE));
291 this->bits[index] &= bit;
292}
293
294//------------------------------------------------------------------------------
297template <unsigned int NUMBITS>
298bool
299BitField<NUMBITS>::SectionIsNull(uint64_t section) const
300{
301 n_assert(section < this->size);
302 return this->bits[section] == 0;
303}
304
305//------------------------------------------------------------------------------
308template <unsigned int NUMBITS>
309constexpr BitField<NUMBITS>
311{
313 for (IndexT i = 0; i < size; i++)
314 {
315 res.bits[i] = b0.bits[i] | b1.bits[i];
316 }
317 return res;
318}
319
320//------------------------------------------------------------------------------
323template <unsigned int NUMBITS>
324constexpr BitField<NUMBITS>
326{
328 for (IndexT i = 0; i < size; i++)
329 {
330 res.bits[i] = b0.bits[i] & b1.bits[i];
331 }
332 return res;
333}
334
335} // namespace Util
336//------------------------------------------------------------------------------
Implements large bit field.
Definition bitfield.h:43
constexpr bool operator==(const BitField< NUMBITS > &rhs) const
equality operator
Definition bitfield.h:160
static constexpr uint64_t BASE
Definition bitfield.h:91
void ClearBit(const uint64_t bitIndex)
clear a bit by index
Definition bitfield.h:286
BitFieldBitType< BASE >::T GetBits(size_t index) const
Definition bitfield.h:99
constexpr bool operator!=(const BitField< NUMBITS > &rhs) const
inequality operator
Definition bitfield.h:177
BitField(const BitField< NUMBITS > &rhs)=default
copy constructor
bool IsNull() const
return true if all bits are 0
Definition bitfield.h:228
constexpr void SetBit()
set a bit by index
Definition bitfield.h:273
BitField(const uint64_t mask)
Construct from mask.
Definition bitfield.h:144
bool SectionIsNull(uint64_t section) const
Check if a section of the bitfield is set.
Definition bitfield.h:299
static constexpr BitField< NUMBITS > And(const BitField< NUMBITS > &b0, const BitField< NUMBITS > &b1)
set bitfield to AND combination
Definition bitfield.h:325
constexpr void SetBitIf(const uint64_t bitIndex, uint64_t cond)
set a bit by index. Multiplies the bit with cond before OR-ing which means it won't set the bit if mu...
Definition bitfield.h:259
constexpr void SetBit(const uint64_t bitIndex)
set a bit by index
Definition bitfield.h:246
constexpr BitField(std::initializer_list< unsigned int > list)
constructs a bitfield based on multiple values
Definition bitfield.h:127
typename BitFieldBitType< BASE >::T TYPE
Definition bitfield.h:94
void Clear()
clear content
Definition bitfield.h:214
BitField()
constructor
Definition bitfield.h:115
constexpr bool IsSet(const uint64_t bitIndex) const
Check if single bit is set.
Definition bitfield.h:187
TYPE bits[size]
Definition bitfield.h:96
constexpr bool IsSet() const
Check if single bit is set.
Definition bitfield.h:201
static constexpr BitField< NUMBITS > Or(const BitField< NUMBITS > &b0, const BitField< NUMBITS > &b1)
set bitfield to OR combination
Definition bitfield.h:310
void SetBits(size_t index, typename BitFieldBitType< BASE >::T value)
Definition bitfield.h:105
static const int size
Definition bitfield.h:92
#define n_assert(exp)
Definition debug.h:58
A quad tree designed to return regions of free 2D space.
Definition Random.cs:5
uint16_t T
Definition bitfield.h:31
uint32_t T
Definition bitfield.h:35
uint64_t T
Definition bitfield.h:39
Definition bitfield.h:26
uint8_t T
Definition bitfield.h:27
int IndexT
Definition types.h:49