fennec
Loading...
Searching...
No Matches
bitfield.h
Go to the documentation of this file.
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
30
31#ifndef FENNEC_CONTAINERS_BITFIELD_H
32#define FENNEC_CONTAINERS_BITFIELD_H
33
35#include <fennec/lang/types.h>
36#include <fennec/lang/utility.h>
37
38namespace fennec
39{
40
44template<size_t N>
45struct bitfield {
46 static constexpr size_t bits = N;
47 static constexpr size_t bytes = (N + 7) / 8;
48
49public:
50 constexpr bitfield()
51 : _bytes() {
52 }
53
54 explicit constexpr bitfield(const bool (&arr)[N])
55 : _bytes() {
56 for (size_t i = 0; i < arr; ++i) {
57 this->store(i, arr[i]);
58 }
59 }
60
61 template<size_t I>
62 explicit constexpr bitfield(const size_t (&arr)[I])
63 : _bytes() {
64 for (size_t i : arr) {
65 this->set(i);
66 }
67 }
68
69 template<typename...ArgsT>
70 constexpr bitfield(ArgsT&&...args)
71 : _bytes() {
72 (this->store(fennec::forward<ArgsT>(args), true), ...);
73 }
74
75 template<typename...ArgsT> requires((is_bool_v<ArgsT> or is_convertible_v<ArgsT, bool>) and ...)
76 constexpr bitfield(ArgsT&&...args)
77 : _bytes() {
78 size_t i = 0;
79 (this->store(i++, fennec::forward<ArgsT>(args)), ...);
80 }
81
82 bitfield(const bitfield& bf)
83 : _bytes(bf._bytes) {
84 }
85
86 bitfield(bitfield&& bf) noexcept
87 : _bytes(bf._bytes) {
88 }
89
90 constexpr ~bitfield() = default;
91
92 bitfield& operator=(const bitfield& bf) = default;
93 bitfield& operator=(bitfield&& bf) noexcept = default;
94
95 bool test(size_t i) const {
96 assertd(i < bits, "Index out of Bounds!");
97 size_t b = i / 8;
98 size_t o = i % 8;
99 return _bytes[b] & (1 << o);
100 }
101
102 void set(size_t i) {
103 assertd(i < bits, "Index out of Bounds!");
104 size_t b = i / 8;
105 size_t o = i % 8;
106 _bytes[b] |= (1 << o);
107 }
108
109 void clear(size_t i) {
110 assertd(i < bits, "Index out of Bounds!");
111 size_t b = i / 8;
112 size_t o = i % 8;
113 _bytes[b] &= ~(1 << o);
114 }
115
116 void toggle(size_t i) {
117 assertd(i < bits, "Index out of Bounds!");
118 size_t b = i / 8;
119 size_t o = i % 8;
120 _bytes[b] ^= (1 << o);
121 }
122
123 void store(size_t i, bool v) {
124 assertd(i < bits, "Index out of Bounds!");
125 size_t b = i / 8;
126 size_t o = i % 8;
127 (_bytes[b] &= ~((1 << o))) |= ((v << o));
128 }
129
130 bitfield operator~() const {
131 bitfield res = *this;
132 res._inv_helper(make_index_metasequence_t<bytes>{});
133 return res;
134 }
135
136private:
138
139 template<size_t...I>
140 void _inv_helper(index_metasequence<I...>) {
141 ((_bytes[I] = ~_bytes[I]), ...);
142 }
143};
144
145
146}
147
148#endif // FENNEC_CONTAINERS_BITFIELD_H
A header containing the definition for a static/stack allocated array.
typename make_index_metasequence< N >::type make_index_metasequence_t
shorthand for typename make_index_metasequence<N>::type
Definition metasequences.h:189
Data Structure that defines a compile-time allocated array.
Definition array.h:64
Bitfield Container with basic Bit Ops.
Definition bitfield.h:45
metaprogramming integral metasequence
Definition metasequences.h:162
wrapper for sets of elements
Definition set.h:68
Types
Utility