New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use range-based for loops (C++11) when looping over map elements #10493
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK
src/addrman.cpp
Outdated
@@ -390,7 +390,7 @@ int CAddrMan::Check_() | |||
if (vRandom.size() != nTried + nNew) | |||
return -7; | |||
|
|||
for (std::map<int, CAddrInfo>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++) { | |||
for (auto& it : mapInfo) { | |||
int n = (*it).first; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be it.first
and it.second
.
@benma Thanks for reviewing! Fixed! :-) |
utACK cf02bb1a6c3c0077f1cf37f60e13d6cb71fd9187 |
Rebased! |
Overall comment: the loop variables should probably not keep the name |
@sipa Good point! Any suggestion on an appropriate generic variable name to use instead? Perhaps |
@practicalswift Perhaps |
@sipa Ah, yes, |
e1a81c7
to
5551e35
Compare
@sipa Fixed! :-) |
src/rpc/net.cpp
Outdated
@@ -567,11 +567,11 @@ UniValue listbanned(const JSONRPCRequest& request) | |||
g_connman->GetBanned(banMap); | |||
|
|||
UniValue bannedAddresses(UniValue::VARR); | |||
for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++) | |||
for (auto& entry : banMap) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are touching all those lines anyway, you can change them to const references for free. Something like the following diff should do:
diff --git a/src/addrdb.h b/src/addrdb.h
index c3d509b..e0a49c8 100644
--- a/src/addrdb.h
+++ b/src/addrdb.h
@@ -61,7 +61,7 @@ public:
banReason = BanReasonUnknown;
}
- std::string banReasonToString()
+ std::string banReasonToString() const
{
switch (banReason) {
case BanReasonNodeMisbehaving:
diff --git a/src/addrman.cpp b/src/addrman.cpp
index 0edf111..2f7c014 100644
--- a/src/addrman.cpp
+++ b/src/addrman.cpp
@@ -390,9 +390,9 @@ int CAddrMan::Check_()
if (vRandom.size() != nTried + nNew)
return -7;
- for (auto& entry : mapInfo) {
+ for (const auto& entry : mapInfo) {
int n = entry.first;
- CAddrInfo& info = entry.second;
+ const CAddrInfo& info = entry.second;
if (info.fInTried) {
if (!info.nLastSuccess)
return -1;
diff --git a/src/net.cpp b/src/net.cpp
index e6c5da8..c071fec 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -106,7 +106,7 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
int nBestReachability = -1;
{
LOCK(cs_mapLocalHost);
- for (auto& entry : mapLocalHost)
+ for (const auto& entry : mapLocalHost)
{
int nScore = entry.second.nScore;
int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
@@ -472,10 +472,10 @@ bool CConnman::IsBanned(CNetAddr ip)
bool fResult = false;
{
LOCK(cs_setBanned);
- for (auto& entry : setBanned)
+ for (const auto& entry : setBanned)
{
- CSubNet subNet = entry.first;
- CBanEntry banEntry = entry.second;
+ const CSubNet& subNet = entry.first;
+ const CBanEntry& banEntry = entry.second;
if(subNet.Match(ip) && GetTime() < banEntry.nBanUntil)
fResult = true;
diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp
index 5b75886..99c2475 100644
--- a/src/qt/transactiontablemodel.cpp
+++ b/src/qt/transactiontablemodel.cpp
@@ -82,7 +82,7 @@ public:
cachedWallet.clear();
{
LOCK2(cs_main, wallet->cs_wallet);
- for (auto& entry : wallet->mapWallet)
+ for (const auto& entry : wallet->mapWallet)
{
if (TransactionRecord::showTransaction(entry.second))
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, entry.second));
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
index 85df33a..345befd 100644
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -570,9 +570,9 @@ UniValue listbanned(const JSONRPCRequest& request)
g_connman->GetBanned(banMap);
UniValue bannedAddresses(UniValue::VARR);
- for (auto& entry : banMap)
+ for (const auto& entry : banMap)
{
- CBanEntry banEntry = entry.second;
+ const CBanEntry& banEntry = entry.second;
UniValue rec(UniValue::VOBJ);
rec.push_back(Pair("address", entry.first.ToString()));
rec.push_back(Pair("banned_until", banEntry.nBanUntil));
diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp
index db39ac6..6ee1f7d 100644
--- a/src/test/coins_tests.cpp
+++ b/src/test/coins_tests.cpp
@@ -88,7 +88,7 @@ public:
// Manually recompute the dynamic usage of the whole data, and compare it.
size_t ret = memusage::DynamicUsage(cacheCoins);
size_t count = 0;
- for (auto& entry : cacheCoins) {
+ for (const auto& entry : cacheCoins) {
ret += entry.second.coin.DynamicMemoryUsage();
++count;
}
@@ -182,7 +182,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
// Once every 1000 iterations and at the end, verify the full cache.
if (insecure_rand() % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
- for (auto& entry : result) {
+ for (const auto& entry : result) {
bool have = stack.back()->HaveCoin(entry.first);
const Coin& coin = stack.back()->AccessCoin(entry.first);
BOOST_CHECK(have == !coin.IsSpent());
@@ -413,7 +413,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
// Once every 1000 iterations and at the end, verify the full cache.
if (insecure_rand() % 1000 == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
- for (auto& entry : result) {
+ for (const auto& entry : result) {
bool have = stack.back()->HaveCoin(entry.first);
const Coin& coin = stack.back()->AccessCoin(entry.first);
BOOST_CHECK(have == !coin.IsSpent());
diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp
index a5143aa..e85732e 100644
--- a/src/wallet/rpcwallet.cpp
+++ b/src/wallet/rpcwallet.cpp
@@ -1263,7 +1263,7 @@ UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool fByA
if (fByAccounts)
{
- for (auto& entry : mapAccountTally)
+ for (const auto& entry : mapAccountTally)
{
CAmount nAmount = entry.second.nAmount;
int nConf = entry.second.nConf;
@MarcoFalke Good idea! You're suggestions are now part of the PR :-) |
Merge conflicts resolved! |
Needs rebase |
Rebased! |
utACK 875ebee20e048892d999c41acb7100d0fdac1f91 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there may be a few more possible const
s missing.
src/qt/bantablemodel.cpp
Outdated
@@ -55,11 +55,11 @@ class BanTablePriv | |||
#if QT_VERSION >= 0x040700 | |||
cachedBanlist.reserve(banMap.size()); | |||
#endif | |||
for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++) | |||
for (auto& entry : banMap) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be const
src/validation.cpp
Outdated
@@ -3120,8 +3120,8 @@ static uint64_t CalculateCurrentUsage() | |||
/* Prune a block file (modify associated database entries)*/ | |||
void PruneOneBlockFile(const int fileNumber) | |||
{ | |||
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) { | |||
CBlockIndex* pindex = it->second; | |||
for (auto& entry : mapBlockIndex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be const
(the loop modifies the entries pointed to by mapBlockIndex
, not the entries directly.
src/validation.cpp
Outdated
@@ -3570,8 +3570,8 @@ bool RewindBlockIndex(const CChainParams& params) | |||
// Reduce validity flag and have-data flags. | |||
// We do this after actual disconnecting, otherwise we'll end up writing the lack of data | |||
// to disk before writing the chainstate, resulting in a failure to continue if interrupted. | |||
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) { | |||
CBlockIndex* pindexIter = it->second; | |||
for (auto& entry : mapBlockIndex) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, can be const
.
55b5d9d
to
f41f744
Compare
@promag Yes, but with an additional condition ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestedACK 680bc2c. Looks good to me. There's a bunch of untouched for()'s with vector::iterator and set::iterator's that could presumably have similar refactors in a later commit.
@ajtowns Thanks for the review and the tested ACK! This PR is intentionally only touching |
Does this PR stand a chance of being merged? If not I'll close it :-) |
utACK 680bc2c |
1 similar comment
utACK 680bc2c |
Ready for merge? :-) |
… elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
7a5d181 Use the character based overload for std::string::find. (Alin Rus) c19401b Don't use pass by reference to const for cheaply-copied types (bool, char, etc.). (practicalswift) 4c5fe36 [Refactor] Remove unused fQuit var from checkqueue.h (donaloconnor) fda7a5f Cleanup: remove unneeded header includes (random-zebra) ac2476c Add test cases covering the relevant key length boundaries: 64 bytes +/- 1 byte for HMAC-SHA256 and 128 bytes +/- 1 byte for HMAC-SHA512 (practicalswift) a346262 Fix code constness in CBlockIndex::GetAncestor() overloads (Dan Raviv) 65e3f4e Refactor: More range-based for loops (random-zebra) dd3d3c4 Use range-based for loops (C++11) when looping over map elements (practicalswift) 5a7750a Move RPC registration out of AppInitParameterInteraction (Russell Yanofsky) 402b4c4 Use compile-time constants instead of unnamed enumerations (practicalswift) bbac2d0 [Trivial] Fix indentation in coins.cpp (random-zebra) e539c62 Small refactor of CCoinsViewCache::BatchWrite() (Dan Raviv) ec91759 Use MakeUnique<T>(...) instead of std::unique_ptr<T>(new T(...)) (random-zebra) 93487b1 Use unique_ptr for pcoinscatcher/pcoinsdbview/pcoinsTip/pblocktree (random-zebra) ff43d69 Use unique_ptr for pdbCopy (Db) and fix potential memory leak (practicalswift) b4d9641 Use unique_ptr for dbenv (DbEnv) (practicalswift) 36108b9 Use unique_ptr for pfilter (CBloomFilter) (practicalswift) ff1c454 Use unique_ptr for sem{Addnode,Outbound} (CSemaphore) (practicalswift) 93daf17 Use unique_ptr for httpRPCTimerInterface (HTTPRPCTimerInterface) (practicalswift) 020ac16 Init: Remove redundant exit(EXIT_FAILURE) instances and replace with (random-zebra) b9f5d1f Remove duplicate uriParts.size() > 0 check (practicalswift) 440d961 Remove redundant check (!ecc is always true) (practicalswift) bfd295b Remove redundant NULL checks after new (practicalswift) 97aad32 Make fUseCrypto atomic (MeshCollider) 2711f78 Consistent parameter names in txdb.h (MeshCollider) d40df3a Fix race for mapBlockIndex in AppInitMain (random-zebra) 03b7766 Cleanup: remove unused functions to Hash the concat of 4 or more objects (random-zebra) c520e0f Remove some unused functions and methods (Pieter Wuille) 508f1a1 range-based loops and const qualifications in net.cpp (Marko Bencun) 79b1e50 Refactor: implement CPubKey::data() (random-zebra) 614d26c Refactor: more &vec[0] to vec.data() (random-zebra) 02b6337 Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) c1c8b05 Ensure that data types are consistent (jjz) 732bb9d Fix potential null dereferences (MeshCollider) 80f35f9 Remove unreachable code (practicalswift) Pull request description: This is a collection of simple refactorings coming from upstream Bitcoin v0.16 (adapting/extending to PIVX-specific code where needed). Pull requests backported: - bitcoin#10845 (practicalswift) - bitcoin#11238 (MeshCollider) - bitcoin#11232 (jjz) - bitcoin#10793 (MeshCollider) - bitcoin#10888 (benma) - ~~bitcoin#11351 (danra)~~ [edit: removed - included in #2423] - bitcoin#11385 (sipa) - bitcoin#11107 (MeshCollider) - bitcoin#10898 (practicalswift) - bitcoin#11511 (donaloconnor) - bitcoin#11043 (practicalswift) - bitcoin#11353 (danra) - bitcoin#10749 (practicalswift) - bitcoin#11603 (ryanofsky) - bitcoin#10493 (practicalswift) - bitcoin#11337 (danra) - bitcoin#11516 (practicalswift) - bitcoin#10574 (practicalswift) - bitcoin#12108 (donaloconnor) - bitcoin#10839 (practicalswift) - bitcoin#12159 (kekimusmaximus) ACKs for top commit: Fuzzbawls: ACK 7a5d181 furszy: re-ACK 7a5d181 after rebase, no code changes. Merging.. Tree-SHA512: d92f5df47f443391a6531274a2efb9a4882c62d32eff628f795b3abce703f108c8b40ec80ac841cde6c5fdd5c9ff2b6056a31546ac2edda279f5f18fccc99c32
…t.cpp 05cae8a range-based loops and const qualifications in net.cpp (Marko Bencun) Pull request description: Plus a use of std::copy() instead of manual copying. (The loop on line 117 is already done in bitcoin#10493). Tree-SHA512: d9839e330c71bb9781a4efa81ee353c9e3fd8a93c2120a309f7a0e516b119dd7abe0f0988546797801258b867a29581978515c05dda9e5b23097e15f705139b4
…ver map elements 680bc2c Use range-based for loops (C++11) when looping over map elements (practicalswift) Pull request description: Before this commit: ```c++ for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) { T1 z = (*x).first; … } ``` After this commit: ```c++ for (auto& x : y) { T1 z = x.first; … } ``` Tree-SHA512: 954b136b7f5e6df09f39248a6b530fd9baa9ab59d7c2c7eb369fd4afbb591b7a52c92ee25f87f1745f47b41d6828b7abfd395b43daf84a55b4e6a3d45015e3a0
Before this commit:
After this commit: