mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
改行コード統一
This commit is contained in:
parent
7abc568a53
commit
f3a7041250
29 changed files with 4034 additions and 4034 deletions
|
@ -1,132 +1,132 @@
|
|||
#ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename IntType, IntType m>
|
||||
class const_mod {
|
||||
private:
|
||||
typedef typename std::make_unsigned<IntType>::type unsigned_type;
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR IntType suppress_warnings = m == 0;
|
||||
SPROUT_STATIC_CONSTEXPR IntType modulus = m + suppress_warnings;
|
||||
private:
|
||||
static_assert(suppress_warnings == 0, "suppress_warnings == 0");
|
||||
static_assert(modulus == m, "modulus == m");
|
||||
private:
|
||||
static SPROUT_CONSTEXPR IntType pow_1(IntType a, std::uintmax_t exponent, IntType result = 1) {
|
||||
return exponent != 0
|
||||
? pow_1(mult(a, a), exponent / 2, exponent % 2 == 1 ? mult(result, a) : result)
|
||||
: result
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_small(IntType a, IntType x) {
|
||||
return a * x % (m + suppress_warnings);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_schrage_1(IntType a, IntType value, IntType q, IntType r) {
|
||||
return r < q
|
||||
? sub(a * (value % q), r * (value / q))
|
||||
: throw "assert(r < q)"
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_schrage(IntType a, IntType value) {
|
||||
return mult_schrage_1(a, value, m / a, m % a);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_general(IntType a, IntType b) {
|
||||
return std::uintmax_t(modulus) <= std::numeric_limits<std::uintmax_t>::max() / modulus
|
||||
? static_cast<IntType>(std::uintmax_t(a) * b % modulus)
|
||||
: /*static_cast<IntType>(sprout::random::detail::mulmod(a, b, modulus))*/throw "Sorry, not implemented."
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType sub(IntType a, IntType b) {
|
||||
return a < b ? m - (b - a) : a - b;
|
||||
}
|
||||
static SPROUT_CONSTEXPR unsigned_type unsigned_m() {
|
||||
return m == 0 ? unsigned_type((std::numeric_limits<IntType>::max)()) + 1 : unsigned_type(m);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return n == 0 ? m - l1 : invert_euclidian_1(c, l1, l2, n, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return p == 0 ? l2 : invert_euclidian_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return invert_euclidian_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m)
|
||||
: throw "assert(c > 0)"
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return n == 0 ? m - l1 : invert_euclidian0_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return p == 0 ? l2 : invert_euclidian0_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return std::numeric_limits<IntType>::max() % n != n - 1
|
||||
? invert_euclidian0_2(c, l1 + (std::numeric_limits<IntType>::max() / n) * l2, l2, n, std::numeric_limits<IntType>::max() - (std::numeric_limits<IntType>::max() / n) * n + 1)
|
||||
: throw "assert(std::numeric_limits<IntType>::max() % n != n - 1)"
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m)
|
||||
: throw "assert(c > 0)"
|
||||
;
|
||||
}
|
||||
public:
|
||||
static SPROUT_CONSTEXPR IntType apply(IntType x) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0
|
||||
? (unsigned_type(x)) & (unsigned_m() - 1)
|
||||
: x % (m + suppress_warnings)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType add(IntType x, IntType c) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
||||
: c == 0 ? x
|
||||
: x < m - c ? x + c
|
||||
: x - (m - c)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult(IntType a, IntType x) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? unsigned_type(a) * unsigned_type(x) & (unsigned_m() - 1)
|
||||
: a == 0 ? 0
|
||||
: a == 1 ? x
|
||||
: m <= std::numeric_limits<IntType>::max() / a ? mult_small(a, x)
|
||||
: std::numeric_limits<IntType>::is_signed && (m % a < m / a) ? mult_schrage(a, x)
|
||||
: mult_general(a, x)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_add(IntType a, IntType x, IntType c) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
||||
: a == 0 ? c
|
||||
: m <= (std::numeric_limits<IntType>::max() - c) / a ? (a * x + c) % (m + suppress_warnings)
|
||||
: add(mult(a, x), c)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType pow(IntType a, std::uintmax_t exponent) {
|
||||
return pow_1(a, exponent);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert(IntType x) {
|
||||
return x == 0 ? 0
|
||||
: m == 0 ? invert_euclidian0(x)
|
||||
: invert_euclidian(x)
|
||||
;
|
||||
}
|
||||
private:
|
||||
const_mod() = delete;
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
|
||||
#ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename IntType, IntType m>
|
||||
class const_mod {
|
||||
private:
|
||||
typedef typename std::make_unsigned<IntType>::type unsigned_type;
|
||||
private:
|
||||
SPROUT_STATIC_CONSTEXPR IntType suppress_warnings = m == 0;
|
||||
SPROUT_STATIC_CONSTEXPR IntType modulus = m + suppress_warnings;
|
||||
private:
|
||||
static_assert(suppress_warnings == 0, "suppress_warnings == 0");
|
||||
static_assert(modulus == m, "modulus == m");
|
||||
private:
|
||||
static SPROUT_CONSTEXPR IntType pow_1(IntType a, std::uintmax_t exponent, IntType result = 1) {
|
||||
return exponent != 0
|
||||
? pow_1(mult(a, a), exponent / 2, exponent % 2 == 1 ? mult(result, a) : result)
|
||||
: result
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_small(IntType a, IntType x) {
|
||||
return a * x % (m + suppress_warnings);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_schrage_1(IntType a, IntType value, IntType q, IntType r) {
|
||||
return r < q
|
||||
? sub(a * (value % q), r * (value / q))
|
||||
: throw "assert(r < q)"
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_schrage(IntType a, IntType value) {
|
||||
return mult_schrage_1(a, value, m / a, m % a);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_general(IntType a, IntType b) {
|
||||
return std::uintmax_t(modulus) <= std::numeric_limits<std::uintmax_t>::max() / modulus
|
||||
? static_cast<IntType>(std::uintmax_t(a) * b % modulus)
|
||||
: /*static_cast<IntType>(sprout::random::detail::mulmod(a, b, modulus))*/throw "Sorry, not implemented."
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType sub(IntType a, IntType b) {
|
||||
return a < b ? m - (b - a) : a - b;
|
||||
}
|
||||
static SPROUT_CONSTEXPR unsigned_type unsigned_m() {
|
||||
return m == 0 ? unsigned_type((std::numeric_limits<IntType>::max)()) + 1 : unsigned_type(m);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return n == 0 ? m - l1 : invert_euclidian_1(c, l1, l2, n, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return p == 0 ? l2 : invert_euclidian_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return invert_euclidian_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian_1(c, 0, 1, c, m)
|
||||
: throw "assert(c > 0)"
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_3(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return n == 0 ? m - l1 : invert_euclidian0_2(c, l1 + (p / n) * l2, l2, n, p - (p / n) * n);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_2(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return p == 0 ? l2 : invert_euclidian0_3(c, l1, l2 + (n / p) * l1, n - (n / p) * p, p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0_1(IntType c, IntType l1, IntType l2, IntType n, IntType p) {
|
||||
return std::numeric_limits<IntType>::max() % n != n - 1
|
||||
? invert_euclidian0_2(c, l1 + (std::numeric_limits<IntType>::max() / n) * l2, l2, n, std::numeric_limits<IntType>::max() - (std::numeric_limits<IntType>::max() / n) * n + 1)
|
||||
: throw "assert(std::numeric_limits<IntType>::max() % n != n - 1)"
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert_euclidian0(IntType c) {
|
||||
return c > 0
|
||||
? c == 1 ? 1 : invert_euclidian0_1(c, 0, 1, c, m)
|
||||
: throw "assert(c > 0)"
|
||||
;
|
||||
}
|
||||
public:
|
||||
static SPROUT_CONSTEXPR IntType apply(IntType x) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0
|
||||
? (unsigned_type(x)) & (unsigned_m() - 1)
|
||||
: x % (m + suppress_warnings)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType add(IntType x, IntType c) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
||||
: c == 0 ? x
|
||||
: x < m - c ? x + c
|
||||
: x - (m - c)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult(IntType a, IntType x) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? unsigned_type(a) * unsigned_type(x) & (unsigned_m() - 1)
|
||||
: a == 0 ? 0
|
||||
: a == 1 ? x
|
||||
: m <= std::numeric_limits<IntType>::max() / a ? mult_small(a, x)
|
||||
: std::numeric_limits<IntType>::is_signed && (m % a < m / a) ? mult_schrage(a, x)
|
||||
: mult_general(a, x)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType mult_add(IntType a, IntType x, IntType c) {
|
||||
return ((unsigned_m() - 1) & unsigned_m()) == 0 ? (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1)
|
||||
: a == 0 ? c
|
||||
: m <= (std::numeric_limits<IntType>::max() - c) / a ? (a * x + c) % (m + suppress_warnings)
|
||||
: add(mult(a, x), c)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType pow(IntType a, std::uintmax_t exponent) {
|
||||
return pow_1(a, exponent);
|
||||
}
|
||||
static SPROUT_CONSTEXPR IntType invert(IntType x) {
|
||||
return x == 0 ? 0
|
||||
: m == 0 ? invert_euclidian0(x)
|
||||
: invert_euclidian(x)
|
||||
;
|
||||
}
|
||||
private:
|
||||
const_mod() = delete;
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_DETAIL_CONST_MOD_HPP
|
||||
|
||||
|
|
|
@ -1,73 +1,73 @@
|
|||
#ifndef SPROUT_RANDOM_DETAIL_PTR_HELPER_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_PTR_HELPER_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct ptr_helper {
|
||||
typedef T value_type;
|
||||
typedef T& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T const& rvalue_type;
|
||||
static reference_type ref(T& r) {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const& r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T&> {
|
||||
typedef T value_type;
|
||||
typedef T& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T& rvalue_type;
|
||||
static reference_type ref(T& r) {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const& r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T const&> {
|
||||
typedef T value_type;
|
||||
typedef T const& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T const& rvalue_type;
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const& r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T*> {
|
||||
typedef T value_type;
|
||||
typedef T& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T* rvalue_type;
|
||||
static reference_type ref(T* p) {
|
||||
return *p;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const* p) {
|
||||
return *p;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T const*> {
|
||||
typedef T value_type;
|
||||
typedef T const& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T const* rvalue_type;
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const* p) {
|
||||
return *p;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_DETAIL_PTR_HELPER_HPP
|
||||
|
||||
#ifndef SPROUT_RANDOM_DETAIL_PTR_HELPER_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_PTR_HELPER_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct ptr_helper {
|
||||
typedef T value_type;
|
||||
typedef T& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T const& rvalue_type;
|
||||
static reference_type ref(T& r) {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const& r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T&> {
|
||||
typedef T value_type;
|
||||
typedef T& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T& rvalue_type;
|
||||
static reference_type ref(T& r) {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const& r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T const&> {
|
||||
typedef T value_type;
|
||||
typedef T const& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T const& rvalue_type;
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const& r) {
|
||||
return r;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T*> {
|
||||
typedef T value_type;
|
||||
typedef T& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T* rvalue_type;
|
||||
static reference_type ref(T* p) {
|
||||
return *p;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const* p) {
|
||||
return *p;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct ptr_helper<T const*> {
|
||||
typedef T value_type;
|
||||
typedef T const& reference_type;
|
||||
typedef T const& const_reference_type;
|
||||
typedef T const* rvalue_type;
|
||||
static SPROUT_CONSTEXPR const_reference_type ref(T const* p) {
|
||||
return *p;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_DETAIL_PTR_HELPER_HPP
|
||||
|
||||
|
|
|
@ -1,63 +1,63 @@
|
|||
#ifndef SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename T, bool B = std::numeric_limits<T>::is_signed>
|
||||
struct subtract {};
|
||||
template<typename T>
|
||||
struct subtract<T, false> {
|
||||
public:
|
||||
typedef T result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T x, T y) const {
|
||||
return x - y;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct subtract<T, true> {
|
||||
public:
|
||||
typedef typename std::make_unsigned<T>::type result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T x, T y) const {
|
||||
return y >= 0 ? result_type(x) - result_type(y)
|
||||
: x >= 0 ? result_type(x) + result_type(-(y + 1)) + 1
|
||||
: result_type(x - y)
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T1, typename T2, bool B = std::numeric_limits<T2>::is_signed>
|
||||
struct add {};
|
||||
template<typename T1, typename T2>
|
||||
struct add<T1, T2, false> {
|
||||
public:
|
||||
typedef T2 result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T1 x, T2 y) const {
|
||||
return T2(x) + y;
|
||||
}
|
||||
};
|
||||
template<typename T1, typename T2>
|
||||
struct add<T1, T2, true> {
|
||||
public:
|
||||
typedef T2 result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T1 x, T2 y) const {
|
||||
return y >= 0 ? T2(x) + y
|
||||
: x >= T1(-(y + 1)) ? T2(x - T1(-(y + 1)) - 1)
|
||||
: T2(x) + y
|
||||
;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
|
||||
#ifndef SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<typename T, bool B = std::numeric_limits<T>::is_signed>
|
||||
struct subtract {};
|
||||
template<typename T>
|
||||
struct subtract<T, false> {
|
||||
public:
|
||||
typedef T result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T x, T y) const {
|
||||
return x - y;
|
||||
}
|
||||
};
|
||||
template<typename T>
|
||||
struct subtract<T, true> {
|
||||
public:
|
||||
typedef typename std::make_unsigned<T>::type result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T x, T y) const {
|
||||
return y >= 0 ? result_type(x) - result_type(y)
|
||||
: x >= 0 ? result_type(x) + result_type(-(y + 1)) + 1
|
||||
: result_type(x - y)
|
||||
;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T1, typename T2, bool B = std::numeric_limits<T2>::is_signed>
|
||||
struct add {};
|
||||
template<typename T1, typename T2>
|
||||
struct add<T1, T2, false> {
|
||||
public:
|
||||
typedef T2 result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T1 x, T2 y) const {
|
||||
return T2(x) + y;
|
||||
}
|
||||
};
|
||||
template<typename T1, typename T2>
|
||||
struct add<T1, T2, true> {
|
||||
public:
|
||||
typedef T2 result_type;
|
||||
public:
|
||||
SPROUT_CONSTEXPR result_type operator()(T1 x, T2 y) const {
|
||||
return y >= 0 ? T2(x) + y
|
||||
: x >= T1(-(y + 1)) ? T2(x - T1(-(y + 1)) - 1)
|
||||
: T2(x) + y
|
||||
;
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // SPROUT_RANDOM_DETAIL_SIGNED_UNSIGNED_TOOLS_HPP
|
||||
|
||||
|
|
|
@ -1,74 +1,74 @@
|
|||
#ifndef SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/detail/generator_bits.hpp>
|
||||
#include <sprout/detail/integer.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<class URNG>
|
||||
class uniform_int_float {
|
||||
public:
|
||||
typedef URNG base_type;
|
||||
typedef typename base_type::result_type base_result;
|
||||
typedef typename sprout::detail::uint_t<
|
||||
(std::numeric_limits<std::uintmax_t>::digits < std::numeric_limits<base_result>::digits)
|
||||
? std::numeric_limits<std::uintmax_t>::digits
|
||||
: std::numeric_limits<base_result>::digits
|
||||
>::fast result_type;
|
||||
private:
|
||||
base_type rng_;
|
||||
public:
|
||||
//uniform_int_float = default; // ???
|
||||
SPROUT_CONSTEXPR uniform_int_float()
|
||||
: rng_()
|
||||
{}
|
||||
SPROUT_CONSTEXPR explicit uniform_int_float(base_type const& rng)
|
||||
: rng_(rng)
|
||||
{}
|
||||
SPROUT_CONSTEXPR result_type min() const {
|
||||
return 0;
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type max() const {
|
||||
return (
|
||||
result_type(2) << (
|
||||
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(
|
||||
std::numeric_limits<result_type>::digits,
|
||||
sprout::random::detail::generator_bits<base_type>::value()
|
||||
) - 1
|
||||
)
|
||||
) - 1
|
||||
;
|
||||
}
|
||||
base_type& base() {
|
||||
return rng_;
|
||||
}
|
||||
SPROUT_CONSTEXPR base_type const& base() const {
|
||||
return rng_;
|
||||
}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<uniform_int_float> generate(
|
||||
sprout::random::random_result<base_type> const& rnd
|
||||
) const
|
||||
{
|
||||
return sprout::random::random_result<uniform_int_float>(
|
||||
static_cast<result_type>(rnd.result() * (static_cast<base_result>(max()) + 1)),
|
||||
uniform_int_float(rnd.engine())
|
||||
);
|
||||
}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<uniform_int_float> operator()() const {
|
||||
return generate(rng_());
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
||||
|
||||
#ifndef SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
||||
#define SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/random/random_result.hpp>
|
||||
#include <sprout/random/detail/generator_bits.hpp>
|
||||
#include <sprout/detail/integer.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
|
||||
namespace sprout {
|
||||
namespace random {
|
||||
namespace detail {
|
||||
template<class URNG>
|
||||
class uniform_int_float {
|
||||
public:
|
||||
typedef URNG base_type;
|
||||
typedef typename base_type::result_type base_result;
|
||||
typedef typename sprout::detail::uint_t<
|
||||
(std::numeric_limits<std::uintmax_t>::digits < std::numeric_limits<base_result>::digits)
|
||||
? std::numeric_limits<std::uintmax_t>::digits
|
||||
: std::numeric_limits<base_result>::digits
|
||||
>::fast result_type;
|
||||
private:
|
||||
base_type rng_;
|
||||
public:
|
||||
//uniform_int_float = default; // ???
|
||||
SPROUT_CONSTEXPR uniform_int_float()
|
||||
: rng_()
|
||||
{}
|
||||
SPROUT_CONSTEXPR explicit uniform_int_float(base_type const& rng)
|
||||
: rng_(rng)
|
||||
{}
|
||||
SPROUT_CONSTEXPR result_type min() const {
|
||||
return 0;
|
||||
}
|
||||
SPROUT_CONSTEXPR result_type max() const {
|
||||
return (
|
||||
result_type(2) << (
|
||||
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(
|
||||
std::numeric_limits<result_type>::digits,
|
||||
sprout::random::detail::generator_bits<base_type>::value()
|
||||
) - 1
|
||||
)
|
||||
) - 1
|
||||
;
|
||||
}
|
||||
base_type& base() {
|
||||
return rng_;
|
||||
}
|
||||
SPROUT_CONSTEXPR base_type const& base() const {
|
||||
return rng_;
|
||||
}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<uniform_int_float> generate(
|
||||
sprout::random::random_result<base_type> const& rnd
|
||||
) const
|
||||
{
|
||||
return sprout::random::random_result<uniform_int_float>(
|
||||
static_cast<result_type>(rnd.result() * (static_cast<base_result>(max()) + 1)),
|
||||
uniform_int_float(rnd.engine())
|
||||
);
|
||||
}
|
||||
SPROUT_CONSTEXPR sprout::random::random_result<uniform_int_float> operator()() const {
|
||||
return generate(rng_());
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
} // namespace random
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_DETAIL_UNIFORM_INT_FLOAT_HPP
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue