[Bf-blender-cvs] [c6ebe253378] functions-experimental-refactor: initial VirtualListListRef

Jacques Lucke noreply at git.blender.org
Tue Oct 29 16:54:46 CET 2019


Commit: c6ebe2533783a4e4a2de759e916f5b27f685a232
Author: Jacques Lucke
Date:   Tue Oct 29 15:00:47 2019 +0100
Branches: functions-experimental-refactor
https://developer.blender.org/rBc6ebe2533783a4e4a2de759e916f5b27f685a232

initial VirtualListListRef

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

A	source/blender/blenlib/BLI_virtual_list_list_ref.h
A	tests/gtests/blenlib/BLI_virtual_list_list_ref_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_virtual_list_list_ref.h b/source/blender/blenlib/BLI_virtual_list_list_ref.h
new file mode 100644
index 00000000000..5ba76186b80
--- /dev/null
+++ b/source/blender/blenlib/BLI_virtual_list_list_ref.h
@@ -0,0 +1,85 @@
+#ifndef __BLI_VIRTUAL_ARRAY_LIST_REF_H__
+#define __BLI_VIRTUAL_ARRAY_LIST_REF_H__
+
+#include "BLI_virtual_list_ref.h"
+
+namespace BLI {
+
+template<typename T> class VirtualListListRef {
+ private:
+  enum Category {
+    SingleArray,
+    ListOfStartPointers,
+  };
+
+  uint m_virtual_size;
+  Category m_category;
+
+  union {
+    struct {
+      const T *start;
+      uint size;
+    } single_array;
+    struct {
+      const T *const *starts;
+      const uint *sizes;
+    } list_of_start_pointers;
+  } m_data;
+
+ public:
+  VirtualListListRef()
+  {
+    m_virtual_size = 0;
+    m_category = ListOfStartPointers;
+    m_data.list_of_start_pointers.starts = nullptr;
+    m_data.list_of_start_pointers.sizes = nullptr;
+  }
+
+  static VirtualListListRef FromSingleArray(ArrayRef<T> array, uint virtual_list_size)
+  {
+    VirtualListListRef list;
+    list.m_virtual_size = virtual_list_size;
+    list.m_category = Category::SingleArray;
+    list.m_data.single_array.start = array.begin();
+    list.m_data.single_array.size = array.size();
+    return list;
+  }
+
+  static VirtualListListRef FromListOfStartPointers(ArrayRef<const T *> starts,
+                                                    ArrayRef<uint> sizes)
+  {
+    BLI_assert(starts.size() == sizes.size());
+    VirtualListListRef list;
+    list.m_virtual_size = starts.size();
+    list.m_category = Category::ListOfStartPointers;
+    list.m_data.list_of_start_pointers.starts = starts.begin();
+    list.m_data.list_of_start_pointers.sizes = sizes.begin();
+    return list;
+  }
+
+  uint size() const
+  {
+    return m_virtual_size;
+  }
+
+  VirtualListRef<T> operator[](uint index) const
+  {
+    BLI_assert(index < m_virtual_size);
+
+    switch (m_category) {
+      case Category::SingleArray:
+        return VirtualListRef<T>::FromFullArray(
+            ArrayRef<T>(m_data.single_array.start, m_data.single_array.size));
+      case Category::ListOfStartPointers:
+        return VirtualListRef<T>::FromFullArray(m_data.list_of_start_pointers.starts[index],
+                                                m_data.list_of_start_pointers.sizes[index]);
+    }
+
+    BLI_assert(false);
+    return {};
+  }
+};
+
+}  // namespace BLI
+
+#endif /* __BLI_VIRTUAL_ARRAY_LIST_REF_H__ */
diff --git a/tests/gtests/blenlib/BLI_virtual_list_list_ref_test.cc b/tests/gtests/blenlib/BLI_virtual_list_list_ref_test.cc
new file mode 100644
index 00000000000..2a34b867416
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_virtual_list_list_ref_test.cc
@@ -0,0 +1,60 @@
+#include "testing/testing.h"
+#include "BLI_virtual_list_list_ref.h"
+#include <vector>
+#include <array>
+
+using namespace BLI;
+
+TEST(virtual_list_list_ref, DefaultConstruct)
+{
+  VirtualListListRef<int> list;
+  EXPECT_EQ(list.size(), 0);
+}
+
+TEST(virtual_list_list_ref, FromSingleArray)
+{
+  std::array<int, 3> values = {3, 4, 5};
+  VirtualListListRef<int> list = VirtualListListRef<int>::FromSingleArray(values, 6);
+  EXPECT_EQ(list.size(), 6);
+
+  EXPECT_EQ(list[0].size(), 3);
+  EXPECT_EQ(list[1].size(), 3);
+  EXPECT_EQ(list[2].size(), 3);
+  EXPECT_EQ(list[3].size(), 3);
+  EXPECT_EQ(list[4].size(), 3);
+  EXPECT_EQ(list[5].size(), 3);
+
+  EXPECT_EQ(list[2][0], 3);
+  EXPECT_EQ(list[2][1], 4);
+  EXPECT_EQ(list[2][2], 5);
+}
+
+TEST(virtual_list_list_ref, FromListOfStartPointers)
+{
+  std::array<int, 3> values1 = {1, 2, 3};
+  std::array<int, 2> values2 = {4, 5};
+  std::array<int, 4> values3 = {6, 7, 8, 9};
+
+  std::array<const int *, 3> starts = {values1.data(), values2.data(), values3.data()};
+  std::array<uint, 3> sizes = {values1.size(), values2.size(), values3.size()};
+
+  VirtualListListRef<int> list = VirtualListListRef<int>::FromListOfStartPointers(starts, sizes);
+
+  EXPECT_EQ(list.size(), 3);
+
+  EXPECT_EQ(list[0].size(), 3);
+  EXPECT_EQ(list[1].size(), 2);
+  EXPECT_EQ(list[2].size(), 4);
+
+  EXPECT_EQ(list[0][0], 1);
+  EXPECT_EQ(list[0][1], 2);
+  EXPECT_EQ(list[0][2], 3);
+
+  EXPECT_EQ(list[1][0], 4);
+  EXPECT_EQ(list[1][1], 5);
+
+  EXPECT_EQ(list[2][0], 6);
+  EXPECT_EQ(list[2][1], 7);
+  EXPECT_EQ(list[2][2], 8);
+  EXPECT_EQ(list[2][3], 9);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index b27ea37ecc8..3b401da1aaf 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -78,6 +78,7 @@ BLENDER_TEST(BLI_task "bf_blenlib;bf_intern_numaapi")
 BLENDER_TEST(BLI_vector "bf_blenlib")
 BLENDER_TEST(BLI_vector_adaptor "bf_blenlib")
 BLENDER_TEST(BLI_vector_set "bf_blenlib")
+BLENDER_TEST(BLI_virtual_list_list_ref "bf_blenlib")
 BLENDER_TEST(BLI_virtual_list_ref "bf_blenlib")
 
 BLENDER_TEST_PERFORMANCE(BLI_ghash_performance "bf_blenlib")



More information about the Bf-blender-cvs mailing list