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();
147 if (it != m_hash.
end())
150 m_vector[it->second].second = std::forward<T>(data);
151 return &m_vector[it->second];
155 m_vector.
push_back(std::make_pair(key, std::forward<T>(data)));
179 return push_back(key, std::forward<T>(data));
195 auto oldIt = m_hash.
find( oldKey );
196 if ( oldIt == m_hash.
end() )
199 size_t index = oldIt->second;
200 m_vector[ index ] = std::make_pair( newKey, data );
201 m_hash.
erase( oldIt );
202 m_hash.
insert( newKey, index );
204 return begin() + index;
235 if (it == m_hash.
end())
238 return &m_vector[it->second];
253 if (it == m_hash.
end())
257 return &m_vector[it->second];
270 return m_vector.
begin(); }
278 return m_vector.
begin();
288 return m_vector.
end();
297 return m_vector.
end();
306 return m_vector[0].second;
315 return m_vector[0].second;
324 return m_vector[m_vector.
size()-1].second;
333 return m_vector[m_vector.
size()-1].second;
353 if (it == m_hash.
end())
382 KeyT& key = it->first;
387 hashIt =
erase( hashIt );
389 if ( hashIt == m_hash.
end() )
392 return &m_vector[ hashIt->second ];
416 return m_vector[i].second;
428 return m_vector[i].second;
492 for(; it != m_vector.
end(); ++it)
495 if (!purger(it->first, it->second))
496 savedElements.
push_back( std::make_pair(it->first, it->second));
509 m_hash.
insert( it->first, m_vector.
size()-1 );
517 if (!purger((*it).first, (*it).second ))
523 this->
erase( (**it).first );
526 return removeElements.
size();
533 bool checkSize()
const {
534 return m_vector.
size() == m_hash.
size();
537 typename HashType::iterator
erase(
typename HashType::iterator it )
539 size_t idx = it->second;
541 typename HashType::iterator retIt = m_hash.
erase( it );
544 if (m_vector.
size() > 1 && idx != (m_vector.
size()-1))
550 m_vector[idx] = lastElement;
556 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
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
iterator push_back(const KeyT &key, T &&data)
Insert a key,data pair in to the end of the container.
iterator insert(const KeyT &key, T &&data)
Insert a key,data pair in to the end of the container.
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 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.