AGX Dynamics  2.38.0.2
SetVector.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 #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
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  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 
102  const VectorType& vector() const
103  {
104  return m_vector;
105  }
106 
110  const HashType& table() const
111  {
112  return m_hash;
113  }
114 
118  inline size_t size() const
119  {
120  agxAssert( checkSize() );
121 
122  return m_vector.size();
123  }
124 
128  inline bool empty() const
129  {
130  return m_vector.empty();
131  }
132 
147  template< typename T2 >
148  inline iterator push_back(const T2& data)
149  {
150  agxAssert( checkSize() );
151 
152  typename HashType::iterator it = m_hash.find(data);
153  if (it != m_hash.end())
154  {
155  // Replace the already existing data value with the new
156  m_vector[it->second] = data;
157  return &m_vector[it->second];
158  }
159 
160  // Key did not exist, insert at the end of the vector
161  m_vector.push_back( data );
162 
163  // Hash on data, index is data value
164  m_hash.insert( data, m_vector.size()-1 );
165 
166  return end()-1; // Return iterator to the newly inserted element.
167  }
168 
181  template< typename T2 >
182  inline iterator insert(const T2& data)
183  {
184  agxAssert( checkSize() );
185 
186  return push_back(data);
187  }
188 
199  template< typename OldDataT, typename NewDataT >
200  iterator replace( const OldDataT& oldData, const NewDataT& newData )
201  {
202  agxAssert( checkSize() );
203 
204  auto oldIt = m_hash.find( oldData );
205  if ( oldIt == m_hash.end() )
206  return end();
207 
208  size_t index = oldIt->second;
209  m_vector[ index ] = newData;
210  m_hash.erase( oldIt );
211  m_hash.insert( newData, index );
212 
213  return begin() + index;
214  }
215 
224  template< typename T2 >
225  inline bool contains(const T2& key) const
226  {
227  agxAssert( checkSize() );
228 
229  return m_hash.contains( key );
230  }
231 
240  template< typename T2 >
241  iterator find(const T2& key)
242  {
243  agxAssert( checkSize() );
244 
245  typename HashType::iterator it = m_hash.find( key );
246  if (it == m_hash.end())
247  return end();
248 
249  return &m_vector[it->second];
250  }
251 
259  template< typename T2 >
260  const_iterator find(const T2& key) const
261  {
262  agxAssert( checkSize() );
263 
264  typename HashType::const_iterator it = m_hash.find( key );
265  if (it == m_hash.end())
266  return end();
267 
268  // Return index to the element in the vector
269  return &m_vector[it->second];
270  }
271 
272 #ifndef SWIG
273 
276  inline iterator begin()
277  {
278  agxAssert( checkSize() );
279 
280  return m_vector.begin();
281  }
282 
286  inline const_iterator begin() const
287  {
288  agxAssert( checkSize() );
289 
290  return m_vector.begin();
291  }
292 
297  {
298  agxAssert( checkSize() );
299 
300  return m_vector.rbegin();
301  }
302 
307  {
308  agxAssert( checkSize() );
309 
310  return m_vector.rbegin();
311  }
312 
316  inline iterator end()
317  {
318  agxAssert( checkSize() );
319 
320  return m_vector.end();
321  }
322 
326  inline const_iterator end() const
327  {
328  agxAssert( checkSize() );
329  return m_vector.end();
330  }
331 
336  {
337  agxAssert( checkSize() );
338 
339  return m_vector.rend(); }
340 
345  {
346  agxAssert( checkSize() );
347  return m_vector.rend();
348  }
349 #endif
350 
353  inline DataT& front()
354  {
355  agxAssert( checkSize() );
356  return m_vector[0];
357  }
358 
362  inline const DataT& front() const
363  {
364  agxAssert( checkSize() );
365  return m_vector[0];
366  }
367 
371  inline DataT& back()
372  {
373  agxAssert( checkSize() );
374  return m_vector[m_vector.size()-1];
375  }
376 
380  inline const DataT& back() const
381  {
382  agxAssert( checkSize() );
383  return m_vector[m_vector.size()-1];
384  }
385 
386 
396  template< typename T2 >
397  bool erase(const T2& key)
398  {
399  agxAssert( checkSize() );
400 
401  typename HashType::iterator it = m_hash.find( key );
402  if (it == m_hash.end())
403  return false;
404 
405  erase( it );
406  return true;
407  }
408 
422  bool erase(const iterator& it)
423  {
424 
425  agxAssert( checkSize() );
426 
427  if (it == end())
428  return false;
429  agxAssert( it != end() );
430 
431  DataT& key = *it;
432  typename HashType::iterator hashIt = m_hash.find( key );
433 
434  agxAssert ( hashIt != m_hash.end() );
435 
436  erase( hashIt );
437 
438  return true;
439  }
440 
445 #ifndef SWIG
447 #else
448  void clear()
449 #endif
450  {
451 
452  agxAssert( checkSize() );
453 
454  m_hash.clear( (typename HashType::ClearPolicy)policy );
455  m_vector.clear( (typename VectorType::ClearPolicy)policy );
456  }
457 
462  inline DataT& operator[](size_t i )
463  {
464  agxAssert( checkSize() );
465 
466  agxAssert(i < m_vector.size());
467  return m_vector[i];
468  }
469 
474  inline const DataT& operator[](size_t i ) const
475  {
476  agxAssert( checkSize() );
477 
478  agxAssert(i < m_vector.size());
479  return m_vector[i];
480  }
481 
482 
488  inline const DataT& at(size_t index) const
489  {
490  return m_vector.at(index);
491  }
492 
493 
499  inline DataT& at(size_t index)
500  {
501  return m_vector.at(index);
502  }
503 
504 
509  void reserve(size_t size)
510  {
511  agxAssert( checkSize() );
512 
513  m_vector.reserve( size );
514  m_hash.reserve( size );
515  }
516 
517 
518 
544  template<typename T>
545  size_t purge(T purger)
546  {
547  if (empty()) // Nothing to do
548  return 0;
549 
550 #if 0 // Slower implementation
551  typename VectorType savedElements; // Elements to keep
552  savedElements.reserve( m_vector.size());
553  typename VectorType::const_iterator it = m_vector.begin();
554  for(; it != m_vector.end(); ++it)
555  {
556  // Should we keep it?
557  if (!purger(*it))
558  savedElements.push_back(*it); // store all elements that should remain
559  }
562 
563  // Make sure we can fit all the saved elements
564  m_vector.reserve( savedElements.size() );
565  m_hash.reserve( savedElements.size() );
566 
567  for(typename VectorType::const_iterator it = savedElements.begin(); it != savedElements.end(); ++it)
568  {
569  m_vector.push_back( *it );
570  // Hash on data, index is data value
571  m_hash.insert( *it, m_vector.size()-1 );
572  }
573 #else // Faster implementation
574  typename agx::Vector<typename VectorType::value_type *> removeElements; // Safe to use pointers?
575  removeElements.reserve( m_vector.size());
576  for(typename VectorType::iterator it = m_vector.begin(); it != m_vector.end(); ++it)
577  {
578  // Should we keep it?
579  if (!purger(*it))
580  removeElements.push_back(&(*it)); // store all elements that should remain
581  }
582 
583  for(typename agx::Vector<typename VectorType::value_type *>::const_iterator it = removeElements.begin(); it != removeElements.end(); ++it)
584  {
585  this->erase( **it );
586  }
587 
588  return removeElements.size();
589 #endif
590  }
591 
592  private:
593 
594  // Return true if size of vector and hash is equal
595  AGX_FORCE_INLINE bool checkSize() const {
596  return m_vector.size() == m_hash.size();
597  }
598 
599  void erase( typename HashType::iterator it )
600  {
601  size_t idx = it->second;
602 
603  m_hash.erase( it ); // remove from hash
604 
605  // Remove from vector too:
606  if (m_vector.size() > 1 && idx != (m_vector.size()-1))
607  {
608  // Get the last element
609  typename VectorType::value_type lastElement = m_vector.back();
610 
611  // Put it where we have removed an element
612  m_vector[idx] = lastElement;
613 
614  // remove the last now
615  m_vector.pop_back();
616 
617  // Rehash the moved one with the new index
618  m_hash.insert( lastElement, idx );
619  }
620  else
621  // Only one element, OR its the last we are removing: just remove it
622  m_vector.pop_back();
623 
624  agxAssert( checkSize() );
625  }
626 
627 
628  };
629 
630 }
631 
agx::Vector< DataT >::const_iterator
const typedef DataT * const_iterator
Definition: Vector.h:63
agx::SetVector::rbegin
const_reverse_iterator rbegin() const
Definition: SetVector.h:306
agx::SetVector::const_reference
const typedef DataT & const_reference
Definition: SetVector.h:42
agx::LinearProbingHashTableImplementation::erase
bool erase(const KeyT &key)
Erase an element from the hash table.
Definition: LinearProbingHashTable.h:612
agx::SetVector::back
const DataT & back() const
Definition: SetVector.h:380
Vector.h
agx::SetVector::insert
iterator insert(const T2 &data)
Insert a data element in to the end of the container.
Definition: SetVector.h:182
agx::Vector::end
iterator end()
Definition: Vector.h:1044
agx::SetVector::ContainerType
SetVector< DataT, HashT > ContainerType
Definition: SetVector.h:44
agx::Vector< DataT >
agx::Vector< DataT >::value_type
DataT value_type
Definition: Vector.h:56
agx::Vector::begin
iterator begin()
Definition: Vector.h:1041
agx::Container::size
size_t size() const
Definition: Container.h:134
agx::LinearProbingHashTableImplementation::clear
void clear(int policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
Definition: LinearProbingHashTable.h:638
agx::SetVector::size
size_t size() const
Return the number of inserted elements.
Definition: SetVector.h:118
agx::LinearProbingHashTableImplementation< KeyT, DataT, agx::HashFn< KeyT >, ByteAllocator >::ClearPolicy
ClearPolicy
Definition: LinearProbingHashTable.h:78
HashTable.h
agx::Vector::pop_back
void pop_back()
Definition: Vector.h:754
agx::SetVector::const_iterator
VectorType::const_iterator const_iterator
Definition: SetVector.h:63
agx::SetVector::erase
bool erase(const iterator &it)
Erase an element using an iterator.
Definition: SetVector.h:422
agx::Container::empty
bool empty() const
Definition: Container.h:136
agx::SetVector::reverse_iterator
VectorType::reverse_iterator reverse_iterator
Definition: SetVector.h:64
agx::SetVector::erase
bool erase(const T2 &key)
Erase an element from the container.
Definition: SetVector.h:397
agx::SetVector::end
const_iterator end() const
Definition: SetVector.h:326
agx::SetVector::~SetVector
~SetVector()
Destructor.
Definition: SetVector.h:93
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agx::Vector::reserve
void reserve(size_t size)
Reserve capacity in the vector.
Definition: Vector.h:583
agx::SetVector::end
iterator end()
Definition: SetVector.h:316
agx::SetVector::find
const_iterator find(const T2 &key) const
Perform a hash search for a given key.
Definition: SetVector.h:260
agx::SetVector::front
const DataT & front() const
Definition: SetVector.h:362
agx::Container::SHRINK_BUFFER_AVERAGED
@ SHRINK_BUFFER_AVERAGED
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold.
Definition: Container.h:45
agx::SetVector::rbegin
reverse_iterator rbegin()
Definition: SetVector.h:296
agx::Container::SHRINK_BUFFER
@ SHRINK_BUFFER
Buffer is deallocated and replaced by an newly allocated empty buffer.
Definition: Container.h:44
agx::SetVector::begin
iterator begin()
Definition: SetVector.h:276
agx::LinearProbingHashTableImplementation::end
iterator end()
Iterator marking end of hash table.
Definition: LinearProbingHashTable.h:370
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
agx::Vector::rend
reverse_iterator rend()
Definition: Vector.h:1056
agx::LinearProbingHashTableImplementation::find
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
Definition: LinearProbingHashTable.h:411
agx::SetVector::clear
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
Definition: SetVector.h:446
agxPowerLine::Unit
Pure virtual class.
Definition: Unit.h:71
agx::SetVector::empty
bool empty() const
Return true if the container is empty.
Definition: SetVector.h:128
agx::SetVector::rend
const_reverse_iterator rend() const
Definition: SetVector.h:344
agx::Vector::clear
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
Definition: Vector.h:491
agx::SetVector::size_type
size_t size_type
Definition: SetVector.h:43
agx::SetVector
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: SetVector.h:37
agx::SetVector::at
const DataT & at(size_t index) const
Access an element in the vector using an index.
Definition: SetVector.h:488
agx::SetVector::front
DataT & front()
Definition: SetVector.h:353
agx::LinearProbingHashTableImplementation::contains
bool contains(const KeyT &k) const
Check if the hash table contains a key/value pair for the given key.
Definition: LinearProbingHashTable.h:440
agx::SetVector::SHRINK_BUFFER
@ SHRINK_BUFFER
Definition: SetVector.h:56
agx::LinearProbingHashTable
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: LinearProbingHashTable.h:926
agx::SetVector::operator[]
DataT & operator[](size_t i)
Access an element in the vector using an index.
Definition: SetVector.h:462
agx::SetVector::purge
size_t purge(T purger)
Purge the hash set.
Definition: SetVector.h:545
agx::SetVector::rend
reverse_iterator rend()
Definition: SetVector.h:335
agx::SetVector::push_back
iterator push_back(const T2 &data)
Insert a data element in to the end of the container.
Definition: SetVector.h:148
agx::SetVector::HashType
HashTable< DataT, size_t, HashT > HashType
Definition: SetVector.h:46
agx::Container::ClearPolicy
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
Definition: Container.h:42
agx::SetVector::table
const HashType & table() const
Definition: SetVector.h:110
agx::SetVector< const agxPowerLine::Unit * >::ClearPolicy
ClearPolicy
Definition: SetVector.h:54
agx::SetVector::begin
const_iterator begin() const
Definition: SetVector.h:286
agx::LinearProbingHashTableImplementation::reserve
void reserve(size_t num_elems)
Make room for this many elements in the hash table.
Definition: LinearProbingHashTable.h:686
agx::SetVector::operator[]
const DataT & operator[](size_t i) const
Access an element in the vector using an index.
Definition: SetVector.h:474
agx::SetVector::at
DataT & at(size_t index)
Access an element in the vector using an index.
Definition: SetVector.h:499
agx::SetVector::MAINTAIN_BUFFER
@ MAINTAIN_BUFFER
Definition: SetVector.h:58
agx::SetVector::const_reverse_iterator
VectorType::const_reverse_iterator const_reverse_iterator
Definition: SetVector.h:65
agx::Vector< const agxPowerLine::Unit * >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: Vector.h:137
agx::LinearProbingHashTableImplementation::size
size_t size() const
Definition: LinearProbingHashTable.h:383
agx::SetVector::vector
const VectorType & vector() const
Definition: SetVector.h:102
agx::SetVector::SHRINK_BUFFER_AVERAGED
@ SHRINK_BUFFER_AVERAGED
Definition: SetVector.h:57
agx::SetVector::operator=
ContainerType & operator=(const ContainerType &other)
Assignment operator.
Definition: SetVector.h:80
agx::Vector::at
T & at(size_t index) const
Definition: Vector.h:691
agx::Container::MAINTAIN_BUFFER
@ MAINTAIN_BUFFER
Buffer is maintained (normal stl behavior).
Definition: Container.h:46
agx::SetVector::replace
iterator replace(const OldDataT &oldData, const NewDataT &newData)
Replace data in set vector.
Definition: SetVector.h:200
agx::Vector< const agxPowerLine::Unit * >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: Vector.h:138
agx::Vector::back
T & back() const
Definition: Vector.h:707
agx::SetVector::find
iterator find(const T2 &key)
Perform a hash search for a given key.
Definition: SetVector.h:241
agx::SetVector::SetVector
SetVector()
Constructor.
Definition: SetVector.h:69
agx::LinearProbingHashTableImplementation::insert
iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
Definition: LinearProbingHashTable.h:506
agx::SetVector::back
DataT & back()
Definition: SetVector.h:371
agx::SetVector::value_type
DataT value_type
Definition: SetVector.h:41
agx::Vector::rbegin
reverse_iterator rbegin()
Definition: Vector.h:1053
agx::SetVector::SetVector
SetVector(const ContainerType &other)
Copy constructor.
Definition: SetVector.h:73
agx::SetVector::reserve
void reserve(size_t size)
Reserve capacity in the hash table.
Definition: SetVector.h:509
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agx::SetVector::VectorType
Vector< DataT > VectorType
Definition: SetVector.h:45
agx::SetVector::iterator
VectorType::iterator iterator
Definition: SetVector.h:62
agx::SetVector::contains
bool contains(const T2 &key) const
Hash search for the data in the container.
Definition: SetVector.h:225
agx::Vector::push_back
void push_back(const T2 &value)
Definition: Vector.h:715