17 #ifndef AGX_HASHVECTOR_H
18 #define AGX_HASHVECTOR_H
39 template <
typename KeyT,
typename DataT=KeyT,
typename HashT = agx::HashFn<KeyT> >
82 m_vector = other.m_vector;
83 m_hash = other.m_hash;
112 return m_vector.
size();
120 return m_vector.
empty();
141 typename HashType::iterator it = m_hash.
find(key);
142 if (it != m_hash.
end())
145 m_vector[it->second].second = data;
146 return &m_vector[it->second];
150 m_vector.
push_back( std::make_pair(key, data) );
189 auto oldIt = m_hash.
find( oldKey );
190 if ( oldIt == m_hash.
end() )
193 size_t index = oldIt->second;
194 m_vector[ index ] = std::make_pair( newKey, data );
195 m_hash.
erase( oldIt );
196 m_hash.
insert( newKey, index );
198 return begin() + index;
228 typename HashType::iterator it = m_hash.
find( key );
229 if (it == m_hash.
end())
232 return &m_vector[it->second];
246 typename HashType::const_iterator it = m_hash.
find( key );
247 if (it == m_hash.
end())
251 return &m_vector[it->second];
264 return m_vector.
begin(); }
272 return m_vector.
begin();
282 return m_vector.
end();
291 return m_vector.
end();
300 return m_vector[0].second;
309 return m_vector[0].second;
318 return m_vector[m_vector.
size()-1].second;
327 return m_vector[m_vector.
size()-1].second;
346 typename HashType::iterator it = m_hash.
find( key );
347 if (it == m_hash.
end())
374 KeyT& key = it->first;
375 typename HashType::iterator hashIt = m_hash.
find( key );
379 hashIt =
erase( hashIt );
381 if ( hashIt == m_hash.
end() )
384 return &m_vector[ hashIt->second ];
408 return m_vector[i].second;
420 return m_vector[i].second;
484 for(; it != m_vector.
end(); ++it)
487 if (!purger(it->first, it->second))
488 savedElements.
push_back( std::make_pair(it->first, it->second));
501 m_hash.
insert( it->first, m_vector.
size()-1 );
509 if (!purger((*it).first, (*it).second ))
515 this->
erase( (**it).first );
518 return removeElements.
size();
525 bool checkSize()
const {
526 return m_vector.
size() == m_hash.
size();
529 typename HashType::iterator
erase(
typename HashType::iterator it )
531 size_t idx = it->second;
533 typename HashType::iterator retIt = m_hash.
erase( it );
536 if (m_vector.
size() > 1 && idx != (m_vector.
size()-1))
542 m_vector[idx] = lastElement;
548 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
HashVector(const ContainerType &other)
Copy constructor.
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.
const HashType & table() 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.
const DataT & back() const
void reserve(size_t size)
Reserve capacity in the hash table.
HashVector< KeyT, DataT, HashT > ContainerType
const VectorType & vector() const
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.
const DataT & front() const
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
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_iterator begin() const
DataT & operator[](size_t i)
Access an element in the vector using an index.
HashVector< KeyT, DataT, HashT > & operator=(const ContainerType &other)
Assignment operator.
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
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 reserve(size_t num_elems)
Make room for this many elements in the hash table.
insert_iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
void clear(int=0)
Remove all elements, keeping full capacity.
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.