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
34template<typename TYPE>
35class iterator : public printable, public cloneable, public comparable<iterator<TYPE>> {
36protected:
42 virtual bool equalPtr(const iterator* other) const = 0;
43
44public:
49 TYPE& operator*();
50
55 TYPE operator*() const;
56
60 void operator++() const;
61
66 void operator++(int postfix) const;
67
71 void operator--() const;
72
77 void operator--(int postfix) const;
78
83 virtual void operator+=(integer steps) const = 0;
84
89 virtual void operator-=(integer steps) const = 0;
90
96 integer compareTo(const iterator& other) const override;
97
103 virtual integer operator-(const iterator& other) const = 0;
104
109 iterator* clone() const override = 0;
110
115 explicit operator bool() const;
116
121 [[nodiscard]] virtual bool hasNext() const = 0;
122
127 [[nodiscard]] virtual bool hasPrev() const = 0;
128
134 virtual bool atPrev(const iterator* other) const = 0;
135
141 virtual bool atNext(const iterator* other) const = 0;
142
148 bool atPrev(const iterator& other) const;
149
155 bool atNext(const iterator& other) const;
156
160 virtual void next() const = 0;
161
165 virtual void prev() const = 0;
166
171 virtual iterator* getNext() const;
172
177 virtual iterator* getPrev() const = 0;
178
183 virtual TYPE& get() = 0;
184
189 virtual TYPE get() const = 0;
190
195 virtual TYPE getElem() const;
196
201 virtual void set(const TYPE& data) = 0;
202
208 bool equal(const iterator* other) const;
209
215 bool equal(const iterator& other) const;
216
221 [[nodiscard]] virtual bool isValid() const = 0;
222
227 [[nodiscard]] std::string className() const override;
228
234 [[nodiscard]] std::string toString(bool enter) const override;
235
239 ~iterator() override = default;
240
241 // Friends for operator overloading
242 template<typename T>
243 friend iterator<T>* operator+(const iterator<T>& it, integer steps);
244
245 template<typename T>
246 friend iterator<T>* operator-(const iterator<T>& it, integer steps);
247};
248
258template <typename TYPE>
259class baseIterator : public iterator<TYPE> {
260public:
265 baseIterator* clone() const override = 0;
266
270 ~baseIterator() override = default;
271};
272
282template <typename T>
283auto operator+(const iterator<T>& it, integer steps) -> iterator<T>*;
284
294template <typename T>
295auto operator-(const iterator<T>& it, integer steps) -> iterator<T>*;
296
297} // namespace original
298 template<typename TYPE>
300 return this->get();
301 }
302
303 template<typename TYPE>
305 return this->get();
306 }
307
308 template<typename TYPE>
310 this->next();
311 }
312
313 template<typename TYPE>
314 auto original::iterator<TYPE>::operator++(int) const -> void {
315 this->operator++();
316 }
317
318 template<typename TYPE>
320 this->prev();
321 }
322
323 template<typename TYPE>
324 auto original::iterator<TYPE>::operator--(int) const -> void {
325 this->operator--();
326 }
327
328 template<typename TYPE>
330 return this->operator-(other);
331 }
332
333 template<typename TYPE>
335 return this->isValid();
336 }
337
338 template<typename TYPE>
339 auto original::iterator<TYPE>::atPrev(const iterator &other) const -> bool {
340 return this->atPrev(&other);
341 }
342
343 template<typename TYPE>
344 auto original::iterator<TYPE>::atNext(const iterator &other) const -> bool {
345 return this->atNext(&other);
346 }
347
348 template<typename TYPE>
350 if (!this->isValid()) throw outOfBoundError();
351 auto it = this->clone();
352 it->next();
353 return it;
354 }
355
356 template <typename TYPE>
358 {
359 return this->get();
360 }
361
362 template<typename TYPE>
363 auto original::iterator<TYPE>::equal(const iterator *other) const -> bool {
364 return this->equalPtr(other);
365 }
366
367 template<typename TYPE>
368 auto original::iterator<TYPE>::equal(const iterator &other) const -> bool {
369 return this->equal(&other);
370 }
371
372 template<typename TYPE>
373 auto original::iterator<TYPE>::className() const -> std::string {
374 return "iterator";
375 }
376
377 template<typename TYPE>
378 auto original::iterator<TYPE>::toString(const bool enter) const -> std::string {
379 std::stringstream ss;
380 ss << this->className() << "(";
381 if (this->isValid()) ss << formatString(this->get());
382 ss << ")";
383 if (enter) {
384 ss << "\n";
385 }
386 return ss.str();
387 }
388
389 template <typename TYPE>
391 {
392 auto* nit = it.clone();
393 nit->operator+=(steps);
394 return nit;
395 }
396
397 template <typename TYPE>
398 auto original::operator-(const iterator<TYPE>& it, integer steps) -> iterator<TYPE>*
399 {
400 auto* nit = it.clone();
401 nit->operator-=(steps);
402 return nit;
403 }
404
405#endif //ITERATOR_H
A base class for basic iterators.
Definition iterator.h:259
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:35
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 object.
void operator++(int postfix) const
Moves the iterator forward by one position (postfix).
Definition iterator.h:314
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:339
~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:363
void operator--(int postfix) const
Moves the iterator backward by one position (postfix).
Definition iterator.h:324
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:319
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:378
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:373
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:299
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:329
TYPE operator*() const
Dereferences the iterator to get a copy of the element.
Definition iterator.h:304
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:309
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:368
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:344
virtual iterator * getNext() const
Returns a new iterator pointing to the next element.
Definition iterator.h:349
virtual TYPE getElem() const
Returns a copy of the element.
Definition iterator.h:357
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:25
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:15
Interface for polymorphic string formatting and output.