From 1c0527caab0fed04f152c96bb54798f0836065b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Fri, 3 Nov 2017 11:21:17 +0100 Subject: [PATCH] Arrays - resize( ) --- include/ebcl/Arrays.hh | 16 ++++++++++++++++ include/ebcl/inline/Arrays.hh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/include/ebcl/Arrays.hh b/include/ebcl/Arrays.hh index ce3275a..75ae7fc 100644 --- a/include/ebcl/Arrays.hh +++ b/include/ebcl/Arrays.hh @@ -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; diff --git a/include/ebcl/inline/Arrays.hh b/include/ebcl/inline/Arrays.hh index 594aed1..29576d1 100644 --- a/include/ebcl/inline/Arrays.hh +++ b/include/ebcl/inline/Arrays.hh @@ -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 >