Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/boost/random/lagged_fibonacci.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ class lagged_fibonacci_engine
*/
BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, lagged_fibonacci_engine, f)
{
is >> f.i >> std::ws;
is >> f.i;
for(unsigned int j = 0; j < f.long_lag; ++j)
is >> f.x[j] >> std::ws;
is >> std::ws >> f.x[j];
return is;
}

Expand Down
51 changes: 17 additions & 34 deletions include/boost/random/mersenne_twister.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <boost/random/detail/ptr_helper.hpp>
#include <boost/random/detail/seed.hpp>
#include <boost/random/detail/seed_impl.hpp>
#include <boost/random/detail/operators.hpp>
#include <boost/random/detail/generator_seed_seq.hpp>
#include <boost/random/detail/polynomial.hpp>

Expand Down Expand Up @@ -216,26 +217,28 @@ class mersenne_twister_engine

#ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
/** Writes a mersenne_twister_engine to a @c std::ostream */
template<class CharT, class Traits>
friend std::basic_ostream<CharT,Traits>&
operator<<(std::basic_ostream<CharT,Traits>& os,
const mersenne_twister_engine& mt)
BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, mersenne_twister_engine, mt)
{
mt.print(os);
UIntType data[mt.state_size];
for (std::size_t j = 0; j < mt.i; ++j) {
data[j + mt.state_size - mt.i] = mt.x[j];
}
if (mt.i != mt.state_size) {
mt.rewind(&data[mt.state_size - mt.i - 1], mt.state_size - mt.i);
}
os << data[0];
for (std::size_t j = 1; j < mt.state_size; ++j) {
os << ' ' << data[j];
}
return os;
}

/** Reads a mersenne_twister_engine from a @c std::istream */
template<class CharT, class Traits>
friend std::basic_istream<CharT,Traits>&
operator>>(std::basic_istream<CharT,Traits>& is,
mersenne_twister_engine& mt)
BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, mersenne_twister_engine, mt)
{
for(std::size_t j = 0; j < mt.state_size; ++j)
is >> mt.x[j] >> std::ws;
// MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template
// value parameter "n" available from the class template scope, so use
// the static constant with the same value
is >> mt.x[0];
for(std::size_t j = 1; j < mt.state_size; ++j)
is >> std::ws >> mt.x[j];
mt.i = mt.state_size;
return is;
}
Expand Down Expand Up @@ -286,26 +289,6 @@ class mersenne_twister_engine
return true;
}

/**
* Does the work of operator<<. This is in a member function
* for portability.
*/
template<class CharT, class Traits>
void print(std::basic_ostream<CharT, Traits>& os) const
{
UIntType data[n];
for(std::size_t j = 0; j < i; ++j) {
data[j + n - i] = x[j];
}
if(i != n) {
rewind(&data[n - i - 1], n - i);
}
os << data[0];
for(std::size_t j = 1; j < n; ++j) {
os << ' ' << data[j];
}
}

/**
* Copies z elements of the state preceding x[0] into
* the array whose last element is last.
Expand Down
30 changes: 14 additions & 16 deletions include/boost/random/mixmax.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ class mixmax_engine{
template<class CharT, class Traits>
friend std::basic_ostream<CharT,Traits>&
operator<< (std::basic_ostream<CharT,Traits>& ost, const mixmax_engine& me){
ost << Ndim << " " << me.S.counter << " " << me.S.sumtot << " ";
for (int j=0; (j< (Ndim) ); j++) {
ost << (std::uint64_t)me.S.V[j] << " ";
ost << Ndim << " " << me.S.counter << " " << me.S.sumtot;
for (const std::uint64_t v: me.S.V) {
ost << " " << v;
}
ost << "\n";
ost.flush();
return ost;
}
}

/** read the state of the RNG from a stream */
template<class CharT, class Traits>
Expand All @@ -141,16 +139,16 @@ class mixmax_engine{
BOOST_ASSERT(counter==Ndim);
in >> counter >> std::ws;
in >> savedsum >> std::ws;
for(int j=0;j<Ndim;j++) {
in >> std::ws >> vec[j] ;
sum=me.MOD_MERSENNE(sum+vec[j]);
}
if (sum == savedsum && counter>0 && counter<Ndim){
me.S.V=vec; me.S.counter = counter; me.S.sumtot=savedsum;
}else{
in.setstate(std::ios::failbit);
}
return in;
for(std::uint64_t& v : vec) {
in >> std::ws >> v ;
sum=me.MOD_MERSENNE(sum+v);
}
if (sum == savedsum && counter>0 && counter<Ndim){
me.S.V=vec; me.S.counter = counter; me.S.sumtot=savedsum;
}else{
in.setstate(std::ios::failbit);
}
return in;
}

friend bool operator==(const mixmax_engine & x,
Expand Down
18 changes: 2 additions & 16 deletions include/boost/random/splitmix64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define BOOST_RANDOM_SPLITMIX64_HPP

#include <cstdint>
#include <cstdlib>
#include <limits>
#include <array>
#include <string>
Expand Down Expand Up @@ -142,28 +141,15 @@ class splitmix64
inline friend std::basic_ostream<CharT,Traits>& operator<<(std::basic_ostream<CharT,Traits>& ost,
const splitmix64& e)
{
ost << e.state_;
return ost;
return ost << e.state_;
}

/** Writes a @c splitmix64 to a @c std::istream. */
template <typename CharT, typename Traits>
inline friend std::basic_istream<CharT,Traits>& operator>>(std::basic_istream<CharT,Traits>& ist,
splitmix64& e)
{
std::string sstate;
CharT val;
while (ist >> val)
{
if (std::isdigit(val))
{
sstate.push_back(val);
}
}

e.state_ = std::strtoull(sstate.c_str(), nullptr, 10);

return ist;
return ist >> e.state_;
}

/** Fills a range with random values */
Expand Down
5 changes: 3 additions & 2 deletions test/test_generator.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ void do_test_streaming(const BOOST_RANDOM_URNG& urng)
{
BOOST_RANDOM_URNG urng2;
std::basic_ostringstream<CharT> output;
output << urng;
BOOST_TEST_REQUIRE(static_cast<bool>(output << urng));
BOOST_CHECK_NE(urng, urng2);
// restore old state
std::basic_istringstream<CharT> input(output.str());
input >> urng2;
BOOST_TEST(static_cast<bool>(input >> urng2));
BOOST_TEST(input.eof());
BOOST_CHECK_EQUAL(urng, urng2);
}

Expand Down
Loading