1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
10 #define _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
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>
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
26 #include <__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 template<class _Engine
, size_t __p
, size_t __r
>
31 class _LIBCPP_TEMPLATE_VIS discard_block_engine
36 static_assert( 0 < __r
, "discard_block_engine invalid parameters");
37 static_assert(__r
<= __p
, "discard_block_engine invalid parameters");
38 #ifndef _LIBCPP_CXX03_LANG // numeric_limits::max() is not constexpr in C++03
39 static_assert(__r
<= numeric_limits
<int>::max(), "discard_block_engine invalid parameters");
43 typedef typename
_Engine::result_type result_type
;
45 // engine characteristics
46 static _LIBCPP_CONSTEXPR
const size_t block_size
= __p
;
47 static _LIBCPP_CONSTEXPR
const size_t used_block
= __r
;
49 #ifdef _LIBCPP_CXX03_LANG
50 static const result_type _Min
= _Engine::_Min
;
51 static const result_type _Max
= _Engine::_Max
;
53 static _LIBCPP_CONSTEXPR
const result_type _Min
= _Engine::min();
54 static _LIBCPP_CONSTEXPR
const result_type _Max
= _Engine::max();
57 _LIBCPP_INLINE_VISIBILITY
58 static _LIBCPP_CONSTEXPR result_type
min() { return _Engine::min(); }
59 _LIBCPP_INLINE_VISIBILITY
60 static _LIBCPP_CONSTEXPR result_type
max() { return _Engine::max(); }
62 // constructors and seeding functions
63 _LIBCPP_INLINE_VISIBILITY
64 discard_block_engine() : __n_(0) {}
65 _LIBCPP_INLINE_VISIBILITY
66 explicit discard_block_engine(const _Engine
& __e
)
67 : __e_(__e
), __n_(0) {}
68 #ifndef _LIBCPP_CXX03_LANG
69 _LIBCPP_INLINE_VISIBILITY
70 explicit discard_block_engine(_Engine
&& __e
)
71 : __e_(_VSTD::move(__e
)), __n_(0) {}
72 #endif // _LIBCPP_CXX03_LANG
73 _LIBCPP_INLINE_VISIBILITY
74 explicit discard_block_engine(result_type __sd
) : __e_(__sd
), __n_(0) {}
75 template<class _Sseq
, __enable_if_t
<__is_seed_sequence
<_Sseq
, discard_block_engine
>::value
&&
76 !is_convertible
<_Sseq
, _Engine
>::value
, int> = 0>
77 _LIBCPP_INLINE_VISIBILITY
78 explicit discard_block_engine(_Sseq
& __q
)
79 : __e_(__q
), __n_(0) {}
80 _LIBCPP_INLINE_VISIBILITY
81 void seed() {__e_
.seed(); __n_
= 0;}
82 _LIBCPP_INLINE_VISIBILITY
83 void seed(result_type __sd
) {__e_
.seed(__sd
); __n_
= 0;}
84 template<class _Sseq
, __enable_if_t
<__is_seed_sequence
<_Sseq
, discard_block_engine
>::value
, int> = 0>
85 _LIBCPP_INLINE_VISIBILITY
87 seed(_Sseq
& __q
) {__e_
.seed(__q
); __n_
= 0;}
89 // generating functions
90 _LIBCPP_HIDE_FROM_ABI result_type
operator()();
91 _LIBCPP_INLINE_VISIBILITY
92 void discard(unsigned long long __z
) {for (; __z
; --__z
) operator()();}
95 _LIBCPP_INLINE_VISIBILITY
96 const _Engine
& base() const _NOEXCEPT
{return __e_
;}
98 template<class _Eng
, size_t _Pp
, size_t _Rp
>
102 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
,
103 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __y
);
105 template<class _Eng
, size_t _Pp
, size_t _Rp
>
109 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
,
110 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __y
);
112 template <class _CharT
, class _Traits
,
113 class _Eng
, size_t _Pp
, size_t _Rp
>
115 basic_ostream
<_CharT
, _Traits
>&
116 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
117 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
);
119 template <class _CharT
, class _Traits
,
120 class _Eng
, size_t _Pp
, size_t _Rp
>
122 basic_istream
<_CharT
, _Traits
>&
123 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
124 discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
);
127 template<class _Engine
, size_t __p
, size_t __r
>
128 _LIBCPP_CONSTEXPR
const size_t discard_block_engine
<_Engine
, __p
, __r
>::block_size
;
130 template<class _Engine
, size_t __p
, size_t __r
>
131 _LIBCPP_CONSTEXPR
const size_t discard_block_engine
<_Engine
, __p
, __r
>::used_block
;
133 template<class _Engine
, size_t __p
, size_t __r
>
134 typename discard_block_engine
<_Engine
, __p
, __r
>::result_type
135 discard_block_engine
<_Engine
, __p
, __r
>::operator()()
137 if (__n_
>= static_cast<int>(__r
))
139 __e_
.discard(__p
- __r
);
146 template<class _Eng
, size_t _Pp
, size_t _Rp
>
147 inline _LIBCPP_INLINE_VISIBILITY
149 operator==(const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
,
150 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __y
)
152 return __x
.__n_
== __y
.__n_
&& __x
.__e_
== __y
.__e_
;
155 template<class _Eng
, size_t _Pp
, size_t _Rp
>
156 inline _LIBCPP_INLINE_VISIBILITY
158 operator!=(const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
,
159 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __y
)
161 return !(__x
== __y
);
164 template <class _CharT
, class _Traits
,
165 class _Eng
, size_t _Pp
, size_t _Rp
>
166 _LIBCPP_HIDE_FROM_ABI basic_ostream
<_CharT
, _Traits
>&
167 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
168 const discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
)
170 __save_flags
<_CharT
, _Traits
> __lx(__os
);
171 typedef basic_ostream
<_CharT
, _Traits
> _Ostream
;
172 __os
.flags(_Ostream::dec
| _Ostream::left
);
173 _CharT __sp
= __os
.widen(' ');
175 return __os
<< __x
.__e_
<< __sp
<< __x
.__n_
;
178 template <class _CharT
, class _Traits
,
179 class _Eng
, size_t _Pp
, size_t _Rp
>
180 _LIBCPP_HIDE_FROM_ABI basic_istream
<_CharT
, _Traits
>&
181 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
182 discard_block_engine
<_Eng
, _Pp
, _Rp
>& __x
)
184 __save_flags
<_CharT
, _Traits
> __lx(__is
);
185 typedef basic_istream
<_CharT
, _Traits
> _Istream
;
186 __is
.flags(_Istream::dec
| _Istream::skipws
);
198 _LIBCPP_END_NAMESPACE_STD
202 #endif // _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H