#pragma once #include "CoreDefs.h" #include template class ArrayViewIterator { public: #if GP_DEBUG_CONFIG ArrayViewIterator(T *items, size_t count, size_t index); #else ArrayViewIterator(T *item); #endif ArrayViewIterator(const ArrayViewIterator &other); ArrayViewIterator operator++(int); ArrayViewIterator &operator++(); ArrayViewIterator operator--(int); ArrayViewIterator &operator--(); ArrayViewIterator &operator+=(ptrdiff_t delta); ArrayViewIterator &operator-=(ptrdiff_t delta); bool operator==(const ArrayViewIterator &other) const; bool operator!=(const ArrayViewIterator &other) const; operator T*() const; private: #if GP_DEBUG_CONFIG T *m_items; size_t m_count; size_t m_index; #else T *m_iter; #endif }; #if GP_DEBUG_CONFIG #include template inline ArrayViewIterator::ArrayViewIterator(T *items, size_t count, size_t index) : m_items(items) , m_count(count) , m_index(index) { } template inline ArrayViewIterator::ArrayViewIterator(const ArrayViewIterator &other) : m_items(other.m_items) , m_count(other.m_count) , m_index(other.m_index) { } template inline ArrayViewIterator &ArrayViewIterator::operator+=(ptrdiff_t delta) { if (delta < 0) { assert(-static_cast(m_index) >= delta); } else { assert(static_cast(m_count) >= delta && static_cast(m_count - m_index) >= delta); } m_index = static_cast(static_cast(m_index) + delta); return *this; } template inline ArrayViewIterator &ArrayViewIterator::operator-=(ptrdiff_t delta) { if (delta >= 0) { assert(static_cast(m_index) >= delta); } else { assert(-static_cast(m_count) <= delta && -static_cast(m_count - m_index) <= delta); } m_index = static_cast(static_cast(m_index) - delta); return *this; } template inline bool ArrayViewIterator::operator==(const ArrayViewIterator &other) const { return m_index == other.m_index && m_items == other.m_items; } template inline bool ArrayViewIterator::operator!=(const ArrayViewIterator &other) const { return m_index != other.m_index || m_items != other.m_items; } template inline ArrayViewIterator::operator T*() const { assert(m_index < m_count); return m_items + m_index; } #else template inline ArrayViewIterator::ArrayViewIterator(T *item) : m_iter(item) { } template inline ArrayViewIterator::ArrayViewIterator(const ArrayViewIterator &other) : m_iter(other.m_iter) { } template inline ArrayViewIterator &ArrayViewIterator::operator+=(ptrdiff_t delta) { m_iter += delta; return *this; } template inline ArrayViewIterator &ArrayViewIterator::operator-=(ptrdiff_t delta) { m_iter += delta; return *this; } template inline bool ArrayViewIterator::operator==(const ArrayViewIterator &other) const { return m_iter == other.m_iter; } template inline bool ArrayViewIterator::operator!=(const ArrayViewIterator &other) const { return m_iter != other.m_iter; } template inline ArrayViewIterator::operator T*() const { return m_iter; } #endif template inline ArrayViewIterator ArrayViewIterator::operator++(int) { ArrayViewIterator copy = *this; ++(*this); return copy; } template inline ArrayViewIterator ArrayViewIterator::operator--(int) { ArrayViewIterator copy = *this; --(*this); return copy; } template inline ArrayViewIterator &ArrayViewIterator::operator++() { (*this) += 1; return *this; } template inline ArrayViewIterator &ArrayViewIterator::operator--() { (*this) -= 1; return *this; }