diff options
| author | Maxime Coste <mawww@kakoune.org> | 2017-10-17 22:45:17 +0800 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2017-10-20 12:21:22 +0800 |
| commit | d486ea84e57bdabec07fe38a0bb4fb5bdf21848f (patch) | |
| tree | 619966c9d6f0a58b329f646be6926b45b72e5fb1 /src | |
| parent | ddff35e5ab6a9bed29c49547eaee91637854f029 (diff) | |
Constexprify various hash functions
Diffstat (limited to 'src')
| -rw-r--r-- | src/color.hh | 2 | ||||
| -rw-r--r-- | src/coord.hh | 4 | ||||
| -rw-r--r-- | src/hash.hh | 21 | ||||
| -rw-r--r-- | src/keys.hh | 2 | ||||
| -rw-r--r-- | src/string.hh | 2 | ||||
| -rw-r--r-- | 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<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); } 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; } |
