From f50b9e38037ea0c9b7c0d3d3daa3b39f21a38b32 Mon Sep 17 00:00:00 2001 From: koe Date: Mon, 26 Dec 2022 11:36:44 -0600 Subject: [PATCH] revisions --- src/common/container_helpers.h | 40 ++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/common/container_helpers.h b/src/common/container_helpers.h index 238f0ddad..5824c9c37 100644 --- a/src/common/container_helpers.h +++ b/src/common/container_helpers.h @@ -45,8 +45,15 @@ namespace tools { +/// convert an arbitrary function to functor +template +inline auto as_functor(F f) +{ + return [f = std::move(f)](auto&&... args) { return f(std::forward(args)...); }; +} /// convert a binary comparison function to a functor -template +/// note: for most use-cases 'const T&' will work because only non-trivial types need a user-defined comparison operation +template inline auto compare_func(ComparisonOpT comparison_op_func) { static_assert( @@ -62,7 +69,7 @@ inline auto compare_func(ComparisonOpT comparison_op_func) "invalid callable - expected callable in form bool(T, T)" ); - return [func = std::move(comparison_op_func)] (const T &a, const T &b) -> bool { return func(a, b); }; + return as_functor(std::move(comparison_op_func)); } /// test if a container is sorted and unique according to a comparison criteria (defaults to operator<) /// NOTE: ComparisonOpT must establish 'strict weak ordering' https://en.cppreference.com/w/cpp/named_req/Compare @@ -88,7 +95,7 @@ bool is_sorted_and_unique(const T &container, ComparisonOpT comparison_op = Comp if (std::adjacent_find(container.begin(), container.end(), - [comparison_op](const ValueT &a, const ValueT &b) -> bool + [comparison_op = std::move(comparison_op)](const ValueT &a, const ValueT &b) -> bool { return !comparison_op(a, b) && !comparison_op(b, a); }) @@ -100,31 +107,32 @@ bool is_sorted_and_unique(const T &container, ComparisonOpT comparison_op = Comp /// specialization for raw function pointers template bool is_sorted_and_unique(const T &container, - bool (*const comparison_op_func)(const typename T::value_type &a, const typename T::value_type &b)) + bool (*const comparison_op_func)(const typename T::value_type&, const typename T::value_type&)) { return is_sorted_and_unique(container, compare_func(comparison_op_func)); } -/// convenience wrapper for checking if a mapped object is mapped to a key embedded in that object +/// convenience wrapper for checking if the key to a mapped object is correct for that object /// example: std::unorderd_map> where the map key is supposed to -/// reproduce the pair's rct::key; use the predicate to get the pair's rct::key element +/// reproduce the pair's rct::key; use the predicate to check that relationship template -bool keys_match_internal_values(const std::unordered_map &map, PredT get_internal_key_func) +bool keys_match_internal_values(const std::unordered_map &map, PredT check_key_func) { static_assert( std::is_same< - std::remove_cv_t>, - std::remove_cv_t>>())) - >> + bool, + decltype( + check_key_func( + std::declval>(), + std::declval>() + ) + ) >::value, - "invalid callable - expected callable in form Key(Value)" + "invalid callable - expected callable in form bool(KeyT, ValueT)" ); for (const auto &map_element : map) { - if (!(map_element.first == get_internal_key_func(map_element.second))) + if (!check_key_func(map_element.first, map_element.second)) return false; } @@ -145,7 +153,7 @@ void for_all_in_map_erase_if(std::unordered_map &map_inout, PredT static_assert( std::is_same< bool, - decltype(predicate(std::declval>>())) + decltype(predicate(std::declval>())) >::value, "invalid callable - expected callable in form bool(Value)" );