Arrays - C++-style iterators

This commit is contained in:
Emmanuel BENOîT 2017-11-02 14:23:19 +01:00
parent 2e8092af9a
commit 90048ad3cc
5 changed files with 1444 additions and 0 deletions
include/ebcl/inline

View file

@ -463,6 +463,489 @@ inline T_Array< T > T_Array< T >::moveRange(
return output;
}
/*----------------------------------------------------------------------------*/
#define M_ITER_( NAME ) typename T_Array< T >::T_ ##NAME
template< typename T >
inline M_ITER_( ConstIterator ) T_Array< T >::begin( ) const noexcept
{
return T_ConstIterator( *this , 0 );
}
template< typename T >
inline M_ITER_( ConstIterator ) T_Array< T >::end( ) const noexcept
{
return T_ConstIterator( *this , size_ );
}
template< typename T >
inline M_ITER_( Iterator ) T_Array< T >::begin( ) noexcept
{
return T_Iterator( *this , 0 );
}
template< typename T >
inline M_ITER_( Iterator ) T_Array< T >::end( ) noexcept
{
return T_Iterator( *this , size_ );
}
template< typename T >
inline M_ITER_( ConstReverseIterator ) T_Array< T >::rbegin( ) const noexcept
{
return T_ConstReverseIterator( *this , 0 );
}
template< typename T >
inline M_ITER_( ConstReverseIterator ) T_Array< T >::rend( ) const noexcept
{
return T_ConstReverseIterator( *this , size_ );
}
template< typename T >
inline M_ITER_( ReverseIterator ) T_Array< T >::rbegin( ) noexcept
{
return T_ReverseIterator( *this , size_ - 1 );
}
template< typename T >
inline M_ITER_( ReverseIterator ) T_Array< T >::rend( ) noexcept
{
return T_ReverseIterator( *this , -1 );
}
template< typename T >
inline M_ITER_( ConstIterator ) T_Array< T >::cbegin( ) const noexcept
{
return T_ConstIterator( *this , 0 );
}
template< typename T >
inline M_ITER_( ConstIterator ) T_Array< T >::cend( ) const noexcept
{
return T_ConstIterator( *this , size_ );
}
template< typename T >
inline M_ITER_( ConstReverseIterator ) T_Array< T >::crbegin( ) const noexcept
{
return T_ConstReverseIterator( *this , 0 );
}
template< typename T >
inline M_ITER_( ConstReverseIterator ) T_Array< T >::crend( ) const noexcept
{
return T_ConstReverseIterator( *this , size_ );
}
#undef M_ITER_
/*= T_Array::T_Iterator_ =====================================================*/
#define M_HDR_ template< typename T > template< ptrdiff_t D >
#define M_CNAME_ T_Array< T >::T_Iterator_< D >
#define M_CNAME_T_ typename T_Array< T >::template T_Iterator_< D >
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_::T_Iterator_(
T_Array< T >& 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;
}
/*----------------------------------------------------------------------------*/
template< typename T , ptrdiff_t D>
inline void swap(
M_CNAME_T_& lhs ,
M_CNAME_T_& rhs ) noexcept
{
std::swap( lhs.array_ , rhs.array_ );
std::swap( lhs.pos_ , rhs.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 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( valid( ) );
pos_ += D;
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator++ (int) noexcept
{
assert( valid( ) );
auto copy( *this );
pos_ += D;
return copy;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator-- () noexcept
{
assert( valid( ) );
pos_ -= D;
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator-- (int) noexcept
{
assert( valid( ) );
auto copy( *this );
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( valid( ) );
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( valid( ) );
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 T_Array< T >* 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_
/*= T_Array::T_ConstIterator_ ================================================*/
#define M_HDR_ template< typename T > template< ptrdiff_t D >
#define M_CNAME_ T_Array< T >::T_ConstIterator_< D >
#define M_CNAME_T_ typename T_Array< T >::template T_ConstIterator_< D >
/*----------------------------------------------------------------------------*/
M_HDR_ inline M_CNAME_::T_ConstIterator_(
T_Array< T > 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;
}
/*----------------------------------------------------------------------------*/
template< typename T , ptrdiff_t D>
inline void swap(
M_CNAME_T_& lhs ,
M_CNAME_T_& rhs ) noexcept
{
std::swap( lhs.array_ , rhs.array_ );
std::swap( lhs.pos_ , rhs.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 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( valid( ) );
pos_ += D;
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator++ (int) noexcept
{
assert( valid( ) );
auto copy( *this );
pos_ += D;
return copy;
}
M_HDR_ inline M_CNAME_T_& M_CNAME_::operator-- () noexcept
{
assert( valid( ) );
pos_ -= D;
return *this;
}
M_HDR_ inline M_CNAME_T_ M_CNAME_::operator-- (int) noexcept
{
assert( valid( ) );
auto copy( *this );
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( valid( ) );
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( valid( ) );
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 T_Array< T >* 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_
/*= T_StaticArray ============================================================*/