17 #ifndef AGX_QUADRATIC_PROBING_HASH_TABLE_H 18 #define AGX_QUADRATIC_PROBING_HASH_TABLE_H 32 # pragma warning(push) 33 # pragma warning(disable: 6011) // Disable warningC6011: dereferencing nullptr pointer 46 template <
typename KeyT,
typename DataT,
typename HashT = HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
55 #define AGX_HASH_TABLE_MIN_SIZE 4 57 #define AGX_HASH_TABLE_GROW_THRESHOLD 0.8 58 #define AGX_HASH_TABLE_SHRINK_THRESHOLD 0.25 59 #define AGX_HASH_TABLE_GROW_FACTOR 2.0 60 #define AGX_HASH_TABLE_SHRINK_FACTOR 0.5 61 #define AGX_HASH_TABLE_SMOOTHING_FACTOR 0.8f 62 #define AGX_HASH_TABLE_MAX_PROBE_LENGTH 10 86 AGX_FORCE_INLINE operator std::pair<KeyT, DataT>() {
return std::make_pair(first, second); }
105 {
return state >=
ACTIVE; }
110 return state ==
VALID;
123 {
return (state &
VALID) != 0; }
144 { this->gotoNextElement(); }
150 constIt.m_hashTable = m_hashTable;
151 constIt.m_currentIndex = m_currentIndex;
157 this->gotoNextElement();
163 size_t oldIndex = m_currentIndex;
164 this->gotoNextElement();
171 this->gotoPreviousElement();
177 size_t oldIndex = m_currentIndex;
178 this->gotoPreviousElement();
184 {
return this->m_currentIndex == rhs.m_currentIndex && this->m_hashTable == rhs.m_hashTable; }
187 {
return this->m_currentIndex != rhs.m_currentIndex || this->m_hashTable != rhs.m_hashTable; }
191 agxAssert1(m_hashTable && m_currentIndex != m_hashTable->numBuckets(),
"Can not dereference an invalid bucket!");
192 return m_hashTable->buckets()[m_currentIndex];
201 m_hashTable = copy.m_hashTable;
202 m_currentIndex = copy.m_currentIndex;
210 while (m_currentIndex < m_hashTable->
numBuckets() && !m_hashTable->buckets()[m_currentIndex].isValid())
217 while (!m_hashTable->buckets()[m_currentIndex].isValid())
222 QuadraticProbingHashTableT* m_hashTable;
223 size_t m_currentIndex;
227 typedef template_iterator< QuadraticProbingHashTableImplementation<KeyT, DataT, HashT, AllocatorT> >
iterator;
228 typedef template_iterator< const QuadraticProbingHashTableImplementation<KeyT, DataT, HashT, AllocatorT> >
const_iterator;
260 { m_allocator.setContainer(
this); }
271 m_allocator = other.m_allocator;
272 m_allocator.setContainer(
this);
277 for (const_iterator it = other.
begin(); it != other.
end(); ++it)
278 this->
insert(it->first, it->second);
301 iterator it =
find( key );
310 const_iterator it =
find( key );
323 return (*
this)[ key ];
330 insert_iterator
insert(
const KeyT& key,
const DataT& data)
332 UInt32 hashValue = m_hashFunctor(key);
342 bool alwaysTrue =
true;
348 if (!bucket->isActive() || (bucket->isValid() &&
hashKeyEqual(bucket->first, key)))
350 targetBucket = bucket;
355 if (!bucket->isValid() && !targetBucket)
356 targetBucket = bucket;
359 if (targetBucket && offset >= m_maxProbeLength)
369 return this->
insert(key, data);
371 }
while (alwaysTrue);
375 if (offset > m_maxProbeLength)
376 m_maxProbeLength = (
UInt16)offset;
379 targetBucket->first = key;
380 targetBucket->second = data;
384 bool overwrite = targetBucket->isValid();
391 return insert_iterator(
this, targetBucket -
buckets(), overwrite);
452 return iterator(
this, it.m_currentIndex);
480 m_smoothingAverage = 0.0;
506 m_allocator.deallocateBytes(oldBuffer, oldCapacity *
sizeof(
Bucket));
521 m_maxProbeLength = 0;
549 if (bucket.isValid() && purger(bucket.first, bucket.second))
552 bucket.
first.~KeyT();
553 bucket.second.~DataT();
566 const AllocatorT&
allocator()
const {
return m_allocator; }
579 template <
typename T1,
typename T2,
typename T3>
605 const size_t desiredNumBytes = desiredNumBuckets *
sizeof(
Bucket);
607 m_buffer = m_allocator.allocateBytes(desiredNumBytes);
608 for (
size_t i = 0; i < desiredNumBuckets; ++i)
614 m_maxProbeLength = 0;
616 for (
size_t i = 0; i < oldNumBuckets; i++)
618 Bucket& bucket = oldBuckets[i];
620 if (bucket.isValid())
621 this->
insert(bucket.first, bucket.second);
624 for (
size_t i = 0; i < oldNumBuckets; ++i)
627 m_allocator.deallocateBytes((
void *)oldBuckets, oldNumBuckets *
sizeof(
Bucket));
631 template <
typename T2>
637 UInt32 hashValue = m_hashFunctor(key);
642 if (bucket->isValid() &&
hashKeyEqual(bucket->first, key))
646 for (
size_t offset = 1; bucket->isActive() && offset <= m_maxProbeLength; offset++)
648 index = (hashValue + offset * offset) %
numBuckets();
651 if (bucket->isValid() &&
hashKeyEqual(bucket->first, key))
665 if (!bucket->isValid())
671 bucket->first.~KeyT();
672 bucket->second.~DataT();
682 Real32 m_smoothingAverage;
684 AllocatorT m_allocator;
697 template <
typename KeyT,
typename DataT,
typename HashT = HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
708 template <
typename KeyT,
typename DataT,
typename HashT,
typename AllocatorT>
719 using Implementation::contains;
725 using Implementation::find;
736 using Implementation::erase;
747 # pragma warning(pop)
#define AGX_HASH_TABLE_MAX_PROBE_LENGTH
bool hashKeyEqual(const T1 &key1, const T2 &key2)
bool contains(const KeyT *key) const
template_iterator()
Constructors.
QuadraticProbingHashTable(const AllocatorT &allocator=AllocatorT())
const Bucket * getBucketBuffer() const
Bucket & operator=(const Bucket &b)
The Container is the base class for several of the container classes proided by AGX, such as agx::Vector and agx::HashSet.
A simple hash table using quadratic probing.
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Bucket & operator*() const
#define AGX_HASH_TABLE_SHRINK_THRESHOLD
size_t findBucket(const T2 &key) const
Implementation::const_iterator const_iterator
QuadraticProbingHashTableImplementation< agx::ref_ptr< KeyT >, DataT, HashT, AllocatorT > Implementation
const DataT & operator[](const KeyT &key) const
bool contains(const KeyT &key) const
Boolean query to test existence of an element.
#define AGX_HASH_TABLE_GROW_FACTOR
insert_iterator(QuadraticProbingHashTableT *hashTable, size_t startIndex, bool overwrite)
Inheritance with partial specialization due to bug with ref_ptr containers.
template_iterator< const QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > > const_iterator
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
QuadraticProbingHashTable(const AllocatorT &allocator=AllocatorT())
bool operator==(const template_iterator< T > &rhs)
iterator find(const KeyT &key)
Find an element.
template_iterator(T *hashTable, size_t startIndex=0)
friend class HashTableComponent
template_iterator< T > & operator++()
QuadraticProbingHashTableImplementation(const QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > &other)
Copy constructor.
size_t numBuckets() const
template_iterator< T > operator--(int)
Buffer is deallocated and replaced by an newly allocated empty buffer.
bool didOverwrite() const
QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > Implementation
bool eraseBucket(size_t index)
void reserve(size_t size)
Reserve capacity in the hash table.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
iterator find(const KeyT *key)
const AllocatorT & allocator() const
template_iterator< T > & operator--()
template_iterator< QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > > iterator
Iterators.
bool erase(const KeyT &key)
Erase an element from the hash table.
Smart pointer for handling referenced counted objects.
const_iterator find(const KeyT *key) const
const_iterator find(const KeyT &key) const
#define AGX_HASH_TABLE_MIN_SIZE
void purge(T purger)
Purge the hash table.
AGXCORE_EXPORT int nextPrime(int n)
#define AGX_HASH_TABLE_SMOOTHING_FACTOR
#define AGX_HASH_TABLE_SHRINK_FACTOR
agx::UInt8 getState() const
KeyT first
We used to wrap key,data in a std::pair.
Bucket * operator->() const
iterator end()
Iterator marking end of hash table.
iterator::QuadraticProbingHashTableT QuadraticProbingHashTableT
bool operator!=(const template_iterator< T > &rhs)
size_t getNumBuckets() const
const_iterator end() const
#define AGX_HASH_TABLE_GROW_THRESHOLD
QuadraticProbingHashTableImplementation(const AllocatorT &allocator=AllocatorT())
Constructor.
#define agxAssert1(expr, msg)
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold...
DataT & getOrCreate(const KeyT &key)
Returns value given key.
void rebuild(size_t size)
Rebuild, and optionally resize hash table.
iterator erase(const iterator &it)
Erase an element using an iterator.
const_iterator begin() const
insert_iterator insert(const KeyT &key, const DataT &data)
Insert an element, overwriting if key already exists.
bool erase(const KeyT *key)
~QuadraticProbingHashTableImplementation()
Destructor.
Buffer is maintained (normal stl behavior).
template_iterator< T > operator++(int)
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
DataT & operator[](const KeyT &key)
Index operator for accessing elements in the hash, inserting it if not found.
size_t getMaxProbeLength() const
#define DOXYGEN_END_INTERNAL_BLOCK()
iterator begin()
Iterator to first element in hash table.
#define DOXYGEN_START_INTERNAL_BLOCK()
T QuadraticProbingHashTableT
Vec3T< T > operator*(const Vec3T< T > &v, const AffineMatrix4x4T< T > &m)
Implementation::iterator iterator