1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
/** \file
* \ingroup bli
*
* A virtual array is a data structure that behaves similar to an array, but its elements are
* accessed through virtual methods. This improves the decoupling of a function from its callers,
* because it does not have to know exactly how the data is laid out in memory, or if it is stored
* in memory at all. It could just as well be computed on the fly.
*
* Taking a virtual array as parameter instead of a more specific non-virtual type has some
* tradeoffs. Access to individual elements of the individual elements is higher due to function
* call overhead. On the other hand, potential callers don't have to convert the data into the
* specific format required for the function. This can be a costly conversion if only few of the
* elements are accessed in the end.
*
* Functions taking a virtual array as input can still optimize for different data layouts. For
* example, they can check if the array is stored as an array internally or if it is the same
* element for all indices. Whether it is worth to optimize for different data layouts in a
* function has to be decided on a case by case basis. One should always do some benchmarking to
* see of the increased compile time and binary size is worth it.
*/
#include "BLI_any.hh"
#include "BLI_array.hh"
#include "BLI_index_mask.hh"
#include "BLI_span.hh"
namespace blender {
/** Forward declarations for generic virtual arrays. */
class GVArray;
class GVMutableArray;
/**
* Implements the specifics of how the elements of a virtual array are accessed. It contains a
* bunch of virtual methods that are wrapped by #VArray.
*/
template<typename T> class VArrayImpl {
protected:
/**
* Number of elements in the virtual array. All virtual arrays have a size, but in some cases it
* may make sense to set it to the max value.
*/
int64_t size_;
public:
VArrayImpl(const int64_t size) : size_(size)
{
BLI_assert(size_ >= 0);
}
virtual ~VArrayImpl() = default;
int64_t size() const
{
return size_;
}
/**
* Get the element at #index. This does not return a reference, because the value may be computed
* on the fly.
*/
virtual T get(int64_t index) const = 0;
/**
* Return true when the virtual array is a plain array internally.
*/
virtual bool is_span() const
{
return false;
}
/**
* Return the span of the virtual array.
* This invokes undefined behavior when #is_span returned false.
*/
virtual Span<T> get_internal_span() const
{
/* Provide a default implementation, so that subclasses don't have to provide it. This method
* should never be called because #is_span returns false by default. */
BLI_assert_unreachable();
return {};
}
/**
* Return true when the virtual array has the same value at every index.
*/
virtual bool is_single() const
{
return false;
}
/**
* Return the value that is used at every index.
* This invokes undefined behavior when #is_single returned false.
*/
virtual T get_internal_single() const
{
/* Provide a default implementation, so that subclasses don't have to provide it. This method
* should never be called because #is_single returns false by default. */
BLI_assert_unreachable();
return T();
}
/**
* Copy values from the virtual array into the provided span. The index of the value in the
* virtual array is the same as the index in the span.
*/
virtual void materialize(IndexMask mask, MutableSpan<T> r_span) const
{
T *dst = r_span.data();
/* Optimize for a few different common cases. */
if (this->is_span()) {
const T *src = this->get_internal_span().data();
mask.foreach_index([&](const int64_t i) { dst[i] = src[i]; });
}
else if (this->is_single()) {
const T single = this->get_internal_single();
mask.foreach_index([&](const int64_t i) { dst[i] = single; });
}
else {
mask.foreach_index([&](const int64_t i) { dst[i] = this->get(i); });
}
}
/**
* Same as #materialize but #r_span is expected to be uninitialized.
*/
virtual void materialize_to_uninitialized(IndexMask mask, MutableSpan<T> r_span) const
{
T *dst = r_span.data();
/* Optimize for a few different common cases. */
if (this->is_span()) {
const T *src = this->get_internal_span().data();
mask.foreach_index([&](const int64_t i) { new (dst + i) T(src[i]); });
}
else if (this->is_single()) {
const T single = this->get_internal_single();
mask.foreach_index([&](const int64_t i) { new (dst + i) T(single); });
}
else {
mask.foreach_index([&](const int64_t i) { new (dst + i) T(this->get(i)); });
}
}
/**
* Copy values from the virtual array into the provided span. Contrary to #materialize, the index
* in virtual array is not the same as the index in the output span. Instead, the span is filled
* without gaps.
*/
virtual void materialize_compressed(IndexMask mask, MutableSpan<T> r_span) const
{
BLI_assert(mask.size() == r_span.size());
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
r_span[i] = this->get(best_mask[i]);
}
});
}
/**
* Same as #materialize_compressed but #r_span is expected to be uninitialized.
*/
virtual void materialize_compressed_to_uninitialized(IndexMask mask, MutableSpan<T> r_span) const
{
BLI_assert(mask.size() == r_span.size());
T *dst = r_span.data();
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
new (dst + i) T(this->get(best_mask[i]));
}
});
}
/**
* If this virtual wraps another #GVArray, this method should assign the wrapped array to the
* provided reference. This allows losslessly converting between generic and typed virtual
* arrays in all cases.
* Return true when the virtual array was assigned and false when nothing was done.
*/
virtual bool try_assign_GVArray(GVArray &UNUSED(varray)) const
{
return false;
}
/**
* Return true when this virtual array may own any of the memory it references. This can be used
* for optimization purposes when converting or copying the virtual array.
*/
virtual bool may_have_ownership() const
{
/* Use true by default to be on the safe side. Subclasses that know for sure that they don't
* own anything can overwrite this with false. */
return true;
}
/**
* Return true when the other virtual array should be considered to be the same, e.g. because it
* shares the same underlying memory.
*/
virtual bool is_same(const VArrayImpl<T> &UNUSED(other)) const
{
return false;
}
};
/** Similar to #VArrayImpl, but adds methods that allow modifying the referenced elements. */
template<typename T> class VMutableArrayImpl : public VArrayImpl<T> {
public:
using VArrayImpl<T>::VArrayImpl;
/**
* Assign the provided #value to the #index.
*/
virtual void set(int64_t index, T value) = 0;
/**
* Copy all elements from the provided span into the virtual array.
*/
virtual void set_all(Span<T> src)
{
if (this->is_span()) {
const Span<T> const_span = this->get_internal_span();
const MutableSpan<T> span{(T *)const_span.data(), const_span.size()};
initialized_copy_n(src.data(), this->size_, span.data());
}
else {
const int64_t size = this->size_;
for (int64_t i = 0; i < size; i++) {
this->set(i, src[i]);
}
}
}
/**
* Similar to #VArrayImpl::try_assign_GVArray but for mutable virtual arrays.
*/
virtual bool try_assign_GVMutableArray(GVMutableArray &UNUSED(varray)) const
{
return false;
}
};
/**
* A virtual array implementation that references that wraps a span. This implementation is used by
* mutable and immutable spans to avoid code duplication.
*/
template<typename T> class VArrayImpl_For_Span : public VMutableArrayImpl<T> {
protected:
T *data_ = nullptr;
public:
VArrayImpl_For_Span(const MutableSpan<T> data)
: VMutableArrayImpl<T>(data.size()), data_(data.data())
{
}
protected:
VArrayImpl_For_Span(const int64_t size) : VMutableArrayImpl<T>(size)
{
}
T get(const int64_t index) const final
{
return data_[index];
}
void set(const int64_t index, T value) final
{
data_[index] = value;
}
bool is_span() const override
{
return true;
}
Span<T> get_internal_span() const override
{
return Span<T>(data_, this->size_);
}
bool is_same(const VArrayImpl<T> &other) const final
{
if (other.size() != this->size_) {
return false;
}
if (!other.is_span()) {
return false;
}
const Span<T> other_span = other.get_internal_span();
return data_ == other_span.data();
}
void materialize_compressed(IndexMask mask, MutableSpan<T> r_span) const override
{
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
r_span[i] = data_[best_mask[i]];
}
});
}
void materialize_compressed_to_uninitialized(IndexMask mask,
MutableSpan<T> r_span) const override
{
T *dst = r_span.data();
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
new (dst + i) T(data_[best_mask[i]]);
}
});
}
};
/**
* A version of #VArrayImpl_For_Span that can not be subclassed. This allows safely overwriting the
* #may_have_ownership method.
*/
template<typename T> class VArrayImpl_For_Span_final final : public VArrayImpl_For_Span<T> {
public:
using VArrayImpl_For_Span<T>::VArrayImpl_For_Span;
private:
bool may_have_ownership() const override
{
return false;
}
};
/**
* A variant of `VArrayImpl_For_Span` that owns the underlying data.
* The `Container` type has to implement a `size()` and `data()` method.
* The `data()` method has to return a pointer to the first element in the continuous array of
* elements.
*/
template<typename Container, typename T = typename Container::value_type>
class VArrayImpl_For_ArrayContainer : public VArrayImpl_For_Span<T> {
private:
Container container_;
public:
VArrayImpl_For_ArrayContainer(Container container)
: VArrayImpl_For_Span<T>((int64_t)container.size()), container_(std::move(container))
{
this->data_ = const_cast<T *>(container_.data());
}
};
/**
* A virtual array implementation that returns the same value for every index. This class is final
* so that it can be devirtualized by the compiler in some cases (e.g. when #devirtualize_varray is
* used).
*/
template<typename T> class VArrayImpl_For_Single final : public VArrayImpl<T> {
private:
T value_;
public:
VArrayImpl_For_Single(T value, const int64_t size)
: VArrayImpl<T>(size), value_(std::move(value))
{
}
protected:
T get(const int64_t UNUSED(index)) const override
{
return value_;
}
bool is_span() const override
{
return this->size_ == 1;
}
Span<T> get_internal_span() const override
{
return Span<T>(&value_, 1);
}
bool is_single() const override
{
return true;
}
T get_internal_single() const override
{
return value_;
}
void materialize_compressed(IndexMask mask, MutableSpan<T> r_span) const override
{
BLI_assert(mask.size() == r_span.size());
UNUSED_VARS_NDEBUG(mask);
r_span.fill(value_);
}
void materialize_compressed_to_uninitialized(IndexMask mask,
MutableSpan<T> r_span) const override
{
BLI_assert(mask.size() == r_span.size());
uninitialized_fill_n(r_span.data(), mask.size(), value_);
}
};
/**
* This class makes it easy to create a virtual array for an existing function or lambda. The
* `GetFunc` should take a single `index` argument and return the value at that index.
*/
template<typename T, typename GetFunc> class VArrayImpl_For_Func final : public VArrayImpl<T> {
private:
GetFunc get_func_;
public:
VArrayImpl_For_Func(const int64_t size, GetFunc get_func)
: VArrayImpl<T>(size), get_func_(std::move(get_func))
{
}
private:
T get(const int64_t index) const override
{
return get_func_(index);
}
void materialize(IndexMask mask, MutableSpan<T> r_span) const override
{
T *dst = r_span.data();
mask.foreach_index([&](const int64_t i) { dst[i] = get_func_(i); });
}
void materialize_to_uninitialized(IndexMask mask, MutableSpan<T> r_span) const override
{
T *dst = r_span.data();
mask.foreach_index([&](const int64_t i) { new (dst + i) T(get_func_(i)); });
}
void materialize_compressed(IndexMask mask, MutableSpan<T> r_span) const override
{
BLI_assert(mask.size() == r_span.size());
T *dst = r_span.data();
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
dst[i] = get_func_(best_mask[i]);
}
});
}
void materialize_compressed_to_uninitialized(IndexMask mask,
MutableSpan<T> r_span) const override
{
BLI_assert(mask.size() == r_span.size());
T *dst = r_span.data();
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
new (dst + i) T(get_func_(best_mask[i]));
}
});
}
};
/**
* \note: This is `final` so that #may_have_ownership can be implemented reliably.
*/
template<typename StructT,
typename ElemT,
ElemT (*GetFunc)(const StructT &),
void (*SetFunc)(StructT &, ElemT) = nullptr>
class VArrayImpl_For_DerivedSpan final : public VMutableArrayImpl<ElemT> {
private:
StructT *data_;
public:
VArrayImpl_For_DerivedSpan(const MutableSpan<StructT> data)
: VMutableArrayImpl<ElemT>(data.size()), data_(data.data())
{
}
template<typename OtherStructT,
typename OtherElemT,
OtherElemT (*OtherGetFunc)(const OtherStructT &),
void (*OtherSetFunc)(OtherStructT &, OtherElemT)>
friend class VArrayImpl_For_DerivedSpan;
private:
ElemT get(const int64_t index) const override
{
return GetFunc(data_[index]);
}
void set(const int64_t index, ElemT value) override
{
SetFunc(data_[index], std::move(value));
}
void materialize(IndexMask mask, MutableSpan<ElemT> r_span) const override
{
ElemT *dst = r_span.data();
mask.foreach_index([&](const int64_t i) { dst[i] = GetFunc(data_[i]); });
}
void materialize_to_uninitialized(IndexMask mask, MutableSpan<ElemT> r_span) const override
{
ElemT *dst = r_span.data();
mask.foreach_index([&](const int64_t i) { new (dst + i) ElemT(GetFunc(data_[i])); });
}
void materialize_compressed(IndexMask mask, MutableSpan<ElemT> r_span) const override
{
BLI_assert(mask.size() == r_span.size());
ElemT *dst = r_span.data();
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
dst[i] = GetFunc(data_[best_mask[i]]);
}
});
}
void materialize_compressed_to_uninitialized(IndexMask mask,
MutableSpan<ElemT> r_span) const override
{
BLI_assert(mask.size() == r_span.size());
ElemT *dst = r_span.data();
mask.to_best_mask_type([&](auto best_mask) {
for (const int64_t i : IndexRange(best_mask.size())) {
new (dst + i) ElemT(GetFunc(data_[best_mask[i]]));
}
});
}
bool may_have_ownership() const override
{
return false;
}
bool is_same(const VArrayImpl<ElemT> &other) const override
{
if (other.size() != this->size_) {
return false;
}
if (const VArrayImpl_For_DerivedSpan<StructT, ElemT, GetFunc> *other_typed =
dynamic_cast<const VArrayImpl_For_DerivedSpan<StructT, ElemT, GetFunc> *>(&other)) {
return other_typed->data_ == data_;
}
if (const VArrayImpl_For_DerivedSpan<StructT, ElemT, GetFunc, SetFunc> *other_typed =
dynamic_cast<const VArrayImpl_For_DerivedSpan<StructT, ElemT, GetFunc, SetFunc> *>(
&other)) {
return other_typed->data_ == data_;
}
return false;
}
};
namespace detail {
/**
* Struct that can be passed as `ExtraInfo` into an #Any.
* This struct is only intended to be used by #VArrayCommon.
*/
template<typename T> struct VArrayAnyExtraInfo {
/**
* Gets the virtual array that is stored at the given pointer.
*/
const VArrayImpl<T> *(*get_varray)(const void *buffer);
template<typename StorageT> static constexpr VArrayAnyExtraInfo get()
{
/* These are the only allowed types in the #Any. */
static_assert(
std::is_base_of_v<VArrayImpl<T>, StorageT> ||
is_same_any_v<StorageT, const VArrayImpl<T> *, std::shared_ptr<const VArrayImpl<T>>>);
/* Depending on how the virtual array implementation is stored in the #Any, a different
* #get_varray function is required. */
if constexpr (std::is_base_of_v<VArrayImpl<T>, StorageT>) {
return {[](const void *buffer) {
return static_cast<const VArrayImpl<T> *>((const StorageT *)buffer);
}};
}
else if constexpr (std::is_same_v<StorageT, const VArrayImpl<T> *>) {
return {[](const void *buffer) { return *(const StorageT *)buffer; }};
}
else if constexpr (std::is_same_v<StorageT, std::shared_ptr<const VArrayImpl<T>>>) {
return {[](const void *buffer) { return ((const StorageT *)buffer)->get(); }};
}
else {
BLI_assert_unreachable();
return {};
}
}
};
} // namespace detail
/**
* Utility class to reduce code duplication for methods available on #VArray and #VMutableArray.
* Deriving #VMutableArray from #VArray would have some issues:
* - Static methods on #VArray would also be available on #VMutableArray.
* - It would allow assigning a #VArray to a #VMutableArray under some circumstances which is not
* allowed and could result in hard to find bugs.
*/
template<typename T> class VArrayCommon {
protected:
/**
* Store the virtual array implementation in an #Any. This makes it easy to avoid a memory
* allocation if the implementation is small enough and is copyable. This is the case for the
* most common virtual arrays.
* Other virtual array implementations are typically stored as #std::shared_ptr. That works even
* when the implementation itself is not copyable and makes copying #VArrayCommon cheaper.
*/
using Storage = Any<detail::VArrayAnyExtraInfo<T>, 24, 8>;
/**
* Pointer to the currently contained virtual array implementation. This is allowed to be null.
*/
const VArrayImpl<T> *impl_ = nullptr;
/**
* Does the memory management for the virtual array implementation. It contains one of the
* following:
* - Inlined subclass of #VArrayImpl.
* - Non-owning pointer to a #VArrayImpl.
* - Shared pointer to a #VArrayImpl.
*/
Storage storage_;
protected:
VArrayCommon() = default;
/** Copy constructor. */
VArrayCommon(const VArrayCommon &other) : storage_(other.storage_)
{
impl_ = this->impl_from_storage();
}
/** Move constructor. */
VArrayCommon(VArrayCommon &&other) noexcept : storage_(std::move(other.storage_))
{
impl_ = this->impl_from_storage();
other.storage_.reset();
other.impl_ = nullptr;
}
/**
* Wrap an existing #VArrayImpl and don't take ownership of it. This should rarely be used in
* practice.
*/
VArrayCommon(const VArrayImpl<T> *impl) : impl_(impl)
{
storage_ = impl_;
}
/**
* Wrap an existing #VArrayImpl that is contained in a #std::shared_ptr. This takes ownership.
*/
VArrayCommon(std::shared_ptr<const VArrayImpl<T>> impl) : impl_(impl.get())
{
if (impl) {
storage_ = std::move(impl);
}
}
/**
* Replace the contained #VArrayImpl.
*/
template<typename ImplT, typename... Args> void emplace(Args &&...args)
{
/* Make sure we are actually constructing a #VArrayImpl. */
static_assert(std::is_base_of_v<VArrayImpl<T>, ImplT>);
if constexpr (std::is_copy_constructible_v<ImplT> && Storage::template is_inline_v<ImplT>) {
/* Only inline the implementation when it is copyable and when it fits into the inline
* buffer of the storage. */
impl_ = &storage_.template emplace<ImplT>(std::forward<Args>(args)...);
}
else {
/* If it can't be inlined, create a new #std::shared_ptr instead and store that in the
* storage. */
std::shared_ptr<const VArrayImpl<T>> ptr = std::make_shared<ImplT>(
std::forward<Args>(args)...);
impl_ = &*ptr;
storage_ = std::move(ptr);
}
}
/** Utility to implement a copy assignment operator in a subclass. */
void copy_from(const VArrayCommon &other)
{
if (this == &other) {
return;
}
storage_ = other.storage_;
impl_ = this->impl_from_storage();
}
/** Utility to implement a move assignment operator in a subclass. */
void move_from(VArrayCommon &&other) noexcept
{
if (this == &other) {
return;
}
storage_ = std::move(other.storage_);
impl_ = this->impl_from_storage();
other.storage_.reset();
other.impl_ = nullptr;
}
/** Get a pointer to the virtual array implementation that is currently stored in #storage_, or
* null. */
const VArrayImpl<T> *impl_from_storage() const
{
if (!storage_.has_value()) {
return nullptr;
}
return storage_.extra_info().get_varray(storage_.get());
}
public:
/** Return false when there is no virtual array implementation currently. */
operator bool() const
{
return impl_ != nullptr;
}
/**
* Get the element at a specific index.
* \note: This can't return a reference because the value may be computed on the fly. This also
* implies that one can not use this method for assignments.
*/
T operator[](const int64_t index) const
{
BLI_assert(*this);
BLI_assert(index >= 0);
BLI_assert(index < this->size());
return impl_->get(index);
}
/**
* Same as the #operator[] but is sometimes easier to use when one has a pointer to a virtual
* array.
*/
T get(const int64_t index) const
{
return (*this)[index];
}
/**
* Return the size of the virtual array. It's allowed to call this method even when there is no
* virtual array. In this case 0 is returned.
*/
int64_t size() const
{
if (impl_ == nullptr) {
return 0;
}
return impl_->size();
}
/** True when the size is zero or when there is no virtual array. */
bool is_empty() const
{
return this->size() == 0;
}
IndexRange index_range() const
{
return IndexRange(this->size());
}
/** Return true when the virtual array is stored as a span internally. */
bool is_span() const
{
BLI_assert(*this);
return impl_->is_span();
}
/**
* Returns the internally used span of the virtual array. This invokes undefined behavior if the
* virtual array is not stored as a span internally.
*/
Span<T> get_internal_span() const
{
BLI_assert(this->is_span());
return impl_->get_internal_span();
}
/** Return true when the virtual array returns the same value for every index. */
bool is_single() const
{
BLI_assert(*this);
return impl_->is_single();
}
/**
* Return the value that is returned for every index. This invokes undefined behavior if the
* virtual array would not return the same value for every index.
*/
T get_internal_single() const
{
BLI_assert(this->is_single());
return impl_->get_internal_single();
}
/**
* Return true when the other virtual references the same underlying memory.
*/
bool is_same(const VArrayCommon<T> &other) const
{
if (!*this || !other) {
return false;
}
/* Check in both directions in case one does not know how to compare to the other
* implementation. */
if (impl_->is_same(*other.impl_)) {
return true;
}
if (other.impl_->is_same(*impl_)) {
return true;
}
return false;
}
/** Copy the entire virtual array into a span. */
void materialize(MutableSpan<T> r_span) const
{
this->materialize(IndexMask(this->size()), r_span);
}
/** Copy some indices of the virtual array into a span. */
void materialize(IndexMask mask, MutableSpan<T> r_span) const
{
BLI_assert(mask.min_array_size() <= this->size());
impl_->materialize(mask, r_span);
}
void materialize_to_uninitialized(MutableSpan<T> r_span) const
{
this->materialize_to_uninitialized(IndexMask(this->size()), r_span);
}
void materialize_to_uninitialized(IndexMask mask, MutableSpan<T> r_span) const
{
BLI_assert(mask.min_array_size() <= this->size());
impl_->materialize_to_uninitialized(mask, r_span);
}
/** Copy some elements of the virtual array into a span. */
void materialize_compressed(IndexMask mask, MutableSpan<T> r_span) const
{
impl_->materialize_compressed(mask, r_span);
}
void materialize_compressed_to_uninitialized(IndexMask mask, MutableSpan<T> r_span) const
{
impl_->materialize_compressed_to_uninitialized(mask, r_span);
}
/** See #GVArrayImpl::try_assign_GVArray. */
bool try_assign_GVArray(GVArray &varray) const
{
return impl_->try_assign_GVArray(varray);
}
/** See #GVArrayImpl::may_have_ownership. */
bool may_have_ownership() const
{
return impl_->may_have_ownership();
}
};
template<typename T> class VMutableArray;
/**
* A #VArray wraps a virtual array implementation and provides easy access to its elements. It can
* be copied and moved. While it is relatively small, it should still be passed by reference if
* possible (other than e.g. #Span).
*/
template<typename T> class VArray : public VArrayCommon<T> {
friend VMutableArray<T>;
public:
VArray() = default;
VArray(const VArray &other) = default;
VArray(VArray &&other) noexcept = default;
VArray(const VArrayImpl<T> *impl) : VArrayCommon<T>(impl)
{
}
VArray(std::shared_ptr<const VArrayImpl<T>> impl) : VArrayCommon<T>(std::move(impl))
{
}
/**
* Construct a new virtual array for a custom #VArrayImpl.
*/
template<typename ImplT, typename... Args> static VArray For(Args &&...args)
{
static_assert(std::is_base_of_v<VArrayImpl<T>, ImplT>);
VArray varray;
varray.template emplace<ImplT>(std::forward<Args>(args)...);
return varray;
}
/**
* Construct a new virtual array that has the same value at every index.
*/
static VArray ForSingle(T value, const int64_t size)
{
return VArray::For<VArrayImpl_For_Single<T>>(std::move(value), size);
}
/**
* Construct a new virtual array for an existing span. This does not take ownership of the
* underlying memory.
*/
static VArray ForSpan(Span<T> values)
{
/* Cast const away, because the virtual array implementation for const and non const spans is
* shared. */
MutableSpan<T> span{const_cast<T *>(values.data()), values.size()};
return VArray::For<VArrayImpl_For_Span_final<T>>(span);
}
/**
* Construct a new virtual that will invoke the provided function whenever an element is
* accessed.
*/
template<typename GetFunc> static VArray ForFunc(const int64_t size, GetFunc get_func)
{
return VArray::For<VArrayImpl_For_Func<T, decltype(get_func)>>(size, std::move(get_func));
}
/**
* Construct a new virtual array for an existing span with a mapping function. This does not take
* ownership of the span.
*/
template<typename StructT, T (*GetFunc)(const StructT &)>
static VArray ForDerivedSpan(Span<StructT> values)
{
/* Cast const away, because the virtual array implementation for const and non const derived
* spans is shared. */
MutableSpan<StructT> span{const_cast<StructT *>(values.data()), values.size()};
return VArray::For<VArrayImpl_For_DerivedSpan<StructT, T, GetFunc>>(span);
}
/**
* Construct a new virtual array for an existing container. Every container that lays out the
* elements in a plain array works. This takes ownership of the passed in container. If that is
* not desired, use #ForSpan instead.
*/
template<typename ContainerT> static VArray ForContainer(ContainerT container)
{
return VArray::For<VArrayImpl_For_ArrayContainer<ContainerT>>(std::move(container));
}
VArray &operator=(const VArray &other)
{
this->copy_from(other);
return *this;
}
VArray &operator=(VArray &&other) noexcept
{
this->move_from(std::move(other));
return *this;
}
};
/**
* Similar to #VArray but references a virtual array that can be modified.
*/
template<typename T> class VMutableArray : public VArrayCommon<T> {
public:
VMutableArray() = default;
VMutableArray(const VMutableArray &other) = default;
VMutableArray(VMutableArray &&other) noexcept = default;
VMutableArray(const VMutableArrayImpl<T> *impl) : VArrayCommon<T>(impl)
{
}
VMutableArray(std::shared_ptr<const VMutableArrayImpl<T>> impl)
: VArrayCommon<T>(std::move(impl))
{
}
/**
* Construct a new virtual array for a custom #VMutableArrayImpl.
*/
template<typename ImplT, typename... Args> static VMutableArray For(Args &&...args)
{
static_assert(std::is_base_of_v<VMutableArrayImpl<T>, ImplT>);
VMutableArray varray;
varray.template emplace<ImplT>(std::forward<Args>(args)...);
return varray;
}
/**
* Construct a new virtual array for an existing span. This does not take ownership of the span.
*/
static VMutableArray ForSpan(MutableSpan<T> values)
{
return VMutableArray::For<VArrayImpl_For_Span_final<T>>(values);
}
/**
* Construct a new virtual array for an existing span with a mapping function. This does not take
* ownership of the span.
*/
template<typename StructT, T (*GetFunc)(const StructT &), void (*SetFunc)(StructT &, T)>
static VMutableArray ForDerivedSpan(MutableSpan<StructT> values)
{
return VMutableArray::For<VArrayImpl_For_DerivedSpan<StructT, T, GetFunc, SetFunc>>(values);
}
/** Convert to a #VArray by copying. */
operator VArray<T>() const &
{
VArray<T> varray;
varray.copy_from(*this);
return varray;
}
/** Convert to a #VArray by moving. */
operator VArray<T>() &&noexcept
{
VArray<T> varray;
varray.move_from(std::move(*this));
return varray;
}
VMutableArray &operator=(const VMutableArray &other)
{
this->copy_from(other);
return *this;
}
VMutableArray &operator=(VMutableArray &&other) noexcept
{
this->move_from(std::move(other));
return *this;
}
/**
* Get access to the internal span. This invokes undefined behavior if the #is_span returned
* false.
*/
MutableSpan<T> get_internal_span() const
{
BLI_assert(this->is_span());
const Span<T> span = this->impl_->get_internal_span();
return MutableSpan<T>(const_cast<T *>(span.data()), span.size());
}
/**
* Set the value at the given index.
*/
void set(const int64_t index, T value)
{
BLI_assert(index >= 0);
BLI_assert(index < this->size());
this->get_impl()->set(index, std::move(value));
}
/**
* Copy the values from the source span to all elements in the virtual array.
*/
void set_all(Span<T> src)
{
BLI_assert(src.size() == this->size());
this->get_impl()->set_all(src);
}
/** See #GVMutableArrayImpl::try_assign_GVMutableArray. */
bool try_assign_GVMutableArray(GVMutableArray &varray) const
{
return this->get_impl()->try_assign_GVMutableArray(varray);
}
private:
/** Utility to get the pointer to the wrapped #VMutableArrayImpl. */
VMutableArrayImpl<T> *get_impl() const
{
/* This cast is valid by the invariant that a #VMutableArray->impl_ is always a
* #VMutableArrayImpl. */
return (VMutableArrayImpl<T> *)this->impl_;
}
};
/**
* In many cases a virtual array is a span internally. In those cases, access to individual could
* be much more efficient than calling a virtual method. When the underlying virtual array is not a
* span, this class allocates a new array and copies the values over.
*
* This should be used in those cases:
* - All elements in the virtual array are accessed multiple times.
* - In most cases, the underlying virtual array is a span, so no copy is necessary to benefit
* from faster access.
* - An API is called, that does not accept virtual arrays, but only spans.
*/
template<typename T> class VArray_Span final : public Span<T> {
private:
VArray<T> varray_;
Array<T> owned_data_;
public:
VArray_Span(VArray<T> varray) : Span<T>(), varray_(std::move(varray))
{
this->size_ = varray_.size();
if (varray_.is_span()) {
this->data_ = varray_.get_internal_span().data();
}
else {
owned_data_.~Array();
new (&owned_data_) Array<T>(varray_.size(), NoInitialization{});
varray_.materialize_to_uninitialized(owned_data_);
this->data_ = owned_data_.data();
}
}
};
/**
* Same as #VArray_Span, but for a mutable span.
* The important thing to note is that when changing this span, the results might not be
* immediately reflected in the underlying virtual array (only when the virtual array is a span
* internally). The #save method can be used to write all changes to the underlying virtual array,
* if necessary.
*/
template<typename T> class VMutableArray_Span final : public MutableSpan<T> {
private:
VMutableArray<T> varray_;
Array<T> owned_data_;
bool save_has_been_called_ = false;
bool show_not_saved_warning_ = true;
public:
/* Create a span for any virtual array. This is cheap when the virtual array is a span itself. If
* not, a new array has to be allocated as a wrapper for the underlying virtual array. */
VMutableArray_Span(VMutableArray<T> varray, const bool copy_values_to_span = true)
: MutableSpan<T>(), varray_(std::move(varray))
{
this->size_ = varray_.size();
if (varray_.is_span()) {
this->data_ = varray_.get_internal_span().data();
}
else {
if (copy_values_to_span) {
owned_data_.~Array();
new (&owned_data_) Array<T>(varray_.size(), NoInitialization{});
varray_.materialize_to_uninitialized(owned_data_);
}
else {
owned_data_.reinitialize(varray_.size());
}
this->data_ = owned_data_.data();
}
}
~VMutableArray_Span()
{
if (show_not_saved_warning_) {
if (!save_has_been_called_) {
std::cout << "Warning: Call `save()` to make sure that changes persist in all cases.\n";
}
}
}
/* Write back all values from a temporary allocated array to the underlying virtual array. */
void save()
{
save_has_been_called_ = true;
if (this->data_ != owned_data_.data()) {
return;
}
varray_.set_all(owned_data_);
}
void disable_not_applied_warning()
{
show_not_saved_warning_ = false;
}
};
template<typename T> class SingleAsSpan {
private:
T value_;
int64_t size_;
public:
SingleAsSpan(T value, int64_t size) : value_(std::move(value)), size_(size)
{
BLI_assert(size_ >= 0);
}
SingleAsSpan(const VArray<T> &varray) : SingleAsSpan(varray.get_internal_single(), varray.size())
{
}
const T &operator[](const int64_t index) const
{
BLI_assert(index >= 0);
BLI_assert(index < size_);
UNUSED_VARS_NDEBUG(index);
return value_;
}
};
/**
* Generate multiple versions of the given function optimized for different virtual arrays.
* One has to be careful with nesting multiple devirtualizations, because that results in an
* exponential number of function instantiations (increasing compile time and binary size).
*
* Generally, this function should only be used when the virtual method call overhead to get an
* element from a virtual array is significant.
*/
template<typename T, typename Func>
inline void devirtualize_varray(const VArray<T> &varray, const Func &func, bool enable = true)
{
/* Support disabling the devirtualization to simplify benchmarking. */
if (enable) {
if (varray.is_single()) {
func(SingleAsSpan<T>(varray));
return;
}
if (varray.is_span()) {
func(varray.get_internal_span());
return;
}
}
func(varray);
}
/**
* Same as `devirtualize_varray`, but devirtualizes two virtual arrays at the same time.
* This is better than nesting two calls to `devirtualize_varray`, because it instantiates fewer
* cases.
*/
template<typename T1, typename T2, typename Func>
inline void devirtualize_varray2(const VArray<T1> &varray1,
const VArray<T2> &varray2,
const Func &func,
bool enable = true)
{
/* Support disabling the devirtualization to simplify benchmarking. */
if (enable) {
const bool is_span1 = varray1.is_span();
const bool is_span2 = varray2.is_span();
const bool is_single1 = varray1.is_single();
const bool is_single2 = varray2.is_single();
if (is_span1 && is_span2) {
func(varray1.get_internal_span(), varray2.get_internal_span());
return;
}
if (is_span1 && is_single2) {
func(varray1.get_internal_span(), SingleAsSpan(varray2));
return;
}
if (is_single1 && is_span2) {
func(SingleAsSpan(varray1), varray2.get_internal_span());
return;
}
if (is_single1 && is_single2) {
func(SingleAsSpan(varray1), SingleAsSpan(varray2));
return;
}
}
/* This fallback is used even when one of the inputs could be optimized. It's probably not worth
* it to optimize just one of the inputs, because then the compiler still has to call into
* unknown code, which inhibits many compiler optimizations. */
func(varray1, varray2);
}
} // namespace blender
|