[Bf-blender-cvs] [270f4cb1f0e] functions: SmallVector: Append N Times

Jacques Lucke noreply at git.blender.org
Mon Jun 17 15:52:48 CEST 2019


Commit: 270f4cb1f0e3b376f2fc0d8cccec067f813d310f
Author: Jacques Lucke
Date:   Mon Jun 17 15:13:19 2019 +0200
Branches: functions
https://developer.blender.org/rB270f4cb1f0e3b376f2fc0d8cccec067f813d310f

SmallVector: Append N Times

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

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 879563b7baa..9fb7d1f117b 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -128,6 +128,13 @@ template<typename T, uint N = 4> class SmallVector {
     m_size++;
   }
 
+  void append_n_times(const T &value, uint n)
+  {
+    this->reserve(m_size + n);
+    std::uninitialized_fill_n(this->end(), n, value);
+    m_size += n;
+  }
+
   void extend(const SmallVector &other)
   {
     this->extend(other.begin(), other.size());
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc b/tests/gtests/blenlib/BLI_small_vector_test.cc
index de3ab1d7832..5cb0d23461e 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -239,3 +239,16 @@ TEST(small_vector, Last)
   IntVector a{3, 5, 7};
   EXPECT_EQ(a.last(), 7);
 }
+
+TEST(small_vector, AppendNTimes)
+{
+  IntVector a;
+  a.append_n_times(5, 3);
+  a.append_n_times(2, 2);
+  EXPECT_EQ(a.size(), 5);
+  EXPECT_EQ(a[0], 5);
+  EXPECT_EQ(a[1], 5);
+  EXPECT_EQ(a[2], 5);
+  EXPECT_EQ(a[3], 2);
+  EXPECT_EQ(a[4], 2);
+}



More information about the Bf-blender-cvs mailing list