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

@ -18,95 +18,39 @@ namespace ebcl {
/*= DYNAMIC ARRAYS ===========================================================*/
template< class T >
template< typename Type >
class T_Array final
{
public:
using T_Self = T_Array< Type >;
static constexpr uint32_t DEFAULT_GROWTH( )
{
return std::max< uint32_t >( 1 , 4096 / sizeof( T ) );
return std::max< uint32_t >( 1 , 4096 / sizeof( Type ) );
}
template< ptrdiff_t Direction >
class T_Iterator_ final : public std::iterator<
std::random_access_iterator_tag , T >
{
private:
T_Array< T >* array_;
ptrdiff_t pos_;
public:
T_Iterator_( T_Array< T >& array ,
const ptrdiff_t pos ) noexcept;
M_ITER_CONS_DEF( T_Iterator_ );
M_ITER_CONS_COPY( T_Iterator_ );
M_ITER_SWAP( T_Iterator_ , T_Array );
M_ITER_CMP_ALL( T_Iterator_ );
M_ITER_DEREF_RANDOM( T& , T* );
M_ITER_OPS_RANDOM( T_Iterator_ );
bool valid( ) const noexcept;
T_Array< T >* target( ) const noexcept;
ptrdiff_t pos( ) const noexcept;
};
using T_Iterator = T_Iterator_< 1 >;
using T_ReverseIterator = T_Iterator_< -1 >;
template< ptrdiff_t Direction >
class T_ConstIterator_ : public std::iterator<
std::random_access_iterator_tag , T >
{
private:
T_Array< T > const* array_;
ptrdiff_t pos_;
public:
T_ConstIterator_( T_Array< T > const& array ,
const ptrdiff_t pos ) noexcept;
T_ConstIterator_( typename T_Array< T >::T_Iterator const& iterator ) noexcept;
T_ConstIterator_& operator =(
typename T_Array< T >::T_Iterator const& iterator ) noexcept;
M_ITER_CONS_DEF( T_ConstIterator_ );
M_ITER_CONS_COPY( T_ConstIterator_ );
M_ITER_SWAP( T_ConstIterator_ , T_Array );
M_ITER_CMP_ALL( T_ConstIterator_ );
M_ITER_DEREF_RANDOM( T const& , T const* );
M_ITER_OPS_RANDOM( T_ConstIterator_ );
bool valid( ) const noexcept;
T_Array< T >* target( ) const noexcept;
ptrdiff_t pos( ) const noexcept;
};
using T_ConstIterator = T_ConstIterator_< 1 >;
using T_ConstReverseIterator = T_ConstIterator_< -1 >;
private:
typedef T_Array< T > MyType_;
T* data_;
Type* data_;
uint32_t capacity_;
uint32_t size_;
uint32_t growth_;
public:
M_TEMPLATE_POINTERS( MyType_ );
M_TEMPLATE_POINTERS( T_Self );
T_Array( ) noexcept;
explicit T_Array( uint32_t growth ) noexcept;
// ---------------------------------------------------------------------
T_Array( MyType_ const& source ) noexcept;
MyType_& operator= ( MyType_ const& other ) noexcept;
T_Array( T_Self const& source ) noexcept;
T_Self& operator= ( T_Self const& other ) noexcept;
T_Array( MyType_&& source ) noexcept;
MyType_& operator= ( MyType_&& other ) noexcept;
T_Array( T_Self&& source ) noexcept;
T_Self& operator= ( T_Self&& other ) noexcept;
T_Array( std::initializer_list< T > list ) noexcept;
MyType_& operator= ( std::initializer_list< T > list ) noexcept;
T_Array( std::initializer_list< Type > list ) noexcept;
T_Self& operator= ( std::initializer_list< Type > list ) noexcept;
template< typename TP >
friend void swap( T_Array< TP >& lhs ,
@ -116,8 +60,8 @@ class T_Array final
~T_Array( );
MyType_& clear( ) noexcept;
MyType_& free( ) noexcept;
T_Self& clear( ) noexcept;
T_Self& free( ) noexcept;
// ---------------------------------------------------------------------
@ -126,65 +70,65 @@ class T_Array final
uint32_t growth( ) const noexcept;
bool empty( ) const noexcept;
MyType_& ensureCapacity( uint32_t capacity ) noexcept;
T_Self& ensureCapacity( uint32_t capacity ) noexcept;
template<
typename Q = T ,
typename Q = Type ,
typename = std::enable_if_t< std::is_default_constructible< Q >::value >
> MyType_& resize( const uint32_t size );
> T_Self& resize( const uint32_t size );
template<
typename Q = T ,
typename Q = Type ,
typename = std::enable_if_t< std::is_copy_constructible< Q >::value >
> MyType_& resize( const uint32_t size ,
T const& value );
> T_Self& resize( const uint32_t size ,
Type const& value );
// ---------------------------------------------------------------------
T& operator[] ( uint32_t index ) noexcept;
T const& operator[] ( uint32_t index ) const noexcept;
Type& operator[] ( uint32_t index ) noexcept;
Type const& operator[] ( uint32_t index ) const noexcept;
T& last( ) noexcept;
T const& last( ) const noexcept;
Type& last( ) noexcept;
Type const& last( ) const noexcept;
// Find items (requires comparison operator)
int32_t indexOf( T const& item ) const noexcept;
bool contains( T const& item ) const noexcept;
int32_t indexOf( Type const& item ) const noexcept;
bool contains( Type const& item ) const noexcept;
// Find items based on a predicate
int32_t indexOf( std::function< bool( T const& ) > pred ) const noexcept;
bool contains( std::function< bool( T const& ) > pred ) const noexcept;
int32_t indexOf( std::function< bool( Type const& ) > pred ) const noexcept;
bool contains( std::function< bool( Type const& ) > pred ) const noexcept;
// ---------------------------------------------------------------------
uint32_t add( T const& item ) noexcept;
uint32_t add( T&& item ) noexcept;
uint32_t add( Type const& item ) noexcept;
uint32_t add( Type&& item ) noexcept;
template< typename... Args >
T& addNew( Args&& ... args );
Type& addNew( Args&& ... args );
// ---------------------------------------------------------------------
MyType_& addAll( MyType_ const& other ) noexcept;
MyType_& addAll( MyType_&& other ) noexcept;
MyType_& addAll( std::initializer_list< T > values ) noexcept;
T_Self& addAll( T_Self const& other ) noexcept;
T_Self& addAll( T_Self&& other ) noexcept;
T_Self& addAll( std::initializer_list< Type > values ) noexcept;
// ---------------------------------------------------------------------
MyType_& operator<< ( T const& item ) noexcept;
MyType_& operator<< ( T&& item ) noexcept;
MyType_& operator<< ( MyType_ const& other ) noexcept;
MyType_& operator<< ( MyType_&& other ) noexcept;
T_Self& operator<< ( Type const& item ) noexcept;
T_Self& operator<< ( Type&& item ) noexcept;
T_Self& operator<< ( T_Self const& other ) noexcept;
T_Self& operator<< ( T_Self&& other ) noexcept;
// ---------------------------------------------------------------------
void insert( uint32_t index ,
T const& item ) noexcept;
Type const& item ) noexcept;
void insert( uint32_t index ,
T&& item ) noexcept;
Type&& item ) noexcept;
template< typename... Args >
T& insertNew( uint32_t index ,
Type& insertNew( uint32_t index ,
Args&& ... args );
// ---------------------------------------------------------------------
@ -195,39 +139,24 @@ class T_Array final
// ---------------------------------------------------------------------
void sort( F_Comparator< T > cmp = T_Comparator< T >::compare ) noexcept;
void sort( F_Comparator< Type > cmp = T_Comparator< Type >::compare ) noexcept;
void sort( uint32_t first ,
uint32_t items ,
F_Comparator< T > cmp = T_Comparator< T >::compare ) noexcept;
F_Comparator< Type > cmp = T_Comparator< Type >::compare ) noexcept;
// ---------------------------------------------------------------------
MyType_ copyRange( uint32_t first ,
T_Self copyRange( uint32_t first ,
uint32_t last = UINT32_MAX ) const noexcept;
MyType_ moveRange( uint32_t first ,
T_Self moveRange( uint32_t first ,
uint32_t last = UINT32_MAX ) noexcept;
// ---------------------------------------------------------------------
// Iterators for C++ for loops
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;
// C++ iterators
# include "ebcl/bits/ArrayIteratorDecl.hh"
# include "ebcl/bits/ArrayConstIteratorDecl.hh"
# include "ebcl/bits/IteratorMethodsDecl.hh"
};
// Instantiate some common types directly
@ -347,6 +276,12 @@ class T_StaticArray final
uint32_t last = UINT32_MAX ) const noexcept;
T_Self moveRange( uint32_t first ,
uint32_t last = UINT32_MAX ) noexcept;
// ---------------------------------------------------------------------
// C++ iterators
# include "ebcl/bits/ArrayIteratorDecl.hh"
# include "ebcl/bits/ArrayConstIteratorDecl.hh"
# include "ebcl/bits/IteratorMethodsDecl.hh"
};
@ -442,16 +377,16 @@ extern template class T_MultiArray< uint32_t >;
*/
template<
typename T , uint32_t StaticSize ,
typename Type , uint32_t StaticSize ,
uint32_t DynamicGrowth = StaticSize * 4
> class T_AutoArray
{
public:
using T_Self = T_AutoArray< T , StaticSize , DynamicGrowth >;
using T_Self = T_AutoArray< Type , StaticSize , DynamicGrowth >;
private:
using T_Static_ = T_StaticArray< T , StaticSize >;
using T_Dynamic_ = T_Array< T >;
using T_Static_ = T_StaticArray< Type , StaticSize >;
using T_Dynamic_ = T_Array< Type >;
T_Union< T_Static_ , T_Dynamic_ > array_;
@ -492,27 +427,27 @@ template<
// ---------------------------------------------------------------------
T& operator[] ( uint32_t index ) noexcept;
T const& operator[] ( uint32_t index ) const noexcept;
Type& operator[] ( uint32_t index ) noexcept;
Type const& operator[] ( uint32_t index ) const noexcept;
T& last( ) noexcept;
T const& last( ) const noexcept;
Type& last( ) noexcept;
Type const& last( ) const noexcept;
// Find items (requires comparison operator)
int32_t indexOf( T const& item ) const noexcept;
bool contains( T const& item ) const noexcept;
int32_t indexOf( Type const& item ) const noexcept;
bool contains( Type const& item ) const noexcept;
// Find items based on a predicate
int32_t indexOf( std::function< bool( T const& ) > pred ) const noexcept;
bool contains( std::function< bool( T const& ) > pred ) const noexcept;
int32_t indexOf( std::function< bool( Type const& ) > pred ) const noexcept;
bool contains( std::function< bool( Type const& ) > pred ) const noexcept;
// ---------------------------------------------------------------------
uint32_t add( T const& item ) noexcept;
uint32_t add( T&& item ) noexcept;
uint32_t add( Type const& item ) noexcept;
uint32_t add( Type&& item ) noexcept;
template< typename... Args >
T& addNew( Args&& ... args );
Type& addNew( Args&& ... args );
// ---------------------------------------------------------------------
@ -521,20 +456,20 @@ template<
// ---------------------------------------------------------------------
T_Self& operator<< ( T const& item ) noexcept;
T_Self& operator<< ( T&& item ) noexcept;
T_Self& operator<< ( Type const& item ) noexcept;
T_Self& operator<< ( Type&& item ) noexcept;
T_Self& operator<< ( T_Self const& other ) noexcept;
T_Self& operator<< ( T_Self&& other ) noexcept;
// ---------------------------------------------------------------------
void insert( uint32_t index ,
T const& item ) noexcept;
Type const& item ) noexcept;
void insert( uint32_t index ,
T&& item ) noexcept;
Type&& item ) noexcept;
template< typename... Args >
T& insertNew( uint32_t index ,
Type& insertNew( uint32_t index ,
Args&& ... args );
// ---------------------------------------------------------------------
@ -545,10 +480,10 @@ template<
// ---------------------------------------------------------------------
void sort( F_Comparator< T > cmp = T_Comparator< T >::compare ) noexcept;
void sort( F_Comparator< Type > cmp = T_Comparator< Type >::compare ) noexcept;
void sort( uint32_t first ,
uint32_t items ,
F_Comparator< T > cmp = T_Comparator< T >::compare ) noexcept;
F_Comparator< Type > cmp = T_Comparator< Type >::compare ) noexcept;
// ---------------------------------------------------------------------
@ -559,6 +494,12 @@ template<
uint32_t last = UINT32_MAX ) noexcept;
// ---------------------------------------------------------------------
// C++ iterators
# include "ebcl/bits/ArrayIteratorDecl.hh"
# include "ebcl/bits/ArrayConstIteratorDecl.hh"
# include "ebcl/bits/IteratorMethodsDecl.hh"
// ---------------------------------------------------------------------
private:
void convertToDynamic_( uint32_t capacity ) noexcept;

View file

@ -276,11 +276,8 @@ struct UseTag
NAME& operator =( NAME&& other ) noexcept
// Swap (all types)
#define M_ITER_SWAP( NAME , CONTAINER ) \
template< typename SwapType_ > \
friend void swap( typename CONTAINER< SwapType_ >::NAME& lhs , \
typename CONTAINER< SwapType_ >::NAME& rhs \
) noexcept
#define M_ITER_SWAP( NAME ) \
NAME& swap( NAME& other ) noexcept
// Equality/inequality (all types)
#define M_ITER_CMP_EQ( NAME ) \

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;

View file

@ -535,495 +535,24 @@ inline T_Array< T > T_Array< T >::moveRange(
/*----------------------------------------------------------------------------*/
#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_
#define M_TMPL_ typename T
#define M_TGT_ T_Array< T >
#include "ebcl/bits/ArrayIteratorMethods.hh"
/*= 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( 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 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_
#define M_TMPL_ typename T
#define M_TGT_ T_Array< T >
#include "ebcl/bits/ArrayIterator.hh"
/*= 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 >
#define M_TMPL_ typename T
#define M_TGT_ T_Array< T >
#include "ebcl/bits/ArrayConstIterator.hh"
/*----------------------------------------------------------------------------*/
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( 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 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 ============================================================*/
@ -1465,6 +994,26 @@ inline T_StaticArray< T , S > T_StaticArray< T , S >::moveRange(
return output;
}
/*----------------------------------------------------------------------------*/
#define M_TMPL_ typename T , uint32_t SZ
#define M_TGT_ T_StaticArray< T , SZ >
#include "ebcl/bits/ArrayIteratorMethods.hh"
/*= T_StaticArray::T_Iterator_ ===============================================*/
#define M_TMPL_ typename T , uint32_t SZ
#define M_TGT_ T_StaticArray< T , SZ >
#include "ebcl/bits/ArrayIterator.hh"
/*= T_StaticArray::T_ConstIterator_ ==========================================*/
#define M_TMPL_ typename T , uint32_t SZ
#define M_TGT_ T_StaticArray< T , SZ >
#include "ebcl/bits/ArrayConstIterator.hh"
/*= T_MultiArray ============================================================-*/
@ -2222,6 +1771,26 @@ inline T_Array< T > const& T_AutoArray< T , S , G >::dynamic_( ) const noexcept
return array_.template value< T_Dynamic_ >( );
}
/*----------------------------------------------------------------------------*/
#define M_TMPL_ typename T , uint32_t S , uint32_t G
#define M_TGT_ T_AutoArray< T , S , G >
#include "ebcl/bits/ArrayIteratorMethods.hh"
/*= T_StaticArray::T_Iterator_ ===============================================*/
#define M_TMPL_ typename T , uint32_t S , uint32_t G
#define M_TGT_ T_AutoArray< T , S , G >
#include "ebcl/bits/ArrayIterator.hh"
/*= T_StaticArray::T_ConstIterator_ ==========================================*/
#define M_TMPL_ typename T , uint32_t S , uint32_t G
#define M_TGT_ T_AutoArray< T , S , G >
#include "ebcl/bits/ArrayConstIterator.hh"
} // namespace
#endif // _H_EBCL_INLINE_ARRAYS