discard_block_engine.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #ifndef _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
  9. #define _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H
  10. #include <__config>
  11. #include <__random/is_seed_sequence.h>
  12. #include <__utility/move.h>
  13. #include <climits>
  14. #include <iosfwd>
  15. #include <type_traits>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. _LIBCPP_PUSH_MACROS
  20. #include <__undef_macros>
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. template<class _Engine, size_t __p, size_t __r>
  23. class _LIBCPP_TEMPLATE_VIS discard_block_engine
  24. {
  25. _Engine __e_;
  26. int __n_;
  27. static_assert( 0 < __r, "discard_block_engine invalid parameters");
  28. static_assert(__r <= __p, "discard_block_engine invalid parameters");
  29. static_assert(__r <= INT_MAX, "discard_block_engine invalid parameters");
  30. public:
  31. // types
  32. typedef typename _Engine::result_type result_type;
  33. // engine characteristics
  34. static _LIBCPP_CONSTEXPR const size_t block_size = __p;
  35. static _LIBCPP_CONSTEXPR const size_t used_block = __r;
  36. #ifdef _LIBCPP_CXX03_LANG
  37. static const result_type _Min = _Engine::_Min;
  38. static const result_type _Max = _Engine::_Max;
  39. #else
  40. static _LIBCPP_CONSTEXPR const result_type _Min = _Engine::min();
  41. static _LIBCPP_CONSTEXPR const result_type _Max = _Engine::max();
  42. #endif
  43. _LIBCPP_INLINE_VISIBILITY
  44. static _LIBCPP_CONSTEXPR result_type min() { return _Engine::min(); }
  45. _LIBCPP_INLINE_VISIBILITY
  46. static _LIBCPP_CONSTEXPR result_type max() { return _Engine::max(); }
  47. // constructors and seeding functions
  48. _LIBCPP_INLINE_VISIBILITY
  49. discard_block_engine() : __n_(0) {}
  50. _LIBCPP_INLINE_VISIBILITY
  51. explicit discard_block_engine(const _Engine& __e)
  52. : __e_(__e), __n_(0) {}
  53. #ifndef _LIBCPP_CXX03_LANG
  54. _LIBCPP_INLINE_VISIBILITY
  55. explicit discard_block_engine(_Engine&& __e)
  56. : __e_(_VSTD::move(__e)), __n_(0) {}
  57. #endif // _LIBCPP_CXX03_LANG
  58. _LIBCPP_INLINE_VISIBILITY
  59. explicit discard_block_engine(result_type __sd) : __e_(__sd), __n_(0) {}
  60. template<class _Sseq>
  61. _LIBCPP_INLINE_VISIBILITY
  62. explicit discard_block_engine(_Sseq& __q,
  63. typename enable_if<__is_seed_sequence<_Sseq, discard_block_engine>::value &&
  64. !is_convertible<_Sseq, _Engine>::value>::type* = 0)
  65. : __e_(__q), __n_(0) {}
  66. _LIBCPP_INLINE_VISIBILITY
  67. void seed() {__e_.seed(); __n_ = 0;}
  68. _LIBCPP_INLINE_VISIBILITY
  69. void seed(result_type __sd) {__e_.seed(__sd); __n_ = 0;}
  70. template<class _Sseq>
  71. _LIBCPP_INLINE_VISIBILITY
  72. typename enable_if
  73. <
  74. __is_seed_sequence<_Sseq, discard_block_engine>::value,
  75. void
  76. >::type
  77. seed(_Sseq& __q) {__e_.seed(__q); __n_ = 0;}
  78. // generating functions
  79. result_type operator()();
  80. _LIBCPP_INLINE_VISIBILITY
  81. void discard(unsigned long long __z) {for (; __z; --__z) operator()();}
  82. // property functions
  83. _LIBCPP_INLINE_VISIBILITY
  84. const _Engine& base() const _NOEXCEPT {return __e_;}
  85. template<class _Eng, size_t _Pp, size_t _Rp>
  86. friend
  87. bool
  88. operator==(
  89. const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  90. const discard_block_engine<_Eng, _Pp, _Rp>& __y);
  91. template<class _Eng, size_t _Pp, size_t _Rp>
  92. friend
  93. bool
  94. operator!=(
  95. const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  96. const discard_block_engine<_Eng, _Pp, _Rp>& __y);
  97. template <class _CharT, class _Traits,
  98. class _Eng, size_t _Pp, size_t _Rp>
  99. friend
  100. basic_ostream<_CharT, _Traits>&
  101. operator<<(basic_ostream<_CharT, _Traits>& __os,
  102. const discard_block_engine<_Eng, _Pp, _Rp>& __x);
  103. template <class _CharT, class _Traits,
  104. class _Eng, size_t _Pp, size_t _Rp>
  105. friend
  106. basic_istream<_CharT, _Traits>&
  107. operator>>(basic_istream<_CharT, _Traits>& __is,
  108. discard_block_engine<_Eng, _Pp, _Rp>& __x);
  109. };
  110. template<class _Engine, size_t __p, size_t __r>
  111. _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
  112. template<class _Engine, size_t __p, size_t __r>
  113. _LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
  114. template<class _Engine, size_t __p, size_t __r>
  115. typename discard_block_engine<_Engine, __p, __r>::result_type
  116. discard_block_engine<_Engine, __p, __r>::operator()()
  117. {
  118. if (__n_ >= static_cast<int>(__r))
  119. {
  120. __e_.discard(__p - __r);
  121. __n_ = 0;
  122. }
  123. ++__n_;
  124. return __e_();
  125. }
  126. template<class _Eng, size_t _Pp, size_t _Rp>
  127. inline _LIBCPP_INLINE_VISIBILITY
  128. bool
  129. operator==(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  130. const discard_block_engine<_Eng, _Pp, _Rp>& __y)
  131. {
  132. return __x.__n_ == __y.__n_ && __x.__e_ == __y.__e_;
  133. }
  134. template<class _Eng, size_t _Pp, size_t _Rp>
  135. inline _LIBCPP_INLINE_VISIBILITY
  136. bool
  137. operator!=(const discard_block_engine<_Eng, _Pp, _Rp>& __x,
  138. const discard_block_engine<_Eng, _Pp, _Rp>& __y)
  139. {
  140. return !(__x == __y);
  141. }
  142. template <class _CharT, class _Traits,
  143. class _Eng, size_t _Pp, size_t _Rp>
  144. basic_ostream<_CharT, _Traits>&
  145. operator<<(basic_ostream<_CharT, _Traits>& __os,
  146. const discard_block_engine<_Eng, _Pp, _Rp>& __x)
  147. {
  148. __save_flags<_CharT, _Traits> __lx(__os);
  149. typedef basic_ostream<_CharT, _Traits> _Ostream;
  150. __os.flags(_Ostream::dec | _Ostream::left);
  151. _CharT __sp = __os.widen(' ');
  152. __os.fill(__sp);
  153. return __os << __x.__e_ << __sp << __x.__n_;
  154. }
  155. template <class _CharT, class _Traits,
  156. class _Eng, size_t _Pp, size_t _Rp>
  157. basic_istream<_CharT, _Traits>&
  158. operator>>(basic_istream<_CharT, _Traits>& __is,
  159. discard_block_engine<_Eng, _Pp, _Rp>& __x)
  160. {
  161. __save_flags<_CharT, _Traits> __lx(__is);
  162. typedef basic_istream<_CharT, _Traits> _Istream;
  163. __is.flags(_Istream::dec | _Istream::skipws);
  164. _Eng __e;
  165. int __n;
  166. __is >> __e >> __n;
  167. if (!__is.fail())
  168. {
  169. __x.__e_ = __e;
  170. __x.__n_ = __n;
  171. }
  172. return __is;
  173. }
  174. _LIBCPP_END_NAMESPACE_STD
  175. _LIBCPP_POP_MACROS
  176. #endif // _LIBCPP___RANDOM_DISCARD_BLOCK_ENGINE_H