-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsoa_vector.hpp
More file actions
1068 lines (927 loc) · 37.7 KB
/
Copy pathsoa_vector.hpp
File metadata and controls
1068 lines (927 loc) · 37.7 KB
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
/*
soa_vector.hpp
MIT license (2018)
Header repository : https://github.com/Dwarfobserver/soa_vector
You can contact me at sidney.congard@gmail.com
*/
#pragma once
#include <cstring>
#include <cstddef>
#include <utility>
#include <memory>
#include <tuple>
#include <string>
#include <string_view>
#include <typeinfo>
#if __has_include(<cxxabi.h>)
#include <cxxabi.h>
#endif
namespace soa {
// Holds arrays for each T component in a single allocation.
// The allocator will be rebound to 'std::byte'.
template <class T, class Allocator = std::allocator<T>>
class vector;
// Iterable object accessed in soa::vector<Aggregate> through soa::member<Aggregate>.
template <size_t Pos, class Aggregate, class T>
class vector_span;
// Specialized for aggregates so soa::vector<T> can be istanciated.
// Specialization of non-template types can be done with the macro
// 'SOA_DEFINE_TYPE(type, members...);' in the global namespace.
template <class Aggregate>
struct members {};
// These proxy types are defined with the macro. They are created when iterating on a
// soa::vector and mimic the given aggregate members as references.
template <class Aggregate>
struct ref_proxy {};
template <class Aggregate>
struct cref_proxy {};
// A trait allows to check if the three class above have been defined for the given type.
template <class Aggregate>
constexpr bool is_defined_v =
!std::is_empty_v<members <Aggregate>> &&
!std::is_empty_v<ref_proxy <Aggregate>> &&
!std::is_empty_v<cref_proxy<Aggregate>>;
namespace detail {
namespace impl {
template <class T, size_t I>
using indexed_alias = T;
template <class T, class Seq>
struct repeat_tuple {};
template <class T, size_t...Is>
struct repeat_tuple<T, std::index_sequence<Is...>> {
using type = std::tuple<indexed_alias<T, Is>...>;
};
}
// Equivalent of std::tuple<T, T, T...N times>.
template <class T, size_t N>
using repeat_tuple_t = typename impl::repeat_tuple<T, std::make_index_sequence<N>>::type;
namespace impl {
template <size_t I, class...Ts>
struct get {};
template <size_t I, class T, class...Ts>
struct get<I, T, Ts...> {
using type = typename get<I - 1, Ts...>::type;
};
template <class T, class...Ts>
struct get<0, T, Ts...> {
using type = T;
};
}
// An empty type used to pass types.
template <class...Ts>
struct type_tag {
template <size_t I>
using get = typename impl::get<I, Ts...>::type;
using type = get<0>;
};
namespace impl {
template <class Tuple>
struct tuple_tag {};
template <class...Ts>
struct tuple_tag<std::tuple<Ts...>> {
using type = type_tag<Ts...>;
};
}
// std::tuple<Ts...> gives type_tag<Ts...>.
template <class Tuple>
using tuple_tag = typename impl::tuple_tag<Tuple>::type;
// Base class of soa::vector<T>.
// Used to retrieve the size by soa::vector_span<Offset, T, MemberT> from members<T>.
template <class T>
class members_with_size : public members<T> {
template <size_t, class, class>
friend class ::soa::vector_span;
protected:
int size_;
};
template <class T>
auto type_name() {
auto const name = typeid(T).name();
#if __has_include(<cxxabi.h>)
int errc;
unsigned long size;
auto const free_ptr = std::free;
auto const demangled_name = std::unique_ptr<char, decltype(free_ptr)>{
abi::__cxa_demangle(name, nullptr, &size, &errc),
free_ptr
};
return errc
? std::string{ name }
: std::string(demangled_name.get(), size);
#else
return std::string_view{ name };
#endif
}
template <class...Strings>
std::string concatene(Strings const&...str) {
auto message = std::string{};
message.reserve((0 + ... + str.size()));
(message.append(str), ...);
return message;
}
template <class T>
[[noreturn]]
void throw_out_of_range(int index, int size) {
using namespace std::literals;
throw std::out_of_range{detail::concatene(
"Out of bounds access when calling "sv, detail::type_name<T>(), "::at("sv,
std::to_string(index), ") while size = "sv, std::to_string(size)
)};
}
} // ::detail
template <size_t Pos, class Aggregate, class T>
class vector_span {
template <class, class>
friend class vector;
template <class>
friend struct members;
public:
using value_type = T;
// Informations
T * data() noexcept { return ptr_; }
T const* data() const noexcept { return ptr_; }
int size() const noexcept;
// Accessors
T & operator[](int i) noexcept { return ptr_[i]; }
T const& operator[](int i) const noexcept { return ptr_[i]; }
T & at(int i) { check_at(i); return ptr_[i]; }
T const& at(int i) const { check_at(i); return ptr_[i]; }
T & front() noexcept { return ptr_[0]; }
T const& front() const noexcept { return ptr_[0]; }
T & back() noexcept { return ptr_[size() - 1]; }
T const& back() const noexcept { return ptr_[size() - 1]; }
// Iterators
T * begin() noexcept { return ptr_; }
T const* begin() const noexcept { return ptr_; }
T * end() noexcept { return ptr_ + size(); }
T const* end() const noexcept { return ptr_ + size(); }
private:
void check_at(int i) const {
if (i >= size()) detail::throw_out_of_range<vector_span<Pos, Aggregate, T>>(i, size());
}
vector_span() = default;
vector_span(vector_span const&) = default;
vector_span& operator=(vector_span const&) = default;
vector_span(std::byte * ptr) noexcept :
ptr_{ reinterpret_cast<T *>(ptr) }
{}
T * ptr_;
};
// Template arguments are used to retrieve the size from detail::members_with_size<Aggregate>.
template <size_t Pos, class Aggregate, class T>
int vector_span<Pos, Aggregate, T>::size() const noexcept {
auto const mem_ptr = reinterpret_cast<members<Aggregate> const*>(this - Pos);
auto const mws_ptr = static_cast<detail::members_with_size<Aggregate> const*>(mem_ptr);
return mws_ptr->size_;
}
namespace detail {
// Aggregate to tuple implementation, only for soa::member<T>.
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 1>) {
auto & [v1] = agg;
return std::forward_as_tuple(v1);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 2>) {
auto & [v1, v2] = agg;
return std::forward_as_tuple(v1, v2);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 3>) {
auto & [v1, v2, v3] = agg;
return std::forward_as_tuple(v1, v2, v3);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 4>) {
auto & [v1, v2, v3, v4] = agg;
return std::forward_as_tuple(v1, v2, v3, v4);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 5>) {
auto & [v1, v2, v3, v4, v5] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 6>) {
auto & [v1, v2, v3, v4, v5, v6] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 7>) {
auto & [v1, v2, v3, v4, v5, v6, v7] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 8>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 9>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 10>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 11>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 12>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 13>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 14>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 15>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 16>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 17>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 18>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 19>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19);
}
template <class T>
auto as_tuple(T & agg, std::integral_constant<int, 20>) {
auto & [v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20] = agg;
return std::forward_as_tuple(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20);
}
// The arity is the number of members of a well-formed soa::member<T>.
template <class Members>
constexpr int arity_v = sizeof(Members) / sizeof(vector_span<0, vector<char>, char>);
// Continue the overloads above to increase the max_arity.
constexpr int max_arity = 10;
// Converts a well-formed soa::member<T> to a tuple with references on each member of the class.
template <class T>
auto as_tuple(members<T> const& agg) {
return as_tuple(agg, std::integral_constant<int, arity_v<members<T>>>{});
}
template <class T>
auto as_tuple(members<T> & agg) {
return as_tuple(agg, std::integral_constant<int, arity_v<members<T>>>{});
}
// Allows to converts any aggregate to a tuple given it's arity.
template <size_t Arity, class T>
auto as_tuple(T && agg) {
return as_tuple(agg, std::integral_constant<int, Arity>{});
}
// for_each loops takes a function object to operate on one or two tuples of references.
// Note : C++20 template lambdas would be cleaner to retrieve the type.
template <class F, size_t...Is, class...Ts>
constexpr void for_each(std::tuple<Ts &...> const& tuple, F && f, std::index_sequence<Is...>) {
(f(std::get<Is>(tuple), type_tag<typename Ts::value_type>{}), ...);
}
template <class F, class...Ts>
constexpr void for_each(std::tuple<Ts &...> const& tuple, F && f) {
using seq = std::make_index_sequence<sizeof...(Ts)>;
detail::for_each(tuple, f, seq{});
}
template <class F, size_t...Is, class...Ts1, class...Ts2>
constexpr void for_each(std::tuple<Ts1 &...> const& t1, std::tuple<Ts2 &...> const& t2, F && f, std::index_sequence<Is...>) {
(f(std::get<Is>(t1), std::get<Is>(t2), type_tag<typename Ts1::value_type>{}), ...);
}
template <class F, class...Ts1, class...Ts2>
constexpr void for_each(std::tuple<Ts1 &...> const& t1, std::tuple<Ts2 &...> const& t2, F && f) {
static_assert(sizeof...(Ts1) == sizeof...(Ts2));
using seq = std::make_index_sequence<sizeof...(Ts1)>;
detail::for_each(t1, t2, f, seq{});
}
// Apply a function for every object in two ranges.
// Used for soa::vector copy/move assignments and constructors.
template <class T, class SizeT, class F>
constexpr void apply_two_arrays(T const* __restrict src, T * dst, SizeT size, F&& f) {
for (SizeT i = 0; i < size; ++i) {
f(src[i], dst[i]);
}
}
template <class T, class SizeT, class F>
constexpr void apply_two_arrays(T * __restrict src, T * dst, SizeT size, F&& f) {
for (SizeT i = 0; i < size; ++i) {
f(src[i], dst[i]);
}
}
// Iterator used by soa::vector to return new proxies with references to the elements.
template <class Vector, bool IsConst>
class proxy_iterator {
friend Vector;
using vector_pointer_type = std::conditional_t<IsConst,
Vector const*,
Vector *>;
vector_pointer_type vec_;
int index_;
proxy_iterator(vector_pointer_type vec, int index) noexcept :
vec_{vec}, index_{index} {}
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = std::conditional_t<IsConst,
typename Vector::const_reference_type,
typename Vector::reference_type>;
using reference = value_type;
using pointer = void;
using difference_type = int;
private:
template <size_t...Is>
value_type make_proxy(std::index_sequence<Is...>) const noexcept {
return { vec_->template get_span<Is>()[index_] ... };
}
public:
value_type operator*() const noexcept { return make_proxy(typename Vector::sequence_type{}); }
bool operator==(proxy_iterator const& rhs) const noexcept { return index_ == rhs.index_; }
bool operator!=(proxy_iterator const& rhs) const noexcept { return !(*this == rhs); }
bool operator<(proxy_iterator const& rhs) const noexcept { return index_ < rhs.index_; }
bool operator>(proxy_iterator const& rhs) const noexcept { return rhs < *this; }
bool operator<=(proxy_iterator const& rhs) const noexcept { return !(rhs < *this); }
bool operator>=(proxy_iterator const& rhs) const noexcept { return !(*this < rhs); }
proxy_iterator & operator++() noexcept { return ++index_, *this; }
proxy_iterator & operator--() noexcept { return --index_, *this; }
proxy_iterator & operator++(int) noexcept { const auto old = *this; return ++index_, old; }
proxy_iterator & operator--(int) noexcept { const auto old = *this; return --index_, old; }
proxy_iterator & operator+=(int shift) noexcept { return index_ += shift, *this; }
proxy_iterator & operator-=(int shift) noexcept { return index_ -= shift, *this; }
proxy_iterator operator+(int shift) const noexcept { return { vec_, index_ + shift }; }
proxy_iterator operator-(int shift) const noexcept { return { vec_, index_ - shift }; }
int operator-(proxy_iterator const& rhs) const noexcept { return index_ - rhs.index_; }
};
} // ::detail
// Stores components of the aggregate T (given by the specialization soa::member<T>)
// in successives arrays from an unique continuous allocation.
// It increases the performance when the access patterns are differents for the
// aggregate's members.
// Over-aligned types are not supported.
template <class T, class Allocator>
class vector : public detail::members_with_size<T> {
public:
static_assert(is_defined_v<T>,
"soa::vector<T> can't be instancied because the required types 'soa::members<T>', "
"'soa::ref_proxy<T>' or 'soa::cref_proxy<T>' haven't been defined. "
"Did you forget to call the macro SOA_DEFINE_TYPE(T, members...) ?");
// The given allocator is reboud to std::byte to store the different member types.
using allocator_type = typename std::allocator_traits<Allocator>::template rebind_alloc<std::byte>;
using value_type = T;
using reference_type = ref_proxy<T>;
using const_reference_type = cref_proxy<T>;
using iterator = detail::proxy_iterator<vector, false>;
using const_iterator = detail::proxy_iterator<vector, true>;
// The number of T members.
static constexpr int components_count = detail::arity_v<members<T>>;
// Constructors.
vector(Allocator allocator = Allocator{}) noexcept;
vector(vector && rhs) noexcept;
vector(vector const& rhs);
// Assignments.
vector& operator=(vector && rhs) noexcept;
vector& operator=(vector const& rhs);
// Destructor.
~vector();
// Size or capacity modifiers.
void clear() noexcept;
void reserve(int capacity);
void resize(int size);
void resize(int size, T const& value);
void shrink_to_fit();
// Add and remove an element.
template <class...Ts>
void emplace_back(Ts &&...components);
void push_back(T const& value);
void push_back(T && value);
void pop_back() noexcept;
// Informations.
int size() const noexcept { return this->size_; }
int capacity() const noexcept { return capacity_; }
bool empty() const noexcept { return size() == 0; }
// Accessors.
reference_type operator[](int i) noexcept { return *(begin() + i); }
const_reference_type operator[](int i) const noexcept { return *(begin() + i); }
reference_type at(int i) { check_at(i); return *(begin() + i); }
const_reference_type at(int i) const { check_at(i); return *(begin() + i); }
reference_type front() noexcept { return *begin(); }
const_reference_type front() const noexcept { return *begin(); }
reference_type back() noexcept { return *(end() - 1); }
const_reference_type back() const noexcept { return *(end() - 1); }
// Iterators.
iterator begin() noexcept { return { this, 0 }; }
const_iterator begin() const noexcept { return { this, 0 }; }
const_iterator cbegin() const noexcept { return begin(); }
iterator end() noexcept { return { this, size() }; }
const_iterator end() const noexcept { return { this, size() }; }
const_iterator cend() const noexcept { return end(); }
// Components accessors.
template <size_t I>
auto & get_span() noexcept;
template <size_t I>
auto const & get_span() const noexcept;
private:
friend iterator;
friend const_iterator;
// Some static asserts on the soa::member<T> type.
// Workaround MSVC : must returns a value to be constexpr.
static constexpr int check_members();
static constexpr int check_members_trigger = check_members();
// Explicit cast to base class.
members<T>& base() noexcept { return *this; }
members<T>const& base() const noexcept { return *this; }
detail::members_with_size<T>& base_with_size() noexcept { return *this; }
detail::members_with_size<T> const& base_with_size() const noexcept { return *this; }
using sequence_type = std::make_index_sequence<components_count>;
// components_tag = detail::type_tag<Ts...>.
template <class Tuple>
struct components_tag_impl;
template <class...Members>
struct components_tag_impl<std::tuple<Members&...>> {
using type = detail::type_tag<typename Members::value_type...>;
};
using components_tag = typename components_tag_impl<decltype(
detail::as_tuple(std::declval<members<T>>())
)>::type;
using allocator_traits = std::allocator_traits<allocator_type>;
// Functions implementations.
void check_at(int index) const;
template <class Tuple, size_t...Is>
void push_back_copy(Tuple const& tuple, std::index_sequence<Is...>);
template <class Tuple, size_t...Is>
void push_back_move(Tuple& tuple, std::index_sequence<Is...>);
template <size_t I, class...Members, class T1, class...Ts>
void emplace_back_impl(std::tuple<Members&...> const& members, T1&& component, Ts&&...nexts);
template <size_t I, class...Members>
void emplace_back_impl(std::tuple<Members&...> const& members);
// Computes the bytes padding for each component,
// assuming we start with a 8-bytes aligned address.
template <size_t I, class...Ints>
static void update_shift(std::tuple<Ints...>& shifts, int nb, int acc);
// Creates vector_spans based on the data allocated
// and the computed shift for each component.
template <class Tuple, size_t...Is>
static members<T> create_members(std::byte* ptr, Tuple const& shift, std::index_sequence<Is...>);
struct alloc_result {
members<T> new_members;
int nb_bytes;
};
// Allocates unitialized array of 'nb' elements.
alloc_result allocate(int nb);
static void construct_copy_array(members<T> const& src, members<T>& dst, int nb);
static void construct_move_array(members<T> & src, members<T>& dst, int nb);
template <class F>
static void apply_on_arrays(members<T> const& mem_src, members<T> & mem_dst, int nb, F && f);
template <class F>
static void apply_on_arrays(members<T> & mem_src, members<T> & mem_dst, int nb, F && f);
void destroy() noexcept;
void destroy(int begin, int end) noexcept;
void deallocate() noexcept;
// Sets the vector fields (size, capacity, ...) according to an empty vector.
void to_zero() noexcept;
int capacity_;
allocator_type allocator_;
int nb_bytes_;
};
// soa::vector implementation.
// The check function returns an arbitrary value to be executed at compile-time :
// The msvc version used don't support constexpr void functions.
template <class T, class Allocator>
constexpr int vector<T, Allocator>::check_members() {
static_assert(!std::is_empty_v<members<T>>,
"soa::members<T> must be specialized to hold "
"an soa::vector_span for each member of T");
static_assert(detail::arity_v<members<T>> <= detail::max_arity,
"soa::members<T> must have less than 'max_arity' members. "
"This limit can be increased by writing more overloads of 'as_tuple'.");
return 0;
}
// Constructors.
template <class T, class Allocator>
vector<T, Allocator>::vector(Allocator allocator) noexcept :
detail::members_with_size<T>{},
capacity_ { 0 },
allocator_{ allocator },
nb_bytes_ { 0 }
{}
template <class T, class Allocator>
vector<T, Allocator>::vector(vector&& rhs) noexcept :
detail::members_with_size<T>{ rhs.base_with_size() },
capacity_ { rhs.capacity() },
allocator_{ rhs.allocator_ },
nb_bytes_ { rhs.nb_bytes_ }
{
rhs.to_zero();
}
template <class T, class Allocator>
vector<T, Allocator>::vector(vector const& rhs) :
detail::members_with_size<T>{ rhs.base_with_size() },
capacity_ { rhs.size() },
allocator_{ rhs.allocator_ },
nb_bytes_ { rhs.nb_bytes_ }
{
if (rhs.empty()) return;
auto [new_members, nb_bytes] = allocate(size());
construct_copy_array(rhs.base(), new_members, size());
base() = new_members;
nb_bytes_ = nb_bytes;
}
// Assignments.
template <class T, class Allocator>
vector<T, Allocator>& vector<T, Allocator>::operator=(vector&& rhs) noexcept {
destroy();
deallocate();
base_with_size() = rhs.base_with_size();
capacity_ = rhs.capacity();
allocator_ = rhs.allocator_;
nb_bytes_ = rhs.nb_bytes_;
rhs.to_zero();
return *this;
}
template <class T, class Allocator>
vector<T, Allocator>& vector<T, Allocator>::operator=(vector const& rhs) {
destroy();
this->size_ = rhs.size();
if (capacity() < size()) {
deallocate();
auto [new_members, nb_bytes] = allocate(size());
base() = new_members;
nb_bytes_ = nb_bytes;
capacity_ = size();
}
construct_copy_array(rhs.base(), base(), size());
return *this;
}
// Destructor.
template <class T, class Allocator>
vector<T, Allocator>::~vector() {
destroy();
deallocate();
}
// Size & capacity modifiers.
template <class T, class Allocator>
void vector<T, Allocator>::clear() noexcept {
destroy();
this->size_ = 0;
}
template <class T, class Allocator>
void vector<T, Allocator>::reserve(int capacity) {
if (capacity <= this->capacity()) return;
auto [new_members, nb_bytes] = allocate(capacity);
construct_move_array(base(), new_members, size());
base() = new_members;
nb_bytes_ = nb_bytes;
capacity_ = capacity;
}
template <class T, class Allocator>
void vector<T, Allocator>::resize(int size) {
if (size <= this->size()) {
destroy(size, this->size());
this->size_ = size;
return;
}
reserve(size);
detail::for_each(detail::as_tuple(base()), [this, size] (auto& span, auto tag) {
using type = typename decltype(tag)::type;
auto it = span.begin() + this->size();
auto const end = span.begin() + size;
for (; it < end; ++it) {
new (it) type();
}
});
this->size_ = size;
}
template <class T, class Allocator>
void vector<T, Allocator>::resize(int size, T const& value) {
if (size <= this->size()) {
destroy(size, this->size());
this->size_ = size;
return;
}
reserve(size);
auto const tuple = detail::as_tuple<components_count>(value);
detail::for_each(detail::as_tuple(base()), tuple, [this, size] (auto& span, auto& val, auto tag) {
using type = typename decltype(tag)::type;
auto it = span.begin() + this->size();
auto const end = span.begin() + size;
while (it < end) {
new (it) type(val); ++it;
}
});
this->size_ = size;
}
template <class T, class Allocator>
void vector<T, Allocator>::shrink_to_fit() {
if (size() == capacity()) return;
reallocate(size());
}
// Add and remove an element.
template <class T, class Allocator>
void vector<T, Allocator>::push_back(T const& value) {
auto const tuple = detail::as_tuple<components_count>(value);
push_back_copy(tuple, sequence_type{});
}
template <class T, class Allocator>
void vector<T, Allocator>::push_back(T&& value) {
auto tuple = detail::as_tuple<components_count>(value);
push_back_move(tuple, sequence_type{});
}
template <class T, class Allocator>
template <class...Ts>
void vector<T, Allocator>::emplace_back(Ts&&...components) {
if (size() == capacity()) {
auto const new_capacity = size() == 0 ? 1 : capacity() * 2;
reserve(new_capacity);
}
emplace_back_impl<0>(detail::as_tuple(base()), std::forward<Ts>(components)...);
++this->size_;
}
template <class T, class Allocator>
void vector<T, Allocator>::pop_back() noexcept {
--this->size_;
detail::for_each(detail::as_tuple(base()), [this] (auto& span, auto tag) {
using type = typename decltype(tag)::type;
span[size()].~type();
});
}
// Components accessors.
template <class T, class Allocator>
template <size_t I>
auto& vector<T, Allocator>::get_span() noexcept {
static_assert(I < components_count);
return std::get<I>(detail::as_tuple(base()));
}
template <class T, class Allocator>
template <size_t I>
auto const& vector<T, Allocator>::get_span() const noexcept {
static_assert(I < components_count);
return std::get<I>(detail::as_tuple(base()));
}
// Private functions.
template <class T, class Allocator>
void vector<T, Allocator>::check_at(int i) const {
if (i >= size()) detail::throw_out_of_range<vector<T, Allocator>>(i, size());
}
template <class T, class Allocator>
void vector<T, Allocator>::construct_copy_array(members<T> const& mem_src, members<T>& mem_dst, int nb) {
apply_on_arrays(mem_src, mem_dst, nb, [] (auto src, auto & dst) {
using type = decltype(src);
new (&dst) type(src);
});
}
template <class T, class Allocator>
void vector<T, Allocator>::construct_move_array(members<T> & mem_src, members<T> & mem_dst, int nb) {
apply_on_arrays(mem_src, mem_dst, nb, [] (auto & src, auto & dst) {
using type = std::remove_reference_t<decltype(src)>;
new (&dst) type(std::move(src));
});
}
template <class T, class Allocator>
template <class F>
void vector<T, Allocator>::apply_on_arrays(members<T> const& mem_src, members<T> & mem_dst, int nb, F && f) {
auto const t1 = detail::as_tuple(mem_src);
auto const t2 = detail::as_tuple(mem_dst);
detail::for_each(t1, t2, [f, nb] (auto const& span_src, auto & span_dst, auto) {
detail::apply_two_arrays(span_src.data(), span_dst.data(), nb, f);
});
}
template <class T, class Allocator>
template <class F>
void vector<T, Allocator>::apply_on_arrays(members<T> & mem_src, members<T> & mem_dst, int nb, F && f) {
auto const t1 = detail::as_tuple(mem_src);
auto const t2 = detail::as_tuple(mem_dst);
detail::for_each(t1, t2, [f, nb] (auto & span_src, auto & span_dst, auto) {
detail::apply_two_arrays(span_src.data(), span_dst.data(), nb, f);
});
}
template <class T, class Allocator>
template <class Tuple, size_t...Is>
void vector<T, Allocator>::push_back_copy(Tuple const& tuple, std::index_sequence<Is...>) {
emplace_back(std::get<Is>(tuple)...);
}
template <class T, class Allocator>
template <class Tuple, size_t...Is>
void vector<T, Allocator>::push_back_move(Tuple& tuple, std::index_sequence<Is...>) {
emplace_back(std::move(std::get<Is>(tuple))...);
}
template <class T, class Allocator>
template <size_t I, class...Members, class T1, class...Ts>
void vector<T, Allocator>::emplace_back_impl(std::tuple<Members&...> const& tuple, T1&& component, Ts&&...nexts) {
if constexpr (I < sizeof...(Members)) {
using type = typename components_tag::template get<I>;
auto const it = std::get<I>(tuple).ptr_ + size();
new (it) type(std::forward<T1>(component));
emplace_back_impl<I + 1>(tuple, std::forward<Ts>(nexts)...);
}
}
template <class T, class Allocator>
template <size_t I, class...Members>
void vector<T, Allocator>::emplace_back_impl(std::tuple<Members&...> const& tuple) {
if constexpr (I < sizeof...(Members)) {
using type = typename components_tag::template get<I>;
auto const it = std::get<I>(tuple).ptr_ + size();
new (it) type();
emplace_back_impl<I + 1>(tuple);
}
}
template <class T, class Allocator>
template <size_t I, class...Ints>
void vector<T, Allocator>::update_shift(std::tuple<Ints...>& tuple, int nb, int shift) {
using prev = typename components_tag::template get<I - 1>;
if constexpr (I == sizeof...(Ints) - 1) {
std::get<I>(tuple) = shift + nb * sizeof(prev);
}
else {
using type = typename components_tag::template get<I>;
constexpr auto align = alignof(type) - 1;
shift += (nb * sizeof(prev) + align) & ~align;
std::get<I>(tuple) = shift;
update_shift<I + 1>(tuple, nb, shift);
}
}
template <class T, class Allocator>
template <class Tuple, size_t...Is>
members<T> vector<T, Allocator>::create_members(std::byte* ptr, Tuple const& shift, std::index_sequence<Is...>) {
return { (ptr + std::get<Is>(shift))... };
}
template <class T, class Allocator>
typename vector<T, Allocator>::alloc_result
vector<T, Allocator>::allocate(int nb) {
constexpr int arity = detail::arity_v<members<T>>;
auto shift = detail::repeat_tuple_t<int, arity + 1>{};
update_shift<1>(shift, nb, 0);
auto const nb_bytes = std::get<arity>(shift);
auto const ptr = allocator_traits::allocate(allocator_, nb_bytes);
return { create_members(ptr, shift, sequence_type{}), nb_bytes };
}
template <class T, class Allocator>
void vector<T, Allocator>::destroy() noexcept {
detail::for_each(detail::as_tuple(base()), [] (auto& span, auto tag) {
using type = typename decltype(tag)::type;
for (auto& val : span) val.~type();
});
}
template <class T, class Allocator>
void vector<T, Allocator>::destroy(int begin, int end) noexcept {
detail::for_each(detail::as_tuple(base()), [min = begin, max = end] (auto& span, auto tag) {
using type = typename decltype(tag)::type;
auto it = span.begin() + min;
auto const end = span.begin() + max;
for (; it < end; ++it) it->~type();
});
}
template <class T, class Allocator>
void vector<T, Allocator>::deallocate() noexcept {
if (capacity() == 0) return;
auto const data = reinterpret_cast<std::byte*>(get_span<0>().ptr_);
allocator_traits::deallocate(allocator_, data, nb_bytes_);
}
template <class T, class Allocator>
void vector<T, Allocator>::to_zero() noexcept {
base_with_size() = {};
capacity_ = 0;
nb_bytes_ = 0;
}
} // namespace soa
// Private macros.
#define SOA_PP_EMPTY
#define SOA_PP_EMPTY_ARGS(...)
#define SOA_PP_EVAL0(...) __VA_ARGS__
#define SOA_PP_EVAL1(...) SOA_PP_EVAL0 (SOA_PP_EVAL0 (SOA_PP_EVAL0 (__VA_ARGS__)))
#define SOA_PP_EVAL2(...) SOA_PP_EVAL1 (SOA_PP_EVAL1 (SOA_PP_EVAL1 (__VA_ARGS__)))
#define SOA_PP_EVAL3(...) SOA_PP_EVAL2 (SOA_PP_EVAL2 (SOA_PP_EVAL2 (__VA_ARGS__)))
#define SOA_PP_EVAL4(...) SOA_PP_EVAL3 (SOA_PP_EVAL3 (SOA_PP_EVAL3 (__VA_ARGS__)))
#define SOA_PP_EVAL(...) SOA_PP_EVAL4 (SOA_PP_EVAL4 (SOA_PP_EVAL4 (__VA_ARGS__)))
#define SOA_PP_MAP_GET_END() 0, SOA_PP_EMPTY_ARGS
#define SOA_PP_MAP_NEXT0(item, next, ...) next SOA_PP_EMPTY
#if defined(_MSC_VER)
#define SOA_PP_MAP_NEXT1(item, next) SOA_PP_EVAL0(SOA_PP_MAP_NEXT0 (item, next, 0))
#else
#define SOA_PP_MAP_NEXT1(item, next) SOA_PP_MAP_NEXT0 (item, next, 0)
#endif
#define SOA_PP_MAP_NEXT(item, next) SOA_PP_MAP_NEXT1 (SOA_PP_MAP_GET_END item, next)
#define SOA_PP_MAP0(f, n, t, x, peek, ...) f(n, t, x) SOA_PP_MAP_NEXT (peek, SOA_PP_MAP1) (f, n+1, t, peek, __VA_ARGS__)
#define SOA_PP_MAP1(f, n, t, x, peek, ...) f(n, t, x) SOA_PP_MAP_NEXT (peek, SOA_PP_MAP0) (f, n+1, t, peek, __VA_ARGS__)
#define SOA_PP_MAP(f, t, ...) SOA_PP_EVAL (SOA_PP_MAP1 (f, 0, t, __VA_ARGS__, (), 0))
#define SOA_PP_MEMBER(nb, type, name) \
vector_span<nb, type, decltype(std::declval<type>().name)> name;
#define SOA_PP_REF(nb, type, name) \
decltype(std::declval<type>().name) & name;
#define SOA_PP_CREF(nb, type, name) \
decltype(std::declval<type>().name) const& name;
#define SOA_PP_COPY(nb, type, name) \
name = rhs.name;
#define SOA_PP_MOVE(nb, type, name) \
name = std::move(rhs.name);
#define SOA_PP_ENABLE_FOR_COPYABLE(type, alias) \
template <class alias, class = std::enable_if_t< \
std::is_same_v<alias, type> && \
std::is_copy_constructible_v<type> \
>>
// Shortcut to specialize soa::member<my_type>, by listing all the members
// in their declaration order. It must be used in the global namespace.
// Usage exemple :
//
// namespace user {
// struct person {
// std::string name;
// int age;
// };
// }
//
// SOA_DEFINE_TYPE(user::person, name, age);
//
// This is equivalent to typing :
//
// namespace soa {
// template <>
// struct members<user::person> {
// vector_span<0, user::person, std::string> name;