ezEngine  Milestone 7
ArrayIterator.h
1 
2 #pragma once
3 
4 #include <algorithm>
5 
7 template<class ARRAY, class T, bool reverse = false>
9 {
10 public:
11  typedef std::random_access_iterator_tag iterator_category;
12  typedef T value_type;
13  typedef ptrdiff_t difference_type;
14  typedef T const * pointer;
15  typedef T const & reference;
16 
17  const_iterator_base() { m_Array = nullptr; m_iIndex = 0; }
18  const_iterator_base(const ARRAY& deque, size_t index) { m_Array = const_cast<ARRAY*>(&deque); m_iIndex = index; }
19 
20  EZ_FORCE_INLINE const_iterator_base& operator++() { m_iIndex += 1; return *this; }
21  EZ_FORCE_INLINE const_iterator_base& operator--() { m_iIndex -= 1; return *this; }
22 
23  EZ_FORCE_INLINE const_iterator_base operator++(int) { m_iIndex += 1; return const_iterator_base(*m_Array, m_iIndex - 1); }
24  EZ_FORCE_INLINE const_iterator_base operator--(int) { m_iIndex -= 1; return const_iterator_base(*m_Array, m_iIndex + 1); }
25 
26  EZ_FORCE_INLINE bool operator==(const const_iterator_base& rhs) const { return m_Array == rhs.m_Array && m_iIndex == rhs.m_iIndex; }
27  EZ_FORCE_INLINE bool operator!=(const const_iterator_base& rhs) const { return !(*this == rhs); }
28 
29  EZ_FORCE_INLINE ptrdiff_t operator-(const const_iterator_base& rhs) const { return m_iIndex - rhs.m_iIndex; }
30 
31  EZ_FORCE_INLINE const_iterator_base operator+(ptrdiff_t rhs) const { return const_iterator_base(*m_Array, m_iIndex + rhs); }
32  EZ_FORCE_INLINE const_iterator_base operator-(ptrdiff_t rhs) const { return const_iterator_base(*m_Array, m_iIndex - rhs); }
33 
34  EZ_FORCE_INLINE void operator+=(ptrdiff_t rhs) { m_iIndex += rhs; }
35  EZ_FORCE_INLINE void operator-=(ptrdiff_t rhs) { m_iIndex -= rhs; }
36 
37  EZ_FORCE_INLINE const T& operator*() const { if (reverse) return (*m_Array)[m_Array->GetCount() - (ezUInt32)m_iIndex - 1]; else return (*m_Array)[(ezUInt32)m_iIndex]; }
38  EZ_FORCE_INLINE const T* operator->() const { return &(**this); }
39 
40  EZ_FORCE_INLINE bool operator< (const const_iterator_base& rhs) const { return m_iIndex < rhs.m_iIndex; }
41  EZ_FORCE_INLINE bool operator>(const const_iterator_base& rhs) const { return m_iIndex > rhs.m_iIndex; }
42  EZ_FORCE_INLINE bool operator<=(const const_iterator_base& rhs) const { return m_iIndex <= rhs.m_iIndex; }
43  EZ_FORCE_INLINE bool operator>=(const const_iterator_base& rhs) const { return m_iIndex >= rhs.m_iIndex; }
44 
45 protected:
46  ARRAY* m_Array;
47  size_t m_iIndex;
48 };
49 
51 template<class ARRAY, class T, bool reverse = false>
52 struct iterator_base : public const_iterator_base < ARRAY, T, reverse >
53 {
54 public:
55  typedef T* pointer;
56  typedef T& reference;
57 
58  iterator_base() { }
59  iterator_base(ARRAY& deque, size_t index) : const_iterator_base<ARRAY, T, reverse>(deque, index) { }
60 
61  EZ_FORCE_INLINE iterator_base& operator++() { this->m_iIndex += 1; return *this; }
62  EZ_FORCE_INLINE iterator_base& operator--() { this->m_iIndex -= 1; return *this; }
63 
64  EZ_FORCE_INLINE iterator_base operator++(int) { this->m_iIndex += 1; return iterator_base(*this->m_Array, this->m_iIndex - 1); }
65  EZ_FORCE_INLINE iterator_base operator--(int) { this->m_iIndex -= 1; return iterator_base(*this->m_Array, this->m_iIndex + 1); }
66 
69 
70  EZ_FORCE_INLINE iterator_base operator+(ptrdiff_t rhs) const { return iterator_base(*this->m_Array, this->m_iIndex + rhs); }
71  EZ_FORCE_INLINE iterator_base operator-(ptrdiff_t rhs) const { return iterator_base(*this->m_Array, this->m_iIndex - rhs); }
72 
74  EZ_FORCE_INLINE T& operator*() { if (reverse) return (*this->m_Array)[this->m_Array->GetCount() - (ezUInt32) this->m_iIndex - 1]; else return (*this->m_Array)[(ezUInt32) this->m_iIndex]; }
76  EZ_FORCE_INLINE T* operator->() { return &(**this); }
77 
78 };
79 
81 template<class T>
83 {
84 public:
85  typedef std::random_access_iterator_tag iterator_category;
86  typedef T value_type;
87  typedef ptrdiff_t difference_type;
88  typedef T* pointer;
89  typedef T& reference;
90 
91  const_reverse_pointer_iterator() { m_ptr = nullptr; }
92  const_reverse_pointer_iterator(T const * ptr) : m_ptr(const_cast<T*>(ptr)) {}
93 
94  EZ_FORCE_INLINE const_reverse_pointer_iterator& operator++() { m_ptr--; return *this; }
95  EZ_FORCE_INLINE const_reverse_pointer_iterator& operator--() { m_ptr++; return *this; }
96 
97  EZ_FORCE_INLINE const_reverse_pointer_iterator operator++(int) { m_ptr--; return const_reverse_pointer_iterator(m_ptr + 1); }
98  EZ_FORCE_INLINE const_reverse_pointer_iterator operator--(int) { m_ptr++; return const_reverse_pointer_iterator(m_ptr - 1); }
99 
100  EZ_FORCE_INLINE bool operator==(const const_reverse_pointer_iterator& rhs) const { return m_ptr == rhs.m_ptr; }
101  EZ_FORCE_INLINE bool operator!=(const const_reverse_pointer_iterator& rhs) const { return m_ptr != rhs.m_ptr; }
102 
103  EZ_FORCE_INLINE ptrdiff_t operator-(const const_reverse_pointer_iterator& rhs) const { return rhs.m_ptr - m_ptr; }
104 
105  EZ_FORCE_INLINE const_reverse_pointer_iterator operator+(ptrdiff_t rhs) const { return const_reverse_pointer_iterator(m_ptr - rhs); }
106  EZ_FORCE_INLINE const_reverse_pointer_iterator operator-(ptrdiff_t rhs) const { return const_reverse_pointer_iterator(m_ptr + rhs); }
107 
108  EZ_FORCE_INLINE void operator+=(ptrdiff_t rhs) { m_ptr -= rhs; }
109  EZ_FORCE_INLINE void operator-=(ptrdiff_t rhs) { m_ptr += rhs; }
110 
111  EZ_FORCE_INLINE const T& operator*() const { return *m_ptr; }
112  EZ_FORCE_INLINE const T* operator->() const { return m_ptr; }
113 
114  EZ_FORCE_INLINE bool operator< (const const_reverse_pointer_iterator& rhs) const { return m_ptr > rhs.m_ptr; }
115  EZ_FORCE_INLINE bool operator>(const const_reverse_pointer_iterator& rhs) const { return m_ptr < rhs.m_ptr; }
116  EZ_FORCE_INLINE bool operator<=(const const_reverse_pointer_iterator& rhs) const { return m_ptr >= rhs.m_ptr; }
117  EZ_FORCE_INLINE bool operator>=(const const_reverse_pointer_iterator& rhs) const { return m_ptr <= rhs.m_ptr; }
118 
119 protected:
120  T* m_ptr;
121 };
122 
124 template<class T>
126 {
127 public:
128  typedef T* pointer;
129  typedef T& reference;
130 
133 
134  EZ_FORCE_INLINE reverse_pointer_iterator& operator++() { this->m_ptr--; return *this; }
135  EZ_FORCE_INLINE reverse_pointer_iterator& operator--() { this->m_ptr++; return *this; }
136 
137  EZ_FORCE_INLINE reverse_pointer_iterator operator++(int) { this->m_ptr--; return reverse_pointer_iterator(this->m_ptr + 1); }
138  EZ_FORCE_INLINE reverse_pointer_iterator operator--(int) { this->m_ptr++; return reverse_pointer_iterator(this->m_ptr - 1); }
139 
142 
143  EZ_FORCE_INLINE reverse_pointer_iterator operator+(ptrdiff_t rhs) const { return reverse_pointer_iterator(this->m_ptr - rhs); }
144  EZ_FORCE_INLINE reverse_pointer_iterator operator-(ptrdiff_t rhs) const { return reverse_pointer_iterator(this->m_ptr + rhs); }
145 
147  EZ_FORCE_INLINE T& operator*() { return *(this->m_ptr); }
149  EZ_FORCE_INLINE T* operator->() { return this->m_ptr; }
150 
151 };