Plasma Engine  2.0
Loading...
Searching...
No Matches
ArrayIterator.h
1
2#pragma once
3
4#include <algorithm>
5
7template <class ARRAY, class T, bool reverse = false>
9{
10public:
11 using iterator_category = std::random_access_iterator_tag;
12 using value_type = T;
13 using difference_type = std::ptrdiff_t;
14 using pointer = const T*;
15 using reference = const T&;
16
18 {
19 m_pArray = nullptr;
20 m_uiIndex = 0;
21 }
22 const_iterator_base(const ARRAY& deque, size_t uiIndex)
23 {
24 m_pArray = const_cast<ARRAY*>(&deque);
25 m_uiIndex = uiIndex;
26 }
27
28 PL_ALWAYS_INLINE const_iterator_base& operator++()
29 {
30 m_uiIndex += 1;
31 return *this;
32 }
33 PL_ALWAYS_INLINE const_iterator_base& operator--()
34 {
35 m_uiIndex -= 1;
36 return *this;
37 }
38
39 PL_ALWAYS_INLINE const_iterator_base operator++(int)
40 {
41 m_uiIndex += 1;
42 return const_iterator_base(*m_pArray, m_uiIndex - 1);
43 }
44 PL_ALWAYS_INLINE const_iterator_base operator--(int)
45 {
46 m_uiIndex -= 1;
47 return const_iterator_base(*m_pArray, m_uiIndex + 1);
48 }
49
50 PL_ALWAYS_INLINE bool operator==(const const_iterator_base& rhs) const { return m_pArray == rhs.m_pArray && m_uiIndex == rhs.m_uiIndex; }
51 PL_ALWAYS_INLINE bool operator!=(const const_iterator_base& rhs) const { return !(*this == rhs); }
52
53 PL_ALWAYS_INLINE std::ptrdiff_t operator-(const const_iterator_base& rhs) const { return m_uiIndex - rhs.m_uiIndex; }
54
55 PL_ALWAYS_INLINE const_iterator_base operator+(std::ptrdiff_t rhs) const { return const_iterator_base(*m_pArray, m_uiIndex + rhs); }
56 PL_ALWAYS_INLINE const_iterator_base operator-(std::ptrdiff_t rhs) const { return const_iterator_base(*m_pArray, m_uiIndex - rhs); }
57
58 PL_ALWAYS_INLINE void operator+=(std::ptrdiff_t rhs) { m_uiIndex += rhs; }
59 PL_ALWAYS_INLINE void operator-=(std::ptrdiff_t rhs) { m_uiIndex -= rhs; }
60
61 inline const T& operator*() const
62 {
63 if (reverse)
64 return (*m_pArray)[m_pArray->GetCount() - (plUInt32)m_uiIndex - 1];
65 else
66 return (*m_pArray)[(plUInt32)m_uiIndex];
67 }
68 PL_ALWAYS_INLINE const T* operator->() const { return &(**this); }
69
70 PL_ALWAYS_INLINE bool operator<(const const_iterator_base& rhs) const { return m_uiIndex < rhs.m_uiIndex; }
71 PL_ALWAYS_INLINE bool operator>(const const_iterator_base& rhs) const { return m_uiIndex > rhs.m_uiIndex; }
72 PL_ALWAYS_INLINE bool operator<=(const const_iterator_base& rhs) const { return m_uiIndex <= rhs.m_uiIndex; }
73 PL_ALWAYS_INLINE bool operator>=(const const_iterator_base& rhs) const { return m_uiIndex >= rhs.m_uiIndex; }
74
75 PL_ALWAYS_INLINE const T& operator[](size_t uiIndex) const
76 {
77 if (reverse)
78 return (*m_pArray)[m_pArray->GetCount() - static_cast<plUInt32>(m_uiIndex + uiIndex) - 1];
79 else
80 return (*m_pArray)[static_cast<plUInt32>(m_uiIndex + uiIndex)];
81 }
82
83protected:
84 ARRAY* m_pArray;
85 size_t m_uiIndex;
86};
87
89template <class ARRAY, class T, bool reverse = false>
90struct iterator_base : public const_iterator_base<ARRAY, T, reverse>
91{
92public:
93 using pointer = T*;
94 using reference = T&;
95
96 iterator_base() {}
97 iterator_base(ARRAY& ref_deque, size_t uiIndex)
98 : const_iterator_base<ARRAY, T, reverse>(ref_deque, uiIndex)
99 {
100 }
101
102 PL_ALWAYS_INLINE iterator_base& operator++()
103 {
104 this->m_uiIndex += 1;
105 return *this;
106 }
107 PL_ALWAYS_INLINE iterator_base& operator--()
108 {
109 this->m_uiIndex -= 1;
110 return *this;
111 }
112
113 PL_ALWAYS_INLINE iterator_base operator++(int)
114 {
115 this->m_uiIndex += 1;
116 return iterator_base(*this->m_pArray, this->m_uiIndex - 1);
117 }
118 PL_ALWAYS_INLINE iterator_base operator--(int)
119 {
120 this->m_uiIndex -= 1;
121 return iterator_base(*this->m_pArray, this->m_uiIndex + 1);
122 }
123
124 using const_iterator_base<ARRAY, T, reverse>::operator+;
125 using const_iterator_base<ARRAY, T, reverse>::operator-;
126
127 PL_ALWAYS_INLINE iterator_base operator+(std::ptrdiff_t rhs) const { return iterator_base(*this->m_pArray, this->m_uiIndex + rhs); }
128 PL_ALWAYS_INLINE iterator_base operator-(std::ptrdiff_t rhs) const { return iterator_base(*this->m_pArray, this->m_uiIndex - rhs); }
129
130 inline T& operator*() const
131 {
132 if (reverse)
133 return (*this->m_pArray)[this->m_pArray->GetCount() - (plUInt32)this->m_uiIndex - 1];
134 else
135 return (*this->m_pArray)[(plUInt32)this->m_uiIndex];
136 }
137
138 PL_ALWAYS_INLINE T* operator->() const { return &(**this); }
139
140 PL_ALWAYS_INLINE T& operator[](size_t uiIndex) const
141 {
142 if (reverse)
143 return (*this->m_pArray)[this->m_pArray->GetCount() - static_cast<plUInt32>(this->m_uiIndex + uiIndex) - 1];
144 else
145 return (*this->m_pArray)[static_cast<plUInt32>(this->m_uiIndex + uiIndex)];
146 }
147};
148
150template <class T>
152{
153public:
154 using iterator_category = std::random_access_iterator_tag;
155 using value_type = T;
156 using difference_type = std::ptrdiff_t;
157 using pointer = T*;
158 using reference = T&;
159
160 const_reverse_pointer_iterator() { m_pPtr = nullptr; }
161 const_reverse_pointer_iterator(T const* pPtr)
162 : m_pPtr(const_cast<T*>(pPtr))
163 {
164 }
165
166 PL_ALWAYS_INLINE const_reverse_pointer_iterator& operator++()
167 {
168 m_pPtr--;
169 return *this;
170 }
171 PL_ALWAYS_INLINE const_reverse_pointer_iterator& operator--()
172 {
173 m_pPtr++;
174 return *this;
175 }
176
177 PL_ALWAYS_INLINE const_reverse_pointer_iterator operator++(int)
178 {
179 m_pPtr--;
180 return const_reverse_pointer_iterator(m_pPtr + 1);
181 }
182 PL_ALWAYS_INLINE const_reverse_pointer_iterator operator--(int)
183 {
184 m_pPtr++;
185 return const_reverse_pointer_iterator(m_pPtr - 1);
186 }
187
188 PL_ALWAYS_INLINE bool operator==(const const_reverse_pointer_iterator& rhs) const { return m_pPtr == rhs.m_pPtr; }
189 PL_ALWAYS_INLINE bool operator!=(const const_reverse_pointer_iterator& rhs) const { return m_pPtr != rhs.m_pPtr; }
190
191 PL_ALWAYS_INLINE std::ptrdiff_t operator-(const const_reverse_pointer_iterator& rhs) const { return rhs.m_pPtr - m_pPtr; }
192
193 PL_ALWAYS_INLINE const_reverse_pointer_iterator operator+(std::ptrdiff_t rhs) const { return const_reverse_pointer_iterator(m_pPtr - rhs); }
194 PL_ALWAYS_INLINE const_reverse_pointer_iterator operator-(std::ptrdiff_t rhs) const { return const_reverse_pointer_iterator(m_pPtr + rhs); }
195
196 PL_ALWAYS_INLINE void operator+=(std::ptrdiff_t rhs) { m_pPtr -= rhs; }
197 PL_ALWAYS_INLINE void operator-=(std::ptrdiff_t rhs) { m_pPtr += rhs; }
198
199 PL_ALWAYS_INLINE const T& operator*() const { return *m_pPtr; }
200 PL_ALWAYS_INLINE const T* operator->() const { return m_pPtr; }
201
202 PL_ALWAYS_INLINE bool operator<(const const_reverse_pointer_iterator& rhs) const { return m_pPtr > rhs.m_pPtr; }
203 PL_ALWAYS_INLINE bool operator>(const const_reverse_pointer_iterator& rhs) const { return m_pPtr < rhs.m_pPtr; }
204 PL_ALWAYS_INLINE bool operator<=(const const_reverse_pointer_iterator& rhs) const { return m_pPtr >= rhs.m_pPtr; }
205 PL_ALWAYS_INLINE bool operator>=(const const_reverse_pointer_iterator& rhs) const { return m_pPtr <= rhs.m_pPtr; }
206
207 PL_ALWAYS_INLINE const T& operator[](std::ptrdiff_t index) const { return *(m_pPtr - index); }
208
209protected:
210 T* m_pPtr;
211};
212
214template <class T>
216{
217public:
218 using pointer = T*;
219 using reference = T&;
220
224 {
225 }
226
227 PL_ALWAYS_INLINE reverse_pointer_iterator& operator++()
228 {
229 this->m_pPtr--;
230 return *this;
231 }
232 PL_ALWAYS_INLINE reverse_pointer_iterator& operator--()
233 {
234 this->m_pPtr++;
235 return *this;
236 }
237
238 PL_ALWAYS_INLINE reverse_pointer_iterator operator++(int)
239 {
240 this->m_pPtr--;
241 return reverse_pointer_iterator(this->m_pPtr + 1);
242 }
243 PL_ALWAYS_INLINE reverse_pointer_iterator operator--(int)
244 {
245 this->m_pPtr++;
246 return reverse_pointer_iterator(this->m_pPtr - 1);
247 }
248
249 using const_reverse_pointer_iterator<T>::operator+;
250 using const_reverse_pointer_iterator<T>::operator-;
251
252 PL_ALWAYS_INLINE reverse_pointer_iterator operator+(std::ptrdiff_t rhs) const { return reverse_pointer_iterator(this->m_pPtr - rhs); }
253 PL_ALWAYS_INLINE reverse_pointer_iterator operator-(std::ptrdiff_t rhs) const { return reverse_pointer_iterator(this->m_pPtr + rhs); }
254
255 PL_ALWAYS_INLINE T& operator*() const { return *(this->m_pPtr); }
256 PL_ALWAYS_INLINE T* operator->() const { return this->m_pPtr; }
257 PL_ALWAYS_INLINE T& operator[](std::ptrdiff_t index) const { return *(this->m_pPtr - index); }
258};
Base class for STL like random access iterators.
Definition ArrayIterator.h:9
Base class for Pointer like reverse iterators.
Definition ArrayIterator.h:152
Non-const STL like iterators.
Definition ArrayIterator.h:91
Non-Const class for Pointer like reverse iterators.
Definition ArrayIterator.h:216