td::allocator<_Tp_nc>(), std::forward<_Args>(__args)...); } /** @brief Create an object that is owned by a shared_ptr. * @param __a An allocator. * @param __args Arguments for the @a _Tp object's constructor. * @return A shared_ptr that owns the newly created object. * @throw An exception thrown from @a _Alloc::allocate or from the * constructor of @a _Tp. * * A copy of @a __a will be used to allocate memory for the shared_ptr * and the new object. */ template inline shared_ptr<_Tp> allocate_shared(_Alloc __a, _Args&&... __args) { return shared_ptr<_Tp>(_Sp_make_shared_tag(), std::forward<_Alloc>(__a), std::forward<_Args>(__args)...); } /** @brief Create an object that is owned by a shared_ptr. * @param __args Arguments for the @a _Tp object's constructor. * @return A shared_ptr that owns the newly created object. * @throw std::bad_alloc, or an exception thrown from the * constructor of @a _Tp. */ template inline shared_ptr<_Tp> make_shared(_Args&&... __args) { typedef typename std::remove_const<_Tp>::type _Tp_nc; return allocate_shared<_Tp>(std::allocator<_Tp_nc>(), std::forward<_Args>(__args)...); } #endif _GLIBCXX_END_NAMESPACE_TR1 } // TR1 cfenv -*- C++ -*- // Copyright (C) 2007, 2008 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 2, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING. If not, write to the Free // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, // USA. // As a special exception, you may use this file as part of a free software // library without restriction. Specifically, if other files instantiate // templates or use macros or inline functions from this file, or you compile // this file and link it with other files to produce an executable, this // file does not by itself cause the resulting executable to be covered by // the GNU General Public License. This exception does not however // invalidate any other reasons why the executable file might be covered by // the GNU General Public License. /** @file tr1_impl/cfenv * This is an internal header file, included by other library headers. * You should not attempt to use it directly. */ #pragma GCC system_header #if _GLIBCXX_USE_C99_FENV_TR1 #undef feclearexcept #undef fegetexceptflag #undef feraiseexcept #undef fesetexceptflag #undef fetestexcept #undef fegetround #undef fesetround #undef fegetenv #undef feholdexcept #undef fesetenv #undef feupdateenv namespace std { _GLIBCXX_BEGIN_NAMESPACE_TR1 // types using ::fenv_t; using ::fexcept_t; // functions using ::feclearexcept; using ::fegetexceptflag; using ::feraiseexcept; using ::fesetexceptflag; using ::fetestexcept; using ::fegetround; using ::fesetround; using ::fegetenv; using ::feholdexcept; using ::fesetenv; using ::feupdateenv; _GLIBCXX_END_NAMESPACE_TR1 } #endif // -*- C++ -*- // Copyright (C) 2007 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 2, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING. If not, write to the Free // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, // USA. // As a special exception, you may use this file as part of a free software // library without restriction. Specifically, if other files instantiate // templates or use macros or inline functions from this file, or you compile // this file and link it with other files to produce an executable, this // file does not by itself cause the resulting executable to be covered by // the GNU General Public License. This exception does not however // invalidate any other reasons why the executable file might be covered by // the GNU General Public License. // shared_count.hpp // Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd. // shared_ptr.hpp // Copyright (C) 1998, 1999 Greg Colvin and Beman Dawes. // Copyright (C) 2001, 2002, 2003 Peter Dimov // weak_ptr.hpp // Copyright (C) 2001, 2002, 2003 Peter Dimov // enable_shared_from_this.hpp // Copyright (C) 2002 Peter Dimov // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // GCC Note: based on version 1.32.0 of the Boost library. /** @file tr1_impl/boost_sp_counted_base.h * This is an internal header file, included by other library headers. * You should not attempt to use it directly. */ namespace std { _GLIBCXX_BEGIN_NAMESPACE_TR1 class bad_weak_ptr : public std::exception { public: virtual char const* what() const throw() #ifdef _GLIBCXX_INCLUDE_AS_CXX0X { return "std::bad_weak_ptr"; } #else { return "tr1::bad_weak_ptr"; } #endif }; // Substitute for bad_weak_ptr object in the case of -fno-exceptions. inline void __throw_bad_weak_ptr() { #if __EXCEPTIONS throw bad_weak_ptr(); #else __builtin_abort(); #endif } using __gnu_cxx::_Lock_policy; using __gnu_cxx::__default_lock_policy; using __gnu_cxx::_S_single; using __gnu_cxx::_S_mutex; using __gnu_cxx::_S_atomic; // Empty helper class except when the template argument is _S_mutex. template<_Lock_policy _Lp> class _Mutex_base { protected: // The atomic policy uses fully-fenced builtins, single doesn't care. enum { _S_need_barriers = 0 }; }; template<> class _Mutex_base<_S_mutex> : public __gnu_cxx::__mutex { protected: // This policy is used when atomic builtins are not available. // The replacement atomic operations might not have the necessary // memory barriers. enum { _S_need_barriers = 1 }; }; template<_Lock_policy _Lp = __default_lock_policy> class _Sp_counted_base : public _Mutex_base<_Lp> { public: _Sp_counted_base() : _M_use_count(1), _M_weak_count(1) { } virtual ~_Sp_counted_base() // nothrow { } // Called when _M_use_count drops to zero, to release the resources // managed by *this. virtual void _M_dispose() = 0; // nothrow // Called when _M_weak_count drops to zero. virtual void _M_destroy() // nothrow { delete this; } virtual void* _M_get_deleter(const std::type_info&) = 0; void _M_add_ref_copy() { __gnu_cxx::__atomic_add_dispatch(&_M_use_count, 1); } void _M_add_ref_lock(); void _M_release() // nothrow { if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, -1) == 1) { _M_dispose(); // There must be a memory barrier between dispose() and destroy() // to ensure that the effects of dispose() are observed in the // thread that runs destroy(). // See http://gcc.gnu.org/ml/libstdc++/2005-11/msg00136.html if (_Mutex_base<_Lp>::_S_need_barriers) { _GLIBCXX_READ_MEM_BARRIER; _GLIBCXX_WRITE_MEM_BARRIER; } if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) _M_destroy(); } } void _M_weak_add_ref() // nothrow { __gnu_cxx::__atomic_add_dispatch(&_M_weak_count, 1); } void _M_weak_release() // nothrow { if (__gnu_cxx::__exchange_and_add_dispatch(&_M_weak_count, -1) == 1) { if (_Mutex_base<_Lp>::_S_need_barriers) { // See _M_release(), // destroy() must observe results of dispose() _GLIBCXX_READ_MEM_BARRIER; _GLIBCXX_WRITE_MEM_BARRIER; } _M_destroy(); } } long _M_get_use_count() const // nothrow { // No memory barrier is used here so there is no synchronization // with other threads. return const_cast(_M_use_count); } private: _Sp_counted_base(_Sp_counted_base const&); _Sp_counted_base& operator=(_Sp_counted_base const&); _Atomic_word _M_use_count; // #shared _Atomic_word _M_weak_count; // #weak + (#shared != 0) }; template<> inline void _Sp_counted_base<_S_single>:: _M_add_ref_lock() { if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) { _M_use_count = 0; __throw_bad_weak_ptr(); } } template<> inline void _Sp_counted_base<_S_mutex>:: _M_add_ref_lock() { __gnu_cxx::__scoped_lock sentry(*this); if (__gnu_cxx::__exchange_and_add_dispatch(&_M_use_count, 1) == 0) { _M_use_count = 0; __throw_bad_weak_ptr(); } } template<> inline void _Sp_counted_base<_S_atomic>:: _M_add_ref_lock() { // Perform lock-free add-if-not-zero operation. _Atomic_word __count; do { __count = _M_use_count; if (__count == 0) __throw_bad_weak_ptr(); // Replace the current counter value with the old value + 1, as // long as it's not changed meanwhile. } while (!__sync_bool_compare_and_swap(&_M_use_count, __count, __count + 1)); } _GLIBCXX_END_NAMESPACE_TR1 } // random number generation -*- C++ -*- // Copyright (C) 2007, 2008 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the // Free Software Foundation; either version 2, or (at your option) // any later version. // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License along // with this library; see the file COPYING. If not, write to the Free // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, // USA. // As a special exception, you may use this file as part of a free software // library without restriction. Specifically, if other files instantiate // templates or use macros or inline functions from this file, or you compile // this file and link it with other files to produce an executable, this // file does not by itself cause the resulting executable to be covered by // the GNU General Public License. This exception does not however // invalidate any other reasons why the executable file might be covered by // the GNU General Public License. /** * @file tr1_impl/random * This is an internal header file, included by other library headers. * You should not attempt to use it directly. */ namespace std { _GLIBCXX_BEGIN_NAMESPACE_TR1 // [5.1] Random number generation /** * @addtogroup tr1_random Random Number Generation * A facility for generating random numbers on selected distributions. * @{ */ /* * Implementation-space details. */ namespace __detail { template::digits> struct _Shift { static const _UIntType __value = 0; }; template struct _Shift<_UIntType, __w, true> { static const _UIntType __value = _UIntType(1) << __w; }; template struct _Mod; // Dispatch based on modulus value to prevent divide-by-zero compile-time // errors when m == 0. template inline _Tp __mod(_Tp __x) { return _Mod<_Tp, __a, __c, __m, __m == 0>::__calc(__x); } typedef __gnu_cxx::__conditional_type<(sizeof(unsigned) == 4), unsigned, unsigned long>::__type _UInt32Type; /* * An adaptor class for converting the output of any Generator into * the input for a specific Distribution. */ template struct _Adaptor { typedef typename _Engine::result_type _Engine_result_type; typedef typename _Distribution::input_type result_type; public: _Adaptor(const _Engine& __g) : _M_g(__g) { } result_type min() const { result_type __return_value; if (is_integral<_Engine_result_type>::value && is_integral::value) __return_value = _M_g.min(); else __return_value = result_type(0); return __return_value; } result_type max() const { result_type __return_value; if (is_integral<_Engine_result_type>::value && is_integral::value) __return_value = _M_g.max(); else if (!is_integral::value) __return_value = result_type(1); else __return_value = std::numeric_limits::max() - 1; return __return_value; } /* * Converts a value generated by the adapted random number generator * into a value in the input domain for the dependent random number * distribution. * * Because the type traits are compile time constants only the * appropriate clause of the if statements will actually be emitted * by the compiler. */ result_type operator()() { result_type __return_value; if (is_integral<_Engine_result_type>::value && is_integral::value) __return_value = _M_g(); else if (!is_integral<_Engine_result_type>::value && !is_integral::value) __return_value = result_type(_M_g() - _M_g.min()) / result_type(_M_g.max() - _M_g.min()); else if (is_integral<_Engine_result_type>::value && !is_integral::value) __return_value = result_type(_M_g() - _M_g.min()) / result_type(_M_g.max() - _M_g.min() + result_type(1)); else __return_value = (((_M_g() - _M_g.min()) / (_M_g.max() - _M_g.min())) * std::numeric_limits::max()); return __return_value; } private: _Engine _M_g; }; } // namespace __detail /** * Produces random numbers on a given distribution function using a * non-uniform random number generation engine. * * @todo the engine_value_type needs to be studied more carefully. */ template class variate_generator { // Concept requirements. __glibcxx_class_requires(_Engine, _CopyConstructibleConcept) // __glibcxx_class_requires(_Engine, _EngineConcept) // __glibcxx_class_requires(_Dist, _EngineConcept) public: typedef _Engine engine_type; typedef __detail::_Adaptor<_Engine, _Dist> engine_value_type; typedef _Dist distribution_type; typedef typename _Dist::result_type result_type; // tr1:5.1.1 table 5.1 requirement typedef typename __gnu_cxx::__enable_if< is_arithmetic::value, result_type>::__type _IsValidType; /** * Constructs a variate generator with the uniform random number * generator @p __eng for the random distribution @p __dist. * * @throws Any exceptions which may thrown by the copy constructors of * the @p _Engine or @p _Dist objects. */ variate_generator(engine_type __eng, distribution_type __dist) : _M_engine(__eng), _M_dist(__dist) { } /** * Gets the next generated value on the distribution. */ result_type operator()() { return _M_dist(_M_engine); } /** * WTF? */ template result_type operator()(_Tp __value) { return _M_dist(_M_engine, __value); } /** * Gets a reference to the underlying uniform random number generator * object. */ engine_value_type& engine() { return _M_engine; } /** * Gets a const reference to the underlying uniform random number * generator object. */ const engine_value_type& engine() const { return _M_engine; } /** * Gets a reference to the underlying random distribution. */ distribution_type& distribution() { return _M_dist; } /** * Gets a const reference to the underlying random distribution. */ const distribution_type& distribution() const { return _M_dist; } /** * Gets the closed lower bound of the distribution interval. */ result_type min() const { return this->distribution().min(); } /** * Gets the closed upper bound of the distribution interval. */ result_type max() const { return this->distribution().max(); } private: engine_value_type _M_engine; distribution_type _M_dist; }; /** * @addtogroup tr1_random_generators Random Number Generators * @ingroup tr1_random * * These classes define objects which provide random or pseudorandom * numbers, either from a discrete or a continuous interval. The * random number generator supplied as a part of this library are * all uniform random number generators which provide a sequence of * random number uniformly distributed over their range. * * A number generator is a function object with an operator() that * takes zero arguments and returns a number. * * A compliant random number generator must satisfy the following * requirements. * *
Random Number Generator Requirements
To be documented.
* * @{ */ /** * @brief A model of a linear congruential random number generator. * * A random number generator that produces pseudorandom numbers using the * linear function @f$x_{i+1}\leftarrow(ax_{i} + c) \bmod m @f$. * * The template parameter @p _UIntType must be an unsigned integral type * large enough to store values up to (__m-1). If the template parameter * @p __m is 0, the modulus @p __m used is * std::numeric_limits<_UIntType>::max() plus 1. Otherwise, the template * parameters @p __a and @p __c must be less than @p __m. * * The size of the state is @f$ 1 @f$. */ template class linear_congruential { __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept) // __glibcpp_class_requires(__a < __m && __c < __m) public: /** The type of the generated random value. */ typedef _UIntType result_type; /** The multiplier. */ static const _UIntType multiplier = __a; /** An increment. */ static const _UIntType increment = __c; /** The modulus. */ static const _UIntType modulus = __m; /** * Constructs a %linear_congruential random number generator engine with * seed @p __s. The default seed value is 1. * * @param __s The initial seed value. */ explicit linear_congruential(unsigned long __x0 = 1) { this->seed(__x0); } /** * Constructs a %linear_congruential random number generator engine * seeded from the generator function @p __g. * * @param __g The seed generator function. */ template linear_congruential(_Gen& __g) { this->seed(__g); } /** * Reseeds the %linear_congruential random number generator engine * sequence to the seed @g __s. * * @param __s The new seed. */ void seed(unsigned long __s = 1); /** * Reseeds the %linear_congruential random number generator engine * sequence using values from the generator function @p __g. * * @param __g the seed generator function. */ template void seed(_Gen& __g) { seed(__g, typename is_fundamental<_Gen>::type()); } /** * Gets the smallest possible value in the output range. * * The minimum depends on the @p __c parameter: if it is zero, the * minimum generated must be > 0, otherwise 0 is allowed. */ result_type min() const { return (__detail::__mod<_UIntType, 1, 0, __m>(__c) == 0) ? 1 : 0; } /** * Gets the largest possible value in the output range. */ result_type max() const { return __m - 1; } /** * Gets the next random number in the sequence. */ result_type operator()(); /** * Compares two linear congruential random number generator * objects of the same type for equality. * * @param __lhs A linear congruential random number generator object. * @param __rhs Another linear congruential random number generator obj. * * @returns true if the two objects are equal, false otherwise. */ friend bool operator==(const linear_congruential& __lhs, const linear_congruential& __rhs) { return __lhs._M_x == __rhs._M_x; } /** * Compares two linear congruential random number generator * objects of the same type for inequality. * * @param __lhs A linear congruential random number generator object. * @param __rhs Another linear congruential random number generator obj. * .......................................................... * @returns true if the two objects are not equal, false otherwise. */ friend bool operator!=(const linear_congruential& __lhs, const linear_congruential& __rhs) { return !(__lhs == __rhs); } /** * Writes the textual representation of the state x(i) of x to @p __os. * * @param __os The output stream. * @param __lcr A % linear_congruential random number generator. * @returns __os. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr); /** * Sets the state of the engine by reading its textual * representation from @p __is. * * The textual representation must have been previously written using an * output stream whose imbued locale and whose type's template * specialization arguments _CharT and _Traits were the same as those of * @p __is. * * @param __is The input stream. * @param __lcr A % linear_congruential random number generator. * @returns __is. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, linear_congruential<_UIntType1, __a1, __c1, __m1>& __lcr); private: template void seed(_Gen& __g, true_type) { return seed(static_cast(__g)); } template void seed(_Gen& __g, false_type); _UIntType _M_x; }; /** * The classic Minimum Standard rand0 of Lewis, Goodman, and Miller. */ typedef linear_congruential minstd_rand0; /** * An alternative LCR (Lehmer Generator function) . */ typedef linear_congruential minstd_rand; /** * A generalized feedback shift register discrete random number generator. * * This algorithm avoids multiplication and division and is designed to be * friendly to a pipelined architecture. If the parameters are chosen * correctly, this generator will produce numbers with a very long period and * fairly good apparent entropy, although still not cryptographically strong. * * The best way to use this generator is with the predefined mt19937 class. * * This algorithm was originally invented by Makoto Matsumoto and * Takuji Nishimura. * * @var word_size The number of bits in each element of the state vector. * @var state_size The degree of recursion. * @var shift_size The period parameter. * @var mask_bits The separation point bit index. * @var parameter_a The last row of the twist matrix. * @var output_u The first right-shift tempering matrix parameter. * @var output_s The first left-shift tempering matrix parameter. * @var output_b The first left-shift tempering matrix mask. * @var output_t The second left-shift tempering matrix parameter. * @var output_c The second left-shift tempering matrix mask. * @var output_l The second right-shift tempering matrix parameter. */ template class mersenne_twister { __glibcxx_class_requires(_UIntType, _UnsignedIntegerConcept) public: // types typedef _UIntType result_type; // parameter values static const int word_size = __w; static const int state_size = __n; static const int shift_size = __m; static const int mask_bits = __r; static const _UIntType parameter_a = __a; static const int output_u = __u; static const int output_s = __s; static const _UIntType output_b = __b; static const int output_t = __t; static const _UIntType output_c = __c; static const int output_l = __l; // constructors and member function mersenne_twister() { seed(); } explicit mersenne_twister(unsigned long __value) { seed(__value); } template mersenne_twister(_Gen& __g) { seed(__g); } void seed() { seed(5489UL); } void seed(unsigned long __value); template void seed(_Gen& __g) { seed(__g, typename is_fundamental<_Gen>::type()); } result_type min() const { return 0; }; result_type max() const { return __detail::_Shift<_UIntType, __w>::__value - 1; } result_type operator()(); /** * Compares two % mersenne_twister random number generator objects of * the same type for equality. * * @param __lhs A % mersenne_twister random number generator object. * @param __rhs Another % mersenne_twister random number generator * object. * * @returns true if the two objects are equal, false otherwise. */ friend bool operator==(const mersenne_twister& __lhs, const mersenne_twister& __rhs) { return std::equal(__lhs._M_x, __lhs._M_x + state_size, __rhs._M_x); } /** * Compares two % mersenne_twister random number generator objects of * the same type for inequality. * * @param __lhs A % mersenne_twister random number generator object. * @param __rhs Another % mersenne_twister random number generator * object. * * @returns true if the two objects are not equal, false otherwise. */ friend bool operator!=(const mersenne_twister& __lhs, const mersenne_twister& __rhs) { return !(__lhs == __rhs); } /** * Inserts the current state of a % mersenne_twister random number * generator engine @p __x into the output stream @p __os. * * @param __os An output stream. * @param __x A % mersenne_twister random number generator engine. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1, __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x); /** * Extracts the current state of a % mersenne_twister random number * generator engine @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A % mersenne_twister random number generator engine. * * @returns The input stream with the state of @p __x extracted or in * an error state. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, mersenne_twister<_UIntType1, __w1, __n1, __m1, __r1, __a1, __u1, __s1, __b1, __t1, __c1, __l1>& __x); private: template void seed(_Gen& __g, true_type) { return seed(static_cast(__g)); } template void seed(_Gen& __g, false_type); _UIntType _M_x[state_size]; int _M_p; }; /** * The classic Mersenne Twister. * * Reference: * M. Matsumoto and T. Nishimura, "Mersenne Twister: A 623-Dimensionally * Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions * on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pp 3-30. */ typedef mersenne_twister< unsigned long, 32, 624, 397, 31, 0x9908b0dful, 11, 7, 0x9d2c5680ul, 15, 0xefc60000ul, 18 > mt19937; /** * @brief The Marsaglia-Zaman generator. * * This is a model of a Generalized Fibonacci discrete random number * generator, sometimes referred to as the SWC generator. * * A discrete random number generator that produces pseudorandom * numbers using @f$x_{i}\leftarrow(x_{i - s} - x_{i - r} - * carry_{i-1}) \bmod m @f$. * * The size of the state is @f$ r @f$ * and the maximum period of the generator is @f$ m^r - m^s -1 @f$. * * N1688[4.13] says "the template parameter _IntType shall denote an integral * type large enough to store values up to m." * * @var _M_x The state of the generator. This is a ring buffer. * @var _M_carry The carry. * @var _M_p Current index of x(i - r). */ template class subtract_with_carry { __glibcxx_class_requires(_IntType, _IntegerConcept) public: /** The type of the generated random value. */ typedef _IntType result_type; // parameter values static const _IntType modulus = __m; static const int long_lag = __r; static const int short_lag = __s; /** * Constructs a default-initialized % subtract_with_carry random number * generator. */ subtract_with_carry() { this->seed(); } /** * Constructs an explicitly seeded % subtract_with_carry random number * generator. */ explicit subtract_with_carry(unsigned long __value) { this->seed(__value); } /** * Constructs a %subtract_with_carry random number generator engine * seeded from the generator function @p __g. * * @param __g The seed generator function. */ template subtract_with_carry(_Gen& __g) { this->seed(__g); } /** * Seeds the initial state @f$ x_0 @f$ of the random number generator. * * N1688[4.19] modifies this as follows. If @p __value == 0, * sets value to 19780503. In any case, with a linear * congruential generator lcg(i) having parameters @f$ m_{lcg} = * 2147483563, a_{lcg} = 40014, c_{lcg} = 0, and lcg(0) = value * @f$, sets @f$ x_{-r} \dots x_{-1} @f$ to @f$ lcg(1) \bmod m * \dots lcg(r) \bmod m @f$ respectively. If @f$ x_{-1} = 0 @f$ * set carry to 1, otherwise sets carry to 0. */ void seed(unsigned long __value = 19780503); /** * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry * random number generator. */ template void seed(_Gen& __g) { seed(__g, typename is_fundamental<_Gen>::type()); } /** * Gets the inclusive minimum value of the range of random integers * returned by this generator. */ result_type min() const { return 0; } /** * Gets the inclusive maximum value of the range of random integers * returned by this generator. */ result_type max() const { return this->modulus - 1; } /** * Gets the next random number in the sequence. */ result_type operator()(); /** * Compares two % subtract_with_carry random number generator objects of * the same type for equality. * * @param __lhs A % subtract_with_carry random number generator object. * @param __rhs Another % subtract_with_carry random number generator * object. * * @returns true if the two objects are equal, false otherwise. */ friend bool operator==(const subtract_with_carry& __lhs, const subtract_with_carry& __rhs) { return std::equal(__lhs._M_x, __lhs._M_x + long_lag, __rhs._M_x); } /** * Compares two % subtract_with_carry random number generator objects of * the same type for inequality. * * @param __lhs A % subtract_with_carry random number generator object. * @param __rhs Another % subtract_with_carry random number generator * object. * * @returns true if the two objects are not equal, false otherwise. */ friend bool operator!=(const subtract_with_carry& __lhs, const subtract_with_carry& __rhs) { return !(__lhs == __rhs); } /** * Inserts the current state of a % subtract_with_carry random number * generator engine @p __x into the output stream @p __os. * * @param __os An output stream. * @param __x A % subtract_with_carry random number generator engine. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x); /** * Extracts the current state of a % subtract_with_carry random number * generator engine @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A % subtract_with_carry random number generator engine. * * @returns The input stream with the state of @p __x extracted or in * an error state. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, subtract_with_carry<_IntType1, __m1, __s1, __r1>& __x); private: template void seed(_Gen& __g, true_type) { return seed(static_cast(__g)); } template void seed(_Gen& __g, false_type); typedef typename __gnu_cxx::__add_unsigned<_IntType>::__type _UIntType; _UIntType _M_x[long_lag]; _UIntType _M_carry; int _M_p; }; /** * @brief The Marsaglia-Zaman generator (floats version). * * @var _M_x The state of the generator. This is a ring buffer. * @var _M_carry The carry. * @var _M_p Current index of x(i - r). * @var _M_npows Precomputed negative powers of 2. */ template class subtract_with_carry_01 { public: /** The type of the generated random value. */ typedef _RealType result_type; // parameter values static const int word_size = __w; static const int long_lag = __r; static const int short_lag = __s; /** * Constructs a default-initialized % subtract_with_carry_01 random * number generator. */ subtract_with_carry_01() { this->seed(); _M_initialize_npows(); } /** * Constructs an explicitly seeded % subtract_with_carry_01 random number * generator. */ explicit subtract_with_carry_01(unsigned long __value) { this->seed(__value); _M_initialize_npows(); } /** * Constructs a % subtract_with_carry_01 random number generator engine * seeded from the generator function @p __g. * * @param __g The seed generator function. */ template subtract_with_carry_01(_Gen& __g) { this->seed(__g); _M_initialize_npows(); } /** * Seeds the initial state @f$ x_0 @f$ of the random number generator. */ void seed(unsigned long __value = 19780503); /** * Seeds the initial state @f$ x_0 @f$ of the % subtract_with_carry_01 * random number generator. */ template void seed(_Gen& __g) { seed(__g, typename is_fundamental<_Gen>::type()); } /** * Gets the minimum value of the range of random floats * returned by this generator. */ result_type min() const { return 0.0; } /** * Gets the maximum value of the range of random floats * returned by this generator. */ result_type max() const { return 1.0; } /** * Gets the next random number in the sequence. */ result_type operator()(); /** * Compares two % subtract_with_carry_01 random number generator objects * of the same type for equality. * * @param __lhs A % subtract_with_carry_01 random number * generator object. * @param __rhs Another % subtract_with_carry_01 random number generator * object. * * @returns true if the two objects are equal, false otherwise. */ friend bool operator==(const subtract_with_carry_01& __lhs, const subtract_with_carry_01& __rhs) { for (int __i = 0; __i < long_lag; ++__i) if (!std::equal(__lhs._M_x[__i], __lhs._M_x[__i] + __n, __rhs._M_x[__i])) return false; return true; } /** * Compares two % subtract_with_carry_01 random number generator objects * of the same type for inequality. * * @param __lhs A % subtract_with_carry_01 random number * generator object. * * @param __rhs Another % subtract_with_carry_01 random number generator * object. * * @returns true if the two objects are not equal, false otherwise. */ friend bool operator!=(const subtract_with_carry_01& __lhs, const subtract_with_carry_01& __rhs) { return !(__lhs == __rhs); } /** * Inserts the current state of a % subtract_with_carry_01 random number * generator engine @p __x into the output stream @p __os. * * @param __os An output stream. * @param __x A % subtract_with_carry_01 random number generator engine. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x); /** * Extracts the current state of a % subtract_with_carry_01 random number * generator engine @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A % subtract_with_carry_01 random number generator engine. * * @returns The input stream with the state of @p __x extracted or in * an error state. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, subtract_with_carry_01<_RealType1, __w1, __s1, __r1>& __x); private: template void seed(_Gen& __g, true_type) { return seed(static_cast(__g)); } template void seed(_Gen& __g, false_type); void _M_initialize_npows(); static const int __n = (__w + 31) / 32; typedef __detail::_UInt32Type _UInt32Type; _UInt32Type _M_x[long_lag][__n]; _RealType _M_npows[__n]; _UInt32Type _M_carry; int _M_p; }; typedef subtract_with_carry_01 ranlux_base_01; // _GLIBCXX_RESOLVE_LIB_DEFECTS // 508. Bad parameters for ranlux64_base_01. typedef subtract_with_carry_01 ranlux64_base_01; /** * Produces random numbers from some base engine by discarding blocks of * data. * * 0 <= @p __r <= @p __p */ template class discard_block { // __glibcxx_class_requires(typename base_type::result_type, // ArithmeticTypeConcept) public: /** The type of the underlying generator engine. */ typedef _UniformRandomNumberGenerator base_type; /** The type of the generated random value. */ typedef typename base_type::result_type result_type; // parameter values static const int block_size = __p; static const int used_block = __r; /** * Constructs a default %discard_block engine. * * The underlying engine is default constructed as well. */ discard_block() : _M_n(0) { } /** * Copy constructs a %discard_block engine. * * Copies an existing base class random number generator. * @param rng An existing (base class) engine object. */ explicit discard_block(const base_type& __rng) : _M_b(__rng), _M_n(0) { } /** * Seed constructs a %discard_block engine. * * Constructs the underlying generator engine seeded with @p __s. * @param __s A seed value for the base class engine. */ explicit discard_block(unsigned long __s) : _M_b(__s), _M_n(0) { } /** * Generator construct a %discard_block engine. * * @param __g A seed generator function. */ template discard_block(_Gen& __g) : _M_b(__g), _M_n(0) { } /** * Reseeds the %discard_block object with the default seed for the * underlying base class generator engine. */ void seed() { _M_b.seed(); _M_n = 0; } /** * Reseeds the %discard_block object with the given seed generator * function. * @param __g A seed generator function. */ template void seed(_Gen& __g) { _M_b.seed(__g); _M_n = 0; } /** * Gets a const reference to the underlying generator engine object. */ const base_type& base() const { return _M_b; } /** * Gets the minimum value in the generated random number range. */ result_type min() const { return _M_b.min(); } /** * Gets the maximum value in the generated random number range. */ result_type max() const { return _M_b.max(); } /** * Gets the next value in the generated random number sequence. */ result_type operator()(); /** * Compares two %discard_block random number generator objects of * the same type for equality. * * @param __lhs A %discard_block random number generator object. * @param __rhs Another %discard_block random number generator * object. * * @returns true if the two objects are equal, false otherwise. */ friend bool operator==(const discard_block& __lhs, const discard_block& __rhs) { return (__lhs._M_b == __rhs._M_b) && (__lhs._M_n == __rhs._M_n); } /** * Compares two %discard_block random number generator objects of * the same type for inequality. * * @param __lhs A %discard_block random number generator object. * @param __rhs Another %discard_block random number generator * object. * * @returns true if the two objects are not equal, false otherwise. */ friend bool operator!=(const discard_block& __lhs, const discard_block& __rhs) { return !(__lhs == __rhs); } /** * Inserts the current state of a %discard_block random number * generator engine @p __x into the output stream @p __os. * * @param __os An output stream. * @param __x A %discard_block random number generator engine. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const discard_block<_UniformRandomNumberGenerator1, __p1, __r1>& __x); /** * Extracts the current state of a % subtract_with_carry random number * generator engine @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A %discard_block random number generator engine. * * @returns The input stream with the state of @p __x extracted or in * an error state. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, discard_block<_UniformRandomNumberGenerator1, __p1, __r1>& __x); private: base_type _M_b; int _M_n; }; /** * James's luxury-level-3 integer adaptation of Luescher's generator. */ typedef discard_block< subtract_with_carry, 223, 24 > ranlux3; /** * James's luxury-level-4 integer adaptation of Luescher's generator. */ typedef discard_block< subtract_with_carry, 389, 24 > ranlux4; typedef discard_block< subtract_with_carry_01, 223, 24 > ranlux3_01; typedef discard_block< subtract_with_carry_01, 389, 24 > ranlux4_01; /** * A random number generator adaptor class that combines two random number * generator engines into a single output sequence. */ template class xor_combine { // __glibcxx_class_requires(typename _UniformRandomNumberGenerator1:: // result_type, ArithmeticTypeConcept) // __glibcxx_class_requires(typename _UniformRandomNumberGenerator2:: // result_type, ArithmeticTypeConcept) public: /** The type of the first underlying generator engine. */ typedef _UniformRandomNumberGenerator1 base1_type; /** The type of the second underlying generator engine. */ typedef _UniformRandomNumberGenerator2 base2_type; private: typedef typename base1_type::result_type _Result_type1; typedef typename base2_type::result_type _Result_type2; public: /** The type of the generated random value. */ typedef typename __gnu_cxx::__conditional_type<(sizeof(_Result_type1) > sizeof(_Result_type2)), _Result_type1, _Result_type2>::__type result_type; // parameter values static const int shift1 = __s1; static const int shift2 = __s2; // constructors and member function xor_combine() : _M_b1(), _M_b2() { _M_initialize_max(); } xor_combine(const base1_type& __rng1, const base2_type& __rng2) : _M_b1(__rng1), _M_b2(__rng2) { _M_initialize_max(); } xor_combine(unsigned long __s) : _M_b1(__s), _M_b2(__s + 1) { _M_initialize_max(); } template xor_combine(_Gen& __g) : _M_b1(__g), _M_b2(__g) { _M_initialize_max(); } void seed() { _M_b1.seed(); _M_b2.seed(); } template void seed(_Gen& __g) { _M_b1.seed(__g); _M_b2.seed(__g); } const base1_type& base1() const { return _M_b1; } const base2_type& base2() const { return _M_b2; } result_type min() const { return 0; } result_type max() const { return _M_max; } /** * Gets the next random number in the sequence. */ // NB: Not exactly the TR1 formula, per N2079 instead. result_type operator()() { return ((result_type(_M_b1() - _M_b1.min()) << shift1) ^ (result_type(_M_b2() - _M_b2.min()) << shift2)); } /** * Compares two %xor_combine random number generator objects of * the same type for equality. * * @param __lhs A %xor_combine random number generator object. * @param __rhs Another %xor_combine random number generator * object. * * @returns true if the two objects are equal, false otherwise. */ friend bool operator==(const xor_combine& __lhs, const xor_combine& __rhs) { return (__lhs.base1() == __rhs.base1()) && (__lhs.base2() == __rhs.base2()); } /** * Compares two %xor_combine random number generator objects of * the same type for inequality. * * @param __lhs A %xor_combine random number generator object. * @param __rhs Another %xor_combine random number generator * object. * * @returns true if the two objects are not equal, false otherwise. */ friend bool operator!=(const xor_combine& __lhs, const xor_combine& __rhs) { return !(__lhs == __rhs); } /** * Inserts the current state of a %xor_combine random number * generator engine @p __x into the output stream @p __os. * * @param __os An output stream. * @param __x A %xor_combine random number generator engine. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const xor_combine<_UniformRandomNumberGenerator11, __s11, _UniformRandomNumberGenerator21, __s21>& __x); /** * Extracts the current state of a %xor_combine random number * generator engine @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A %xor_combine random number generator engine. * * @returns The input stream with the state of @p __x extracted or in * an error state. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, xor_combine<_UniformRandomNumberGenerator11, __s11, _UniformRandomNumberGenerator21, __s21>& __x); private: void _M_initialize_max(); result_type _M_initialize_max_aux(result_type, result_type, int); base1_type _M_b1; base2_type _M_b2; result_type _M_max; }; /** * A standard interface to a platform-specific non-deterministic * random number generator (if any are available). */ class random_device { public: // types typedef unsigned int result_type; // constructors, destructors and member functions #ifdef _GLIBCXX_USE_RANDOM_TR1 explicit random_device(const std::string& __token = "/dev/urandom") { if ((__token != "/dev/urandom" && __token != "/dev/random") || !(_M_file = std::fopen(__token.c_str(), "rb"))) std::__throw_runtime_error(__N("random_device::" "random_device(const std::string&)")); } ~random_device() { std::fclose(_M_file); } #else explicit random_device(const std::string& __token = "mt19937") : _M_mt(_M_strtoul(__token)) { } private: static unsigned long _M_strtoul(const std::string& __str) { unsigned long __ret = 5489UL; if (__str != "mt19937") { const char* __nptr = __str.c_str(); char* __endptr; __ret = std::strtoul(__nptr, &__endptr, 0); if (*__nptr == '\0' || *__endptr != '\0') std::__throw_runtime_error(__N("random_device::_M_strtoul" "(const std::string&)")); } return __ret; } public: #endif result_type min() const { return std::numeric_limits::min(); } result_type max() const { return std::numeric_limits::max(); } double entropy() const { return 0.0; } result_type operator()() { #ifdef _GLIBCXX_USE_RANDOM_TR1 result_type __ret; std::fread(reinterpret_cast(&__ret), sizeof(result_type), 1, _M_file); return __ret; #else return _M_mt(); #endif } private: random_device(const random_device&); void operator=(const random_device&); #ifdef _GLIBCXX_USE_RANDOM_TR1 FILE* _M_file; #else mt19937 _M_mt; #endif }; /* @} */ // group tr1_random_generators /** * @addtogroup tr1_random_distributions Random Number Distributions * @ingroup tr1_random * @{ */ /** * @addtogroup tr1_random_distributions_discrete Discrete Distributions * @ingroup tr1_random_distributions * @{ */ /** * @brief Uniform discrete distribution for random numbers. * A discrete random distribution on the range @f$[min, max]@f$ with equal * probability throughout the range. */ template class uniform_int { __glibcxx_class_requires(_IntType, _IntegerConcept) public: /** The type of the parameters of the distribution. */ typedef _IntType input_type; /** The type of the range of the distribution. */ typedef _IntType result_type; public: /** * Constructs a uniform distribution object. */ explicit uniform_int(_IntType __min = 0, _IntType __max = 9) : _M_min(__min), _M_max(__max) { _GLIBCXX_DEBUG_ASSERT(_M_min <= _M_max); } /** * Gets the inclusive lower bound of the distribution range. */ result_type min() const { return _M_min; } /** * Gets the inclusive upper bound of the distribution range. */ result_type max() const { return _M_max; } /** * Resets the distribution state. * * Does nothing for the uniform integer distribution. */ void reset() { } /** * Gets a uniformly distributed random number in the range * @f$(min, max)@f$. */ template result_type operator()(_UniformRandomNumberGenerator& __urng) { typedef typename _UniformRandomNumberGenerator::result_type _UResult_type; return _M_call(__urng, _M_min, _M_max, typename is_integral<_UResult_type>::type()); } /** * Gets a uniform random number in the range @f$[0, n)@f$. * * This function is aimed at use with std::random_shuffle. */ template result_type operator()(_UniformRandomNumberGenerator& __urng, result_type __n) { typedef typename _UniformRandomNumberGenerator::result_type _UResult_type; return _M_call(__urng, 0, __n - 1, typename is_integral<_UResult_type>::type()); } /** * Inserts a %uniform_int random number distribution @p __x into the * output stream @p os. * * @param __os An output stream. * @param __x A %uniform_int random number distribution. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const uniform_int<_IntType1>& __x); /** * Extracts a %uniform_int random number distribution * @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A %uniform_int random number generator engine. * * @returns The input stream with @p __x extracted or in an error state. */ template friend std::basic_istream<_CharT, _Traits>& operator>>(std::basic_istream<_CharT, _Traits>& __is, uniform_int<_IntType1>& __x); private: template result_type _M_call(_UniformRandomNumberGenerator& __urng, result_type __min, result_type __max, true_type); template result_type _M_call(_UniformRandomNumberGenerator& __urng, result_type __min, result_type __max, false_type) { return result_type((__urng() - __urng.min()) / (__urng.max() - __urng.min()) * (__max - __min + 1)) + __min; } _IntType _M_min; _IntType _M_max; }; /** * @brief A Bernoulli random number distribution. * * Generates a sequence of true and false values with likelihood @f$ p @f$ * that true will come up and @f$ (1 - p) @f$ that false will appear. */ class bernoulli_distribution { public: typedef int input_type; typedef bool result_type; public: /** * Constructs a Bernoulli distribution with likelihood @p p. * * @param __p [IN] The likelihood of a true result being returned. Must * be in the interval @f$ [0, 1] @f$. */ explicit bernoulli_distribution(double __p = 0.5) : _M_p(__p) { _GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0)); } /** * Gets the @p p parameter of the distribution. */ double p() const { return _M_p; } /** * Resets the distribution state. * * Does nothing for a Bernoulli distribution. */ void reset() { } /** * Gets the next value in the Bernoullian sequence. */ template result_type operator()(_UniformRandomNumberGenerator& __urng) { if ((__urng() - __urng.min()) < _M_p * (__urng.max() - __urng.min())) return true; return false; } /** * Inserts a %bernoulli_distribution random number distribution * @p __x into the output stream @p __os. * * @param __os An output stream. * @param __x A %bernoulli_distribution random number distribution. * * @returns The output stream with the state of @p __x inserted or in * an error state. */ template friend std::basic_ostream<_CharT, _Traits>& operator<<(std::basic_ostream<_CharT, _Traits>& __os, const bernoulli_distribution& __x); /** * Extracts a %bernoulli_distribution random number distribution * @p __x from the input stream @p __is. * * @param __is An input stream. * @param __x A %bernoulli_distribution random number generator engine.