3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_BASE_ITERATORS_H
7#define ELEMENTA_BASE_ITERATORS_H
156 using Ptr = std::shared_ptr<MultItImpl>;
174 if (
this != &oth) Validatable::operator=(oth);
184 if (
this != &oth) Validatable::operator=(std::move(oth));
234 {
return(valid() && oth.valid() &&
240 {
return(!
operator==(oth)); }
245 {
return(std::string{
"MultItImpl("} +
257 static_assert( !std::is_void<Data>::value,
258 "MultItImpl cannot work with void Data" );
297template <
class Data,
bool BIDIR = false>
335 << (impl_.get() ==
nullptr ?
337 impl_->to_string())); }
365 bool valid(
void)
const {
return(impl_->valid()); }
372 { impl_->operator++();
return(*
this);}
376 {
MultIterator copyme{*
this}; impl_->operator++();
return(copyme); }
380 {
return( impl_->operator==(*(oth.impl_)) ); }
384 {
return( impl_->operator!=(*(oth.impl_)) ); }
388 {
return( ((impl_->valid()) && (oth.impl_->
valid()) &&
389 (impl_->pointedData() < oth.impl_->pointedData())) ); }
395 bool isEnd(
void)
const {
return(impl_->isEnd()); }
399 {
return(std::string{
"FMultIterator(valid = "} +
402 (impl_.get() !=
nullptr ? impl_->to_string() :
"null impl.") +
416 ELE_CODE_TRACE({},
"Cloning to this the impl of " <<oth.
to_string());
417 impl_ = oth.impl_->clone();
418 ELE_CODE_TRACE({},
"Cloned: " <<
to_string());}
420 static_assert( !std::is_void<Data>::value,
421 "MultIterator cannot work with void Data" );
467 << (impl_.get() ==
nullptr ?
469 impl_->to_string())); }
497 bool valid(
void)
const {
return(impl_->valid()); }
504 { impl_->operator++();
return(*
this);}
508 {
MultIterator copyme{*
this}; impl_->operator++();
return(copyme); }
512 { impl_->operator--();
return(*
this);}
516 {
MultIterator copyme{*
this}; impl_->operator--();
return(copyme); }
520 {
return( ((impl_->valid()) && (oth.impl_->
valid()) &&
521 (impl_->pointedData() == oth.impl_->pointedData())) ); }
525 {
return(!
operator==(oth)); }
529 {
return( ((impl_->valid()) && (oth.impl_->
valid()) &&
530 (impl_->pointedData() < oth.impl_->pointedData())) ); }
534 { std::swap(impl_,oth.impl_); }
538 {
return((impl_->valid())&&(impl_->pointedData()==
nullptr)); }
542 {
return(std::string{
"BMultIterator( isend = "} +
544 (impl_.get() !=
nullptr ? impl_->to_string() :
"null impl.") +
559 ELE_CODE_TRACE({},
"Cloning to this the impl of " <<oth.
to_string());
560 impl_ = oth.impl_->clone();
561 ELE_CODE_TRACE({},
"Cloned: " <<
to_string());}
563 static_assert( !std::is_void<Data>::value,
564 "MultIterator cannot work with void Data" );
569template <
class Data,
bool BIDIR>
616 virtual It
end(
void)
const = 0;
642 SubView(
const It & itbeg,
const It & itend):itbeg_{itbeg},itend_{itend} {}
645 It
begin(
void)
const {
return(itbeg_); }
648 It
end(
void)
const {
return(itend_); }
671template <
typename IT =
int>
685 {
if ((!alwaysend_) &&
686 ((unbounded_) || (value_ < bound_)))
688 if (value_ == std::numeric_limits<IT>::max())
694 IT operator*(
void)
const { checkderef();
return(value_); }
696 bool operator==(
const iterator_t & oth)
const noexcept
697 {
if (isend())
return(oth.isend());
698 else if (oth.isend())
return(
false);
699 return(value_ == oth.value_); }
701 bool operator!=(
const iterator_t & oth)
const noexcept
702 {
return(!
operator==(oth)); }
707 value_{value},bound_{bound},
708 unbounded_{
false},alwaysend_{
false}
712 unbounded_{
true},alwaysend_{
false}
715 iterator_t(
void)
noexcept: alwaysend_{
true},unbounded_{
true}
718 bool isend(
void)
const noexcept
720 ELE_CODE_TRACE({},
"returning " <<
722 ((!unbounded_) && (value_ == bound_))) <<
724 return((alwaysend_) ||
725 ((!unbounded_) && (value_ == bound_))); }
726 void checkderef(
void)
const
731 const bool unbounded_;
742 IotaView(IT start, IT finish)
noexcept: start_{start},finish_{finish},
743 bounded_{
true},alwaysend_{
false}
745 ", " << finish_ <<
"]"); }
749 bounded_{
false},alwaysend_{
false}
783 static_assert( std::is_integral<IT>::value,
784 "IotaView only works with integral types" );
Any class derived from this must be clonable.
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
constexpr const char * kYesNoCString(bool b)
User-friendly yes/no output.
#define ELE_CODE_INVSTATE(expl)
To throw an invalid-state exception with an explanation.
#define ELE_CODE_UNIMPLEMENTED
To throw an unimplemented exception.
LongestUnsigned to_number(const void *p)
Convert a pointer address to a number.
bool operator!=(const MultIterator &oth) const
Inequality.
void swap(MultIterator &oth)
To swap iterators is to swap their internal implementations.
MultIterator & operator=(const MultIterator &oth)
Copy assignment. Makes sure the copy is deep.
MultItImpl & operator=(const MultItImpl &oth)
Copy assignment.
MultIterator & operator--(int)
Postfix decrement.
IotaView(IT start, IT finish) noexcept
Bounded constructor.
bool operator<(const MultIterator &oth) const
Ordering (for using MultIterator in std::maps, for instance).
virtual bool operator!=(const MultItImpl &oth) const
Unequality between iterators.
Reference operator*(void) const
Return a ref to the pointed data, or throw in invalid state.
bool isEnd(void) const
Check for end. If it is invalid or points not to end, return false.
bool isEnd(void) const
Check for end. If it is invalid or points not to end, return false.
bool operator==(const MultIterator &oth) const
Check whether both are valid and point to the same data.
std::ptrdiff_t Distance
Distance between iterators (for STL-compatibility)
ImplementationPtr implementation(void) const
Consulting the implementation.
bool operator==(const MultIterator &oth) const
Check whether both are valid and point to the same data.
MultIterator(ImplementationPtr impl=ImplementationPtr{})
Constructor for a given behavior.
MultItImpl(const MultItImpl &oth)
Copy constructor.
MultIterator(MultIterator &&oth)
Move constructor. Do not move actually, just copy.
virtual bool isEnd(void) const
Detect whether it is end iterator.
MultIterator & operator++(int)
Postfix increment.
It begin(void) const
Provides the begin iterator.
MultIterator & operator++(int)
Postfix increment.
virtual ~MultItImpl(void)=default
Virtual destructor.
virtual Data * pointedData(void) const =0
Must return a pointer to the data under the iterator.
~MultIterator(void)
Destructor.
virtual bool operator==(const MultItImpl &oth) const
Equality between iterators. By default, equality of the pointed data.
IotaView(IT start) noexcept
Unbounded constructor.
void invalidate(void)
Invalidate the iterator.
std::string to_string(void) const
Return a string with info about the iterator.
SubView(const It &itbeg, const It &itend)
Create the view, from ITBEG to ITEND.
Data T
Iterated data (for STL-compatibility)
std::string to_string(void) const
Return a string with info about the iterator.
virtual MultItImpl & operator--(void)
Prefix decrement, for possible bidirectional iterations.
std::shared_ptr< MultItImpl > Ptr
A smart pointer to the iterator implementation.
T & Reference
Reference to data (for STL-compatibility)
IotaView(void) noexcept
Always end constructor (begin == end).
~MultIterator(void)
Destructor.
MultItImpl(void)
Default constructor: invalid state.
Reference operator*(void) const
Return a ref to the pointed data, or throw in invalid state.
bool valid(void) const
Return true if the iterator is in valid state.
bool operator<(const MultIterator &oth) const
Ordering (for using MultIterator in std::maps, for instance).
MultItImpl(MultItImpl &&oth)
Move constructor.
T * Pointer
Pointer to data (for STL-compatibility)
bool valid(void) const
Return true if the iterator is in valid state.
It end(void) const
Provides the end iterator.
MultIterator & operator=(MultIterator &&oth)
Move assignment. Do not move actually, just copy.
Data T
Iterated data (for STL-compatibility)
virtual It end(void) const =0
Must return an iterator pointing past the last element.
std::forward_iterator_tag Category
Category of the iterator (for STL-compatibility)
MultIterator(MultIterator &&oth)
Move constructor. Do not move actually, just copy.
MultIterator(ImplementationPtr impl=ImplementationPtr{})
Constructor for a given behavior.
virtual std::string to_string(void) const
Return a string with info about the iterator implementation.
T & Reference
Reference to data (for STL-compatibility)
ImplementationPtr implementation(void) const
Consulting the implementation.
virtual MultItImpl & operator++(void)=0
Prefix increment.
MultIterator & operator++(void)
Prefix increment.
iterator_t begin(void) const noexcept
Provides the begin iterator.
T * Pointer
Pointer to data (for STL-compatibility)
MultIterator(const MultIterator &oth)
Copy constructor. Makes sure the copy is deep.
MultIterator & operator=(MultIterator &&oth)
Move assignment. Do not move actually, just copy.
virtual It begin(void) const =0
Must return an iterator pointing to the first element.
void swap(MultIterator &oth)
To swap iterators is to swap their internal implementations.
virtual Data & operator*(void)=0
Must return a ref to the pointed data, or throw in invalid state.
std::bidirectional_iterator_tag Category
Category of the iterator (for STL-compatibility)
bool operator!=(const MultIterator &oth) const
Inequality.
MultIterator & operator++(void)
Prefix increment.
typename MultItImpl< Data >::Ptr ImplementationPtr
Pointer to an implementation.
MultIterator(const MultIterator &oth)
Copy constructor. Makes sure the copy is deep.
MultIterator & operator=(const MultIterator &oth)
Copy assignment. Makes sure the copy is deep.
typename MultItImpl< Data >::Ptr ImplementationPtr
Pointer to an implementation.
MultItImpl & operator=(MultItImpl &&oth)
Move assignment.
MultIterator & operator--(void)
Prefix decrement.
std::ptrdiff_t Distance
Distance between iterators (for STL-compatibility)
iterator_t end(void) const noexcept
Provides the end iterator.
A view that refers to a sequence of incrementable elements, bounded or not.
Base class for implementing diverse iteration behaviors.
A mutable iterator that can have multiple iteration behaviors. Forward case.
A range is a provider of begin() and end() iterators.
A view that refers to a segment of an existing container.
void swap(MultIterator< Data, BIDIR > &it1, MultIterator< Data, BIDIR > &it2)
Swap function for MultIterators.
bool valid(void) const noexcept
Public method to be used by users of the derived class.
#define ELE_CLASS_VALIDATABLE
Declare a class derived from Validatable.
The iterator type returned by begin() and end().