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