17#ifndef AGX_QUADRATIC_PROBING_HASH_TABLE_H
18#define AGX_QUADRATIC_PROBING_HASH_TABLE_H
33# pragma warning(disable: 6011)
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
105 {
return state >=
ACTIVE; }
110 return state ==
VALID;
123 {
return (state &
VALID) != 0; }
127 friend class QuadraticProbingHashTableImplementation<KeyT, DataT, HashT, AllocatorT>;
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())
223 size_t m_currentIndex;
260 { m_allocator.setContainer(
this); }
271 m_allocator = other.m_allocator;
272 m_allocator.setContainer(
this);
278 this->
insert(it->first, it->second);
323 return (*
this)[ key ];
332 UInt32 hashValue = m_hashFunctor(key);
342 bool alwaysTrue =
true;
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();
452 return iterator(
this, it.m_currentIndex);
480 m_smoothingAverage = 0.0;
506 m_allocator.deallocateBytes(oldBuffer, oldCapacity *
sizeof(
Bucket));
521 m_maxProbeLength = 0;
552 bucket.
first.~KeyT();
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];
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);
646 for (
size_t offset = 1; bucket->
isActive() && offset <= m_maxProbeLength; offset++)
648 index = (hashValue + offset * offset) %
numBuckets();
671 bucket->
first.~KeyT();
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>
#define AGX_HASH_TABLE_GROW_FACTOR
#define AGX_HASH_TABLE_GROW_THRESHOLD
#define AGX_HASH_TABLE_MAX_PROBE_LENGTH
#define AGX_HASH_TABLE_SHRINK_THRESHOLD
#define AGX_HASH_TABLE_MIN_SIZE
#define AGX_HASH_TABLE_SHRINK_FACTOR
#define AGX_HASH_TABLE_SMOOTHING_FACTOR
The Container is the base class for several of the container classes proided by AGX,...
ClearPolicy
agxData::Values from this enumeration is passed to the subclasses' 'clear' method in order to control...
@ SHRINK_BUFFER
Buffer is deallocated and replaced by an newly allocated empty buffer.
@ MAINTAIN_BUFFER
Buffer is maintained (normal stl behavior).
@ SHRINK_BUFFER_AVERAGED
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold.
KeyT first
We used to wrap key,data in a std::pair.
agx::UInt8 getState() const
Bucket & operator=(const Bucket &b)
iterator::QuadraticProbingHashTableT QuadraticProbingHashTableT
bool didOverwrite() const
insert_iterator(QuadraticProbingHashTableT *hashTable, size_t startIndex, bool overwrite)
bool operator!=(const template_iterator< T > &rhs)
template_iterator< T > & operator++()
T QuadraticProbingHashTableT
template_iterator< T > operator++(int)
template_iterator< T > & operator--()
template_iterator()
Constructors.
bool operator==(const template_iterator< T > &rhs)
Bucket & operator*() const
template_iterator< T > operator--(int)
template_iterator(T *hashTable, size_t startIndex=0)
template_iterator< T > & operator=(const template_iterator< T > ©)
Bucket * operator->() const
A simple hash table using quadratic probing.
const AllocatorT & allocator() const
size_t getMaxProbeLength() const
iterator begin()
Iterator to first element in hash table.
const_iterator end() const
bool erase(const KeyT &key)
Erase an element from the hash table.
size_t getNumBuckets() const
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements.
~QuadraticProbingHashTableImplementation()
Destructor.
iterator erase(const iterator &it)
Erase an element using an iterator.
DataT & getOrCreate(const KeyT &key)
Returns value given key.
bool contains(const KeyT &key) const
Boolean query to test existence of an element.
iterator find(const KeyT &key)
Find an element.
DataT & operator[](const KeyT &key)
Index operator for accessing elements in the hash, inserting it if not found.
const_iterator find(const KeyT &key) const
QuadraticProbingHashTableImplementation(const AllocatorT &allocator=AllocatorT())
Constructor.
insert_iterator insert(const KeyT &key, const DataT &data)
Insert an element, overwriting if key already exists.
template_iterator< const QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > > const_iterator
const DataT & operator[](const KeyT &key) const
QuadraticProbingHashTableImplementation(const QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > &other)
Copy constructor.
void reserve(size_t size)
Reserve capacity in the hash table.
bool eraseBucket(size_t index)
iterator end()
Iterator marking end of hash table.
size_t numBuckets() const
size_t findBucket(const T2 &key) const
const Bucket * getBucketBuffer() const
const_iterator begin() const
friend class HashTableComponent
void purge(T purger)
Purge the hash table.
template_iterator< QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > > iterator
Iterators.
void rebuild(size_t size)
Rebuild, and optionally resize hash table.
QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > & operator=(const QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > &other)
Assignment operator.
QuadraticProbingHashTableImplementation< agx::ref_ptr< KeyT >, DataT, HashT, AllocatorT > Implementation
bool contains(const KeyT *key) const
bool erase(const KeyT *key)
const_iterator find(const KeyT *key) const
Implementation::iterator iterator
iterator find(const KeyT *key)
Implementation::const_iterator const_iterator
QuadraticProbingHashTable(const AllocatorT &allocator=AllocatorT())
Inheritance with partial specialization due to bug with ref_ptr containers.
QuadraticProbingHashTable(const AllocatorT &allocator=AllocatorT())
QuadraticProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > Implementation
Smart pointer for handling referenced counted objects.
#define agxAssert1(expr, msg)
#define DOXYGEN_END_INTERNAL_BLOCK()
#define DOXYGEN_START_INTERNAL_BLOCK()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
AGXCORE_EXPORT int nextPrime(int n)
bool hashKeyEqual(const T1 &key1, const T2 &key2)