aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/libcxx/include/__random/shuffle_order_engine.h
blob: c2f76f609ae571477a88f07fc770c2df624c3d01 (plain) (blame)
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
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___RANDOM_SHUFFLE_ORDER_ENGINE_H
#define _LIBCPP___RANDOM_SHUFFLE_ORDER_ENGINE_H

#include <__algorithm/equal.h>
#include <__config>
#include <__random/is_seed_sequence.h>
#include <__utility/move.h>
#include <cstdint>
#include <iosfwd>
#include <type_traits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#  pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

template <uint64_t _Xp, uint64_t _Yp>
struct __ugcd
{
    static _LIBCPP_CONSTEXPR const uint64_t value = __ugcd<_Yp, _Xp % _Yp>::value;
};

template <uint64_t _Xp>
struct __ugcd<_Xp, 0>
{
    static _LIBCPP_CONSTEXPR const uint64_t value = _Xp;
};

template <uint64_t _Np, uint64_t _Dp>
class __uratio
{
    static_assert(_Dp != 0, "__uratio divide by 0");
    static _LIBCPP_CONSTEXPR const uint64_t __gcd = __ugcd<_Np, _Dp>::value;
public:
    static _LIBCPP_CONSTEXPR const uint64_t num = _Np / __gcd;
    static _LIBCPP_CONSTEXPR const uint64_t den = _Dp / __gcd;

    typedef __uratio<num, den> type;
};

template<class _Engine, size_t __k>
class _LIBCPP_TEMPLATE_VIS shuffle_order_engine
{
    static_assert(0 < __k, "shuffle_order_engine invalid parameters");
public:
    // types
    typedef typename _Engine::result_type result_type;

private:
    _Engine __e_;
    result_type _V_[__k];
    result_type _Y_;

public:
    // engine characteristics
    static _LIBCPP_CONSTEXPR const size_t table_size = __k;

#ifdef _LIBCPP_CXX03_LANG
    static const result_type _Min = _Engine::_Min;
    static const result_type _Max = _Engine::_Max;
#else
    static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
    static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
#endif
    static_assert(_Min < _Max, "shuffle_order_engine invalid parameters");
    _LIBCPP_INLINE_VISIBILITY
    static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
    _LIBCPP_INLINE_VISIBILITY
    static _LIBCPP_CONSTEXPR result_type max() { return _Max; }

    static _LIBCPP_CONSTEXPR const unsigned long long _Rp = _Max - _Min + 1ull;

    // constructors and seeding functions
    _LIBCPP_INLINE_VISIBILITY
    shuffle_order_engine() {__init();}
    _LIBCPP_INLINE_VISIBILITY
    explicit shuffle_order_engine(const _Engine& __e)
        : __e_(__e) {__init();}
#ifndef _LIBCPP_CXX03_LANG
    _LIBCPP_INLINE_VISIBILITY
    explicit shuffle_order_engine(_Engine&& __e)
        : __e_(_VSTD::move(__e)) {__init();}
#endif // _LIBCPP_CXX03_LANG
    _LIBCPP_INLINE_VISIBILITY
    explicit shuffle_order_engine(result_type __sd) : __e_(__sd) {__init();}
    template<class _Sseq>
        _LIBCPP_INLINE_VISIBILITY
        explicit shuffle_order_engine(_Sseq& __q,
        typename enable_if<__is_seed_sequence<_Sseq, shuffle_order_engine>::value &&
                           !is_convertible<_Sseq, _Engine>::value>::type* = 0)
         : __e_(__q) {__init();}
    _LIBCPP_INLINE_VISIBILITY
    void seed() {__e_.seed(); __init();}
    _LIBCPP_INLINE_VISIBILITY
    void seed(result_type __sd) {__e_.seed(__sd); __init();}
    template<class _Sseq>
        _LIBCPP_INLINE_VISIBILITY
        typename enable_if
        <
            __is_seed_sequence<_Sseq, shuffle_order_engine>::value,
            void
        >::type
        seed(_Sseq& __q) {__e_.seed(__q); __init();}

    // generating functions
    _LIBCPP_INLINE_VISIBILITY
    result_type operator()() {return __eval(integral_constant<bool, _Rp != 0>());}
    _LIBCPP_INLINE_VISIBILITY
    void discard(unsigned long long __z) {for (; __z; --__z) operator()();}

    // property functions
    _LIBCPP_INLINE_VISIBILITY
    const _Engine& base() const _NOEXCEPT {return __e_;}

private:
    template<class _Eng, size_t _Kp>
    friend
    bool
    operator==(
        const shuffle_order_engine<_Eng, _Kp>& __x,
        const shuffle_order_engine<_Eng, _Kp>& __y);

    template<class _Eng, size_t _Kp>
    friend
    bool
    operator!=(
        const shuffle_order_engine<_Eng, _Kp>& __x,
        const shuffle_order_engine<_Eng, _Kp>& __y);

    template <class _CharT, class _Traits,
              class _Eng, size_t _Kp>
    friend
    basic_ostream<_CharT, _Traits>&
    operator<<(basic_ostream<_CharT, _Traits>& __os,
               const shuffle_order_engine<_Eng, _Kp>& __x);

    template <class _CharT, class _Traits,
              class _Eng, size_t _Kp>
    friend
    basic_istream<_CharT, _Traits>&
    operator>>(basic_istream<_CharT, _Traits>& __is,
               shuffle_order_engine<_Eng, _Kp>& __x);

    _LIBCPP_INLINE_VISIBILITY
    void __init()
    {
        for (size_t __i = 0; __i < __k; ++__i)
            _V_[__i] = __e_();
        _Y_ = __e_();
    }

    _LIBCPP_INLINE_VISIBILITY
    result_type __eval(false_type) {return __eval2(integral_constant<bool, __k & 1>());}
    _LIBCPP_INLINE_VISIBILITY
    result_type __eval(true_type) {return __eval(__uratio<__k, _Rp>());}

    _LIBCPP_INLINE_VISIBILITY
    result_type __eval2(false_type) {return __eval(__uratio<__k/2, 0x8000000000000000ull>());}
    _LIBCPP_INLINE_VISIBILITY
    result_type __eval2(true_type) {return __evalf<__k, 0>();}

    template <uint64_t _Np, uint64_t _Dp>
        _LIBCPP_INLINE_VISIBILITY
        typename enable_if
        <
            (__uratio<_Np, _Dp>::num > 0xFFFFFFFFFFFFFFFFull / (_Max - _Min)),
            result_type
        >::type
        __eval(__uratio<_Np, _Dp>)
            {return __evalf<__uratio<_Np, _Dp>::num, __uratio<_Np, _Dp>::den>();}

    template <uint64_t _Np, uint64_t _Dp>
        _LIBCPP_INLINE_VISIBILITY
        typename enable_if
        <
            __uratio<_Np, _Dp>::num <= 0xFFFFFFFFFFFFFFFFull / (_Max - _Min),
            result_type
        >::type
        __eval(__uratio<_Np, _Dp>)
        {
            const size_t __j = static_cast<size_t>(__uratio<_Np, _Dp>::num * (_Y_ - _Min)
                                                   / __uratio<_Np, _Dp>::den);
            _Y_ = _V_[__j];
            _V_[__j] = __e_();
            return _Y_;
        }

    template <uint64_t __n, uint64_t __d>
        _LIBCPP_INLINE_VISIBILITY
        result_type __evalf()
        {
            const double _Fp = __d == 0 ?
                __n / (2. * 0x8000000000000000ull) :
                __n / (double)__d;
            const size_t __j = static_cast<size_t>(_Fp * (_Y_ - _Min));
            _Y_ = _V_[__j];
            _V_[__j] = __e_();
            return _Y_;
        }
};

template<class _Engine, size_t __k>
    _LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;

template<class _Eng, size_t _Kp>
bool
operator==(
    const shuffle_order_engine<_Eng, _Kp>& __x,
    const shuffle_order_engine<_Eng, _Kp>& __y)
{
    return __x._Y_ == __y._Y_ && _VSTD::equal(__x._V_, __x._V_ + _Kp, __y._V_) &&
           __x.__e_ == __y.__e_;
}

template<class _Eng, size_t _Kp>
inline _LIBCPP_INLINE_VISIBILITY
bool
operator!=(
    const shuffle_order_engine<_Eng, _Kp>& __x,
    const shuffle_order_engine<_Eng, _Kp>& __y)
{
    return !(__x == __y);
}

template <class _CharT, class _Traits,
          class _Eng, size_t _Kp>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os,
           const shuffle_order_engine<_Eng, _Kp>& __x)
{
    __save_flags<_CharT, _Traits> __lx(__os);
    typedef basic_ostream<_CharT, _Traits> _Ostream;
    __os.flags(_Ostream::dec | _Ostream::left);
    _CharT __sp = __os.widen(' ');
    __os.fill(__sp);
    __os << __x.__e_ << __sp << __x._V_[0];
    for (size_t __i = 1; __i < _Kp; ++__i)
        __os << __sp << __x._V_[__i];
    return __os << __sp << __x._Y_;
}

template <class _CharT, class _Traits,
          class _Eng, size_t _Kp>
basic_istream<_CharT, _Traits>&
operator>>(basic_istream<_CharT, _Traits>& __is,
           shuffle_order_engine<_Eng, _Kp>& __x)
{
    typedef typename shuffle_order_engine<_Eng, _Kp>::result_type result_type;
    __save_flags<_CharT, _Traits> __lx(__is);
    typedef basic_istream<_CharT, _Traits> _Istream;
    __is.flags(_Istream::dec | _Istream::skipws);
    _Eng __e;
    result_type _Vp[_Kp+1];
    __is >> __e;
    for (size_t __i = 0; __i < _Kp+1; ++__i)
        __is >> _Vp[__i];
    if (!__is.fail())
    {
        __x.__e_ = __e;
        for (size_t __i = 0; __i < _Kp; ++__i)
            __x._V_[__i] = _Vp[__i];
        __x._Y_ = _Vp[_Kp];
    }
    return __is;
}

_LIBCPP_END_NAMESPACE_STD

_LIBCPP_POP_MACROS

#endif // _LIBCPP___RANDOM_SHUFFLE_ORDER_ENGINE_H