Arrays - Iterators for T_{Array,StaticArray,AutoArray}

This commit is contained in:
Emmanuel BENOîT 2018-12-16 15:24:40 +01:00
parent 89b67eb0d0
commit 42c2d48f1b
9 changed files with 735 additions and 627 deletions

View file

@ -0,0 +1,219 @@
#ifndef M_TMPL_
# error M_TMPL_ not defined
#endif
#ifndef M_TGT_
# error M_TGT_ not defined
#endif
#define M_HDR_ template< M_TMPL_ > template< ptrdiff_t D >
#define M_CNAME_ M_TGT_::T_ConstIterator_< D >
#define M_CNAME_T_ typename M_TGT_::template T_ConstIterator_< D >
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_::T_ConstIterator_(
M_TGT_ const& array ,
const ptrdiff_t pos ) noexcept
: array_( &array ) , pos_( pos )
{ }
M_HDR_ inline M_CNAME_::T_ConstIterator_( ) noexcept
: array_( nullptr ) , pos_( 0 )
{ }
M_HDR_ inline M_CNAME_::T_ConstIterator_(
T_ConstIterator_ const& other ) noexcept
: array_( other.array_ ) , pos_( other.pos_ )
{ }
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator=(
T_ConstIterator_ const& other ) noexcept
{
array_ = other.array_;
pos_ = other.pos_;
return *this;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_& M_CNAME_::swap(
M_CNAME_& other ) noexcept
{
std::swap( array_ , other.array_ );
std::swap( pos_ , other.pos_ );
return *this;
}
template< M_TMPL_ , ptrdiff_t D>
inline void swap(
M_CNAME_T_& lhs ,
M_CNAME_T_& rhs ) noexcept
{
lhs.swap( rhs );
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline bool M_CNAME_::operator==(
T_ConstIterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ == other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator!=(
T_ConstIterator_ const& other ) const noexcept
{
return array_ != other.array_ || pos_ != other.pos_;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline bool M_CNAME_::operator <(
T_ConstIterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ < other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator<=(
T_ConstIterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ <= other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator >(
T_ConstIterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ > other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator>=(
T_ConstIterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ >= other.pos_;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline T const& M_CNAME_::operator*( ) const noexcept
{
assert( valid( ) );
return (*array_)[ pos_ ];
}
M_HDR_ inline T const* M_CNAME_::operator->( ) const noexcept
{
assert( valid( ) );
return &((*array_)[ pos_ ]);
}
M_HDR_ inline T const& M_CNAME_::operator[](
const ptrdiff_t pos ) const noexcept
{
const auto idx( pos + pos_ );
assert( array_ && idx >= 0 && idx < array_->size( ) );
return (*array_)[ idx ];
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator++ () noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ + D ) );
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator++ (int) noexcept
{
assert( array_ );
auto copy( *this );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ + D ) );
return copy;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator-- () noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ - D ) );
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator-- (int) noexcept
{
assert( array_ );
auto copy( *this );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ - D ) );
return copy;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator +(
const ptrdiff_t value ) const noexcept
{
auto copy( *this );
copy += value;
return copy;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator -(
const ptrdiff_t value ) const noexcept
{
auto copy( *this );
copy -= value;
return copy;
}
M_HDR_ inline ptrdiff_t M_CNAME_::operator -(
M_CNAME_T_ const& other ) const noexcept
{
assert( array_ && array_ == other.array_ );
return pos_ - other.pos_;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator +=(
const ptrdiff_t value ) noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ + value * D ) );
return *this;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator -=(
const ptrdiff_t value ) noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ - value * D ) );
return *this;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline bool M_CNAME_::valid( ) const noexcept
{
return array_ && pos_ >= 0 && pos_ < array_->size( );
}
M_HDR_ inline M_TGT_* M_CNAME_::target( ) const noexcept
{
return array_;
}
M_HDR_ inline ptrdiff_t M_CNAME_::pos( ) const noexcept
{
return pos_;
}
/*----------------------------------------------------------------------------*/
#undef M_HDR_
#undef M_CNAME_
#undef M_CNAME_T_
#undef M_TMPL_
#undef M_TGT_

View file

@ -0,0 +1,36 @@
#ifndef M_TGT_
# define M_TGT_ T_Self
#endif
template< ptrdiff_t Direction >
class T_ConstIterator_ : public std::iterator<
std::random_access_iterator_tag , Type >
{
private:
M_TGT_ const* array_;
ptrdiff_t pos_;
public:
T_ConstIterator_( M_TGT_ const& array ,
const ptrdiff_t pos ) noexcept;
T_ConstIterator_( typename M_TGT_::T_Iterator const& iterator ) noexcept;
T_ConstIterator_& operator =(
typename M_TGT_::T_Iterator const& iterator ) noexcept;
M_ITER_CONS_DEF( T_ConstIterator_ );
M_ITER_CONS_COPY( T_ConstIterator_ );
M_ITER_SWAP( T_ConstIterator_ );
M_ITER_CMP_ALL( T_ConstIterator_ );
M_ITER_DEREF_RANDOM( Type const& , Type const* );
M_ITER_OPS_RANDOM( T_ConstIterator_ );
bool valid( ) const noexcept;
M_TGT_* target( ) const noexcept;
ptrdiff_t pos( ) const noexcept;
};
using T_ConstIterator = T_ConstIterator_< 1 >;
using T_ConstReverseIterator = T_ConstIterator_< -1 >;
#undef M_TGT_

View file

@ -0,0 +1,219 @@
#ifndef M_TMPL_
# error M_TMPL_ not defined
#endif
#ifndef M_TGT_
# error M_TGT_ not defined
#endif
#define M_HDR_ template< M_TMPL_ > template< ptrdiff_t D >
#define M_CNAME_ M_TGT_::T_Iterator_< D >
#define M_CNAME_T_ typename M_TGT_::template T_Iterator_< D >
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_::T_Iterator_(
M_TGT_& array ,
const ptrdiff_t pos ) noexcept
: array_( &array ) , pos_( pos )
{ }
M_HDR_ inline M_CNAME_::T_Iterator_( ) noexcept
: array_( nullptr ) , pos_( 0 )
{ }
M_HDR_ inline M_CNAME_::T_Iterator_(
T_Iterator_ const& other ) noexcept
: array_( other.array_ ) , pos_( other.pos_ )
{ }
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator=(
T_Iterator_ const& other ) noexcept
{
array_ = other.array_;
pos_ = other.pos_;
return *this;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_& M_CNAME_::swap(
M_CNAME_& other ) noexcept
{
std::swap( array_ , other.array_ );
std::swap( pos_ , other.pos_ );
return *this;
}
template< M_TMPL_ , ptrdiff_t D>
inline void swap(
M_CNAME_T_& lhs ,
M_CNAME_T_& rhs ) noexcept
{
lhs.swap( rhs );
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline bool M_CNAME_::operator==(
T_Iterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ == other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator!=(
T_Iterator_ const& other ) const noexcept
{
return array_ != other.array_ || pos_ != other.pos_;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline bool M_CNAME_::operator <(
T_Iterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ < other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator<=(
T_Iterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ <= other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator >(
T_Iterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ > other.pos_;
}
M_HDR_ inline bool M_CNAME_::operator>=(
T_Iterator_ const& other ) const noexcept
{
return array_ == other.array_ && pos_ >= other.pos_;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline T& M_CNAME_::operator*( ) const noexcept
{
assert( valid( ) );
return (*array_)[ pos_ ];
}
M_HDR_ inline T* M_CNAME_::operator->( ) const noexcept
{
assert( valid( ) );
return &((*array_)[ pos_ ]);
}
M_HDR_ inline T& M_CNAME_::operator[](
const ptrdiff_t pos ) const noexcept
{
const auto idx( pos + pos_ );
assert( array_ && idx >= 0 && idx < array_->size( ) );
return (*array_)[ idx ];
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator++ () noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ + D ) );
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator++ (int) noexcept
{
assert( array_ );
auto copy( *this );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ + D ) );
return copy;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator-- () noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ - D ) );
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator-- (int) noexcept
{
assert( array_ );
auto copy( *this );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ - D ) );
return copy;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator +(
const ptrdiff_t value ) const noexcept
{
auto copy( *this );
copy += value;
return copy;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator -(
const ptrdiff_t value ) const noexcept
{
auto copy( *this );
copy -= value;
return copy;
}
M_HDR_ inline ptrdiff_t M_CNAME_::operator -(
M_CNAME_T_ const& other ) const noexcept
{
assert( array_ && array_ == other.array_ );
return pos_ - other.pos_;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator +=(
const ptrdiff_t value ) noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ + value * D ) );
return *this;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator -=(
const ptrdiff_t value ) noexcept
{
assert( array_ );
pos_ = std::min( ptrdiff_t( array_->size( ) ) ,
std::max( ptrdiff_t( -1 ) , pos_ - value * D ) );
return *this;
}
/*----------------------------------------------------------------------------*/
M_HDR_ inline bool M_CNAME_::valid( ) const noexcept
{
return array_ && pos_ >= 0 && pos_ < array_->size( );
}
M_HDR_ inline M_TGT_* M_CNAME_::target( ) const noexcept
{
return array_;
}
M_HDR_ inline ptrdiff_t M_CNAME_::pos( ) const noexcept
{
return pos_;
}
/*----------------------------------------------------------------------------*/
#undef M_HDR_
#undef M_CNAME_
#undef M_CNAME_T_
#undef M_TMPL_
#undef M_TGT_

View file

@ -0,0 +1,31 @@
#ifndef M_TGT_
# define M_TGT_ T_Self
#endif
template< ptrdiff_t Direction >
class T_Iterator_ final : public std::iterator<
std::random_access_iterator_tag , Type >
{
private:
M_TGT_* array_;
ptrdiff_t pos_;
public:
T_Iterator_( M_TGT_& array ,
const ptrdiff_t pos ) noexcept;
M_ITER_CONS_DEF( T_Iterator_ );
M_ITER_CONS_COPY( T_Iterator_ );
M_ITER_SWAP( T_Iterator_ );
M_ITER_CMP_ALL( T_Iterator_ );
M_ITER_DEREF_RANDOM( Type& , Type* );
M_ITER_OPS_RANDOM( T_Iterator_ );
bool valid( ) const noexcept;
M_TGT_* target( ) const noexcept;
ptrdiff_t pos( ) const noexcept;
};
using T_Iterator = T_Iterator_< 1 >;
using T_ReverseIterator = T_Iterator_< -1 >;
#undef M_TGT_

View file

@ -0,0 +1,79 @@
#ifndef M_TMPL_
# error M_TMPL_ not defined
#endif
#ifndef M_TGT_
# error M_TGT_ not defined
#endif
#define M_HDR_ template< M_TMPL_ >
#define M_ITER_( NAME ) typename M_TGT_::T_ ##NAME
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_ITER_( ConstIterator ) M_TGT_::begin( ) const noexcept
{
return T_ConstIterator( *this , 0 );
}
M_HDR_ inline M_ITER_( ConstIterator ) M_TGT_::end( ) const noexcept
{
return T_ConstIterator( *this , size( ) );
}
M_HDR_ inline M_ITER_( Iterator ) M_TGT_::begin( ) noexcept
{
return T_Iterator( *this , 0 );
}
M_HDR_ inline M_ITER_( Iterator ) M_TGT_::end( ) noexcept
{
return T_Iterator( *this , size( ) );
}
M_HDR_ inline M_ITER_( ConstReverseIterator ) M_TGT_::rbegin( ) const noexcept
{
return T_ConstReverseIterator( *this , 0 );
}
M_HDR_ inline M_ITER_( ConstReverseIterator ) M_TGT_::rend( ) const noexcept
{
return T_ConstReverseIterator( *this , size( ) );
}
M_HDR_ inline M_ITER_( ReverseIterator ) M_TGT_::rbegin( ) noexcept
{
return T_ReverseIterator( *this , size( ) - 1 );
}
M_HDR_ inline M_ITER_( ReverseIterator ) M_TGT_::rend( ) noexcept
{
return T_ReverseIterator( *this , -1 );
}
M_HDR_ inline M_ITER_( ConstIterator ) M_TGT_::cbegin( ) const noexcept
{
return T_ConstIterator( *this , 0 );
}
M_HDR_ inline M_ITER_( ConstIterator ) M_TGT_::cend( ) const noexcept
{
return T_ConstIterator( *this , size( ) );
}
M_HDR_ inline M_ITER_( ConstReverseIterator ) M_TGT_::crbegin( ) const noexcept
{
return T_ConstReverseIterator( *this , 0 );
}
M_HDR_ inline M_ITER_( ConstReverseIterator ) M_TGT_::crend( ) const noexcept
{
return T_ConstReverseIterator( *this , size( ) );
}
/*----------------------------------------------------------------------------*/
#undef M_ITER_
#undef M_HDR_
#undef M_TMPL_
#undef M_TGT_

View file

@ -0,0 +1,17 @@
T_ConstIterator begin( ) const noexcept;
T_ConstIterator end( ) const noexcept;
T_ConstReverseIterator rbegin( ) const noexcept;
T_ConstReverseIterator rend( ) const noexcept;
T_ConstIterator cbegin( ) const noexcept;
T_ConstIterator cend( ) const noexcept;
T_ConstReverseIterator crbegin( ) const noexcept;
T_ConstReverseIterator crend( ) const noexcept;
T_Iterator begin( ) noexcept;
T_Iterator end( ) noexcept;
T_ReverseIterator rbegin( ) noexcept;
T_ReverseIterator rend( ) noexcept;