From fb55337487e44f0ad091e52595dacabb150ac210 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 15 Nov 2022 23:31:21 +0100 Subject: entity: more metaprogramming --- src/entity.hpp | 25 ++++++++++++++++++++----- test/entity.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/entity.hpp b/src/entity.hpp index 3c32ea23..51698651 100644 --- a/src/entity.hpp +++ b/src/entity.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace floormat {} @@ -314,14 +315,28 @@ struct except_nth_<0, parameter_pack, X> { template using except_nth = typename except_nth_, T>::type; -template -struct sort_tuple_; - -template class Tuple, typename... Ts> -struct sort_tuple_> { +template +struct sort_tuple_ { + template + static consteval auto sort_indices(const Tuple& tuple, const std::index_sequence&) { + std::array indices = { Is..., }; + using KeyT = decltype(KeyFn(std::get<0>(tuple))); + std::array keys = { KeyFn(std::get(tuple))..., }; + std::sort(indices.begin(), indices.end(), [&](std::size_t a, std::size_t b) { return Comp(keys[a], keys[b]); }); + return indices; + } + template + struct helper { + template + static consteval auto do_sort(const Tuple& tuple, const std::index_sequence&) { + return std::make_tuple(std::get(tuple)...); + } + }; }; + + } // namespace detail template diff --git a/test/entity.cpp b/test/entity.cpp index 838d18cd..e1bbb055 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -64,10 +64,39 @@ static constexpr bool test_visitor() return true; } +namespace test_sorting { + +template +struct item { + static constexpr std::size_t size = I-1; + std::array data; + consteval item(const char(&str)[I]) { + std::copy(str, str+I, data.data()); + } + template + constexpr bool operator==(const item& o) const { return data == o.data; } +}; + +static constexpr void test() +{ + using namespace floormat::entities::detail; + constexpr auto tuple = std::make_tuple(item{"bb"}, item{"aaa"}, item{"cccc"}, item{"d"}); + constexpr auto size = std::tuple_size_v>; + constexpr auto key = [](const auto& x) constexpr { return StringView(x.data.data(), x.data.size()); }; + constexpr auto comp = [](auto a, auto b) constexpr { return a < b; }; + using Sort = sort_tuple_, key, comp>; + constexpr auto indices = Sort::sort_indices(tuple, std::make_index_sequence()); + constexpr auto tuple2 = Sort::helper::do_sort(tuple, std::make_index_sequence()); + static_assert(tuple2 == std::make_tuple(item{"aaa"}, item{"bb"}, item{"cccc"}, item{"d"})); +} + +} // namespace test_sorting + void test_app::test_entity() { static_assert(test_accessors()); static_assert(test_visitor()); + test_sorting::test(); } namespace type_tests { -- cgit v1.2.3