From 66da9f4feaeae0471fe7c8ad639464b9ae67a077 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 7 Feb 2013 03:11:17 +0900 Subject: [PATCH] fix math::log implementation support std::hash --- sprout/array/hash.hpp | 11 ++++++++ sprout/bitset/hash.hpp | 10 +++++++ sprout/complex/hash.hpp | 10 +++++++ sprout/detail/pow.hpp | 26 ++++++++++++++++--- sprout/logic/tribool/hash.hpp | 14 ++++++++++ sprout/math/exponential.hpp | 3 +++ sprout/math/ldexp.hpp | 38 +++++++++++++++++++++++++++ sprout/math/log.hpp | 3 ++- sprout/math/scalbln.hpp | 39 ++++++++++++++++++++++++++++ sprout/math/scalbn.hpp | 39 ++++++++++++++++++++++++++++ sprout/optional/hash.hpp | 10 +++++++ sprout/pit/hash.hpp | 10 +++++++ sprout/rational/hash.hpp | 10 +++++++ sprout/string/hash.hpp | 10 +++++++ sprout/sub_array/hash.hpp | 10 +++++++ sprout/tuple/tuple/hash.hpp | 10 +++++++ sprout/utility/pair/hash.hpp | 10 +++++++ sprout/utility/value_holder/hash.hpp | 10 +++++++ sprout/uuid/hash.hpp | 10 +++++++ sprout/variant/hash.hpp | 10 +++++++ 20 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 sprout/math/ldexp.hpp create mode 100644 sprout/math/scalbln.hpp create mode 100644 sprout/math/scalbn.hpp diff --git a/sprout/array/hash.hpp b/sprout/array/hash.hpp index 7eefce86..1f59c2db 100644 --- a/sprout/array/hash.hpp +++ b/sprout/array/hash.hpp @@ -2,6 +2,7 @@ #define SPROUT_ARRAY_HASH_HPP #include +#include #include #include #include @@ -17,4 +18,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_ARRAY_HASH_HPP diff --git a/sprout/bitset/hash.hpp b/sprout/bitset/hash.hpp index 2178dc72..8632660a 100644 --- a/sprout/bitset/hash.hpp +++ b/sprout/bitset/hash.hpp @@ -18,4 +18,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_BITSET_HASH_HPP diff --git a/sprout/complex/hash.hpp b/sprout/complex/hash.hpp index dcfc4f89..1218477c 100644 --- a/sprout/complex/hash.hpp +++ b/sprout/complex/hash.hpp @@ -17,4 +17,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_COMPLEX_HASH_HPP diff --git a/sprout/detail/pow.hpp b/sprout/detail/pow.hpp index 41a90e8d..4f32f190 100644 --- a/sprout/detail/pow.hpp +++ b/sprout/detail/pow.hpp @@ -1,7 +1,11 @@ #ifndef SPROUT_DETAIL_POW_HPP #define SPROUT_DETAIL_POW_HPP +#include #include +#include +#include +#include namespace sprout { namespace detail { @@ -17,21 +21,35 @@ namespace sprout { return x * x * x; } - template + template inline SPROUT_CONSTEXPR T - pow_n_impl(T const& x, int n) { + pow_n_impl(T const& x, IntType n) { return n == 1 ? x : n % 2 ? x * sprout::detail::pow2(sprout::detail::pow_n_impl(x, n / 2)) : sprout::detail::pow2(sprout::detail::pow_n_impl(x, n / 2)) ; } - template + template< + typename T, typename UIntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > inline SPROUT_CONSTEXPR T - pow_n(T const& x, int n) { + pow_n(T const& x, UIntType n) { return n == 0 ? T(1) : sprout::detail::pow_n_impl(x, n) ; } + template< + typename T, typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR T + pow_n(T const& x, IntType n) { + return n == 0 ? T(1) + : n > 0 ? sprout::detail::pow_n_impl(x, n) + : T(1) / sprout::detail::pow_n_impl(x, -n) + ; + } } // namespace detail } // namespace sprout diff --git a/sprout/logic/tribool/hash.hpp b/sprout/logic/tribool/hash.hpp index 66119f8a..ea91a4d9 100644 --- a/sprout/logic/tribool/hash.hpp +++ b/sprout/logic/tribool/hash.hpp @@ -25,4 +25,18 @@ namespace sprout { } // namespace logic } // namespace sprout +namespace std { + // + // hash + // + template<> + struct hash + : public sprout::hash + {}; + template<> + struct hash + : public sprout::hash + {}; +} // namespace std + #endif // #ifndef SPROUT_LOGIC_TRIBOOL_HASH_HPP diff --git a/sprout/math/exponential.hpp b/sprout/math/exponential.hpp index 95602a4f..54c9e7a4 100644 --- a/sprout/math/exponential.hpp +++ b/sprout/math/exponential.hpp @@ -10,5 +10,8 @@ #include #include #include +#include +#include +#include #endif // #ifndef SPROUT_MATH_EXPONENTIAL_HPP diff --git a/sprout/math/ldexp.hpp b/sprout/math/ldexp.hpp new file mode 100644 index 00000000..55c31fcd --- /dev/null +++ b/sprout/math/ldexp.hpp @@ -0,0 +1,38 @@ +#ifndef SPROUT_MATH_LDEXP_HPP +#define SPROUT_MATH_LDEXP_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + ldexp(FloatType x, int exp) { + return static_cast(x * sprout::detail::pow_n(FloatType(2), exp)); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + ldexp(IntType x, int exp) { + return sprout::math::detail::ldexp(static_cast(x), exp); + } + } // namespace detail + + using NS_SPROUT_MATH_DETAIL::ldexp; + } // namespace math + + using sprout::math::ldexp; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_LDEXP_HPP diff --git a/sprout/math/log.hpp b/sprout/math/log.hpp index 09731056..ee2969b7 100644 --- a/sprout/math/log.hpp +++ b/sprout/math/log.hpp @@ -42,7 +42,8 @@ namespace sprout { typedef double type; return x == 0 ? std::numeric_limits::quiet_NaN() : !(x > 0) ? -std::numeric_limits::infinity() - : static_cast(sprout::math::detail::log_impl(x)) + : x < 1 ? static_cast(-sprout::math::detail::log_impl(type(1) / static_cast(x))) + : static_cast(sprout::math::detail::log_impl(static_cast(x))) ; } diff --git a/sprout/math/scalbln.hpp b/sprout/math/scalbln.hpp new file mode 100644 index 00000000..7f30a3d9 --- /dev/null +++ b/sprout/math/scalbln.hpp @@ -0,0 +1,39 @@ +#ifndef SPROUT_MATH_SCALBLN_HPP +#define SPROUT_MATH_SCALBLN_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + scalbln(FloatType x, long exp) { + return static_cast(x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp)); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + scalbln(IntType x, long exp) { + return sprout::math::detail::scalbln(static_cast(x), exp); + } + } // namespace detail + + using NS_SPROUT_MATH_DETAIL::scalbln; + } // namespace math + + using sprout::math::scalbln; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_SCALBLN_HPP diff --git a/sprout/math/scalbn.hpp b/sprout/math/scalbn.hpp new file mode 100644 index 00000000..f56737ca --- /dev/null +++ b/sprout/math/scalbn.hpp @@ -0,0 +1,39 @@ +#ifndef SPROUT_MATH_SCALBN_HPP +#define SPROUT_MATH_SCALBN_HPP + +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + namespace detail { + template< + typename FloatType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR FloatType + scalbn(FloatType x, int exp) { + return static_cast(x * sprout::detail::pow_n(FloatType(FLT_RADIX), exp)); + } + + template< + typename IntType, + typename sprout::enabler_if::value>::type = sprout::enabler + > + inline SPROUT_CONSTEXPR double + scalbn(IntType x, int exp) { + return sprout::math::detail::scalbn(static_cast(x), exp); + } + } // namespace detail + + using NS_SPROUT_MATH_DETAIL::scalbn; + } // namespace math + + using sprout::math::scalbn; +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_SCALBN_HPP diff --git a/sprout/optional/hash.hpp b/sprout/optional/hash.hpp index d20a8313..f5437df6 100644 --- a/sprout/optional/hash.hpp +++ b/sprout/optional/hash.hpp @@ -19,4 +19,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_OPTIONAL_HASH_HPP diff --git a/sprout/pit/hash.hpp b/sprout/pit/hash.hpp index 5963844e..9fb0c57b 100644 --- a/sprout/pit/hash.hpp +++ b/sprout/pit/hash.hpp @@ -17,4 +17,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_PIT_HASH_HPP diff --git a/sprout/rational/hash.hpp b/sprout/rational/hash.hpp index e9368f32..e02234a0 100644 --- a/sprout/rational/hash.hpp +++ b/sprout/rational/hash.hpp @@ -17,4 +17,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // SPROUT_RATIONAL_HASH_HPP diff --git a/sprout/string/hash.hpp b/sprout/string/hash.hpp index 2a5d9301..07bb94be 100644 --- a/sprout/string/hash.hpp +++ b/sprout/string/hash.hpp @@ -17,4 +17,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_STRING_HASH_HPP diff --git a/sprout/sub_array/hash.hpp b/sprout/sub_array/hash.hpp index 056b3007..a6fd250b 100644 --- a/sprout/sub_array/hash.hpp +++ b/sprout/sub_array/hash.hpp @@ -16,4 +16,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_SUB_ARRAY_HASH_HPP diff --git a/sprout/tuple/tuple/hash.hpp b/sprout/tuple/tuple/hash.hpp index 14baf391..4c243f56 100644 --- a/sprout/tuple/tuple/hash.hpp +++ b/sprout/tuple/tuple/hash.hpp @@ -31,4 +31,14 @@ namespace sprout { } // namespace tuples } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_TUPLE_TUPLE_HASH_HPP diff --git a/sprout/utility/pair/hash.hpp b/sprout/utility/pair/hash.hpp index a3d94a8b..e070e16c 100644 --- a/sprout/utility/pair/hash.hpp +++ b/sprout/utility/pair/hash.hpp @@ -17,4 +17,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_UTILITY_PAIR_HASH_HPP diff --git a/sprout/utility/value_holder/hash.hpp b/sprout/utility/value_holder/hash.hpp index 4bcd301a..2ea7d574 100644 --- a/sprout/utility/value_holder/hash.hpp +++ b/sprout/utility/value_holder/hash.hpp @@ -25,4 +25,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HASH_HPP diff --git a/sprout/uuid/hash.hpp b/sprout/uuid/hash.hpp index f674df3a..1e515c66 100644 --- a/sprout/uuid/hash.hpp +++ b/sprout/uuid/hash.hpp @@ -16,4 +16,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template<> + struct hash + : public sprout::hash + {}; +} // namespace std + #endif // #ifndef SPROUT_UUID_HASH_HPP diff --git a/sprout/variant/hash.hpp b/sprout/variant/hash.hpp index 66db3172..8aa5d325 100644 --- a/sprout/variant/hash.hpp +++ b/sprout/variant/hash.hpp @@ -33,4 +33,14 @@ namespace sprout { } } // namespace sprout +namespace std { + // + // hash + // + template + struct hash > + : public sprout::hash > + {}; +} // namespace std + #endif // #ifndef SPROUT_VARIANT_HASH_HPP