From f23439c6d5d60d467d1e1626c288e9f1f986c473 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 5 Aug 2022 14:27:51 -0500 Subject: [PATCH 1/2] refactor: use more ranges # Conflicts: # src/validation.cpp --- src/evo/simplifiedmns.cpp | 2 +- src/rpc/server.cpp | 4 ++-- src/util/ranges.h | 23 +++++++++++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index 41c3138c6652..255d176e8892 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -132,7 +132,7 @@ uint256 CSimplifiedMNList::CalcMerkleRoot(bool* pmutated) const bool CSimplifiedMNList::operator==(const CSimplifiedMNList& rhs) const { return mnList.size() == rhs.mnList.size() && - std::equal(mnList.begin(), mnList.end(), rhs.mnList.begin(), + ranges::equal(mnList, rhs.mnList, [](const std::unique_ptr& left, const std::unique_ptr& right) { return *left == *right; diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 7af6478c0f72..ebf5a7c13e83 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include @@ -595,8 +596,7 @@ static bool ExecuteCommand(const CRPCCommand& command, const JSONRPCRequest& req continue; } - if (std::equal(vecAllowedParam.begin(), vecAllowedParam.end(), - request.params.getValues().begin(), + if (ranges::equal(vecAllowedParam, request.params.getValues(), [](const UniValue& left, const UniValue& right) { return left.type() == right.type() && left.getValStr() == right.getValStr(); })) { diff --git a/src/util/ranges.h b/src/util/ranges.h index dca6866eea31..ae70ef2ecca3 100644 --- a/src/util/ranges.h +++ b/src/util/ranges.h @@ -15,13 +15,30 @@ #define MK_RANGE(FUN) \ template \ inline auto FUN(const X& ds, const Z& fn) { \ - return std::FUN(cbegin(ds), cend(ds), fn); \ + return std::FUN(std::cbegin(ds), std::cend(ds), fn); \ } \ template \ inline auto FUN(X& ds, const Z& fn) { \ - return std::FUN(begin(ds), end(ds), fn); \ + return std::FUN(std::begin(ds), std::end(ds), fn); \ } +#define MK_RANGE2(FUN) \ +template \ +inline auto FUN(const X& first, const Y& second, const Z& fn) { \ + return std::FUN(std::cbegin(first), std::cend(first), std::cbegin(second), std::cend(second), fn); \ +} \ +template \ +inline auto FUN(X& first, Y& second, const Z& fn) { \ + return std::FUN(std::begin(first), std::end(first), std::begin(second), std::end(second), fn); \ +} \ +template \ +inline auto FUN(const X& first, const Y& second) { \ + return std::FUN(std::cbegin(first), std::cend(first), std::cbegin(second), std::cend(second)); \ +} \ +template \ +inline auto FUN(X& first, Y& second) { \ + return std::FUN(std::begin(first), std::end(first), std::begin(second), std::end(second)); \ +} namespace ranges { MK_RANGE(all_of) @@ -29,6 +46,8 @@ namespace ranges { MK_RANGE(count_if) MK_RANGE(find_if) + MK_RANGE2(equal) + template constexpr inline auto find_if_opt(const X& ds, const Z& fn) { const auto it = ranges::find_if(ds, fn); From f03afb2dd59b736f963ffba8c7dfc0fa2ee30edf Mon Sep 17 00:00:00 2001 From: pasta Date: Thu, 5 Sep 2024 14:08:48 -0500 Subject: [PATCH 2/2] fmt: apply clang format --- src/evo/simplifiedmns.cpp | 9 ++---- src/util/ranges.h | 58 +++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/evo/simplifiedmns.cpp b/src/evo/simplifiedmns.cpp index 255d176e8892..c0e059a358c1 100644 --- a/src/evo/simplifiedmns.cpp +++ b/src/evo/simplifiedmns.cpp @@ -132,12 +132,9 @@ uint256 CSimplifiedMNList::CalcMerkleRoot(bool* pmutated) const bool CSimplifiedMNList::operator==(const CSimplifiedMNList& rhs) const { return mnList.size() == rhs.mnList.size() && - ranges::equal(mnList, rhs.mnList, - [](const std::unique_ptr& left, const std::unique_ptr& right) - { - return *left == *right; - } - ); + ranges::equal(mnList, rhs.mnList, + [](const std::unique_ptr& left, + const std::unique_ptr& right) { return *left == *right; }); } CSimplifiedMNListDiff::CSimplifiedMNListDiff() = default; diff --git a/src/util/ranges.h b/src/util/ranges.h index ae70ef2ecca3..19dd502b969a 100644 --- a/src/util/ranges.h +++ b/src/util/ranges.h @@ -12,33 +12,39 @@ //namespace ranges = std::ranges; //#else -#define MK_RANGE(FUN) \ -template \ -inline auto FUN(const X& ds, const Z& fn) { \ - return std::FUN(std::cbegin(ds), std::cend(ds), fn); \ -} \ -template \ -inline auto FUN(X& ds, const Z& fn) { \ - return std::FUN(std::begin(ds), std::end(ds), fn); \ -} +#define MK_RANGE(FUN) \ + template \ + inline auto FUN(const X& ds, const Z& fn) \ + { \ + return std::FUN(std::cbegin(ds), std::cend(ds), fn); \ + } \ + template \ + inline auto FUN(X& ds, const Z& fn) \ + { \ + return std::FUN(std::begin(ds), std::end(ds), fn); \ + } -#define MK_RANGE2(FUN) \ -template \ -inline auto FUN(const X& first, const Y& second, const Z& fn) { \ - return std::FUN(std::cbegin(first), std::cend(first), std::cbegin(second), std::cend(second), fn); \ -} \ -template \ -inline auto FUN(X& first, Y& second, const Z& fn) { \ - return std::FUN(std::begin(first), std::end(first), std::begin(second), std::end(second), fn); \ -} \ -template \ -inline auto FUN(const X& first, const Y& second) { \ - return std::FUN(std::cbegin(first), std::cend(first), std::cbegin(second), std::cend(second)); \ -} \ -template \ -inline auto FUN(X& first, Y& second) { \ - return std::FUN(std::begin(first), std::end(first), std::begin(second), std::end(second)); \ -} +#define MK_RANGE2(FUN) \ + template \ + inline auto FUN(const X& first, const Y& second, const Z& fn) \ + { \ + return std::FUN(std::cbegin(first), std::cend(first), std::cbegin(second), std::cend(second), fn); \ + } \ + template \ + inline auto FUN(X& first, Y& second, const Z& fn) \ + { \ + return std::FUN(std::begin(first), std::end(first), std::begin(second), std::end(second), fn); \ + } \ + template \ + inline auto FUN(const X& first, const Y& second) \ + { \ + return std::FUN(std::cbegin(first), std::cend(first), std::cbegin(second), std::cend(second)); \ + } \ + template \ + inline auto FUN(X& first, Y& second) \ + { \ + return std::FUN(std::begin(first), std::end(first), std::begin(second), std::end(second)); \ + } namespace ranges { MK_RANGE(all_of)