ORIGINAL
Loading...
Searching...
No Matches
iterable.h
Go to the documentation of this file.
1#ifndef ITERABLE_H
2#define ITERABLE_H
3
4#include "transform.h"
5#include "types.h"
6#include "iterator.h"
7
20
21namespace original {
22
31 template <typename TYPE>
32 class iterable {
33 public:
34
42 class iterAdaptor final : public iterator<TYPE> {
44
49 explicit iterAdaptor(baseIterator<TYPE>* it);
50
51 protected:
57 bool equalPtr(const iterator<TYPE>* other) const override;
58
63 iterAdaptor* clone() const override;
64
69 iterAdaptor* getPrev() const override;
70
75 iterAdaptor* getNext() const override;
76
77 public:
78 friend class iterable;
79
84 iterAdaptor(const iterAdaptor& other);
85
91 iterAdaptor& operator=(const iterAdaptor& other);
92
97 const iterator<TYPE>& getIt() const;
98
103 [[nodiscard]] bool hasNext() const override;
104
109 [[nodiscard]] bool hasPrev() const override;
110
116 bool atPrev(const iterator<TYPE>* other) const override;
117
123 bool atNext(const iterator<TYPE>* other) const override;
124
128 void next() const override;
129
133 void prev() const override;
134
139 void operator+=(integer steps) const override;
140
145 void operator-=(integer steps) const override;
146
152 integer operator-(const iterator<TYPE>& other) const override;
153
158 TYPE& get() override;
159
164 void set(const TYPE& data) override;
165
170 TYPE get() const override;
171
176 [[nodiscard]] bool isValid() const override;
177
182 [[nodiscard]] std::string className() const override;
183
189 [[nodiscard]] std::string toString(bool enter) const override;
190
195 ~iterAdaptor() override;
196 };
197
198 virtual ~iterable() = default;
199
205 iterAdaptor begin();
206
212 iterAdaptor end();
213
219 iterAdaptor begin() const;
220
226 iterAdaptor end() const;
227
232 iterAdaptor first();
233
238 iterAdaptor last();
239
244 iterAdaptor first() const;
245
250 iterAdaptor last() const;
251
257 virtual baseIterator<TYPE>* begins() const = 0;
258
264 virtual baseIterator<TYPE>* ends() const = 0;
265
271 template<typename Callback = transform<TYPE>>
273 void forEach(Callback operation = Callback{});
274
280 template<typename Callback = transform<TYPE>>
282 void forEach(const Callback& operation = Callback{}) const;
283 };
284}
285
286 template <typename TYPE>
287 original::iterable<TYPE>::iterAdaptor::iterAdaptor(baseIterator<TYPE>* it) : it_(it) {}
288
289 template <typename TYPE>
291 {
292 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
293 if (other_it == nullptr)
294 return this->it_->equal(other);
295 return this->it_->equal(other_it->it_);
296 }
297
298 template <typename TYPE>
300 {
301 return new iterAdaptor(*this);
302 }
303
304 template <typename TYPE>
306 {
307 auto* it = this->clone();
308 it->next();
309 return it;
310 }
311
312 template <typename TYPE>
314 {
315 auto* it = this->clone();
316 it->prev();
317 return it;
318 }
319
320 template <typename TYPE>
321 original::iterable<TYPE>::iterAdaptor::iterAdaptor(const iterAdaptor& other) : iterAdaptor(nullptr)
322 {
323 this->operator=(other);
324 }
325
326 template <typename TYPE>
327 auto original::iterable<TYPE>::iterAdaptor::operator=(const iterAdaptor& other) -> iterAdaptor&
328 {
329 if (this == &other) return *this;
330
331 delete this->it_;
332 this->it_ = other.it_->clone();
333 return *this;
334 }
335
336 template <typename TYPE>
338 {
339 return *this->it_;
340 }
341
342 template <typename TYPE>
344 {
345 return this->it_->hasNext();
346 }
347
348 template <typename TYPE>
350 {
351 return this->it_->hasPrev();
352 }
353
354 template <typename TYPE>
356 {
357 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
358 if (other_it == nullptr)
359 return this->it_->atPrev(other);
360 return this->it_->atPrev(other_it->it_);
361 }
362
363 template <typename TYPE>
365 {
366 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
367 if (other_it == nullptr)
368 return this->it_->atNext(other);
369 return this->it_->atNext(other_it->it_);
370 }
371
372 template <typename TYPE>
374 {
375 this->it_->next();
376 }
377
378 template <typename TYPE>
380 {
381 this->it_->prev();
382 }
383
384 template <typename TYPE>
386 {
387 this->it_->operator+=(steps);
388 }
389
390 template <typename TYPE>
392 {
393 this->it_->operator-=(steps);
394 }
395
396 template <typename TYPE>
398 {
399 auto* other_it = dynamic_cast<const iterAdaptor*>(&other);
400 if (other_it == nullptr)
401 return this->it_->operator-(other);
402 return this->it_->operator-(*other_it->it_);
403 }
404
405 template <typename TYPE>
407 {
408 return this->it_->get();
409 }
410
411 template <typename TYPE>
412 auto original::iterable<TYPE>::iterAdaptor::set(const TYPE& data) -> void
413 {
414 this->it_->set(data);
415 }
416
417 template<typename TYPE>
419 {
420 return this->it_->getElem();
421 }
422
423 template<typename TYPE>
425 return this->it_->isValid();
426 }
427
428 template<typename TYPE>
430 return "iterAdaptor";
431 }
432
433 template<typename TYPE>
434 auto original::iterable<TYPE>::iterAdaptor::toString(const bool enter) const -> std::string {
435 std::stringstream ss;
436 ss << this->className();
437 ss << "(" << *this->it_ << ")";
438 if (enter) ss << "\n";
439 return ss.str();
440 }
441
442 template<typename TYPE>
446
447 template <typename TYPE>
449 return iterAdaptor(this->begins());
450 }
451
452 template <typename TYPE>
454 auto* it = this->ends();
455 it->next();
456 return iterAdaptor(it);
457 }
458
459 template <typename TYPE>
461 return iterAdaptor(this->begins());
462 }
463
464 template <typename TYPE>
466 auto* it = this->ends();
467 it->next();
468 return iterAdaptor(it);
469 }
470
471 template <typename TYPE>
473 {
474 return this->begin();
475 }
476
477 template <typename TYPE>
479 {
480 return iterAdaptor(this->ends());
481 }
482
483 template <typename TYPE>
485 {
486 return this->begin();
487 }
488
489 template <typename TYPE>
491 {
492 return iterAdaptor(this->ends());
493 }
494
495 template <typename TYPE>
496 template<typename Callback>
498 auto original::iterable<TYPE>::forEach(Callback operation) -> void
499 {
500 for (auto it = this->first(); it.isValid(); it.next()) {
501 operation(it.get());
502 }
503 }
504
505 template<typename TYPE>
506 template<typename Callback>
507 requires original::Operation<Callback, TYPE>
508 auto original::iterable<TYPE>::forEach(const Callback &operation) const -> void {
509 for (auto it = this->first(); it.isValid(); it.next()) {
510 operation(it.getElem());
511 }
512 }
513
514#endif //ITERABLE_H
A base class for basic iterators.
Definition iterator.h:259
An iterator adapter for the iterable container.
Definition iterable.h:42
const iterator< TYPE > & getIt() const
Gets the underlying iterator.
Definition iterable.h:337
TYPE & get() override
Gets the value of the element the iterator is pointing to.
Definition iterable.h:406
void operator-=(integer steps) const override
Moves the iterator backward by a specified number of steps.
Definition iterable.h:391
bool atNext(const iterator< TYPE > *other) const override
Checks if the current iterator is at the next element relative to another iterator.
Definition iterable.h:364
iterAdaptor * clone() const override
Creates a copy of the current iterator.
Definition iterable.h:299
iterAdaptor * getPrev() const override
Gets the previous iterator.
Definition iterable.h:305
void set(const TYPE &data) override
Sets the value of the element the iterator is pointing to.
Definition iterable.h:412
integer operator-(const iterator< TYPE > &other) const override
Calculates the distance between this iterator and another iterator.
Definition iterable.h:397
bool hasPrev() const override
Checks if there is a previous element.
Definition iterable.h:349
void prev() const override
Moves the iterator to the previous element.
Definition iterable.h:379
void operator+=(integer steps) const override
Advances the iterator by a specified number of steps.
Definition iterable.h:385
std::string toString(bool enter) const override
Converts the iterator to a string representation.
Definition iterable.h:434
iterAdaptor * getNext() const override
Gets the next iterator.
Definition iterable.h:313
bool equalPtr(const iterator< TYPE > *other) const override
Compares the current iterator with another iterator.
Definition iterable.h:290
void next() const override
Moves the iterator to the next element.
Definition iterable.h:373
~iterAdaptor() override
Destructor for iterAdaptor.
Definition iterable.h:443
std::string className() const override
Returns the class name.
Definition iterable.h:429
bool isValid() const override
Checks if the iterator is pointing to a valid element.
Definition iterable.h:424
bool atPrev(const iterator< TYPE > *other) const override
Checks if the current iterator is at the previous element relative to another iterator.
Definition iterable.h:355
bool hasNext() const override
Checks if there is a next element.
Definition iterable.h:343
iterAdaptor & operator=(const iterAdaptor &other)
Copy assignment operator for the iterAdaptor.
Definition iterable.h:327
A base class for iterable containers that support iterators.
Definition iterable.h:32
iterAdaptor end() const
Returns a constant iterator pointing to the end of the iterable container.
Definition iterable.h:465
void forEach(const Callback &operation=Callback{}) const
Applies a given operation to each element in the iterable container (const version).
iterAdaptor first()
Returns an iterator pointing to the first element.
Definition iterable.h:472
iterAdaptor end()
Returns an iterator pointing to the end of the iterable container.
Definition iterable.h:453
virtual baseIterator< TYPE > * begins() const =0
Returns the iterator to the beginning of the container.
iterAdaptor begin() const
Returns a constant iterator pointing to the beginning of the iterable container.
Definition iterable.h:460
iterAdaptor last() const
Returns a constant iterator pointing to the last element.
Definition iterable.h:490
iterAdaptor begin()
Returns an iterator pointing to the beginning of the iterable container.
Definition iterable.h:448
void forEach(Callback operation=Callback{})
Applies a given operation to each element in the iterable container.
iterAdaptor last()
Returns an iterator pointing to the last element.
Definition iterable.h:478
iterAdaptor first() const
Returns a constant iterator pointing to the first element.
Definition iterable.h:484
virtual baseIterator< TYPE > * ends() const =0
Returns the iterator to the end of the container.
Base iterator interface that supports common operations for iteration.
Definition iterator.h:35
virtual bool hasNext() const =0
Checks if there is a next element.
Constraint for mutating operations.
Definition types.h:117
Defines the iterator class for traversing and manipulating container elements.
Main namespace for the project Original.
Definition algorithms.h:21
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:15
Transformation classes for various operations on elements.
Type system foundations and concept definitions.