Arrays - Predicate-based indexOf/contains methods

This commit is contained in:
Emmanuel BENOîT 2018-05-09 14:12:27 +02:00
parent d823721fb2
commit ae2ad1f8b4
7 changed files with 385 additions and 0 deletions
include/ebcl

View file

@ -147,9 +147,14 @@ class T_Array final
T& last( ) noexcept;
T const& last( ) const noexcept;
// Find items (requires comparison operator)
int32_t indexOf( T const& item ) const noexcept;
bool contains( T 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;
// ---------------------------------------------------------------------
uint32_t add( T const& item ) noexcept;
@ -286,9 +291,14 @@ class T_StaticArray final
Type& last( ) noexcept;
Type const& last( ) const noexcept;
// Find items (requires comparison operator)
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( Type const& ) > pred ) const noexcept;
bool contains( std::function< bool( Type const& ) > pred ) const noexcept;
// ---------------------------------------------------------------------
uint32_t add( Type&& item ) noexcept;
@ -488,9 +498,14 @@ template<
T& last( ) noexcept;
T const& last( ) const noexcept;
// Find items (requires comparison operator)
int32_t indexOf( T const& item ) const noexcept;
bool contains( T 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;
// ---------------------------------------------------------------------
uint32_t add( T const& item ) noexcept;

View file

@ -281,6 +281,30 @@ inline bool T_Array< T >::contains(
return indexOf( item ) != -1;
}
template< typename T >
inline int32_t T_Array< T >::indexOf(
std::function< bool( T const& ) > pred ) const noexcept
{
for ( uint32_t i = 0 ; i < size_ ; i ++ ) {
if ( pred( data_[ i ] ) ) {
return i;
}
}
return -1;
}
template< typename T >
inline bool T_Array< T >::contains(
std::function< bool( T const& ) > pred ) const noexcept
{
for ( uint32_t i = 0 ; i < size_ ; i ++ ) {
if ( pred( data_[ i ] ) ) {
return true;
}
}
return false;
}
/*----------------------------------------------------------------------------*/
template< typename T >
@ -1233,6 +1257,30 @@ inline bool T_StaticArray< T , S >::contains(
return indexOf( item ) != -1;
}
template< typename T , uint32_t S >
inline int32_t T_StaticArray< T , S >::indexOf(
std::function< bool( T const& ) > pred ) const noexcept
{
for ( uint32_t i = 0 ; i < size_ ; i ++ ) {
if ( pred( (*this)[ i ] ) ) {
return i;
}
}
return -1;
}
template< typename T , uint32_t S >
inline bool T_StaticArray< T , S >::contains(
std::function< bool( T const& ) > pred ) const noexcept
{
for ( uint32_t i = 0 ; i < size_ ; i ++ ) {
if ( pred( (*this)[ i ] ) ) {
return true;
}
}
return false;
}
/*----------------------------------------------------------------------------*/
template< typename T , uint32_t S >
@ -1883,6 +1931,24 @@ inline bool T_AutoArray< T , S , G >::contains(
return indexOf( item ) != -1;
}
template< typename T , uint32_t S , uint32_t G >
inline int32_t T_AutoArray< T , S , G >::indexOf(
std::function< bool( T const& ) > pred ) const noexcept
{
return isStatic( )
? static_( ).indexOf( std::move( pred ) )
: dynamic_( ).indexOf( std::move( pred ) );
}
template< typename T , uint32_t S , uint32_t G >
inline bool T_AutoArray< T , S , G >::contains(
std::function< bool( T const& ) > pred ) const noexcept
{
return isStatic( )
? static_( ).contains( std::move( pred ) )
: dynamic_( ).contains( std::move( pred ) );
}
/*----------------------------------------------------------------------------*/
template< typename T , uint32_t S , uint32_t G >