aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxx/include/__random/seed_seq.h
blob: 8640cd1b4e43a4dbdcbf537f76bec1b441ef5c4e (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
//===----------------------------------------------------------------------===//
//
// 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_SEED_SEQ_H
#define _LIBCPP___RANDOM_SEED_SEQ_H

#include <__algorithm/copy.h>
#include <__algorithm/fill.h>
#include <__algorithm/max.h>
#include <__config>
#include <initializer_list>
#include <vector>

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

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

class _LIBCPP_TEMPLATE_VIS seed_seq
{
public:
    // types
    typedef uint32_t result_type;

    // constructors
    _LIBCPP_INLINE_VISIBILITY
    seed_seq() _NOEXCEPT {}
#ifndef _LIBCPP_CXX03_LANG
    template<class _Tp, __enable_if_t<is_integral<_Tp>::value>* = nullptr>
    _LIBCPP_INLINE_VISIBILITY
    seed_seq(initializer_list<_Tp> __il) {
        __init(__il.begin(), __il.end());
    }
#endif // _LIBCPP_CXX03_LANG

    template<class _InputIterator>
    _LIBCPP_INLINE_VISIBILITY
    seed_seq(_InputIterator __first, _InputIterator __last) {
        static_assert(is_integral<typename iterator_traits<_InputIterator>::value_type>::value,
            "Mandates: iterator_traits<InputIterator>::value_type is an integer type");
        __init(__first, __last);
    }

    // generating functions
    template<class _RandomAccessIterator>
        void generate(_RandomAccessIterator __first, _RandomAccessIterator __last);

    // property functions
    _LIBCPP_INLINE_VISIBILITY
    size_t size() const _NOEXCEPT {return __v_.size();}
    template<class _OutputIterator>
        _LIBCPP_INLINE_VISIBILITY
        void param(_OutputIterator __dest) const
            {_VSTD::copy(__v_.begin(), __v_.end(), __dest);}

    seed_seq(const seed_seq&) = delete;
    void operator=(const seed_seq&) = delete;

    _LIBCPP_INLINE_VISIBILITY
    static result_type _Tp(result_type __x) {return __x ^ (__x >> 27);}

private:
    template<class _InputIterator>
    void __init(_InputIterator __first, _InputIterator __last);

    vector<result_type> __v_;
};

template<class _InputIterator>
void
seed_seq::__init(_InputIterator __first, _InputIterator __last)
{
    for (_InputIterator __s = __first; __s != __last; ++__s)
        __v_.push_back(*__s & 0xFFFFFFFF);
}

template<class _RandomAccessIterator>
void
seed_seq::generate(_RandomAccessIterator __first, _RandomAccessIterator __last)
{
    if (__first != __last)
    {
        _VSTD::fill(__first, __last, 0x8b8b8b8b);
        const size_t __n = static_cast<size_t>(__last - __first);
        const size_t __s = __v_.size();
        const size_t __t = (__n >= 623) ? 11
                         : (__n >= 68) ? 7
                         : (__n >= 39) ? 5
                         : (__n >= 7)  ? 3
                         : (__n - 1) / 2;
        const size_t __p = (__n - __t) / 2;
        const size_t __q = __p + __t;
        const size_t __m = _VSTD::max(__s + 1, __n);
        // __k = 0;
        {
            result_type __r = 1664525 * _Tp(__first[0] ^ __first[__p]
                                                      ^  __first[__n - 1]);
            __first[__p] += __r;
            __r += __s;
            __first[__q] += __r;
            __first[0] = __r;
        }
        for (size_t __k = 1; __k <= __s; ++__k)
        {
            const size_t __kmodn = __k % __n;
            const size_t __kpmodn = (__k + __p) % __n;
            result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
                                           ^ __first[(__k - 1) % __n]);
            __first[__kpmodn] += __r;
            __r +=  __kmodn + __v_[__k-1];
            __first[(__k + __q) % __n] += __r;
            __first[__kmodn] = __r;
        }
        for (size_t __k = __s + 1; __k < __m; ++__k)
        {
            const size_t __kmodn = __k % __n;
            const size_t __kpmodn = (__k + __p) % __n;
            result_type __r = 1664525 * _Tp(__first[__kmodn] ^ __first[__kpmodn]
                                           ^ __first[(__k - 1) % __n]);
            __first[__kpmodn] += __r;
            __r +=  __kmodn;
            __first[(__k + __q) % __n] += __r;
            __first[__kmodn] = __r;
        }
        for (size_t __k = __m; __k < __m + __n; ++__k)
        {
            const size_t __kmodn = __k % __n;
            const size_t __kpmodn = (__k + __p) % __n;
            result_type __r = 1566083941 * _Tp(__first[__kmodn] +
                                              __first[__kpmodn] +
                                              __first[(__k - 1) % __n]);
            __first[__kpmodn] ^= __r;
            __r -= __kmodn;
            __first[(__k + __q) % __n] ^= __r;
            __first[__kmodn] = __r;
        }
    }
}

_LIBCPP_END_NAMESPACE_STD

_LIBCPP_POP_MACROS

#endif // _LIBCPP___RANDOM_SEED_SEQ_H