mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
functional/hash.hpp 追加
random/unique_seed.hpp 追加
This commit is contained in:
parent
1ebc833ea8
commit
305ae86d92
11 changed files with 381 additions and 0 deletions
8
sprout/functional/hash.hpp
Normal file
8
sprout/functional/hash.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_HPP
|
||||
|
17
sprout/functional/hash/array.hpp
Normal file
17
sprout/functional/hash/array.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_ARRAY_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_ARRAY_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename T, std::size_t N>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(sprout::array<T, N> const& v) {
|
||||
return sprout::hash_range(v.begin(), v.end());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_ARRAY_HPP
|
||||
|
226
sprout/functional/hash/hash.hpp
Normal file
226
sprout/functional/hash/hash.hpp
Normal file
|
@ -0,0 +1,226 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <limits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash_fwd.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// hash_value
|
||||
//
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(bool v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(char v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(wchar_t v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned char v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(signed char v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(short v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned short v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(int v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned int v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(long v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned long v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(long long v);
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned long long v);
|
||||
//SPROUT_CONSTEXPR std::size_t inline hash_value(float v);
|
||||
//SPROUT_CONSTEXPR std::size_t inline hash_value(double v);
|
||||
//SPROUT_CONSTEXPR std::size_t inline hash_value(long double v);
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(T*);
|
||||
template<typename T, std::size_t N>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(T const (&v)[N]);
|
||||
|
||||
namespace hash_detail {
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_signed_2(T val, int length, std::size_t seed, T positive, std::size_t i) {
|
||||
return i > 0
|
||||
? hash_value_signed_2(
|
||||
val,
|
||||
length,
|
||||
seed ^ static_cast<std::size_t>((positive >> i) + (seed << 6) + (seed >> 2)),
|
||||
positive,
|
||||
i - std::numeric_limits<std::size_t>::digits
|
||||
)
|
||||
: seed ^ static_cast<std::size_t>(val + (seed << 6) + (seed >> 2))
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_signed_1(T val, int length, std::size_t seed, T positive) {
|
||||
return hash_value_signed_2(val, length, seed, positive, length * std::numeric_limits<std::size_t>::digits);
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_signed(T val) {
|
||||
return sprout::hash_detail::hash_value_signed_1(
|
||||
val,
|
||||
(std::numeric_limits<T>::digits - 1) / std::numeric_limits<std::size_t>::digits,
|
||||
0,
|
||||
val < 0 ? -1 - val : val
|
||||
);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_unsigned_2(T val, int length, std::size_t seed, std::size_t i) {
|
||||
return i > 0
|
||||
? hash_value_unsigned_2(
|
||||
val,
|
||||
length,
|
||||
seed ^ static_cast<std::size_t>((val >> i) + (seed << 6) + (seed >> 2)),
|
||||
i - std::numeric_limits<std::size_t>::digits
|
||||
)
|
||||
: seed ^ static_cast<std::size_t>(val + (seed << 6) + (seed >> 2))
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_unsigned_1(T val, int length, std::size_t seed) {
|
||||
return hash_value_unsigned_2(val, length, seed, length * std::numeric_limits<std::size_t>::digits);
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_unsigned(T val) {
|
||||
return sprout::hash_detail::hash_value_unsigned_1(
|
||||
val,
|
||||
(std::numeric_limits<T>::digits - 1) / std::numeric_limits<std::size_t>::digits,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value_pointer_1(std::size_t x) {
|
||||
return x + (x >> 3);
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value_pointer(T* v) {
|
||||
return sprout::hash_detail::hash_value_pointer_1(static_cast<std::size_t>(reinterpret_cast<std::ptrdiff_t>(v)));
|
||||
}
|
||||
} // namespace hash_detail
|
||||
|
||||
//
|
||||
// hash_value
|
||||
//
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(bool v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(char v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(wchar_t v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned char v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(signed char v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(short v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned short v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(int v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned int v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(long v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned long v) {
|
||||
return static_cast<std::size_t>(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(long long v) {
|
||||
return sprout::hash_detail::hash_value_signed(v);
|
||||
}
|
||||
SPROUT_CONSTEXPR inline std::size_t hash_value(unsigned long long v) {
|
||||
return sprout::hash_detail::hash_value_unsigned(v);
|
||||
}
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(T* v) {
|
||||
return sprout::hash_detail::hash_value_pointer(v);
|
||||
}
|
||||
template<typename T, std::size_t N >
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(T const (&v)[N]) {
|
||||
return sprout::hash_range(&v[0], &v[0] + N);
|
||||
}
|
||||
|
||||
//
|
||||
// hash_combine
|
||||
//
|
||||
template<typename T>
|
||||
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));
|
||||
}
|
||||
|
||||
//
|
||||
// hash_range
|
||||
//
|
||||
template<typename Iterator>
|
||||
SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last) {
|
||||
return sprout::hash_range(0, first, last);
|
||||
}
|
||||
template<typename Iterator>
|
||||
SPROUT_CONSTEXPR std::size_t hash_range(std::size_t seed, Iterator first, Iterator last) {
|
||||
return first != last
|
||||
? sprout::hash_range(sprout::hash_combine(seed, *first), first + 1, last)
|
||||
: seed
|
||||
;
|
||||
}
|
||||
|
||||
#define SPROUT_HASH_SPECIALIZE(type) \
|
||||
template<> \
|
||||
struct hash<type> { \
|
||||
public: \
|
||||
typedef type argument_type; \
|
||||
typedef std::size_t result_type; \
|
||||
public: \
|
||||
SPROUT_CONSTEXPR std::size_t operator()(type v) const { \
|
||||
return sprout::hash_value(v); \
|
||||
} \
|
||||
}
|
||||
#define SPROUT_HASH_SPECIALIZE_REF(type) \
|
||||
template<> \
|
||||
struct hash<type> { \
|
||||
public: \
|
||||
typedef type argument_type; \
|
||||
typedef std::size_t result_type; \
|
||||
public: \
|
||||
SPROUT_CONSTEXPR std::size_t operator()(type const& v) const { \
|
||||
return sprout::hash_value(v); \
|
||||
} \
|
||||
}
|
||||
|
||||
//
|
||||
// hash
|
||||
//
|
||||
SPROUT_HASH_SPECIALIZE(bool);
|
||||
SPROUT_HASH_SPECIALIZE(char);
|
||||
SPROUT_HASH_SPECIALIZE(wchar_t);
|
||||
SPROUT_HASH_SPECIALIZE(signed char);
|
||||
SPROUT_HASH_SPECIALIZE(unsigned char);
|
||||
SPROUT_HASH_SPECIALIZE(short);
|
||||
SPROUT_HASH_SPECIALIZE(unsigned short);
|
||||
SPROUT_HASH_SPECIALIZE(int);
|
||||
SPROUT_HASH_SPECIALIZE(unsigned int);
|
||||
SPROUT_HASH_SPECIALIZE(long);
|
||||
SPROUT_HASH_SPECIALIZE(unsigned long);
|
||||
SPROUT_HASH_SPECIALIZE(long long);
|
||||
SPROUT_HASH_SPECIALIZE(unsigned long long);
|
||||
|
||||
#undef SPROUT_HASH_SPECIALIZE
|
||||
#undef SPROUT_HASH_SPECIALIZE_REF
|
||||
|
||||
template <class T>
|
||||
struct hash<T*> {
|
||||
public:
|
||||
typedef T* argument_type;
|
||||
typedef std::size_t result_type;
|
||||
public: \
|
||||
SPROUT_CONSTEXPR std::size_t operator()(T* v) const {
|
||||
return sprout::hash_value(v);
|
||||
}
|
||||
};
|
||||
} //namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_HPP
|
||||
|
30
sprout/functional/hash/hash_fwd.hpp
Normal file
30
sprout/functional/hash/hash_fwd.hpp
Normal file
|
@ -0,0 +1,30 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_HASH_FWD_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_HASH_FWD_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// hash
|
||||
//
|
||||
template<typename T>
|
||||
struct hash;
|
||||
|
||||
//
|
||||
// hash_combine
|
||||
//
|
||||
template<typename T>
|
||||
SPROUT_CONSTEXPR std::size_t hash_combine(std::size_t seed, T const& v);
|
||||
|
||||
//
|
||||
// hash_range
|
||||
//
|
||||
template<typename Iterator>
|
||||
SPROUT_CONSTEXPR std::size_t hash_range(Iterator first, Iterator last);
|
||||
template<typename Iterator>
|
||||
SPROUT_CONSTEXPR std::size_t hash_range(std::size_t seed, Iterator first, Iterator last);
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_FWD_HPP
|
||||
|
17
sprout/functional/hash/sscrisk/cel/array.hpp
Normal file
17
sprout/functional/hash/sscrisk/cel/array.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_SSCRISK_CEL_ARRAY_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_SSCRISK_CEL_ARRAY_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash.hpp>
|
||||
#include <sscrisk/cel/array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename T, std::size_t N>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(sscrisk::cel::array<T, N> const& v) {
|
||||
return sprout::hash_range(v.begin(), v.end());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_SSCRISK_CEL_ARRAY_HPP
|
||||
|
17
sprout/functional/hash/string.hpp
Normal file
17
sprout/functional/hash/string.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_STRING_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_STRING_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash.hpp>
|
||||
#include <sprout/string.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename T, std::size_t N, typename Traits = sprout::char_traits<T> >
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(sprout::basic_string<T, N, Traits> const& v) {
|
||||
return sprout::hash_range(v.begin(), v.end());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_STRING_HPP
|
||||
|
17
sprout/functional/hash/sub_array.hpp
Normal file
17
sprout/functional/hash/sub_array.hpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_SUB_ARRAY_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_SUB_ARRAY_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR std::size_t hash_value(sprout::sub_array<Container> const& v) {
|
||||
return sprout::hash_range(v.begin(), v.end());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_SUB_ARRAY_HPP
|
||||
|
8
sprout/functional/hash_fwd.hpp
Normal file
8
sprout/functional/hash_fwd.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_FUNCTIONAL_HASH_FWD_HPP
|
||||
#define SPROUT_FUNCTIONAL_HASH_FWD_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash_fwd.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_FWD_HPP
|
||||
|
14
sprout/preprocessor/stringize.hpp
Normal file
14
sprout/preprocessor/stringize.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef SPROUT_PREPROCESSOR_STRINGIZE_HPP
|
||||
#define SPROUT_PREPROCESSOR_STRINGIZE_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
|
||||
#define SPROUT_PP_STRINGIZE_I(text) #text
|
||||
|
||||
//
|
||||
// SPROUT_PP_STRINGIZE
|
||||
//
|
||||
#define SPROUT_PP_STRINGIZE(text) SPROUT_PP_STRINGIZE_I(text)
|
||||
|
||||
#endif // #ifndef SPROUT_PREPROCESSOR_STRINGIZE_HPP
|
||||
|
13
sprout/preprocessor/unique_string.hpp
Normal file
13
sprout/preprocessor/unique_string.hpp
Normal file
|
@ -0,0 +1,13 @@
|
|||
#ifndef SPROUT_PREPROCESSOR_UNIQUE_STRING_HPP
|
||||
#define SPROUT_PREPROCESSOR_UNIQUE_STRING_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/preprocessor/stringize.hpp>
|
||||
|
||||
//
|
||||
// SPROUT_PP_UNIQUE_STRING
|
||||
//
|
||||
#define SPROUT_PP_UNIQUE_STRING __DATE__ " " __TIME__ " : " __FILE__ "(" SPROUT_PP_STRINGIZE(__LINE__) ")"
|
||||
|
||||
#endif // #ifndef SPROUT_PREPROCESSOR_UNIQUE_STRING_HPP
|
||||
|
14
sprout/random/unique_seed.hpp
Normal file
14
sprout/random/unique_seed.hpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP
|
||||
#define SPROUT_RANDOM_UNIQUE_SEED_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/functional/hash/hash.hpp>
|
||||
#include <sprout/preprocessor/unique_string.hpp>
|
||||
|
||||
//
|
||||
// SPROUT_UNIQUE_SEED
|
||||
//
|
||||
#define SPROUT_UNIQUE_SEED (::sprout::hash_value(SPROUT_PP_UNIQUE_STRING))
|
||||
|
||||
#endif // #ifndef SPROUT_RANDOM_UNIQUE_SEED_HPP
|
||||
|
Loading…
Reference in a new issue