Compare commits

..

No commits in common. "9debe74b3295a9e114a6c6cbead3cdbc9a053619" and "bc3cc3d2cc16b0712418901bf836281f2cc0fe06" have entirely different histories.

3 changed files with 151 additions and 267 deletions

View File

@ -14,38 +14,35 @@ void TestContainers()
TestArray(); TestArray();
} }
NAMESPACE_UNNAMED_BEGIN void TestArray()
template <typename Allocator, size_t Capacity>
void TestArrayTemplate()
{ {
{ {
TArray<int32, Allocator> ArrayA; TArray<int32> ArrayA;
TArray<int32, Allocator> ArrayB(4); TArray<int32> ArrayB(4);
TArray<int32, Allocator> ArrayC(4, 4); TArray<int32> ArrayC(4, 4);
TArray<int32, Allocator> ArrayD(ArrayC); TArray<int32> ArrayD(ArrayC);
TArray<int32, Allocator> ArrayE(MoveTemp(ArrayB)); TArray<int32> ArrayE(MoveTemp(ArrayB));
TArray<int32, Allocator> ArrayF({ 0, 1, 2, 3 }); TArray<int32> ArrayF({ 0, 1, 2, 3 });
TArray<int32, Allocator> ArrayG; TArray<int32> ArrayG;
TArray<int32, Allocator> ArrayH; TArray<int32> ArrayH;
TArray<int32, Allocator> ArrayI; TArray<int32> ArrayI;
ArrayG = ArrayD; ArrayG = ArrayD;
ArrayH = MoveTemp(ArrayE); ArrayH = MoveTemp(ArrayE);
ArrayI = { 0, 1, 2, 3 }; ArrayI = { 0, 1, 2, 3 };
always_check((ArrayC == TArray<int32, Allocator>({ 4, 4, 4, 4 }))); always_check((ArrayC == TArray<int32>({ 4, 4, 4, 4 })));
always_check((ArrayD == TArray<int32, Allocator>({ 4, 4, 4, 4 }))); always_check((ArrayD == TArray<int32>({ 4, 4, 4, 4 })));
always_check((ArrayG == TArray<int32, Allocator>({ 4, 4, 4, 4 }))); always_check((ArrayG == TArray<int32>({ 4, 4, 4, 4 })));
always_check((ArrayF == TArray<int32, Allocator>({ 0, 1, 2, 3 }))); always_check((ArrayF == TArray<int32>({ 0, 1, 2, 3 })));
always_check((ArrayI == TArray<int32, Allocator>({ 0, 1, 2, 3 }))); always_check((ArrayI == TArray<int32>({ 0, 1, 2, 3 })));
} }
{ {
TArray<int32, Allocator> ArrayA = { 1, 2, 3 }; TArray<int32> ArrayA = { 1, 2, 3 };
TArray<int32, Allocator> ArrayB = { 7, 8, 9, 10 }; TArray<int32> ArrayB = { 7, 8, 9, 10 };
TArray<int32, Allocator> ArrayC = { 1, 2, 3 }; TArray<int32> ArrayC = { 1, 2, 3 };
always_check((!(ArrayA == ArrayB))); always_check((!(ArrayA == ArrayB)));
always_check(( (ArrayA != ArrayB))); always_check(( (ArrayA != ArrayB)));
@ -63,25 +60,25 @@ void TestArrayTemplate()
} }
{ {
TArray<int32, Allocator> Array = { 1, 2, 3 }; TArray<int32> Array = { 1, 2, 3 };
Array.Insert(Array.Begin() + 1, 2); Array.Insert(Array.Begin() + 1, 2);
always_check((Array == TArray<int32, Allocator>({ 1, 2, 2, 3 }))); always_check((Array == TArray<int32>({ 1, 2, 2, 3 })));
Array.Insert(Array.End(), 2, 4); Array.Insert(Array.End(), 2, 4);
always_check((Array == TArray<int32, Allocator>({ 1, 2, 2, 3, 4, 4 }))); always_check((Array == TArray<int32>({ 1, 2, 2, 3, 4, 4 })));
Array.Insert(Array.Begin(), { 1, 1, 4, 5, 1, 4 }); Array.Insert(Array.Begin(), { 1, 1, 4, 5, 1, 4 });
always_check((Array == TArray<int32, Allocator>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4 }))); always_check((Array == TArray<int32>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4 })));
Array.Emplace(Array.End(), 5); Array.Emplace(Array.End(), 5);
always_check((Array == TArray<int32, Allocator>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4, 5 }))); always_check((Array == TArray<int32>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4, 5 })));
Array.StableErase(Array.End() - 1); Array.StableErase(Array.End() - 1);
always_check((Array == TArray<int32, Allocator>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4 }))); always_check((Array == TArray<int32>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3, 4, 4 })));
Array.StableErase(Array.End() - 2, Array.End()); Array.StableErase(Array.End() - 2, Array.End());
always_check((Array == TArray<int32, Allocator>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3 }))); always_check((Array == TArray<int32>({ 1, 1, 4, 5, 1, 4, 1, 2, 2, 3 })));
Array.Erase(Array.End() - 2); Array.Erase(Array.End() - 2);
always_check((Array.Num() == 9)); always_check((Array.Num() == 9));
@ -91,43 +88,33 @@ void TestArrayTemplate()
} }
{ {
TArray<int32, Allocator> Array = { 1, 2, 3 }; TArray<int32> Array = { 1, 2, 3 };
Array.PushBack(4); Array.PushBack(4);
always_check((Array == TArray<int32, Allocator>({ 1, 2, 3, 4 }))); always_check((Array == TArray<int32>({ 1, 2, 3, 4 })));
Array.EmplaceBack(5); Array.EmplaceBack(5);
always_check((Array == TArray<int32, Allocator>({ 1, 2, 3, 4, 5 }))); always_check((Array == TArray<int32>({ 1, 2, 3, 4, 5 })));
Array.EmplaceBack(5) = 6; Array.EmplaceBack(5) = 6;
always_check((Array == TArray<int32, Allocator>({ 1, 2, 3, 4, 5, 6 }))); always_check((Array == TArray<int32>({ 1, 2, 3, 4, 5, 6 })));
Array.PopBack(); Array.PopBack();
always_check((Array == TArray<int32, Allocator>({ 1, 2, 3, 4, 5 }))); always_check((Array == TArray<int32>({ 1, 2, 3, 4, 5 })));
Array.SetNum(4); Array.SetNum(4);
always_check((Array == TArray<int32, Allocator>({ 1, 2, 3, 4 }))); always_check((Array == TArray<int32>({ 1, 2, 3, 4 })));
Array.Reserve(64); Array.Reserve(64);
always_check((Array.Num() == 4)); always_check((Array.Num() == 4));
always_check((Array.Max() == 64 || Array.Max() == Capacity)); always_check((Array.Max() == 64));
Array.Shrink(); Array.Shrink();
always_check((Array.Num() == 4)); always_check((Array.Num() == 4));
always_check((Array.Max() == 4 || Array.Max() == Capacity)); always_check((Array.Max() == 4));
} }
} }
NAMESPACE_UNNAMED_END
void TestArray()
{
TestArrayTemplate<FDefaultAllocator, 0>();
TestArrayTemplate<FHeapAllocator, 0>();
TestArrayTemplate<TInlineAllocator<8>, 8>();
TestArrayTemplate<TFixedAllocator<64>, 64>();
}
NAMESPACE_END(Testing) NAMESPACE_END(Testing)
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)

View File

@ -26,23 +26,23 @@ class TArrayStorage<T, A, true> : private A
{ {
public: public:
FORCEINLINE TArrayStorage() = default; FORCEINLINE constexpr TArrayStorage() = default;
FORCEINLINE TArrayStorage(const TArrayStorage&) = delete; FORCEINLINE constexpr TArrayStorage(const TArrayStorage&) = delete;
FORCEINLINE TArrayStorage(TArrayStorage&& InValue) = delete; FORCEINLINE constexpr TArrayStorage(TArrayStorage&& InValue) = delete;
FORCEINLINE TArrayStorage& operator=(const TArrayStorage&) = delete; FORCEINLINE constexpr TArrayStorage& operator=(const TArrayStorage&) = delete;
FORCEINLINE TArrayStorage& operator=(TArrayStorage&&) = delete; FORCEINLINE constexpr TArrayStorage& operator=(TArrayStorage&&) = delete;
FORCEINLINE T*& GetPointer() { return Pointer; } FORCEINLINE constexpr T*& GetPointer() { return Pointer; }
FORCEINLINE T* GetPointer() const { return Pointer; } FORCEINLINE constexpr T* GetPointer() const { return Pointer; }
FORCEINLINE size_t& GetNum() { return ArrayNum; } FORCEINLINE constexpr size_t& GetNum() { return ArrayNum; }
FORCEINLINE size_t GetNum() const { return ArrayNum; } FORCEINLINE constexpr size_t GetNum() const { return ArrayNum; }
FORCEINLINE size_t& GetMax() { return ArrayMax; } FORCEINLINE constexpr size_t& GetMax() { return ArrayMax; }
FORCEINLINE size_t GetMax() const { return ArrayMax; } FORCEINLINE constexpr size_t GetMax() const { return ArrayMax; }
FORCEINLINE A& GetAllocator() { return *this; } FORCEINLINE constexpr A& GetAllocator() { return *this; }
FORCEINLINE const A& GetAllocator() const { return *this; } FORCEINLINE constexpr const A& GetAllocator() const { return *this; }
private: private:
@ -61,23 +61,23 @@ class TArrayStorage<T, A, false>
{ {
public: public:
FORCEINLINE TArrayStorage() = default; FORCEINLINE constexpr TArrayStorage() = default;
FORCEINLINE TArrayStorage(const TArrayStorage&) = delete; FORCEINLINE constexpr TArrayStorage(const TArrayStorage&) = delete;
FORCEINLINE TArrayStorage(TArrayStorage&& InValue) = delete; FORCEINLINE constexpr TArrayStorage(TArrayStorage&& InValue) = delete;
FORCEINLINE TArrayStorage& operator=(const TArrayStorage&) = delete; FORCEINLINE constexpr TArrayStorage& operator=(const TArrayStorage&) = delete;
FORCEINLINE TArrayStorage& operator=(TArrayStorage&&) = delete; FORCEINLINE constexpr TArrayStorage& operator=(TArrayStorage&&) = delete;
FORCEINLINE T*& GetPointer() { return Pointer; } FORCEINLINE constexpr T*& GetPointer() { return Pointer; }
FORCEINLINE T* GetPointer() const { return Pointer; } FORCEINLINE constexpr T* GetPointer() const { return Pointer; }
FORCEINLINE size_t& GetNum() { return ArrayNum; } FORCEINLINE constexpr size_t& GetNum() { return ArrayNum; }
FORCEINLINE size_t GetNum() const { return ArrayNum; } FORCEINLINE constexpr size_t GetNum() const { return ArrayNum; }
FORCEINLINE size_t& GetMax() { return ArrayMax; } FORCEINLINE constexpr size_t& GetMax() { return ArrayMax; }
FORCEINLINE size_t GetMax() const { return ArrayMax; } FORCEINLINE constexpr size_t GetMax() const { return ArrayMax; }
FORCEINLINE A& GetAllocator() { return Allocator; } FORCEINLINE constexpr A& GetAllocator() { return Allocator; }
FORCEINLINE const A& GetAllocator() const { return Allocator; } FORCEINLINE constexpr const A& GetAllocator() const { return Allocator; }
private: private:
@ -98,53 +98,53 @@ public:
using ElementType = T; using ElementType = T;
# if DO_CHECK # if DO_CHECK
FORCEINLINE TArrayIterator() : Owner(nullptr) { } FORCEINLINE constexpr TArrayIterator() : Owner(nullptr) { }
# else # else
FORCEINLINE TArrayIterator() = default; FORCEINLINE constexpr TArrayIterator() = default;
# endif # endif
# if DO_CHECK # if DO_CHECK
FORCEINLINE TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>) FORCEINLINE constexpr TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Owner(InValue.Owner), Pointer(InValue.Pointer) : Owner(InValue.Owner), Pointer(InValue.Pointer)
{ } { }
# else # else
FORCEINLINE TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>) FORCEINLINE constexpr TArrayIterator(const TArrayIterator<ArrayType, TRemoveConst<ElementType>>& InValue) requires (CConst<ElementType>)
: Pointer(InValue.Pointer) : Pointer(InValue.Pointer)
{ } { }
# endif # endif
FORCEINLINE TArrayIterator(const TArrayIterator&) = default; FORCEINLINE constexpr TArrayIterator(const TArrayIterator&) = default;
FORCEINLINE TArrayIterator(TArrayIterator&&) = default; FORCEINLINE constexpr TArrayIterator(TArrayIterator&&) = default;
FORCEINLINE TArrayIterator& operator=(const TArrayIterator&) = default; FORCEINLINE constexpr TArrayIterator& operator=(const TArrayIterator&) = default;
FORCEINLINE TArrayIterator& operator=(TArrayIterator&&) = default; FORCEINLINE constexpr TArrayIterator& operator=(TArrayIterator&&) = default;
NODISCARD friend FORCEINLINE bool operator==(const TArrayIterator& LHS, const TArrayIterator& RHS) { return LHS.Pointer == RHS.Pointer; } NODISCARD friend FORCEINLINE constexpr bool operator==(const TArrayIterator& LHS, const TArrayIterator& RHS) { return LHS.Pointer == RHS.Pointer; }
NODISCARD friend FORCEINLINE strong_ordering operator<=>(const TArrayIterator & LHS, const TArrayIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; } NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TArrayIterator & LHS, const TArrayIterator & RHS) { return LHS.Pointer <=> RHS.Pointer; }
NODISCARD FORCEINLINE ElementType& operator*() const { CheckThis(true); return *Pointer; } NODISCARD FORCEINLINE constexpr ElementType& operator*() const { CheckThis(true); return *Pointer; }
NODISCARD FORCEINLINE ElementType* operator->() const { CheckThis(true); return Pointer; } NODISCARD FORCEINLINE constexpr ElementType* operator->() const { CheckThis(true); return Pointer; }
NODISCARD FORCEINLINE ElementType& operator[](ptrdiff Index) const { TArrayIterator Temp = *this + Index; return *Temp; } NODISCARD FORCEINLINE constexpr ElementType& operator[](ptrdiff Index) const { TArrayIterator Temp = *this + Index; return *Temp; }
FORCEINLINE TArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; } FORCEINLINE constexpr TArrayIterator& operator++() { ++Pointer; CheckThis(); return *this; }
FORCEINLINE TArrayIterator& operator--() { --Pointer; CheckThis(); return *this; } FORCEINLINE constexpr TArrayIterator& operator--() { --Pointer; CheckThis(); return *this; }
FORCEINLINE TArrayIterator operator++(int) { TArrayIterator Temp = *this; ++Pointer; CheckThis(); return Temp; } FORCEINLINE constexpr TArrayIterator operator++(int) { TArrayIterator Temp = *this; ++Pointer; CheckThis(); return Temp; }
FORCEINLINE TArrayIterator operator--(int) { TArrayIterator Temp = *this; --Pointer; CheckThis(); return Temp; } FORCEINLINE constexpr TArrayIterator operator--(int) { TArrayIterator Temp = *this; --Pointer; CheckThis(); return Temp; }
FORCEINLINE TArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; } FORCEINLINE constexpr TArrayIterator& operator+=(ptrdiff Offset) { Pointer += Offset; CheckThis(); return *this; }
FORCEINLINE TArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; } FORCEINLINE constexpr TArrayIterator& operator-=(ptrdiff Offset) { Pointer -= Offset; CheckThis(); return *this; }
NODISCARD friend FORCEINLINE TArrayIterator operator+(TArrayIterator Iter, ptrdiff Offset) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; } NODISCARD friend FORCEINLINE constexpr TArrayIterator operator+(TArrayIterator Iter, ptrdiff Offset) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD friend FORCEINLINE TArrayIterator operator+(ptrdiff Offset, TArrayIterator Iter) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; } NODISCARD friend FORCEINLINE constexpr TArrayIterator operator+(ptrdiff Offset, TArrayIterator Iter) { TArrayIterator Temp = Iter; Temp += Offset; return Temp; }
NODISCARD FORCEINLINE TArrayIterator operator-(ptrdiff Offset) const { TArrayIterator Temp = *this; Temp -= Offset; return Temp; } NODISCARD FORCEINLINE constexpr TArrayIterator operator-(ptrdiff Offset) const { TArrayIterator Temp = *this; Temp -= Offset; return Temp; }
NODISCARD friend FORCEINLINE ptrdiff operator-(const TArrayIterator& LHS, const TArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; } NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TArrayIterator& LHS, const TArrayIterator& RHS) { LHS.CheckThis(); RHS.CheckThis(); return LHS.Pointer - RHS.Pointer; }
NODISCARD FORCEINLINE explicit operator ElementType*() requires (!CConst<ElementType>) { CheckThis(); return Pointer; } NODISCARD FORCEINLINE constexpr explicit operator ElementType*() requires (!CConst<ElementType>) { CheckThis(); return Pointer; }
NODISCARD FORCEINLINE explicit operator const ElementType*() const { CheckThis(); return Pointer; } NODISCARD FORCEINLINE constexpr explicit operator const ElementType*() const { CheckThis(); return Pointer; }
private: private:
@ -155,11 +155,11 @@ private:
ElementType* Pointer; ElementType* Pointer;
# if DO_CHECK # if DO_CHECK
FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer) FORCEINLINE constexpr TArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Owner(InContainer), Pointer(InPointer) : Owner(InContainer), Pointer(InPointer)
{ } { }
# else # else
FORCEINLINE TArrayIterator(const ArrayType* InContainer, ElementType* InPointer) FORCEINLINE constexpr TArrayIterator(const ArrayType* InContainer, ElementType* InPointer)
: Pointer(InPointer) : Pointer(InPointer)
{ } { }
# endif # endif
@ -198,10 +198,10 @@ public:
static_assert(CContiguousIterator<ConstIterator>); static_assert(CContiguousIterator<ConstIterator>);
/** Default constructor. Constructs an empty container with a default-constructed allocator. */ /** Default constructor. Constructs an empty container with a default-constructed allocator. */
FORCEINLINE TArray() : TArray(0) { } FORCEINLINE constexpr TArray() : TArray(0) { }
/** Constructs the container with 'Count' default instances of T. */ /** Constructs the container with 'Count' default instances of T. */
explicit TArray(size_t Count) requires (CDefaultConstructible<ElementType>) constexpr explicit TArray(size_t Count) requires (CDefaultConstructible<ElementType>)
{ {
Storage.GetNum() = Count; Storage.GetNum() = Count;
Storage.GetMax() = Storage.GetAllocator().CalculateSlackReserve(Num()); Storage.GetMax() = Storage.GetAllocator().CalculateSlackReserve(Num());
@ -211,7 +211,7 @@ public:
} }
/** Constructs the container with 'Count' copies of elements with 'InValue'. */ /** Constructs the container with 'Count' copies of elements with 'InValue'. */
TArray(size_t Count, const ElementType& InValue) requires (CCopyConstructible<ElementType>) constexpr TArray(size_t Count, const ElementType& InValue) requires (CCopyConstructible<ElementType>)
{ {
Storage.GetNum() = Count; Storage.GetNum() = Count;
Storage.GetMax() = Storage.GetAllocator().CalculateSlackReserve(Num()); Storage.GetMax() = Storage.GetAllocator().CalculateSlackReserve(Num());
@ -225,11 +225,11 @@ public:
/** Constructs the container with the contents of the range ['First', 'Last'). */ /** Constructs the container with the contents of the range ['First', 'Last'). */
template <CInputIterator I, CSentinelFor<I> S> requires (CConstructibleFrom<ElementType, TIteratorReferenceType<I>> && CMovable<ElementType>) template <CInputIterator I, CSentinelFor<I> S> requires (CConstructibleFrom<ElementType, TIteratorReferenceType<I>> && CMovable<ElementType>)
TArray(I First, S Last) constexpr TArray(I First, S Last)
{ {
if constexpr (CForwardIterator<I>) if constexpr (CForwardIterator<I>)
{ {
if (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); if constexpr (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last."));
const size_t Count = Iteration::Distance(First, Last); const size_t Count = Iteration::Distance(First, Last);
@ -257,7 +257,7 @@ public:
} }
/** Copy constructor. Constructs the container with the copy of the contents of 'InValue'. */ /** Copy constructor. Constructs the container with the copy of the contents of 'InValue'. */
TArray(const TArray& InValue) requires (CCopyConstructible<ElementType>) constexpr TArray(const TArray& InValue) requires (CCopyConstructible<ElementType>)
{ {
Storage.GetNum() = InValue.Num(); Storage.GetNum() = InValue.Num();
Storage.GetMax() = Storage.GetAllocator().CalculateSlackReserve(Num()); Storage.GetMax() = Storage.GetAllocator().CalculateSlackReserve(Num());
@ -267,7 +267,7 @@ public:
} }
/** Move constructor. After the move, 'InValue' is guaranteed to be empty. */ /** Move constructor. After the move, 'InValue' is guaranteed to be empty. */
TArray(TArray&& InValue) requires (CMoveConstructible<ElementType>) constexpr TArray(TArray&& InValue) requires (CMoveConstructible<ElementType>)
{ {
Storage.GetNum() = InValue.Num(); Storage.GetNum() = InValue.Num();
@ -290,17 +290,17 @@ public:
} }
/** Constructs the container with the contents of the initializer list. */ /** Constructs the container with the contents of the initializer list. */
FORCEINLINE TArray(initializer_list<ElementType> IL) requires (CCopyConstructible<ElementType>) : TArray(Iteration::Begin(IL), Iteration::End(IL)) { } FORCEINLINE constexpr TArray(initializer_list<ElementType> IL) requires (CCopyConstructible<ElementType>) : TArray(Iteration::Begin(IL), Iteration::End(IL)) { }
/** Destructs the array. The destructors of the elements are called and the used storage is deallocated. */ /** Destructs the array. The destructors of the elements are called and the used storage is deallocated. */
~TArray() constexpr ~TArray()
{ {
Memory::Destruct(Storage.GetPointer(),Num()); Memory::Destruct(Storage.GetPointer(),Num());
Storage.GetAllocator().Deallocate(Storage.GetPointer()); Storage.GetAllocator().Deallocate(Storage.GetPointer());
} }
/** Copy assignment operator. Replaces the contents with a copy of the contents of 'InValue'. */ /** Copy assignment operator. Replaces the contents with a copy of the contents of 'InValue'. */
TArray& operator=(const TArray& InValue) requires (CCopyable<ElementType>) constexpr TArray& operator=(const TArray& InValue) requires (CCopyable<ElementType>)
{ {
if (&InValue == this) UNLIKELY return *this; if (&InValue == this) UNLIKELY return *this;
@ -341,7 +341,7 @@ public:
} }
/** Move assignment operator. After the move, 'InValue' is guaranteed to be empty. */ /** Move assignment operator. After the move, 'InValue' is guaranteed to be empty. */
TArray& operator=(TArray&& InValue) requires (CMovable<ElementType>) constexpr TArray& operator=(TArray&& InValue) requires (CMovable<ElementType>)
{ {
if (&InValue == this) UNLIKELY return *this; if (&InValue == this) UNLIKELY return *this;
@ -400,7 +400,7 @@ public:
} }
/** Replaces the contents with those identified by initializer list. */ /** Replaces the contents with those identified by initializer list. */
TArray& operator=(initializer_list<ElementType> IL) requires (CCopyable<ElementType>) constexpr TArray& operator=(initializer_list<ElementType> IL) requires (CCopyable<ElementType>)
{ {
size_t NumToAllocate = GetNum(IL); size_t NumToAllocate = GetNum(IL);
@ -439,7 +439,7 @@ public:
} }
/** Compares the contents of two arrays. */ /** Compares the contents of two arrays. */
NODISCARD friend bool operator==(const TArray& LHS, const TArray& RHS) requires (CWeaklyEqualityComparable<ElementType>) NODISCARD friend constexpr bool operator==(const TArray& LHS, const TArray& RHS) requires (CWeaklyEqualityComparable<ElementType>)
{ {
if (LHS.Num() != RHS.Num()) return false; if (LHS.Num() != RHS.Num()) return false;
@ -460,7 +460,7 @@ public:
} }
/** Compares the contents of two arrays. */ /** Compares the contents of two arrays. */
NODISCARD friend auto operator<=>(const TArray& LHS, const TArray& RHS) requires (CSynthThreeWayComparable<ElementType>) NODISCARD friend constexpr auto operator<=>(const TArray& LHS, const TArray& RHS) requires (CSynthThreeWayComparable<ElementType>)
{ {
using OrderingType = TSynthThreeWayResult<ElementType>; using OrderingType = TSynthThreeWayResult<ElementType>;
@ -486,7 +486,7 @@ public:
} }
/** Inserts 'InValue' before 'Iter' in the container. */ /** Inserts 'InValue' before 'Iter' in the container. */
Iterator Insert(ConstIterator Iter, const ElementType& InValue) requires (CCopyable<ElementType>) constexpr Iterator Insert(ConstIterator Iter, const ElementType& InValue) requires (CCopyable<ElementType>)
{ {
checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator()."));
@ -534,7 +534,7 @@ public:
} }
/** Inserts 'InValue' before 'Iter' in the container. */ /** Inserts 'InValue' before 'Iter' in the container. */
Iterator Insert(ConstIterator Iter, ElementType&& InValue) requires (CMovable<ElementType>) constexpr Iterator Insert(ConstIterator Iter, ElementType&& InValue) requires (CMovable<ElementType>)
{ {
checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator()."));
@ -582,7 +582,7 @@ public:
} }
/** Inserts 'Count' copies of the 'InValue' before 'Iter' in the container. */ /** Inserts 'Count' copies of the 'InValue' before 'Iter' in the container. */
Iterator Insert(ConstIterator Iter, size_t Count, const ElementType& InValue) requires (CCopyable<ElementType>) constexpr Iterator Insert(ConstIterator Iter, size_t Count, const ElementType& InValue) requires (CCopyable<ElementType>)
{ {
checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator()."));
@ -683,13 +683,13 @@ public:
/** Inserts elements from range ['First', 'Last') before 'Iter'. */ /** Inserts elements from range ['First', 'Last') before 'Iter'. */
template <CInputIterator I, CSentinelFor<I> S> requires (CConstructibleFrom<ElementType, TIteratorReferenceType<I>> template <CInputIterator I, CSentinelFor<I> S> requires (CConstructibleFrom<ElementType, TIteratorReferenceType<I>>
&& CAssignableFrom<ElementType&, TIteratorReferenceType<I>> && CMovable<ElementType>) && CAssignableFrom<ElementType&, TIteratorReferenceType<I>> && CMovable<ElementType>)
Iterator Insert(ConstIterator Iter, I First, S Last) constexpr Iterator Insert(ConstIterator Iter, I First, S Last)
{ {
checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator()."));
if constexpr (CForwardIterator<I>) if constexpr (CForwardIterator<I>)
{ {
if (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last.")); if constexpr (CSizedSentinelFor<S, I>) checkf(First <= Last, TEXT("Illegal range iterator. Please check First <= Last."));
const size_t InsertIndex = Iter - Begin(); const size_t InsertIndex = Iter - Begin();
const size_t Count = Iteration::Distance(First, Last); const size_t Count = Iteration::Distance(First, Last);
@ -766,14 +766,14 @@ public:
} }
/** Inserts elements from initializer list before 'Iter' in the container. */ /** Inserts elements from initializer list before 'Iter' in the container. */
FORCEINLINE Iterator Insert(ConstIterator Iter, initializer_list<ElementType> IL) requires (CCopyable<ElementType>) FORCEINLINE constexpr Iterator Insert(ConstIterator Iter, initializer_list<ElementType> IL) requires (CCopyable<ElementType>)
{ {
return Insert(Iter, Iteration::Begin(IL), Iteration::End(IL)); return Insert(Iter, Iteration::Begin(IL), Iteration::End(IL));
} }
/** Inserts a new element into the container directly before 'Iter'. */ /** Inserts a new element into the container directly before 'Iter'. */
template <typename... Ts> requires (CConstructibleFrom<ElementType, Ts...> && CMovable<ElementType>) template <typename... Ts> requires (CConstructibleFrom<ElementType, Ts...> && CMovable<ElementType>)
Iterator Emplace(ConstIterator Iter, Ts&&... Args) constexpr Iterator Emplace(ConstIterator Iter, Ts&&... Args)
{ {
checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter), TEXT("Read access violation. Please check IsValidIterator()."));
@ -821,7 +821,7 @@ public:
} }
/** Removes the element at 'Iter' in the container. Without changing the order of elements. */ /** Removes the element at 'Iter' in the container. Without changing the order of elements. */
FORCEINLINE Iterator StableErase(ConstIterator Iter, bool bAllowShrinking = true) requires (CMovable<ElementType>) FORCEINLINE constexpr Iterator StableErase(ConstIterator Iter, bool bAllowShrinking = true) requires (CMovable<ElementType>)
{ {
checkf(IsValidIterator(Iter) && Iter != End(), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter) && Iter != End(), TEXT("Read access violation. Please check IsValidIterator()."));
@ -829,7 +829,7 @@ public:
} }
/** Removes the elements in the range ['First', 'Last') in the container. Without changing the order of elements. */ /** Removes the elements in the range ['First', 'Last') in the container. Without changing the order of elements. */
Iterator StableErase(ConstIterator First, ConstIterator Last, bool bAllowShrinking = true) requires (CMovable<ElementType>) constexpr Iterator StableErase(ConstIterator First, ConstIterator Last, bool bAllowShrinking = true) requires (CMovable<ElementType>)
{ {
checkf(IsValidIterator(First) && IsValidIterator(Last) && First <= Last, TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(First) && IsValidIterator(Last) && First <= Last, TEXT("Read access violation. Please check IsValidIterator()."));
@ -871,7 +871,7 @@ public:
} }
/** Removes the element at 'Iter' in the container. But it may change the order of elements. */ /** Removes the element at 'Iter' in the container. But it may change the order of elements. */
FORCEINLINE Iterator Erase(ConstIterator Iter, bool bAllowShrinking = true) requires (CMovable<ElementType>) FORCEINLINE constexpr Iterator Erase(ConstIterator Iter, bool bAllowShrinking = true) requires (CMovable<ElementType>)
{ {
checkf(IsValidIterator(Iter) && Iter != End(), TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(Iter) && Iter != End(), TEXT("Read access violation. Please check IsValidIterator()."));
@ -879,7 +879,7 @@ public:
} }
/** Removes the elements in the range ['First', 'Last') in the container. But it may change the order of elements. */ /** Removes the elements in the range ['First', 'Last') in the container. But it may change the order of elements. */
Iterator Erase(ConstIterator First, ConstIterator Last, bool bAllowShrinking = true) requires (CMovable<ElementType>) constexpr Iterator Erase(ConstIterator First, ConstIterator Last, bool bAllowShrinking = true) requires (CMovable<ElementType>)
{ {
checkf(IsValidIterator(First) && IsValidIterator(Last) && First <= Last, TEXT("Read access violation. Please check IsValidIterator().")); checkf(IsValidIterator(First) && IsValidIterator(Last) && First <= Last, TEXT("Read access violation. Please check IsValidIterator()."));
@ -923,20 +923,20 @@ public:
} }
/** Appends the given element value to the end of the container. */ /** Appends the given element value to the end of the container. */
FORCEINLINE void PushBack(const ElementType& InValue) requires (CCopyable<ElementType>) FORCEINLINE constexpr void PushBack(const ElementType& InValue) requires (CCopyable<ElementType>)
{ {
EmplaceBack(InValue); EmplaceBack(InValue);
} }
/** Appends the given element value to the end of the container. */ /** Appends the given element value to the end of the container. */
FORCEINLINE void PushBack(ElementType&& InValue) requires (CMovable<ElementType>) FORCEINLINE constexpr void PushBack(ElementType&& InValue) requires (CMovable<ElementType>)
{ {
EmplaceBack(MoveTemp(InValue)); EmplaceBack(MoveTemp(InValue));
} }
/** Appends a new element to the end of the container. */ /** Appends a new element to the end of the container. */
template <typename... Ts> requires (CConstructibleFrom<ElementType, Ts...> && CMovable<ElementType>) template <typename... Ts> requires (CConstructibleFrom<ElementType, Ts...> && CMovable<ElementType>)
ElementType& EmplaceBack(Ts&&... Args) constexpr ElementType& EmplaceBack(Ts&&... Args)
{ {
const size_t NumToAllocate = Num() + 1 > Max() ? Storage.GetAllocator().CalculateSlackGrow(Num() + 1, Max()) : Max(); const size_t NumToAllocate = Num() + 1 > Max() ? Storage.GetAllocator().CalculateSlackGrow(Num() + 1, Max()) : Max();
@ -968,13 +968,13 @@ public:
} }
/** Removes the last element of the container. The array cannot be empty. */ /** Removes the last element of the container. The array cannot be empty. */
FORCEINLINE void PopBack(bool bAllowShrinking = true) requires (CMovable<ElementType>) FORCEINLINE constexpr void PopBack(bool bAllowShrinking = true) requires (CMovable<ElementType>)
{ {
Erase(End() - 1, bAllowShrinking); Erase(End() - 1, bAllowShrinking);
} }
/** Resizes the container to contain 'Count' elements. Additional default elements are appended. */ /** Resizes the container to contain 'Count' elements. Additional default elements are appended. */
void SetNum(size_t Count, bool bAllowShrinking = true) requires (CDefaultConstructible<ElementType> && CMovable<ElementType>) constexpr void SetNum(size_t Count, bool bAllowShrinking = true) requires (CDefaultConstructible<ElementType> && CMovable<ElementType>)
{ {
size_t NumToAllocate = Count; size_t NumToAllocate = Count;
@ -1020,7 +1020,7 @@ public:
} }
/** Resizes the container to contain 'Count' elements. Additional copies of 'InValue' are appended. */ /** Resizes the container to contain 'Count' elements. Additional copies of 'InValue' are appended. */
void SetNum(size_t Count, const ElementType& InValue, bool bAllowShrinking = true) requires (CCopyConstructible<ElementType> && CMovable<ElementType>) constexpr void SetNum(size_t Count, const ElementType& InValue, bool bAllowShrinking = true) requires (CCopyConstructible<ElementType> && CMovable<ElementType>)
{ {
size_t NumToAllocate = Count; size_t NumToAllocate = Count;
@ -1073,7 +1073,7 @@ public:
} }
/** Increase the max capacity of the array to a value that's greater or equal to 'Count'. */ /** Increase the max capacity of the array to a value that's greater or equal to 'Count'. */
void Reserve(size_t Count) requires (CMovable<ElementType>) constexpr void Reserve(size_t Count) requires (CMovable<ElementType>)
{ {
if (Count <= Max()) return; if (Count <= Max()) return;
@ -1092,7 +1092,7 @@ public:
} }
/** Requests the removal of unused capacity. */ /** Requests the removal of unused capacity. */
void Shrink() constexpr void Shrink()
{ {
size_t NumToAllocate = Storage.GetAllocator().CalculateSlackReserve(Num()); size_t NumToAllocate = Storage.GetAllocator().CalculateSlackReserve(Num());
@ -1112,45 +1112,45 @@ public:
} }
/** @return The pointer to the underlying element storage. */ /** @return The pointer to the underlying element storage. */
NODISCARD FORCEINLINE TObserverPtr< ElementType[]> GetData() { return Storage.GetPointer(); } NODISCARD FORCEINLINE constexpr TObserverPtr< ElementType[]> GetData() { return Storage.GetPointer(); }
NODISCARD FORCEINLINE TObserverPtr<const ElementType[]> GetData() const { return Storage.GetPointer(); } NODISCARD FORCEINLINE constexpr TObserverPtr<const ElementType[]> GetData() const { return Storage.GetPointer(); }
/** @return The iterator to the first or end element. */ /** @return The iterator to the first or end element. */
NODISCARD FORCEINLINE Iterator Begin() { return Iterator(this, Storage.GetPointer()); } NODISCARD FORCEINLINE constexpr Iterator Begin() { return Iterator(this, Storage.GetPointer()); }
NODISCARD FORCEINLINE ConstIterator Begin() const { return ConstIterator(this, Storage.GetPointer()); } NODISCARD FORCEINLINE constexpr ConstIterator Begin() const { return ConstIterator(this, Storage.GetPointer()); }
NODISCARD FORCEINLINE Iterator End() { return Iterator(this, Storage.GetPointer() + Num()); } NODISCARD FORCEINLINE constexpr Iterator End() { return Iterator(this, Storage.GetPointer() + Num()); }
NODISCARD FORCEINLINE ConstIterator End() const { return ConstIterator(this, Storage.GetPointer() + Num()); } NODISCARD FORCEINLINE constexpr ConstIterator End() const { return ConstIterator(this, Storage.GetPointer() + Num()); }
/** @return The reverse iterator to the first or end element. */ /** @return The reverse iterator to the first or end element. */
NODISCARD FORCEINLINE ReverseIterator RBegin() { return ReverseIterator(End()); } NODISCARD FORCEINLINE constexpr ReverseIterator RBegin() { return ReverseIterator(End()); }
NODISCARD FORCEINLINE ConstReverseIterator RBegin() const { return ConstReverseIterator(End()); } NODISCARD FORCEINLINE constexpr ConstReverseIterator RBegin() const { return ConstReverseIterator(End()); }
NODISCARD FORCEINLINE ReverseIterator REnd() { return ReverseIterator(Begin()); } NODISCARD FORCEINLINE constexpr ReverseIterator REnd() { return ReverseIterator(Begin()); }
NODISCARD FORCEINLINE ConstReverseIterator REnd() const { return ConstReverseIterator(Begin()); } NODISCARD FORCEINLINE constexpr ConstReverseIterator REnd() const { return ConstReverseIterator(Begin()); }
/** @return The number of elements in the container. */ /** @return The number of elements in the container. */
NODISCARD FORCEINLINE size_t Num() const { return Storage.GetNum(); } NODISCARD FORCEINLINE constexpr size_t Num() const { return Storage.GetNum(); }
/** @return The number of elements that can be held in currently allocated storage. */ /** @return The number of elements that can be held in currently allocated storage. */
NODISCARD FORCEINLINE size_t Max() const { return Storage.GetMax(); } NODISCARD FORCEINLINE constexpr size_t Max() const { return Storage.GetMax(); }
/** @return true if the container is empty, false otherwise. */ /** @return true if the container is empty, false otherwise. */
NODISCARD FORCEINLINE bool IsEmpty() const { return Num() == 0; } NODISCARD FORCEINLINE constexpr bool IsEmpty() const { return Num() == 0; }
/** @return true if the iterator is valid, false otherwise. */ /** @return true if the iterator is valid, false otherwise. */
NODISCARD FORCEINLINE bool IsValidIterator(ConstIterator Iter) const { return Begin() <= Iter && Iter <= End(); } NODISCARD FORCEINLINE constexpr bool IsValidIterator(ConstIterator Iter) const { return Begin() <= Iter && Iter <= End(); }
/** @return The reference to the requested element. */ /** @return The reference to the requested element. */
NODISCARD FORCEINLINE ElementType& operator[](size_t Index) { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Storage.GetPointer()[Index]; } NODISCARD FORCEINLINE constexpr ElementType& operator[](size_t Index) { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Storage.GetPointer()[Index]; }
NODISCARD FORCEINLINE const ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Storage.GetPointer()[Index]; } NODISCARD FORCEINLINE constexpr const ElementType& operator[](size_t Index) const { checkf(Index < Num(), TEXT("Read access violation. Please check IsValidIterator().")); return Storage.GetPointer()[Index]; }
/** @return The reference to the first or last element. */ /** @return The reference to the first or last element. */
NODISCARD FORCEINLINE ElementType& Front() { return *Begin(); } NODISCARD FORCEINLINE constexpr ElementType& Front() { return *Begin(); }
NODISCARD FORCEINLINE const ElementType& Front() const { return *Begin(); } NODISCARD FORCEINLINE constexpr const ElementType& Front() const { return *Begin(); }
NODISCARD FORCEINLINE ElementType& Back() { return *(End() - 1); } NODISCARD FORCEINLINE constexpr ElementType& Back() { return *(End() - 1); }
NODISCARD FORCEINLINE const ElementType& Back() const { return *(End() - 1); } NODISCARD FORCEINLINE constexpr const ElementType& Back() const { return *(End() - 1); }
/** Erases all elements from the container. After this call, Num() returns zero. */ /** Erases all elements from the container. After this call, Num() returns zero. */
void Reset(bool bAllowShrinking = true) constexpr void Reset(bool bAllowShrinking = true)
{ {
const size_t NumToAllocate = Storage.GetAllocator().CalculateSlackReserve(0); const size_t NumToAllocate = Storage.GetAllocator().CalculateSlackReserve(0);
@ -1171,7 +1171,7 @@ public:
} }
/** Overloads the GetTypeHash algorithm for TArray. */ /** Overloads the GetTypeHash algorithm for TArray. */
NODISCARD friend FORCEINLINE size_t GetTypeHash(const TArray& A) requires (CHashable<ElementType>) NODISCARD friend FORCEINLINE constexpr size_t GetTypeHash(const TArray& A) requires (CHashable<ElementType>)
{ {
size_t Result = 0; size_t Result = 0;
@ -1184,7 +1184,7 @@ public:
} }
/** Overloads the Swap algorithm for TArray. */ /** Overloads the Swap algorithm for TArray. */
friend void Swap(TArray& A, TArray& B) requires (CMovable<ElementType>) friend constexpr void Swap(TArray& A, TArray& B) requires (CMovable<ElementType>)
{ {
const bool bIsTransferable = const bool bIsTransferable =
A.Storage.GetAllocator().IsTransferable(A.Storage.GetPointer()) && A.Storage.GetAllocator().IsTransferable(A.Storage.GetPointer()) &&

View File

@ -24,10 +24,8 @@ concept CInstantiableAllocator = CDerivedFrom<T, FAllocatorInterface> && !CSameA
struct FAllocatorInterface struct FAllocatorInterface
{ {
template <CObject T> template <CObject T>
class ForElementType : private FSingleton struct ForElementType : private FSingleton
{ {
public:
/** /**
* Allocates uninitialized storage. * Allocates uninitialized storage.
* Should be allocated according to the results given by the CalculateSlackReserve() family, * Should be allocated according to the results given by the CalculateSlackReserve() family,
@ -41,16 +39,16 @@ struct FAllocatorInterface
FORCEINLINE void Deallocate(T* InPtr) = delete; FORCEINLINE void Deallocate(T* InPtr) = delete;
/** @return true if allocation can be deallocated by another allocator, otherwise false. */ /** @return true if allocation can be deallocated by another allocator, otherwise false. */
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const { return true; } NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) { return true; }
/** Calculates the amount of slack to allocate for an array that has just grown to a given number of elements. */ /** Calculates the amount of slack to allocate for an array that has just grown to a given number of elements. */
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const = delete; NODISCARD FORCEINLINE constexpr size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const = delete;
/** Calculates the amount of slack to allocate for an array that has just shrunk to a given number of elements. */ /** Calculates the amount of slack to allocate for an array that has just shrunk to a given number of elements. */
NODISCARD FORCEINLINE size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const = delete; NODISCARD FORCEINLINE constexpr size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const = delete;
/** Calculates the amount of slack to allocate for an array that has just grown or shrunk to a given number of elements. */ /** Calculates the amount of slack to allocate for an array that has just grown or shrunk to a given number of elements. */
NODISCARD FORCEINLINE size_t CalculateSlackReserve(size_t Num) const = delete; NODISCARD FORCEINLINE constexpr size_t CalculateSlackReserve(size_t Num) const = delete;
}; };
}; };
@ -59,10 +57,8 @@ struct FAllocatorInterface
struct FHeapAllocator : public FAllocatorInterface struct FHeapAllocator : public FAllocatorInterface
{ {
template <CObject T> template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T> struct ForElementType : public FAllocatorInterface::ForElementType<T>
{ {
public:
NODISCARD FORCEINLINE T* Allocate(size_t InNum) NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{ {
return InNum != 0 ? static_cast<T*>(Memory::Malloc(Memory::QuantizeSize(InNum * sizeof(T)), alignof(T))) : nullptr; return InNum != 0 ? static_cast<T*>(Memory::Malloc(Memory::QuantizeSize(InNum * sizeof(T)), alignof(T))) : nullptr;
@ -73,7 +69,7 @@ struct FHeapAllocator : public FAllocatorInterface
Memory::Free(InPtr); Memory::Free(InPtr);
} }
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const NODISCARD FORCEINLINE constexpr size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const
{ {
const size_t FirstGrow = 4; const size_t FirstGrow = 4;
const size_t ConstantGrow = 16; const size_t ConstantGrow = 16;
@ -89,7 +85,7 @@ struct FHeapAllocator : public FAllocatorInterface
return Result; return Result;
} }
NODISCARD FORCEINLINE size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const NODISCARD FORCEINLINE constexpr size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const
{ {
size_t Result; size_t Result;
@ -112,7 +108,7 @@ struct FHeapAllocator : public FAllocatorInterface
return Result; return Result;
} }
NODISCARD FORCEINLINE size_t CalculateSlackReserve(size_t Num) const NODISCARD FORCEINLINE constexpr size_t CalculateSlackReserve(size_t Num) const
{ {
return Num != 0 ? Memory::QuantizeSize(Num * sizeof(T), alignof(T)) / sizeof(T) : 0; return Num != 0 ? Memory::QuantizeSize(Num * sizeof(T), alignof(T)) / sizeof(T) : 0;
} }
@ -122,105 +118,6 @@ struct FHeapAllocator : public FAllocatorInterface
using FDefaultAllocator = FHeapAllocator; using FDefaultAllocator = FHeapAllocator;
/**
* The inline allocator allocates up to a specified number of elements in the same allocation as the container.
* Any allocation needed beyond that causes all data to be moved into an indirect allocation.
*/
template <size_t NumInline, CInstantiableAllocator SecondaryAllocator = FDefaultAllocator>
struct TInlineAllocator : public FAllocatorInterface
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
{
public:
NODISCARD FORCEINLINE T* Allocate(size_t InNum)
{
if (InNum == 0) return nullptr;
check(InNum >= NumInline);
if (InNum == NumInline) return reinterpret_cast<T*>(&InlineStorage);
return Secondary.Allocate(InNum);
}
FORCEINLINE void Deallocate(T* InPtr)
{
if (InPtr == reinterpret_cast<T*>(&InlineStorage)) return;
Secondary.Deallocate(InPtr);
}
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const
{
if (InPtr == reinterpret_cast<const T*>(&InlineStorage)) return false;
return Secondary.IsTransferable(InPtr);
}
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const
{
check(Num > NumAllocated);
check(NumAllocated >= NumInline);
if (Num <= NumInline) return NumInline;
return Secondary.CalculateSlackGrow(Num, NumAllocated <= NumInline ? 0 : NumAllocated);
}
NODISCARD FORCEINLINE size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const
{
check(Num < NumAllocated);
check(NumAllocated >= NumInline);
if (Num <= NumInline) return NumInline;
return Secondary.CalculateSlackShrink(Num, NumAllocated);
}
NODISCARD FORCEINLINE size_t CalculateSlackReserve(size_t Num) const
{
if (Num <= NumInline) return NumInline;
return Secondary.CalculateSlackReserve(Num);
}
private:
TAlignedStorage<sizeof(T), alignof(T)> InlineStorage[NumInline];
typename SecondaryAllocator::template ForElementType<T> Secondary;
};
};
/** This is a null allocator for which all operations are illegal. */
struct FNullAllocator : public FAllocatorInterface
{
template <CObject T>
class ForElementType : public FAllocatorInterface::ForElementType<T>
{
public:
NODISCARD FORCEINLINE T* Allocate(size_t InNum) { check_no_entry(); return nullptr; }
FORCEINLINE void Deallocate(T* InPtr) { check_no_entry(); }
NODISCARD FORCEINLINE bool IsTransferable(T* InPtr) const { check_no_entry(); return false; }
NODISCARD FORCEINLINE size_t CalculateSlackGrow(size_t Num, size_t NumAllocated) const { check_no_entry(); return 0; }
NODISCARD FORCEINLINE size_t CalculateSlackShrink(size_t Num, size_t NumAllocated) const { check_no_entry(); return 0; }
NODISCARD FORCEINLINE size_t CalculateSlackReserve(size_t Num) const { check_no_entry(); return 0; }
};
};
template <size_t Num>
using TFixedAllocator = TInlineAllocator<Num, FNullAllocator>;
NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft) NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END NAMESPACE_REDCRAFT_END