diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/zencore/include/zencore/guid.h | 8 | ||||
| -rw-r--r-- | src/zencore/include/zencore/hashutils.h | 34 | ||||
| -rw-r--r-- | src/zencore/include/zencore/uid.h | 9 |
3 files changed, 51 insertions, 0 deletions
diff --git a/src/zencore/include/zencore/guid.h b/src/zencore/include/zencore/guid.h index 751aa945f..13e1b5540 100644 --- a/src/zencore/include/zencore/guid.h +++ b/src/zencore/include/zencore/guid.h @@ -2,7 +2,9 @@ #pragma once +#include <zencore/hashutils.h> #include <zencore/zencore.h> + #include <compare> namespace zen { @@ -24,3 +26,9 @@ struct Guid }; } // namespace zen + +template<> +struct std::hash<zen::Guid> +{ + std::size_t operator()(const zen::Guid& In) const noexcept { return zen::CombineHashes(In.A, In.B, In.C, In.D); } +}; diff --git a/src/zencore/include/zencore/hashutils.h b/src/zencore/include/zencore/hashutils.h new file mode 100644 index 000000000..4e877e219 --- /dev/null +++ b/src/zencore/include/zencore/hashutils.h @@ -0,0 +1,34 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +namespace zen { + +template<typename T> +void +_CombineHashes(size_t& Seed, const T& Val) +{ + Seed ^= std::hash<T>()(Val) + 0x9e3779b97f4a7c15ull + (Seed << 12) + (Seed >> 4); +} + +/** Utility function to generate a high quality hash from multiple + contributing items + + Example usage: + + const Guid A = GetA(); + const std::string B = GetB(); + + const size_t AbHash = CombineHashes(A, B); + + */ +template<typename... Types> +size_t +CombineHashes(const Types&... Args) +{ + size_t Seed = 0; + (_CombineHashes(Seed, Args), ...); + return Seed; +} + +} // namespace zen diff --git a/src/zencore/include/zencore/uid.h b/src/zencore/include/zencore/uid.h index 4a1285416..557567473 100644 --- a/src/zencore/include/zencore/uid.h +++ b/src/zencore/include/zencore/uid.h @@ -85,3 +85,12 @@ struct Oid extern void uid_forcelink(); } // namespace zen + +namespace std { + +template<> +struct hash<zen::Oid> : public zen::Oid::Hasher +{ +}; + +} // namespace std |