[Bf-blender-cvs] [b04aed8db67] functions-experimental-refactor: experimental VirtualListRef

Jacques Lucke noreply at git.blender.org
Sun Oct 20 15:55:50 CEST 2019


Commit: b04aed8db6763ecdec2cd49b11bf2749c270c37c
Author: Jacques Lucke
Date:   Sun Oct 20 15:11:52 2019 +0200
Branches: functions-experimental-refactor
https://developer.blender.org/rBb04aed8db6763ecdec2cd49b11bf2749c270c37c

experimental VirtualListRef

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

A	source/blender/blenlib/BLI_virtual_list_ref.h
D	tests/gtests/blenlib/BLI_array_or_single_ref_test.cc
A	tests/gtests/blenlib/BLI_virtual_list_ref_test.cc
M	tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_virtual_list_ref.h b/source/blender/blenlib/BLI_virtual_list_ref.h
new file mode 100644
index 00000000000..9a236cefb68
--- /dev/null
+++ b/source/blender/blenlib/BLI_virtual_list_ref.h
@@ -0,0 +1,112 @@
+#ifndef __BLI_VIRTUAL_LIST_REF_H__
+#define __BLI_VIRTUAL_LIST_REF_H__
+
+#include "BLI_array_ref.h"
+
+namespace BLI {
+
+template<typename T> class VirtualListRef {
+ private:
+  enum Category {
+    Single,
+    FullArray,
+    RepeatedArray,
+  };
+
+  uint m_virtual_size;
+  Category m_category;
+
+  union {
+    struct {
+      const T *data;
+    } single;
+    struct {
+      const T *data;
+    } full_array;
+    struct {
+      const T *data;
+      uint real_size;
+    } repeated_array;
+  } m_data;
+
+ public:
+  VirtualListRef()
+  {
+    m_virtual_size = 0;
+    m_category = FullArray;
+    m_data.single.data = nullptr;
+  }
+
+  static VirtualListRef FromSingle(const T *data, uint size)
+  {
+    VirtualListRef list;
+    list.m_virtual_size = size;
+    list.m_category = Single;
+    list.m_data.single.data = data;
+    return list;
+  }
+
+  static VirtualListRef FromFullArray(const T *data, uint size)
+  {
+    VirtualListRef list;
+    list.m_virtual_size = size;
+    list.m_category = FullArray;
+    list.m_data.full_array.data = data;
+    return list;
+  }
+
+  static VirtualListRef FromFullArray(ArrayRef<T> array)
+  {
+    return VirtualListRef::FromFullArray(array.begin(), array.size());
+  }
+
+  static VirtualListRef FromRepeatedArray(const T *data, uint real_size, uint virtual_size)
+  {
+    BLI_assert(virtual_size == 0 || real_size > 0);
+
+    VirtualListRef list;
+    list.m_virtual_size = virtual_size;
+    list.m_category = RepeatedArray;
+    list.m_data.repeated_array.data = data;
+    list.m_data.repeated_array.real_size = real_size;
+    return list;
+  }
+
+  static VirtualListRef FromRepeatedArray(ArrayRef<T> array, uint virtual_size)
+  {
+    return VirtualListRef::FromRepeatedArray(array.begin(), array.size(), virtual_size);
+  }
+
+  const T &operator[](uint index) const
+  {
+    BLI_assert(index < m_virtual_size);
+    switch (m_category) {
+      case Single:
+        return *m_data.single.data;
+      case FullArray:
+        return m_data.full_array.data[index];
+      case RepeatedArray:
+        uint real_index = index % m_data.repeated_array.real_size;
+        return m_data.repeated_array.data[real_index];
+    }
+    BLI_assert(false);
+    return *m_data.single.data;
+  }
+
+  uint size() const
+  {
+    return m_virtual_size;
+  }
+
+  void materialize(MutableArrayRef<T> dst) const
+  {
+    BLI_assert(dst.size() == m_virtual_size);
+    for (uint i = 0; i < m_virtual_size; i++) {
+      dst[i] = (*this)[i];
+    }
+  }
+};
+
+}  // namespace BLI
+
+#endif /* __BLI_VIRTUAL_LIST_REF_H__ */
diff --git a/tests/gtests/blenlib/BLI_array_or_single_ref_test.cc b/tests/gtests/blenlib/BLI_array_or_single_ref_test.cc
deleted file mode 100644
index 1a30fec2867..00000000000
--- a/tests/gtests/blenlib/BLI_array_or_single_ref_test.cc
+++ /dev/null
@@ -1,10 +0,0 @@
-#include "testing/testing.h"
-#include "BLI_array_or_single_ref.h"
-
-using namespace BLI;
-
-TEST(array_ref, DefaultConstruct)
-{
-  ArrayOrSingleRef<int> ref;
-  EXPECT_EQ(ref.size(), 0);
-}
diff --git a/tests/gtests/blenlib/BLI_virtual_list_ref_test.cc b/tests/gtests/blenlib/BLI_virtual_list_ref_test.cc
new file mode 100644
index 00000000000..dd1350c27ad
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_virtual_list_ref_test.cc
@@ -0,0 +1,45 @@
+#include "testing/testing.h"
+#include "BLI_virtual_list_ref.h"
+#include <vector>
+#include <array>
+
+using namespace BLI;
+
+TEST(virtual_list_ref, DefaultConstruct)
+{
+  VirtualListRef<int> list;
+  EXPECT_EQ(list.size(), 0);
+}
+
+TEST(virtual_list_ref, FromSingle)
+{
+  int value = 5;
+  auto list = VirtualListRef<int>::FromSingle(&value, 3);
+  EXPECT_EQ(list.size(), 3);
+  EXPECT_EQ(list[0], 5);
+  EXPECT_EQ(list[1], 5);
+  EXPECT_EQ(list[2], 5);
+}
+
+TEST(virtual_list_ref, FromFullArray)
+{
+  std::vector<int> values = {5, 6, 7, 8};
+  auto list = VirtualListRef<int>::FromFullArray(values);
+  EXPECT_EQ(list.size(), 4);
+  EXPECT_EQ(list[0], 5);
+  EXPECT_EQ(list[1], 6);
+  EXPECT_EQ(list[2], 7);
+  EXPECT_EQ(list[3], 8);
+}
+
+TEST(virtual_list_ref, FromRepeatedArray)
+{
+  std::vector<int> values = {3, 4};
+  auto list = VirtualListRef<int>::FromRepeatedArray(values, 5);
+  EXPECT_EQ(list.size(), 5);
+  EXPECT_EQ(list[0], 3);
+  EXPECT_EQ(list[1], 4);
+  EXPECT_EQ(list[2], 3);
+  EXPECT_EQ(list[3], 4);
+  EXPECT_EQ(list[4], 3);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt
index 667b8b74a68..b27ea37ecc8 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -39,7 +39,6 @@ else()
 endif()
 
 BLENDER_TEST(BLI_array "bf_blenlib")
-BLENDER_TEST(BLI_array_or_single_ref "bf_blenlib")
 BLENDER_TEST(BLI_array_ref "bf_blenlib")
 BLENDER_TEST(BLI_array_store "bf_blenlib")
 BLENDER_TEST(BLI_array_utils "bf_blenlib")
@@ -79,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_ref "bf_blenlib")
 
 BLENDER_TEST_PERFORMANCE(BLI_ghash_performance "bf_blenlib")
 BLENDER_TEST_PERFORMANCE(BLI_task_performance "bf_blenlib")



More information about the Bf-blender-cvs mailing list