Skip to content

Commit

Permalink
refactor: use c++20 ranges instead of dumb wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
PastaPastaPasta committed Oct 6, 2023
1 parent d04fa75 commit 7c22ff8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/bls/bls_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <memory>
#include <utility>
#include <ranges>

template <typename T>
bool VerifyVectorHelper(Span<T> vec)
Expand Down Expand Up @@ -109,7 +110,7 @@ bool CBLSWorker::GenerateContributions(int quorumThreshold, Span<CBLSId> ids, BL
};
futures.emplace_back(workerPool.push(f));
}
return ranges::all_of(futures, [](auto& f){
return std::ranges::all_of(futures, [](auto& f){
return f.get();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ class CCoinJoin

static constexpr int CalculateAmountPriority(CAmount nInputAmount)
{
if (auto optDenom = ranges::find_if_opt(GetStandardDenominations(), [&nInputAmount](const auto& denom) {
if (auto optDenom = ranges_util::find_if_opt(GetStandardDenominations(), [&nInputAmount](const auto& denom) {
return nInputAmount == denom;
})) {
return (float)COIN / *optDenom * 10000;
Expand Down
2 changes: 1 addition & 1 deletion src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ CDeterministicMNCPtr CDeterministicMNList::GetValidMN(const uint256& proTxHash)

CDeterministicMNCPtr CDeterministicMNList::GetMNByOperatorKey(const CBLSPublicKey& pubKey) const
{
const auto it = ranges::find_if(mnMap,
const auto it = std::find_if(mnMap.begin(), mnMap.end(),
[&pubKey](const auto& p){return p.second->pdmnState->pubKeyOperator.Get() == pubKey;});
if (it == mnMap.end()) {
return nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/evo/deterministicmns.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,17 @@ class CDeterministicMNList

[[nodiscard]] size_t GetValidMNsCount() const
{
return ranges::count_if(mnMap, [this](const auto& p){ return IsMNValid(*p.second); });
return std::count_if(mnMap.begin(), mnMap.end(), [this](const auto& p){ return IsMNValid(*p.second); });
}

[[nodiscard]] size_t GetAllEvoCount() const
{
return ranges::count_if(mnMap, [this](const auto& p) { return p.second->nType == MnType::Evo; });
return std::count_if(mnMap.begin(), mnMap.end(), [this](const auto& p) { return p.second->nType == MnType::Evo; });
}

[[nodiscard]] size_t GetValidEvoCount() const
{
return ranges::count_if(mnMap, [this](const auto& p) { return p.second->nType == MnType::Evo && IsMNValid(*p.second); });
return std::count_if(mnMap.begin(), mnMap.end(), [this](const auto& p) { return p.second->nType == MnType::Evo && IsMNValid(*p.second); });
}

[[nodiscard]] size_t GetValidWeightedMNsCount() const
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ std::map<Consensus::LLMQType, QvvecSyncMode> GetEnabledQuorumVvecSyncEntries()
throw std::invalid_argument(strprintf("Invalid format in -llmq-qvvec-sync: %s", strEntry));
}

if (auto optLLMQParams = ranges::find_if_opt(Params().GetConsensus().llmqs,
if (auto optLLMQParams = ranges_util::find_if_opt(Params().GetConsensus().llmqs,
[&strLLMQType](const auto& params){return params.name == strLLMQType;})) {
llmqType = optLLMQParams->type;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/spork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ SporkValue CSporkManager::GetSporkValue(SporkId nSporkID) const
}


if (auto optSpork = ranges::find_if_opt(sporkDefs,
if (auto optSpork = ranges_util::find_if_opt(sporkDefs,
[&nSporkID](const auto& sporkDef){return sporkDef.sporkId == nSporkID;})) {
return optSpork->defaultValue;
} else {
Expand All @@ -283,7 +283,7 @@ SporkValue CSporkManager::GetSporkValue(SporkId nSporkID) const

SporkId CSporkManager::GetSporkIDByName(std::string_view strName)
{
if (auto optSpork = ranges::find_if_opt(sporkDefs,
if (auto optSpork = ranges_util::find_if_opt(sporkDefs,
[&strName](const auto& sporkDef){return sporkDef.name == strName;})) {
return optSpork->sporkId;
}
Expand Down
13 changes: 8 additions & 5 deletions src/util/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include <algorithm>
#include <optional>

//#if __cplusplus > 201703L // C++20 compiler
//namespace ranges = std::ranges;
//#else
#if __cplusplus > 201703L // C++20 compiler
#include <ranges>
namespace ranges = std::ranges;
#else

#define MK_RANGE(FUN) \
template <typename X, typename Z> \
Expand All @@ -28,7 +29,11 @@ namespace ranges {
MK_RANGE(any_of)
MK_RANGE(count_if)
MK_RANGE(find_if)
}

#endif // C++20 compiler

namespace ranges_util {
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 All @@ -37,8 +42,6 @@ namespace ranges {
}
return std::optional<std::decay_t<decltype(*it)>>{};
}

}

//#endif // C++20 compiler
#endif // BITCOIN_UTIL_RANGES_H

0 comments on commit 7c22ff8

Please sign in to comment.