os/ossrv/ossrv_pub/boost_apis/boost/random/shuffle_output.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* boost random/shuffle_output.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: shuffle_output.hpp,v 1.10 2005/08/25 16:27:22 johnmaddock 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_SHUFFLE_OUTPUT_HPP
sl@0
    17
#define BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
sl@0
    18
sl@0
    19
#include <iostream>
sl@0
    20
#include <algorithm>     // std::copy
sl@0
    21
#include <cassert>
sl@0
    22
#include <boost/config.hpp>
sl@0
    23
#include <boost/limits.hpp>
sl@0
    24
#include <boost/static_assert.hpp>
sl@0
    25
#include <boost/cstdint.hpp>
sl@0
    26
#include <boost/random/linear_congruential.hpp>
sl@0
    27
sl@0
    28
namespace boost {
sl@0
    29
namespace random {
sl@0
    30
sl@0
    31
// Carter Bays and S.D. Durham 1979
sl@0
    32
template<class UniformRandomNumberGenerator, int k,
sl@0
    33
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
sl@0
    34
  typename UniformRandomNumberGenerator::result_type 
sl@0
    35
#else
sl@0
    36
  uint32_t
sl@0
    37
#endif
sl@0
    38
  val = 0>
sl@0
    39
class shuffle_output
sl@0
    40
{
sl@0
    41
public:
sl@0
    42
  typedef UniformRandomNumberGenerator base_type;
sl@0
    43
  typedef typename base_type::result_type result_type;
sl@0
    44
sl@0
    45
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
sl@0
    46
  BOOST_STATIC_CONSTANT(int, buffer_size = k);
sl@0
    47
sl@0
    48
  shuffle_output() : _rng() { init(); }
sl@0
    49
#if defined(BOOST_MSVC) && _MSC_VER < 1300
sl@0
    50
  // MSVC does not implicitly generate the copy constructor here
sl@0
    51
  shuffle_output(const shuffle_output & x)
sl@0
    52
    : _rng(x._rng), y(x.y) { std::copy(x.v, x.v+k, v); }
sl@0
    53
#endif
sl@0
    54
  template<class T>
sl@0
    55
  explicit shuffle_output(T seed) : _rng(seed) { init(); }
sl@0
    56
  explicit shuffle_output(const base_type & rng) : _rng(rng) { init(); }
sl@0
    57
  template<class It> shuffle_output(It& first, It last)
sl@0
    58
    : _rng(first, last) { init(); }
sl@0
    59
  void seed() { _rng.seed(); init(); }
sl@0
    60
  template<class T>
sl@0
    61
  void seed(T s) { _rng.seed(s); init(); }
sl@0
    62
  template<class It> void seed(It& first, It last)
sl@0
    63
  {
sl@0
    64
    _rng.seed(first, last);
sl@0
    65
    init();
sl@0
    66
  }
sl@0
    67
sl@0
    68
  const base_type& base() const { return _rng; }
sl@0
    69
sl@0
    70
  result_type operator()() {
sl@0
    71
    // calculating the range every time may seem wasteful.  However, this
sl@0
    72
    // makes the information locally available for the optimizer.
sl@0
    73
    result_type range = (max)()-(min)()+1;
sl@0
    74
    int j = k*(y-(min)())/range;
sl@0
    75
    // assert(0 <= j && j < k);
sl@0
    76
    y = v[j];
sl@0
    77
    v[j] = _rng();
sl@0
    78
    return y;
sl@0
    79
  }
sl@0
    80
sl@0
    81
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
sl@0
    82
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
sl@0
    83
  static bool validation(result_type x) { return val == x; }
sl@0
    84
sl@0
    85
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
sl@0
    86
sl@0
    87
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
sl@0
    88
  template<class CharT, class Traits>
sl@0
    89
  friend std::basic_ostream<CharT,Traits>&
sl@0
    90
  operator<<(std::basic_ostream<CharT,Traits>& os, const shuffle_output& s)
sl@0
    91
  {
sl@0
    92
    os << s._rng << " " << s.y << " ";
sl@0
    93
    for(int i = 0; i < s.buffer_size; ++i)
sl@0
    94
      os << s.v[i] << " ";
sl@0
    95
    return os;
sl@0
    96
  }
sl@0
    97
sl@0
    98
  template<class CharT, class Traits>
sl@0
    99
  friend std::basic_istream<CharT,Traits>&
sl@0
   100
  operator>>(std::basic_istream<CharT,Traits>& is, shuffle_output& s)
sl@0
   101
  {
sl@0
   102
    is >> s._rng >> std::ws >> s.y >> std::ws;
sl@0
   103
    for(int i = 0; i < s.buffer_size; ++i)
sl@0
   104
      is >> s.v[i] >> std::ws;
sl@0
   105
    return is;
sl@0
   106
  }
sl@0
   107
#endif
sl@0
   108
sl@0
   109
  friend bool operator==(const shuffle_output& x, const shuffle_output& y)
sl@0
   110
  { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
sl@0
   111
  friend bool operator!=(const shuffle_output& x, const shuffle_output& y)
sl@0
   112
  { return !(x == y); }
sl@0
   113
#else
sl@0
   114
  // Use a member function; Streamable concept not supported.
sl@0
   115
  bool operator==(const shuffle_output& rhs) const
sl@0
   116
  { return _rng == rhs._rng && y == rhs.y && std::equal(v, v+k, rhs.v); }
sl@0
   117
  bool operator!=(const shuffle_output& rhs) const
sl@0
   118
  { return !(*this == rhs); }
sl@0
   119
#endif
sl@0
   120
private:
sl@0
   121
  void init()
sl@0
   122
  {
sl@0
   123
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
   124
    BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
sl@0
   125
#endif
sl@0
   126
    result_type range = (max)()-(min)();
sl@0
   127
    assert(range > 0);      // otherwise there would be little choice
sl@0
   128
    if(static_cast<unsigned long>(k * range) < 
sl@0
   129
       static_cast<unsigned long>(range))  // not a sufficient condition
sl@0
   130
      // likely overflow with bucket number computation
sl@0
   131
      assert(!"overflow will occur");
sl@0
   132
sl@0
   133
    // we cannot use std::generate, because it uses pass-by-value for _rng
sl@0
   134
    for(result_type * p = v; p != v+k; ++p)
sl@0
   135
      *p = _rng();
sl@0
   136
    y = _rng();
sl@0
   137
  }
sl@0
   138
sl@0
   139
  base_type _rng;
sl@0
   140
  result_type v[k];
sl@0
   141
  result_type y;
sl@0
   142
};
sl@0
   143
sl@0
   144
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
sl@0
   145
//  A definition is required even for integral static constants
sl@0
   146
template<class UniformRandomNumberGenerator, int k, 
sl@0
   147
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
sl@0
   148
  typename UniformRandomNumberGenerator::result_type 
sl@0
   149
#else
sl@0
   150
  uint32_t
sl@0
   151
#endif
sl@0
   152
  val>
sl@0
   153
const bool shuffle_output<UniformRandomNumberGenerator, k, val>::has_fixed_range;
sl@0
   154
sl@0
   155
template<class UniformRandomNumberGenerator, int k, 
sl@0
   156
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
sl@0
   157
  typename UniformRandomNumberGenerator::result_type 
sl@0
   158
#else
sl@0
   159
  uint32_t
sl@0
   160
#endif
sl@0
   161
  val>
sl@0
   162
const int shuffle_output<UniformRandomNumberGenerator, k, val>::buffer_size;
sl@0
   163
#endif
sl@0
   164
sl@0
   165
} // namespace random
sl@0
   166
sl@0
   167
// validation by experiment from Harry Erwin's generator.h (private e-mail)
sl@0
   168
typedef random::shuffle_output<
sl@0
   169
    random::linear_congruential<uint32_t, 1366, 150889, 714025, 0>,
sl@0
   170
  97, 139726> kreutzer1986;
sl@0
   171
sl@0
   172
sl@0
   173
} // namespace boost
sl@0
   174
sl@0
   175
#endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP