ORIGINAL
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1#ifndef HASH_H
2#define HASH_H
3
4#include "config.h"
5#include <cstring>
6#include <string>
7#include "types.h"
8
29namespace original {
30
35 template <typename DERIVED>
36 class hashable;
37
61 template <typename TYPE>
62 class hash {
63
75 template <typename T>
76 static u_integer hashFuncImpl(const T& t) noexcept;
77
78 public:
79 template <typename>
80 friend class hashable;
81
83 static constexpr u_integer FNV_OFFSET_BASIS = 0x811C9DC5;
84
86 static constexpr u_integer FNV_32_PRIME = 0x01000193;
87
94 template <typename T>
95 static void hashCombine(u_integer& seed, const T& value) noexcept;
96
104 static u_integer fnv1a(const byte* data, u_integer size) noexcept;
105
114 template <typename T, typename... Rest>
115 static void hashCombine(u_integer& seed, const T& value, const Rest&... rest) noexcept;
116
125 template <typename T>
126 static u_integer hashFunc(const T& t) noexcept;
127
139 template <HashTraits T>
140 static u_integer hashFunc(const T& t) noexcept;
141
148 template<std::integral T>
149 static u_integer hashFunc(const T& t) noexcept;
150
157 template <typename T>
158 static u_integer hashFunc(T* const& t) noexcept;
159
165 static u_integer hashFunc(const std::nullptr_t& null) noexcept;
166
172 static u_integer hashFunc(const char& t) noexcept;
173
180 static u_integer hashFunc(const char* str) noexcept;
181
187 static u_integer hashFunc(const std::string& str) noexcept;
188
195 u_integer operator()(const TYPE& t) const noexcept;
196 };
197
219 template <typename DERIVED>
220 class hashable {
221 public:
229
238
244 };
245}
246
247
264struct std::hash<T> { // NOLINT
270 std::size_t operator()(const T& t) const noexcept;
271};
272
273template<typename TYPE>
274template<typename T>
275void original::hash<TYPE>::hashCombine(u_integer &seed, const T& value) noexcept {
276 seed ^= hash<T>::hashFunc(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
277}
278
279template<typename TYPE>
280template<typename T>
282 if constexpr (std::is_trivially_copyable_v<T>) {
283 byte buffer[sizeof(T)];
284 std::memcpy(buffer, &t, sizeof(T));
285 return fnv1a(buffer, sizeof(T));
286 } else {
287 return static_cast<u_integer>(reinterpret_cast<uintptr_t>(&t));
288 }
289}
290
291template<typename TYPE>
292original::u_integer original::hash<TYPE>::fnv1a(const byte* data, const u_integer size) noexcept {
293 u_integer hash = FNV_OFFSET_BASIS;
294 for (u_integer i = 0; i < size; ++i) {
295 hash ^= data[i];
296 hash *= FNV_32_PRIME;
297 }
298 return hash;
299}
300
301template<typename TYPE>
302template<typename T, typename... Rest>
303void original::hash<TYPE>::hashCombine(u_integer &seed, const T& value, const Rest&... rest) noexcept {
304 hashCombine(seed, value);
305 (hashCombine(seed, rest), ...);
306}
307
308template<typename TYPE>
309template<typename T>
311 return hashFuncImpl(t);
312}
313
314template <typename TYPE>
315template <original::HashTraits T>
317 return t.toHash();
318}
319
320template<typename TYPE>
322 return 0;
323}
324
325template<typename TYPE>
326template<std::integral T>
328 return static_cast<u_integer>(t);
329}
330
331template<typename TYPE>
332template<typename T>
334 return static_cast<u_integer>(reinterpret_cast<uintptr_t>(t));
335}
336
337template<typename TYPE>
339 return static_cast<u_integer>(t);
340}
341
342template<typename TYPE>
344 if (str == nullptr) return 0;
345 return fnv1a(reinterpret_cast<const byte*>(str), std::strlen(str));
346}
347
348template<typename TYPE>
350 return fnv1a(reinterpret_cast<const byte*>(str.data()), str.size());
351}
352
353template<typename TYPE>
355 return hashFunc(t);
356}
357
358template <typename DERIVED>
362
363template<typename DERIVED>
365 return static_cast<const DERIVED&>(*this) == other;
366}
367
368template <typename DERIVED>
370
371template <original::HashTraits T>
372std::size_t std::hash<T>::operator()(const T& t) const noexcept {
373 return static_cast<std::size_t>(t.toHash());
374}
375
376#endif //HASH_H
u_integer toHash() const noexcept override
Compute hash value for the pointer.
Definition autoPtr.h:735
Generic hash function object supporting multiple types.
Definition hash.h:62
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
static void hashCombine(u_integer &seed, const T &value, const Rest &... rest) noexcept
Combines multiple hash values into one.
Definition hash.h:303
static u_integer hashFunc(const std::nullptr_t &null) noexcept
Hash function for nullptr.
Definition hash.h:321
static u_integer hashFunc(T *const &t) noexcept
Hash function for pointers.
static void hashCombine(u_integer &seed, const T &value) noexcept
Combines a hash value with another value's hash.
Definition hash.h:275
u_integer operator()(const TYPE &t) const noexcept
Hash function object call operator.
Definition hash.h:354
static u_integer hashFunc(const char &t) noexcept
Hash function for single character.
Definition hash.h:338
static u_integer hashFunc(const char *str) noexcept
Hash function for C-style strings.
Definition hash.h:343
static u_integer hashFunc(const std::string &str) noexcept
Hash function for std::string.
Definition hash.h:349
static constexpr u_integer FNV_OFFSET_BASIS
FNV-1a initial offset value (0x811C9DC5)
Definition hash.h:83
static constexpr u_integer FNV_32_PRIME
FNV-1a prime multiplier (0x01000193)
Definition hash.h:86
static u_integer fnv1a(const byte *data, u_integer size) noexcept
FNV-1a hash implementation for raw byte data.
Definition hash.h:292
Forward declaration of hashable interface template.
Definition hash.h:220
virtual bool equals(const DERIVED &other) const noexcept
Compares two objects for equality.
Definition hash.h:364
virtual ~hashable()=0
Virtual destructor.
virtual u_integer toHash() const noexcept
Computes the hash of the object.
Definition hash.h:359
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Requires type to implement the hashable interface with toHash method.
Definition types.h:311
Platform-independent type definitions and compiler/platform detection.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
Main namespace for the project Original.
Definition algorithms.h:21
Standard namespace extensions for original::alternative.
Definition allocator.h:351
Core type system foundations and concept definitions.