32 #define AGX_DECLARE_HASHSET_TYPES(type) \ 33 typedef agx::HashSet<type ## Ref> type ## RefSet; \ 34 typedef agx::HashSet<type ## Observer> type ## ObserverSet; \ 35 typedef agx::HashSet<type *> type ## PtrSet 38 # pragma warning(push) 39 # pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer 48 template <
typename KeyT,
typename HashT = HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
63 #define AGX_HASH_SET_MIN_SIZE 1 65 #define AGX_HASH_SET_GROW_THRESHOLD 0.8 66 #define AGX_HASH_SET_SHRINK_THRESHOLD 0.25 67 #define AGX_HASH_SET_GROW_FACTOR 2.0 68 #define AGX_HASH_SET_SHRINK_FACTOR 0.5 69 #define AGX_HASH_SET_SMOOTHING_FACTOR 0.8 70 #define AGX_HASH_SET_MAX_PROBE_LENGTH 10 80 typedef typename std::iterator_traits<T>::value_type
value_type;
82 typedef typename std::iterator_traits<T>::reference
reference;
83 typedef typename std::iterator_traits<T>::pointer
pointer;
90 { this->gotoNextElement(); }
96 constIt.m_hashSet = m_hashSet;
97 constIt.m_currentIndex = m_currentIndex;
103 this->gotoNextElement();
109 size_t oldIndex = m_currentIndex;
110 this->gotoNextElement();
118 this->gotoPreviousElement();
124 size_t oldIndex = m_currentIndex;
125 this->gotoPreviousElement();
131 {
return this->m_currentIndex == rhs.m_currentIndex && this->m_hashSet == rhs.m_hashSet; }
134 {
return this->m_currentIndex != rhs.m_currentIndex || this->m_hashSet != rhs.m_hashSet; }
139 agxAssert1(m_hashSet && m_currentIndex != m_hashSet->numBuckets(),
"Can not dereference an invalid bucket!");
140 return m_hashSet->buckets()[m_currentIndex].key;
149 m_hashSet = copy.m_hashSet;
150 m_currentIndex = copy.m_currentIndex;
158 while (m_currentIndex < m_hashSet->
numBuckets() && !m_hashSet->buckets()[m_currentIndex].isValid())
164 if (m_currentIndex > 0) {
166 while (m_currentIndex > 0 && !m_hashSet->buckets()[m_currentIndex].isValid())
173 size_t m_currentIndex;
177 typedef template_iterator< HashSetImplementation<KeyT, HashT, AllocatorT> >
iterator;
178 typedef template_iterator< const HashSetImplementation<KeyT, HashT, AllocatorT> >
const_iterator;
209 { m_allocator.setContainer(
this); }
220 if (
this == &other ) {
224 m_allocator = other.m_allocator;
225 m_allocator.setContainer(
this);
230 for (const_iterator it = other.
begin(); it != other.
end(); ++it)
252 UInt32 hashValue = m_hashFunctor(key);
268 if (!bucket->isActive() || (bucket->isValid() &&
hashKeyEqual(bucket->key, key)))
270 targetBucket = bucket;
275 if (!bucket->isValid() && !targetBucket)
276 targetBucket = bucket;
279 if (targetBucket && offset >= m_maxProbeLength)
295 if (offset > m_maxProbeLength)
296 m_maxProbeLength = (
UInt16)offset;
298 targetBucket->key = key;
301 bool overwrite = targetBucket->isValid();
308 return insert_iterator(
this, targetBucket -
buckets(), overwrite);
327 iterator it =
find( key );
336 iterator it =
find( key );
393 return iterator(
this, it.m_currentIndex);
417 m_smoothingAverage = 0.0;
449 m_maxProbeLength = 0;
476 if (bucket.isValid() && purger(bucket.key))
489 const AllocatorT&
allocator()
const {
return m_allocator; }
515 {
return state >= ACTIVE; }
518 {
return state == VALID; }
545 const size_t desiredNumBytes = desiredNumBuckets *
sizeof(
Bucket);
547 m_buffer = m_allocator.allocateBytes(desiredNumBytes);
548 for (
size_t i = 0; i < desiredNumBuckets; ++i)
553 m_maxProbeLength = 0;
555 for (
size_t i = 0; i < oldNumBuckets; i++)
557 Bucket& bucket = oldBuckets[i];
559 if (bucket.isValid())
563 for (
size_t i = 0; i < oldNumBuckets; ++i)
566 m_allocator.deallocateBytes((
void *)oldBuckets, oldNumBuckets *
sizeof(
Bucket));
570 template <
typename T2>
576 UInt32 hashValue = m_hashFunctor(key);
581 if (bucket->isValid() &&
hashKeyEqual(bucket->key, key))
585 for (
size_t offset = 1; bucket->isActive() && offset <= m_maxProbeLength; offset++)
587 index = (hashValue + offset * offset) %
numBuckets();
590 if (bucket->isValid() &&
hashKeyEqual(bucket->key, key))
604 if (!bucket->isValid())
623 Real32 m_smoothingAverage;
625 AllocatorT m_allocator;
668 template <
typename KeyT,
typename HashT = HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
678 template <
typename KeyT,
typename HashT,
typename AllocatorT>
689 using Implementation::contains;
695 using Implementation::find;
706 using Implementation::erase;
717 # pragma warning(pop) bool hashKeyEqual(const T1 &key1, const T2 &key2)
HashSetImplementation< KeyT, HashT, AllocatorT > Implementation
template_iterator()
Constructors.
KeyT & operator[](const KeyT &key)
Index operator for accessing elements in the hash, inserting it if not found.
The Container is the base class for several of the container classes proided by AGX, such as agx::Vector and agx::HashSet.
Inheritance with partial specialization due to bug with ref_ptr containers.
void reserve(size_t size)
Reserve capacity in the hash set.
Same as hash table, but only containing keys, not key-value pairs.
template_iterator< T > & operator++()
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
#define AGX_HASH_SET_SHRINK_THRESHOLD
HashSetImplementation(const HashSetImplementation< KeyT, HashT, AllocatorT > &other)
Copy constructor.
iterator::HashSetT HashSetT
std::iterator_traits< T >::value_type value_type
bool didOverwrite() const
std::pair< agx::Name, agx::Real > Bucket
bool operator==(const template_iterator< T > &rhs)
Implementation::const_iterator const_iterator
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
bool contains(const KeyT *key) const
const AllocatorT & allocator() const
bool eraseBucket(size_t index)
iterator end()
Iterator marking end of hash set.
const KeyT & const_reference
~HashSetImplementation()
Destructor.
void rebuild(size_t size)
Rebuild, and optionally resize hash set.
#define AGX_HASH_SET_GROW_THRESHOLD
template_iterator< T > operator--()
iterator begin()
Iterator to first element in hash set.
HashSet(const AllocatorT &allocator=AllocatorT())
bool erase(const KeyT *key)
#define AGX_HASH_SET_GROW_FACTOR
HashSetImplementation< agx::ref_ptr< KeyT >, HashT, AllocatorT > Implementation
bool erase(const KeyT &key)
Erase an element from the hash set.
Buffer is deallocated and replaced by an newly allocated empty buffer.
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::iterator_traits< T >::iterator_category iterator_category
template_iterator< T > operator--(int)
bool contains(const KeyT &key) const
Boolean query to test existence of an element.
#define AGX_HASH_SET_SHRINK_FACTOR
std::iterator_traits< T >::difference_type difference_type
Implementation::iterator iterator
const_iterator end() const
insert_iterator insert(const KeyT &key)
Insert an element, overwriting if key already exists.
#define AGX_HASH_SET_SMOOTHING_FACTOR
#define AGX_HASH_SET_MAX_PROBE_LENGTH
Smart pointer for handling referenced counted objects.
bool operator!=(const template_iterator< T > &rhs)
const KeyT * const_pointer
iterator erase(const iterator &it)
Erase an element using an iterator.
HashSet(const AllocatorT &allocator=AllocatorT())
HashSetImplementation(const AllocatorT &allocator=AllocatorT())
Constructor.
std::iterator_traits< T >::reference reference
KeyT * operator->() const
AGXCORE_EXPORT int nextPrime(int n)
template_iterator< const HashSetImplementation< KeyT, HashT, AllocatorT > > const_iterator
const_iterator find(const KeyT &key) const
const_iterator find(const KeyT *key) const
size_t numBuckets() const
#define AGX_HASH_SET_MIN_SIZE
iterator find(const KeyT *key)
template_iterator< T > & operator=(const template_iterator< T > ©)
template_iterator(T *hashSet, size_t startIndex=0)
#define agxAssert1(expr, msg)
insert_iterator(HashSetT *hashSet, size_t startIndex, bool overwrite)
const_iterator begin() const
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold...
std::bidirectional_iterator_tag iterator_category
std::iterator_traits< T >::pointer pointer
template_iterator< T > operator++(int)
Buffer is maintained (normal stl behavior).
template_iterator< HashSetImplementation< KeyT, HashT, AllocatorT > > iterator
Iterators.
iterator find(const KeyT &key)
Find an element.
#define DOXYGEN_END_INTERNAL_BLOCK()
void purge(T purger)
Purge the hash set.
#define DOXYGEN_START_INTERNAL_BLOCK()
const KeyT & operator[](const KeyT &key) const
size_t findBucket(const T2 &key) const