Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
HashCombine.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8
15inline uint64 HashBytes(const void *inData, uint inSize, uint64 inSeed = 0xcbf29ce484222325UL)
16{
17 uint64 hash = inSeed;
18 for (const uint8 *data = reinterpret_cast<const uint8 *>(inData); data < reinterpret_cast<const uint8 *>(inData) + inSize; ++data)
19 {
20 hash = hash ^ uint64(*data);
21 hash = hash * 0x100000001b3UL;
22 }
23 return hash;
24}
25
30inline uint64 Hash64(uint64 inValue)
31{
32 uint64 hash = inValue;
33 hash = (~hash) + (hash << 21); // hash = (hash << 21) - hash - 1;
34 hash = hash ^ (hash >> 24);
35 hash = (hash + (hash << 3)) + (hash << 8); // hash * 265
36 hash = hash ^ (hash >> 14);
37 hash = (hash + (hash << 2)) + (hash << 4); // hash * 21
38 hash = hash ^ (hash >> 28);
39 hash = hash + (hash << 31);
40 return hash;
41}
42
45template <typename T>
46inline void HashCombineHelper(size_t &ioSeed, const T &inValue)
47{
48 std::hash<T> hasher;
49 ioSeed ^= hasher(inValue) + 0x9e3779b9 + (ioSeed << 6) + (ioSeed >> 2);
50}
51
64template <typename... Values>
65inline void HashCombine(std::size_t &ioSeed, Values... inValues)
66{
67 // Hash all values together using a fold expression
68 (HashCombineHelper(ioSeed, inValues), ...);
69}
70
72
73JPH_SUPPRESS_WARNING_PUSH
74JPH_CLANG_SUPPRESS_WARNING("-Wc++98-compat-pedantic")
75
76#define JPH_MAKE_HASH_STRUCT(type, name, ...) \
77 struct [[nodiscard]] name \
78 { \
79 std::size_t operator()(const type &t) const \
80 { \
81 std::size_t ret = 0; \
82 ::JPH::HashCombine(ret, __VA_ARGS__); \
83 return ret; \
84 } \
85 };
86
87#define JPH_MAKE_HASHABLE(type, ...) \
88 JPH_SUPPRESS_WARNING_PUSH \
89 JPH_SUPPRESS_WARNINGS \
90 namespace std \
91 { \
92 template<> \
93 JPH_MAKE_HASH_STRUCT(type, hash<type>, __VA_ARGS__) \
94 } \
95 JPH_SUPPRESS_WARNING_POP
96
97JPH_SUPPRESS_WARNING_POP
std::uint8_t uint8
Definition: Core.h:440
std::uint64_t uint64
Definition: Core.h:443
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_CLANG_SUPPRESS_WARNING(w)
Definition: Core.h:254
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
JPH_NAMESPACE_BEGIN uint64 HashBytes(const void *inData, uint inSize, uint64 inSeed=0xcbf29ce484222325UL)
Definition: HashCombine.h:15
void HashCombine(std::size_t &ioSeed, Values... inValues)
Definition: HashCombine.h:65
void HashCombineHelper(size_t &ioSeed, const T &inValue)
Helper function that hashes a single value into ioSeed Taken from: https://stackoverflow....
Definition: HashCombine.h:46
uint64 Hash64(uint64 inValue)
Definition: HashCombine.h:30