Skip to content

Commit

Permalink
Use CTRX library for precondition checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-moeller committed Jun 18, 2023
1 parent e4334ae commit 64f324f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 33 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ endif ()
include(cmake/CPM.cmake)

CPMAddPackage("gh:TheLartians/PackageProject.cmake@1.11.0")
CPMAddPackage("gh:jan-moeller/ctrx@1.2.0")

#############################################################################################################
# Build configuration
Expand Down Expand Up @@ -86,6 +87,9 @@ target_include_directories(
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/${PROJECT_NAME}-${PROJECT_VERSION}>
)
target_link_libraries(${PROJECT_NAME} PUBLIC
ctrx::ctrx
)
set_target_properties(${PROJECT_NAME} PROPERTIES
LINKER_LANGUAGE CXX
CXX_STANDARD 20
Expand All @@ -104,5 +108,5 @@ packageProject(
INCLUDE_DESTINATION include/${PROJECT_NAME}-${PROJECT_VERSION}
VERSION_HEADER "${VERSION_HEADER_LOCATION}"
COMPATIBILITY SameMajorVersion
DEPENDENCIES ""
DEPENDENCIES "ctrx 1.2.0"
)
9 changes: 5 additions & 4 deletions include/structural/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
#ifndef STRUCTURAL_BITSET_HPP
#define STRUCTURAL_BITSET_HPP

#include <ctrx/contracts.hpp>

#include <algorithm>
#include <array>
#include <bit>
#include <numeric>
#include <ostream>

#include <cassert>
#include <climits>
#include <cstddef>
#include <cstdint>
Expand All @@ -52,8 +53,8 @@ constexpr std::size_t chunk_size = 1 + ((N - 1) / bits_per_chunk<N>);
template<std::size_t N>
constexpr auto chunk_index_for(bitset<N> const& bs, std::size_t pos) noexcept -> std::size_t
{
assert(pos < N);
assert(N > 0);
CTRX_PRECONDITION(pos < N);
CTRX_PRECONDITION(N > 0);
return bs.chunks.size() - pos / bits_per_chunk<N> - 1;
}

Expand Down Expand Up @@ -353,7 +354,7 @@ constexpr auto operator"" _bits() noexcept
for (std::size_t i = 0; i < str.size(); ++i)
{
char const c = str[str.size() - i - 1];
assert(c == '0' || c == '1' || c == '\'');
CTRX_ASSERT(c == '0' || c == '1' || c == '\'');
if (c == '1')
bs.set(i - offset);
if (c == '\'')
Expand Down
23 changes: 12 additions & 11 deletions include/structural/detail/static_hash_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
#include "structural/hash.hpp"
#include "structural/pair.hpp"

#include <ctrx/contracts.hpp>

#include <algorithm>
#include <array>
#include <functional>
#include <utility>

#include <cassert>
#include <cstddef>

namespace structural::detail
Expand Down Expand Up @@ -158,7 +159,7 @@ struct static_hash_table
template<typename... Ks>
constexpr auto allocate_node(Ks&&... values) -> size_type
{
assert(next_available_idx < Capacity);
CTRX_PRECONDITION(next_available_idx < Capacity);
size_type const idx = next_available_idx;
next_available_idx = nodes[idx].inactive.next_free_idx;
std::construct_at(&nodes[idx].active, std::forward<Ks>(values)...);
Expand All @@ -168,7 +169,7 @@ struct static_hash_table

constexpr void deallocate_node(size_type idx)
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
std::destroy_at(&nodes[idx].active);
nodes[idx].inactive = inactive_node_t{.next_free_idx = next_available_idx};
next_available_idx = idx;
Expand All @@ -177,12 +178,12 @@ struct static_hash_table

constexpr auto get_node(size_type idx) noexcept -> active_node_t&
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
return nodes[idx].active;
}
constexpr auto get_node(size_type idx) const noexcept -> active_node_t const&
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
return nodes[idx].active;
}

Expand Down Expand Up @@ -229,7 +230,7 @@ struct static_hash_table
template<typename... Args>
constexpr auto emplace(Args&&... args) -> structural::pair<iterator, bool>
{
assert(node_count < Capacity);
CTRX_PRECONDITION(node_count < Capacity);
auto const new_idx = allocate_node(std::forward<Args>(args)...);
auto const& new_node = get_node(new_idx).payload;
auto const h = hash_fn(new_node);
Expand Down Expand Up @@ -272,7 +273,7 @@ struct static_hash_table
for (; n != s_invalid_idx && get_node(n).next != node_idx; n = get_node(n).next)
{
}
assert(get_node(n).next == node_idx);
CTRX_ASSERT(get_node(n).next == node_idx);
get_node(n).next = get_node(get_node(n).next).next;
deallocate_node(node_idx);
return iterator{this, pos.idx, pos.node_idx};
Expand Down Expand Up @@ -364,9 +365,9 @@ struct static_hash_table_iterator

constexpr static_hash_table_iterator(static_hash_table_iterator<T, Capacity, Hash, Equal, false> const& rhs)
requires(Const)
: container(rhs.container)
, idx(rhs.idx)
, node_idx(rhs.node_idx){};
: container(rhs.container)
, idx(rhs.idx)
, node_idx(rhs.node_idx){};

constexpr static_hash_table_iterator(static_hash_table_iterator const&) = default;

Expand All @@ -375,7 +376,7 @@ struct static_hash_table_iterator

constexpr auto operator++() noexcept -> static_hash_table_iterator&
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
if (container->get_node(node_idx).next != s_invalid_idx)
{
node_idx = container->get_node(node_idx).next;
Expand Down
17 changes: 9 additions & 8 deletions include/structural/detail/static_red_black_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@

#include "structural/pair.hpp"

#include <ctrx/contracts.hpp>

#include <algorithm>
#include <array>
#include <functional>
#include <limits>
#include <memory>

#include <cassert>
#include <cstdint>

namespace structural::detail
Expand Down Expand Up @@ -148,12 +149,12 @@ struct static_red_black_tree

constexpr auto get_node(size_type idx) noexcept -> active_node_t&
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
return nodes[idx].active;
}
constexpr auto get_node(size_type idx) const noexcept -> active_node_t const&
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
return nodes[idx].active;
}
constexpr void set_left(size_type idx, size_type node) noexcept
Expand Down Expand Up @@ -251,7 +252,7 @@ struct static_red_black_tree
template<typename... Ks>
constexpr auto allocate_node(Ks&&... values) -> size_type
{
assert(next_available_idx < Capacity);
CTRX_PRECONDITION(next_available_idx < Capacity);
size_type const idx = next_available_idx;
next_available_idx = nodes[idx].inactive.next_free_idx;
std::construct_at(&nodes[idx].active, std::forward<Ks>(values)...);
Expand All @@ -260,7 +261,7 @@ struct static_red_black_tree
}
constexpr void deallocate_node(size_type idx)
{
assert(idx != s_invalid_idx);
CTRX_PRECONDITION(idx != s_invalid_idx);
std::destroy_at(&nodes[idx].active);
nodes[idx].inactive = inactive_node_t{.next_free_idx = next_available_idx};
next_available_idx = idx;
Expand Down Expand Up @@ -375,7 +376,7 @@ struct static_red_black_tree
idx = rotate_right(idx);
if (!cmp(get_node(idx).payload, value) && get_node(idx).right == s_invalid_idx)
{
assert(get_node(idx).left == s_invalid_idx);
CTRX_ASSERT(get_node(idx).left == s_invalid_idx);
*erased_idx = idx;
return s_invalid_idx;
}
Expand Down Expand Up @@ -582,8 +583,8 @@ struct static_red_black_tree_iterator

constexpr static_red_black_tree_iterator(static_red_black_tree_iterator<T, Capacity, Compare, false> const& rhs)
requires(Const)
: container(rhs.container)
, idx(rhs.idx){};
: container(rhs.container)
, idx(rhs.idx){};

constexpr static_red_black_tree_iterator(static_red_black_tree_iterator const&) = default;

Expand Down
8 changes: 5 additions & 3 deletions include/structural/named_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "structural/detail/to_underlying.hpp"
#include "structural/detail/trim.hpp"

#include <ctrx/contracts.hpp>

#include <ranges>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -61,7 +63,7 @@
STRUCTURAL_MAKE_NAMED_BITSET_IMPL_GEN_OP(|, enum_name, bitset_name) \
STRUCTURAL_MAKE_NAMED_BITSET_IMPL_GEN_OP(^, enum_name, bitset_name) \
\
constexpr auto to_string_view(enum_name e)->std::string_view \
constexpr auto to_string_view(enum_name e) -> std::string_view \
{ \
constexpr auto names = []() \
{ \
Expand All @@ -72,10 +74,10 @@
return names; \
}(); \
auto const idx = structural::detail::to_underlying(e); \
assert(std::size_t(idx) < names.size()); \
CTRX_ASSERT(std::size_t(idx) < names.size()); \
return names[idx]; \
} \
auto to_string(enum_name e)->std::string \
auto to_string(enum_name e) -> std::string \
{ \
return std::string{to_string_view(e)}; \
} // TODO: Make constexpr as soon as std lib supports constexpr std::string
Expand Down
13 changes: 7 additions & 6 deletions include/structural/static_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@

#include "structural/uninitialized_array.hpp"

#include <ctrx/contracts.hpp>

#include <algorithm>
#include <compare>

#include <cassert>
#include <cstddef>

namespace structural
Expand Down Expand Up @@ -152,7 +153,7 @@ struct static_vector
constexpr auto insert(const_iterator pos, const_reference value) -> iterator { return insert(pos, {value}); }
constexpr auto insert(const_iterator pos, value_type&& value) -> iterator
{
assert(size() < capacity());
CTRX_PRECONDITION(size() < capacity());
if (pos == end())
{
push_back(std::move(value));
Expand Down Expand Up @@ -210,7 +211,7 @@ struct static_vector
template<typename... Args>
constexpr auto emplace_back(Args&&... args) -> reference
{
assert(size() < capacity());
CTRX_PRECONDITION(size() < capacity());
array.construct_at(end(), std::forward<Args>(args)...);
++count;
return back();
Expand All @@ -223,9 +224,9 @@ struct static_vector
return std::ranges::equal(begin(), end(), other.begin(), other.end());
}
constexpr auto operator<=>(static_vector const& rhs) const -> decltype(auto)
requires(std::three_way_comparable_with<value_type const, value_type const>
|| (requires(value_type const e) { e < e; }
&& std::convertible_to<decltype(std::declval<value_type>() < std::declval<value_type>()), bool>))
requires(std::three_way_comparable_with<value_type const, value_type const> || (requires(value_type const e) {
e < e;
} && std::convertible_to<decltype(std::declval<value_type>() < std::declval<value_type>()), bool>))
{
return std::lexicographical_compare_three_way(
begin(),
Expand Down

0 comments on commit 64f324f

Please sign in to comment.