AGX Dynamics  2.35.0.1
HashVector.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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
61  MAINTAIN_BUFFER=VectorType::MAINTAIN_BUFFER // Buffer is maintained
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 
81  {
82  m_vector = other.m_vector;
83  m_hash = other.m_hash;
84  return *this;
85  }
86 
87 
90  {
91  // don't change buffer, will be dealloc'ed soon anyway
93  }
94 
95  const VectorType& vector() const
96  {
97  return m_vector;
98  }
99 
100  const HashType& table() const
101  {
102  return m_hash;
103  }
104 
108  size_t size() const
109  {
110  agxAssert( checkSize() );
111 
112  return m_vector.size();
113  }
114 
118  bool empty() const
119  {
120  return m_vector.empty();
121  }
122 
137  iterator push_back(const KeyT& key, const DataT& data)
138  {
139  agxAssert( checkSize() );
140 
141  typename HashType::iterator it = m_hash.find(key);
142  if (it != m_hash.end())
143  {
144  // Replace the already existing data value with the new
145  m_vector[it->second].second = data;
146  return &m_vector[it->second];
147  }
148 
149  // Key did not exist, insert at the end of the vector
150  m_vector.push_back( std::make_pair(key, data) );
151 
152  // Hash on data, index is data value
153  m_hash.insert(key, m_vector.size()-1);
154 
155  return end()-1; // Return iterator to the newly inserted element.
156  }
157 
169  iterator insert(const KeyT& key, const DataT& data)
170  {
171  agxAssert( checkSize() );
172 
173  return push_back(key, data);
174  }
175 
186  iterator replace( const KeyT& oldKey, const KeyT& newKey, const DataT& data )
187  {
188  agxAssert( checkSize() );
189  auto oldIt = m_hash.find( oldKey );
190  if ( oldIt == m_hash.end() )
191  return end();
192 
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 );
197 
198  return begin() + index;
199  }
200 
209  bool contains(const KeyT& key) const
210  {
211  agxAssert( checkSize() );
212 
213  return m_hash.contains( key );
214  }
215 
224  iterator find(const KeyT& key)
225  {
226  agxAssert( checkSize() );
227 
228  typename HashType::iterator it = m_hash.find( key );
229  if (it == m_hash.end())
230  return end();
231 
232  return &m_vector[it->second];
233  }
234 
242  const_iterator find(const KeyT& key) const
243  {
244  agxAssert( checkSize() );
245 
246  typename HashType::const_iterator it = m_hash.find( key );
247  if (it == m_hash.end())
248  return end();
249 
250  // Return index to the element in the vector
251  return &m_vector[it->second];
252 
253  }
254 
255 
260  {
261  agxAssert( checkSize() );
262 
263 
264  return m_vector.begin(); }
265 
270  {
271  agxAssert( checkSize() );
272  return m_vector.begin();
273  }
274 
275 
280  {
281  agxAssert( checkSize() );
282  return m_vector.end();
283  }
284 
289  {
290  agxAssert( checkSize() );
291  return m_vector.end();
292  }
293 
297  DataT& front()
298  {
299  agxAssert( checkSize() );
300  return m_vector[0].second;
301  }
302 
306  const DataT& front() const
307  {
308  agxAssert( checkSize() );
309  return m_vector[0].second;
310  }
311 
315  DataT& back()
316  {
317  agxAssert( checkSize() );
318  return m_vector[m_vector.size()-1].second;
319  }
320 
324  const DataT& back() const
325  {
326  agxAssert( checkSize() );
327  return m_vector[m_vector.size()-1].second;
328  }
329 
330 
331 
341  bool erase(const KeyT& key)
342  {
343 
344  agxAssert( checkSize() );
345 
346  typename HashType::iterator it = m_hash.find( key );
347  if (it == m_hash.end())
348  return false;
349 
350  erase( it );
351  return true;
352  }
353 
368  {
369  agxAssert( checkSize() );
370 
371  if (it == end())
372  return it;
373 
374  KeyT& key = it->first;
375  typename HashType::iterator hashIt = m_hash.find( key );
376 
377  agxAssert ( hashIt != m_hash.end() );
378 
379  hashIt = erase( hashIt );
380 
381  if ( hashIt == m_hash.end() )
382  return end();
383 
384  return &m_vector[ hashIt->second ];
385  }
386 
392  {
393  agxAssert( checkSize() );
394 
395  m_hash.clear( (typename HashType::ClearPolicy)policy );
396  m_vector.clear( (typename VectorType::ClearPolicy)policy );
397  }
398 
403  DataT& operator[](size_t i )
404  {
405  agxAssert( checkSize() );
406 
407  agxAssert(i < m_vector.size());
408  return m_vector[i].second;
409  }
410 
415  const DataT& operator[](size_t i ) const
416  {
417  agxAssert( checkSize() );
418 
419  agxAssert(i < m_vector.size());
420  return m_vector[i].second;
421  }
422 
427  void reserve(size_t size)
428  {
429  agxAssert( checkSize() );
430 
431  m_vector.reserve( size );
432  m_hash.reserve( size );
433  }
434 
441  DataT& getOrCreate( const KeyT& key )
442  {
443  iterator it = find( key );
444  if ( it == end() )
445  it = insert( key, value_type() );
446  return it->second;
447  }
448 
474  template<typename T>
475  size_t purge(T purger)
476  {
477  if (empty()) // Nothing to do
478  return 0;
479 
480 #if 0 // Slow version
481  VectorType savedElements; // Elements to keep
482  savedElements.reserve( m_vector.size());
483  VectorType::const_iterator it = m_vector.begin();
484  for(; it != m_vector.end(); ++it)
485  {
486  // Should we keep it?
487  if (!purger(it->first, it->second))
488  savedElements.push_back( std::make_pair(it->first, it->second)); // store all elements that should remain
489  }
492 
493  // Make sure we can fit all the saved elements
494  m_vector.reserve( savedElements.size() );
495  m_hash.reserve( savedElements.size() );
496 
497  for(VectorType::const_iterator it = savedElements.begin(); it != savedElements.end(); ++it)
498  {
499  m_vector.push_back( *it );
500  // Hash on data, index is data value
501  m_hash.insert( it->first, m_vector.size()-1 );
502  }
503 #else // Faster version (2x)
504  typename agx::Vector<typename VectorType::value_type *> removeElements; //PointerVector ; // Safe to use pointers?
505  removeElements.reserve( m_vector.size());
506  for(typename VectorType::iterator it = m_vector.begin(); it != m_vector.end(); ++it)
507  {
508  // Should we keep it?
509  if (!purger((*it).first, (*it).second ))
510  removeElements.push_back(&(*it)); // store all elements that should remain
511  }
512 
513  for(typename agx::Vector<typename VectorType::value_type *>::const_iterator it = removeElements.begin(); it != removeElements.end(); ++it)
514  {
515  this->erase( (**it).first );
516  }
517 
518  return removeElements.size();
519 #endif
520  }
521 
522  private:
523 
524  // Return true if size of vector and hash is equal
525  bool checkSize() const {
526  return m_vector.size() == m_hash.size();
527  }
528 
529  typename HashType::iterator erase( typename HashType::iterator it )
530  {
531  size_t idx = it->second;
532 
533  typename HashType::iterator retIt = m_hash.erase( it ); // remove from hash
534 
535  // Remove from vector too:
536  if (m_vector.size() > 1 && idx != (m_vector.size()-1))
537  {
538  // Get the last element
539  typename VectorType::value_type lastElement = m_vector.back();
540 
541  // Put it where we have removed an element
542  m_vector[idx] = lastElement;
543 
544  // remove the last now
545  m_vector.pop_back();
546 
547  // Rehash the moved one with the new index
548  m_hash.insert( lastElement.first, idx );
549  }
550  else
551  // Only one element, OR its the last we are removing: just remove it
552  m_vector.pop_back();
553 
554  agxAssert( checkSize() );
555 
556  return retIt;
557  }
558 
559 
560  };
561 
562 }
563 
564 
565 #endif /* AGX_HASHVECTOR_H */
bool empty() const
Definition: Container.h:136
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
Definition: Container.h:43
@ SHRINK_BUFFER
Buffer is deallocated and replaced by an newly allocated empty buffer.
Definition: Container.h:44
@ MAINTAIN_BUFFER
Buffer is maintained (normal stl behavior).
Definition: Container.h:46
@ SHRINK_BUFFER_AVERAGED
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold.
Definition: Container.h:45
size_t size() const
Definition: Container.h:134
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:41
Vector< std::pair< KeyT, DataT > > VectorType
Definition: HashVector.h:47
iterator end()
Definition: HashVector.h:279
HashVector(const ContainerType &other)
Copy constructor.
Definition: HashVector.h:73
VectorType::const_iterator const_iterator
Definition: HashVector.h:65
DataT & back()
Definition: HashVector.h:315
const_iterator end() const
Definition: HashVector.h:288
iterator insert(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
Definition: HashVector.h:169
const HashType & table() const
Definition: HashVector.h:100
size_t size() const
Return the number of inserted elements.
Definition: HashVector.h:108
DataT value_type
Definition: HashVector.h:44
const_iterator find(const KeyT &key) const
Perform a hash search for a given key.
Definition: HashVector.h:242
@ SHRINK_BUFFER_AVERAGED
Definition: HashVector.h:60
HashVector()
Constructor.
Definition: HashVector.h:69
const DataT & back() const
Definition: HashVector.h:324
void reserve(size_t size)
Reserve capacity in the hash table.
Definition: HashVector.h:427
HashVector< KeyT, DataT, HashT > ContainerType
Definition: HashVector.h:45
const VectorType & vector() const
Definition: HashVector.h:95
bool empty() const
Return true if the container is empty.
Definition: HashVector.h:118
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
Definition: HashVector.h:391
~HashVector()
Destructor.
Definition: HashVector.h:89
bool erase(const KeyT &key)
Erase an element from the container.
Definition: HashVector.h:341
const DataT & front() const
Definition: HashVector.h:306
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
Definition: HashVector.h:415
iterator find(const KeyT &key)
Perform a hash search for a given key.
Definition: HashVector.h:224
size_t purge(T purger)
Purge the hash set.
Definition: HashVector.h:475
DataT & front()
Definition: HashVector.h:297
iterator replace(const KeyT &oldKey, const KeyT &newKey, const DataT &data)
Replace data in hash vector.
Definition: HashVector.h:186
bool contains(const KeyT &key) const
Hash search for the key in the container.
Definition: HashVector.h:209
const_iterator begin() const
Definition: HashVector.h:269
DataT & operator[](size_t i)
Access an element in the vector using an index.
Definition: HashVector.h:403
HashVector< KeyT, DataT, HashT > & operator=(const ContainerType &other)
Assignment operator.
Definition: HashVector.h:80
VectorType::iterator iterator
Definition: HashVector.h:64
iterator push_back(const KeyT &key, const DataT &data)
Insert a key,data pair in to the end of the container.
Definition: HashVector.h:137
iterator erase(const iterator &it)
Erase an element using an iterator.
Definition: HashVector.h:367
DataT & getOrCreate(const KeyT &key)
Returns value given key.
Definition: HashVector.h:441
HashTable< KeyT, size_t, HashT > HashType
Definition: HashVector.h:48
iterator begin()
Definition: HashVector.h:259
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.
Templated vector class.
Definition: agx/Vector.h:53
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
Definition: agx/Vector.h:491
iterator end()
Definition: agx/Vector.h:1044
const T * const_iterator
Definition: agx/Vector.h:63
iterator begin()
Definition: agx/Vector.h:1041
T & back() const
Definition: agx/Vector.h:707
void reserve(size_t size)
Reserve capacity in the vector.
Definition: agx/Vector.h:583
T * iterator
Definition: agx/Vector.h:62
void pop_back()
Definition: agx/Vector.h:754
void push_back(const T2 &value)
Definition: agx/Vector.h:715
#define agxAssert(expr)
Definition: debug.h:143
The agx namespace contains the dynamics/math part of the AGX Dynamics API.