AGX Dynamics  2.35.0.1
SetVector.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 #pragma once
18 
19 
20 #include <agx/Vector.h>
21 #include <agx/HashTable.h>
22 
23 namespace agx
24 {
25 
36  template <typename DataT, typename HashT = HashFn<DataT> >
37  class SetVector
38  {
39  public:
40 
41  typedef DataT value_type;
42  typedef const DataT& const_reference;
43  typedef size_t size_type;
47 
48  private:
49  VectorType m_vector;
50  HashType m_hash;
51 
52  public:
53 #ifndef SWIG
55  {
56  SHRINK_BUFFER=VectorType::SHRINK_BUFFER, // Buffer is deallocated and replaced by an empty buffer
57  SHRINK_BUFFER_AVERAGED=VectorType::SHRINK_BUFFER_AVERAGED, // Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold
58  MAINTAIN_BUFFER=VectorType::MAINTAIN_BUFFER // Buffer is maintained
59  };
60 #endif
61 
62  typedef typename VectorType::iterator iterator;
66 
67 
70  {}
71 
73  SetVector(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  ContainerType& operator= (const ContainerType& other) // NOLINT clang-tidy-7 doesn't realize that ContainerType is a SetVector.
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 
98  const VectorType& vector() const
99  {
100  return m_vector;
101  }
102 
106  const HashType& table() const
107  {
108  return m_hash;
109  }
110 
114  inline size_t size() const
115  {
116  agxAssert( checkSize() );
117 
118  return m_vector.size();
119  }
120 
124  inline bool empty() const
125  {
126  return m_vector.empty();
127  }
128 
143  template< typename T2 >
144  inline iterator push_back(const T2& data)
145  {
146  agxAssert( checkSize() );
147 
148  typename HashType::iterator it = m_hash.find(data);
149  if (it != m_hash.end())
150  {
151  // Replace the already existing data value with the new
152  m_vector[it->second] = data;
153  return &m_vector[it->second];
154  }
155 
156  // Key did not exist, insert at the end of the vector
157  m_vector.push_back( data );
158 
159  // Hash on data, index is data value
160  m_hash.insert( data, m_vector.size()-1 );
161 
162  return end()-1; // Return iterator to the newly inserted element.
163  }
164 
177  template< typename T2 >
178  inline iterator insert(const T2& data)
179  {
180  agxAssert( checkSize() );
181 
182  return push_back(data);
183  }
184 
195  template< typename OldDataT, typename NewDataT >
196  iterator replace( const OldDataT& oldData, const NewDataT& newData )
197  {
198  agxAssert( checkSize() );
199 
200  auto oldIt = m_hash.find( oldData );
201  if ( oldIt == m_hash.end() )
202  return end();
203 
204  size_t index = oldIt->second;
205  m_vector[ index ] = newData;
206  m_hash.erase( oldIt );
207  m_hash.insert( newData, index );
208 
209  return begin() + index;
210  }
211 
220  template< typename T2 >
221  inline bool contains(const T2& key) const
222  {
223  agxAssert( checkSize() );
224 
225  return m_hash.contains( key );
226  }
227 
236  template< typename T2 >
237  iterator find(const T2& key)
238  {
239  agxAssert( checkSize() );
240 
241  typename HashType::iterator it = m_hash.find( key );
242  if (it == m_hash.end())
243  return end();
244 
245  return &m_vector[it->second];
246  }
247 
255  template< typename T2 >
256  const_iterator find(const T2& key) const
257  {
258  agxAssert( checkSize() );
259 
260  typename HashType::const_iterator it = m_hash.find( key );
261  if (it == m_hash.end())
262  return end();
263 
264  // Return index to the element in the vector
265  return &m_vector[it->second];
266  }
267 
268 #ifndef SWIG
272  inline iterator begin()
273  {
274  agxAssert( checkSize() );
275 
276  return m_vector.begin();
277  }
278 
282  inline const_iterator begin() const
283  {
284  agxAssert( checkSize() );
285 
286  return m_vector.begin();
287  }
288 
293  {
294  agxAssert( checkSize() );
295 
296  return m_vector.rbegin();
297  }
298 
303  {
304  agxAssert( checkSize() );
305 
306  return m_vector.rbegin();
307  }
308 
312  inline iterator end()
313  {
314  agxAssert( checkSize() );
315 
316  return m_vector.end();
317  }
318 
322  inline const_iterator end() const
323  {
324  agxAssert( checkSize() );
325  return m_vector.end();
326  }
327 
332  {
333  agxAssert( checkSize() );
334 
335  return m_vector.rend(); }
336 
341  {
342  agxAssert( checkSize() );
343  return m_vector.rend();
344  }
345 #endif
349  inline DataT& front()
350  {
351  agxAssert( checkSize() );
352  return m_vector[0];
353  }
354 
358  inline const DataT& front() const
359  {
360  agxAssert( checkSize() );
361  return m_vector[0];
362  }
363 
367  inline DataT& back()
368  {
369  agxAssert( checkSize() );
370  return m_vector[m_vector.size()-1];
371  }
372 
376  inline const DataT& back() const
377  {
378  agxAssert( checkSize() );
379  return m_vector[m_vector.size()-1];
380  }
381 
382 
392  template< typename T2 >
393  bool erase(const T2& key)
394  {
395  agxAssert( checkSize() );
396 
397  typename HashType::iterator it = m_hash.find( key );
398  if (it == m_hash.end())
399  return false;
400 
401  erase( it );
402  return true;
403  }
404 
418  bool erase(const iterator& it)
419  {
420 
421  agxAssert( checkSize() );
422 
423  if (it == end())
424  return false;
425  agxAssert( it != end() );
426 
427  DataT& key = *it;
428  typename HashType::iterator hashIt = m_hash.find( key );
429 
430  agxAssert ( hashIt != m_hash.end() );
431 
432  erase( hashIt );
433 
434  return true;
435  }
436 
441 #ifndef SWIG
443 #else
444  void clear()
445 #endif
446  {
447 
448  agxAssert( checkSize() );
449 
450  m_hash.clear( (typename HashType::ClearPolicy)policy );
451  m_vector.clear( (typename VectorType::ClearPolicy)policy );
452  }
453 
458  inline DataT& operator[](size_t i )
459  {
460  agxAssert( checkSize() );
461 
462  agxAssert(i < m_vector.size());
463  return m_vector[i];
464  }
465 
470  inline const DataT& operator[](size_t i ) const
471  {
472  agxAssert( checkSize() );
473 
474  agxAssert(i < m_vector.size());
475  return m_vector[i];
476  }
477 
478 
484  inline const DataT& at(size_t index) const
485  {
486  return m_vector.at(index);
487  }
488 
489 
495  inline DataT& at(size_t index)
496  {
497  return m_vector.at(index);
498  }
499 
500 
505  void reserve(size_t size)
506  {
507  agxAssert( checkSize() );
508 
509  m_vector.reserve( size );
510  m_hash.reserve( size );
511  }
512 
513 
514 
540  template<typename T>
541  size_t purge(T purger)
542  {
543  if (empty()) // Nothing to do
544  return 0;
545 
546 #if 0 // Slower implementation
547  typename VectorType savedElements; // Elements to keep
548  savedElements.reserve( m_vector.size());
549  typename VectorType::const_iterator it = m_vector.begin();
550  for(; it != m_vector.end(); ++it)
551  {
552  // Should we keep it?
553  if (!purger(*it))
554  savedElements.push_back(*it); // store all elements that should remain
555  }
558 
559  // Make sure we can fit all the saved elements
560  m_vector.reserve( savedElements.size() );
561  m_hash.reserve( savedElements.size() );
562 
563  for(typename VectorType::const_iterator it = savedElements.begin(); it != savedElements.end(); ++it)
564  {
565  m_vector.push_back( *it );
566  // Hash on data, index is data value
567  m_hash.insert( *it, m_vector.size()-1 );
568  }
569 #else // Faster implementation
570  typename agx::Vector<typename VectorType::value_type *> removeElements; // Safe to use pointers?
571  removeElements.reserve( m_vector.size());
572  for(typename VectorType::iterator it = m_vector.begin(); it != m_vector.end(); ++it)
573  {
574  // Should we keep it?
575  if (!purger(*it))
576  removeElements.push_back(&(*it)); // store all elements that should remain
577  }
578 
579  for(typename agx::Vector<typename VectorType::value_type *>::const_iterator it = removeElements.begin(); it != removeElements.end(); ++it)
580  {
581  this->erase( **it );
582  }
583 
584  return removeElements.size();
585 #endif
586  }
587 
588  private:
589 
590  // Return true if size of vector and hash is equal
591  AGX_FORCE_INLINE bool checkSize() const {
592  return m_vector.size() == m_hash.size();
593  }
594 
595  void erase( typename HashType::iterator it )
596  {
597  size_t idx = it->second;
598 
599  m_hash.erase( it ); // remove from hash
600 
601  // Remove from vector too:
602  if (m_vector.size() > 1 && idx != (m_vector.size()-1))
603  {
604  // Get the last element
605  typename VectorType::value_type lastElement = m_vector.back();
606 
607  // Put it where we have removed an element
608  m_vector[idx] = lastElement;
609 
610  // remove the last now
611  m_vector.pop_back();
612 
613  // Rehash the moved one with the new index
614  m_hash.insert( lastElement, idx );
615  }
616  else
617  // Only one element, OR its the last we are removing: just remove it
618  m_vector.pop_back();
619 
620  agxAssert( checkSize() );
621  }
622 
623 
624  };
625 
626 }
627 
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
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.
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:38
const VectorType & vector() const
Definition: SetVector.h:98
void reserve(size_t size)
Reserve capacity in the hash table.
Definition: SetVector.h:505
size_t purge(T purger)
Purge the hash set.
Definition: SetVector.h:541
bool contains(const T2 &key) const
Hash search for the data in the container.
Definition: SetVector.h:221
VectorType::const_reverse_iterator const_reverse_iterator
Definition: SetVector.h:65
const_iterator end() const
Definition: SetVector.h:322
iterator find(const T2 &key)
Perform a hash search for a given key.
Definition: SetVector.h:237
HashTable< DataT, size_t, HashT > HashType
Definition: SetVector.h:46
iterator begin()
Definition: SetVector.h:272
@ SHRINK_BUFFER_AVERAGED
Definition: SetVector.h:57
const_iterator find(const T2 &key) const
Perform a hash search for a given key.
Definition: SetVector.h:256
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
Definition: SetVector.h:442
VectorType::reverse_iterator reverse_iterator
Definition: SetVector.h:64
DataT & back()
Definition: SetVector.h:367
iterator end()
Definition: SetVector.h:312
const_iterator begin() const
Definition: SetVector.h:282
const_reverse_iterator rbegin() const
Definition: SetVector.h:302
bool erase(const iterator &it)
Erase an element using an iterator.
Definition: SetVector.h:418
reverse_iterator rbegin()
Definition: SetVector.h:292
SetVector()
Constructor.
Definition: SetVector.h:69
SetVector(const ContainerType &other)
Copy constructor.
Definition: SetVector.h:73
DataT & operator[](size_t i)
Access an element in the vector using an index.
Definition: SetVector.h:458
iterator push_back(const T2 &data)
Insert a data element in to the end of the container.
Definition: SetVector.h:144
ContainerType & operator=(const ContainerType &other)
Assignment operator.
Definition: SetVector.h:80
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
Definition: SetVector.h:470
const HashType & table() const
Definition: SetVector.h:106
bool empty() const
Return true if the container is empty.
Definition: SetVector.h:124
const DataT & front() const
Definition: SetVector.h:358
DataT & at(size_t index)
Access an element in the vector using an index.
Definition: SetVector.h:495
iterator insert(const T2 &data)
Insert a data element in to the end of the container.
Definition: SetVector.h:178
DataT & front()
Definition: SetVector.h:349
size_t size() const
Return the number of inserted elements.
Definition: SetVector.h:114
Vector< DataT > VectorType
Definition: SetVector.h:45
iterator replace(const OldDataT &oldData, const NewDataT &newData)
Replace data in set vector.
Definition: SetVector.h:196
reverse_iterator rend()
Definition: SetVector.h:331
const DataT & at(size_t index) const
Access an element in the vector using an index.
Definition: SetVector.h:484
VectorType::const_iterator const_iterator
Definition: SetVector.h:63
const DataT & const_reference
Definition: SetVector.h:42
bool erase(const T2 &key)
Erase an element from the container.
Definition: SetVector.h:393
SetVector< DataT, HashT > ContainerType
Definition: SetVector.h:44
DataT value_type
Definition: SetVector.h:41
VectorType::iterator iterator
Definition: SetVector.h:62
size_t size_type
Definition: SetVector.h:43
const DataT & back() const
Definition: SetVector.h:376
~SetVector()
Destructor.
Definition: SetVector.h:89
const_reverse_iterator rend() const
Definition: SetVector.h:340
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
Definition: agx/Vector.h:491
reverse_iterator rbegin()
Definition: agx/Vector.h:1053
iterator end()
Definition: agx/Vector.h:1044
T & at(size_t index) const
Definition: agx/Vector.h:691
const DataT * 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
void pop_back()
Definition: agx/Vector.h:754
void push_back(const T2 &value)
Definition: agx/Vector.h:715
std::reverse_iterator< iterator > reverse_iterator
Definition: agx/Vector.h:137
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: agx/Vector.h:138
reverse_iterator rend()
Definition: agx/Vector.h:1056
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.