From 61ead4f4701a79b5f4d4f53fa73d534ebe15b244 Mon Sep 17 00:00:00 2001
From: "alexey.lysiuk" <alexey.lysiuk@gmail.com>
Date: Sun, 5 Nov 2017 13:18:22 +0200
Subject: [PATCH] Better STL compatibility for array iterator

Extended iterator with types and functions required to use array objects in STL algorithms
---
 src/tarray.h | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/tarray.h b/src/tarray.h
index acc080012..63eeb4eed 100644
--- a/src/tarray.h
+++ b/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; }