Sets - Free & clear (inb4 blood parasites)

This commit is contained in:
Emmanuel BENOîT 2017-11-09 09:10:11 +01:00
parent e40417384d
commit 752b4977e6
2 changed files with 119 additions and 1 deletions

View file

@ -25,10 +25,14 @@ class T_SetImplementation< Type , ArrayBacked< 0 , Growth > >
uint32_t size( ) const noexcept; uint32_t size( ) const noexcept;
int32_t indexOf( Type const& item ) const noexcept; int32_t indexOf( Type const& item ) const noexcept;
Type const* access( uint32_t item ) const noexcept;
bool add( Type const& item ) noexcept; bool add( Type const& item ) noexcept;
bool add( Type&& item ) noexcept; bool add( Type&& item ) noexcept;
bool remove( Type const& item ) noexcept; bool remove( Type const& item ) noexcept;
void free( ) noexcept;
void clear( ) noexcept;
}; };
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -48,6 +52,8 @@ M_TYPE_::T_SetImplementation( ) noexcept
: data_( Growth ) : data_( Growth )
{ } { }
/*----------------------------------------------------------------------------*/
M_TMPL_ uint32_t M_TYPE_::size( ) const noexcept M_TMPL_ uint32_t M_TYPE_::size( ) const noexcept
{ {
return data_.size( ); return data_.size( );
@ -59,6 +65,14 @@ M_TMPL_ int32_t M_TYPE_::indexOf(
return data_.indexOf( item ); return data_.indexOf( item );
} }
M_TMPL_ Type const* M_TYPE_::access(
const uint32_t item ) const noexcept
{
return &data_[ item ];
}
/*----------------------------------------------------------------------------*/
M_TMPL_ bool M_TYPE_::add( M_TMPL_ bool M_TYPE_::add(
Type const& item ) noexcept Type const& item ) noexcept
{ {
@ -89,6 +103,18 @@ M_TMPL_ bool M_TYPE_::remove(
return index >= 0; return index >= 0;
} }
/*----------------------------------------------------------------------------*/
M_TMPL_ void M_TYPE_::free( ) noexcept
{
data_.free( );
}
M_TMPL_ void M_TYPE_::clear( ) noexcept
{
data_.clear( );
}
#undef M_TMPL_ #undef M_TMPL_
#undef M_TYPE_ #undef M_TYPE_
@ -106,10 +132,14 @@ class T_SetImplementation< Type , ArrayBacked< InPlace , Growth > >
public: public:
uint32_t size( ) const noexcept; uint32_t size( ) const noexcept;
int32_t indexOf( Type const& item ) const noexcept; int32_t indexOf( Type const& item ) const noexcept;
Type const* access( uint32_t item ) const noexcept;
bool add( Type const& item ) noexcept; bool add( Type const& item ) noexcept;
bool add( Type&& item ) noexcept; bool add( Type&& item ) noexcept;
bool remove( Type const& item ) noexcept; bool remove( Type const& item ) noexcept;
void free( ) noexcept;
void clear( ) noexcept;
}; };
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
@ -128,6 +158,14 @@ M_TMPL_ int32_t M_TYPE_::indexOf(
return data_.indexOf( item ); return data_.indexOf( item );
} }
M_TMPL_ Type const* M_TYPE_::access(
const uint32_t item ) const noexcept
{
return &data_[ item ];
}
/*----------------------------------------------------------------------------*/
M_TMPL_ bool M_TYPE_::add( M_TMPL_ bool M_TYPE_::add(
Type const& item ) noexcept Type const& item ) noexcept
{ {
@ -158,6 +196,18 @@ M_TMPL_ bool M_TYPE_::remove(
return index >= 0; return index >= 0;
} }
/*----------------------------------------------------------------------------*/
M_TMPL_ void M_TYPE_::free( ) noexcept
{
data_.free( );
}
M_TMPL_ void M_TYPE_::clear( ) noexcept
{
data_.clear( );
}
#undef M_TMPL_ #undef M_TMPL_
#undef M_TYPE_ #undef M_TYPE_
@ -203,6 +253,11 @@ void T_SetHelper::T_InPlaceHandler< Type , ImplTag >::shdl(
*(Type const*) rArg ); *(Type const*) rArg );
break; break;
case ACCESS:
*((Type const**) output) = ((T_Impl_ const*)rArg)->access(
*(uint32_t const*) wArg );
break;
//------------------------------------------------------------------ //------------------------------------------------------------------
case ADD_COPY: case ADD_COPY:
@ -217,6 +272,16 @@ void T_SetHelper::T_InPlaceHandler< Type , ImplTag >::shdl(
case REMOVE: case REMOVE:
*((bool*)output) = ((T_Impl_*)wArg)->remove( *((Type const*) rArg) ); *((bool*)output) = ((T_Impl_*)wArg)->remove( *((Type const*) rArg) );
break; break;
//------------------------------------------------------------------
case FREE:
((T_Impl_*)wArg)->free( );
break;
case CLEAR:
((T_Impl_*)wArg)->clear( );
break;
} }
} }
@ -261,6 +326,11 @@ void T_SetHelper::T_HeapHandler< Type , ImplTag >::shdl(
*(Type const*) rArg ); *(Type const*) rArg );
break; break;
case ACCESS:
*((Type const**) output) = (*(T_Impl_ const**)rArg)->access(
*(uint32_t const*) wArg );
break;
//------------------------------------------------------------------ //------------------------------------------------------------------
case ADD_COPY: case ADD_COPY:
@ -275,6 +345,16 @@ void T_SetHelper::T_HeapHandler< Type , ImplTag >::shdl(
case REMOVE: case REMOVE:
*((bool*)output) = (*(T_Impl_**)wArg)->remove( *((Type const*) rArg) ); *((bool*)output) = (*(T_Impl_**)wArg)->remove( *((Type const*) rArg) );
break; break;
//------------------------------------------------------------------
case FREE:
(*(T_Impl_**)wArg)->free( );
break;
case CLEAR:
(*(T_Impl_**)wArg)->clear( );
break;
} }
} }
@ -368,6 +448,15 @@ int32_t T_Set< Type >::indexOf(
return r; return r;
} }
template< typename Type >
Type const& T_Set< Type >::operator[](
uint32_t index ) const noexcept
{
Type* ptr;
handler_( T_SetHelper::ACCESS , &index , &storage_ , &ptr );
return *ptr;
}
/*----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------*/
template< typename Type > template< typename Type >
@ -397,6 +486,20 @@ bool T_Set< Type >::remove(
return r; return r;
} }
/*----------------------------------------------------------------------------*/
template< typename Type >
void T_Set< Type >::free( ) noexcept
{
handler_( T_SetHelper::FREE , &storage_ , nullptr , nullptr );
}
template< typename Type >
void T_Set< Type >::clear( ) noexcept
{
handler_( T_SetHelper::CLEAR , &storage_ , nullptr , nullptr );
}
} }
#endif // _H_EBCL_INLINE_SETS #endif // _H_EBCL_INLINE_SETS

17
Sets.hh
View file

@ -36,7 +36,7 @@ template< typename Type , typename ImplTag >
// ArrayBacked implementation tag. InPlace is the amount of items to // ArrayBacked implementation tag. InPlace is the amount of items to
// store in-place (0 will cause a T_Array to be used // store in-place (0 will cause a fully dynamic array to be used)
M_DEFINE_TEMPLATE_TAG( ArrayBacked , M_DEFINE_TEMPLATE_TAG( ArrayBacked ,
uint32_t InPlace , uint32_t InPlace ,
uint32_t Growth = 0 uint32_t Growth = 0
@ -85,6 +85,8 @@ struct T_SetHelper
SIZE , SIZE ,
// Get index of element; wArg = storage, rArg = element, output = result // Get index of element; wArg = storage, rArg = element, output = result
INDEX_OF , INDEX_OF ,
// Access an element; wArg = index , rArg = storage, output = pointer
ACCESS ,
// Add copy of element; wArg = storage, rArg = element, output = result // Add copy of element; wArg = storage, rArg = element, output = result
ADD_COPY , ADD_COPY ,
@ -92,6 +94,11 @@ struct T_SetHelper
ADD_MOVE , ADD_MOVE ,
// Delete element; wArg = storage, rArg = element, output = result // Delete element; wArg = storage, rArg = element, output = result
REMOVE , REMOVE ,
// Free memory; wArg = storage
FREE ,
// Clear contents; wArg = storage
CLEAR ,
}; };
// Handler function type // Handler function type
@ -172,6 +179,9 @@ class T_Set
// Returns underlying index of item, or -1 if not in the set // Returns underlying index of item, or -1 if not in the set
int32_t indexOf( Type const& item ) const noexcept; int32_t indexOf( Type const& item ) const noexcept;
// Access an element using its underlying index
Type const& operator[]( uint32_t index ) const noexcept;
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// Add copy of item, returns true if added // Add copy of item, returns true if added
@ -180,6 +190,11 @@ class T_Set
bool add( Type&& item ) noexcept; bool add( Type&& item ) noexcept;
// Remove item, returns true if removed // Remove item, returns true if removed
bool remove( Type const& item ) noexcept; bool remove( Type const& item ) noexcept;
// ---------------------------------------------------------------------
void clear( ) noexcept;
void free( ) noexcept;
}; };