epoc32/include/stdapis/boost/random/shuffle_output.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
/* boost random/shuffle_output.hpp header file
williamr@2
     2
 *
williamr@2
     3
 * Copyright Jens Maurer 2000-2001
williamr@2
     4
 * Distributed under the Boost Software License, Version 1.0. (See
williamr@2
     5
 * accompanying file LICENSE_1_0.txt or copy at
williamr@2
     6
 * http://www.boost.org/LICENSE_1_0.txt)
williamr@2
     7
 *
williamr@2
     8
 * See http://www.boost.org for most recent version including documentation.
williamr@2
     9
 *
williamr@2
    10
 * $Id: shuffle_output.hpp,v 1.10 2005/08/25 16:27:22 johnmaddock Exp $
williamr@2
    11
 *
williamr@2
    12
 * Revision history
williamr@2
    13
 *  2001-02-18  moved to individual header files
williamr@2
    14
 */
williamr@2
    15
williamr@2
    16
#ifndef BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
williamr@2
    17
#define BOOST_RANDOM_SHUFFLE_OUTPUT_HPP
williamr@2
    18
williamr@2
    19
#include <iostream>
williamr@2
    20
#include <algorithm>     // std::copy
williamr@2
    21
#include <cassert>
williamr@2
    22
#include <boost/config.hpp>
williamr@2
    23
#include <boost/limits.hpp>
williamr@2
    24
#include <boost/static_assert.hpp>
williamr@2
    25
#include <boost/cstdint.hpp>
williamr@2
    26
#include <boost/random/linear_congruential.hpp>
williamr@2
    27
williamr@2
    28
namespace boost {
williamr@2
    29
namespace random {
williamr@2
    30
williamr@2
    31
// Carter Bays and S.D. Durham 1979
williamr@2
    32
template<class UniformRandomNumberGenerator, int k,
williamr@2
    33
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
williamr@2
    34
  typename UniformRandomNumberGenerator::result_type 
williamr@2
    35
#else
williamr@2
    36
  uint32_t
williamr@2
    37
#endif
williamr@2
    38
  val = 0>
williamr@2
    39
class shuffle_output
williamr@2
    40
{
williamr@2
    41
public:
williamr@2
    42
  typedef UniformRandomNumberGenerator base_type;
williamr@2
    43
  typedef typename base_type::result_type result_type;
williamr@2
    44
williamr@2
    45
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
williamr@2
    46
  BOOST_STATIC_CONSTANT(int, buffer_size = k);
williamr@2
    47
williamr@2
    48
  shuffle_output() : _rng() { init(); }
williamr@2
    49
#if defined(BOOST_MSVC) && _MSC_VER < 1300
williamr@2
    50
  // MSVC does not implicitly generate the copy constructor here
williamr@2
    51
  shuffle_output(const shuffle_output & x)
williamr@2
    52
    : _rng(x._rng), y(x.y) { std::copy(x.v, x.v+k, v); }
williamr@2
    53
#endif
williamr@2
    54
  template<class T>
williamr@2
    55
  explicit shuffle_output(T seed) : _rng(seed) { init(); }
williamr@2
    56
  explicit shuffle_output(const base_type & rng) : _rng(rng) { init(); }
williamr@2
    57
  template<class It> shuffle_output(It& first, It last)
williamr@2
    58
    : _rng(first, last) { init(); }
williamr@2
    59
  void seed() { _rng.seed(); init(); }
williamr@2
    60
  template<class T>
williamr@2
    61
  void seed(T s) { _rng.seed(s); init(); }
williamr@2
    62
  template<class It> void seed(It& first, It last)
williamr@2
    63
  {
williamr@2
    64
    _rng.seed(first, last);
williamr@2
    65
    init();
williamr@2
    66
  }
williamr@2
    67
williamr@2
    68
  const base_type& base() const { return _rng; }
williamr@2
    69
williamr@2
    70
  result_type operator()() {
williamr@2
    71
    // calculating the range every time may seem wasteful.  However, this
williamr@2
    72
    // makes the information locally available for the optimizer.
williamr@2
    73
    result_type range = (max)()-(min)()+1;
williamr@2
    74
    int j = k*(y-(min)())/range;
williamr@2
    75
    // assert(0 <= j && j < k);
williamr@2
    76
    y = v[j];
williamr@2
    77
    v[j] = _rng();
williamr@2
    78
    return y;
williamr@2
    79
  }
williamr@2
    80
williamr@2
    81
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.min)(); }
williamr@2
    82
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const { return (_rng.max)(); }
williamr@2
    83
  static bool validation(result_type x) { return val == x; }
williamr@2
    84
williamr@2
    85
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2
    86
williamr@2
    87
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2
    88
  template<class CharT, class Traits>
williamr@2
    89
  friend std::basic_ostream<CharT,Traits>&
williamr@2
    90
  operator<<(std::basic_ostream<CharT,Traits>& os, const shuffle_output& s)
williamr@2
    91
  {
williamr@2
    92
    os << s._rng << " " << s.y << " ";
williamr@2
    93
    for(int i = 0; i < s.buffer_size; ++i)
williamr@2
    94
      os << s.v[i] << " ";
williamr@2
    95
    return os;
williamr@2
    96
  }
williamr@2
    97
williamr@2
    98
  template<class CharT, class Traits>
williamr@2
    99
  friend std::basic_istream<CharT,Traits>&
williamr@2
   100
  operator>>(std::basic_istream<CharT,Traits>& is, shuffle_output& s)
williamr@2
   101
  {
williamr@2
   102
    is >> s._rng >> std::ws >> s.y >> std::ws;
williamr@2
   103
    for(int i = 0; i < s.buffer_size; ++i)
williamr@2
   104
      is >> s.v[i] >> std::ws;
williamr@2
   105
    return is;
williamr@2
   106
  }
williamr@2
   107
#endif
williamr@2
   108
williamr@2
   109
  friend bool operator==(const shuffle_output& x, const shuffle_output& y)
williamr@2
   110
  { return x._rng == y._rng && x.y == y.y && std::equal(x.v, x.v+k, y.v); }
williamr@2
   111
  friend bool operator!=(const shuffle_output& x, const shuffle_output& y)
williamr@2
   112
  { return !(x == y); }
williamr@2
   113
#else
williamr@2
   114
  // Use a member function; Streamable concept not supported.
williamr@2
   115
  bool operator==(const shuffle_output& rhs) const
williamr@2
   116
  { return _rng == rhs._rng && y == rhs.y && std::equal(v, v+k, rhs.v); }
williamr@2
   117
  bool operator!=(const shuffle_output& rhs) const
williamr@2
   118
  { return !(*this == rhs); }
williamr@2
   119
#endif
williamr@2
   120
private:
williamr@2
   121
  void init()
williamr@2
   122
  {
williamr@2
   123
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
williamr@2
   124
    BOOST_STATIC_ASSERT(std::numeric_limits<result_type>::is_integer);
williamr@2
   125
#endif
williamr@2
   126
    result_type range = (max)()-(min)();
williamr@2
   127
    assert(range > 0);      // otherwise there would be little choice
williamr@2
   128
    if(static_cast<unsigned long>(k * range) < 
williamr@2
   129
       static_cast<unsigned long>(range))  // not a sufficient condition
williamr@2
   130
      // likely overflow with bucket number computation
williamr@2
   131
      assert(!"overflow will occur");
williamr@2
   132
williamr@2
   133
    // we cannot use std::generate, because it uses pass-by-value for _rng
williamr@2
   134
    for(result_type * p = v; p != v+k; ++p)
williamr@2
   135
      *p = _rng();
williamr@2
   136
    y = _rng();
williamr@2
   137
  }
williamr@2
   138
williamr@2
   139
  base_type _rng;
williamr@2
   140
  result_type v[k];
williamr@2
   141
  result_type y;
williamr@2
   142
};
williamr@2
   143
williamr@2
   144
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2
   145
//  A definition is required even for integral static constants
williamr@2
   146
template<class UniformRandomNumberGenerator, int k, 
williamr@2
   147
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
williamr@2
   148
  typename UniformRandomNumberGenerator::result_type 
williamr@2
   149
#else
williamr@2
   150
  uint32_t
williamr@2
   151
#endif
williamr@2
   152
  val>
williamr@2
   153
const bool shuffle_output<UniformRandomNumberGenerator, k, val>::has_fixed_range;
williamr@2
   154
williamr@2
   155
template<class UniformRandomNumberGenerator, int k, 
williamr@2
   156
#ifndef BOOST_NO_DEPENDENT_TYPES_IN_TEMPLATE_VALUE_PARAMETERS
williamr@2
   157
  typename UniformRandomNumberGenerator::result_type 
williamr@2
   158
#else
williamr@2
   159
  uint32_t
williamr@2
   160
#endif
williamr@2
   161
  val>
williamr@2
   162
const int shuffle_output<UniformRandomNumberGenerator, k, val>::buffer_size;
williamr@2
   163
#endif
williamr@2
   164
williamr@2
   165
} // namespace random
williamr@2
   166
williamr@2
   167
// validation by experiment from Harry Erwin's generator.h (private e-mail)
williamr@2
   168
typedef random::shuffle_output<
williamr@2
   169
    random::linear_congruential<uint32_t, 1366, 150889, 714025, 0>,
williamr@2
   170
  97, 139726> kreutzer1986;
williamr@2
   171
williamr@2
   172
williamr@2
   173
} // namespace boost
williamr@2
   174
williamr@2
   175
#endif // BOOST_RANDOM_SHUFFLE_OUTPUT_HPP