KV tables - getOrCreate

This commit is contained in:
Emmanuel BENOîT 2017-11-05 13:00:35 +01:00
parent c35f4c6644
commit 1b1be9e6b2
2 changed files with 20 additions and 0 deletions

View file

@ -99,6 +99,9 @@ class T_KeyValueTable
ValueType const * get( KeyType const& k ) const;
ValueType * get( KeyType const& k );
template< typename... ArgTypes >
ValueType& getOrCreate( KeyType const& k , ArgTypes&&... args );
ValueType& operator[] ( uint32_t index );
ValueType const& operator[] ( uint32_t index ) const;

View file

@ -185,6 +185,23 @@ inline V* T_KeyValueTable< K , V >::get( K const& k )
return &values_[ idx ];
}
template< typename K , typename V >
template< typename... ArgTypes >
V& T_KeyValueTable< K , V >::getOrCreate(
K const& k ,
ArgTypes&&... args )
{
const uint32_t hash = ComputeHash( k );
uint32_t idx = find( k , hash );
if ( idx != T_HashIndex::INVALID_INDEX ) {
return values_[ idx ];
}
index_.add( hash );
keys_.add( k );
return values_.addNew( std::forward< ArgTypes >( args ) ... );
}
/*----------------------------------------------------------------------------*/
template< typename K , typename V >