1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add typed_rand

This commit is contained in:
bolero-MURAKAMI 2015-05-17 18:35:53 +09:00
parent f934939acc
commit 8ee04c6048
4 changed files with 284 additions and 291 deletions

View file

@ -11,9 +11,9 @@
#include <sprout/config.hpp>
#include <sprout/stateful/counter.hpp>
#include <sprout/stateful/rand.hpp>
#include <sprout/stateful/slot.hpp>
#include <sprout/stateful/typed_id.hpp>
#include <sprout/stateful/typed_slot.hpp>
#include <sprout/stateful/typed_counter.hpp>
#include <sprout/stateful/typed_rand.hpp>
#include <sprout/stateful/typed_slot.hpp>
#endif // #ifndef SPROUT_STATEFUL_HPP

View file

@ -1,181 +0,0 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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_STATEFUL_SLOT_HPP
#define SPROUT_STATEFUL_SLOT_HPP
#include <cstdint>
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/assert.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace slot_detail {
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wnon-template-friend"
#endif
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wundefined-inline"
#endif
template<int N>
struct tag {
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<N>);
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<N>);
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<N>);
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<N>, sprout::integral_constant<int, K>);
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#endif
template<int N, int Key, std::intmax_t Value>
struct state
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<N>) {
return N;
}
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<N>) {
return Key;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<N>) {
return Value;
}
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<N>, sprout::integral_constant<int, K>) {
return get<K>();
}
template<int K>
static SPROUT_CONSTEXPR std::intmax_t get() {
return K == Key ? Value
: state<
N - 1,
adl_key(sprout::slot_detail::tag<N - 1>()),
adl_value(sprout::slot_detail::tag<N - 1>())
>::template get<K>()
;
}
};
template<int Key, std::intmax_t Value>
struct state<0, Key, Value>
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::slot_detail::tag<0>) {
return 0;
}
friend SPROUT_CONSTEXPR int adl_key(sprout::slot_detail::tag<0>) {
return Key;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::slot_detail::tag<0>) {
return Value;
}
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::slot_detail::tag<0>, sprout::integral_constant<int, K>) {
return get<K>();
}
template<int K>
static SPROUT_CONSTEXPR std::intmax_t get() {
return SPROUT_ASSERT(K == Key), Value;
}
};
template<int N, int = adl_counter(sprout::slot_detail::tag<N>())>
SPROUT_CONSTEXPR bool check(int, sprout::slot_detail::tag<N>) {
return true;
}
template<int N>
SPROUT_CONSTEXPR bool check(long, sprout::slot_detail::tag<N>) {
return false;
}
template<int N>
SPROUT_CONSTEXPR bool check(bool R = sprout::slot_detail::check(0, sprout::slot_detail::tag<N>())) {
return R;
}
template<int N>
SPROUT_CONSTEXPR int counter(sprout::false_type, sprout::slot_detail::tag<N>) {
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter(
sprout::true_type, sprout::slot_detail::tag<N>,
int R = !sprout::slot_detail::check(0, sprout::slot_detail::tag<N>()) ? N
: counter(sprout::bool_constant<sprout::slot_detail::check(0, sprout::slot_detail::tag<N>())>(), sprout::slot_detail::tag<N + 1>())
)
{
return R;
}
template<int N = 0>
SPROUT_CONSTEXPR int counter(int R = sprout::slot_detail::counter(sprout::true_type(), sprout::slot_detail::tag<N>())) {
return R;
}
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(sprout::false_type, sprout::slot_detail::tag<N>) {
return 0;
}
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(
sprout::true_type, sprout::slot_detail::tag<N>,
std::intmax_t R = !sprout::slot_detail::check<N>() ? adl_get(sprout::slot_detail::tag<N - 1>(), sprout::integral_constant<int, K>())
: get_impl<K>(sprout::bool_constant<sprout::slot_detail::check<N>()>(), sprout::slot_detail::tag<N + 1>())
)
{
return R;
}
template<int K, int N = 0>
SPROUT_CONSTEXPR std::intmax_t get(int R = sprout::slot_detail::get_impl<K>(sprout::true_type(), sprout::slot_detail::tag<N>())) {
return R;
}
} // namespace slot_detail
//
// slot
//
template<
int K,
std::intmax_t R = sprout::slot_detail::get<K>()
>
SPROUT_CONSTEXPR std::intmax_t slot() {
return R;
}
//
// assign_slot
// assign_slot_return
//
template<
int K, std::intmax_t Value,
int N = 0,
std::intmax_t = sprout::slot_detail::state<
sprout::slot_detail::counter(sprout::true_type(), sprout::slot_detail::tag<N>()),
K, Value
>::value
>
SPROUT_CXX14_CONSTEXPR void assign_slot() {}
template<
int K, std::intmax_t Value,
int N = 0,
std::intmax_t R = sprout::slot_detail::state<
sprout::slot_detail::counter(sprout::true_type(), sprout::slot_detail::tag<N>()),
K, Value
>::value
>
SPROUT_CONSTEXPR std::intmax_t assign_slot_return() {
return R;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_STATEFUL_SLOT_HPP

View file

@ -0,0 +1,213 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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_STATEFUL_TYPED_RAND_HPP
#define SPROUT_STATEFUL_TYPED_RAND_HPP
#include <cstdlib>
#include <sprout/config.hpp>
#include <sprout/utility/as_const.hpp>
#include <sprout/random/unique_seed.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/stateful/rand.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace typed_rand_detail {
template<typename T>
struct typed {
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wnon-template-friend"
#endif
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wundefined-inline"
#endif
template<int N>
struct tag {
friend SPROUT_CONSTEXPR int adl_counter(tag<N>);
friend SPROUT_CONSTEXPR bool adl_is_srand(tag<N>);
friend SPROUT_CONSTEXPR unsigned adl_seed(tag<N>);
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
#endif
template<int N, bool IsSrand = false, unsigned Seed = 0>
struct state {
friend SPROUT_CONSTEXPR int adl_counter(tag<N>) {
return N;
}
friend SPROUT_CONSTEXPR bool adl_is_srand(tag<N>) {
return IsSrand;
}
friend SPROUT_CONSTEXPR unsigned adl_seed(tag<N>) {
return Seed;
}
// generate a next random number
SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER((
!IsSrand
? state<
N - 1,
adl_is_srand(tag<N - 1>()),
adl_seed(tag<N - 1>())
>::result()
: sprout::detail::rand_result_type(
Seed,
sprout::detail::rand_generator_type(Seed),
sprout::detail::rand_distribution_type(0, RAND_MAX)
)
))
;
SPROUT_STATIC_CONSTEXPR int value = result;
};
template<bool IsSrand, unsigned Seed>
struct state<0, IsSrand, Seed> {
friend SPROUT_CONSTEXPR int adl_counter(tag<0>) {
return 0;
}
friend SPROUT_CONSTEXPR bool adl_is_srand(tag<0>) {
return IsSrand;
}
friend SPROUT_CONSTEXPR unsigned adl_seed(tag<0>) {
return Seed;
}
// generate a first random number between [0, RAND_MAX]
SPROUT_STATIC_CONSTEXPR sprout::detail::rand_result_type result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_INNER((
!IsSrand
? sprout::detail::rand_distribution_type(0, RAND_MAX)
(sprout::as_const(sprout::detail::rand_generator_type(!IsSrand ? sprout::detail::rand_default_seed : Seed)))
: sprout::detail::rand_result_type(
Seed,
sprout::detail::rand_generator_type(Seed),
sprout::detail::rand_distribution_type(0, RAND_MAX)
)
))
;
static SPROUT_CONSTEXPR int value = result;
};
template<int N, int = adl_counter(tag<N>())>
static SPROUT_CONSTEXPR bool check(int, tag<N>) {
return true;
}
template<int N>
static SPROUT_CONSTEXPR bool check(long, tag<N>) {
return false;
}
template<int N>
static SPROUT_CONSTEXPR bool check(bool R = check(0, tag<N>())) {
return R;
}
template<int N>
static SPROUT_CONSTEXPR int counter(sprout::false_type, tag<N>) {
return 0;
}
template<int N>
static SPROUT_CONSTEXPR int counter(
sprout::true_type, tag<N>,
int R = !check<N>() ? N
: counter(sprout::bool_constant<check<N>()>(), tag<N + 1>())
)
{
return R;
}
template<int N = 0>
static SPROUT_CONSTEXPR int counter(int R = counter(sprout::true_type(), tag<N>())) {
return R;
}
};
template<typename T>
template<int N, bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::typed_rand_detail::typed<T>::state<N, IsSrand, Seed>::result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER((
!IsSrand
? state<
N - 1,
adl_is_srand(tag<N - 1>()),
adl_seed(tag<N - 1>())
>::result()
: sprout::detail::rand_result_type(
0,
sprout::detail::rand_generator_type(Seed),
sprout::detail::rand_distribution_type(0, RAND_MAX)
)
))
;
template<typename T>
template<int N, bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST int sprout::typed_rand_detail::typed<T>::state<N, IsSrand, Seed>::value;
template<typename T>
template<bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST sprout::detail::rand_result_type sprout::typed_rand_detail::typed<T>::state<0, IsSrand, Seed>::result
SPROUT_STATIC_CONSTEXPR_DATA_MEMBER_OUTER(
!IsSrand
? sprout::detail::rand_distribution_type(0, RAND_MAX)
(sprout::as_const(sprout::detail::rand_generator_type(!IsSrand ? sprout::detail::rand_default_seed : Seed)))
: sprout::detail::rand_result_type(
Seed,
sprout::detail::rand_generator_type(Seed),
sprout::detail::rand_distribution_type(0, RAND_MAX)
)
)
;
template<typename T>
template<bool IsSrand, unsigned Seed>
SPROUT_CONSTEXPR_OR_CONST int sprout::typed_rand_detail::typed<T>::state<0, IsSrand, Seed>::value;
} // namespace typed_rand_detail
//
// rand
//
template<
typename T,
int N = 1,
int R = sprout::typed_rand_detail::typed<T>::template state<
sprout::typed_rand_detail::typed<T>::template counter(sprout::true_type(), typename sprout::typed_rand_detail::typed<T>::template tag<N - 1>()) + N - 1
>::value
>
SPROUT_CONSTEXPR int rand() {
return R;
}
//
// srand
// srand_return
//
template<
typename T, unsigned Seed,
int N = 1,
int = sprout::typed_rand_detail::typed<T>::template state<
sprout::typed_rand_detail::typed<T>::template counter(sprout::true_type(), typename sprout::typed_rand_detail::typed<T>::template tag<N - 1>()) + N - 1,
true, Seed
>::value
>
SPROUT_CXX14_CONSTEXPR void srand() {}
template<
typename T, unsigned Seed,
int N = 1,
int R = sprout::typed_rand_detail::typed<T>::template state<
sprout::typed_rand_detail::typed<T>::template counter(sprout::true_type(), typename sprout::typed_rand_detail::typed<T>::template tag<N - 1>()) + N - 1,
true, Seed
>::value
>
SPROUT_CONSTEXPR unsigned srand_return() {
return R;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_STATEFUL_TYPED_RAND_HPP

View file

@ -11,13 +11,13 @@
#include <cstdint>
#include <sprout/config.hpp>
#include <sprout/type_traits/integral_constant.hpp>
#include <sprout/assert.hpp>
#include <sprout/stateful/typed_id.hpp>
namespace sprout {
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR
namespace typed_slot_detail {
template<typename T>
struct typed {
#if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wnon-template-friend"
@ -26,14 +26,11 @@ namespace sprout {
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wundefined-inline"
#endif
template<int N>
struct tag {
friend SPROUT_CONSTEXPR int adl_counter(sprout::typed_slot_detail::tag<N>);
friend SPROUT_CONSTEXPR int adl_key(sprout::typed_slot_detail::tag<N>);
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::typed_slot_detail::tag<N>);
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::typed_slot_detail::tag<N>, sprout::integral_constant<int, K>);
};
template<int N>
struct tag {
friend SPROUT_CONSTEXPR int adl_counter(tag<N>);
friend SPROUT_CONSTEXPR std::intmax_t adl_value(tag<N>);
};
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
@ -41,135 +38,99 @@ namespace sprout {
# pragma GCC diagnostic pop
#endif
template<int N, int Key, std::intmax_t Value>
struct state
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::typed_slot_detail::tag<N>) {
return N;
template<int N, std::intmax_t Value>
struct state
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(tag<N>) {
return N;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(tag<N>) {
return Value;
}
};
template<int N, int = adl_counter(tag<N>())>
static SPROUT_CONSTEXPR bool check(int, tag<N>) {
return true;
}
friend SPROUT_CONSTEXPR int adl_key(sprout::typed_slot_detail::tag<N>) {
return Key;
template<int N>
static SPROUT_CONSTEXPR bool check(long, tag<N>) {
return false;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::typed_slot_detail::tag<N>) {
return Value;
template<int N>
static SPROUT_CONSTEXPR bool check(bool R = check(0, tag<N>())) {
return R;
}
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::typed_slot_detail::tag<N>, sprout::integral_constant<int, K>) {
return get<K>();
}
template<int K>
static SPROUT_CONSTEXPR std::intmax_t get() {
return K == Key ? Value
: state<
N - 1,
adl_key(sprout::typed_slot_detail::tag<N - 1>()),
adl_value(sprout::typed_slot_detail::tag<N - 1>())
>::template get<K>()
;
}
};
template<int Key, std::intmax_t Value>
struct state<0, Key, Value>
: public sprout::integral_constant<std::intmax_t, Value>
{
friend SPROUT_CONSTEXPR int adl_counter(sprout::typed_slot_detail::tag<0>) {
template<int N>
static SPROUT_CONSTEXPR int counter(sprout::false_type, tag<N>) {
return 0;
}
friend SPROUT_CONSTEXPR int adl_key(sprout::typed_slot_detail::tag<0>) {
return Key;
template<int N>
static SPROUT_CONSTEXPR int counter(
sprout::true_type, tag<N>,
int R = !check<N>() ? N
: counter(sprout::bool_constant<check<N>()>(), tag<N + 1>())
)
{
return R;
}
friend SPROUT_CONSTEXPR std::intmax_t adl_value(sprout::typed_slot_detail::tag<0>) {
return Value;
template<int N = 0>
static SPROUT_CONSTEXPR int counter(int R = counter(sprout::true_type(), tag<N>())) {
return R;
}
template<int K>
friend SPROUT_CONSTEXPR std::intmax_t adl_get(sprout::typed_slot_detail::tag<0>, sprout::integral_constant<int, K>) {
return get<K>();
template<int N>
static SPROUT_CONSTEXPR std::intmax_t val(sprout::false_type, tag<N>) {
return 0;
}
template<int K>
static SPROUT_CONSTEXPR std::intmax_t get() {
return SPROUT_ASSERT(K == Key), Value;
template<int N>
static SPROUT_CONSTEXPR std::intmax_t val(
sprout::true_type, tag<N>,
std::intmax_t R = !check<N>() ? adl_value(tag<N - 1>())
: val(sprout::bool_constant<check<N>()>(), tag<N + 1>())
)
{
return R;
}
template<int N = 1>
static SPROUT_CONSTEXPR std::intmax_t val(std::intmax_t R = val(sprout::true_type(), tag<N>())) {
return R;
}
};
template<int N, int = adl_counter(sprout::typed_slot_detail::tag<N>())>
SPROUT_CONSTEXPR bool check(int, sprout::typed_slot_detail::tag<N>) {
return true;
}
template<int N>
SPROUT_CONSTEXPR bool check(long, sprout::typed_slot_detail::tag<N>) {
return false;
}
template<int N>
SPROUT_CONSTEXPR bool check(bool R = sprout::typed_slot_detail::check(0, sprout::typed_slot_detail::tag<N>())) {
return R;
}
template<int N>
SPROUT_CONSTEXPR int counter(sprout::false_type, sprout::typed_slot_detail::tag<N>) {
return 0;
}
template<int N>
SPROUT_CONSTEXPR int counter(
sprout::true_type, sprout::typed_slot_detail::tag<N>,
int R = !sprout::typed_slot_detail::check(0, sprout::typed_slot_detail::tag<N>()) ? N
: counter(sprout::bool_constant<sprout::typed_slot_detail::check(0, sprout::typed_slot_detail::tag<N>())>(), sprout::typed_slot_detail::tag<N + 1>())
)
{
return R;
}
template<int N = 0>
SPROUT_CONSTEXPR int counter(int R = sprout::typed_slot_detail::counter(sprout::true_type(), sprout::typed_slot_detail::tag<N>())) {
return R;
}
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(sprout::false_type, sprout::typed_slot_detail::tag<N>) {
return 0;
}
template<int K, int N>
SPROUT_CONSTEXPR std::intmax_t get_impl(
sprout::true_type, sprout::typed_slot_detail::tag<N>,
std::intmax_t R = !sprout::typed_slot_detail::check(0, sprout::typed_slot_detail::tag<N>()) ? adl_get(sprout::typed_slot_detail::tag<N - 1>(), sprout::integral_constant<int, K>())
: get_impl<K>(sprout::bool_constant<sprout::typed_slot_detail::check(0, sprout::typed_slot_detail::tag<N>())>(), sprout::typed_slot_detail::tag<N + 1>())
)
{
return R;
}
template<int K, int N = 0>
SPROUT_CONSTEXPR std::intmax_t get(int R = sprout::typed_slot_detail::get_impl<K>(sprout::true_type(), sprout::typed_slot_detail::tag<N>())) {
return R;
}
} // namespace typed_slot_detail
//
// slot
//
template<
typename T,
std::intmax_t R = sprout::typed_slot_detail::get<sprout::typed_id<T>::value>()
int N = 1,
std::intmax_t R = sprout::typed_slot_detail::typed<T>::template val(sprout::true_type(), typename sprout::typed_slot_detail::typed<T>::template tag<N>())
>
SPROUT_CONSTEXPR std::intmax_t slot() {
return R;
}
//
// assign_slot
// assign_slot_return
//
template<
typename T, std::intmax_t Value,
int N = 0,
std::intmax_t = sprout::typed_slot_detail::state<
sprout::typed_slot_detail::counter(sprout::true_type(), sprout::typed_slot_detail::tag<N>()),
sprout::typed_id<T>::value, Value
int N = 1,
std::intmax_t = sprout::typed_slot_detail::typed<T>::template state<
sprout::typed_slot_detail::typed<T>::template counter(sprout::true_type(), typename sprout::typed_slot_detail::typed<T>::template tag<N - 1>()) + N - 1,
Value
>::value
>
SPROUT_CXX14_CONSTEXPR void assign_slot() {}
template<
typename T, std::intmax_t Value,
int N = 0,
std::intmax_t R = sprout::typed_slot_detail::state<
sprout::typed_slot_detail::counter(sprout::true_type(), sprout::typed_slot_detail::tag<N>()),
sprout::typed_id<T>::value, Value
int N = 1,
std::intmax_t R = sprout::typed_slot_detail::typed<T>::template state<
sprout::typed_slot_detail::typed<T>::template counter(sprout::true_type(), typename sprout::typed_slot_detail::typed<T>::template tag<N - 1>()) + N - 1,
Value
>::value
>
SPROUT_CONSTEXPR std::intmax_t assign_slot_return() {