refactor(containers): remove TInputIterator and TOutputIterator

This commit is contained in:
Redstone1024 2024-10-24 15:24:29 +08:00
parent 3e93da7f44
commit 1d101ce034
2 changed files with 29 additions and 77 deletions

View File

@ -54,7 +54,7 @@ public:
Memory::DefaultConstruct<ElementType>(Impl.Pointer, Num()); Memory::DefaultConstruct<ElementType>(Impl.Pointer, Num());
} }
/** 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>) TArray(size_t Count, const ElementType& InValue) requires (CCopyConstructible<ElementType>)
{ {
@ -158,7 +158,7 @@ public:
{ {
Memory::Destruct(Impl.Pointer, Num()); Memory::Destruct(Impl.Pointer, Num());
Impl->Deallocate(Impl.Pointer); Impl->Deallocate(Impl.Pointer);
Impl.ArrayNum = InValue.Num(); Impl.ArrayNum = InValue.Num();
Impl.ArrayMax = NumToAllocate; Impl.ArrayMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(Max()); Impl.Pointer = Impl->Allocate(Max());
@ -213,13 +213,13 @@ public:
{ {
Memory::Destruct(Impl.Pointer, Num()); Memory::Destruct(Impl.Pointer, Num());
Impl->Deallocate(Impl.Pointer); Impl->Deallocate(Impl.Pointer);
Impl.ArrayNum = InValue.Num(); Impl.ArrayNum = InValue.Num();
Impl.ArrayMax = NumToAllocate; Impl.ArrayMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(Max()); Impl.Pointer = Impl->Allocate(Max());
Memory::MoveConstruct<ElementType>(Impl.Pointer, InValue.Impl.Pointer, Num()); Memory::MoveConstruct<ElementType>(Impl.Pointer, InValue.Impl.Pointer, Num());
InValue.Reset(); InValue.Reset();
return *this; return *this;
@ -256,7 +256,7 @@ public:
{ {
Memory::Destruct(Impl.Pointer, Num()); Memory::Destruct(Impl.Pointer, Num());
Impl->Deallocate(Impl.Pointer); Impl->Deallocate(Impl.Pointer);
Impl.ArrayNum = GetNum(IL); Impl.ArrayNum = GetNum(IL);
Impl.ArrayMax = NumToAllocate; Impl.ArrayMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(Max()); Impl.Pointer = Impl->Allocate(Max());
@ -308,7 +308,7 @@ public:
return LHS.Num() <=> RHS.Num(); return LHS.Num() <=> RHS.Num();
} }
/** Inserts 'InValue' before 'Iter' in the container. */ /** Inserts 'InValue' before 'Iter' in the container. */
Iterator Insert(ConstIterator Iter, const ElementType& InValue) requires (CCopyable<ElementType>) Iterator Insert(ConstIterator Iter, const ElementType& InValue) requires (CCopyable<ElementType>)
{ {
@ -399,7 +399,7 @@ public:
Impl.Pointer[InsertIndex] = MoveTemp(InValue); Impl.Pointer[InsertIndex] = MoveTemp(InValue);
} }
else new (Impl.Pointer + Num()) ElementType(MoveTemp(InValue)); else new (Impl.Pointer + Num()) ElementType(MoveTemp(InValue));
Impl.ArrayNum = Num() + 1; Impl.ArrayNum = Num() + 1;
return Iterator(this, Impl.Pointer + InsertIndex); return Iterator(this, Impl.Pointer + InsertIndex);
@ -443,12 +443,12 @@ public:
} }
/* /*
* NO(XA) - No Operation * NO(XA) - No Operation
* IA(AB) - Insert Assignment * IA(AB) - Insert Assignment
* IC(BC) - Insert Construction * IC(BC) - Insert Construction
* MA(CD) - Move Assignment * MA(CD) - Move Assignment
* MC(DO) - Move Construction * MC(DO) - Move Construction
* *
* IR(AC) - Insert Range * IR(AC) - Insert Range
* UI(UO) - Uninitialized * UI(UO) - Uninitialized
* *
@ -503,7 +503,7 @@ public:
return Iterator(this, Impl.Pointer + InsertIndex); return Iterator(this, Impl.Pointer + InsertIndex);
} }
/** 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>)
@ -585,7 +585,7 @@ public:
else else
{ {
TArray Temp(MoveTemp(First), MoveTemp(Last)); TArray Temp(MoveTemp(First), MoveTemp(Last));
return Insert(Iter, TMoveIterator(Temp.Begin()), TMoveSentinel(Temp.End())); return Insert(Iter, MakeMoveIterator(Temp.Begin()), MakeMoveSentinel(Temp.End()));
} }
} }
@ -706,7 +706,7 @@ public:
Iterator Erase(ConstIterator First, ConstIterator Last, bool bAllowShrinking = true) requires (CMovable<ElementType>) 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()."));
const size_t EraseIndex = First - Begin(); const size_t EraseIndex = First - Begin();
const size_t EraseCount = Last - First; const size_t EraseCount = Last - First;
@ -770,7 +770,7 @@ public:
{ {
ElementType* OldAllocation = Impl.Pointer; ElementType* OldAllocation = Impl.Pointer;
const size_t NumToDestruct = Num(); const size_t NumToDestruct = Num();
Impl.ArrayNum = Num() + 1; Impl.ArrayNum = Num() + 1;
Impl.ArrayMax = NumToAllocate; Impl.ArrayMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(Max()); Impl.Pointer = Impl->Allocate(Max());
@ -801,7 +801,7 @@ public:
void SetNum(size_t Count, bool bAllowShrinking = true) requires (CDefaultConstructible<ElementType> && CMovable<ElementType>) void SetNum(size_t Count, bool bAllowShrinking = true) requires (CDefaultConstructible<ElementType> && CMovable<ElementType>)
{ {
size_t NumToAllocate = Count; size_t NumToAllocate = Count;
NumToAllocate = NumToAllocate > Max() ? Impl->CalculateSlackGrow(Count, Max()) : NumToAllocate; NumToAllocate = NumToAllocate > Max() ? Impl->CalculateSlackGrow(Count, Max()) : NumToAllocate;
NumToAllocate = NumToAllocate < Max() ? (bAllowShrinking ? Impl->CalculateSlackShrink(Count, Max()) : Max()) : NumToAllocate; NumToAllocate = NumToAllocate < Max() ? (bAllowShrinking ? Impl->CalculateSlackShrink(Count, Max()) : Max()) : NumToAllocate;
@ -829,7 +829,7 @@ public:
return; return;
} }
if (Count <= Num()) if (Count <= Num())
{ {
Memory::Destruct(Impl.Pointer + Count, Num() - Count); Memory::Destruct(Impl.Pointer + Count, Num() - Count);
@ -847,7 +847,7 @@ public:
void SetNum(size_t Count, const ElementType& InValue, bool bAllowShrinking = true) requires (CCopyConstructible<ElementType> && CMovable<ElementType>) void SetNum(size_t Count, const ElementType& InValue, bool bAllowShrinking = true) requires (CCopyConstructible<ElementType> && CMovable<ElementType>)
{ {
size_t NumToAllocate = Count; size_t NumToAllocate = Count;
NumToAllocate = NumToAllocate > Max() ? Impl->CalculateSlackGrow(Count, Max()) : NumToAllocate; NumToAllocate = NumToAllocate > Max() ? Impl->CalculateSlackGrow(Count, Max()) : NumToAllocate;
NumToAllocate = NumToAllocate < Max() ? (bAllowShrinking ? Impl->CalculateSlackShrink(Count, Max()) : Max()) : NumToAllocate; NumToAllocate = NumToAllocate < Max() ? (bAllowShrinking ? Impl->CalculateSlackShrink(Count, Max()) : Max()) : NumToAllocate;
@ -879,7 +879,7 @@ public:
return; return;
} }
if (Count <= Num()) if (Count <= Num())
{ {
Memory::Destruct(Impl.Pointer + Count, Num() - Count); Memory::Destruct(Impl.Pointer + Count, Num() - Count);
@ -925,7 +925,7 @@ public:
if (NumToAllocate == Max()) return; if (NumToAllocate == Max()) return;
ElementType* OldAllocation = Impl.Pointer; ElementType* OldAllocation = Impl.Pointer;
Impl.ArrayMax = NumToAllocate; Impl.ArrayMax = NumToAllocate;
Impl.Pointer = Impl->Allocate(Max()); Impl.Pointer = Impl->Allocate(Max());

View File

@ -3,7 +3,6 @@
#include "CoreTypes.h" #include "CoreTypes.h"
#include "Templates/Invoke.h" #include "Templates/Invoke.h"
#include "Templates/Utility.h" #include "Templates/Utility.h"
#include "Templates/Optional.h"
#include "Templates/Noncopyable.h" #include "Templates/Noncopyable.h"
#include "TypeTraits/TypeTraits.h" #include "TypeTraits/TypeTraits.h"
#include "Miscellaneous/Compare.h" #include "Miscellaneous/Compare.h"
@ -204,13 +203,10 @@ private:
static_assert(CRandomAccessIterator<TReverseIterator<int32*>>); static_assert(CRandomAccessIterator<TReverseIterator<int32*>>);
template <typename I>
TReverseIterator(I) -> TReverseIterator<I>;
template <typename I, typename J> requires (!CSizedSentinelFor<I, J>) template <typename I, typename J> requires (!CSizedSentinelFor<I, J>)
inline constexpr bool bDisableSizedSentinelFor<TReverseIterator<I>, TReverseIterator<J>> = true; inline constexpr bool bDisableSizedSentinelFor<TReverseIterator<I>, TReverseIterator<J>> = true;
/** An iterator adaptor which dereferences to an rvalue reference. */ /** An iterator adaptor which dereferences to a rvalue reference. */
template <CInputIterator I> template <CInputIterator I>
class TMoveIterator final class TMoveIterator final
{ {
@ -281,9 +277,6 @@ private:
static_assert(CRandomAccessIterator<TMoveIterator<int32*>>); static_assert(CRandomAccessIterator<TMoveIterator<int32*>>);
template <typename I>
TMoveIterator(I) -> TMoveIterator<I>;
/** A sentinel adaptor for use with TMoveIterator. */ /** A sentinel adaptor for use with TMoveIterator. */
template <CSemiregular S> template <CSemiregular S>
class TMoveSentinel class TMoveSentinel
@ -304,7 +297,7 @@ public:
template <CSemiregular T> requires (!CSameAs<SentinelType, T> && CConstructibleFrom<SentinelType, const T&>) template <CSemiregular T> requires (!CSameAs<SentinelType, T> && CConstructibleFrom<SentinelType, const T&>)
FORCEINLINE constexpr explicit (!CConvertibleTo<const T&, SentinelType>) TMoveSentinel(const TMoveSentinel<T>& InValue) : Last(InValue.GetBase()) { } FORCEINLINE constexpr explicit (!CConvertibleTo<const T&, SentinelType>) TMoveSentinel(const TMoveSentinel<T>& InValue) : Last(InValue.GetBase()) { }
template <CSemiregular T> requires (!CSameAs<SentinelType, T> && CConstructibleFrom<SentinelType, T>) template <CSemiregular T> requires (!CSameAs<SentinelType, T> && CConstructibleFrom<SentinelType, T>)
FORCEINLINE constexpr explicit (!CConvertibleTo<T&&, SentinelType>) TMoveSentinel(TMoveSentinel<T>&& InValue) : Last(MoveTemp(InValue).GetBase()) { } FORCEINLINE constexpr explicit (!CConvertibleTo<T&&, SentinelType>) TMoveSentinel(TMoveSentinel<T>&& InValue) : Last(MoveTemp(InValue).GetBase()) { }
@ -316,7 +309,7 @@ public:
template <CInputIterator I> requires (CSentinelFor<SentinelType, I>) template <CInputIterator I> requires (CSentinelFor<SentinelType, I>)
NODISCARD FORCEINLINE constexpr bool operator==(const TMoveIterator<I>& InValue) const& { return GetBase() == InValue.GetBase(); } NODISCARD FORCEINLINE constexpr bool operator==(const TMoveIterator<I>& InValue) const& { return GetBase() == InValue.GetBase(); }
template <CInputIterator I> requires (CSizedSentinelFor<SentinelType, I>) template <CInputIterator I> requires (CSizedSentinelFor<SentinelType, I>)
NODISCARD FORCEINLINE constexpr TCompareThreeWayResult<SentinelType, I> operator<=>(const TMoveIterator<I>& InValue) const& { return GetBase() <=> InValue.GetBase(); } NODISCARD FORCEINLINE constexpr TCompareThreeWayResult<SentinelType, I> operator<=>(const TMoveIterator<I>& InValue) const& { return GetBase() <=> InValue.GetBase(); }
@ -337,9 +330,6 @@ private:
static_assert(CSizedSentinelFor<TMoveSentinel<int32*>, TMoveIterator<int32*>>); static_assert(CSizedSentinelFor<TMoveSentinel<int32*>, TMoveIterator<int32*>>);
template <typename I>
TMoveSentinel(I) -> TMoveSentinel<I>;
struct FDefaultSentinel { explicit FDefaultSentinel() = default; }; struct FDefaultSentinel { explicit FDefaultSentinel() = default; };
inline constexpr FDefaultSentinel DefaultSentinel{ }; inline constexpr FDefaultSentinel DefaultSentinel{ };
@ -456,47 +446,7 @@ private:
static_assert(CContiguousIterator<TCountedIterator<int32*>>); static_assert(CContiguousIterator<TCountedIterator<int32*>>);
static_assert(CSizedSentinelFor<FDefaultSentinel, TCountedIterator<int32*>>); static_assert(CSizedSentinelFor<FDefaultSentinel, TCountedIterator<int32*>>);
template <typename I> NAMESPACE_PRIVATE_BEGIN
TCountedIterator(I, ptrdiff) -> TCountedIterator<I>;
/** An input iterator adapter that wraps a callable object. */
template <CRegularInvocable F> requires (CTOptional<TRemoveCVRef<TInvokeResult<F>>> && CMoveConstructible<F>
&& CConstructibleFrom<TRemoveCVRef<TInvokeResult<F>>, TInvokeResult<F>> && CAssignableFrom<TRemoveCVRef<TInvokeResult<F>>&, TInvokeResult<F>>)
class TInputIterator final : private FNoncopyable
{
public:
using Inputer = F;
using ElementType = typename TRemoveCVRef<TInvokeResult<Inputer>>::ValueType;
FORCEINLINE constexpr TInputIterator() requires (CDefaultConstructible<Inputer>) : LookAhead(Invoke(Storage)) { };
template <typename T> requires (!CSameAs<TInputIterator, TRemoveCVRef<T>> && CConstructibleFrom<Inputer, T>)
FORCEINLINE constexpr explicit TInputIterator(T&& InInputer) : Storage(Forward<T>(InInputer)), LookAhead(Invoke(Storage)) { }
NODISCARD FORCEINLINE constexpr bool operator==(FDefaultSentinel) const& { return !LookAhead.IsValid(); }
NODISCARD FORCEINLINE constexpr const ElementType& operator*() const { return LookAhead.GetValue(); }
FORCEINLINE constexpr TInputIterator& operator++() { LookAhead = Invoke(Storage); return *this; }
FORCEINLINE constexpr void operator++(int) { LookAhead = Invoke(Storage); }
NODISCARD FORCEINLINE constexpr const Inputer& GetInputer() const& { return Storage; }
NODISCARD FORCEINLINE constexpr Inputer GetInputer() && { return Storage; }
private:
Inputer Storage;
TOptional<ElementType> LookAhead;
};
static_assert(CInputIterator<TInputIterator<TOptional<int32>(*)()>>);
template <typename F>
TInputIterator(F) -> TInputIterator<F>;
/** An output iterator adapter that wraps a callable object. */ /** An output iterator adapter that wraps a callable object. */
template <CMoveConstructible F> template <CMoveConstructible F>
@ -572,7 +522,7 @@ private:
public: public:
FORCEINLINE constexpr TOutputIterator() requires (CDefaultConstructible<Outputer>) { check_code({ bIsProduced = false; }); } FORCEINLINE constexpr TOutputIterator() requires (CDefaultConstructible<Outputer>) { check_code({ bIsProduced = false; }); }
template <typename T> requires (!CSameAs<TOutputIterator, TRemoveCVRef<T>> && CConstructibleFrom<Outputer, T>) template <typename T> requires (!CSameAs<TOutputIterator, TRemoveCVRef<T>> && CConstructibleFrom<Outputer, T>)
FORCEINLINE constexpr explicit TOutputIterator(T&& InOutputer) : Storage(Forward<T>(InOutputer)) { check_code({ bIsProduced = false; }); } FORCEINLINE constexpr explicit TOutputIterator(T&& InOutputer) : Storage(Forward<T>(InOutputer)) { check_code({ bIsProduced = false; }); }
@ -609,6 +559,8 @@ static_assert(COutputIterator<TOutputIterator<void(*)(int32)>, int32>);
template <typename F> template <typename F>
TOutputIterator(F) -> TOutputIterator<F>; TOutputIterator(F) -> TOutputIterator<F>;
NAMESPACE_PRIVATE_END
/** Creates a TReverseIterator of type inferred from the argument. */ /** Creates a TReverseIterator of type inferred from the argument. */
template <typename I> requires (CBidirectionalIterator<TDecay<I>> && CConstructibleFrom<TDecay<I>, I>) template <typename I> requires (CBidirectionalIterator<TDecay<I>> && CConstructibleFrom<TDecay<I>, I>)
NODISCARD FORCEINLINE constexpr auto MakeReverseIterator(I&& Iter) NODISCARD FORCEINLINE constexpr auto MakeReverseIterator(I&& Iter)
@ -641,21 +593,21 @@ NODISCARD FORCEINLINE constexpr auto MakeCountedIterator(I&& Iter, ptrdiff N)
template <typename C> template <typename C>
NODISCARD FORCEINLINE constexpr auto MakeFrontInserter(C& Container) NODISCARD FORCEINLINE constexpr auto MakeFrontInserter(C& Container)
{ {
return TOutputIterator([&Container]<typename T>(T&& A) { Container.PushFront(Forward<T>(A)); }); return NAMESPACE_PRIVATE::TOutputIterator([&Container]<typename T>(T&& A) { Container.PushFront(Forward<T>(A)); });
} }
/** Creates an iterator adapter inserted in the back of the container. */ /** Creates an iterator adapter inserted in the back of the container. */
template <typename C> template <typename C>
NODISCARD FORCEINLINE constexpr auto MakeBackInserter(C& Container) NODISCARD FORCEINLINE constexpr auto MakeBackInserter(C& Container)
{ {
return TOutputIterator([&Container]<typename T>(T&& A) { Container.PushBack(Forward<T>(A)); }); return NAMESPACE_PRIVATE::TOutputIterator([&Container]<typename T>(T&& A) { Container.PushBack(Forward<T>(A)); });
} }
/** Creates an iterator adapter inserted in the container. */ /** Creates an iterator adapter inserted in the container. */
template <typename C> template <typename C>
NODISCARD FORCEINLINE constexpr auto MakeInserter(C& Container, const typename C::ConstIterator& InIter) NODISCARD FORCEINLINE constexpr auto MakeInserter(C& Container, const typename C::ConstIterator& InIter)
{ {
return TOutputIterator([&Container, Iter = InIter]<typename T>(T&& A) mutable { Iter = Container.Insert(Iter, Forward<T>(A)); }); return NAMESPACE_PRIVATE::TOutputIterator([&Container, Iter = InIter]<typename T>(T&& A) mutable { Iter = Container.Insert(Iter, Forward<T>(A)); });
} }
NAMESPACE_BEGIN(Iteration) NAMESPACE_BEGIN(Iteration)
@ -671,7 +623,7 @@ FORCEINLINE constexpr void Advance(I& Iter, ptrdiff N)
else if constexpr (CBidirectionalIterator<I>) else if constexpr (CBidirectionalIterator<I>)
{ {
for (; N > 0; --N) ++Iter; for (; N > 0; --N) ++Iter;
for (; N < 0; ++N) --Iter; for (; N < 0; ++N) --Iter;
} }
else else
{ {