Arrays - resize( )

This commit is contained in:
Emmanuel BENOîT 2017-11-03 11:21:17 +01:00
parent 4d336e7086
commit 1c0527caab
2 changed files with 47 additions and 0 deletions

View file

@ -11,6 +11,11 @@
namespace ebcl {
// TODO:
// * add addAll({})
// * add ={} / cons({})
/*= DYNAMIC ARRAYS ===========================================================*/
template< class T >
@ -120,6 +125,17 @@ class T_Array final
MyType_& ensureCapacity( uint32_t capacity ) noexcept;
template<
typename Q = T ,
typename = std::enable_if_t< std::is_default_constructible< Q >::value >
> MyType_& resize( const uint32_t size );
template<
typename Q = T ,
typename = std::enable_if_t< std::is_copy_constructible< Q >::value >
> MyType_& resize( const uint32_t size ,
T const& value );
// ---------------------------------------------------------------------
T& operator[] ( uint32_t index ) noexcept;

View file

@ -179,6 +179,37 @@ inline T_Array< T >& T_Array< T >::ensureCapacity(
return *this;
}
template< typename T >
template< typename Q , typename >
inline T_Array< T >& T_Array< T >::resize(
const uint32_t size )
{
if ( size > size_ ) {
ensureCapacity( size );
for ( auto i = size_ ; i < size_ ; i ++ ) {
::new ( reinterpret_cast< char* >( &data_[ i ] ) ) T( );
}
}
size_ = size;
return *this;
}
template< typename T >
template< typename Q , typename >
inline T_Array< T >& T_Array< T >::resize(
const uint32_t size ,
T const& value )
{
if ( size > size_ ) {
ensureCapacity( size );
for ( auto i = size_ ; i < size_ ; i ++ ) {
::new ( reinterpret_cast< char* >( &data_[ i ] ) ) T( value );
}
}
size_ = size;
return *this;
}
/*----------------------------------------------------------------------------*/
template< typename T >