forked from hsutter/gcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitflags.h
More file actions
183 lines (150 loc) · 4.9 KB
/
bitflags.h
File metadata and controls
183 lines (150 loc) · 4.9 KB
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
///////////////////////////////////////////////////////////////////////////////
//
// 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>
namespace gcpp {
//----------------------------------------------------------------------------
//
// vector<bool> operations aren't always optimized, so here's a custom class.
//
//----------------------------------------------------------------------------
class bitflags {
using unit = unsigned int;
static_assert(std::is_unsigned<unit>::value, "unit must be an unsigned integral type.");
std::unique_ptr<unit[]> bits;
const int size;
static constexpr auto bits_per_unit = static_cast<int>(sizeof(unit) * CHAR_BIT);
// 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 non-negative");
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 ret = std::none_of(bits.get(), bits.get() + unit_count(size), [](unit u) { return u != unit(0); });
return ret;
}
// 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 {
if (from >= to) {
return;
}
Expects(0 <= from && to <= size && "bitflags set() out of range");
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;
const auto n_whole_units = to_unit - from_unit - 1;
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 (n_whole_units < 0) {
// set all bits in mask that are >= to as well
mask |= ~((unit(1) << to_mod) - 1);
}
if (value) {
*data |= ~mask;
}
else {
*data &= mask;
}
if (n_whole_units < 0) {
return;
}
++data;
}
// then set whole units (makes a significant performance difference)
data = std::fill_n(data, n_whole_units, 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;
}
}
}
};
// 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