9#ifndef FORTRAN_COMMON_CONSTEXPR_BITSET_H_
10#define FORTRAN_COMMON_CONSTEXPR_BITSET_H_
15#include "bit-population-count.h"
19#include <initializer_list>
26 static_assert(BITS > 0 && BITS <= 128);
27 using Word = HostUnsignedIntType<(BITS <= 32 ? 32 : BITS)>;
28 static constexpr Word allBits{
29 ~static_cast<Word>(0) >> (8 *
sizeof(Word) - BITS)};
31 constexpr BitSet(Word b) : bits_{b} {}
35 constexpr BitSet(
const std::initializer_list<int> &xs) {
70 constexpr BitSet operator~()
const {
return ~bits_; }
72 return bits_ & that.bits_;
74 constexpr BitSet operator&(
BitSet &&that)
const {
return bits_ & that.bits_; }
76 return bits_ ^ that.bits_;
78 constexpr BitSet operator^(
BitSet &&that)
const {
return bits_ & that.bits_; }
80 return bits_ | that.bits_;
82 constexpr BitSet operator|(
BitSet &&that)
const {
return bits_ | that.bits_; }
84 constexpr bool operator==(
const BitSet &that)
const {
85 return bits_ == that.bits_;
87 constexpr bool operator==(
BitSet &&that)
const {
return bits_ == that.bits_; }
88 constexpr bool operator!=(
const BitSet &that)
const {
89 return bits_ != that.bits_;
91 constexpr bool operator!=(
BitSet &&that)
const {
return bits_ != that.bits_; }
93 static constexpr std::size_t size() {
return BITS; }
94 constexpr bool test(std::size_t x)
const {
95 return x < BITS && ((bits_ >> x) & 1) != 0;
98 constexpr bool all()
const {
return bits_ == allBits; }
99 constexpr bool any()
const {
return bits_ != 0; }
100 constexpr bool none()
const {
return bits_ == 0; }
102 constexpr std::size_t count()
const {
return BitPopulationCount(bits_); }
108 constexpr BitSet set(std::size_t x,
bool value =
true) {
112 bits_ |=
static_cast<Word
>(1) << x;
116 constexpr BitSet &reset() {
120 constexpr BitSet &reset(std::size_t x) {
121 bits_ &= ~(
static_cast<Word
>(1) << x);
124 constexpr BitSet &flip() {
128 constexpr BitSet &flip(std::size_t x) {
129 bits_ ^=
static_cast<Word
>(1) << x;
133 constexpr std::optional<std::size_t> LeastElement()
const {
137 return {TrailingZeroBitCount(bits_)};
141 Word bits()
const {
return bits_; }
Definition: constexpr-bitset.h:25
Definition: bit-population-count.h:20