~ubuntu-branches/ubuntu/breezy/aqsis/breezy

« back to all changes in this revision

Viewing changes to boost/boost/random/discard_block.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Will Newton
  • Date: 2004-12-07 20:06:49 UTC
  • Revision ID: james.westby@ubuntu.com-20041207200649-fccswkrvp4oc8lmn
Tags: upstream-0.9.3
ImportĀ upstreamĀ versionĀ 0.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* boost random/discard_block.hpp header file
 
2
 *
 
3
 * Copyright Jens Maurer 2002
 
4
 * Permission to use, copy, modify, sell, and distribute this software
 
5
 * is hereby granted without fee provided that the above copyright notice
 
6
 * appears in all copies and that both that copyright notice and this
 
7
 * permission notice appear in supporting documentation,
 
8
 *
 
9
 * Jens Maurer makes no representations about the suitability of this
 
10
 * software for any purpose. It is provided "as is" without express or
 
11
 * implied warranty.
 
12
 *
 
13
 * See http://www.boost.org for most recent version including documentation.
 
14
 *
 
15
 * $Id: discard_block.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
 
16
 *
 
17
 * Revision history
 
18
 *  2001-03-02  created
 
19
 */
 
20
 
 
21
#ifndef BOOST_RANDOM_DISCARD_BLOCK_HPP
 
22
#define BOOST_RANDOM_DISCARD_BLOCK_HPP
 
23
 
 
24
#include <iostream>
 
25
#include <boost/config.hpp>
 
26
#include <boost/limits.hpp>
 
27
#include <boost/static_assert.hpp>
 
28
 
 
29
 
 
30
namespace boost {
 
31
namespace random {
 
32
 
 
33
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
 
34
class discard_block
 
35
{
 
36
public:
 
37
  typedef UniformRandomNumberGenerator base_type;
 
38
  typedef typename base_type::result_type result_type;
 
39
 
 
40
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
 
41
  BOOST_STATIC_CONSTANT(unsigned int, total_block = p);
 
42
  BOOST_STATIC_CONSTANT(unsigned int, returned_block = r);
 
43
 
 
44
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
 
45
  BOOST_STATIC_ASSERT(total_block >= returned_block);
 
46
#endif
 
47
 
 
48
  discard_block() : _rng(), _n(0) { }
 
49
  explicit discard_block(const base_type & rng) : _rng(rng), _n(0) { }
 
50
  template<class It> discard_block(It& first, It last)
 
51
    : _rng(first, last), _n(0) { }
 
52
  template<class T> void seed(T s) { _rng.seed(s); _n = 0; }
 
53
  template<class It> void seed(It& first, It last)
 
54
  { _n = 0; _rng.seed(first, last); }
 
55
 
 
56
  const base_type& base() const { return _rng; }
 
57
 
 
58
  result_type operator()()
 
59
  {
 
60
    if(_n >= returned_block) {
 
61
      // discard values of random number generator
 
62
      for( ; _n < total_block; ++_n)
 
63
        _rng();
 
64
      _n = 0;
 
65
    }
 
66
    ++_n;
 
67
    return _rng();
 
68
  }
 
69
 
 
70
  result_type min() const { return _rng.min(); }
 
71
  result_type max() const { return _rng.max(); }
 
72
  static bool validation(result_type x) { return true; }  // dummy
 
73
 
 
74
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
 
75
 
 
76
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
 
77
  template<class CharT, class Traits>
 
78
  friend std::basic_ostream<CharT,Traits>&
 
79
  operator<<(std::basic_ostream<CharT,Traits>& os, const discard_block& s)
 
80
  {
 
81
    os << s._rng << " " << s._n << " ";
 
82
    return os;
 
83
  }
 
84
 
 
85
  template<class CharT, class Traits>
 
86
  friend std::basic_istream<CharT,Traits>&
 
87
  operator>>(std::basic_istream<CharT,Traits>& is, discard_block& s)
 
88
  {
 
89
    is >> s._rng >> std::ws >> s._n >> std::ws;
 
90
    return is;
 
91
  }
 
92
#endif
 
93
 
 
94
  friend bool operator==(const discard_block& x, const discard_block& y)
 
95
  { return x._rng == y._rng && x._n == y._n; }
 
96
  friend bool operator!=(const discard_block& x, const discard_block& y)
 
97
  { return !(x == y); }
 
98
#else
 
99
  // Use a member function; Streamable concept not supported.
 
100
  bool operator==(const discard_block& rhs) const
 
101
  { return _rng == rhs._rng && _n == rhs._n; }
 
102
  bool operator!=(const discard_block& rhs) const
 
103
  { return !(*this == rhs); }
 
104
#endif
 
105
 
 
106
private:
 
107
  base_type _rng;
 
108
  unsigned int _n;
 
109
};
 
110
 
 
111
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
 
112
//  A definition is required even for integral static constants
 
113
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
 
114
const bool discard_block<UniformRandomNumberGenerator, p, r>::has_fixed_range;
 
115
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
 
116
const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::total_block;
 
117
template<class UniformRandomNumberGenerator, unsigned int p, unsigned int r>
 
118
const unsigned int discard_block<UniformRandomNumberGenerator, p, r>::returned_block;
 
119
#endif
 
120
 
 
121
} // namespace random
 
122
 
 
123
} // namespace boost
 
124
 
 
125
#endif // BOOST_RANDOM_DISCARD_BLOCK_HPP