os/ossrv/ossrv_pub/boost_apis/boost/random/discard_block.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/discard_block.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2002
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: discard_block.hpp,v 1.12 2005/05/21 15:57:00 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 * Revision history
sl@0
    13
 *  2001-03-02  created
sl@0
    14
 */
sl@0
    15
sl@0
    16
#ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
sl@0
    17
#define BOOST_RANDOM_DISCARD_BLOCK_HPP
sl@0
    18
sl@0
    19
#include <iostream>
sl@0
    20
#include <boost/config.hpp>
sl@0
    21
#include <boost/limits.hpp>
sl@0
    22
#include <boost/static_assert.hpp>
sl@0
    23
sl@0
    24
sl@0
    25
namespace boost {
sl@0
    26
namespace random {
sl@0
    27
sl@0
    28
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
sl@0
    29
class discard_block
sl@0
    30
{
sl@0
    31
public:
sl@0
    32
  typedef UniformRandomNumberGenerator base_type;
sl@0
    33
  typedef typename base_type::result_type result_type;
sl@0
    34
sl@0
    35
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
sl@0
    36
  BOOST_STATIC_CONSTANT(unsigned int, total_block = p);
sl@0
    37
  BOOST_STATIC_CONSTANT(unsigned int, returned_block = r);
sl@0
    38
sl@0
    39
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    40
  BOOST_STATIC_ASSERT(total_block >= returned_block);
sl@0
    41
#endif
sl@0
    42
sl@0
    43
  discard_block() : _rng(), _n(0) { }
sl@0
    44
  explicit discard_block(const base_type & rng) : _rng(rng), _n(0) { }
sl@0
    45
  template<class It> discard_block(It& first, It last)
sl@0
    46
    : _rng(first, last), _n(0) { }
sl@0
    47
  void seed() { _rng.seed(); _n = 0; }
sl@0
    48
  template<class T> void seed(T s) { _rng.seed(s); _n = 0; }
sl@0
    49
  template<class It> void seed(It& first, It last)
sl@0
    50
  { _n = 0; _rng.seed(first, last); }
sl@0
    51
sl@0
    52
  const base_type& base() const { return _rng; }
sl@0
    53
sl@0
    54
  result_type operator()()
sl@0
    55
  {
sl@0
    56
    if(_n >= returned_block) {
sl@0
    57
      // discard values of random number generator
sl@0
    58
      for( ; _n < total_block; ++_n)
sl@0
    59
        _rng();
sl@0
    60
      _n = 0;
sl@0
    61
    }
sl@0
    62
    ++_n;
sl@0
    63
    return _rng();
sl@0
    64
  }
sl@0
    65
sl@0
    66
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
sl@0
    67
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
sl@0
    68
  static bool validation(result_type x) { return true; }  // dummy
sl@0
    69
sl@0
    70
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
sl@0
    71
sl@0
    72
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
    73
  template<class CharT, class Traits>
sl@0
    74
  friend std::basic_ostream<CharT,Traits>&
sl@0
    75
  operator<<(std::basic_ostream<CharT,Traits>& os, const discard_block& s)
sl@0
    76
  {
sl@0
    77
    os << s._rng << " " << s._n << " ";
sl@0
    78
    return os;
sl@0
    79
  }
sl@0
    80
sl@0
    81
  template<class CharT, class Traits>
sl@0
    82
  friend std::basic_istream<CharT,Traits>&
sl@0
    83
  operator>>(std::basic_istream<CharT,Traits>& is, discard_block& s)
sl@0
    84
  {
sl@0
    85
    is >> s._rng >> std::ws >> s._n >> std::ws;
sl@0
    86
    return is;
sl@0
    87
  }
sl@0
    88
#endif
sl@0
    89
sl@0
    90
  friend bool operator==(const discard_block& x, const discard_block& y)
sl@0
    91
  { return x._rng == y._rng && x._n == y._n; }
sl@0
    92
  friend bool operator!=(const discard_block& x, const discard_block& y)
sl@0
    93
  { return !(x == y); }
sl@0
    94
#else
sl@0
    95
  // Use a member function; Streamable concept not supported.
sl@0
    96
  bool operator==(const discard_block& rhs) const
sl@0
    97
  { return _rng == rhs._rng && _n == rhs._n; }
sl@0
    98
  bool operator!=(const discard_block& rhs) const
sl@0
    99
  { return !(*this == rhs); }
sl@0
   100
#endif
sl@0
   101
sl@0
   102
private:
sl@0
   103
  base_type _rng;
sl@0
   104
  unsigned int _n;
sl@0
   105
};
sl@0
   106
sl@0
   107
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
   108
//  A definition is required even for integral static constants
sl@0
   109
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
sl@0
   110
const bool discard_block<UniformRandomNumberGenerator, p, r>::has_fixed_range;
sl@0
   111
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
sl@0
   112
const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::total_block;
sl@0
   113
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
sl@0
   114
const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::returned_block;
sl@0
   115
#endif
sl@0
   116
sl@0
   117
} // namespace random
sl@0
   118
sl@0
   119
} // namespace boost
sl@0
   120
sl@0
   121
#endif // BOOST_RANDOM_DISCARD_BLOCK_HPP