Skip to content

Commit

Permalink
STYLE: Replace if (x > f()) x = f() with x = std::min(x, f())
Browse files Browse the repository at this point in the history
Simplified the code by using `std::min`. 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 added the necessary template argument to the `std::min` call in
`IntensityWindowingImageFilter`, because it could not be deduced automatically.
  • Loading branch information
N-Dekker authored and dzenanz committed Feb 21, 2024
1 parent 755cd10 commit 1b9fc82
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 32 deletions.
6 changes: 2 additions & 4 deletions Modules/Core/Common/src/itkPlatformMultiThreaderPosix.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "itkObjectFactory.h"
#include "itksys/SystemTools.hxx"
#include <unistd.h>
#include <algorithm> // For min.

namespace itk
{
Expand All @@ -46,10 +47,7 @@ PlatformMultiThreader::MultipleMethodExecute()
pthread_t process_id[ITK_MAX_THREADS];

// obey the global maximum number of threads limit
if (m_NumberOfWorkUnits > MultiThreaderBase::GetGlobalMaximumNumberOfThreads())
{
m_NumberOfWorkUnits = MultiThreaderBase::GetGlobalMaximumNumberOfThreads();
}
m_NumberOfWorkUnits = std::min(m_NumberOfWorkUnits, MultiThreaderBase::GetGlobalMaximumNumberOfThreads());
for (ThreadIdType thread_loop = 0; thread_loop < m_NumberOfWorkUnits; ++thread_loop)
{
if (m_MultipleMethod[thread_loop] == (ThreadFunctionType) nullptr)
Expand Down
6 changes: 2 additions & 4 deletions Modules/Core/Common/src/itkPlatformMultiThreaderSingle.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "itkObjectFactory.h"
#include "itksys/SystemTools.hxx"
#include <cstdlib>
#include <algorithm> // For min.

namespace itk
{
Expand All @@ -39,10 +40,7 @@ PlatformMultiThreader::MultipleMethodExecute()
ThreadIdType thread_loop;

// obey the global maximum number of threads limit
if (m_NumberOfWorkUnits > MultiThreaderBase::GetGlobalMaximumNumberOfThreads())
{
m_NumberOfWorkUnits = MultiThreaderBase::GetGlobalMaximumNumberOfThreads();
}
m_NumberOfWorkUnits = std::min(m_NumberOfWorkUnits, MultiThreaderBase::GetGlobalMaximumNumberOfThreads());

for (thread_loop = 0; thread_loop < m_NumberOfWorkUnits; ++thread_loop)
{
Expand Down
6 changes: 2 additions & 4 deletions Modules/Core/Common/src/itkPlatformMultiThreaderWindows.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "itkWindows.h"
#include <process.h>
#include <algorithm> // For min.

namespace itk
{
Expand All @@ -44,10 +45,7 @@ PlatformMultiThreader::MultipleMethodExecute()
HANDLE processId[ITK_MAX_THREADS];

// obey the global maximum number of threads limit
if (m_NumberOfWorkUnits > MultiThreaderBase::GetGlobalMaximumNumberOfThreads())
{
m_NumberOfWorkUnits = MultiThreaderBase::GetGlobalMaximumNumberOfThreads();
}
m_NumberOfWorkUnits = std::min(m_NumberOfWorkUnits, MultiThreaderBase::GetGlobalMaximumNumberOfThreads());
for (threadCount = 0; threadCount < m_NumberOfWorkUnits; ++threadCount)
{
if (m_MultipleMethod[threadCount] == nullptr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#define itkIntensityWindowingImageFilter_hxx

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

namespace itk
{
Expand Down Expand Up @@ -58,10 +59,7 @@ IntensityWindowingImageFilter<TInputImage, TOutputImage>::SetWindowLevel(const I
}

tmp2 = static_cast<InputRealType>(level) + (static_cast<InputRealType>(window) / 2.0);
if (tmp2 > NumericTraits<InputPixelType>::max())
{
tmp2 = NumericTraits<InputPixelType>::max();
}
tmp2 = std::min<InputRealType>(tmp2, NumericTraits<InputPixelType>::max());

this->m_WindowMinimum = static_cast<InputPixelType>(tmp1);
this->m_WindowMaximum = static_cast<InputPixelType>(tmp2);
Expand Down
11 changes: 3 additions & 8 deletions Modules/Filtering/Smoothing/include/itkBoxUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "itkOffset.h"
#include "itkNeighborhoodAlgorithm.h"
#include "itkZeroFluxNeumannBoundaryCondition.h"
#include <algorithm> // For min.

/*
*
Expand Down Expand Up @@ -358,10 +359,7 @@ BoxMeanCalculatorFunction(const TInputImage * accImage,
if (unitCorners[k][j] > 0)
{
// leading edge - crop it
if (thisCorner[j] > static_cast<OffsetValueType>(regionLimit[j]))
{
thisCorner[j] = static_cast<OffsetValueType>(regionLimit[j]);
}
thisCorner[j] = std::min(thisCorner[j], static_cast<OffsetValueType>(regionLimit[j]));
}
else
{
Expand Down Expand Up @@ -551,10 +549,7 @@ BoxSigmaCalculatorFunction(const TInputImage * accImage,
if (unitCorners[k][j] > 0)
{
// leading edge - crop it
if (thisCorner[j] > static_cast<OffsetValueType>(regionLimit[j]))
{
thisCorner[j] = static_cast<OffsetValueType>(regionLimit[j]);
}
thisCorner[j] = std::min(thisCorner[j], static_cast<OffsetValueType>(regionLimit[j]));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "itkFloodFilledImageFunctionConditionalIterator.h"
#include "itkProgressReporter.h"
#include "itkPrintHelper.h"
#include <algorithm> // For min.

namespace itk
{
Expand Down Expand Up @@ -272,10 +273,7 @@ ConfidenceConnectedImageFilter<TInputImage, TOutputImage>::GenerateData()
{
lower = static_cast<InputRealType>(NumericTraits<InputImagePixelType>::NonpositiveMin());
}
if (upper > static_cast<InputRealType>(NumericTraits<InputImagePixelType>::max()))
{
upper = static_cast<InputRealType>(NumericTraits<InputImagePixelType>::max());
}
upper = std::min(upper, static_cast<InputRealType>(NumericTraits<InputImagePixelType>::max()));

function->ThresholdBetween(static_cast<InputImagePixelType>(lower), static_cast<InputImagePixelType>(upper));

Expand Down Expand Up @@ -359,10 +357,7 @@ ConfidenceConnectedImageFilter<TInputImage, TOutputImage>::GenerateData()
{
lower = static_cast<InputRealType>(NumericTraits<InputImagePixelType>::NonpositiveMin());
}
if (upper > static_cast<InputRealType>(NumericTraits<InputImagePixelType>::max()))
{
upper = static_cast<InputRealType>(NumericTraits<InputImagePixelType>::max());
}
upper = std::min(upper, static_cast<InputRealType>(NumericTraits<InputImagePixelType>::max()));

function->ThresholdBetween(static_cast<InputImagePixelType>(lower), static_cast<InputImagePixelType>(upper));

Expand Down

0 comments on commit 1b9fc82

Please sign in to comment.