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) {
366 return iterator(
this, bucket);
375 while (bucket<_num_buckets && _states[bucket] != State::FILLED) {
378 return const_iterator(
this, bucket);
386 {
return iterator(
this, _num_buckets); }
392 const_iterator
end()
const
393 {
return const_iterator(
this, _num_buckets); }
416 return _num_filled==0;
429 if (bucket == (
size_t)-1) {
432 return iterator(
this, bucket);
440 const_iterator
find(
const KeyT& key)
const
443 if (bucket == (
size_t)-1)
447 return const_iterator(
this, bucket);
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);
521 insert_iterator
insert(
const KeyT& key,
const ValueT value)
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;
537 return insert_iterator(
this, bucket, didOverwrite);
540 insert_iterator
insert(
const std::pair<KeyT, ValueT>& p)
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) {