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) {
  }
  • Loading branch information
practicalswift authored and random-zebra committed Jul 25, 2021
1 parent 5a7750a commit dd3d3c4
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/addrdb.h
Expand Up @@ -55,7 +55,7 @@ class CBanEntry
banReason = BanReasonUnknown;
}

std::string banReasonToString()
std::string banReasonToString() const
{
switch (banReason) {
case BanReasonNodeMisbehaving:
Expand Down
6 changes: 3 additions & 3 deletions src/addrman.cpp
Expand Up @@ -405,9 +405,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
10 changes: 5 additions & 5 deletions src/addrman.h
Expand Up @@ -325,18 +325,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
8 changes: 4 additions & 4 deletions src/net.cpp
Expand Up @@ -97,11 +97,11 @@ 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++) {
int nScore = (*it).second.nScore;
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
for (const auto& entry : mapLocalHost) {
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
Expand Up @@ -56,11 +56,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/rpc/net.cpp
Expand Up @@ -521,11 +521,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 (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());
Expand Down
4 changes: 2 additions & 2 deletions src/serialize.h
Expand Up @@ -1009,8 +1009,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: 11 additions & 11 deletions src/test/coins_tests.cpp
Expand Up @@ -200,8 +200,8 @@ class CCoinsViewCacheTest : public CCoinsViewCache
memusage::DynamicUsage(cacheSaplingAnchors) +
memusage::DynamicUsage(cacheSaplingNullifiers);
size_t count = 0;
for (CCoinsMap::iterator it = cacheCoins.begin(); it != cacheCoins.end(); it++) {
ret += memusage::DynamicUsage(it->second.coin);
for (const auto& entry : cacheCoins) {
ret += memusage::DynamicUsage(entry.second.coin);
++count;
}
BOOST_CHECK_EQUAL(GetCacheSize(), count);
Expand Down Expand Up @@ -690,15 +690,15 @@ 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 (auto it = result.begin(); it != result.end(); it++) {
bool have = stack.back()->HaveCoin(it->first);
const Coin& coin = stack.back()->AccessCoin(it->first);
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());
BOOST_CHECK(coin == it->second);
BOOST_CHECK(coin == entry.second);
if (coin.IsSpent()) {
missed_an_entry = true;
} else {
BOOST_CHECK(stack.back()->HaveCoinInCache(it->first));
BOOST_CHECK(stack.back()->HaveCoinInCache(entry.first));
found_an_entry = true;
}
}
Expand Down Expand Up @@ -895,11 +895,11 @@ 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 (auto it = result.begin(); it != result.end(); it++) {
bool have = stack.back()->HaveCoin(it->first);
const Coin& coin = stack.back()->AccessCoin(it->first);
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());
BOOST_CHECK(coin == it->second);
BOOST_CHECK(coin == entry.second);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/validation.cpp
Expand Up @@ -3948,8 +3948,8 @@ void static CheckBlockIndex()

// 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.emplace(it->second->pprev, it->second);
for (auto& entry : mapBlockIndex) {
forward.emplace(entry.second->pprev, entry.second);
}

assert(forward.size() == mapBlockIndex.size());
Expand Down
9 changes: 4 additions & 5 deletions src/wallet/wallet.cpp
Expand Up @@ -2512,7 +2512,6 @@ CWallet::OutputAvailabilityResult CWallet::CheckOutputAvailability(

// Check if we should include zero value utxo
if (output.nValue <= 0) return res;

if (fCoinsSelected && coinControl && !coinControl->fAllowOtherInputs && !coinControl->IsSelected(COutPoint(wtxid, outIndex)))
return res;

Expand Down Expand Up @@ -3893,9 +3892,9 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const

// 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.m_confirm.hashBlock);
if (blit != mapBlockIndex.end() && chainActive.Contains(blit->second)) {
// ... which are already in a block
Expand All @@ -3915,8 +3914,8 @@ void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64_t>& mapKeyBirth) const
}

// 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() - TIMESTAMP_WINDOW; // block times can be 2h off
for (const auto& entry : mapKeyFirstBlock)
mapKeyBirth[entry.first] = entry.second->GetBlockTime() - TIMESTAMP_WINDOW; // 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
Expand Up @@ -39,15 +39,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 (gArgs.IsArgSet(arg))
{
CZMQNotifierFactory factory = i->second;
CZMQNotifierFactory factory = entry.second;
std::string address = gArgs.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 dd3d3c4

Please sign in to comment.