Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,45 +95,31 @@ class NumericTraits<VariableLengthVector<T>>
static const Self
max(const Self & a)
{
Self b(a.Size());

b.Fill(NumericTraits<T>::max());
return b;
return Self(a.Size(), NumericTraits<T>::max());
}

static const Self
min(const Self & a)
{
Self b(a.Size());

b.Fill(NumericTraits<T>::min());
return b;
return Self(a.Size(), NumericTraits<T>::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<T>::OneValue());
return b;
return Self(a.Size(), NumericTraits<T>::OneValue());
}

static const Self
NonpositiveMin(const Self & a)
{
Self b(a.Size());
b.Fill(NumericTraits<T>::NonpositiveMin());
return b;
return Self(a.Size(), NumericTraits<T>::NonpositiveMin());
}

static bool
Expand Down
3 changes: 3 additions & 0 deletions Modules/Core/Common/include/itkVariableLengthVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Modules/Core/Common/include/itkVariableLengthVector.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ VariableLengthVector<TValue>::VariableLengthVector(unsigned int length)
itkAssertInDebugAndIgnoreInReleaseMacro(m_Data != nullptr);
}

template <typename TValue>
VariableLengthVector<TValue>::VariableLengthVector(unsigned int length, const TValue & value)
: VariableLengthVector(length)
{
std::fill_n(m_Data, length, value);
}

template <typename TValue>
VariableLengthVector<TValue>::VariableLengthVector(ValueType * datain, unsigned int sz, bool LetArrayManageMemory)
: m_LetArrayManageMemory(LetArrayManageMemory)
Expand Down
22 changes: 22 additions & 0 deletions Modules/Core/Common/test/itkVariableLengthVectorGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValueType>;
using ValueLimits = std::numeric_limits<ValueType>;

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);
}
}
}
Loading