refactor(*): refactor the tool library's parent folder to plural form
This commit is contained in:
250
Redcraft.Utility/Source/Public/Iterators/BasicIterator.h
Normal file
250
Redcraft.Utility/Source/Public/Iterators/BasicIterator.h
Normal file
@ -0,0 +1,250 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Memory/Address.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "Iterators/Sentinel.h"
|
||||
#include "Miscellaneous/Compare.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
#if PLATFORM_COMPILER_GCC
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wnon-template-friend"
|
||||
#endif
|
||||
|
||||
/** A concept specifies a type is an input iterator. It is input iterator, incrementable, and sentinel for itself. */
|
||||
template <typename I>
|
||||
concept CForwardIterator = CInputIterator<I> && CIncrementable<I> && CSentinelFor<I, I>;
|
||||
|
||||
/** This is an example of a forward iterator, indicate the traits that define a forward iterator. */
|
||||
template <CReferenceable T>
|
||||
struct IForwardIterator /* : IInputIterator<T>, IIncrementable, ISentinelFor<IForwardIterator> */
|
||||
{
|
||||
// ~Begin CInputIterator.
|
||||
|
||||
using FElementType = TRemoveCVRef<T>;
|
||||
|
||||
// ~End CInputIterator.
|
||||
|
||||
// ~Begin CIncrementable and CSentinelFor<IForwardIterator>.
|
||||
|
||||
IForwardIterator();
|
||||
IForwardIterator(const IForwardIterator&);
|
||||
IForwardIterator(IForwardIterator&&); // Also satisfies IInputIterator.
|
||||
IForwardIterator* operator=(const IForwardIterator&);
|
||||
IForwardIterator* operator=(IForwardIterator&&); // Also satisfies IInputIterator.
|
||||
|
||||
friend bool operator==(const IForwardIterator&, const IForwardIterator&);
|
||||
|
||||
// ~End CIncrementable and CSentinelFor<IForwardIterator>.
|
||||
|
||||
// ~Begin CInputIterator.
|
||||
|
||||
T operator*() const; // Optional satisfies CIndirectlyWritable.
|
||||
|
||||
IForwardIterator& operator++(); // Also satisfies CIncrementable.
|
||||
|
||||
IForwardIterator operator++(int); // Also satisfies CIncrementable.
|
||||
|
||||
// ~End CInputIterator.
|
||||
};
|
||||
|
||||
// Use IForwardIterator<int> represents a forward iterator.
|
||||
static_assert(CForwardIterator<IForwardIterator<int&>>);
|
||||
static_assert( COutputIterator<IForwardIterator<int&>, int>);
|
||||
|
||||
/** A concept specifies a type is a bidirectional iterator. Add the decrement operator to the forward iterator. */
|
||||
template <typename I>
|
||||
concept CBidirectionalIterator = CForwardIterator<I>
|
||||
&& requires(I Iter) {
|
||||
{ --Iter } -> CSameAs<I&>;
|
||||
{ Iter-- } -> CSameAs<I >;
|
||||
};
|
||||
|
||||
/**
|
||||
* This is an example of a bidirectional iterator, indicate the traits that define a bidirectional iterator.
|
||||
* Regardless of the order in which the increment and decrement operators are applied,
|
||||
* the result is always the same if both operations are performed the same number of times.
|
||||
*/
|
||||
template <CReferenceable T>
|
||||
struct IBidirectionalIterator /* : IForwardIterator<T> */
|
||||
{
|
||||
// ~Begin CForwardIterator.
|
||||
|
||||
using FElementType = TRemoveCVRef<T>;
|
||||
|
||||
IBidirectionalIterator();
|
||||
IBidirectionalIterator(const IBidirectionalIterator&);
|
||||
IBidirectionalIterator(IBidirectionalIterator&&);
|
||||
IBidirectionalIterator* operator=(const IBidirectionalIterator&);
|
||||
IBidirectionalIterator* operator=(IBidirectionalIterator&&);
|
||||
|
||||
friend bool operator==(const IBidirectionalIterator&, const IBidirectionalIterator&);
|
||||
|
||||
T operator*() const;
|
||||
|
||||
// ~End CForwardIterator.
|
||||
|
||||
IBidirectionalIterator& operator++(); // Also satisfies CForwardIterator.
|
||||
IBidirectionalIterator& operator--();
|
||||
|
||||
IBidirectionalIterator operator++(int); // Also satisfies CForwardIterator.
|
||||
IBidirectionalIterator operator--(int);
|
||||
};
|
||||
|
||||
// Use IBidirectionalIterator<int> represents a bidirectional iterator.
|
||||
static_assert(CBidirectionalIterator<IBidirectionalIterator<int&>>);
|
||||
static_assert( COutputIterator<IBidirectionalIterator<int&>, int>);
|
||||
|
||||
/**
|
||||
* A concept specifies a type is a random access iterator.
|
||||
* Add the three-way comparison, addition, subtraction and subscript operators to the bidirectional iterator.
|
||||
*/
|
||||
template <typename I>
|
||||
concept CRandomAccessIterator = CBidirectionalIterator<I> && CTotallyOrdered<I> && CSizedSentinelFor<I, I>
|
||||
&& requires(I Iter, const I Jter, const ptrdiff N) {
|
||||
{ Iter += N } -> CSameAs<I&>;
|
||||
{ Jter + N } -> CSameAs<I >;
|
||||
{ N + Jter } -> CSameAs<I >;
|
||||
{ Iter -= N } -> CSameAs<I&>;
|
||||
{ Jter - N } -> CSameAs<I >;
|
||||
{ Jter[N] } -> CSameAs<TIteratorReference<I>>;
|
||||
};
|
||||
|
||||
/** This is an example of a random access iterator, indicate the traits that define a random access iterator. */
|
||||
template <CReferenceable T>
|
||||
struct IRandomAccessIterator /* : IBidirectionalIterator<T>, ISizedSentinelFor<IRandomAccessIterator> */
|
||||
{
|
||||
// ~Begin CBidirectionalIterator.
|
||||
|
||||
using FElementType = TRemoveCVRef<T>;
|
||||
|
||||
// ~End CBidirectionalIterator.
|
||||
|
||||
// ~Begin CBidirectionalIterator and CSizedSentinelFor<IRandomAccessIterator>.
|
||||
|
||||
IRandomAccessIterator();
|
||||
IRandomAccessIterator(const IRandomAccessIterator&);
|
||||
IRandomAccessIterator(IRandomAccessIterator&&);
|
||||
IRandomAccessIterator* operator=(const IRandomAccessIterator&);
|
||||
IRandomAccessIterator* operator=(IRandomAccessIterator&&);
|
||||
|
||||
friend bool operator==(const IRandomAccessIterator&, const IRandomAccessIterator&);
|
||||
|
||||
// ~End CBidirectionalIterator and CSizedSentinelFor<IRandomAccessIterator>.
|
||||
|
||||
friend strong_ordering operator<=>(const IRandomAccessIterator&, const IRandomAccessIterator&);
|
||||
|
||||
T operator*() const; // Also satisfies CBidirectionalIterator.
|
||||
|
||||
T operator[](ptrdiff) const;
|
||||
|
||||
// ~Begin CBidirectionalIterator.
|
||||
|
||||
IRandomAccessIterator& operator++();
|
||||
IRandomAccessIterator& operator--();
|
||||
|
||||
IRandomAccessIterator operator++(int);
|
||||
IRandomAccessIterator operator--(int);
|
||||
|
||||
// ~End CBidirectionalIterator.
|
||||
|
||||
IRandomAccessIterator& operator+=(ptrdiff);
|
||||
IRandomAccessIterator& operator-=(ptrdiff);
|
||||
|
||||
IRandomAccessIterator operator+(ptrdiff) const;
|
||||
IRandomAccessIterator operator-(ptrdiff) const;
|
||||
|
||||
friend IRandomAccessIterator operator+(ptrdiff, const IRandomAccessIterator&);
|
||||
|
||||
friend ptrdiff operator-(const IRandomAccessIterator&, const IRandomAccessIterator&); // Also satisfies CSizedSentinelFor<IRandomAccessIterator>.
|
||||
};
|
||||
|
||||
// Use IRandomAccessIterator<int> represents a random access iterator
|
||||
static_assert(CRandomAccessIterator<IRandomAccessIterator<int&>>);
|
||||
static_assert( COutputIterator<IRandomAccessIterator<int&>, int>);
|
||||
|
||||
/**
|
||||
* A concept specifies a type is a contiguous iterator.
|
||||
* Add the operator-> to the random access iterator and requires the operator* returns a true reference type.
|
||||
*/
|
||||
template <typename I>
|
||||
concept CContiguousIterator = CRandomAccessIterator<I> && CLValueReference<TIteratorReference<I>>
|
||||
&& CSameAs<TIteratorElement<I>, TRemoveCVRef<TIteratorReference<I>>>
|
||||
&& CSameAs<TIteratorPointer<I>, TAddPointer<TIteratorReference<I>>>
|
||||
&& requires(I& Iter)
|
||||
{
|
||||
{ ToAddress(Iter) } -> CSameAs<TAddPointer<TIteratorReference<I>>>;
|
||||
};
|
||||
|
||||
/** This is an example of a contiguous iterator, indicate the traits that define a contiguous iterator. */
|
||||
template <CLValueReference T>
|
||||
struct IContiguousIterator /* : IRandomAccessIterator<T> */
|
||||
{
|
||||
// ~Begin CRandomAccessIterator.
|
||||
|
||||
using FElementType = TRemoveCVRef<T>;
|
||||
|
||||
IContiguousIterator();
|
||||
IContiguousIterator(const IContiguousIterator&);
|
||||
IContiguousIterator(IContiguousIterator&&);
|
||||
IContiguousIterator* operator=(const IContiguousIterator&);
|
||||
IContiguousIterator* operator=(IContiguousIterator&&);
|
||||
|
||||
friend bool operator==(const IContiguousIterator&, const IContiguousIterator&);
|
||||
|
||||
friend strong_ordering operator<=>(const IContiguousIterator&, const IContiguousIterator&);
|
||||
|
||||
// ~End CRandomAccessIterator.
|
||||
|
||||
/**
|
||||
* Dereference operator. See IForwardIterator.
|
||||
* Specify, the return type must be a true reference type and refer to an element of a contiguous sequence, not a proxy class.
|
||||
* Also satisfies CRandomAccessIterator.
|
||||
*/
|
||||
T operator*() const;
|
||||
|
||||
/** Indirection operator. Return the address of the element that the iterator is pointing to. */
|
||||
TAddPointer<T> operator->() const;
|
||||
|
||||
// ~Begin CRandomAccessIterator.
|
||||
|
||||
T operator[](ptrdiff) const;
|
||||
|
||||
IContiguousIterator& operator++();
|
||||
IContiguousIterator& operator--();
|
||||
|
||||
IContiguousIterator operator++(int);
|
||||
IContiguousIterator operator--(int);
|
||||
|
||||
IContiguousIterator& operator+=(ptrdiff);
|
||||
IContiguousIterator& operator-=(ptrdiff);
|
||||
|
||||
IContiguousIterator operator+(ptrdiff) const;
|
||||
IContiguousIterator operator-(ptrdiff) const;
|
||||
|
||||
friend IContiguousIterator operator+(ptrdiff, const IContiguousIterator&);
|
||||
|
||||
friend ptrdiff operator-(const IContiguousIterator&, const IContiguousIterator&);
|
||||
|
||||
// ~End CRandomAccessIterator.
|
||||
};
|
||||
|
||||
// Use IContiguousIterator<int> represents a contiguous iterator
|
||||
static_assert(CContiguousIterator<IContiguousIterator<int&>>);
|
||||
static_assert( COutputIterator<IContiguousIterator<int&>, int>);
|
||||
|
||||
// The int* is the most typical example of a contiguous iterator
|
||||
static_assert(CContiguousIterator<int*>);
|
||||
|
||||
#if PLATFORM_COMPILER_GCC
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
145
Redcraft.Utility/Source/Public/Iterators/CountedIterator.h
Normal file
145
Redcraft.Utility/Source/Public/Iterators/CountedIterator.h
Normal file
@ -0,0 +1,145 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "Iterators/Sentinel.h"
|
||||
#include "Iterators/BasicIterator.h"
|
||||
#include "Miscellaneous/AssertionMacros.h"
|
||||
#include "Miscellaneous/Compare.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
#include "Templates/Utility.h"
|
||||
#include "Memory/Address.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
|
||||
template <typename T> class TCountedIteratorImpl { };
|
||||
template <CIndirectlyReadable T> class TCountedIteratorImpl<T> { public: using FElementType = TIteratorElement<T>; };
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
/**
|
||||
* An iterator adaptor that tracks the distance to the end of the range.
|
||||
* When based on an input or output iterator, the counted iterator satisfies at least an input or output iterator
|
||||
* up to a contiguous iterator. When based on an output iterator, the counted iterator satisfies an output iterator.
|
||||
* When based on iterator satisfies sentinel for itself, the counted iterator satisfies sized sentinel for itself.
|
||||
*/
|
||||
template <CInputOrOutputIterator I>
|
||||
class TCountedIterator final : public NAMESPACE_PRIVATE::TCountedIteratorImpl<I>
|
||||
{
|
||||
public:
|
||||
|
||||
using FIteratorType = I;
|
||||
|
||||
# if DO_CHECK
|
||||
FORCEINLINE constexpr TCountedIterator() requires (CDefaultConstructible<I>) : Length(1), MaxLength(0) { }
|
||||
# else
|
||||
FORCEINLINE constexpr TCountedIterator() requires (CDefaultConstructible<I>) = default;
|
||||
# endif
|
||||
|
||||
FORCEINLINE constexpr TCountedIterator(const TCountedIterator&) = default;
|
||||
FORCEINLINE constexpr TCountedIterator(TCountedIterator&&) = default;
|
||||
FORCEINLINE constexpr TCountedIterator& operator=(const TCountedIterator&) = default;
|
||||
FORCEINLINE constexpr TCountedIterator& operator=(TCountedIterator&&) = default;
|
||||
FORCEINLINE constexpr ~TCountedIterator() = default;
|
||||
|
||||
FORCEINLINE constexpr explicit TCountedIterator(FIteratorType InValue, ptrdiff N) : Current(MoveTemp(InValue)), Length(N) { check_code({ MaxLength = N; }); }
|
||||
|
||||
template <CInputOrOutputIterator J> requires (!CSameAs<I, J> && CConstructibleFrom<I, const J&>)
|
||||
FORCEINLINE constexpr explicit (!CConvertibleTo<const J&, I>) TCountedIterator(const TCountedIterator<J>& InValue)
|
||||
: Current(InValue.GetBase()), Length(InValue.Num())
|
||||
{
|
||||
check_code({ MaxLength = InValue.MaxLength; });
|
||||
}
|
||||
|
||||
template <CInputOrOutputIterator J> requires (!CSameAs<I, J> && CConvertibleTo<const J&, I> && CAssignableFrom<I&, const J&>)
|
||||
FORCEINLINE constexpr TCountedIterator& operator=(const TCountedIterator<J>& InValue)
|
||||
{
|
||||
Current = InValue.GetBase();
|
||||
Length = InValue.Num();
|
||||
|
||||
check_code({ MaxLength = InValue.MaxLength; });
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <CInputOrOutputIterator J> requires (CCommonType<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr bool operator==(const TCountedIterator& LHS, const TCountedIterator<J>& RHS) { return LHS.Num() == RHS.Num(); }
|
||||
|
||||
template <CInputOrOutputIterator J> requires (CCommonType<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr strong_ordering operator<=>(const TCountedIterator& LHS, const TCountedIterator<J>& RHS) { return LHS.Num() <=> RHS.Num(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr bool operator==(FDefaultSentinel) const& { return Num() == static_cast<ptrdiff>(0); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TIteratorReference<I> operator*() { CheckThis(true); return *GetBase(); }
|
||||
NODISCARD FORCEINLINE constexpr TIteratorReference<I> operator*() const requires (CDereferenceable<const I>) { CheckThis(true); return *GetBase(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr auto operator->() const requires (requires(const I Iter) { { ToAddress(Iter) } -> CSameAs<TIteratorPointer<I>>; }) { return ToAddress(GetBase()); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TIteratorReference<I> operator[](ptrdiff Index) const requires (CRandomAccessIterator<I>) { TCountedIterator Temp = *this + Index; return *Temp; }
|
||||
|
||||
FORCEINLINE constexpr TCountedIterator& operator++() { ++Current; --Length; CheckThis(); return *this; }
|
||||
FORCEINLINE constexpr TCountedIterator& operator--() requires (CBidirectionalIterator<I>) { --Current; ++Length; CheckThis(); return *this; }
|
||||
|
||||
FORCEINLINE constexpr auto operator++(int) { --Length; CheckThis(); return Current++; }
|
||||
FORCEINLINE constexpr TCountedIterator operator++(int) requires (CForwardIterator<I>) { TCountedIterator Temp = *this; ++Current; --Length; CheckThis(); return Temp; }
|
||||
FORCEINLINE constexpr TCountedIterator operator--(int) requires (CBidirectionalIterator<I>) { TCountedIterator Temp = *this; --Current; ++Length; CheckThis(); return Temp; }
|
||||
|
||||
FORCEINLINE constexpr TCountedIterator& operator+=(ptrdiff Offset) requires (CRandomAccessIterator<I>) { Current += Offset; Length -= Offset; CheckThis(); return *this; }
|
||||
FORCEINLINE constexpr TCountedIterator& operator-=(ptrdiff Offset) requires (CRandomAccessIterator<I>) { Current -= Offset; Length += Offset; CheckThis(); return *this; }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TCountedIterator operator+(ptrdiff Offset) const requires (CRandomAccessIterator<I>) { TCountedIterator Temp = *this; Temp += Offset; return Temp; }
|
||||
NODISCARD FORCEINLINE constexpr TCountedIterator operator-(ptrdiff Offset) const requires (CRandomAccessIterator<I>) { TCountedIterator Temp = *this; Temp -= Offset; return Temp; }
|
||||
|
||||
NODISCARD friend FORCEINLINE constexpr TCountedIterator operator+(ptrdiff Offset, TCountedIterator Iter) requires (CRandomAccessIterator<I>) { return Iter + Offset; }
|
||||
|
||||
template <CInputOrOutputIterator J> requires (CCommonType<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TCountedIterator& LHS, const TCountedIterator<J>& RHS) { return RHS.Num() - LHS.Num(); }
|
||||
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TCountedIterator& LHS, FDefaultSentinel) { LHS.CheckThis(); return -LHS.Num(); }
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(FDefaultSentinel, const TCountedIterator& RHS) { RHS.CheckThis(); return RHS.Num(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr const FIteratorType& GetBase() const& { CheckThis(); return Current; }
|
||||
NODISCARD FORCEINLINE constexpr FIteratorType GetBase() && { CheckThis(); return MoveTemp(Current); }
|
||||
NODISCARD FORCEINLINE constexpr ptrdiff Num() const { CheckThis(); return Length; }
|
||||
|
||||
private:
|
||||
|
||||
FIteratorType Current;
|
||||
ptrdiff Length;
|
||||
|
||||
# if DO_CHECK
|
||||
ptrdiff MaxLength;
|
||||
# endif
|
||||
|
||||
FORCEINLINE void CheckThis(bool bExceptEnd = false) const
|
||||
{
|
||||
checkf(static_cast<ptrdiff>(0) <= Length && Length <= MaxLength, TEXT("Read access violation. Please check Num()."));
|
||||
checkf(!(bExceptEnd && Length == static_cast<ptrdiff>(0)), TEXT("Read access violation. Please check Num()."));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
static_assert( CInputIterator<TCountedIterator< IInputIterator<int&>>>);
|
||||
static_assert( CForwardIterator<TCountedIterator< IForwardIterator<int&>>>);
|
||||
static_assert(CBidirectionalIterator<TCountedIterator<IBidirectionalIterator<int&>>>);
|
||||
static_assert( CRandomAccessIterator<TCountedIterator< IRandomAccessIterator<int&>>>);
|
||||
static_assert( CContiguousIterator<TCountedIterator< IContiguousIterator<int&>>>);
|
||||
|
||||
static_assert(COutputIterator<TCountedIterator<IOutputIterator<int&>>, int>);
|
||||
|
||||
static_assert(CSizedSentinelFor<TCountedIterator<IForwardIterator<int>>, TCountedIterator<IForwardIterator<int>>>);
|
||||
|
||||
/** Creates a TCountedIterator of type inferred from the argument. */
|
||||
template <typename I> requires (CInputOrOutputIterator<TDecay<I>> && CConstructibleFrom<TDecay<I>, I>)
|
||||
NODISCARD FORCEINLINE constexpr auto MakeCountedIterator(I&& Iter, ptrdiff N)
|
||||
{
|
||||
return TCountedIterator<TDecay<I>>(Forward<I>(Iter), N);
|
||||
}
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
152
Redcraft.Utility/Source/Public/Iterators/InsertIterator.h
Normal file
152
Redcraft.Utility/Source/Public/Iterators/InsertIterator.h
Normal file
@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "Templates/Utility.h"
|
||||
#include "Templates/Noncopyable.h"
|
||||
#include "Templates/Invoke.h"
|
||||
#include "Miscellaneous/AssertionMacros.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
|
||||
template <typename F> class TInsertProxy;
|
||||
template <typename F> class TPostIncrementProxy;
|
||||
template <typename F> class TInsertIterator;
|
||||
|
||||
template <typename F>
|
||||
class TInsertProxy final : private FSingleton
|
||||
{
|
||||
public:
|
||||
|
||||
# if DO_CHECK
|
||||
FORCEINLINE ~TInsertProxy() { checkf(bIsProduced, TEXT("Exception insert, Ensures that the value is assigned to the inserter.")); }
|
||||
# endif
|
||||
|
||||
template <typename T> requires (CInvocable<F, T>)
|
||||
FORCEINLINE constexpr void operator=(T&& InValue) const
|
||||
{
|
||||
checkf(!bIsProduced, TEXT("Exception insert, Ensure that no multiple values are assigned to the inserter."));
|
||||
Invoke(Iter.Storage, Forward<T>(InValue));
|
||||
check_code({ bIsProduced = true; });
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
TInsertIterator<F>& Iter;
|
||||
|
||||
# if DO_CHECK
|
||||
mutable bool bIsProduced;
|
||||
# endif
|
||||
|
||||
FORCEINLINE constexpr TInsertProxy(TInsertIterator<F>& InIter) : Iter(InIter) { check_code({ bIsProduced = false; }); }
|
||||
|
||||
template <typename> friend class TPostIncrementProxy;
|
||||
template <typename> friend class TInsertIterator;
|
||||
};
|
||||
|
||||
static_assert(CAssignableFrom<TInsertProxy<void(*)(int)>, int>);
|
||||
|
||||
template <typename F>
|
||||
class TPostIncrementProxy : private FSingleton
|
||||
{
|
||||
public:
|
||||
|
||||
# if DO_CHECK
|
||||
FORCEINLINE ~TPostIncrementProxy() { checkf(bIsProduced, TEXT("Exception insert, Ensures that the value is assigned to the inserter.")); }
|
||||
# endif
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TInsertProxy<F> operator*() const
|
||||
{
|
||||
checkf(!bIsProduced, TEXT("Exception insert, Ensure that no multiple values are assigned to the inserter."));
|
||||
check_code({ bIsProduced = true; });
|
||||
return TInsertProxy(Iter);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
TInsertIterator<F>& Iter;
|
||||
|
||||
# if DO_CHECK
|
||||
mutable bool bIsProduced;
|
||||
# endif
|
||||
|
||||
FORCEINLINE constexpr TPostIncrementProxy(TInsertIterator<F>& InIter) : Iter(InIter) { check_code({ bIsProduced = false; }); }
|
||||
|
||||
template <typename> friend class TInsertProxy;
|
||||
template <typename> friend class TInsertIterator;
|
||||
};
|
||||
|
||||
static_assert(CIndirectlyWritable<TPostIncrementProxy<void(*)(int)>, int>);
|
||||
|
||||
template <typename F>
|
||||
class TInsertIterator final : private FNoncopyable
|
||||
{
|
||||
public:
|
||||
|
||||
FORCEINLINE constexpr TInsertIterator() requires (CDefaultConstructible<F>) = default;
|
||||
|
||||
FORCEINLINE constexpr explicit TInsertIterator(F InInserter) : Storage(MoveTemp(InInserter)) { check_code({ bIsProduced = false; }); }
|
||||
|
||||
FORCEINLINE constexpr TInsertIterator(TInsertIterator&&) = default;
|
||||
FORCEINLINE constexpr TInsertIterator& operator=(TInsertIterator&&) = default;
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TInsertProxy<F> operator*()
|
||||
{
|
||||
checkf(!bIsProduced, TEXT("Exception insert, Ensure that no multiple values are assigned to the inserter."));
|
||||
check_code({ bIsProduced = true; });
|
||||
return TInsertProxy<F>(*this);
|
||||
}
|
||||
|
||||
FORCEINLINE constexpr TInsertIterator& operator++() { check_code({ bIsProduced = false; }); return *this; }
|
||||
|
||||
FORCEINLINE constexpr TPostIncrementProxy<F> operator++(int)
|
||||
{
|
||||
checkf(!bIsProduced, TEXT("Exception insert, Ensure that no multiple values are assigned to the inserter."));
|
||||
return TPostIncrementProxy<F>(*this);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
F Storage;
|
||||
|
||||
# if DO_CHECK
|
||||
bool bIsProduced;
|
||||
# endif
|
||||
|
||||
template <typename> friend class TInsertProxy;
|
||||
template <typename> friend class TPostIncrementProxy;
|
||||
};
|
||||
|
||||
static_assert(COutputIterator<TInsertIterator<void(*)(int)>, int>);
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
/** Creates an iterator adapter inserted in the front of the container. */
|
||||
template <typename C>
|
||||
NODISCARD FORCEINLINE constexpr auto MakeFrontInserter(C& Container)
|
||||
{
|
||||
return NAMESPACE_PRIVATE::TInsertIterator([&Container]<typename T>(T&& A) { Container.PushFront(Forward<T>(A)); });
|
||||
}
|
||||
|
||||
/** Creates an iterator adapter inserted in the back of the container. */
|
||||
template <typename C>
|
||||
NODISCARD FORCEINLINE constexpr auto MakeBackInserter(C& Container)
|
||||
{
|
||||
return NAMESPACE_PRIVATE::TInsertIterator([&Container]<typename T>(T&& A) { Container.PushBack(Forward<T>(A)); });
|
||||
}
|
||||
|
||||
/** Creates an iterator adapter inserted in the container. */
|
||||
template <typename C>
|
||||
NODISCARD FORCEINLINE constexpr auto MakeInserter(C& Container, const typename C::FConstIterator& InIter)
|
||||
{
|
||||
return NAMESPACE_PRIVATE::TInsertIterator([&Container, Iter = InIter]<typename T>(T&& A) mutable { Iter = Container.Insert(Iter, Forward<T>(A)); ++Iter; });
|
||||
}
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
10
Redcraft.Utility/Source/Public/Iterators/Iterators.h
Normal file
10
Redcraft.Utility/Source/Public/Iterators/Iterators.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "Iterators/Sentinel.h"
|
||||
#include "Iterators/BasicIterator.h"
|
||||
#include "Iterators/ReverseIterator.h"
|
||||
#include "Iterators/MoveIterator.h"
|
||||
#include "Iterators/CountedIterator.h"
|
||||
#include "Iterators/InsertIterator.h"
|
154
Redcraft.Utility/Source/Public/Iterators/MoveIterator.h
Normal file
154
Redcraft.Utility/Source/Public/Iterators/MoveIterator.h
Normal file
@ -0,0 +1,154 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "Iterators/Sentinel.h"
|
||||
#include "Iterators/BasicIterator.h"
|
||||
#include "Miscellaneous/Compare.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
#include "Templates/Utility.h"
|
||||
#include "Memory/Address.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
/**
|
||||
* An iterator adaptor which dereferences to a rvalue reference.
|
||||
* When based on at least an input iterator, the move iterator satisfies at least an input iterator
|
||||
* up to a random access iterator.
|
||||
*/
|
||||
template <CInputIterator I>
|
||||
class TMoveIterator final
|
||||
{
|
||||
public:
|
||||
|
||||
using FIteratorType = I;
|
||||
|
||||
using FElementType = TIteratorElement<I>;
|
||||
|
||||
FORCEINLINE constexpr TMoveIterator() requires (CDefaultConstructible<I>) = default;
|
||||
|
||||
FORCEINLINE constexpr TMoveIterator(const TMoveIterator&) = default;
|
||||
FORCEINLINE constexpr TMoveIterator(TMoveIterator&&) = default;
|
||||
FORCEINLINE constexpr TMoveIterator& operator=(const TMoveIterator&) = default;
|
||||
FORCEINLINE constexpr TMoveIterator& operator=(TMoveIterator&&) = default;
|
||||
FORCEINLINE constexpr ~TMoveIterator() = default;
|
||||
|
||||
FORCEINLINE constexpr explicit TMoveIterator(FIteratorType InValue) : Current(MoveTemp(InValue)) { }
|
||||
|
||||
template <CInputIterator J> requires (!CSameAs<I, J> && CConstructibleFrom<I, const J&>)
|
||||
FORCEINLINE constexpr explicit (!CConvertibleTo<const J&, I>) TMoveIterator(const TMoveIterator<J>& InValue) : Current(InValue.GetBase()) { }
|
||||
|
||||
template <CInputIterator J> requires (!CSameAs<I, J> && CConvertibleTo<const J&, I> && CAssignableFrom<I&, const J&>)
|
||||
FORCEINLINE constexpr TMoveIterator& operator=(const TMoveIterator<J>& InValue) { Current = InValue.GetBase(); return *this; }
|
||||
|
||||
template <CInputIterator J> requires (CEqualityComparable<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr bool operator==(const TMoveIterator& LHS, const TMoveIterator<J>& RHS) { return LHS.GetBase() == RHS.GetBase(); }
|
||||
|
||||
template <CInputIterator J> requires (CThreeWayComparable<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<I, J> operator<=>(const TMoveIterator& LHS, const TMoveIterator<J>& RHS) { return LHS.GetBase() <=> RHS.GetBase(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TIteratorRValueReference<I> operator*() const { return MoveTemp(*GetBase()); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TIteratorRValueReference<I> operator[](ptrdiff Index) const requires (CRandomAccessIterator<I>) { return MoveTemp(GetBase()[Index]); }
|
||||
|
||||
FORCEINLINE constexpr TMoveIterator& operator++() { ++Current; return *this; }
|
||||
FORCEINLINE constexpr TMoveIterator& operator--() requires (CBidirectionalIterator<I>) { --Current; return *this; }
|
||||
|
||||
FORCEINLINE constexpr void operator++(int) { Current++; }
|
||||
FORCEINLINE constexpr TMoveIterator operator++(int) requires (CForwardIterator<I>) { return TMoveIterator(Current++); }
|
||||
FORCEINLINE constexpr TMoveIterator operator--(int) requires (CBidirectionalIterator<I>) { return TMoveIterator(Current--); }
|
||||
|
||||
FORCEINLINE constexpr TMoveIterator& operator+=(ptrdiff Offset) requires (CRandomAccessIterator<I>) { Current += Offset; return *this; }
|
||||
FORCEINLINE constexpr TMoveIterator& operator-=(ptrdiff Offset) requires (CRandomAccessIterator<I>) { Current -= Offset; return *this; }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TMoveIterator operator+(ptrdiff Offset) const requires (CRandomAccessIterator<I>) { TMoveIterator Temp = *this; Temp += Offset; return Temp; }
|
||||
NODISCARD FORCEINLINE constexpr TMoveIterator operator-(ptrdiff Offset) const requires (CRandomAccessIterator<I>) { TMoveIterator Temp = *this; Temp -= Offset; return Temp; }
|
||||
|
||||
NODISCARD friend FORCEINLINE constexpr TMoveIterator operator+(ptrdiff Offset, const TMoveIterator& Iter) requires (CRandomAccessIterator<I>) { return Iter + Offset; }
|
||||
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TMoveIterator& LHS, const TMoveIterator& RHS) requires (CSizedSentinelFor<I, I>) { return LHS.GetBase() - RHS.GetBase(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr const FIteratorType& GetBase() const& { return Current; }
|
||||
NODISCARD FORCEINLINE constexpr FIteratorType GetBase() && { return MoveTemp(Current); }
|
||||
|
||||
private:
|
||||
|
||||
FIteratorType Current;
|
||||
|
||||
};
|
||||
|
||||
template <typename I, typename J> requires (!CSizedSentinelFor<I, J>)
|
||||
inline constexpr bool bDisableSizedSentinelFor<TMoveIterator<I>, TMoveIterator<J>> = true;
|
||||
|
||||
static_assert( CInputIterator<TMoveIterator< IInputIterator<int&>>>);
|
||||
static_assert( CForwardIterator<TMoveIterator< IForwardIterator<int&>>>);
|
||||
static_assert(CBidirectionalIterator<TMoveIterator<IBidirectionalIterator<int&>>>);
|
||||
static_assert( CRandomAccessIterator<TMoveIterator< IRandomAccessIterator<int&>>>);
|
||||
static_assert( CRandomAccessIterator<TMoveIterator< IContiguousIterator<int&>>>);
|
||||
|
||||
/**
|
||||
* A sentinel adaptor for use with TMoveIterator.
|
||||
* Whether based on un-sized or sized sentinel, the move sentinel satisfies the corresponding concept.
|
||||
*/
|
||||
template <CSemiregular S>
|
||||
class TMoveSentinel
|
||||
{
|
||||
public:
|
||||
|
||||
using FSentinelType = S;
|
||||
|
||||
FORCEINLINE constexpr TMoveSentinel() = default;
|
||||
FORCEINLINE constexpr TMoveSentinel(const TMoveSentinel&) = default;
|
||||
FORCEINLINE constexpr TMoveSentinel(TMoveSentinel&&) = default;
|
||||
FORCEINLINE constexpr TMoveSentinel& operator=(const TMoveSentinel&) = default;
|
||||
FORCEINLINE constexpr TMoveSentinel& operator=(TMoveSentinel&&) = default;
|
||||
FORCEINLINE constexpr ~TMoveSentinel() = default;
|
||||
|
||||
FORCEINLINE constexpr explicit TMoveSentinel(FSentinelType InValue) : Last(InValue) { }
|
||||
|
||||
template <CSemiregular T> requires (!CSameAs<S, T> && CConstructibleFrom<S, const T&>)
|
||||
FORCEINLINE constexpr explicit (!CConvertibleTo<const T&, S>) TMoveSentinel(const TMoveSentinel<T>& InValue) : Last(InValue.Last) { }
|
||||
|
||||
template <CSemiregular T> requires (!CSameAs<S, T> && CConvertibleTo<const T&, S> && CAssignableFrom<S&, const T&>)
|
||||
FORCEINLINE constexpr TMoveSentinel& operator=(const TMoveSentinel<T>& InValue) { Last = InValue.GetBase(); return *this; }
|
||||
|
||||
template <CInputIterator I> requires (CSentinelFor<S, I>)
|
||||
NODISCARD FORCEINLINE constexpr bool operator==(const TMoveIterator<I>& InValue) const& { return GetBase() == InValue.GetBase(); }
|
||||
|
||||
template <CInputIterator I> requires (CSizedSentinelFor<S, I>)
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TMoveSentinel& Sentinel, const TMoveIterator<I>& Iter) { return Sentinel.GetBase() - Iter.GetBase(); }
|
||||
|
||||
template <CInputIterator I> requires (CSizedSentinelFor<S, I>)
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TMoveIterator<I>& Iter, const TMoveSentinel& Sentinel) { return Iter.GetBase() - Sentinel.GetBase(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr const FSentinelType& GetBase() const& { return Last; }
|
||||
NODISCARD FORCEINLINE constexpr FSentinelType GetBase() && { return MoveTemp(Last); }
|
||||
|
||||
private:
|
||||
|
||||
FSentinelType Last;
|
||||
|
||||
};
|
||||
|
||||
static_assert( CSentinelFor<TMoveSentinel< ISentinelFor<IInputIterator<int>>>, TMoveIterator<IInputIterator<int>>>);
|
||||
static_assert(CSizedSentinelFor<TMoveSentinel<ISizedSentinelFor<IInputIterator<int>>>, TMoveIterator<IInputIterator<int>>>);
|
||||
|
||||
/** Creates a TMoveIterator of type inferred from the argument. */
|
||||
template <typename I> requires (CInputIterator<TDecay<I>> && CConstructibleFrom<TDecay<I>, I>)
|
||||
NODISCARD FORCEINLINE constexpr auto MakeMoveIterator(I&& Iter)
|
||||
{
|
||||
return TMoveIterator<TDecay<I>>(Forward<I>(Iter));
|
||||
}
|
||||
|
||||
/** Creates a TMoveSentinel of type inferred from the argument. */
|
||||
template <typename I> requires (CSemiregular<TDecay<I>> && CConstructibleFrom<TDecay<I>, I>)
|
||||
NODISCARD FORCEINLINE constexpr auto MakeMoveSentinel(I&& Iter)
|
||||
{
|
||||
return TMoveSentinel<TDecay<I>>(Forward<I>(Iter));
|
||||
}
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
100
Redcraft.Utility/Source/Public/Iterators/ReverseIterator.h
Normal file
100
Redcraft.Utility/Source/Public/Iterators/ReverseIterator.h
Normal file
@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "Iterators/Sentinel.h"
|
||||
#include "Iterators/BasicIterator.h"
|
||||
#include "Miscellaneous/Compare.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
#include "Templates/Utility.h"
|
||||
#include "Memory/Address.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
/**
|
||||
* An iterator adaptor for reverse-order traversal.
|
||||
* When based on at least a bidirectional iterator, the reverse iterator satisfies at least a bidirectional iterator
|
||||
* up to a random access iterator. When based on an output iterator, the reverse iterator satisfies an output iterator.
|
||||
*/
|
||||
template <CBidirectionalIterator I>
|
||||
class TReverseIterator final
|
||||
{
|
||||
public:
|
||||
|
||||
using FIteratorType = I;
|
||||
|
||||
using FElementType = TIteratorElement<I>;
|
||||
|
||||
FORCEINLINE constexpr TReverseIterator() = default;
|
||||
FORCEINLINE constexpr TReverseIterator(const TReverseIterator&) = default;
|
||||
FORCEINLINE constexpr TReverseIterator(TReverseIterator&&) = default;
|
||||
FORCEINLINE constexpr TReverseIterator& operator=(const TReverseIterator&) = default;
|
||||
FORCEINLINE constexpr TReverseIterator& operator=(TReverseIterator&&) = default;
|
||||
FORCEINLINE constexpr ~TReverseIterator() = default;
|
||||
|
||||
FORCEINLINE constexpr explicit TReverseIterator(FIteratorType InValue) : Current(InValue) { }
|
||||
|
||||
template <CBidirectionalIterator J> requires (!CSameAs<I, J> && CConstructibleFrom<I, const J&>)
|
||||
FORCEINLINE constexpr explicit (!CConvertibleTo<const J&, I>) TReverseIterator(const TReverseIterator<J>& InValue) : Current(InValue.GetBase()) { }
|
||||
|
||||
template <CBidirectionalIterator J> requires (!CSameAs<I, J> && CConvertibleTo<const J&, I> && CAssignableFrom<I&, const J&>)
|
||||
FORCEINLINE constexpr TReverseIterator& operator=(const TReverseIterator<J>& InValue) { Current = InValue.GetBase(); return *this; }
|
||||
|
||||
template <CBidirectionalIterator J> requires (CEqualityComparable<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr bool operator==(const TReverseIterator& LHS, const TReverseIterator<J>& RHS) { return LHS.GetBase() == RHS.GetBase(); }
|
||||
|
||||
template <CBidirectionalIterator J> requires (CThreeWayComparable<I, J>)
|
||||
NODISCARD friend FORCEINLINE constexpr TCompareThreeWayResult<I, J> operator<=>(const TReverseIterator& LHS, const TReverseIterator<J>& RHS) { return RHS.GetBase() <=> LHS.GetBase(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TIteratorReference<I> operator*() const { FIteratorType Temp = GetBase(); return *--Temp; }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr auto operator->() const requires (requires(const I Iter) { { ToAddress(Iter) } -> CSameAs<TIteratorPointer<I>>; }) { FIteratorType Temp = GetBase(); return ToAddress(--Temp); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TIteratorReference<I> operator[](ptrdiff Index) const requires (CRandomAccessIterator<I>) { return GetBase()[-Index - 1]; }
|
||||
|
||||
FORCEINLINE constexpr TReverseIterator& operator++() { --Current; return *this; }
|
||||
FORCEINLINE constexpr TReverseIterator& operator--() { ++Current; return *this; }
|
||||
|
||||
FORCEINLINE constexpr TReverseIterator operator++(int) { TReverseIterator Temp = *this; ++*this; return Temp; }
|
||||
FORCEINLINE constexpr TReverseIterator operator--(int) { TReverseIterator Temp = *this; --*this; return Temp; }
|
||||
|
||||
FORCEINLINE constexpr TReverseIterator& operator+=(ptrdiff Offset) requires (CRandomAccessIterator<I>) { Current -= Offset; return *this; }
|
||||
FORCEINLINE constexpr TReverseIterator& operator-=(ptrdiff Offset) requires (CRandomAccessIterator<I>) { Current += Offset; return *this; }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr TReverseIterator operator+(ptrdiff Offset) const requires (CRandomAccessIterator<I>) { TReverseIterator Temp = *this; Temp += Offset; return Temp; }
|
||||
NODISCARD FORCEINLINE constexpr TReverseIterator operator-(ptrdiff Offset) const requires (CRandomAccessIterator<I>) { TReverseIterator Temp = *this; Temp -= Offset; return Temp; }
|
||||
|
||||
NODISCARD friend FORCEINLINE constexpr TReverseIterator operator+(ptrdiff Offset, const TReverseIterator& Iter) requires (CRandomAccessIterator<I>) { return Iter + Offset; }
|
||||
|
||||
NODISCARD friend FORCEINLINE constexpr ptrdiff operator-(const TReverseIterator& LHS, const TReverseIterator& RHS) requires (CSizedSentinelFor<I, I>) { return RHS.GetBase() - LHS.GetBase(); }
|
||||
|
||||
NODISCARD FORCEINLINE constexpr const FIteratorType& GetBase() const& { return Current; }
|
||||
NODISCARD FORCEINLINE constexpr FIteratorType GetBase() && { return MoveTemp(Current); }
|
||||
|
||||
private:
|
||||
|
||||
FIteratorType Current;
|
||||
|
||||
};
|
||||
|
||||
template <typename I, typename J> requires (!CSizedSentinelFor<I, J>)
|
||||
inline constexpr bool bDisableSizedSentinelFor<TReverseIterator<I>, TReverseIterator<J>> = true;
|
||||
|
||||
static_assert(CBidirectionalIterator<TReverseIterator<IBidirectionalIterator<int&>>>);
|
||||
static_assert( CRandomAccessIterator<TReverseIterator< IRandomAccessIterator<int&>>>);
|
||||
static_assert( CRandomAccessIterator<TReverseIterator< IContiguousIterator<int&>>>);
|
||||
|
||||
static_assert(COutputIterator<TReverseIterator<IBidirectionalIterator<int&>>, int>);
|
||||
|
||||
/** Creates a TReverseIterator of type inferred from the argument. */
|
||||
template <typename I> requires (CBidirectionalIterator<TDecay<I>> && CConstructibleFrom<TDecay<I>, I>)
|
||||
NODISCARD FORCEINLINE constexpr auto MakeReverseIterator(I&& Iter)
|
||||
{
|
||||
return TReverseIterator<TDecay<I>>(Forward<I>(Iter));
|
||||
}
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
115
Redcraft.Utility/Source/Public/Iterators/Sentinel.h
Normal file
115
Redcraft.Utility/Source/Public/Iterators/Sentinel.h
Normal file
@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Iterators/Utility.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
#if PLATFORM_COMPILER_GCC
|
||||
# pragma GCC diagnostic push
|
||||
# pragma GCC diagnostic ignored "-Wnon-template-friend"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* A concept specifies a type is a sentinel for an iterator and expression 'Iter == Sentinel' is valid.
|
||||
* In addition, the type must be default constructible and copyable.
|
||||
*/
|
||||
template <typename S, typename I>
|
||||
concept CSentinelFor = CSemiregular<S> && CInputOrOutputIterator<I> && CWeaklyEqualityComparable<S, I>;
|
||||
|
||||
/** This is an example of a sentinel for an iterator, indicate the traits that define a sentinel for an iterator. */
|
||||
template <CInputOrOutputIterator I>
|
||||
struct ISentinelFor
|
||||
{
|
||||
ISentinelFor();
|
||||
ISentinelFor(const ISentinelFor&);
|
||||
ISentinelFor* operator=(const ISentinelFor&);
|
||||
|
||||
bool operator==(const I&) const&;
|
||||
};
|
||||
|
||||
// Use ISentinelFor represents a sentinel for an iterator.
|
||||
static_assert(CSentinelFor<ISentinelFor<IInputOrOutputIterator<int>>, IInputOrOutputIterator<int>>);
|
||||
|
||||
// The CSentinelFor requires this code is valid.
|
||||
static_assert(
|
||||
requires(ISentinelFor<IInputOrOutputIterator<int>> Sentinel, IInputOrOutputIterator<int> Iter)
|
||||
{
|
||||
{ Iter == Sentinel } -> CBooleanTestable;
|
||||
{ Sentinel == Iter } -> CBooleanTestable;
|
||||
}
|
||||
);
|
||||
|
||||
/** Disable the CSizedSentinelFor concept for specific types. */
|
||||
template <typename S, typename I>
|
||||
inline constexpr bool bDisableSizedSentinelFor = false;
|
||||
|
||||
/**
|
||||
* A concept specifies a type is a sized sentinel for an iterator and expressions 'Sentinel - Iter' and 'Iter - Sentinel' are valid,
|
||||
* and the 'Sentinel - Iter' is equal to negative 'Iter - Sentinel'.
|
||||
* In addition, the type must be default constructible and copyable.
|
||||
*/
|
||||
template <typename S, typename I>
|
||||
concept CSizedSentinelFor = CSentinelFor<S, I>
|
||||
&& !bDisableSizedSentinelFor<TRemoveCVRef<S>, TRemoveCVRef<I>>
|
||||
&& requires(const I& Iter, const S& Sentinel)
|
||||
{
|
||||
{ Sentinel - Iter } -> CSameAs<ptrdiff>;
|
||||
{ Iter - Sentinel } -> CSameAs<ptrdiff>;
|
||||
};
|
||||
|
||||
/** This is an example of a sized sentinel for an iterator, indicate the traits that define a sized sentinel for an iterator. */
|
||||
template <CInputOrOutputIterator I>
|
||||
struct ISizedSentinelFor /* : ISentinelFor<I> */
|
||||
{
|
||||
ISizedSentinelFor(); // Also satisfies ISentinelFor<I>.
|
||||
ISizedSentinelFor(const ISizedSentinelFor&);
|
||||
ISizedSentinelFor(ISizedSentinelFor&&); // Also satisfies ISentinelFor<I>.
|
||||
ISizedSentinelFor& operator=(const ISizedSentinelFor&);
|
||||
ISizedSentinelFor& operator=(ISizedSentinelFor&&); // Also satisfies ISentinelFor<I>.
|
||||
|
||||
bool operator==(const I&) const&; // Also satisfies ISentinelFor<I>.
|
||||
|
||||
/** Subtraction operator. The 'Sentinel - Iter' is equal to negative 'Iter - Sentinel'. */
|
||||
friend ptrdiff operator-(const I&, const ISizedSentinelFor&);
|
||||
friend ptrdiff operator-(const ISizedSentinelFor&, const I&);
|
||||
};
|
||||
|
||||
// Use ISizedSentinelFor represents a sized sentinel for an iterator.
|
||||
static_assert(CSizedSentinelFor<ISizedSentinelFor<IInputOrOutputIterator<int>>, IInputOrOutputIterator<int>>);
|
||||
|
||||
// The CSentinelFor requires this code is valid.
|
||||
static_assert(
|
||||
requires(ISizedSentinelFor<IInputOrOutputIterator<int>> Sentinel, IInputOrOutputIterator<int> Iter)
|
||||
{
|
||||
{ Iter == Sentinel } -> CBooleanTestable;
|
||||
{ Sentinel == Iter } -> CBooleanTestable;
|
||||
{ Iter - Sentinel } -> CSameAs<ptrdiff>;
|
||||
{ Sentinel - Iter } -> CSameAs<ptrdiff>;
|
||||
}
|
||||
);
|
||||
|
||||
struct FDefaultSentinel { explicit FDefaultSentinel() = default; };
|
||||
|
||||
inline constexpr FDefaultSentinel DefaultSentinel{ };
|
||||
|
||||
struct FUnreachableSentinel
|
||||
{
|
||||
explicit FUnreachableSentinel() = default;
|
||||
|
||||
template <CWeaklyIncrementable I>
|
||||
NODISCARD FORCEINLINE constexpr bool operator==(const I&) const& { return false; }
|
||||
};
|
||||
|
||||
inline constexpr FUnreachableSentinel UnreachableSentinel{ };
|
||||
|
||||
#if PLATFORM_COMPILER_GCC
|
||||
# pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
315
Redcraft.Utility/Source/Public/Iterators/Utility.h
Normal file
315
Redcraft.Utility/Source/Public/Iterators/Utility.h
Normal file
@ -0,0 +1,315 @@
|
||||
#pragma once
|
||||
|
||||
#include "CoreTypes.h"
|
||||
#include "Templates/Utility.h"
|
||||
#include "TypeTraits/TypeTraits.h"
|
||||
|
||||
NAMESPACE_REDCRAFT_BEGIN
|
||||
NAMESPACE_MODULE_BEGIN(Redcraft)
|
||||
NAMESPACE_MODULE_BEGIN(Utility)
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
|
||||
template <typename T> using TWithReference = T&;
|
||||
|
||||
template <typename I> struct TIteratorElementImpl { };
|
||||
template <typename T> struct TIteratorElementImpl<T*> { using FType = TRemoveCV<T>; };
|
||||
|
||||
template <typename I> requires (requires { typename I::FElementType; })
|
||||
struct TIteratorElementImpl<I> { using FType = typename I::FElementType; };
|
||||
|
||||
template <typename I> struct TIteratorPointerImpl { };
|
||||
template <typename T> struct TIteratorPointerImpl<T*> { using FType = T*; };
|
||||
|
||||
template <typename I> requires (requires(I& Iter) { { Iter.operator->() } -> CPointer; })
|
||||
struct TIteratorPointerImpl<I> { using FType = decltype(DeclVal<I&>().operator->()); };
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
template <typename T>
|
||||
concept CReferenceable = requires { typename NAMESPACE_PRIVATE::TWithReference<T>; };
|
||||
|
||||
template <typename T>
|
||||
concept CDereferenceable = requires(T& A) { { *A } -> CReferenceable; };
|
||||
|
||||
template <typename I>
|
||||
using TIteratorElement = typename NAMESPACE_PRIVATE::TIteratorElementImpl<TRemoveCVRef<I>>::FType;
|
||||
|
||||
template <typename I>
|
||||
using TIteratorPointer = typename NAMESPACE_PRIVATE::TIteratorPointerImpl<TRemoveCVRef<I>>::FType;
|
||||
|
||||
template <CReferenceable I>
|
||||
using TIteratorReference = decltype(*DeclVal<I&>());
|
||||
|
||||
template <CReferenceable I> requires (requires(I& Iter) { { MoveTemp(*Iter) } -> CReferenceable; })
|
||||
using TIteratorRValueReference = decltype(MoveTemp(*DeclVal<I&>()));
|
||||
|
||||
NAMESPACE_PRIVATE_BEGIN
|
||||
|
||||
template <typename I>
|
||||
concept CIndirectlyReadable =
|
||||
requires(const I Iter)
|
||||
{
|
||||
typename TIteratorElement<I>;
|
||||
typename TIteratorReference<I>;
|
||||
typename TIteratorRValueReference<I>;
|
||||
{ *Iter } -> CSameAs<TIteratorReference<I>>;
|
||||
{ MoveTemp(*Iter) } -> CSameAs<TIteratorRValueReference<I>>;
|
||||
}
|
||||
&& CSameAs<TIteratorElement<I>, TRemoveCVRef<TIteratorElement<I>>>
|
||||
&& CCommonReference<TIteratorReference<I>&&, TIteratorElement<I>&>
|
||||
&& CCommonReference<TIteratorReference<I>&&, TIteratorRValueReference<I>&&>
|
||||
&& CCommonReference<TIteratorRValueReference<I>&&, const TIteratorElement<I>&>;
|
||||
|
||||
NAMESPACE_PRIVATE_END
|
||||
|
||||
/** A concept specifies a type is indirectly readable by expression '*Iter'. */
|
||||
template <typename I>
|
||||
concept CIndirectlyReadable = NAMESPACE_PRIVATE::CIndirectlyReadable<TRemoveCVRef<I>>;
|
||||
|
||||
/** This is an example of an indirectly readable type, indicate the traits that define an indirectly readable type. */
|
||||
template <CReferenceable T>
|
||||
struct IIndirectlyReadable
|
||||
{
|
||||
/**
|
||||
* The element type of the indirectly readable type.
|
||||
* It must be a non-const, non-volatile and non-reference type and can be referenced, i.e. not a void type.
|
||||
*/
|
||||
using FElementType = TRemoveCVRef<T>;
|
||||
|
||||
/**
|
||||
* Indirectly read the element from the indirectly readable type.
|
||||
* The return type may not be const FElementType&, this concept only requires that the return type
|
||||
* and FElementType has some relationship, such as copy constructible to FElementType if the type is copyable.
|
||||
* This means that returning a proxy class castable to FElementType is also valid.
|
||||
* If this is an iterator adaptor, use decltype(auto) to forward the return value.
|
||||
*/
|
||||
T operator*() const;
|
||||
};
|
||||
|
||||
// Use IIndirectlyReadable<int> represents an indirectly readable type and int is the regular element type.
|
||||
static_assert(CIndirectlyReadable<IIndirectlyReadable<int>> && CRegular<int>);
|
||||
|
||||
// The CIndirectlyReadable requires this code is valid.
|
||||
static_assert(
|
||||
requires(IIndirectlyReadable<int> Iter, int& A)
|
||||
{
|
||||
A = *Iter;
|
||||
}
|
||||
);
|
||||
|
||||
/** A concept specifies a type is indirectly writable by expression '*Iter = A'. */
|
||||
template <typename I, typename T>
|
||||
concept CIndirectlyWritable =
|
||||
requires(I&& Iter, T&& A)
|
||||
{
|
||||
*Iter = Forward<T>(A);
|
||||
*Forward<I>(Iter) = Forward<T>(A);
|
||||
const_cast<const TIteratorReference<I>&&>(*Iter) = Forward<T>(A);
|
||||
const_cast<const TIteratorReference<I>&&>(*Forward<I>(Iter)) = Forward<T>(A);
|
||||
};
|
||||
|
||||
/** This is an example of an indirectly writable type, indicate the traits that define an indirectly writable type. */
|
||||
template <CReferenceable T>
|
||||
struct IIndirectlyWritable
|
||||
{
|
||||
/**
|
||||
* Indirectly write the element from the indirectly writable type.
|
||||
* The return type may not be T&, this concept only requires that the return type and T has some relationship,
|
||||
* such as can be assigned from T& if the type is copyable or T&& if the type is movable.
|
||||
* This means that returning a proxy class can be assigned from T is also valid.
|
||||
* If this is also an indirectly readable type, the equivalent value is read after writing.
|
||||
* If this is an iterator adaptor, use decltype(auto) to forward the return value.
|
||||
*/
|
||||
T operator*() const;
|
||||
};
|
||||
|
||||
// Use IIndirectlyWritable<int> represents an indirectly writable type and int is the regular element type.
|
||||
static_assert(CIndirectlyWritable<IIndirectlyWritable<int&>, int> && CRegular<int>);
|
||||
|
||||
// The CIndirectlyWritable requires this code is valid.
|
||||
static_assert(
|
||||
requires(IIndirectlyWritable<int&> Iter, int& A)
|
||||
{
|
||||
*Iter = A;
|
||||
}
|
||||
);
|
||||
|
||||
/** A concept specifies a type is incrementable by expression '++Iter' and the type must be movable. */
|
||||
template <typename I>
|
||||
concept CWeaklyIncrementable = CMovable<I>
|
||||
&& requires(I Iter) { { ++Iter } -> CSameAs<I&>; Iter++; };
|
||||
|
||||
/** This is an example of a weakly incrementable type, indicate the traits that define a weakly incrementable type. */
|
||||
struct IWeaklyIncrementable
|
||||
{
|
||||
IWeaklyIncrementable(IWeaklyIncrementable&&);
|
||||
IWeaklyIncrementable* operator=(IWeaklyIncrementable&&);
|
||||
|
||||
IWeaklyIncrementable& operator++();
|
||||
|
||||
/** Post-increment operator. Specify, the concept not requires the return type is any specific type, so the return type can be void. */
|
||||
void operator++(int);
|
||||
};
|
||||
|
||||
// Use IWeaklyIncrementable represents a weakly incrementable type.
|
||||
static_assert(CWeaklyIncrementable<IWeaklyIncrementable>);
|
||||
|
||||
/**
|
||||
* A concept specifies a type is incrementable by expression 'Iter++' and the expression returns the original value.
|
||||
* In addition, the type must be default constructible, copyable and weakly equality comparable.
|
||||
*/
|
||||
template <typename I>
|
||||
concept CIncrementable = CRegular<I> && CWeaklyIncrementable<I>
|
||||
&& requires(I Iter) { { Iter++ } -> CSameAs<I>; };
|
||||
|
||||
/**
|
||||
* This is an example of an incrementable type, indicate the traits that define an incrementable type.
|
||||
* The copy object of this type produced by copy constructor, copy assignment or post-increment
|
||||
* should produce the same effect as the original object when incrementing.
|
||||
*/
|
||||
struct IIncrementable /* : IWeaklyIncrementable */
|
||||
{
|
||||
IIncrementable();
|
||||
IIncrementable(const IIncrementable&);
|
||||
IIncrementable(IIncrementable&&); // Also satisfies IWeaklyIncrementable.
|
||||
IIncrementable* operator=(const IIncrementable&);
|
||||
IIncrementable* operator=(IIncrementable&&); // Also satisfies IWeaklyIncrementable.
|
||||
|
||||
friend bool operator==(const IIncrementable&, const IIncrementable&);
|
||||
|
||||
IIncrementable& operator++(); // Also satisfies IWeaklyIncrementable.
|
||||
|
||||
/** Post-increment operator. Specify, the concept requires the return value is the original value before incrementing. */
|
||||
IIncrementable operator++(int);
|
||||
};
|
||||
|
||||
// Use IIncrementable represents an incrementable type.
|
||||
static_assert(CIncrementable<IIncrementable>);
|
||||
|
||||
/**
|
||||
* A concept specifies a type is potentially an iterator. It only requires weakly incrementable and dereferenceable.
|
||||
* This concept should only be used in scenarios where the specific type of the iterator is not important, such as iterator adapters.
|
||||
*/
|
||||
template <typename I>
|
||||
concept CInputOrOutputIterator = CWeaklyIncrementable<I>
|
||||
&& requires(I Iter) { { *Iter } -> CReferenceable; };
|
||||
|
||||
/** This is an example of an input or output iterator, indicate the traits that define an input or output iterator. */
|
||||
template <CReferenceable T>
|
||||
struct IInputOrOutputIterator /* : IWeaklyIncrementable */
|
||||
{
|
||||
// ~Begin CWeklyIncrementable.
|
||||
|
||||
IInputOrOutputIterator(IInputOrOutputIterator&&);
|
||||
IInputOrOutputIterator* operator=(IInputOrOutputIterator&&);
|
||||
|
||||
IInputOrOutputIterator& operator++();
|
||||
|
||||
void operator++(int);
|
||||
|
||||
// ~End CWeklyIncrementable.
|
||||
|
||||
/** Dereference operator. It does not matter what the return type is, as long as it is referenceable. */
|
||||
T operator*() const;
|
||||
};
|
||||
|
||||
// Use IInputOrOutputIterator represents an input or output iterator.
|
||||
static_assert(CInputOrOutputIterator<IInputOrOutputIterator<int>>);
|
||||
|
||||
/** A concept specifies a type is an input iterator. */
|
||||
template <typename I>
|
||||
concept CInputIterator = CInputOrOutputIterator<I> && CIndirectlyReadable<I>;
|
||||
|
||||
/** This is an example of an input iterator, indicate the traits that define an input iterator. */
|
||||
template <CReferenceable T>
|
||||
struct IInputIterator /* : IInputOrOutputIterator, IIndirectlyReadable */
|
||||
{
|
||||
// ~Begin CIndirectlyReadable.
|
||||
|
||||
using FElementType = TRemoveCVRef<T>;
|
||||
|
||||
// ~End CIndirectlyReadable.
|
||||
|
||||
// ~Begin CInputOrOutputIterator.
|
||||
|
||||
IInputIterator(IInputIterator&&);
|
||||
IInputIterator* operator=(IInputIterator&&);
|
||||
|
||||
T operator*() const; // Also satisfies CIndirectlyReadable.
|
||||
|
||||
IInputIterator& operator++();
|
||||
|
||||
void operator++(int);
|
||||
|
||||
// ~End CInputOrOutputIterator.
|
||||
};
|
||||
|
||||
// Use IInputIterator<int> represents an input iterator and int is the regular element type.
|
||||
static_assert(CInputIterator<IInputIterator<int>> && CRegular<int>);
|
||||
|
||||
// The CInputIterator requires this code is valid.
|
||||
static_assert(
|
||||
requires(IInputIterator<int> Iter, int& A)
|
||||
{
|
||||
++Iter;
|
||||
Iter++;
|
||||
A = *++Iter;
|
||||
A = *Iter;
|
||||
}
|
||||
);
|
||||
|
||||
/** A concept specifies a type is an output iterator and expression '*Iter++ = A' is valid to write the element. */
|
||||
template <typename I, typename T>
|
||||
concept COutputIterator = CInputOrOutputIterator<I> && CIndirectlyWritable<I, T>
|
||||
&& requires(I Iter) { { Iter++ } -> CIndirectlyWritable<T>; };
|
||||
|
||||
/** This is an example of an output iterator, indicate the traits that define an output iterator. */
|
||||
template <CReferenceable T>
|
||||
struct IOutputIterator /* : IInputOrOutputIterator, IIndirectlyWritable<T> */
|
||||
{
|
||||
// ~Begin CIndirectlyWritable.
|
||||
|
||||
IOutputIterator(IOutputIterator&&);
|
||||
IOutputIterator* operator=(IOutputIterator&&);
|
||||
|
||||
T operator*() const; // Also satisfies CIndirectlyWritable.
|
||||
|
||||
IOutputIterator& operator++();
|
||||
|
||||
/**
|
||||
* Post-increment operator.
|
||||
* Specify, the concept not requires the return type is self type,
|
||||
* but requires the expression '*Iter++ = A;' is equivalent to '*Iter = A; ++Iter;'.
|
||||
* This means that returning a proxy class that satisfies CIndirectlyWritable<T> is also valid.
|
||||
* Also satisfies CIndirectlyWritable.
|
||||
*/
|
||||
IIndirectlyWritable<T> operator++(int);
|
||||
|
||||
// ~End CIndirectlyWritable.
|
||||
};
|
||||
|
||||
// Use IOutputIterator<int> represents an output iterator and int is the regular element type.
|
||||
static_assert(COutputIterator<IOutputIterator<int&>, int> && CRegular<int>);
|
||||
|
||||
// The CInputIterator requires this code is valid.
|
||||
static_assert(
|
||||
requires(IOutputIterator<int&> Iter, int& A)
|
||||
{
|
||||
++Iter;
|
||||
Iter++;
|
||||
*++Iter = A;
|
||||
*Iter++ = A;
|
||||
*Iter = A;
|
||||
}
|
||||
);
|
||||
|
||||
#define ENABLE_RANGE_BASED_FOR_LOOP_SUPPORT public: \
|
||||
NODISCARD FORCEINLINE constexpr auto begin() { return Begin(); } \
|
||||
NODISCARD FORCEINLINE constexpr auto begin() const { return Begin(); } \
|
||||
NODISCARD FORCEINLINE constexpr auto end() { return End(); } \
|
||||
NODISCARD FORCEINLINE constexpr auto end() const { return End(); }
|
||||
|
||||
NAMESPACE_MODULE_END(Utility)
|
||||
NAMESPACE_MODULE_END(Redcraft)
|
||||
NAMESPACE_REDCRAFT_END
|
Reference in New Issue
Block a user