Improving hash performance for pairs of integral types

* spot/misc/hash.hh: Here
This commit is contained in:
philipp 2021-07-09 23:05:50 +02:00
parent f4776efb63
commit 3da41aa9a1

View file

@ -22,6 +22,7 @@
#pragma once #pragma once
#include <climits>
#include <string> #include <string>
#include <functional> #include <functional>
#include <spot/misc/hashfunc.hh> #include <spot/misc/hashfunc.hh>
@ -79,6 +80,20 @@ namespace spot
{ {
template<typename T, typename U> template<typename T, typename U>
std::size_t operator()(const std::pair<T, U> &p) const noexcept std::size_t operator()(const std::pair<T, U> &p) const noexcept
{
if constexpr (std::is_integral<T>::value
&& sizeof(T) <= sizeof(std::size_t)/2
&& std::is_integral<U>::value
&& sizeof(U) <= sizeof(std::size_t)/2)
{
constexpr unsigned shift = (sizeof(std::size_t)/2)*CHAR_BIT;
std::size_t h = p.first;
h <<= shift;
h += p.second;
return h;
}
else
{ {
std::hash<T> th; std::hash<T> th;
std::hash<U> uh; std::hash<U> uh;
@ -86,6 +101,7 @@ namespace spot
return wang32_hash(static_cast<size_t>(th(p.first)) ^ return wang32_hash(static_cast<size_t>(th(p.first)) ^
static_cast<size_t>(uh(p.second))); static_cast<size_t>(uh(p.second)));
} }
}
}; };
// From primes.utm.edu shuffled. This primes are used when we simulate // From primes.utm.edu shuffled. This primes are used when we simulate