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`.
This commit is contained in:
PixelyIon 2021-12-27 00:13:42 +05:30
parent 97cfcba0da
commit ff27dce24c

View File

@ -7,6 +7,7 @@
#include <span> #include <span>
#include <frozen/unordered_map.h> #include <frozen/unordered_map.h>
#include <frozen/string.h> #include <frozen/string.h>
#include <xxhash.h>
#include "base.h" #include "base.h"
namespace skyline::util { namespace skyline::util {
@ -190,6 +191,16 @@ namespace skyline::util {
return frozen::elsa<frozen::string>{}(frozen::string(view.data(), view.size()), 0); return frozen::elsa<frozen::string>{}(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<typename T> requires std::is_trivial_v<T>
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 * @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<typename T, typename... TArgs, size_t... Is> template<typename T, typename... TArgs, size_t... Is>
std::array<T, sizeof...(Is)> MakeFilledArray(std::index_sequence<Is...>, TArgs &&... args) std::array<T, sizeof...(Is)> MakeFilledArray(std::index_sequence<Is...>, TArgs &&... args) {
{
return {(void(Is), T(args...))...}; return {(void(Is), T(args...))...};
} }
@ -272,4 +282,4 @@ namespace skyline::util {
std::array<T, Size> MakeFilledArray(TArgs &&... args) { std::array<T, Size> MakeFilledArray(TArgs &&... args) {
return MakeFilledArray<T>(std::make_index_sequence<Size>(), std::forward<TArgs>(args)...); return MakeFilledArray<T>(std::make_index_sequence<Size>(), std::forward<TArgs>(args)...);
} }
} }