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