From 06548409f880c662e18cc1f228c53721874da82e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 15 Aug 2019 02:50:16 -0400 Subject: [PATCH] hecl/hecl: Make Hash constexpr --- hecl/include/hecl/hecl.hpp | 41 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/hecl/include/hecl/hecl.hpp b/hecl/include/hecl/hecl.hpp index 57d51c93a..e4cdfc4c4 100644 --- a/hecl/include/hecl/hecl.hpp +++ b/hecl/include/hecl/hecl.hpp @@ -409,35 +409,36 @@ protected: uint64_t hash = 0; public: - Hash() = default; - operator bool() const { return hash != 0; } + constexpr Hash() = default; + constexpr Hash(const Hash&) = default; + constexpr Hash(Hash&&) noexcept = default; + constexpr Hash(uint64_t hashin) : hash(hashin) {} Hash(const void* buf, size_t len) : hash(XXH64((uint8_t*)buf, len, 0)) {} Hash(std::string_view str) : hash(XXH64((uint8_t*)str.data(), str.size(), 0)) {} Hash(std::wstring_view str) : hash(XXH64((uint8_t*)str.data(), str.size() * 2, 0)) {} - Hash(uint64_t hashin) : hash(hashin) {} - Hash(const Hash& other) { hash = other.hash; } - uint32_t val32() const { return uint32_t(hash) ^ uint32_t(hash >> 32); } - uint64_t val64() const { return uint64_t(hash); } - size_t valSizeT() const { return size_t(hash); } + + constexpr uint32_t val32() const { return uint32_t(hash) ^ uint32_t(hash >> 32); } + constexpr uint64_t val64() const { return uint64_t(hash); } + constexpr size_t valSizeT() const { return size_t(hash); } template - T valT() const; - Hash& operator=(const Hash& other) { - hash = other.hash; - return *this; - } - bool operator==(const Hash& other) const { return hash == other.hash; } - bool operator!=(const Hash& other) const { return hash != other.hash; } - bool operator<(const Hash& other) const { return hash < other.hash; } - bool operator>(const Hash& other) const { return hash > other.hash; } - bool operator<=(const Hash& other) const { return hash <= other.hash; } - bool operator>=(const Hash& other) const { return hash >= other.hash; } + constexpr T valT() const; + + constexpr Hash& operator=(const Hash& other) = default; + constexpr Hash& operator=(Hash&& other) noexcept = default; + constexpr bool operator==(const Hash& other) const { return hash == other.hash; } + constexpr bool operator!=(const Hash& other) const { return hash != other.hash; } + constexpr bool operator<(const Hash& other) const { return hash < other.hash; } + constexpr bool operator>(const Hash& other) const { return hash > other.hash; } + constexpr bool operator<=(const Hash& other) const { return hash <= other.hash; } + constexpr bool operator>=(const Hash& other) const { return hash >= other.hash; } + constexpr explicit operator bool() const { return hash != 0; } }; template <> -inline uint32_t Hash::valT() const { +constexpr uint32_t Hash::valT() const { return val32(); } template <> -inline uint64_t Hash::valT() const { +constexpr uint64_t Hash::valT() const { return val64(); }