17 #ifndef AGX_HASHVECTOR_H 18 #define AGX_HASHVECTOR_H 39 template <
typename KeyT,
typename DataT=KeyT,
typename HashT = agx::HashFn<KeyT> >
73 HashVector(
const ContainerType& other) : m_vector( other.m_vector ), m_hash( other.m_hash )
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();
145 typename HashType::iterator it = m_hash.
find(key);
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) );
173 iterator
insert(
const KeyT& key,
const DataT& data)
190 iterator
replace(
const KeyT& oldKey,
const KeyT& newKey,
const DataT& 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;
232 typename HashType::iterator it = m_hash.
find( key );
233 if (it == m_hash.
end())
236 return &m_vector[it->second];
246 const_iterator
find(
const KeyT& key)
const 250 typename HashType::const_iterator it = m_hash.
find( key );
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();
292 const_iterator
end()
const 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;
350 typename HashType::iterator it = m_hash.
find( key );
351 if (it == m_hash.
end())
380 KeyT& key = it->first;
381 typename HashType::iterator hashIt = m_hash.
find( key );
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;
449 iterator it =
find( key );
486 #if 0 // Slow version 487 VectorType savedElements;
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 );
509 #else // Faster version (2x) 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 );
const VectorType & vector() const
iterator replace(const KeyT &oldKey, const KeyT &newKey, const DataT &data)
Replace data in hash vector.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
iterator insert(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
const_iterator begin() const
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
void clear(int policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
size_t purge(T purger)
Purge the hash set.
void reserve(size_t size)
Reserve capacity in the hash table.
size_t size() const
Return the number of inserted elements.
VectorType::iterator iterator
const std::pair< agx::UInt32, agx::ref_ptr< RenderProxy > > * const_iterator
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
iterator push_back(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
void reserve(size_t size)
Reserve capacity in the vector.
std::pair< agx::UInt32, agx::ref_ptr< RenderProxy > > * iterator
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
bool empty() const
Return true if the container is empty.
std::pair< agx::UInt32, agx::ref_ptr< RenderProxy > > value_type
Buffer is deallocated and replaced by an newly allocated empty buffer.
void reserve(size_t num_elems)
Make room for this many elements in the hash table.
DataT & operator[](size_t i)
Access an element in the vector using an index.
iterator find(const KeyT &key)
Perform a hash search for a given key.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
DataT & getOrCreate(const KeyT &key)
Returns value given key.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
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.
Vector< std::pair< KeyT, DataT > > VectorType
bool erase(const KeyT &key)
Erase an element from the container.
iterator erase(const iterator &it)
Erase an element using an iterator.
bool erase(const KeyT &key)
Erase an element from the hash table.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
bool contains(const KeyT &key) const
Hash search for the key in the container.
iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold...
const DataT & back() const
const_iterator find(const KeyT &key) const
Perform a hash search for a given key.
Inheritance with partial specialization due to bug with ref_ptr containers.
HashVector< KeyT, DataT, HashT > ContainerType
Buffer is maintained (normal stl behavior).
const HashType & table() const
void push_back(const T2 &value)
const_iterator end() const
HashVector< KeyT, DataT, HashT > & operator=(const ContainerType &other)
Assignment operator.
const DataT & front() const
VectorType::const_iterator const_iterator
HashTable< KeyT, size_t, HashT > HashType
HashVector(const ContainerType &other)
Copy constructor.