1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/stdapis/boost/random/geometric_distribution.hpp Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,97 @@
1.4 +/* boost random/geometric_distribution.hpp header file
1.5 + *
1.6 + * Copyright Jens Maurer 2000-2001
1.7 + * Distributed under the Boost Software License, Version 1.0. (See
1.8 + * accompanying file LICENSE_1_0.txt or copy at
1.9 + * http://www.boost.org/LICENSE_1_0.txt)
1.10 + *
1.11 + * See http://www.boost.org for most recent version including documentation.
1.12 + *
1.13 + * $Id: geometric_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $
1.14 + *
1.15 + * Revision history
1.16 + * 2001-02-18 moved to individual header files
1.17 + */
1.18 +
1.19 +#ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
1.20 +#define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
1.21 +
1.22 +#include <cmath> // std::log
1.23 +#include <cassert>
1.24 +#include <iostream>
1.25 +#include <boost/random/uniform_01.hpp>
1.26 +
1.27 +namespace boost {
1.28 +
1.29 +#if defined(__GNUC__) && (__GNUC__ < 3)
1.30 +// Special gcc workaround: gcc 2.95.x ignores using-declarations
1.31 +// in template classes (confirmed by gcc author Martin v. Loewis)
1.32 + using std::log;
1.33 +#endif
1.34 +
1.35 +// geometric distribution: p(i) = (1-p) * pow(p, i-1) (integer)
1.36 +template<class IntType = int, class RealType = double>
1.37 +class geometric_distribution
1.38 +{
1.39 +public:
1.40 + typedef RealType input_type;
1.41 + typedef IntType result_type;
1.42 +
1.43 + explicit geometric_distribution(const RealType& p = RealType(0.5))
1.44 + : _p(p)
1.45 + {
1.46 + assert(RealType(0) < p && p < RealType(1));
1.47 + init();
1.48 + }
1.49 +
1.50 + // compiler-generated copy ctor and assignment operator are fine
1.51 +
1.52 + RealType p() const { return _p; }
1.53 + void reset() { }
1.54 +
1.55 + template<class Engine>
1.56 + result_type operator()(Engine& eng)
1.57 + {
1.58 +#ifndef BOOST_NO_STDC_NAMESPACE
1.59 + using std::log;
1.60 + using std::floor;
1.61 +#endif
1.62 + return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
1.63 + }
1.64 +
1.65 +#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
1.66 + template<class CharT, class Traits>
1.67 + friend std::basic_ostream<CharT,Traits>&
1.68 + operator<<(std::basic_ostream<CharT,Traits>& os, const geometric_distribution& gd)
1.69 + {
1.70 + os << gd._p;
1.71 + return os;
1.72 + }
1.73 +
1.74 + template<class CharT, class Traits>
1.75 + friend std::basic_istream<CharT,Traits>&
1.76 + operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
1.77 + {
1.78 + is >> std::ws >> gd._p;
1.79 + gd.init();
1.80 + return is;
1.81 + }
1.82 +#endif
1.83 +
1.84 +private:
1.85 + void init()
1.86 + {
1.87 +#ifndef BOOST_NO_STDC_NAMESPACE
1.88 + using std::log;
1.89 +#endif
1.90 + _log_p = log(_p);
1.91 + }
1.92 +
1.93 + RealType _p;
1.94 + RealType _log_p;
1.95 +};
1.96 +
1.97 +} // namespace boost
1.98 +
1.99 +#endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
1.100 +