42 template <
typename KeyT,
typename HashT = agx::HashFn<KeyT>>
47 enum class State : uint8_t
80 this->goto_next_element();
87 this->goto_next_element();
114 void goto_next_element()
119 }
while (_bucket < _set->_num_buckets &&
_set->_states[
_bucket] != State::FILLED);
151 this->goto_next_element();
158 this->goto_next_element();
185 void goto_next_element()
190 }
while (_bucket < _set->_num_buckets &&
_set->_states[
_bucket] != State::FILLED);
210 , m_overwrite(overwrite)
238 *
this = std::move(other);
257 for (
size_t bucket=0; bucket<_num_buckets; ++bucket) {
258 if (_states[bucket] == State::FILLED) {
259 _keys[bucket].~KeyT();
268 std::swap(_hasher, other._hasher);
269 std::swap(_states, other._states);
270 std::swap(_keys, other._keys);
271 std::swap(_num_buckets, other._num_buckets);
272 std::swap(_num_filled, other._num_filled);
273 std::swap(_max_probe_length, other._max_probe_length);
274 std::swap(_mask, other._mask);
282 while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
291 while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
298 {
return iterator(
this, _num_buckets); }
310 return _num_filled==0;
318 if (bucket == (
size_t)-1) {
327 if (bucket == (
size_t)-1) {
353 auto bucket = find_or_allocate(key);
355 if (_states[bucket] == State::FILLED) {
359 _states[bucket] = State::FILLED;
360 new(_keys + bucket) KeyT(key);
374 auto bucket = find_or_allocate(key);
376 if (_states[bucket] == State::FILLED) {
377 _keys[bucket] = std::move(key);
380 _states[bucket] = State::FILLED;
381 new(_keys + bucket) KeyT(std::move(key));
387 template<
class... Args>
390 return insert(KeyT(std::forward<Args>(args)...));
405 auto bucket = find_empty_bucket(key);
406 _states[bucket] = State::FILLED;
407 new(_keys + bucket) KeyT(std::move(key));
418 if (bucket != (
size_t)-1) {
439 for (
size_t bucket=0; bucket<_num_buckets; ++bucket) {
440 if (_states[bucket] == State::FILLED) {
441 _states[bucket] = State::INACTIVE;
442 _keys[bucket].~KeyT();
446 _max_probe_length = -1;
452 size_t required_buckets = num_elems + num_elems/2 + 1;
453 if (required_buckets <= _num_buckets) {
456 size_t num_buckets = 4;
457 while (num_buckets < required_buckets) { num_buckets *= 2; }
459 auto new_states = (State*)malloc(num_buckets *
sizeof(State));
460 auto new_keys = (KeyT*)malloc(num_buckets *
sizeof(KeyT));
462 if (!new_states || !new_keys) {
465 throw std::bad_alloc();
469 auto old_num_buckets = _num_buckets;
470 auto old_states = _states;
471 auto old_keys = _keys;
474 _num_buckets = num_buckets;
475 _mask = _num_buckets - 1;
476 _states = new_states;
479 std::fill_n(_states, num_buckets, State::INACTIVE);
481 _max_probe_length = -1;
483 for (
size_t src_bucket=0; src_bucket<old_num_buckets; src_bucket++) {
484 if (old_states[src_bucket] == State::FILLED) {
485 auto& src = old_keys[src_bucket];
487 auto dst_bucket = find_empty_bucket(src);
489 agxAssert(_states[dst_bucket] != State::FILLED);
490 _states[dst_bucket] = State::FILLED;
491 new(_keys + dst_bucket) KeyT(std::move(src));
506 template<
typename T2>
509 if (
empty()) {
return (
size_t)-1; }
511 auto hash_value = _hasher(key);
512 for (
int offset=0; offset<=_max_probe_length; ++offset) {
513 auto bucket = (hash_value + offset) & _mask;
517 if (_states[bucket] == State::INACTIVE) {
525 _states[bucket] = State::ACTIVE;
526 _keys[bucket].~KeyT();
532 void check_expand_need()
539 size_t find_or_allocate(
const KeyT& key)
541 auto hash_value = _hasher(key);
542 size_t hole = (size_t)-1;
544 for (; offset<=_max_probe_length; ++offset) {
545 auto bucket = (hash_value + offset) & _mask;
547 if (_states[bucket] == State::FILLED) {
551 }
else if (_states[bucket] == State::INACTIVE) {
555 if (hole == (
size_t)-1) {
563 agxAssert(offset == _max_probe_length+1);
565 if (hole != (
size_t)-1) {
571 auto bucket = (hash_value + offset) & _mask;
573 if (_states[bucket] != State::FILLED) {
574 _max_probe_length = offset;
581 size_t find_empty_bucket(
const KeyT& key)
583 auto hash_value = _hasher(key);
584 for (
int offset=0; ; ++offset) {
585 auto bucket = (hash_value + offset) & _mask;
586 if (_states[bucket] != State::FILLED) {
587 if (offset > _max_probe_length) {
588 _max_probe_length = offset;
597 State* _states =
nullptr;
598 KeyT* _keys =
nullptr;
599 size_t _num_buckets = 0;
600 size_t _num_filled = 0;
601 int _max_probe_length = -1;
605 template <
typename KeyT,
typename HashT>
611 template <
typename KeyT,
typename HashT>
618 template <
typename KeyT,
typename HashT>
624 template <
typename KeyT,
typename HashT>
638 template <
typename KeyT,
typename HashT = agx::HashFn<KeyT>>
651 template <
typename KeyT,
typename HashT>
656 typedef typename Implementation::iterator
iterator;
672 if (bucket == (
size_t)-1) {
675 return iterator(
this, bucket);
681 if (bucket == (
size_t)-1) {
684 return const_iterator(
this, bucket);
691 if (bucket != (
size_t)-1) {
const_iterator operator++(int)
pointer operator->() const
const_iterator(iterator proto)
const_iterator & operator++()
bool operator!=(const const_iterator &rhs)
bool operator==(const const_iterator &rhs)
std::forward_iterator_tag iterator_category
reference operator*() const
const_iterator(const MyType *hash_set, size_t bucket)
insert_iterator(MyType *hash_set, size_t bucket, bool overwrite)
bool didOverwrite() const
bool operator==(const iterator &rhs)
bool operator!=(const iterator &rhs)
pointer operator->() const
std::forward_iterator_tag iterator_category
iterator(MyType *hash_set, size_t bucket)
reference operator*() const
A cache-friendly hash set with open addressing, linear probing and power-of-two capacity.
size_t count(const KeyT &k) const
size_t find_filled_bucket(const T2 &key) const
void erase_bucket(size_t bucket)
const_iterator end() const
~LinearProbingHashSetImplementation()
void swap(LinearProbingHashSetImplementation &other)
iterator erase(iterator it)
LinearProbingHashSetImplementation(LinearProbingHashSetImplementation &&other)
LinearProbingHashSetImplementation()=default
bool erase(const KeyT &key)
const_iterator find(const KeyT &key) const
const_iterator begin() const
LinearProbingHashSetImplementation & operator=(LinearProbingHashSetImplementation &&other)
void insert_unique(KeyT &&key)
insert_iterator insert(const KeyT &key)
void insert(const_iterator begin, const_iterator end)
LinearProbingHashSetImplementation(const LinearProbingHashSetImplementation &other)
iterator find(const KeyT &key)
insert_iterator insert(KeyT &&key)
LinearProbingHashSetImplementation & operator=(const LinearProbingHashSetImplementation &other)
insert_iterator emplace(Args &&... args)
void reserve(size_t num_elems)
bool contains(const KeyT &k) const
const KeyT & const_reference
Implementation::const_iterator const_iterator
Implementation::iterator iterator
const_iterator find(const KeyT *key) const
iterator find(const KeyT *key)
bool contains(const KeyT *key) const
LinearProbingHashSetImplementation< agx::ref_ptr< KeyT >, HashT > Implementation
bool erase(const KeyT *key)
Inheritance with partial specialization due to bug with ref_ptr containers.
LinearProbingHashSetImplementation< KeyT, HashT > Implementation
Smart pointer for handling referenced counted objects.
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
LinearProbingHashSetImplementation< KeyT, HashT >::const_iterator cend(const LinearProbingHashSetImplementation< KeyT, HashT > &set)
bool hashKeyEqual(const T1 &key1, const T2 &key2)
LinearProbingHashSetImplementation< KeyT, HashT >::const_iterator cbegin(const LinearProbingHashSetImplementation< KeyT, HashT > &set)