Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:38

0001 //===----------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___RANDOM_DISCARD_BLOCK_ENGINE_H
0010 #define _LIBCPP___CXX03___RANDOM_DISCARD_BLOCK_ENGINE_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__random/is_seed_sequence.h>
0014 #include <__cxx03/__type_traits/enable_if.h>
0015 #include <__cxx03/__type_traits/is_convertible.h>
0016 #include <__cxx03/__utility/move.h>
0017 #include <__cxx03/cstddef>
0018 #include <__cxx03/iosfwd>
0019 #include <__cxx03/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 <__cxx03/__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 // numeric_limits::max() is not constexpr in C++03
0038   static_assert(__r <= numeric_limits<int>::max(), "discard_block_engine invalid parameters");
0039 #endif
0040 
0041 public:
0042   // types
0043   typedef typename _Engine::result_type result_type;
0044 
0045   // engine characteristics
0046   static _LIBCPP_CONSTEXPR const size_t block_size = __p;
0047   static _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   // constructors and seeding functions
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 // _LIBCPP_CXX03_LANG
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   // generating functions
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   // property functions
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 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
0115 
0116 template <class _Engine, size_t __p, size_t __r>
0117 _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
0118 
0119 template <class _Engine, size_t __p, size_t __r>
0120 typename discard_block_engine<_Engine, __p, __r>::result_type discard_block_engine<_Engine, __p, __r>::operator()() {
0121   if (__n_ >= static_cast<int>(__r)) {
0122     __e_.discard(__p - __r);
0123     __n_ = 0;
0124   }
0125   ++__n_;
0126   return __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.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
0133 }
0134 
0135 template <class _Eng, size_t _Pp, size_t _Rp>
0136 inline _LIBCPP_HIDE_FROM_ABI bool
0137 operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x, const discard_block_engine<_Eng, _Pp, _Rp>& __y) {
0138   return !(__x == __y);
0139 }
0140 
0141 template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
0142 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
0143 operator<<(basic_ostream<_CharT, _Traits>& __os, const discard_block_engine<_Eng, _Pp, _Rp>& __x) {
0144   __save_flags<_CharT, _Traits> __lx(__os);
0145   typedef basic_ostream<_CharT, _Traits> _Ostream;
0146   __os.flags(_Ostream::dec | _Ostream::left);
0147   _CharT __sp = __os.widen(' ');
0148   __os.fill(__sp);
0149   return __os << __x.__e_ << __sp << __x.__n_;
0150 }
0151 
0152 template <class _CharT, class _Traits, class _Eng, size_t _Pp, size_t _Rp>
0153 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
0154 operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x) {
0155   __save_flags<_CharT, _Traits> __lx(__is);
0156   typedef basic_istream<_CharT, _Traits> _Istream;
0157   __is.flags(_Istream::dec | _Istream::skipws);
0158   _Eng __e;
0159   int __n;
0160   __is >> __e >> __n;
0161   if (!__is.fail()) {
0162     __x.__e_ = __e;
0163     __x.__n_ = __n;
0164   }
0165   return __is;
0166 }
0167 
0168 _LIBCPP_END_NAMESPACE_STD
0169 
0170 _LIBCPP_POP_MACROS
0171 
0172 #endif // _LIBCPP___CXX03___RANDOM_DISCARD_BLOCK_ENGINE_H