8#ifndef NUMAPP_BITMASK_HPP_
9#define NUMAPP_BITMASK_HPP_
23using NumaBitmaskPtr = std::unique_ptr<
struct bitmask, void (*)(
struct bitmask*)>;
46template <
class Class,
class F>
47void ForEach(Bitmask<Class>
const& bitmask, F&& f,
bool value =
true);
57 Bitmask() : m_bitmask(Class::Alloc()) {
60 Bitmask(Bitmask&& rhs)
noexcept =
default;
61 Bitmask(Bitmask
const& rhs) : m_bitmask(Class::Alloc()) {
64 Bitmask& operator=(Bitmask&&)
noexcept =
default;
73 copy_bitmask_to_bitmask(rhs.m_bitmask.get(), m_bitmask.get());
84 [[nodiscard]]
bool operator==(Bitmask
const& rhs)
const noexcept {
85 return numa_bitmask_equal(m_bitmask.get(), rhs.m_bitmask.get()) == 1;
90 [[nodiscard]]
bool operator!=(Bitmask
const& rhs)
const noexcept {
91 return !(
this == rhs);
105 return m_bitmask.get();
113 return m_bitmask.get();
121 [[nodiscard]] std::size_t
GetSize() const noexcept {
122 return m_bitmask->size;
131 [[nodiscard]]
bool Test(
unsigned int position)
const noexcept {
132 return numa_bitmask_isbitset(
GetNative(), position);
152 Bitmask&
Set(
unsigned int position,
bool value =
true) noexcept {
154 numa_bitmask_setbit(
GetNative(), position);
156 numa_bitmask_clearbit(
GetNative(), position);
169 Bitmask&
Reset(
unsigned int position)
noexcept {
170 numa_bitmask_clearbit(
GetNative(), position);
198template <
class Class>
199std::ostream&
FormatBitmask(std::ostream& os, Bitmask<Class>
const& mask,
int min_bits);
212std::ostream&
FormatBitmask(std::ostream& os, bitmask
const* mask,
int min_bits);
214template <
class Class>
215std::ostream&
FormatBitmask(std::ostream& os, Bitmask<Class>
const& mask,
int min_bits) {
219template <
class Class,
class F>
220void ForEach(Bitmask<Class>
const& bits, F&& f,
bool value) {
221 static_assert(std::is_invocable_v<F, unsigned int>,
222 "Function must be invocable with signature `f(unsigned int)`");
223 auto const num_bits = bits.
GetSize();
224 for (
unsigned int position = 0u; position < num_bits; ++position) {
225 if (bits.
Test(position) == value) {
std::size_t GetSize() const noexcept
Query number of bits in mask.
bool operator==(Bitmask const &rhs) const noexcept
Bitmask & Set(unsigned int position, bool value=true) noexcept
Set bit in position to specified value.
struct bitmask * GetNative() noexcept
Access native bitmask type.
Bitmask & operator=(Bitmask const &rhs) noexcept
Copy from rhs to this.
Bitmask(NumaBitmaskPtr &&bitmask)
Construction from native bitmask type can be error prone in that the mask wasn't allocated using the ...
Bitmask & Reset(unsigned int position) noexcept
Sets bit in specified position to false.
struct bitmask const * GetNative() const noexcept
Access native bitmask type.
bool Test(unsigned int position) const noexcept
Test if bit in specified position is set.
bool operator!=(Bitmask const &rhs) const noexcept
std::unique_ptr< struct bitmask, void(*)(struct bitmask *)> NumaBitmaskPtr
Lowlevel bitmask type.
void ForEach(Bitmask< Class > const &bitmask, F &&f, bool value=true)
Invoke function for each bit in mask that matches value.
void ForEach(Bitmask< Class > const &bitmask, F &&f, bool value=true)
Invoke function for each bit in mask that matches value.
std::ostream & FormatBitmask(std::ostream &os, Bitmask< Class > const &mask, int min_bits)
Formats mask and inserts it to os.
std::ostream & FormatBitmask(std::ostream &os, bitmask const *mask, int min_bits)
Formats mask and inserts it to os.