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/inline

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 >