From d486ea84e57bdabec07fe38a0bb4fb5bdf21848f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 17 Oct 2017 22:45:17 +0800 Subject: [PATCH] Constexprify various hash functions --- src/color.hh | 2 +- src/coord.hh | 4 ++-- src/hash.hh | 21 ++++++++++++++------- src/keys.hh | 2 +- src/string.hh | 2 +- src/units.hh | 4 ++-- 6 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/color.hh b/src/color.hh index d8a2fcce..e4ef24cb 100644 --- a/src/color.hh +++ b/src/color.hh @@ -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) diff --git a/src/coord.hh b/src/coord.hh index 06c1032d..6109bf14 100644 --- a/src/coord.hh +++ b/src/coord.hh @@ -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); } diff --git a/src/hash.hh b/src/hash.hh index 35e89e84..e90232d8 100644 --- a/src/hash.hh +++ b/src/hash.hh @@ -12,39 +12,46 @@ namespace Kakoune size_t hash_data(const char* data, size_t len); template -size_t hash_value(const Type&... val) +constexpr size_t hash_value(const Type&... val) { static_assert(sizeof...(Type) == 1, ""); return std::hash()(val...); } +template +std::enable_if_t::value, size_t> +constexpr hash_value(const Type& val) +{ + return (size_t)val; +} + template std::enable_if_t::value, size_t> -hash_value(const Type& val) +constexpr hash_value(const Type& val) { return hash_value((std::underlying_type_t)val); } template -size_t hash_values(Type&& t) +constexpr size_t hash_values(Type&& t) { return hash_value(std::forward(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 -size_t hash_values(Type&& t, RemainingTypes&&... rt) +constexpr size_t hash_values(Type&& t, RemainingTypes&&... rt) { size_t seed = hash_values(std::forward(rt)...); return combine_hash(seed, hash_value(std::forward(t))); } template -size_t hash_value(const std::pair& val) +constexpr size_t hash_value(const std::pair& val) { return hash_values(val.first, val.second); } @@ -52,7 +59,7 @@ size_t hash_value(const std::pair& val) template struct Hash { - size_t operator()(const Type& val) const + constexpr size_t operator()(const Type& val) const { return hash_value(val); } diff --git a/src/keys.hh b/src/keys.hh index 3d023453..b1219fc2 100644 --- a/src/keys.hh +++ b/src/keys.hh @@ -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); } } diff --git a/src/string.hh b/src/string.hh index a4d62804..ddb5be8d 100644 --- a/src/string.hh +++ b/src/string.hh @@ -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()); } diff --git a/src/units.hh b/src/units.hh index 0218055f..1c10c3af 100644 --- a/src/units.hh +++ b/src/units.hh @@ -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; }