IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
hash.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/constants.hxx>
8#include <ice/utility.hxx>
9
10namespace ice
11{
12
13 template<typename T>
14 constexpr auto hash(T value) noexcept -> ice::u64;
15
16 template<>
17 constexpr auto hash(std::u8string_view value) noexcept -> ice::u64;
18
19 template<>
20 constexpr auto hash(ice::utf8 const* value) noexcept -> ice::u64;
21
22 template<>
23 constexpr auto hash(std::string_view value) noexcept -> ice::u64;
24
25 template<>
26 constexpr auto hash(char const* value) noexcept -> ice::u64;
27
28
29
30 template<typename T>
31 constexpr auto hash32(T value) noexcept -> uint32_t;
32
33 template<>
34 constexpr auto hash32(std::u8string_view value) noexcept -> ice::u32;
35
36 template<>
37 constexpr auto hash32(ice::utf8 const* value) noexcept -> ice::u32;
38
39 template<>
40 constexpr auto hash32(std::string_view value) noexcept -> ice::u32;
41
42 template<>
43 constexpr auto hash32(char const* value) noexcept -> ice::u32;
44
45
46 template<typename T>
47 constexpr auto hash_from_ptr(T* ptr) noexcept -> ice::u64;
48
49
50 // IMPLEMENTATION DETAILS
51
52
53 template<typename T>
54 constexpr auto hash(T value) noexcept -> ice::u64
55 {
57 return static_cast<ice::u64>(value);
58 }
59
60 template<>
61 constexpr auto hash(std::u8string_view value) noexcept -> ice::u64
62 {
63 using namespace ice::detail::murmur2_hash;
64
66 return result.h[0];
67 }
68
69 template<>
70 constexpr auto hash(ice::utf8 const* value) noexcept -> ice::u64
71 {
72 return ice::hash(std::u8string_view{ value });
73 }
74
75 template<>
76 constexpr auto hash(std::string_view value) noexcept -> ice::u64
77 {
78 using namespace ice::detail::murmur2_hash;
79
81 return result.h[0];
82 }
83
84 template<>
85 constexpr auto hash(char const* value) noexcept -> ice::u64
86 {
87 return ice::hash(std::string_view{ value });
88 }
89
90
91 template<typename T>
92 constexpr auto hash32(T value) noexcept -> ice::u32
93 {
94 return static_cast<ice::u32>(value);
95 }
96
97 template<>
98 constexpr auto hash32(std::u8string_view value) noexcept -> ice::u32
99 {
100 using namespace ice::detail::murmur3_hash;
101
103 return result.h[0];
104 }
105
106 template<>
107 constexpr auto hash32(ice::utf8 const* value) noexcept -> ice::u32
108 {
109 return ice::hash32(std::u8string_view{ value });
110 }
111
112 template<>
113 constexpr auto hash32(std::string_view value) noexcept -> ice::u32
114 {
115 using namespace ice::detail::murmur3_hash;
116
118 return result.h[0];
119 }
120
121 template<>
122 constexpr auto hash32(char const* value) noexcept -> ice::u32
123 {
124 return ice::hash32(std::string_view{ value });
125 }
126
127
128 template<typename T>
129 constexpr auto hash_from_ptr(T* ptr) noexcept -> ice::u64
130 {
131 return hash(reinterpret_cast<ice::uptr>(ptr));
132 }
133
134
135 // COMPILE TILE CHECKS
136
137
138 namespace _validation
139 {
140
141 // Check if numeric values behave in hashing scenarios as expected.
142 static_assert(ice::hash32(0x0000'0000) == ice::hash(0x0000'0000));
143 static_assert(ice::hash32(0xffff'ffff) == ice::hash(0xffff'ffff));
144
145 static_assert(ice::hash32(0x0000'0000) == ice::hash(0x0000'0000'0000'0000));
146 static_assert(ice::hash32(0xffff'ffff) != ice::hash(0xffff'ffff'ffff'ffff));
147
148 static_assert(ice::hash32(0xffff'ffff) == ice::u32_max);
149 static_assert(ice::hash(0xffff'ffff'ffff'ffff) == ice::u64_max);
150
151 // Check string values behave is hasing scenarios as expected.
152 static_assert(ice::hash32(u8"test_string_1") == ice::hash32(u8"test_string_1"));
153 static_assert(ice::hash32(u8"test_string_1") != ice::hash32(u8"test_string_2"));
154
155 static_assert(ice::hash32(u8"test_string_1") != ice::hash(u8"test_string_1"));
156 static_assert(ice::hash32(u8"test_string_1") != ice::hash(u8"test_string_2"));
157
158 static_assert(ice::hash(u8"test_string_1") == ice::hash(u8"test_string_1"));
159 static_assert(ice::hash(u8"test_string_1") != ice::hash(u8"test_string_2"));
160
161 static_assert(ice::hash32(u8"") == ice::hash32(u8""));
162 static_assert(ice::hash32(std::u8string_view{}) == ice::hash32(std::u8string_view{}));
163
164 static_assert(ice::hash32(u8"") != ice::hash(u8""));
165 static_assert(ice::hash32(std::u8string_view{}) != ice::hash(std::u8string_view{}));
166
167 static_assert(ice::hash(u8"") == ice::hash(u8""));
168 static_assert(ice::hash(std::u8string_view{}) == ice::hash(std::u8string_view{}));
169
170 } // namespace _validation
171
172} // namespace ice
Definition utility.hxx:111
Definition hash.hxx:139
static constexpr uint32_t Constant_Hash64_DefaultSeed
The seed used to generate hash values using the ice::hash function.
Definition constants.hxx:14
static constexpr uint32_t Constant_Hash32_DefaultSeed
The seed used to generate hash values using the ice::hash32 function.
Definition constants.hxx:11
Definition murmur2.hxx:42
constexpr auto cexpr_murmur2_x64_64(std::u8string_view key, ice::u64 seed) noexcept -> mm2_x64_64
Definition murmur2.hxx:107
Definition murmur3.hxx:18
constexpr auto cexpr_murmur3_x86_32(std::u8string_view key, ice::u32 seed) noexcept -> mm3_x86_h32
Definition murmur3.hxx:428
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr auto hash_from_ptr(T *ptr) noexcept -> ice::u64
Definition hash.hxx:129
constexpr ice::u64 const u64_max
Definition constants.hxx:38
std::uint64_t u64
Definition types.hxx:27
constexpr ice::u32 const u32_max
Definition constants.hxx:37
constexpr auto hash32(ice::String value) noexcept -> ice::u32
Definition string.hxx:105
std::uintptr_t uptr
Definition types.hxx:29
std::uint32_t u32
Definition types.hxx:26
constexpr auto hash(ice::HeapString<> const &value) noexcept -> ice::u64
Definition heap_string.hxx:251
std::uint8_t u8
Definition types.hxx:24
char8_t utf8
Definition types.hxx:11
Definition murmur2.hxx:45
ice::u64 h[1]
Definition murmur2.hxx:46
ice::u32 h[1]
Definition murmur3.hxx:22