Skip to content

Commit

Permalink
STYLE: Use range-based loops from C++11
Browse files Browse the repository at this point in the history
C++11 Range based for loops can be used in

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 loops are more explicit for only computing the
end location once for containers.

Changed variable names for modified loops.
Prefer auto (or const auto) to explicit type designation.
  • Loading branch information
hjmjohnson committed Dec 22, 2024
1 parent e1a44fd commit ac77fef
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 147 deletions.
8 changes: 4 additions & 4 deletions Modules/Core/Mesh/include/itkBinaryMask3DMeshSource.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,9 @@ BinaryMask3DMeshSource<TInputImage, TOutputMesh>::CreateMesh()
}
}
}
for (int j = 0; j < 14; ++j)
for (auto & voxelElem : m_CurrentVoxel)
{
m_CurrentVoxel[j] = 0;
voxelElem = 0;
}

if ((vertexindex == 0) || (vertexindex == 255))
Expand Down Expand Up @@ -1206,9 +1206,9 @@ BinaryMask3DMeshSource<TInputImage, TOutputMesh>::AddCells(unsigned char celltyp
if ((index % m_ImageWidth == 0) || (index > m_LastVoxelIndex + 1))
{
m_ColFlag = 0;
for (int i = 0; i < 14; ++i)
for (auto & i : m_LastVoxel)
{
m_LastVoxel[i] = 0;
i = 0;
}
}
else
Expand Down
27 changes: 13 additions & 14 deletions Modules/Core/Mesh/include/itkConnectedRegionsMeshFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>::DeleteSeed(IdentifierType id)
{
std::vector<IdentifierType> tmpVector;
for (auto i = m_SeedList.begin(); i != m_SeedList.end(); ++i)
for (const auto & seed : m_SeedList)
{
if (*i != id)
if (seed != id)
{
tmpVector.push_back(*i);
tmpVector.push_back(seed);
}
}
m_SeedList.clear();
for (auto i = tmpVector.begin(); i != tmpVector.end(); ++i)
for (const auto & seedID : tmpVector)
{
m_SeedList.push_back(*i);
m_SeedList.push_back(seedID);
}
}

Expand All @@ -78,17 +78,17 @@ void
ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>::DeleteSpecifiedRegion(IdentifierType id)
{
std::vector<IdentifierType> tmpVector;
for (auto i = m_RegionList.begin(); i != m_RegionList.end(); ++i)
for (const auto & regionID : m_RegionList)
{
if (*i != id)
if (regionID != id)
{
tmpVector.push_back(*i);
tmpVector.push_back(regionID);
}
}
m_RegionList.clear();
for (auto i = tmpVector.begin(); i != tmpVector.end(); ++i)
for (const auto & regionID : tmpVector)
{
m_RegionList.push_back(*i);
m_RegionList.push_back(regionID);
}
}

Expand Down Expand Up @@ -322,9 +322,9 @@ ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>::GenerateData()
{
IdentifierType regionId = static_cast<IdentifierType>(m_Visited[cellId]);
// see if cell is on region
for (auto i = m_RegionList.begin(); i != m_RegionList.end(); ++i)
for (const auto & regionID : m_RegionList)
{
if (*i == regionId)
if (regionID == regionId)
{
inReg = true;
break;
Expand Down Expand Up @@ -404,9 +404,8 @@ ConnectedRegionsMeshFilter<TInputMesh, TOutputMesh>::PropagateConnectedWave()

while (!m_Wave->empty())
{
for (auto i = m_Wave->begin(); i != m_Wave->end(); ++i)
for (const auto cellId : *m_Wave)
{
IdentifierType cellId = *i;
if (m_Visited[cellId] < 0)
{
m_Visited[cellId] = static_cast<OffsetValueType>(m_RegionNumber);
Expand Down
18 changes: 9 additions & 9 deletions Modules/Core/Mesh/test/itkMeshTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,12 @@ itkMeshTest(int, char *[])
&neighborSet); // Where to put result.

std::cout << "Neighbors (hex edge 0):" << std::endl;
for (auto cell = neighborSet.begin(); cell != neighborSet.end(); ++cell)
for (const auto cell : neighborSet)
{
std::cout << "Id " << *cell << ": ";
std::cout << "Id " << cell << ": ";
CellAutoPointer cellPointer;

if (mesh->GetCell(*cell, cellPointer))
if (mesh->GetCell(cell, cellPointer))
{
std::cout << cellPointer->GetNameOfClass();
}
Expand Down Expand Up @@ -449,12 +449,12 @@ itkMeshTest(int, char *[])
&neighborSet); // Where to put result.

std::cout << "Neighbors (hex edge 1):" << std::endl;
for (auto cell = neighborSet.begin(); cell != neighborSet.end(); ++cell)
for (const auto cell : neighborSet)
{
std::cout << "Id " << *cell << ": ";
std::cout << "Id " << cell << ": ";
CellAutoPointer cellPointer;

if (mesh->GetCell(*cell, cellPointer))
if (mesh->GetCell(cell, cellPointer))
{
std::cout << cellPointer->GetNameOfClass();
}
Expand All @@ -473,12 +473,12 @@ itkMeshTest(int, char *[])
&neighborSet); // Where to put result.

std::cout << "Neighbors (tet edge 3):" << std::endl;
for (auto cell = neighborSet.begin(); cell != neighborSet.end(); ++cell)
for (const auto cell : neighborSet)
{
std::cout << "Id " << *cell << ": ";
std::cout << "Id " << cell << ": ";
CellAutoPointer cellPointer;

if (mesh->GetCell(*cell, cellPointer))
if (mesh->GetCell(cell, cellPointer))
{
std::cout << cellPointer->GetNameOfClass();
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/TestKernel/src/itkTestDriverInclude.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -886,10 +886,10 @@ HashTestImage(const char * testImageFilename, const std::vector<std::string> & b


// print out all md5 baselines
for (auto iter = baselineMD5Vector.begin(); iter != baselineMD5Vector.end(); ++iter)
for (const auto & baselienMD5 : baselineMD5Vector)
{
std::cout << "<DartMeasurement name=\"BaselineMD5\" type=\"text/string\">";
std::cout << *iter;
std::cout << baselienMD5;
std::cout << "</DartMeasurement>" << std::endl;
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/GDCM/src/itkGDCMSeriesFileNames.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ GDCMSeriesFileNames::GetFileNames(const std::string serie)
if (!flist->empty())
{
ProgressReporter progress(this, 0, static_cast<itk::SizeValueType>(flist->size()), 10);
for (gdcm::FileList::iterator it = flist->begin(); it != flist->end(); ++it)
for (auto & element : *flist)
{
gdcm::FileWithName * header = *it;
gdcm::FileWithName * header = element;
m_InputFileNames.push_back(header->filename);
progress.CompletedPixel();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ RegularExpressionSeriesFileNames::GetFileNames()
// Now, store the sorted names in a vector
m_FileNames.clear();

for (auto siter = sortedBySubMatch.begin(); siter != sortedBySubMatch.end(); ++siter)
for (auto & currPair : sortedBySubMatch)
{
m_FileNames.push_back(siter->first);
m_FileNames.push_back(currPair.first);
}

return m_FileNames;
Expand Down
4 changes: 2 additions & 2 deletions Modules/IO/ImageBase/test/itkArchetypeSeriesFileNamesTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ itkArchetypeSeriesFileNamesTest(int argc, char * argv[])
std::vector<std::string> names = fit->GetFileNames();

std::cout << "List of returned filenames: " << std::endl;
for (auto nit = names.begin(); nit != names.end(); ++nit)
for (auto & name : names)
{
std::cout << "File: " << nit->c_str() << std::endl;
std::cout << "File: " << name.c_str() << std::endl;
}

std::cout << fit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ itkRegularExpressionSeriesFileNamesTest(int argc, char * argv[])

// normal sort
std::cout << "Normal Sort--------" << std::endl;
for (auto nit = names.begin(); nit != names.end(); ++nit)
for (auto & name : names)
{
std::cout << "File: " << nit->c_str() << std::endl;
std::cout << "File: " << name.c_str() << std::endl;
}

// Show only those files with numbers in the names
Expand All @@ -70,9 +70,9 @@ itkRegularExpressionSeriesFileNamesTest(int argc, char * argv[])
fit->SetSubMatch(subMatch);
names = fit->GetFileNames();
std::cout << "Numeric sort on only files with numbers in the names--------" << std::endl;
for (auto nit = names.begin(); nit != names.end(); ++nit)
for (auto & name : names)
{
std::cout << "File: " << nit->c_str() << std::endl;
std::cout << "File: " << name.c_str() << std::endl;
}


Expand All @@ -89,9 +89,9 @@ itkRegularExpressionSeriesFileNamesTest(int argc, char * argv[])
names = fit->GetFileNames();
std::cout << "Numeric sort on only files with numbers in the names. Sort on the first set of numbers.--------"
<< std::endl;
for (auto nit = names.begin(); nit != names.end(); ++nit)
for (auto & name : names)
{
std::cout << "File: " << nit->c_str() << std::endl;
std::cout << "File: " << name.c_str() << std::endl;
}

// Show only those files with numbers in the names followed by other
Expand All @@ -106,9 +106,9 @@ itkRegularExpressionSeriesFileNamesTest(int argc, char * argv[])
names = fit->GetFileNames();
std::cout << "Numeric sort on only files with numbers in the names. Sort on the second set of numbers.--------"
<< std::endl;
for (auto nit = names.begin(); nit != names.end(); ++nit)
for (auto & name : names)
{
std::cout << "File: " << nit->c_str() << std::endl;
std::cout << "File: " << name.c_str() << std::endl;
}


Expand Down
46 changes: 23 additions & 23 deletions Modules/IO/Meta/src/itkMetaImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ MetaImageIO::WriteImageInformation()
// Save out the metadatadictionary key/value pairs as part of
// the metaio header.
std::vector<std::string> keys = metaDict.GetKeys();
for (auto keyIt = keys.begin(); keyIt != keys.end(); ++keyIt)
for (auto & key : keys)
{
if (*keyIt == ITK_ExperimentDate || *keyIt == ITK_VoxelUnits)
if (key == ITK_ExperimentDate || key == ITK_VoxelUnits)
{
continue;
}
Expand All @@ -590,69 +590,69 @@ MetaImageIO::WriteImageInformation()
bool bval = false;
std::vector<double> vval(0);
std::string value = "";
if (ExposeMetaData<std::string>(metaDict, *keyIt, value))
if (ExposeMetaData<std::string>(metaDict, key, value))
{
strs << value;
}
else if (ExposeMetaData<double>(metaDict, *keyIt, dval))
else if (ExposeMetaData<double>(metaDict, key, dval))
{
strs << dval;
}
else if (ExposeMetaData<float>(metaDict, *keyIt, fval))
else if (ExposeMetaData<float>(metaDict, key, fval))
{
strs << fval;
}
else if (ExposeMetaData<long>(metaDict, *keyIt, lval))
else if (ExposeMetaData<long>(metaDict, key, lval))
{
strs << lval;
}
else if (ExposeMetaData<unsigned long>(metaDict, *keyIt, ulval))
else if (ExposeMetaData<unsigned long>(metaDict, key, ulval))
{
strs << ulval;
}
else if (ExposeMetaData<long long>(metaDict, *keyIt, llval))
else if (ExposeMetaData<long long>(metaDict, key, llval))
{
strs << llval;
}
else if (ExposeMetaData<unsigned long long>(metaDict, *keyIt, ullval))
else if (ExposeMetaData<unsigned long long>(metaDict, key, ullval))
{
strs << ullval;
}
else if (ExposeMetaData<int>(metaDict, *keyIt, ival))
else if (ExposeMetaData<int>(metaDict, key, ival))
{
strs << ival;
}
else if (ExposeMetaData<unsigned int>(metaDict, *keyIt, uval))
else if (ExposeMetaData<unsigned int>(metaDict, key, uval))
{
strs << uval;
}
else if (ExposeMetaData<short>(metaDict, *keyIt, shval))
else if (ExposeMetaData<short>(metaDict, key, shval))
{
strs << shval;
}
else if (ExposeMetaData<unsigned short>(metaDict, *keyIt, ushval))
else if (ExposeMetaData<unsigned short>(metaDict, key, ushval))
{
strs << ushval;
}
else if (ExposeMetaData<char>(metaDict, *keyIt, cval))
else if (ExposeMetaData<char>(metaDict, key, cval))
{
strs << cval;
}
else if (ExposeMetaData<unsigned char>(metaDict, *keyIt, ucval))
else if (ExposeMetaData<unsigned char>(metaDict, key, ucval))
{
strs << ucval;
}
else if (ExposeMetaData<bool>(metaDict, *keyIt, bval))
else if (ExposeMetaData<bool>(metaDict, key, bval))
{
strs << bval;
}
else if (ExposeMetaData<std::vector<double>>(metaDict, *keyIt, vval))
else if (ExposeMetaData<std::vector<double>>(metaDict, key, vval))
{
_join(vval, ' ', strs);
}
else if (WriteMatrixInMetaData<1>(strs, metaDict, *keyIt) || WriteMatrixInMetaData<2>(strs, metaDict, *keyIt) ||
WriteMatrixInMetaData<3>(strs, metaDict, *keyIt) || WriteMatrixInMetaData<4>(strs, metaDict, *keyIt) ||
WriteMatrixInMetaData<5>(strs, metaDict, *keyIt) || WriteMatrixInMetaData<6>(strs, metaDict, *keyIt))
else if (WriteMatrixInMetaData<1>(strs, metaDict, key) || WriteMatrixInMetaData<2>(strs, metaDict, key) ||
WriteMatrixInMetaData<3>(strs, metaDict, key) || WriteMatrixInMetaData<4>(strs, metaDict, key) ||
WriteMatrixInMetaData<5>(strs, metaDict, key) || WriteMatrixInMetaData<6>(strs, metaDict, key))
{
// Nothing to do, everything is done in WriteMatrixInMetaData
}
Expand All @@ -664,16 +664,16 @@ MetaImageIO::WriteImageInformation()
// if the value is an empty string then the resulting entry in
// the header will not be able to be read by the metaIO
// library, which results is an unreadable/corrupt file.
itkWarningMacro("Unsupported or empty metaData item " << *keyIt << " of type "
<< metaDict[*keyIt]->GetMetaDataObjectTypeName()
itkWarningMacro("Unsupported or empty metaData item " << key << " of type "
<< metaDict[key]->GetMetaDataObjectTypeName()
<< "found, won't be written to image file");
// so this entry should be skipped.
continue;
}

// Rolling this back out so that the tests pass.
// The meta image AddUserField requires control of the memory space.
m_MetaImage.AddUserField(keyIt->c_str(), MET_STRING, static_cast<int>(value.size()), value.c_str(), true, -1);
m_MetaImage.AddUserField(key.c_str(), MET_STRING, static_cast<int>(value.size()), value.c_str(), true, -1);
}
}

Expand Down
Loading

0 comments on commit ac77fef

Please sign in to comment.