| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* A set abstraction for enumeration values. */ |
| 8 |
|
| 9 |
#ifndef mozilla_EnumSet_h |
| 10 |
#define mozilla_EnumSet_h |
| 11 |
|
| 12 |
#include "mozilla/Assertions.h" |
| 13 |
#include "mozilla/Attributes.h" |
| 14 |
#include "mozilla/MathAlgorithms.h" |
| 15 |
|
| 16 |
#include <initializer_list> |
| 17 |
#include <type_traits> |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
namespace mozilla { |
| 22 |
|
| 23 |
/** |
| 24 |
* EnumSet<T, U> is a set of values defined by an enumeration. It is implemented |
| 25 |
* using a bit mask with the size of U for each value. It works both for enum |
| 26 |
* and enum class types. EnumSet also works with U being a BitSet. |
| 27 |
*/ |
| 28 |
template <typename T, typename Serialized = typename std::make_unsigned< |
| 29 |
typename std::underlying_type<T>::type>::type> |
| 30 |
class EnumSet { |
| 31 |
public: |
| 32 |
using valueType = T; |
| 33 |
using serializedType = Serialized; |
| 34 |
|
| 35 |
constexpr EnumSet() : mBitField() {} |
| 36 |
|
| 37 |
constexpr MOZ_IMPLICIT EnumSet(T aEnum) : mBitField(BitFor(aEnum)) {} |
| 38 |
|
| 39 |
constexpr EnumSet(T aEnum1, T aEnum2) |
| 40 |
: mBitField(BitFor(aEnum1) | BitFor(aEnum2)) {} |
| 41 |
|
| 42 |
constexpr EnumSet(T aEnum1, T aEnum2, T aEnum3) |
| 43 |
: mBitField(BitFor(aEnum1) | BitFor(aEnum2) | BitFor(aEnum3)) {} |
| 44 |
|
| 45 |
constexpr EnumSet(T aEnum1, T aEnum2, T aEnum3, T aEnum4) |
| 46 |
: mBitField(BitFor(aEnum1) | BitFor(aEnum2) | BitFor(aEnum3) | |
| 47 |
BitFor(aEnum4)) {} |
| 48 |
|
| 49 |
constexpr MOZ_IMPLICIT EnumSet(std::initializer_list<T> list) : mBitField() { |
| 50 |
for (auto value : list) { |
| 51 |
(*this) += value; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
constexpr explicit EnumSet(Serialized aValue) : mBitField(aValue) {} |
| 56 |
|
| 57 |
#ifdef DEBUG |
| 58 |
constexpr EnumSet(const EnumSet& aEnumSet) : mBitField(aEnumSet.mBitField) {} |
| 59 |
|
| 60 |
constexpr EnumSet& operator=(const EnumSet& aEnumSet) { |
| 61 |
mBitField = aEnumSet.mBitField; |
| 62 |
IncVersion(); |
| 63 |
return *this; |
| 64 |
} |
| 65 |
#endif |
| 66 |
|
| 67 |
/** |
| 68 |
* Add an element |
| 69 |
*/ |
| 70 |
constexpr void operator+=(T aEnum) { |
| 71 |
IncVersion(); |
| 72 |
mBitField |= BitFor(aEnum); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add an element |
| 77 |
*/ |
| 78 |
constexpr EnumSet operator+(T aEnum) const { |
| 79 |
EnumSet result(*this); |
| 80 |
result += aEnum; |
| 81 |
return result; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Union |
| 86 |
*/ |
| 87 |
constexpr void operator+=(const EnumSet& aEnumSet) { |
| 88 |
IncVersion(); |
| 89 |
mBitField |= aEnumSet.mBitField; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Union |
| 94 |
*/ |
| 95 |
constexpr EnumSet operator+(const EnumSet& aEnumSet) const { |
| 96 |
EnumSet result(*this); |
| 97 |
result += aEnumSet; |
| 98 |
return result; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Remove an element |
| 103 |
*/ |
| 104 |
constexpr void operator-=(T aEnum) { |
| 105 |
IncVersion(); |
| 106 |
mBitField &= ~(BitFor(aEnum)); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Remove an element |
| 111 |
*/ |
| 112 |
constexpr EnumSet operator-(T aEnum) const { |
| 113 |
EnumSet result(*this); |
| 114 |
result -= aEnum; |
| 115 |
return result; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Remove a set of elements |
| 120 |
*/ |
| 121 |
constexpr void operator-=(const EnumSet& aEnumSet) { |
| 122 |
IncVersion(); |
| 123 |
mBitField &= ~(aEnumSet.mBitField); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Remove a set of elements |
| 128 |
*/ |
| 129 |
constexpr EnumSet operator-(const EnumSet& aEnumSet) const { |
| 130 |
EnumSet result(*this); |
| 131 |
result -= aEnumSet; |
| 132 |
return result; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Clear |
| 137 |
*/ |
| 138 |
constexpr void clear() { |
| 139 |
IncVersion(); |
| 140 |
mBitField = Serialized(); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Intersection |
| 145 |
*/ |
| 146 |
constexpr void operator&=(const EnumSet& aEnumSet) { |
| 147 |
IncVersion(); |
| 148 |
mBitField &= aEnumSet.mBitField; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Intersection |
| 153 |
*/ |
| 154 |
constexpr EnumSet operator&(const EnumSet& aEnumSet) const { |
| 155 |
EnumSet result(*this); |
| 156 |
result &= aEnumSet; |
| 157 |
return result; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Equality |
| 162 |
*/ |
| 163 |
constexpr bool operator==(const EnumSet& aEnumSet) const { |
| 164 |
return mBitField == aEnumSet.mBitField; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Equality |
| 169 |
*/ |
| 170 |
constexpr bool operator==(T aEnum) const { |
| 171 |
return mBitField == BitFor(aEnum); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Not equal |
| 176 |
*/ |
| 177 |
constexpr bool operator!=(const EnumSet& aEnumSet) const { |
| 178 |
return !operator==(aEnumSet); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Not equal |
| 183 |
*/ |
| 184 |
constexpr bool operator!=(T aEnum) const { return !operator==(aEnum); } |
| 185 |
|
| 186 |
/** |
| 187 |
* Test is an element is contained in the set. |
| 188 |
*/ |
| 189 |
constexpr bool contains(T aEnum) const { return HasBitFor(aEnum); } |
| 190 |
|
| 191 |
/** |
| 192 |
* Test if a set is contained in the set. |
| 193 |
*/ |
| 194 |
constexpr bool contains(const EnumSet& aEnumSet) const { |
| 195 |
return (mBitField & aEnumSet.mBitField) == aEnumSet.mBitField; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Return the number of elements in the set. |
| 200 |
*/ |
| 201 |
size_t size() const { |
| 202 |
if constexpr (std::is_unsigned_v<Serialized>) { |
| 203 |
if constexpr (kMaxBits > 32) { |
| 204 |
return CountPopulation64(mBitField); |
| 205 |
} else { |
| 206 |
return CountPopulation32(mBitField); |
| 207 |
} |
| 208 |
} else { |
| 209 |
return mBitField.Count(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
constexpr bool isEmpty() const { |
| 214 |
if constexpr (std::is_unsigned_v<Serialized>) { |
| 215 |
return mBitField == 0; |
| 216 |
} else { |
| 217 |
return mBitField.IsEmpty(); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
Serialized serialize() const { return mBitField; } |
| 222 |
|
| 223 |
void deserialize(Serialized aValue) { |
| 224 |
IncVersion(); |
| 225 |
mBitField = aValue; |
| 226 |
} |
| 227 |
|
| 228 |
class ConstIterator { |
| 229 |
const EnumSet* mSet; |
| 230 |
size_t mPos; |
| 231 |
#ifdef DEBUG |
| 232 |
uint64_t mVersion; |
| 233 |
#endif |
| 234 |
|
| 235 |
void checkVersion() const { |
| 236 |
// Check that the set has not been modified while being iterated. |
| 237 |
MOZ_ASSERT_IF(mSet, mSet->mVersion == mVersion); |
| 238 |
} |
| 239 |
|
| 240 |
public: |
| 241 |
ConstIterator(const EnumSet& aSet, size_t aPos) : mSet(&aSet), mPos(aPos) { |
| 242 |
#ifdef DEBUG |
| 243 |
mVersion = mSet->mVersion; |
| 244 |
#endif |
| 245 |
MOZ_ASSERT(aPos <= kMaxBits); |
| 246 |
if (aPos != kMaxBits && !mSet->HasBitAt(mPos)) { |
| 247 |
++*this; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
ConstIterator(const ConstIterator& aOther) |
| 252 |
: mSet(aOther.mSet), mPos(aOther.mPos) { |
| 253 |
#ifdef DEBUG |
| 254 |
mVersion = aOther.mVersion; |
| 255 |
checkVersion(); |
| 256 |
#endif |
| 257 |
} |
| 258 |
|
| 259 |
ConstIterator(ConstIterator&& aOther) |
| 260 |
: mSet(aOther.mSet), mPos(aOther.mPos) { |
| 261 |
#ifdef DEBUG |
| 262 |
mVersion = aOther.mVersion; |
| 263 |
checkVersion(); |
| 264 |
#endif |
| 265 |
aOther.mSet = nullptr; |
| 266 |
} |
| 267 |
|
| 268 |
~ConstIterator() { checkVersion(); } |
| 269 |
|
| 270 |
bool operator==(const ConstIterator& other) const { |
| 271 |
MOZ_ASSERT(mSet == other.mSet); |
| 272 |
checkVersion(); |
| 273 |
return mPos == other.mPos; |
| 274 |
} |
| 275 |
|
| 276 |
bool operator!=(const ConstIterator& other) const { |
| 277 |
return !(*this == other); |
| 278 |
} |
| 279 |
|
| 280 |
T operator*() const { |
| 281 |
MOZ_ASSERT(mSet); |
| 282 |
MOZ_ASSERT(mPos < kMaxBits); |
| 283 |
MOZ_ASSERT(mSet->HasBitAt(mPos)); |
| 284 |
checkVersion(); |
| 285 |
return T(mPos); |
| 286 |
} |
| 287 |
|
| 288 |
ConstIterator& operator++() { |
| 289 |
MOZ_ASSERT(mSet); |
| 290 |
MOZ_ASSERT(mPos < kMaxBits); |
| 291 |
checkVersion(); |
| 292 |
do { |
| 293 |
mPos++; |
| 294 |
} while (mPos < kMaxBits && !mSet->HasBitAt(mPos)); |
| 295 |
return *this; |
| 296 |
} |
| 297 |
}; |
| 298 |
|
| 299 |
ConstIterator begin() const { return ConstIterator(*this, 0); } |
| 300 |
|
| 301 |
ConstIterator end() const { return ConstIterator(*this, kMaxBits); } |
| 302 |
|
| 303 |
private: |
| 304 |
constexpr static Serialized BitFor(T aEnum) { |
| 305 |
const auto pos = static_cast<size_t>(aEnum); |
| 306 |
return BitAt(pos); |
| 307 |
} |
| 308 |
|
| 309 |
constexpr static Serialized BitAt(size_t aPos) { |
| 310 |
MOZ_DIAGNOSTIC_ASSERT(aPos < kMaxBits); |
| 311 |
if constexpr (std::is_unsigned_v<Serialized>) { |
| 312 |
return static_cast<Serialized>(Serialized{1} << aPos); |
| 313 |
} else { |
| 314 |
Serialized bitField; |
| 315 |
bitField[aPos] = true; |
| 316 |
return bitField; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
constexpr bool HasBitFor(T aEnum) const { |
| 321 |
const auto pos = static_cast<size_t>(aEnum); |
| 322 |
return HasBitAt(pos); |
| 323 |
} |
| 324 |
|
| 325 |
constexpr bool HasBitAt(size_t aPos) const { |
| 326 |
if constexpr (std::is_unsigned_v<Serialized>) { |
| 327 |
return mBitField & BitAt(aPos); |
| 328 |
} else { |
| 329 |
// for std::bitset and mozilla::BitSet |
| 330 |
return mBitField.test(aPos); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
constexpr void IncVersion() { |
| 335 |
#ifdef DEBUG |
| 336 |
mVersion++; |
| 337 |
#endif |
| 338 |
} |
| 339 |
|
| 340 |
static constexpr size_t MaxBits() { |
| 341 |
if constexpr (std::is_unsigned_v<Serialized>) { |
| 342 |
return sizeof(Serialized) * 8; |
| 343 |
} else { |
| 344 |
return Serialized().size(); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
static constexpr size_t kMaxBits = MaxBits(); |
| 349 |
|
| 350 |
Serialized mBitField; |
| 351 |
|
| 352 |
#ifdef DEBUG |
| 353 |
uint64_t mVersion = 0; |
| 354 |
#endif |
| 355 |
}; |
| 356 |
|
| 357 |
} // namespace mozilla |
| 358 |
|
| 359 |
#endif /* mozilla_EnumSet_h_*/ |