Skip to content

Commit 24ced52

Browse files
committed
Merge bitcoin/bitcoin#28687: C++20 std::views::reverse
2925bd5 refactor: use c++20 std::views::reverse instead of reverse_iterator.h (stickies-v) Pull request description: C++20 introduces [`std::ranges::views::reverse`](https://en.cppreference.com/w/cpp/ranges/reverse_view), which allows us to drop our own `reverse_iterator.h` implementation and also makes it easier to chain views (even though I think we currently don't use this). ACKs for top commit: achow101: ACK 2925bd5 maflcko: ACK 2925bd5 🎷 Tree-SHA512: 567666ec44af5d1beb7a271836bcc89c4c577abc77f522fcc18bc6d4de516ae9b0df766d0bfa6dd217569e6878331c2aee1d9815620860375e3510dad7fed476
2 parents 27a770b + 2925bd5 commit 24ced52

File tree

9 files changed

+22
-66
lines changed

9 files changed

+22
-66
lines changed

contrib/devtools/copyright_header.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
'src/qt/bitcoinstrings.cpp',
2020
'src/chainparamsseeds.h',
2121
# other external copyrights:
22-
'src/reverse_iterator.h',
2322
'src/test/fuzz/FuzzedDataProvider.h',
2423
'src/tinyformat.h',
2524
'src/bench/nanobench.h',

src/Makefile.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ BITCOIN_CORE_H = \
257257
random.h \
258258
randomenv.h \
259259
rest.h \
260-
reverse_iterator.h \
261260
rpc/blockchain.h \
262261
rpc/client.h \
263262
rpc/mempool.h \

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <primitives/block.h>
3333
#include <primitives/transaction.h>
3434
#include <random.h>
35-
#include <reverse_iterator.h>
3635
#include <scheduler.h>
3736
#include <streams.h>
3837
#include <sync.h>
@@ -51,6 +50,7 @@
5150
#include <future>
5251
#include <memory>
5352
#include <optional>
53+
#include <ranges>
5454
#include <typeinfo>
5555
#include <utility>
5656

@@ -2259,7 +2259,7 @@ void PeerManagerImpl::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlock
22592259
for (auto& it : m_peer_map) {
22602260
Peer& peer = *it.second;
22612261
LOCK(peer.m_block_inv_mutex);
2262-
for (const uint256& hash : reverse_iterate(vHashes)) {
2262+
for (const uint256& hash : vHashes | std::views::reverse) {
22632263
peer.m_blocks_for_headers_relay.push_back(hash);
22642264
}
22652265
}
@@ -2958,7 +2958,7 @@ void PeerManagerImpl::HeadersDirectFetchBlocks(CNode& pfrom, const Peer& peer, c
29582958
} else {
29592959
std::vector<CInv> vGetData;
29602960
// Download as much as possible, from earliest to latest.
2961-
for (const CBlockIndex *pindex : reverse_iterate(vToFetch)) {
2961+
for (const CBlockIndex* pindex : vToFetch | std::views::reverse) {
29622962
if (nodestate->vBlocksInFlight.size() >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
29632963
// Can't download any more from this peer
29642964
break;

src/node/blockstorage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <primitives/block.h>
2121
#include <primitives/transaction.h>
2222
#include <random.h>
23-
#include <reverse_iterator.h>
2423
#include <serialize.h>
2524
#include <signet.h>
2625
#include <span.h>
@@ -38,6 +37,7 @@
3837
#include <validation.h>
3938

4039
#include <map>
40+
#include <ranges>
4141
#include <unordered_map>
4242

4343
namespace kernel {
@@ -579,7 +579,7 @@ const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
579579
{
580580
const MapCheckpoints& checkpoints = data.mapCheckpoints;
581581

582-
for (const MapCheckpoints::value_type& i : reverse_iterate(checkpoints)) {
582+
for (const MapCheckpoints::value_type& i : checkpoints | std::views::reverse) {
583583
const uint256& hash = i.second;
584584
const CBlockIndex* pindex = LookupBlockIndex(hash);
585585
if (pindex) {

src/reverse_iterator.h

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/test/fuzz/prevector.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <test/fuzz/FuzzedDataProvider.h>
6-
#include <test/fuzz/fuzz.h>
7-
85
#include <prevector.h>
9-
#include <vector>
10-
11-
#include <reverse_iterator.h>
126
#include <serialize.h>
137
#include <streams.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
1410

11+
#include <ranges>
12+
#include <vector>
1513
namespace {
1614

1715
template <unsigned int N, typename T>
@@ -47,15 +45,15 @@ class prevector_tester
4745
assert(v == real_vector[pos]);
4846
++pos;
4947
}
50-
for (const T& v : reverse_iterate(pre_vector)) {
48+
for (const T& v : pre_vector | std::views::reverse) {
5149
--pos;
5250
assert(v == real_vector[pos]);
5351
}
5452
for (const T& v : const_pre_vector) {
5553
assert(v == real_vector[pos]);
5654
++pos;
5755
}
58-
for (const T& v : reverse_iterate(const_pre_vector)) {
56+
for (const T& v : const_pre_vector | std::views::reverse) {
5957
--pos;
6058
assert(v == real_vector[pos]);
6159
}

src/test/prevector_tests.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <prevector.h>
6-
#include <vector>
7-
8-
#include <reverse_iterator.h>
96
#include <serialize.h>
107
#include <streams.h>
11-
128
#include <test/util/random.h>
139
#include <test/util/setup_common.h>
1410

1511
#include <boost/test/unit_test.hpp>
1612

13+
#include <ranges>
14+
#include <vector>
15+
1716
BOOST_FIXTURE_TEST_SUITE(prevector_tests, TestingSetup)
1817

1918
template<unsigned int N, typename T>
@@ -58,14 +57,14 @@ class prevector_tester {
5857
for (const T& v : pre_vector) {
5958
local_check(v == real_vector[pos++]);
6059
}
61-
for (const T& v : reverse_iterate(pre_vector)) {
62-
local_check(v == real_vector[--pos]);
60+
for (const T& v : pre_vector | std::views::reverse) {
61+
local_check(v == real_vector[--pos]);
6362
}
6463
for (const T& v : const_pre_vector) {
6564
local_check(v == real_vector[pos++]);
6665
}
67-
for (const T& v : reverse_iterate(const_pre_vector)) {
68-
local_check(v == real_vector[--pos]);
66+
for (const T& v : const_pre_vector | std::views::reverse) {
67+
local_check(v == real_vector[--pos]);
6968
}
7069
DataStream ss1{};
7170
DataStream ss2{};

src/txmempool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <policy/policy.h>
1616
#include <policy/settings.h>
1717
#include <random.h>
18-
#include <reverse_iterator.h>
1918
#include <tinyformat.h>
2019
#include <util/check.h>
2120
#include <util/feefrac.h>
@@ -31,6 +30,7 @@
3130
#include <cmath>
3231
#include <numeric>
3332
#include <optional>
33+
#include <ranges>
3434
#include <string_view>
3535
#include <utility>
3636

@@ -121,7 +121,7 @@ void CTxMemPool::UpdateTransactionsFromBlock(const std::vector<uint256>& vHashes
121121
// This maximizes the benefit of the descendant cache and guarantees that
122122
// CTxMemPoolEntry::m_children will be updated, an assumption made in
123123
// UpdateForDescendants.
124-
for (const uint256 &hash : reverse_iterate(vHashesToUpdate)) {
124+
for (const uint256& hash : vHashesToUpdate | std::views::reverse) {
125125
// calculate children from mapNextTx
126126
txiter it = mapTx.find(hash);
127127
if (it == mapTx.end()) {

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
#include <primitives/block.h>
4141
#include <primitives/transaction.h>
4242
#include <random.h>
43-
#include <reverse_iterator.h>
4443
#include <script/script.h>
4544
#include <script/sigcache.h>
4645
#include <signet.h>
@@ -70,6 +69,7 @@
7069
#include <deque>
7170
#include <numeric>
7271
#include <optional>
72+
#include <ranges>
7373
#include <string>
7474
#include <tuple>
7575
#include <utility>
@@ -3357,7 +3357,7 @@ bool Chainstate::ActivateBestChainStep(BlockValidationState& state, CBlockIndex*
33573357
nHeight = nTargetHeight;
33583358

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

0 commit comments

Comments
 (0)