#pragma once #include "CoreTypes.h" #include "TypeTraits/TypeTraits.h" #include "Templates/Utility.h" #include "Templates/Meta.h" #include "Templates/TypeHash.h" #include "Iterator/Utility.h" #include "Iterator/BasicIterator.h" #include "Iterator/ReverseIterator.h" #include "Miscellaneous/Compare.h" #include "Miscellaneous/AssertionMacros.h" NAMESPACE_REDCRAFT_BEGIN NAMESPACE_MODULE_BEGIN(Redcraft) NAMESPACE_MODULE_BEGIN(Utility) /** TStaticArray is a container that encapsulates fixed size arrays. */ template requires (!CConst && !CVolatile) struct TStaticArray { private: template > class TIteratorImpl; public: using FElementType = T; using FReference = T&; using FConstReference = const T&; using FIterator = TIteratorImpl; using FConstIterator = TIteratorImpl; using FReverseIterator = TReverseIterator< FIterator>; using FConstReverseIterator = TReverseIterator; static_assert(CContiguousIterator< FIterator>); 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; for (size_t Index = 0; Index < LHS.Num(); ++Index) { if (LHS[Index] != RHS[Index]) return false; } return true; } /** Compares the contents of 'LHS' and 'RHS' lexicographically. */ NODISCARD friend constexpr auto operator<=>(const TStaticArray& LHS, const TStaticArray& RHS) requires (CSynthThreeWayComparable) { const size_t NumToCompare = LHS.Num() < RHS.Num() ? LHS.Num() : RHS.Num(); for (size_t Index = 0; Index < NumToCompare; ++Index) { if (const auto Result = SynthThreeWayCompare(LHS[Index], RHS[Index]); Result != 0) return Result; } return LHS.Num() <=> RHS.Num(); } /** @return The pointer to the underlying element storage. */ NODISCARD FORCEINLINE constexpr FElementType* GetData() { return _; } NODISCARD FORCEINLINE constexpr const FElementType* GetData() const { return _; } /** @return The iterator to the first or end element. */ NODISCARD FORCEINLINE constexpr FIterator Begin() { return FIterator(this, _); } NODISCARD FORCEINLINE constexpr FConstIterator Begin() const { return FConstIterator(this, _); } NODISCARD FORCEINLINE constexpr FIterator End() { return FIterator(this, _ + Num()); } NODISCARD FORCEINLINE constexpr FConstIterator End() const { return FConstIterator(this, _ + Num()); } /** @return The reverse iterator to the first or end element. */ NODISCARD FORCEINLINE constexpr FReverseIterator RBegin() { return FReverseIterator(End()); } NODISCARD FORCEINLINE constexpr FConstReverseIterator RBegin() const { return FConstReverseIterator(End()); } NODISCARD FORCEINLINE constexpr FReverseIterator REnd() { return FReverseIterator(Begin()); } NODISCARD FORCEINLINE constexpr FConstReverseIterator REnd() const { return FConstReverseIterator(Begin()); } /** @return The number of elements in the container. */ NODISCARD FORCEINLINE constexpr size_t Num() 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(FConstIterator Iter) const { return Begin() <= Iter && Iter <= End(); } /** @return The reference to the requested element. */ NODISCARD FORCEINLINE constexpr FElementType& operator[](size_t Index) { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return _[Index]; } NODISCARD FORCEINLINE constexpr const FElementType& 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 FElementType& Front() { return *Begin(); } NODISCARD FORCEINLINE constexpr const FElementType& Front() const { return *Begin(); } NODISCARD FORCEINLINE constexpr FElementType& Back() { return *(End() - 1); } NODISCARD FORCEINLINE constexpr const FElementType& 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 (FConstIterator 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]; private: template class TIteratorImpl final { public: using FElementType = TRemoveCV; FORCEINLINE constexpr TIteratorImpl() = default; # if DO_CHECK FORCEINLINE TIteratorImpl(const TIteratorImpl& InValue) requires (bConst) : Owner(InValue.Owner), Pointer(InValue.Pointer) { } # else FORCEINLINE TIteratorImpl(const TIteratorImpl& InValue) requires (bConst) : Pointer(InValue.Pointer) { } # endif FORCEINLINE constexpr TIteratorImpl(const TIteratorImpl&) = default; FORCEINLINE constexpr TIteratorImpl(TIteratorImpl&&) = default; FORCEINLINE constexpr TIteratorImpl& operator=(const TIteratorImpl&) = default; FORCEINLINE constexpr TIteratorImpl& operator=(TIteratorImpl&&) = default; NODISCARD friend FORCEINLINE constexpr bool operator==(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { return LHS.Pointer == RHS.Pointer; } NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { return LHS.Pointer <=> RHS.Pointer; } NODISCARD FORCEINLINE constexpr U& operator*() const { CheckThis(true ); return *Pointer; } NODISCARD FORCEINLINE constexpr U* operator->() const { CheckThis(false); return Pointer; } NODISCARD FORCEINLINE constexpr U& operator[](ptrdiff Index) const { TIteratorImpl Temp = *this + Index; return *Temp; } FORCEINLINE constexpr TIteratorImpl& operator++() { ++Pointer; CheckThis(); return *this; } FORCEINLINE constexpr TIteratorImpl& operator--() { --Pointer; CheckThis(); return *this; } FORCEINLINE constexpr TIteratorImpl operator++(int) { TIteratorImpl Temp = *this; ++*this; return Temp; } FORCEINLINE constexpr TIteratorImpl operator--(int) { TIteratorImpl Temp = *this; --*this; return Temp; } FORCEINLINE constexpr TIteratorImpl& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; } FORCEINLINE constexpr TIteratorImpl& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; } NODISCARD friend FORCEINLINE constexpr TIteratorImpl operator+(TIteratorImpl Iter, ptrdiff Offset) { TIteratorImpl Temp = Iter; Temp += Offset; return Temp; } NODISCARD friend FORCEINLINE constexpr TIteratorImpl operator+(ptrdiff Offset, TIteratorImpl Iter) { TIteratorImpl Temp = Iter; Temp += Offset; return Temp; } NODISCARD FORCEINLINE constexpr TIteratorImpl operator-(ptrdiff Offset) const { TIteratorImpl Temp = *this; Temp -= Offset; return Temp; } NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TIteratorImpl& LHS, const TIteratorImpl& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; } private: # if DO_CHECK const TStaticArray* Owner = nullptr; # endif U* Pointer = nullptr; # if DO_CHECK FORCEINLINE constexpr TIteratorImpl(const TStaticArray* InContainer, U* InPointer) : Owner(InContainer), Pointer(InPointer) { } # else FORCEINLINE constexpr TIteratorImpl(const TStaticArray* InContainer, U* 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().")); } template friend class TIteratorImpl; friend TStaticArray; }; }; 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 // ReSharper disable CppInconsistentNaming 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 // ReSharper restore CppInconsistentNaming