![]() |
Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
|
Diverse extensions of container iterators.
In this module you will find "multiple iterators": iterators that can iterate in a given container showing different behaviors (e.g., in a graph there are multiple ways of iterating on vertices). A multiple iterator is implemented with three classes due to the necessity of composition (there must be a base iterator class that, after copies or moves -not polymorphic- still can be differentiated in diverse iteration behaviours):
You will also find minimal Ranges (full-blown ranges are included in C++20):
begin() and end() methods that any normal container must have to be used in for-each and many other situations. This is specially useful in multiple iterators since they prevent to use begin() and end() in the very container as such (there would be a set of multiple begin() and end()).Usage:
Derive hidden implementations from MultItImpl, one per behavior.
1bis.-Don't forget to add copy/move constructors and assignements to these derived classes. Call from them the corresponding methods in the base class, which take care of copying validity status.
begin() and end() methods pure virtual protected methods that you define in that base class (e.g., begin_behavior1(), end_behavior1(), ...). Then, define in the derived containers those protected methods with the proper implementation.
Classes | |
| class | elementa::base::MultItImpl< Data > |
| Base class for implementing diverse iteration behaviors. More... | |
| class | elementa::base::MultIterator< Data, BIDIR > |
| A mutable iterator that can have multiple iteration behaviors. Forward case. More... | |
| class | elementa::base::MultIterator< Data, true > |
| A mutable bidirectional iterator that can have multiple iteration behaviors. More... | |
| class | elementa::base::Range< It > |
| A range is a provider of begin() and end() iterators. More... | |
| class | elementa::base::SubView< It > |
| A view that refers to a segment of an existing container. More... | |
| class | elementa::base::IotaView< IT > |
| A view that refers to a sequence of incrementable elements, bounded or not. More... | |
Functions | |
| template<class Data , bool BIDIR> | |
| void | elementa::base::swap (MultIterator< Data, BIDIR > &it1, MultIterator< Data, BIDIR > &it2) |
| Swap function for MultIterators. More... | |
| class elementa::base::MultItImpl |
Base class for implementing diverse iteration behaviors.
The user must derive from this class and implement the pure virtual methods (and override operator--() if bidirectional) to build a new iteration behaviour.
| Data | is the type of the objects that will be pointed by the iterator, and cannot be void. |
Definition at line 147 of file iterators.h.


Public Types | |
Types, consts., etc. | |
| using | Ptr = std::shared_ptr< MultItImpl > |
| A smart pointer to the iterator implementation. More... | |
Public Member Functions | |
| virtual std::shared_ptr< MultItImpl< Data > > | clone (void) const=0 |
| Derived classes must implement this method, returning the same return. | |
| virtual std::shared_ptr< MultItImpl< Data > > * | emptyClone (void) const |
| Derived classes can override this to provide a clone that is "empty". More... | |
Constructors | |
| MultItImpl (void) | |
| Default constructor: invalid state. More... | |
| MultItImpl (const MultItImpl &oth) | |
| Copy constructor. More... | |
| MultItImpl & | operator= (const MultItImpl &oth) |
| Copy assignment. More... | |
| MultItImpl (MultItImpl &&oth) | |
| Move constructor. More... | |
| MultItImpl & | operator= (MultItImpl &&oth) |
| Move assignment. More... | |
| virtual | ~MultItImpl (void)=default |
| Virtual destructor. | |
Interface methods | |
| virtual Data & | operator* (void)=0 |
| Must return a ref to the pointed data, or throw in invalid state. More... | |
| virtual Data * | pointedData (void) const =0 |
| Must return a pointer to the data under the iterator. More... | |
| virtual MultItImpl & | operator++ (void)=0 |
| Prefix increment. More... | |
| virtual bool | isEnd (void) const |
| Detect whether it is end iterator. More... | |
Other methods | |
| void | invalidate (void) |
| Invalidate the iterator. More... | |
| virtual MultItImpl & | operator-- (void) |
| Prefix decrement, for possible bidirectional iterations. More... | |
| virtual bool | operator== (const MultItImpl &oth) const |
| Equality between iterators. By default, equality of the pointed data. More... | |
| virtual bool | operator!= (const MultItImpl &oth) const |
| Unequality between iterators. More... | |
| virtual std::string | to_string (void) const |
| Return a string with info about the iterator implementation. More... | |
| using elementa::base::MultItImpl< Data >::Ptr = std::shared_ptr<MultItImpl> |
A smart pointer to the iterator implementation.
Definition at line 156 of file iterators.h.
|
inline |
Default constructor: invalid state.
Definition at line 165 of file iterators.h.
|
inline |
|
inline |
|
inline |
Copy assignment.
Definition at line 172 of file iterators.h.
References ELE_CODE_TRACE_OFF.
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::eenteringend().
|
inline |
|
pure virtual |
Must return a ref to the pointed data, or throw in invalid state.
| invalid_state | On invalid state. |
|
pure virtual |
Must return a pointer to the data under the iterator.
If the iterator impl. is invalid, this must throw; if it points to a non-data place (e.g., end()), must return nullptr. This method will be used for detecting equality between iterators.
Referenced by elementa::base::MultItImpl< Data >::isEnd(), elementa::base::MultItImpl< Data >::operator==(), and elementa::base::MultItImpl< Data >::to_string().
|
pure virtual |
Prefix increment.
The only one you need to implement (postfix is in MultIterator)
|
inlinevirtual |
Detect whether it is end iterator.
This can be overriden with a different implementation if the iterator does not work with pointedData.
Definition at line 213 of file iterators.h.
References elementa::base::MultItImpl< Data >::pointedData().
|
inline |
Invalidate the iterator.
Definition at line 224 of file iterators.h.
|
inlinevirtual |
Prefix decrement, for possible bidirectional iterations.
Definition at line 227 of file iterators.h.
References ELE_CODE_UNIMPLEMENTED.
|
inlinevirtual |
Equality between iterators. By default, equality of the pointed data.
This can be overriden for special cases, e.g., when there is no pointed data but calculated on-the-fly one.
Definition at line 233 of file iterators.h.
References elementa::base::MultItImpl< Data >::pointedData().
|
inlinevirtual |
|
inlinevirtual |
Return a string with info about the iterator implementation.
Derived classes may override this one.
Definition at line 244 of file iterators.h.
References elementa::base::kYesNoCString(), elementa::base::MultItImpl< Data >::pointedData(), elementa::base::to_number(), and elementa::patterns::Validatable::valid().
|
inlinevirtualinherited |
Derived classes can override this to provide a clone that is "empty".
"Emptiness" here means that the clone is not actually a clone of the derived object, but a new object of the same class of the derived object, without no content (i.e., right after creation).
Definition at line 119 of file clonable.h.
| class elementa::base::MultIterator |
A mutable iterator that can have multiple iteration behaviors. Forward case.
This is the iterator that the final user will receive and use. Each iteration behavior is implemented with MultItImpl.
| Data | is the type of the objects pointed by the iterator, and cannot be void. |
== and != based on the fact that both iterators are valid, pointing into the same container, and the referenced elements are or not the same)++it, it++)operator--().Definition at line 298 of file iterators.h.
Public Types | |
Types, consts, etc. | |
| using | Category = std::forward_iterator_tag |
| Category of the iterator (for STL-compatibility) More... | |
| using | T = Data |
| Iterated data (for STL-compatibility) More... | |
| using | Distance = std::ptrdiff_t |
| Distance between iterators (for STL-compatibility) More... | |
| using | Pointer = T * |
| Pointer to data (for STL-compatibility) More... | |
| using | Reference = T & |
| Reference to data (for STL-compatibility) More... | |
| using | ImplementationPtr = typename MultItImpl< Data >::Ptr |
| Pointer to an implementation. More... | |
Public Member Functions | |
Constructors | |
| MultIterator (ImplementationPtr impl=ImplementationPtr{}) | |
| Constructor for a given behavior. More... | |
| MultIterator (const MultIterator &oth) | |
| Copy constructor. Makes sure the copy is deep. More... | |
| MultIterator & | operator= (const MultIterator &oth) |
| Copy assignment. Makes sure the copy is deep. More... | |
| MultIterator (MultIterator &&oth) | |
| Move constructor. Do not move actually, just copy. More... | |
| MultIterator & | operator= (MultIterator &&oth) |
| Move assignment. Do not move actually, just copy. More... | |
| ~MultIterator (void) | |
| Destructor. More... | |
Methods | |
| bool | valid (void) const |
Return true if the iterator is in valid state. More... | |
| Reference | operator* (void) const |
| Return a ref to the pointed data, or throw in invalid state. More... | |
| MultIterator & | operator++ (void) |
| Prefix increment. More... | |
| MultIterator & | operator++ (int) |
| Postfix increment. More... | |
| bool | operator== (const MultIterator &oth) const |
| Check whether both are valid and point to the same data. More... | |
| bool | operator!= (const MultIterator &oth) const |
| Inequality. More... | |
| bool | operator< (const MultIterator &oth) const |
Ordering (for using MultIterator in std::maps, for instance). More... | |
| void | swap (MultIterator &oth) |
| To swap iterators is to swap their internal implementations. More... | |
| bool | isEnd (void) const |
Check for end. If it is invalid or points not to end, return false. More... | |
| std::string | to_string (void) const |
| Return a string with info about the iterator. More... | |
| ImplementationPtr | implementation (void) const |
| Consulting the implementation. More... | |
| using elementa::base::MultIterator< Data, BIDIR >::Category = std::forward_iterator_tag |
Category of the iterator (for STL-compatibility)
Definition at line 306 of file iterators.h.
| using elementa::base::MultIterator< Data, BIDIR >::T = Data |
Iterated data (for STL-compatibility)
Definition at line 309 of file iterators.h.
| using elementa::base::MultIterator< Data, BIDIR >::Distance = std::ptrdiff_t |
Distance between iterators (for STL-compatibility)
Definition at line 312 of file iterators.h.
| using elementa::base::MultIterator< Data, BIDIR >::Pointer = T* |
Pointer to data (for STL-compatibility)
Definition at line 315 of file iterators.h.
| using elementa::base::MultIterator< Data, BIDIR >::Reference = T& |
Reference to data (for STL-compatibility)
Definition at line 318 of file iterators.h.
| using elementa::base::MultIterator< Data, BIDIR >::ImplementationPtr = typename MultItImpl<Data>::Ptr |
Pointer to an implementation.
Definition at line 321 of file iterators.h.
|
inline |
Constructor for a given behavior.
This provides the default constructor, needed for ForwardLegacyIterator and up.
Definition at line 332 of file iterators.h.
|
inline |
Copy constructor. Makes sure the copy is deep.
Definition at line 340 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Move constructor. Do not move actually, just copy.
Definition at line 348 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Destructor.
Definition at line 356 of file iterators.h.
|
inline |
Copy assignment. Makes sure the copy is deep.
Definition at line 344 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Move assignment. Do not move actually, just copy.
Definition at line 352 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Return true if the iterator is in valid state.
Definition at line 365 of file iterators.h.
Referenced by elementa::adts::Tree< Vertex, Edge >::addChild(), elementa::adts::graphs::Graph< Vertex, Edge >::ERange_DepthFirst::forceBacktrack(), elementa::base::MultIterator< Data, BIDIR >::operator<(), elementa::base::MultIterator< Data, true >::operator<(), and elementa::base::MultIterator< Data, true >::operator==().
|
inline |
Return a ref to the pointed data, or throw in invalid state.
Definition at line 368 of file iterators.h.
|
inline |
Prefix increment.
Definition at line 371 of file iterators.h.
|
inline |
Postfix increment.
Definition at line 375 of file iterators.h.
|
inline |
Check whether both are valid and point to the same data.
Definition at line 379 of file iterators.h.
|
inline |
Inequality.
Definition at line 383 of file iterators.h.
|
inline |
Ordering (for using MultIterator in std::maps, for instance).
Definition at line 387 of file iterators.h.
References elementa::base::MultIterator< Data, BIDIR >::valid().
|
inline |
To swap iterators is to swap their internal implementations.
Definition at line 392 of file iterators.h.
Referenced by elementa::base::swap().
|
inline |
Check for end. If it is invalid or points not to end, return false.
Definition at line 395 of file iterators.h.
Referenced by elementa::adts::Tree< Vertex, Edge >::addChild(), elementa::adts::Tree< Vertex, Edge >::depth(), elementa::adts::graphs::Szer_GraphML< Vertex, Edge >::ser(), elementa::base::MultIterator< Data, BIDIR >::to_string(), and elementa::base::MultIterator< Data, true >::to_string().
|
inline |
Return a string with info about the iterator.
Definition at line 398 of file iterators.h.
References elementa::base::MultIterator< Data, BIDIR >::isEnd(), and elementa::base::kYesNoCString().
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::ERange_DepthFirst::ERange_DepthFirst(), elementa::adts::Tree< Vertex, Edge >::addChild(), elementa::adts::graphs::Graph< Vertex, Edge >::edgeToPos(), elementa::adts::graphs::Szer_GraphCompactText< Vertex, Edge >::ser(), elementa::adts::graphs::FloydWarshall< Vertex, Edge, Weight >::shortest(), and elementa::adts::graphs::Graph< Vertex, Edge >::vertexToPos().
|
inline |
Consulting the implementation.
Definition at line 406 of file iterators.h.
Referenced by elementa::adts::graphs::Graph< Vertex, Edge >::MapOfEItPos::MapOfEItPos(), elementa::adts::graphs::Graph< Vertex, Edge >::MapOfVItPos::MapOfVItPos(), elementa::adts::graphs::Graph< Vertex, Edge >::MapOfEItPos::at(), elementa::adts::graphs::Graph< Vertex, Edge >::MapOfVItPos::at(), and elementa::adts::graphs::Graph< Vertex, Edge >::ERange_DepthFirst::forceBacktrack().
| class elementa::base::MultIterator< Data, true > |
A mutable bidirectional iterator that can have multiple iteration behaviors.
Bidirectional case.
Definition at line 430 of file iterators.h.
Public Types | |
Types, consts, etc. | |
| using | Category = std::bidirectional_iterator_tag |
| Category of the iterator (for STL-compatibility) More... | |
| using | T = Data |
| Iterated data (for STL-compatibility) More... | |
| using | Distance = std::ptrdiff_t |
| Distance between iterators (for STL-compatibility) More... | |
| using | Pointer = T * |
| Pointer to data (for STL-compatibility) More... | |
| using | Reference = T & |
| Reference to data (for STL-compatibility) More... | |
| using | ImplementationPtr = typename MultItImpl< Data >::Ptr |
| Pointer to an implementation. More... | |
Public Member Functions | |
Constructors | |
| MultIterator (ImplementationPtr impl=ImplementationPtr{}) | |
| Constructor for a given behavior. More... | |
| MultIterator (const MultIterator &oth) | |
| Copy constructor. Makes sure the copy is deep. More... | |
| MultIterator & | operator= (const MultIterator &oth) |
| Copy assignment. Makes sure the copy is deep. More... | |
| MultIterator (MultIterator &&oth) | |
| Move constructor. Do not move actually, just copy. More... | |
| MultIterator & | operator= (MultIterator &&oth) |
| Move assignment. Do not move actually, just copy. More... | |
| ~MultIterator (void) | |
| Destructor. More... | |
Methods | |
| bool | valid (void) const |
Return true if the iterator is in valid state. More... | |
| Reference | operator* (void) const |
| Return a ref to the pointed data, or throw in invalid state. More... | |
| MultIterator & | operator++ (void) |
| Prefix increment. More... | |
| MultIterator & | operator++ (int) |
| Postfix increment. More... | |
| MultIterator & | operator-- (void) |
| Prefix decrement. More... | |
| MultIterator & | operator-- (int) |
| Postfix decrement. More... | |
| bool | operator== (const MultIterator &oth) const |
| Check whether both are valid and point to the same data. More... | |
| bool | operator!= (const MultIterator &oth) const |
| Inequality. More... | |
| bool | operator< (const MultIterator &oth) const |
Ordering (for using MultIterator in std::maps, for instance). More... | |
| void | swap (MultIterator &oth) |
| To swap iterators is to swap their internal implementations. More... | |
| bool | isEnd (void) const |
Check for end. If it is invalid or points not to end, return false. More... | |
| std::string | to_string (void) const |
| Return a string with info about the iterator. More... | |
| ImplementationPtr | implementation (void) const |
| Consulting the implementation. More... | |
| using elementa::base::MultIterator< Data, true >::Category = std::bidirectional_iterator_tag |
Category of the iterator (for STL-compatibility)
Definition at line 438 of file iterators.h.
| using elementa::base::MultIterator< Data, true >::T = Data |
Iterated data (for STL-compatibility)
Definition at line 441 of file iterators.h.
| using elementa::base::MultIterator< Data, true >::Distance = std::ptrdiff_t |
Distance between iterators (for STL-compatibility)
Definition at line 444 of file iterators.h.
| using elementa::base::MultIterator< Data, true >::Pointer = T* |
Pointer to data (for STL-compatibility)
Definition at line 447 of file iterators.h.
| using elementa::base::MultIterator< Data, true >::Reference = T& |
Reference to data (for STL-compatibility)
Definition at line 450 of file iterators.h.
| using elementa::base::MultIterator< Data, true >::ImplementationPtr = typename MultItImpl<Data>::Ptr |
Pointer to an implementation.
Definition at line 453 of file iterators.h.
|
inline |
Constructor for a given behavior.
This provides the default constructor, needed for ForwardLegacyIterator and up.
Definition at line 464 of file iterators.h.
|
inline |
Copy constructor. Makes sure the copy is deep.
Definition at line 472 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Move constructor. Do not move actually, just copy.
Definition at line 480 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Destructor.
Definition at line 488 of file iterators.h.
|
inline |
Copy assignment. Makes sure the copy is deep.
Definition at line 476 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Move assignment. Do not move actually, just copy.
Definition at line 484 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inline |
Return true if the iterator is in valid state.
Definition at line 497 of file iterators.h.
|
inline |
Return a ref to the pointed data, or throw in invalid state.
Definition at line 500 of file iterators.h.
|
inline |
Prefix increment.
Definition at line 503 of file iterators.h.
|
inline |
Postfix increment.
Definition at line 507 of file iterators.h.
|
inline |
Prefix decrement.
Definition at line 511 of file iterators.h.
|
inline |
Postfix decrement.
Definition at line 515 of file iterators.h.
|
inline |
Check whether both are valid and point to the same data.
Definition at line 519 of file iterators.h.
References elementa::base::MultIterator< Data, BIDIR >::valid().
|
inline |
Inequality.
Definition at line 524 of file iterators.h.
|
inline |
Ordering (for using MultIterator in std::maps, for instance).
Definition at line 528 of file iterators.h.
References elementa::base::MultIterator< Data, BIDIR >::valid().
|
inline |
To swap iterators is to swap their internal implementations.
Definition at line 533 of file iterators.h.
|
inline |
Check for end. If it is invalid or points not to end, return false.
Definition at line 537 of file iterators.h.
|
inline |
Return a string with info about the iterator.
Definition at line 541 of file iterators.h.
References elementa::base::MultIterator< Data, BIDIR >::isEnd(), and elementa::base::kYesNoCString().
|
inline |
Consulting the implementation.
Definition at line 548 of file iterators.h.
| class elementa::base::Range |
A range is a provider of begin() and end() iterators.
Actually, any container is a range. A range is a concept in C++20, not a class.
| It | is the iterator type. |
Definition at line 607 of file iterators.h.

Public Member Functions | |
| virtual It | begin (void) const =0 |
| Must return an iterator pointing to the first element. More... | |
| virtual It | end (void) const =0 |
| Must return an iterator pointing past the last element. More... | |
|
pure virtual |
Must return an iterator pointing to the first element.
If the container is empty, it must return the same as end().
Implemented in elementa::adts::graphs::Graph< Vertex, Edge >::VRange_All, elementa::adts::graphs::Graph< Vertex, Edge >::VRange_Pred, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_All, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Pred, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Leaving, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Entering, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_DepthFirst, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_BreadthFirst, and elementa::base::SubView< It >.
|
pure virtual |
Must return an iterator pointing past the last element.
Implemented in elementa::adts::graphs::Graph< Vertex, Edge >::VRange_All, elementa::adts::graphs::Graph< Vertex, Edge >::VRange_Pred, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_All, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Pred, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Leaving, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_Entering, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_DepthFirst, elementa::adts::graphs::Graph< Vertex, Edge >::ERange_BreadthFirst, and elementa::base::SubView< It >.
| class elementa::base::SubView |
A view that refers to a segment of an existing container.
A view is a concept in C++20 referring to a range that does not hold its data and evaluates its iteration lazily (and allows for composition).
| It | is the iterator type. |
Definition at line 633 of file iterators.h.


Public Member Functions | |
| SubView (const It &itbeg, const It &itend) | |
| Create the view, from ITBEG to ITEND. More... | |
| It | begin (void) const |
| Provides the begin iterator. More... | |
| It | end (void) const |
| Provides the end iterator. More... | |
|
inline |
Create the view, from ITBEG to ITEND.
Definition at line 642 of file iterators.h.
|
inlinevirtual |
Provides the begin iterator.
Implements elementa::base::Range< It >.
Definition at line 645 of file iterators.h.
|
inlinevirtual |
Provides the end iterator.
Implements elementa::base::Range< It >.
Definition at line 648 of file iterators.h.
| class elementa::base::IotaView |
A view that refers to a sequence of incrementable elements, bounded or not.
Its behavior is a minimalistic version of std::iota_view in C++20.
| IT | must be an integral type. |
Definition at line 672 of file iterators.h.
Public Member Functions | |
Constructors | |
| IotaView (IT start, IT finish) noexcept | |
| Bounded constructor. More... | |
| IotaView (IT start) noexcept | |
| Unbounded constructor. More... | |
| IotaView (void) noexcept | |
| Always end constructor (begin == end). More... | |
Methods | |
| iterator_t | begin (void) const noexcept |
| Provides the begin iterator. More... | |
| iterator_t | end (void) const noexcept |
| Provides the end iterator. More... | |
|
inlinenoexcept |
Bounded constructor.
Definition at line 742 of file iterators.h.
|
inlinenoexcept |
Unbounded constructor.
Definition at line 748 of file iterators.h.
|
inlinenoexcept |
Always end constructor (begin == end).
Definition at line 754 of file iterators.h.
|
inlinenoexcept |
Provides the begin iterator.
Definition at line 764 of file iterators.h.
References ELE_CODE_TRACE_OFF.
|
inlinenoexcept |
Provides the end iterator.
Definition at line 771 of file iterators.h.
References ELE_CODE_TRACE_OFF.
| void elementa::base::swap | ( | MultIterator< Data, BIDIR > & | it1, |
| MultIterator< Data, BIDIR > & | it2 | ||
| ) |
#include <elementa/base/iterators.h>
Swap function for MultIterators.
Definition at line 570 of file iterators.h.
References elementa::base::MultIterator< Data, BIDIR >::swap().