BAN: Add basic hashing functionality

This commit is contained in:
Bananymous 2023-02-10 02:59:30 +02:00
parent 4f8fcf0bd7
commit 7630df84ad
1 changed files with 50 additions and 0 deletions

50
BAN/include/BAN/Hash.h Normal file
View File

@ -0,0 +1,50 @@
#pragma once
#include <BAN/Traits.h>
#include <stddef.h>
#include <stdint.h>
namespace BAN
{
template<typename T>
struct hash;
using hash_t = uint32_t;
inline constexpr hash_t u32_hash(uint32_t val)
{
val = ((val >> 16) ^ val) * 0x119de1f3;
val = ((val >> 16) ^ val) * 0x119de1f3;
val = ((val >> 16) ^ val);
return val;
}
inline constexpr hash_t u64_hash(uint64_t val)
{
hash_t low = u32_hash(val);
hash_t high = u32_hash(val >> 32);
return low ^ high;
}
template<integral T>
struct hash<T>
{
constexpr hash_t operator()(T val) const
{
if constexpr(sizeof(val) <= sizeof(uint32_t))
return u32_hash(val);
return u64_hash(val);
}
};
template<pointer T>
struct hash<T>
{
constexpr hash_t operator()(T val) const
{
return hash<uintptr_t>()((uintptr_t)val);
}
};
}