29 #ifndef AGX_LINEAR_PROBING_HASH_TABLE_H
30 #define AGX_LINEAR_PROBING_HASH_TABLE_H
46 template <
typename KeyT,
typename ValueT,
typename HashT = agx::HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
51 using PairT = std::pair<KeyT, ValueT>;
52 enum class State : uint8_t
100 this->goto_next_element();
107 this->goto_next_element();
115 this->goto_previous_element();
124 this->goto_previous_element();
151 void goto_next_element()
156 }
while (_bucket < _map->_num_buckets &&
_map->_states[
_bucket] != State::FILLED);
161 void goto_previous_element()
199 this->goto_next_element();
206 this->goto_next_element();
214 this->goto_previous_element();
223 this->goto_previous_element();
250 void goto_next_element()
255 }
while (_bucket < _map->_num_buckets &&
_map->_states[
_bucket] != State::FILLED);
260 void goto_previous_element()
286 iterator(hash_map, bucket), m_overwrite(overwrite)
314 *
this = std::move(other);
333 for (
size_t bucket=0; bucket<_num_buckets; ++bucket) {
334 if (_states[bucket] == State::FILLED) {
335 _pairs[bucket].~PairT();
339 _allocator.deallocateBytes(_states, _num_buckets *
sizeof(State));
340 _allocator.deallocateBytes(_pairs, _num_buckets *
sizeof(PairT));
345 std::swap(_hasher, other._hasher);
346 std::swap(_allocator, other._allocator);
347 std::swap(_states, other._states);
348 std::swap(_pairs, other._pairs);
349 std::swap(_num_buckets, other._num_buckets);
350 std::swap(_num_filled, other._num_filled);
351 std::swap(_max_probe_length, other._max_probe_length);
352 std::swap(_mask, other._mask);
363 while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
375 while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
386 {
return iterator(
this, _num_buckets); }
416 return _num_filled==0;
429 if (bucket == (
size_t)-1) {
443 if (bucket == (
size_t)-1)
479 if (bucket != (
size_t)-1) {
480 return &_pairs[bucket].second;
494 if (bucket != (
size_t)-1) {
495 return &_pairs[bucket].second;
504 const ValueT* ret =
try_get(k);
525 auto bucket = find_or_allocate(key);
527 if (_states[bucket] == State::FILLED) {
528 (_pairs + bucket)->second = value;
531 _states[bucket] = State::FILLED;
532 new(_pairs + bucket) PairT(key, value);
534 didOverwrite =
false;
542 return insert(p.first, p.second);
570 auto bucket = find_empty_bucket(key);
571 _states[bucket] = State::FILLED;
572 new(_pairs + bucket) PairT(std::move(key), std::move(value));
582 ValueT
set_get(
const KeyT& key,
const ValueT& new_value)
586 auto bucket = find_or_allocate(key);
589 if (_states[bucket] == State::FILLED) {
590 ValueT old_value = _pairs[bucket].second;
591 _pairs[bucket] = new_value.second;
594 _states[bucket] = State::FILLED;
595 new(_pairs + bucket) PairT(key, new_value);
612 auto bucket = find_or_allocate(key);
615 if (_states[bucket] != State::FILLED) {
616 _states[bucket] = State::FILLED;
617 new(_pairs + bucket) PairT(key, ValueT());
621 return _pairs[bucket].second;
633 if (bucket != (
size_t)-1) {
658 for (
size_t bucket=0; bucket<_num_buckets; ++bucket) {
659 if (_states[bucket] == State::FILLED) {
660 _states[bucket] = State::INACTIVE;
661 _pairs[bucket].~PairT();
665 _max_probe_length = -1;
673 size_t required_buckets = num_elems + num_elems/2 + 1;
674 if (required_buckets <= _num_buckets) {
677 size_t num_buckets = 4;
678 while (num_buckets < required_buckets) { num_buckets *= 2; }
680 auto new_states = (State*)_allocator.allocateBytes(num_buckets *
sizeof(State));
681 auto new_pairs = (PairT*)_allocator.allocateBytes(num_buckets *
sizeof(PairT));
683 if (!new_states || !new_pairs) {
684 _allocator.deallocateBytes(new_states, num_buckets *
sizeof(State));
685 _allocator.deallocateBytes(new_pairs, num_buckets *
sizeof(PairT));
686 throw std::bad_alloc();
690 auto old_num_buckets = _num_buckets;
691 auto old_states = _states;
692 auto old_pairs = _pairs;
695 _num_buckets = num_buckets;
696 _mask = _num_buckets - 1;
697 _states = new_states;
700 std::fill_n(_states, num_buckets, State::INACTIVE);
702 _max_probe_length = -1;
704 for (
size_t src_bucket=0; src_bucket<old_num_buckets; src_bucket++) {
705 if (old_states[src_bucket] == State::FILLED) {
706 auto& src_pair = old_pairs[src_bucket];
708 auto dst_bucket = find_empty_bucket(src_pair.first);
710 agxAssert(_states[dst_bucket] != State::FILLED);
711 _states[dst_bucket] = State::FILLED;
712 new(_pairs + dst_bucket) PairT(std::move(src_pair));
721 _allocator.deallocateBytes(old_states, old_num_buckets *
sizeof(State));
722 _allocator.deallocateBytes(old_pairs, old_num_buckets *
sizeof(PairT));
736 template<
typename T2>
739 if (
empty()) {
return (
size_t)-1; }
741 auto hash_value = _hasher(key);
742 for (
int offset=0; offset<=_max_probe_length; ++offset) {
743 auto bucket = (hash_value + offset) & _mask;
744 if (_states[bucket] == State::FILLED &&
agx::hashKeyEqual(_pairs[bucket].first, key)) {
747 if (_states[bucket] == State::INACTIVE) {
755 _states[bucket] = State::ACTIVE;
756 _pairs[bucket].~PairT();
763 void check_expand_need()
769 size_t find_or_allocate(
const KeyT& key)
771 auto hash_value = _hasher(key);
772 size_t hole = (size_t)-1;
774 for (; offset<=_max_probe_length; ++offset) {
775 auto bucket = (hash_value + offset) & _mask;
777 if (_states[bucket] == State::FILLED) {
781 }
else if (_states[bucket] == State::INACTIVE) {
785 if (hole == (
size_t)-1) {
793 agxAssert(offset == _max_probe_length+1);
795 if (hole != (
size_t)-1) {
801 auto bucket = (hash_value + offset) & _mask;
803 if (_states[bucket] != State::FILLED) {
804 _max_probe_length = offset;
811 size_t find_empty_bucket(
const KeyT& key)
813 auto hash_value = _hasher(key);
814 for (
int offset=0; ; ++offset) {
815 auto bucket = (hash_value + offset) & _mask;
816 if (_states[bucket] != State::FILLED) {
817 if (offset > _max_probe_length) {
818 _max_probe_length = offset;
827 AllocatorT _allocator = AllocatorT();
828 State* _states =
nullptr;
829 PairT* _pairs =
nullptr;
830 size_t _num_buckets = 0;
831 size_t _num_filled = 0;
832 int _max_probe_length = -1;
837 template <
typename KeyT,
typename ValueT,
typename HashT,
typename AllocatorT>
840 return table.
begin();
843 template <
typename KeyT,
typename ValueT,
typename HashT,
typename AllocatorT>
850 template <
typename KeyT,
typename ValueT,
typename HashT,
typename AllocatorT>
853 return table.
begin();
856 template <
typename KeyT,
typename ValueT,
typename HashT,
typename AllocatorT>
870 template <
typename KeyT,
typename DataT,
typename HashT = agx::HashFn<KeyT>,
typename AllocatorT = ByteAllocator>
883 template <
typename KeyT,
typename DataT,
typename HashT,
typename AllocatorT>
888 typedef typename Implementation::iterator
iterator;
904 if (bucket == (
size_t)-1) {
907 return iterator(
this, bucket);
913 if (bucket == (
size_t)-1) {
916 return const_iterator(
this, bucket);
923 if (bucket != (
size_t)-1) {
const_iterator operator--(int)
Does not do any bounds checking, so only call this if there really is a previous element.
const_iterator(const MyType *hash_map, size_t bucket)
pointer operator->() const
reference operator*() const
bool operator!=(const const_iterator &rhs) const
const_iterator operator++(int)
bool operator==(const const_iterator &rhs) const
std::pair< KeyT, ValueT > bucket_type
const_iterator operator--()
Does not do any bounds checking, so only call this if there really is a previous element.
std::forward_iterator_tag iterator_category
const_iterator(iterator proto)
const_iterator & operator++()
insert_iterator(MyType *hash_map, size_t bucket, bool overwrite)
bool didOverwrite() const
bool operator!=(const iterator &rhs) const
pointer operator->() const
reference operator*() const
iterator operator--(int)
Does not do any bounds checking, so only call this if there really is a previous element.
bool operator==(const iterator &rhs) const
iterator operator--()
Does not do any bounds checking, so only call this if there really is a previous element.
std::forward_iterator_tag iterator_category
iterator(MyType *hash_map, size_t bucket)
std::pair< KeyT, ValueT > bucket_type
A cache-friendly hash table with open addressing, linear probing and power-of-two capacity.
iterator begin()
Iterator to first element in hash table.
bool contains(const KeyT &k) const
Check if the hash table contains a key/value pair for the given key.
void insert(const_iterator begin, const_iterator end)
Insert all elements in the given range.
iterator erase(iterator it)
Erase an element from the hash table.
const_iterator end() const
Iterator marking end of hash table.
iterator end()
Iterator marking end of hash table.
@ SHRINK_BUFFER_AVERAGED
Buffer is shrunk if a smoothing average (which is updated each clear call) goes below a threshold.
@ MAINTAIN_BUFFER
Buffer is maintained (normal stl behavior).
@ SHRINK_BUFFER
Buffer is deallocated and replaced by an newly allocated empty buffer.
bool erase(const KeyT &key)
Erase an element from the hash table.
LinearProbingHashTableImplementation & operator=(const LinearProbingHashTableImplementation &other)
const PairT & const_reference
const ValueT * try_get(const KeyT &k) const
Find the value associated with the given key.
ValueT & operator[](const KeyT &key)
Return the value associated with the given key.
void insert_unique(KeyT &&key, ValueT &&value)
Insert a key/value pair into the hash table assuming that the given key does not already exist in the...
void insert_unique(std::pair< KeyT, ValueT > &&p)
ValueT set_get(const KeyT &key, const ValueT &new_value)
void erase_bucket(size_t bucket)
const_iterator find(const KeyT &key) const
Find a key/value pair in the hash table given a key.
const ValueT get_or_return_default(const KeyT &k) const
~LinearProbingHashTableImplementation()
void reserve(size_t num_elems)
Make room for this many elements in the hash table.
LinearProbingHashTableImplementation(LinearProbingHashTableImplementation &&other)
void swap(LinearProbingHashTableImplementation &other)
insert_iterator insert(const KeyT &key, const ValueT value)
Insert a key/value pair into the hash table.
iterator find(const KeyT &key)
Find a key/value pair in the hash table given a key.
void clear(int=0)
Remove all elements, keeping full capacity.
ValueT * try_get(const KeyT &k)
Find the value associated with the given key.
LinearProbingHashTableImplementation & operator=(LinearProbingHashTableImplementation &&other)
insert_iterator insert(const std::pair< KeyT, ValueT > &p)
LinearProbingHashTableImplementation()=default
size_t find_filled_bucket(const T2 &key) const
const_iterator begin() const
Iterator to first element in hash table.
size_t count(const KeyT &k) const
Count the number of key/value pairs matching the given key.
LinearProbingHashTableImplementation(const LinearProbingHashTableImplementation &other)
bool contains(const KeyT *key) const
const_iterator find(const KeyT *key) const
Implementation::iterator iterator
bool erase(const KeyT *key)
Implementation::const_iterator const_iterator
iterator find(const KeyT *key)
LinearProbingHashTableImplementation< agx::ref_ptr< KeyT >, DataT, HashT, AllocatorT > Implementation
Inheritance with partial specialization due to bug with ref_ptr containers.
LinearProbingHashTableImplementation< KeyT, DataT, HashT, AllocatorT > 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)