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
7 changes: 6 additions & 1 deletion Modules/Core/Common/include/itkArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ class ITK_TEMPLATE_EXPORT Array : public vnl_vector<TValue>
/** Construct from a VnlVectorType */
explicit Array(const VnlVectorType &);

/** Constructor with size. Size can only be changed by assignment */
/** Constructor with size. Size can only be changed by assignment.
* \note This constructor may not initialize its elements.
*/
explicit Array(SizeValueType dimension);

/** Constructor with size and initial value for each element. */
explicit Array(SizeValueType dimension, const ValueType & 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 array does not manage the memory of the buffer. It merely points to
Expand Down
7 changes: 7 additions & 0 deletions Modules/Core/Common/include/itkArray.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ Array<TValue>::Array(SizeValueType dimension)
m_LetArrayManageMemory(true)
{}

/** Constructor with size and initial value for each element. */
template <typename TValue>
Array<TValue>::Array(const SizeValueType dimension, const TValue & value)
: vnl_vector<TValue>(dimension, value)
, m_LetArrayManageMemory{ true }
{}

/** Constructor with user specified data */
template <typename TValue>
Array<TValue>::Array(ValueType * datain, SizeValueType sz, bool LetArrayManageMemory)
Expand Down
9 changes: 8 additions & 1 deletion Modules/Core/Common/include/itkOptimizerParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class ITK_TEMPLATE_EXPORT OptimizerParameters : public Array<TParametersValueTyp
// something different.
}

/** Constructor with size. Size can only be changed by assignment */
/** Constructor with size. Size can only be changed by assignment.
* \note This constructor may not initialize its elements.
*/
explicit OptimizerParameters(SizeValueType dimension)
: Array<TParametersValueType>(dimension)
{}
Expand All @@ -77,6 +79,11 @@ class ITK_TEMPLATE_EXPORT OptimizerParameters : public Array<TParametersValueTyp
: Array<TParametersValueType>(array)
{}

/** Constructor with size and initial value for each element. */
explicit OptimizerParameters(const SizeValueType dimension, const ValueType & value)
: Array<TParametersValueType>(dimension, value)
{}

/** Initialize. Initialization called by constructors. */
void
Initialize()
Expand Down
1 change: 1 addition & 0 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ set(ITKCommonGTests
itkIndexRangeGTest.cxx
itkMersenneTwisterRandomVariateGeneratorGTest.cxx
itkNeighborhoodAllocatorGTest.cxx
itkOptimizerParametersGTest.cxx
itkPointGTest.cxx
itkShapedImageNeighborhoodRangeGTest.cxx
itkSizeGTest.cxx
Expand Down
42 changes: 42 additions & 0 deletions Modules/Core/Common/test/itkOptimizerParametersGTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// First include the header file to be tested:
#include "itkOptimizerParameters.h"
#include <gtest/gtest.h>
#include <algorithm> // For std::count.


// Tests constructing OptimizerParameters of the specified size and initial value.
TEST(OptimizerParameters, ConstructWithSpecifiedSizeAndInitialValue)
{
using OptimizerParametersType = itk::OptimizerParameters<double>;

for (double initialValue{ -1.0 }; initialValue <= 1.0; ++initialValue)
{
EXPECT_EQ(OptimizerParametersType(0, initialValue).size(), 0);

for (std::size_t size{ 1 }; size <= 4; ++size)
{
const OptimizerParametersType optimizerParameters(size, initialValue);

EXPECT_EQ(optimizerParameters.size(), size);
EXPECT_EQ(std::count(optimizerParameters.begin(), optimizerParameters.end(), initialValue), size);
}
}
}