AGX Dynamics  2.37.2.0
HashTableComponent.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 
18 
19 
20 
21 #ifndef AGX_HASHTABLECONTEXT_H
22 #define AGX_HASHTABLECONTEXT_H
23 
24 
25 #include <agx/Component.h>
27 
28 #include <agxData/Buffer.h>
29 
30 #ifdef _MSC_VER
31 # pragma warning(push)
32 # pragma warning( disable : 4251 ) // needs to have dll-interface to be used by clients
33 #endif
34 
36 
37 namespace agx
38 {
39  class AGXCORE_EXPORT AbstractHashTableComponent : public Component
40  {
41  public:
42  static agx::Model *ClassModel();
43 
44  static AbstractHashTableComponent *load(TiXmlElement *eHashTable, Device *device);
45 
46  public:
47  AbstractHashTableComponent(const agx::Name& name);
48 
49  // Access bucket buffer
50  agxData::Buffer *getBucketBuffer();
51 
52  protected:
53  virtual ~AbstractHashTableComponent();
54 
55  protected:
56  agxData::BufferRef m_buckets;
57  // agxData::Val<UInt> m_numBuckets; // Use "buckets.numElements" instead
58  agxData::Val<UInt> m_numFilled;
59  agxData::Val<UInt> m_maxProbeLength;
60  };
61 
62 
63  template <typename KeyT, typename DataT, typename HashT = agx::HashFn<KeyT> >
64  class HashTableComponent : public AbstractHashTableComponent
65  {
66  public:
67 
68  // Cannot use implementation agnostic HashTable type alias because the
69  // HashTableComponent make assumptions about the memory layout of its
70  // underlying HashTable container. Must therefore use
71  // QuadraticProbingHashTable.
72  typedef QuadraticProbingHashTable<KeyT, DataT, HashT, agxData::BufferProxyAllocator> ImplementationType;
73  // void configure(TiXmlElement *eHashTable);
74 
75  public:
76  HashTableComponent(const agx::Name& name);
77 
78  // Synchronize implementation with buffer and values
79  void commitImplementation();
80  void updateImplementation();
81 
82  // Access the agx::HashTable implementation
83  ImplementationType& impl();
84  const ImplementationType& impl() const;
85 
86 
87  // 'Generic' constructor
88  HashTableComponent(const agx::Name& name, size_t numBytes);
89 
90  protected:
91  virtual ~HashTableComponent();
92 
93  private:
94  ImplementationType m_implementation;
95  };
96 
97 
98 
99  /* Implementation */
100  AGX_FORCE_INLINE agxData::Buffer *AbstractHashTableComponent::getBucketBuffer() { return m_buckets; }
101 
102  template <typename KeyT, typename DataT, typename HashT>
103  HashTableComponent<KeyT, DataT, HashT>::HashTableComponent(const agx::Name& name) : AbstractHashTableComponent(name)
104  {
105  this->addObject(&m_numFilled);
106  this->addObject(&m_maxProbeLength);
107 
108  m_buckets = new agxData::Buffer("buckets", agxData::getFormat<typename ImplementationType::Bucket>());
109  this->addObject(m_buckets);
110 
111  m_implementation.allocator().setBuffer(m_buckets);
112  }
113 
114  template <typename KeyT, typename DataT, typename HashT>
115  HashTableComponent<KeyT, DataT, HashT>::HashTableComponent(const agx::Name& name, size_t numBytes) : AbstractHashTableComponent(name)
116  {
117  this->addObject(&m_numFilled);
118  this->addObject(&m_maxProbeLength);
119 
120  m_buckets = new agxData::Buffer("buckets", agxData::getGenericStructFormat(numBytes));
121  this->addObject(m_buckets);
122 
123  m_implementation.allocator().setBuffer(m_buckets);
124  }
125 
126 
127  template <typename KeyT, typename DataT, typename HashT>
128  HashTableComponent<KeyT, DataT, HashT>::~HashTableComponent()
129  {
130  m_implementation.clear(Container::SHRINK_BUFFER);
131  // m_implementation.setExplicitBuffer(nullptr, 0);
132  this->removeAllObjects();
133  }
134 
135  template <typename KeyT, typename DataT, typename HashT>
136  AGX_FORCE_INLINE typename HashTableComponent<KeyT, DataT, HashT>::ImplementationType& HashTableComponent<KeyT, DataT, HashT>::impl() { return m_implementation; }
137 
138  template <typename KeyT, typename DataT, typename HashT>
139  AGX_FORCE_INLINE const typename HashTableComponent<KeyT, DataT, HashT>::ImplementationType& HashTableComponent<KeyT, DataT, HashT>::impl() const { return m_implementation; }
140 
141  template <typename KeyT, typename DataT, typename HashT>
142  void HashTableComponent<KeyT, DataT, HashT>::commitImplementation()
143  {
144  m_numFilled = m_implementation.m_size;
145  m_maxProbeLength = m_implementation.m_maxProbeLength;
146  // m_buckets->m_minSize = m_implementation.capacity();
147  m_implementation.allocator().commit();
148  }
149 
150  template <typename KeyT, typename DataT, typename HashT>
151  void HashTableComponent<KeyT, DataT, HashT>::updateImplementation()
152  {
153  m_implementation.allocator().update();
154  m_implementation.m_size = m_numFilled;
155  m_implementation.m_maxProbeLength = m_maxProbeLength;
156  }
157 
158 }
160 
161 
162 #ifdef _MSC_VER
163 # pragma warning(pop)
164 #endif
165 
166 
167 #endif /* AGX_HASHTABLECONTEXT_H */
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:40
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:59
Buffer is deallocated and replaced by an newly allocated empty buffer.
Definition: Container.h:44
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Abstract representation of a data buffer.
Definition: Buffer.h:55
AGXCORE_EXPORT Format * getGenericStructFormat(size_t numBytes)
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define AGXCORE_EXPORT
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97