AGXDynamics  2.31.1.0
SetVector.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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_SETVECTOR_H
18 #define AGX_SETVECTOR_H
19 
20 
21 #include <agx/Vector.h>
22 #include <agx/HashTable.h>
23 
24 namespace agx
25 {
26 
37  template <typename DataT, typename HashT = HashFn<DataT> >
38  class SetVector
39  {
40  public:
41 
42  typedef DataT value_type;
43  typedef const DataT& const_reference;
44  typedef size_t size_type;
48 
49  private:
50  VectorType m_vector;
51  HashType m_hash;
52 
53  public:
54 #ifndef SWIG
56  {
57  SHRINK_BUFFER=VectorType::SHRINK_BUFFER, // Buffer is deallocated and replaced by an empty buffer
58  SHRINK_BUFFER_AVERAGED=VectorType::SHRINK_BUFFER_AVERAGED, // Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold
60  };
61 #endif
62 
63  typedef typename VectorType::iterator iterator;
67 
68 
71  {}
72 
74  SetVector(const ContainerType& other) : m_vector( other.m_vector ), m_hash( other.m_hash )
75  {
76  this->clear(SHRINK_BUFFER);
77  *this = other;
78  }
79 
81  ContainerType& operator= (const ContainerType& other) // NOLINT clang-tidy-7 doesn't realize that ContainerType is a SetVector.
82  {
83  m_vector = other.m_vector;
84  m_hash = other.m_hash;
85  return *this;
86  }
87 
88 
91  {
92  // don't change buffer, will be dealloc'ed soon anyway
94  }
95 
99  const VectorType& vector() const
100  {
101  return m_vector;
102  }
103 
107  const HashType& table() const
108  {
109  return m_hash;
110  }
111 
115  inline size_t size() const
116  {
117  agxAssert( checkSize() );
118 
119  return m_vector.size();
120  }
121 
125  inline bool empty() const
126  {
127  return m_vector.empty();
128  }
129 
144  template< typename T2 >
145  inline iterator push_back(const T2& data)
146  {
147  agxAssert( checkSize() );
148 
149  typename HashType::iterator it = m_hash.find(data);
150  if (it != m_hash.end())
151  {
152  // Replace the already existing data value with the new
153  m_vector[it->second] = data;
154  return &m_vector[it->second];
155  }
156 
157  // Key did not exist, insert at the end of the vector
158  m_vector.push_back( data );
159 
160  // Hash on data, index is data value
161  m_hash.insert( data, m_vector.size()-1 );
162 
163  return end()-1; // Return iterator to the newly inserted element.
164  }
165 
178  template< typename T2 >
179  inline iterator insert(const T2& data)
180  {
181  agxAssert( checkSize() );
182 
183  return push_back(data);
184  }
185 
196  template< typename OldDataT, typename NewDataT >
197  iterator replace( const OldDataT& oldData, const NewDataT& newData )
198  {
199  agxAssert( checkSize() );
200 
201  auto oldIt = m_hash.find( oldData );
202  if ( oldIt == m_hash.end() )
203  return end();
204 
205  size_t index = oldIt->second;
206  m_vector[ index ] = newData;
207  m_hash.erase( oldIt );
208  m_hash.insert( newData, index );
209 
210  return begin() + index;
211  }
212 
221  template< typename T2 >
222  inline bool contains(const T2& key) const
223  {
224  agxAssert( checkSize() );
225 
226  return m_hash.contains( key );
227  }
228 
237  template< typename T2 >
238  iterator find(const T2& key)
239  {
240  agxAssert( checkSize() );
241 
242  typename HashType::iterator it = m_hash.find( key );
243  if (it == m_hash.end())
244  return end();
245 
246  return &m_vector[it->second];
247  }
248 
256  template< typename T2 >
257  const_iterator find(const T2& key) const
258  {
259  agxAssert( checkSize() );
260 
261  typename HashType::const_iterator it = m_hash.find( key );
262  if (it == m_hash.end())
263  return end();
264 
265  // Return index to the element in the vector
266  return &m_vector[it->second];
267  }
268 
269 #ifndef SWIG
270 
273  inline iterator begin()
274  {
275  agxAssert( checkSize() );
276 
277  return m_vector.begin();
278  }
279 
283  inline const_iterator begin() const
284  {
285  agxAssert( checkSize() );
286 
287  return m_vector.begin();
288  }
289 
293  inline reverse_iterator rbegin()
294  {
295  agxAssert( checkSize() );
296 
297  return m_vector.rbegin();
298  }
299 
303  inline const_reverse_iterator rbegin() const
304  {
305  agxAssert( checkSize() );
306 
307  return m_vector.rbegin();
308  }
309 
313  inline iterator end()
314  {
315  agxAssert( checkSize() );
316 
317  return m_vector.end();
318  }
319 
323  inline const_iterator end() const
324  {
325  agxAssert( checkSize() );
326  return m_vector.end();
327  }
328 
332  inline reverse_iterator rend()
333  {
334  agxAssert( checkSize() );
335 
336  return m_vector.rend(); }
337 
341  inline const_reverse_iterator rend() const
342  {
343  agxAssert( checkSize() );
344  return m_vector.rend();
345  }
346 #endif
347 
350  inline DataT& front()
351  {
352  agxAssert( checkSize() );
353  return m_vector[0];
354  }
355 
359  inline const DataT& front() const
360  {
361  agxAssert( checkSize() );
362  return m_vector[0];
363  }
364 
368  inline DataT& back()
369  {
370  agxAssert( checkSize() );
371  return m_vector[m_vector.size()-1];
372  }
373 
377  inline const DataT& back() const
378  {
379  agxAssert( checkSize() );
380  return m_vector[m_vector.size()-1];
381  }
382 
383 
393  template< typename T2 >
394  bool erase(const T2& key)
395  {
396  agxAssert( checkSize() );
397 
398  typename HashType::iterator it = m_hash.find( key );
399  if (it == m_hash.end())
400  return false;
401 
402  erase( it );
403  return true;
404  }
405 
419  bool erase(const iterator& it)
420  {
421 
422  agxAssert( checkSize() );
423 
424  if (it == end())
425  return false;
426  agxAssert( it != end() );
427 
428  DataT& key = *it;
429  typename HashType::iterator hashIt = m_hash.find( key );
430 
431  agxAssert ( hashIt != m_hash.end() );
432 
433  erase( hashIt );
434 
435  return true;
436  }
437 
442 #ifndef SWIG
444 #else
445  void clear()
446 #endif
447  {
448 
449  agxAssert( checkSize() );
450 
451  m_hash.clear( (typename HashType::ClearPolicy)policy );
452  m_vector.clear( (typename VectorType::ClearPolicy)policy );
453  }
454 
459  inline DataT& operator[](size_t i )
460  {
461  agxAssert( checkSize() );
462 
463  agxAssert(i < m_vector.size());
464  return m_vector[i];
465  }
466 
471  inline const DataT& operator[](size_t i ) const
472  {
473  agxAssert( checkSize() );
474 
475  agxAssert(i < m_vector.size());
476  return m_vector[i];
477  }
478 
479 
485  inline const DataT& at(size_t index) const
486  {
487  return m_vector.at(index);
488  }
489 
490 
496  inline DataT& at(size_t index)
497  {
498  return m_vector.at(index);
499  }
500 
501 
506  void reserve(size_t size)
507  {
508  agxAssert( checkSize() );
509 
510  m_vector.reserve( size );
511  m_hash.reserve( size );
512  }
513 
514 
515 
541  template<typename T>
542  size_t purge(T purger)
543  {
544  if (empty()) // Nothing to do
545  return 0;
546 
547 #if 0 // Slower implementation
548  typename VectorType savedElements; // Elements to keep
549  savedElements.reserve( m_vector.size());
550  typename VectorType::const_iterator it = m_vector.begin();
551  for(; it != m_vector.end(); ++it)
552  {
553  // Should we keep it?
554  if (!purger(*it))
555  savedElements.push_back(*it); // store all elements that should remain
556  }
559 
560  // Make sure we can fit all the saved elements
561  m_vector.reserve( savedElements.size() );
562  m_hash.reserve( savedElements.size() );
563 
564  for(typename VectorType::const_iterator it = savedElements.begin(); it != savedElements.end(); ++it)
565  {
566  m_vector.push_back( *it );
567  // Hash on data, index is data value
568  m_hash.insert( *it, m_vector.size()-1 );
569  }
570 #else // Faster implementation
571  typename agx::Vector<typename VectorType::value_type *> removeElements; // Safe to use pointers?
572  removeElements.reserve( m_vector.size());
573  for(typename VectorType::iterator it = m_vector.begin(); it != m_vector.end(); ++it)
574  {
575  // Should we keep it?
576  if (!purger(*it))
577  removeElements.push_back(&(*it)); // store all elements that should remain
578  }
579 
580  for(typename agx::Vector<typename VectorType::value_type *>::const_iterator it = removeElements.begin(); it != removeElements.end(); ++it)
581  {
582  this->erase( **it );
583  }
584 
585  return removeElements.size();
586 #endif
587  }
588 
589  private:
590 
591  // Return true if size of vector and hash is equal
592  AGX_FORCE_INLINE bool checkSize() const {
593  return m_vector.size() == m_hash.size();
594  }
595 
596  void erase( typename HashType::iterator it )
597  {
598  size_t idx = it->second;
599 
600  m_hash.erase( it ); // remove from hash
601 
602  // Remove from vector too:
603  if (m_vector.size() > 1 && idx != (m_vector.size()-1))
604  {
605  // Get the last element
606  typename VectorType::value_type lastElement = m_vector.back();
607 
608  // Put it where we have removed an element
609  m_vector[idx] = lastElement;
610 
611  // remove the last now
612  m_vector.pop_back();
613 
614  // Rehash the moved one with the new index
615  m_hash.insert( lastElement, idx );
616  }
617  else
618  // Only one element, OR its the last we are removing: just remove it
619  m_vector.pop_back();
620 
621  agxAssert( checkSize() );
622  }
623 
624 
625  };
626 
627 }
628 
629 
630 #endif /* AGX_SETVECTOR_H */
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
Definition: SetVector.h:443
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
DataT & front()
Definition: SetVector.h:350
const HashType & table() const
Definition: SetVector.h:107
SetVector(const ContainerType &other)
Copy constructor.
Definition: SetVector.h:74
void pop_back()
Definition: Vector.h:754
HashTable< DataT, size_t, HashT > HashType
Definition: SetVector.h:47
iterator end()
Definition: SetVector.h:313
iterator begin()
Definition: Vector.h:1041
VectorType::const_iterator const_iterator
Definition: SetVector.h:64
const DataT & front() const
Definition: SetVector.h:359
void reserve(size_t size)
Reserve capacity in the hash table.
Definition: SetVector.h:506
iterator replace(const OldDataT &oldData, const NewDataT &newData)
Replace data in set vector.
Definition: SetVector.h:197
const_reverse_iterator rbegin() const
Definition: SetVector.h:303
~SetVector()
Destructor.
Definition: SetVector.h:90
const T * 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
#define AGX_FORCE_INLINE
Definition: Object.h:63
const VectorType & vector() const
Definition: SetVector.h:99
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Vector.h:138
reverse_iterator rend()
Definition: SetVector.h:332
VectorType::iterator iterator
Definition: SetVector.h:63
const DataT & const_reference
Definition: SetVector.h:43
bool empty() const
Return true if the container is empty.
Definition: SetVector.h:125
const DataT & back() const
Definition: SetVector.h:377
iterator end()
Definition: Vector.h:1044
reverse_iterator rbegin()
Definition: SetVector.h:293
ContainerType & operator=(const ContainerType &other)
Assignment operator.
Definition: SetVector.h:81
void reserve(size_t size)
Reserve capacity in the vector.
Definition: Vector.h:583
const_iterator find(const T2 &key) const
Perform a hash search for a given key.
Definition: SetVector.h:257
Buffer is deallocated and replaced by an newly allocated empty buffer.
Definition: Container.h:44
bool erase(const iterator &it)
Erase an element using an iterator.
Definition: SetVector.h:419
reverse_iterator rbegin()
Definition: Vector.h:1053
void reserve(size_t num_elems)
Make room for this many elements in the hash table.
void clear(int=0)
Remove all elements, keeping full capacity.
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
reverse_iterator rend()
Definition: Vector.h:1056
T & at(size_t index) const
Definition: Vector.h:691
const_iterator begin() const
Definition: SetVector.h:283
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.
Definition: Vector.h:491
iterator begin()
Definition: SetVector.h:273
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.
DataT & operator[](size_t i)
Access an element in the vector using an index.
Definition: SetVector.h:459
SetVector()
Constructor.
Definition: SetVector.h:70
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
Definition: SetVector.h:471
#define agxAssert(expr)
Definition: debug.h:143
bool erase(const KeyT &key)
Erase an element from the hash table.
DataT value_type
Definition: SetVector.h:42
size_t size_type
Definition: SetVector.h:44
DataT & at(size_t index)
Access an element in the vector using an index.
Definition: SetVector.h:496
VectorType::reverse_iterator reverse_iterator
Definition: SetVector.h:65
const_reverse_iterator rend() const
Definition: SetVector.h:341
VectorType::const_reverse_iterator const_reverse_iterator
Definition: SetVector.h:66
size_t size() const
Definition: Container.h:134
DataT & back()
Definition: SetVector.h:368
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
Return the number of inserted elements.
Definition: SetVector.h:115
const DataT & at(size_t index) const
Access an element in the vector using an index.
Definition: SetVector.h:485
iterator insert(const T2 &data)
Insert a data element in to the end of the container.
Definition: SetVector.h:179
SetVector< DataT, HashT > ContainerType
Definition: SetVector.h:45
const_iterator end() const
Definition: SetVector.h:323
Inheritance with partial specialization due to bug with ref_ptr containers.
std::reverse_iterator< iterator > reverse_iterator
Definition: Vector.h:137
Vector< DataT > VectorType
Definition: SetVector.h:46
Buffer is maintained (normal stl behavior).
Definition: Container.h:46
iterator find(const T2 &key)
Perform a hash search for a given key.
Definition: SetVector.h:238
bool contains(const T2 &key) const
Hash search for the data in the container.
Definition: SetVector.h:222
void push_back(const T2 &value)
Definition: Vector.h:715
size_t purge(T purger)
Purge the hash set.
Definition: SetVector.h:542
bool empty() const
Definition: Container.h:136
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:38
bool erase(const T2 &key)
Erase an element from the container.
Definition: SetVector.h:394
iterator push_back(const T2 &data)
Insert a data element in to the end of the container.
Definition: SetVector.h:145
T & back() const
Definition: Vector.h:707