aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkoe <ukoe@protonmail.com>2022-12-19 15:50:16 -0600
committerkoe <ukoe@protonmail.com>2022-12-19 15:50:16 -0600
commitc60b11f3da40cf5dd6ccbd02aa441ce33198aa73 (patch)
treeba2ed610aabbc215e840c9dd63d56b1c322d695e /src
parentadjust is_sorted_and_unique() (diff)
downloadmonero-c60b11f3da40cf5dd6ccbd02aa441ce33198aa73.tar.xz
add compare_func() method so user-defined comparison functions are easier to use
Diffstat (limited to 'src')
-rw-r--r--src/common/container_helpers.h14
1 files changed, 14 insertions, 0 deletions
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 <typename T, typename ComparisonOpT = bool(const T &a, const T &b)>
+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 <typename T, typename ComparisonOpT = std::less<typename T::value_type>>
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 <typename T>
+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<typename T::value_type>(ComparisonOpFunc));
+}
/// convenience wrapper for checking if a mapped object is mapped to a key embedded in that object
/// example: std::unorderd_map<rct::key, std::pair<rct::key, rct::xmr_amount>> where the map key is supposed to
/// reproduce the pair's rct::key; use the predicate to get the pair's rct::key element