Skip to content

Commit

Permalink
STYLE: Replace if (x > a[i]) x = a[i] with x = std::min(x, a[i])
Browse files Browse the repository at this point in the history
Using Notepad++, Replace in Files:

    Find what: ([ ]+)if \((.+) > (.+\])\)\r\n\1{\r\n\1  \2 = \3;\r\n\1}
    Replace with: \1\2 = std::min\(\2, \3\);
    Filters: itk*.hxx;itk*.h;itk*.cxx
    [v] Match case
    (*) Regular expression

Manually adjusted the type of `numberOfPieces` in
"itkMetaImageStreamingWriterIOTest.cxx", to make sure that both arguments of
`std::min` have the same type.

Follow-up to pull request #4470
commit 1b9fc82
"STYLE: Replace `if (x > f()) x = f()` with `x = std::min(x, f())`"
  • Loading branch information
N-Dekker committed Feb 27, 2024
1 parent 0a30e7f commit 93bef51
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 53 deletions.
6 changes: 2 additions & 4 deletions Modules/Core/Common/include/itkNeighborhoodAlgorithm.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkImageRegionIterator.h"
#include "itkImageRegion.h"
#include "itkConstSliceIterator.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -106,10 +107,7 @@ ImageBoundaryFacesCalculator<TImage>::Compute(const TImage & img, RegionType reg
}

// Boundary region cannot be outside the region to process
if (fSize[j] > rSize[j])
{
fSize[j] = rSize[j];
}
fSize[j] = std::min(fSize[j], rSize[j]);
}
// avoid unsigned overflow if the non-boundary region is too small to
// process
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkConceptChecking.h"

#include "itkMath.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -77,10 +78,7 @@ LinearInterpolateImageFunction<TInputImage, TCoordRep>::EvaluateUnoptimized(cons
++(neighIndex[dim]);
// Take care of the case where the pixel is just
// in the outer upper boundary of the image grid.
if (neighIndex[dim] > this->m_EndIndex[dim])
{
neighIndex[dim] = this->m_EndIndex[dim];
}
neighIndex[dim] = std::min(neighIndex[dim], this->m_EndIndex[dim]);
overlap *= distance[dim];
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


#include "itkMath.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -72,10 +73,7 @@ VectorLinearInterpolateImageFunction<TInputImage, TCoordRep>::EvaluateAtContinuo
neighIndex[dim] = baseIndex[dim] + 1;
// Take care of the case where the pixel is just
// in the outer upper boundary of the image grid.
if (neighIndex[dim] > this->m_EndIndex[dim])
{
neighIndex[dim] = this->m_EndIndex[dim];
}
neighIndex[dim] = std::min(neighIndex[dim], this->m_EndIndex[dim]);
overlap *= distance[dim];
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkContourSpatialObject_hxx

#include "itkNumericTraits.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -81,10 +82,7 @@ ContourSpatialObject<TDimension>::GetOrientationInObjectSpace() const
PointType curpoint = it->GetPositionInObjectSpace();
for (unsigned int i = 0; i < TDimension; ++i)
{
if (minPnt[i] > curpoint[i])
{
minPnt[i] = curpoint[i];
}
minPnt[i] = std::min(minPnt[i], curpoint[i]);
if (maxPnt[i] < curpoint[i])
{
maxPnt[i] = curpoint[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define itkPolygonSpatialObject_hxx

#include "itkMath.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -69,10 +70,7 @@ PolygonSpatialObject<TDimension>::GetOrientationInObjectSpace() const
PointType curpoint = it->GetPositionInObjectSpace();
for (unsigned int i = 0; i < TDimension; ++i)
{
if (minPnt[i] > curpoint[i])
{
minPnt[i] = curpoint[i];
}
minPnt[i] = std::min(minPnt[i], curpoint[i]);
if (maxPnt[i] < curpoint[i])
{
maxPnt[i] = curpoint[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "vnl/algo/vnl_symmetric_eigensystem.h"
#include "vnl/algo/vnl_matrix_inverse.h"
#include "itkCastImageFilter.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -262,10 +263,7 @@ DisplacementFieldTransform<TParametersValueType, VDimension>::ComputeJacobianWit
{
difIndex[0][col] = startingIndex[col];
}
if (difIndex[3][col] > upperIndex[col])
{
difIndex[3][col] = upperIndex[col];
}
difIndex[3][col] = std::min(difIndex[3][col], upperIndex[col]);

OutputVectorType pixDisp[4];
for (unsigned int i = 0; i < 4; ++i)
Expand Down
6 changes: 2 additions & 4 deletions Modules/Filtering/ImageSources/include/itkGridImageSource.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkImageLinearIteratorWithIndex.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkTotalProgressReporter.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -50,10 +51,7 @@ GridImageSource<TOutputImage>::BeforeThreadedGenerateData()

for (unsigned int i = 0; i < ImageDimension; ++i)
{
if (this->m_GridOffset[i] > this->m_GridSpacing[i])
{
this->m_GridOffset[i] = this->m_GridSpacing[i];
}
this->m_GridOffset[i] = std::min(this->m_GridOffset[i], this->m_GridSpacing[i]);
PixelArrayType pixels = m_PixelArrays->CreateElementAt(i);
pixels.set_size(this->GetSize()[i]);
pixels.fill(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageLinearConstIteratorWithIndex.h"
#include "itkImageScanlineConstIterator.h"
#include "itkTotalProgressReporter.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -87,10 +88,7 @@ LabelStatisticsImageFilter<TInputImage, TLabelImage>::MergeMap(MapType & m1, Map
// bounding box is min,max pairs
for (unsigned int ii = 0; ii < (ImageDimension * 2); ii += 2)
{
if (labelStats.m_BoundingBox[ii] > m2_value.second.m_BoundingBox[ii])
{
labelStats.m_BoundingBox[ii] = m2_value.second.m_BoundingBox[ii];
}
labelStats.m_BoundingBox[ii] = std::min(labelStats.m_BoundingBox[ii], m2_value.second.m_BoundingBox[ii]);
if (labelStats.m_BoundingBox[ii + 1] < m2_value.second.m_BoundingBox[ii + 1])
{
labelStats.m_BoundingBox[ii + 1] = m2_value.second.m_BoundingBox[ii + 1];
Expand Down Expand Up @@ -222,10 +220,7 @@ LabelStatisticsImageFilter<TInputImage, TLabelImage>::ThreadedStreamedGenerateDa
for (unsigned int i = 0; i < (2 * TInputImage::ImageDimension); i += 2)
{
const IndexType & index = it.GetIndex();
if (labelStats.m_BoundingBox[i] > index[i / 2])
{
labelStats.m_BoundingBox[i] = index[i / 2];
}
labelStats.m_BoundingBox[i] = std::min(labelStats.m_BoundingBox[i], index[i / 2]);
if (labelStats.m_BoundingBox[i + 1] < index[i / 2])
{
labelStats.m_BoundingBox[i + 1] = index[i / 2];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkImageRegion.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionReverseIterator.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -79,10 +80,7 @@ BinomialBlurImageFilter<TInputImage, TOutputImage>::GenerateInputRequestedRegion
}

inputSize[i] += m_Repetitions;
if (inputSize[i] > inputLargestPossibleRegionSize[i])
{
inputSize[i] = inputLargestPossibleRegionSize[i];
}
inputSize[i] = std::min(inputSize[i], inputLargestPossibleRegionSize[i]);
}

inputRegion.SetIndex(inputIndex);
Expand Down
8 changes: 3 additions & 5 deletions Modules/IO/Meta/test/itkMetaImageStreamingWriterIOTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "itkImageFileWriter.h"
#include "itkMetaImageIO.h"
#include "itkTestingMacros.h"
#include <algorithm> // For min.

int
itkMetaImageStreamingWriterIOTest(int argc, char * argv[])
Expand Down Expand Up @@ -53,7 +54,7 @@ itkMetaImageStreamingWriterIOTest(int argc, char * argv[])
ImageType::SizeType fullsize;
ImageType::IndexType index;

unsigned int numberOfPieces = 10;
itk::SizeValueType numberOfPieces = 10;

// We decide how we want to read the image and we split accordingly
// The image is read slice by slice
Expand All @@ -65,10 +66,7 @@ itkMetaImageStreamingWriterIOTest(int argc, char * argv[])
size[1] = fullsize[1];
size[2] = 0;

if (numberOfPieces > fullsize[2])
{
numberOfPieces = fullsize[2];
}
numberOfPieces = std::min(numberOfPieces, fullsize[2]);
unsigned int zsize = fullsize[2] / numberOfPieces;

// Setup the writer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "itkAffineTransform.h"
#include "itkResampleImageFilter.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -219,10 +220,7 @@ LabelGeometryImageFilter<TLabelImage, TIntensityImage>::GenerateData()
for (unsigned int i = 0; i < (2 * ImageDimension); i += 2)
{
// Update min
if (mapIt->second.m_BoundingBox[i] > index[i / 2])
{
mapIt->second.m_BoundingBox[i] = index[i / 2];
}
mapIt->second.m_BoundingBox[i] = std::min(mapIt->second.m_BoundingBox[i], index[i / 2]);
// Update max
if (mapIt->second.m_BoundingBox[i + 1] < index[i / 2])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkPointSet.h"
#include "itkObjectToObjectMetric.h"
#include "itkPrintHelper.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -59,10 +60,7 @@ RegistrationParameterScalesEstimator<TMetric>::EstimateMaximumStepSize() -> Floa

for (SizeValueType d = 0; d < dim; ++d)
{
if (minSpacing > spacing[d])
{
minSpacing = spacing[d];
}
minSpacing = std::min(minSpacing, spacing[d]);
}

return minSpacing;
Expand Down

0 comments on commit 93bef51

Please sign in to comment.