KV tables - getOrCreate
This commit is contained in:
parent
c35f4c6644
commit
1b1be9e6b2
2 changed files with 20 additions and 0 deletions
|
@ -99,6 +99,9 @@ class T_KeyValueTable
|
||||||
ValueType const * get( KeyType const& k ) const;
|
ValueType const * get( KeyType const& k ) const;
|
||||||
ValueType * get( KeyType const& k );
|
ValueType * get( KeyType const& k );
|
||||||
|
|
||||||
|
template< typename... ArgTypes >
|
||||||
|
ValueType& getOrCreate( KeyType const& k , ArgTypes&&... args );
|
||||||
|
|
||||||
ValueType& operator[] ( uint32_t index );
|
ValueType& operator[] ( uint32_t index );
|
||||||
ValueType const& operator[] ( uint32_t index ) const;
|
ValueType const& operator[] ( uint32_t index ) const;
|
||||||
|
|
||||||
|
|
|
@ -185,6 +185,23 @@ inline V* T_KeyValueTable< K , V >::get( K const& k )
|
||||||
return &values_[ idx ];
|
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 >
|
template< typename K , typename V >
|
||||||
|
|
Loading…
Reference in a new issue