[Bf-blender-cvs] [c0b36372d0b] functions: easy access last element in vector

Jacques Lucke noreply at git.blender.org
Sat Jun 8 14:21:48 CEST 2019


Commit: c0b36372d0b3b31a935d34e71ad7f25f66f00ded
Author: Jacques Lucke
Date:   Sat Jun 8 12:37:39 2019 +0200
Branches: functions
https://developer.blender.org/rBc0b36372d0b3b31a935d34e71ad7f25f66f00ded

easy access last element in vector

===================================================================

M	source/blender/blenlib/BLI_small_vector.hpp
M	tests/gtests/blenlib/BLI_small_vector_test.cc

===================================================================

diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 5cc14347d56..879563b7baa 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -140,6 +140,12 @@ template<typename T, uint N = 4> class SmallVector {
     m_size += amount;
   }
 
+  T &last() const
+  {
+    BLI_assert(m_size > 0);
+    return m_elements[m_size - 1];
+  }
+
   void fill(const T &value)
   {
     for (uint i = 0; i < m_size; i++) {
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc b/tests/gtests/blenlib/BLI_small_vector_test.cc
index ca35520bbd5..de3ab1d7832 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -233,3 +233,9 @@ TEST(small_vector, ExtendArray)
   EXPECT_EQ(a[0], 3);
   EXPECT_EQ(a[1], 4);
 }
+
+TEST(small_vector, Last)
+{
+  IntVector a{3, 5, 7};
+  EXPECT_EQ(a.last(), 7);
+}



More information about the Bf-blender-cvs mailing list