From 882422d4bcb5b369f2f142508e8319ec461374f2 Mon Sep 17 00:00:00 2001 From: Erik Zenker Date: Sun, 4 Jul 2021 22:08:34 +0200 Subject: [PATCH] feat: Improve index of compile time (#165) --- benchmark/components/index_of_benchmark.cpp | 107 +++++++++++++++++++- include/hsm/details/fill_dispatch_table.h | 2 + include/hsm/details/index_map.h | 34 +++++-- test/unit/stateidx_tests.cpp | 2 + 4 files changed, 135 insertions(+), 10 deletions(-) diff --git a/benchmark/components/index_of_benchmark.cpp b/benchmark/components/index_of_benchmark.cpp index d2fe44c..3cba31e 100644 --- a/benchmark/components/index_of_benchmark.cpp +++ b/benchmark/components/index_of_benchmark.cpp @@ -1,10 +1,111 @@ -#include - -#include "ComplexStateMachine.h" #include "hsm/details/index_map.h" using namespace hsm; +namespace { +class s1 { +}; +class s2 { +}; +class s3 { +}; +class s4 { +}; +class s5 { +}; +class s6 { +}; +class s7 { +}; +class s8 { +}; +class s9 { +}; +class s10 { +}; +class s11 { +}; +class s12 { +}; +class s13 { +}; +class s14 { +}; +class s15 { +}; +class s16 { +}; +class s17 { +}; +class s18 { +}; +class s19 { +}; +class s20 { +}; +class s21 { +}; +class s22 { +}; +class s23 { +}; +class s24 { +}; +class s25 { +}; +class s26 { +}; +class s27 { +}; +class s28 { +}; +class s29 { +}; +class s30 { +}; +class s31 { +}; +class s32 { +}; +class s33 { +}; +class s34 { +}; +class s35 { +}; +class s36 { +}; +class s37 { +}; +class s38 { +}; +class s39 { +}; +class s40 { +}; +class s41 { +}; +class s42 { +}; +class s43 { +}; +class s44 { +}; +class s45 { +}; +class s46 { +}; +class s47 { +}; +class s48 { +}; +class s49 { +}; +class s50 { +}; + +} + int main() { using namespace boost::hana; diff --git a/include/hsm/details/fill_dispatch_table.h b/include/hsm/details/fill_dispatch_table.h index 6e67e5e..2731534 100644 --- a/include/hsm/details/fill_dispatch_table.h +++ b/include/hsm/details/fill_dispatch_table.h @@ -75,6 +75,8 @@ constexpr auto addDispatchTableEntry( [=](auto& dispatchTable, auto&& transition2, bool internal) -> void { const auto defer = false; const auto valid = true; + + // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index) dispatchTable[fromIdx].push_front( { toIdx, history, defer, valid, internal, std::move(transition2) }); }, diff --git a/include/hsm/details/index_map.h b/include/hsm/details/index_map.h index 9c83843..ac28212 100644 --- a/include/hsm/details/index_map.h +++ b/include/hsm/details/index_map.h @@ -11,7 +11,31 @@ #include #include -//#include +namespace { +template class GetIdx; + +template class GetIdx> { + template + static constexpr auto find_idx(std::index_sequence seq) -> std::size_t + { + return seq.size() + ((std::is_same::value ? idx - seq.size() : 0) + ...); + } + + public: + static constexpr std::size_t value = find_idx(std::index_sequence_for {}); +}; + +template class GetIdx> { + template + static constexpr auto find_idx(std::index_sequence seq) -> std::size_t + { + return seq.size() + ((std::is_same::value ? idx - seq.size() : 0) + ...); + } + + public: + static constexpr std::size_t value = find_idx(std::index_sequence_for {}); +}; +} namespace hsm { @@ -19,13 +43,9 @@ namespace bh { using namespace boost::hana; } -template -constexpr auto index_of(Iterable const& iterable, Element const& element) +template constexpr auto index_of(Iterable const&, Element const&) { - return bh::apply( - [](auto size, auto dropped) { return size - dropped; }, - bh::size(iterable), - bh::size(bh::drop_while(iterable, bh::not_equal.to(element)))); + return GetIdx::value; } template constexpr auto make_index_map(Typeids typeids) diff --git a/test/unit/stateidx_tests.cpp b/test/unit/stateidx_tests.cpp index d90ec45..614d089 100644 --- a/test/unit/stateidx_tests.cpp +++ b/test/unit/stateidx_tests.cpp @@ -1,6 +1,8 @@ #include "hsm/details/fill_dispatch_table.h" +#include "hsm/details/index_map.h" #include "hsm/details/state.h" #include "hsm/details/transition_table.h" + #include "hsm/front/transition_tuple.h" #include