110 lines
3.2 KiB
C++
110 lines
3.2 KiB
C++
// =====================================================================================================================
|
|
// fennec, a free and open source game engine
|
|
// Copyright © 2025 Medusa Slockbower
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
// =====================================================================================================================
|
|
|
|
#ifndef FENNEC_LANG_HASHING_H
|
|
#define FENNEC_LANG_HASHING_H
|
|
|
|
#include <fennec/lang/types.h>
|
|
#include <fennec/lang/type_traits.h>
|
|
#include <fennec/lang/bits.h>
|
|
|
|
namespace fennec
|
|
{
|
|
|
|
///
|
|
/// \brief Struct for hashing types, there is no default hashing function
|
|
/// \tparam Key The type to hash
|
|
template<typename Key> struct hash;
|
|
|
|
/// \brief Murmur3 Hash for 64-bit ints
|
|
template<>
|
|
struct hash<uint64_t> {
|
|
using type_t = uint64_t; //!< the type of the hash
|
|
/// \brief hash operator
|
|
/// \param x the value to hash
|
|
/// \returns an integer hash for the value
|
|
constexpr size_t operator()(uint64_t x) const {
|
|
// Murmur3
|
|
x ^= x >> 33U;
|
|
x *= 0xff51afd7ed558ccd;
|
|
x ^= x >> 33U;
|
|
x *= 0xc4ceb9fe1a85ec53;
|
|
x ^= x >> 33U;
|
|
return x;
|
|
}
|
|
};
|
|
|
|
/// \brief Hashing for integer types
|
|
/// \tparam IntT the integer type
|
|
template<typename IntT>
|
|
requires is_integral_v<IntT>
|
|
struct hash<IntT> : hash<uint64_t> {
|
|
using type_t = IntT; //!< the type of the hash
|
|
};
|
|
|
|
/// \brief Hashing for pointer types
|
|
/// \tparam PtrT The base type
|
|
template<typename PtrT>
|
|
struct hash<PtrT*> : hash<uintptr_t> {
|
|
using type_t = PtrT*; //!< the type of the hash
|
|
/// \brief hash operator
|
|
/// \param ptr the pointer to hash
|
|
/// \returns an integer hash for the value
|
|
constexpr size_t operator()(PtrT* ptr) const {
|
|
return hash<uintptr_t>::operator()((uintptr_t)(const void*)ptr);
|
|
}
|
|
};
|
|
|
|
// Float
|
|
template<>
|
|
struct hash<float> : hash<uint32_t> {
|
|
using type_t = float; //!< the type of the hash
|
|
/// \brief hash operator
|
|
/// \param x the value to hash
|
|
/// \returns an integer hash for the value
|
|
constexpr size_t operator()(float x) const {
|
|
return hash<uint32_t>::operator()(bit_cast<uint32_t>(x));
|
|
}
|
|
};
|
|
|
|
template<>
|
|
struct hash<double> : hash<uint64_t> {
|
|
using type_t = double; //!< the type of the hash
|
|
/// \brief hash operator
|
|
/// \param x the value to hash
|
|
/// \returns an integer hash for the value
|
|
constexpr size_t operator()(double x) const {
|
|
return hash<uint64_t>::operator()(bit_cast<uint64_t>(x));
|
|
}
|
|
};
|
|
|
|
|
|
///
|
|
/// \brief Pairs two hashes
|
|
/// \param x first hash
|
|
/// \param y second hash
|
|
/// \returns a pairing of the two hashes
|
|
constexpr size_t pair_hash(size_t x, size_t y) {
|
|
// Szudzik Pairing
|
|
return (x >= y ? (x * x) + x + y : (y * y) + x);
|
|
}
|
|
|
|
}
|
|
|
|
#endif // FENNEC_LANG_HASHING_H
|