Use range-based for loops (C++11) when looping over map elements

Before this commit:

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

After this commit:

  for (auto& x : y) {
  }
This commit is contained in:
practicalswift 2017-06-04 22:02:43 +02:00
parent c63364610f
commit 680bc2cbb3
13 changed files with 74 additions and 74 deletions

View File

@ -390,9 +390,9 @@ int CAddrMan::Check_()
if (vRandom.size() != nTried + nNew) if (vRandom.size() != nTried + nNew)
return -7; return -7;
for (std::map<int, CAddrInfo>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++) { for (const auto& entry : mapInfo) {
int n = (*it).first; int n = entry.first;
CAddrInfo& info = (*it).second; const CAddrInfo& info = entry.second;
if (info.fInTried) { if (info.fInTried) {
if (!info.nLastSuccess) if (!info.nLastSuccess)
return -1; return -1;

View File

@ -313,9 +313,9 @@ public:
s << nUBuckets; s << nUBuckets;
std::map<int, int> mapUnkIds; std::map<int, int> mapUnkIds;
int nIds = 0; int nIds = 0;
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) { for (const auto& entry : mapInfo) {
mapUnkIds[(*it).first] = nIds; mapUnkIds[entry.first] = nIds;
const CAddrInfo &info = (*it).second; const CAddrInfo &info = entry.second;
if (info.nRefCount) { if (info.nRefCount) {
assert(nIds != nNew); // this means nNew was wrong, oh ow assert(nIds != nNew); // this means nNew was wrong, oh ow
s << info; s << info;
@ -323,8 +323,8 @@ public:
} }
} }
nIds = 0; nIds = 0;
for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++) { for (const auto& entry : mapInfo) {
const CAddrInfo &info = (*it).second; const CAddrInfo &info = entry.second;
if (info.fInTried) { if (info.fInTried) {
assert(nIds != nTried); // this means nTried was wrong, oh ow assert(nIds != nTried); // this means nTried was wrong, oh ow
s << info; s << info;

View File

@ -110,13 +110,13 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
int nBestReachability = -1; int nBestReachability = -1;
{ {
LOCK(cs_mapLocalHost); 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 nScore = entry.second.nScore;
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer); int nReachability = entry.first.GetReachabilityFrom(paddrPeer);
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
{ {
addr = CService((*it).first, (*it).second.nPort); addr = CService(entry.first, entry.second.nPort);
nBestReachability = nReachability; nBestReachability = nReachability;
nBestScore = nScore; nBestScore = nScore;
} }

View File

@ -55,11 +55,11 @@ public:
#if QT_VERSION >= 0x040700 #if QT_VERSION >= 0x040700
cachedBanlist.reserve(banMap.size()); cachedBanlist.reserve(banMap.size());
#endif #endif
for (banmap_t::iterator it = banMap.begin(); it != banMap.end(); it++) for (const auto& entry : banMap)
{ {
CCombinedBan banEntry; CCombinedBan banEntry;
banEntry.subnet = (*it).first; banEntry.subnet = entry.first;
banEntry.banEntry = (*it).second; banEntry.banEntry = entry.second;
cachedBanlist.append(banEntry); cachedBanlist.append(banEntry);
} }

View File

@ -80,10 +80,10 @@ public:
cachedWallet.clear(); cachedWallet.clear();
{ {
LOCK2(cs_main, wallet->cs_wallet); 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)) if (TransactionRecord::showTransaction(entry.second))
cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, it->second)); cachedWallet.append(TransactionRecord::decomposeTransaction(wallet, entry.second));
} }
} }
} }

View File

@ -569,11 +569,11 @@ UniValue listbanned(const JSONRPCRequest& request)
g_connman->GetBanned(banMap); g_connman->GetBanned(banMap);
UniValue bannedAddresses(UniValue::VARR); 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); UniValue rec(UniValue::VOBJ);
rec.push_back(Pair("address", (*it).first.ToString())); rec.push_back(Pair("address", entry.first.ToString()));
rec.push_back(Pair("banned_until", banEntry.nBanUntil)); rec.push_back(Pair("banned_until", banEntry.nBanUntil));
rec.push_back(Pair("ban_created", banEntry.nCreateTime)); rec.push_back(Pair("ban_created", banEntry.nCreateTime));
rec.push_back(Pair("ban_reason", banEntry.banReasonToString())); rec.push_back(Pair("ban_reason", banEntry.banReasonToString()));

View File

@ -160,8 +160,8 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
std::set<rpcfn_type> setDone; std::set<rpcfn_type> setDone;
std::vector<std::pair<std::string, const CRPCCommand*> > vCommands; std::vector<std::pair<std::string, const CRPCCommand*> > vCommands;
for (std::map<std::string, const CRPCCommand*>::const_iterator mi = mapCommands.begin(); mi != mapCommands.end(); ++mi) for (const auto& entry : mapCommands)
vCommands.push_back(make_pair(mi->second->category + mi->first, mi->second)); vCommands.push_back(make_pair(entry.second->category + entry.first, entry.second));
sort(vCommands.begin(), vCommands.end()); sort(vCommands.begin(), vCommands.end());
JSONRPCRequest jreq(helpreq); JSONRPCRequest jreq(helpreq);

View File

@ -732,8 +732,8 @@ template<typename Stream, typename K, typename T, typename Pred, typename A>
void Serialize(Stream& os, const std::map<K, T, Pred, A>& m) void Serialize(Stream& os, const std::map<K, T, Pred, A>& m)
{ {
WriteCompactSize(os, m.size()); WriteCompactSize(os, m.size());
for (typename std::map<K, T, Pred, A>::const_iterator mi = m.begin(); mi != m.end(); ++mi) for (const auto& entry : m)
Serialize(os, (*mi)); Serialize(os, entry);
} }
template<typename Stream, typename K, typename T, typename Pred, typename A> template<typename Stream, typename K, typename T, typename Pred, typename A>

View File

@ -81,8 +81,8 @@ public:
// Manually recompute the dynamic usage of the whole data, and compare it. // Manually recompute the dynamic usage of the whole data, and compare it.
size_t ret = memusage::DynamicUsage(cacheCoins); size_t ret = memusage::DynamicUsage(cacheCoins);
size_t count = 0; size_t count = 0;
for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) { for (const auto& entry : cacheCoins) {
ret += it->second.coin.DynamicMemoryUsage(); ret += entry.second.coin.DynamicMemoryUsage();
++count; ++count;
} }
BOOST_CHECK_EQUAL(GetCacheSize(), count); BOOST_CHECK_EQUAL(GetCacheSize(), count);
@ -189,15 +189,15 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
// Once every 1000 iterations and at the end, verify the full cache. // Once every 1000 iterations and at the end, verify the full cache.
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) { if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (auto it = result.begin(); it != result.end(); it++) { for (const auto& entry : result) {
bool have = stack.back()->HaveCoin(it->first); bool have = stack.back()->HaveCoin(entry.first);
const Coin& coin = stack.back()->AccessCoin(it->first); const Coin& coin = stack.back()->AccessCoin(entry.first);
BOOST_CHECK(have == !coin.IsSpent()); BOOST_CHECK(have == !coin.IsSpent());
BOOST_CHECK(coin == it->second); BOOST_CHECK(coin == entry.second);
if (coin.IsSpent()) { if (coin.IsSpent()) {
missed_an_entry = true; missed_an_entry = true;
} else { } else {
BOOST_CHECK(stack.back()->HaveCoinInCache(it->first)); BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
found_an_entry = true; found_an_entry = true;
} }
} }
@ -420,11 +420,11 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
// Once every 1000 iterations and at the end, verify the full cache. // Once every 1000 iterations and at the end, verify the full cache.
if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) { if (InsecureRandRange(1000) == 1 || i == NUM_SIMULATION_ITERATIONS - 1) {
for (auto it = result.begin(); it != result.end(); it++) { for (const auto& entry : result) {
bool have = stack.back()->HaveCoin(it->first); bool have = stack.back()->HaveCoin(entry.first);
const Coin& coin = stack.back()->AccessCoin(it->first); const Coin& coin = stack.back()->AccessCoin(entry.first);
BOOST_CHECK(have == !coin.IsSpent()); BOOST_CHECK(have == !coin.IsSpent());
BOOST_CHECK(coin == it->second); BOOST_CHECK(coin == entry.second);
} }
} }

View File

@ -3249,8 +3249,8 @@ void PruneOneBlockFile(const int fileNumber)
{ {
LOCK(cs_LastBlockFile); LOCK(cs_LastBlockFile);
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) { for (const auto& entry : mapBlockIndex) {
CBlockIndex* pindex = it->second; CBlockIndex* pindex = entry.second;
if (pindex->nFile == fileNumber) { if (pindex->nFile == fileNumber) {
pindex->nStatus &= ~BLOCK_HAVE_DATA; pindex->nStatus &= ~BLOCK_HAVE_DATA;
pindex->nStatus &= ~BLOCK_HAVE_UNDO; pindex->nStatus &= ~BLOCK_HAVE_UNDO;
@ -3802,8 +3802,8 @@ bool RewindBlockIndex(const CChainParams& params)
// Reduce validity flag and have-data flags. // Reduce validity flag and have-data flags.
// We do this after actual disconnecting, otherwise we'll end up writing the lack of data // 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. // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) { for (const auto& entry : mapBlockIndex) {
CBlockIndex* pindexIter = it->second; CBlockIndex* pindexIter = entry.second;
// Note: If we encounter an insufficiently validated block that // Note: If we encounter an insufficiently validated block that
// is on chainActive, it must be because we are a pruning node, and // is on chainActive, it must be because we are a pruning node, and
@ -4078,8 +4078,8 @@ void static CheckBlockIndex(const Consensus::Params& consensusParams)
// Build forward-pointing map of the entire block tree. // Build forward-pointing map of the entire block tree.
std::multimap<CBlockIndex*,CBlockIndex*> forward; std::multimap<CBlockIndex*,CBlockIndex*> forward;
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) { for (auto& entry : mapBlockIndex) {
forward.insert(std::make_pair(it->second->pprev, it->second)); forward.insert(std::make_pair(entry.second->pprev, entry.second));
} }
assert(forward.size() == mapBlockIndex.size()); assert(forward.size() == mapBlockIndex.size());

View File

@ -1398,14 +1398,14 @@ UniValue ListReceived(CWallet * const pwallet, const UniValue& params, bool fByA
if (fByAccounts) if (fByAccounts)
{ {
for (std::map<std::string, tallyitem>::iterator it = mapAccountTally.begin(); it != mapAccountTally.end(); ++it) for (const auto& entry : mapAccountTally)
{ {
CAmount nAmount = (*it).second.nAmount; CAmount nAmount = entry.second.nAmount;
int nConf = (*it).second.nConf; int nConf = entry.second.nConf;
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
if((*it).second.fIsWatchonly) if (entry.second.fIsWatchonly)
obj.push_back(Pair("involvesWatchonly", true)); obj.push_back(Pair("involvesWatchonly", true));
obj.push_back(Pair("account", (*it).first)); obj.push_back(Pair("account", entry.first));
obj.push_back(Pair("amount", ValueFromAmount(nAmount))); obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf))); obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
ret.push_back(obj); ret.push_back(obj);

View File

@ -701,9 +701,9 @@ DBErrors CWallet::ReorderTransactions()
typedef std::multimap<int64_t, TxPair > TxItems; typedef std::multimap<int64_t, TxPair > TxItems;
TxItems txByTime; TxItems txByTime;
for (std::map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) for (auto& entry : mapWallet)
{ {
CWalletTx* wtx = &((*it).second); CWalletTx* wtx = &entry.second;
txByTime.insert(std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr))); txByTime.insert(std::make_pair(wtx->nTimeReceived, TxPair(wtx, nullptr)));
} }
std::list<CAccountingEntry> acentries; std::list<CAccountingEntry> acentries;
@ -1966,9 +1966,9 @@ CAmount CWallet::GetBalance() const
CAmount nTotal = 0; CAmount nTotal = 0;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (std::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()) if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableCredit(); nTotal += pcoin->GetAvailableCredit();
} }
@ -1982,9 +1982,9 @@ CAmount CWallet::GetUnconfirmedBalance() const
CAmount nTotal = 0; CAmount nTotal = 0;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (std::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()) if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
nTotal += pcoin->GetAvailableCredit(); nTotal += pcoin->GetAvailableCredit();
} }
@ -1997,9 +1997,9 @@ CAmount CWallet::GetImmatureBalance() const
CAmount nTotal = 0; CAmount nTotal = 0;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (std::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(); nTotal += pcoin->GetImmatureCredit();
} }
} }
@ -2011,9 +2011,9 @@ CAmount CWallet::GetWatchOnlyBalance() const
CAmount nTotal = 0; CAmount nTotal = 0;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (std::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()) if (pcoin->IsTrusted())
nTotal += pcoin->GetAvailableWatchOnlyCredit(); nTotal += pcoin->GetAvailableWatchOnlyCredit();
} }
@ -2027,9 +2027,9 @@ CAmount CWallet::GetUnconfirmedWatchOnlyBalance() const
CAmount nTotal = 0; CAmount nTotal = 0;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (std::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()) if (!pcoin->IsTrusted() && pcoin->GetDepthInMainChain() == 0 && pcoin->InMempool())
nTotal += pcoin->GetAvailableWatchOnlyCredit(); nTotal += pcoin->GetAvailableWatchOnlyCredit();
} }
@ -2042,9 +2042,9 @@ CAmount CWallet::GetImmatureWatchOnlyBalance() const
CAmount nTotal = 0; CAmount nTotal = 0;
{ {
LOCK2(cs_main, cs_wallet); LOCK2(cs_main, cs_wallet);
for (std::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(); nTotal += pcoin->GetImmatureWatchOnlyCredit();
} }
} }
@ -2118,10 +2118,10 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
CAmount nTotal = 0; CAmount nTotal = 0;
for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) for (const auto& entry : mapWallet)
{ {
const uint256& wtxid = it->first; const uint256& wtxid = entry.first;
const CWalletTx* pcoin = &(*it).second; const CWalletTx* pcoin = &entry.second;
if (!CheckFinalTx(*pcoin)) if (!CheckFinalTx(*pcoin))
continue; continue;
@ -2182,10 +2182,10 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
if (pcoin->tx->vout[i].nValue < nMinimumAmount || pcoin->tx->vout[i].nValue > nMaximumAmount) if (pcoin->tx->vout[i].nValue < nMinimumAmount || pcoin->tx->vout[i].nValue > nMaximumAmount)
continue; 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; continue;
if (IsLockedCoin((*it).first, i)) if (IsLockedCoin(entry.first, i))
continue; continue;
if (IsSpent(wtxid, i)) if (IsSpent(wtxid, i))
@ -3640,9 +3640,9 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
// find first block that affects those keys, if there are any left // find first block that affects those keys, if there are any left
std::vector<CKeyID> vAffected; 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... // iterate over all wallet transactions...
const CWalletTx &wtx = (*it).second; const CWalletTx &wtx = entry.second;
BlockMap::const_iterator blit = mapBlockIndex.find(wtx.hashBlock); BlockMap::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) { if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) {
// ... which are already in a block // ... which are already in a block
@ -3662,8 +3662,8 @@ void CWallet::GetKeyBirthTimes(std::map<CTxDestination, int64_t> &mapKeyBirth) c
} }
// Extract block timestamps for those keys // Extract block timestamps for those keys
for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++) for (const auto& entry : mapKeyFirstBlock)
mapKeyBirth[it->first] = it->second->GetBlockTime() - TIMESTAMP_WINDOW; // block times can be 2h off mapKeyBirth[entry.first] = entry.second->GetBlockTime() - TIMESTAMP_WINDOW; // block times can be 2h off
} }
/** /**

View File

@ -40,15 +40,15 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>; factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>; 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 (gArgs.IsArgSet(arg)) if (gArgs.IsArgSet(arg))
{ {
CZMQNotifierFactory factory = i->second; CZMQNotifierFactory factory = entry.second;
std::string address = gArgs.GetArg(arg, ""); std::string address = gArgs.GetArg(arg, "");
CZMQAbstractNotifier *notifier = factory(); CZMQAbstractNotifier *notifier = factory();
notifier->SetType(i->first); notifier->SetType(entry.first);
notifier->SetAddress(address); notifier->SetAddress(address);
notifiers.push_back(notifier); notifiers.push_back(notifier);
} }