From 35ebf5c413db690896aa1774056b6862e141e92f Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 9 May 2024 09:44:00 +0200 Subject: [PATCH] hashed_string: avoid unnecessary class specializations --- src/entt/core/hashed_string.hpp | 37 ++++++++++++++++---------------- test/entt/core/hashed_string.cpp | 4 ++-- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/entt/core/hashed_string.hpp b/src/entt/core/hashed_string.hpp index 51768ce29..6f100db2d 100644 --- a/src/entt/core/hashed_string.hpp +++ b/src/entt/core/hashed_string.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "fwd.hpp" namespace entt { @@ -10,20 +11,21 @@ namespace entt { /*! @cond TURN_OFF_DOXYGEN */ namespace internal { -template -struct fnv1a_traits; - -template<> -struct fnv1a_traits { - static constexpr std::uint32_t offset = 2166136261; - static constexpr std::uint32_t prime = 16777619; -}; +[[nodiscard]] inline constexpr auto offset() noexcept { + if constexpr(std::is_same_v) { + return 2166136261; + } else if constexpr(std::is_same_v) { + return 14695981039346656037ull; + } +} -template<> -struct fnv1a_traits { - static constexpr std::uint64_t offset = 14695981039346656037ull; - static constexpr std::uint64_t prime = 1099511628211ull; -}; +[[nodiscard]] inline constexpr auto prime() noexcept { + if constexpr(std::is_same_v) { + return 16777619; + } else if constexpr(std::is_same_v) { + return 1099511628211ull; + } +} template struct basic_hashed_string { @@ -57,7 +59,6 @@ struct basic_hashed_string { template class basic_hashed_string: internal::basic_hashed_string { using base_type = internal::basic_hashed_string; - using traits_type = internal::fnv1a_traits; struct const_wrapper { // non-explicit constructor on purpose @@ -69,10 +70,10 @@ class basic_hashed_string: internal::basic_hashed_string { // Fowler–Noll–Vo hash function v. 1a - the good [[nodiscard]] static constexpr auto helper(const Char *str) noexcept { - base_type base{str, 0u, traits_type::offset}; + base_type base{str, 0u, internal::offset()}; for(; str[base.length]; ++base.length) { - base.hash = (base.hash ^ static_cast(str[base.length])) * traits_type::prime; + base.hash = (base.hash ^ static_cast(str[base.length])) * internal::prime(); } return base; @@ -80,10 +81,10 @@ class basic_hashed_string: internal::basic_hashed_string { // Fowler–Noll–Vo hash function v. 1a - the good [[nodiscard]] static constexpr auto helper(const Char *str, const std::size_t len) noexcept { - base_type base{str, len, traits_type::offset}; + base_type base{str, len, internal::offset()}; for(size_type pos{}; pos < len; ++pos) { - base.hash = (base.hash ^ static_cast(str[pos])) * traits_type::prime; + base.hash = (base.hash ^ static_cast(str[pos])) * internal::prime(); } return base; diff --git a/test/entt/core/hashed_string.cpp b/test/entt/core/hashed_string.cpp index 9f66a6720..b5a346ef6 100644 --- a/test/entt/core/hashed_string.cpp +++ b/test/entt/core/hashed_string.cpp @@ -68,7 +68,7 @@ TEST_F(HashedString, Empty) { const entt::hashed_string hs{}; ASSERT_EQ(hs.size(), 0u); - ASSERT_EQ(static_cast(hs), entt::internal::fnv1a_traits::offset); + ASSERT_EQ(static_cast(hs), entt::internal::offset()); ASSERT_EQ(static_cast(hs), nullptr); } @@ -162,7 +162,7 @@ TEST_F(HashedWString, Empty) { const entt::hashed_wstring hws{}; ASSERT_EQ(hws.size(), 0u); - ASSERT_EQ(static_cast(hws), entt::internal::fnv1a_traits::offset); + ASSERT_EQ(static_cast(hws), entt::internal::offset()); ASSERT_EQ(static_cast(hws), nullptr); }