ORIGINAL
Loading...
Searching...
No Matches
iterator.h
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 template<typename TYPE>
11 class iterator : public printable, public cloneable, public comparable<iterator<TYPE>> {
12 protected:
13 virtual bool equalPtr(const iterator* other) const = 0;
14 public:
15 TYPE& operator*();
16 TYPE operator*() const;
17 void operator++() const;
18 void operator++(int) const;
19 void operator--() const;
20 void operator--(int) const;
21 virtual void operator+=(int64_t steps) const = 0;
22 virtual void operator-=(int64_t steps) const = 0;
23 int compareTo(const iterator &other) const override;
24 virtual int64_t operator-(const iterator& other) const = 0;
25 iterator* clone() const override = 0;
26 explicit operator bool() const;
27 [[nodiscard]] virtual bool hasNext() const = 0;
28 [[nodiscard]] virtual bool hasPrev() const = 0;
29 virtual bool atPrev(const iterator* other) const = 0;
30 virtual bool atNext(const iterator* other) const = 0;
31 bool atPrev(const iterator& other) const;
32 bool atNext(const iterator& other) const;
33 virtual void next() const = 0;
34 virtual void prev() const = 0;
35 virtual iterator* getNext() const;
36 virtual iterator* getPrev() const = 0;
37 virtual TYPE& get() = 0;
38 virtual TYPE get() const = 0;
39 virtual TYPE getElem() const;
40 virtual void set(const TYPE& data) = 0;
41 bool equal(const iterator* other) const;
42 bool equal(const iterator& other) const;
43 [[nodiscard]] virtual bool isValid() const = 0;
44 [[nodiscard]] std::string className() const override;
45 [[nodiscard]] std::string toString(bool enter) const override;
46 ~iterator() override = default;
47
48 template<typename T>
49 friend iterator<T>* operator+(const iterator<T>& it, int64_t steps);
50 template<typename T>
51 friend iterator<T>* operator-(const iterator<T>& it, int64_t steps);
52 };
53
54 template <typename TYPE>
55 class baseIterator : public iterator<TYPE>
56 {
57 public:
58 baseIterator* clone() const override = 0;
59 ~baseIterator() override = default;
60 };
61
62 template<typename TYPE>
63 iterator<TYPE>* operator+(const iterator<TYPE>& it, int64_t steps);
64 template<typename TYPE>
65 iterator<TYPE>* operator-(const iterator<TYPE>& it, int64_t steps);
66}
67
68 template<typename TYPE>
69 auto original::iterator<TYPE>::operator*() -> TYPE& {
70 return this->get();
71 }
72
73 template<typename TYPE>
74 auto original::iterator<TYPE>::operator*() const -> TYPE {
75 return this->get();
76 }
77
78 template<typename TYPE>
79 auto original::iterator<TYPE>::operator++() const -> void {
80 this->next();
81 }
82
83 template<typename TYPE>
84 auto original::iterator<TYPE>::operator++(int) const -> void {
85 this->operator++();
86 }
87
88 template<typename TYPE>
89 auto original::iterator<TYPE>::operator--() const -> void {
90 this->prev();
91 }
92
93 template<typename TYPE>
94 auto original::iterator<TYPE>::operator--(int) const -> void {
95 this->operator--();
96 }
97
98 template<typename TYPE>
99 auto original::iterator<TYPE>::compareTo(const iterator &other) const -> int {
100 return this->operator-(other);
101 }
102
103 template<typename TYPE>
105 return this->isValid();
106 }
107
108 template<typename TYPE>
109 auto original::iterator<TYPE>::atPrev(const iterator &other) const -> bool {
110 return this->atPrev(&other);
111 }
112
113 template<typename TYPE>
114 auto original::iterator<TYPE>::atNext(const iterator &other) const -> bool {
115 return this->atNext(&other);
116 }
117
118 template<typename TYPE>
119 auto original::iterator<TYPE>::getNext() const -> iterator* {
120 if (!this->isValid()) throw outOfBoundError();
121 auto it = this->clone();
122 it->next();
123 return it;
124 }
125
126 template <typename TYPE>
127 auto original::iterator<TYPE>::getElem() const -> TYPE
128 {
129 return this->get();
130 }
131
132 template<typename TYPE>
133 auto original::iterator<TYPE>::equal(const iterator *other) const -> bool {
134 return this->equalPtr(other);
135 }
136
137 template<typename TYPE>
138 auto original::iterator<TYPE>::equal(const iterator &other) const -> bool {
139 return this->equal(&other);
140 }
141
142 template<typename TYPE>
143 auto original::iterator<TYPE>::className() const -> std::string {
144 return "iterator";
145 }
146
147 template<typename TYPE>
148 auto original::iterator<TYPE>::toString(const bool enter) const -> std::string {
149 std::stringstream ss;
150 ss << this->className() << "(";
151 if (this->isValid()) ss << formatString(this->get());
152 ss << ")";
153 if (enter) {
154 ss << "\n";
155 }
156 return ss.str();
157 }
158
159 template <typename TYPE>
160 auto original::operator+(const iterator<TYPE>& it, int64_t steps) -> iterator<TYPE>*
161 {
162 auto* nit = it.clone();
163 nit->operator+=(steps);
164 return nit;
165 }
166
167 template <typename TYPE>
168 auto original::operator-(const iterator<TYPE>& it, int64_t steps) -> iterator<TYPE>*
169 {
170 auto* nit = it.clone();
171 nit->operator-=(steps);
172 return nit;
173 }
174
175#endif //ITERATOR_H
Definition iterator.h:56
Definition comparable.h:6
Definition iterator.h:11
Definition printable.h:9