diff options
author | koe <ukoe@protonmail.com> | 2022-12-18 10:18:46 -0600 |
---|---|---|
committer | koe <ukoe@protonmail.com> | 2022-12-18 10:18:46 -0600 |
commit | 73298734d64d68656aae2da687342761065ab4e2 (patch) | |
tree | 0e9cd6fc57809a687a9fc85f2e2f6bb981e4345a /src/common | |
parent | comment updates (diff) | |
download | monero-73298734d64d68656aae2da687342761065ab4e2.tar.xz |
adjust is_sorted_and_unique()
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/container_helpers.h | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/src/common/container_helpers.h b/src/common/container_helpers.h index 6ac94b51a..1865bd111 100644 --- a/src/common/container_helpers.h +++ b/src/common/container_helpers.h @@ -47,21 +47,20 @@ namespace tools { -/// use operator< to get operator== -/// WARNING: equality is not always implied by operator<, depending on implementation -struct equals_from_less final +/// test if a container is sorted and unique according to a comparison criteria (defaults to operator<) +template <typename T, typename ComparisonOpT = std::less<typename T::value_type>> +bool is_sorted_and_unique(const T &container, const ComparisonOpT &ComparisonOp = ComparisonOpT{}) { - template <typename T> - bool operator()(const T &a, const T &b) { return !(a < b) && !(b < a); } -}; -/// note: test for sorted and uniqueness using the same criteria (operator<) -template <typename T> -bool is_sorted_and_unique(const T& container) -{ - if (!std::is_sorted(container.begin(), container.end())) + if (!std::is_sorted(container.begin(), container.end(), ComparisonOp)) return false; - if (std::adjacent_find(container.begin(), container.end(), equals_from_less{}) != container.end()) + if (std::adjacent_find(container.begin(), + container.end(), + [&ComparisonOp](const typename T::value_type &a, const typename T::value_type &b) -> bool + { + return !ComparisonOp(a, b) && !ComparisonOp(b, a); + }) + != container.end()) return false; return true; |