CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Hash.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
13
14#include <functional>
15#include <utility>
16#include <string>
17
18namespace CeresEngine {
19
25 template<typename T> constexpr size_t hash(const T& v) { return std::hash<T>{}(v); }
26
32 template<typename T> constexpr void combine(std::size_t& seed, const T& v) {
33 seed ^= hash(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); // NOLINT
34 }
35
41 template<typename... Ts> constexpr size_t hash(const Ts&... values) {
42 size_t hash = 0;
43 ((void)combine<decltype(values)>(hash, values), ...);
44 return hash;
45 }
46
48 template<typename T> struct Hasher {
50 using ReflectedType = std::decay_t<T>;
51
54
56 size_t hash;
57
59 explicit constexpr Hasher(T& object, const size_t hash = 0) noexcept : object(object), hash(hash) {}
60
63 template<auto P> constexpr void data(StringView) noexcept { combine(hash, object.*P); }
64
66 template<auto P> constexpr void func(StringView) noexcept {}
67 };
68
69 // ---------------------------------------------------------------------------------------------
70
71 // FNV1a c++11 constexpr compile time hash functions, 32 and 64 bit
72 // str should be implicitly convertible BasicStringView<T>, value should
73 // be left out e.g staticHash32("example")
74 // code license: public domain or equivalent
75 // post: https://notes.underscorediscovery.com/constexpr-fnv1a/
76
77 template<typename T> inline constexpr UInt32 staticHash32(BasicStringView<T> string, const UInt32 value = 0x811c9dc5) noexcept {
78 constexpr UInt32 prime = 0x1000193;
79 return (string.size() == 0) ? value : staticHash32(BasicStringView<T>(string.data() + 1, string.size() - 1), (value ^ UInt32(string[0])) * prime);
80 }
81
82 template<typename T> inline constexpr UInt64 staticHash64(BasicStringView<T> string, const UInt64 value = 0xcbf29ce484222325) noexcept {
83 constexpr UInt64 prime = 0x100000001b3;
84 return (string.size() == 0) ? value : staticHash64(BasicStringView<T>(string.data() + 1, string.size() - 1), (value ^ UInt64(string[0])) * prime);
85 }
86
87} // namespace CeresEngine
88
89#define CE_REFLECT_HASH(T) \
90 template<> struct std::hash<T> { \
91 inline constexpr size_t operator()(const T& object) const noexcept { \
92 ::CeresEngine::Hasher hasher{object}; \
93 T::reflect(hasher); \
94 return hasher.hash; \
95 } \
96 }
97
98template<typename T, size_t N> struct std::hash<T[N]> {
99 inline constexpr size_t operator()(const T values[N]) const noexcept {
100 size_t seed = 0;
101 for(size_t i = 0; i < N; i++) {
102 CeresEngine::combine(seed, values[i]);
103 }
104 return seed;
105 }
106};
107
108template<typename T, typename CharTraits, typename RawAllocator>
109struct std::hash<CeresEngine::BasicString<T, CharTraits, RawAllocator>>
110 : public std::hash<std::basic_string<T, CharTraits, CeresEngine::StdAllocator<T, RawAllocator>>> {};
111
112template<typename T, typename CharTraits>
113struct std::hash<CeresEngine::BasicStringView<T, CharTraits>> : public std::hash<std::basic_string_view<T, CharTraits>> {};
Definition Application.hpp:19
std::uint64_t UInt64
Definition DataTypes.hpp:26
constexpr UInt32 staticHash32(BasicStringView< T > string, const UInt32 value=0x811c9dc5) noexcept
Definition Hash.hpp:77
constexpr void combine(std::size_t &seed, const T &v)
Generates a new hash for the provided type using the default standard hasher and combines it with a p...
Definition Hash.hpp:32
constexpr UInt64 staticHash64(BasicStringView< T > string, const UInt64 value=0xcbf29ce484222325) noexcept
Definition Hash.hpp:82
auto values(Container &container)
Returns an iterable object that iterates over the values of a map-like container.
Definition Iterator.hpp:400
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
A reflection pre-processor that creates a hash for an object.
Definition Hash.hpp:48
T & object
The object currently being hashed.
Definition Hash.hpp:53
constexpr void func(StringView) noexcept
No-op.
Definition Hash.hpp:66
constexpr Hasher(T &object, const size_t hash=0) noexcept
Creates a new hasher for the given object.
Definition Hash.hpp:59
std::decay_t< T > ReflectedType
The type currently being hashed.
Definition Hash.hpp:50
constexpr void data(StringView) noexcept
Combines the hash with the.
Definition Hash.hpp:63
size_t hash
The computed hash.
Definition Hash.hpp:56