From 304f5623d8502ddc4469785e1ae7c89139d5d73b Mon Sep 17 00:00:00 2001 From: Maximilien Colange Date: Wed, 7 Mar 2018 17:21:50 +0100 Subject: [PATCH] Clean a hash function definition * spot/misc/hashfunc.hh: make the definition of FNV hash magic constants more generic --- spot/misc/hashfunc.hh | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/spot/misc/hashfunc.hh b/spot/misc/hashfunc.hh index 60d7e2a09..0ab4fba0c 100644 --- a/spot/misc/hashfunc.hh +++ b/spot/misc/hashfunc.hh @@ -23,7 +23,7 @@ #pragma once #include -#include +#include namespace spot { @@ -64,25 +64,30 @@ namespace spot return (key >> 3) * 2654435761U; } + /// Struct for Fowler-Noll-Vo parameters - template + template struct fnv {}; /// Fowler-Noll-Vo hash parameters for 32 bits - template<> - struct fnv + template + struct fnv::type> { - static constexpr uint32_t init = 2166136261UL; - static constexpr uint32_t prime = 16777619UL; + static_assert(std::is_integral::value && std::is_unsigned::value, + "Fowler-Noll-Vo hash requires an unsigned integral type"); + static constexpr T init = 2166136261UL; + static constexpr T prime = 16777619UL; }; /// Fowler-Noll-Vo hash parameters for 64 bits - template<> - struct fnv + template + struct fnv::type> { - static constexpr uint64_t init = 14695981039346656037UL; - static constexpr uint64_t prime = 1099511628211UL; + static_assert(std::is_integral::value && std::is_unsigned::value, + "Fowler-Noll-Vo hash requires an unsigned integral type"); + static constexpr T init = 14695981039346656037ULL; + static constexpr T prime = 1099511628211ULL; }; /// \brief Fowler-Noll-Vo hash function