31 #define _HASHTABLE_H 1 33 #pragma GCC system_header 36 #if __cplusplus > 201402L 40 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 template<
typename _Tp,
typename _Hash>
47 __is_fast_hash<_Hash>,
49 __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
169 template<
typename _Key,
typename _Value,
typename _Alloc,
170 typename _ExtractKey,
typename _Equal,
171 typename _H1,
typename _H2,
typename _Hash,
172 typename _RehashPolicy,
typename _Traits>
175 _H1, _H2, _Hash, _Traits>,
177 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
179 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
181 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
183 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
185 __alloc_rebind<_Alloc,
186 __detail::_Hash_node<_Value,
187 _Traits::__hash_cached::value>>>
189 static_assert(
is_same<
typename remove_cv<_Value>::type, _Value>::value,
190 "unordered container must have a non-const, non-volatile value_type");
191 #ifdef __STRICT_ANSI__ 193 "unordered container must have the same value_type as its allocator");
195 static_assert(__is_invocable<const _H1&, const _Key&>{},
196 "hash function must be invocable with an argument of key type");
197 static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
198 "key equality predicate must be invocable with two arguments of " 201 using __traits_type = _Traits;
202 using __hash_cached =
typename __traits_type::__hash_cached;
204 using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
208 using __value_alloc_traits =
209 typename __hashtable_alloc::__value_alloc_traits;
210 using __node_alloc_traits =
216 typedef _Key key_type;
217 typedef _Value value_type;
218 typedef _Alloc allocator_type;
219 typedef _Equal key_equal;
223 typedef typename __value_alloc_traits::pointer pointer;
224 typedef typename __value_alloc_traits::const_pointer const_pointer;
225 typedef value_type& reference;
226 typedef const value_type& const_reference;
229 using __rehash_type = _RehashPolicy;
230 using __rehash_state =
typename __rehash_type::_State;
232 using __constant_iterators =
typename __traits_type::__constant_iterators;
233 using __unique_keys =
typename __traits_type::__unique_keys;
236 __constant_iterators::value,
238 __detail::_Select1st>::type;
241 _Hashtable_base<_Key, _Value, _ExtractKey,
242 _Equal, _H1, _H2, _Hash, _Traits>;
245 using __hash_code =
typename __hashtable_base::__hash_code;
246 using __ireturn_type =
typename __hashtable_base::__ireturn_type;
249 _Equal, _H1, _H2, _Hash,
250 _RehashPolicy, _Traits>;
255 _RehashPolicy, _Traits>;
258 _Equal, _H1, _H2, _Hash,
259 _RehashPolicy, _Traits>;
261 using __reuse_or_alloc_node_type =
262 __detail::_ReuseOrAllocNode<__node_alloc_type>;
265 template<
typename _Cond>
266 using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
268 template<
typename _Cond>
269 using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
275 struct __hash_code_base_access : __hash_code_base
276 {
using __hash_code_base::_M_bucket_index; };
280 static_assert(noexcept(declval<const __hash_code_base_access&>()
283 "Cache the hash code or qualify your functors involved" 284 " in hash code and bucket index computation with noexcept");
292 "Functor used to map hash code to bucket index" 293 " must be default constructible");
295 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
296 typename _ExtractKeya,
typename _Equala,
297 typename _H1a,
typename _H2a,
typename _Hasha,
298 typename _RehashPolicya,
typename _Traitsa,
302 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
303 typename _ExtractKeya,
typename _Equala,
304 typename _H1a,
typename _H2a,
typename _Hasha,
305 typename _RehashPolicya,
typename _Traitsa>
308 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
309 typename _ExtractKeya,
typename _Equala,
310 typename _H1a,
typename _H2a,
typename _Hasha,
311 typename _RehashPolicya,
typename _Traitsa,
312 bool _Constant_iteratorsa>
316 using size_type =
typename __hashtable_base::size_type;
317 using difference_type =
typename __hashtable_base::difference_type;
323 using const_local_iterator =
typename __hashtable_base::
324 const_local_iterator;
326 #if __cplusplus > 201402L 327 using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
328 using insert_return_type = _Node_insert_return<iterator, node_type>;
332 __bucket_type* _M_buckets = &_M_single_bucket;
333 size_type _M_bucket_count = 1;
334 __node_base _M_before_begin;
335 size_type _M_element_count = 0;
336 _RehashPolicy _M_rehash_policy;
344 __bucket_type _M_single_bucket =
nullptr;
347 _M_uses_single_bucket(__bucket_type* __bkts)
const 348 {
return __builtin_expect(__bkts == &_M_single_bucket,
false); }
351 _M_uses_single_bucket()
const 352 {
return _M_uses_single_bucket(_M_buckets); }
355 _M_base_alloc() {
return *
this; }
358 _M_allocate_buckets(size_type __n)
360 if (__builtin_expect(__n == 1,
false))
362 _M_single_bucket =
nullptr;
363 return &_M_single_bucket;
366 return __hashtable_alloc::_M_allocate_buckets(__n);
370 _M_deallocate_buckets(__bucket_type* __bkts, size_type __n)
372 if (_M_uses_single_bucket(__bkts))
375 __hashtable_alloc::_M_deallocate_buckets(__bkts, __n);
379 _M_deallocate_buckets()
380 { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
385 _M_bucket_begin(size_type __bkt)
const;
389 {
return static_cast<__node_type*
>(_M_before_begin._M_nxt); }
391 template<
typename _NodeGenerator>
393 _M_assign(
const _Hashtable&,
const _NodeGenerator&);
404 _Hashtable(
const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
405 const _Equal& __eq,
const _ExtractKey& __exk,
406 const allocator_type& __a)
415 const _H1&,
const _H2&,
const _Hash&,
416 const _Equal&,
const _ExtractKey&,
417 const allocator_type&);
419 template<
typename _InputIterator>
420 _Hashtable(_InputIterator __first, _InputIterator __last,
421 size_type __bucket_hint,
422 const _H1&,
const _H2&,
const _Hash&,
423 const _Equal&,
const _ExtractKey&,
424 const allocator_type&);
442 const _H1& __hf = _H1(),
443 const key_equal& __eql = key_equal(),
444 const allocator_type& __a = allocator_type())
445 :
_Hashtable(__n, __hf, _H2(), _Hash(), __eql,
446 __key_extract(), __a)
449 template<
typename _InputIterator>
450 _Hashtable(_InputIterator __f, _InputIterator __l,
452 const _H1& __hf = _H1(),
453 const key_equal& __eql = key_equal(),
454 const allocator_type& __a = allocator_type())
455 :
_Hashtable(__f, __l, __n, __hf, _H2(), _Hash(), __eql,
456 __key_extract(), __a)
461 const _H1& __hf = _H1(),
462 const key_equal& __eql = key_equal(),
463 const allocator_type& __a = allocator_type())
464 :
_Hashtable(__l.begin(), __l.end(), __n, __hf, _H2(), _Hash(), __eql,
465 __key_extract(), __a)
473 noexcept(__node_alloc_traits::_S_nothrow_move()
477 constexpr
bool __move_storage =
478 __node_alloc_traits::_S_propagate_on_move_assign()
479 || __node_alloc_traits::_S_always_equal();
487 __reuse_or_alloc_node_type __roan(_M_begin(), *
this);
488 _M_before_begin._M_nxt =
nullptr;
490 this->_M_insert_range(__l.begin(), __l.end(), __roan, __unique_keys());
498 noexcept(__and_<__is_nothrow_swappable<_H1>,
499 __is_nothrow_swappable<_Equal>>::value);
504 {
return iterator(_M_begin()); }
507 begin()
const noexcept
508 {
return const_iterator(_M_begin()); }
512 {
return iterator(
nullptr); }
516 {
return const_iterator(
nullptr); }
520 {
return const_iterator(_M_begin()); }
523 cend()
const noexcept
524 {
return const_iterator(
nullptr); }
527 size()
const noexcept
528 {
return _M_element_count; }
531 empty()
const noexcept
532 {
return size() == 0; }
535 get_allocator()
const noexcept
536 {
return allocator_type(this->_M_node_allocator()); }
539 max_size()
const noexcept
540 {
return __node_alloc_traits::max_size(this->_M_node_allocator()); }
545 {
return this->_M_eq(); }
551 bucket_count()
const noexcept
552 {
return _M_bucket_count; }
555 max_bucket_count()
const noexcept
556 {
return max_size(); }
559 bucket_size(size_type __n)
const 563 bucket(
const key_type& __k)
const 564 {
return _M_bucket_index(__k, this->_M_hash_code(__k)); }
569 return local_iterator(*
this, _M_bucket_begin(__n),
570 __n, _M_bucket_count);
575 {
return local_iterator(*
this,
nullptr, __n, _M_bucket_count); }
578 begin(size_type __n)
const 580 return const_local_iterator(*
this, _M_bucket_begin(__n),
581 __n, _M_bucket_count);
585 end(size_type __n)
const 586 {
return const_local_iterator(*
this,
nullptr, __n, _M_bucket_count); }
590 cbegin(size_type __n)
const 592 return const_local_iterator(*
this, _M_bucket_begin(__n),
593 __n, _M_bucket_count);
597 cend(size_type __n)
const 598 {
return const_local_iterator(*
this,
nullptr, __n, _M_bucket_count); }
601 load_factor()
const noexcept
603 return static_cast<float>(size()) / static_cast<float>(bucket_count());
612 __rehash_policy()
const 613 {
return _M_rehash_policy; }
616 __rehash_policy(
const _RehashPolicy& __pol)
617 { _M_rehash_policy = __pol; }
621 find(
const key_type& __k);
624 find(
const key_type& __k)
const;
627 count(
const key_type& __k)
const;
630 equal_range(
const key_type& __k);
633 equal_range(
const key_type& __k)
const;
639 {
return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
642 _M_bucket_index(
const key_type& __k, __hash_code __c)
const 643 {
return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
648 _M_find_before_node(size_type,
const key_type&, __hash_code)
const;
651 _M_find_node(size_type __bkt,
const key_type& __key,
652 __hash_code __c)
const 654 __node_base* __before_n = _M_find_before_node(__bkt, __key, __c);
656 return static_cast<__node_type*
>(__before_n->_M_nxt);
666 _M_remove_bucket_begin(size_type __bkt,
__node_type* __next_n,
667 size_type __next_bkt);
671 _M_get_previous_node(size_type __bkt, __node_base* __n);
677 _M_insert_unique_node(size_type __bkt, __hash_code __code,
686 template<
typename... _Args>
690 template<
typename... _Args>
693 {
return _M_emplace(
cend(), __uk, std::forward<_Args>(__args)...); }
696 template<
typename... _Args>
698 _M_emplace(const_iterator,
std::true_type __uk, _Args&&... __args)
699 {
return _M_emplace(__uk, std::forward<_Args>(__args)...).first; }
701 template<
typename... _Args>
705 template<
typename _Arg,
typename _NodeGenerator>
707 _M_insert(_Arg&&,
const _NodeGenerator&,
true_type, size_type = 1);
709 template<
typename _Arg,
typename _NodeGenerator>
711 _M_insert(_Arg&& __arg,
const _NodeGenerator& __node_gen,
714 return _M_insert(
cend(), std::forward<_Arg>(__arg), __node_gen,
719 template<
typename _Arg,
typename _NodeGenerator>
721 _M_insert(const_iterator, _Arg&& __arg,
722 const _NodeGenerator& __node_gen,
true_type __uk)
725 _M_insert(std::forward<_Arg>(__arg), __node_gen, __uk).
first;
729 template<
typename _Arg,
typename _NodeGenerator>
731 _M_insert(const_iterator, _Arg&&,
741 _M_erase(size_type __bkt, __node_base* __prev_n,
__node_type* __n);
745 template<
typename... _Args>
747 emplace(_Args&&... __args)
748 {
return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
750 template<
typename... _Args>
752 emplace_hint(const_iterator __hint, _Args&&... __args)
754 return _M_emplace(__hint, __unique_keys(),
755 std::forward<_Args>(__args)...);
762 erase(const_iterator);
767 {
return erase(const_iterator(__it)); }
770 erase(
const key_type& __k)
771 {
return _M_erase(__unique_keys(), __k); }
774 erase(const_iterator, const_iterator);
780 void rehash(size_type __n);
785 #if __cplusplus > 201402L 788 _M_reinsert_node(node_type&& __nh)
790 insert_return_type __ret;
792 __ret.position =
end();
795 __glibcxx_assert(get_allocator() == __nh.get_allocator());
797 const key_type& __k = __nh._M_key();
798 __hash_code __code = this->_M_hash_code(__k);
799 size_type __bkt = _M_bucket_index(__k, __code);
800 if (
__node_type* __n = _M_find_node(__bkt, __k, __code))
802 __ret.node = std::move(__nh);
803 __ret.position = iterator(__n);
804 __ret.inserted =
false;
809 = _M_insert_unique_node(__bkt, __code, __nh._M_ptr);
810 __nh._M_ptr =
nullptr;
811 __ret.inserted =
true;
819 _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
826 __glibcxx_assert(get_allocator() == __nh.get_allocator());
828 auto __code = this->_M_hash_code(__nh._M_key());
831 __ret = _M_insert_multi_node(__hint._M_cur, __code, __node);
838 extract(const_iterator __pos)
841 size_t __bkt = _M_bucket_index(__n);
846 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
848 if (__prev_n == _M_buckets[__bkt])
849 _M_remove_bucket_begin(__bkt, __n->_M_next(),
850 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
851 else if (__n->_M_nxt)
853 size_type __next_bkt = _M_bucket_index(__n->_M_next());
854 if (__next_bkt != __bkt)
855 _M_buckets[__next_bkt] = __prev_n;
858 __prev_n->_M_nxt = __n->_M_nxt;
859 __n->_M_nxt =
nullptr;
861 return { __n, this->_M_node_allocator() };
866 extract(
const _Key& __k)
869 auto __pos = find(__k);
871 __nh = extract(const_iterator(__pos));
876 template<
typename _Compatible_Hashtable>
878 _M_merge_unique(_Compatible_Hashtable& __src) noexcept
880 static_assert(is_same_v<
typename _Compatible_Hashtable::node_type,
881 node_type>,
"Node types are compatible");
882 __glibcxx_assert(get_allocator() == __src.get_allocator());
884 auto __n_elt = __src.size();
885 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
888 const key_type& __k = this->_M_extract()(__pos._M_cur->_M_v());
889 __hash_code __code = this->_M_hash_code(__k);
890 size_type __bkt = _M_bucket_index(__k, __code);
891 if (_M_find_node(__bkt, __k, __code) ==
nullptr)
893 auto __nh = __src.extract(__pos);
894 _M_insert_unique_node(__bkt, __code, __nh._M_ptr, __n_elt);
895 __nh._M_ptr =
nullptr;
898 else if (__n_elt != 1)
904 template<
typename _Compatible_Hashtable>
906 _M_merge_multi(_Compatible_Hashtable& __src) noexcept
908 static_assert(is_same_v<
typename _Compatible_Hashtable::node_type,
909 node_type>,
"Node types are compatible");
910 __glibcxx_assert(get_allocator() == __src.get_allocator());
912 this->reserve(size() + __src.size());
913 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
914 _M_reinsert_node_multi(
cend(), __src.extract(__i++));
927 void _M_rehash(size_type __n,
const __rehash_state& __state);
932 template<
typename _Key,
typename _Value,
933 typename _Alloc,
typename _ExtractKey,
typename _Equal,
934 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
937 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
938 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
939 _M_bucket_begin(size_type __bkt)
const 942 __node_base* __n = _M_buckets[__bkt];
943 return __n ?
static_cast<__node_type*
>(__n->_M_nxt) :
nullptr;
946 template<
typename _Key,
typename _Value,
947 typename _Alloc,
typename _ExtractKey,
typename _Equal,
948 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
950 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
951 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
952 _Hashtable(size_type __bucket_hint,
953 const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
954 const _Equal& __eq,
const _ExtractKey& __exk,
955 const allocator_type& __a)
956 : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
958 auto __bkt = _M_rehash_policy._M_next_bkt(__bucket_hint);
959 if (__bkt > _M_bucket_count)
961 _M_buckets = _M_allocate_buckets(__bkt);
962 _M_bucket_count = __bkt;
966 template<
typename _Key,
typename _Value,
967 typename _Alloc,
typename _ExtractKey,
typename _Equal,
968 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
970 template<
typename _InputIterator>
971 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
972 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
973 _Hashtable(_InputIterator __f, _InputIterator __l,
974 size_type __bucket_hint,
975 const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
976 const _Equal& __eq,
const _ExtractKey& __exk,
977 const allocator_type& __a)
978 : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
980 auto __nb_elems = __detail::__distance_fw(__f, __l);
982 _M_rehash_policy._M_next_bkt(
983 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
986 if (__bkt_count > _M_bucket_count)
988 _M_buckets = _M_allocate_buckets(__bkt_count);
989 _M_bucket_count = __bkt_count;
992 for (; __f != __l; ++__f)
996 template<
typename _Key,
typename _Value,
997 typename _Alloc,
typename _ExtractKey,
typename _Equal,
998 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1001 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1002 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1003 operator=(
const _Hashtable& __ht)
1009 if (__node_alloc_traits::_S_propagate_on_copy_assign())
1011 auto& __this_alloc = this->_M_node_allocator();
1012 auto& __that_alloc = __ht._M_node_allocator();
1013 if (!__node_alloc_traits::_S_always_equal()
1014 && __this_alloc != __that_alloc)
1017 this->_M_deallocate_nodes(_M_begin());
1018 _M_before_begin._M_nxt =
nullptr;
1019 _M_deallocate_buckets();
1020 _M_buckets =
nullptr;
1021 std::__alloc_on_copy(__this_alloc, __that_alloc);
1022 __hashtable_base::operator=(__ht);
1023 _M_bucket_count = __ht._M_bucket_count;
1024 _M_element_count = __ht._M_element_count;
1025 _M_rehash_policy = __ht._M_rehash_policy;
1029 [
this](
const __node_type* __n)
1030 {
return this->_M_allocate_node(__n->_M_v()); });
1037 __throw_exception_again;
1041 std::__alloc_on_copy(__this_alloc, __that_alloc);
1045 __bucket_type* __former_buckets =
nullptr;
1046 std::size_t __former_bucket_count = _M_bucket_count;
1047 const __rehash_state& __former_state = _M_rehash_policy._M_state();
1049 if (_M_bucket_count != __ht._M_bucket_count)
1051 __former_buckets = _M_buckets;
1052 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1053 _M_bucket_count = __ht._M_bucket_count;
1056 __builtin_memset(_M_buckets, 0,
1057 _M_bucket_count *
sizeof(__bucket_type));
1061 __hashtable_base::operator=(__ht);
1062 _M_element_count = __ht._M_element_count;
1063 _M_rehash_policy = __ht._M_rehash_policy;
1064 __reuse_or_alloc_node_type __roan(_M_begin(), *
this);
1065 _M_before_begin._M_nxt =
nullptr;
1067 [&__roan](
const __node_type* __n)
1068 {
return __roan(__n->_M_v()); });
1069 if (__former_buckets)
1070 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1074 if (__former_buckets)
1077 _M_deallocate_buckets();
1078 _M_rehash_policy._M_reset(__former_state);
1079 _M_buckets = __former_buckets;
1080 _M_bucket_count = __former_bucket_count;
1082 __builtin_memset(_M_buckets, 0,
1083 _M_bucket_count *
sizeof(__bucket_type));
1084 __throw_exception_again;
1089 template<
typename _Key,
typename _Value,
1090 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1091 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1093 template<
typename _NodeGenerator>
1095 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1096 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1097 _M_assign(
const _Hashtable& __ht,
const _NodeGenerator& __node_gen)
1099 __bucket_type* __buckets =
nullptr;
1101 _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
1105 if (!__ht._M_before_begin._M_nxt)
1110 __node_type* __ht_n = __ht._M_begin();
1111 __node_type* __this_n = __node_gen(__ht_n);
1112 this->_M_copy_code(__this_n, __ht_n);
1113 _M_before_begin._M_nxt = __this_n;
1114 _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
1117 __node_base* __prev_n = __this_n;
1118 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
1120 __this_n = __node_gen(__ht_n);
1121 __prev_n->_M_nxt = __this_n;
1122 this->_M_copy_code(__this_n, __ht_n);
1123 size_type __bkt = _M_bucket_index(__this_n);
1124 if (!_M_buckets[__bkt])
1125 _M_buckets[__bkt] = __prev_n;
1126 __prev_n = __this_n;
1133 _M_deallocate_buckets();
1134 __throw_exception_again;
1138 template<
typename _Key,
typename _Value,
1139 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1140 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1143 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1144 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1147 _M_rehash_policy._M_reset();
1148 _M_bucket_count = 1;
1149 _M_single_bucket =
nullptr;
1150 _M_buckets = &_M_single_bucket;
1151 _M_before_begin._M_nxt =
nullptr;
1152 _M_element_count = 0;
1155 template<
typename _Key,
typename _Value,
1156 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1157 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1160 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1161 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1164 this->_M_deallocate_nodes(_M_begin());
1165 _M_deallocate_buckets();
1166 __hashtable_base::operator=(std::move(__ht));
1167 _M_rehash_policy = __ht._M_rehash_policy;
1168 if (!__ht._M_uses_single_bucket())
1169 _M_buckets = __ht._M_buckets;
1172 _M_buckets = &_M_single_bucket;
1173 _M_single_bucket = __ht._M_single_bucket;
1175 _M_bucket_count = __ht._M_bucket_count;
1176 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1177 _M_element_count = __ht._M_element_count;
1178 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1183 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1187 template<
typename _Key,
typename _Value,
1188 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1189 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1192 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1193 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1196 if (__ht._M_node_allocator() == this->_M_node_allocator())
1201 __bucket_type* __former_buckets =
nullptr;
1202 size_type __former_bucket_count = _M_bucket_count;
1203 const __rehash_state& __former_state = _M_rehash_policy._M_state();
1205 if (_M_bucket_count != __ht._M_bucket_count)
1207 __former_buckets = _M_buckets;
1208 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1209 _M_bucket_count = __ht._M_bucket_count;
1212 __builtin_memset(_M_buckets, 0,
1213 _M_bucket_count *
sizeof(__bucket_type));
1217 __hashtable_base::operator=(std::move(__ht));
1218 _M_element_count = __ht._M_element_count;
1219 _M_rehash_policy = __ht._M_rehash_policy;
1220 __reuse_or_alloc_node_type __roan(_M_begin(), *
this);
1221 _M_before_begin._M_nxt =
nullptr;
1223 [&__roan](__node_type* __n)
1229 if (__former_buckets)
1231 _M_deallocate_buckets();
1232 _M_rehash_policy._M_reset(__former_state);
1233 _M_buckets = __former_buckets;
1234 _M_bucket_count = __former_bucket_count;
1236 __builtin_memset(_M_buckets, 0,
1237 _M_bucket_count *
sizeof(__bucket_type));
1238 __throw_exception_again;
1243 template<
typename _Key,
typename _Value,
1244 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1245 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1247 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1248 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1249 _Hashtable(
const _Hashtable& __ht)
1250 : __hashtable_base(__ht),
1252 __rehash_base(__ht),
1254 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1255 _M_buckets(nullptr),
1256 _M_bucket_count(__ht._M_bucket_count),
1257 _M_element_count(__ht._M_element_count),
1258 _M_rehash_policy(__ht._M_rehash_policy)
1261 [
this](
const __node_type* __n)
1262 {
return this->_M_allocate_node(__n->_M_v()); });
1265 template<
typename _Key,
typename _Value,
1266 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1267 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1269 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1270 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1271 _Hashtable(_Hashtable&& __ht) noexcept
1272 : __hashtable_base(__ht),
1274 __rehash_base(__ht),
1275 __hashtable_alloc(std::move(__ht._M_base_alloc())),
1276 _M_buckets(__ht._M_buckets),
1277 _M_bucket_count(__ht._M_bucket_count),
1278 _M_before_begin(__ht._M_before_begin._M_nxt),
1279 _M_element_count(__ht._M_element_count),
1280 _M_rehash_policy(__ht._M_rehash_policy)
1283 if (__ht._M_uses_single_bucket())
1285 _M_buckets = &_M_single_bucket;
1286 _M_single_bucket = __ht._M_single_bucket;
1292 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1297 template<
typename _Key,
typename _Value,
1298 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1299 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1301 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1302 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1303 _Hashtable(
const _Hashtable& __ht,
const allocator_type& __a)
1304 : __hashtable_base(__ht),
1306 __rehash_base(__ht),
1307 __hashtable_alloc(__node_alloc_type(__a)),
1309 _M_bucket_count(__ht._M_bucket_count),
1310 _M_element_count(__ht._M_element_count),
1311 _M_rehash_policy(__ht._M_rehash_policy)
1314 [
this](
const __node_type* __n)
1315 {
return this->_M_allocate_node(__n->_M_v()); });
1318 template<
typename _Key,
typename _Value,
1319 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1320 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1322 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1323 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1324 _Hashtable(_Hashtable&& __ht,
const allocator_type& __a)
1325 : __hashtable_base(__ht),
1327 __rehash_base(__ht),
1328 __hashtable_alloc(__node_alloc_type(__a)),
1329 _M_buckets(nullptr),
1330 _M_bucket_count(__ht._M_bucket_count),
1331 _M_element_count(__ht._M_element_count),
1332 _M_rehash_policy(__ht._M_rehash_policy)
1334 if (__ht._M_node_allocator() == this->_M_node_allocator())
1336 if (__ht._M_uses_single_bucket())
1338 _M_buckets = &_M_single_bucket;
1339 _M_single_bucket = __ht._M_single_bucket;
1342 _M_buckets = __ht._M_buckets;
1344 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1348 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1354 [
this](__node_type* __n)
1356 return this->_M_allocate_node(
1363 template<
typename _Key,
typename _Value,
1364 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1365 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1367 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1368 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1369 ~_Hashtable() noexcept
1372 _M_deallocate_buckets();
1375 template<
typename _Key,
typename _Value,
1376 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1377 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1380 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1381 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1382 swap(_Hashtable& __x)
1383 noexcept(__and_<__is_nothrow_swappable<_H1>,
1384 __is_nothrow_swappable<_Equal>>::value)
1391 std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
1392 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1395 if (this->_M_uses_single_bucket())
1397 if (!__x._M_uses_single_bucket())
1399 _M_buckets = __x._M_buckets;
1400 __x._M_buckets = &__x._M_single_bucket;
1403 else if (__x._M_uses_single_bucket())
1405 __x._M_buckets = _M_buckets;
1406 _M_buckets = &_M_single_bucket;
1409 std::swap(_M_buckets, __x._M_buckets);
1411 std::swap(_M_bucket_count, __x._M_bucket_count);
1412 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1413 std::swap(_M_element_count, __x._M_element_count);
1414 std::swap(_M_single_bucket, __x._M_single_bucket);
1419 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1422 __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
1423 = &__x._M_before_begin;
1426 template<
typename _Key,
typename _Value,
1427 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1428 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1431 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1432 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1433 find(
const key_type& __k)
1436 __hash_code __code = this->_M_hash_code(__k);
1437 std::size_t __n = _M_bucket_index(__k, __code);
1438 __node_type* __p = _M_find_node(__n, __k, __code);
1439 return __p ? iterator(__p) :
end();
1442 template<
typename _Key,
typename _Value,
1443 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1444 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1447 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1448 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1449 find(
const key_type& __k)
const 1452 __hash_code __code = this->_M_hash_code(__k);
1453 std::size_t __n = _M_bucket_index(__k, __code);
1454 __node_type* __p = _M_find_node(__n, __k, __code);
1455 return __p ? const_iterator(__p) :
end();
1458 template<
typename _Key,
typename _Value,
1459 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1460 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1463 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1464 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1465 count(
const key_type& __k)
const 1468 __hash_code __code = this->_M_hash_code(__k);
1469 std::size_t __n = _M_bucket_index(__k, __code);
1470 __node_type* __p = _M_bucket_begin(__n);
1474 std::size_t __result = 0;
1475 for (;; __p = __p->_M_next())
1477 if (this->_M_equals(__k, __code, __p))
1484 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1490 template<
typename _Key,
typename _Value,
1491 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1492 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1495 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1496 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1497 equal_range(
const key_type& __k)
1498 -> pair<iterator, iterator>
1500 __hash_code __code = this->_M_hash_code(__k);
1501 std::size_t __n = _M_bucket_index(__k, __code);
1502 __node_type* __p = _M_find_node(__n, __k, __code);
1506 __node_type* __p1 = __p->_M_next();
1507 while (__p1 && _M_bucket_index(__p1) == __n
1508 && this->_M_equals(__k, __code, __p1))
1509 __p1 = __p1->_M_next();
1517 template<
typename _Key,
typename _Value,
1518 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1519 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1522 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1523 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1524 equal_range(
const key_type& __k)
const 1525 -> pair<const_iterator, const_iterator>
1527 __hash_code __code = this->_M_hash_code(__k);
1528 std::size_t __n = _M_bucket_index(__k, __code);
1529 __node_type* __p = _M_find_node(__n, __k, __code);
1533 __node_type* __p1 = __p->_M_next();
1534 while (__p1 && _M_bucket_index(__p1) == __n
1535 && this->_M_equals(__k, __code, __p1))
1536 __p1 = __p1->_M_next();
1538 return std::make_pair(const_iterator(__p), const_iterator(__p1));
1546 template<
typename _Key,
typename _Value,
1547 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1548 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1551 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1552 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1553 _M_find_before_node(size_type __n,
const key_type& __k,
1554 __hash_code __code)
const 1557 __node_base* __prev_p = _M_buckets[__n];
1561 for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
1562 __p = __p->_M_next())
1564 if (this->_M_equals(__k, __code, __p))
1567 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1574 template<
typename _Key,
typename _Value,
1575 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1576 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1579 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1580 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1581 _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
1583 if (_M_buckets[__bkt])
1587 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1588 _M_buckets[__bkt]->_M_nxt = __node;
1595 __node->_M_nxt = _M_before_begin._M_nxt;
1596 _M_before_begin._M_nxt = __node;
1600 _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
1601 _M_buckets[__bkt] = &_M_before_begin;
1605 template<
typename _Key,
typename _Value,
1606 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1607 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1610 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1611 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1612 _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
1613 size_type __next_bkt)
1615 if (!__next || __next_bkt != __bkt)
1620 _M_buckets[__next_bkt] = _M_buckets[__bkt];
1623 if (&_M_before_begin == _M_buckets[__bkt])
1624 _M_before_begin._M_nxt = __next;
1625 _M_buckets[__bkt] =
nullptr;
1629 template<
typename _Key,
typename _Value,
1630 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1631 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1634 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1635 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1636 _M_get_previous_node(size_type __bkt, __node_base* __n)
1639 __node_base* __prev_n = _M_buckets[__bkt];
1640 while (__prev_n->_M_nxt != __n)
1641 __prev_n = __prev_n->_M_nxt;
1645 template<
typename _Key,
typename _Value,
1646 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1647 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1649 template<
typename... _Args>
1651 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1652 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1654 -> pair<iterator, bool>
1657 __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
1658 const key_type& __k = this->_M_extract()(__node->_M_v());
1662 __code = this->_M_hash_code(__k);
1666 this->_M_deallocate_node(__node);
1667 __throw_exception_again;
1670 size_type __bkt = _M_bucket_index(__k, __code);
1671 if (__node_type* __p = _M_find_node(__bkt, __k, __code))
1674 this->_M_deallocate_node(__node);
1679 return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
1683 template<
typename _Key,
typename _Value,
1684 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1685 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1687 template<
typename... _Args>
1689 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1690 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1695 __node_type* __node =
1696 this->_M_allocate_node(std::forward<_Args>(__args)...);
1701 __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
1705 this->_M_deallocate_node(__node);
1706 __throw_exception_again;
1709 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1712 template<
typename _Key,
typename _Value,
1713 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1714 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1717 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1718 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1719 _M_insert_unique_node(size_type __bkt, __hash_code __code,
1720 __node_type* __node, size_type __n_elt)
1723 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1725 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
1730 if (__do_rehash.
first)
1732 _M_rehash(__do_rehash.
second, __saved_state);
1733 __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
1736 this->_M_store_code(__node, __code);
1739 _M_insert_bucket_begin(__bkt, __node);
1741 return iterator(__node);
1745 this->_M_deallocate_node(__node);
1746 __throw_exception_again;
1752 template<
typename _Key,
typename _Value,
1753 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1754 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1757 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1758 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1759 _M_insert_multi_node(__node_type* __hint, __hash_code __code,
1760 __node_type* __node)
1763 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1765 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1769 if (__do_rehash.
first)
1770 _M_rehash(__do_rehash.
second, __saved_state);
1772 this->_M_store_code(__node, __code);
1773 const key_type& __k = this->_M_extract()(__node->_M_v());
1774 size_type __bkt = _M_bucket_index(__k, __code);
1779 = __builtin_expect(__hint !=
nullptr,
false)
1780 && this->_M_equals(__k, __code, __hint)
1782 : _M_find_before_node(__bkt, __k, __code);
1786 __node->_M_nxt = __prev->_M_nxt;
1787 __prev->_M_nxt = __node;
1788 if (__builtin_expect(__prev == __hint,
false))
1792 && !this->_M_equals(__k, __code, __node->_M_next()))
1794 size_type __next_bkt = _M_bucket_index(__node->_M_next());
1795 if (__next_bkt != __bkt)
1796 _M_buckets[__next_bkt] = __node;
1804 _M_insert_bucket_begin(__bkt, __node);
1806 return iterator(__node);
1810 this->_M_deallocate_node(__node);
1811 __throw_exception_again;
1816 template<
typename _Key,
typename _Value,
1817 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1818 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1820 template<
typename _Arg,
typename _NodeGenerator>
1822 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1823 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1824 _M_insert(_Arg&& __v,
const _NodeGenerator& __node_gen,
true_type,
1826 -> pair<iterator, bool>
1828 const key_type& __k = this->_M_extract()(__v);
1829 __hash_code __code = this->_M_hash_code(__k);
1830 size_type __bkt = _M_bucket_index(__k, __code);
1832 __node_type* __n = _M_find_node(__bkt, __k, __code);
1836 __n = __node_gen(std::forward<_Arg>(__v));
1837 return { _M_insert_unique_node(__bkt, __code, __n, __n_elt),
true };
1841 template<
typename _Key,
typename _Value,
1842 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1843 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1845 template<
typename _Arg,
typename _NodeGenerator>
1847 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1848 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1849 _M_insert(const_iterator __hint, _Arg&& __v,
1850 const _NodeGenerator& __node_gen,
false_type)
1855 __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
1858 __node_type* __node = __node_gen(std::forward<_Arg>(__v));
1860 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1863 template<
typename _Key,
typename _Value,
1864 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1865 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1868 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1869 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1870 erase(const_iterator __it)
1873 __node_type* __n = __it._M_cur;
1874 std::size_t __bkt = _M_bucket_index(__n);
1879 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1880 return _M_erase(__bkt, __prev_n, __n);
1883 template<
typename _Key,
typename _Value,
1884 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1885 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1888 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1889 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1890 _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
1893 if (__prev_n == _M_buckets[__bkt])
1894 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1895 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
1896 else if (__n->_M_nxt)
1898 size_type __next_bkt = _M_bucket_index(__n->_M_next());
1899 if (__next_bkt != __bkt)
1900 _M_buckets[__next_bkt] = __prev_n;
1903 __prev_n->_M_nxt = __n->_M_nxt;
1904 iterator __result(__n->_M_next());
1905 this->_M_deallocate_node(__n);
1911 template<
typename _Key,
typename _Value,
1912 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1913 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1916 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1917 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1921 __hash_code __code = this->_M_hash_code(__k);
1922 std::size_t __bkt = _M_bucket_index(__k, __code);
1925 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1930 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1931 _M_erase(__bkt, __prev_n, __n);
1935 template<
typename _Key,
typename _Value,
1936 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1937 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1940 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1941 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1945 __hash_code __code = this->_M_hash_code(__k);
1946 std::size_t __bkt = _M_bucket_index(__k, __code);
1949 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1959 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1960 __node_type* __n_last = __n;
1961 std::size_t __n_last_bkt = __bkt;
1964 __n_last = __n_last->_M_next();
1967 __n_last_bkt = _M_bucket_index(__n_last);
1969 while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
1972 size_type __result = 0;
1975 __node_type* __p = __n->_M_next();
1976 this->_M_deallocate_node(__n);
1981 while (__n != __n_last);
1983 if (__prev_n == _M_buckets[__bkt])
1984 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
1985 else if (__n_last && __n_last_bkt != __bkt)
1986 _M_buckets[__n_last_bkt] = __prev_n;
1987 __prev_n->_M_nxt = __n_last;
1991 template<
typename _Key,
typename _Value,
1992 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1993 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1996 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1997 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1998 erase(const_iterator __first, const_iterator __last)
2001 __node_type* __n = __first._M_cur;
2002 __node_type* __last_n = __last._M_cur;
2003 if (__n == __last_n)
2004 return iterator(__n);
2006 std::size_t __bkt = _M_bucket_index(__n);
2008 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
2009 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2010 std::size_t __n_bkt = __bkt;
2015 __node_type* __tmp = __n;
2016 __n = __n->_M_next();
2017 this->_M_deallocate_node(__tmp);
2021 __n_bkt = _M_bucket_index(__n);
2023 while (__n != __last_n && __n_bkt == __bkt);
2024 if (__is_bucket_begin)
2025 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2026 if (__n == __last_n)
2028 __is_bucket_begin =
true;
2032 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
2033 _M_buckets[__n_bkt] = __prev_n;
2034 __prev_n->_M_nxt = __n;
2035 return iterator(__n);
2038 template<
typename _Key,
typename _Value,
2039 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2040 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2043 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2044 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2047 this->_M_deallocate_nodes(_M_begin());
2048 __builtin_memset(_M_buckets, 0, _M_bucket_count *
sizeof(__bucket_type));
2049 _M_element_count = 0;
2050 _M_before_begin._M_nxt =
nullptr;
2053 template<
typename _Key,
typename _Value,
2054 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2055 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2058 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2059 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2060 rehash(size_type __n)
2062 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
2063 std::size_t __buckets
2064 =
std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
2066 __buckets = _M_rehash_policy._M_next_bkt(__buckets);
2068 if (__buckets != _M_bucket_count)
2069 _M_rehash(__buckets, __saved_state);
2072 _M_rehash_policy._M_reset(__saved_state);
2075 template<
typename _Key,
typename _Value,
2076 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2077 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2080 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2081 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2082 _M_rehash(size_type __n,
const __rehash_state& __state)
2086 _M_rehash_aux(__n, __unique_keys());
2092 _M_rehash_policy._M_reset(__state);
2093 __throw_exception_again;
2098 template<
typename _Key,
typename _Value,
2099 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2100 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2103 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2104 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2107 __bucket_type* __new_buckets = _M_allocate_buckets(__n);
2108 __node_type* __p = _M_begin();
2109 _M_before_begin._M_nxt =
nullptr;
2110 std::size_t __bbegin_bkt = 0;
2113 __node_type* __next = __p->_M_next();
2114 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2115 if (!__new_buckets[__bkt])
2117 __p->_M_nxt = _M_before_begin._M_nxt;
2118 _M_before_begin._M_nxt = __p;
2119 __new_buckets[__bkt] = &_M_before_begin;
2121 __new_buckets[__bbegin_bkt] = __p;
2122 __bbegin_bkt = __bkt;
2126 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2127 __new_buckets[__bkt]->_M_nxt = __p;
2132 _M_deallocate_buckets();
2133 _M_bucket_count = __n;
2134 _M_buckets = __new_buckets;
2139 template<
typename _Key,
typename _Value,
2140 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2141 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2144 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2145 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2148 __bucket_type* __new_buckets = _M_allocate_buckets(__n);
2150 __node_type* __p = _M_begin();
2151 _M_before_begin._M_nxt =
nullptr;
2152 std::size_t __bbegin_bkt = 0;
2153 std::size_t __prev_bkt = 0;
2154 __node_type* __prev_p =
nullptr;
2155 bool __check_bucket =
false;
2159 __node_type* __next = __p->_M_next();
2160 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2162 if (__prev_p && __prev_bkt == __bkt)
2167 __p->_M_nxt = __prev_p->_M_nxt;
2168 __prev_p->_M_nxt = __p;
2175 __check_bucket =
true;
2183 if (__prev_p->_M_nxt)
2185 std::size_t __next_bkt
2186 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
2188 if (__next_bkt != __prev_bkt)
2189 __new_buckets[__next_bkt] = __prev_p;
2191 __check_bucket =
false;
2194 if (!__new_buckets[__bkt])
2196 __p->_M_nxt = _M_before_begin._M_nxt;
2197 _M_before_begin._M_nxt = __p;
2198 __new_buckets[__bkt] = &_M_before_begin;
2200 __new_buckets[__bbegin_bkt] = __p;
2201 __bbegin_bkt = __bkt;
2205 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2206 __new_buckets[__bkt]->_M_nxt = __p;
2214 if (__check_bucket && __prev_p->_M_nxt)
2216 std::size_t __next_bkt
2217 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
2218 if (__next_bkt != __prev_bkt)
2219 __new_buckets[__next_bkt] = __prev_p;
2222 _M_deallocate_buckets();
2223 _M_bucket_count = __n;
2224 _M_buckets = __new_buckets;
2227 #if __cplusplus > 201402L 2228 template<
typename,
typename,
typename>
class _Hash_merge_helper { };
2231 _GLIBCXX_END_NAMESPACE_VERSION
2234 #endif // _HASHTABLE_H
_Tp exchange(_Tp &__obj, _Up &&__new_val)
Assign __new_val to __obj and return its previous value.
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
Define a member typedef type to one of two argument types.
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
constexpr pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
is_nothrow_move_assignable
_T2 second
first is a copy of the first object
_T1 first
second_type is the second bound type
constexpr conditional< __move_if_noexcept_cond< _Tp >::value, const _Tp &, _Tp && >::type move_if_noexcept(_Tp &__x) noexcept
Conditionally convert a value to an rvalue.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
Node const_iterators, used to iterate through all the hashtable.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
ISO C++ entities toplevel namespace is std.
Uniform interface to C++98 and C++11 allocators.
_GLIBCXX17_CONSTEXPR iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Node iterators, used to iterate through all the hashtable.
Struct holding two objects of arbitrary type.
_GLIBCXX14_CONSTEXPR const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.