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 "ownerPtr.h"
8#include "wrapper.h"
9#include "error.h"
10#include "maths.h"
11
19
20namespace original
21{
36 template<typename TYPE>
37 class stepIterator : public baseIterator<TYPE>
38 {
39 protected:
41
46 explicit stepIterator(wrapper<TYPE>* ptr);
47
54 static integer ptrDistance(const stepIterator* start, const stepIterator* end);
55
61 bool equalPtr(const iterator<TYPE>* other) const override;
62
63 public:
68 stepIterator(const stepIterator& other);
69
75 stepIterator& operator=(const stepIterator& other);
76
81 stepIterator* clone() const override;
82
87 [[nodiscard]] bool hasNext() const override;
88
93 [[nodiscard]] bool hasPrev() const override;
94
100 bool atPrev(const iterator<TYPE>* other) const override;
101
107 bool atNext(const iterator<TYPE>* other) const override;
108
112 void next() const override;
113
118 void prev() const override;
119
124 void operator+=(integer steps) const override;
125
131 void operator-=(integer steps) const override;
132
138 integer operator-(const iterator<TYPE>& other) const override;
139
144 stepIterator* getNext() const override;
145
150 iterator<TYPE>* getPrev() const override;
151
156 TYPE& get() override;
157
162 TYPE get() const override;
163
168 void set(const TYPE& data) override;
169
174 [[nodiscard]] bool isValid() const override;
175
180 [[nodiscard]] std::string className() const override;
181 };
182}
183
184 template <typename TYPE>
187
188 template <typename TYPE>
190 {
191 auto s = ownerPtr(start->clone());
192 auto e = ownerPtr(end->clone());
193 integer dis = 0;
194 while (s->isValid()){
195 if (s->_ptr == e->_ptr){
196 return dis;
197 }
198 dis += 1;
199 s->next();
200 }
201 if (e->isValid()){
202 dis = std::numeric_limits<integer>::max();
203 }
204 return dis;
205 }
206
207 template<typename TYPE>
209 auto* other_it = dynamic_cast<const stepIterator*>(other);
210 return other_it != nullptr && _ptr == other_it->_ptr;
211 }
212
213 template <typename TYPE>
215 : _ptr(nullptr)
216 {
217 this->operator=(other);
218 }
219
220 template <typename TYPE>
222 const stepIterator& other) -> stepIterator&
223 {
224 if (this == &other) {
225 return *this;
226 }
227 this->_ptr = other._ptr;
228 return *this;
229 }
230
231 template<typename TYPE>
233 return new stepIterator(*this);
234 }
235
236 template <typename TYPE>
238 {
239 return this->isValid();
240 }
241
242 template<typename TYPE>
245 }
246
247 template <typename TYPE>
249 {
251 }
252
253 template <typename TYPE>
255 {
257 }
258
259 template <typename TYPE>
261 {
262 if (!this->isValid()) throw nullPointerError();
263 this->_ptr = this->_ptr->getPNext();
264 }
265
266 template<typename TYPE>
269 }
270
271 template <typename TYPE>
272 auto original::stepIterator<TYPE>::operator+=(const integer steps) const -> void
273 {
274 if (steps < 0) {
275 this->operator-=(abs(steps));
276 return;
277 }
278
279 for (integer i = 0; i < steps; i++)
280 {
281 this->next();
282 }
283 }
284
285 template <typename TYPE>
286 auto original::stepIterator<TYPE>::operator-=(const integer steps) const -> void
287 {
288 if (steps < 0) {
289 this->operator+=(abs(steps));
290 return;
291 }
292
293 for (integer i = 0; i < steps; i++)
294 {
295 this->prev();
296 }
297 }
298
299 template <typename TYPE>
301 {
302 auto* other_it = dynamic_cast<const stepIterator*>(&other);
303 if (other_it == nullptr)
304 return this > &other ?
305 std::numeric_limits<integer>::max() :
306 std::numeric_limits<integer>::min();
307 if (const integer pos_dis = ptrDistance(other_it, this);
308 pos_dis != std::numeric_limits<integer>::max()) return pos_dis;
309 if (const integer neg_dis = ptrDistance(this, other_it);
310 neg_dis != std::numeric_limits<integer>::max()) return -neg_dis;
311 return this->_ptr > other_it->_ptr ?
312 std::numeric_limits<integer>::max() :
313 std::numeric_limits<integer>::min();
314 }
315
316 template <typename TYPE>
318 return new stepIterator(this->_ptr->getPNext());
319 }
320
321 template<typename TYPE>
324 }
325
326 template <typename TYPE>
328 {
329 if (!this->isValid()) throw nullPointerError();
330 return this->_ptr->getVal();
331 }
332
333 template <typename TYPE>
335 {
336 if (!this->isValid()) throw nullPointerError();
337 return this->_ptr->getVal();
338 }
339
340 template <typename TYPE>
341 auto original::stepIterator<TYPE>::set(const TYPE& data) -> void
342 {
343 if (!this->isValid()) throw nullPointerError();
344 this->_ptr->setVal(data);
345 }
346
347 template <typename TYPE>
349 {
350 return this->_ptr != nullptr;
351 }
352
353 template <typename TYPE>
354 auto original::stepIterator<TYPE>::className() const -> std::string
355 {
356 return "stepIterator";
357 }
358
359#endif //STEPITERATOR_H
A base class for basic iterators.
Definition iterator.h:296
Base iterator interface that supports common operations for iteration.
Definition iterator.h:37
Exception for null pointer dereference attempts.
Definition error.h:110
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:30
iterator< TYPE > * getPrev() const override
Creates a new iterator pointing to the previous element.
Definition stepIterator.h:322
wrapper< TYPE > * _ptr
Pointer to the current wrapper.
Definition stepIterator.h:40
stepIterator * clone() const override
Creates a heap-allocated copy of the iterator.
Definition stepIterator.h:232
bool hasPrev() const override
Checks if the iterator can move backward.
Definition stepIterator.h:243
void set(const TYPE &data) override
Sets the value of the current element.
Definition stepIterator.h:341
bool equalPtr(const iterator< TYPE > *other) const override
Equality comparison for two iterators.
Definition stepIterator.h:208
integer operator-(const iterator< TYPE > &other) const override
Calculates the number of steps between the current iterator and another iterator.
Definition stepIterator.h:300
void next() const override
Advances to the next position in the container.
Definition stepIterator.h:260
std::string className() const override
Returns the class name of the iterator.
Definition stepIterator.h:354
TYPE & get() override
Gets the reference of current element in the container.
Definition stepIterator.h:327
void prev() const override
Retreats to the previous position in the container.
Definition stepIterator.h:267
stepIterator(wrapper< TYPE > *ptr)
Protected constructor for derived classes.
Definition stepIterator.h:185
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:254
bool hasNext() const override
Checks if the iterator can move forward.
Definition stepIterator.h:237
static integer ptrDistance(const stepIterator *start, const stepIterator *end)
Calculates the distance between two iterators.
Definition stepIterator.h:189
stepIterator & operator=(const stepIterator &other)
Copy assignment operator for stepIterator.
Definition stepIterator.h:221
void operator+=(integer steps) const override
Moves forward by a specified number of steps.
Definition stepIterator.h:272
bool isValid() const override
Checks if the iterator is valid.
Definition stepIterator.h:348
stepIterator * getNext() const override
Creates a new iterator pointing to the next element.
Definition stepIterator.h:317
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:248
void operator-=(integer steps) const override
Moves backward by a specified number of steps.
Definition stepIterator.h:286
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:35
Exclusive-ownership smart pointer implementation.
Abstract polymorphic container with value encapsulation and navigation support.