From 8abbd1ba3a194a652a158fcb6f3a0e9a13e4c9c1 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Tue, 30 Jun 2026 12:03:19 +0100 Subject: [PATCH] chore: Use std::ranges where possible (#7634) --- src/libxrpl/tx/ApplyContext.cpp | 3 ++- src/libxrpl/tx/invariants/InvariantCheck.cpp | 7 +++--- .../tx/transactors/bridge/XChainBridge.cpp | 7 +++--- src/test/app/AmendmentTable_test.cpp | 2 +- src/test/app/Manifest_test.cpp | 7 +++--- src/test/app/OfferMPT_test.cpp | 14 +++++------ src/test/app/XChain_test.cpp | 5 ++-- .../beast/aged_associative_container_test.cpp | 20 ++++------------ .../beast/beast_io_latency_probe_test.cpp | 9 +++----- .../consensus/RCLCensorshipDetector_test.cpp | 4 ++-- src/test/csf/random.h | 2 +- src/test/peerfinder/Livecache_test.cpp | 14 +++++------ src/xrpld/app/ledger/detail/InboundLedger.cpp | 5 ++-- src/xrpld/consensus/LedgerTrie.h | 6 ++--- src/xrpld/consensus/Validations.h | 23 +++++++++---------- src/xrpld/peerfinder/detail/Handouts.h | 6 ++--- src/xrpld/peerfinder/detail/Logic.h | 18 +++++++-------- src/xrpld/rpc/detail/ServerHandler.cpp | 5 ++-- 18 files changed, 64 insertions(+), 93 deletions(-) diff --git a/src/libxrpl/tx/ApplyContext.cpp b/src/libxrpl/tx/ApplyContext.cpp index 93b0d101af..5e5ab90441 100644 --- a/src/libxrpl/tx/ApplyContext.cpp +++ b/src/libxrpl/tx/ApplyContext.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -114,7 +115,7 @@ ApplyContext::checkInvariantsHelper( tx, result, fee, *view_, journal)...}}; // NOLINT(bugprone-unchecked-optional-access) // call each check's finalizer to see that it passes - if (!std::all_of(finalizers.cbegin(), finalizers.cend(), [](auto const& b) { return b; })) + if (!std::ranges::all_of(finalizers, [](auto const& b) { return b; })) { JLOG(journal.fatal()) << "Transaction has failed one or more global invariants: " << to_string(tx.getJson(JsonOptions::Values::None)); diff --git a/src/libxrpl/tx/invariants/InvariantCheck.cpp b/src/libxrpl/tx/invariants/InvariantCheck.cpp index 231705efaf..308342da74 100644 --- a/src/libxrpl/tx/invariants/InvariantCheck.cpp +++ b/src/libxrpl/tx/invariants/InvariantCheck.cpp @@ -876,10 +876,9 @@ ValidPseudoAccounts::visitEntry(bool isDelete, SLE::const_ref before, SLE::const { std::vector const& fields = getPseudoAccountFields(); - auto const numFields = - std::count_if(fields.begin(), fields.end(), [&after](SField const* sf) -> bool { - return after->isFieldPresent(*sf); - }); + auto const numFields = std::ranges::count_if( + fields, + [&after](SField const* sf) -> bool { return after->isFieldPresent(*sf); }); if (numFields != 1) { std::stringstream error; diff --git a/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp b/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp index 1e9e0bfc61..9092ae4140 100644 --- a/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp +++ b/src/libxrpl/tx/transactors/bridge/XChainBridge.cpp @@ -38,6 +38,7 @@ #include #include +#include #include #include #include @@ -301,10 +302,8 @@ onNewAttestations( } auto const& claimSigningAccount = att->attestationSignerAccount; - if (auto i = std::find_if( - attestations.begin(), - attestations.end(), - [&](auto const& a) { return a.keyAccount == claimSigningAccount; }); + if (auto i = std::ranges::find_if( + attestations, [&](auto const& a) { return a.keyAccount == claimSigningAccount; }); i != attestations.end()) { // existing attestation diff --git a/src/test/app/AmendmentTable_test.cpp b/src/test/app/AmendmentTable_test.cpp index 7c2087bd4a..d420990cbc 100644 --- a/src/test/app/AmendmentTable_test.cpp +++ b/src/test/app/AmendmentTable_test.cpp @@ -140,7 +140,7 @@ private: combineArg(std::vector& dest, std::vector const& src, Args const&... args) { assert(dest.capacity() >= dest.size() + src.size()); - std::copy(src.begin(), src.end(), std::back_inserter(dest)); + std::ranges::copy(src, std::back_inserter(dest)); if constexpr (sizeof...(args) > 0) combineArg(dest, args...); } diff --git a/src/test/app/Manifest_test.cpp b/src/test/app/Manifest_test.cpp index 0cf1155cf5..50e8ab4a8d 100644 --- a/src/test/app/Manifest_test.cpp +++ b/src/test/app/Manifest_test.cpp @@ -290,10 +290,9 @@ public: if (inManifests.size() == loadedManifests.size()) { BEAST_EXPECT( - std::equal( - inManifests.begin(), - inManifests.end(), - loadedManifests.begin(), + std::ranges::equal( + inManifests, + loadedManifests, [](Manifest const* lhs, Manifest const* rhs) { return *lhs == *rhs; })); } else diff --git a/src/test/app/OfferMPT_test.cpp b/src/test/app/OfferMPT_test.cpp index ac50924ac2..9958515c5f 100644 --- a/src/test/app/OfferMPT_test.cpp +++ b/src/test/app/OfferMPT_test.cpp @@ -3728,10 +3728,9 @@ public: auto actorOffers = offersOnAccount(env, actor.acct); auto const offerCount = std::distance( actorOffers.begin(), - std::remove_if( - actorOffers.begin(), actorOffers.end(), [](SLE::const_pointer& offer) { - return (*offer)[sfTakerGets].signum() == 0; - })); + std::ranges::remove_if(actorOffers, [](SLE::const_pointer& offer) { + return (*offer)[sfTakerGets].signum() == 0; + }).begin()); BEAST_EXPECT(offerCount == actor.offers); env.require(Balance(actor.acct, actor.xrp)); @@ -3898,10 +3897,9 @@ public: auto actorOffers = offersOnAccount(env, actor.acct); auto const offerCount = std::distance( actorOffers.begin(), - std::remove_if( - actorOffers.begin(), actorOffers.end(), [](SLE::const_pointer& offer) { - return (*offer)[sfTakerGets].signum() == 0; - })); + std::ranges::remove_if(actorOffers, [](SLE::const_pointer& offer) { + return (*offer)[sfTakerGets].signum() == 0; + }).begin()); BEAST_EXPECT(offerCount == actor.offers); env.require(Balance(actor.acct, actor.xrp)); diff --git a/src/test/app/XChain_test.cpp b/src/test/app/XChain_test.cpp index 36c1a08144..437c329e02 100644 --- a/src/test/app/XChain_test.cpp +++ b/src/test/app/XChain_test.cpp @@ -306,9 +306,8 @@ struct BalanceTransfer [[nodiscard]] bool payeesReceived(STAmount const& reward) const { - return std::all_of(rewardAccounts.begin(), rewardAccounts.end(), [&](balance const& b) { - return b.diff() == reward; - }); + return std::ranges::all_of( + rewardAccounts, [&](balance const& b) { return b.diff() == reward; }); } bool diff --git a/src/test/beast/aged_associative_container_test.cpp b/src/test/beast/aged_associative_container_test.cpp index 2ac5fc5a33..413194491a 100644 --- a/src/test/beast/aged_associative_container_test.cpp +++ b/src/test/beast/aged_associative_container_test.cpp @@ -1296,13 +1296,7 @@ AgedAssociativeContainerTestBase::testChronological() typename Traits::template Cont<> c(v.begin(), v.end(), clock); - BEAST_EXPECT( - std::equal( - c.chronological.cbegin(), - c.chronological.cend(), - v.begin(), - v.end(), - EqualValue())); + BEAST_EXPECT(std::ranges::equal(c.chronological, v, EqualValue())); // Test touch() with a non-const iterator. for (auto iter(v.crbegin()); iter != v.crend(); ++iter) @@ -1336,13 +1330,7 @@ AgedAssociativeContainerTestBase::testChronological() c.touch(found); } - BEAST_EXPECT( - std::equal( - c.chronological.cbegin(), - c.chronological.cend(), - v.cbegin(), - v.cend(), - EqualValue())); + BEAST_EXPECT(std::ranges::equal(c.chronological, v, EqualValue())); { // Because touch (reverse_iterator pos) is not allowed, the following @@ -1407,8 +1395,8 @@ AgedAssociativeContainerTestBase::reverseFillAgedContainer(Container& c, Values clk.set(0); Values rev(values); - std::sort(rev.begin(), rev.end()); - std::reverse(rev.begin(), rev.end()); + std::ranges::sort(rev); + std::ranges::reverse(rev); for (auto& v : rev) { // Add values in reverse order so they are reversed chronologically. diff --git a/src/test/beast/beast_io_latency_probe_test.cpp b/src/test/beast/beast_io_latency_probe_test.cpp index b7e4980f05..9a93968dab 100644 --- a/src/test/beast/beast_io_latency_probe_test.cpp +++ b/src/test/beast/beast_io_latency_probe_test.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include // IWYU pragma: keep #include @@ -94,18 +95,14 @@ class io_latency_probe_test : public beast::unit_test::Suite, public beast::test auto getMax() { - return std::chrono::duration_cast( - *std::max_element(elapsedTimes.begin(), elapsedTimes.end())) - .count(); + return std::chrono::duration_cast(*std::ranges::max_element(elapsedTimes)).count(); } template auto getMin() { - return std::chrono::duration_cast( - *std::min_element(elapsedTimes.begin(), elapsedTimes.end())) - .count(); + return std::chrono::duration_cast(*std::ranges::min_element(elapsedTimes)).count(); } }; #endif diff --git a/src/test/consensus/RCLCensorshipDetector_test.cpp b/src/test/consensus/RCLCensorshipDetector_test.cpp index 32117e0089..722a34f937 100644 --- a/src/test/consensus/RCLCensorshipDetector_test.cpp +++ b/src/test/consensus/RCLCensorshipDetector_test.cpp @@ -31,12 +31,12 @@ class RCLCensorshipDetector_test : public beast::unit_test::Suite cdet.check(std::move(accepted), [&remove, &remain](auto id, auto seq) { // If the item is supposed to be removed from the censorship // detector internal tracker manually, do it now: - if (std::find(remove.begin(), remove.end(), id) != remove.end()) + if (std::ranges::find(remove, id) != remove.end()) return true; // If the item is supposed to still remain in the censorship // detector internal tracker; remove it from the vector. - auto it = std::find(remain.begin(), remain.end(), id); + auto it = std::ranges::find(remain, id); if (it != remain.end()) remain.erase(it); return false; diff --git a/src/test/csf/random.h b/src/test/csf/random.h index 30f98e37fe..c506397d88 100644 --- a/src/test/csf/random.h +++ b/src/test/csf/random.h @@ -44,7 +44,7 @@ std::vector sample(std::size_t size, RandomNumberDistribution dist, Generator& g) { std::vector res(size); - std::generate(res.begin(), res.end(), [&dist, &g]() { return dist(g); }); + std::ranges::generate(res, [&dist, &g]() { return dist(g); }); return res; } diff --git a/src/test/peerfinder/Livecache_test.cpp b/src/test/peerfinder/Livecache_test.cpp index 9dae410b5b..4f2d6e97e1 100644 --- a/src/test/peerfinder/Livecache_test.cpp +++ b/src/test/peerfinder/Livecache_test.cpp @@ -167,10 +167,9 @@ public: for (auto i = std::make_pair(0, c.hops.begin()); i.second != c.hops.end(); ++i.first, ++i.second) { - std::copy((*i.second).begin(), (*i.second).end(), std::back_inserter(before[i.first])); - std::copy( - (*i.second).begin(), (*i.second).end(), std::back_inserter(beforeSorted[i.first])); - std::sort(beforeSorted[i.first].begin(), beforeSorted[i.first].end(), cmpEp); + std::ranges::copy(*i.second, std::back_inserter(before[i.first])); + std::ranges::copy(*i.second, std::back_inserter(beforeSorted[i.first])); + std::ranges::sort(beforeSorted[i.first], cmpEp); } c.hops.shuffle(); @@ -180,10 +179,9 @@ public: for (auto i = std::make_pair(0, c.hops.begin()); i.second != c.hops.end(); ++i.first, ++i.second) { - std::copy((*i.second).begin(), (*i.second).end(), std::back_inserter(after[i.first])); - std::copy( - (*i.second).begin(), (*i.second).end(), std::back_inserter(afterSorted[i.first])); - std::sort(afterSorted[i.first].begin(), afterSorted[i.first].end(), cmpEp); + std::ranges::copy(*i.second, std::back_inserter(after[i.first])); + std::ranges::copy(*i.second, std::back_inserter(afterSorted[i.first])); + std::ranges::sort(afterSorted[i.first], cmpEp); } // each hop bucket should contain the same items diff --git a/src/xrpld/app/ledger/detail/InboundLedger.cpp b/src/xrpld/app/ledger/detail/InboundLedger.cpp index e4df126ee8..4affffd1c1 100644 --- a/src/xrpld/app/ledger/detail/InboundLedger.cpp +++ b/src/xrpld/app/ledger/detail/InboundLedger.cpp @@ -127,9 +127,8 @@ std::size_t InboundLedger::getPeerCount() const { auto const& peerIds = peerSet_->getPeerIds(); - return std::count_if(peerIds.begin(), peerIds.end(), [this](auto id) { - return (app_.getOverlay().findPeerByShortID(id) != nullptr); - }); + return std::ranges::count_if( + peerIds, [this](auto id) { return (app_.getOverlay().findPeerByShortID(id) != nullptr); }); } void diff --git a/src/xrpld/consensus/LedgerTrie.h b/src/xrpld/consensus/LedgerTrie.h index cd9662ff02..b11a69a641 100644 --- a/src/xrpld/consensus/LedgerTrie.h +++ b/src/xrpld/consensus/LedgerTrie.h @@ -204,10 +204,8 @@ struct Node void erase(Node const* child) { - auto it = std::find_if( - children.begin(), children.end(), [child](std::unique_ptr const& curr) { - return curr.get() == child; - }); + auto it = std::ranges::find_if( + children, [child](std::unique_ptr const& curr) { return curr.get() == child; }); XRPL_ASSERT(it != children.end(), "xrpl::Node::erase : valid input"); std::swap(*it, children.back()); children.pop_back(); diff --git a/src/xrpld/consensus/Validations.h b/src/xrpld/consensus/Validations.h index f109ae620b..d4da8a2887 100644 --- a/src/xrpld/consensus/Validations.h +++ b/src/xrpld/consensus/Validations.h @@ -817,16 +817,15 @@ public: if (!preferred) { // fall back to majority over acquiring ledgers - auto it = std::max_element( - acquiring_.begin(), acquiring_.end(), [](auto const& a, auto const& b) { - std::pair const& aKey = a.first; - typename hash_set::size_type const& aSize = a.second.size(); - std::pair const& bKey = b.first; - typename hash_set::size_type const& bSize = b.second.size(); - // order by number of trusted peers validating that ledger - // break ties with ledger ID - return std::tie(aSize, aKey.second) < std::tie(bSize, bKey.second); - }); + auto it = std::ranges::max_element(acquiring_, [](auto const& a, auto const& b) { + std::pair const& aKey = a.first; + typename hash_set::size_type const& aSize = a.second.size(); + std::pair const& bKey = b.first; + typename hash_set::size_type const& bSize = b.second.size(); + // order by number of trusted peers validating that ledger + // break ties with ledger ID + return std::tie(aSize, aKey.second) < std::tie(bSize, bKey.second); + }); if (it != acquiring_.end()) return it->first; return std::nullopt; @@ -896,7 +895,7 @@ public: return (preferred->first >= minSeq) ? preferred->second : lcl.id(); // Otherwise, rely on peer ledgers - auto it = std::max_element(peerCounts.begin(), peerCounts.end(), [](auto& a, auto& b) { + auto it = std::ranges::max_element(peerCounts, [](auto const& a, auto const& b) { // Prefer larger counts, then larger ids on ties // (max_element expects this to return true if a < b) return std::tie(a.second, a.first) < std::tie(b.second, b.first); @@ -932,7 +931,7 @@ public: } // Count parent ledgers as fallback - return std::count_if(lastLedger_.begin(), lastLedger_.end(), [&ledgerID](auto const& it) { + return std::ranges::count_if(lastLedger_, [&ledgerID](auto const& it) { auto const& curr = it.second; return curr.seq() > Seq{0} && curr[curr.seq() - Seq{1}] == ledgerID; }); diff --git a/src/xrpld/peerfinder/detail/Handouts.h b/src/xrpld/peerfinder/detail/Handouts.h index 19a367f8c1..7523197c40 100644 --- a/src/xrpld/peerfinder/detail/Handouts.h +++ b/src/xrpld/peerfinder/detail/Handouts.h @@ -143,7 +143,7 @@ RedirectHandouts::tryInsert(Endpoint const& ep) return false; // Make sure the address isn't already in our list - if (std::any_of(list_.begin(), list_.end(), [&ep](Endpoint const& other) { + if (std::ranges::any_of(list_, [&ep](Endpoint const& other) { // Ignore port for security reasons return other.address.address() == ep.address.address(); })) @@ -222,7 +222,7 @@ SlotHandouts::tryInsert(Endpoint const& ep) return false; // Make sure the address isn't already in our list - if (std::any_of(list_.begin(), list_.end(), [&ep](Endpoint const& other) { + if (std::ranges::any_of(list_, [&ep](Endpoint const& other) { // Ignore port for security reasons return other.address.address() == ep.address.address(); })) @@ -311,7 +311,7 @@ ConnectHandouts::tryInsert(beast::IP::Endpoint const& endpoint) return false; // Make sure the address isn't already in our list - if (std::any_of(list_.begin(), list_.end(), [&endpoint](beast::IP::Endpoint const& other) { + if (std::ranges::any_of(list_, [&endpoint](beast::IP::Endpoint const& other) { // Ignore port for security reasons return other.address() == endpoint.address(); })) diff --git a/src/xrpld/peerfinder/detail/Logic.h b/src/xrpld/peerfinder/detail/Logic.h index 55cce506ba..b74643f7a5 100644 --- a/src/xrpld/peerfinder/detail/Logic.h +++ b/src/xrpld/peerfinder/detail/Logic.h @@ -573,19 +573,17 @@ public: // build list of active slots std::vector activeSlots; activeSlots.reserve(slots.size()); - std::for_each( - slots.cbegin(), slots.cend(), [&activeSlots](Slots::value_type const& value) { - if (value.second->state() == Slot::State::Active) - activeSlots.emplace_back(value.second); - }); + std::ranges::for_each(slots, [&activeSlots](Slots::value_type const& value) { + if (value.second->state() == Slot::State::Active) + activeSlots.emplace_back(value.second); + }); std::shuffle(activeSlots.begin(), activeSlots.end(), defaultPrng()); // build target vector targets.reserve(activeSlots.size()); - std::for_each( - activeSlots.cbegin(), activeSlots.cend(), [&targets](SlotImp::ptr const& slot) { - targets.emplace_back(slot); - }); + std::ranges::for_each(activeSlots, [&targets](SlotImp::ptr const& slot) { + targets.emplace_back(slot); + }); } /* VFALCO NOTE @@ -987,7 +985,7 @@ public: { auto const& address(iter->first.address()); if (iter->second.when() <= now && squelches.find(address) == squelches.end() && - std::none_of(slots.cbegin(), slots.cend(), [address](Slots::value_type const& v) { + std::ranges::none_of(slots, [address](Slots::value_type const& v) { return address == v.first.address(); })) { diff --git a/src/xrpld/rpc/detail/ServerHandler.cpp b/src/xrpld/rpc/detail/ServerHandler.cpp index 5177c85738..768cbf0dc0 100644 --- a/src/xrpld/rpc/detail/ServerHandler.cpp +++ b/src/xrpld/rpc/detail/ServerHandler.cpp @@ -1179,9 +1179,8 @@ parsePorts(Config const& config, std::ostream& log) } else { - auto const count = std::count_if(result.cbegin(), result.cend(), [](Port const& p) { - return p.protocol.contains("peer"); - }); + auto const count = std::ranges::count_if( + result, [](Port const& p) { return p.protocol.contains("peer"); }); if (count > 1) {