[Bf-blender-cvs] [5b8feb038d9] functions: simple multivector data structure

Jacques Lucke noreply at git.blender.org
Mon Aug 5 13:07:47 CEST 2019


Commit: 5b8feb038d92004cc873d26c1f7a979671020033
Author: Jacques Lucke
Date:   Mon Aug 5 12:49:21 2019 +0200
Branches: functions
https://developer.blender.org/rB5b8feb038d92004cc873d26c1f7a979671020033

simple multivector data structure

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

A	source/blender/blenlib/BLI_multi_vector.hpp
M	source/blender/blenlib/CMakeLists.txt
A	tests/gtests/blenlib/BLI_multi_vector_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_multi_vector.hpp b/source/blender/blenlib/BLI_multi_vector.hpp
new file mode 100644
index 00000000000..2bd3675d880
--- /dev/null
+++ b/source/blender/blenlib/BLI_multi_vector.hpp
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "BLI_array_ref.hpp"
+#include "BLI_vector.hpp"
+
+namespace BLI {
+
+template<typename T, uint N = 4> class MultiVector {
+ private:
+  Vector<T, N> m_elements;
+  Vector<uint> m_starts;
+
+ public:
+  MultiVector() : m_starts({0})
+  {
+  }
+
+  void append(ArrayRef<T> values)
+  {
+    m_elements.extend(values);
+    m_starts.append(m_elements.size());
+  }
+
+  uint size() const
+  {
+    return m_starts.size() - 1;
+  }
+
+  ArrayRef<T> operator[](uint index)
+  {
+    uint start = m_starts[index];
+    uint one_after_end = m_starts[index + 1];
+    uint size = one_after_end - start;
+    return ArrayRef<T>(m_elements.begin() + start, size);
+  }
+};
+
+};  // namespace BLI
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 02a256c3dd5..7a6df49b573 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -260,6 +260,7 @@ set(SRC
   BLI_string_ref.hpp
   BLI_timeit.hpp
   BLI_vector_adaptor.hpp
+  BLI_multi_vector.hpp
 )
 
 set(LIB
diff --git a/tests/gtests/blenlib/BLI_multi_vector_test.cc b/tests/gtests/blenlib/BLI_multi_vector_test.cc
new file mode 100644
index 00000000000..5ce4a56001c
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_multi_vector_test.cc
@@ -0,0 +1,20 @@
+#include "testing/testing.h"
+#include "BLI_multi_vector.hpp"
+
+using namespace BLI;
+
+using IntMultiVector = MultiVector<int>;
+
+TEST(multi_vector, DefaultConstructor)
+{
+  IntMultiVector vec;
+  EXPECT_EQ(vec.size(), 0);
+}
+
+TEST(multi_vector, Append)
+{
+  IntMultiVector vec;
+  vec.append({4, 5, 6});
+  EXPECT_EQ(vec.size(), 1);
+  EXPECT_EQ(vec[0].size(), 3);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index dfb290249ab..608537d5204 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -59,6 +59,7 @@ BLENDER_TEST(BLI_math_color "bf_blenlib")
 BLENDER_TEST(BLI_math_geom "bf_blenlib")
 BLENDER_TEST(BLI_memiter "bf_blenlib")
 BLENDER_TEST(BLI_mempool "bf_blenlib")
+BLENDER_TEST(BLI_multi_vector "bf_blenlib")
 BLENDER_TEST(BLI_optional "bf_blenlib")
 BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}")
 BLENDER_TEST(BLI_polyfill_2d "bf_blenlib")



More information about the Bf-blender-cvs mailing list