#pragma once #include "CoreTypes.h" #include "Iterator/Utility.h" #include "Iterator/Sentinel.h" #include "Iterator/BidirectionalIterator.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 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 ElementType = 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>); #if PLATFORM_COMPILER_GCC # pragma GCC diagnostic pop #endif NAMESPACE_MODULE_END(Utility) NAMESPACE_MODULE_END(Redcraft) NAMESPACE_REDCRAFT_END