[Bf-blender-cvs] [a4de314d209] functions: ArrayRef take_front and take_back methods

Jacques Lucke noreply at git.blender.org
Sat Jun 8 14:22:04 CEST 2019


Commit: a4de314d209458b0dd9645bc93266df499d75855
Author: Jacques Lucke
Date:   Sat Jun 8 13:55:21 2019 +0200
Branches: functions
https://developer.blender.org/rBa4de314d209458b0dd9645bc93266df499d75855

ArrayRef take_front and take_back methods

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

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 c30016b2a03..a114e945e32 100644
--- a/source/blender/blenlib/BLI_array_ref.hpp
+++ b/source/blender/blenlib/BLI_array_ref.hpp
@@ -72,6 +72,18 @@ template<typename T> class ArrayRef {
     return this->slice(0, this->size() - n);
   }
 
+  ArrayRef take_front(uint n) const
+  {
+    BLI_assert(n <= this->size());
+    return this->slice(0, n);
+  }
+
+  ArrayRef take_back(uint n) const
+  {
+    BLI_assert(n <= this->size());
+    return this->slice(this->size() - n, n);
+  }
+
   void fill(const T &element)
   {
     std::fill_n(m_start, m_size, element);
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc
index 4ff819bf797..df4e45fa46f 100644
--- a/tests/gtests/blenlib/BLI_array_ref_test.cc
+++ b/tests/gtests/blenlib/BLI_array_ref_test.cc
@@ -57,6 +57,24 @@ TEST(array_ref, DropFrontAll)
   EXPECT_EQ(slice.size(), 0);
 }
 
+TEST(array_ref, TakeFront)
+{
+  IntVector a = {4, 5, 6, 7};
+  auto slice = IntArrayRef(a).take_front(2);
+  EXPECT_EQ(slice.size(), 2);
+  EXPECT_EQ(slice[0], 4);
+  EXPECT_EQ(slice[1], 5);
+}
+
+TEST(array_ref, TakeBack)
+{
+  IntVector a = {5, 6, 7, 8};
+  auto slice = IntArrayRef(a).take_back(2);
+  EXPECT_EQ(slice.size(), 2);
+  EXPECT_EQ(slice[0], 7);
+  EXPECT_EQ(slice[1], 8);
+}
+
 TEST(array_ref, Slice)
 {
   IntVector a = {4, 5, 6, 7};



More information about the Bf-blender-cvs mailing list