From 66baa51e30ee137c9d03d2f43de3cdb332fc643b Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Tue, 3 Jan 2023 10:12:11 +0800 Subject: [PATCH] feat(templates): overloads the GetTypeHash algorithm for array --- .../Source/Public/Templates/Container.h | 15 +++++++++++++++ .../Source/Public/Templates/TypeHash.h | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Redcraft.Utility/Source/Public/Templates/Container.h b/Redcraft.Utility/Source/Public/Templates/Container.h index 0079552..85b2393 100644 --- a/Redcraft.Utility/Source/Public/Templates/Container.h +++ b/Redcraft.Utility/Source/Public/Templates/Container.h @@ -1,6 +1,7 @@ #pragma once #include "CoreTypes.h" +#include "Templates/TypeHash.h" #include "TypeTraits/Swappable.h" NAMESPACE_REDCRAFT_BEGIN @@ -71,6 +72,20 @@ FORCEINLINE constexpr void Swap(T(&A)[N], T(&B)[N]) } } +/** Overloads the GetTypeHash algorithm for arrays. */ +template requires (CHashable>) +FORCEINLINE constexpr size_t GetTypeHash(T(&A)[N]) +{ + size_t Result = 3516520171; + + for (size_t Index = 0; Index < N; ++Index) + { + Result = HashCombine(Result, GetTypeHash(A[Index])); + } + + return Result; +} + NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Templates/TypeHash.h b/Redcraft.Utility/Source/Public/Templates/TypeHash.h index 230cac7..549b0d3 100644 --- a/Redcraft.Utility/Source/Public/Templates/TypeHash.h +++ b/Redcraft.Utility/Source/Public/Templates/TypeHash.h @@ -74,7 +74,7 @@ FORCEINLINE constexpr size_t GetTypeHash(T A) if constexpr (sizeof(T) == 2) return GetTypeHash(*reinterpret_cast(&A)); if constexpr (sizeof(T) == 4) return GetTypeHash(*reinterpret_cast(&A)); if constexpr (sizeof(T) == 8) return GetTypeHash(*reinterpret_cast(&A)); - if constexpr (sizeof(T) == 16) return GetTypeHash(*reinterpret_cast(&A) + *(reinterpret_cast(&A) + 1)); + if constexpr (sizeof(T) == 16) return GetTypeHash(*reinterpret_cast(&A) ^ *(reinterpret_cast(&A) + 1)); else check_no_entry(); return INDEX_NONE;