66#include " traits/is_integral.h"
77#include " hash/city_hash.h"
88#include " templates/bit_cast.h"
9+ #include < core/cstring.h>
910
1011namespace hud
1112{
@@ -80,15 +81,43 @@ namespace hud
8081 }
8182
8283 /* * Retrieves the 32 bits hash of a ansichar null-terminated string. */
83- [[nodiscard]] static inline u32 hash_32 (const ansichar *value, usize length) noexcept
84+ [[nodiscard]] static constexpr u32 hash_32 (const ansichar *value, usize length) noexcept
8485 {
8586 return hud::hash_algorithm::city_hash::hash_32 (value, length);
8687 }
8788
89+ /* * Retrieves the 32 bits hash of a ansichar null-terminated string. */
90+ [[nodiscard]] static constexpr u32 hash_32 (const ansichar *const value) noexcept
91+ {
92+ return hash_32 (value, hud::cstring::length (value));
93+ }
94+
8895 /* * Retrieves the 32 bits hash of a wchar null-terminated string. */
89- [[nodiscard]] static inline u32 hash_32 (const wchar *value, usize length) noexcept
96+ [[nodiscard]] static constexpr u32 hash_32 (const wchar *value, usize length) noexcept
9097 {
91- return hud::hash_algorithm::city_hash::hash_32 (reinterpret_cast <const ansichar *>(value), length * sizeof (wchar));
98+ struct A
99+ {
100+ union
101+ {
102+ const ansichar *a_;
103+ const wchar *w_;
104+ };
105+
106+ constexpr A (const wchar *w)
107+ : w_(w)
108+ {
109+ }
110+ };
111+
112+ A a (value);
113+ a.w_ ;
114+ return hud::hash_algorithm::city_hash::hash_32 (a.a_ , length * sizeof (wchar));
115+ }
116+
117+ /* * Retrieves the 32 bits hash of a ansichar null-terminated string. */
118+ [[nodiscard]] static constexpr u32 hash_32 (const wchar *const value) noexcept
119+ {
120+ return hash_32 (value, hud::cstring::length (value));
92121 }
93122
94123 /* * Retrieves the 32 bits hash of an enumeration. */
@@ -100,8 +129,7 @@ namespace hud
100129 }
101130
102131 /* * Retrieves the 32 bits hash of a pointer of a type type_t. */
103- template <typename type_t >
104- [[nodiscard]] static u32 hash_32 (type_t *const pointer) noexcept
132+ [[nodiscard]] static inline u32 hash_32 (const void *const pointer) noexcept
105133 {
106134 const uptr ptr = reinterpret_cast <uptr>(pointer);
107135 if constexpr (sizeof (uptr) == 4 )
@@ -184,17 +212,29 @@ namespace hud
184212 }
185213
186214 /* * Retrieves the 64 bits hash of a ansichar null-terminated string. */
187- [[nodiscard]] static inline u64 hash_64 (const ansichar *value, usize length) noexcept
215+ [[nodiscard]] static constexpr u64 hash_64 (const ansichar *value, usize length) noexcept
188216 {
189217 return hud::hash_algorithm::city_hash::hash_64 (value, length);
190218 }
191219
220+ /* * Retrieves the 32 bits hash of a ansichar null-terminated string. */
221+ [[nodiscard]] static constexpr u32 hash_64 (const ansichar *const value) noexcept
222+ {
223+ return hash_64 (value, hud::cstring::length (value));
224+ }
225+
192226 /* * Retrieves the 64 bits hash of a wchar null-terminated string. */
193227 [[nodiscard]] static inline u64 hash_64 (const wchar *value, usize length) noexcept
194228 {
195229 return hud::hash_algorithm::city_hash::hash_64 (reinterpret_cast <const ansichar *>(value), length * sizeof (wchar));
196230 }
197231
232+ /* * Retrieves the 32 bits hash of a ansichar null-terminated string. */
233+ [[nodiscard]] static inline u64 hash_64 (const wchar *const value) noexcept
234+ {
235+ return hash_64 (value, hud::cstring::length (value));
236+ }
237+
198238 /* * Retrieves the 64 bits hash of an enumeration. */
199239 template <typename type_t >
200240 requires (is_enum_v<type_t >)
@@ -204,8 +244,7 @@ namespace hud
204244 }
205245
206246 /* * Retrieves the 64 bits hash of a pointer of a type type_t. */
207- template <typename type_t >
208- [[nodiscard]] static u64 hash_64 (type_t *const pointer) noexcept
247+ [[nodiscard]] static inline u64 hash_64 (const void *const pointer) noexcept
209248 {
210249 const uptr ptr = reinterpret_cast <uptr>(pointer);
211250 if constexpr (sizeof (uptr) == 4 )
@@ -224,18 +263,30 @@ namespace hud
224263 return hud::hash_algorithm::city_hash::combine_64 (a, b);
225264 }
226265
227- struct Hasher32
266+ struct hasher_32
228267 {
229- template <typename T >
230- [[nodiscard]] constexpr u32 operator ()(const T &value ) noexcept
268+ template <typename ... type_t >
269+ [[nodiscard]] constexpr u32 operator ()(type_t &&...values ) noexcept
231270 {
232- state_ = hud::combine_32 (state_, hud::hash_32 (value ));
271+ state_ = hud::combine_32 (state_, hud::hash_32 (hud::forward< type_t >(values)... ));
233272 return state_;
234273 }
235274
236275 u32 state_ {0 }; // Default is 0, but can be a seed
237276 };
238277
278+ struct hasher_64
279+ {
280+ template <typename ... type_t >
281+ [[nodiscard]] constexpr u64 operator ()(type_t &&...values) noexcept
282+ {
283+ state_ = hud::combine_64 (state_, hud::hash_64 (hud::forward<type_t >(values)...));
284+ return state_;
285+ }
286+
287+ u64 state_ {0 }; // Default is 0, but can be a seed
288+ };
289+
239290} // namespace hud
240291
241292#endif // HD_INC_CORE_HASH_H
0 commit comments