-
Notifications
You must be signed in to change notification settings - Fork 31
/
uintwide_t.h
7968 lines (6486 loc) · 391 KB
/
uintwide_t.h
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 Christopher Kormanyos 1999 - 2024. //
// Distributed under the Boost Software License, //
// Version 1.0. (See accompanying file LICENSE_1_0.txt //
// or copy at http://www.boost.org/LICENSE_1_0.txt) //
///////////////////////////////////////////////////////////////////
#ifndef UINTWIDE_T_2018_10_02_H // NOLINT(llvm-header-guard)
#define UINTWIDE_T_2018_10_02_H
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#endif
#if ((__cplusplus < 202100L) || (defined(__GNUC__) && defined(__AVR__)))
#include <ciso646>
#else
#include <version>
#endif
#if (defined(__cpp_lib_to_chars) && (__cpp_lib_to_chars >= 201611L))
#include <charconv>
#endif
#include <cinttypes>
#if !defined(WIDE_INTEGER_DISABLE_FLOAT_INTEROP)
#include <cmath>
#endif
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#if !defined(WIDE_INTEGER_DISABLE_IOSTREAM)
#include <iomanip>
#include <istream>
#endif
#include <limits>
#if !defined(WIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY)
#include <memory>
#endif
#if (defined(__cpp_lib_gcd_lcm) && (__cpp_lib_gcd_lcm >= 201606L))
#include <numeric>
#endif
#if !defined(WIDE_INTEGER_DISABLE_IOSTREAM)
#include <ostream>
#include <sstream>
#endif
#if !defined(WIDE_INTEGER_DISABLE_TO_STRING)
#include <string>
#endif
#include <type_traits>
#include <utility>
#if (defined(__clang__) && (__clang_major__ <= 9))
#define WIDE_INTEGER_NUM_LIMITS_CLASS_TYPE struct // NOLINT(cppcoreguidelines-macro-usage)
#else
#define WIDE_INTEGER_NUM_LIMITS_CLASS_TYPE class // NOLINT(cppcoreguidelines-macro-usage)
#endif
#if (defined(_MSC_VER) && (!defined(__GNUC__) && !defined(__clang__)))
#if (_MSC_VER >= 1900) && defined(_HAS_CXX20) && (_HAS_CXX20 != 0)
#define WIDE_INTEGER_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage)
#else
#define WIDE_INTEGER_NODISCARD
#endif
#else
#if ((defined(__cplusplus) && (__cplusplus >= 201703L)) && (defined(__has_cpp_attribute) && (__has_cpp_attribute(nodiscard) >= 201603L)))
#define WIDE_INTEGER_NODISCARD [[nodiscard]] // NOLINT(cppcoreguidelines-macro-usage)
#else
#define WIDE_INTEGER_NODISCARD
#endif
#endif
#if defined(WIDE_INTEGER_NAMESPACE_BEGIN) || defined(WIDE_INTEGER_NAMESPACE_END)
#error internal pre-processor macro already defined
#endif
#if defined(WIDE_INTEGER_NAMESPACE)
#define WIDE_INTEGER_NAMESPACE_BEGIN namespace WIDE_INTEGER_NAMESPACE { // NOLINT(cppcoreguidelines-macro-usage)
#define WIDE_INTEGER_NAMESPACE_END } // namespace WIDE_INTEGER_NAMESPACE // NOLINT(cppcoreguidelines-macro-usage)
#else
#define WIDE_INTEGER_NAMESPACE_BEGIN
#define WIDE_INTEGER_NAMESPACE_END
#endif
// Forward declaration needed for class-friendship with the uintwide_t template class.
namespace test_uintwide_t_edge { auto test_various_isolated_edge_cases() -> bool; } // namespace test_uintwide_t_edge
WIDE_INTEGER_NAMESPACE_BEGIN
#if(__cplusplus >= 201703L)
namespace math::wide_integer::detail {
#else
namespace math { namespace wide_integer { namespace detail { // NOLINT(modernize-concat-nested-namespaces)
#endif
namespace iterator_detail {
class input_iterator_tag {};
class output_iterator_tag {};
class forward_iterator_tag : public input_iterator_tag {};
class bidirectional_iterator_tag : public forward_iterator_tag {};
class random_access_iterator_tag : public bidirectional_iterator_tag {};
template<typename iterator_type>
class iterator_traits
{
public:
using difference_type = typename iterator_type::difference_type;
using value_type = typename iterator_type::value_type;
using pointer = typename iterator_type::pointer;
using reference = typename iterator_type::reference;
using iterator_category = typename iterator_type::iterator_category;
};
template<typename T>
class iterator_traits<T*>
{
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = random_access_iterator_tag;
};
template<typename T>
class iterator_traits<const T*>
{
public:
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = const value_type*;
using reference = const value_type&;
using iterator_category = random_access_iterator_tag;
};
template<typename my_category,
typename my_value_type,
typename my_difference_type = std::ptrdiff_t,
typename my_pointer_type = my_value_type*,
typename my_reference_type = my_value_type&>
struct my_iterator
{
using iterator_category = my_category;
using value_type = my_value_type;
using difference_type = my_difference_type;
using pointer = my_pointer_type;
using reference = my_reference_type;
constexpr my_iterator() = default;
};
template <typename iterator_type>
class reverse_iterator : public my_iterator<typename iterator_traits<iterator_type>::iterator_category,
typename iterator_traits<iterator_type>::value_type,
typename iterator_traits<iterator_type>::difference_type,
typename iterator_traits<iterator_type>::pointer,
typename iterator_traits<iterator_type>::reference>
{
public:
using value_type = typename iterator_traits<iterator_type>::value_type;
using difference_type = typename iterator_traits<iterator_type>::difference_type;
using pointer = typename iterator_traits<iterator_type>::pointer;
using reference = typename iterator_traits<iterator_type>::reference;
using iterator_category = typename iterator_traits<iterator_type>::iterator_category;
constexpr reverse_iterator() = default;
explicit constexpr reverse_iterator(iterator_type x) : current(x) { }
template<typename other>
constexpr reverse_iterator(const reverse_iterator<other>& u) : current(u.current) { } // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
WIDE_INTEGER_NODISCARD constexpr auto base() const -> iterator_type { return current; }
constexpr auto operator* () const -> reference { iterator_type tmp = current; return *--tmp; }
constexpr auto operator->() const -> pointer { return &(operator*()); }
constexpr auto operator++() -> reverse_iterator& { --current; return *this; }
constexpr auto operator--() -> reverse_iterator& { ++current; return *this; }
constexpr auto operator++(int) -> reverse_iterator { reverse_iterator tmp = *this; --current; return tmp; }
constexpr auto operator--(int) -> reverse_iterator { reverse_iterator tmp = *this; ++current; return tmp; }
constexpr auto operator+(typename reverse_iterator<iterator_type>::difference_type n) const -> reverse_iterator { return reverse_iterator(current - n); }
constexpr auto operator-(typename reverse_iterator<iterator_type>::difference_type n) const -> reverse_iterator { return reverse_iterator(current + n); }
constexpr auto operator+=(typename reverse_iterator<iterator_type>::difference_type n) -> reverse_iterator& { current -= n; return *this; }
constexpr auto operator-=(typename reverse_iterator<iterator_type>::difference_type n) -> reverse_iterator& { current += n; return *this; }
constexpr auto operator[](typename reverse_iterator<iterator_type>::difference_type n) const -> reference { return current[-n - 1]; }
private:
iterator_type current; // NOLINT(readability-identifier-naming)
friend constexpr auto operator< (const reverse_iterator& x, const reverse_iterator& y) -> bool { return (x.current > y.current); }
friend constexpr auto operator<=(const reverse_iterator& x, const reverse_iterator& y) -> bool { return (x.current >= y.current); }
friend constexpr auto operator==(const reverse_iterator& x, const reverse_iterator& y) -> bool { return (x.current == y.current); }
friend constexpr auto operator!=(const reverse_iterator& x, const reverse_iterator& y) -> bool { return (x.current != y.current); }
friend constexpr auto operator>=(const reverse_iterator& x, const reverse_iterator& y) -> bool { return (x.current <= y.current); }
friend constexpr auto operator> (const reverse_iterator& x, const reverse_iterator& y) -> bool { return (x.current < y.current); }
friend constexpr auto operator-(const reverse_iterator& x, const reverse_iterator& y) -> typename reverse_iterator::difference_type
{
return (y.current - x.current);
}
friend constexpr auto operator+(typename reverse_iterator::difference_type n, const reverse_iterator& x) -> reverse_iterator
{
return reverse_iterator(x.current - n);
}
};
} // namespace iterator_detail
// Forward declaration of:
// Use a local, constexpr, unsafe implementation of the abs-function.
template<typename ArithmeticType>
constexpr auto abs_unsafe(ArithmeticType val) -> ArithmeticType;
// Use a local, constexpr, unsafe implementation of the fill-function.
template<typename DestinationIterator,
typename ValueType>
constexpr auto fill_unsafe(DestinationIterator first, DestinationIterator last, ValueType val) -> void
{
while(first != last)
{
using local_destination_value_type = typename iterator_detail::iterator_traits<DestinationIterator>::value_type;
*first = static_cast<local_destination_value_type>(val);
++first;
}
}
// Use a local, constexpr, unsafe implementation of the max-function.
template<typename ArithmeticType>
constexpr auto max_unsafe(const ArithmeticType& left, const ArithmeticType& right) -> ArithmeticType
{
return ((left < right) ? right : left);
}
// Use a local, constexpr, unsafe implementation of the max-function.
template<typename ArithmeticType>
constexpr auto min_unsafe(const ArithmeticType& left, const ArithmeticType& right) -> ArithmeticType
{
return ((right < left) ? right : left);
}
// Use a local, constexpr, unsafe implementation of the copy-function.
template<typename InputIterator,
typename DestinationIterator>
constexpr auto copy_unsafe(InputIterator first, InputIterator last, DestinationIterator dest) -> DestinationIterator
{
while(first != last)
{
using local_destination_value_type = typename iterator_detail::iterator_traits<DestinationIterator>::value_type;
#if (defined(__GNUC__) && (__GNUC__ > 9))
#pragma GCC diagnostic ignored "-Wstringop-overflow"
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
*dest++ = static_cast<local_destination_value_type>(*first++);
#if (defined(__GNUC__) && (__GNUC__ > 9))
#pragma GCC diagnostic pop
#pragma GCC diagnostic pop
#endif
}
return dest;
}
// Use a local, constexpr, unsafe implementation of the copy-backward-function.
template<typename InputIterator,
typename DestinationIterator>
constexpr auto copy_backward_unsafe(InputIterator first, InputIterator last, DestinationIterator dest) -> DestinationIterator
{
using local_destination_value_type = typename iterator_detail::iterator_traits<DestinationIterator>::value_type;
while(first != last)
{
*(--dest) = static_cast<local_destination_value_type>(*(--last));
}
return dest;
}
template<typename T>
constexpr auto swap_unsafe(T& left, T& right) noexcept -> void
{
auto tmp = static_cast<T&&>(left);
left = static_cast<T&&>(right);
right = static_cast<T&&>(tmp);
}
template<typename InputIt, typename UnaryPredicate>
constexpr auto find_if_unsafe(InputIt first, InputIt last, UnaryPredicate p) -> InputIt
{
while(first != last)
{
if(p(*first))
{
return first;
}
++first;
}
return last; // LCOV_EXCL_LINE
}
template<typename ForwardIt, typename T>
constexpr auto lower_bound_unsafe(ForwardIt first, ForwardIt last, const T& value) -> ForwardIt
{
using local_iterator_type = ForwardIt;
using local_difference_type = typename iterator_detail::iterator_traits<ForwardIt>::difference_type;
local_difference_type step { };
auto count = static_cast<local_difference_type>(last - first); // NOLINT(altera-id-dependent-backward-branch)
local_iterator_type itr { };
while(count > static_cast<local_difference_type>(INT8_C(0))) // NOLINT(altera-id-dependent-backward-branch)
{
itr = first;
step = static_cast<local_difference_type>(count / static_cast<local_difference_type>(INT8_C(2)));
itr += step;
if (*itr < value)
{
first = ++itr;
count -= static_cast<local_difference_type>(step + static_cast<local_difference_type>(INT8_C(1)));
}
else
{
count = step;
}
}
return first;
}
template<class ForwardIt, class T>
constexpr auto binary_search_unsafe(ForwardIt first, ForwardIt last, const T& value) -> bool
{
first = lower_bound_unsafe(first, last, value);
return ((!(first == last)) && (!(value < *first)));
}
namespace distance_detail
{
template<class It>
constexpr auto do_distance_unsafe(It first, It last, detail::iterator_detail::random_access_iterator_tag) -> typename detail::iterator_detail::iterator_traits<It>::difference_type // NOLINT(hicpp-named-parameter,readability-named-parameter)
{
using local_difference_type = typename detail::iterator_detail::iterator_traits<It>::difference_type;
return static_cast<local_difference_type>(last - first);
}
} // namespace distance_detail
template<class It>
constexpr auto distance_unsafe(It first, It last) -> typename iterator_detail::iterator_traits<It>::difference_type
{
using local_iterator_category_type = typename iterator_detail::iterator_traits<It>::iterator_category;
return
distance_detail::do_distance_unsafe
(
first,
last,
local_iterator_category_type()
);
}
template<class InputIt1, class InputIt2>
constexpr auto equal_unsafe(InputIt1 first1, InputIt1 last1, InputIt2 first2) -> bool
{
while(first1 != last1)
{
if(!(*first1 == *first2))
{
return false;
}
++first1;
++first2;
}
return true;
}
template<class InputIt1, class InputIt2>
constexpr auto lexicographical_compare_unsafe(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) -> bool
{
while((first1 != last1) && (first2 != last2))
{
if(*first1 < *first2)
{
return true;
}
if(*first2 < *first1)
{
return false;
}
++first1;
++first2;
}
return ((first1 == last1) && (first2 != last2));
}
template<typename Iterator1, typename Iterator2>
constexpr auto iter_swap_unsafe(Iterator1 a, Iterator2 b) -> void
{
using local_value_type = typename iterator_detail::iterator_traits<Iterator1>::value_type;
swap_unsafe(static_cast<local_value_type&>(*a), static_cast<local_value_type&>(*b));
}
template<class ForwardIt1, class ForwardIt2>
constexpr auto swap_ranges_unsafe(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2) -> ForwardIt2
{
while(first1 != last1)
{
iter_swap_unsafe(first1, first2);
++first1;
++first2;
}
return first2;
}
template <class UnsignedIntegralType>
constexpr auto clz_unsafe(UnsignedIntegralType v) noexcept -> std::enable_if_t<( (std::is_integral<UnsignedIntegralType>::value)
&& (std::is_unsigned<UnsignedIntegralType>::value)), unsigned>
{
using local_unsigned_integral_type = UnsignedIntegralType;
auto yy_val = static_cast<local_unsigned_integral_type>(UINT8_C(0));
auto nn_val = static_cast<unsigned>(std::numeric_limits<local_unsigned_integral_type>::digits);
auto cc_val = // NOLINT(altera-id-dependent-backward-branch)
static_cast<unsigned>
(
std::numeric_limits<local_unsigned_integral_type>::digits / static_cast<int>(INT8_C(2))
);
do
{
yy_val = static_cast<local_unsigned_integral_type>(v >> cc_val);
if(yy_val != static_cast<local_unsigned_integral_type>(UINT8_C(0)))
{
nn_val -= cc_val;
v = yy_val;
}
cc_val >>= static_cast<unsigned>(UINT8_C(1));
}
while(cc_val != static_cast<unsigned>(UINT8_C(0))); // NOLINT(altera-id-dependent-backward-branch)
return
static_cast<unsigned>
(
static_cast<unsigned>(nn_val) - static_cast<unsigned>(v)
);
}
template<typename UnsignedIntegralType>
constexpr auto ctz_unsafe(const UnsignedIntegralType v) noexcept -> std::enable_if_t<( (std::is_integral<UnsignedIntegralType>::value)
&& (std::is_unsigned<UnsignedIntegralType>::value)), unsigned>
{
using local_unsigned_integral_type = UnsignedIntegralType;
constexpr auto local_digits = static_cast<unsigned>(std::numeric_limits<local_unsigned_integral_type>::digits);
const auto clz_mask =
static_cast<local_unsigned_integral_type>
(
static_cast<local_unsigned_integral_type>(~v)
& static_cast<local_unsigned_integral_type>(v - static_cast<local_unsigned_integral_type>(UINT8_C(1)))
);
return static_cast<unsigned>(local_digits - clz_unsafe(clz_mask));
}
template<typename UnsignedIntegralType>
constexpr auto gcd_unsafe(UnsignedIntegralType u, UnsignedIntegralType v) -> std::enable_if_t<( (std::is_integral<UnsignedIntegralType>::value) // NOLINT(altera-id-dependent-backward-branch)
&& (std::is_unsigned<UnsignedIntegralType>::value)), UnsignedIntegralType>
{
using local_unsigned_integral_type = UnsignedIntegralType;
// Handle cases having (u != 0) and (v != 0).
if(u == static_cast<local_unsigned_integral_type>(UINT8_C(0))) { return v; }
if(v == static_cast<local_unsigned_integral_type>(UINT8_C(0))) { return u; }
// Shift the greatest power of 2 dividing both u and v.
const auto trz = static_cast<unsigned>(ctz_unsafe(u));
const auto shift_amount = (detail::min_unsafe)(trz, ctz_unsafe(v));
v >>= shift_amount;
u >>= trz;
do
{
// Reduce the GCD.
v >>= ctz_unsafe(v);
if(u > v)
{
swap_unsafe(u, v);
}
v -= u;
}
while(v != static_cast<local_unsigned_integral_type>(UINT8_C(0))); // NOLINT(altera-id-dependent-backward-branch)
return static_cast<local_unsigned_integral_type>(u << shift_amount);
}
namespace array_detail {
template<typename T, std::size_t N>
class array
{
public:
// Standard container-local type definitions.
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = iterator_detail::reverse_iterator<iterator>;
using const_reverse_iterator = iterator_detail::reverse_iterator<const_iterator>;
value_type elems[N] { }; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,misc-non-private-member-variables-in-classes)
static constexpr size_type static_size = N;
WIDE_INTEGER_NODISCARD constexpr auto begin() -> iterator { return elems; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto end () -> iterator { return elems + N; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto begin() const -> const_iterator { return elems; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto end () const -> const_iterator { return elems + N; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto cbegin() const -> const_iterator { return elems; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto cend () const -> const_iterator { return elems + N; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto rbegin() -> reverse_iterator { return reverse_iterator(elems + N); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto rend () -> reverse_iterator { return reverse_iterator(elems); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto rbegin() const -> const_reverse_iterator { return const_reverse_iterator(elems + N); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto rend () const -> const_reverse_iterator { return const_reverse_iterator(elems); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto crbegin() const -> const_reverse_iterator { return const_reverse_iterator(elems + N); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto crend () const -> const_reverse_iterator { return const_reverse_iterator(elems); } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
WIDE_INTEGER_NODISCARD constexpr auto operator[](const size_type i) -> reference { return elems[i]; } // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
WIDE_INTEGER_NODISCARD constexpr auto operator[](const size_type i) const -> const_reference { return elems[i]; } // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
WIDE_INTEGER_NODISCARD constexpr auto at(const size_type i) -> reference { return elems[i]; } // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
WIDE_INTEGER_NODISCARD constexpr auto at(const size_type i) const -> const_reference { return elems[i]; } // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index)
WIDE_INTEGER_NODISCARD constexpr auto front() -> reference { return elems[static_cast<size_type>(UINT8_C(0))]; }
WIDE_INTEGER_NODISCARD constexpr auto front() const -> const_reference { return elems[static_cast<size_type>(UINT8_C(0))]; }
WIDE_INTEGER_NODISCARD constexpr auto back() -> reference { return elems[static_cast<size_type>(N - static_cast<size_type>(UINT8_C(1)))]; }
WIDE_INTEGER_NODISCARD constexpr auto back() const -> const_reference { return elems[static_cast<size_type>(N - static_cast<size_type>(UINT8_C(1)))]; }
WIDE_INTEGER_NODISCARD static constexpr auto size() -> size_type { return N; }
WIDE_INTEGER_NODISCARD static constexpr auto empty() -> bool { return false; }
WIDE_INTEGER_NODISCARD static constexpr auto max_size() -> size_type { return N; }
template<typename T2>
constexpr auto swap(array<T2, N>& y) noexcept -> void
{
swap_ranges_unsafe(begin(), end(), y.begin());
}
WIDE_INTEGER_NODISCARD constexpr auto data() const -> const_pointer { return elems; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
WIDE_INTEGER_NODISCARD constexpr auto data() -> pointer { return elems; } // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
WIDE_INTEGER_NODISCARD constexpr auto c_array() -> pointer { return elems; }
template<typename T2>
constexpr auto operator=(const array<T2, N>& y) -> array&
{
copy_unsafe(y.begin(), y.end(), begin());
return *this;
}
constexpr auto assign(const value_type& value) -> void
{
fill_unsafe(elems, elems + N, value); // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay,cppcoreguidelines-pro-bounds-pointer-arithmetic)
}
constexpr auto fill(const value_type& value) -> void
{
assign(value);
}
};
template<typename T, size_t N>
constexpr auto operator==(const array<T, N>& left, const array<T, N>& right) -> bool
{
return equal_unsafe(left.begin(), left.end(), right.begin());
}
template<typename T, size_t N>
constexpr auto operator<(const array<T, N>& left, const array<T, N>& right) -> bool
{
return lexicographical_compare_unsafe(left.begin(),
left.end(),
right.begin(),
right.end());
}
template<typename T, size_t N>
constexpr auto operator!=(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (!(left == right));
}
template<typename T, size_t N>
constexpr auto operator>(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (right < left);
}
template<typename T, size_t N>
constexpr auto operator>=(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (!(left < right));
}
template<typename T, size_t N>
constexpr auto operator<=(const array<T, N>& left, const array<T, N>& right) -> bool
{
return (!(right < left));
}
template<typename T, size_t N >
constexpr auto swap(array<T, N>& x, array<T, N>& y) noexcept -> void
{
swap_ranges_unsafe(x.begin(), x.end(), y.begin());
}
template<typename T>
class tuple_size;
template<typename T, typename std::size_t N>
class tuple_size<array<T, N>> : public std::integral_constant<std::size_t, N> { };
template<const std::size_t N, typename T>
class tuple_element;
template<const std::size_t I,
typename T,
const std::size_t N>
class tuple_element<I, array<T, N> >
{
static_assert(I < N, "Sorry, tuple_element index is out of bounds.");
using type = T;
};
} // namespace array_detail
#if(__cplusplus >= 201703L)
} // namespace math::wide_integer::detail
#else
} // namespace detail
} // namespace wide_integer
} // namespace math
#endif
WIDE_INTEGER_NAMESPACE_END
#if !defined(WIDE_INTEGER_DISABLE_IMPLEMENT_UTIL_DYNAMIC_ARRAY)
WIDE_INTEGER_NAMESPACE_BEGIN
namespace util {
template<typename ValueType,
typename AllocatorType = std::allocator<ValueType>,
typename SizeType = std::size_t,
typename DiffType = std::ptrdiff_t>
class dynamic_array;
template<typename ValueType,
typename AllocatorType,
typename SizeType,
typename DiffType>
class dynamic_array
{
public:
// Type definitions.
using allocator_type = typename std::allocator_traits<AllocatorType>::template rebind_alloc<ValueType>;
using value_type = typename allocator_type::value_type;
using reference = value_type&;
using const_reference = const value_type&;
using iterator = value_type*;
using const_iterator = const value_type*;
using pointer = value_type*;
using const_pointer = const value_type*;
using size_type = SizeType;
using difference_type = DiffType;
#if defined(WIDE_INTEGER_NAMESPACE)
using reverse_iterator = WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::iterator_detail::reverse_iterator< value_type*>;
using const_reverse_iterator = WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::iterator_detail::reverse_iterator<const value_type*>;
#else
using reverse_iterator = ::math::wide_integer::detail::iterator_detail::reverse_iterator< value_type*>;
using const_reverse_iterator = ::math::wide_integer::detail::iterator_detail::reverse_iterator<const value_type*>;
#endif
// Constructors.
constexpr dynamic_array() = delete;
explicit constexpr dynamic_array( size_type count_in,
const_reference value_in = value_type(),
const allocator_type& alloc_in = allocator_type())
: elem_count(count_in)
{
if(elem_count > static_cast<size_type>(UINT8_C(0)))
{
allocator_type my_alloc(alloc_in);
elems = std::allocator_traits<allocator_type>::allocate(my_alloc, elem_count);
iterator it = begin();
while(it != end())
{
std::allocator_traits<allocator_type>::construct(my_alloc, it, value_in);
++it;
}
}
}
constexpr dynamic_array(const dynamic_array& other)
: elem_count(other.size())
{
allocator_type my_alloc;
if(elem_count > static_cast<size_type>(UINT8_C(0)))
{
elems = std::allocator_traits<allocator_type>::allocate(my_alloc, elem_count);
}
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::copy_unsafe(other.elems, other.elems + elem_count, elems);
#else
::math::wide_integer::detail::copy_unsafe(other.elems, other.elems + elem_count, elems);
#endif
}
template<typename input_iterator>
constexpr dynamic_array(input_iterator first,
input_iterator last,
const allocator_type& alloc_in = allocator_type())
: elem_count(static_cast<size_type>(last - first))
{
allocator_type my_alloc(alloc_in);
if(elem_count > static_cast<size_type>(UINT8_C(0)))
{
elems = std::allocator_traits<allocator_type>::allocate(my_alloc, elem_count);
}
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::copy_unsafe(first, last, elems);
#else
::math::wide_integer::detail::copy_unsafe(first, last, elems);
#endif
}
constexpr dynamic_array(std::initializer_list<value_type> lst,
const allocator_type& alloc_in = allocator_type())
: elem_count(lst.size())
{
allocator_type my_alloc(alloc_in);
if(elem_count > static_cast<size_type>(UINT8_C(0)))
{
elems = std::allocator_traits<allocator_type>::allocate(my_alloc, elem_count);
}
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::copy_unsafe(lst.begin(), lst.end(), elems);
#else
::math::wide_integer::detail::copy_unsafe(lst.begin(), lst.end(), elems);
#endif
}
// Move constructor.
constexpr dynamic_array(dynamic_array&& other) noexcept : elem_count(other.elem_count),
elems (other.elems)
{
other.elem_count = static_cast<size_type>(UINT8_C(0));
other.elems = nullptr;
}
// Destructor.
//constexpr
virtual ~dynamic_array()
{
if(!empty())
{
using local_allocator_traits_type = std::allocator_traits<allocator_type>;
allocator_type my_alloc;
auto p = begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto)
while(p != end())
{
local_allocator_traits_type::destroy(my_alloc, p);
++p;
}
// Destroy the elements and deallocate the range.
local_allocator_traits_type::deallocate(my_alloc, elems, elem_count);
}
}
// Assignment operator.
constexpr auto operator=(const dynamic_array& other) -> dynamic_array&
{
if(this != &other)
{
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::copy_unsafe(other.elems,
other.elems + WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::min_unsafe(elem_count, other.elem_count),
elems);
#else
::math::wide_integer::detail::copy_unsafe(other.elems,
other.elems + ::math::wide_integer::detail::min_unsafe(elem_count, other.elem_count),
elems);
#endif
}
return *this;
}
// Move assignment operator.
constexpr auto operator=(dynamic_array&& other) noexcept -> dynamic_array&
{
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::swap_unsafe(elem_count, other.elem_count);
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::swap_unsafe(elems, other.elems);
#else
::math::wide_integer::detail::swap_unsafe(elem_count, other.elem_count);
::math::wide_integer::detail::swap_unsafe(elems, other.elems);
#endif
return *this;
}
// Iterator members:
constexpr auto begin () -> iterator { return elems; }
constexpr auto end () -> iterator { return elems + elem_count; }
constexpr auto begin () const -> const_iterator { return elems; }
constexpr auto end () const -> const_iterator { return elems + elem_count; }
constexpr auto cbegin () const -> const_iterator { return elems; }
constexpr auto cend () const -> const_iterator { return elems + elem_count; }
constexpr auto rbegin () -> reverse_iterator { return reverse_iterator(elems + elem_count); }
constexpr auto rend () -> reverse_iterator { return reverse_iterator(elems); }
constexpr auto rbegin () const -> const_reverse_iterator { return const_reverse_iterator(elems + elem_count); }
constexpr auto rend () const -> const_reverse_iterator { return const_reverse_iterator(elems); }
constexpr auto crbegin() const -> const_reverse_iterator { return const_reverse_iterator(elems + elem_count); }
constexpr auto crend () const -> const_reverse_iterator { return const_reverse_iterator(elems); }
// Raw pointer access.
constexpr auto data() -> pointer { return elems; }
constexpr auto data() const -> const_pointer { return elems; }
// Size and capacity.
constexpr auto size () const -> size_type { return elem_count; }
constexpr auto max_size() const -> size_type { return elem_count; }
constexpr auto empty () const -> bool { return (elem_count == static_cast<size_type>(UINT8_C(0))); }
// Element access members.
constexpr auto operator[](const size_type i) -> reference { return elems[i]; }
constexpr auto operator[](const size_type i) const -> const_reference { return elems[i]; }
constexpr auto front() -> reference { return elems[static_cast<size_type>(UINT8_C(0))]; }
constexpr auto front() const -> const_reference { return elems[static_cast<size_type>(UINT8_C(0))]; }
constexpr auto back() -> reference { return ((elem_count > static_cast<size_type>(UINT8_C(0))) ? elems[static_cast<size_type>(elem_count - static_cast<size_type>(UINT8_C(1)))] : elems[static_cast<size_type>(UINT8_C(0))]); }
constexpr auto back() const -> const_reference { return ((elem_count > static_cast<size_type>(UINT8_C(0))) ? elems[static_cast<size_type>(elem_count - static_cast<size_type>(UINT8_C(1)))] : elems[static_cast<size_type>(UINT8_C(0))]); }
constexpr auto at(const size_type i) -> reference { return ((i < elem_count) ? elems[i] : elems[static_cast<size_type>(UINT8_C(0))]); }
constexpr auto at(const size_type i) const -> const_reference { return ((i < elem_count) ? elems[i] : elems[static_cast<size_type>(UINT8_C(0))]); }
// Element manipulation members.
constexpr auto fill(const value_type& value_in) -> void
{
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::fill_unsafe(begin(), begin() + elem_count, value_in);
#else
::math::wide_integer::detail::fill_unsafe(begin(), begin() + elem_count, value_in);
#endif
}
constexpr auto swap(dynamic_array& other) noexcept -> void
{
if(this != &other)
{
#if defined(WIDE_INTEGER_NAMESPACE)
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::swap_unsafe(elems, other.elems);
WIDE_INTEGER_NAMESPACE::math::wide_integer::detail::swap_unsafe(elem_count, other.elem_count);
#else
::math::wide_integer::detail::swap_unsafe(elems, other.elems);
::math::wide_integer::detail::swap_unsafe(elem_count, other.elem_count);
#endif
}
}
constexpr auto swap(dynamic_array&& other) noexcept -> void
{
const auto tmp = std::move(*this);
*this = std::move(other);
other = std::move(tmp);
}
private:
mutable size_type elem_count; // NOLINT(readability-identifier-naming)
pointer elems { nullptr }; // NOLINT(readability-identifier-naming,altera-id-dependent-backward-branch)
};
template<typename ValueType, typename AllocatorType>
constexpr auto operator==(const dynamic_array<ValueType, AllocatorType>& lhs,
const dynamic_array<ValueType, AllocatorType>& rhs) -> bool
{
using local_size_type = typename dynamic_array<ValueType, AllocatorType>::size_type;
return
(
(lhs.size() == rhs.size())
&& (
(lhs.size() == static_cast<local_size_type>(UINT8_C(0)))
|| std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin())
)
);
}
template<typename ValueType, typename AllocatorType>
constexpr auto operator<(const dynamic_array<ValueType, AllocatorType>& lhs,
const dynamic_array<ValueType, AllocatorType>& rhs) -> bool
{
using size_type = typename dynamic_array<ValueType, AllocatorType>::size_type;
const auto size_of_left_is_zero = (lhs.size() == static_cast<size_type>(UINT8_C(0)));
bool b_result { };
if(size_of_left_is_zero)
{
const auto size_of_right_is_zero = (rhs.size() == static_cast<size_type>(UINT8_C(0)));
b_result = (!size_of_right_is_zero);
}
else
{
if(size_of_left_is_zero)
{
const auto size_of_right_is_zero = (rhs.size() == static_cast<size_type>(UINT8_C(0)));
b_result = (!size_of_right_is_zero);
}
else
{
const size_type my_count = min_unsafe(lhs.size(), rhs.size());