2014-01-11 07:05:18 +00:00
|
|
|
/*=============================================================================
|
2015-01-10 10:13:57 +00:00
|
|
|
Copyright (c) 2011-2015 Bolero MURAKAMI
|
2014-01-11 07:05:18 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
|
|
|
#ifndef SPROUT_RANDOM_GENERATE_ARRAY_HPP
|
|
|
|
#define SPROUT_RANDOM_GENERATE_ARRAY_HPP
|
|
|
|
|
2014-12-27 04:56:41 +00:00
|
|
|
#include <type_traits>
|
2014-01-11 07:05:18 +00:00
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2014-01-11 07:05:18 +00:00
|
|
|
#include <sprout/array/array.hpp>
|
2014-12-27 04:56:41 +00:00
|
|
|
#include <sprout/utility/forward.hpp>
|
2014-01-11 07:05:18 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2014-01-17 13:09:08 +00:00
|
|
|
#include <sprout/random/results.hpp>
|
2014-01-14 15:06:38 +00:00
|
|
|
#include <sprout/generator/results.hpp>
|
2014-01-11 07:05:18 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace random {
|
|
|
|
//
|
|
|
|
// generate_array
|
|
|
|
//
|
2014-01-14 15:06:38 +00:00
|
|
|
template<std::size_t N, typename RandomNumberGenerator>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR sprout::array<typename RandomNumberGenerator::result_type, N>
|
|
|
|
generate_array(RandomNumberGenerator& rng) {
|
|
|
|
sprout::array<typename RandomNumberGenerator::result_type, N> result{{}};
|
2014-01-11 07:05:18 +00:00
|
|
|
for (std::size_t i = 0; i != N; ++i) {
|
|
|
|
result[i] = rng();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-12-27 04:56:41 +00:00
|
|
|
template<std::size_t N, typename RandomNumberGenerator, typename Distribution>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR sprout::array<typename std::remove_reference<Distribution>::type::result_type, N>
|
|
|
|
generate_array(RandomNumberGenerator& rng, Distribution&& dist) {
|
|
|
|
sprout::array<typename std::remove_reference<Distribution>::type::result_type, N> result{{}};
|
|
|
|
for (std::size_t i = 0; i != N; ++i) {
|
|
|
|
result[i] = SPROUT_FORWARD(Distribution, dist)(rng);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-01-11 07:05:18 +00:00
|
|
|
|
|
|
|
namespace detail {
|
2014-01-14 15:06:38 +00:00
|
|
|
template<std::size_t N, typename RandomNumberGenerator, typename Random, typename... Args>
|
2014-01-11 07:05:18 +00:00
|
|
|
static SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
(sizeof...(Args) + 1 == N),
|
2014-01-14 15:06:38 +00:00
|
|
|
sprout::pair<
|
|
|
|
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
|
|
|
|
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
|
|
|
|
> const
|
2014-01-11 07:05:18 +00:00
|
|
|
>::type generate_array_impl(Random const& rnd, Args const&... args) {
|
2014-01-14 15:06:38 +00:00
|
|
|
typedef sprout::pair<
|
|
|
|
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
|
|
|
|
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
|
|
|
|
> const pair_type;
|
2014-01-11 07:05:18 +00:00
|
|
|
return pair_type{
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::array<typename RandomNumberGenerator::result_type, N>{{args..., sprout::random::result(rnd)}},
|
|
|
|
sprout::random::next(rnd)
|
2014-01-11 07:05:18 +00:00
|
|
|
};
|
|
|
|
}
|
2014-01-14 15:06:38 +00:00
|
|
|
template<std::size_t N, typename RandomNumberGenerator, typename Random, typename... Args>
|
2014-01-11 07:05:18 +00:00
|
|
|
static SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
(sizeof...(Args) + 1 < N),
|
2014-01-14 15:06:38 +00:00
|
|
|
sprout::pair<
|
|
|
|
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
|
|
|
|
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
|
|
|
|
> const
|
2014-01-11 07:05:18 +00:00
|
|
|
>::type generate_array_impl(Random const& rnd, Args const&... args) {
|
2014-01-14 15:06:38 +00:00
|
|
|
return sprout::random::detail::generate_array_impl<N, RandomNumberGenerator>(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd)(),
|
|
|
|
args..., sprout::random::result(rnd)
|
2014-01-14 15:06:38 +00:00
|
|
|
);
|
2014-01-11 07:05:18 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// generate_array
|
|
|
|
//
|
2014-01-14 15:06:38 +00:00
|
|
|
template<std::size_t N, typename RandomNumberGenerator>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<
|
|
|
|
sprout::array<typename sprout::generators::results::generated_value<RandomNumberGenerator const>::type, N>,
|
|
|
|
typename sprout::generators::results::next_generator<RandomNumberGenerator const>::type
|
|
|
|
> const
|
|
|
|
generate_array(RandomNumberGenerator const& rng) {
|
|
|
|
return sprout::random::detail::generate_array_impl<N, RandomNumberGenerator>(rng());
|
2014-01-11 07:05:18 +00:00
|
|
|
}
|
|
|
|
} // namespace random
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_RANDOM_GENERATE_ARRAY_HPP
|