1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-30 23:37:17 +03:00

Constexprify various hash functions

This commit is contained in:
Maxime Coste 2017-10-17 22:45:17 +08:00
parent ddff35e5ab
commit d486ea84e5
6 changed files with 21 additions and 14 deletions

View File

@ -55,7 +55,7 @@ void option_from_string(StringView str, Color& color);
bool is_color_name(StringView color);
inline size_t hash_value(const Color& val)
constexpr size_t hash_value(const Color& val)
{
return val.color == Color::RGB ?
hash_values(val.color, val.r, val.g, val.b)

View File

@ -81,7 +81,7 @@ struct LineAndColumn
return line != other.line or column != other.column;
}
friend size_t hash_value(const EffectiveType& val)
friend constexpr size_t hash_value(const EffectiveType& val)
{
return hash_values(val.line, val.column);
}
@ -116,7 +116,7 @@ struct BufferCoordAndTarget : BufferCoord
ColumnCount target;
};
inline size_t hash_value(const BufferCoordAndTarget& val)
constexpr size_t hash_value(const BufferCoordAndTarget& val)
{
return hash_values(val.line, val.column, val.target);
}

View File

@ -12,39 +12,46 @@ namespace Kakoune
size_t hash_data(const char* data, size_t len);
template<typename... Type>
size_t hash_value(const Type&... val)
constexpr size_t hash_value(const Type&... val)
{
static_assert(sizeof...(Type) == 1, "");
return std::hash<Type...>()(val...);
}
template<typename Type>
std::enable_if_t<std::is_integral<Type>::value, size_t>
constexpr hash_value(const Type& val)
{
return (size_t)val;
}
template<typename Type>
std::enable_if_t<std::is_enum<Type>::value, size_t>
hash_value(const Type& val)
constexpr hash_value(const Type& val)
{
return hash_value((std::underlying_type_t<Type>)val);
}
template<typename Type>
size_t hash_values(Type&& t)
constexpr size_t hash_values(Type&& t)
{
return hash_value(std::forward<Type>(t));
}
inline size_t combine_hash(size_t lhs, size_t rhs)
constexpr size_t combine_hash(size_t lhs, size_t rhs)
{
return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
}
template<typename Type, typename... RemainingTypes>
size_t hash_values(Type&& t, RemainingTypes&&... rt)
constexpr size_t hash_values(Type&& t, RemainingTypes&&... rt)
{
size_t seed = hash_values(std::forward<RemainingTypes>(rt)...);
return combine_hash(seed, hash_value(std::forward<Type>(t)));
}
template<typename T1, typename T2>
size_t hash_value(const std::pair<T1, T2>& val)
constexpr size_t hash_value(const std::pair<T1, T2>& val)
{
return hash_values(val.first, val.second);
}
@ -52,7 +59,7 @@ size_t hash_value(const std::pair<T1, T2>& val)
template<typename Type>
struct Hash
{
size_t operator()(const Type& val) const
constexpr size_t operator()(const Type& val) const
{
return hash_value(val);
}

View File

@ -112,7 +112,7 @@ constexpr Codepoint encode_coord(DisplayCoord coord) { return (Codepoint)(((int)
constexpr Key resize(DisplayCoord dim) { return { Key::Modifiers::Resize, encode_coord(dim) }; }
inline size_t hash_value(const Key& key) { return hash_values(key.modifiers, key.key); }
constexpr size_t hash_value(const Key& key) { return hash_values(key.modifiers, key.key); }
}

View File

@ -19,7 +19,7 @@ class StringOps
public:
using value_type = CharType;
friend inline size_t hash_value(const Type& str)
friend constexpr size_t hash_value(const Type& str)
{
return hash_data(str.data(), (int)str.length());
}

View File

@ -115,8 +115,8 @@ public:
[[gnu::always_inline]]
explicit constexpr operator bool() const { return m_value; }
friend size_t hash_value(RealType val) { return hash_value(val.m_value); }
friend size_t abs(RealType val) { return val.m_value < ValueType(0) ? -val.m_value : val.m_value; }
friend constexpr size_t hash_value(RealType val) { return hash_value(val.m_value); }
friend constexpr size_t abs(RealType val) { return val.m_value < ValueType(0) ? -val.m_value : val.m_value; }
explicit operator size_t() { kak_assert(m_value >= 0); return (size_t)m_value; }