Isle
Loading...
Searching...
No Matches
mxbitset.h
Go to the documentation of this file.
1#ifndef MXBITSET_H
2#define MXBITSET_H
3
4#pragma warning(disable : 4237)
5
6#include "mxtypes.h"
7
8#include <assert.h>
9#include <limits.h> // CHAR_BIT
10
17template <size_t N>
18class MxBitset {
19public:
24 MxBitset() { Tidy(); }
25
26 // SIZE 0x08
31 class Reference {
32 friend class MxBitset<N>;
33
34 public:
40 {
41 m_bitset->Flip(m_offset);
42 return (*this);
43 }
44
49 bool operator~() const { return (!m_bitset->Test(m_offset)); }
50
55 operator bool() const { return (m_bitset->Test(m_offset)); }
56
57 private:
63 Reference(MxBitset<N>& p_bitset, size_t p_offset) : m_bitset(&p_bitset), m_offset(p_offset) {}
64 MxBitset<N>* m_bitset;
65 size_t m_offset;
66 };
67
73 Reference operator[](size_t p_bit) { return (Reference(*this, p_bit)); }
74
81 MxBitset<N>& Flip(size_t p_bit)
82 {
83 if (N <= p_bit) {
84 Xran();
85 }
86 m_blocks[p_bit / e_bitsPerBlock] ^= 1 << p_bit % e_bitsPerBlock;
87 return (*this);
88 }
89
95 size_t Count()
96 {
97 // debug only, intentionally unimplemented
98 return 0;
99 }
100
107 bool Test(MxU32 p_bit)
108 {
109 if (p_bit >= N) {
110 Xran();
111 }
112
113 return (m_blocks[p_bit / e_bitsPerBlock] & (1 << p_bit % e_bitsPerBlock)) != 0;
114 }
115
120 MxU32 Size() const { return N; }
121
122private:
128 void Tidy(MxU32 p_value = 0)
129 {
130 for (MxS32 i = e_blocksRequired; i >= 0; --i) {
131 m_blocks[i] = p_value;
132 }
133
134 // No need to trim if all bits were zeroed out
135 if (p_value != 0) {
136 Trim();
137 }
138 }
139
144 void Trim()
145 {
146 if (N % e_bitsPerBlock != 0) {
147 m_blocks[e_blocksRequired] &= ((1 << (N % e_bitsPerBlock)) - 1);
148 }
149 }
150
155 void Xran() { assert("invalid MxBitset<N> position" == NULL); }
156
161 // Not a real enum. This is how STL BITSET defines these constants.
162 enum {
163 e_bitsPerBlock = CHAR_BIT * sizeof(MxU32),
164 e_blocksRequired = N == 0 ? 0 : (N - 1) / e_bitsPerBlock
165 };
166
167 MxU32 m_blocks[e_blocksRequired + 1];
168};
169
170#endif // MXBITSET_H
Proxy class to reference a single bit within the MxBitset.
Definition: mxbitset.h:31
bool operator~() const
Returns true if the referenced bit is not set.
Definition: mxbitset.h:49
Reference & Flip()
Flips the referenced bit (inverts its value).
Definition: mxbitset.h:39
Templated fixed-size bitset for bit manipulation.
Definition: mxbitset.h:18
MxU32 Size() const
Returns the number of bits (N) this bitset manages.
Definition: mxbitset.h:120
bool Test(MxU32 p_bit)
Tests if the given bit is set (1) or not (0).
Definition: mxbitset.h:107
Reference operator[](size_t p_bit)
Provides a reference-like object for a specific bit.
Definition: mxbitset.h:73
MxBitset()
Constructs an empty MxBitset with all bits cleared.
Definition: mxbitset.h:24
size_t Count()
Counts the number of set bits.
Definition: mxbitset.h:95
MxBitset< N > & Flip(size_t p_bit)
Flips (toggles) a single bit at the given position.
Definition: mxbitset.h:81
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
signed int MxS32
[AI]
Definition: mxtypes.h:38
unsigned int MxU32
[AI]
Definition: mxtypes.h:32