Skip to content

Commit

Permalink
ENH: Add explicit OptimizerParameters(inputData, dimension) constructor
Browse files Browse the repository at this point in the history
Eases constructing an `OptimizerParameters` object from the data of a
contiguous container, like `std::vector` or `std::array`.

Included a GTest, `OptimizerParameters.ConstructWithSpecifiedDataAndSize`.
  • Loading branch information
N-Dekker committed Mar 8, 2021
1 parent a44407b commit 800e59e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Modules/Core/Common/include/itkOptimizerParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class ITK_TEMPLATE_EXPORT OptimizerParameters : public Array<TParametersValueTyp
: Array<TParametersValueType>(dimension, value)
{}


/** Constructor with input data and size (number of elements). */
explicit OptimizerParameters(const ValueType * const inputData, const SizeValueType dimension)
: Array<TParametersValueType>(inputData, dimension)
{}


/** Initialize. Initialization called by constructors. */
void
Initialize()
Expand Down
29 changes: 28 additions & 1 deletion Modules/Core/Common/test/itkOptimizerParametersGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
// First include the header file to be tested:
#include "itkOptimizerParameters.h"
#include <gtest/gtest.h>
#include <algorithm> // For std::count.

#include <algorithm> // For count and equal.
#include <array>
#include <vector>


// Tests constructing OptimizerParameters of the specified size and initial value.
Expand All @@ -40,3 +43,27 @@ TEST(OptimizerParameters, ConstructWithSpecifiedSizeAndInitialValue)
}
}
}


// Tests constructing OptimizerParameters with the specified data and size.
TEST(OptimizerParameters, ConstructWithSpecifiedDataAndSize)
{
using OptimizerParametersType = itk::OptimizerParameters<double>;

// First test for size = zero.
EXPECT_EQ(OptimizerParametersType(std::vector<double>{}.data(), 0), OptimizerParametersType{});
EXPECT_EQ(OptimizerParametersType(std::array<double, 0>{}.data(), 0), OptimizerParametersType{});

// Test for an arbitrary size:
const auto testConstructOptimizerParameters = [](const double * const inputData, const itk::SizeValueType dimension) {
const OptimizerParametersType optimizerParameters(inputData, dimension);
ASSERT_EQ(optimizerParameters.size(), dimension);
EXPECT_TRUE(std::equal(optimizerParameters.begin(), optimizerParameters.end(), inputData));
};

testConstructOptimizerParameters(std::array<double, 1>{}.data(), 1);
testConstructOptimizerParameters(std::vector<double>{ 1.0 }.data(), 1);

const std::vector<double> stdVector{ 1.0, 2.0, 4.0 };
testConstructOptimizerParameters(stdVector.data(), stdVector.size());
}

0 comments on commit 800e59e

Please sign in to comment.