2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-11-07 03:23:13 +04:00
|
|
|
/* 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"
|
2014-06-07 07:17:06 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2021-11-09 15:34:51 +03:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2016-10-19 07:33:14 +03:00
|
|
|
|
|
|
|
#include <initializer_list>
|
2018-09-07 17:40:02 +03:00
|
|
|
#include <type_traits>
|
2013-07-30 18:25:31 +04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2012-11-07 03:23:13 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
2018-09-07 17:40:02 +03:00
|
|
|
* 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
|
2021-11-09 15:34:51 +03:00
|
|
|
* and enum class types. EnumSet also works with U being a BitSet.
|
2012-11-07 03:23:13 +04:00
|
|
|
*/
|
2018-09-07 17:40:02 +03:00
|
|
|
template <typename T, typename Serialized = typename std::make_unsigned<
|
|
|
|
typename std::underlying_type<T>::type>::type>
|
2012-11-07 03:23:13 +04:00
|
|
|
class EnumSet {
|
2014-06-13 10:34:08 +04:00
|
|
|
public:
|
2021-11-09 15:34:51 +03:00
|
|
|
using valueType = T;
|
|
|
|
using serializedType = Serialized;
|
2018-01-21 23:23:21 +03:00
|
|
|
|
2021-11-09 15:34:51 +03:00
|
|
|
constexpr EnumSet() : mBitField() {}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2024-02-02 12:43:10 +03:00
|
|
|
constexpr MOZ_IMPLICIT EnumSet(T aEnum) : mBitField(BitFor(aEnum)) {}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2018-10-01 23:09:10 +03:00
|
|
|
constexpr EnumSet(T aEnum1, T aEnum2)
|
2024-02-02 12:43:10 +03:00
|
|
|
: mBitField(BitFor(aEnum1) | BitFor(aEnum2)) {}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2018-10-01 23:09:10 +03:00
|
|
|
constexpr EnumSet(T aEnum1, T aEnum2, T aEnum3)
|
2024-02-02 12:43:10 +03:00
|
|
|
: mBitField(BitFor(aEnum1) | BitFor(aEnum2) | BitFor(aEnum3)) {}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2018-10-01 23:09:10 +03:00
|
|
|
constexpr EnumSet(T aEnum1, T aEnum2, T aEnum3, T aEnum4)
|
2024-02-02 12:43:10 +03:00
|
|
|
: mBitField(BitFor(aEnum1) | BitFor(aEnum2) | BitFor(aEnum3) |
|
|
|
|
BitFor(aEnum4)) {}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2021-11-09 15:34:51 +03:00
|
|
|
constexpr MOZ_IMPLICIT EnumSet(std::initializer_list<T> list) : mBitField() {
|
2016-04-28 16:25:05 +03:00
|
|
|
for (auto value : list) {
|
|
|
|
(*this) += value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 23:40:37 +03:00
|
|
|
#ifdef DEBUG
|
2018-10-01 23:09:10 +03:00
|
|
|
constexpr EnumSet(const EnumSet& aEnumSet) : mBitField(aEnumSet.mBitField) {}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2020-08-06 23:40:37 +03:00
|
|
|
constexpr EnumSet& operator=(const EnumSet& aEnumSet) {
|
|
|
|
mBitField = aEnumSet.mBitField;
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
2020-08-06 23:40:37 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Add an element
|
|
|
|
*/
|
2019-03-06 20:38:11 +03:00
|
|
|
constexpr void operator+=(T aEnum) {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
|
|
|
mBitField |= BitFor(aEnum);
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an element
|
|
|
|
*/
|
2019-03-06 20:38:11 +03:00
|
|
|
constexpr EnumSet operator+(T aEnum) const {
|
2018-09-07 17:40:02 +03:00
|
|
|
EnumSet result(*this);
|
2014-06-13 10:34:08 +04:00
|
|
|
result += aEnum;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Union
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
void operator+=(const EnumSet& aEnumSet) {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField |= aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Union
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
EnumSet operator+(const EnumSet& aEnumSet) const {
|
2018-09-07 17:40:02 +03:00
|
|
|
EnumSet result(*this);
|
2014-06-13 10:34:08 +04:00
|
|
|
result += aEnumSet;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an element
|
|
|
|
*/
|
|
|
|
void operator-=(T aEnum) {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
|
|
|
mBitField &= ~(BitFor(aEnum));
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an element
|
|
|
|
*/
|
2018-09-07 17:40:02 +03:00
|
|
|
EnumSet operator-(T aEnum) const {
|
|
|
|
EnumSet result(*this);
|
2014-06-13 10:34:08 +04:00
|
|
|
result -= aEnum;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a set of elements
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
void operator-=(const EnumSet& aEnumSet) {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField &= ~(aEnumSet.mBitField);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a set of elements
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
EnumSet operator-(const EnumSet& aEnumSet) const {
|
2018-09-07 17:40:02 +03:00
|
|
|
EnumSet result(*this);
|
2014-06-13 10:34:08 +04:00
|
|
|
result -= aEnumSet;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:28:53 +03:00
|
|
|
/**
|
|
|
|
* Clear
|
|
|
|
*/
|
|
|
|
void clear() {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
2021-11-09 15:34:51 +03:00
|
|
|
mBitField = Serialized();
|
2015-10-30 18:28:53 +03:00
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Intersection
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
void operator&=(const EnumSet& aEnumSet) {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField &= aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intersection
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
EnumSet operator&(const EnumSet& aEnumSet) const {
|
2018-09-07 17:40:02 +03:00
|
|
|
EnumSet result(*this);
|
2014-06-13 10:34:08 +04:00
|
|
|
result &= aEnumSet;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equality
|
|
|
|
*/
|
2018-09-25 21:47:38 +03:00
|
|
|
bool operator==(const EnumSet& aEnumSet) const {
|
2014-06-13 10:34:08 +04:00
|
|
|
return mBitField == aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
2018-09-25 21:47:38 +03:00
|
|
|
/**
|
|
|
|
* Equality
|
|
|
|
*/
|
2024-02-02 12:43:10 +03:00
|
|
|
bool operator==(T aEnum) const { return mBitField == BitFor(aEnum); }
|
2018-09-25 21:47:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Not equal
|
|
|
|
*/
|
|
|
|
bool operator!=(const EnumSet& aEnumSet) const {
|
|
|
|
return !operator==(aEnumSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Not equal
|
|
|
|
*/
|
|
|
|
bool operator!=(T aEnum) const { return !operator==(aEnum); }
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Test is an element is contained in the set.
|
|
|
|
*/
|
2024-02-02 12:43:10 +03:00
|
|
|
bool contains(T aEnum) const { return HasBitFor(aEnum); }
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2018-09-25 21:47:38 +03:00
|
|
|
/**
|
|
|
|
* Test if a set is contained in the set.
|
|
|
|
*/
|
|
|
|
bool contains(const EnumSet& aEnumSet) const {
|
|
|
|
return (mBitField & aEnumSet.mBitField) == aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Return the number of elements in the set.
|
|
|
|
*/
|
2021-11-09 15:34:51 +03:00
|
|
|
size_t size() const {
|
|
|
|
if constexpr (std::is_unsigned_v<Serialized>) {
|
|
|
|
if constexpr (kMaxBits > 32) {
|
|
|
|
return CountPopulation64(mBitField);
|
|
|
|
} else {
|
|
|
|
return CountPopulation32(mBitField);
|
2012-11-07 03:23:13 +04:00
|
|
|
}
|
2021-11-09 15:34:51 +03:00
|
|
|
} else {
|
|
|
|
return mBitField.Count();
|
2012-11-07 03:23:13 +04:00
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
|
|
|
|
2021-11-09 15:34:51 +03:00
|
|
|
bool isEmpty() const {
|
|
|
|
if constexpr (std::is_unsigned_v<Serialized>) {
|
|
|
|
return mBitField == 0;
|
|
|
|
} else {
|
|
|
|
return mBitField.IsEmpty();
|
|
|
|
}
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2018-09-07 17:40:02 +03:00
|
|
|
Serialized serialize() const { return mBitField; }
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2018-09-07 17:40:02 +03:00
|
|
|
void deserialize(Serialized aValue) {
|
2024-02-02 12:43:10 +03:00
|
|
|
IncVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField = aValue;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:25:05 +03:00
|
|
|
class ConstIterator {
|
2018-09-07 17:40:02 +03:00
|
|
|
const EnumSet* mSet;
|
2024-02-02 12:43:10 +03:00
|
|
|
size_t mPos;
|
2016-04-28 16:25:05 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
uint64_t mVersion;
|
|
|
|
#endif
|
|
|
|
|
2017-07-26 16:19:11 +03:00
|
|
|
void checkVersion() const {
|
2016-04-28 16:25:05 +03:00
|
|
|
// Check that the set has not been modified while being iterated.
|
|
|
|
MOZ_ASSERT_IF(mSet, mSet->mVersion == mVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2024-02-02 12:43:10 +03:00
|
|
|
ConstIterator(const EnumSet& aSet, size_t aPos) : mSet(&aSet), mPos(aPos) {
|
2016-04-28 16:25:05 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion = mSet->mVersion;
|
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(aPos <= kMaxBits);
|
2024-02-02 12:43:10 +03:00
|
|
|
if (aPos != kMaxBits && !mSet->HasBitAt(mPos)) {
|
2021-11-09 15:34:51 +03:00
|
|
|
++*this;
|
|
|
|
}
|
2016-04-28 16:25:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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(); }
|
|
|
|
|
2017-07-26 16:19:11 +03:00
|
|
|
bool operator==(const ConstIterator& other) const {
|
2016-04-28 16:25:05 +03:00
|
|
|
MOZ_ASSERT(mSet == other.mSet);
|
|
|
|
checkVersion();
|
|
|
|
return mPos == other.mPos;
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:19:11 +03:00
|
|
|
bool operator!=(const ConstIterator& other) const {
|
2016-04-28 16:25:05 +03:00
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:19:11 +03:00
|
|
|
T operator*() const {
|
2016-04-28 16:25:05 +03:00
|
|
|
MOZ_ASSERT(mSet);
|
|
|
|
MOZ_ASSERT(mPos < kMaxBits);
|
2024-02-02 12:43:10 +03:00
|
|
|
MOZ_ASSERT(mSet->HasBitAt(mPos));
|
2016-04-28 16:25:05 +03:00
|
|
|
checkVersion();
|
|
|
|
return T(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator& operator++() {
|
|
|
|
MOZ_ASSERT(mSet);
|
|
|
|
MOZ_ASSERT(mPos < kMaxBits);
|
|
|
|
checkVersion();
|
|
|
|
do {
|
|
|
|
mPos++;
|
2024-02-02 12:43:10 +03:00
|
|
|
} while (mPos < kMaxBits && !mSet->HasBitAt(mPos));
|
2016-04-28 16:25:05 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
|
|
|
|
|
|
|
ConstIterator end() const { return ConstIterator(*this, kMaxBits); }
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
private:
|
2024-02-02 12:43:10 +03:00
|
|
|
constexpr static Serialized BitFor(T aEnum) {
|
2024-02-02 12:43:10 +03:00
|
|
|
const auto pos = static_cast<size_t>(aEnum);
|
|
|
|
return BitAt(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr static Serialized BitAt(size_t aPos) {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPos < kMaxBits);
|
2021-11-09 15:34:51 +03:00
|
|
|
if constexpr (std::is_unsigned_v<Serialized>) {
|
2024-02-02 12:43:10 +03:00
|
|
|
return static_cast<Serialized>(Serialized{1} << aPos);
|
2021-11-09 15:34:51 +03:00
|
|
|
} else {
|
|
|
|
Serialized bitField;
|
2024-02-02 12:43:10 +03:00
|
|
|
bitField[aPos] = true;
|
2021-11-09 15:34:51 +03:00
|
|
|
return bitField;
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
}
|
|
|
|
|
2024-02-02 12:43:10 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2024-02-02 12:43:10 +03:00
|
|
|
constexpr void IncVersion() {
|
2016-04-28 16:25:05 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-08-17 09:39:12 +03:00
|
|
|
static constexpr size_t MaxBits() {
|
2021-11-09 15:34:51 +03:00
|
|
|
if constexpr (std::is_unsigned_v<Serialized>) {
|
|
|
|
return sizeof(Serialized) * 8;
|
|
|
|
} else {
|
2022-08-17 09:39:12 +03:00
|
|
|
return Serialized::Size();
|
2021-11-09 15:34:51 +03:00
|
|
|
}
|
2022-08-17 09:39:12 +03:00
|
|
|
}
|
2021-11-09 15:34:51 +03:00
|
|
|
|
2022-08-17 09:39:12 +03:00
|
|
|
static constexpr size_t kMaxBits = MaxBits();
|
2018-09-07 17:40:02 +03:00
|
|
|
|
|
|
|
Serialized mBitField;
|
2016-04-28 16:25:05 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2017-10-17 16:19:16 +03:00
|
|
|
uint64_t mVersion = 0;
|
2016-04-28 16:25:05 +03:00
|
|
|
#endif
|
2012-11-07 03:23:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_EnumSet_h_*/
|