-
Notifications
You must be signed in to change notification settings - Fork 58
/
bitflags.h
273 lines (226 loc) · 7.67 KB
/
bitflags.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2016 Herb Sutter. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GCPP_BITFLAGS
#define GCPP_BITFLAGS
#include "util.h"
#include <climits>
#include <memory>
#include <algorithm>
#include <type_traits>
#include <iostream>
namespace gcpp {
//----------------------------------------------------------------------------
//
// vector<bool> operations aren't always optimized, so here's a custom class.
//
//----------------------------------------------------------------------------
class bitflags {
public:
using unit = unsigned int;
static_assert(std::is_unsigned<unit>::value, "unit must be an unsigned integral type.");
static constexpr auto bits_per_unit = static_cast<int>(sizeof(unit) * CHAR_BIT);
private:
std::unique_ptr<unit[]> bits;
const int size;
// Return a unit with all bits set if "set" is true, or all bits cleared otherwise.
//
static constexpr unit all_bits(bool set) noexcept {
return set ? ~unit(0) : unit(0);
}
// Return a mask that will select the bit at position from its unit
//
static unit bit_mask(int at) noexcept {
Expects(0 <= at && "position must be non-negative");
return unit(1) << (at % bits_per_unit);
}
// Return the number of units needed to represent a number of bits
//
static int unit_count(int bit_count) noexcept {
Expects(0 <= bit_count && "bit_count must be non-negative");
return (bit_count + bits_per_unit - 1) / bits_per_unit;
}
// Get the unit that contains the bit at position
//
unit& bit_unit(int at) noexcept {
Expects(0 <= at && "position must be non-negative");
return bits[at / bits_per_unit];
}
const unit& bit_unit(int at) const noexcept {
Expects(0 <= at && "position must be non-negative");
return bits[at / bits_per_unit];
}
public:
bitflags(int nbits, bool value)
: size{ nbits }
{
Expects(nbits > 0 && "#bits must be positive");
bits = std::make_unique<unit[]>(unit_count(nbits));
if (value) {
set_all(true);
}
}
// Get flag value at position
//
bool get(int at) const noexcept {
Expects(0 <= at && at < size && "bitflags get() out of range");
return (bit_unit(at) & bit_mask(at)) != unit(0);
}
// Test whether all bits are false
//
bool all_false() const noexcept {
auto const all_false = [](unit const u) { return u == unit(0); };
if (!std::all_of(bits.get(), bits.get() + unit_count(size) - 1, all_false)) {
return false;
}
auto const mask = size % bits_per_unit == 0 ? unit(0) : bit_mask(size);
return all_false(*(bits.get() + unit_count(size) - 1) & (mask - 1));
}
// Set flag value at position
//
void set(int at, bool value) noexcept {
Expects(0 <= at && at < size && "bitflags set() out of range");
if (value) {
bit_unit(at) |= bit_mask(at);
}
else {
bit_unit(at) &= ~bit_mask(at);
}
}
// Set all flags to value
//
void set_all(bool value) noexcept {
std::fill_n(bits.get(), unit_count(size), all_bits(value));
}
// Set all flags in positions [from,to) to value
//
void set(int from, int to, bool value) noexcept {
Expects(0 <= from && from <= to && to <= size && "bitflags set() out of range");
if (from == to) {
return;
}
const auto from_unit = from / bits_per_unit;
const auto from_mod = from % bits_per_unit;
const auto to_unit = to / bits_per_unit;
const auto to_mod = to % bits_per_unit;
auto data = bits.get() + from_unit;
// first set the remaining bits in the partial unit this range begins within
if (from_mod != 0) {
// set all bits less than from in a mask
auto mask = (unit(1) << from_mod) - 1;
if (from_unit == to_unit) {
// set all bits in mask that are >= to as well
mask |= ~((unit(1) << to_mod) - 1);
}
if (value) {
*data |= ~mask;
}
else {
*data &= mask;
}
if (from_unit == to_unit) {
return;
}
++data;
}
// then set whole units (makes a significant performance difference)
data = std::fill_n(data, bits.get() + to_unit - data, all_bits(value));
// then set the remaining bits in the partial unit this range ends within
if (to_mod != 0) {
// set all bits less than to in a mask
auto mask = (unit(1) << to_mod) - 1;
if (value) {
*data |= mask;
}
else {
*data &= ~mask;
}
}
}
void debug_print() {
for (auto i = 0; i < this->size; ++i) {
std::cout << (get(i) ? "T" : "f");
if (i % 8 == 7) { std::cout << ' '; }
if (i % 64 == 63) { std::cout << '\n'; }
}
std::cout << "\n";
}
// Find next flag in positions [from,to) that is set to value
// Returns index of next flag that is set to value, or "to" if none was found
//
int find_next(int from, int to, bool value) noexcept {
Expects(0 <= from && from <= to && to <= size && "bitflags find_next() out of range");
if (from == to) {
return to;
}
const auto from_unit = from / bits_per_unit;
const auto from_mod = from % bits_per_unit;
const auto to_unit = to / bits_per_unit;
const auto to_mod = to % bits_per_unit;
auto data = bits.get() + from_unit;
// first test the remaining bits in the partial unit this range begins within
if (from_mod != 0) {
// mask all bits less than from
auto mask = (unit(1) << from_mod) - 1;
if (from_unit == to_unit) {
// mask all bits that are >= to as well
mask |= ~((unit(1) << to_mod) - 1);
}
mask = ~mask; // now invert the mask to the bits we care about
if ((value && (*data & mask) != unit(0)) // looking for true and there's one in here
|| (!value && (~*data & mask) != unit(0))) { // looking for false and there's one in here
while (get(from) != value) {
++from;
}
Ensures(from < to && "wait, we should have found the value in the first unit");
return from;
}
if (from_unit == to_unit) {
return to;
}
++data;
}
// then test whole units (makes a significant performance difference)
data = std::find_if(data, bits.get() + to_unit,
[=](unit u) { return value ? u != unit(0) : u != ~unit(0); });
if (data != bits.get() + to_unit) {
from = (data - bits.get()) * bits_per_unit;
while (get(from) != value) {
++from;
}
Ensures(from < to && "wait, we should have found the value in this unit");
return from;
}
// then test the remaining bits in the partial unit this range ends within
if (to_mod != 0) {
// mask all bits less than to
auto mask = (unit(1) << to_mod) - 1;
if ((value && (*data & mask) != unit(0)) // looking for true and there's one in here
|| (!value && (~*data & mask) != unit(0))) { // looking for false and there's one in here
from = (data - bits.get()) * bits_per_unit;
while (get(from) != value) {
++from;
}
Ensures(from < to && "wait, we should have found the value in the last unit");
return from;
}
}
return to;
}
};
// Future: Just set(from,to) is a performance improvement over vector<bool>,
// but also add find_next_false etc. functions to eliminate some loops
}
#endif