Skip to content

Commit 48daed0

Browse files
committed
STYLE: Use range-based loops from C++11
Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container, in the forward direction. ==== Range based loopes are more explicit for only computing the end location once for containers. for ( ImageIORegion::IndexType::const_iterator i = this->GetIndex().begin(); i != this->GetIndex().end(); //<- NOTE: Compute end every loop iteration ++i ) for (long i : this->GetIndex()) //<- NOTE: Implicitly only compute end once ==== Explicitly reduce the amount of index computations: (The compiler probably does this too) for(int i = 0; i < 11; i++) { pos[0] = testPoints[i][0]; pos[1] = testPoints[i][1]; ^^^^ for(auto & testPoint : testPoints) { pos[0] = testPoint[0]; pos[1] = testPoint[1]; ==== SRCDIR=/Users/johnsonhj/Dashboard/src/ITK #My local SRC BLDDIR=/Users/johnsonhj/Dashboard/src/ITK-clangtidy/ #My local BLD cd /Users/johnsonhj/Dashboard/src/ITK-clangtidy/ run-clang-tidy.py -checks=-*,modernize-loop-convert -header-filter=.* -fix Change-Id: I9a6b6a73491beb61fde913e3a2cf209f7c8601e5
1 parent 77b72f5 commit 48daed0

125 files changed

Lines changed: 680 additions & 728 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Examples/Filtering/BilateralImageFilter.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ int main( int argc, char * argv[] )
181181
// Software Guide : BeginCodeSnippet
182182
const unsigned int Dimension = InputImageType::ImageDimension;
183183
double domainSigmas[ Dimension ];
184-
for(unsigned int i=0; i<Dimension; i++)
184+
for(double & domainSigma : domainSigmas)
185185
{
186-
domainSigmas[i] = atof( argv[3] );
186+
domainSigma = atof( argv[3] );
187187
}
188188
const double rangeSigma = atof( argv[4] );
189189
// Software Guide : EndCodeSnippet

Examples/IO/IOFactoryRegistration.cxx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@
2323
// corresponding IO factory will be automatically registered.
2424
int main()
2525
{
26-
std::list<itk::ObjectFactoryBase *> factories =
26+
const std::list<itk::ObjectFactoryBase *> & factories =
2727
itk::ObjectFactoryBase::GetRegisteredFactories();
2828
const std::size_t numFactories = factories.size();
2929

3030
std::cout << numFactories << " Image IO factories registered:" << std::endl;
3131

3232
if (!factories.empty() )
3333
{
34-
for ( std::list<itk::ObjectFactoryBase*>::iterator
35-
f = factories.begin();
36-
f != factories.end(); ++f )
34+
for (const auto & factory : factories)
3735
{
38-
std::istringstream iss( (*f)->GetDescription() );
36+
std::istringstream iss( factory->GetDescription() );
3937
std::string IOType;
4038
iss >> IOType; // the first word of the description
4139
std::cout << IOType << " ";

Examples/IO/IOPlugin.cxx

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,18 @@ int main( int argc, char *argv[] )
6161
std::cout << "Count: " << numFactories << std::endl;
6262
if (!factories.empty())
6363
{
64-
for ( std::list<itk::ObjectFactoryBase*>::iterator
65-
f = factories.begin();
66-
f != factories.end(); ++f )
64+
for (auto & factory : factories)
6765
{
6866
std::cout << " Factory version: "
69-
<< (*f)->GetITKSourceVersion() << std::endl
67+
<< factory->GetITKSourceVersion() << std::endl
7068
<< " Factory description: "
71-
<< (*f)->GetDescription() << std::endl
72-
<< " Library Path: " << (*f)->GetLibraryPath() << std::endl;
69+
<< factory->GetDescription() << std::endl
70+
<< " Library Path: " << factory->GetLibraryPath() << std::endl;
7371

74-
std::list<std::string> overrides = (*f)->GetClassOverrideNames();
75-
std::list<std::string> names = (*f)->GetClassOverrideWithNames();
76-
std::list<std::string> descriptions = (*f)->GetClassOverrideDescriptions();
77-
std::list<bool> enableflags = (*f)->GetEnableFlags();
72+
std::list<std::string> overrides = factory->GetClassOverrideNames();
73+
std::list<std::string> names = factory->GetClassOverrideWithNames();
74+
std::list<std::string> descriptions = factory->GetClassOverrideDescriptions();
75+
std::list<bool> enableflags = factory->GetEnableFlags();
7876
std::list<std::string>::const_iterator n = names.begin();
7977
std::list<std::string>::const_iterator d = descriptions.begin();
8078
std::list<bool>::const_iterator e = enableflags.begin();
@@ -144,23 +142,21 @@ int main( int argc, char *argv[] )
144142

145143
if (!factories.empty())
146144
{
147-
for ( std::list<itk::ObjectFactoryBase*>::iterator
148-
f = factories.begin();
149-
f != factories.end(); ++f )
145+
for (auto & factory : factories)
150146
{
151-
std::cout << "check " << (void *) *f << std::endl;
147+
std::cout << "check " << (void *) factory << std::endl;
152148
std::cout << " Factory version: "
153-
<< (*f)->GetITKSourceVersion() << std::endl
149+
<< factory->GetITKSourceVersion() << std::endl
154150
<< " Factory description: "
155-
<< (*f)->GetDescription() << std::endl;
151+
<< factory->GetDescription() << std::endl;
156152

157-
std::list<std::string> overrides = (*f)->GetClassOverrideNames();
153+
std::list<std::string> overrides = factory->GetClassOverrideNames();
158154
std::cout << "ClassOverrideNames size: " << overrides.size() << std::endl;
159-
std::list<std::string> names = (*f)->GetClassOverrideWithNames();
155+
std::list<std::string> names = factory->GetClassOverrideWithNames();
160156
std::cout << "ClassOverrideWithNames size: " << names.size() << std::endl;
161-
std::list<std::string> descriptions = (*f)->GetClassOverrideDescriptions();
157+
std::list<std::string> descriptions = factory->GetClassOverrideDescriptions();
162158
std::cout << "ClassOverrideDescriptions size: " << descriptions.size() << std::endl;
163-
std::list<bool> enableflags = (*f)->GetEnableFlags();
159+
std::list<bool> enableflags = factory->GetEnableFlags();
164160
std::cout << "EnableFlags size: " << enableflags.size() << std::endl;
165161
std::list<std::string>::const_iterator n = names.begin();
166162
std::list<std::string>::const_iterator d = descriptions.begin();

Examples/IO/XML/itkParticleSwarmOptimizerDOMWriter.cxx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ ParticleSwarmOptimizerDOMWriter::GenerateData( DOMNodeType* outputdom, const voi
6868

6969
std::vector<double> lbound;
7070
std::vector<double> ubound;
71-
ParticleSwarmOptimizer::ParameterBoundsType bounds = ipobj->GetParameterBounds();
72-
for ( size_t i = 0; i < bounds.size(); i++ )
71+
for (auto & bound : ipobj->GetParameterBounds() )
7372
{
74-
lbound.push_back( bounds[i].first );
75-
ubound.push_back( bounds[i].second );
73+
lbound.push_back( bound.first );
74+
ubound.push_back( bound.second );
7675
}
7776
// generate and insert the DOM node for the lower bound of the parameters
7877
logger->Info( "writing LowerBound ...\n" );

Examples/IO/XML/itkParticleSwarmOptimizerSAXReader.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,9 @@ const char* ParticleSwarmOptimizerSAXReader::GetAttribute( const char** atts, co
274274
bool ParticleSwarmOptimizerSAXReader::ContextIs( const char* test ) const
275275
{
276276
std::string s = "";
277-
for ( size_t i = 0; i < this->m_CurrentTags.size(); i++ )
277+
for (const auto currentTag : this->m_CurrentTags)
278278
{
279-
s += "/" + std::string(this->m_CurrentTags[i]);
279+
s += "/" + std::string(currentTag);
280280
}
281281
if ( s.size() == 0 )
282282
{

Examples/IO/XML/itkParticleSwarmOptimizerSAXWriter.cxx

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,13 @@ int ParticleSwarmOptimizerSAXWriter::WriteFile()
8282
ofs << ">";
8383
ofs << "\n";
8484

85-
//
86-
ParticleSwarmOptimizer::ParameterBoundsType bounds = this->m_InputObject->GetParameterBounds();
87-
8885
// write the lower bound
89-
9086
ofs << " <bound id=\"lower\"";
9187

9288
ofs << " value=\"";
93-
for ( size_t i = 0; i < bounds.size(); i++ )
89+
for (const auto & bound : this->m_InputObject->GetParameterBounds() )
9490
{
95-
ofs << " " << bounds[i].first;
91+
ofs << " " << bound.first;
9692
}
9793
ofs << "\"";
9894

@@ -104,9 +100,9 @@ int ParticleSwarmOptimizerSAXWriter::WriteFile()
104100
ofs << " <bound id=\"upper\"";
105101

106102
ofs << " value=\"";
107-
for ( size_t i = 0; i < bounds.size(); i++ )
103+
for (const auto & bound : this->m_InputObject->GetParameterBounds() )
108104
{
109-
ofs << " " << bounds[i].second;
105+
ofs << " " << bound.second;
110106
}
111107
ofs << "\"";
112108

Examples/Statistics/KdTree.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,12 @@ int main()
322322
<< "query point = [" << queryPoint << "]" << std::endl
323323
<< "search radius = " << radius << std::endl;
324324
std::cout << "measurement vector : distance" << std::endl;
325-
for ( unsigned int i = 0; i < neighbors.size(); ++i )
325+
for ( auto neighbor : neighbors)
326326
{
327-
std::cout << "[" << tree->GetMeasurementVector( neighbors[i] )
327+
std::cout << "[" << tree->GetMeasurementVector( neighbor )
328328
<< "] : "
329329
<< distanceMetric->Evaluate(
330-
tree->GetMeasurementVector( neighbors[i]))
330+
tree->GetMeasurementVector( neighbor))
331331
<< std::endl;
332332
}
333333
// Software Guide : EndCodeSnippet
@@ -347,12 +347,12 @@ int main()
347347
<< "query point = [" << queryPoint << "]" << std::endl
348348
<< "search radius = " << radius << std::endl;
349349
std::cout << "measurement vector : distance" << std::endl;
350-
for ( unsigned int i = 0; i < neighbors.size(); ++i )
350+
for ( auto neighbor : neighbors)
351351
{
352-
std::cout << "[" << centroidTree->GetMeasurementVector( neighbors[i] )
352+
std::cout << "[" << centroidTree->GetMeasurementVector( neighbor )
353353
<< "] : "
354354
<< distanceMetric->Evaluate(
355-
centroidTree->GetMeasurementVector( neighbors[i]))
355+
centroidTree->GetMeasurementVector( neighbor))
356356
<< std::endl;
357357
}
358358
// Software Guide : EndCodeSnippet

Examples/Statistics/ScalarImageMarkovRandomField1.cxx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,9 @@ int main( int argc, char * argv [] )
377377
{
378378
totalWeight += *wcIt;
379379
}
380-
for(std::vector< double >::iterator wIt = weights.begin();
381-
wIt != weights.end(); ++wIt )
380+
for(double & weight : weights)
382381
{
383-
*wIt = static_cast< double > ( (*wIt) * meanDistance / (2 * totalWeight));
382+
weight = static_cast< double > ( weight * meanDistance / (2 * totalWeight));
384383
}
385384

386385
mrfFilter->SetMRFNeighborhoodWeight( weights );

Modules/Core/Common/include/itkOctreeNode.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ class ITKCommon_EXPORT OctreeNodeBranch
126126
OctreeNodeBranch() {}
127127
OctreeNodeBranch(OctreeBase *parent)
128128
{
129-
for ( int i = 0; i < 8; i++ )
130-
{
131-
m_Leaves[i].SetParentOctree(parent);
132-
}
129+
for (auto & leaf : m_Leaves)
130+
{
131+
leaf.SetParentOctree(parent);
132+
}
133133
}
134134

135135
inline OctreeNode * GetLeaf(enum LeafIdentifier LeafID)

Modules/Core/Common/include/itkTriangleHelper.hxx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,21 @@ TriangleHelper< TPoint >::ComputeConstrainedCircumCenter(const PointType & iP1,
201201
const PointType & iP2, const PointType & iP3)
202202
{
203203
PointType oPt;
204-
CoordRepType a = iP2.SquaredEuclideanDistanceTo (iP3);
205-
CoordRepType b = iP1.SquaredEuclideanDistanceTo (iP3);
206-
CoordRepType c = iP2.SquaredEuclideanDistanceTo (iP1);
207-
208-
CoordRepType Weight[3];
204+
const CoordRepType a = iP2.SquaredEuclideanDistanceTo (iP3);
205+
const CoordRepType b = iP1.SquaredEuclideanDistanceTo (iP3);
206+
const CoordRepType c = iP2.SquaredEuclideanDistanceTo (iP1);
209207

210-
Weight[0] = a * ( b + c - a );
211-
Weight[1] = b * ( c + a - b );
212-
Weight[2] = c * ( a + b - c );
208+
CoordRepType Weight[3] = {
209+
a * ( b + c - a ),
210+
b * ( c + a - b ),
211+
c * ( a + b - c )
212+
};
213213

214-
for ( unsigned int i = 0; i < 3; i++ )
214+
for (auto & i : Weight)
215215
{
216-
if ( Weight[i] < 0.0 )
216+
if ( i < 0.0 )
217217
{
218-
Weight[i] = 0.;
218+
i = 0.;
219219
}
220220
}
221221

0 commit comments

Comments
 (0)