From ff27dce24c2fb123194f2673026bb25e9edb7a75 Mon Sep 17 00:00:00 2001 From: PixelyIon Date: Mon, 27 Dec 2021 00:13:42 +0530 Subject: [PATCH] Implement `ObjectHash` for hashing trivial objects in maps `std::hash` doesn't have a generic template where it can be utilized for arbitrary trivial objects and implementing this might result in conflicts with other types. To fix this a generic templated hash is now provided as a utility structure, that can be utilized directly in hash-based containers such as `unordered_map`. --- app/src/main/cpp/skyline/common/utils.h | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/src/main/cpp/skyline/common/utils.h b/app/src/main/cpp/skyline/common/utils.h index 1e9c62e7..d6bbe9d1 100644 --- a/app/src/main/cpp/skyline/common/utils.h +++ b/app/src/main/cpp/skyline/common/utils.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "base.h" namespace skyline::util { @@ -190,6 +191,16 @@ namespace skyline::util { return frozen::elsa{}(frozen::string(view.data(), view.size()), 0); } + /** + * @brief A fast hash for any trivial object that is designed to be utilized with hash-based containers + */ + template requires std::is_trivial_v + struct ObjectHash { + size_t operator()(const T &object) const noexcept { + return XXH64(&object, sizeof(object), 0); + } + }; + /** * @brief Selects the largest possible integer type for representing an object alongside providing the size of the object in terms of the underlying type */ @@ -263,8 +274,7 @@ namespace skyline::util { }; template - std::array MakeFilledArray(std::index_sequence, TArgs &&... args) - { + std::array MakeFilledArray(std::index_sequence, TArgs &&... args) { return {(void(Is), T(args...))...}; } @@ -272,4 +282,4 @@ namespace skyline::util { std::array MakeFilledArray(TArgs &&... args) { return MakeFilledArray(std::make_index_sequence(), std::forward(args)...); } -} \ No newline at end of file +}