[Bf-blender-cvs] [a256d4bf36d] temp-T96710-pbvh-pixels: BLI: add CPPType utility to copy elements to a shorter array

Jacques Lucke noreply at git.blender.org
Fri Apr 8 11:07:47 CEST 2022


Commit: a256d4bf36d1497759540b7b8ee52081c2a0a00a
Author: Jacques Lucke
Date:   Thu Apr 7 09:34:07 2022 +0200
Branches: temp-T96710-pbvh-pixels
https://developer.blender.org/rBa256d4bf36d1497759540b7b8ee52081c2a0a00a

BLI: add CPPType utility to copy elements to a shorter array

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

M	source/blender/blenlib/BLI_cpp_type.hh
M	source/blender/blenlib/BLI_cpp_type_make.hh
M	source/blender/blenlib/tests/BLI_cpp_type_test.cc

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

diff --git a/source/blender/blenlib/BLI_cpp_type.hh b/source/blender/blenlib/BLI_cpp_type.hh
index 9453eb89a2e..dd14cca7a0b 100644
--- a/source/blender/blenlib/BLI_cpp_type.hh
+++ b/source/blender/blenlib/BLI_cpp_type.hh
@@ -118,9 +118,11 @@ class CPPType : NonCopyable, NonMovable {
 
   void (*copy_assign_)(const void *src, void *dst) = nullptr;
   void (*copy_assign_indices_)(const void *src, void *dst, IndexMask mask) = nullptr;
+  void (*copy_assign_compressed_)(const void *src, void *dst, IndexMask mask) = nullptr;
 
   void (*copy_construct_)(const void *src, void *dst) = nullptr;
   void (*copy_construct_indices_)(const void *src, void *dst, IndexMask mask) = nullptr;
+  void (*copy_construct_compressed_)(const void *src, void *dst, IndexMask mask) = nullptr;
 
   void (*move_assign_)(void *src, void *dst) = nullptr;
   void (*move_assign_indices_)(void *src, void *dst, IndexMask mask) = nullptr;
@@ -408,6 +410,18 @@ class CPPType : NonCopyable, NonMovable {
     copy_assign_indices_(src, dst, mask);
   }
 
+  /**
+   * Similar to #copy_assign_indices, but does not leave gaps in the #dst array.
+   */
+  void copy_assign_compressed(const void *src, void *dst, IndexMask mask) const
+  {
+    BLI_assert(mask.size() == 0 || src != dst);
+    BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(src));
+    BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(dst));
+
+    copy_assign_compressed_(src, dst, mask);
+  }
+
   /**
    * Copy an instance of this type from src to dst.
    *
@@ -439,6 +453,18 @@ class CPPType : NonCopyable, NonMovable {
     copy_construct_indices_(src, dst, mask);
   }
 
+  /**
+   * Similar to #copy_construct_indices, but does not leave gaps in the #dst array.
+   */
+  void copy_construct_compressed(const void *src, void *dst, IndexMask mask) const
+  {
+    BLI_assert(mask.size() == 0 || src != dst);
+    BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(src));
+    BLI_assert(mask.size() == 0 || this->pointer_can_point_to_instance(dst));
+
+    copy_construct_compressed_(src, dst, mask);
+  }
+
   /**
    * Move an instance of this type from src to dst.
    *
diff --git a/source/blender/blenlib/BLI_cpp_type_make.hh b/source/blender/blenlib/BLI_cpp_type_make.hh
index 2612348075b..b0dbbff7ca8 100644
--- a/source/blender/blenlib/BLI_cpp_type_make.hh
+++ b/source/blender/blenlib/BLI_cpp_type_make.hh
@@ -51,6 +51,17 @@ template<typename T> void copy_assign_indices_cb(const void *src, void *dst, Ind
 
   mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; });
 }
+template<typename T> void copy_assign_compressed_cb(const void *src, void *dst, IndexMask mask)
+{
+  const T *src_ = static_cast<const T *>(src);
+  T *dst_ = static_cast<T *>(dst);
+
+  mask.to_best_mask_type([&](auto best_mask) {
+    for (const int64_t i : IndexRange(best_mask.size())) {
+      dst_[i] = src_[best_mask[i]];
+    }
+  });
+}
 
 template<typename T> void copy_construct_cb(const void *src, void *dst)
 {
@@ -63,6 +74,17 @@ template<typename T> void copy_construct_indices_cb(const void *src, void *dst,
 
   mask.foreach_index([&](int64_t i) { new (dst_ + i) T(src_[i]); });
 }
+template<typename T> void copy_construct_compressed_cb(const void *src, void *dst, IndexMask mask)
+{
+  const T *src_ = static_cast<const T *>(src);
+  T *dst_ = static_cast<T *>(dst);
+
+  mask.to_best_mask_type([&](auto best_mask) {
+    for (const int64_t i : IndexRange(best_mask.size())) {
+      new (dst_ + i) T(src_[best_mask[i]]);
+    }
+  });
+}
 
 template<typename T> void move_assign_cb(void *src, void *dst)
 {
@@ -208,10 +230,12 @@ CPPType::CPPType(CPPTypeParam<T, Flags> /* unused */, StringRef debug_name)
   if constexpr (std::is_copy_assignable_v<T>) {
     copy_assign_ = copy_assign_cb<T>;
     copy_assign_indices_ = copy_assign_indices_cb<T>;
+    copy_assign_compressed_ = copy_assign_compressed_cb<T>;
   }
   if constexpr (std::is_copy_constructible_v<T>) {
     copy_construct_ = copy_construct_cb<T>;
     copy_construct_indices_ = copy_construct_indices_cb<T>;
+    copy_construct_compressed_ = copy_construct_compressed_cb<T>;
   }
   if constexpr (std::is_move_assignable_v<T>) {
     move_assign_ = move_assign_cb<T>;
diff --git a/source/blender/blenlib/tests/BLI_cpp_type_test.cc b/source/blender/blenlib/tests/BLI_cpp_type_test.cc
index f00767eda8c..6a59bedc649 100644
--- a/source/blender/blenlib/tests/BLI_cpp_type_test.cc
+++ b/source/blender/blenlib/tests/BLI_cpp_type_test.cc
@@ -381,4 +381,14 @@ TEST(cpp_type, ToStaticType)
   EXPECT_EQ(types[1], &CPPType::get<float>());
 }
 
+TEST(cpp_type, CopyAssignCompressed)
+{
+  std::array<std::string, 5> array = {"a", "b", "c", "d", "e"};
+  std::array<std::string, 3> array_compressed;
+  CPPType::get<std::string>().copy_assign_compressed(&array, &array_compressed, {0, 2, 3});
+  EXPECT_EQ(array_compressed[0], "a");
+  EXPECT_EQ(array_compressed[1], "c");
+  EXPECT_EQ(array_compressed[2], "d");
+}
+
 }  // namespace blender::tests



More information about the Bf-blender-cvs mailing list