Skip to content

Commit

Permalink
STYLE: Replace NumericTraits<T>::ZeroValue() with T{}
Browse files Browse the repository at this point in the history
For any type `T` that is supported by `NumericTraits<T>`, the expression
`NumericTraits<T>::ZeroValue()` is just equivalent to `T{}`. `T{}` appears
preferred, because it is more generic, and it may yield faster code than the
corresponding `NumericTraits<T>::ZeroValue()` function call.

Using Notepad++, Replace in Files:

    Find what: itk::NumericTraits<(\w+)>::ZeroValue\(\)
    Find what: NumericTraits<(\w+)>::ZeroValue\(\)
    Replace with: $1{}
    Filters: itk*.hxx;itk*.h;itk*.cxx
    Directory: ITK\Modules
    [v] Match case
    (*) Regular expression

Excluded "itkNumericTraitsTest.cxx", because it purposely calls
`NumericTraits::ZeroValue()` as part of the test.

Follow-up to pull request InsightSoftwareConsortium#4017
commit 5ac803e
"STYLE: Replace `T var{ NumericTraits<T>::ZeroValue() }` with `T var{}`"
  • Loading branch information
N-Dekker committed Feb 12, 2024
1 parent 9a35c2f commit eba9c01
Show file tree
Hide file tree
Showing 397 changed files with 898 additions and 926 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ BackwardDifferenceOperator<TPixel, TDimension, TAllocator>::GenerateCoefficients

coeff[0] = -1.0 * NumericTraits<PixelType>::OneValue();
coeff[1] = NumericTraits<PixelType>::OneValue();
coeff[2] = NumericTraits<PixelType>::ZeroValue();
coeff[2] = PixelType{};

return coeff;
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/Common/include/itkBoundingBox.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ BoundingBox<TPointIdentifier, VPointDimension, TCoordRep, TPointsContainer>::Com
{
if (this->GetMTime() > m_BoundsMTime)
{
m_Bounds.Fill(NumericTraits<CoordRepType>::ZeroValue());
m_Bounds.Fill(CoordRepType{});
m_BoundsMTime.Modified();
}
return false;
Expand All @@ -146,7 +146,7 @@ BoundingBox<TPointIdentifier, VPointDimension, TCoordRep, TPointsContainer>::Com
// start by initializing the values
if (m_PointsContainer->Size() < 1)
{
m_Bounds.Fill(NumericTraits<CoordRepType>::ZeroValue());
m_Bounds.Fill(CoordRepType{});
m_BoundsMTime.Modified();
return false;
}
Expand Down Expand Up @@ -285,7 +285,7 @@ auto
BoundingBox<TPointIdentifier, VPointDimension, TCoordRep, TPointsContainer>::GetDiagonalLength2() const
-> AccumulateType
{
typename NumericTraits<CoordRepType>::AccumulateType dist2 = NumericTraits<CoordRepType>::ZeroValue();
typename NumericTraits<CoordRepType>::AccumulateType dist2 = CoordRepType{};

if (this->ComputeBoundingBox())
{
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkCellInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ class ITK_TEMPLATE_EXPORT CellInterface
CoordRepType
GetBoundingBoxDiagonalLength2()
{
return NumericTraits<CoordRepType>::ZeroValue();
return CoordRepType{};
}

/** Intersect the given bounding box (bounds[PointDimension*2]) with a line
Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/Common/include/itkColorTable.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ColorTable<TComponent>::UseDiscreteColors()
else
{
scale = NumericTraits<TComponent>::OneValue();
shift = NumericTraits<TComponent>::ZeroValue();
shift = TComponent{};
}

m_Color[0].Set((TComponent)(0.9 * scale + shift), (TComponent)(shift), (TComponent)(shift));
Expand Down Expand Up @@ -123,7 +123,7 @@ ColorTable<TComponent>::UseGrayColors(unsigned int n)
else
{
range = NumericTraits<TComponent>::OneValue();
minimum = NumericTraits<TComponent>::ZeroValue();
minimum = TComponent{};
}
typename NumericTraits<TComponent>::RealType delta;
if (m_NumberOfColors > 1)
Expand Down Expand Up @@ -178,7 +178,7 @@ ColorTable<TComponent>::UseHeatColors(unsigned int n)
else
{
scale = NumericTraits<TComponent>::OneValue();
shift = NumericTraits<TComponent>::ZeroValue();
shift = TComponent{};
}
// Converting from TComponent to RealType may introduce a rounding error, so do static_cast
constexpr auto max_value_converted =
Expand Down Expand Up @@ -238,7 +238,7 @@ ColorTable<TComponent>::UseRandomColors(unsigned int n)
}
else
{
minimum = NumericTraits<TComponent>::ZeroValue();
minimum = TComponent{};
maximum = NumericTraits<TComponent>::OneValue();
}
for (i = 0; i < n; ++i)
Expand Down
8 changes: 4 additions & 4 deletions Modules/Core/Common/include/itkCompensatedSummation.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CompensatedSummationAddElement(TFloat & compensation, TFloat & sum, const TFloat
template <typename TFloat>
CompensatedSummation<TFloat>::CompensatedSummation(const TFloat value)
: m_Sum(value)
, m_Compensation(NumericTraits<AccumulateType>::ZeroValue())
, m_Compensation(AccumulateType{})
{}

template <typename TFloat>
Expand Down Expand Up @@ -138,16 +138,16 @@ template <typename TFloat>
void
CompensatedSummation<TFloat>::ResetToZero()
{
this->m_Sum = NumericTraits<AccumulateType>::ZeroValue();
this->m_Compensation = NumericTraits<AccumulateType>::ZeroValue();
this->m_Sum = AccumulateType{};
this->m_Compensation = AccumulateType{};
}

template <typename TFloat>
CompensatedSummation<TFloat> &
CompensatedSummation<TFloat>::operator=(const FloatType & rhs)
{
this->m_Sum = rhs;
this->m_Compensation = NumericTraits<AccumulateType>::ZeroValue();
this->m_Compensation = AccumulateType{};

return *this;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkConceptChecking.h
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ struct HasZero
{
T a;

a = NumericTraits<T>::ZeroValue();
a = T{};
Detail::IgnoreUnusedVariable(a);
}
};
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkCovariantVector.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ template <typename T, unsigned int VVectorDimension>
typename CovariantVector<T, VVectorDimension>::ValueType CovariantVector<T, VVectorDimension>::operator*(
const Self & other) const
{
typename NumericTraits<T>::AccumulateType value = NumericTraits<T>::ZeroValue();
typename NumericTraits<T>::AccumulateType value = T{};
for (unsigned int i = 0; i < VVectorDimension; ++i)
{
value += (*this)[i] * other[i];
Expand All @@ -113,7 +113,7 @@ template <typename T, unsigned int VVectorDimension>
typename CovariantVector<T, VVectorDimension>::ValueType CovariantVector<T, VVectorDimension>::operator*(
const Vector<T, VVectorDimension> & other) const
{
typename NumericTraits<T>::AccumulateType value = NumericTraits<T>::ZeroValue();
typename NumericTraits<T>::AccumulateType value = T{};
for (unsigned int i = 0; i < VVectorDimension; ++i)
{
value += (*this)[i] * other[i];
Expand Down
6 changes: 3 additions & 3 deletions Modules/Core/Common/include/itkDiffusionTensor3D.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ DiffusionTensor3D<T>::GetRelativeAnisotropy() const -> RealValueType
// zero.
if (trace < NumericTraits<RealValueType>::min())
{
return NumericTraits<RealValueType>::ZeroValue();
return RealValueType{};
}

const RealValueType anisotropy = 3.0 * isp - trace * trace;

if (anisotropy < NumericTraits<RealValueType>::ZeroValue())
if (anisotropy < RealValueType{})
{
return NumericTraits<RealValueType>::ZeroValue();
return RealValueType{};
}

const auto relativeAnisotropySquared = static_cast<RealValueType>(anisotropy / (std::sqrt(3.0) * trace));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ForwardDifferenceOperator<TPixel, VDimension, TAllocator>::GenerateCoefficients(
{
CoefficientVector coeff(3);

coeff[0] = NumericTraits<PixelType>::ZeroValue();
coeff[0] = PixelType{};
coeff[1] = -1.0 * NumericTraits<PixelType>::OneValue();
coeff[2] = NumericTraits<PixelType>::OneValue();

Expand Down
6 changes: 2 additions & 4 deletions Modules/Core/Common/include/itkHeavisideStepFunction.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ template <typename TInput, typename TOutput>
auto
HeavisideStepFunction<TInput, TOutput>::Evaluate(const InputType & input) const -> OutputType
{
return (input >= NumericTraits<InputType>::ZeroValue()) ? NumericTraits<OutputType>::OneValue()
: NumericTraits<OutputType>::ZeroValue();
return (input >= InputType{}) ? NumericTraits<OutputType>::OneValue() : OutputType{};
}

template <typename TInput, typename TOutput>
auto
HeavisideStepFunction<TInput, TOutput>::EvaluateDerivative(const InputType & input) const -> OutputType
{
return (Math::ExactlyEquals(input, NumericTraits<InputType>::ZeroValue())) ? NumericTraits<OutputType>::OneValue()
: NumericTraits<OutputType>::ZeroValue();
return (Math::ExactlyEquals(input, InputType{})) ? NumericTraits<OutputType>::OneValue() : OutputType{};
}

} // namespace itk
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class ITK_TEMPLATE_EXPORT Matrix
inline vnl_matrix_fixed<T, VColumns, VRows>
GetInverse() const
{
if (vnl_determinant(m_Matrix) == NumericTraits<T>::ZeroValue())
if (vnl_determinant(m_Matrix) == T{})
{
itkGenericExceptionMacro("Singular matrix. Determinant is 0.");
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/Core/Common/include/itkNeighborhoodOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class ITK_TEMPLATE_EXPORT NeighborhoodOperator : public Neighborhood<TPixel, VDi
{
for (unsigned int i = 0; i < this->Size(); ++i)
{
this->operator[](i) = NumericTraits<PixelType>::ZeroValue();
this->operator[](i) = PixelType{};
}
}

Expand Down
34 changes: 17 additions & 17 deletions Modules/Core/Common/include/itkNumericTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class NumericTraits : public std::numeric_limits<T>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
/** Return the length of the scalar. This API is needed for
* VariableLengthVector because
Expand Down Expand Up @@ -368,7 +368,7 @@ class NumericTraits<bool> : public std::numeric_limits<bool>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -474,7 +474,7 @@ class NumericTraits<char> : public std::numeric_limits<char>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -596,7 +596,7 @@ class NumericTraits<signed char> : public std::numeric_limits<signed char>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -700,7 +700,7 @@ class NumericTraits<unsigned char> : public std::numeric_limits<unsigned char>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -802,7 +802,7 @@ class NumericTraits<short> : public std::numeric_limits<short>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -905,7 +905,7 @@ class NumericTraits<unsigned short> : public std::numeric_limits<unsigned short>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1007,7 +1007,7 @@ class NumericTraits<int> : public std::numeric_limits<int>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1129,7 +1129,7 @@ class NumericTraits<unsigned int> : public std::numeric_limits<unsigned int>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1232,7 +1232,7 @@ class NumericTraits<long> : public std::numeric_limits<long>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1335,7 +1335,7 @@ class NumericTraits<unsigned long> : public std::numeric_limits<unsigned long>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1439,7 +1439,7 @@ class NumericTraits<float> : public std::numeric_limits<float>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1542,7 +1542,7 @@ class NumericTraits<double> : public std::numeric_limits<double>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1653,7 +1653,7 @@ class NumericTraits<long double> : public std::numeric_limits<long double>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1757,7 +1757,7 @@ class NumericTraits<long long> : public std::numeric_limits<long long>
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1852,7 +1852,7 @@ class NumericTraits<unsigned long long> : public std::numeric_limits<unsigned lo
{
itkGenericExceptionMacro("Cannot set the size of a scalar to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}
};

Expand Down Expand Up @@ -1978,7 +1978,7 @@ class NumericTraits<std::complex<TComponent>>
{
itkGenericExceptionMacro("Cannot set the size of a complex to " << s);
}
m = NumericTraits<ValueType>::ZeroValue();
m = ValueType{};
}

#if defined(ITK_LEGACY_REMOVE)
Expand Down
4 changes: 2 additions & 2 deletions Modules/Core/Common/include/itkNumericTraitsArrayPixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class NumericTraits<Array<T>>
{
Self b(a.Size());

b.Fill(NumericTraits<T>::ZeroValue());
b.Fill(T{});
return b;
}

Expand Down Expand Up @@ -121,7 +121,7 @@ class NumericTraits<Array<T>>
SetLength(Array<T> & m, const unsigned int s)
{
m.SetSize(s);
m.Fill(NumericTraits<T>::ZeroValue());
m.Fill(T{});
}

/** Get the length of the input array. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class NumericTraits<CovariantVector<T, D>>
{
itkGenericExceptionMacro("Cannot set the size of a CovariantVector of length " << D << " to " << s);
}
m.Fill(NumericTraits<T>::ZeroValue());
m.Fill(T{});
}

/** Return the length of the vector. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class NumericTraits<DiffusionTensor3D<T>>
itkGenericExceptionMacro("Cannot set the size of a DiffusionTensor3D "
"to anything other than 6.");
}
m.Fill(NumericTraits<T>::ZeroValue());
m.Fill(T{});
}

/** Return the size of the tensor. Always returns 6. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class NumericTraits<FixedArray<T, D>>
{
itkGenericExceptionMacro("Cannot set the size of a FixedArray of length " << D << " to " << s);
}
m.Fill(NumericTraits<T>::ZeroValue());
m.Fill(T{});
}

/** Return the length of the array. */
Expand Down
Loading

0 comments on commit eba9c01

Please sign in to comment.