File indexing completed on 2026-05-03 08:13:59
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
0010 #define _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
0011
0012 #include <__config>
0013 #include <__cstddef/size_t.h>
0014 #include <__random/is_seed_sequence.h>
0015 #include <__type_traits/enable_if.h>
0016 #include <__type_traits/is_convertible.h>
0017 #include <__utility/move.h>
0018 #include <iosfwd>
0019 #include <limits>
0020
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 # pragma GCC system_header
0023 #endif
0024
0025 _LIBCPP_PUSH_MACROS
0026 #include <__undef_macros>
0027
0028 _LIBCPP_BEGIN_NAMESPACE_STD
0029
0030 template <class _Engine, size_t __p, size_t __r>
0031 class _LIBCPP_TEMPLATE_VIS discard_block_engine {
0032 _Engine __e_;
0033 int __n_;
0034
0035 static_assert(0 < __r, "discard_block_engine invalid parameters");
0036 static_assert(__r <= __p, "discard_block_engine invalid parameters");
0037 #ifndef _LIBCPP_CXX03_LANG
0038 static_assert(__r <= numeric_limits<int>::max(), "discard_block_engine invalid parameters");
0039 #endif
0040
0041 public:
0042
0043 typedef typename _Engine::result_type result_type;
0044
0045
0046 static inline _LIBCPP_CONSTEXPR const size_t block_size = __p;
0047 static inline _LIBCPP_CONSTEXPR const size_t used_block = __r;
0048
0049 #ifdef _LIBCPP_CXX03_LANG
0050 static const result_type _Min = _Engine::_Min;
0051 static const result_type _Max = _Engine::_Max;
0052 #else
0053 static constexpr result_type _Min = _Engine::min();
0054 static constexpr result_type _Max = _Engine::max();
0055 #endif
0056
0057 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
0058 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
0059
0060
0061 _LIBCPP_HIDE_FROM_ABI discard_block_engine() : __n_(0) {}
0062 _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(const _Engine& __e) : __e_(__e), __n_(0) {}
0063 #ifndef _LIBCPP_CXX03_LANG
0064 _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(_Engine&& __e) : __e_(std::move(__e)), __n_(0) {}
0065 #endif
0066 _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
0067 template <
0068 class _Sseq,
0069 __enable_if_t<__is_seed_sequence<_Sseq, discard_block_engine>::value && !is_convertible<_Sseq, _Engine>::value,
0070 int> = 0>
0071 _LIBCPP_HIDE_FROM_ABI explicit discard_block_engine(_Sseq& __q) : __e_(__q), __n_(0) {}
0072 _LIBCPP_HIDE_FROM_ABI void seed() {
0073 __e_.seed();
0074 __n_ = 0;
0075 }
0076 _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) {
0077 __e_.seed(__sd);
0078 __n_ = 0;
0079 }
0080 template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, discard_block_engine>::value, int> = 0>
0081 _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
0082 __e_.seed(__q);
0083 __n_ = 0;
0084 }
0085
0086
0087 _LIBCPP_HIDE_FROM_ABI result_type operator()();
0088 _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
0089 for (; __z; --__z)
0090 operator()();
0091 }
0092
0093
0094 _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }
0095
0096 template <class _Eng, size_t _Pp, size_t _Rp>
0097 friend bool
0098 operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y);
0099
0100 template <class _Eng, size_t _Pp, size_t _Rp>
0101 friend bool
0102 operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y);
0103
0104 template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
0105 friend basic_ostream<_CharT, _Traits>&
0106 operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x);
0107
0108 template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
0109 friend basic_istream<_CharT, _Traits>&
0110 operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x);
0111 };
0112
0113 template <class _Engine, size_t __p, size_t __r>
0114 typename discard_block_engine<_Engine, __p, __r>::result_type discard_block_engine<_Engine, __p, __r>::operator()() {
0115 if (__n_ >= static_cast<int>(__r)) {
0116 __e_.discard(__p - __r);
0117 __n_ = 0;
0118 }
0119 ++__n_;
0120 return __e_();
0121 }
0122
0123 template <class _Eng, size_t _Pp, size_t _Rp>
0124 inline _LIBCPP_HIDE_FROM_ABI bool
0125 operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) {
0126 return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
0127 }
0128
0129 template <class _Eng, size_t _Pp, size_t _Rp>
0130 inline _LIBCPP_HIDE_FROM_ABI bool
0131 operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) {
0132 return !(__x == __y);
0133 }
0134
0135 template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
0136 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0137 operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x) {
0138 __save_flags<_CharT, _Traits> __lx(__os);
0139 typedef basic_ostream<_CharT, _Traits> _Ostream;
0140 __os.flags(_Ostream::dec | _Ostream::left);
0141 _CharT __sp = __os.widen(' ');
0142 __os.fill(__sp);
0143 return __os << __x.__e_ << __sp << __x.__n_;
0144 }
0145
0146 template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
0147 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0148 operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x) {
0149 __save_flags<_CharT, _Traits> __lx(__is);
0150 typedef basic_istream<_CharT, _Traits> _Istream;
0151 __is.flags(_Istream::dec | _Istream::skipws);
0152 _Eng __e;
0153 int __n;
0154 __is >> __e >> __n;
0155 if (!__is.fail()) {
0156 __x.__e_ = __e;
0157 __x.__n_ = __n;
0158 }
0159 return __is;
0160 }
0161
0162 _LIBCPP_END_NAMESPACE_STD
0163
0164 _LIBCPP_POP_MACROS
0165
0166 #endif