Compare commits

..

6 Commits

2 changed files with 23 additions and 8 deletions

View File

@ -1,14 +1,15 @@
#pragma once
#include "CoreTypes.h"
#include "Templates/TypeHash.h"
#include "TypeTraits/Swappable.h"
NAMESPACE_REDCRAFT_BEGIN
NAMESPACE_MODULE_BEGIN(Redcraft)
NAMESPACE_MODULE_BEGIN(Utility)
/** @return The pointer or iterator to the container element storage. */
template <typename T> requires (requires(T&& Container) { Container.GetData(); })
/** @return The pointer to the container element storage. */
template <typename T> requires (requires(T&& Container) { { Container.GetData() } -> CPointer; })
FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
{
return Container.GetData();
@ -21,7 +22,7 @@ template <typename T, size_t N> FORCEINLINE constexpr const T* GetData(const T(&
template <typename T, size_t N> FORCEINLINE constexpr const T* GetData(const T(&& Container)[N]) { return Container; }
/** Overloads the GetData algorithm for T::data(). */
template <typename T> requires (requires(T&& Container) { Container.data(); })
template <typename T> requires (requires(T&& Container) { { Container.data() } -> CPointer; })
FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
{
return Container.data();
@ -29,13 +30,13 @@ FORCEINLINE constexpr decltype(auto) GetData(T&& Container)
/** Overloads the GetData algorithm for initializer_list. */
template <typename T>
FORCEINLINE constexpr decltype(auto) GetData(initializer_list<T> Container)
FORCEINLINE constexpr const T* GetData(initializer_list<T> Container)
{
return Container.begin();
}
/** @return The number of elements in the container. */
template <typename T> requires (requires(T&& Container) { Container.Num(); })
template <typename T> requires (requires(T&& Container) { { Container.Num() } -> CConvertibleTo<size_t>; })
FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
{
return Container.Num();
@ -48,7 +49,7 @@ template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(& C
template <typename T, size_t N> FORCEINLINE constexpr size_t GetNum(const T(&& Container)[N]) { return N; }
/** Overloads the GetNum algorithm for T::size(). */
template <typename T> requires (requires(T&& Container) { Container.size(); })
template <typename T> requires (requires(T&& Container) { { Container.size() } -> CConvertibleTo<size_t>; })
FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
{
return Container.size();
@ -56,7 +57,7 @@ FORCEINLINE constexpr decltype(auto) GetNum(T&& Container)
/** Overloads the GetNum algorithm for initializer_list. */
template <typename T>
FORCEINLINE constexpr decltype(auto) GetNum(initializer_list<T> Container)
FORCEINLINE constexpr size_t GetNum(initializer_list<T> Container)
{
return Container.size();
}
@ -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;