fennec
Loading...
Searching...
No Matches
hashing.h
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef FENNEC_LANG_HASHING_H
20#define FENNEC_LANG_HASHING_H
21
22#include <fennec/lang/types.h>
24#include <fennec/lang/bits.h>
25
26namespace fennec
27{
28
32template<typename Key> struct hash;
33
34// Murmur3 Hash for 64-bit ints
35template<>
36struct hash<uint64_t> {
37 using type_t = uint64_t;
38 constexpr size_t operator()(uint64_t x) const {
39 // Murmur3
40 x ^= x >> 33U;
41 x *= 0xff51afd7ed558ccd;
42 x ^= x >> 33U;
43 x *= 0xc4ceb9fe1a85ec53;
44 x ^= x >> 33U;
45 return x;
46 }
47};
48
49// Wrapper for casting ints
50template<typename IntT>
51 requires is_integral_v<IntT>
52struct hash<IntT> : hash<uint64_t> {
53 using type_t = IntT;
54};
55
56// Wrapper for pointers
57template<typename PtrT>
58struct hash<PtrT*> : hash<uintptr_t> {
59 constexpr size_t operator()(PtrT* ptr) const {
60 return hash<uintptr_t>::operator()((uintptr_t)(const void*)ptr);
61 }
62};
63
64// Float
65template<>
66struct hash<float> : hash<uint32_t> {
67 constexpr size_t operator()(float x) const {
68 return hash<uint32_t>::operator()(bit_cast<uint32_t>(x));
69 }
70};
71
72template<>
73struct hash<double> : hash<uint64_t> {
74 constexpr size_t operator()(double x) const {
75 return hash<uint64_t>::operator()(bit_cast<uint64_t>(x));
76 }
77};
78
79
85constexpr size_t pair_hash(size_t x, size_t y) {
86 // Szudzik Pairing
87 return (x >= y ? (x * x) + x + y : (y * y) + x);
88}
89
90}
91
92#endif // FENNEC_LANG_HASHING_H
Bit Manipulation
constexpr genType y()
Definition constants.h:672
Struct for hashing types, there is no default hashing function.
Definition hashing.h:32
Type Traits
Types
::uint64_t uint64_t
Unsigned 64-bit integer.
Definition types.h:275