Skip to content

Commit

Permalink
[util] Use constexpr assert in BitSet
Browse files Browse the repository at this point in the history
  • Loading branch information
dergoegge committed Oct 15, 2024
1 parent 15563d3 commit 15d4e9a
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/util/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ class IntBitSet
/** Progress to the next 1 bit (only if != IteratorEnd). */
constexpr Iterator& operator++() noexcept
{
Assume(m_val != 0);
assert(m_val != 0);
m_val &= m_val - I{1U};
if (m_val != 0) m_pos = std::countr_zero(m_val);
return *this;
}
/** Get the current bit position (only if != IteratorEnd). */
constexpr unsigned operator*() const noexcept
{
Assume(m_val != 0);
assert(m_val != 0);
return m_pos;
}
};
Expand All @@ -136,39 +136,39 @@ class IntBitSet
/** Construct a bitset with the singleton i. */
static constexpr IntBitSet Singleton(unsigned i) noexcept
{
Assume(i < MAX_SIZE);
assert(i < MAX_SIZE);
return IntBitSet(I(1U) << i);
}
/** Construct a bitset with bits 0..count-1 (inclusive) set to 1. */
static constexpr IntBitSet Fill(unsigned count) noexcept
{
IntBitSet ret;
Assume(count <= MAX_SIZE);
assert(count <= MAX_SIZE);
if (count) ret.m_val = I(~I{0}) >> (MAX_SIZE - count);
return ret;
}
/** Set a bit to 1. */
constexpr void Set(unsigned pos) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
m_val |= I{1U} << pos;
}
/** Set a bit to the specified value. */
constexpr void Set(unsigned pos, bool val) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
m_val = (m_val & ~I(I{1U} << pos)) | (I(val) << pos);
}
/** Set a bit to 0. */
constexpr void Reset(unsigned pos) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
m_val &= ~I(I{1U} << pos);
}
/** Retrieve a bit at the given position. */
constexpr bool operator[](unsigned pos) const noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
return (m_val >> pos) & 1U;
}
/** Compute the number of 1 bits in the bitset. */
Expand All @@ -186,13 +186,13 @@ class IntBitSet
/** Find the first element (requires Any()). */
constexpr unsigned First() const noexcept
{
Assume(m_val != 0);
assert(m_val != 0);
return std::countr_zero(m_val);
}
/** Find the last element (requires Any()). */
constexpr unsigned Last() const noexcept
{
Assume(m_val != 0);
assert(m_val != 0);
return std::bit_width(m_val) - 1;
}
/** Set this object's bits to be the binary AND between respective bits from this and a. */
Expand Down Expand Up @@ -281,7 +281,7 @@ class MultiIntBitSet
/** Progress to the next 1 bit (only if != IteratorEnd). */
constexpr Iterator& operator++() noexcept
{
Assume(m_idx < N);
assert(m_idx < N);
m_val &= m_val - I{1U};
if (m_val == 0) {
while (true) {
Expand All @@ -301,7 +301,7 @@ class MultiIntBitSet
/** Get the current bit position (only if != IteratorEnd). */
constexpr unsigned operator*() const noexcept
{
Assume(m_idx < N);
assert(m_idx < N);
return m_pos;
}
};
Expand All @@ -316,13 +316,13 @@ class MultiIntBitSet
/** Set a bit to 1. */
void constexpr Set(unsigned pos) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
m_val[pos / LIMB_BITS] |= I{1U} << (pos % LIMB_BITS);
}
/** Set a bit to the specified value. */
void constexpr Set(unsigned pos, bool val) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
m_val[pos / LIMB_BITS] = (m_val[pos / LIMB_BITS] & ~I(I{1U} << (pos % LIMB_BITS))) |
(I{val} << (pos % LIMB_BITS));
}
Expand All @@ -341,27 +341,27 @@ class MultiIntBitSet
/** Set a bit to 0. */
void constexpr Reset(unsigned pos) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
m_val[pos / LIMB_BITS] &= ~I(I{1U} << (pos % LIMB_BITS));
}
/** Retrieve a bit at the given position. */
bool constexpr operator[](unsigned pos) const noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
return (m_val[pos / LIMB_BITS] >> (pos % LIMB_BITS)) & 1U;
}
/** Construct a bitset with the singleton pos. */
static constexpr MultiIntBitSet Singleton(unsigned pos) noexcept
{
Assume(pos < MAX_SIZE);
assert(pos < MAX_SIZE);
MultiIntBitSet ret;
ret.m_val[pos / LIMB_BITS] = I{1U} << (pos % LIMB_BITS);
return ret;
}
/** Construct a bitset with bits 0..count-1 (inclusive) set to 1. */
static constexpr MultiIntBitSet Fill(unsigned count) noexcept
{
Assume(count <= MAX_SIZE);
assert(count <= MAX_SIZE);
MultiIntBitSet ret;
if (count) {
unsigned i = 0;
Expand Down Expand Up @@ -402,7 +402,7 @@ class MultiIntBitSet
unsigned p = 0;
while (m_val[p] == 0) {
++p;
Assume(p < N);
assert(p < N);
}
return std::countr_zero(m_val[p]) + p * LIMB_BITS;
}
Expand All @@ -411,7 +411,7 @@ class MultiIntBitSet
{
unsigned p = N - 1;
while (m_val[p] == 0) {
Assume(p > 0);
assert(p > 0);
--p;
}
return std::bit_width(m_val[p]) - 1 + p * LIMB_BITS;
Expand Down

0 comments on commit 15d4e9a

Please sign in to comment.