Skip to content

Commit 211adc0

Browse files
Use range-based for loops (C++11) when looping over vector elements
1 parent ea6fde3 commit 211adc0

File tree

5 files changed

+22
-25
lines changed

5 files changed

+22
-25
lines changed

src/net_processing.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
754754
const CTransaction& tx = *ptx;
755755

756756
// Which orphan pool entries must we evict?
757-
for (size_t j = 0; j < tx.vin.size(); j++) {
758-
auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
757+
for (const auto& txin : tx.vin) {
758+
auto itByPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
759759
if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
760760
for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
761761
const CTransaction& orphanTx = *(*mi)->second.tx;
@@ -1553,10 +1553,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
15531553

15541554
uint32_t nFetchFlags = GetFetchFlags(pfrom);
15551555

1556-
for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
1556+
for (CInv &inv : vInv)
15571557
{
1558-
CInv &inv = vInv[nInv];
1559-
15601558
if (interruptMsgProc)
15611559
return true;
15621560

src/policy/fees.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ void CBlockPolicyEstimator::processBlock(unsigned int nBlockHeight,
604604

605605
unsigned int countedTxs = 0;
606606
// Update averages with data points from current block
607-
for (unsigned int i = 0; i < entries.size(); i++) {
608-
if (processBlockTx(nBlockHeight, entries[i]))
607+
for (const auto& entry : entries) {
608+
if (processBlockTx(nBlockHeight, entry))
609609
countedTxs++;
610610
}
611611

src/script/interpreter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,24 +1142,24 @@ class CTransactionSignatureSerializer {
11421142

11431143
uint256 GetPrevoutHash(const CTransaction& txTo) {
11441144
CHashWriter ss(SER_GETHASH, 0);
1145-
for (unsigned int n = 0; n < txTo.vin.size(); n++) {
1146-
ss << txTo.vin[n].prevout;
1145+
for (const auto& txin : txTo.vin) {
1146+
ss << txin.prevout;
11471147
}
11481148
return ss.GetHash();
11491149
}
11501150

11511151
uint256 GetSequenceHash(const CTransaction& txTo) {
11521152
CHashWriter ss(SER_GETHASH, 0);
1153-
for (unsigned int n = 0; n < txTo.vin.size(); n++) {
1154-
ss << txTo.vin[n].nSequence;
1153+
for (const auto& txin : txTo.vin) {
1154+
ss << txin.nSequence;
11551155
}
11561156
return ss.GetHash();
11571157
}
11581158

11591159
uint256 GetOutputsHash(const CTransaction& txTo) {
11601160
CHashWriter ss(SER_GETHASH, 0);
1161-
for (unsigned int n = 0; n < txTo.vout.size(); n++) {
1162-
ss << txTo.vout[n];
1161+
for (const auto& txout : txTo.vout) {
1162+
ss << txout;
11631163
}
11641164
return ss.GetHash();
11651165
}

src/validation.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool
309309
return EvaluateSequenceLocks(index, lockPair);
310310
}
311311

312-
313312
void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
314313
int expired = pool.Expire(GetTime() - age);
315314
if (expired != 0) {
@@ -2817,8 +2816,8 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
28172816

28182817
// No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
28192818
if (!fHaveWitness) {
2820-
for (size_t i = 0; i < block.vtx.size(); i++) {
2821-
if (block.vtx[i]->HasWitness()) {
2819+
for (const auto& tx : block.vtx) {
2820+
if (tx->HasWitness()) {
28222821
return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
28232822
}
28242823
}

src/wallet/wallet.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,8 +1781,8 @@ bool CWalletTx::IsEquivalentTo(const CWalletTx& _tx) const
17811781
{
17821782
CMutableTransaction tx1 = *this->tx;
17831783
CMutableTransaction tx2 = *_tx.tx;
1784-
for (unsigned int i = 0; i < tx1.vin.size(); i++) tx1.vin[i].scriptSig = CScript();
1785-
for (unsigned int i = 0; i < tx2.vin.size(); i++) tx2.vin[i].scriptSig = CScript();
1784+
for (auto& txin : tx1.vin) txin.scriptSig = CScript();
1785+
for (auto& txin : tx2.vin) txin.scriptSig = CScript();
17861786
return CTransaction(tx1) == CTransaction(tx2);
17871787
}
17881788

@@ -2183,10 +2183,10 @@ bool CWallet::SelectCoinsMinConf(const CAmount& nTargetValue, const int nConfMin
21832183

21842184
if (nTotalLower == nTargetValue)
21852185
{
2186-
for (unsigned int i = 0; i < vValue.size(); ++i)
2186+
for (const auto& input : vValue)
21872187
{
2188-
setCoinsRet.insert(vValue[i]);
2189-
nValueRet += vValue[i].txout.nValue;
2188+
setCoinsRet.insert(input);
2189+
nValueRet += input.txout.nValue;
21902190
}
21912191
return true;
21922192
}
@@ -2316,7 +2316,7 @@ bool CWallet::SignTransaction(CMutableTransaction &tx)
23162316
// sign the new tx
23172317
CTransaction txNewConst(tx);
23182318
int nIn = 0;
2319-
for (auto& input : tx.vin) {
2319+
for (const auto& input : tx.vin) {
23202320
std::map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(input.prevout.hash);
23212321
if(mi == mapWallet.end() || input.prevout.n >= mi->second.tx->vout.size()) {
23222322
return false;
@@ -3258,11 +3258,11 @@ std::set< std::set<CTxDestination> > CWallet::GetAddressGroupings()
32583258
}
32593259

32603260
// group lone addrs by themselves
3261-
for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++)
3262-
if (IsMine(pcoin->tx->vout[i]))
3261+
for (const auto& txout : pcoin->tx->vout)
3262+
if (IsMine(txout))
32633263
{
32643264
CTxDestination address;
3265-
if(!ExtractDestination(pcoin->tx->vout[i].scriptPubKey, address))
3265+
if(!ExtractDestination(txout.scriptPubKey, address))
32663266
continue;
32673267
grouping.insert(address);
32683268
groupings.insert(grouping);

0 commit comments

Comments
 (0)