mirror of
https://github.com/ZDoom/gzdoom.git
synced 2025-02-20 19:12:07 +00:00
Better STL compatibility for array iterator
Extended iterator with types and functions required to use array objects in STL algorithms
This commit is contained in:
parent
274951839a
commit
61ead4f470
1 changed files with 29 additions and 4 deletions
33
src/tarray.h
33
src/tarray.h
|
@ -39,6 +39,7 @@
|
|||
#include <string.h>
|
||||
#include <new>
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#include <inttypes.h> // for intptr_t
|
||||
|
@ -48,13 +49,37 @@
|
|||
|
||||
#include "m_alloc.h"
|
||||
|
||||
template<typename T> class TIterator
|
||||
template<typename T> class TIterator : public std::iterator<std::random_access_iterator_tag, T>
|
||||
{
|
||||
public:
|
||||
typedef typename TIterator::value_type value_type;
|
||||
typedef typename TIterator::difference_type difference_type;
|
||||
typedef typename TIterator::pointer pointer;
|
||||
typedef typename TIterator::reference reference;
|
||||
typedef typename TIterator::iterator_category iterator_category;
|
||||
|
||||
TIterator(T* ptr = nullptr) { m_ptr = ptr; }
|
||||
bool operator==(const TIterator<T>& other) const { return (m_ptr == other.m_ptr); }
|
||||
bool operator!=(const TIterator<T>& other) const { return (m_ptr != other.m_ptr); }
|
||||
TIterator<T> &operator++() { ++m_ptr; return (*this); }
|
||||
|
||||
// Comparison operators
|
||||
bool operator==(const TIterator &other) const { return m_ptr == other.m_ptr; }
|
||||
bool operator!=(const TIterator &other) const { return m_ptr != other.m_ptr; }
|
||||
bool operator< (const TIterator &other) const { return m_ptr < other.m_ptr; }
|
||||
bool operator<=(const TIterator &other) const { return m_ptr <= other.m_ptr; }
|
||||
bool operator> (const TIterator &other) const { return m_ptr > other.m_ptr; }
|
||||
bool operator>=(const TIterator &other) const { return m_ptr >= other.m_ptr; }
|
||||
|
||||
// Arithmetic operators
|
||||
TIterator &operator++() { ++m_ptr; return *this; }
|
||||
TIterator operator++(int) { pointer tmp = m_ptr; ++*this; return TIterator(tmp); }
|
||||
TIterator &operator--() { --m_ptr; return *this; }
|
||||
TIterator operator--(int) { pointer tmp = m_ptr; --*this; return TIterator(tmp); }
|
||||
TIterator &operator+=(difference_type offset) { m_ptr += offset; return *this; }
|
||||
TIterator operator+(difference_type offset) const { return TIterator(m_ptr + offset); }
|
||||
friend TIterator operator+(difference_type offset, const TIterator &other) { return TIterator(offset + other.m_ptr); }
|
||||
TIterator &operator-=(difference_type offset) { m_ptr -= offset; return *this; }
|
||||
TIterator operator-(difference_type offset) const { return TIterator(m_ptr - offset); }
|
||||
difference_type operator-(const TIterator &other) const { return m_ptr - other.m_ptr; }
|
||||
|
||||
T &operator*() { return *m_ptr; }
|
||||
const T &operator*() const { return *m_ptr; }
|
||||
T* operator->() { return m_ptr; }
|
||||
|
|
Loading…
Reference in a new issue