ORIGINAL
Loading...
Searching...
No Matches
iterator.h
Go to the documentation of this file.
1#ifndef ITERATOR_H
2#define ITERATOR_H
3
4#include "cloneable.h"
5#include "comparable.h"
6#include "error.h"
7#include "printable.h"
8
9namespace original {
10
18
36template<typename TYPE>
37class iterator : public printable, public cloneable, public comparable<iterator<TYPE>> {
38protected:
52 virtual bool equalPtr(const iterator* other) const = 0;
53
54public:
59 TYPE& operator*();
60
65 TYPE operator*() const;
66
70 void operator++() const;
71
76 void operator++(int postfix) const;
77
81 void operator--() const;
82
87 void operator--(int postfix) const;
88
93 virtual void operator+=(integer steps) const = 0;
94
99 virtual void operator-=(integer steps) const = 0;
100
116 integer compareTo(const iterator& other) const override;
117
134 virtual integer operator-(const iterator& other) const = 0;
135
140 iterator* clone() const override = 0;
141
146 explicit operator bool() const;
147
152 [[nodiscard]] virtual bool hasNext() const = 0;
153
158 [[nodiscard]] virtual bool hasPrev() const = 0;
159
165 virtual bool atPrev(const iterator* other) const = 0;
166
172 virtual bool atNext(const iterator* other) const = 0;
173
179 bool atPrev(const iterator& other) const;
180
186 bool atNext(const iterator& other) const;
187
191 virtual void next() const = 0;
192
196 virtual void prev() const = 0;
197
202 virtual iterator* getNext() const;
203
208 virtual iterator* getPrev() const = 0;
209
214 virtual TYPE& get() = 0;
215
220 virtual TYPE get() const = 0;
221
226 virtual TYPE getElem() const;
227
232 virtual void set(const TYPE& data) = 0;
233
239 bool equal(const iterator* other) const;
240
246 bool equal(const iterator& other) const;
247
252 [[nodiscard]] virtual bool isValid() const = 0;
253
258 [[nodiscard]] std::string className() const override;
259
265 [[nodiscard]] std::string toString(bool enter) const override;
266
270 ~iterator() override = default;
271
272 // Friends for operator overloading
273 template<typename T>
274 friend iterator<T>* operator+(const iterator<T>& it, integer steps);
275
276 template<typename T>
277 friend iterator<T>* operator-(const iterator<T>& it, integer steps);
278
279 template<typename T>
280 friend bool operator==(const iterator<T>& l_it, const iterator<T>& r_it);
281
282 template<typename T>
283 friend bool operator!=(const iterator<T>& l_it, const iterator<T>& r_it);
284};
285
295template <typename TYPE>
296class baseIterator : public iterator<TYPE> {
297public:
302 baseIterator* clone() const override = 0;
303
307 ~baseIterator() override = default;
308};
309
319template <typename T>
320auto operator+(const iterator<T>& it, integer steps) -> iterator<T>*;
321
331template <typename T>
332auto operator-(const iterator<T>& it, integer steps) -> iterator<T>*;
333
342template<typename T>
343bool operator==(const iterator<T>& l_it, const iterator<T>& r_it);
344
353template<typename T>
354bool operator!=(const iterator<T>& l_it, const iterator<T>& r_it);
355
356} // namespace original
357 template<typename TYPE>
359 return this->get();
360 }
361
362 template<typename TYPE>
364 return this->get();
365 }
366
367 template<typename TYPE>
369 this->next();
370 }
371
372 template<typename TYPE>
373 auto original::iterator<TYPE>::operator++(int) const -> void {
374 this->operator++();
375 }
376
377 template<typename TYPE>
379 this->prev();
380 }
381
382 template<typename TYPE>
383 auto original::iterator<TYPE>::operator--(int) const -> void {
384 this->operator--();
385 }
386
387 template<typename TYPE>
389 return this->operator-(other);
390 }
391
392 template<typename TYPE>
394 return this->isValid();
395 }
396
397 template<typename TYPE>
398 auto original::iterator<TYPE>::atPrev(const iterator &other) const -> bool {
399 return this->atPrev(&other);
400 }
401
402 template<typename TYPE>
403 auto original::iterator<TYPE>::atNext(const iterator &other) const -> bool {
404 return this->atNext(&other);
405 }
406
407 template<typename TYPE>
409 if (!this->isValid()) throw outOfBoundError();
410 auto it = this->clone();
411 it->next();
412 return it;
413 }
414
415 template <typename TYPE>
417 {
418 return this->get();
419 }
420
421 template<typename TYPE>
422 auto original::iterator<TYPE>::equal(const iterator *other) const -> bool {
423 return this->equalPtr(other);
424 }
425
426 template<typename TYPE>
427 auto original::iterator<TYPE>::equal(const iterator &other) const -> bool {
428 return this->equal(&other);
429 }
430
431 template<typename TYPE>
432 auto original::iterator<TYPE>::className() const -> std::string {
433 return "iterator";
434 }
435
436 template<typename TYPE>
437 auto original::iterator<TYPE>::toString(const bool enter) const -> std::string {
438 std::stringstream ss;
439 ss << this->className() << "(";
440 if (this->isValid()) ss << formatString(this->get());
441 ss << ")";
442 if (enter) {
443 ss << "\n";
444 }
445 return ss.str();
446 }
447
448 template <typename TYPE>
450 {
451 auto* nit = it.clone();
452 nit->operator+=(steps);
453 return nit;
454 }
455
456 template <typename TYPE>
457 auto original::operator-(const iterator<TYPE>& it, integer steps) -> iterator<TYPE>*
458 {
459 auto* nit = it.clone();
460 nit->operator-=(steps);
461 return nit;
462 }
463
464template<typename T>
465bool original::operator==(const iterator<T> &l_it, const iterator<T> &r_it) {
466 return l_it.equal(r_it);
467}
468
469template<typename T>
470bool original::operator!=(const iterator<T> &l_it, const iterator<T> &r_it) {
471 return !l_it.equal(r_it);
472}
473
474#endif //ITERATOR_H
A base class for basic iterators.
Definition iterator.h:296
baseIterator * clone() const override=0
Creates a clone of the iterator.
~baseIterator() override=default
Virtual destructor for proper cleanup of derived objects.
cloneable()=default
Default constructor for cloneable.
Base class for comparable objects.
Definition comparable.h:31
Base iterator interface that supports common operations for iteration.
Definition iterator.h:37
virtual void next() const =0
Moves the iterator to the next element.
virtual integer operator-(const iterator &other) const =0
Returns the distance between this iterator and another iterator.
virtual bool equalPtr(const iterator *other) const =0
Checks if two iterators point to the same underlying element.
void operator++(int postfix) const
Moves the iterator forward by one position (postfix).
Definition iterator.h:373
virtual bool hasPrev() const =0
Checks if there is a previous element.
bool atPrev(const iterator &other) const
Checks if this iterator is positioned at the previous element.
Definition iterator.h:398
~iterator() override=default
Virtual destructor for proper cleanup of derived objects.
bool equal(const iterator *other) const
Checks if two iterators are equal.
Definition iterator.h:422
void operator--(int postfix) const
Moves the iterator backward by one position (postfix).
Definition iterator.h:383
friend bool operator!=(const iterator< T > &l_it, const iterator< T > &r_it)
Inequality comparison operator for iterators.
virtual void operator-=(integer steps) const =0
Subtracts a number of steps from the current position of iterator.
void operator--() const
Moves the iterator backward by one position.
Definition iterator.h:378
friend iterator< T > * operator+(const iterator< T > &it, integer steps)
Adds a number of steps to the iterator's current position and returns a new iterator.
std::string toString(bool enter) const override
Returns a string representation of the iterator.
Definition iterator.h:437
virtual void set(const TYPE &data)=0
Sets the element pointed to by the iterator.
virtual iterator * getPrev() const =0
Returns a new iterator pointing to the previous element.
std::string className() const override
Returns the class name of the iterator.
Definition iterator.h:432
virtual bool atNext(const iterator *other) const =0
Checks if this iterator is positioned at the next element.
virtual bool atPrev(const iterator *other) const =0
Checks if this iterator is positioned at the previous element.
virtual TYPE & get()=0
Gets the element pointed to by the iterator.
TYPE & operator*()
Dereferences the iterator to get the element.
Definition iterator.h:358
friend iterator< T > * operator-(const iterator< T > &it, integer steps)
Subtracts a number of steps from the iterator's current position and returns a new iterator.
integer compareTo(const iterator &other) const override
Compares two iterators to determine their relative positions.
Definition iterator.h:388
TYPE operator*() const
Dereferences the iterator to get a copy of the element.
Definition iterator.h:363
virtual void prev() const =0
Moves the iterator to the previous element.
virtual bool isValid() const =0
Checks if the iterator is valid.
virtual bool hasNext() const =0
Checks if there is a next element.
void operator++() const
Moves the iterator forward by one position.
Definition iterator.h:368
iterator * clone() const override=0
Creates a clone of the iterator.
bool equal(const iterator &other) const
Checks if two iterators are equal.
Definition iterator.h:427
virtual TYPE get() const =0
Gets a copy of the element pointed to by the iterator.
bool atNext(const iterator &other) const
Checks if this iterator is positioned at the next element.
Definition iterator.h:403
virtual iterator * getNext() const
Returns a new iterator pointing to the next element.
Definition iterator.h:408
virtual TYPE getElem() const
Returns a copy of the element.
Definition iterator.h:416
friend bool operator==(const iterator< T > &l_it, const iterator< T > &r_it)
Equality comparison operator for iterators.
virtual void operator+=(integer steps) const =0
Adds a number of steps to the current position of iterator.
Exception for container index out-of-range errors.
Definition error.h:84
Base class providing polymorphic string conversion capabilities.
Definition printable.h:29
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Abstract base class for cloneable objects.
Interface for objects that can be compared.
Custom exception classes and callback validation utilities.
Main namespace for the project Original.
Definition algorithms.h:21
auto operator+(const iterator< T > &it, integer steps) -> iterator< T > *
Adds a number of steps to the iterator's current position and returns a new iterator.
auto operator-(const iterator< T > &it, integer steps) -> iterator< T > *
Subtracts a number of steps from the iterator's current position and returns a new iterator.
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:35
bool operator!=(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Inequality comparison with nullptr.
Definition autoPtr.h:566
bool operator==(const autoPtr< T, DER, DEL > &ptr, const std::nullptr_t &null)
Equality comparison with nullptr.
Definition autoPtr.h:561
Interface for polymorphic string formatting and output.