Skip to content

Commit

Permalink
ENH: Add data() and size() member functions to FixedArray
Browse files Browse the repository at this point in the history
This unifies the interface of FixedArray with std containers.

New names for already existing functionality:
- `data()` is exactly the same as `GetDataPointer()`
- `size()` the same as `Size()`

The goal is to make it easier to provide a common interface
of basic types for python wrappings.
  • Loading branch information
phcerdan authored and dzenanz committed May 10, 2020
1 parent 680f10f commit 38939ff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Modules/Core/Common/include/itkFixedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,18 @@ class ITK_TEMPLATE_EXPORT FixedArray
return m_InternalArray;
}

ValueType *
data()
{
return m_InternalArray;
}

const ValueType *
data() const
{
return m_InternalArray;
}

/** Get various iterators to the array. */
Iterator
Begin();
Expand Down Expand Up @@ -410,9 +422,14 @@ class ITK_TEMPLATE_EXPORT FixedArray
return this->crend();
}

/** Size of the container */
SizeType
Size() const;

SizeType
size() const;

/** Set all the elements of the container to the input value. */
void
Fill(const ValueType &);

Expand Down
7 changes: 7 additions & 0 deletions Modules/Core/Common/include/itkFixedArray.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ FixedArray<TValue, VLength>::Size() const
return VLength;
}

template <typename TValue, unsigned int VLength>
typename FixedArray<TValue, VLength>::SizeType
FixedArray<TValue, VLength>::size() const
{
return VLength;
}

/**
* Fill all elements of the array with the given value.
*/
Expand Down
17 changes: 17 additions & 0 deletions Modules/Core/Common/test/itkFixedArrayGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,20 @@ TEST(FixedArray, IteratorIncrementReturnValue)
Check_iterators_increment_return_value<double, 2>();
Check_iterators_increment_return_value<int, 3>();
}

// Tests data() and size() works
TEST(FixedArray, StdMemberFunctionsWork)
{
using FixedArrayType = itk::FixedArray<double, 3>;
auto d3arr = FixedArrayType(3);
d3arr[0] = 1;
d3arr[1] = 2;
d3arr[2] = 3;
// size
EXPECT_EQ(d3arr.size(), 3);
// const and non-const data
const auto cdata = d3arr.data();
EXPECT_EQ(cdata[0], 1);
d3arr.data()[0] = 10;
EXPECT_EQ(cdata[0], 10);
}

0 comments on commit 38939ff

Please sign in to comment.