Warning: This file is not a C or C++ file. It does not have highlighting.
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___CXX03___RANDOM_DISCARD_BLOCK_ENGINE_H |
10 | #define _LIBCPP___CXX03___RANDOM_DISCARD_BLOCK_ENGINE_H |
11 | |
12 | #include <__cxx03/__config> |
13 | #include <__cxx03/__random/is_seed_sequence.h> |
14 | #include <__cxx03/__type_traits/enable_if.h> |
15 | #include <__cxx03/__type_traits/is_convertible.h> |
16 | #include <__cxx03/__utility/move.h> |
17 | #include <__cxx03/cstddef> |
18 | #include <__cxx03/iosfwd> |
19 | #include <__cxx03/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 <__cxx03/__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 | |
38 | public: |
39 | // types |
40 | typedef typename _Engine::result_type result_type; |
41 | |
42 | // engine characteristics |
43 | static const size_t block_size = __p; |
44 | static const size_t used_block = __r; |
45 | |
46 | static const result_type _Min = _Engine::_Min; |
47 | static const result_type _Max = _Engine::_Max; |
48 | |
49 | _LIBCPP_HIDE_FROM_ABI static result_type min() { return _Engine::min(); } |
50 | _LIBCPP_HIDE_FROM_ABI static result_type max() { return _Engine::max(); } |
51 | |
52 | // constructors and seeding functions |
53 | _LIBCPP_HIDE_FROM_ABI discard_block_engine() : __n_(0) {} |
54 | _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(const _Engine& __e) : __e_(__e), __n_(0) {} |
55 | _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {} |
56 | template < |
57 | class _Sseq, |
58 | __enable_if_t<__is_seed_sequence<_Sseq, discard_block_engine>::value && !is_convertible<_Sseq, _Engine>::value, |
59 | int> = 0> |
60 | _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(_Sseq& __q) : __e_(__q), __n_(0) {} |
61 | _LIBCPP_HIDE_FROM_ABI void seed() { |
62 | __e_.seed(); |
63 | __n_ = 0; |
64 | } |
65 | _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) { |
66 | __e_.seed(__sd); |
67 | __n_ = 0; |
68 | } |
69 | template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, discard_block_engine>::value, int> = 0> |
70 | _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) { |
71 | __e_.seed(__q); |
72 | __n_ = 0; |
73 | } |
74 | |
75 | // generating functions |
76 | _LIBCPP_HIDE_FROM_ABI result_type operator()(); |
77 | _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) { |
78 | for (; __z; --__z) |
79 | operator()(); |
80 | } |
81 | |
82 | // property functions |
83 | _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; } |
84 | |
85 | template <class _Eng, size_t _Pp, size_t _Rp> |
86 | friend bool |
87 | operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
88 | |
89 | template <class _Eng, size_t _Pp, size_t _Rp> |
90 | friend bool |
91 | operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y); |
92 | |
93 | template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp> |
94 | friend basic_ostream<_CharT, _Traits>& |
95 | operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x); |
96 | |
97 | template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp> |
98 | friend basic_istream<_CharT, _Traits>& |
99 | operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x); |
100 | }; |
101 | |
102 | template <class _Engine, size_t __p, size_t __r> |
103 | const size_t discard_block_engine<_Engine, __p, __r>::block_size; |
104 | |
105 | template <class _Engine, size_t __p, size_t __r> |
106 | const size_t discard_block_engine<_Engine, __p, __r>::used_block; |
107 | |
108 | template <class _Engine, size_t __p, size_t __r> |
109 | typename discard_block_engine<_Engine, __p, __r>::result_type discard_block_engine<_Engine, __p, __r>::operator()() { |
110 | if (__n_ >= static_cast<int>(__r)) { |
111 | __e_.discard(__p - __r); |
112 | __n_ = 0; |
113 | } |
114 | ++__n_; |
115 | return __e_(); |
116 | } |
117 | |
118 | template <class _Eng, size_t _Pp, size_t _Rp> |
119 | inline _LIBCPP_HIDE_FROM_ABI bool |
120 | operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) { |
121 | return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; |
122 | } |
123 | |
124 | template <class _Eng, size_t _Pp, size_t _Rp> |
125 | inline _LIBCPP_HIDE_FROM_ABI bool |
126 | operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) { |
127 | return !(__x == __y); |
128 | } |
129 | |
130 | template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp> |
131 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
132 | operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x) { |
133 | __save_flags<_CharT, _Traits> __lx(__os); |
134 | typedef basic_ostream<_CharT, _Traits> _Ostream; |
135 | __os.flags(_Ostream::dec | _Ostream::left); |
136 | _CharT __sp = __os.widen(' '); |
137 | __os.fill(__sp); |
138 | return __os << __x.__e_ << __sp << __x.__n_; |
139 | } |
140 | |
141 | template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp> |
142 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
143 | operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x) { |
144 | __save_flags<_CharT, _Traits> __lx(__is); |
145 | typedef basic_istream<_CharT, _Traits> _Istream; |
146 | __is.flags(_Istream::dec | _Istream::skipws); |
147 | _Eng __e; |
148 | int __n; |
149 | __is >> __e >> __n; |
150 | if (!__is.fail()) { |
151 | __x.__e_ = __e; |
152 | __x.__n_ = __n; |
153 | } |
154 | return __is; |
155 | } |
156 | |
157 | _LIBCPP_END_NAMESPACE_STD |
158 | |
159 | _LIBCPP_POP_MACROS |
160 | |
161 | #endif // _LIBCPP___CXX03___RANDOM_DISCARD_BLOCK_ENGINE_H |
162 |
Warning: This file is not a C or C++ file. It does not have highlighting.