Sprout/sprout/functional/hash/hash.hpp

337 lines
9.7 KiB
C++
Raw Normal View History

#ifndef SPROUT_FUNCTIONAL_HASH_HASH_HPP
#define SPROUT_FUNCTIONAL_HASH_HASH_HPP
2011-10-12 20:28:33 +00:00
#include <cstddef>
#include <limits>
2012-06-20 15:57:27 +00:00
#include <type_traits>
2011-10-12 20:28:33 +00:00
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/functional/hash/hash_fwd.hpp>
2012-06-20 15:57:27 +00:00
#include <sprout/type_traits/enabler_if.hpp>
2011-10-12 20:28:33 +00:00
namespace sprout {
//
// hash_value
//
inline SPROUT_CONSTEXPR std::size_t hash_value(bool v);
inline SPROUT_CONSTEXPR std::size_t hash_value(char v);
inline SPROUT_CONSTEXPR std::size_t hash_value(unsigned char v);
inline SPROUT_CONSTEXPR std::size_t hash_value(signed char v);
inline SPROUT_CONSTEXPR std::size_t hash_value(char16_t v);
inline SPROUT_CONSTEXPR std::size_t hash_value(char32_t v);
inline SPROUT_CONSTEXPR std::size_t hash_value(wchar_t v);
inline SPROUT_CONSTEXPR std::size_t hash_value(short v);
inline SPROUT_CONSTEXPR std::size_t hash_value(unsigned short v);
inline SPROUT_CONSTEXPR std::size_t hash_value(int v);
inline SPROUT_CONSTEXPR std::size_t hash_value(unsigned int v);
inline SPROUT_CONSTEXPR std::size_t hash_value(long v);
inline SPROUT_CONSTEXPR std::size_t hash_value(unsigned long v);
inline SPROUT_CONSTEXPR std::size_t hash_value(long long v);
inline SPROUT_CONSTEXPR std::size_t hash_value(unsigned long long v);
//inline SPROUT_CONSTEXPR std::size_t hash_value(float v);
//inline SPROUT_CONSTEXPR std::size_t hash_value(double v);
//inline SPROUT_CONSTEXPR std::size_t hash_value(long double v);
2012-06-20 15:57:27 +00:00
template<
typename T,
typename sprout::enabler_if<std::is_pointer<typename std::remove_reference<T>::type>::value>::type
>
inline SPROUT_CONSTEXPR std::size_t hash_value(T&& v);
2011-10-12 20:28:33 +00:00
template<typename T, std::size_t N>
inline SPROUT_CONSTEXPR std::size_t hash_value(T const (&v)[N]);
2011-10-12 20:28:33 +00:00
namespace hash_detail {
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value_signed_2(T val, int length, std::size_t seed, T positive, std::size_t i) {
2011-10-12 20:28:33 +00:00
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>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value_signed_1(T val, int length, std::size_t seed, T positive) {
2011-10-12 20:28:33 +00:00
return hash_value_signed_2(val, length, seed, positive, length * std::numeric_limits<std::size_t>::digits);
}
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value_signed(T val) {
2011-10-12 20:28:33 +00:00
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>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value_unsigned_2(T val, int length, std::size_t seed, std::size_t i) {
2011-10-12 20:28:33 +00:00
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>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value_unsigned_1(T val, int length, std::size_t seed) {
2011-10-12 20:28:33 +00:00
return hash_value_unsigned_2(val, length, seed, length * std::numeric_limits<std::size_t>::digits);
}
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value_unsigned(T val) {
2011-10-12 20:28:33 +00:00
return sprout::hash_detail::hash_value_unsigned_1(
val,
(std::numeric_limits<T>::digits - 1) / std::numeric_limits<std::size_t>::digits,
0
);
}
2012-10-06 04:53:07 +00:00
inline std::size_t
hash_value_pointer_1(std::size_t x) {
2011-10-12 20:28:33 +00:00
return x + (x >> 3);
}
template<typename T>
2012-10-06 04:53:07 +00:00
std::size_t
hash_value_pointer(T const* v) {
2011-10-12 20:28:33 +00:00
return sprout::hash_detail::hash_value_pointer_1(static_cast<std::size_t>(reinterpret_cast<std::ptrdiff_t>(v)));
}
} // namespace hash_detail
//
// hash_value
//
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(bool v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(char v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(unsigned char v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(signed char v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(char16_t v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(char32_t v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(wchar_t v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(short v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(unsigned short v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(int v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(unsigned int v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(long v) {
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(unsigned long v) {
2011-10-12 20:28:33 +00:00
return static_cast<std::size_t>(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(long long v) {
2011-10-12 20:28:33 +00:00
return sprout::hash_detail::hash_value_signed(v);
}
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(unsigned long long v) {
2011-10-12 20:28:33 +00:00
return sprout::hash_detail::hash_value_unsigned(v);
}
2012-06-20 15:57:27 +00:00
template<
typename T,
typename sprout::enabler_if<std::is_pointer<typename std::remove_reference<T>::type>::value>::type = sprout::enabler
>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(T&& v) {
2011-10-12 20:28:33 +00:00
return sprout::hash_detail::hash_value_pointer(v);
}
2012-06-20 15:57:27 +00:00
template<typename T, std::size_t N>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_value(T const (&v)[N]) {
2012-06-20 15:57:27 +00:00
return sprout::hash_range(&v[0], &v[0] + N);
}
2011-10-12 20:28:33 +00:00
2011-10-29 03:02:21 +00:00
//
// to_hash
//
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
to_hash(T const& v) {
2011-10-29 03:02:21 +00:00
using sprout::hash_value;
return hash_value(v);
}
2011-10-12 20:28:33 +00:00
//
// hash_combine
//
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_combine(std::size_t seed, T const& v) {
2011-10-29 03:02:21 +00:00
return seed ^ (sprout::to_hash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
2011-10-12 20:28:33 +00:00
}
//
// hash_range
//
template<typename Iterator>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_range(std::size_t seed, Iterator first, Iterator last) {
2011-10-12 20:28:33 +00:00
return first != last
? sprout::hash_range(sprout::hash_combine(seed, *first), sprout::next(first), last)
: seed
;
}
2012-07-15 14:06:29 +00:00
template<typename Iterator>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_range(Iterator first, Iterator last) {
2012-07-15 14:06:29 +00:00
return sprout::hash_range(0, first, last);
}
namespace detail {
template<typename T>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_values_combine_impl(std::size_t seed, T const& v) {
2012-07-15 14:06:29 +00:00
return sprout::hash_combine(seed, v);
}
template<typename Head, typename... Tail>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_values_combine_impl(std::size_t seed, Head const& head, Tail const&... tail) {
2012-07-15 14:06:29 +00:00
return sprout::detail::hash_values_combine_impl(sprout::hash_combine(seed, head), tail...);
}
} // namespace detail
//
// hash_values_combine
//
template<typename... Args>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_values_combine(std::size_t seed, Args const&... args) {
2012-07-15 14:06:29 +00:00
return sprout::detail::hash_values_combine_impl(seed, args...);
}
//
// hash_values
//
template<typename... Args>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR std::size_t
hash_values(Args const&... args) {
2012-07-15 14:06:29 +00:00
return sprout::hash_values_combine(0, args...);
}
2011-10-12 20:28:33 +00:00
//
// hash
//
template<typename T>
struct hash {
public:
typedef T argument_type;
typedef std::size_t result_type;
public:
SPROUT_CONSTEXPR std::size_t operator()(T const& v) const {
2011-10-29 03:02:21 +00:00
return sprout::to_hash(v);
}
};
2012-06-20 15:57:27 +00:00
template<typename T>
struct hash<T const>
: public sprout::hash<T>
{};
template<typename T>
struct hash<T volatile>
: public sprout::hash<T>
{};
template<typename T>
struct hash<T const volatile>
: public sprout::hash<T>
{};
2011-10-12 20:28:33 +00:00
#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 { \
2012-06-20 15:57:27 +00:00
return sprout::to_hash(v); \
2011-10-12 20:28:33 +00:00
} \
}
#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 { \
2012-06-20 15:57:27 +00:00
return sprout::to_hash(v); \
2011-10-12 20:28:33 +00:00
} \
}
SPROUT_HASH_SPECIALIZE(bool);
SPROUT_HASH_SPECIALIZE(char);
SPROUT_HASH_SPECIALIZE(signed char);
SPROUT_HASH_SPECIALIZE(unsigned char);
2012-04-09 16:38:55 +00:00
SPROUT_HASH_SPECIALIZE(char16_t);
SPROUT_HASH_SPECIALIZE(char32_t);
SPROUT_HASH_SPECIALIZE(wchar_t);
2011-10-12 20:28:33 +00:00
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<typename T>
2012-06-20 15:57:27 +00:00
struct hash<T*> {
2011-10-29 03:02:21 +00:00
public:
2012-06-20 15:57:27 +00:00
typedef T* argument_type;
2011-10-29 03:02:21 +00:00
typedef std::size_t result_type;
public:
2012-06-20 15:57:27 +00:00
std::size_t operator()(T* v) const {
2011-10-29 03:02:21 +00:00
return sprout::to_hash(v);
}
};
2011-10-12 20:28:33 +00:00
} //namespace sprout
#endif // #ifndef SPROUT_FUNCTIONAL_HASH_HASH_HPP