forked from microsoft/STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom
5383 lines (4310 loc) · 183 KB
/
random
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
// random standard header
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef _RANDOM_
#define _RANDOM_
#include <yvals_core.h>
#if _STL_COMPILER_PREPROCESSOR
#include <__msvc_int128.hpp>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <ios>
#include <vector>
#include <xbit_ops.h>
#include <xstring>
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
_STL_DISABLE_CLANG_WARNINGS
#pragma push_macro("new")
#undef new
#ifdef _ALLOW_RANDOM_DISTRIBUTION_CONST_OPERATOR
#define _DISTRIBUTION_CONST const
#else
#define _DISTRIBUTION_CONST
#endif
_STD_BEGIN
#define _RNG_PROHIBIT_CHAR(_CheckedType) \
static_assert(!_Is_character<_CheckedType>::value, \
"note: char, signed char, unsigned char, char8_t, int8_t, and uint8_t are not allowed")
#define _RNG_REQUIRE_REALTYPE(_RandType, _CheckedType) \
static_assert(_Is_any_of_v<_CheckedType, float, double, long double>, \
"invalid template argument for " #_RandType \
": N4950 [rand.req.genl]/1.4 requires one of float, double, or long double")
#define _RNG_REQUIRE_INTTYPE(_RandType, _CheckedType) \
static_assert(_Is_any_of_v<_CheckedType, short, int, long, long long, unsigned short, unsigned int, unsigned long, \
unsigned long long>, \
"invalid template argument for " #_RandType \
": N4950 [rand.req.genl]/1.5 requires one of short, int, long, long long, unsigned short, unsigned int, " \
"unsigned long, or unsigned long long"); \
_RNG_PROHIBIT_CHAR(_CheckedType)
#define _RNG_REQUIRE_UINTTYPE(_RandType, _CheckedType) \
static_assert(_Is_any_of_v<_CheckedType, unsigned short, unsigned int, unsigned long, unsigned long long>, \
"invalid template argument for " #_RandType ": N4950 [rand.req.genl]/1.6 requires one of unsigned short, " \
"unsigned int, unsigned long, or unsigned long long"); \
_RNG_PROHIBIT_CHAR(_CheckedType)
template <class _Seed_seq, class _Self, class _Engine = _Self>
using _Enable_if_seed_seq_t =
enable_if_t<!is_convertible_v<remove_cv_t<_Seed_seq>, typename _Self::result_type>
&& !is_same_v<remove_cv_t<_Seed_seq>, _Self> && !is_same_v<remove_cv_t<_Seed_seq>, _Engine>,
int>;
_INLINE_VAR constexpr long double _Pi_val = 3.14159265358979323846264338327950288L;
_INLINE_VAR constexpr long double _Exp1 = 2.71828182845904523536028747135266250L;
_INLINE_VAR constexpr long double _Two32 = 4294967296.0L;
_INLINE_VAR constexpr long double _Two31 = 2147483648.0L;
extern "C++" _CRTIMP2_PURE float __CLRCALL_PURE_OR_CDECL _XLgamma(float) noexcept;
extern "C++" _CRTIMP2_PURE double __CLRCALL_PURE_OR_CDECL _XLgamma(double) noexcept;
extern "C++" _CRTIMP2_PURE long double __CLRCALL_PURE_OR_CDECL _XLgamma(long double) noexcept;
_INLINE_VAR constexpr int _Nwords = 4;
template <class _Elem, class _Traits>
basic_ostream<_Elem, _Traits>& _Write(
basic_ostream<_Elem, _Traits>& _Os, long double _Dx) { // write long double to stream
int _Ex;
long double _Frac = _CSTD frexpl(_Dx, &_Ex);
for (int _Nw = 0; _Nw < _Nwords; ++_Nw) { // break into 31-bit words
_Frac *= _Two31;
long _Digits = static_cast<long>(_Frac);
_Frac -= _Digits;
_Os << ' ' << _Digits;
}
_Os << ' ' << _Ex;
return _Os;
}
template <class _Elem, class _Traits>
basic_istream<_Elem, _Traits>& _Read(
basic_istream<_Elem, _Traits>& _Is, long double& _Dx) { // read long double from stream
long double _Frac = 0.0;
long _Digits;
for (int _Nw = 0; _Nw < _Nwords; ++_Nw) { // accumulate 31-bit words
_Is >> _Digits;
long double _Temp = _Digits / _Two31;
for (int _Idx = 0; _Idx < _Nw; ++_Idx) {
_Temp /= _Two31;
}
_Frac += _Temp;
}
_Is >> _Digits;
_Dx = _CSTD ldexpl(_Frac, _Digits);
return _Is;
}
template <class _Elem, class _Traits, class _Ty>
basic_istream<_Elem, _Traits>& _In(basic_istream<_Elem, _Traits>& _Is, _Ty& _Dx) { // read from stream
long double _Vx;
_Ty _Max = (numeric_limits<_Ty>::max)();
_Read(_Is, _Vx);
if (_CSTD fabsl(_Vx) <= _Max) {
_Dx = static_cast<_Ty>(_Vx);
} else if (_Vx < 0) {
_Dx = -_Max;
} else {
_Dx = _Max;
}
return _Is;
}
template <class _Elem, class _Traits, class _Ty>
basic_ostream<_Elem, _Traits>& _Out(basic_ostream<_Elem, _Traits>& _Os, _Ty _Dx) { // write to stream
return _Write(_Os, _Dx);
}
template <class _Elem, class _Traits, class _Ty>
class _Wrap_istream { // wrap input stream as function object
public:
_Wrap_istream(basic_istream<_Elem, _Traits>& _Is) : _Str(_Is) {}
_Ty operator()() { // read next value
_Ty _Data;
_Str >> _Data;
if (!_Str) {
_Xinvalid_argument("input stream corrupted");
}
return _Data;
}
_Wrap_istream& operator=(const _Wrap_istream&) = delete;
private:
basic_istream<_Elem, _Traits>& _Str;
};
_EXPORT_STD class seed_seq { // standard sequence of seed values
public:
using result_type = unsigned int;
seed_seq() noexcept {}
template <class _Ty, enable_if_t<is_integral_v<_Ty>, int> = 0>
seed_seq(initializer_list<_Ty> _Ilist) {
_Construct(_Ilist.begin(), _Ilist.end());
}
template <class _InIt>
seed_seq(_InIt _First, _InIt _Last) {
_Construct(_First, _Last);
}
template <class _RanIt>
void generate(_RanIt _First, _RanIt _Last) const { // generate randomized interval from seeds
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_First);
const auto _Nx = static_cast<size_t>(_Get_unwrapped(_Last) - _UFirst);
if (0 < _Nx) { // finite interval, fill it
const size_t _Sx = _Myvec.size();
const size_t _Tx = 623 <= _Nx ? 11 : 68 <= _Nx ? 7 : 39 <= _Nx ? 5 : 7 <= _Nx ? 3 : (_Nx - 1) / 2;
const size_t _Px = (_Nx - _Tx) / 2;
const size_t _Qx = _Px + _Tx;
const size_t _Mx = _Nx <= _Sx ? _Sx + 1 : _Nx;
size_t _Kx;
_Iter_value_t<_RanIt> _Mask(_Iter_value_t<_RanIt>(1) << 31);
_Mask <<= 1; // build 32-bit mask safely
_Mask -= 1;
for (_Kx = 0; _Kx < _Nx; ++_Kx) {
_UFirst[_Kx] = 0x8b8b8b8b;
}
for (_Kx = 0; _Kx < _Mx; ++_Kx) { // scramble and add any vector contributions
result_type _Rx1 =
1664525 * _Xor27(_UFirst[_Kx % _Nx] ^ _UFirst[(_Kx + _Px) % _Nx] ^ _UFirst[(_Kx - 1) % _Nx]);
size_t _Off;
if (_Kx == 0) {
_Off = _Sx;
} else if (_Kx <= _Sx) {
_Off = _Kx % _Nx + _Myvec[_Kx - 1];
} else {
_Off = _Kx % _Nx;
}
result_type _Rx2 = static_cast<result_type>((_Rx1 + _Off) & _Mask);
_UFirst[(_Kx + _Px) % _Nx] = (_UFirst[(_Kx + _Px) % _Nx] + _Rx1) & _Mask;
_UFirst[(_Kx + _Qx) % _Nx] = (_UFirst[(_Kx + _Qx) % _Nx] + _Rx2) & _Mask;
_UFirst[_Kx % _Nx] = _Rx2;
}
for (; _Kx < _Mx + _Nx; ++_Kx) { // rescramble
result_type _Rx3 =
1566083941 * _Xor27(_UFirst[_Kx % _Nx] + _UFirst[(_Kx + _Px) % _Nx] + _UFirst[(_Kx - 1) % _Nx]);
result_type _Rx4 = static_cast<result_type>((_Rx3 - _Kx % _Nx) & _Mask);
_UFirst[(_Kx + _Px) % _Nx] = (_UFirst[(_Kx + _Px) % _Nx] ^ _Rx3) & _Mask;
_UFirst[(_Kx + _Qx) % _Nx] = (_UFirst[(_Kx + _Qx) % _Nx] ^ _Rx4) & _Mask;
_UFirst[_Kx % _Nx] = _Rx4;
}
}
}
template <class _OutIt>
void param(_OutIt _Dest) const { // copy seeds
_STD copy(_Myvec.begin(), _Myvec.end(), _Dest);
}
_NODISCARD size_t size() const noexcept { // get size of seed
return _Myvec.size();
}
seed_seq(const seed_seq&) = delete;
void operator=(const seed_seq&) = delete;
private:
template <class _InIt>
void _Construct(_InIt _First, _InIt _Last) {
for (; _First != _Last; ++_First) {
_Myvec.push_back(static_cast<unsigned int>(*_First));
}
}
result_type _Xor27(result_type _Val) const noexcept { // shift and merge
return _Val ^ (_Val >> 27);
}
vector<result_type> _Myvec;
};
_NODISCARD constexpr int _Generate_canonical_iterations(const int _Bits, const uint64_t _Gmin, const uint64_t _Gmax) {
// For a URBG type `G` with range == `(G::max() - G::min()) + 1`, returns the number of calls to generate at least
// _Bits bits of entropy. Specifically, max(1, ceil(_Bits / log2(range))).
_STL_INTERNAL_CHECK(0 <= _Bits && _Bits <= 64);
if (_Bits == 0 || (_Gmax == UINT64_MAX && _Gmin == 0)) {
return 1;
}
const auto _Range = (_Gmax - _Gmin) + 1;
const auto _Target = ~uint64_t{0} >> (64 - _Bits);
uint64_t _Prod = 1;
int _Ceil = 0;
while (_Prod <= _Target) {
++_Ceil;
if (_Prod > UINT64_MAX / _Range) {
break;
}
_Prod *= _Range;
}
return _Ceil;
}
_EXPORT_STD template <class _Real, size_t _Bits, class _Gen>
_NODISCARD _Real generate_canonical(_Gen& _Gx) { // build a floating-point value from random sequence
_RNG_REQUIRE_REALTYPE(generate_canonical, _Real);
constexpr auto _Digits = static_cast<size_t>(numeric_limits<_Real>::digits);
constexpr auto _Minbits = static_cast<int>(_Digits < _Bits ? _Digits : _Bits);
constexpr auto _Gxmin = static_cast<_Real>((_Gen::min)());
constexpr auto _Gxmax = static_cast<_Real>((_Gen::max)());
constexpr auto _Rx = (_Gxmax - _Gxmin) + _Real{1};
constexpr int _Kx = _Generate_canonical_iterations(_Minbits, (_Gen::min)(), (_Gen::max)());
_Real _Ans{0};
_Real _Factor{1};
for (int _Idx = 0; _Idx < _Kx; ++_Idx) { // add in another set of bits
_Ans += (static_cast<_Real>(_Gx()) - _Gxmin) * _Factor;
_Factor *= _Rx;
}
return _Ans / _Factor;
}
template <class _Gen, class = void>
struct _Has_static_min_max : false_type {};
// This checks a requirement of N4950 [rand.req.urng] `concept uniform_random_bit_generator` but doesn't attempt
// to implement the whole concept - we just need to distinguish Standard machinery from tr1 machinery.
template <class _Gen>
struct _Has_static_min_max<_Gen, void_t<decltype(bool_constant<(_Gen::min)() < (_Gen::max)()>::value)>> : true_type {};
template <class _Real, class _Gen>
_NODISCARD _Real _Nrand_impl(_Gen& _Gx) { // build a floating-point value from random sequence
_RNG_REQUIRE_REALTYPE(_Nrand_impl, _Real);
constexpr auto _Digits = static_cast<size_t>(numeric_limits<_Real>::digits);
constexpr auto _Bits = ~size_t{0};
constexpr auto _Minbits = _Digits < _Bits ? _Digits : _Bits;
if constexpr (_Has_static_min_max<_Gen>::value && _Minbits <= 64) {
return _STD generate_canonical<_Real, _Minbits>(_Gx);
} else { // TRANSITION, for tr1 machinery only; Standard machinery can call generate_canonical directly
const _Real _Gxmin = static_cast<_Real>((_Gx.min)());
const _Real _Gxmax = static_cast<_Real>((_Gx.max)());
const _Real _Rx = (_Gxmax - _Gxmin) + _Real{1};
const int _Ceil = static_cast<int>(_STD ceil(static_cast<_Real>(_Minbits) / _STD log2(_Rx)));
const int _Kx = _Ceil < 1 ? 1 : _Ceil;
_Real _Ans{0};
_Real _Factor{1};
for (int _Idx = 0; _Idx < _Kx; ++_Idx) { // add in another set of bits
_Ans += (static_cast<_Real>(_Gx()) - _Gxmin) * _Factor;
_Factor *= _Rx;
}
return _Ans / _Factor;
}
}
#define _NRAND(eng, resty) (_Nrand_impl<resty>(eng))
_INLINE_VAR constexpr int _MP_len = 5;
using _MP_arr = uint64_t[_MP_len];
extern "C++" _NODISCARD _CRTIMP2_PURE uint64_t __CLRCALL_PURE_OR_CDECL _MP_Get(_MP_arr) noexcept;
extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _MP_Add(_MP_arr, uint64_t) noexcept;
extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _MP_Mul(_MP_arr, uint64_t, uint64_t) noexcept;
extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _MP_Rem(_MP_arr, uint64_t) noexcept;
template <class _Uint, _Uint _Ax, _Uint _Cx, _Uint _Mx>
_NODISCARD _Uint _Next_linear_congruential_value(_Uint _Prev) noexcept {
// Choose intermediate type:
// To use type T for the intermediate calculation, we must show
// _Ax * (_Mx - 1) + _Cx <= numeric_limits<T>::max()
// Split _Cx:
// _Cx <= numeric_limits<T>::max()
// && _Ax * (_Mx - 1) <= numeric_limits<T>::max() - _Cx
// Divide by _Ax:
// _Cx <= numeric_limits<T>::max()
// && (_Mx - 1) <= (numeric_limits<T>::max() - _Cx) / _Ax
if constexpr (_Ax == 0) { // degenerate case; avoid divide by 0
return static_cast<_Uint>(_Cx); // relies on _Mx == 0 || _Cx <= _Mx, N4950 [rand.eng.lcong]/3
} else if constexpr (_Mx == 0) {
// N4950 [rand.eng.lcong]/2: "If the template parameter m is 0, the modulus m
// used throughout this subclause [rand.eng.lcong] is
// numeric_limits<result_type>::max() plus 1." That is: Just do the multiply
// and let normal unsigned modulo take care of it
return static_cast<_Uint>(static_cast<_Uint>(_Ax * _Prev) + _Cx);
} else if constexpr (_Cx <= UINT_MAX && static_cast<_Uint>(_Mx - 1) <= (UINT_MAX - _Cx) / _Ax) {
// unsigned int is sufficient to store intermediate calculation
const auto _Mul =
static_cast<unsigned int>(_Prev) * static_cast<unsigned int>(_Ax) + static_cast<unsigned int>(_Cx);
return static_cast<_Uint>(_Mul % _Mx);
} else if constexpr (_Cx <= ULLONG_MAX && static_cast<_Uint>(_Mx - 1) <= (ULLONG_MAX - _Cx) / _Ax) {
// unsigned long long is sufficient to store intermediate calculation
const auto _Mul = static_cast<unsigned long long>(_Prev) * _Ax + _Cx;
return static_cast<_Uint>(_Mul % _Mx);
} else { // no intermediate integral type fits; fall back to multiprecision
_MP_arr _Wx;
_MP_Mul(_Wx, _Prev, _Ax);
_MP_Add(_Wx, _Cx);
_MP_Rem(_Wx, _Mx);
return static_cast<_Uint>(_MP_Get(_Wx));
}
}
template <class _Seed_seq>
_NODISCARD constexpr unsigned int _Seed_seq_to_uint(_Seed_seq& _Seq) {
unsigned int _Arr[4]{};
_Seq.generate(_Arr, _Arr + 4);
return _Arr[3];
}
template <class _Seed_seq>
_NODISCARD constexpr unsigned long long _Seed_seq_to_ull(_Seed_seq& _Seq) {
unsigned int _Arr[5]{};
_Seq.generate(_Arr, _Arr + 5);
unsigned long long _Result = _Arr[4];
_Result <<= 32;
_Result |= _Arr[3];
return _Result;
}
template <class _Uint, _Uint _Cx, _Uint _Mx>
_NODISCARD constexpr _Uint _Get_linear_congruential_seed(_Uint _Sx) noexcept { // N4950 [rand.eng.lcong]/5
if constexpr (_Mx != 0) {
_Sx %= _Mx;
}
if constexpr (_Cx == 0) {
if (_Sx == 0) {
_Sx = _Uint{1};
}
}
return _Sx;
}
template <class _Uint, _Uint _Cx, _Uint _Mx, class _Seed_seq>
_NODISCARD _Uint _Get_linear_congruential_seed_from_seq(_Seed_seq& _Seq) { // N4950 [rand.eng.lcong]/6
_Uint _Sx;
if constexpr (_Mx == 0) {
if constexpr (sizeof(_Uint) <= sizeof(unsigned int)) {
_Sx = static_cast<_Uint>(_Seed_seq_to_uint(_Seq));
} else {
_Sx = static_cast<_Uint>(_Seed_seq_to_ull(_Seq));
}
} else if constexpr (_Mx <= UINT_MAX) {
_Sx = static_cast<_Uint>(_Seed_seq_to_uint(_Seq) % _Mx);
} else {
_Sx = static_cast<_Uint>(_Seed_seq_to_ull(_Seq) % _Mx);
}
return _Get_linear_congruential_seed<_Uint, _Cx, _Mx>(_Sx);
}
_EXPORT_STD template <class _Uint, _Uint _Ax, _Uint _Cx, _Uint _Mx>
class linear_congruential_engine { // a linear congruential generator random engine
public:
_RNG_REQUIRE_UINTTYPE(linear_congruential_engine, _Uint);
static_assert(0 == _Mx || (_Ax < _Mx && _Cx < _Mx), "invalid template argument for linear_congruential_engine");
using result_type = _Uint;
static constexpr result_type multiplier = _Ax;
static constexpr result_type increment = _Cx;
static constexpr result_type modulus = _Mx;
_NODISCARD static constexpr result_type(min)() noexcept /* strengthened */ {
// return minimum possible generated value
return _Cx == 0;
}
#pragma warning(push)
#pragma warning(disable : 4309) // truncation of constant value
_NODISCARD static constexpr result_type(max)() noexcept /* strengthened */ {
// return maximum possible generated value
return static_cast<result_type>(_Mx - 1u); // note 0 wraps around to max
}
#pragma warning(pop)
static constexpr result_type default_seed = 1u;
linear_congruential_engine() noexcept // strengthened
: _Prev(_Get_linear_congruential_seed<result_type, _Cx, _Mx>(default_seed)) {}
explicit linear_congruential_engine(result_type _Sx) noexcept // strengthened
: _Prev(_Get_linear_congruential_seed<result_type, _Cx, _Mx>(_Sx)) {}
template <class _Seed_seq, _Enable_if_seed_seq_t<_Seed_seq, linear_congruential_engine> = 0>
explicit linear_congruential_engine(_Seed_seq& _Seq)
: _Prev(_Get_linear_congruential_seed_from_seq<result_type, _Cx, _Mx>(_Seq)) {}
void seed(result_type _Sx = default_seed) noexcept /* strengthened */ {
// reset sequence from numeric value
_Prev = _Get_linear_congruential_seed<result_type, _Cx, _Mx>(_Sx);
}
template <class _Seed_seq, _Enable_if_seed_seq_t<_Seed_seq, linear_congruential_engine> = 0>
void seed(_Seed_seq& _Seq) { // reset sequence from seed sequence
_Prev = _Get_linear_congruential_seed_from_seq<result_type, _Cx, _Mx>(_Seq);
}
_NODISCARD _Uint operator()() noexcept {
_Prev = _Next_linear_congruential_value<result_type, _Ax, _Cx, _Mx>(_Prev);
return _Prev;
}
void discard(unsigned long long _Nskip) noexcept /* strengthened */ {
// discard _Nskip elements
auto _Temp = _Prev;
for (; 0 < _Nskip; --_Nskip) {
_Temp = _Next_linear_congruential_value<_Uint, _Ax, _Cx, _Mx>(_Temp);
}
_Prev = _Temp;
}
_NODISCARD_FRIEND bool operator==(
const linear_congruential_engine& _Lhs, const linear_congruential_engine& _Rhs) noexcept
/* strengthened */ {
return _Lhs._Prev == _Rhs._Prev;
}
#if !_HAS_CXX20
_NODISCARD_FRIEND bool operator!=(
const linear_congruential_engine& _Lhs, const linear_congruential_engine& _Rhs) noexcept
/* strengthened */ {
return _Lhs._Prev != _Rhs._Prev;
}
#endif // !_HAS_CXX20
template <class _Elem, class _Traits>
friend basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr, linear_congruential_engine& _Eng) {
return _Istr >> _Eng._Prev;
}
template <class _Elem, class _Traits>
friend basic_ostream<_Elem, _Traits>& operator<<(
basic_ostream<_Elem, _Traits>& _Ostr, const linear_congruential_engine& _Eng) {
return _Ostr << _Eng._Prev;
}
private:
result_type _Prev;
};
template <class _Uint, _Uint _Ax, _Uint _Cx, _Uint _Mx>
class linear_congruential { // linear congruential random engine
public:
_RNG_REQUIRE_UINTTYPE(linear_congruential, _Uint);
static_assert(0 == _Mx || (_Ax < _Mx && _Cx < _Mx), "invalid template argument for linear_congruential");
using result_type = _Uint;
static constexpr _Uint multiplier = _Ax;
static constexpr _Uint increment = _Cx;
static constexpr _Uint modulus = _Mx;
linear_congruential() noexcept // strengthened
: _Prev(_Get_linear_congruential_seed<_Uint, _Cx, _Mx>(1u)) {}
explicit linear_congruential(_Uint _Xx0) noexcept // strengthened
: _Prev(_Get_linear_congruential_seed<_Uint, _Cx, _Mx>(_Xx0)) {}
template <class _Gen, _Enable_if_seed_seq_t<_Gen, linear_congruential> = 0>
linear_congruential(_Gen& _Seq) : _Prev(_Get_linear_congruential_seed<_Uint, _Cx, _Mx>(_Seq())) {}
void seed(_Uint _Xx0 = 1u) noexcept /* strengthened */ {
// reset sequence from numeric value
_Prev = _Get_linear_congruential_seed<_Uint, _Cx, _Mx>(_Xx0);
}
template <class _Gen, _Enable_if_seed_seq_t<_Gen, linear_congruential> = 0>
void seed(_Gen& _Seq) { // reset sequence from generator
_Prev = _Get_linear_congruential_seed<_Uint, _Cx, _Mx>(_Seq());
}
_NODISCARD _Uint(min)() const noexcept /* strengthened */ {
// return minimum possible generated value
return _Cx == 0;
}
#pragma warning(push)
#pragma warning(disable : 4309) // truncation of constant value
_NODISCARD _Uint(max)() const noexcept /* strengthened */ {
// return maximum possible generated value
return static_cast<_Uint>(_Mx - 1u); // note 0 wraps around to max
}
#pragma warning(pop)
_NODISCARD _Uint operator()() noexcept /* strengthened */ {
// return next value
_Prev = _Next_linear_congruential_value<_Uint, _Ax, _Cx, _Mx>(_Prev);
return _Prev;
}
void discard(unsigned long long _Nskip) noexcept /* strengthened */ {
// discard _Nskip elements
auto _Temp = _Prev;
for (; 0 < _Nskip; --_Nskip) {
_Temp = _Next_linear_congruential_value<_Uint, _Ax, _Cx, _Mx>(_Temp);
}
_Prev = _Temp;
}
_NODISCARD_FRIEND bool operator==(const linear_congruential& _Lhs, const linear_congruential& _Rhs) noexcept
/* strengthened */ {
return _Lhs._Prev == _Rhs._Prev;
}
#if !_HAS_CXX20
_NODISCARD_FRIEND bool operator!=(const linear_congruential& _Lhs, const linear_congruential& _Rhs) noexcept
/* strengthened */ {
return _Lhs._Prev != _Rhs._Prev;
}
#endif // !_HAS_CXX20
template <class _Elem, class _Traits>
friend basic_istream<_Elem, _Traits>& operator>>(basic_istream<_Elem, _Traits>& _Istr, linear_congruential& _Eng) {
return _Istr >> _Eng._Prev;
}
template <class _Elem, class _Traits>
friend basic_ostream<_Elem, _Traits>& operator<<(
basic_ostream<_Elem, _Traits>& _Ostr, const linear_congruential& _Eng) {
return _Ostr << _Eng._Prev;
}
private:
_Uint _Prev;
};
template <class _Ty, size_t _Nw>
struct _Circ_buf { // holds historical values for generators
_Ty _At(size_t _Ix) const noexcept {
return _Ax[_Base(_Ix)];
}
bool _Equals(const _Circ_buf& _Right) const noexcept {
const _Ty* _Last1 = _Ax + _Idx;
const _Ty* _Last2 = _Right._Ax + _Right._Idx;
const _Ty* _First;
const _Ty* _Last;
const _Ty* _Other;
bool _Use2 = _Base() < _Right._Base();
if (_Use2) { // _Right's range is higher up in the array
// than ours, so scan it first
_First = _Right._Ax + _Right._Base();
_Last = _Last2;
_Other = _Ax + _Base();
} else { // our range is higher up in the array
// than _Right's, so scan ours first
_First = _Ax + _Base();
_Last = _Last1;
_Other = _Right._Ax + _Right._Base();
}
ptrdiff_t _Nx0 = _Nw;
while (0 < _Nx0) { // scan
// note: may need up to three passes; each scan starts at the
// current highest array position and ends at the end of the
// array or the _Idx value, whichever comes first; the
// equality test succeeds only by reaching the _Idx value.
const _Ty* _Limit = _First < _Last ? _Last : _Use2 ? _Right._Ax + 2 * _Nw : _Ax + 2 * _Nw;
_Nx0 -= _Limit - _First;
while (_First != _Limit) {
if (*_First++ != *_Other++) {
return false;
}
}
_First = _Other;
_Last = _Use2 ? _Last1 : _Last2;
_Other = _Use2 ? _Right._Ax : _Ax;
_Use2 = !_Use2;
}
return true;
}
size_t _Base(size_t _Ix = 0) const noexcept {
_Ix += _Idx;
if (_Ix < _Nw) {
return _Ix + _Nw;
} else {
return _Ix - _Nw;
}
}
unsigned int _Idx;
_Ty _Ax[2 * _Nw];
};
template <class _Ty, size_t _Sx, size_t _Rx, class _Swc_Traits>
class _Swc_base : public _Circ_buf<_Ty, _Rx> { // common bits of subtract_with_carry/_01
public:
using result_type = _Ty;
using _Traits = _Swc_Traits;
using _Mybase = _Circ_buf<_Ty, _Rx>;
using _Seed_t = typename _Swc_Traits::_Seed_t;
static constexpr size_t short_lag = _Sx;
static constexpr size_t long_lag = _Rx;
static constexpr uint_least32_t default_seed = 19780503u;
_Swc_base() {
seed();
}
_Swc_base(_Seed_t _Xx0) {
seed(_Xx0);
}
template <class _Gen, _Enable_if_seed_seq_t<_Gen, _Swc_base> = 0>
_Swc_base(_Gen& _Gx) {
seed(_Gx);
}
void seed(_Seed_t _Value = 0u, bool _Readcy = false) { // set initial values from specified seed value
linear_congruential_engine<uint_least32_t, 40014U, 0U, 2147483563U> _Lc{
static_cast<uint_least32_t>(_Value == 0U ? default_seed : _Value)};
_Reset(_Lc, _Readcy);
}
template <class _Gen, enable_if_t<!is_arithmetic_v<_Gen>, int> = 0>
void seed(_Gen& _Gx, bool _Readcy = false) { // set initial values from range
_Reset(_Gx, _Readcy);
}
_NODISCARD result_type(min)() const noexcept /* strengthened */ {
return 0;
}
_NODISCARD result_type(max)() const noexcept /* strengthened */ {
return _Swc_Traits::_Max;
}
_NODISCARD result_type operator()() noexcept /* strengthened */ {
const auto _Ix = 2 * _Rx <= this->_Idx ? 0 : this->_Idx;
if (_Ix < _Sx) {
_Setx(_Ix, this->_Ax[_Ix + 2 * _Rx - _Sx], this->_Ax[_Ix + _Rx]);
} else if (_Ix < _Rx) {
_Setx(_Ix, this->_Ax[_Ix - _Sx], this->_Ax[_Ix + _Rx]);
} else {
_Setx(_Ix, this->_Ax[_Ix - _Sx], this->_Ax[_Ix - _Rx]);
}
this->_Idx = _Ix + 1;
return this->_Ax[_Ix];
}
void discard(unsigned long long _Nskip) { // discard _Nskip elements
for (; 0 < _Nskip; --_Nskip) {
(void) (*this)();
}
}
_NODISCARD_FRIEND bool operator==(const _Swc_base& _Left, const _Swc_base& _Right) noexcept /* strengthened */ {
return static_cast<const _Swc_base::_Mybase&>(_Left)._Equals(_Right) && _Left._Carry == _Right._Carry;
}
#if !_HAS_CXX20
_NODISCARD_FRIEND bool operator!=(const _Swc_base& _Left, const _Swc_base& _Right) noexcept /* strengthened */ {
return !(_Left == _Right);
}
#endif // !_HAS_CXX20
template <class _Elem, class _Traits>
friend basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr, _Swc_base& _Eng) { // read state from _Istr
_Wrap_istream<_Elem, _Traits, typename _Swc_Traits::_Seed_t> _Gen(_Istr);
_Eng.seed(_Gen, true);
return _Istr;
}
template <class _Elem, class _Traits>
friend basic_ostream<_Elem, _Traits>& operator<<(basic_ostream<_Elem, _Traits>& _Ostr,
const _Swc_base& _Eng) { // write state to _Ostr
_Swc_base::_Traits::_Write(_Ostr, _Eng, _Eng._Carry);
return _Ostr;
}
template <class _Elem, class _Traits>
basic_ostream<_Elem, _Traits>& _Write_full(basic_ostream<_Elem, _Traits>& _Ostr) const { // write state to _Ostr
_Swc_Traits::_Write_full(_Ostr, *this, _Carry);
return _Ostr;
}
protected:
template <class _Gen>
void _Reset(_Gen& _Gx, bool _Readcy) { // reset sequence
_Carry = _Swc_Traits::_Reset(_Gx, this->_Ax, _Readcy);
this->_Idx = _Rx;
}
void _Setx(size_t _Ix, _Ty _Xis, _Ty _Xir) noexcept { // update _Ax[_Ix] and _Carry
bool _Underflowed = false;
_Ty _Newx = _Xis;
if (_Newx < _Xir) {
_Underflowed = true;
}
_Newx -= _Xir;
if (_Newx < static_cast<typename _Swc_Traits::_UCy_t>(_Carry)) {
_Underflowed = true;
}
_Newx -= _Carry;
if (_Underflowed) { // underflowed, so add _Mod
_Newx += _Swc_Traits::_Mod;
_Carry = _Swc_Traits::_Cy;
} else {
_Carry = 0;
}
this->_Ax[_Ix] = _Newx;
}
typename _Swc_Traits::_Cy_t _Carry;
};
template <class _Ty, _Ty _Mx, size_t _Nw>
struct _Swc_traits { // traits for subtract_with_carry generator
using _Cy_t = int;
using _UCy_t = unsigned int;
using _Mod_t = _Ty;
using _Seed_t = _Ty;
static constexpr _Cy_t _Cy = 1;
static constexpr _Mod_t _Mod = _Mx;
static constexpr _Ty _Max = static_cast<_Ty>(_Mx - 1);
static int _Get_wc() noexcept { // compute number of 32-bit words per element
int _Kx;
if constexpr (_Mx == 0) {
_Kx = (8 * sizeof(_Ty) + 31) / 32;
} else { // compute number of 32-bit words required
unsigned long long _Val = 1ULL << 32;
for (_Kx = 1; 0 < _Val && _Val < _Mx; ++_Kx) {
_Val = _Val << 32;
}
}
return _Kx;
}
template <class _Gen>
static _Cy_t _Reset(_Gen& _Gx, _Ty* _Ax, bool _Readcy) { // set initial values of _Ax from generator _Gx
// return value of _Cy from range if _Readcy is true,
// otherwise compute from last value
int _Kx = _Get_wc();
for (size_t _Ix = 0; _Ix < _Nw; ++_Ix) { // pack _Kx words
_Ax[_Ix] = _Gx();
for (int _Jx = 1; _Jx < _Kx; ++_Jx) {
_Ax[_Ix] |= static_cast<_Ty>(_Gx()) << (32 * _Jx);
}
}
_Cy_t _Ans = _Reduce(_Ax);
if (!_Readcy) {
return _Ans;
} else {
return static_cast<_Cy_t>(_Gx()); // TRANSITION, investigate this conversion
}
}
#pragma warning(push)
#pragma warning(disable : 4724) // potential mod by 0
static _Cy_t _Reduce(_Ty* _Ax) noexcept { // reduce values to allowed range
if constexpr (_Mx != 0) {
for (size_t _Ix = 0; _Ix < _Nw; ++_Ix) {
_Ax[_Ix] = _Ax[_Ix] % _Mx;
}
}
return _Ax[_Nw - 1] == 0;
}
#pragma warning(pop)
template <class _Elem, class _Traits>
static void _Write(
basic_ostream<_Elem, _Traits>& _Ostr, const _Circ_buf<_Ty, _Nw>& _Buf, _Cy_t _Cy) { // write state to _Ostr
int _Kx = _Get_wc();
for (size_t _Ix = 0; _Ix < _Nw; ++_Ix) {
for (int _Jx = 0; _Jx < _Kx; ++_Jx) { // unpack into _Kx words
const unsigned int _Word = static_cast<unsigned int>(_Buf._At(_Ix) >> (_Jx * 32));
_Ostr << _Word << ' ';
}
}
_Ostr << _Cy;
}
template <class _Elem, class _Traits>
static void _Write_full(
basic_ostream<_Elem, _Traits>& _Ostr, const _Circ_buf<_Ty, _Nw>& _Buf, _Cy_t _Cy) { // write state to _Ostr
for (size_t _Ix = 0; _Ix < _Nw; ++_Ix) {
_Ostr << _Buf._At(_Ix) << ' ';
}
_Ostr << _Cy;
}
};
template <class _Ty, _Ty _Mx, size_t _Sx, size_t _Rx>
class subtract_with_carry
: public _Swc_base<_Ty, _Sx, _Rx, _Swc_traits<_Ty, _Mx, _Rx>> { // subtract_with_carry generator
public:
using _Mybase = _Swc_base<_Ty, _Sx, _Rx, _Swc_traits<_Ty, _Mx, _Rx>>;
static constexpr _Ty modulus = _Mx;
using _Mybase::default_seed;
subtract_with_carry() : _Mybase(0u) {}
explicit subtract_with_carry(_Ty _Xx0) : _Mybase(_Xx0) {}
template <class _Gen, _Enable_if_seed_seq_t<_Gen, subtract_with_carry> = 0>
subtract_with_carry(_Gen& _Gx) : _Mybase(_Gx) {}
};
_EXPORT_STD template <class _Ty, size_t _Wx, size_t _Sx, size_t _Rx>
class subtract_with_carry_engine
: public subtract_with_carry<_Ty, static_cast<_Ty>((_Ty{1} << (_Wx - 1)) << 1), _Sx, _Rx> {
// subtract_with_carry generator
public:
_RNG_REQUIRE_UINTTYPE(subtract_with_carry_engine, _Ty);
static_assert(0U < _Sx && _Sx < _Rx && 0 < _Wx && _Wx <= numeric_limits<_Ty>::digits,
"invalid template argument for subtract_with_carry_engine");
static constexpr _Ty _Mx = static_cast<_Ty>((_Ty{1} << (_Wx - 1)) << 1);
static constexpr size_t word_size = _Wx;
static constexpr size_t short_lag = _Sx;
static constexpr size_t long_lag = _Rx;
using _Mybase = subtract_with_carry<_Ty, _Mx, _Sx, _Rx>;
using _Traits = typename _Mybase::_Traits;
using result_type = _Ty;
using _Mybase::default_seed;
subtract_with_carry_engine() : _Mybase(0u) {}
explicit subtract_with_carry_engine(_Ty _Xx0) : _Mybase(_Xx0) {}
template <class _Seed_seq, _Enable_if_seed_seq_t<_Seed_seq, subtract_with_carry_engine> = 0>
explicit subtract_with_carry_engine(_Seed_seq& _Seq) : _Mybase() {
seed(_Seq);
}
void seed(_Ty _Value = 0u) { // set initial values from specified seed value
_Mybase::seed(_Value);
}
static constexpr int _Kx = (8 * sizeof(_Ty) + 31) / 32;
template <class _Seed_seq, _Enable_if_seed_seq_t<_Seed_seq, subtract_with_carry_engine> = 0>
void seed(_Seed_seq& _Seq) { // reset sequence from seed sequence
unsigned long _Arr[_Kx * _Rx];
_Seq.generate(&_Arr[0], &_Arr[_Kx * _Rx]);
size_t _Idx0 = 0;
for (size_t _Ix = 0; _Ix < _Rx; ++_Ix, _Idx0 += _Kx) { // pack _Kx words
this->_Ax[_Ix] = _Arr[_Idx0];
for (int _Jx = 1; _Jx < _Kx; ++_Jx) {
this->_Ax[_Ix] |= static_cast<_Ty>(_Arr[_Idx0 + _Jx]) << (32 * _Jx);
}
constexpr bool _Mod_non_zero = _Traits::_Mod != 0;
if constexpr (_Mod_non_zero) {
this->_Ax[_Ix] %= _Traits::_Mod;
}
}
this->_Carry = _Traits::_Reduce(this->_Ax);
this->_Idx = _Rx;
}
_NODISCARD static constexpr _Ty(min)() noexcept /* strengthened */ {
return 0;
}
_NODISCARD static constexpr _Ty(max)() noexcept /* strengthened */ {
return _Mx - 1;
}
template <class _Elem, class _Traits>
friend basic_ostream<_Elem, _Traits>& operator<<(
basic_ostream<_Elem, _Traits>& _Ostr, const subtract_with_carry_engine& _Eng) {
const auto _Save_flags = _Ostr.flags(ios_base::dec | ios_base::left);
const auto _Save_fill = _Ostr.fill(' ');
_Eng._Write_full(_Ostr);
_Ostr.flags(_Save_flags);
_Ostr.fill(_Save_fill);
return _Ostr;
}
template <class _Elem, class _Traits>
friend basic_istream<_Elem, _Traits>& operator>>(
basic_istream<_Elem, _Traits>& _Istr, subtract_with_carry_engine& _Eng) {
constexpr auto _Nx = long_lag;
result_type _Buffer[_Nx];
typename _Mybase::_Traits::_Cy_t _Carry_buf;
const auto _Save_flags = _Istr.flags(ios_base::dec | ios_base::skipws);
for (auto& _Val : _Buffer) {
_Istr >> _Val;
}
_Istr >> _Carry_buf;
if (_Istr) {
for (size_t _Ix = 0; _Ix < _Nx; ++_Ix) {
_Eng._Ax[_Ix] = _Buffer[_Ix];
}
_Eng._Carry = _Carry_buf;
_Eng._Idx = _Nx;
} else {