1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___RANDOM_SHUFFLE_ORDER_ENGINE_H
10#define _LIBCPP___RANDOM_SHUFFLE_ORDER_ENGINE_H
11
12#include <__algorithm/equal.h>
13#include <__config>
14#include <__cstddef/size_t.h>
15#include <__random/is_seed_sequence.h>
16#include <__type_traits/enable_if.h>
17#include <__type_traits/integral_constant.h>
18#include <__type_traits/is_convertible.h>
19#include <__utility/move.h>
20#include <cstdint>
21#include <iosfwd>
22
23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24# pragma GCC system_header
25#endif
26
27_LIBCPP_PUSH_MACROS
28#include <__undef_macros>
29
30_LIBCPP_BEGIN_NAMESPACE_STD
31
32template <uint64_t _Xp, uint64_t _Yp>
33struct __ugcd {
34 static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
35};
36
37template <uint64_t _Xp>
38struct __ugcd<_Xp, 0> {
39 static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
40};
41
42template <uint64_t _Np, uint64_t _Dp>
43class __uratio {
44 static_assert(_Dp != 0, "__uratio divide by 0");
45 static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
46
47public:
48 static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
49 static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;
50
51 typedef __uratio<num, den> type;
52};
53
54template <class _Engine, size_t __k>
55class shuffle_order_engine {
56 static_assert(0 < __k, "shuffle_order_engine invalid parameters");
57
58public:
59 // types
60 typedef typename _Engine::result_type result_type;
61
62private:
63 _Engine __e_;
64 result_type __v_[__k];
65 result_type __y_;
66
67public:
68 // engine characteristics
69 static inline _LIBCPP_CONSTEXPR const size_t table_size = __k;
70
71#ifdef _LIBCPP_CXX03_LANG
72 static const result_type _Min = _Engine::_Min;
73 static const result_type _Max = _Engine::_Max;
74#else
75 static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
76 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
77#endif
78 static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
79 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
80 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
81
82 static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;
83
84 // constructors and seeding functions
85 _LIBCPP_HIDE_FROM_ABI shuffle_order_engine() { __init(); }
86 _LIBCPP_HIDE_FROM_ABI explicit shuffle_order_engine(const _Engine& __e) : __e_(__e) { __init(); }
87#ifndef _LIBCPP_CXX03_LANG
88 _LIBCPP_HIDE_FROM_ABI explicit shuffle_order_engine(_Engine&& __e) : __e_(std::move(__e)) { __init(); }
89#endif // _LIBCPP_CXX03_LANG
90 _LIBCPP_HIDE_FROM_ABI explicit shuffle_order_engine(result_type __sd) : __e_(__sd) { __init(); }
91 template <class _Sseq,
92 __enable_if_t<__is_seed_sequence_v<_Sseq, shuffle_order_engine> && !is_convertible<_Sseq, _Engine>::value,
93 int> = 0>
94 _LIBCPP_HIDE_FROM_ABI explicit shuffle_order_engine(_Sseq& __q) : __e_(__q) {
95 __init();
96 }
97 _LIBCPP_HIDE_FROM_ABI void seed() {
98 __e_.seed();
99 __init();
100 }
101 _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) {
102 __e_.seed(__sd);
103 __init();
104 }
105 template <class _Sseq, __enable_if_t<__is_seed_sequence_v<_Sseq, shuffle_order_engine>, int> = 0>
106 _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
107 __e_.seed(__q);
108 __init();
109 }
110
111 // generating functions
112 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI result_type operator()() {
113 if _LIBCPP_CONSTEXPR (_Rp != 0 || !(__k & 1)) {
114 using _Ratio = __uratio<__k, _Rp != 0 ? _Rp : 0x8000000000000000ull>;
115 if _LIBCPP_CONSTEXPR (_Ratio::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)) {
116 return __evalf<_Ratio::num, _Ratio::den>();
117 } else {
118 const size_t __j = static_cast<size_t>(_Ratio::num * (__y_ - _Min) / _Ratio::den);
119 __y_ = __v_[__j];
120 __v_[__j] = __e_();
121 return __y_;
122 }
123 } else
124 return __evalf<__k, 0>();
125 }
126
127 _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
128 for (; __z; --__z)
129 (void)operator()();
130 }
131
132 // property functions
133 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }
134
135private:
136 template <class _Eng, size_t _Kp>
137 friend bool operator==(const shuffle_order_engine<_Eng, _Kp>& __x, const shuffle_order_engine<_Eng, _Kp>& __y);
138
139 template <class _Eng, size_t _Kp>
140 friend bool operator!=(const shuffle_order_engine<_Eng, _Kp>& __x, const shuffle_order_engine<_Eng, _Kp>& __y);
141
142 template <class _CharT, class _Traits, class _Eng, size_t _Kp>
143 friend basic_ostream<_CharT, _Traits>&
144 operator<<(basic_ostream<_CharT, _Traits>& __os, const shuffle_order_engine<_Eng, _Kp>& __x);
145
146 template <class _CharT, class _Traits, class _Eng, size_t _Kp>
147 friend basic_istream<_CharT, _Traits>&
148 operator>>(basic_istream<_CharT, _Traits>& __is, shuffle_order_engine<_Eng, _Kp>& __x);
149
150 _LIBCPP_HIDE_FROM_ABI void __init() {
151 for (size_t __i = 0; __i < __k; ++__i)
152 __v_[__i] = __e_();
153 __y_ = __e_();
154 }
155
156 template <uint64_t __n, uint64_t __d>
157 _LIBCPP_HIDE_FROM_ABI result_type __evalf() {
158 const double __fp = __d == 0 ? __n / (2. * 0x8000000000000000ull) : __n / (double)__d;
159 const size_t __j = static_cast<size_t>(__fp * (__y_ - _Min));
160 __y_ = __v_[__j];
161 __v_[__j] = __e_();
162 return __y_;
163 }
164};
165
166template <class _Eng, size_t _Kp>
167_LIBCPP_HIDE_FROM_ABI bool
168operator==(const shuffle_order_engine<_Eng, _Kp>& __x, const shuffle_order_engine<_Eng, _Kp>& __y) {
169 return __x.__y_ == __y.__y_ && std::equal(__x.__v_, __x.__v_ + _Kp, __y.__v_) && __x.__e_ == __y.__e_;
170}
171
172template <class _Eng, size_t _Kp>
173inline _LIBCPP_HIDE_FROM_ABI bool
174operator!=(const shuffle_order_engine<_Eng, _Kp>& __x, const shuffle_order_engine<_Eng, _Kp>& __y) {
175 return !(__x == __y);
176}
177
178template <class _CharT, class _Traits, class _Eng, size_t _Kp>
179_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
180operator<<(basic_ostream<_CharT, _Traits>& __os, const shuffle_order_engine<_Eng, _Kp>& __x) {
181 __save_flags<_CharT, _Traits> __lx(__os);
182 typedef basic_ostream<_CharT, _Traits> _Ostream;
183 __os.flags(_Ostream::dec | _Ostream::left);
184 _CharT __sp = __os.widen(' ');
185 __os.fill(__sp);
186 __os << __x.__e_ << __sp << __x.__v_[0];
187 for (size_t __i = 1; __i < _Kp; ++__i)
188 __os << __sp << __x.__v_[__i];
189 return __os << __sp << __x.__y_;
190}
191
192template <class _CharT, class _Traits, class _Eng, size_t _Kp>
193_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
194operator>>(basic_istream<_CharT, _Traits>& __is, shuffle_order_engine<_Eng, _Kp>& __x) {
195 typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
196 __save_flags<_CharT, _Traits> __lx(__is);
197 typedef basic_istream<_CharT, _Traits> _Istream;
198 __is.flags(_Istream::dec | _Istream::skipws);
199 _Eng __e;
200 result_type __vp[_Kp + 1];
201 __is >> __e;
202 for (size_t __i = 0; __i < _Kp + 1; ++__i)
203 __is >> __vp[__i];
204 if (!__is.fail()) {
205 __x.__e_ = __e;
206 for (size_t __i = 0; __i < _Kp; ++__i)
207 __x.__v_[__i] = __vp[__i];
208 __x.__y_ = __vp[_Kp];
209 }
210 return __is;
211}
212
213_LIBCPP_END_NAMESPACE_STD
214
215_LIBCPP_POP_MACROS
216
217#endif // _LIBCPP___RANDOM_SHUFFLE_ORDER_ENGINE_H
218