Skip to content

Commit f2474bd

Browse files
authored
Merge pull request InsightSoftwareConsortium#5709 from N-Dekker/FixedArray-front-back
2 parents d66197a + 0fe7645 commit f2474bd

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Modules/Core/Common/include/itkFixedArray.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,31 @@ class ITK_TEMPLATE_EXPORT FixedArray
313313
return m_InternalArray[index];
314314
}
315315
/** @ITKEndGrouping */
316+
317+
[[nodiscard]] constexpr reference
318+
front()
319+
{
320+
return m_InternalArray[0];
321+
}
322+
323+
[[nodiscard]] constexpr const_reference
324+
front() const
325+
{
326+
return m_InternalArray[0];
327+
}
328+
329+
[[nodiscard]] constexpr reference
330+
back()
331+
{
332+
return m_InternalArray[VLength - 1];
333+
}
334+
335+
[[nodiscard]] constexpr const_reference
336+
back() const
337+
{
338+
return m_InternalArray[VLength - 1];
339+
}
340+
316341
/** Return a pointer to the data. */
317342
ValueType *
318343
GetDataPointer()

Modules/Core/Common/test/itkFixedArrayGTest.cxx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,12 @@ TEST(FixedArray, EmptyIsFalse)
368368

369369
EXPECT_FALSE(itk::FixedArray<int>({ 0, 1, 2 }).empty());
370370
}
371+
372+
373+
// Tests front() and back().
374+
TEST(FixedArray, CheckFrontAndBack)
375+
{
376+
itk::RangeGTestUtilities::CheckFrontAndBack(itk::FixedArray<int, 1>{});
377+
itk::RangeGTestUtilities::CheckFrontAndBack(itk::FixedArray<int>({ 0, 1, 2 }));
378+
itk::RangeGTestUtilities::CheckFrontAndBack(itk::FixedArray<double>::Filled(std::numeric_limits<double>::max()));
379+
}

Modules/Core/Common/test/itkRangeGTestUtilities.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ class RangeGTestUtilities
223223
}
224224

225225

226+
template <typename TRange>
227+
static void
228+
CheckFrontAndBack(TRange && range)
229+
{
230+
ASSERT_FALSE(std::empty(range));
231+
232+
// Check that front() is at the begin, and back() is just before the end.
233+
EXPECT_EQ(&range.front(), &*std::cbegin(range));
234+
EXPECT_EQ(&range.back(), &*std::prev(std::cend(range)));
235+
236+
// Check that const and non-const overloads return a reference to the same element.
237+
EXPECT_EQ(&std::as_const(range).front(), &range.front());
238+
EXPECT_EQ(&std::as_const(range).back(), &range.back());
239+
}
240+
241+
226242
// Checks that each element of the specified range is zero.
227243
template <typename TRange>
228244
static void

0 commit comments

Comments
 (0)