independent_bits.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* boost random/independent_bits.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2011
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * See http://www.boost.org for most recent version including documentation.
  9. *
  10. * $Id$
  11. *
  12. */
  13. #ifndef BOOST_RANDOM_INDEPENDENT_BITS_HPP
  14. #define BOOST_RANDOM_INDEPENDENT_BITS_HPP
  15. #include <istream>
  16. #include <iosfwd>
  17. #include <boost/assert.hpp>
  18. #include <boost/limits.hpp>
  19. #include <boost/config.hpp>
  20. #include <boost/cstdint.hpp>
  21. #include <boost/integer/integer_mask.hpp>
  22. #include <boost/random/traits.hpp>
  23. #include <boost/random/detail/config.hpp>
  24. #include <boost/random/detail/integer_log2.hpp>
  25. #include <boost/random/detail/operators.hpp>
  26. #include <boost/random/detail/seed.hpp>
  27. #include <boost/random/detail/seed_impl.hpp>
  28. #include <boost/random/detail/signed_unsigned_tools.hpp>
  29. namespace boost {
  30. namespace random {
  31. /**
  32. * An instantiation of class template @c independent_bits_engine
  33. * model a \pseudo_random_number_generator. It generates random
  34. * numbers distributed between [0, 2^w) by combining one or
  35. * more invocations of the base engine.
  36. *
  37. * Requires: 0 < w <= std::numeric_limits<UIntType>::digits
  38. */
  39. template<class Engine, std::size_t w, class UIntType>
  40. class independent_bits_engine
  41. {
  42. public:
  43. typedef Engine base_type;
  44. typedef UIntType result_type;
  45. typedef typename Engine::result_type base_result_type;
  46. // Required by old Boost.Random concept
  47. BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
  48. /** Returns the smallest value that the generator can produce. */
  49. static BOOST_CONSTEXPR result_type min BOOST_PREVENT_MACRO_SUBSTITUTION ()
  50. { return 0; }
  51. /** Returns the largest value that the generator can produce. */
  52. static BOOST_CONSTEXPR result_type max BOOST_PREVENT_MACRO_SUBSTITUTION ()
  53. { return max_imp(boost::is_integral<UIntType>()); }
  54. /**
  55. * Constructs an @c independent_bits_engine using the
  56. * default constructor of the base generator.
  57. */
  58. independent_bits_engine() { }
  59. /**
  60. * Constructs an @c independent_bits_engine, using seed as
  61. * the constructor argument for both base generators.
  62. */
  63. BOOST_RANDOM_DETAIL_ARITHMETIC_CONSTRUCTOR(independent_bits_engine,
  64. base_result_type, seed_arg)
  65. {
  66. _base.seed(seed_arg);
  67. }
  68. /**
  69. * Constructs an @c independent_bits_engine, using seq as
  70. * the constructor argument for the base generator.
  71. */
  72. BOOST_RANDOM_DETAIL_SEED_SEQ_CONSTRUCTOR(independent_bits_engine,
  73. SeedSeq, seq)
  74. { _base.seed(seq); }
  75. /** Constructs an @c independent_bits_engine by copying @c base. */
  76. independent_bits_engine(const base_type& base_arg) : _base(base_arg) {}
  77. /**
  78. * Contructs an @c independent_bits_engine with
  79. * values from the range defined by the input iterators first
  80. * and last. first will be modified to point to the element
  81. * after the last one used.
  82. *
  83. * Throws: @c std::invalid_argument if the input range is too small.
  84. *
  85. * Exception Safety: Basic
  86. */
  87. template<class It>
  88. independent_bits_engine(It& first, It last) : _base(first, last) { }
  89. /**
  90. * Seeds an @c independent_bits_engine using the default
  91. * seed of the base generator.
  92. */
  93. void seed() { _base.seed(); }
  94. /**
  95. * Seeds an @c independent_bits_engine, using @c seed as the
  96. * seed for the base generator.
  97. */
  98. BOOST_RANDOM_DETAIL_ARITHMETIC_SEED(independent_bits_engine,
  99. base_result_type, seed_arg)
  100. { _base.seed(seed_arg); }
  101. /**
  102. * Seeds an @c independent_bits_engine, using @c seq to
  103. * seed the base generator.
  104. */
  105. BOOST_RANDOM_DETAIL_SEED_SEQ_SEED(independent_bits_engine,
  106. SeedSeq, seq)
  107. { _base.seed(seq); }
  108. /**
  109. * Seeds an @c independent_bits_engine with
  110. * values from the range defined by the input iterators first
  111. * and last. first will be modified to point to the element
  112. * after the last one used.
  113. *
  114. * Throws: @c std::invalid_argument if the input range is too small.
  115. *
  116. * Exception Safety: Basic
  117. */
  118. template<class It> void seed(It& first, It last)
  119. { _base.seed(first, last); }
  120. /** Returns the next value of the generator. */
  121. result_type operator()()
  122. {
  123. // While it may seem wasteful to recalculate this
  124. // every time, both msvc and gcc can propagate
  125. // constants, resolving this at compile time.
  126. base_unsigned range =
  127. detail::subtract<base_result_type>()((_base.max)(), (_base.min)());
  128. std::size_t m =
  129. (range == (std::numeric_limits<base_unsigned>::max)()) ?
  130. std::numeric_limits<base_unsigned>::digits :
  131. detail::integer_log2(range + 1);
  132. std::size_t n = (w + m - 1) / m;
  133. std::size_t w0, n0;
  134. base_unsigned y0, y1;
  135. base_unsigned y0_mask, y1_mask;
  136. calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
  137. if(base_unsigned(range - y0 + 1) > y0 / n) {
  138. // increment n and try again.
  139. ++n;
  140. calc_params(n, range, w0, n0, y0, y1, y0_mask, y1_mask);
  141. }
  142. BOOST_ASSERT(n0*w0 + (n - n0)*(w0 + 1) == w);
  143. BOOST_ASSERT((n == 1) == (w0 == w));
  144. // special case to avoid undefined behavior from shifting
  145. if(n == 1) {
  146. BOOST_ASSERT(n0 == 1);
  147. base_unsigned u;
  148. do {
  149. u = detail::subtract<base_result_type>()(_base(), (_base.min)());
  150. } while(u > base_unsigned(y0 - 1));
  151. return u & y0_mask;
  152. }
  153. result_type S = 0;
  154. for(std::size_t k = 0; k < n0; ++k) {
  155. base_unsigned u;
  156. do {
  157. u = detail::subtract<base_result_type>()(_base(), (_base.min)());
  158. } while(u > base_unsigned(y0 - 1));
  159. S = (S << w0) + (u & y0_mask);
  160. }
  161. for(std::size_t k = 0; k < (n - n0); ++k) {
  162. base_unsigned u;
  163. do {
  164. u = detail::subtract<base_result_type>()(_base(), (_base.min)());
  165. } while(u > base_unsigned(y1 - 1));
  166. S = (S << (w0 + 1)) + (u & y1_mask);
  167. }
  168. return S;
  169. }
  170. /** Fills a range with random values */
  171. template<class Iter>
  172. void generate(Iter first, Iter last)
  173. { detail::generate_from_int(*this, first, last); }
  174. /** Advances the state of the generator by @c z. */
  175. void discard(boost::uintmax_t z)
  176. {
  177. for(boost::uintmax_t i = 0; i < z; ++i) {
  178. (*this)();
  179. }
  180. }
  181. const base_type& base() const { return _base; }
  182. /**
  183. * Writes the textual representation if the generator to a @c std::ostream.
  184. * The textual representation of the engine is the textual representation
  185. * of the base engine.
  186. */
  187. BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, independent_bits_engine, r)
  188. {
  189. os << r._base;
  190. return os;
  191. }
  192. /**
  193. * Reads the state of an @c independent_bits_engine from a
  194. * @c std::istream.
  195. */
  196. BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, independent_bits_engine, r)
  197. {
  198. is >> r._base;
  199. return is;
  200. }
  201. /**
  202. * Returns: true iff the two @c independent_bits_engines will
  203. * produce the same sequence of values.
  204. */
  205. BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(independent_bits_engine, x, y)
  206. { return x._base == y._base; }
  207. /**
  208. * Returns: true iff the two @c independent_bits_engines will
  209. * produce different sequences of values.
  210. */
  211. BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(independent_bits_engine)
  212. private:
  213. /// \cond show_private
  214. typedef typename boost::random::traits::make_unsigned<base_result_type>::type base_unsigned;
  215. static BOOST_CONSTEXPR UIntType max_imp(const boost::true_type&)
  216. {
  217. return boost::low_bits_mask_t<w>::sig_bits;
  218. }
  219. static UIntType max_imp(const boost::false_type&)
  220. {
  221. // We have a multiprecision integer type:
  222. BOOST_STATIC_ASSERT(std::numeric_limits<UIntType>::is_specialized);
  223. return w < std::numeric_limits<UIntType>::digits ? UIntType((UIntType(1) << w) - 1) : UIntType((((UIntType(1) << (w - 1)) - 1) << 1) | 1u);
  224. }
  225. void calc_params(
  226. std::size_t n, base_unsigned range,
  227. std::size_t& w0, std::size_t& n0,
  228. base_unsigned& y0, base_unsigned& y1,
  229. base_unsigned& y0_mask, base_unsigned& y1_mask)
  230. {
  231. BOOST_ASSERT(w >= n);
  232. w0 = w/n;
  233. n0 = n - w % n;
  234. y0_mask = (base_unsigned(2) << (w0 - 1)) - 1;
  235. y1_mask = (y0_mask << 1) | 1;
  236. y0 = (range + 1) & ~y0_mask;
  237. y1 = (range + 1) & ~y1_mask;
  238. BOOST_ASSERT(y0 != 0 || base_unsigned(range + 1) == 0);
  239. }
  240. /// \endcond
  241. Engine _base;
  242. };
  243. #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
  244. template<class Engine, std::size_t w, class UIntType>
  245. const bool independent_bits_engine<Engine, w, UIntType>::has_fixed_range;
  246. #endif
  247. } // namespace random
  248. } // namespace boost
  249. #endif // BOOST_RANDOM_INDEPENDENT_BITS_HPP