Commit: 8dd4082b27e8f39a7c9c823f6f8ba97da29aab65 Author: Jacques Lucke Date: Sat Jun 29 15:36:13 2019 +0200 Branches: functions https://developer.blender.org/rB8dd4082b27e8f39a7c9c823f6f8ba97da29aab65
easy access of first and last element in array ref =================================================================== M source/blender/blenlib/BLI_array_ref.hpp M tests/gtests/blenlib/BLI_array_ref_test.cc =================================================================== diff --git a/source/blender/blenlib/BLI_array_ref.hpp b/source/blender/blenlib/BLI_array_ref.hpp index 1cfd2cfdac7..b41c2e78782 100644 --- a/source/blender/blenlib/BLI_array_ref.hpp +++ b/source/blender/blenlib/BLI_array_ref.hpp @@ -210,6 +210,26 @@ template<typename T> class ArrayRef { } return counter; } + + /** + * Return a reference to the first element in the array. + * Asserts when the array is empty. + */ + T &first() + { + BLI_assert(m_size > 0); + return m_start[0]; + } + + /** + * Return a reference to the last elemeent in the array. + * Asserts when the array is empty. + */ + T &last() + { + BLI_assert(m_size > 0); + return m_start[m_size - 1]; + } }; template<typename ArrayT, typename ValueT, ValueT (*GetValue)(ArrayT &item)> class MappedArrayRef { diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc index af4c31c6018..b73dd60fd3f 100644 --- a/tests/gtests/blenlib/BLI_array_ref_test.cc +++ b/tests/gtests/blenlib/BLI_array_ref_test.cc @@ -211,3 +211,19 @@ TEST(array_ref, CopyTo) EXPECT_EQ(b[1], 6); EXPECT_EQ(b[2], 7); } + +TEST(array_ref, FirstLast) +{ + std::array<int, 4> a = {6, 7, 8, 9}; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.first(), 6); + EXPECT_EQ(a_ref.last(), 9); +} + +TEST(array_ref, FirstLast_OneElement) +{ + int a = 3; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.first(), 3); + EXPECT_EQ(a_ref.last(), 3); +} _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs