Debian Codegraph @

Source/Core/Common/BitSet.h

raw symbols SCIP
1 // SPDX-License-Identifier: CC0-1.0
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 // Similar to std::bitset, this is a class which encapsulates a bitset, i.e.
15 // using the set bits of an integer to represent a set of integers. Like that
16 // class, it acts like an array of bools:
17 // BitSet32 bs;
18 // bs[1] = true;
19 // but also like the underlying integer ([0] = least significant bit):
20 // BitSet32 bs2 = ...;
21 // bs = (bs ^ bs2) & BitSet32(0xffff);
22 // The following additional functionality is provided:
23 // - Construction using an initializer list.
24 // BitSet bs { 1, 2, 4, 8 };
25 // - Efficiently iterating through the set bits:
26 // for (int i : bs)
27 // [i is the *index* of a set bit]
28 // (This uses the appropriate CPU instruction to find the next set bit in one
29 // operation.)
30 // - Counting set bits using .Count() - see comment on that method.
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 // A reference to a particular bit, returned from operator[].
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 // A STL-like iterator is required to be able to use range-based for loops.
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 // Warning: Even though on modern CPUs this is a single fast instruction,
128 // Dolphin's official builds do not currently assume POPCNT support on x86,
129 // so slower explicit bit twiddling is generated. Still should generally
130 // be faster than a loop.
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 } // namespace Common
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>;