[Bf-blender-cvs] [f0cc032f836] functions: construct SmallVector from arbitrary containers

Jacques Lucke noreply at git.blender.org
Tue Jul 16 18:20:09 CEST 2019


Commit: f0cc032f8361938d24dad3479815f553ecbb5df2
Author: Jacques Lucke
Date:   Tue Jul 16 13:04:26 2019 +0200
Branches: functions
https://developer.blender.org/rBf0cc032f8361938d24dad3479815f553ecbb5df2

construct SmallVector from arbitrary containers

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

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 9e344a2617e..53e9d0ccf7f 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -108,6 +108,19 @@ template<typename T, uint N = 4> class SmallVector {
     m_size = values.size();
   }
 
+  /**
+   * Create a vector from any container. It must be possible to use the container in a range-for
+   * loop.
+   */
+  template<typename ContainerT> static SmallVector FromContainer(const ContainerT &container)
+  {
+    SmallVector vector;
+    for (const auto &value : container) {
+      vector.append(value);
+    }
+    return vector;
+  }
+
   /**
    * Create a vector from a mapped array ref. This can e.g. be used to create vectors from
    * map.keys() for map.values().
diff --git a/tests/gtests/blenlib/BLI_small_vector_test.cc b/tests/gtests/blenlib/BLI_small_vector_test.cc
index 6aa3915720e..772d6ca979a 100644
--- a/tests/gtests/blenlib/BLI_small_vector_test.cc
+++ b/tests/gtests/blenlib/BLI_small_vector_test.cc
@@ -1,6 +1,7 @@
 #include "testing/testing.h"
 #include "BLI_small_vector.hpp"
 #include "BLI_small_map.hpp"
+#include <forward_list>
 
 using BLI::SmallMap;
 using BLI::SmallVector;
@@ -98,6 +99,20 @@ TEST(small_vector, IntrusiveListBaseConstructor)
   delete vec[2];
 }
 
+TEST(small_vector, ContainerConstructor)
+{
+  std::forward_list<int> list;
+  list.push_front(3);
+  list.push_front(1);
+  list.push_front(5);
+
+  IntVector vec = IntVector::FromContainer(list);
+  EXPECT_EQ(vec.size(), 3);
+  EXPECT_EQ(vec[0], 5);
+  EXPECT_EQ(vec[1], 1);
+  EXPECT_EQ(vec[2], 3);
+}
+
 TEST(small_vector, CopyConstructor)
 {
   IntVector vec1 = {1, 2, 3};



More information about the Bf-blender-cvs mailing list