Skip to content

Add VariableLengthVector(length, value) constructor#5693

Merged
thewtex merged 2 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:VariableLengthVector-constructor-with-length-and-value
Dec 17, 2025
Merged

Add VariableLengthVector(length, value) constructor#5693
thewtex merged 2 commits intoInsightSoftwareConsortium:mainfrom
N-Dekker:VariableLengthVector-constructor-with-length-and-value

Conversation

@N-Dekker
Copy link
Copy Markdown
Contributor

Proposed a VariableLengthVector(unsigned int length, const TValue & value) constructor that creates a VariableLengthVector of the specified length, filled with the specified value for each element.

Added a use case: simplifying NumericTraits<VariableLengthVector<T>>.

Array and OptimizerParameters already have such a constructor.

@github-actions github-actions Bot added type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module labels Dec 16, 2025
Copy link
Copy Markdown
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on a glance.

Copy link
Copy Markdown
Member

@blowekamp blowekamp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine.

return data.release();
}() }
, m_NumElements{ length }
{}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation looks reasonable.... but, I looks at the other implementation in the hxx file. Why is this in the header and not hxx file? And is this implementation is rather different that VariableLengthVector(unsigned int) which uses Reserve. Might be good to keep these is sync and not diverge too much?

Might even make since to do:

explicit VariableLengthVector(unsigned int length, const TValue & value) : VariableLengthVector(length) {
std::fill_n(m_Data, length, value);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comments @blowekamp Is it preferable to move the implementation of this little constructor to the hxx file? VariableLengthVector does have some of its member function implementations in its h file, already:

template <typename T>
VariableLengthVector(const VariableLengthVector<T> & v)
: m_NumElements(v.Size())
{
if (m_NumElements != 0)

Copy link
Copy Markdown
Contributor Author

@N-Dekker N-Dekker Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion is nice, but it may not be exception-safe:

explicit VariableLengthVector(unsigned int length, const TValue & value) : VariableLengthVector(length) {
std::fill_n(m_Data, length, value);
}

When std::fill_n(m_Data, length, value) throws an exception, is the data still deallocated?


Update: I just checked: your suggestion does not leak, when the body (the std::fill_n call) throws an exception. 👍

Copy link
Copy Markdown
Member

@blowekamp blowekamp Dec 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some extra complication in the VariableLengthVector(unsigned int) construction... but there seems to be a lot of room for improvement in this class.

Thank you for working on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blowekamp Addressing your comment, please wait...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is some extra complication in the VariableLengthVector(unsigned int) construction... but there seems to be a lot of room for improvement in this class.

Indeed! I think VariableLengthVector(unsigned int) should call AllocateElements, instead of Reserve, right? Or simply just do new TValue[length]!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The utility of AllocateElements wrapping a new to throw an exception which requires additional memory allocation to perform ostringstream operations is certainly IMHO, questionable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the changes to the existing constructor is another PR.

@N-Dekker N-Dekker force-pushed the VariableLengthVector-constructor-with-length-and-value branch from e5f6069 to 13477f5 Compare December 16, 2025 15:50
Follow-up to pull request InsightSoftwareConsortium#2248
commit 73ce4ca
"ENH: Array, OptimizerParameters constructors with size and initial value"
Replaced local `b` variables and `b.Fill` calls.
@N-Dekker N-Dekker force-pushed the VariableLengthVector-constructor-with-length-and-value branch from 13477f5 to a912fac Compare December 16, 2025 16:03
@N-Dekker N-Dekker marked this pull request as ready for review December 16, 2025 16:07
@thewtex thewtex merged commit 1773aa4 into InsightSoftwareConsortium:main Dec 17, 2025
17 checks passed
N-Dekker added a commit to N-Dekker/ITK that referenced this pull request Jan 6, 2026
Following pull request InsightSoftwareConsortium#5693
commit a912fac
"STYLE: Do return `VariableLengthVector(length, value)` in NumericTraits"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants