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 { 33 _Engine __e_; 34 int __n_; 35 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"); 40 #endif 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 _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min(); 54 static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max(); 55 #endif 56 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(); } 61 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> 76 _LIBCPP_INLINE_VISIBILITY 77 explicit discard_block_engine(_Sseq& __q, 78 typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value && 79 !is_convertible<_Sseq, _Engine>::value>::type* = 0) 80 : __e_(__q), __n_(0) {} 81 _LIBCPP_INLINE_VISIBILITY 82 void seed() {__e_.seed(); __n_ = 0;} 83 _LIBCPP_INLINE_VISIBILITY 84 void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;} 85 template<class _Sseq> 86 _LIBCPP_INLINE_VISIBILITY 87 typename enable_if 88 < 89 __is_seed_sequence<_Sseq, discard_block_engine>::value, 90 void 91 >::type 92 seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;} 93 94 // generating functions 95 _LIBCPP_HIDE_FROM_ABI result_type operator()(); 96 _LIBCPP_INLINE_VISIBILITY 97 void discard(unsigned long long __z) {for (; __z; --__z) operator()();} 98 99 // property functions 100 _LIBCPP_INLINE_VISIBILITY 101 const _Engine& base() const _NOEXCEPT {return __e_;} 102 103 template<class _Eng, size_t _Pp, size_t _Rp> 104 friend 105 bool 106 operator==( 107 const discard_block_engine<_Eng, _Pp, _Rp>& __x, 108 const discard_block_engine<_Eng, _Pp, _Rp>& __y); 109 110 template<class _Eng, size_t _Pp, size_t _Rp> 111 friend 112 bool 113 operator!=( 114 const discard_block_engine<_Eng, _Pp, _Rp>& __x, 115 const discard_block_engine<_Eng, _Pp, _Rp>& __y); 116 117 template <class _CharT, class _Traits, 118 class _Eng, size_t _Pp, size_t _Rp> 119 friend 120 basic_ostream<_CharT, _Traits>& 121 operator<<(basic_ostream<_CharT, _Traits>& __os, 122 const discard_block_engine<_Eng, _Pp, _Rp>& __x); 123 124 template <class _CharT, class _Traits, 125 class _Eng, size_t _Pp, size_t _Rp> 126 friend 127 basic_istream<_CharT, _Traits>& 128 operator>>(basic_istream<_CharT, _Traits>& __is, 129 discard_block_engine<_Eng, _Pp, _Rp>& __x); 130 }; 131 132 template<class _Engine, size_t __p, size_t __r> 133 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size; 134 135 template<class _Engine, size_t __p, size_t __r> 136 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block; 137 138 template<class _Engine, size_t __p, size_t __r> 139 typename discard_block_engine<_Engine, __p, __r>::result_type 140 discard_block_engine<_Engine, __p, __r>::operator()() 141 { 142 if (__n_ >= static_cast<int>(__r)) 143 { 144 __e_.discard(__p - __r); 145 __n_ = 0; 146 } 147 ++__n_; 148 return __e_(); 149 } 150 151 template<class _Eng, size_t _Pp, size_t _Rp> 152 inline _LIBCPP_INLINE_VISIBILITY 153 bool 154 operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, 155 const discard_block_engine<_Eng, _Pp, _Rp>& __y) 156 { 157 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_; 158 } 159 160 template<class _Eng, size_t _Pp, size_t _Rp> 161 inline _LIBCPP_INLINE_VISIBILITY 162 bool 163 operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, 164 const discard_block_engine<_Eng, _Pp, _Rp>& __y) 165 { 166 return !(__x == __y); 167 } 168 169 template <class _CharT, class _Traits, 170 class _Eng, size_t _Pp, size_t _Rp> 171 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 172 operator<<(basic_ostream<_CharT, _Traits>& __os, 173 const discard_block_engine<_Eng, _Pp, _Rp>& __x) 174 { 175 __save_flags<_CharT, _Traits> __lx(__os); 176 typedef basic_ostream<_CharT, _Traits> _Ostream; 177 __os.flags(_Ostream::dec | _Ostream::left); 178 _CharT __sp = __os.widen(' '); 179 __os.fill(__sp); 180 return __os << __x.__e_ << __sp << __x.__n_; 181 } 182 183 template <class _CharT, class _Traits, 184 class _Eng, size_t _Pp, size_t _Rp> 185 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& 186 operator>>(basic_istream<_CharT, _Traits>& __is, 187 discard_block_engine<_Eng, _Pp, _Rp>& __x) 188 { 189 __save_flags<_CharT, _Traits> __lx(__is); 190 typedef basic_istream<_CharT, _Traits> _Istream; 191 __is.flags(_Istream::dec | _Istream::skipws); 192 _Eng __e; 193 int __n; 194 __is >> __e >> __n; 195 if (!__is.fail()) 196 { 197 __x.__e_ = __e; 198 __x.__n_ = __n; 199 } 200 return __is; 201 } 202 203 _LIBCPP_END_NAMESPACE_STD 204 205 _LIBCPP_POP_MACROS 206 207 #endif // _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H 208