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;
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;
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)
1226 if (__former_buckets)
1227 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1232 if (__former_buckets)
1234 _M_deallocate_buckets();
1235 _M_rehash_policy._M_reset(__former_state);
1236 _M_buckets = __former_buckets;
1237 _M_bucket_count = __former_bucket_count;
1239 __builtin_memset(_M_buckets, 0,
1240 _M_bucket_count *
sizeof(__bucket_type));
1241 __throw_exception_again;
1246 template<
typename _Key,
typename _Value,
1247 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1248 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1250 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1251 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1252 _Hashtable(
const _Hashtable& __ht)
1253 : __hashtable_base(__ht),
1255 __rehash_base(__ht),
1257 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1258 _M_buckets(nullptr),
1259 _M_bucket_count(__ht._M_bucket_count),
1260 _M_element_count(__ht._M_element_count),
1261 _M_rehash_policy(__ht._M_rehash_policy)
1264 [
this](
const __node_type* __n)
1265 {
return this->_M_allocate_node(__n->_M_v()); });
1268 template<
typename _Key,
typename _Value,
1269 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1270 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1272 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1273 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1274 _Hashtable(_Hashtable&& __ht)
noexcept
1275 : __hashtable_base(__ht),
1277 __rehash_base(__ht),
1278 __hashtable_alloc(std::move(__ht._M_base_alloc())),
1279 _M_buckets(__ht._M_buckets),
1280 _M_bucket_count(__ht._M_bucket_count),
1281 _M_before_begin(__ht._M_before_begin._M_nxt),
1282 _M_element_count(__ht._M_element_count),
1283 _M_rehash_policy(__ht._M_rehash_policy)
1286 if (__ht._M_uses_single_bucket())
1288 _M_buckets = &_M_single_bucket;
1289 _M_single_bucket = __ht._M_single_bucket;
1295 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1300 template<
typename _Key,
typename _Value,
1301 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1302 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1304 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1305 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1306 _Hashtable(
const _Hashtable& __ht,
const allocator_type& __a)
1307 : __hashtable_base(__ht),
1309 __rehash_base(__ht),
1310 __hashtable_alloc(__node_alloc_type(__a)),
1312 _M_bucket_count(__ht._M_bucket_count),
1313 _M_element_count(__ht._M_element_count),
1314 _M_rehash_policy(__ht._M_rehash_policy)
1317 [
this](
const __node_type* __n)
1318 {
return this->_M_allocate_node(__n->_M_v()); });
1321 template<
typename _Key,
typename _Value,
1322 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1323 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1325 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1326 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1327 _Hashtable(_Hashtable&& __ht,
const allocator_type& __a)
1328 : __hashtable_base(__ht),
1330 __rehash_base(__ht),
1331 __hashtable_alloc(__node_alloc_type(__a)),
1332 _M_buckets(nullptr),
1333 _M_bucket_count(__ht._M_bucket_count),
1334 _M_element_count(__ht._M_element_count),
1335 _M_rehash_policy(__ht._M_rehash_policy)
1337 if (__ht._M_node_allocator() == this->_M_node_allocator())
1339 if (__ht._M_uses_single_bucket())
1341 _M_buckets = &_M_single_bucket;
1342 _M_single_bucket = __ht._M_single_bucket;
1345 _M_buckets = __ht._M_buckets;
1347 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1351 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1357 [
this](__node_type* __n)
1359 return this->_M_allocate_node(
1366 template<
typename _Key,
typename _Value,
1367 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1368 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1370 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1371 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1375 _M_deallocate_buckets();
1378 template<
typename _Key,
typename _Value,
1379 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1380 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1383 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1384 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1385 swap(_Hashtable& __x)
1386 noexcept(__and_<__is_nothrow_swappable<_H1>,
1387 __is_nothrow_swappable<_Equal>>::value)
1394 std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
1395 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1398 if (this->_M_uses_single_bucket())
1400 if (!__x._M_uses_single_bucket())
1402 _M_buckets = __x._M_buckets;
1403 __x._M_buckets = &__x._M_single_bucket;
1406 else if (__x._M_uses_single_bucket())
1408 __x._M_buckets = _M_buckets;
1409 _M_buckets = &_M_single_bucket;
1412 std::swap(_M_buckets, __x._M_buckets);
1414 std::swap(_M_bucket_count, __x._M_bucket_count);
1415 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1416 std::swap(_M_element_count, __x._M_element_count);
1417 std::swap(_M_single_bucket, __x._M_single_bucket);
1422 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1425 __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
1426 = &__x._M_before_begin;
1429 template<
typename _Key,
typename _Value,
1430 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1431 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1434 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1435 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1436 find(
const key_type& __k)
1439 __hash_code __code = this->_M_hash_code(__k);
1440 std::size_t __n = _M_bucket_index(__k, __code);
1441 __node_type* __p = _M_find_node(__n, __k, __code);
1442 return __p ? iterator(__p) :
end();
1445 template<
typename _Key,
typename _Value,
1446 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1447 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1450 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1451 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1452 find(
const key_type& __k)
const
1455 __hash_code __code = this->_M_hash_code(__k);
1456 std::size_t __n = _M_bucket_index(__k, __code);
1457 __node_type* __p = _M_find_node(__n, __k, __code);
1458 return __p ? const_iterator(__p) :
end();
1461 template<
typename _Key,
typename _Value,
1462 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1463 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1466 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1467 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1468 count(
const key_type& __k)
const
1471 __hash_code __code = this->_M_hash_code(__k);
1472 std::size_t __n = _M_bucket_index(__k, __code);
1473 __node_type* __p = _M_bucket_begin(__n);
1477 std::size_t __result = 0;
1478 for (;; __p = __p->_M_next())
1480 if (this->_M_equals(__k, __code, __p))
1487 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1493 template<
typename _Key,
typename _Value,
1494 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1495 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1498 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1499 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1500 equal_range(
const key_type& __k)
1501 -> pair<iterator, iterator>
1503 __hash_code __code = this->_M_hash_code(__k);
1504 std::size_t __n = _M_bucket_index(__k, __code);
1505 __node_type* __p = _M_find_node(__n, __k, __code);
1509 __node_type* __p1 = __p->_M_next();
1510 while (__p1 && _M_bucket_index(__p1) == __n
1511 && this->_M_equals(__k, __code, __p1))
1512 __p1 = __p1->_M_next();
1520 template<
typename _Key,
typename _Value,
1521 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1522 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1525 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1526 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1527 equal_range(
const key_type& __k)
const
1528 -> pair<const_iterator, const_iterator>
1530 __hash_code __code = this->_M_hash_code(__k);
1531 std::size_t __n = _M_bucket_index(__k, __code);
1532 __node_type* __p = _M_find_node(__n, __k, __code);
1536 __node_type* __p1 = __p->_M_next();
1537 while (__p1 && _M_bucket_index(__p1) == __n
1538 && this->_M_equals(__k, __code, __p1))
1539 __p1 = __p1->_M_next();
1541 return std::make_pair(const_iterator(__p), const_iterator(__p1));
1549 template<
typename _Key,
typename _Value,
1550 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1551 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1554 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1555 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1556 _M_find_before_node(size_type __n,
const key_type& __k,
1557 __hash_code __code)
const
1560 __node_base* __prev_p = _M_buckets[__n];
1564 for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
1565 __p = __p->_M_next())
1567 if (this->_M_equals(__k, __code, __p))
1570 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1577 template<
typename _Key,
typename _Value,
1578 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1579 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1582 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1583 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1584 _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
1586 if (_M_buckets[__bkt])
1590 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1591 _M_buckets[__bkt]->_M_nxt = __node;
1598 __node->_M_nxt = _M_before_begin._M_nxt;
1599 _M_before_begin._M_nxt = __node;
1603 _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
1604 _M_buckets[__bkt] = &_M_before_begin;
1608 template<
typename _Key,
typename _Value,
1609 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1610 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1613 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1614 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1615 _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
1616 size_type __next_bkt)
1618 if (!__next || __next_bkt != __bkt)
1623 _M_buckets[__next_bkt] = _M_buckets[__bkt];
1626 if (&_M_before_begin == _M_buckets[__bkt])
1627 _M_before_begin._M_nxt = __next;
1628 _M_buckets[__bkt] =
nullptr;
1632 template<
typename _Key,
typename _Value,
1633 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1634 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1637 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1638 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1639 _M_get_previous_node(size_type __bkt, __node_base* __n)
1642 __node_base* __prev_n = _M_buckets[__bkt];
1643 while (__prev_n->_M_nxt != __n)
1644 __prev_n = __prev_n->_M_nxt;
1648 template<
typename _Key,
typename _Value,
1649 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1650 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1652 template<
typename... _Args>
1654 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1655 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1657 -> pair<iterator, bool>
1660 __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
1661 const key_type& __k = this->_M_extract()(__node->_M_v());
1665 __code = this->_M_hash_code(__k);
1669 this->_M_deallocate_node(__node);
1670 __throw_exception_again;
1673 size_type __bkt = _M_bucket_index(__k, __code);
1674 if (__node_type* __p = _M_find_node(__bkt, __k, __code))
1677 this->_M_deallocate_node(__node);
1682 return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
1686 template<
typename _Key,
typename _Value,
1687 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1688 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1690 template<
typename... _Args>
1692 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1693 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1698 __node_type* __node =
1699 this->_M_allocate_node(std::forward<_Args>(__args)...);
1704 __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
1708 this->_M_deallocate_node(__node);
1709 __throw_exception_again;
1712 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1715 template<
typename _Key,
typename _Value,
1716 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1717 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1720 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1721 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1722 _M_insert_unique_node(size_type __bkt, __hash_code __code,
1723 __node_type* __node, size_type __n_elt)
1726 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1728 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
1733 if (__do_rehash.
first)
1735 _M_rehash(__do_rehash.
second, __saved_state);
1736 __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
1739 this->_M_store_code(__node, __code);
1742 _M_insert_bucket_begin(__bkt, __node);
1744 return iterator(__node);
1748 this->_M_deallocate_node(__node);
1749 __throw_exception_again;
1755 template<
typename _Key,
typename _Value,
1756 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1757 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1760 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1761 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1762 _M_insert_multi_node(__node_type* __hint, __hash_code __code,
1763 __node_type* __node)
1766 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1768 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1772 if (__do_rehash.
first)
1773 _M_rehash(__do_rehash.
second, __saved_state);
1775 this->_M_store_code(__node, __code);
1776 const key_type& __k = this->_M_extract()(__node->_M_v());
1777 size_type __bkt = _M_bucket_index(__k, __code);
1782 = __builtin_expect(__hint !=
nullptr,
false)
1783 && this->_M_equals(__k, __code, __hint)
1785 : _M_find_before_node(__bkt, __k, __code);
1789 __node->_M_nxt = __prev->_M_nxt;
1790 __prev->_M_nxt = __node;
1791 if (__builtin_expect(__prev == __hint,
false))
1795 && !this->_M_equals(__k, __code, __node->_M_next()))
1797 size_type __next_bkt = _M_bucket_index(__node->_M_next());
1798 if (__next_bkt != __bkt)
1799 _M_buckets[__next_bkt] = __node;
1807 _M_insert_bucket_begin(__bkt, __node);
1809 return iterator(__node);
1813 this->_M_deallocate_node(__node);
1814 __throw_exception_again;
1819 template<
typename _Key,
typename _Value,
1820 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1821 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1823 template<
typename _Arg,
typename _NodeGenerator>
1825 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1826 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1827 _M_insert(_Arg&& __v,
const _NodeGenerator& __node_gen,
true_type,
1829 -> pair<iterator, bool>
1831 const key_type& __k = this->_M_extract()(__v);
1832 __hash_code __code = this->_M_hash_code(__k);
1833 size_type __bkt = _M_bucket_index(__k, __code);
1835 __node_type* __n = _M_find_node(__bkt, __k, __code);
1839 __n = __node_gen(std::forward<_Arg>(__v));
1840 return { _M_insert_unique_node(__bkt, __code, __n, __n_elt),
true };
1844 template<
typename _Key,
typename _Value,
1845 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1846 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1848 template<
typename _Arg,
typename _NodeGenerator>
1850 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1851 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1852 _M_insert(const_iterator __hint, _Arg&& __v,
1853 const _NodeGenerator& __node_gen,
false_type)
1858 __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
1861 __node_type* __node = __node_gen(std::forward<_Arg>(__v));
1863 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1866 template<
typename _Key,
typename _Value,
1867 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1868 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1871 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1872 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1873 erase(const_iterator __it)
1876 __node_type* __n = __it._M_cur;
1877 std::size_t __bkt = _M_bucket_index(__n);
1882 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1883 return _M_erase(__bkt, __prev_n, __n);
1886 template<
typename _Key,
typename _Value,
1887 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1888 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1891 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1892 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1893 _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
1896 if (__prev_n == _M_buckets[__bkt])
1897 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1898 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
1899 else if (__n->_M_nxt)
1901 size_type __next_bkt = _M_bucket_index(__n->_M_next());
1902 if (__next_bkt != __bkt)
1903 _M_buckets[__next_bkt] = __prev_n;
1906 __prev_n->_M_nxt = __n->_M_nxt;
1907 iterator __result(__n->_M_next());
1908 this->_M_deallocate_node(__n);
1914 template<
typename _Key,
typename _Value,
1915 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1916 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1919 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1920 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1924 __hash_code __code = this->_M_hash_code(__k);
1925 std::size_t __bkt = _M_bucket_index(__k, __code);
1928 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1933 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1934 _M_erase(__bkt, __prev_n, __n);
1938 template<
typename _Key,
typename _Value,
1939 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1940 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1943 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1944 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1948 __hash_code __code = this->_M_hash_code(__k);
1949 std::size_t __bkt = _M_bucket_index(__k, __code);
1952 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1962 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1963 __node_type* __n_last = __n;
1964 std::size_t __n_last_bkt = __bkt;
1967 __n_last = __n_last->_M_next();
1970 __n_last_bkt = _M_bucket_index(__n_last);
1972 while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
1975 size_type __result = 0;
1978 __node_type* __p = __n->_M_next();
1979 this->_M_deallocate_node(__n);
1984 while (__n != __n_last);
1986 if (__prev_n == _M_buckets[__bkt])
1987 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
1988 else if (__n_last && __n_last_bkt != __bkt)
1989 _M_buckets[__n_last_bkt] = __prev_n;
1990 __prev_n->_M_nxt = __n_last;
1994 template<
typename _Key,
typename _Value,
1995 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1996 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1999 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2000 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2001 erase(const_iterator __first, const_iterator __last)
2004 __node_type* __n = __first._M_cur;
2005 __node_type* __last_n = __last._M_cur;
2006 if (__n == __last_n)
2007 return iterator(__n);
2009 std::size_t __bkt = _M_bucket_index(__n);
2011 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
2012 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2013 std::size_t __n_bkt = __bkt;
2018 __node_type* __tmp = __n;
2019 __n = __n->_M_next();
2020 this->_M_deallocate_node(__tmp);
2024 __n_bkt = _M_bucket_index(__n);
2026 while (__n != __last_n && __n_bkt == __bkt);
2027 if (__is_bucket_begin)
2028 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2029 if (__n == __last_n)
2031 __is_bucket_begin =
true;
2035 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
2036 _M_buckets[__n_bkt] = __prev_n;
2037 __prev_n->_M_nxt = __n;
2038 return iterator(__n);
2041 template<
typename _Key,
typename _Value,
2042 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2043 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2046 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2047 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2050 this->_M_deallocate_nodes(_M_begin());
2051 __builtin_memset(_M_buckets, 0, _M_bucket_count *
sizeof(__bucket_type));
2052 _M_element_count = 0;
2053 _M_before_begin._M_nxt =
nullptr;
2056 template<
typename _Key,
typename _Value,
2057 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2058 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2061 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2062 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2063 rehash(size_type __n)
2065 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
2066 std::size_t __buckets
2067 =
std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
2069 __buckets = _M_rehash_policy._M_next_bkt(__buckets);
2071 if (__buckets != _M_bucket_count)
2072 _M_rehash(__buckets, __saved_state);
2075 _M_rehash_policy._M_reset(__saved_state);
2078 template<
typename _Key,
typename _Value,
2079 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2080 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2083 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2084 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2085 _M_rehash(size_type __n,
const __rehash_state& __state)
2089 _M_rehash_aux(__n, __unique_keys());
2095 _M_rehash_policy._M_reset(__state);
2096 __throw_exception_again;
2101 template<
typename _Key,
typename _Value,
2102 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2103 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2106 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2107 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2110 __bucket_type* __new_buckets = _M_allocate_buckets(__n);
2111 __node_type* __p = _M_begin();
2112 _M_before_begin._M_nxt =
nullptr;
2113 std::size_t __bbegin_bkt = 0;
2116 __node_type* __next = __p->_M_next();
2117 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2118 if (!__new_buckets[__bkt])
2120 __p->_M_nxt = _M_before_begin._M_nxt;
2121 _M_before_begin._M_nxt = __p;
2122 __new_buckets[__bkt] = &_M_before_begin;
2124 __new_buckets[__bbegin_bkt] = __p;
2125 __bbegin_bkt = __bkt;
2129 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2130 __new_buckets[__bkt]->_M_nxt = __p;
2135 _M_deallocate_buckets();
2136 _M_bucket_count = __n;
2137 _M_buckets = __new_buckets;
2142 template<
typename _Key,
typename _Value,
2143 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2144 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2147 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2148 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2151 __bucket_type* __new_buckets = _M_allocate_buckets(__n);
2153 __node_type* __p = _M_begin();
2154 _M_before_begin._M_nxt =
nullptr;
2155 std::size_t __bbegin_bkt = 0;
2156 std::size_t __prev_bkt = 0;
2157 __node_type* __prev_p =
nullptr;
2158 bool __check_bucket =
false;
2162 __node_type* __next = __p->_M_next();
2163 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2165 if (__prev_p && __prev_bkt == __bkt)
2170 __p->_M_nxt = __prev_p->_M_nxt;
2171 __prev_p->_M_nxt = __p;
2178 __check_bucket =
true;
2186 if (__prev_p->_M_nxt)
2188 std::size_t __next_bkt
2189 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
2191 if (__next_bkt != __prev_bkt)
2192 __new_buckets[__next_bkt] = __prev_p;
2194 __check_bucket =
false;
2197 if (!__new_buckets[__bkt])
2199 __p->_M_nxt = _M_before_begin._M_nxt;
2200 _M_before_begin._M_nxt = __p;
2201 __new_buckets[__bkt] = &_M_before_begin;
2203 __new_buckets[__bbegin_bkt] = __p;
2204 __bbegin_bkt = __bkt;
2208 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2209 __new_buckets[__bkt]->_M_nxt = __p;
2217 if (__check_bucket && __prev_p->_M_nxt)
2219 std::size_t __next_bkt
2220 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
2221 if (__next_bkt != __prev_bkt)
2222 __new_buckets[__next_bkt] = __prev_p;
2225 _M_deallocate_buckets();
2226 _M_bucket_count = __n;
2227 _M_buckets = __new_buckets;
2230 #if __cplusplus > 201402L
2231 template<
typename,
typename,
typename>
class _Hash_merge_helper { };
2234 _GLIBCXX_END_NAMESPACE_VERSION
2237 #endif // _HASHTABLE_H
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.
_GLIBCXX17_CONSTEXPR iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
Uniform interface to C++98 and C++11 allocators.
Node const_iterators, used to iterate through all the hashtable.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
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.
is_nothrow_move_assignable
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
_T2 second
first is a copy of the first object
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.
_GLIBCXX14_CONSTEXPR const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
_Tp exchange(_Tp &__obj, _Up &&__new_val)
Assign __new_val to __obj and return its previous value.
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.
_T1 first
second_type is the second bound type
Define a member typedef type to one of two argument types.
Node iterators, used to iterate through all the hashtable.