AGX Dynamics  2.37.3.2
HashVector.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_HASHVECTOR_H
18 #define AGX_HASHVECTOR_H
19 
20 
21 #include <agx/Vector.h>
22 #include <agx/HashTable.h>
23 
24 namespace agx
25 {
26 
39  template <typename KeyT, typename DataT=KeyT, typename HashT = agx::HashFn<KeyT> >
40  class HashVector
41  {
42  public:
43 
44  typedef DataT value_type;
46 
49 
50  private:
51 
52  VectorType m_vector;
53  HashType m_hash;
54 
55  public:
56 
58  {
59  SHRINK_BUFFER=VectorType::SHRINK_BUFFER, // Buffer is deallocated and replaced by an empty buffer
60  SHRINK_BUFFER_AVERAGED=VectorType::SHRINK_BUFFER_AVERAGED, // Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold
62  };
63 
64  typedef typename VectorType::iterator iterator;
66 
67 
70  {}
71 
73  HashVector(const ContainerType& other) : m_vector( other.m_vector ), m_hash( other.m_hash )
74  {
75  this->clear(SHRINK_BUFFER);
76  *this = other;
77  }
78 
80  HashVector<KeyT, DataT, HashT>& operator= (const ContainerType& other)
81  {
82  if ( this == &other ) {
83  return *this;
84  }
85 
86  m_vector = other.m_vector;
87  m_hash = other.m_hash;
88  return *this;
89  }
90 
91 
94  {
95  // don't change buffer, will be dealloc'ed soon anyway
97  }
98 
99  const VectorType& vector() const
100  {
101  return m_vector;
102  }
103 
104  const HashType& table() const
105  {
106  return m_hash;
107  }
108 
112  size_t size() const
113  {
114  agxAssert( checkSize() );
115 
116  return m_vector.size();
117  }
118 
122  bool empty() const
123  {
124  return m_vector.empty();
125  }
126 
141  iterator push_back(const KeyT& key, const DataT& data)
142  {
143  agxAssert( checkSize() );
144 
145  typename HashType::iterator it = m_hash.find(key);
146  if (it != m_hash.end())
147  {
148  // Replace the already existing data value with the new
149  m_vector[it->second].second = data;
150  return &m_vector[it->second];
151  }
152 
153  // Key did not exist, insert at the end of the vector
154  m_vector.push_back( std::make_pair(key, data) );
155 
156  // Hash on data, index is data value
157  m_hash.insert(key, m_vector.size()-1);
158 
159  return end()-1; // Return iterator to the newly inserted element.
160  }
161 
173  iterator insert(const KeyT& key, const DataT& data)
174  {
175  agxAssert( checkSize() );
176 
177  return push_back(key, data);
178  }
179 
190  iterator replace( const KeyT& oldKey, const KeyT& newKey, const DataT& data )
191  {
192  agxAssert( checkSize() );
193  auto oldIt = m_hash.find( oldKey );
194  if ( oldIt == m_hash.end() )
195  return end();
196 
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 );
201 
202  return begin() + index;
203  }
204 
213  bool contains(const KeyT& key) const
214  {
215  agxAssert( checkSize() );
216 
217  return m_hash.contains( key );
218  }
219 
228  iterator find(const KeyT& key)
229  {
230  agxAssert( checkSize() );
231 
232  typename HashType::iterator it = m_hash.find( key );
233  if (it == m_hash.end())
234  return end();
235 
236  return &m_vector[it->second];
237  }
238 
246  const_iterator find(const KeyT& key) const
247  {
248  agxAssert( checkSize() );
249 
250  typename HashType::const_iterator it = m_hash.find( key );
251  if (it == m_hash.end())
252  return end();
253 
254  // Return index to the element in the vector
255  return &m_vector[it->second];
256 
257  }
258 
259 
263  iterator begin()
264  {
265  agxAssert( checkSize() );
266 
267 
268  return m_vector.begin(); }
269 
273  const_iterator begin() const
274  {
275  agxAssert( checkSize() );
276  return m_vector.begin();
277  }
278 
279 
283  iterator end()
284  {
285  agxAssert( checkSize() );
286  return m_vector.end();
287  }
288 
292  const_iterator end() const
293  {
294  agxAssert( checkSize() );
295  return m_vector.end();
296  }
297 
301  DataT& front()
302  {
303  agxAssert( checkSize() );
304  return m_vector[0].second;
305  }
306 
310  const DataT& front() const
311  {
312  agxAssert( checkSize() );
313  return m_vector[0].second;
314  }
315 
319  DataT& back()
320  {
321  agxAssert( checkSize() );
322  return m_vector[m_vector.size()-1].second;
323  }
324 
328  const DataT& back() const
329  {
330  agxAssert( checkSize() );
331  return m_vector[m_vector.size()-1].second;
332  }
333 
334 
335 
345  bool erase(const KeyT& key)
346  {
347 
348  agxAssert( checkSize() );
349 
350  typename HashType::iterator it = m_hash.find( key );
351  if (it == m_hash.end())
352  return false;
353 
354  erase( it );
355  return true;
356  }
357 
373  iterator erase(const iterator& it)
374  {
375  agxAssert( checkSize() );
376 
377  if (it == end())
378  return it;
379 
380  KeyT& key = it->first;
381  typename HashType::iterator hashIt = m_hash.find( key );
382 
383  agxAssert ( hashIt != m_hash.end() );
384 
385  hashIt = erase( hashIt );
386 
387  if ( hashIt == m_hash.end() )
388  return end();
389 
390  return &m_vector[ hashIt->second ];
391  }
392 
398  {
399  agxAssert( checkSize() );
400 
401  m_hash.clear( (typename HashType::ClearPolicy)policy );
402  m_vector.clear( (typename VectorType::ClearPolicy)policy );
403  }
404 
409  DataT& operator[](size_t i )
410  {
411  agxAssert( checkSize() );
412 
413  agxAssert(i < m_vector.size());
414  return m_vector[i].second;
415  }
416 
421  const DataT& operator[](size_t i ) const
422  {
423  agxAssert( checkSize() );
424 
425  agxAssert(i < m_vector.size());
426  return m_vector[i].second;
427  }
428 
433  void reserve(size_t size)
434  {
435  agxAssert( checkSize() );
436 
437  m_vector.reserve( size );
438  m_hash.reserve( size );
439  }
440 
447  DataT& getOrCreate( const KeyT& key )
448  {
449  iterator it = find( key );
450  if ( it == end() )
451  it = insert( key, value_type() );
452  return it->second;
453  }
454 
480  template<typename T>
481  size_t purge(T purger)
482  {
483  if (empty()) // Nothing to do
484  return 0;
485 
486 #if 0 // Slow version
487  VectorType savedElements; // Elements to keep
488  savedElements.reserve( m_vector.size());
489  VectorType::const_iterator it = m_vector.begin();
490  for(; it != m_vector.end(); ++it)
491  {
492  // Should we keep it?
493  if (!purger(it->first, it->second))
494  savedElements.push_back( std::make_pair(it->first, it->second)); // store all elements that should remain
495  }
498 
499  // Make sure we can fit all the saved elements
500  m_vector.reserve( savedElements.size() );
501  m_hash.reserve( savedElements.size() );
502 
503  for(VectorType::const_iterator it = savedElements.begin(); it != savedElements.end(); ++it)
504  {
505  m_vector.push_back( *it );
506  // Hash on data, index is data value
507  m_hash.insert( it->first, m_vector.size()-1 );
508  }
509 #else // Faster version (2x)
510  typename agx::Vector<typename VectorType::value_type *> removeElements; //PointerVector ; // Safe to use pointers?
511  removeElements.reserve( m_vector.size());
512  for(typename VectorType::iterator it = m_vector.begin(); it != m_vector.end(); ++it)
513  {
514  // Should we keep it?
515  if (!purger((*it).first, (*it).second ))
516  removeElements.push_back(&(*it)); // store all elements that should remain
517  }
518 
519  for(typename agx::Vector<typename VectorType::value_type *>::const_iterator it = removeElements.begin(); it != removeElements.end(); ++it)
520  {
521  this->erase( (**it).first );
522  }
523 
524  return removeElements.size();
525 #endif
526  }
527 
528  private:
529 
530  // Return true if size of vector and hash is equal
531  bool checkSize() const {
532  return m_vector.size() == m_hash.size();
533  }
534 
535  typename HashType::iterator erase( typename HashType::iterator it )
536  {
537  size_t idx = it->second;
538 
539  typename HashType::iterator retIt = m_hash.erase( it ); // remove from hash
540 
541  // Remove from vector too:
542  if (m_vector.size() > 1 && idx != (m_vector.size()-1))
543  {
544  // Get the last element
545  typename VectorType::value_type lastElement = m_vector.back();
546 
547  // Put it where we have removed an element
548  m_vector[idx] = lastElement;
549 
550  // remove the last now
551  m_vector.pop_back();
552 
553  // Rehash the moved one with the new index
554  retIt = m_hash.insert( lastElement.first, idx );
555  }
556  else
557  // Only one element, OR its the last we are removing: just remove it
558  m_vector.pop_back();
559 
560  agxAssert( checkSize() );
561 
562  return retIt;
563  }
564 
565 
566  };
567 
568 }
569 
570 
571 #endif /* AGX_HASHVECTOR_H */
const VectorType & vector() const
Definition: HashVector.h:99
iterator replace(const KeyT &oldKey, const KeyT &newKey, const DataT &data)
Replace data in hash vector.
Definition: HashVector.h:190
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
DataT & back()
Definition: HashVector.h:319
iterator insert(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
Definition: HashVector.h:173
const_iterator begin() const
Definition: HashVector.h:273
void pop_back()
Definition: Vector.h:754
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
Definition: HashVector.h:421
void clear(int policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
iterator begin()
Definition: Vector.h:1041
size_t purge(T purger)
Purge the hash set.
Definition: HashVector.h:481
void reserve(size_t size)
Reserve capacity in the hash table.
Definition: HashVector.h:433
size_t size() const
Return the number of inserted elements.
Definition: HashVector.h:112
iterator end()
Definition: HashVector.h:283
VectorType::iterator iterator
Definition: HashVector.h:64
const std::pair< agx::UInt32, agx::ref_ptr< RenderProxy > > * const_iterator
Definition: Vector.h:63
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses&#39; &#39;clear&#39; method in order to control...
Definition: Container.h:42
iterator end()
Definition: Vector.h:1044
iterator push_back(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
Definition: HashVector.h:141
void reserve(size_t size)
Reserve capacity in the vector.
Definition: Vector.h:583
std::pair< agx::UInt32, agx::ref_ptr< RenderProxy > > * iterator
Definition: Vector.h:62
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
bool empty() const
Return true if the container is empty.
Definition: HashVector.h:122
DataT value_type
Definition: HashVector.h:44
std::pair< agx::UInt32, agx::ref_ptr< RenderProxy > > value_type
Definition: Vector.h:56
Templated vector class.
Definition: Vector.h:52
Buffer is deallocated and replaced by an newly allocated empty buffer.
Definition: Container.h:44
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.
Definition: HashVector.h:409
iterator begin()
Definition: HashVector.h:263
iterator find(const KeyT &key)
Perform a hash search for a given key.
Definition: HashVector.h:228
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
HashVector()
Constructor.
Definition: HashVector.h:69
DataT & getOrCreate(const KeyT &key)
Returns value given key.
Definition: HashVector.h:447
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
Definition: Vector.h:491
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
Definition: HashVector.h:47
DataT & front()
Definition: HashVector.h:301
bool erase(const KeyT &key)
Erase an element from the container.
Definition: HashVector.h:345
iterator erase(const iterator &it)
Erase an element using an iterator.
Definition: HashVector.h:373
#define agxAssert(expr)
Definition: debug.h:143
bool erase(const KeyT &key)
Erase an element from the hash table.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
Definition: HashVector.h:397
bool contains(const KeyT &key) const
Hash search for the key in the container.
Definition: HashVector.h:213
iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
size_t size() const
Definition: Container.h:134
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold...
Definition: Container.h:45
const DataT & back() const
Definition: HashVector.h:328
~HashVector()
Destructor.
Definition: HashVector.h:93
const_iterator find(const KeyT &key) const
Perform a hash search for a given key.
Definition: HashVector.h:246
Inheritance with partial specialization due to bug with ref_ptr containers.
HashVector< KeyT, DataT, HashT > ContainerType
Definition: HashVector.h:45
Buffer is maintained (normal stl behavior).
Definition: Container.h:46
const HashType & table() const
Definition: HashVector.h:104
void push_back(const T2 &value)
Definition: Vector.h:715
const_iterator end() const
Definition: HashVector.h:292
HashVector< KeyT, DataT, HashT > & operator=(const ContainerType &other)
Assignment operator.
Definition: HashVector.h:80
bool empty() const
Definition: Container.h:136
T & back() const
Definition: Vector.h:707
const DataT & front() const
Definition: HashVector.h:310
VectorType::const_iterator const_iterator
Definition: HashVector.h:65
HashTable< KeyT, size_t, HashT > HashType
Definition: HashVector.h:48
HashVector(const ContainerType &other)
Copy constructor.
Definition: HashVector.h:73