Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/libcxx/include/__random/discard_block_engine.h
35233 views
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_DISCARD_BLOCK_ENGINE_H
10
#define _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
11
12
#include <__config>
13
#include <__random/is_seed_sequence.h>
14
#include <__type_traits/enable_if.h>
15
#include <__type_traits/is_convertible.h>
16
#include <__utility/move.h>
17
#include <cstddef>
18
#include <iosfwd>
19
#include <limits>
20
21
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22
# pragma GCC system_header
23
#endif
24
25
_LIBCPP_PUSH_MACROS
26
#include <__undef_macros>
27
28
_LIBCPP_BEGIN_NAMESPACE_STD
29
30
template <class _Engine, size_t __p, size_t __r>
31
class _LIBCPP_TEMPLATE_VIS discard_block_engine {
32
_Engine __e_;
33
int __n_;
34
35
static_assert(0 < __r, "discard_block_engine invalid parameters");
36
static_assert(__r <= __p, "discard_block_engine invalid parameters");
37
#ifndef _LIBCPP_CXX03_LANG // numeric_limits::max() is not constexpr in C++03
38
static_assert(__r <= numeric_limits<int>::max(), "discard_block_engine invalid parameters");
39
#endif
40
41
public:
42
// types
43
typedef typename _Engine::result_type result_type;
44
45
// engine characteristics
46
static _LIBCPP_CONSTEXPR const size_t block_size = __p;
47
static _LIBCPP_CONSTEXPR const size_t used_block = __r;
48
49
#ifdef _LIBCPP_CXX03_LANG
50
static const result_type _Min = _Engine::_Min;
51
static const result_type _Max = _Engine::_Max;
52
#else
53
static constexpr result_type _Min = _Engine::min();
54
static constexpr result_type _Max = _Engine::max();
55
#endif
56
57
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
58
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
59
60
// constructors and seeding functions
61
_LIBCPP_HIDE_FROM_ABI discard_block_engine() : __n_(0) {}
62
_LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(const _Engine& __e) : __e_(__e), __n_(0) {}
63
#ifndef _LIBCPP_CXX03_LANG
64
_LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(_Engine&& __e) : __e_(std::move(__e)), __n_(0) {}
65
#endif // _LIBCPP_CXX03_LANG
66
_LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
67
template <
68
class _Sseq,
69
__enable_if_t<__is_seed_sequence<_Sseq, discard_block_engine>::value && !is_convertible<_Sseq, _Engine>::value,
70
int> = 0>
71
_LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(_Sseq& __q) : __e_(__q), __n_(0) {}
72
_LIBCPP_HIDE_FROM_ABI void seed() {
73
__e_.seed();
74
__n_ = 0;
75
}
76
_LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) {
77
__e_.seed(__sd);
78
__n_ = 0;
79
}
80
template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, discard_block_engine>::value, int> = 0>
81
_LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
82
__e_.seed(__q);
83
__n_ = 0;
84
}
85
86
// generating functions
87
_LIBCPP_HIDE_FROM_ABI result_type operator()();
88
_LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
89
for (; __z; --__z)
90
operator()();
91
}
92
93
// property functions
94
_LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }
95
96
template <class _Eng, size_t _Pp, size_t _Rp>
97
friend bool
98
operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y);
99
100
template <class _Eng, size_t _Pp, size_t _Rp>
101
friend bool
102
operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y);
103
104
template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
105
friend basic_ostream<_CharT, _Traits>&
106
operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x);
107
108
template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
109
friend basic_istream<_CharT, _Traits>&
110
operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x);
111
};
112
113
template <class _Engine, size_t __p, size_t __r>
114
_LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
115
116
template <class _Engine, size_t __p, size_t __r>
117
_LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
118
119
template <class _Engine, size_t __p, size_t __r>
120
typename discard_block_engine<_Engine, __p, __r>::result_type discard_block_engine<_Engine, __p, __r>::operator()() {
121
if (__n_ >= static_cast<int>(__r)) {
122
__e_.discard(__p - __r);
123
__n_ = 0;
124
}
125
++__n_;
126
return __e_();
127
}
128
129
template <class _Eng, size_t _Pp, size_t _Rp>
130
inline _LIBCPP_HIDE_FROM_ABI bool
131
operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) {
132
return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
133
}
134
135
template <class _Eng, size_t _Pp, size_t _Rp>
136
inline _LIBCPP_HIDE_FROM_ABI bool
137
operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) {
138
return !(__x == __y);
139
}
140
141
template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
142
_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
143
operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x) {
144
__save_flags<_CharT, _Traits> __lx(__os);
145
typedef basic_ostream<_CharT, _Traits> _Ostream;
146
__os.flags(_Ostream::dec | _Ostream::left);
147
_CharT __sp = __os.widen(' ');
148
__os.fill(__sp);
149
return __os << __x.__e_ << __sp << __x.__n_;
150
}
151
152
template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
153
_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
154
operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x) {
155
__save_flags<_CharT, _Traits> __lx(__is);
156
typedef basic_istream<_CharT, _Traits> _Istream;
157
__is.flags(_Istream::dec | _Istream::skipws);
158
_Eng __e;
159
int __n;
160
__is >> __e >> __n;
161
if (!__is.fail()) {
162
__x.__e_ = __e;
163
__x.__n_ = __n;
164
}
165
return __is;
166
}
167
168
_LIBCPP_END_NAMESPACE_STD
169
170
_LIBCPP_POP_MACROS
171
172
#endif // _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
173
174