Skip to content

Commit

Permalink
refactor: use more ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
PastaPastaPasta committed Aug 8, 2022
1 parent d47af2c commit 741fb50
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/evo/simplifiedmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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<CSimplifiedMNListEntry>& left, const std::unique_ptr<CSimplifiedMNListEntry>& right)
{
return *left == *right;
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sync.h>
#include <util/strencodings.h>
#include <util/system.h>
#include <util/ranges.h>

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
Expand Down Expand Up @@ -523,8 +524,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();
})) {
Expand Down
23 changes: 21 additions & 2 deletions src/util/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,39 @@
#define MK_RANGE(FUN) \
template <typename X, typename Z> \
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 <typename X, typename Z> \
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 <typename X, typename Y, typename Z> \
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 <typename X, typename Y, typename Z> \
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 <typename X, typename Y> \
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 <typename X, typename Y> \
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)
MK_RANGE(any_of)
MK_RANGE(count_if)
MK_RANGE(find_if)

MK_RANGE2(equal)

template <typename X, typename Z>
constexpr inline auto find_if_opt(const X& ds, const Z& fn) {
const auto it = ranges::find_if(ds, fn);
Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3911,7 +3911,7 @@ static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, c
{
CScript expect = CScript() << nHeight;
if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
!std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
!ranges::equal(expect, block.vtx[0]->vin[0].scriptSig)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-cb-height", "block height mismatch in coinbase");
}
}
Expand Down

0 comments on commit 741fb50

Please sign in to comment.