From 4d336e70863831747b5c7b5b9d3f67798f1b748f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Fri, 3 Nov 2017 09:35:14 +0100 Subject: [PATCH] Arrays - New utility methods empty(), last() and removeLast() --- include/ebcl/Arrays.hh | 15 ++++++ include/ebcl/inline/Arrays.hh | 87 ++++++++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/include/ebcl/Arrays.hh b/include/ebcl/Arrays.hh index 0c5c524..ce3275a 100644 --- a/include/ebcl/Arrays.hh +++ b/include/ebcl/Arrays.hh @@ -116,6 +116,7 @@ class T_Array final uint32_t capacity( ) const noexcept; uint32_t size( ) const noexcept; uint32_t growth( ) const noexcept; + bool empty( ) const noexcept; MyType_& ensureCapacity( uint32_t capacity ) noexcept; @@ -124,6 +125,9 @@ class T_Array final T& operator[] ( uint32_t index ) noexcept; T const& operator[] ( uint32_t index ) const noexcept; + T& last( ) noexcept; + T const& last( ) const noexcept; + int32_t indexOf( T const& item ) const noexcept; bool contains( T const& item ) const noexcept; @@ -162,6 +166,7 @@ class T_Array final void remove( uint32_t index ) noexcept; void removeSwap( uint32_t index ) noexcept; + void removeLast( ) noexcept; // --------------------------------------------------------------------- @@ -251,12 +256,16 @@ class T_StaticArray final constexpr uint32_t capacity( ) const noexcept; uint32_t size( ) const noexcept; + bool empty( ) const noexcept; // --------------------------------------------------------------------- Type& operator[] ( uint32_t index ) noexcept; Type const& operator[] ( uint32_t index ) const noexcept; + Type& last( ) noexcept; + Type const& last( ) const noexcept; + int32_t indexOf( Type const& item ) const noexcept; bool contains( Type const& item ) const noexcept; @@ -293,6 +302,7 @@ class T_StaticArray final void remove( uint32_t index ) noexcept; void removeSwap( uint32_t index ) noexcept; + void removeLast( ) noexcept; // --------------------------------------------------------------------- @@ -444,6 +454,7 @@ template< uint32_t size( ) const noexcept; constexpr uint32_t growth( ) const noexcept; bool isStatic( ) const noexcept; + bool empty( ) const noexcept; T_Self& ensureCapacity( uint32_t capacity ) noexcept; @@ -452,6 +463,9 @@ template< T& operator[] ( uint32_t index ) noexcept; T const& operator[] ( uint32_t index ) const noexcept; + T& last( ) noexcept; + T const& last( ) const noexcept; + int32_t indexOf( T const& item ) const noexcept; bool contains( T const& item ) const noexcept; @@ -490,6 +504,7 @@ template< void remove( uint32_t index ) noexcept; void removeSwap( uint32_t index ) noexcept; + void removeLast( ) noexcept; // --------------------------------------------------------------------- diff --git a/include/ebcl/inline/Arrays.hh b/include/ebcl/inline/Arrays.hh index ea1bb86..594aed1 100644 --- a/include/ebcl/inline/Arrays.hh +++ b/include/ebcl/inline/Arrays.hh @@ -147,6 +147,12 @@ inline uint32_t T_Array< T >::growth( ) const noexcept return growth_; } +template< typename T > +inline bool T_Array< T >::empty( ) const noexcept +{ + return size_ != 0; +} + /*----------------------------------------------------------------------------*/ template< typename T > @@ -191,6 +197,20 @@ inline T const& T_Array< T >::operator[] ( return data_[ index ]; } +template< typename T > +inline T& T_Array< T >::last( ) noexcept +{ + assert( !empty( ) ); + return data_[ size_ - 1 ]; +} + +template< typename T > +inline T const& T_Array< T >::last( ) const noexcept +{ + assert( !empty( ) ); + return data_[ size_ - 1 ]; +} + /*----------------------------------------------------------------------------*/ template< typename T > @@ -401,6 +421,14 @@ inline void T_Array< T >::removeSwap( size_ --; } +template< typename T > +inline void T_Array< T >::removeLast( ) noexcept +{ + assert( size_ ); + data_[ size_ - 1 ].~T( ); + size_ --; +} + /*----------------------------------------------------------------------------*/ template< typename T > @@ -1076,6 +1104,12 @@ inline uint32_t T_StaticArray< T , S >::size( ) const noexcept return size_; } +template< typename T , uint32_t S > +inline bool T_StaticArray< T , S >::empty( ) const noexcept +{ + return size_ != 0; +} + /*----------------------------------------------------------------------------*/ template< typename T , uint32_t S > @@ -1094,6 +1128,20 @@ inline T const& T_StaticArray< T , S >::operator[] ( return *reinterpret_cast< T const* >( &storage_[ index ] ); } +template< typename T , uint32_t S > +inline T& T_StaticArray< T , S >::last( ) noexcept +{ + assert( !empty( ) ); + return *reinterpret_cast< T* >( &storage_[ size_ - 1 ] ); +} + +template< typename T , uint32_t S > +inline T const& T_StaticArray< T , S >::last( ) const noexcept +{ + assert( !empty( ) ); + return *reinterpret_cast< T const* >( &storage_[ size_ - 1 ] ); +} + /*----------------------------------------------------------------------------*/ template< typename T , uint32_t S > @@ -1307,6 +1355,14 @@ inline void T_StaticArray< T , S >::removeSwap( size_ --; } +template< typename T , uint32_t S > +inline void T_StaticArray< T , S >::removeLast( ) noexcept +{ + assert( size_ ); + (*this)[ size_ - 1 ].~T( ); + size_ --; +} + /*----------------------------------------------------------------------------*/ template< typename T , uint32_t S > @@ -1681,6 +1737,12 @@ inline bool T_AutoArray< T , S , G >::isStatic( ) const noexcept return array_.template hasType< T_Static_ >( ); } +template< typename T , uint32_t S , uint32_t G > +inline bool T_AutoArray< T , S , G >::empty( ) const noexcept +{ + return size( ) != 0; +} + /*----------------------------------------------------------------------------*/ template< typename T , uint32_t S , uint32_t G > @@ -1713,6 +1775,20 @@ inline T const& T_AutoArray< T , S , G >::operator[] ( return isStatic( ) ? static_( )[ index ] : dynamic_( )[ index ]; } +template< typename T , uint32_t S , uint32_t G > +inline T& T_AutoArray< T , S , G >::last( ) noexcept +{ + assert( !empty( ) ); + return (*this)[ size( ) - 1 ]; +} + +template< typename T , uint32_t S , uint32_t G > +inline T const& T_AutoArray< T , S , G >::last( ) const noexcept +{ + assert( !empty( ) ); + return (*this)[ size( ) - 1 ]; +} + /*----------------------------------------------------------------------------*/ template< typename T , uint32_t S , uint32_t G > @@ -1881,7 +1957,7 @@ template< typename T , uint32_t S , uint32_t G > inline void T_AutoArray< T , S , G >::remove( const uint32_t index ) noexcept { - return isStatic( ) + isStatic( ) ? static_( ).remove( index ) : dynamic_( ).remove( index ); } @@ -1890,11 +1966,18 @@ template< typename T , uint32_t S , uint32_t G > inline void T_AutoArray< T , S , G >::removeSwap( const uint32_t index ) noexcept { - return isStatic( ) + isStatic( ) ? static_( ).removeSwap( index ) : dynamic_( ).removeSwap( index ); } +template< typename T , uint32_t S , uint32_t G > +inline void T_AutoArray< T , S , G >::removeLast( ) noexcept +{ + assert( !empty( ) ); + remove( size( ) - 1 ); +} + /*----------------------------------------------------------------------------*/ template< typename T , uint32_t S , uint32_t G >