ORIGINAL
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1#ifndef HASH_H
2#define HASH_H
3
4
5#include "config.h"
6#include <cstring>
7#include <string>
8
9
26
27#include "config.h"
28#include <cstring>
29#include <string>
30
31namespace original {
32
37 template <typename TYPE>
38 class hash;
39
44 template <typename DERIVED>
45 class hashable;
46
55 template <typename DERIVED>
56 concept isHashable =
57 requires(const DERIVED& t, const DERIVED& other) {
58 { t.toHash() } -> std::same_as<u_integer>;
59 { t.equals(other) } -> std::same_as<bool>;
60 };
61
79 template <typename TYPE>
80 class hash {
81
102 template <typename T>
103 static u_integer hashFuncImpl(const T& t) noexcept;
104
105 public:
106 template <typename>
107 friend class hashable;
108
110 static constexpr u_integer FNV_OFFSET_BASIS = 0x811C9DC5;
111
113 static constexpr u_integer FNV_32_PRIME = 0x01000193;
114
122 static u_integer fnv1a(const byte* data, u_integer size) noexcept;
123
132 template <typename T>
133 static u_integer hashFunc(const T& t) noexcept;
134
141 template <isHashable T>
142 static u_integer hashFunc(const T& t) noexcept;
143
150 template<std::integral T>
151 static u_integer hashFunc(const T& t) noexcept;
152
159 template <typename T>
160 static u_integer hashFunc(T* const& t) noexcept;
161
167 static u_integer hashFunc(const std::nullptr_t& null) noexcept;
168
174 static u_integer hashFunc(const char& t) noexcept;
175
182 static u_integer hashFunc(const char* str) noexcept;
183
189 static u_integer hashFunc(const std::string& str) noexcept;
190
197 u_integer operator()(const TYPE& t) const noexcept;
198 };
199
215 template <typename DERIVED>
216 class hashable {
217 public:
223 [[nodiscard]] virtual u_integer toHash() const noexcept;
224
231 virtual bool equals(const DERIVED& other) const noexcept;
232
234 virtual ~hashable() = 0;
235 };
236}
237
238template<typename TYPE>
239template<typename T>
240original::u_integer original::hash<TYPE>::hashFuncImpl(const T &t) noexcept {
241 if constexpr (std::is_trivially_copyable_v<T>) {
242 byte buffer[sizeof(T)];
243 std::memcpy(buffer, &t, sizeof(T));
244 return fnv1a(buffer, sizeof(T));
245 } else {
246 return static_cast<u_integer>(reinterpret_cast<uintptr_t>(&t));
247 }
248}
249
250template<typename TYPE>
251original::u_integer original::hash<TYPE>::fnv1a(const byte* data, const u_integer size) noexcept {
253 for (u_integer i = 0; i < size; ++i) {
254 hash ^= data[i];
256 }
257 return hash;
258}
259
260template<typename TYPE>
261template<typename T>
263 return hashFuncImpl(t);
264}
265
266template<typename TYPE>
267template<original::isHashable T>
269 return t.toHash();
270}
271
272template<typename TYPE>
274 return 0;
275}
276
277template<typename TYPE>
278template<std::integral T>
280 return static_cast<u_integer>(t);
281}
282
283template<typename TYPE>
284template<typename T>
286 return static_cast<u_integer>(reinterpret_cast<uintptr_t>(t));
287}
288
289template<typename TYPE>
291 return static_cast<u_integer>(t);
292}
293
294template<typename TYPE>
296 if (str == nullptr) return 0;
297 return fnv1a(reinterpret_cast<const byte*>(str), std::strlen(str));
298}
299
300template<typename TYPE>
301original::u_integer original::hash<TYPE>::hashFunc(const std::string& str) noexcept {
302 return fnv1a(reinterpret_cast<const byte*>(str.data()), str.size());
303}
304
305template<typename TYPE>
307 return hashFunc(t);
308}
309
310template <typename DERIVED>
312 return hash<DERIVED>::hashFuncImpl(static_cast<const DERIVED&>(*this));
313}
314
315template<typename DERIVED>
316bool original::hashable<DERIVED>::equals(const DERIVED &other) const noexcept {
317 return static_cast<const DERIVED&>(*this) == other;
318}
319
320template <typename DERIVED>
322
323#endif //HASH_H
Forward declaration of hash class template.
Definition hash.h:80
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
static u_integer hashFunc(const std::nullptr_t &null) noexcept
Hash function for nullptr.
Definition hash.h:273
static u_integer hashFunc(T *const &t) noexcept
Hash function for pointers.
u_integer operator()(const TYPE &t) const noexcept
Hash function object call operator.
Definition hash.h:306
static u_integer hashFunc(const char &t) noexcept
Hash function for single character.
Definition hash.h:290
static u_integer hashFunc(const char *str) noexcept
Hash function for C-style strings.
Definition hash.h:295
static u_integer hashFunc(const std::string &str) noexcept
Hash function for std::string.
Definition hash.h:301
static constexpr u_integer FNV_OFFSET_BASIS
FNV-1a initial offset value.
Definition hash.h:110
static constexpr u_integer FNV_32_PRIME
FNV-1a prime multiplier.
Definition hash.h:113
static u_integer fnv1a(const byte *data, u_integer size) noexcept
FNV-1a hash implementation for raw byte data.
Definition hash.h:251
Forward declaration of hashable interface template.
Definition hash.h:216
virtual bool equals(const DERIVED &other) const noexcept
Definition hash.h:316
virtual ~hashable()=0
Virtual destructor.
virtual u_integer toHash() const noexcept
Computes the hash of the object.
Definition hash.h:311
Concept checking for hashable types.
Definition hash.h:56
Platform-independent integer and floating-point type definitions.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:43