Skip to content

Commit

Permalink
refactor: use c++20 std::views::reverse instead of reverse_iterator.h
Browse files Browse the repository at this point in the history
Use std::ranges::views::reverse instead of the implementation in
reverse_iterator.h, and remove it as it is no longer used.
  • Loading branch information
stickies-v committed Mar 11, 2024
1 parent 6b3ef25 commit a198735
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 64 deletions.
1 change: 0 additions & 1 deletion contrib/devtools/copyright_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
'src/qt/bitcoinstrings.cpp',
'src/chainparamsseeds.h',
# other external copyrights:
'src/reverse_iterator.h',
'src/test/fuzz/FuzzedDataProvider.h',
'src/tinyformat.h',
'src/bench/nanobench.h',
Expand Down
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ BITCOIN_CORE_H = \
random.h \
randomenv.h \
rest.h \
reverse_iterator.h \
rpc/blockchain.h \
rpc/client.h \
rpc/mempool.h \
Expand Down
6 changes: 3 additions & 3 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <random.h>
#include <reverse_iterator.h>
#include <scheduler.h>
#include <streams.h>
#include <sync.h>
Expand All @@ -50,6 +49,7 @@
#include <future>
#include <memory>
#include <optional>
#include <ranges>
#include <typeinfo>
#include <utility>

Expand Down Expand Up @@ -2125,7 +2125,7 @@ void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlock
for (auto& it : m_peer_map) {
Peer& peer = *it.second;
LOCK(peer.m_block_inv_mutex);
for (const uint256& hash : reverse_iterate(vHashes)) {
for (const uint256& hash : vHashes | std::views::reverse) {
peer.m_blocks_for_headers_relay.push_back(hash);
}
}
Expand Down Expand Up @@ -2811,7 +2811,7 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
} else {
std::vector<CInv> vGetData;
// Download as much as possible, from earliest to latest.
for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
for (const CBlockIndex *pindex : vToFetch | std::views::reverse) {
if (nodestate->vBlocksInFlight.size() >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
// Can't download any more from this peer
break;
Expand Down
4 changes: 2 additions & 2 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <pow.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <reverse_iterator.h>
#include <serialize.h>
#include <signet.h>
#include <span.h>
Expand All @@ -37,6 +36,7 @@
#include <validation.h>

#include <map>
#include <ranges>
#include <unordered_map>

namespace kernel {
Expand Down Expand Up @@ -576,7 +576,7 @@ const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
{
const MapCheckpoints& checkpoints = data.mapCheckpoints;

for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) {
for (const MapCheckpoints::value_type& i : checkpoints | std::views::reverse) {
const uint256& hash = i.second;
const CBlockIndex* pindex = LookupBlockIndex(hash);
if (pindex) {
Expand Down
39 changes: 0 additions & 39 deletions src/reverse_iterator.h

This file was deleted.

14 changes: 6 additions & 8 deletions src/test/fuzz/prevector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>

#include <prevector.h>
#include <vector>

#include <reverse_iterator.h>
#include <serialize.h>
#include <streams.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>

#include <ranges>
#include <vector>
namespace {

template <unsigned int N, typename T>
Expand Down Expand Up @@ -47,15 +45,15 @@ class prevector_tester
assert(v == real_vector[pos]);
++pos;
}
for (const T& v : reverse_iterate(pre_vector)) {
for (const T& v : pre_vector | std::views::reverse) {
--pos;
assert(v == real_vector[pos]);
}
for (const T& v : const_pre_vector) {
assert(v == real_vector[pos]);
++pos;
}
for (const T& v : reverse_iterate(const_pre_vector)) {
for (const T& v : const_pre_vector | std::views::reverse) {
--pos;
assert(v == real_vector[pos]);
}
Expand Down
11 changes: 5 additions & 6 deletions src/test/prevector_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <prevector.h>
#include <vector>

#include <reverse_iterator.h>
#include <serialize.h>
#include <streams.h>

#include <test/util/random.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>

#include <ranges>
#include <vector>

BOOST_FIXTURE_TEST_SUITE(prevector_tests, TestingSetup)

template<unsigned int N, typename T>
Expand Down Expand Up @@ -58,13 +57,13 @@ class prevector_tester {
for (const T& v : pre_vector) {
local_check(v == real_vector[pos++]);
}
for (const T& v : reverse_iterate(pre_vector)) {
for (const T& v : pre_vector | std::views::reverse) {
local_check(v == real_vector[--pos]);
}
for (const T& v : const_pre_vector) {
local_check(v == real_vector[pos++]);
}
for (const T& v : reverse_iterate(const_pre_vector)) {
for (const T& v : const_pre_vector | std::views::reverse) {
local_check(v == real_vector[--pos]);
}
DataStream ss1{};
Expand Down
4 changes: 2 additions & 2 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <policy/policy.h>
#include <policy/settings.h>
#include <random.h>
#include <reverse_iterator.h>
#include <util/check.h>
#include <util/moneystr.h>
#include <util/overflow.h>
Expand All @@ -28,6 +27,7 @@
#include <cmath>
#include <numeric>
#include <optional>
#include <ranges>
#include <string_view>
#include <utility>

Expand Down Expand Up @@ -118,7 +118,7 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256>& vHashes
// This maximizes the benefit of the descendant cache and guarantees that
// CTxMemPoolEntry::m_children will be updated, an assumption made in
// UpdateForDescendants.
for (const uint256 &hash : reverse_iterate(vHashesToUpdate)) {
for (const uint256 &hash : vHashesToUpdate | std::views::reverse) {
// calculate children from mapNextTx
txiter it = mapTx.find(hash);
if (it == mapTx.end()) {
Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <random.h>
#include <reverse_iterator.h>
#include <script/script.h>
#include <script/sigcache.h>
#include <signet.h>
Expand Down Expand Up @@ -71,6 +70,7 @@
#include <deque>
#include <numeric>
#include <optional>
#include <ranges>
#include <string>
#include <tuple>
#include <utility>
Expand Down Expand Up @@ -3146,7 +3146,7 @@ bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex*
nHeight = nTargetHeight;

// Connect new blocks.
for (CBlockIndex* pindexConnect : reverse_iterate(vpindexToConnect)) {
for (CBlockIndex* pindexConnect : vpindexToConnect | std::views::reverse) {
if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
if (state.IsInvalid()) {
// The block violates a consensus rule.
Expand Down

0 comments on commit a198735

Please sign in to comment.