From b2ad149c7481a3a82d4d3d46e0678df0b6daab32 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 7 Apr 2022 09:34:07 +0200 Subject: BLI: add CPPType utility to copy elements to a shorter array --- source/blender/blenlib/BLI_cpp_type.hh | 26 +++++++++++++++++++++++ source/blender/blenlib/BLI_cpp_type_make.hh | 24 +++++++++++++++++++++ source/blender/blenlib/tests/BLI_cpp_type_test.cc | 10 +++++++++ 3 files changed, 60 insertions(+) (limited to 'source') 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 void copy_assign_indices_cb(const void *src, void *dst, Ind mask.foreach_index([&](int64_t i) { dst_[i] = src_[i]; }); } +template void copy_assign_compressed_cb(const void *src, void *dst, IndexMask mask) +{ + const T *src_ = static_cast(src); + T *dst_ = static_cast(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 void copy_construct_cb(const void *src, void *dst) { @@ -63,6 +74,17 @@ template void copy_construct_indices_cb(const void *src, void *dst, mask.foreach_index([&](int64_t i) { new (dst_ + i) T(src_[i]); }); } +template void copy_construct_compressed_cb(const void *src, void *dst, IndexMask mask) +{ + const T *src_ = static_cast(src); + T *dst_ = static_cast(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 void move_assign_cb(void *src, void *dst) { @@ -208,10 +230,12 @@ CPPType::CPPType(CPPTypeParam /* unused */, StringRef debug_name) if constexpr (std::is_copy_assignable_v) { copy_assign_ = copy_assign_cb; copy_assign_indices_ = copy_assign_indices_cb; + copy_assign_compressed_ = copy_assign_compressed_cb; } if constexpr (std::is_copy_constructible_v) { copy_construct_ = copy_construct_cb; copy_construct_indices_ = copy_construct_indices_cb; + copy_construct_compressed_ = copy_construct_compressed_cb; } if constexpr (std::is_move_assignable_v) { move_assign_ = move_assign_cb; 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()); } +TEST(cpp_type, CopyAssignCompressed) +{ + std::array array = {"a", "b", "c", "d", "e"}; + std::array array_compressed; + CPPType::get().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 -- cgit v1.2.3