From 207cb3e9237fa6960a67aab7d05a80ffd897bb17 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Tue, 16 Dec 2025 14:19:13 +0100 Subject: [PATCH 1/2] ENH: Add `VariableLengthVector(length, value)` constructor Follow-up to pull request https://github.com/InsightSoftwareConsortium/ITK/pull/2248 commit 73ce4ca9dd73498a7f8de4d9aca3b72b1f8b59fe "ENH: Array, OptimizerParameters constructors with size and initial value" --- .../Common/include/itkVariableLengthVector.h | 3 +++ .../include/itkVariableLengthVector.hxx | 7 ++++++ .../test/itkVariableLengthVectorGTest.cxx | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/Modules/Core/Common/include/itkVariableLengthVector.h b/Modules/Core/Common/include/itkVariableLengthVector.h index 29c89953295..52cea457f7c 100644 --- a/Modules/Core/Common/include/itkVariableLengthVector.h +++ b/Modules/Core/Common/include/itkVariableLengthVector.h @@ -344,6 +344,9 @@ class ITK_TEMPLATE_EXPORT VariableLengthVector */ explicit VariableLengthVector(unsigned int length); + /** Constructor with size and initial value for each element. */ + explicit VariableLengthVector(unsigned int length, const TValue & value); + /** Constructor that initializes array with contents from a user supplied * buffer. * The pointer to the buffer and the length is specified. By default, the diff --git a/Modules/Core/Common/include/itkVariableLengthVector.hxx b/Modules/Core/Common/include/itkVariableLengthVector.hxx index 48e5b5ca41a..7ea779f5df5 100644 --- a/Modules/Core/Common/include/itkVariableLengthVector.hxx +++ b/Modules/Core/Common/include/itkVariableLengthVector.hxx @@ -35,6 +35,13 @@ VariableLengthVector::VariableLengthVector(unsigned int length) itkAssertInDebugAndIgnoreInReleaseMacro(m_Data != nullptr); } +template +VariableLengthVector::VariableLengthVector(unsigned int length, const TValue & value) + : VariableLengthVector(length) +{ + std::fill_n(m_Data, length, value); +} + template VariableLengthVector::VariableLengthVector(ValueType * datain, unsigned int sz, bool LetArrayManageMemory) : m_LetArrayManageMemory(LetArrayManageMemory) diff --git a/Modules/Core/Common/test/itkVariableLengthVectorGTest.cxx b/Modules/Core/Common/test/itkVariableLengthVectorGTest.cxx index 0823a4076c5..fba4b2aff2b 100644 --- a/Modules/Core/Common/test/itkVariableLengthVectorGTest.cxx +++ b/Modules/Core/Common/test/itkVariableLengthVectorGTest.cxx @@ -140,3 +140,25 @@ TEST(VariableLengthVector, ReverseIteration) } } } + + +// Tests constructing with the specified length and value. +TEST(VariableLengthVector, ConstructWithSpecifiedLengthAndValue) +{ + using ValueType = int; + using VariableLengthVectorType = itk::VariableLengthVector; + using ValueLimits = std::numeric_limits; + + for (const auto value : { ValueLimits::min(), ValueType{}, ValueType{ 1 }, ValueLimits::max() }) + { + EXPECT_TRUE(VariableLengthVectorType(0, value).empty()); + + for (unsigned int length{ 1 }; length < 4; ++length) + { + const VariableLengthVectorType variableLengthVector(length, value); + + EXPECT_EQ(variableLengthVector.size(), length); + EXPECT_EQ(std::count(variableLengthVector.cbegin(), variableLengthVector.cend(), value), length); + } + } +} From a912fac4b35281e14925510010c75bdcacb4aeff Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Tue, 16 Dec 2025 15:00:17 +0100 Subject: [PATCH 2/2] STYLE: Do return `VariableLengthVector(length, value)` in NumericTraits Replaced local `b` variables and `b.Fill` calls. --- ...tkNumericTraitsVariableLengthVectorPixel.h | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/Modules/Core/Common/include/itkNumericTraitsVariableLengthVectorPixel.h b/Modules/Core/Common/include/itkNumericTraitsVariableLengthVectorPixel.h index e4b09ebe453..07e2f116442 100644 --- a/Modules/Core/Common/include/itkNumericTraitsVariableLengthVectorPixel.h +++ b/Modules/Core/Common/include/itkNumericTraitsVariableLengthVectorPixel.h @@ -95,45 +95,31 @@ class NumericTraits> static const Self max(const Self & a) { - Self b(a.Size()); - - b.Fill(NumericTraits::max()); - return b; + return Self(a.Size(), NumericTraits::max()); } static const Self min(const Self & a) { - Self b(a.Size()); - - b.Fill(NumericTraits::min()); - return b; + return Self(a.Size(), NumericTraits::min()); } static const Self ZeroValue(const Self & a) { - Self b(a.Size()); - - b.Fill(T{}); - return b; + return Self(a.Size(), T{}); } static const Self OneValue(const Self & a) { - Self b(a.Size()); - - b.Fill(NumericTraits::OneValue()); - return b; + return Self(a.Size(), NumericTraits::OneValue()); } static const Self NonpositiveMin(const Self & a) { - Self b(a.Size()); - b.Fill(NumericTraits::NonpositiveMin()); - return b; + return Self(a.Size(), NumericTraits::NonpositiveMin()); } static bool