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()
151 this->goto_next_element();
158 this->goto_next_element();
185 void goto_next_element()
210 , m_overwrite(overwrite)
238 *
this = std::move(other);
257 for (
size_t bucket=0; bucket<_num_buckets; ++bucket) {
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) {
285 return iterator(
this, bucket);
291 while (bucket<_num_buckets && _states[bucket] !=
State::FILLED) {
294 return const_iterator(
this, bucket);
298 {
return iterator(
this, _num_buckets); }
300 const_iterator
end()
const
301 {
return const_iterator(
this, _num_buckets); }
310 return _num_filled==0;
318 if (bucket == (
size_t)-1) {
321 return iterator(
this, bucket);
324 const_iterator
find(
const KeyT& key)
const
327 if (bucket == (
size_t)-1) {
330 return const_iterator(
this, bucket);
353 auto bucket = find_or_allocate(key);
357 return insert_iterator(
this, bucket,
true);
360 new(_keys + bucket) KeyT(key);
362 return insert_iterator(
this, bucket,
false);
374 auto bucket = find_or_allocate(key);
377 _keys[bucket] = std::move(key);
378 return insert_iterator(
this, bucket,
true);
381 new(_keys + bucket) KeyT(std::move(key));
383 return insert_iterator(
this, bucket,
false);
387 template<
class... Args>
390 return insert(KeyT(std::forward<Args>(args)...));
405 auto bucket = find_empty_bucket(key);
407 new(_keys + bucket) KeyT(std::move(key));
418 if (bucket != (
size_t)-1) {
439 for (
size_t bucket=0; bucket<_num_buckets; ++bucket) {
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;
481 _max_probe_length = -1;
483 for (
size_t src_bucket=0; src_bucket<old_num_buckets; src_bucket++) {
485 auto& src = old_keys[src_bucket];
487 auto dst_bucket = find_empty_bucket(src);
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;
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;
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;
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;
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) {
681 if (bucket == (
size_t)-1) {
691 if (bucket != (
size_t)-1) {
Inheritance with partial specialization due to bug with ref_ptr containers.
bool hashKeyEqual(const T1 &key1, const T2 &key2)
const_iterator begin() const
insert_iterator(MyType *hash_set, size_t bucket, bool overwrite)
bool didOverwrite() const
LinearProbingHashSetImplementation< agx::ref_ptr< KeyT >, HashT > Implementation
LinearProbingHashSetImplementation< KeyT, HashT > Implementation
std::forward_iterator_tag iterator_category
const_iterator end() const
void insert(const_iterator begin, const_iterator end)
~LinearProbingHashSetImplementation()
void erase_bucket(size_t bucket)
const_iterator operator++(int)
LinearProbingHashSetImplementation(const LinearProbingHashSetImplementation &other)
iterator find(const KeyT *key)
iterator erase(iterator it)
void insert_unique(KeyT &&key)
LinearProbingHashSetImplementation & operator=(const LinearProbingHashSetImplementation &other)
iterator find(const KeyT &key)
bool contains(const KeyT &k) const
const_iterator find(const KeyT &key) const
pointer operator->() const
void swap(LinearProbingHashSetImplementation &other)
bool operator!=(const const_iterator &rhs)
pointer operator->() const
void reserve(size_t num_elems)
LinearProbingHashSetImplementation & operator=(LinearProbingHashSetImplementation &&other)
bool erase(const KeyT &key)
size_t find_filled_bucket(const T2 &key) const
reference operator*() const
const_iterator(iterator proto)
insert_iterator emplace(Args &&...args)
reference operator*() const
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
bool erase(const KeyT *key)
const_iterator find(const KeyT *key) const
insert_iterator insert(KeyT &&key)
Implementation::iterator iterator
bool contains(const KeyT *key) const
LinearProbingHashSetImplementation< KeyT, HashT >::const_iterator cbegin(const LinearProbingHashSetImplementation< KeyT, HashT > &set)
LinearProbingHashSetImplementation< KeyT, HashT >::const_iterator cend(const LinearProbingHashSetImplementation< KeyT, HashT > &set)
insert_iterator insert(const KeyT &key)
LinearProbingHashSetImplementation()=default
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Implementation::const_iterator const_iterator
const_iterator & operator++()
bool operator==(const iterator &rhs)
size_t count(const KeyT &k) const
const_iterator(const MyType *hash_set, size_t bucket)
LinearProbingHashSetImplementation(LinearProbingHashSetImplementation &&other)
bool operator!=(const iterator &rhs)
iterator(MyType *hash_set, size_t bucket)
bool operator==(const const_iterator &rhs)
A cache-friendly hash set with open addressing, linear probing and power-of-two capacity.
std::forward_iterator_tag iterator_category