Skip to content

Commit

Permalink
Remove [[assume]] in lieu of assert
Browse files Browse the repository at this point in the history
  • Loading branch information
rhalbersma committed Jun 18, 2024
1 parent 405d5ca commit dc6bde9
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 76 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,11 @@ This single-header library has no other dependencies than the C++ Standard Libra

| Platform | Compiler | Versions | Build |
| :------- | :------- | -------: | :---- |
| Linux | GCC | 14-trunk | CI currently being ported to GitHub Actions |
| Linux | Clang | 17, 18-trunk | CI currently being ported to GitHub Actions |
| Linux | GCC | 14, 15-trunk | CI currently being ported to GitHub Actions |
| Linux | Clang | 18, 19-trunk | CI currently being ported to GitHub Actions |
| Windows | Visual C++ | 17.7, 17.8, 17.9-trunk | CI currently being ported to GitHub Actions |

Note that this library makes liberal use of C++23 features, such as the `fold_left`, `shift_left` and `shift_right` algorithms, the `pairwise_transform`, `zip` and `zip_transform` views, the `to` range conversion, the `uz` literal for `size_t` and the `unreachable` utility. GCC 14-trunk, Clang 17 and 18-trunk (both only with the GCC 14-trunk standard library) and Visual C++ 17.7 and higher are supported at the moment. Also note that running the unit tests requires the presence of the [range-v3](https://github.com/ericniebler/range-v3) library (for the set algorithm views).
Note that this library makes liberal use of C++23 features, such as the `fold_left`, `shift_left` and `shift_right` algorithms, the `pairwise_transform`, `zip` and `zip_transform` views, the `to` range conversion, the `uz` literal for `size_t` and explicit object parameters (deducing `this`). GCC 14 and 15-trunk, Clang 18 and 19-trunk (both only with the GCC 14 or higher standard library) and Visual C++ 17.7 and higher are supported at the moment. Also note that running the unit tests requires the presence of the [range-v3](https://github.com/ericniebler/range-v3) library (for the set algorithm views).

## License

Expand Down
19 changes: 10 additions & 9 deletions include/ext/boost/dynamic_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <boost/dynamic_bitset.hpp> // dynamic_bitset
#include <algorithm> // lexicographical_compare_three_way
#include <cassert> // assert
#include <compare> // strong_ordering
#include <concepts> // constructible_from, unsigned_integral
#include <cstddef> // ptrdiff_t
Expand Down Expand Up @@ -43,27 +44,27 @@ class dynamic_bitset_iterator
m_ptr(ptr),
m_val(val)
{
[[assume(is_valid())]];
assert(is_valid());
}

[[nodiscard]] friend constexpr bool operator==(dynamic_bitset_iterator lhs, dynamic_bitset_iterator rhs) noexcept
{
[[assume(is_comparable(lhs, rhs))]];
assert(is_comparable(lhs, rhs));
return lhs.m_val == rhs.m_val;
}

[[nodiscard]] constexpr auto operator*() const noexcept
-> dynamic_bitset_reference<Block, Allocator>
{
[[assume(is_dereferenceable())]];
assert(is_dereferenceable());
return { *m_ptr, m_val };
}

dynamic_bitset_iterator& operator++() noexcept
{
[[assume(is_incrementable())]];
assert(is_incrementable());
m_val = m_ptr->find_next(m_val);
[[assume(is_decrementable())]];
assert(is_decrementable());
return *this;
}

Expand All @@ -74,9 +75,9 @@ class dynamic_bitset_iterator

dynamic_bitset_iterator& operator--() noexcept
{
[[assume(is_decrementable())]];
assert(is_decrementable());
m_val = find_prev(m_val);
[[assume(is_incrementable())]];
assert(is_incrementable());
return *this;
}

Expand All @@ -88,7 +89,7 @@ class dynamic_bitset_iterator
private:
[[nodiscard]] value_type find_prev(value_type n) noexcept
{
[[assume(m_ptr->any())]];
assert(m_ptr->any());
return *std::ranges::find_if(std::views::iota(0uz, std::ranges::min(n, m_ptr->size())) | std::views::reverse, [&](auto i) {
return (*m_ptr)[i];
});
Expand Down Expand Up @@ -136,7 +137,7 @@ class dynamic_bitset_reference
m_ref(ref),
m_val(val)
{
[[assume(is_valid())]];
assert(is_valid());
}

[[nodiscard]] constexpr dynamic_bitset_reference(const dynamic_bitset_reference&) noexcept = default;
Expand Down
19 changes: 10 additions & 9 deletions include/ext/std/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <algorithm> // lexicographical_compare_three_way
#include <bitset> // bitset
#include <cassert> // assert
#include <compare> // strong_ordering
#include <concepts> // constructible_from
#include <cstddef> // ptrdiff_t, size_t
Expand Down Expand Up @@ -86,12 +87,12 @@ class bitset_iterator
m_ptr(ptr),
m_val(val)
{
[[assume(is_valid())]];
assert(is_valid());
}

[[nodiscard]] friend constexpr bool operator==(bitset_iterator lhs, bitset_iterator rhs) noexcept
{
[[assume(is_comparable(lhs, rhs))]];
assert(is_comparable(lhs, rhs));
if constexpr (N == 0) {
return true;
} else {
Expand All @@ -102,15 +103,15 @@ class bitset_iterator
[[nodiscard]] constexpr auto operator*() const noexcept
-> bitset_reference<N>
{
[[assume(is_dereferenceable())]];
assert(is_dereferenceable());
return { *m_ptr, m_val };
}

constexpr bitset_iterator& operator++() noexcept
{
[[assume(is_incrementable())]];
assert(is_incrementable());
m_val = find_next(m_val);
[[assume(is_decrementable())]];
assert(is_decrementable());
return *this;
}

Expand All @@ -121,9 +122,9 @@ class bitset_iterator

constexpr bitset_iterator& operator--() noexcept
{
[[assume(is_decrementable())]];
assert(is_decrementable());
m_val = find_prev(m_val);
[[assume(is_incrementable())]];
assert(is_incrementable());
return *this;
}

Expand All @@ -146,7 +147,7 @@ class bitset_iterator

[[nodiscard]] constexpr value_type find_prev(value_type n) const noexcept
{
[[assume(m_ptr->any())]];
assert(m_ptr->any());
return *std::ranges::find_if(std::views::iota(0uz, n) | std::views::reverse, [&](auto i) {
return (*m_ptr)[i];
});
Expand Down Expand Up @@ -194,7 +195,7 @@ class bitset_reference
m_ref(ref),
m_val(val)
{
[[assume(is_valid())]];
assert(is_valid());
}

[[nodiscard]] constexpr bitset_reference(const bitset_reference&) noexcept = default;
Expand Down
49 changes: 25 additions & 24 deletions include/xstd/bit_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// all_of, any_of, fill_n, find_if, fold_left, for_each, max, none_of
#include <array> // array
#include <bit> // countl_zero, countr_zero, popcount
#include <cassert> // assert
#include <compare> // strong_ordering
#include <concepts> // constructible_from, integral, same_as, unsigned_integral
#include <cstddef> // ptrdiff_t, size_t
Expand Down Expand Up @@ -297,7 +298,7 @@ class bit_set
} else if constexpr (N > 0) {
m_bits.fill(ones);
}
[[assume(full())]];
assert(full());
}

constexpr void pop(key_type x) noexcept
Expand All @@ -308,7 +309,7 @@ class bit_set

constexpr iterator erase(const_iterator position) noexcept
{
[[assume(position != end())]];
assert(position != end());
pop(*position++);
return position;
}
Expand All @@ -323,7 +324,7 @@ class bit_set

constexpr iterator erase(const_iterator first, const_iterator last) noexcept
{
[[assume(N > 0 || first == last)]];
assert(N > 0 || first == last);
std::ranges::for_each(first, last, [this](auto x) {
pop(x);
});
Expand All @@ -342,7 +343,7 @@ class bit_set
if constexpr (N > 0) {
m_bits.fill(zero);
}
[[assume(empty())]];
assert(empty());
}

constexpr void complement(value_type x) noexcept
Expand Down Expand Up @@ -428,7 +429,7 @@ class bit_set

constexpr bit_set& operator<<=(size_type n [[maybe_unused]]) noexcept
{
[[assume(is_valid(n))]];
assert(is_valid(n));
if constexpr (num_blocks == 1) {
m_bits[0] >>= n;
} else if constexpr (num_blocks >= 2) {
Expand Down Expand Up @@ -465,7 +466,7 @@ class bit_set

constexpr bit_set& operator>>=(size_type n [[maybe_unused]]) noexcept
{
[[assume(is_valid(n))]];
assert(is_valid(n));
if constexpr (num_blocks == 1) {
m_bits[0] <<= n;
} else if constexpr (num_blocks >= 2) {
Expand Down Expand Up @@ -688,7 +689,7 @@ class bit_set
std::pair<block_type&, block_type>
>
{
[[assume(is_valid(n))]];
assert(is_valid(n));
auto const [ index, offset ] = index_offset(n);
return { self.m_bits[index], static_cast<block_type>(left_mask >> offset) };
}
Expand All @@ -702,13 +703,13 @@ class bit_set
static constexpr void insert(block_type& block, block_type mask) noexcept
{
block |= mask;
[[assume(contains(block, mask))]];
assert(contains(block, mask));
}

static constexpr void erase(block_type& block, block_type mask) noexcept
{
block &= static_cast<block_type>(~mask);
[[assume(!contains(block, mask))]];
assert(!contains(block, mask));
}

static constexpr void complement(block_type& block, block_type mask) noexcept
Expand Down Expand Up @@ -741,7 +742,7 @@ class bit_set
constexpr void do_insert(I first, S last) noexcept
requires std::constructible_from<value_type, decltype(*first)>
{
[[assume(N > 0 || first == last)]];
assert(N > 0 || first == last);
std::ranges::for_each(first, last, [this](auto x) {
add(x);
});
Expand All @@ -756,28 +757,28 @@ class bit_set

[[nodiscard]] constexpr size_type find_front() const noexcept
{
[[assume(!empty())]];
assert(!empty());
if constexpr (num_blocks == 1) {
return static_cast<size_type>(std::countl_zero(m_bits[0]));
} else if constexpr (num_blocks == 2) {
return m_bits[0] ? static_cast<size_type>(std::countl_zero(m_bits[0])) : block_size + static_cast<size_type>(std::countl_zero(m_bits[1]));
} else if constexpr (num_blocks >= 3) {
auto const front = std::ranges::find_if(m_bits, std::identity());
[[assume(front != std::ranges::end(m_bits))]];
assert(front != std::ranges::end(m_bits));
return static_cast<size_type>(std::ranges::distance(std::ranges::begin(m_bits), front)) * block_size + static_cast<size_type>(std::countl_zero(*front));
}
}

[[nodiscard]] constexpr size_type find_back() const noexcept
{
[[assume(!empty())]];
assert(!empty());
if constexpr (num_blocks == 1) {
return last_bit - static_cast<size_type>(std::countr_zero(m_bits[0]));
} else if constexpr (num_blocks == 2) {
return m_bits[1] ? last_bit - static_cast<size_type>(std::countr_zero(m_bits[1])) : left_bit - static_cast<size_type>(std::countr_zero(m_bits[0]));
} else if constexpr (num_blocks >= 3) {
auto const back = std::ranges::find_if(m_bits | std::views::reverse, std::identity());
[[assume(back != std::ranges::rend(m_bits))]];
assert(back != std::ranges::rend(m_bits));
return last_bit - static_cast<size_type>(std::ranges::distance(std::ranges::rbegin(m_bits), back)) * block_size - static_cast<size_type>(std::countr_zero(*back));
}
}
Expand Down Expand Up @@ -842,7 +843,7 @@ class bit_set

[[nodiscard]] constexpr size_type find_prev(size_type n) const noexcept
{
[[assume(!empty())]];
assert(!empty());
if constexpr (num_blocks == 1) {
return n - static_cast<size_type>(std::countr_zero(static_cast<block_type>(m_bits[0] >> (left_bit - n))));
} else if constexpr (num_blocks >= 2) {
Expand All @@ -856,7 +857,7 @@ class bit_set
}
auto const rg = m_bits | std::views::reverse | std::views::drop(last_block - index);
auto const prev = std::ranges::find_if(rg, std::identity());
[[assume(prev != std::ranges::end(rg))]];
assert(prev != std::ranges::end(rg));
return n - static_cast<size_type>(std::ranges::distance(std::ranges::begin(rg), prev)) * block_size - static_cast<size_type>(std::countr_zero(*prev));
}
}
Expand Down Expand Up @@ -884,7 +885,7 @@ class bit_set
m_ptr(ptr),
m_val(val)
{
[[assume(is_valid())]];
assert(is_valid());
}

[[nodiscard]] constexpr proxy_const_iterator(pimpl_type ptr, value_type val) noexcept
Expand All @@ -895,7 +896,7 @@ class bit_set

[[nodiscard]] friend constexpr bool operator==(proxy_const_iterator lhs [[maybe_unused]], proxy_const_iterator rhs [[maybe_unused]]) noexcept
{
[[assume(is_comparable(lhs, rhs))]];
assert(is_comparable(lhs, rhs));
if constexpr (N == 0) {
return true;
} else {
Expand All @@ -906,15 +907,15 @@ class bit_set
[[nodiscard]] constexpr auto operator*() const noexcept
-> proxy_const_reference
{
[[assume(is_dereferenceable())]];
assert(is_dereferenceable());
return { *m_ptr, m_val };
}

constexpr proxy_const_iterator& operator++() noexcept
{
[[assume(is_incrementable())]];
assert(is_incrementable());
m_val = m_ptr->find_next(m_val + 1);
[[assume(is_decrementable())]];
assert(is_decrementable());
return *this;
}

Expand All @@ -925,9 +926,9 @@ class bit_set

constexpr proxy_const_iterator& operator--() noexcept
{
[[assume(is_decrementable())]];
assert(is_decrementable());
m_val = m_ptr->find_prev(m_val - 1);
[[assume(is_incrementable())]];
assert(is_incrementable());
return *this;
}

Expand Down Expand Up @@ -979,7 +980,7 @@ class bit_set
m_ref(ref),
m_val(val)
{
[[assume(is_valid())]];
assert(is_valid());
}

[[nodiscard]] constexpr proxy_const_reference(const proxy_const_reference&) noexcept = default;
Expand Down
Loading

0 comments on commit dc6bde9

Please sign in to comment.