diff options
| author | Stefan Boberg <[email protected]> | 2023-09-13 12:14:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-13 12:14:02 +0200 |
| commit | d83bc50cc7df1e7b4a9810c837fb258d92e78465 (patch) | |
| tree | 0a7a93cf28a275a71011339c89da4547ef716c23 /src | |
| parent | ZenCacheStore is now reference counted (#398) (diff) | |
| download | zen-d83bc50cc7df1e7b4a9810c837fb258d92e78465.tar.xz zen-d83bc50cc7df1e7b4a9810c837fb258d92e78465.zip | |
Hash utility functions (#385)
* added std::hash implementation for Guid and Oid types
* Added CombineHashes implementation
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 |