Clean a hash function definition
* spot/misc/hashfunc.hh: make the definition of FNV hash magic constants more generic
This commit is contained in:
parent
85bc05e737
commit
304f5623d8
1 changed files with 15 additions and 10 deletions
|
|
@ -23,7 +23,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace spot
|
namespace spot
|
||||||
{
|
{
|
||||||
|
|
@ -64,25 +64,30 @@ namespace spot
|
||||||
return (key >> 3) * 2654435761U;
|
return (key >> 3) * 2654435761U;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Struct for Fowler-Noll-Vo parameters
|
/// Struct for Fowler-Noll-Vo parameters
|
||||||
template<class T>
|
template<class T, class Enable = void>
|
||||||
struct fnv
|
struct fnv
|
||||||
{};
|
{};
|
||||||
|
|
||||||
/// Fowler-Noll-Vo hash parameters for 32 bits
|
/// Fowler-Noll-Vo hash parameters for 32 bits
|
||||||
template<>
|
template<class T>
|
||||||
struct fnv<uint32_t>
|
struct fnv<T, typename std::enable_if<sizeof(T) == 4>::type>
|
||||||
{
|
{
|
||||||
static constexpr uint32_t init = 2166136261UL;
|
static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
|
||||||
static constexpr uint32_t prime = 16777619UL;
|
"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
|
/// Fowler-Noll-Vo hash parameters for 64 bits
|
||||||
template<>
|
template<class T>
|
||||||
struct fnv<uint64_t>
|
struct fnv<T, typename std::enable_if<sizeof(T) == 8>::type>
|
||||||
{
|
{
|
||||||
static constexpr uint64_t init = 14695981039346656037UL;
|
static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
|
||||||
static constexpr uint64_t prime = 1099511628211UL;
|
"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
|
/// \brief Fowler-Noll-Vo hash function
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue