Blender V5.3
BLI_virtual_array.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
27
28#include <functional>
29#include <optional>
30
31#include "BLI_any_derived.hh"
32#include "BLI_array.hh"
34#include "BLI_index_mask.hh"
35#include "BLI_span.hh"
36
37namespace blender {
38
40class GVArray;
41class GVMutableArray;
42
48 enum class Type : uint8_t {
49 /* Is not one of the common special types below. */
53 };
54
56
58 bool may_have_ownership = true;
59
64 const void *data;
65
66 CommonVArrayInfo() = default;
67 CommonVArrayInfo(const Type _type, const bool _may_have_ownership, const void *_data)
68 : type(_type), may_have_ownership(_may_have_ownership), data(_data)
69 {
70 }
71};
72
77template<typename T> class VArrayImpl {
78 protected:
84
85 public:
87 {
88 BLI_assert(size_ >= 0);
89 }
90
91 virtual ~VArrayImpl() = default;
92
93 int64_t size() const
94 {
95 return size_;
96 }
97
102 virtual T get(int64_t index) const = 0;
103
105 {
106 return {};
107 }
108
113 virtual void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const
114 {
115 if constexpr (std::is_trivially_copyable_v<T>) {
116 mask.foreach_index([&](const int64_t i) { dst[i] = this->get(i); });
117 }
118 else {
119 if (dst_is_uninitialized) {
120 mask.foreach_index([&](const int64_t i) { new (dst + i) T(this->get(i)); });
121 }
122 else {
123 mask.foreach_index([&](const int64_t i) { dst[i] = this->get(i); });
124 }
125 }
126 }
127
134 T *dst,
135 const bool dst_is_uninitialized) const
136 {
137 if constexpr (std::is_trivially_copyable_v<T>) {
138 mask.foreach_index([&](const int64_t i, const int64_t pos) { dst[pos] = this->get(i); });
139 }
140 else {
141 if (dst_is_uninitialized) {
142 mask.foreach_index(
143 [&](const int64_t i, const int64_t pos) { new (dst + pos) T(this->get(i)); });
144 }
145 else {
146 mask.foreach_index([&](const int64_t i, const int64_t pos) { dst[pos] = this->get(i); });
147 }
148 }
149 }
150
157 virtual bool try_assign_GVArray(GVArray & /*varray*/) const
158 {
159 return false;
160 }
161};
162
164template<typename T> class VMutableArrayImpl : public VArrayImpl<T> {
165 public:
167
171 virtual void set(int64_t index, T value) = 0;
172
176 virtual void set_all(Span<T> src)
177 {
178 const CommonVArrayInfo info = this->common_info();
181 src.data(), this->size_, const_cast<T *>(static_cast<const T *>(info.data)));
182 }
183 else {
184 const int64_t size = this->size_;
185 for (int64_t i = 0; i < size; i++) {
186 this->set(i, src[i]);
187 }
188 }
189 }
190
194 virtual bool try_assign_GVMutableArray(GVMutableArray & /*varray*/) const
195 {
196 return false;
197 }
198};
199
204template<typename T> class VArrayImpl_For_Span : public VMutableArrayImpl<T> {
205 protected:
206 T *data_ = nullptr;
207
208 public:
213
214 protected:
216
217 T get(const int64_t index) const final
218 {
219 return data_[index];
220 }
221
222 void set(const int64_t index, T value) final
223 {
224 data_[index] = value;
225 }
226
228 {
230 }
231
232 void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
233 {
234 if constexpr (std::is_trivially_copyable_v<T>) {
236 }
237 else {
238 if (dst_is_uninitialized) {
239 mask.foreach_index([&](const int64_t i) { new (dst + i) T(data_[i]); });
240 }
241 else {
242 mask.foreach_index([&](const int64_t i) { dst[i] = data_[i]; });
243 }
244 }
245 }
246
248 T *dst,
249 const bool dst_is_uninitialized) const override
250 {
251 if constexpr (std::is_trivially_copyable_v<T>) {
253 }
254 else {
255 if (dst_is_uninitialized) {
256 mask.foreach_index(
257 [&](const int64_t i, const int64_t pos) { new (dst + pos) T(data_[i]); });
258 }
259 else {
260 mask.foreach_index([&](const int64_t i, const int64_t pos) { dst[pos] = data_[i]; });
261 }
262 }
263 }
264};
265
270template<typename T> class VArrayImpl_For_Span_final final : public VArrayImpl_For_Span<T> {
271 public:
273
275 /* Cast const away, because the implementation for const and non const spans is shared. */
276 : VArrayImpl_For_Span<T>({const_cast<T *>(data.data()), data.size()})
277 {
278 }
279
280 private:
281 CommonVArrayInfo common_info() const final
282 {
284 }
285};
286
287template<typename T>
289
296template<typename Container, typename T = typename Container::value_type>
298 private:
299 Container container_;
300
301 public:
302 VArrayImpl_For_ArrayContainer(Container container)
303 : VArrayImpl_For_Span<T>(int64_t(container.size())), container_(std::move(container))
304 {
305 this->data_ = const_cast<T *>(container_.data());
306 }
307};
308
314template<typename T> class VArrayImpl_For_Single final : public VArrayImpl<T> {
315 private:
316 T value_;
317
318 public:
320 : VArrayImpl<T>(size), value_(std::move(value))
321 {
322 }
323
324 protected:
325 T get(const int64_t /*index*/) const override
326 {
327 return value_;
328 }
329
331 {
333 }
334
335 void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
336 {
337 if constexpr (std::is_trivially_copyable_v<T>) {
338 index_mask::detail::fill(dst, value_, mask);
339 }
340 else {
341 if (dst_is_uninitialized) {
342 mask.foreach_index([&](const int64_t i) { new (dst + i) T(value_); });
343 }
344 else {
345 mask.foreach_index([&](const int64_t i) { dst[i] = value_; });
346 }
347 }
348 }
349
351 T *dst,
352 const bool dst_is_uninitialized) const override
353 {
354 if constexpr (std::is_trivially_copyable_v<T>) {
355 initialized_fill_n(dst, mask.size(), value_);
356 }
357 else {
358 if (dst_is_uninitialized) {
359 uninitialized_fill_n(dst, mask.size(), value_);
360 }
361 else {
362 initialized_fill_n(dst, mask.size(), value_);
363 }
364 }
365 }
366};
367
368template<typename T>
370
375template<typename T, typename GetFunc> class VArrayImpl_For_Func final : public VArrayImpl<T> {
376 private:
377 GetFunc get_func_;
378
379 public:
380 VArrayImpl_For_Func(const int64_t size, GetFunc get_func)
381 : VArrayImpl<T>(size), get_func_(std::move(get_func))
382 {
383 }
384
385 private:
386 T get(const int64_t index) const override
387 {
388 return get_func_(index);
389 }
390
391 void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
392 {
393 if constexpr (std::is_trivially_copyable_v<T>) {
394 mask.foreach_index([&](const int64_t i) { dst[i] = get_func_(i); });
395 }
396 else {
397 if (dst_is_uninitialized) {
398 mask.foreach_index([&](const int64_t i) { new (dst + i) T(get_func_(i)); });
399 }
400 else {
401 mask.foreach_index([&](const int64_t i) { dst[i] = get_func_(i); });
402 }
403 }
404 }
405
406 void materialize_compressed(const IndexMask &mask,
407 T *dst,
408 const bool dst_is_uninitialized) const override
409 {
410 if constexpr (std::is_trivially_copyable_v<T>) {
411 mask.foreach_index([&](const int64_t i, const int64_t pos) { dst[pos] = get_func_(i); });
412 }
413 else {
414 if (dst_is_uninitialized) {
415 mask.foreach_index(
416 [&](const int64_t i, const int64_t pos) { new (dst + pos) T(get_func_(i)); });
417 }
418 else {
419 mask.foreach_index([&](const int64_t i, const int64_t pos) { dst[pos] = get_func_(i); });
420 }
421 }
422 }
423};
424
428template<typename StructT,
429 typename ElemT,
430 ElemT (*GetFunc)(const StructT &),
431 void (*SetFunc)(StructT &, ElemT) = nullptr>
433 private:
434 StructT *data_;
435
436 public:
441
442 template<typename OtherStructT,
443 typename OtherElemT,
444 OtherElemT (*OtherGetFunc)(const OtherStructT &),
445 void (*OtherSetFunc)(OtherStructT &, OtherElemT)>
447
448 private:
449 ElemT get(const int64_t index) const override
450 {
451 return GetFunc(data_[index]);
452 }
453
454 void set(const int64_t index, ElemT value) override
455 {
456 SetFunc(data_[index], std::move(value));
457 }
458
459 void materialize(const IndexMask &mask,
460 ElemT *dst,
461 const bool dst_is_uninitialized) const override
462 {
463 if constexpr (std::is_trivially_copyable_v<ElemT>) {
464 mask.foreach_index([&](const int64_t i) { dst[i] = GetFunc(data_[i]); });
465 }
466 else {
467 if (dst_is_uninitialized) {
468 mask.foreach_index([&](const int64_t i) { new (dst + i) ElemT(GetFunc(data_[i])); });
469 }
470 else {
471 mask.foreach_index([&](const int64_t i) { dst[i] = GetFunc(data_[i]); });
472 }
473 }
474 }
475
476 void materialize_compressed(const IndexMask &mask,
477 ElemT *dst,
478 const bool dst_is_uninitialized) const override
479 {
480 if constexpr (std::is_trivially_copyable_v<ElemT>) {
481 mask.foreach_index(
482 [&](const int64_t i, const int64_t pos) { dst[pos] = GetFunc(data_[i]); });
483 }
484 else {
485 if (dst_is_uninitialized) {
486 mask.foreach_index(
487 [&](const int64_t i, const int64_t pos) { new (dst + pos) ElemT(GetFunc(data_[i])); });
488 }
489 else {
490 mask.foreach_index(
491 [&](const int64_t i, const int64_t pos) { dst[pos] = GetFunc(data_[i]); });
492 }
493 }
494 }
495};
496
497template<typename StructT,
498 typename ElemT,
499 ElemT (*GetFunc)(const StructT &),
500 void (*SetFunc)(StructT &, ElemT)>
501inline constexpr bool
503
511template<typename T> class VArrayCommon {
512 protected:
514
515 VArrayCommon() = default;
516
521 VArrayCommon(const VArrayImpl<T> *impl) : impl_(impl) {}
522
526 VArrayCommon(std::shared_ptr<const VArrayImpl<T>> impl) : impl_(std::move(impl)) {}
527
531 template<typename ImplT, typename... Args> void emplace(Args &&...args)
532 {
533 impl_.template emplace<ImplT>(std::forward<Args>(args)...);
534 }
535
536 public:
538 operator bool() const
539 {
540 return impl_;
541 }
542
549 {
550 BLI_assert(*this);
551 BLI_assert(index >= 0);
552 BLI_assert(index < this->size());
553 return impl_->get(index);
554 }
555
560 T get(const int64_t index) const
561 {
562 return (*this)[index];
563 }
564
569 int64_t size() const
570 {
571 if (!impl_) {
572 return 0;
573 }
574 return impl_->size();
575 }
576
579 T first() const
580 {
581 return (*this)[0];
582 }
583
586 T last(const int64_t n = 0) const
587 {
588 return (*this)[this->size() - 1 - n];
589 }
590
592 bool is_empty() const
593 {
594 return this->size() == 0;
595 }
596
598 {
599 return IndexRange(this->size());
600 }
601
603 {
604 BLI_assert(*this);
605 return impl_->common_info();
606 }
607
609 bool is_span() const
610 {
611 BLI_assert(*this);
612 const CommonVArrayInfo info = impl_->common_info();
613 return info.type == CommonVArrayInfo::Type::Span;
614 }
615
621 {
622 BLI_assert(this->is_span());
623 const CommonVArrayInfo info = impl_->common_info();
624 return Span<T>(static_cast<const T *>(info.data), this->size());
625 }
626
628 bool is_single() const
629 {
630 BLI_assert(*this);
631 const CommonVArrayInfo info = impl_->common_info();
633 }
634
640 {
641 BLI_assert(this->is_single());
642 const CommonVArrayInfo info = impl_->common_info();
643 return *static_cast<const T *>(info.data);
644 }
645
649 std::optional<T> get_if_single() const
650 {
651 const CommonVArrayInfo info = impl_->common_info();
653 return std::nullopt;
654 }
655 return *static_cast<const T *>(info.data);
656 }
657
659 void materialize(MutableSpan<T> r_span) const
660 {
661 this->materialize(IndexMask(this->size()), r_span);
662 }
663
665 void materialize(const IndexMask &mask, MutableSpan<T> r_span) const
666 {
667 BLI_assert(mask.min_array_size() <= this->size());
668 impl_->materialize(mask, r_span.data(), false);
669 }
670
672 {
673 this->materialize_to_uninitialized(IndexMask(this->size()), r_span);
674 }
675
677 {
678 BLI_assert(mask.min_array_size() <= this->size());
679 impl_->materialize(mask, r_span.data(), true);
680 }
681
684 {
685 impl_->materialize_compressed(mask, r_span.data(), false);
686 }
687
689 {
690 impl_->materialize_compressed(mask, r_span.data(), true);
691 }
692
694 bool try_assign_GVArray(GVArray &varray) const
695 {
696 return impl_->try_assign_GVArray(varray);
697 }
698
700 {
701 return impl_.get();
702 }
703};
704
705template<typename T> class VMutableArray;
706
714namespace varray_tag {
715struct span {};
716struct single_ref {};
717struct single {};
718} // namespace varray_tag
719
725template<typename T> class VArray : public VArrayCommon<T> {
726 friend VMutableArray<T>;
727
728 public:
729 VArray() = default;
730
731 VArray(const VArrayImpl<T> *impl) : VArrayCommon<T>(impl) {}
732
733 VArray(std::shared_ptr<const VArrayImpl<T>> impl) : VArrayCommon<T>(std::move(impl)) {}
734
736 {
737 this->template emplace<VArrayImpl_For_Span_final<T>>(span);
738 }
739
740 VArray(varray_tag::single /*tag*/, T value, const int64_t size)
741 {
742 this->template emplace<VArrayImpl_For_Single<T>>(std::move(value), size);
743 }
744
748 template<typename ImplT, typename... Args> static VArray from(Args &&...args)
749 {
750 static_assert(std::is_base_of_v<VArrayImpl<T>, ImplT>);
751 VArray varray;
752 varray.template emplace<ImplT>(std::forward<Args>(args)...);
753 return varray;
754 }
755
759 static VArray from_single(T value, const int64_t size)
760 {
761 return VArray(varray_tag::single{}, std::move(value), size);
762 }
763
768 static VArray from_span(Span<T> values)
769 {
770 return VArray(varray_tag::span{}, values);
771 }
772
777 template<typename GetFunc> static VArray from_func(const int64_t size, GetFunc get_func)
778 {
779 return VArray::from<VArrayImpl_For_Func<T, decltype(get_func)>>(size, std::move(get_func));
780 }
781
786 static VArray from_std_func(const int64_t size, std::function<T(int64_t index)> get_func)
787 {
788 return VArray::from_func(size, get_func);
789 }
790
795 template<typename StructT, T (*GetFunc)(const StructT &)>
797 {
798 /* Cast const away, because the virtual array implementation for const and non const derived
799 * spans is shared. */
800 MutableSpan<StructT> span{const_cast<StructT *>(values.data()), values.size()};
802 }
803
809 template<typename ContainerT> static VArray from_container(ContainerT container)
810 {
811 return VArray::from<VArrayImpl_For_ArrayContainer<ContainerT>>(std::move(container));
812 }
813};
814
818template<typename T> class VMutableArray : public VArrayCommon<T> {
819 public:
820 VMutableArray() = default;
821
823
824 VMutableArray(std::shared_ptr<const VMutableArrayImpl<T>> impl)
825 : VArrayCommon<T>(std::move(impl))
826 {
827 }
828
832 template<typename ImplT, typename... Args> static VMutableArray from(Args &&...args)
833 {
834 static_assert(std::is_base_of_v<VMutableArrayImpl<T>, ImplT>);
835 VMutableArray varray;
836 varray.template emplace<ImplT>(std::forward<Args>(args)...);
837 return varray;
838 }
839
847
852 template<typename StructT, T (*GetFunc)(const StructT &), void (*SetFunc)(StructT &, T)>
857
863 template<typename ContainerT> static VMutableArray from_container(ContainerT container)
864 {
866 }
867
869 operator VArray<T>() const &
870 {
871 VArray<T> varray;
872 *static_cast<VArrayCommon<T> *>(&varray) = *this;
873 return varray;
874 }
875
877 operator VArray<T>() && noexcept
878 {
879 VArray<T> varray;
880 *static_cast<VArrayCommon<T> *>(&varray) = std::move(*this);
881 return varray;
882 }
883
889 {
890 BLI_assert(this->is_span());
891 const CommonVArrayInfo info = this->get_impl()->common_info();
892 return MutableSpan<T>(const_cast<T *>(static_cast<const T *>(info.data)), this->size());
893 }
894
898 void set(const int64_t index, T value)
899 {
900 BLI_assert(index >= 0);
901 BLI_assert(index < this->size());
902/* Quiet false-positive warning in GCC. */
903#if defined(__GNUC__) && !defined(__clang__)
904# pragma GCC diagnostic push
905# pragma GCC diagnostic ignored "-Warray-bounds"
906#endif
907 this->get_impl()->set(index, std::move(value));
908#if defined(__GNUC__) && !defined(__clang__)
909# pragma GCC diagnostic pop
910#endif
911 }
912
916 void set_all(Span<T> src)
917 {
918 BLI_assert(src.size() == this->size());
919 this->get_impl()->set_all(src);
920 }
921
924 {
925 return this->get_impl()->try_assign_GVMutableArray(varray);
926 }
927
928 private:
930 VMutableArrayImpl<T> *get_impl() const
931 {
932 /* This cast is valid by the invariant that a #VMutableArray->impl_ is always a
933 * #VMutableArrayImpl. */
934 return reinterpret_cast<VMutableArrayImpl<T> *>(
935 const_cast<VArrayImpl<T> *>((this->impl_.get())));
936 }
937};
938
939template<typename T> static constexpr bool is_VArray_v = false;
940template<typename T> static constexpr bool is_VArray_v<VArray<T>> = true;
941
942template<typename T> static constexpr bool is_VMutableArray_v = false;
943template<typename T> static constexpr bool is_VMutableArray_v<VMutableArray<T>> = true;
944
956template<typename T> class VArraySpan final : public Span<T> {
957 private:
958 VArray<T> varray_;
959 Array<T> owned_data_;
960
961 public:
962 VArraySpan() = default;
963
964 VArraySpan(const VArray<T> &varray) : VArraySpan(VArray<T>(varray)) {}
965
966 VArraySpan(VArray<T> &&varray) : Span<T>(), varray_(std::move(varray))
967 {
968 if (!varray_) {
969 return;
970 }
971 this->size_ = varray_.size();
972 const CommonVArrayInfo info = varray_.common_info();
974 this->data_ = static_cast<const T *>(info.data);
975 }
976 else {
977 owned_data_.~Array();
978 new (&owned_data_) Array<T>(varray_.size(), NoInitialization{});
979 varray_.materialize_to_uninitialized(owned_data_);
980 this->data_ = owned_data_.data();
981 }
982 }
983
985 : varray_(std::move(other.varray_)), owned_data_(std::move(other.owned_data_))
986 {
987 if (!varray_) {
988 return;
989 }
990 this->size_ = varray_.size();
991 const CommonVArrayInfo info = varray_.common_info();
993 this->data_ = static_cast<const T *>(info.data);
994 }
995 else {
996 this->data_ = owned_data_.data();
997 }
998 other.data_ = nullptr;
999 other.size_ = 0;
1000 }
1001
1003 {
1004 if (this == &other) {
1005 return *this;
1006 }
1007 std::destroy_at(this);
1008 new (this) VArraySpan(std::move(other));
1009 return *this;
1010 }
1011};
1012
1013namespace internal {
1014void print_mutable_varray_span_warning();
1015}
1016
1024template<typename T> class MutableVArraySpan final : public MutableSpan<T> {
1025 private:
1026 VMutableArray<T> varray_;
1027 Array<T> owned_data_;
1028 bool save_has_been_called_ = false;
1029 bool show_not_saved_warning_ = true;
1030
1031 public:
1033
1034 /* Create a span for any virtual array. This is cheap when the virtual array is a span itself. If
1035 * not, a new array has to be allocated as a wrapper for the underlying virtual array. */
1036 MutableVArraySpan(VMutableArray<T> varray, const bool copy_values_to_span = true)
1037 : MutableSpan<T>(), varray_(std::move(varray))
1038 {
1039 if (!varray_) {
1040 return;
1041 }
1042
1043 this->size_ = varray_.size();
1044 const CommonVArrayInfo info = varray_.common_info();
1045 if (info.type == CommonVArrayInfo::Type::Span) {
1046 this->data_ = const_cast<T *>(static_cast<const T *>(info.data));
1047 }
1048 else {
1049 if (copy_values_to_span) {
1050 owned_data_.~Array();
1051 new (&owned_data_) Array<T>(varray_.size(), NoInitialization{});
1052 varray_.materialize_to_uninitialized(owned_data_);
1053 }
1054 else {
1055 owned_data_.reinitialize(varray_.size());
1056 }
1057 this->data_ = owned_data_.data();
1058 }
1059 }
1060
1062 : varray_(std::move(other.varray_)),
1063 owned_data_(std::move(other.owned_data_)),
1064 show_not_saved_warning_(other.show_not_saved_warning_)
1065 {
1066 if (!varray_) {
1067 return;
1068 }
1069
1070 this->size_ = varray_.size();
1071 const CommonVArrayInfo info = varray_.common_info();
1072 if (info.type == CommonVArrayInfo::Type::Span) {
1073 this->data_ = static_cast<T *>(const_cast<void *>(info.data));
1074 }
1075 else {
1076 this->data_ = owned_data_.data();
1077 }
1078 other.data_ = nullptr;
1079 other.size_ = 0;
1080 }
1081
1083 {
1084 if (varray_) {
1085 if (show_not_saved_warning_) {
1086 if (!save_has_been_called_) {
1088 }
1089 }
1090 }
1091 }
1092
1094 {
1095 if (this == &other) {
1096 return *this;
1097 }
1098 std::destroy_at(this);
1099 new (this) MutableVArraySpan(std::move(other));
1100 return *this;
1101 }
1102
1104 {
1105 return varray_;
1106 }
1107
1108 /* Write back all values from a temporary allocated array to the underlying virtual array. */
1109 void save()
1110 {
1111 save_has_been_called_ = true;
1112 if (this->data_ != owned_data_.data()) {
1113 return;
1114 }
1115 varray_.set_all(owned_data_);
1116 }
1117
1119 {
1120 show_not_saved_warning_ = false;
1121 }
1122};
1123
1124template<typename T> class SingleAsSpan {
1125 private:
1126 T value_;
1127 int64_t size_;
1128
1129 public:
1130 SingleAsSpan(T value, int64_t size) : value_(std::move(value)), size_(size)
1131 {
1132 BLI_assert(size_ >= 0);
1133 }
1134
1135 SingleAsSpan(const VArray<T> &varray) : SingleAsSpan(varray.get_internal_single(), varray.size())
1136 {
1137 }
1138
1139 const T &operator[](const int64_t index) const
1140 {
1141 BLI_assert(index >= 0);
1142 BLI_assert(index < size_);
1144 return value_;
1145 }
1146};
1147
1148template<typename T> class VArrayRef {
1149 private:
1150 const VArray<T> &ref_;
1151
1152 public:
1153 VArrayRef(const VArray<T> &ref) : ref_(ref) {}
1154
1156 {
1157 return ref_[index];
1158 }
1159};
1160
1162template<typename T, bool UseSingle, bool UseSpan> struct VArrayDevirtualizer {
1164
1165 template<typename Fn> bool devirtualize(const Fn &fn) const
1166 {
1167 const CommonVArrayInfo info = this->varray.common_info();
1168 const int64_t size = this->varray.size();
1169 if constexpr (UseSingle) {
1171 return fn(SingleAsSpan<T>(*static_cast<const T *>(info.data), size));
1172 }
1173 }
1174 if constexpr (UseSpan) {
1175 if (info.type == CommonVArrayInfo::Type::Span) {
1176 return fn(Span<T>(static_cast<const T *>(info.data), size));
1177 }
1178 }
1179 return false;
1180 }
1181};
1182
1191template<typename T, typename Func>
1192inline void devirtualize_varray(const VArray<T> &varray, const Func &func, bool enable = true)
1193{
1194 if (enable) {
1196 std::make_tuple(VArrayDevirtualizer<T, true, true>{varray}), func))
1197 {
1198 return;
1199 }
1200 }
1201 func(VArrayRef<T>(varray));
1202}
1203
1209template<typename T1, typename T2, typename Func>
1210inline void devirtualize_varray2(const VArray<T1> &varray1,
1211 const VArray<T2> &varray2,
1212 const Func &func,
1213 bool enable = true)
1214{
1215 if (enable) {
1217 std::make_tuple(VArrayDevirtualizer<T1, true, true>{varray1},
1219 func))
1220 {
1221 return;
1222 }
1223 }
1224 func(VArrayRef<T1>(varray1), VArrayRef<T2>(varray2));
1225}
1226
1227} // namespace blender
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define UNUSED_VARS_NDEBUG(...)
long long int int64_t
constexpr IndexRange()=default
constexpr MutableSpan()=default
constexpr T * data() const
Definition BLI_span.hh:545
const VMutableArray< T > & varray() const
MutableVArraySpan & operator=(MutableVArraySpan &&other)
MutableVArraySpan(MutableVArraySpan &&other)
MutableVArraySpan(VMutableArray< T > varray, const bool copy_values_to_span=true)
SingleAsSpan(const VArray< T > &varray)
const T & operator[](const int64_t index) const
SingleAsSpan(T value, int64_t size)
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
std::optional< T > get_if_single() const
IndexRange index_range() const
VArrayCommon(std::shared_ptr< const VArrayImpl< T > > impl)
void materialize(MutableSpan< T > r_span) const
T operator[](const int64_t index) const
T last(const int64_t n=0) const
VArrayCommon(const VArrayImpl< T > *impl)
void materialize_compressed_to_uninitialized(const IndexMask &mask, MutableSpan< T > r_span) const
void materialize(const IndexMask &mask, MutableSpan< T > r_span) const
AnyDerived< const VArrayImpl< T > > impl_
T get(const int64_t index) const
void materialize_to_uninitialized(const IndexMask &mask, MutableSpan< T > r_span) const
const VArrayImpl< T > * get_implementation() const
bool try_assign_GVArray(GVArray &varray) const
void materialize_to_uninitialized(MutableSpan< T > r_span) const
void materialize_compressed(const IndexMask &mask, MutableSpan< T > r_span) const
CommonVArrayInfo common_info() const
Span< T > get_internal_span() const
void emplace(Args &&...args)
VArrayImpl_For_DerivedSpan(const MutableSpan< StructT > data)
VArrayImpl_For_Func(const int64_t size, GetFunc get_func)
void materialize_compressed(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
VArrayImpl_For_Single(T value, const int64_t size)
void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
CommonVArrayInfo common_info() const override
T get(const int64_t) const override
VArrayImpl_For_Span_final(const Span< T > data)
CommonVArrayInfo common_info() const override
void set(const int64_t index, T value) final
VArrayImpl_For_Span(const int64_t size)
void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
T get(const int64_t index) const final
VArrayImpl_For_Span(const MutableSpan< T > data)
void materialize_compressed(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const override
virtual bool try_assign_GVArray(GVArray &) const
virtual void materialize(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const
virtual CommonVArrayInfo common_info() const
virtual void materialize_compressed(const IndexMask &mask, T *dst, const bool dst_is_uninitialized) const
virtual T get(int64_t index) const =0
VArrayImpl(const int64_t size)
virtual ~VArrayImpl()=default
VArrayRef(const VArray< T > &ref)
T operator[](const int64_t index) const
VArraySpan(VArray< T > &&varray)
VArraySpan(VArraySpan &&other)
VArraySpan(const VArray< T > &varray)
VArraySpan & operator=(VArraySpan &&other)
static VArray from_derived_span(Span< StructT > values)
VArray(const VArrayImpl< T > *impl)
static VArray from(Args &&...args)
VArray(varray_tag::single, T value, const int64_t size)
static VArray from_std_func(const int64_t size, std::function< T(int64_t index)> get_func)
static VArray from_single(T value, const int64_t size)
static VArray from_func(const int64_t size, GetFunc get_func)
VArray(varray_tag::span, Span< T > span)
VArray(std::shared_ptr< const VArrayImpl< T > > impl)
static VArray from_span(Span< T > values)
static VArray from_container(ContainerT container)
VArray()=default
virtual void set(int64_t index, T value)=0
virtual bool try_assign_GVMutableArray(GVMutableArray &) const
virtual void set_all(Span< T > src)
static VMutableArray from_derived_span(MutableSpan< StructT > values)
void set(const int64_t index, T value)
VMutableArray(std::shared_ptr< const VMutableArrayImpl< T > > impl)
static VMutableArray from_container(ContainerT container)
static VMutableArray from(Args &&...args)
bool try_assign_GVMutableArray(GVMutableArray &varray) const
MutableSpan< T > get_internal_span() const
VMutableArray(const VMutableArrayImpl< T > *impl)
static VMutableArray from_span(MutableSpan< T > values)
void set_all(Span< T > src)
constexpr MutableSpan()=default
constexpr Span()=default
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define T
BLI_NOINLINE void fill(T *__restrict data, const T &value, const IndexMask &mask)
BLI_NOINLINE void copy_assign(const T *__restrict src, const IndexMask &mask, T *__restrict dst)
BLI_NOINLINE void gather_assign(const T *__restrict src, const IndexMask &indices, T *__restrict dst)
void print_mutable_varray_span_warning()
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
void devirtualize_varray(const VArray< T > &varray, const Func &func, bool enable=true)
void devirtualize_varray2(const VArray< T1 > &varray1, const VArray< T2 > &varray2, const Func &func, bool enable=true)
static constexpr bool is_VArray_v
void initialized_fill_n(T *dst, int64_t n, const T &value)
bool call_with_devirtualized_parameters(const std::tuple< Devirtualizers... > &devis, const Fn &fn)
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
static constexpr bool is_VMutableArray_v
void uninitialized_fill_n(T *dst, int64_t n, const T &value)
void initialized_copy_n(const T *src, int64_t n, T *dst)
constexpr bool is_trivial_extended_v
void mask(PageMask &srt, TileMaps &tilemaps, const uint3 global_id, const uint local_tile_index)
CommonVArrayInfo(const Type _type, const bool _may_have_ownership, const void *_data)
bool devirtualize(const Fn &fn) const