17#ifndef AGX_HASHVECTOR_H
18#define AGX_HASHVECTOR_H
39 template <
typename KeyT,
typename DataT=KeyT,
typename HashT = agx::HashFn<KeyT> >
82 if (
this == &other ) {
86 m_vector = other.m_vector;
87 m_hash = other.m_hash;
116 return m_vector.
size();
124 return m_vector.
empty();
146 if (it != m_hash.
end())
149 m_vector[it->second].second = data;
150 return &m_vector[it->second];
154 m_vector.
push_back( std::make_pair(key, data) );
193 auto oldIt = m_hash.
find( oldKey );
194 if ( oldIt == m_hash.
end() )
197 size_t index = oldIt->second;
198 m_vector[ index ] = std::make_pair( newKey, data );
199 m_hash.
erase( oldIt );
200 m_hash.
insert( newKey, index );
202 return begin() + index;
233 if (it == m_hash.
end())
236 return &m_vector[it->second];
251 if (it == m_hash.
end())
255 return &m_vector[it->second];
268 return m_vector.
begin(); }
276 return m_vector.
begin();
286 return m_vector.
end();
295 return m_vector.
end();
304 return m_vector[0].second;
313 return m_vector[0].second;
322 return m_vector[m_vector.
size()-1].second;
331 return m_vector[m_vector.
size()-1].second;
351 if (it == m_hash.
end())
380 KeyT& key = it->first;
385 hashIt =
erase( hashIt );
387 if ( hashIt == m_hash.
end() )
390 return &m_vector[ hashIt->second ];
414 return m_vector[i].second;
426 return m_vector[i].second;
490 for(; it != m_vector.
end(); ++it)
493 if (!purger(it->first, it->second))
494 savedElements.
push_back( std::make_pair(it->first, it->second));
507 m_hash.
insert( it->first, m_vector.
size()-1 );
515 if (!purger((*it).first, (*it).second ))
521 this->
erase( (**it).first );
524 return removeElements.
size();
531 bool checkSize()
const {
532 return m_vector.
size() == m_hash.
size();
535 typename HashType::iterator
erase(
typename HashType::iterator it )
537 size_t idx = it->second;
539 typename HashType::iterator retIt = m_hash.
erase( it );
542 if (m_vector.
size() > 1 && idx != (m_vector.
size()-1))
548 m_vector[idx] = lastElement;
554 retIt = m_hash.
insert( lastElement.first, idx );
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
@ SHRINK_BUFFER
Buffer is deallocated and replaced by an newly allocated empty buffer.
@ MAINTAIN_BUFFER
Buffer is maintained (normal stl behavior).
@ SHRINK_BUFFER_AVERAGED
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold.
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Vector< std::pair< KeyT, DataT > > VectorType
const DataT & back() const
const HashType & table() const
HashVector(const ContainerType &other)
Copy constructor.
const VectorType & vector() const
VectorType::const_iterator const_iterator
const_iterator end() const
iterator insert(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
size_t size() const
Return the number of inserted elements.
const_iterator find(const KeyT &key) const
Perform a hash search for a given key.
DataT & operator[](size_t i)
Access an element in the vector using an index.
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
void reserve(size_t size)
Reserve capacity in the hash table.
HashVector< KeyT, DataT, HashT > ContainerType
bool empty() const
Return true if the container is empty.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
bool erase(const KeyT &key)
Erase an element from the container.
iterator find(const KeyT &key)
Perform a hash search for a given key.
size_t purge(T purger)
Purge the hash set.
iterator replace(const KeyT &oldKey, const KeyT &newKey, const DataT &data)
Replace data in hash vector.
bool contains(const KeyT &key) const
Hash search for the key in the container.
const DataT & front() const
const_iterator begin() const
VectorType::iterator iterator
iterator push_back(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
iterator erase(const iterator &it)
Erase an element using an iterator.
DataT & getOrCreate(const KeyT &key)
Returns value given key.
HashTable< KeyT, size_t, HashT > HashType
HashVector< KeyT, DataT, HashT > & operator=(const ContainerType &other)
Assignment operator.
iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
bool contains(const KeyT &k) const
Check if the hash table contains a key/value pair for the given key.
iterator end()
Iterator marking end of hash table.
bool erase(const KeyT &key)
Erase an element from the hash table.
void clear(int policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
void reserve(size_t num_elems)
Make room for this many elements in the hash table.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
Inheritance with partial specialization due to bug with ref_ptr containers.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
void reserve(size_t size)
Reserve capacity in the vector.
void push_back(const T2 &value)
The agx namespace contains the dynamics/math part of the AGX Dynamics API.