2014-12-16 19:57:19 +01:00
|
|
|
#ifndef hash_hh_INCLUDED
|
|
|
|
#define hash_hh_INCLUDED
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <functional>
|
|
|
|
|
2015-02-02 19:46:55 +01:00
|
|
|
#include <cstddef>
|
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
size_t hash_data(const char* data, size_t len);
|
|
|
|
|
2015-02-07 21:15:22 +01:00
|
|
|
template<typename... Type>
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t hash_value(const Type&... val)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
2015-02-07 21:15:22 +01:00
|
|
|
static_assert(sizeof...(Type) == 1, "");
|
|
|
|
return std::hash<Type...>()(val...);
|
2014-12-16 19:57:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-17 16:45:17 +02:00
|
|
|
template<typename Type>
|
|
|
|
std::enable_if_t<std::is_integral<Type>::value, size_t>
|
|
|
|
constexpr hash_value(const Type& val)
|
|
|
|
{
|
|
|
|
return (size_t)val;
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
template<typename Type>
|
2016-03-12 16:27:54 +01:00
|
|
|
std::enable_if_t<std::is_enum<Type>::value, size_t>
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr hash_value(const Type& val)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
2016-03-12 16:27:54 +01:00
|
|
|
return hash_value((std::underlying_type_t<Type>)val);
|
2014-12-16 19:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t hash_values(Type&& t)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
|
|
|
return hash_value(std::forward<Type>(t));
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t combine_hash(size_t lhs, size_t rhs)
|
2015-06-20 16:04:31 +02:00
|
|
|
{
|
|
|
|
return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
template<typename Type, typename... RemainingTypes>
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t hash_values(Type&& t, RemainingTypes&&... rt)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
|
|
|
size_t seed = hash_values(std::forward<RemainingTypes>(rt)...);
|
2015-06-20 16:04:31 +02:00
|
|
|
return combine_hash(seed, hash_value(std::forward<Type>(t)));
|
2014-12-16 19:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T1, typename T2>
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t hash_value(const std::pair<T1, T2>& val)
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
|
|
|
return hash_values(val.first, val.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Type>
|
|
|
|
struct Hash
|
|
|
|
{
|
2017-10-17 16:45:17 +02:00
|
|
|
constexpr size_t operator()(const Type& val) const
|
2014-12-16 19:57:19 +01:00
|
|
|
{
|
|
|
|
return hash_value(val);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-06 20:47:26 +01:00
|
|
|
// Traits specifying if two types have compatible hashing, that is,
|
|
|
|
// if lhs == rhs => hash_value(lhs) == hash_value(rhs)
|
|
|
|
template<typename Lhs, typename Rhs>
|
2017-08-18 03:17:02 +02:00
|
|
|
struct HashCompatible : std::false_type {};
|
2017-03-06 20:47:26 +01:00
|
|
|
|
2017-08-18 03:17:02 +02:00
|
|
|
template<typename T> struct HashCompatible<T, T> : std::true_type {};
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs>
|
|
|
|
constexpr bool IsHashCompatible = HashCompatible<Lhs, Rhs>::value;
|
2017-05-11 00:22:24 +02:00
|
|
|
|
2014-12-16 19:57:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // hash_hh_INCLUDED
|