19 #ifndef GEOSX_DATAREPOSITORY_MAPPEDVECTOR_HPP_ 20 #define GEOSX_DATAREPOSITORY_MAPPEDVECTOR_HPP_ 47 typename INDEX_TYPE =
int >
51 static_assert( std::is_same< T_PTR, T * >::value || std::is_same< T_PTR, std::unique_ptr< T > >::value,
52 "invalid second template argument for MappedVector<T,T_PTR,KEY_TYPE,INDEX_TYPE>. Allowable types are T * and std::unique_ptr<T>." );
64 using value_type =
typename std::pair< KEY_TYPE, T_PTR >;
82 using pointer =
typename valueContainer::pointer;
88 using reference =
typename valueContainer::reference;
94 using size_type =
typename valueContainer::size_type;
97 using iterator =
typename constKeyValueContainer::iterator;
160 index<static_cast< INDEX_TYPE >( m_values.size() ) ) ?
const_cast< T
const *
>(&(*(m_values[index].second))) :
nullptr;
176 inline T
const *
operator[]( KEY_TYPE
const & keyName )
const 178 typename LookupMapType::const_iterator iter = m_keyLookup.find( keyName );
179 return ( iter!=m_keyLookup.end() ? this->
operator[]( iter->second ) : nullptr );
197 INDEX_TYPE index = keyIndex.
Index();
204 #ifdef MAPPED_VECTOR_RANGE_CHECKING 205 else if( m_values[index].first!=keyIndex.
Key() )
236 {
return m_constKeyValues.begin(); }
243 {
return m_constValues.begin(); }
250 {
return m_constValues.begin(); }
257 {
return m_constKeyValues.end(); }
264 {
return m_constValues.end(); }
271 {
return m_constValues.end(); }
280 inline INDEX_TYPE
getIndex( KEY_TYPE
const & key )
const 282 typename LookupMapType::const_iterator iter = m_keyLookup.find( key );
300 T *
insert( KEY_TYPE
const & keyName,
303 bool overwrite =
false );
317 deleteValue( index );
320 m_keyLookup.erase( m_values[index].first );
323 m_values.erase( m_values.begin() + index );
324 m_ownsValues.erase( m_ownsValues.begin() + index );
327 m_constKeyValues.resize( index );
328 m_constValues.resize( index );
329 for(
typename valueContainer::size_type i = index; i < m_values.size(); ++i )
331 m_constKeyValues.emplace_back( m_values[i].first, rawPtr( index ) );
332 m_constValues.emplace_back( m_values[i].first, rawPtr( index ) );
336 for(
typename valueContainer::size_type i = index; i < m_values.size(); ++i )
338 m_keyLookup[m_values[i].first] = i;
350 typename LookupMapType::const_iterator iter = m_keyLookup.find( key );
351 if( iter!=m_keyLookup.end() )
353 erase( iter->second );
365 INDEX_TYPE index = keyIndex.
Index();
380 for(
typename valueContainer::size_type a = 0; a < m_values.size(); ++a )
382 deleteValue( LvArray::integerConversion< INDEX_TYPE >( a ) );
384 m_constKeyValues.clear();
385 m_constValues.clear();
387 m_ownsValues.clear();
400 return LvArray::integerConversion< INDEX_TYPE >( m_values.size() );
408 {
return this->m_values; }
415 {
return this->m_constValues; }
422 {
return m_keyLookup; }
427 T * rawPtr( INDEX_TYPE index )
429 return &(*(m_values[index].second));
432 template<
typename U = T_PTR >
433 typename std::enable_if< std::is_same< U, T * >::value,
void >::type
434 deleteValue( INDEX_TYPE index )
436 if( m_ownsValues[index] )
438 delete m_values[index].second;
442 template<
typename U = T_PTR >
443 typename std::enable_if< !std::is_same< U, T * >::value,
void >::type
460 std::vector< int > m_ownsValues;
463 template<
typename T,
typename T_PTR,
typename KEY_TYPE,
typename INDEX_TYPE >
470 typename LookupMapType::iterator iterKeyLookup = m_keyLookup.find( keyName );
474 if( iterKeyLookup == m_keyLookup.end() )
476 value_type newEntry = std::make_pair( keyName, std::move( source ) );
477 m_values.push_back( std::move( newEntry ) );
479 index =
static_cast< INDEX_TYPE
>(m_values.size()) - 1;
480 m_ownsValues.resize( index + 1 );
483 m_ownsValues[index] =
true;
486 m_keyLookup.insert( std::make_pair( keyName, index ) );
487 m_constKeyValues.emplace_back( keyName, rawPtr( index ) );
488 m_constValues.emplace_back( keyName, rawPtr( index ) );
494 index = iterKeyLookup->second;
498 m_ownsValues[index] =
true;
502 if( m_values[index].second==
nullptr )
504 m_values[index].second = std::move( source );
505 m_constKeyValues[index].second = rawPtr( index );
506 m_constValues[index].second = rawPtr( index );
512 deleteValue( index );
513 m_values[index].second = std::move( source );
514 m_constKeyValues[index].second = rawPtr( index );
515 m_constValues[index].second = rawPtr( index );
517 else if( source->get_typeid() != m_values[index].second->get_typeid() )
519 GEOSX_ERROR(
"MappedVector::insert(): Tried to insert existing key ("<<keyName<<
520 ") with a different type without overwrite flag\n"<<
" "<<source->get_typeid().name()<<
" != "<<
521 m_values[index].second->get_typeid().name() );
530 return &(*(m_values[index].second));
void setIndex(INDEX_TYPE const &index) const
Set the index.
typename constValueContainer::const_iterator const_iterator
the iterator to const type of the value container
T const * operator[](INDEX_TYPE index) const
typename valueContainer::pointer pointer
the pointer type of the value container
void clear()
function to clear the MappedVector
typename constValueContainer::const_reverse_iterator const_reverse_iterator
the reverse iterator to const type of the value container
typename constKeyValueContainer::reverse_iterator reverse_iterator
the reverse iterator type of the value container
std::vector< const_value_type > constValueContainer
a const type of the vector container
keyType key_type
The type used for the key of the map.
typename valueContainer::const_pointer const_pointer
the pointer to const type of the value container
Contains portable access to std::numeric_limits and functions for converting between integral types...
typename std::pair< keyType const, Group * > const_key_value_type
the type of the values with const keys held in the vector
MappedVector & operator=(MappedVector const &)=default
Default copy assignment operator.
T * insert(KEY_TYPE const &keyName, T_PTR source, bool takeOwnership, bool overwrite=false)
insert new entry into MappedVector
const_iterator begin() const
std::unordered_map< keyType, indexType > LookupMapType
the type of the lookup map
MappedVector()=default
deleted default constructor
LookupMapType const & keys() const
access for key lookup
typename valueContainer::reference reference
reference type of the value container
typename std::pair< keyType, Group * > value_type
the type of the values held in the vector
std::vector< const_key_value_type > constKeyValueContainer
a const type of the vector container
~MappedVector()
default destructor
typename valueContainer::size_type size_type
the size_type of the value container
void erase(INDEX_TYPE index)
Remove element at given index.
typename std::pair< keyType const, Group const * > const_value_type
a const type of the values held in the vector
static constexpr INDEX_TYPE invalid_index
the value of an invalid index
T const * operator[](KeyIndex const &keyIndex) const
#define GEOSX_ERROR(msg)
Raise a hard error and terminate the program.
valueContainer const & values()
access for value container
void erase(KEY_TYPE const &key)
Remove element at given key.
KEY_TYPE const & Key() const
Access for the key.
void erase(KeyIndex &keyIndex)
Remove element at given key.
constValueContainer const & values() const
access for value container
T const * operator[](KEY_TYPE const &keyName) const
T * operator[](INDEX_TYPE index)
const_iterator end() const
typename valueContainer::const_reference const_reference
reference to const type of the value container
std::string string
String type.
std::vector< value_type > valueContainer
the type of the vector container
INDEX_TYPE getIndex(KEY_TYPE const &key) const
INDEX_TYPE size() const
function to return the number of entries stored
#define GEOSX_UNUSED_PARAM(X)
Mark an unused argument and silence compiler warnings.
typename constKeyValueContainer::iterator iterator
the iterator type of the value container
T * operator[](KeyIndex const &keyIndex)
INDEX_TYPE const & Index() const
Access for the index.
T * operator[](KEY_TYPE const &keyName)