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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* A set abstraction for enumeration values. */
#ifndef mozilla_EnumSet_h
#define mozilla_EnumSet_h
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/MathAlgorithms.h"
#include <initializer_list>
#include <type_traits>
#include <stdint.h>
namespace mozilla {
/**
* EnumSet<T, U> is a set of values defined by an enumeration. It is implemented
* using a bit mask with the size of U for each value. It works both for enum
* and enum class types. EnumSet also works with U being a BitSet.
*/
template <typename T, typename Serialized = typename std::make_unsigned<
typename std::underlying_type<T>::type>::type>
class EnumSet {
public:
using valueType = T;
using serializedType = Serialized;
constexpr EnumSet() : mBitField() {}
constexpr MOZ_IMPLICIT EnumSet(T aEnum) : mBitField(BitFor(aEnum)) {}
constexpr EnumSet(T aEnum1, T aEnum2)
: mBitField(BitFor(aEnum1) | BitFor(aEnum2)) {}
constexpr EnumSet(T aEnum1, T aEnum2, T aEnum3)
: mBitField(BitFor(aEnum1) | BitFor(aEnum2) | BitFor(aEnum3)) {}
constexpr EnumSet(T aEnum1, T aEnum2, T aEnum3, T aEnum4)
: mBitField(BitFor(aEnum1) | BitFor(aEnum2) | BitFor(aEnum3) |
BitFor(aEnum4)) {}
constexpr MOZ_IMPLICIT EnumSet(std::initializer_list<T> list) : mBitField() {
for (auto value : list) {
(*this) += value;
}
}
#ifdef DEBUG
constexpr EnumSet(const EnumSet& aEnumSet) : mBitField(aEnumSet.mBitField) {}
constexpr EnumSet& operator=(const EnumSet& aEnumSet) {
mBitField = aEnumSet.mBitField;
IncVersion();
return *this;
}
#endif
/**
* Add an element
*/
constexpr void operator+=(T aEnum) {
IncVersion();
mBitField |= BitFor(aEnum);
}
/**
* Add an element
*/
constexpr EnumSet operator+(T aEnum) const {
EnumSet result(*this);
result += aEnum;
return result;
}
/**
* Union
*/
void operator+=(const EnumSet& aEnumSet) {
IncVersion();
mBitField |= aEnumSet.mBitField;
}
/**
* Union
*/
EnumSet operator+(const EnumSet& aEnumSet) const {
EnumSet result(*this);
result += aEnumSet;
return result;
}
/**
* Remove an element
*/
void operator-=(T aEnum) {
IncVersion();
mBitField &= ~(BitFor(aEnum));
}
/**
* Remove an element
*/
EnumSet operator-(T aEnum) const {
EnumSet result(*this);
result -= aEnum;
return result;
}
/**
* Remove a set of elements
*/
void operator-=(const EnumSet& aEnumSet) {
IncVersion();
mBitField &= ~(aEnumSet.mBitField);
}
/**
* Remove a set of elements
*/
EnumSet operator-(const EnumSet& aEnumSet) const {
EnumSet result(*this);
result -= aEnumSet;
return result;
}
/**
* Clear
*/
void clear() {
IncVersion();
mBitField = Serialized();
}
/**
* Intersection
*/
void operator&=(const EnumSet& aEnumSet) {
IncVersion();
mBitField &= aEnumSet.mBitField;
}
/**
* Intersection
*/
EnumSet operator&(const EnumSet& aEnumSet) const {
EnumSet result(*this);
result &= aEnumSet;
return result;
}
/**
* Equality
*/
bool operator==(const EnumSet& aEnumSet) const {
return mBitField == aEnumSet.mBitField;
}
/**
* Equality
*/
bool operator==(T aEnum) const { return mBitField == BitFor(aEnum); }
/**
* Not equal
*/
bool operator!=(const EnumSet& aEnumSet) const {
return !operator==(aEnumSet);
}
/**
* Not equal
*/
bool operator!=(T aEnum) const { return !operator==(aEnum); }
/**
* Test is an element is contained in the set.
*/
bool contains(T aEnum) const { return HasBitFor(aEnum); }
/**
* Test if a set is contained in the set.
*/
bool contains(const EnumSet& aEnumSet) const {
return (mBitField & aEnumSet.mBitField) == aEnumSet.mBitField;
}
/**
* Return the number of elements in the set.
*/
size_t size() const {
if constexpr (std::is_unsigned_v<Serialized>) {
if constexpr (kMaxBits > 32) {
return CountPopulation64(mBitField);
} else {
return CountPopulation32(mBitField);
}
} else {
return mBitField.Count();
}
}
bool isEmpty() const {
if constexpr (std::is_unsigned_v<Serialized>) {
return mBitField == 0;
} else {
return mBitField.IsEmpty();
}
}
Serialized serialize() const { return mBitField; }
void deserialize(Serialized aValue) {
IncVersion();
mBitField = aValue;
}
class ConstIterator {
const EnumSet* mSet;
size_t mPos;
#ifdef DEBUG
uint64_t mVersion;
#endif
void checkVersion() const {
// Check that the set has not been modified while being iterated.
MOZ_ASSERT_IF(mSet, mSet->mVersion == mVersion);
}
public:
ConstIterator(const EnumSet& aSet, size_t aPos) : mSet(&aSet), mPos(aPos) {
#ifdef DEBUG
mVersion = mSet->mVersion;
#endif
MOZ_ASSERT(aPos <= kMaxBits);
if (aPos != kMaxBits && !mSet->HasBitAt(mPos)) {
++*this;
}
}
ConstIterator(const ConstIterator& aOther)
: mSet(aOther.mSet), mPos(aOther.mPos) {
#ifdef DEBUG
mVersion = aOther.mVersion;
checkVersion();
#endif
}
ConstIterator(ConstIterator&& aOther)
: mSet(aOther.mSet), mPos(aOther.mPos) {
#ifdef DEBUG
mVersion = aOther.mVersion;
checkVersion();
#endif
aOther.mSet = nullptr;
}
~ConstIterator() { checkVersion(); }
bool operator==(const ConstIterator& other) const {
MOZ_ASSERT(mSet == other.mSet);
checkVersion();
return mPos == other.mPos;
}
bool operator!=(const ConstIterator& other) const {
return !(*this == other);
}
T operator*() const {
MOZ_ASSERT(mSet);
MOZ_ASSERT(mPos < kMaxBits);
MOZ_ASSERT(mSet->HasBitAt(mPos));
checkVersion();
return T(mPos);
}
ConstIterator& operator++() {
MOZ_ASSERT(mSet);
MOZ_ASSERT(mPos < kMaxBits);
checkVersion();
do {
mPos++;
} while (mPos < kMaxBits && !mSet->HasBitAt(mPos));
return *this;
}
};
ConstIterator begin() const { return ConstIterator(*this, 0); }
ConstIterator end() const { return ConstIterator(*this, kMaxBits); }
private:
constexpr static Serialized BitFor(T aEnum) {
const auto pos = static_cast<size_t>(aEnum);
return BitAt(pos);
}
constexpr static Serialized BitAt(size_t aPos) {
MOZ_DIAGNOSTIC_ASSERT(aPos < kMaxBits);
if constexpr (std::is_unsigned_v<Serialized>) {
return static_cast<Serialized>(Serialized{1} << aPos);
} else {
Serialized bitField;
bitField[aPos] = true;
return bitField;
}
}
constexpr bool HasBitFor(T aEnum) const {
const auto pos = static_cast<size_t>(aEnum);
return HasBitAt(pos);
}
constexpr bool HasBitAt(size_t aPos) const {
return static_cast<bool>(mBitField & BitAt(aPos));
}
constexpr void IncVersion() {
#ifdef DEBUG
mVersion++;
#endif
}
static constexpr size_t MaxBits() {
if constexpr (std::is_unsigned_v<Serialized>) {
return sizeof(Serialized) * 8;
} else {
return Serialized::Size();
}
}
static constexpr size_t kMaxBits = MaxBits();
Serialized mBitField;
#ifdef DEBUG
uint64_t mVersion = 0;
#endif
};
} // namespace mozilla
#endif /* mozilla_EnumSet_h_*/
|