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 "coroutines.h"
5#include "transform.h"
6#include "types.h"
7#include "iterator.h"
8
25namespace original {
26
69 template <typename TYPE>
70 class iterable {
71 public:
72
86 class iterAdaptor final : public iterator<TYPE> {
88
96
97 protected:
104 bool equalPtr(const iterator<TYPE>* other) const override;
105
111 iterAdaptor* clone() const override;
112
119 iterAdaptor* getPrev() const override;
120
127 iterAdaptor* getNext() const override;
128
129 public:
130 friend class iterable;
131
137 iterAdaptor(const iterAdaptor& other);
138
145 iterAdaptor& operator=(const iterAdaptor& other);
146
151 const iterator<TYPE>& getIt() const;
152
158 [[nodiscard]] bool hasNext() const override;
159
165 [[nodiscard]] bool hasPrev() const override;
166
173 bool atPrev(const iterator<TYPE>* other) const override;
174
181 bool atNext(const iterator<TYPE>* other) const override;
182
188 void next() const override;
189
195 void prev() const override;
196
203 void operator+=(integer steps) const override;
204
211 void operator-=(integer steps) const override;
212
220 integer operator-(const iterator<TYPE>& other) const override;
221
227 TYPE& get() override;
228
234 void set(const TYPE& data) override;
235
241 TYPE get() const override;
242
248 [[nodiscard]] bool isValid() const override;
249
254 [[nodiscard]] std::string className() const override;
255
262 [[nodiscard]] std::string toString(bool enter) const override;
263
268 ~iterAdaptor() override;
269 };
270
271 virtual ~iterable() = default;
272
289 iterAdaptor begin();
290
307 iterAdaptor end();
308
314 iterAdaptor begin() const;
315
321 iterAdaptor end() const;
322
333 iterAdaptor first();
334
344 iterAdaptor last();
345
351 iterAdaptor first() const;
352
358 iterAdaptor last() const;
359
376 virtual baseIterator<TYPE>* begins() const = 0;
377
394 virtual baseIterator<TYPE>* ends() const = 0;
395
408 template<typename Callback = transform<TYPE>>
411
424 template<typename Callback = transform<TYPE>>
426 void forEach(const Callback& operation = Callback{}) const;
427
428 using T = std::remove_const_t<TYPE>;
461 };
462}
463
464 template <typename TYPE>
465 original::iterable<TYPE>::iterAdaptor::iterAdaptor(baseIterator<TYPE>* it) : it_(it) {}
466
467 template <typename TYPE>
469 {
470 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
471 if (other_it == nullptr)
472 return this->it_->equal(other);
473 return this->it_->equal(other_it->it_);
474 }
475
476 template <typename TYPE>
478 {
479 return new iterAdaptor(*this);
480 }
481
482 template <typename TYPE>
484 {
485 auto* it = this->clone();
486 it->next();
487 return it;
488 }
489
490 template <typename TYPE>
492 {
493 auto* it = this->clone();
494 it->prev();
495 return it;
496 }
497
498 template <typename TYPE>
503
504 template <typename TYPE>
506 {
507 if (this == &other) return *this;
508
509 delete this->it_;
510 this->it_ = other.it_->clone();
511 return *this;
512 }
513
514 template <typename TYPE>
516 {
517 return *this->it_;
518 }
519
520 template <typename TYPE>
522 {
523 return this->it_->hasNext();
524 }
525
526 template <typename TYPE>
528 {
529 return this->it_->hasPrev();
530 }
531
532 template <typename TYPE>
534 {
535 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
536 if (other_it == nullptr)
537 return this->it_->atPrev(other);
538 return this->it_->atPrev(other_it->it_);
539 }
540
541 template <typename TYPE>
543 {
544 auto* other_it = dynamic_cast<const iterAdaptor*>(other);
545 if (other_it == nullptr)
546 return this->it_->atNext(other);
547 return this->it_->atNext(other_it->it_);
548 }
549
550 template <typename TYPE>
552 {
553 this->it_->next();
554 }
555
556 template <typename TYPE>
558 {
559 this->it_->prev();
560 }
561
562 template <typename TYPE>
564 {
565 this->it_->operator+=(steps);
566 }
567
568 template <typename TYPE>
570 {
571 this->it_->operator-=(steps);
572 }
573
574 template <typename TYPE>
576 {
577 auto* other_it = dynamic_cast<const iterAdaptor*>(&other);
578 if (other_it == nullptr)
579 return this->it_->operator-(other);
580 return this->it_->operator-(*other_it->it_);
581 }
582
583 template <typename TYPE>
585 {
586 return this->it_->get();
587 }
588
589 template <typename TYPE>
591 {
592 this->it_->set(data);
593 }
594
595 template<typename TYPE>
597 {
598 return this->it_->getElem();
599 }
600
601 template<typename TYPE>
603 return this->it_->isValid();
604 }
605
606 template<typename TYPE>
608 return "iterAdaptor";
609 }
610
611 template<typename TYPE>
612 auto original::iterable<TYPE>::iterAdaptor::toString(const bool enter) const -> std::string {
613 std::stringstream ss;
614 ss << this->className();
615 ss << "(" << *this->it_ << ")";
616 if (enter) ss << "\n";
617 return ss.str();
618 }
619
620 template<typename TYPE>
624
625 template <typename TYPE>
627 return iterAdaptor(this->begins());
628 }
629
630 template <typename TYPE>
632 auto* it = this->ends();
633 it->next();
634 return iterAdaptor(it);
635 }
636
637 template <typename TYPE>
641
642 template <typename TYPE>
644 auto* it = this->ends();
645 it->next();
646 return iterAdaptor(it);
647 }
648
649 template <typename TYPE>
651 {
652 return this->begin();
653 }
654
655 template <typename TYPE>
657 {
658 return iterAdaptor(this->ends());
659 }
660
661 template <typename TYPE>
663 {
664 return this->begin();
665 }
666
667 template <typename TYPE>
669 {
670 return iterAdaptor(this->ends());
671 }
672
673 template <typename TYPE>
674 template<typename Callback>
677 {
678 for (auto it = this->first(); it.isValid(); it.next()) {
679 operation(it.get());
680 }
681 }
682
683 template<typename TYPE>
684 template<typename Callback>
686 auto original::iterable<TYPE>::forEach(const Callback &operation) const -> void {
687 for (auto it = this->first(); it.isValid(); it.next()) {
688 operation(it.getElem());
689 }
690 }
691
692 template <typename TYPE>
695 {
696 for (const auto& elem : *this)
697 {
698 co_yield std::remove_const_t<TYPE>(elem);
699 }
700 }
701
702#endif //ITERABLE_H
const TYPE * get() const
Get managed pointer const version.
Definition autoPtr.h:629
Lazy sequence generator using C++20 coroutines.
Definition coroutines.h:57
RAII wrapper for base iterators that provides standard iteration interface.
Definition iterable.h:86
const iterator< TYPE > & getIt() const
Gets the underlying iterator.
Definition iterable.h:515
TYPE & get() override
Gets the value of the element the iterator is pointing to.
Definition iterable.h:584
void operator-=(integer steps) const override
Moves the iterator backward by a specified number of steps.
Definition iterable.h:569
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:542
iterAdaptor * clone() const override
Creates a copy of the current iterator.
Definition iterable.h:477
iterAdaptor * getPrev() const override
Gets the previous iterator.
Definition iterable.h:483
void set(const TYPE &data) override
Sets the value of the element the iterator is pointing to.
Definition iterable.h:590
bool hasPrev() const override
Checks if there is a previous element.
Definition iterable.h:527
void prev() const override
Moves the iterator to the previous element.
Definition iterable.h:557
void operator+=(integer steps) const override
Advances the iterator by a specified number of steps.
Definition iterable.h:563
std::string toString(bool enter) const override
Converts the iterator to a string representation.
Definition iterable.h:612
iterAdaptor * getNext() const override
Gets the next iterator.
Definition iterable.h:491
bool equalPtr(const iterator< TYPE > *other) const override
Compares the current iterator with another iterator.
Definition iterable.h:468
void next() const override
Moves the iterator to the next element.
Definition iterable.h:551
~iterAdaptor() override
Destructor for iterAdaptor.
Definition iterable.h:621
std::string className() const override
Returns the class name.
Definition iterable.h:607
bool isValid() const override
Checks if the iterator is pointing to a valid element.
Definition iterable.h:602
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:533
bool hasNext() const override
Checks if there is a next element.
Definition iterable.h:521
iterAdaptor & operator=(const iterAdaptor &other)
Copy assignment operator for the iterAdaptor.
Definition iterable.h:505
A base class for iterable containers that support multiple iteration patterns.
Definition iterable.h:70
iterAdaptor end() const
Returns a const iterator adapter pointing to the end sentinel of the container.
Definition iterable.h:643
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:650
iterAdaptor end()
Returns an iterator adapter pointing to the end sentinel of the container.
Definition iterable.h:631
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:638
iterAdaptor last() const
Returns a const iterator adapter pointing to the last element.
Definition iterable.h:668
iterAdaptor begin()
Returns an iterator adapter pointing to the beginning of the container.
Definition iterable.h:626
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:656
coroutine::generator< T > generator() const
Creates a coroutine generator that yields elements from this container.
Definition iterable.h:694
iterAdaptor first() const
Returns a const iterator adapter pointing to the first element.
Definition iterable.h:662
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
bool equal(const iterator *other) const
Checks if two iterators are equal.
Definition iterator.h:422
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.
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Abstract base class for unique element containers.
Definition set.h:44
Constraint for mutating operations.
Definition types.h:419
C++20 coroutine support with generator pattern implementation.
Defines the iterator class for traversing and manipulating container elements.
Main namespace for the project Original.
Definition algorithms.h:21
Standard namespace extensions for original::alternative.
Definition allocator.h:351
Transformation classes for various operations on elements.
Core type system foundations and concept definitions.