| 1 |
|
| 2 |
|
| 3 |
#pragma once |
| 4 |
|
| 5 |
#include <bit> |
| 6 |
#include <cstddef> |
| 7 |
#include <initializer_list> |
| 8 |
#include <type_traits> |
| 9 |
|
| 10 |
#include "Common/CommonTypes.h" |
| 11 |
|
| 12 |
namespace Common |
| 13 |
{ |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
template <typename IntTy> |
| 33 |
class BitSet |
| 34 |
{ |
| 35 |
static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); |
| 36 |
|
| 37 |
public: |
| 38 |
|
| 39 |
class Ref |
| 40 |
{ |
| 41 |
public: |
| 42 |
constexpr Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} |
| 43 |
constexpr Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} |
| 44 |
constexpr operator bool() const { return (m_bs->m_val & m_mask) != 0; } |
| 45 |
bool operator=(bool set) |
| 46 |
{ |
| 47 |
m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); |
| 48 |
return set; |
| 49 |
} |
| 50 |
|
| 51 |
private: |
| 52 |
BitSet* m_bs; |
| 53 |
IntTy m_mask; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
class Iterator |
| 58 |
{ |
| 59 |
public: |
| 60 |
constexpr Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {} |
| 61 |
constexpr Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {} |
| 62 |
Iterator& operator=(Iterator other) |
| 63 |
{ |
| 64 |
new (this) Iterator(other); |
| 65 |
return *this; |
| 66 |
} |
| 67 |
Iterator& operator++() |
| 68 |
{ |
| 69 |
if (m_val == 0) |
| 70 |
{ |
| 71 |
m_bit = -1; |
| 72 |
} |
| 73 |
else |
| 74 |
{ |
| 75 |
int bit = std::countr_zero(m_val); |
| 76 |
m_val &= ~(IntTy{1} << bit); |
| 77 |
m_bit = bit; |
| 78 |
} |
| 79 |
return *this; |
| 80 |
} |
| 81 |
Iterator operator++(int) |
| 82 |
{ |
| 83 |
Iterator other(*this); |
| 84 |
++*this; |
| 85 |
return other; |
| 86 |
} |
| 87 |
constexpr int operator*() const { return m_bit; } |
| 88 |
constexpr bool operator==(Iterator other) const { return m_bit == other.m_bit; } |
| 89 |
constexpr bool operator!=(Iterator other) const { return m_bit != other.m_bit; } |
| 90 |
|
| 91 |
private: |
| 92 |
IntTy m_val; |
| 93 |
int m_bit; |
| 94 |
}; |
| 95 |
|
| 96 |
constexpr BitSet() = default; |
| 97 |
constexpr explicit BitSet(IntTy val) : m_val(val) {} |
| 98 |
constexpr BitSet(std::initializer_list<int> init) |
| 99 |
{ |
| 100 |
for (int bit : init) |
| 101 |
m_val |= IntTy{1} << bit; |
| 102 |
} |
| 103 |
|
| 104 |
constexpr static BitSet AllTrue(size_t count) |
| 105 |
{ |
| 106 |
return BitSet(count == sizeof(IntTy) * 8 ? ~IntTy{0} : ((IntTy{1} << count) - 1)); |
| 107 |
} |
| 108 |
|
| 109 |
Ref operator[](size_t bit) { return Ref(this, IntTy{1} << bit); } |
| 110 |
constexpr const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; } |
| 111 |
constexpr bool operator==(BitSet other) const { return m_val == other.m_val; } |
| 112 |
constexpr bool operator!=(BitSet other) const { return m_val != other.m_val; } |
| 113 |
constexpr bool operator<(BitSet other) const { return m_val < other.m_val; } |
| 114 |
constexpr bool operator>(BitSet other) const { return m_val > other.m_val; } |
| 115 |
constexpr BitSet operator|(BitSet other) const { return BitSet(m_val | other.m_val); } |
| 116 |
constexpr BitSet operator&(BitSet other) const { return BitSet(m_val & other.m_val); } |
| 117 |
constexpr BitSet operator^(BitSet other) const { return BitSet(m_val ^ other.m_val); } |
| 118 |
constexpr BitSet operator~() const { return BitSet(~m_val); } |
| 119 |
constexpr BitSet operator<<(IntTy shift) const { return BitSet(m_val << shift); } |
| 120 |
constexpr BitSet operator>>(IntTy shift) const { return BitSet(m_val >> shift); } |
| 121 |
constexpr explicit operator bool() const { return m_val != 0; } |
| 122 |
BitSet& operator|=(BitSet other) { return *this = *this | other; } |
| 123 |
BitSet& operator&=(BitSet other) { return *this = *this & other; } |
| 124 |
BitSet& operator^=(BitSet other) { return *this = *this ^ other; } |
| 125 |
BitSet& operator<<=(IntTy shift) { return *this = *this << shift; } |
| 126 |
BitSet& operator>>=(IntTy shift) { return *this = *this >> shift; } |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
constexpr unsigned int Count() const { return std::popcount(m_val); } |
| 132 |
constexpr Iterator begin() const { return ++Iterator(m_val, 0); } |
| 133 |
constexpr Iterator end() const { return Iterator(m_val, -1); } |
| 134 |
IntTy m_val{}; |
| 135 |
}; |
| 136 |
} |
| 137 |
|
| 138 |
using BitSet8 = Common::BitSet<u8>; |
| 139 |
using BitSet16 = Common::BitSet<u16>; |
| 140 |
using BitSet32 = Common::BitSet<u32>; |
| 141 |
using BitSet64 = Common::BitSet<u64>; |