From c60b11f3da40cf5dd6ccbd02aa441ce33198aa73 Mon Sep 17 00:00:00 2001 From: koe Date: Mon, 19 Dec 2022 15:50:16 -0600 Subject: [PATCH] add compare_func() method so user-defined comparison functions are easier to use --- src/common/container_helpers.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/common/container_helpers.h b/src/common/container_helpers.h index 1865bd111..2c526bb4e 100644 --- a/src/common/container_helpers.h +++ b/src/common/container_helpers.h @@ -47,7 +47,14 @@ namespace tools { +/// convert a binary comparison function to a functor +template +inline auto compare_func(const ComparisonOpT &comparison_function) +{ + return [&comparison_function](const T &a, const T &b) -> bool { return comparison_function(a, b); }; +} /// 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 template > bool is_sorted_and_unique(const T &container, const ComparisonOpT &ComparisonOp = ComparisonOpT{}) { @@ -65,6 +72,13 @@ bool is_sorted_and_unique(const T &container, const ComparisonOpT &ComparisonOp return true; } +/// specialization for raw function pointers +template +bool is_sorted_and_unique(const T &container, + bool (*const ComparisonOpFunc)(const typename T::value_type &a, const typename T::value_type &b)) +{ + return is_sorted_and_unique(container, compare_func(ComparisonOpFunc)); +} /// convenience wrapper for checking if a mapped object is mapped to a key embedded in 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