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 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) );
169 iterator
insert(
const KeyT& key,
const DataT& data)
186 iterator
replace(
const KeyT& oldKey,
const KeyT& newKey,
const DataT& 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];
242 const_iterator
find(
const KeyT& key)
const 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();
288 const_iterator
end()
const 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;
443 iterator it =
find( key );
480 #if 0 // Slow version 481 VectorType savedElements;
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 );
503 #else // Faster version (2x) 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 );
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.
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.
void clear(int=0)
Remove all elements, keeping full capacity.
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
DataT & getOrCreate(const KeyT &key)
Returns value given key.
insert_iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
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.
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.