feat(templates): overloads the GetTypeHash algorithm for array

This commit is contained in:
_Redstone_c_ 2023-01-03 10:12:11 +08:00
parent 1deb09480f
commit 66baa51e30
2 changed files with 16 additions and 1 deletions

View File

@ -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 <typename T, size_t N> requires (CHashable<TRemoveAllExtents<T>>)
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

View File

@ -74,7 +74,7 @@ FORCEINLINE constexpr size_t GetTypeHash(T A)
if constexpr (sizeof(T) == 2) return GetTypeHash(*reinterpret_cast<uint16*>(&A));
if constexpr (sizeof(T) == 4) return GetTypeHash(*reinterpret_cast<uint32*>(&A));
if constexpr (sizeof(T) == 8) return GetTypeHash(*reinterpret_cast<uint64*>(&A));
if constexpr (sizeof(T) == 16) return GetTypeHash(*reinterpret_cast<uint64*>(&A) + *(reinterpret_cast<uint64*>(&A) + 1));
if constexpr (sizeof(T) == 16) return GetTypeHash(*reinterpret_cast<uint64*>(&A) ^ *(reinterpret_cast<uint64*>(&A) + 1));
else check_no_entry();
return INDEX_NONE;