#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 concept CForwardIterator = CInputIterator && CIncrementable && CSentinelFor; /** This is an example of a forward iterator, indicate the traits that define a forward iterator. */ template struct IForwardIterator /* : IInputIterator, IIncrementable, ISentinelFor */ { // ~Begin CInputIterator. using FElementType = TRemoveCVRef; // ~End CInputIterator. // ~Begin CIncrementable and CSentinelFor. 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. // ~Begin CInputIterator. T operator*() const; // Optional satisfies CIndirectlyWritable. IForwardIterator& operator++(); // Also satisfies CIncrementable. IForwardIterator operator++(int); // Also satisfies CIncrementable. // ~End CInputIterator. }; // Use IForwardIterator represents a forward iterator. static_assert(CForwardIterator>); static_assert( COutputIterator, int>); /** A concept specifies a type is a bidirectional iterator. Add the decrement operator to the forward iterator. */ template concept CBidirectionalIterator = CForwardIterator && requires(I Iter) { { --Iter } -> CSameAs; { Iter-- } -> CSameAs; }; /** * 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 struct IBidirectionalIterator /* : IForwardIterator */ { // ~Begin CForwardIterator. using FElementType = TRemoveCVRef; 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 represents a bidirectional iterator. static_assert(CBidirectionalIterator>); static_assert( COutputIterator, 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 concept CRandomAccessIterator = CBidirectionalIterator && CTotallyOrdered && CSizedSentinelFor && requires(I Iter, const I Jter, const ptrdiff N) { { Iter += N } -> CSameAs; { Jter + N } -> CSameAs; { N + Jter } -> CSameAs; { Iter -= N } -> CSameAs; { Jter - N } -> CSameAs; { Jter[N] } -> CSameAs>; }; /** This is an example of a random access iterator, indicate the traits that define a random access iterator. */ template struct IRandomAccessIterator /* : IBidirectionalIterator, ISizedSentinelFor */ { // ~Begin CBidirectionalIterator. using FElementType = TRemoveCVRef; // ~End CBidirectionalIterator. // ~Begin CBidirectionalIterator and CSizedSentinelFor. IRandomAccessIterator(); IRandomAccessIterator(const IRandomAccessIterator&); IRandomAccessIterator(IRandomAccessIterator&&); IRandomAccessIterator* operator=(const IRandomAccessIterator&); IRandomAccessIterator* operator=(IRandomAccessIterator&&); friend bool operator==(const IRandomAccessIterator&, const IRandomAccessIterator&); // ~End CBidirectionalIterator and CSizedSentinelFor. 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. }; // Use IRandomAccessIterator represents a random access iterator static_assert(CRandomAccessIterator>); static_assert( COutputIterator, 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 concept CContiguousIterator = CRandomAccessIterator && CLValueReference> && CSameAs, TRemoveCVRef>> && CSameAs, TAddPointer>> && requires(I& Iter) { { ToAddress(Iter) } -> CSameAs>>; }; /** This is an example of a contiguous iterator, indicate the traits that define a contiguous iterator. */ template struct IContiguousIterator /* : IRandomAccessIterator */ { // ~Begin CRandomAccessIterator. using FElementType = TRemoveCVRef; 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 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 represents a contiguous iterator static_assert(CContiguousIterator>); static_assert( COutputIterator, int>); // The int* is the most typical example of a contiguous iterator static_assert(CContiguousIterator); #if PLATFORM_COMPILER_GCC # pragma GCC diagnostic pop #endif NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END