independent_bits_engine.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_INDEPENDENT_BITS_ENGINE_H
  9. #define _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H
  10. #include <__config>
  11. #include <__fwd/istream.h>
  12. #include <__fwd/ostream.h>
  13. #include <__random/is_seed_sequence.h>
  14. #include <__random/log2.h>
  15. #include <__type_traits/conditional.h>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_convertible.h>
  18. #include <__utility/move.h>
  19. #include <cstddef>
  20. #include <limits>
  21. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  22. # pragma GCC system_header
  23. #endif
  24. _LIBCPP_PUSH_MACROS
  25. #include <__undef_macros>
  26. _LIBCPP_BEGIN_NAMESPACE_STD
  27. template <class _Engine, size_t __w, class _UIntType>
  28. class _LIBCPP_TEMPLATE_VIS independent_bits_engine {
  29. template <class _UInt, _UInt _R0, size_t _Wp, size_t _Mp>
  30. class __get_n {
  31. static _LIBCPP_CONSTEXPR const size_t _Dt = numeric_limits<_UInt>::digits;
  32. static _LIBCPP_CONSTEXPR const size_t _Np = _Wp / _Mp + (_Wp % _Mp != 0);
  33. static _LIBCPP_CONSTEXPR const size_t _W0 = _Wp / _Np;
  34. static _LIBCPP_CONSTEXPR const _UInt _Y0 = _W0 >= _Dt ? 0 : (_R0 >> _W0) << _W0;
  35. public:
  36. static _LIBCPP_CONSTEXPR const size_t value = _R0 - _Y0 > _Y0 / _Np ? _Np + 1 : _Np;
  37. };
  38. public:
  39. // types
  40. typedef _UIntType result_type;
  41. private:
  42. _Engine __e_;
  43. static _LIBCPP_CONSTEXPR const result_type _Dt = numeric_limits<result_type>::digits;
  44. static_assert(0 < __w, "independent_bits_engine invalid parameters");
  45. static_assert(__w <= _Dt, "independent_bits_engine invalid parameters");
  46. typedef typename _Engine::result_type _Engine_result_type;
  47. typedef __conditional_t<sizeof(_Engine_result_type) <= sizeof(result_type), result_type, _Engine_result_type>
  48. _Working_result_type;
  49. #ifdef _LIBCPP_CXX03_LANG
  50. static const _Working_result_type _Rp = _Engine::_Max - _Engine::_Min + _Working_result_type(1);
  51. #else
  52. static _LIBCPP_CONSTEXPR const _Working_result_type _Rp = _Engine::max() - _Engine::min() + _Working_result_type(1);
  53. #endif
  54. static _LIBCPP_CONSTEXPR const size_t __m = __log2<_Working_result_type, _Rp>::value;
  55. static _LIBCPP_CONSTEXPR const size_t __n = __get_n<_Working_result_type, _Rp, __w, __m>::value;
  56. static _LIBCPP_CONSTEXPR const size_t __w0 = __w / __n;
  57. static _LIBCPP_CONSTEXPR const size_t __n0 = __n - __w % __n;
  58. static _LIBCPP_CONSTEXPR const size_t _WDt = numeric_limits<_Working_result_type>::digits;
  59. static _LIBCPP_CONSTEXPR const size_t _EDt = numeric_limits<_Engine_result_type>::digits;
  60. static _LIBCPP_CONSTEXPR const _Working_result_type __y0 = __w0 >= _WDt ? 0 : (_Rp >> __w0) << __w0;
  61. static _LIBCPP_CONSTEXPR const _Working_result_type __y1 = __w0 >= _WDt - 1 ? 0 : (_Rp >> (__w0 + 1)) << (__w0 + 1);
  62. static _LIBCPP_CONSTEXPR const
  63. _Engine_result_type __mask0 = __w0 > 0 ? _Engine_result_type(~0) >> (_EDt - __w0) : _Engine_result_type(0);
  64. static _LIBCPP_CONSTEXPR const _Engine_result_type __mask1 =
  65. __w0 < _EDt - 1 ? _Engine_result_type(~0) >> (_EDt - (__w0 + 1)) : _Engine_result_type(~0);
  66. public:
  67. static _LIBCPP_CONSTEXPR const result_type _Min = 0;
  68. static _LIBCPP_CONSTEXPR const result_type _Max =
  69. __w == _Dt ? result_type(~0) : (result_type(1) << __w) - result_type(1);
  70. static_assert(_Min < _Max, "independent_bits_engine invalid parameters");
  71. // engine characteristics
  72. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
  73. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
  74. // constructors and seeding functions
  75. _LIBCPP_HIDE_FROM_ABI independent_bits_engine() {}
  76. _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(const _Engine& __e) : __e_(__e) {}
  77. #ifndef _LIBCPP_CXX03_LANG
  78. _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Engine&& __e) : __e_(std::move(__e)) {}
  79. #endif // _LIBCPP_CXX03_LANG
  80. _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(result_type __sd) : __e_(__sd) {}
  81. template <
  82. class _Sseq,
  83. __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value && !is_convertible<_Sseq, _Engine>::value,
  84. int> = 0>
  85. _LIBCPP_HIDE_FROM_ABI explicit independent_bits_engine(_Sseq& __q) : __e_(__q) {}
  86. _LIBCPP_HIDE_FROM_ABI void seed() { __e_.seed(); }
  87. _LIBCPP_HIDE_FROM_ABI void seed(result_type __sd) { __e_.seed(__sd); }
  88. template <class _Sseq, __enable_if_t<__is_seed_sequence<_Sseq, independent_bits_engine>::value, int> = 0>
  89. _LIBCPP_HIDE_FROM_ABI void seed(_Sseq& __q) {
  90. __e_.seed(__q);
  91. }
  92. // generating functions
  93. _LIBCPP_HIDE_FROM_ABI result_type operator()() { return __eval(integral_constant<bool, _Rp != 0>()); }
  94. _LIBCPP_HIDE_FROM_ABI void discard(unsigned long long __z) {
  95. for (; __z; --__z)
  96. operator()();
  97. }
  98. // property functions
  99. _LIBCPP_HIDE_FROM_ABI const _Engine& base() const _NOEXCEPT { return __e_; }
  100. template <class _Eng, size_t _Wp, class _UInt>
  101. friend bool operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
  102. const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
  103. template <class _Eng, size_t _Wp, class _UInt>
  104. friend bool operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x,
  105. const independent_bits_engine<_Eng, _Wp, _UInt>& __y);
  106. template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
  107. friend basic_ostream<_CharT, _Traits>&
  108. operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x);
  109. template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
  110. friend basic_istream<_CharT, _Traits>&
  111. operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x);
  112. private:
  113. _LIBCPP_HIDE_FROM_ABI result_type __eval(false_type);
  114. _LIBCPP_HIDE_FROM_ABI result_type __eval(true_type);
  115. template <size_t __count,
  116. __enable_if_t<__count< _Dt, int> = 0> _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type __x) {
  117. return __x << __count;
  118. }
  119. template <size_t __count, __enable_if_t<(__count >= _Dt), int> = 0>
  120. _LIBCPP_HIDE_FROM_ABI static result_type __lshift(result_type) {
  121. return result_type(0);
  122. }
  123. };
  124. template <class _Engine, size_t __w, class _UIntType>
  125. inline _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(false_type) {
  126. return static_cast<result_type>(__e_() & __mask0);
  127. }
  128. template <class _Engine, size_t __w, class _UIntType>
  129. _UIntType independent_bits_engine<_Engine, __w, _UIntType>::__eval(true_type) {
  130. result_type __sp = 0;
  131. for (size_t __k = 0; __k < __n0; ++__k) {
  132. _Engine_result_type __u;
  133. do {
  134. __u = __e_() - _Engine::min();
  135. } while (__u >= __y0);
  136. __sp = static_cast<result_type>(__lshift<__w0>(__sp) + (__u & __mask0));
  137. }
  138. for (size_t __k = __n0; __k < __n; ++__k) {
  139. _Engine_result_type __u;
  140. do {
  141. __u = __e_() - _Engine::min();
  142. } while (__u >= __y1);
  143. __sp = static_cast<result_type>(__lshift<__w0 + 1>(__sp) + (__u & __mask1));
  144. }
  145. return __sp;
  146. }
  147. template <class _Eng, size_t _Wp, class _UInt>
  148. inline _LIBCPP_HIDE_FROM_ABI bool
  149. operator==(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {
  150. return __x.base() == __y.base();
  151. }
  152. template <class _Eng, size_t _Wp, class _UInt>
  153. inline _LIBCPP_HIDE_FROM_ABI bool
  154. operator!=(const independent_bits_engine<_Eng, _Wp, _UInt>& __x, const independent_bits_engine<_Eng, _Wp, _UInt>& __y) {
  155. return !(__x == __y);
  156. }
  157. template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
  158. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  159. operator<<(basic_ostream<_CharT, _Traits>& __os, const independent_bits_engine<_Eng, _Wp, _UInt>& __x) {
  160. return __os << __x.base();
  161. }
  162. template <class _CharT, class _Traits, class _Eng, size_t _Wp, class _UInt>
  163. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  164. operator>>(basic_istream<_CharT, _Traits>& __is, independent_bits_engine<_Eng, _Wp, _UInt>& __x) {
  165. _Eng __e;
  166. __is >> __e;
  167. if (!__is.fail())
  168. __x.__e_ = __e;
  169. return __is;
  170. }
  171. _LIBCPP_END_NAMESPACE_STD
  172. _LIBCPP_POP_MACROS
  173. #endif // _LIBCPP___RANDOM_INDEPENDENT_BITS_ENGINE_H