-
Notifications
You must be signed in to change notification settings - Fork 0
/
cntgs.hpp
4154 lines (3428 loc) · 143 KB
/
cntgs.hpp
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
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_CNTGS_CONTIGUOUS_HPP
#define CNTGS_CNTGS_CONTIGUOUS_HPP
// #include "cntgs/element.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_CNTGS_ELEMENT_HPP
#define CNTGS_CNTGS_ELEMENT_HPP
// #include "cntgs/detail/allocator.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_ALLOCATOR_HPP
#define CNTGS_DETAIL_ALLOCATOR_HPP
// #include "cntgs/detail/memory.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_MEMORY_HPP
#define CNTGS_DETAIL_MEMORY_HPP
// #include "cntgs/detail/attributes.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_ATTRIBUTES_HPP
#define CNTGS_DETAIL_ATTRIBUTES_HPP
#ifdef _MSC_VER
#define CNTGS_RESTRICT_RETURN __declspec(restrict)
#else
#define CNTGS_RESTRICT_RETURN __attribute__((malloc)) __attribute__((returns_nonnull))
#endif
#define CNTGS_RESTRICT __restrict
#endif // CNTGS_DETAIL_ATTRIBUTES_HPP
// #include "cntgs/detail/iterator.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_ITERATOR_HPP
#define CNTGS_DETAIL_ITERATOR_HPP
// #include "cntgs/detail/typeTraits.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_TYPETRAITS_HPP
#define CNTGS_DETAIL_TYPETRAITS_HPP
#include <cstddef>
#include <type_traits>
#include <utility>
#include <version>
namespace cntgs::detail
{
template <class Derived, class Base>
inline constexpr auto IS_DERIVED_FROM =
std::is_base_of_v<Base, Derived> && std::is_convertible_v<const volatile Derived*, const volatile Base*>;
#ifdef __cpp_lib_remove_cvref
template <class T>
using RemoveCvrefT = std::remove_cvref_t<T>;
#else
template <class T>
struct RemoveCvref
{
using Type = std::remove_cv_t<std::remove_reference_t<T>>;
};
template <class T>
using RemoveCvrefT = typename RemoveCvref<T>::Type;
#endif
template <bool B>
struct EqualSizeof
{
template <class, class>
static constexpr bool VALUE = false;
};
template <>
struct EqualSizeof<false>
{
template <class T, class U>
static constexpr bool VALUE = sizeof(T) == sizeof(U);
};
template <class T, class U>
inline constexpr bool EQUAL_SIZEOF =
EqualSizeof<(std::is_same_v<void, T> || std::is_same_v<void, U>)>::template VALUE<T, U>;
template <class T>
inline constexpr bool IS_BYTE = std::is_same_v<char, T> || std::is_same_v<signed char, T> ||
std::is_same_v<unsigned char, T> || std::is_same_v<std::byte, T>
#ifdef __cpp_char8_t
|| std::is_same_v<char8_t, T>
#endif
;
namespace has_adl_swap_detail
{
void swap(); // undefined (deliberate shadowing)
template <class, class = void>
inline constexpr bool HAS_ADL_SWAP = false;
template <class T>
inline constexpr bool HAS_ADL_SWAP<T, std::void_t<decltype(swap(std::declval<T&>(), std::declval<T&>()))>> = true;
} // namespace has_adl_swap_detail
// Implementation taken from MSVC _Is_trivially_swappable
template <class T>
inline constexpr bool IS_TRIVIALLY_SWAPPABLE =
std::is_trivially_destructible_v<T> && std::is_trivially_move_constructible_v<T> &&
std::is_trivially_move_assignable_v<T> && !has_adl_swap_detail::HAS_ADL_SWAP<T>;
template <class T>
struct IsTriviallySwappable : std::bool_constant<IS_TRIVIALLY_SWAPPABLE<T>>
{
};
template <bool B>
struct Conditional
{
template <class T, class>
using Type = T;
};
template <>
struct Conditional<false>
{
template <class, class U>
using Type = U;
};
template <bool B, class T, class U>
using ConditionalT = typename detail::Conditional<B>::template Type<T, U>;
template <class Lhs, class Rhs, class = void>
inline constexpr bool ARE_EQUALITY_COMPARABLE = false;
template <class Lhs, class Rhs>
inline constexpr bool
ARE_EQUALITY_COMPARABLE<Lhs, Rhs,
std::void_t<decltype(std::declval<const Lhs&>() == std::declval<const Rhs&>()),
decltype(std::declval<const Rhs&>() == std::declval<const Lhs&>())>> = true;
template <class T, class = void>
inline constexpr bool IS_NOTRHOW_EQUALITY_COMPARABLE = false;
template <class T>
inline constexpr bool
IS_NOTRHOW_EQUALITY_COMPARABLE<T, std::void_t<decltype(std::declval<const T&>() == std::declval<const T&>())>> =
noexcept(std::declval<const T&>() == std::declval<const T&>());
template <class T, class = void>
inline constexpr bool IS_NOTRHOW_LEXICOGRAPHICAL_COMPARABLE = false;
template <class T>
inline constexpr bool IS_NOTRHOW_LEXICOGRAPHICAL_COMPARABLE<
T, std::void_t<decltype(std::declval<const T&>() < std::declval<const T&>())>> =
noexcept(std::declval<const T&>() < std::declval<const T&>());
template <class T, class U>
inline constexpr bool MEMCPY_COMPATIBLE =
detail::EQUAL_SIZEOF<T, U> && std::is_trivially_copyable_v<T> && std::is_trivially_copyable_v<U> &&
std::is_floating_point_v<T> == std::is_floating_point_v<U>;
// Implementation taken from MSVC _Can_memcmp_elements
template <class T, class U = T,
bool = (detail::EQUAL_SIZEOF<T, U> && std::is_integral_v<T> && !std::is_volatile_v<T> &&
std::is_integral_v<U> && !std::is_volatile_v<U>)>
inline constexpr bool EQUALITY_MEMCMP_COMPATIBLE = std::is_same_v<T, bool> || std::is_same_v<U, bool> || T(-1) == U(-1);
template <>
inline constexpr bool EQUALITY_MEMCMP_COMPATIBLE<std::byte, std::byte, false> = true;
template <class T, class U>
inline constexpr bool EQUALITY_MEMCMP_COMPATIBLE<T*, U*, false> =
std::is_same_v<std::remove_cv_t<T>, std::remove_cv_t<U>>;
template <class T, class U>
inline constexpr bool EQUALITY_MEMCMP_COMPATIBLE<T, U, false> = false;
template <class T>
struct EqualityMemcmpCompatible
: std::bool_constant<detail::EQUALITY_MEMCMP_COMPATIBLE<std::remove_const_t<T>, std::remove_const_t<T>>>
{
};
// Implementation taken from MSVC+GCC _Lex_compare_check_element_types_helper and __is_memcmp_ordered
template <class T, bool = detail::IS_BYTE<T>>
inline constexpr bool LEXICOGRAPHICAL_MEMCMP_COMPATIBLE = T(-1) > T(1) && !std::is_volatile_v<T>;
template <>
inline constexpr bool LEXICOGRAPHICAL_MEMCMP_COMPATIBLE<std::byte, false> = true;
template <class T>
inline constexpr bool LEXICOGRAPHICAL_MEMCMP_COMPATIBLE<T, false> = false;
template <class T>
struct LexicographicalMemcmpCompatible : std::bool_constant<LEXICOGRAPHICAL_MEMCMP_COMPATIBLE<T>>
{
};
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_TYPETRAITS_HPP
#include <iterator>
#include <version>
namespace cntgs::detail
{
template <class, class = void>
inline constexpr bool HAS_OPERATOR_ARROW = false;
template <class T>
inline constexpr bool HAS_OPERATOR_ARROW<T, std::void_t<decltype(std::declval<const T&>().operator->())>> = true;
template <class T>
struct ArrowProxy
{
T t_;
constexpr const T* operator->() const noexcept { return &t_; }
};
template <class I>
constexpr auto operator_arrow_produces_pointer_to_iterator_reference_type() noexcept
{
if constexpr (detail::HAS_OPERATOR_ARROW<I>)
{
return std::is_same_v<decltype(std::declval<const I&>().operator->()),
std::add_pointer_t<typename std::iterator_traits<I>::reference>>;
}
else
{
return false;
}
}
template <class I>
inline constexpr bool CONTIGUOUS_ITERATOR_V =
detail::IS_DERIVED_FROM<typename std::iterator_traits<I>::iterator_category, std::random_access_iterator_tag> &&
std::is_lvalue_reference_v<typename std::iterator_traits<I>::reference> &&
std::is_same_v<typename std::iterator_traits<I>::value_type,
detail::RemoveCvrefT<typename std::iterator_traits<I>::reference>> &&
detail::operator_arrow_produces_pointer_to_iterator_reference_type<I>();
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_ITERATOR_HPP
// #include "cntgs/detail/range.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_RANGE_HPP
#define CNTGS_DETAIL_RANGE_HPP
#include <iterator>
#include <type_traits>
#include <utility>
namespace cntgs::detail
{
template <class, class = void>
inline constexpr bool HAS_DATA_AND_SIZE = false;
template <class T>
inline constexpr bool HAS_DATA_AND_SIZE<
T, std::void_t<decltype(std::data(std::declval<T&>())), decltype(std::size(std::declval<T&>()))>> = true;
template <class, class = void>
inline constexpr bool IS_RANGE = false;
template <class T>
inline constexpr bool
IS_RANGE<T, std::void_t<decltype(std::begin(std::declval<T&>())), decltype(std::end(std::declval<T&>()))>> = true;
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_RANGE_HPP
// #include "cntgs/detail/typeTraits.hpp"
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <iterator>
#include <limits>
#include <memory>
namespace cntgs::detail
{
using Byte = std::underlying_type_t<std::byte>;
template <std::size_t N>
struct Aligned
{
alignas(N) std::byte v[N];
};
template <class T>
auto memcpy(const T* CNTGS_RESTRICT source, std::byte* CNTGS_RESTRICT target, std::size_t size) noexcept
{
std::memcpy(target, source, size * sizeof(T));
return target + size * sizeof(T);
}
template <class Range, class TargetIterator>
constexpr auto uninitialized_move(Range&& source, TargetIterator&& target)
{
#ifdef __cpp_lib_ranges
return std::ranges::uninitialized_move(
std::forward<Range>(source),
std::ranges::subrange{std::forward<TargetIterator>(target), std::unreachable_sentinel})
.out;
#else
return std::uninitialized_move(std::begin(source), std::end(source), std::forward<TargetIterator>(target));
#endif
}
template <class Range, class TargetIterator>
constexpr auto uninitialized_copy(Range&& source, TargetIterator&& target)
{
#ifdef __cpp_lib_ranges
return std::ranges::uninitialized_copy(
std::forward<Range>(source),
std::ranges::subrange{std::forward<TargetIterator>(target), std::unreachable_sentinel})
.out;
#else
return std::uninitialized_copy(std::begin(source), std::end(source), std::forward<TargetIterator>(target));
#endif
}
template <class SourceIterator, class DifferenceType, class TargetIterator>
constexpr auto uninitialized_copy_n(SourceIterator&& source, DifferenceType count, TargetIterator&& target)
{
#ifdef __cpp_lib_ranges
return std::ranges::uninitialized_copy_n(std::forward<SourceIterator>(source),
static_cast<std::iter_difference_t<SourceIterator>>(count),
std::forward<TargetIterator>(target), std::unreachable_sentinel)
.out;
#else
return std::uninitialized_copy_n(std::forward<SourceIterator>(source), count, std::forward<TargetIterator>(target));
#endif
}
template <bool IgnoreAliasing, class TargetType, class Range>
auto uninitialized_range_construct(Range&& range, TargetType* address)
{
using RangeValueType = typename std::iterator_traits<decltype(std::begin(range))>::value_type;
if constexpr (IgnoreAliasing && detail::HAS_DATA_AND_SIZE<std::decay_t<Range>> &&
detail::MEMCPY_COMPATIBLE<TargetType, RangeValueType>)
{
return detail::memcpy(std::data(range), reinterpret_cast<std::byte*>(address), std::size(range));
}
else
{
if constexpr (!std::is_lvalue_reference_v<Range>)
{
return reinterpret_cast<std::byte*>(detail::uninitialized_move(std::forward<Range>(range), address));
}
else
{
return reinterpret_cast<std::byte*>(detail::uninitialized_copy(std::forward<Range>(range), address));
}
}
}
template <bool IgnoreAliasing, class TargetType, class Range>
auto uninitialized_construct(Range&& range, TargetType* address,
std::size_t) -> std::enable_if_t<detail::IS_RANGE<Range>, std::byte*>
{
return detail::uninitialized_range_construct<IgnoreAliasing>(std::forward<Range>(range), address);
}
template <bool IgnoreAliasing, class TargetType, class Iterator>
auto uninitialized_construct(const Iterator& iterator, TargetType* address,
std::size_t size) -> std::enable_if_t<!detail::IS_RANGE<Iterator>, std::byte*>
{
using IteratorValueType = typename std::iterator_traits<Iterator>::value_type;
if constexpr (IgnoreAliasing && std::is_pointer_v<Iterator> &&
detail::MEMCPY_COMPATIBLE<TargetType, IteratorValueType>)
{
return detail::memcpy(iterator, reinterpret_cast<std::byte*>(address), size);
}
else if constexpr (IgnoreAliasing && detail::CONTIGUOUS_ITERATOR_V<Iterator> &&
detail::MEMCPY_COMPATIBLE<TargetType, IteratorValueType>)
{
return detail::memcpy(iterator.operator->(), reinterpret_cast<std::byte*>(address), size);
}
else
{
return reinterpret_cast<std::byte*>(detail::uninitialized_copy_n(iterator, size, address));
}
}
#ifdef __cpp_lib_ranges
using std::construct_at;
#else
template <class T, class... Args>
constexpr T* construct_at(T* ptr, Args&&... args)
{
return ::new (const_cast<void*>(static_cast<const volatile void*>(ptr))) T(std::forward<Args>(args)...);
}
#endif
#ifdef __cpp_lib_assume_aligned
using std::assume_aligned;
#else
template <std::size_t Alignment, class T>
[[nodiscard]] constexpr T* assume_aligned(T* const ptr) noexcept
{
return static_cast<T*>(::__builtin_assume_aligned(ptr, Alignment));
}
#endif
[[nodiscard]] inline bool is_aligned(void* ptr, size_t alignment) noexcept
{
void* void_ptr = ptr;
auto size = std::numeric_limits<size_t>::max();
std::align(alignment, 0, void_ptr, size);
return void_ptr == ptr;
}
[[nodiscard]] constexpr auto align(std::uintptr_t position, std::size_t alignment) noexcept
{
return (position - 1u + alignment) & (alignment * std::numeric_limits<std::size_t>::max());
}
template <std::size_t Alignment>
[[nodiscard]] constexpr auto align(std::uintptr_t position) noexcept
{
if constexpr (Alignment > 1)
{
return detail::align(position, Alignment);
}
else
{
return position;
}
}
template <std::size_t Alignment, class T>
[[nodiscard]] constexpr T* align(T* ptr) noexcept
{
if constexpr (Alignment > 1)
{
const auto uintptr = reinterpret_cast<std::uintptr_t>(ptr);
const auto aligned = detail::align<Alignment>(uintptr);
return detail::assume_aligned<Alignment>(reinterpret_cast<T*>(aligned));
}
else
{
return ptr;
}
}
template <bool NeedsAlignment, std::size_t Alignment, class T>
[[nodiscard]] constexpr auto align_if(T* ptr) noexcept
{
if constexpr (NeedsAlignment && Alignment > 1)
{
ptr = detail::align<Alignment>(ptr);
}
return detail::assume_aligned<Alignment>(ptr);
}
template <bool NeedsAlignment, std::size_t Alignment>
[[nodiscard]] constexpr auto align_if(std::uintptr_t position) noexcept
{
if constexpr (NeedsAlignment && Alignment > 1)
{
position = detail::align<Alignment>(position);
}
return position;
}
template <class T>
[[nodiscard]] constexpr T extract_lowest_set_bit(T value) noexcept
{
return value & (~value + T{1});
}
[[nodiscard]] constexpr std::size_t trailing_alignment(std::size_t byte_size, std::size_t alignment) noexcept
{
return (std::min)(detail::extract_lowest_set_bit(byte_size), alignment);
}
inline constexpr auto SIZE_T_TRAILING_ALIGNMENT = detail::trailing_alignment(sizeof(std::size_t), alignof(std::size_t));
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_MEMORY_HPP
// #include "cntgs/detail/utility.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_UTILITY_HPP
#define CNTGS_DETAIL_UTILITY_HPP
// #include "cntgs/span.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_CNTGS_SPAN_HPP
#define CNTGS_CNTGS_SPAN_HPP
#include <cstddef>
#include <iterator>
#include <version>
#ifdef __cpp_lib_span
#include <span>
#endif
namespace cntgs
{
template <class T>
struct Span
{
using element_type = T;
using value_type = std::remove_cv_t<T>;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = T*;
using reverse_iterator = std::reverse_iterator<iterator>;
iterator first_;
iterator last_;
Span() = default;
template <class U>
constexpr explicit Span(const Span<U>& other) noexcept : first_(other.first_), last_(other.last_)
{
}
Span(const Span& other) = default;
Span(Span&& other) = default;
Span& operator=(const Span& other) = default;
Span& operator=(Span&& other) = default;
constexpr Span(iterator first, iterator last) noexcept : first_(first), last_(last) {}
constexpr Span(iterator first, size_type size) noexcept(noexcept(first + size)) : first_(first), last_(first + size)
{
}
[[nodiscard]] constexpr iterator begin() const noexcept { return first_; }
[[nodiscard]] constexpr iterator end() const noexcept { return last_; }
[[nodiscard]] constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
[[nodiscard]] constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
[[nodiscard]] constexpr bool empty() const noexcept { return first_ == last_; }
[[nodiscard]] constexpr size_type size() const noexcept { return last_ - first_; }
[[nodiscard]] constexpr pointer data() const noexcept { return first_; }
[[nodiscard]] constexpr reference operator[](size_type i) const noexcept { return first_[i]; }
[[nodiscard]] constexpr reference front() const noexcept { return first_[0]; }
[[nodiscard]] constexpr reference back() const noexcept { return *(last_ - 1); }
#ifdef __cpp_lib_span
constexpr operator std::span<T>() const noexcept { return std::span<T>{first_, last_}; }
#endif
};
} // namespace cntgs
#endif // CNTGS_CNTGS_SPAN_HPP
#include <utility>
namespace cntgs::detail
{
template <class T>
struct MoveDefaultingValue
{
T value_{};
MoveDefaultingValue() = default;
constexpr explicit MoveDefaultingValue(T value) noexcept : value_(value) {}
~MoveDefaultingValue() = default;
MoveDefaultingValue(const MoveDefaultingValue&) = default;
constexpr MoveDefaultingValue(MoveDefaultingValue&& other) noexcept : value_(other.value_) { other.value_ = T{}; }
MoveDefaultingValue& operator=(const MoveDefaultingValue& other) = default;
constexpr MoveDefaultingValue& operator=(MoveDefaultingValue&& other) noexcept
{
value_ = other.value_;
other.value_ = T{};
return *this;
}
};
template <class T, bool = (std::is_empty_v<T> && !std::is_final_v<T>)>
class EmptyBaseOptimization
{
private:
T value_;
public:
EmptyBaseOptimization() = default;
constexpr explicit EmptyBaseOptimization(const T& value) noexcept(std::is_nothrow_copy_constructible_v<T>)
: value_{value}
{
}
constexpr explicit EmptyBaseOptimization(T&& value) noexcept(std::is_nothrow_move_constructible_v<T>)
: value_{std::move(value)}
{
}
constexpr auto& get() noexcept { return value_; }
constexpr const auto& get() const noexcept { return value_; }
};
template <class T>
class EmptyBaseOptimization<T, true> : private T
{
public:
EmptyBaseOptimization() = default;
constexpr explicit EmptyBaseOptimization(const T& value) noexcept(std::is_nothrow_copy_constructible_v<T>)
: T{value}
{
}
constexpr explicit EmptyBaseOptimization(T&& value) noexcept(std::is_nothrow_move_constructible_v<T>)
: T{std::move(value)}
{
}
constexpr T& get() noexcept { return *this; }
constexpr const T& get() const noexcept { return *this; }
};
template <class T>
constexpr auto as_const(cntgs::Span<T> value) noexcept
{
return cntgs::Span<std::add_const_t<T>>{value};
}
template <class T>
constexpr decltype(auto) as_const(T& value) noexcept
{
return std::as_const(value);
}
template <class T>
constexpr decltype(auto) as_ref(T* value) noexcept
{
return (*value);
}
template <class T>
constexpr decltype(auto) as_ref(T& value) noexcept
{
return (value);
}
template <class T>
constexpr decltype(auto) as_const_ref(T&& value) noexcept
{
return detail::as_const(detail::as_ref(std::forward<T>(value)));
}
template <bool UseMove, class T>
constexpr decltype(auto) move_if(T& value)
{
if constexpr (UseMove)
{
return std::move(value);
}
else
{
return (value);
}
}
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_UTILITY_HPP
#include <cstddef>
#include <memory>
#include <type_traits>
namespace cntgs::detail
{
template <class Allocator>
class AllocatorAwarePointer
{
private:
using AllocatorTraits = std::allocator_traits<Allocator>;
using Pointer = typename AllocatorTraits::pointer;
public:
using allocator_type = Allocator;
using pointer = Pointer;
using value_type = typename AllocatorTraits::value_type;
private:
struct Impl : detail::EmptyBaseOptimization<Allocator>
{
using Base = detail::EmptyBaseOptimization<Allocator>;
Pointer ptr_{};
std::size_t size_{};
Impl() = default;
constexpr Impl(Pointer ptr, std::size_t size, const Allocator& allocator) noexcept
: Base{allocator}, ptr_(ptr), size_(size)
{
}
};
Impl impl_;
constexpr auto allocate() { return AllocatorTraits::allocate(get_allocator(), size()); }
constexpr void deallocate() noexcept
{
if (get())
{
AllocatorTraits::deallocate(get_allocator(), get(), size());
}
}
static constexpr auto allocate_if_not_zero(std::size_t size, Allocator allocator)
{
#ifdef __cpp_lib_is_constant_evaluated
if (std::is_constant_evaluated() && size == 0)
{
return Pointer{};
}
#endif
return AllocatorTraits::allocate(allocator, size);
}
public:
AllocatorAwarePointer() = default;
constexpr AllocatorAwarePointer(std::size_t size, const Allocator& allocator)
: impl_(AllocatorAwarePointer::allocate_if_not_zero(size, allocator), size, allocator)
{
}
constexpr AllocatorAwarePointer(pointer ptr, std::size_t size, const Allocator& allocator) noexcept
: impl_(ptr, size, allocator)
{
}
constexpr AllocatorAwarePointer(const AllocatorAwarePointer& other)
: AllocatorAwarePointer(other.size(),
AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
{
}
constexpr AllocatorAwarePointer(AllocatorAwarePointer&& other) noexcept
: impl_(other.release(), other.size(), other.get_allocator())
{
}
#if __cpp_constexpr_dynamic_alloc
constexpr
#endif
~AllocatorAwarePointer() noexcept
{
deallocate();
}
constexpr AllocatorAwarePointer& operator=(const AllocatorAwarePointer& other)
{
if (this != std::addressof(other))
{
if constexpr (AllocatorTraits::propagate_on_container_copy_assignment::value &&
!AllocatorTraits::is_always_equal::value)
{
if (get_allocator() != other.get_allocator())
{
deallocate();
propagate_on_container_copy_assignment(other);
size() = other.size();
get() = allocate();
return *this;
}
}
propagate_on_container_copy_assignment(other);
if (size() < other.size() || !get())
{
deallocate();
size() = other.size();
get() = allocate();
}
}
return *this;
}
constexpr AllocatorAwarePointer& operator=(AllocatorAwarePointer&& other) noexcept
{
if (this != std::addressof(other))
{
propagate_on_container_move_assignment(other);
deallocate();
get() = other.release();
size() = other.size();
}
return *this;
}
constexpr decltype(auto) get_allocator() noexcept { return impl_.get(); }
constexpr auto get_allocator() const noexcept { return impl_.get(); }
constexpr auto& get() noexcept { return impl_.ptr_; }
constexpr auto get() const noexcept { return impl_.ptr_; }
constexpr auto& size() noexcept { return impl_.size_; }
constexpr auto size() const noexcept { return impl_.size_; }
constexpr explicit operator bool() const noexcept { return get() != nullptr; }
constexpr auto release() noexcept { return std::exchange(impl_.ptr_, nullptr); }
constexpr void reset(AllocatorAwarePointer&& other) noexcept
{
deallocate();
get() = other.release();
size() = other.size();
}
constexpr void propagate_on_container_copy_assignment(const AllocatorAwarePointer& other) noexcept
{
if constexpr (AllocatorTraits::propagate_on_container_copy_assignment::value)
{
get_allocator() = other.get_allocator();
}
}
constexpr void propagate_on_container_move_assignment(AllocatorAwarePointer& other) noexcept
{
if constexpr (AllocatorTraits::propagate_on_container_move_assignment::value)
{
get_allocator() = std::move(other.get_allocator());
}
}
};
template <class Allocator>
constexpr void swap(detail::AllocatorAwarePointer<Allocator>& lhs,
detail::AllocatorAwarePointer<Allocator>& rhs) noexcept
{
using std::swap;
if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value)
{
swap(lhs.get_allocator(), rhs.get_allocator());
}
swap(lhs.get(), rhs.get());
swap(lhs.size(), rhs.size());
}
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_ALLOCATOR_HPP
// #include "cntgs/detail/elementTraits.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_ELEMENTTRAITS_HPP
#define CNTGS_DETAIL_ELEMENTTRAITS_HPP
// #include "cntgs/detail/algorithm.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_ALGORITHM_HPP
#define CNTGS_DETAIL_ALGORITHM_HPP
// #include "cntgs/detail/memory.hpp"
#include <algorithm>
#include <array>
#include <cstddef>
#include <utility>
namespace cntgs::detail
{
// Some compilers (e.g. MSVC) perform hand rolled optimizations or call C functions if the argument type
// fulfills certain criteria. These checks are not always performed correctly for std::byte, therefore
// cast it to a more reliable type.
template <class T>
constexpr auto trivial_swap_ranges(T* begin, T* end, T* begin2) noexcept
{
return std::swap_ranges(reinterpret_cast<detail::Byte*>(begin), reinterpret_cast<detail::Byte*>(end),
reinterpret_cast<detail::Byte*>(begin2));
}
template <class T>
constexpr auto trivial_equal(const T* begin, const T* end, const T* begin2, const T* end2) noexcept
{
return std::equal(reinterpret_cast<const detail::Byte*>(begin), reinterpret_cast<const detail::Byte*>(end),
reinterpret_cast<const detail::Byte*>(begin2), reinterpret_cast<const detail::Byte*>(end2));
}
template <class T>
constexpr auto trivial_lexicographical_compare(const T* begin, const T* end, const T* begin2, const T* end2) noexcept
{
return std::lexicographical_compare(
reinterpret_cast<const detail::Byte*>(begin), reinterpret_cast<const detail::Byte*>(end),
reinterpret_cast<const detail::Byte*>(begin2), reinterpret_cast<const detail::Byte*>(end2));
}
} // namespace cntgs::detail
#endif // CNTGS_DETAIL_ALGORITHM_HPP
// #include "cntgs/detail/attributes.hpp"
// #include "cntgs/detail/forward.hpp"
// Copyright (c) 2021 Dennis Hezel
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#ifndef CNTGS_DETAIL_FORWARD_HPP
#define CNTGS_DETAIL_FORWARD_HPP
#include <cstddef>
namespace cntgs
{
template <class T>
struct VaryingSize;
template <class T>
struct FixedSize;
template <class T, std::size_t Alignment = 1>
struct AlignAs;
template <class... Option>
struct Options;