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