os/ossrv/ossrv_pub/boost_apis/boost/random/geometric_distribution.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/geometric_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2000-2001
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: geometric_distribution.hpp,v 1.16 2004/07/27 03:43:32 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-02-18  moved to individual header files
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
sl@0
    17
#define BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
sl@0
    18
sl@0
    19
#include <cmath>          // std::log
sl@0
    20
#include <cassert>
sl@0
    21
#include <iostream>
sl@0
    22
#include <boost/random/uniform_01.hpp>
sl@0
    23
sl@0
    24
namespace boost {
sl@0
    25
sl@0
    26
#if defined(__GNUC__) && (__GNUC__ < 3)
sl@0
    27
// Special gcc workaround: gcc 2.95.x ignores using-declarations
sl@0
    28
// in template classes (confirmed by gcc author Martin v. Loewis)
sl@0
    29
  using std::log;
sl@0
    30
#endif
sl@0
    31
sl@0
    32
// geometric distribution: p(i) = (1-p) * pow(p, i-1)   (integer)
sl@0
    33
template<class IntType = int, class RealType = double>
sl@0
    34
class geometric_distribution
sl@0
    35
{
sl@0
    36
public:
sl@0
    37
  typedef RealType input_type;
sl@0
    38
  typedef IntType result_type;
sl@0
    39
sl@0
    40
  explicit geometric_distribution(const RealType& p = RealType(0.5))
sl@0
    41
    : _p(p)
sl@0
    42
  {
sl@0
    43
    assert(RealType(0) < p && p < RealType(1));
sl@0
    44
    init();
sl@0
    45
  }
sl@0
    46
sl@0
    47
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    48
sl@0
    49
  RealType p() const { return _p; }
sl@0
    50
  void reset() { }
sl@0
    51
sl@0
    52
  template<class Engine>
sl@0
    53
  result_type operator()(Engine& eng)
sl@0
    54
  {
sl@0
    55
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    56
    using std::log;
sl@0
    57
    using std::floor;
sl@0
    58
#endif
sl@0
    59
    return IntType(floor(log(RealType(1)-eng()) / _log_p)) + IntType(1);
sl@0
    60
  }
sl@0
    61
sl@0
    62
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    63
  template<class CharT, class Traits>
sl@0
    64
  friend std::basic_ostream<CharT,Traits>&
sl@0
    65
  operator<<(std::basic_ostream<CharT,Traits>& os, const geometric_distribution& gd)
sl@0
    66
  {
sl@0
    67
    os << gd._p;
sl@0
    68
    return os;
sl@0
    69
  }
sl@0
    70
sl@0
    71
  template<class CharT, class Traits>
sl@0
    72
  friend std::basic_istream<CharT,Traits>&
sl@0
    73
  operator>>(std::basic_istream<CharT,Traits>& is, geometric_distribution& gd)
sl@0
    74
  {
sl@0
    75
    is >> std::ws >> gd._p;
sl@0
    76
    gd.init();
sl@0
    77
    return is;
sl@0
    78
  }
sl@0
    79
#endif
sl@0
    80
sl@0
    81
private:
sl@0
    82
  void init()
sl@0
    83
  {
sl@0
    84
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    85
    using std::log;
sl@0
    86
#endif
sl@0
    87
    _log_p = log(_p);
sl@0
    88
  }
sl@0
    89
sl@0
    90
  RealType _p;
sl@0
    91
  RealType _log_p;
sl@0
    92
};
sl@0
    93
sl@0
    94
} // namespace boost
sl@0
    95
sl@0
    96
#endif // BOOST_RANDOM_GEOMETRIC_DISTRIBUTION_HPP
sl@0
    97