Skip to content

Commit

Permalink
Use range-based for loops (C++11) when looping over map elements
Browse files Browse the repository at this point in the history
Before this commit:

  for (std::map<T1, T2>::iterator x = y.begin(); x != y.end(); ++x) {
  }

After this commit:

  for (auto& x : y) {
  }

Cherry-picked from: 953f620
  • Loading branch information
xanimo committed Jul 6, 2024
1 parent 962b020 commit a0d1994
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 78 deletions.
6 changes: 3 additions & 3 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ int CAddrMan::Check_()
if (vRandom.size() != nTried + nNew)
return -7;

for (std::map<int, CAddrInfo>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
int n = (*it).first;
CAddrInfo& info = (*it).second;
for (const auto& entry : mapInfo) {
int n = entry.first;
const CAddrInfo& info = entry.second;
if (info.fInTried) {
if (!info.nLastSuccess)
return -1;
Expand Down
20 changes: 10 additions & 10 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,18 +313,18 @@ class CAddrMan
s << nUBuckets;
std::map<int, int> mapUnkIds;
int nIds = 0;
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
mapUnkIds[(*it).first] = nIds;
const CAddrInfo &info = (*it).second;
for (const auto& entry : mapInfo) {
mapUnkIds[entry.first] = nIds;
const CAddrInfo &info = entry.second;
if (info.nRefCount) {
assert(nIds != nNew); // this means nNew was wrong, oh ow
s << info;
nIds++;
}
}
nIds = 0;
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) {
const CAddrInfo &info = (*it).second;
for (const auto& entry : mapInfo) {
const CAddrInfo &info = entry.second;
if (info.fInTried) {
assert(nIds != nTried); // this means nTried was wrong, oh ow
s << info;
Expand Down Expand Up @@ -437,13 +437,13 @@ class CAddrMan

// Prune new entries with refcount 0 (as a result of collisions).
int nLostUnk = 0;
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); ) {
if (it->second.fInTried == false && it->second.nRefCount == 0) {
std::map<int, CAddrInfo>::const_iterator itCopy = it++;
Delete(itCopy->first);
for (const auto& entry : mapInfo) {
if (entry.second.fInTried == false && entry.second.nRefCount == 0) {
std::pair<const int, CAddrInfo> itCopy = entry;
Delete(itCopy.first);
nLostUnk++;
} else {
it++;
continue;
}
}
if (nLost + nLostUnk > 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
int nBestReachability = -1;
{
LOCK(cs_mapLocalHost);
for (std::map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
for (const auto& entry : mapLocalHost)
{
int nScore = (*it).second.nScore;
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
int nScore = entry.second.nScore;
int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
{
addr = CService((*it).first, (*it).second.nPort);
addr = CService(entry.first, entry.second.nPort);
nBestReachability = nReachability;
nBestScore = nScore;
}
Expand Down
6 changes: 3 additions & 3 deletions src/qt/bantablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class BanTablePriv
cachedBanlist.clear();
cachedBanlist.reserve(banMap.size());

for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++)
for (const auto& entry : banMap)
{
CCombinedBan banEntry;
banEntry.subnet = (*it).first;
banEntry.banEntry = (*it).second;
banEntry.subnet = entry.first;
banEntry.banEntry = entry.second;
cachedBanlist.append(banEntry);
}

Expand Down
6 changes: 3 additions & 3 deletions src/qt/transactiontablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ class TransactionTablePriv
cachedWallet.clear();
{
LOCK2(cs_main, wallet->cs_wallet);
for(std::map<uint256, CWalletTx>::iterator it = wallet->mapWallet.begin(); it != wallet->mapWallet.end(); ++it)
for (const auto& entry : wallet->mapWallet)
{
if(TransactionRecord::showTransaction(it->second))
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, it->second));
if (TransactionRecord::showTransaction(entry.second))
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, entry.second));
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,14 @@ 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 (const auto& entry : banMap)
{
CBanEntry banEntry = (*it).second;
const CBanEntry& banEntry = entry.second;
UniValue rec(UniValue::VOBJ);
rec.pushKV("address", (*it).first.ToString());
rec.pushKV("address", entry.first.ToString());
rec.pushKV("banned_until", banEntry.nBanUntil);
rec.pushKV("ban_created", banEntry.nCreateTime);
rec.pushKV("ban_reason", banEntry.banReasonToString());
// rec.pushKV("ban_reason", banEntry.banReasonToString()); // TODO

bannedAddresses.push_back(rec);
}
Expand Down
4 changes: 2 additions & 2 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ template<typename Stream, typename K, typename T, typename Pred, typename A>
void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
{
WriteCompactSize(os, m.size());
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi)
Serialize(os, (*mi));
for (const auto& entry : m)
Serialize(os, entry);
}

template<typename Stream, typename K, typename T, typename Pred, typename A>
Expand Down
22 changes: 12 additions & 10 deletions src/test/coins_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ class CCoinsViewCacheTest : public CCoinsViewCache
{
// Manually recompute the dynamic usage of the whole data, and compare it.
size_t ret = memusage::DynamicUsage(cacheCoins);
for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) {
ret += it->second.coins.DynamicMemoryUsage();
size_t count = 0;
for (const auto& entry : cacheCoins) {
ret += entry.second.coins.DynamicMemoryUsage();
++count;
}
BOOST_CHECK_EQUAL(DynamicMemoryUsage(), ret);
}
Expand Down Expand Up @@ -154,13 +156,13 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)

// Once every 1000 iterations and at the end, verify the full cache.
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (std::map<uint256, CCoins>::iterator it = result.begin(); it != result.end(); it++) {
const CCoins* coins = stack.back()->AccessCoins(it->first);
for (const auto& entry : result) {
const CCoins* coins = stack.back()->AccessCoins(entry.first);
if (coins) {
BOOST_CHECK(*coins == it->second);
BOOST_CHECK(*coins == entry.second);
found_an_entry = true;
} else {
BOOST_CHECK(it->second.IsPruned());
BOOST_CHECK(entry.second.IsPruned());
missed_an_entry = true;
}
}
Expand Down Expand Up @@ -385,12 +387,12 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)

// Once every 1000 iterations and at the end, verify the full cache.
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (std::map<uint256, CCoins>::iterator it = result.begin(); it != result.end(); it++) {
const CCoins* coins = stack.back()->AccessCoins(it->first);
for (const auto& entry : result) {
const CCoins* coins = stack.back()->AccessCoins(entry.first);
if (coins) {
BOOST_CHECK(*coins == it->second);
BOOST_CHECK(*coins == entry.second);
} else {
BOOST_CHECK(it->second.IsPruned());
BOOST_CHECK(entry.second.IsPruned());
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3460,8 +3460,8 @@ void PruneOneBlockFile(const int fileNumber)
{
LOCK(cs_LastBlockFile);

for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
CBlockIndex* pindex = it->second;
for (const auto& entry : mapBlockIndex) {
CBlockIndex* pindex = entry.second;
if (pindex->nFile == fileNumber) {
pindex->nStatus &= ~BLOCK_HAVE_DATA;
pindex->nStatus &= ~BLOCK_HAVE_UNDO;
Expand Down Expand Up @@ -3897,8 +3897,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 (const auto& entry : mapBlockIndex) {
CBlockIndex* pindexIter = entry.second;

// Note: If we encounter an insufficiently validated block that
// is on chainActive, it must be because we are a pruning node, and
Expand Down Expand Up @@ -4158,8 +4158,8 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams)

// Build forward-pointing map of the entire block tree.
std::multimap<CBlockIndex*,CBlockIndex*> forward;
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
forward.insert(std::make_pair(it->second->pprev, it->second));
for (auto& entry : mapBlockIndex) {
forward.insert(std::make_pair(entry.second->pprev, entry.second));
}

assert(forward.size() == mapBlockIndex.size());
Expand Down
10 changes: 5 additions & 5 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1327,14 +1327,14 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)

if (fByAccounts)
{
for (map<string, tallyitem>::iterator it = mapAccountTally.begin(); it != mapAccountTally.end(); ++it)
for (const auto& entry : mapAccountTally)
{
CAmount nAmount = (*it).second.nAmount;
int nConf = (*it).second.nConf;
CAmount nAmount = entry.second.nAmount;
int nConf = entry.second.nConf;
UniValue obj(UniValue::VOBJ);
if((*it).second.fIsWatchonly)
if(entry.second.fIsWatchonly)
obj.pushKV("involvesWatchonly", true);
obj.pushKV("account", (*it).first);
obj.pushKV("account", entry.first);
obj.pushKV("amount", ValueFromAmount(nAmount));
obj.pushKV("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf));
ret.push_back(obj);
Expand Down
48 changes: 24 additions & 24 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,10 @@ DBErrors CWallet::ReorderTransactions()
typedef multimap<int64_t, TxPair > TxItems;
TxItems txByTime;

for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (auto& entry : mapWallet)
{
CWalletTx* wtx = &((*it).second);
txByTime.insert(make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
CWalletTx* wtx = &entry.second;
txByTime.insert(std::make_pair(wtx->nTimeReceived, TxPair(wtx, (CAccountingEntry*)0)));
}
list<CAccountingEntry> acentries;
walletdb.ListAccountCreditDebit("", acentries);
Expand Down Expand Up @@ -1905,9 +1905,9 @@ CAmount CWallet::GetBalance() const
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const CWalletTx* pcoin = &(*it).second;
const CWalletTx* pcoin = &entry.second;
if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableCredit();
}
Expand All @@ -1921,9 +1921,9 @@ CAmount CWallet::GetUnconfirmedBalance() const
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const CWalletTx* pcoin = &(*it).second;
const CWalletTx* pcoin = &entry.second;
if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
nTotal += pcoin->GetAvailableCredit();
}
Expand All @@ -1936,9 +1936,9 @@ CAmount CWallet::GetImmatureBalance() const
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const CWalletTx* pcoin = &(*it).second;
const CWalletTx* pcoin = &entry.second;
nTotal += pcoin->GetImmatureCredit();
}
}
Expand All @@ -1950,9 +1950,9 @@ CAmount CWallet::GetWatchOnlyBalance() const
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const CWalletTx* pcoin = &(*it).second;
const CWalletTx* pcoin = &entry.second;
if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableWatchOnlyCredit();
}
Expand All @@ -1966,9 +1966,9 @@ CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const CWalletTx* pcoin = &(*it).second;
const CWalletTx* pcoin = &entry.second;
if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
nTotal += pcoin->GetAvailableWatchOnlyCredit();
}
Expand All @@ -1981,9 +1981,9 @@ CAmount CWallet::GetImmatureWatchOnlyBalance() const
CAmount nTotal = 0;
{
LOCK2(cs_main, cs_wallet);
for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const CWalletTx* pcoin = &(*it).second;
const CWalletTx* pcoin = &entry.second;
nTotal += pcoin->GetImmatureWatchOnlyCredit();
}
}
Expand All @@ -1999,10 +1999,10 @@ void CWallet::AvailableCoins(vector<COutput> &vCoins, bool fOnlyConfirmed, const

CAmount nTotal = 0;

for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
for (const auto& entry : mapWallet)
{
const uint256& wtxid = it->first;
const CWalletTx* pcoin = &(*it).second;
const uint256& wtxid = entry.first;
const CWalletTx* pcoin = &entry.second;

if (!CheckFinalTx(*pcoin))
continue;
Expand Down Expand Up @@ -2060,10 +2060,10 @@ void CWallet::AvailableCoins(vector<COutput> &vCoins, bool fOnlyConfirmed, const
if (pcoin->tx->vout[i].nValue < nMinimumAmount || pcoin->tx->vout[i].nValue > nMaximumAmount)
continue;

if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(COutPoint((*it).first, i)))
if (coinControl && coinControl->HasSelected() && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(COutPoint(entry.first, i)))
continue;

if (IsLockedCoin((*it).first, i))
if (IsLockedCoin(entry.first, i))
continue;

if (IsSpent(wtxid, i))
Expand Down Expand Up @@ -3571,9 +3571,9 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c

// find first block that affects those keys, if there are any left
std::vector<CKeyID> vAffected;
for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
for (const auto& entry : mapWallet) {
// iterate over all wallet transactions...
const CWalletTx &wtx = (*it).second;
const CWalletTx &wtx = entry.second;
BlockMap::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) {
// ... which are already in a block
Expand All @@ -3593,8 +3593,8 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
}

// Extract block timestamps for those keys
for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
mapKeyBirth[it->first] = it->second->GetBlockTime() - 7200; // block times can be 2h off
for (const auto& entry : mapKeyFirstBlock)
mapKeyBirth[entry.first] = entry.second->GetBlockTime() - 7200; // block times can be 2h off
}

bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
Expand Down
8 changes: 4 additions & 4 deletions src/zmq/zmqnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;

for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
for (const auto& entry : factories)
{
std::string arg("-zmq" + i->first);
std::string arg("-zmq" + entry.first);
if (IsArgSet(arg))
{
CZMQNotifierFactory factory = i->second;
CZMQNotifierFactory factory = entry.second;
std::string address = GetArg(arg, "");
CZMQAbstractNotifier *notifier = factory();
notifier->SetType(i->first);
notifier->SetType(entry.first);
notifier->SetAddress(address);
notifiers.push_back(notifier);
}
Expand Down

0 comments on commit a0d1994

Please sign in to comment.