20 #ifndef GEOS_DATAREPOSITORY_MAPPEDVECTOR_HPP_
21 #define GEOS_DATAREPOSITORY_MAPPEDVECTOR_HPP_
27 #include "LvArray/src/limits.hpp"
48 typename INDEX_TYPE =
int >
52 static_assert( std::is_same< T_PTR, T * >::value || std::is_same< T_PTR, std::unique_ptr< T > >::value,
53 "invalid second template argument for MappedVector<T,T_PTR,KEY_TYPE,INDEX_TYPE>. Allowable types are T * and std::unique_ptr<T>." );
65 using value_type =
typename std::pair< KEY_TYPE, T_PTR >;
83 using pointer =
typename valueContainer::pointer;
89 using reference =
typename valueContainer::reference;
95 using size_type =
typename valueContainer::size_type;
98 using iterator =
typename constKeyValueContainer::iterator;
161 index<
static_cast< INDEX_TYPE
>( m_values.size() ) ) ?
const_cast< T
const *
>(&(*(m_values[index].second))) :
nullptr;
177 inline T
const *
operator[]( KEY_TYPE
const & keyName )
const
179 typename LookupMapType::const_iterator iter = m_keyLookup.find( keyName );
180 return ( iter!=m_keyLookup.end() ? this->operator[]( iter->second ) :
nullptr );
198 INDEX_TYPE index = keyIndex.
index();
205 #ifdef MAPPED_VECTOR_RANGE_CHECKING
206 else if( m_values[index].first!=keyIndex.Key() )
237 {
return m_constKeyValues.begin(); }
244 {
return m_constValues.begin(); }
251 {
return m_constValues.begin(); }
258 {
return m_constKeyValues.end(); }
265 {
return m_constValues.end(); }
272 {
return m_constValues.end(); }
281 inline INDEX_TYPE
getIndex( KEY_TYPE
const & key )
const
283 typename LookupMapType::const_iterator iter = m_keyLookup.find( key );
304 bool overwrite =
false );
318 deleteValue( index );
321 m_keyLookup.erase( m_values[index].first );
324 m_values.erase( m_values.begin() + index );
325 m_ownsValues.erase( m_ownsValues.begin() + index );
328 m_constKeyValues.resize( index );
329 m_constValues.resize( index );
330 for(
typename valueContainer::size_type i = index; i < m_values.size(); ++i )
332 m_constKeyValues.emplace_back( m_values[i].first, rawPtr( i ) );
333 m_constValues.emplace_back( m_values[i].first, rawPtr( i ) );
337 for(
typename valueContainer::size_type i = index; i < m_values.size(); ++i )
339 m_keyLookup[m_values[i].first] = i;
351 typename LookupMapType::const_iterator iter = m_keyLookup.find( key );
352 if( iter!=m_keyLookup.end() )
354 erase( iter->second );
366 INDEX_TYPE index = keyIndex.Index();
381 for(
typename valueContainer::size_type a = 0; a < m_values.size(); ++a )
383 deleteValue( LvArray::integerConversion< INDEX_TYPE >( a ) );
385 m_constKeyValues.clear();
386 m_constValues.clear();
388 m_ownsValues.clear();
401 return LvArray::integerConversion< INDEX_TYPE >( m_values.size() );
409 {
return this->m_values; }
416 {
return this->m_constValues; }
423 {
return m_keyLookup; }
428 T * rawPtr( INDEX_TYPE index )
430 return &(*(m_values[index].second));
433 template<
typename U = T_PTR >
434 typename std::enable_if< std::is_same< U, T * >::value,
void >::type
435 deleteValue( INDEX_TYPE index )
437 if( m_ownsValues[index] )
439 delete m_values[index].second;
443 template<
typename U = T_PTR >
444 typename std::enable_if< !std::is_same< U, T * >::value,
void >::type
461 std::vector< int > m_ownsValues;
464 template<
typename T,
typename T_PTR,
typename KEY_TYPE,
typename INDEX_TYPE >
470 INDEX_TYPE index = KeyIndex::invalid_index;
471 typename LookupMapType::iterator iterKeyLookup = m_keyLookup.find( keyName );
475 if( iterKeyLookup == m_keyLookup.end() )
477 value_type newEntry = std::make_pair( keyName, std::move( source ) );
478 m_values.push_back( std::move( newEntry ) );
480 index =
static_cast< INDEX_TYPE
>(m_values.size()) - 1;
481 m_ownsValues.resize( index + 1 );
484 m_ownsValues[index] =
true;
487 m_keyLookup.insert( std::make_pair( keyName, index ) );
488 m_constKeyValues.emplace_back( keyName, rawPtr( index ) );
489 m_constValues.emplace_back( keyName, rawPtr( index ) );
495 index = iterKeyLookup->second;
499 m_ownsValues[index] =
true;
503 if( m_values[index].second==
nullptr )
505 m_values[index].second = std::move( source );
506 m_constKeyValues[index].second = rawPtr( index );
507 m_constValues[index].second = rawPtr( index );
513 deleteValue( index );
514 m_values[index].second = std::move( source );
515 m_constKeyValues[index].second = rawPtr( index );
516 m_constValues[index].second = rawPtr( index );
518 else if(
typeid( source ) !=
typeid( m_values[index].second ) )
520 GEOS_ERROR(
"MappedVector::insert(): Tried to insert existing key (" << keyName <<
521 ") with a different type without overwrite flag\n " <<
" " << LvArray::system::demangleType( source ) <<
522 " != " << LvArray::system::demangleType( m_values[ index ].second ) );
531 return &(*(m_values[index].second));
#define GEOS_UNUSED_PARAM(X)
Mark an unused argument and silence compiler warnings.
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
constexpr static INDEX_TYPE invalid_index
the value of an invalid index
void setIndex(INDEX_TYPE const &index) const
Set the index.
KEY_TYPE const & key() const
Access for the key.
INDEX_TYPE const & index() const
Access for the index.
void clear()
function to clear the MappedVector
T * operator[](KEY_TYPE const &keyName)
MappedVector()=default
deleted default constructor
T * operator[](INDEX_TYPE index)
constValueContainer const & values() const
access for value container
~MappedVector()
default destructor
MappedVector & operator=(MappedVector const &)=default
Default copy assignment operator.
typename valueContainer::size_type size_type
the size_type of the value container
T const * operator[](KeyIndex const &keyIndex) const
typename std::pair< KEY_TYPE, T_PTR > value_type
the type of the values held in the vector
T * operator[](KeyIndex const &keyIndex)
MappedVector & operator=(MappedVector &&)=default
Default move assignment operator.
std::vector< value_type > valueContainer
the type of the vector container
INDEX_TYPE size() const
function to return the number of entries stored
typename std::pair< KEY_TYPE const, T * > const_key_value_type
the type of the values with const keys held in the vector
void erase(KEY_TYPE const &key)
Remove element at given key.
typename valueContainer::const_pointer const_pointer
the pointer to const type of the value container
void erase(INDEX_TYPE index)
Remove element at given index.
typename std::pair< KEY_TYPE const, T const * > const_value_type
a const type of the values held in the vector
typename valueContainer::pointer pointer
the pointer type of the value container
MappedVector(MappedVector &&)=default
Default move operator.
typename constValueContainer::const_reverse_iterator const_reverse_iterator
the reverse iterator to const type of the value container
T_PTR mapped_type
pointer to the value type
const_iterator end() const
valueContainer const & values()
access for value container
typename valueContainer::const_reference const_reference
reference to const type of the value container
MappedVector(MappedVector const &)=default
Default copy constructor.
std::vector< const_value_type > constValueContainer
a const type of the vector container
typename constKeyValueContainer::reverse_iterator reverse_iterator
the reverse iterator type of the value container
KEY_TYPE key_type
The type used for the key of the map.
std::unordered_map< KEY_TYPE, INDEX_TYPE > LookupMapType
the type of the lookup map
T const * operator[](KEY_TYPE const &keyName) const
typename valueContainer::reference reference
reference type of the value container
typename constKeyValueContainer::iterator iterator
the iterator type of the value container
void erase(KeyIndex &keyIndex)
Remove element at given key.
LookupMapType const & keys() const
access for key lookup
T * insert(KEY_TYPE const &keyName, T_PTR source, bool takeOwnership, bool overwrite=false)
insert new entry into MappedVector
const_iterator begin() const
INDEX_TYPE getIndex(KEY_TYPE const &key) const
T const * operator[](INDEX_TYPE index) const
std::vector< const_key_value_type > constKeyValueContainer
a const type of the vector container
typename constValueContainer::const_iterator const_iterator
the iterator to const type of the value container
std::string string
String type.