9#ifndef FORTRAN_COMMON_ENUM_SET_H_
10#define FORTRAN_COMMON_ENUM_SET_H_
17#include "constexpr-bitset.h"
21#include <initializer_list>
28template <
typename ENUM, std::
size_t BITS>
class EnumSet {
29 static_assert(BITS > 0);
35 std::conditional_t<(BITS <= 64), common::BitSet<BITS>, std::bitset<BITS>>;
36 using enumerationType = ENUM;
39 constexpr EnumSet(
const std::initializer_list<enumerationType> &enums) {
40 for (
auto it{enums.begin()}; it != enums.end(); ++it) {
50 const bitsetType &bitset()
const {
return bitset_; }
53 bitset_ &= that.bitset_;
57 bitset_ &= that.bitset_;
61 bitset_ |= that.bitset_;
65 bitset_ |= that.bitset_;
69 bitset_ ^= that.bitset_;
73 bitset_ ^= that.bitset_;
77 constexpr EnumSet operator~()
const {
79 result.bitset_ = ~bitset_;
84 result.bitset_ &= that.bitset_;
89 result.bitset_ &= that.bitset_;
94 result.bitset_ |= that.bitset_;
99 result.bitset_ |= that.bitset_;
104 result.bitset_ ^= that.bitset_;
109 result.bitset_ ^= that.bitset_;
113 constexpr EnumSet operator+(enumerationType v)
const {
116 constexpr EnumSet operator-(enumerationType v)
const {
120 constexpr bool operator==(
const EnumSet &that)
const {
121 return bitset_ == that.bitset_;
123 constexpr bool operator==(
EnumSet &&that)
const {
124 return bitset_ == that.bitset_;
126 constexpr bool operator!=(
const EnumSet &that)
const {
127 return bitset_ != that.bitset_;
129 constexpr bool operator!=(
EnumSet &&that)
const {
130 return bitset_ != that.bitset_;
135 static constexpr std::size_t max_size() {
return BITS; }
136 constexpr bool test(enumerationType x)
const {
137 return bitset_.test(
static_cast<std::size_t
>(x));
139 constexpr bool all()
const {
return bitset_.all(); }
140 constexpr bool any()
const {
return bitset_.any(); }
141 constexpr bool none()
const {
return bitset_.none(); }
145 constexpr std::size_t count()
const {
return bitset_.count(); }
146 constexpr std::size_t count(enumerationType x)
const {
147 return test(x) ? 1 : 0;
154 constexpr EnumSet &set(enumerationType x,
bool value =
true) {
155 bitset_.set(
static_cast<std::size_t
>(x), value);
162 constexpr EnumSet &reset(enumerationType x) {
163 bitset_.reset(
static_cast<std::size_t
>(x));
170 constexpr EnumSet &flip(enumerationType x) {
171 bitset_.flip(
static_cast<std::size_t
>(x));
175 constexpr bool empty()
const {
return none(); }
176 void clear() { reset(); }
177 void insert(enumerationType x) { set(x); }
178 void insert(enumerationType &&x) { set(x); }
179 void emplace(enumerationType &&x) { set(x); }
180 void erase(enumerationType x) { reset(x); }
181 void erase(enumerationType &&x) { reset(x); }
183 constexpr std::optional<enumerationType> LeastElement()
const {
186 }
else if constexpr (std::is_same_v<bitsetType, common::BitSet<BITS>>) {
187 return {
static_cast<enumerationType
>(bitset_.LeastElement().value())};
190 for (std::size_t j{0}; j < BITS; ++j) {
191 auto enumerator{
static_cast<enumerationType
>(j)};
192 if (bitset_.test(j)) {
196 die(
"EnumSet::LeastElement(): no bit found in non-empty std::bitset");
200 template <
typename FUNC>
void IterateOverMembers(
const FUNC &f)
const {
202 while (
auto least{copy.LeastElement()}) {
208 template <
typename STREAM>
210 STREAM &o, std::string_view EnumToString(enumerationType))
const {
212 IterateOverMembers([&](
auto e) {
213 o << sep << EnumToString(e);
216 return o << (sep ==
'{' ?
"{}" :
"}");
220 bitsetType bitset_{};
224template <
typename ENUM, std::
size_t values>
225struct std::hash<
Fortran::common::EnumSet<ENUM, values>> {
226 std::size_t operator()(
228 return std::hash(x.bitset());
Definition: enum-set.h:28
Definition: bit-population-count.h:20
Definition: bit-population-count.h:20