ORIGINAL
Loading...
Searching...
No Matches
stepIterator.h
Go to the documentation of this file.
1#ifndef STEPITERATOR_H
2#define STEPITERATOR_H
3
4#include <limits>
5
6#include "iterator.h"
7#include "wrapper.h"
8#include "error.h"
9#include "maths.h"
10
18
19namespace original
20{
35 template<typename TYPE>
36 class stepIterator : public baseIterator<TYPE>
37 {
38 protected:
40
45 explicit stepIterator(wrapper<TYPE>* ptr);
46
53 static integer ptrDistance(const stepIterator* start, const stepIterator* end);
54
60 bool equalPtr(const iterator<TYPE>* other) const override;
61
62 public:
67 stepIterator(const stepIterator& other);
68
74 stepIterator& operator=(const stepIterator& other);
75
80 stepIterator* clone() const override;
81
86 [[nodiscard]] bool hasNext() const override;
87
92 [[nodiscard]] bool hasPrev() const override;
93
99 bool atPrev(const iterator<TYPE>* other) const override;
100
106 bool atNext(const iterator<TYPE>* other) const override;
107
111 void next() const override;
112
117 void prev() const override;
118
123 void operator+=(integer steps) const override;
124
130 void operator-=(integer steps) const override;
131
137 integer operator-(const iterator<TYPE>& other) const override;
138
143 stepIterator* getNext() const override;
144
149 iterator<TYPE>* getPrev() const override;
150
155 TYPE& get() override;
156
161 TYPE get() const override;
162
167 void set(const TYPE& data) override;
168
173 [[nodiscard]] bool isValid() const override;
174
179 [[nodiscard]] std::string className() const override;
180 };
181}
182
183 template <typename TYPE>
186
187 template <typename TYPE>
189 {
190 auto* s = start->clone();
191 auto* e = end->clone();
192 integer dis = 0;
193 while (s->isValid()){
194 if (s->_ptr == e->_ptr){
195 delete s;
196 delete e;
197 return dis;
198 }
199 dis += 1;
200 s->next();
201 }
202 if (e->isValid()){
203 dis = std::numeric_limits<integer>::max();
204 }
205 delete s;
206 delete e;
207 return dis;
208 }
209
210 template<typename TYPE>
212 auto* other_it = dynamic_cast<const stepIterator*>(other);
213 return other_it != nullptr && _ptr == other_it->_ptr;
214 }
215
216 template <typename TYPE>
218 : _ptr(nullptr)
219 {
220 this->operator=(other);
221 }
222
223 template <typename TYPE>
225 const stepIterator& other) -> stepIterator&
226 {
227 if (this == &other) {
228 return *this;
229 }
230 this->_ptr = other._ptr;
231 return *this;
232 }
233
234 template<typename TYPE>
236 return new stepIterator(*this);
237 }
238
239 template <typename TYPE>
241 {
242 return this->isValid();
243 }
244
245 template<typename TYPE>
248 }
249
250 template <typename TYPE>
252 {
254 }
255
256 template <typename TYPE>
258 {
260 }
261
262 template <typename TYPE>
264 {
265 if (!this->isValid()) throw nullPointerError();
266 this->_ptr = this->_ptr->getPNext();
267 }
268
269 template<typename TYPE>
272 }
273
274 template <typename TYPE>
275 auto original::stepIterator<TYPE>::operator+=(const integer steps) const -> void
276 {
277 if (steps < 0) {
278 this->operator-=(original::abs(steps));
279 return;
280 }
281
282 for (integer i = 0; i < steps; i++)
283 {
284 this->next();
285 }
286 }
287
288 template <typename TYPE>
289 auto original::stepIterator<TYPE>::operator-=(const integer steps) const -> void
290 {
291 if (steps < 0) {
292 this->operator+=(original::abs(steps));
293 return;
294 }
295
296 for (integer i = 0; i < steps; i++)
297 {
298 this->prev();
299 }
300 }
301
302 template <typename TYPE>
304 {
305 auto* other_it = dynamic_cast<const stepIterator*>(&other);
306 if (other_it == nullptr)
307 return this > &other ?
308 std::numeric_limits<integer>::max() :
309 std::numeric_limits<integer>::min();
310 if (const integer pos_dis = ptrDistance(other_it, this);
311 pos_dis != std::numeric_limits<integer>::max()) return pos_dis;
312 if (const integer neg_dis = ptrDistance(this, other_it);
313 neg_dis != std::numeric_limits<integer>::max()) return -neg_dis;
314 return this->_ptr > other_it->_ptr ?
315 std::numeric_limits<integer>::max() :
316 std::numeric_limits<integer>::min();
317 }
318
319 template <typename TYPE>
321 return new stepIterator(this->_ptr->getPNext());
322 }
323
324 template<typename TYPE>
327 }
328
329 template <typename TYPE>
331 {
332 if (!this->isValid()) throw nullPointerError();
333 return this->_ptr->getVal();
334 }
335
336 template <typename TYPE>
338 {
339 if (!this->isValid()) throw nullPointerError();
340 return this->_ptr->getVal();
341 }
342
343 template <typename TYPE>
344 auto original::stepIterator<TYPE>::set(const TYPE& data) -> void
345 {
346 if (!this->isValid()) throw nullPointerError();
347 this->_ptr->setVal(data);
348 }
349
350 template <typename TYPE>
352 {
353 return this->_ptr != nullptr;
354 }
355
356 template <typename TYPE>
357 auto original::stepIterator<TYPE>::className() const -> std::string
358 {
359 return "stepIterator";
360 }
361
362#endif //STEPITERATOR_H
A base class for basic iterators.
Definition iterator.h:259
Base iterator interface that supports common operations for iteration.
Definition iterator.h:35
Exception for null pointer dereference attempts.
Definition error.h:110
iterator< TYPE > * getPrev() const override
Creates a new iterator pointing to the previous element.
Definition stepIterator.h:325
wrapper< TYPE > * _ptr
Pointer to the current wrapper.
Definition stepIterator.h:39
stepIterator * clone() const override
Creates a heap-allocated copy of the iterator.
Definition stepIterator.h:235
bool hasPrev() const override
Checks if the iterator can move backward.
Definition stepIterator.h:246
void set(const TYPE &data) override
Sets the value of the current element.
Definition stepIterator.h:344
bool equalPtr(const iterator< TYPE > *other) const override
Equality comparison for two iterators.
Definition stepIterator.h:211
integer operator-(const iterator< TYPE > &other) const override
Calculates the number of steps between the current iterator and another iterator.
Definition stepIterator.h:303
void next() const override
Advances to the next position in the container.
Definition stepIterator.h:263
std::string className() const override
Returns the class name of the iterator.
Definition stepIterator.h:357
TYPE & get() override
Gets the reference of current element in the container.
Definition stepIterator.h:330
void prev() const override
Retreats to the previous position in the container.
Definition stepIterator.h:270
stepIterator(wrapper< TYPE > *ptr)
Protected constructor for derived classes.
Definition stepIterator.h:184
bool atNext(const iterator< TYPE > *other) const override
Checks if the current iterator is at the next position relative to another iterator.
Definition stepIterator.h:257
bool hasNext() const override
Checks if the iterator can move forward.
Definition stepIterator.h:240
static integer ptrDistance(const stepIterator *start, const stepIterator *end)
Calculates the distance between two iterators.
Definition stepIterator.h:188
stepIterator & operator=(const stepIterator &other)
Copy assignment operator for stepIterator.
Definition stepIterator.h:224
void operator+=(integer steps) const override
Moves forward by a specified number of steps.
Definition stepIterator.h:275
bool isValid() const override
Checks if the iterator is valid.
Definition stepIterator.h:351
stepIterator * getNext() const override
Creates a new iterator pointing to the next element.
Definition stepIterator.h:320
bool atPrev(const iterator< TYPE > *other) const override
Checks if the current iterator is at the previous position relative to another iterator.
Definition stepIterator.h:251
void operator-=(integer steps) const override
Moves backward by a specified number of steps.
Definition stepIterator.h:289
Exception for unimplemented method calls.
Definition error.h:123
Base class for linked value containers with formatted output.
Definition wrapper.h:28
Custom exception classes and callback validation utilities.
Defines the iterator class for traversing and manipulating container elements.
Mathematical utilities and constants.
Main namespace for the project Original.
Definition algorithms.h:21
TYPE abs(TYPE a)
Returns the absolute value of a given number.
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:15
Abstract polymorphic container with value encapsulation and navigation support.