diff options
| author | Martino Fontana <tinozzo123@gmail.com> | 2026-02-15 10:42:25 +0100 |
|---|---|---|
| committer | Martino Fontana <tinozzo123@gmail.com> | 2026-11-15 22:31:04 +0100 |
| commit | 14d5fe714d5c270fdce4e8541b3903de3d5b2cff (patch) | |
| tree | d21ccfa76a9c9853fea69d963d63a0d35ce2269f /Source/Core/Common | |
| parent | c73fcc15300546f598481203f3ae9526968106c2 (diff) | |
BitSet: Some small additions
Also add requirements for using range functions.
And micro-optimize testing if a bit is set (GCC and MSVC struggle to emit `bt` or `tbz`/`tbnz` with `bs & (1 << n)` compared to `(bs >> n) & 1`).
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/BitSet.h | 36 |
1 files changed, 23 insertions, 13 deletions
diff --git a/Source/Core/Common/BitSet.h b/Source/Core/Common/BitSet.h index 87e0038554..7c465e5558 100644 --- a/Source/Core/Common/BitSet.h +++ b/Source/Core/Common/BitSet.h @@ -3,9 +3,9 @@ #pragma once #include <bit> +#include <concepts> #include <cstddef> #include <initializer_list> -#include <type_traits> #include "Common/CommonTypes.h" @@ -29,28 +29,26 @@ namespace Common // operation.) // - Counting set bits using .Count() - see comment on that method. -template <typename IntTy> +template <std::unsigned_integral IntTy> class BitSet { - static_assert(!std::is_signed<IntTy>::value, "BitSet should not be used with signed types"); - public: // A reference to a particular bit, returned from operator[]. class Ref { public: - constexpr Ref(Ref&& other) : m_bs(other.m_bs), m_mask(other.m_mask) {} - constexpr Ref(BitSet* bs, IntTy mask) : m_bs(bs), m_mask(mask) {} - constexpr operator bool() const { return (m_bs->m_val & m_mask) != 0; } - bool operator=(bool set) + constexpr Ref(Ref&& other) : m_bs(other.m_bs), m_shift(other.m_shift) {} + constexpr Ref(BitSet* bs, size_t shift) : m_bs(bs), m_shift(shift) {} + constexpr operator bool() const { return (m_bs->m_val >> m_shift) & 1; } + constexpr Ref& operator=(bool set) { - m_bs->m_val = (m_bs->m_val & ~m_mask) | (set ? m_mask : 0); - return set; + m_bs->m_val = (m_bs->m_val & ~(!set << m_shift)) | (set << m_shift); + return *this; } private: BitSet* m_bs; - IntTy m_mask; + size_t m_shift; }; // A STL-like iterator is required to be able to use range-based for loops. @@ -58,6 +56,9 @@ public: { public: constexpr Iterator(const Iterator& other) : m_val(other.m_val), m_bit(other.m_bit) {} + using value_type = int; + using difference_type = int; + constexpr Iterator() : m_val(0), m_bit(-1) {} constexpr Iterator(IntTy val, int bit) : m_val(val), m_bit(bit) {} Iterator& operator=(Iterator other) { @@ -101,13 +102,15 @@ public: m_val |= IntTy{1} << bit; } + constexpr static BitSet AllTrue() { return BitSet(~IntTy{0}); } + constexpr static BitSet AllTrue(size_t count) { return BitSet(count == sizeof(IntTy) * 8 ? ~IntTy{0} : ((IntTy{1} << count) - 1)); } - Ref operator[](size_t bit) { return Ref(this, IntTy{1} << bit); } - constexpr const Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; } + constexpr Ref operator[](size_t bit) { return Ref(this, bit); } + constexpr Ref operator[](size_t bit) const { return (*const_cast<BitSet*>(this))[bit]; } constexpr bool operator==(BitSet other) const { return m_val == other.m_val; } constexpr bool operator!=(BitSet other) const { return m_val != other.m_val; } constexpr bool operator<(BitSet other) const { return m_val < other.m_val; } @@ -131,6 +134,13 @@ public: constexpr unsigned int Count() const { return std::popcount(m_val); } constexpr Iterator begin() const { return ++Iterator(m_val, 0); } constexpr Iterator end() const { return Iterator(m_val, -1); } + + template <std::unsigned_integral IntTyTo> + constexpr BitSet<IntTyTo> Cast() + { + return BitSet<IntTyTo>((IntTyTo)m_val); + } + IntTy m_val{}; }; } // namespace Common |
