diff --git a/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp b/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp index e10fbac..c26d9b2 100644 --- a/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp +++ b/Redcraft.Utility/Source/Private/Testing/ContainersTesting.cpp @@ -12,6 +12,7 @@ NAMESPACE_BEGIN(Testing) void TestContainers() { TestArray(); + TestStaticArray(); } NAMESPACE_UNNAMED_BEGIN @@ -128,6 +129,55 @@ void TestArray() TestArrayTemplate, 64>(); } +void TestStaticArray() +{ + { + TStaticArray ArrayA = { 0, 0, 0, 0 }; + TStaticArray ArrayB = { 0, 0, 0, 0 }; + TStaticArray ArrayC = { 4, 4, 4, 4 }; + TStaticArray ArrayD(ArrayC); + TStaticArray ArrayE(MoveTemp(ArrayB)); + TStaticArray ArrayF = { 0, 1, 2, 3 }; + + TStaticArray ArrayG; + TStaticArray ArrayH; + TStaticArray ArrayI; + + ArrayG = ArrayD; + ArrayH = MoveTemp(ArrayE); + ArrayI = { 0, 1, 2, 3 }; + + always_check((ArrayC == TStaticArray({ 4, 4, 4, 4 }))); + always_check((ArrayD == TStaticArray({ 4, 4, 4, 4 }))); + always_check((ArrayG == TStaticArray({ 4, 4, 4, 4 }))); + always_check((ArrayF == TStaticArray({ 0, 1, 2, 3 }))); + always_check((ArrayI == TStaticArray({ 0, 1, 2, 3 }))); + } + + { + TStaticArray ArrayA = { 1, 2, 3 }; + TStaticArray ArrayC = { 1, 2, 3 }; + + always_check(( (ArrayA == ArrayC))); + always_check((!(ArrayA != ArrayC))); + always_check((!(ArrayA < ArrayC))); + always_check(( (ArrayA <= ArrayC))); + always_check((!(ArrayA > ArrayC))); + always_check(( (ArrayA >= ArrayC))); + } + + { + int32 ArrayA[4] = { 1, 2, 3, 4 }; + TStaticArray ArrayB = ToArray(ArrayA); + auto [A, B, C, D] = ArrayB; + + always_check(A == 1); + always_check(B == 2); + always_check(C == 3); + always_check(D == 4); + } +} + NAMESPACE_END(Testing) NAMESPACE_MODULE_END(Utility) diff --git a/Redcraft.Utility/Source/Public/Containers/Array.h b/Redcraft.Utility/Source/Public/Containers/Array.h index 58a9aae..e38b86a 100644 --- a/Redcraft.Utility/Source/Public/Containers/Array.h +++ b/Redcraft.Utility/Source/Public/Containers/Array.h @@ -97,11 +97,7 @@ public: using ElementType = T; -# if DO_CHECK - FORCEINLINE TArrayIterator() : Owner(nullptr) { } -# else FORCEINLINE TArrayIterator() = default; -# endif # if DO_CHECK FORCEINLINE TArrayIterator(const TArrayIterator>& InValue) requires (CConst) @@ -149,10 +145,10 @@ public: private: # if DO_CHECK - const ArrayType* Owner; + const ArrayType* Owner = nullptr; # endif - ElementType* Pointer; + ElementType* Pointer = nullptr; # if DO_CHECK FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer) @@ -1112,8 +1108,8 @@ public: } /** @return The pointer to the underlying element storage. */ - NODISCARD FORCEINLINE TObserverPtr< ElementType[]> GetData() { return Storage.GetPointer(); } - NODISCARD FORCEINLINE TObserverPtr GetData() const { return Storage.GetPointer(); } + NODISCARD FORCEINLINE TObserverPtr< ElementType[]> GetData() { return TObserverPtr< ElementType[]>(Storage.GetPointer()); } + NODISCARD FORCEINLINE TObserverPtr GetData() const { return TObserverPtr(Storage.GetPointer()); } /** @return The iterator to the first or end element. */ NODISCARD FORCEINLINE Iterator Begin() { return Iterator(this, Storage.GetPointer()); } @@ -1175,9 +1171,9 @@ public: { size_t Result = 0; - for (Iterator Iter = Begin(); Iter != End(); ++Iter) + for (ConstIterator Iter = A.Begin(); Iter != A.End(); ++Iter) { - HashCombine(Result, GetTypeHash(*Iter)); + Result = HashCombine(Result, GetTypeHash(*Iter)); } return Result; diff --git a/Redcraft.Utility/Source/Public/Containers/Containers.h b/Redcraft.Utility/Source/Public/Containers/Containers.h index 9b7b3ff..77a0553 100644 --- a/Redcraft.Utility/Source/Public/Containers/Containers.h +++ b/Redcraft.Utility/Source/Public/Containers/Containers.h @@ -3,3 +3,4 @@ #include "CoreTypes.h" #include "Containers/Iterator.h" #include "Containers/Array.h" +#include "Containers/StaticArray.h" diff --git a/Redcraft.Utility/Source/Public/Containers/StaticArray.h b/Redcraft.Utility/Source/Public/Containers/StaticArray.h new file mode 100644 index 0000000..5b350dc --- /dev/null +++ b/Redcraft.Utility/Source/Public/Containers/StaticArray.h @@ -0,0 +1,267 @@ +#pragma once + +#include "CoreTypes.h" +#include "Templates/Meta.h" +#include "Templates/Utility.h" +#include "Templates/TypeHash.h" +#include "Templates/Container.h" +#include "Containers/Iterator.h" +#include "TypeTraits/TypeTraits.h" +#include "Miscellaneous/Compare.h" +#include "Memory/ObserverPointer.h" +#include "Miscellaneous/AssertionMacros.h" + +NAMESPACE_REDCRAFT_BEGIN +NAMESPACE_MODULE_BEGIN(Redcraft) +NAMESPACE_MODULE_BEGIN(Utility) + +NAMESPACE_PRIVATE_BEGIN + +template +class TStaticArrayIterator +{ +public: + + using ElementType = T; + + FORCEINLINE constexpr TStaticArrayIterator() = default; + +# if DO_CHECK + FORCEINLINE constexpr TStaticArrayIterator(const TStaticArrayIterator>& InValue) requires (CConst) + : Owner(InValue.Owner), Pointer(InValue.Pointer) + { } +# else + FORCEINLINE constexpr TStaticArrayIterator(const TStaticArrayIterator>& InValue) requires (CConst) + : Pointer(InValue.Pointer) + { } +# endif + + FORCEINLINE constexpr TStaticArrayIterator(const TStaticArrayIterator&) = default; + FORCEINLINE constexpr TStaticArrayIterator(TStaticArrayIterator&&) = default; + FORCEINLINE constexpr TStaticArrayIterator& operator=(const TStaticArrayIterator&) = default; + FORCEINLINE constexpr TStaticArrayIterator& operator=(TStaticArrayIterator&&) = default; + + NODISCARD friend FORCEINLINE constexpr bool operator==(const TStaticArrayIterator& LHS, const TStaticArrayIterator& RHS) { return LHS.Pointer == RHS.Pointer; } + + NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TStaticArrayIterator & LHS, const TStaticArrayIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; } + + NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; } + NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; } + + NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { TStaticArrayIterator Temp = *this + Index; return *Temp; } + + FORCEINLINE constexpr TStaticArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; } + FORCEINLINE constexpr TStaticArrayIterator& operator--() { --Pointer; CheckThis(); return *this; } + + FORCEINLINE constexpr TStaticArrayIterator operator++(int) { TStaticArrayIterator Temp = *this; ++Pointer; CheckThis(); return Temp; } + FORCEINLINE constexpr TStaticArrayIterator operator--(int) { TStaticArrayIterator Temp = *this; --Pointer; CheckThis(); return Temp; } + + FORCEINLINE constexpr TStaticArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; } + FORCEINLINE constexpr TStaticArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; } + + NODISCARD friend FORCEINLINE constexpr TStaticArrayIterator operator+(TStaticArrayIterator Iter, ptrdiff Offset) { TStaticArrayIterator Temp = Iter; Temp += Offset; return Temp; } + NODISCARD friend FORCEINLINE constexpr TStaticArrayIterator operator+(ptrdiff Offset, TStaticArrayIterator Iter) { TStaticArrayIterator Temp = Iter; Temp += Offset; return Temp; } + + NODISCARD FORCEINLINE constexpr TStaticArrayIterator operator-(ptrdiff Offset) const { TStaticArrayIterator Temp = *this; Temp -= Offset; return Temp; } + + NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TStaticArrayIterator& LHS, const TStaticArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; } + + NODISCARD FORCEINLINE constexpr explicit operator ElementType*() requires (!CConst) { CheckThis(); return Pointer; } + NODISCARD FORCEINLINE constexpr explicit operator const ElementType*() const { CheckThis(); return Pointer; } + +private: + +# if DO_CHECK + const ArrayType* Owner = nullptr; +# endif + + ElementType* Pointer = nullptr; + +# if DO_CHECK + FORCEINLINE constexpr TStaticArrayIterator(const ArrayType* InContainer, ElementType* InPointer) + : Owner(InContainer), Pointer(InPointer) + { } +# else + FORCEINLINE constexpr TStaticArrayIterator(const ArrayType* InContainer, ElementType* InPointer) + : Pointer(InPointer) + { } +# endif + + FORCEINLINE constexpr void CheckThis(bool bExceptEnd = false) const + { + checkf(Owner && Owner->IsValidIterator(*this), TEXT("Read access violation. Please check IsValidIterator().")); + checkf(!(bExceptEnd && Owner->End() == *this), TEXT("Read access violation. Please check IsValidIterator().")); + } + + friend ArrayType; + + template + friend class TStaticArrayIterator; + +}; + +NAMESPACE_PRIVATE_END + +/** TStaticArray is a container that encapsulates fixed size arrays. */ +template +struct TStaticArray final +{ + + using ElementType = T; + + using Iterator = NAMESPACE_PRIVATE::TStaticArrayIterator; + using ConstIterator = NAMESPACE_PRIVATE::TStaticArrayIterator; + + using ReverseIterator = TReverseIterator< Iterator>; + using ConstReverseIterator = TReverseIterator; + + static_assert(CContiguousIterator< Iterator>); + static_assert(CContiguousIterator); + + /** Compares the contents of two arrays. */ + NODISCARD friend constexpr bool operator==(const TStaticArray& LHS, const TStaticArray& RHS) requires (CWeaklyEqualityComparable) + { + if (LHS.Num() != RHS.Num()) return false; + + ConstIterator LHSIter = LHS.Begin(); + ConstIterator RHSIter = RHS.Begin(); + + while (LHSIter != LHS.End()) + { + if (*LHSIter != *RHSIter) return false; + + ++LHSIter; + ++RHSIter; + } + + check(RHSIter == RHS.End()); + + return true; + } + + /** Compares the contents of two arrays. */ + NODISCARD friend constexpr auto operator<=>(const TStaticArray& LHS, const TStaticArray& RHS) requires (CSynthThreeWayComparable) + { + using OrderingType = TSynthThreeWayResult; + + if (LHS.Num() < RHS.Num()) return OrderingType::less; + if (LHS.Num() > RHS.Num()) return OrderingType::greater; + + ConstIterator LHSIter = LHS.Begin(); + ConstIterator RHSIter = RHS.Begin(); + + while (LHSIter != LHS.End()) + { + TSynthThreeWayResult Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter); + + if (Ordering != OrderingType::equivalent) return Ordering; + + ++LHSIter; + ++RHSIter; + } + + check(RHSIter == RHS.End()); + + return OrderingType::equivalent; + } + + /** @return The pointer to the underlying element storage. */ + NODISCARD FORCEINLINE constexpr TObserverPtr< ElementType[]> GetData() { return TObserverPtr< ElementType[]>(_); } + NODISCARD FORCEINLINE constexpr TObserverPtr GetData() const { return TObserverPtr(_); } + + /** @return The iterator to the first or end element. */ + NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, _); } + NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return ConstIterator(this, _); } + NODISCARD FORCEINLINE constexpr Iterator End() { return Iterator(this, _ + Num()); } + NODISCARD FORCEINLINE constexpr ConstIterator End() const { return ConstIterator(this, _ + Num()); } + + /** @return The reverse iterator to the first or end element. */ + NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); } + NODISCARD FORCEINLINE constexpr ConstReverseIterator RBegin() const { return ConstReverseIterator(End()); } + NODISCARD FORCEINLINE constexpr ReverseIterator REnd() { return ReverseIterator(Begin()); } + NODISCARD FORCEINLINE constexpr ConstReverseIterator REnd() const { return ConstReverseIterator(Begin()); } + + /** @return The number of elements in the container. */ + NODISCARD FORCEINLINE constexpr size_t Num() const { return N; } + NODISCARD FORCEINLINE constexpr size_t Max() const { return N; } + + /** @return true if the container is empty, false otherwise. */ + NODISCARD FORCEINLINE constexpr bool IsEmpty() const { return Num() == 0; } + + /** @return true if the iterator is valid, false otherwise. */ + NODISCARD FORCEINLINE constexpr bool IsValidIterator(ConstIterator Iter) const { return Begin() <= Iter && Iter <= End(); } + + /** @return The reference to the requested element. */ + NODISCARD FORCEINLINE constexpr ElementType& operator[](size_t Index) { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return _[Index]; } + NODISCARD FORCEINLINE constexpr const ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return _[Index]; } + + /** @return The reference to the first or last element. */ + NODISCARD FORCEINLINE constexpr ElementType& Front() { return *Begin(); } + NODISCARD FORCEINLINE constexpr const ElementType& Front() const { return *Begin(); } + NODISCARD FORCEINLINE constexpr ElementType& Back() { return *(End() - 1); } + NODISCARD FORCEINLINE constexpr const ElementType& Back() const { return *(End() - 1); } + + /** Overloads the GetTypeHash algorithm for TStaticArray. */ + NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TStaticArray& A) requires (CHashable) + { + size_t Result = 0; + + for (ConstIterator Iter = A.Begin(); Iter != A.End(); ++Iter) + { + Result = HashCombine(Result, GetTypeHash(*Iter)); + } + + return Result; + } + + /** Overloads the Swap algorithm for TStaticArray. */ + friend FORCEINLINE constexpr void Swap(TStaticArray& A, TStaticArray& B) requires (CSwappable) { Swap(A._, B._); } + + ENABLE_RANGE_BASED_FOR_LOOP_SUPPORT + + T _[N]; + +}; + +template requires (true && ... && CSameAs) +TStaticArray(T, U...) -> TStaticArray; + +/** Creates a TStaticArray object from a built-in array. */ +template +constexpr TStaticArray, N> ToArray(T(& Array)[N]) +{ + return [&Array](TIndexSequence) -> TStaticArray, N> { return { Array[Indices]... }; } (TMakeIndexSequence()); +} + +/** Creates a TStaticArray object from a built-in array. */ +template +constexpr TStaticArray, N> ToArray(T(&& Array)[N]) +{ + return [&Array](TIndexSequence) -> TStaticArray, N> { return { MoveTemp(Array[Indices])... }; } (TMakeIndexSequence()); +} + +NAMESPACE_MODULE_END(Utility) +NAMESPACE_MODULE_END(Redcraft) +NAMESPACE_REDCRAFT_END + +NAMESPACE_STD_BEGIN + +// Support structure binding, should not be directly used. +template struct tuple_size> : integral_constant { }; +template struct tuple_element> { using type = T; }; + +NAMESPACE_STD_END + +NAMESPACE_REDCRAFT_BEGIN +NAMESPACE_MODULE_BEGIN(Redcraft) +NAMESPACE_MODULE_BEGIN(Utility) + +// Support structure binding, should not be directly used. +template FORCEINLINE constexpr decltype(auto) get( TStaticArray& InValue) { return static_cast< TStaticArray& >(InValue)[Index]; } +template FORCEINLINE constexpr decltype(auto) get(const TStaticArray& InValue) { return static_cast& >(InValue)[Index]; } +template FORCEINLINE constexpr decltype(auto) get( TStaticArray&& InValue) { return static_cast< TStaticArray&&>(InValue)[Index]; } +template FORCEINLINE constexpr decltype(auto) get(const TStaticArray&& InValue) { return static_cast&&>(InValue)[Index]; } + +NAMESPACE_MODULE_END(Utility) +NAMESPACE_MODULE_END(Redcraft) +NAMESPACE_REDCRAFT_END diff --git a/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h b/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h index 4378dde..e2b8088 100644 --- a/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h +++ b/Redcraft.Utility/Source/Public/Memory/ObserverPointer.h @@ -79,6 +79,9 @@ private: T* Pointer; + template requires (CObject && !CBoundedArray) + friend class TObserverPtr; + }; template @@ -139,6 +142,9 @@ private: T* Pointer; + template requires (CObject && !CBoundedArray) + friend class TObserverPtr; + }; template requires (CObject && !CBoundedArray) diff --git a/Redcraft.Utility/Source/Public/Testing/ContainersTesting.h b/Redcraft.Utility/Source/Public/Testing/ContainersTesting.h index d4cef50..3fcfaf8 100644 --- a/Redcraft.Utility/Source/Public/Testing/ContainersTesting.h +++ b/Redcraft.Utility/Source/Public/Testing/ContainersTesting.h @@ -10,6 +10,7 @@ NAMESPACE_BEGIN(Testing) REDCRAFTUTILITY_API void TestContainers(); REDCRAFTUTILITY_API void TestArray(); +REDCRAFTUTILITY_API void TestStaticArray(); NAMESPACE_END(Testing)