2021-04-10 00:41:33 +00:00
|
|
|
#ifndef MISC_HASH_H
|
|
|
|
#define MISC_HASH_H
|
|
|
|
|
2021-11-13 15:14:50 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
|
2021-04-10 00:41:33 +00:00
|
|
|
namespace Misc
|
|
|
|
{
|
|
|
|
/// Implemented similar to the boost::hash_combine
|
2021-11-13 15:14:50 +00:00
|
|
|
template <class Seed, class T>
|
|
|
|
inline void hashCombine(Seed& seed, const T& v)
|
2021-04-10 00:41:33 +00:00
|
|
|
{
|
2021-11-13 15:14:50 +00:00
|
|
|
static_assert(sizeof(Seed) >= sizeof(std::size_t), "Resulting hash will be truncated");
|
2021-04-10 00:41:33 +00:00
|
|
|
std::hash<T> hasher;
|
2021-11-13 15:14:50 +00:00
|
|
|
seed ^= static_cast<Seed>(hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2));
|
2021-04-10 00:41:33 +00:00
|
|
|
}
|
2023-04-02 20:00:39 +00:00
|
|
|
|
|
|
|
// Comes from https://stackoverflow.com/questions/2634690/good-hash-function-for-a-2d-index
|
|
|
|
// Effective Java (2nd edition) is cited as the source
|
|
|
|
inline std::size_t hash2dCoord(int32_t x, int32_t y)
|
|
|
|
{
|
|
|
|
return (53 + std::hash<int32_t>{}(x)) * 53 + std::hash<int32_t>{}(y);
|
|
|
|
}
|
2021-04-10 00:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-13 15:14:50 +00:00
|
|
|
#endif
|