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 m_allocator = other.m_allocator;
221 m_allocator.setContainer(
this);
248 UInt32 hashValue = m_hashFunctor(key);
264 if (!bucket->isActive() || (bucket->isValid() &&
hashKeyEqual(bucket->key, key)))
266 targetBucket = bucket;
271 if (!bucket->isValid() && !targetBucket)
272 targetBucket = bucket;
275 if (targetBucket && offset >= m_maxProbeLength)
291 if (offset > m_maxProbeLength)
292 m_maxProbeLength = (
UInt16)offset;
294 targetBucket->key = key;
297 bool overwrite = targetBucket->isValid();
304 return insert_iterator(
this, targetBucket -
buckets(), overwrite);
389 return iterator(
this, it.m_currentIndex);
413 m_smoothingAverage = 0.0;
445 m_maxProbeLength = 0;
472 if (bucket.isValid() && purger(bucket.key))
485 const AllocatorT&
allocator()
const {
return m_allocator; }
511 {
return state >= ACTIVE; }
514 {
return state == VALID; }
541 const size_t desiredNumBytes = desiredNumBuckets *
sizeof(
Bucket);
543 m_buffer = m_allocator.allocateBytes(desiredNumBytes);
544 for (
size_t i = 0; i < desiredNumBuckets; ++i)
549 m_maxProbeLength = 0;
551 for (
size_t i = 0; i < oldNumBuckets; i++)
553 Bucket& bucket = oldBuckets[i];
555 if (bucket.isValid())
559 for (
size_t i = 0; i < oldNumBuckets; ++i)
562 m_allocator.deallocateBytes((
void *)oldBuckets, oldNumBuckets *
sizeof(
Bucket));
566 template <
typename T2>
572 UInt32 hashValue = m_hashFunctor(key);
577 if (bucket->isValid() &&
hashKeyEqual(bucket->key, key))
581 for (
size_t offset = 1; bucket->isActive() && offset <= m_maxProbeLength; offset++)
583 index = (hashValue + offset * offset) %
numBuckets();
586 if (bucket->isValid() &&
hashKeyEqual(bucket->key, key))
600 if (!bucket->isValid())
619 Real32 m_smoothingAverage;
621 AllocatorT m_allocator;
664 template <
typename KeyT,
typename HashT = HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
674 template <
typename KeyT,
typename HashT,
typename AllocatorT>
713 # 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.
size_t numBuckets() const
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.
bool contains(const KeyT &key) const
Boolean query to test existence of an element.
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.
bool didOverwrite() const
iterator::HashSetT HashSetT
std::iterator_traits< T >::value_type value_type
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 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)
bool contains(const KeyT *key) const
#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.
const KeyT & operator[](const KeyT &key) const
const AllocatorT & allocator() const
std::iterator_traits< T >::iterator_category iterator_category
template_iterator< T > operator--(int)
#define AGX_HASH_SET_SHRINK_FACTOR
std::iterator_traits< T >::difference_type difference_type
Implementation::iterator iterator
insert_iterator insert(const KeyT &key)
Insert an element, overwriting if key already exists.
#define AGX_HASH_SET_SMOOTHING_FACTOR
const_iterator find(const KeyT &key) const
#define AGX_HASH_SET_MAX_PROBE_LENGTH
Smart pointer for handling referenced counted objects.
HashSetImplementation< KeyT, HashT, AllocatorT > & operator=(const HashSetImplementation< KeyT, HashT, AllocatorT > &other)
Assignment operator.
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())
const_iterator end() const
HashSetImplementation(const AllocatorT &allocator=AllocatorT())
Constructor.
std::iterator_traits< T >::reference reference
AGXCORE_EXPORT int nextPrime(int n)
template_iterator< const HashSetImplementation< KeyT, HashT, AllocatorT > > const_iterator
const_iterator find(const KeyT *key) 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)
size_t findBucket(const T2 &key) const
insert_iterator(HashSetT *hashSet, size_t startIndex, bool overwrite)
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold...
std::bidirectional_iterator_tag iterator_category
KeyT * operator->() const
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_iterator begin() const