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
58 template <typename TYPE>
59 class iterable {
60 public:
61
74 class iterAdaptor final : public iterator<TYPE> {
76
81 explicit iterAdaptor(baseIterator<TYPE>* it);
82
83 protected:
89 bool equalPtr(const iterator<TYPE>* other) const override;
90
95 iterAdaptor* clone() const override;
96
101 iterAdaptor* getPrev() const override;
102
107 iterAdaptor* getNext() const override;
108
109 public:
110 friend class iterable;
111
116 iterAdaptor(const iterAdaptor& other);
117
123 iterAdaptor& operator=(const iterAdaptor& other);
124
129 const iterator<TYPE>& getIt() const;
130
135 [[nodiscard]] bool hasNext() const override;
136
141 [[nodiscard]] bool hasPrev() const override;
142
148 bool atPrev(const iterator<TYPE>* other) const override;
149
155 bool atNext(const iterator<TYPE>* other) const override;
156
160 void next() const override;
161
165 void prev() const override;
166
171 void operator+=(integer steps) const override;
172
177 void operator-=(integer steps) const override;
178
184 integer operator-(const iterator<TYPE>& other) const override;
185
190 TYPE& get() override;
191
196 void set(const TYPE& data) override;
197
202 TYPE get() const override;
203
208 [[nodiscard]] bool isValid() const override;
209
214 [[nodiscard]] std::string className() const override;
215
221 [[nodiscard]] std::string toString(bool enter) const override;
222
227 ~iterAdaptor() override;
228 };
229
230 virtual ~iterable() = default;
231
248 iterAdaptor begin();
249
266 iterAdaptor end();
267
273 iterAdaptor begin() const;
274
280 iterAdaptor end() const;
281
292 iterAdaptor first();
293
303 iterAdaptor last();
304
310 iterAdaptor first() const;
311
317 iterAdaptor last() const;
318
335 virtual baseIterator<TYPE>* begins() const = 0;
336
353 virtual baseIterator<TYPE>* ends() const = 0;
354
360 template<typename Callback = transform<TYPE>>
362 void forEach(Callback operation = Callback{});
363
369 template<typename Callback = transform<TYPE>>
371 void forEach(const Callback& operation = Callback{}) const;
372 };
373}
374
375 template <typename TYPE>
376 original::iterable<TYPE>::iterAdaptor::iterAdaptor(baseIterator<TYPE>* it) : it_(it) {}
377
378 template <typename TYPE>
380 {
381 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
382 if (other_it == nullptr)
383 return this->it_->equal(other);
384 return this->it_->equal(other_it->it_);
385 }
386
387 template <typename TYPE>
389 {
390 return new iterAdaptor(*this);
391 }
392
393 template <typename TYPE>
395 {
396 auto* it = this->clone();
397 it->next();
398 return it;
399 }
400
401 template <typename TYPE>
403 {
404 auto* it = this->clone();
405 it->prev();
406 return it;
407 }
408
409 template <typename TYPE>
410 original::iterable<TYPE>::iterAdaptor::iterAdaptor(const iterAdaptor& other) : iterAdaptor(nullptr)
411 {
412 this->operator=(other);
413 }
414
415 template <typename TYPE>
416 auto original::iterable<TYPE>::iterAdaptor::operator=(const iterAdaptor& other) -> iterAdaptor&
417 {
418 if (this == &other) return *this;
419
420 delete this->it_;
421 this->it_ = other.it_->clone();
422 return *this;
423 }
424
425 template <typename TYPE>
427 {
428 return *this->it_;
429 }
430
431 template <typename TYPE>
433 {
434 return this->it_->hasNext();
435 }
436
437 template <typename TYPE>
439 {
440 return this->it_->hasPrev();
441 }
442
443 template <typename TYPE>
445 {
446 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
447 if (other_it == nullptr)
448 return this->it_->atPrev(other);
449 return this->it_->atPrev(other_it->it_);
450 }
451
452 template <typename TYPE>
454 {
455 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
456 if (other_it == nullptr)
457 return this->it_->atNext(other);
458 return this->it_->atNext(other_it->it_);
459 }
460
461 template <typename TYPE>
463 {
464 this->it_->next();
465 }
466
467 template <typename TYPE>
469 {
470 this->it_->prev();
471 }
472
473 template <typename TYPE>
475 {
476 this->it_->operator+=(steps);
477 }
478
479 template <typename TYPE>
481 {
482 this->it_->operator-=(steps);
483 }
484
485 template <typename TYPE>
487 {
488 auto* other_it = dynamic_cast<const iterAdaptor*>(&other);
489 if (other_it == nullptr)
490 return this->it_->operator-(other);
491 return this->it_->operator-(*other_it->it_);
492 }
493
494 template <typename TYPE>
496 {
497 return this->it_->get();
498 }
499
500 template <typename TYPE>
501 auto original::iterable<TYPE>::iterAdaptor::set(const TYPE& data) -> void
502 {
503 this->it_->set(data);
504 }
505
506 template<typename TYPE>
508 {
509 return this->it_->getElem();
510 }
511
512 template<typename TYPE>
514 return this->it_->isValid();
515 }
516
517 template<typename TYPE>
519 return "iterAdaptor";
520 }
521
522 template<typename TYPE>
523 auto original::iterable<TYPE>::iterAdaptor::toString(const bool enter) const -> std::string {
524 std::stringstream ss;
525 ss << this->className();
526 ss << "(" << *this->it_ << ")";
527 if (enter) ss << "\n";
528 return ss.str();
529 }
530
531 template<typename TYPE>
535
536 template <typename TYPE>
538 return iterAdaptor(this->begins());
539 }
540
541 template <typename TYPE>
543 auto* it = this->ends();
544 it->next();
545 return iterAdaptor(it);
546 }
547
548 template <typename TYPE>
550 return iterAdaptor(this->begins());
551 }
552
553 template <typename TYPE>
555 auto* it = this->ends();
556 it->next();
557 return iterAdaptor(it);
558 }
559
560 template <typename TYPE>
562 {
563 return this->begin();
564 }
565
566 template <typename TYPE>
568 {
569 return iterAdaptor(this->ends());
570 }
571
572 template <typename TYPE>
574 {
575 return this->begin();
576 }
577
578 template <typename TYPE>
580 {
581 return iterAdaptor(this->ends());
582 }
583
584 template <typename TYPE>
585 template<typename Callback>
587 auto original::iterable<TYPE>::forEach(Callback operation) -> void
588 {
589 for (auto it = this->first(); it.isValid(); it.next()) {
590 operation(it.get());
591 }
592 }
593
594 template<typename TYPE>
595 template<typename Callback>
596 requires original::Operation<Callback, TYPE>
597 auto original::iterable<TYPE>::forEach(const Callback &operation) const -> void {
598 for (auto it = this->first(); it.isValid(); it.next()) {
599 operation(it.getElem());
600 }
601 }
602
603#endif //ITERABLE_H
A base class for basic iterators.
Definition iterator.h:296
RAII wrapper for base iterators that provides standard iteration interface.
Definition iterable.h:74
const iterator< TYPE > & getIt() const
Gets the underlying iterator.
Definition iterable.h:426
TYPE & get() override
Gets the value of the element the iterator is pointing to.
Definition iterable.h:495
void operator-=(integer steps) const override
Moves the iterator backward by a specified number of steps.
Definition iterable.h:480
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:453
iterAdaptor * clone() const override
Creates a copy of the current iterator.
Definition iterable.h:388
iterAdaptor * getPrev() const override
Gets the previous iterator.
Definition iterable.h:394
void set(const TYPE &data) override
Sets the value of the element the iterator is pointing to.
Definition iterable.h:501
integer operator-(const iterator< TYPE > &other) const override
Calculates the distance between this iterator and another iterator.
Definition iterable.h:486
bool hasPrev() const override
Checks if there is a previous element.
Definition iterable.h:438
void prev() const override
Moves the iterator to the previous element.
Definition iterable.h:468
void operator+=(integer steps) const override
Advances the iterator by a specified number of steps.
Definition iterable.h:474
std::string toString(bool enter) const override
Converts the iterator to a string representation.
Definition iterable.h:523
iterAdaptor * getNext() const override
Gets the next iterator.
Definition iterable.h:402
bool equalPtr(const iterator< TYPE > *other) const override
Compares the current iterator with another iterator.
Definition iterable.h:379
void next() const override
Moves the iterator to the next element.
Definition iterable.h:462
~iterAdaptor() override
Destructor for iterAdaptor.
Definition iterable.h:532
std::string className() const override
Returns the class name.
Definition iterable.h:518
bool isValid() const override
Checks if the iterator is pointing to a valid element.
Definition iterable.h:513
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:444
bool hasNext() const override
Checks if there is a next element.
Definition iterable.h:432
iterAdaptor & operator=(const iterAdaptor &other)
Copy assignment operator for the iterAdaptor.
Definition iterable.h:416
A base class for iterable containers that support multiple iteration patterns.
Definition iterable.h:59
iterAdaptor end() const
Returns a const iterator adapter pointing to the end sentinel of the container.
Definition iterable.h:554
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 adapter pointing to the first element.
Definition iterable.h:561
iterAdaptor end()
Returns an iterator adapter pointing to the end sentinel of the container.
Definition iterable.h:542
virtual baseIterator< TYPE > * begins() const =0
Returns the iterator to the beginning of the container.
iterAdaptor begin() const
Returns a const iterator adapter pointing to the beginning of the container.
Definition iterable.h:549
iterAdaptor last() const
Returns a const iterator adapter pointing to the last element.
Definition iterable.h:579
iterAdaptor begin()
Returns an iterator adapter pointing to the beginning of the container.
Definition iterable.h:537
void forEach(Callback operation=Callback{})
Applies a given operation to each element in the iterable container.
iterAdaptor last()
Returns an iterator adapter pointing to the last element.
Definition iterable.h:567
iterAdaptor first() const
Returns a const iterator adapter pointing to the first element.
Definition iterable.h:573
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:37
virtual bool hasNext() const =0
Checks if there is a next element.
Constraint for mutating operations.
Definition types.h:133
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:35
Transformation classes for various operations on elements.
Core type system foundations and concept definitions.