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

random/unique_seed.hpp 更新

This commit is contained in:
bolero-MURAKAMI 2011-10-29 12:02:21 +09:00
parent 9d7eecc585
commit cf64e4c457
3 changed files with 75 additions and 17 deletions

View file

@ -85,11 +85,11 @@ namespace sprout {
); );
} }
SPROUT_CONSTEXPR inline std::size_t hash_value_pointer_1(std::size_t x) { inline std::size_t hash_value_pointer_1(std::size_t x) {
return x + (x >> 3); return x + (x >> 3);
} }
template<typename T> template<typename T>
SPROUT_CONSTEXPR std::size_t hash_value_pointer(T* v) { std::size_t hash_value_pointer(T const* v) {
return sprout::hash_detail::hash_value_pointer_1(static_cast<std::size_t>(reinterpret_cast<std::ptrdiff_t>(v))); return sprout::hash_detail::hash_value_pointer_1(static_cast<std::size_t>(reinterpret_cast<std::ptrdiff_t>(v)));
} }
} // namespace hash_detail } // namespace hash_detail
@ -145,12 +145,21 @@ namespace sprout {
return sprout::hash_range(&v[0], &v[0] + N); return sprout::hash_range(&v[0], &v[0] + N);
} }
//
// to_hash
//
template<typename T>
SPROUT_CONSTEXPR std::size_t to_hash(T const& v) {
using sprout::hash_value;
return hash_value(v);
}
// //
// hash_combine // hash_combine
// //
template<typename T> template<typename T>
SPROUT_CONSTEXPR std::size_t hash_combine(std::size_t seed, T const& v) { SPROUT_CONSTEXPR std::size_t hash_combine(std::size_t seed, T const& v) {
return seed ^ (sprout::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2)); return seed ^ (sprout::to_hash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
} }
// //
@ -178,8 +187,7 @@ namespace sprout {
typedef std::size_t result_type; typedef std::size_t result_type;
public: public:
SPROUT_CONSTEXPR std::size_t operator()(T const& v) const { SPROUT_CONSTEXPR std::size_t operator()(T const& v) const {
using sprout::hash_value; return sprout::to_hash(v);
return hash_value(v);
} }
}; };
@ -224,16 +232,26 @@ namespace sprout {
#undef SPROUT_HASH_SPECIALIZE_REF #undef SPROUT_HASH_SPECIALIZE_REF
template<typename T> template<typename T>
struct hash<T*> { struct hash<T const> {
public: public:
typedef T* argument_type; typedef T argument_type;
typedef std::size_t result_type; typedef std::size_t result_type;
public: public:
SPROUT_CONSTEXPR std::size_t operator()(T* v) const { SPROUT_CONSTEXPR std::size_t operator()(T const& v) const {
using sprout::hash_value; using sprout::hash_value;
return hash_value(v); return hash_value(v);
} }
}; };
template<typename T>
struct hash<T const*> {
public:
typedef T const* argument_type;
typedef std::size_t result_type;
public:
std::size_t operator()(T const* v) const {
return sprout::to_hash(v);
}
};
} //namespace sprout } //namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_HPP #endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_HPP

View file

@ -11,6 +11,12 @@ namespace sprout {
template<typename T> template<typename T>
struct hash; struct hash;
//
// to_hash
//
template<typename T>
SPROUT_CONSTEXPR std::size_t to_hash(T const& v);
// //
// hash_combine // hash_combine
// //

View file

@ -1,22 +1,56 @@
#ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP #ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP
#define SPROUT_RANDOM_UNIQUE_SEED_HPP #define SPROUT_RANDOM_UNIQUE_SEED_HPP
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR #include <cstddef>
# include <cstddef> #include <type_traits>
# include <type_traits>
#endif
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/array.hpp>
#include <sprout/functional/hash/hash.hpp> #include <sprout/functional/hash/hash.hpp>
#include <sprout/preprocessor/unique_string.hpp> #include <sprout/preprocessor/unique_string.hpp>
namespace sprout {
//
// make_seed
//
template<typename T>
SPROUT_CONSTEXPR std::size_t make_seed(T const& v) {
return sprout::to_hash(v);
}
namespace detail {
template<std::size_t N, typename T, typename... Args>
SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Args) + 1 == N,
sprout::array<std::size_t, N>
>::type make_seed_seq_impl(T const& v, std::size_t seed, Args const&... args) {
return sprout::array<std::size_t, N>{{args..., seed}};
}
template<std::size_t N, typename T, typename... Args>
SPROUT_CONSTEXPR typename std::enable_if<
sizeof...(Args) + 1 != N,
sprout::array<std::size_t, N>
>::type make_seed_seq_impl(T const& v, std::size_t seed, Args const&... args) {
return sprout::detail::make_seed_seq_impl<N>(v, sprout::hash_combine(seed, v), args..., seed);
}
} // namespace detail
//
// make_seed_seq
//
template<std::size_t N, typename T>
SPROUT_CONSTEXPR sprout::array<std::size_t, N> make_seed_seq(T const& v) {
return sprout::detail::make_seed_seq_impl<N>(v, sprout::to_hash(v));
}
} // namespace sprout
// //
// SPROUT_UNIQUE_SEED // SPROUT_UNIQUE_SEED
// //
#ifndef SPROUT_CONFIG_DISABLE_CONSTEXPR #define SPROUT_UNIQUE_SEED (::sprout::make_seed(SPROUT_PP_UNIQUE_STRING))
# define SPROUT_UNIQUE_SEED (::std::integral_constant< ::std::size_t, ::sprout::hash_value(SPROUT_PP_UNIQUE_STRING)>::value)
#else //
# define SPROUT_UNIQUE_SEED (::sprout::hash_value(SPROUT_PP_UNIQUE_STRING)) // SPROUT_UNIQUE_SEED_SEQ
#endif //
#define SPROUT_UNIQUE_SEED_SEQ(N) (::sprout::make_seed_seq<N>(SPROUT_PP_UNIQUE_STRING))
#endif // #ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP #endif // #ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP