ORIGINAL
Loading...
Searching...
No Matches
iterable.h
1#ifndef ITERABLE_H
2#define ITERABLE_H
3
4#include "transform.h"
5#include "types.h"
6#include "iterator.h"
7
8namespace original{
9 template <typename TYPE>
10 class iterable{
11 public:
12 class iterAdaptor final : public iterator<TYPE>{
14 explicit iterAdaptor(baseIterator<TYPE>* it);
15 protected:
16 bool equalPtr(const iterator<TYPE>* other) const override;
17 iterAdaptor* clone() const override;
18 iterAdaptor* getPrev() const override;
19 iterAdaptor* getNext() const override;
20 public:
21 friend class iterable;
22 iterAdaptor(const iterAdaptor& other);
23 iterAdaptor& operator=(const iterAdaptor& other);
24 const iterator<TYPE>& getIt() const;
25 [[nodiscard]] bool hasNext() const override;
26 [[nodiscard]] bool hasPrev() const override;
27 bool atPrev(const iterator<TYPE>* other) const override;
28 bool atNext(const iterator<TYPE>* other) const override;
29 void next() const override;
30 void prev() const override;
31 void operator+=(int64_t steps) const override;
32 void operator-=(int64_t steps) const override;
33 int64_t operator-(const iterator<TYPE>& other) const override;
34 TYPE& get() override;
35 void set(const TYPE& data) override;
36 TYPE get() const override;
37 [[nodiscard]] bool isValid() const override;
38 [[nodiscard]] std::string className() const override;
39 [[nodiscard]] std::string toString(bool enter) const override;
40 ~iterAdaptor() override;
41 };
42
43 virtual ~iterable() = default;
44 virtual baseIterator<TYPE>* begins() const = 0;
45 virtual baseIterator<TYPE>* ends() const = 0;
46
47 iterAdaptor begin();
48 iterAdaptor end();
49 iterAdaptor begin() const;
50 iterAdaptor end() const;
51
52 iterAdaptor first();
53 iterAdaptor last();
54 iterAdaptor first() const;
55 iterAdaptor last() const;
56
57 template<typename Callback = transform<TYPE>>
59 void forEach(Callback operation = Callback{});
60
61 template<typename Callback = transform<TYPE>>
63 void forEach(const Callback& operation = Callback{}) const;
64 };
65}
66
67 template <typename TYPE>
68 original::iterable<TYPE>::iterAdaptor::iterAdaptor(baseIterator<TYPE>* it) : it_(it) {}
69
70 template <typename TYPE>
71 auto original::iterable<TYPE>::iterAdaptor::equalPtr(const iterator<TYPE>* other) const -> bool
72 {
73 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
74 if (other_it == nullptr)
75 return this->it_->equal(other);
76 return this->it_->equal(other_it->it_);
77 }
78
79 template <typename TYPE>
80 auto original::iterable<TYPE>::iterAdaptor::clone() const -> iterAdaptor*
81 {
82 return new iterAdaptor(*this);
83 }
84
85 template <typename TYPE>
86 auto original::iterable<TYPE>::iterAdaptor::getPrev() const -> iterAdaptor*
87 {
88 auto* it = this->clone();
89 it->next();
90 return it;
91 }
92
93 template <typename TYPE>
94 auto original::iterable<TYPE>::iterAdaptor::getNext() const -> iterAdaptor*
95 {
96 auto* it = this->clone();
97 it->prev();
98 return it;
99 }
100
101 template <typename TYPE>
102 original::iterable<TYPE>::iterAdaptor::iterAdaptor(const iterAdaptor& other) : iterAdaptor(nullptr)
103 {
104 this->operator=(other);
105 }
106
107 template <typename TYPE>
108 auto original::iterable<TYPE>::iterAdaptor::operator=(const iterAdaptor& other) -> iterAdaptor&
109 {
110 if (this == &other) return *this;
111
112 delete this->it_;
113 this->it_ = other.it_->clone();
114 return *this;
115 }
116
117 template <typename TYPE>
118 auto original::iterable<TYPE>::iterAdaptor::getIt() const -> const iterator<TYPE>&
119 {
120 return *this->it_;
121 }
122
123 template <typename TYPE>
124 auto original::iterable<TYPE>::iterAdaptor::hasNext() const -> bool
125 {
126 return this->it_->hasNext();
127 }
128
129 template <typename TYPE>
130 auto original::iterable<TYPE>::iterAdaptor::hasPrev() const -> bool
131 {
132 return this->it_->hasPrev();
133 }
134
135 template <typename TYPE>
136 auto original::iterable<TYPE>::iterAdaptor::atPrev(const iterator<TYPE>* other) const -> bool
137 {
138 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
139 if (other_it == nullptr)
140 return this->it_->atPrev(other);
141 return this->it_->atPrev(other_it->it_);
142 }
143
144 template <typename TYPE>
145 auto original::iterable<TYPE>::iterAdaptor::atNext(const iterator<TYPE>* other) const -> bool
146 {
147 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
148 if (other_it == nullptr)
149 return this->it_->atNext(other);
150 return this->it_->atNext(other_it->it_);
151 }
152
153 template <typename TYPE>
154 auto original::iterable<TYPE>::iterAdaptor::next() const -> void
155 {
156 this->it_->next();
157 }
158
159 template <typename TYPE>
160 auto original::iterable<TYPE>::iterAdaptor::prev() const -> void
161 {
162 this->it_->prev();
163 }
164
165 template <typename TYPE>
166 auto original::iterable<TYPE>::iterAdaptor::operator+=(int64_t steps) const -> void
167 {
168 this->it_->operator+=(steps);
169 }
170
171 template <typename TYPE>
172 auto original::iterable<TYPE>::iterAdaptor::operator-=(int64_t steps) const -> void
173 {
174 this->it_->operator-=(steps);
175 }
176
177 template <typename TYPE>
178 auto original::iterable<TYPE>::iterAdaptor::operator-(const iterator<TYPE>& other) const -> int64_t
179 {
180 auto* other_it = dynamic_cast<const iterAdaptor*>(&other);
181 if (other_it == nullptr)
182 return this->it_->operator-(other);
183 return this->it_->operator-(*other_it->it_);
184 }
185
186 template <typename TYPE>
187 auto original::iterable<TYPE>::iterAdaptor::get() -> TYPE&
188 {
189 return this->it_->get();
190 }
191
192 template <typename TYPE>
193 auto original::iterable<TYPE>::iterAdaptor::set(const TYPE& data) -> void
194 {
195 this->it_->set(data);
196 }
197
198 template<typename TYPE>
199 auto original::iterable<TYPE>::iterAdaptor::get() const -> TYPE
200 {
201 return this->it_->getElem();
202 }
203
204 template<typename TYPE>
205 bool original::iterable<TYPE>::iterAdaptor::isValid() const {
206 return this->it_->isValid();
207 }
208
209 template<typename TYPE>
210 auto original::iterable<TYPE>::iterAdaptor::className() const -> std::string {
211 return "iterAdaptor";
212 }
213
214 template<typename TYPE>
215 auto original::iterable<TYPE>::iterAdaptor::toString(const bool enter) const -> std::string {
216 std::stringstream ss;
217 ss << this->className();
218 ss << "(" << *this->it_ << ")";
219 if (enter) ss << "\n";
220 return ss.str();
221 }
222
223 template<typename TYPE>
224 original::iterable<TYPE>::iterAdaptor::~iterAdaptor() {
225 delete this->it_;
226 }
227
228 template <typename TYPE>
229 auto original::iterable<TYPE>::begin() -> iterAdaptor {
230 return iterAdaptor(this->begins());
231 }
232
233 template <typename TYPE>
234 auto original::iterable<TYPE>::end() -> iterAdaptor {
235 auto* it = this->ends();
236 it->next();
237 return iterAdaptor(it);
238 }
239
240 template <typename TYPE>
241 auto original::iterable<TYPE>::begin() const -> iterAdaptor {
242 return iterAdaptor(this->begins());
243 }
244
245 template <typename TYPE>
246 auto original::iterable<TYPE>::end() const -> iterAdaptor{
247 auto* it = this->ends();
248 it->next();
249 return iterAdaptor(it);
250 }
251
252 template <typename TYPE>
253 auto original::iterable<TYPE>::first() -> iterAdaptor
254 {
255 return this->begin();
256 }
257
258 template <typename TYPE>
259 auto original::iterable<TYPE>::last() -> iterAdaptor
260 {
261 return iterAdaptor(this->ends());
262 }
263
264 template <typename TYPE>
265 auto original::iterable<TYPE>::first() const -> iterAdaptor
266 {
267 return this->begin();
268 }
269
270 template <typename TYPE>
271 auto original::iterable<TYPE>::last() const -> iterAdaptor
272 {
273 return iterAdaptor(this->ends());
274 }
275
276 template <typename TYPE>
277 template<typename Callback>
278 requires original::Operation<Callback, TYPE>
279 auto original::iterable<TYPE>::forEach(Callback operation) -> void
280 {
281 for (auto* it = this->begins(); it->isValid(); it->next()) {
282 operation(it->get());
283 }
284 }
285
286 template<typename TYPE>
287 template<typename Callback>
288 requires original::Operation<Callback, TYPE>
289 auto original::iterable<TYPE>::forEach(const Callback &operation) const -> void {
290 for (auto* it = this->begins(); it->isValid(); it->next()) {
291 operation(it->getElem());
292 }
293 }
294
295#endif //ITERABLE_H
Definition iterator.h:56
Definition iterable.h:12
Definition iterable.h:10
Definition iterator.h:11
Definition types.h:31