31 #define _HASHTABLE_H 1
33 #pragma GCC system_header
37 #if __cplusplus > 201402L
41 namespace std _GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 template<
typename _Tp,
typename _Hash>
48 __is_fast_hash<_Hash>,
50 __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
55 template<
typename _Equal,
typename _Hash,
typename _Allocator>
56 using _Hashtable_enable_default_ctor
57 = _Enable_default_constructor<__and_<is_default_constructible<_Equal>,
58 is_default_constructible<_Hash>,
59 is_default_constructible<_Allocator>>{},
60 __detail::_Hash_node_base>;
180 template<
typename _Key,
typename _Value,
typename _Alloc,
181 typename _ExtractKey,
typename _Equal,
182 typename _H1,
typename _H2,
typename _Hash,
183 typename _RehashPolicy,
typename _Traits>
186 _H1, _H2, _Hash, _Traits>,
188 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
190 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
192 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
194 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
196 __alloc_rebind<_Alloc,
197 __detail::_Hash_node<_Value,
198 _Traits::__hash_cached::value>>>,
201 static_assert(
is_same<
typename remove_cv<_Value>::type, _Value>::value,
202 "unordered container must have a non-const, non-volatile value_type");
203 #if __cplusplus > 201703L || defined __STRICT_ANSI__
205 "unordered container must have the same value_type as its allocator");
208 using __traits_type = _Traits;
209 using __hash_cached =
typename __traits_type::__hash_cached;
211 using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
215 using __value_alloc_traits =
216 typename __hashtable_alloc::__value_alloc_traits;
217 using __node_alloc_traits =
225 typedef _Key key_type;
226 typedef _Value value_type;
227 typedef _Alloc allocator_type;
228 typedef _Equal key_equal;
232 typedef typename __value_alloc_traits::pointer pointer;
233 typedef typename __value_alloc_traits::const_pointer const_pointer;
234 typedef value_type& reference;
235 typedef const value_type& const_reference;
238 using __rehash_type = _RehashPolicy;
239 using __rehash_state =
typename __rehash_type::_State;
241 using __constant_iterators =
typename __traits_type::__constant_iterators;
242 using __unique_keys =
typename __traits_type::__unique_keys;
245 __constant_iterators::value,
247 __detail::_Select1st>::type;
250 _Hashtable_base<_Key, _Value, _ExtractKey,
251 _Equal, _H1, _H2, _Hash, _Traits>;
254 using __hash_code =
typename __hashtable_base::__hash_code;
255 using __ireturn_type =
typename __hashtable_base::__ireturn_type;
258 _Equal, _H1, _H2, _Hash,
259 _RehashPolicy, _Traits>;
264 _RehashPolicy, _Traits>;
267 _Equal, _H1, _H2, _Hash,
268 _RehashPolicy, _Traits>;
270 using __reuse_or_alloc_node_gen_t =
271 __detail::_ReuseOrAllocNode<__node_alloc_type>;
272 using __alloc_node_gen_t =
273 __detail::_AllocNode<__node_alloc_type>;
280 : _M_h(__h), _M_node(__n) { }
283 template<
typename... _Args>
286 _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
290 ~_Scoped_node() {
if (_M_node) _M_h->_M_deallocate_node(_M_node); };
292 _Scoped_node(
const _Scoped_node&) =
delete;
293 _Scoped_node& operator=(
const _Scoped_node&) =
delete;
299 template<
typename _Ht>
302 const value_type&, value_type&&>::type
303 __fwd_value_for(value_type& __val) noexcept
307 template<
typename _Cond>
308 using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
310 template<
typename _Cond>
311 using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
317 struct __hash_code_base_access : __hash_code_base
318 {
using __hash_code_base::_M_bucket_index; };
322 static_assert(noexcept(declval<const __hash_code_base_access&>()
325 "Cache the hash code or qualify your functors involved"
326 " in hash code and bucket index computation with noexcept");
331 "Functor used to map hash code to bucket index"
332 " must be default constructible");
334 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
335 typename _ExtractKeya,
typename _Equala,
336 typename _H1a,
typename _H2a,
typename _Hasha,
337 typename _RehashPolicya,
typename _Traitsa,
341 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
342 typename _ExtractKeya,
typename _Equala,
343 typename _H1a,
typename _H2a,
typename _Hasha,
344 typename _RehashPolicya,
typename _Traitsa>
347 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
348 typename _ExtractKeya,
typename _Equala,
349 typename _H1a,
typename _H2a,
typename _Hasha,
350 typename _RehashPolicya,
typename _Traitsa,
351 bool _Constant_iteratorsa>
354 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
355 typename _ExtractKeya,
typename _Equala,
356 typename _H1a,
typename _H2a,
typename _Hasha,
357 typename _RehashPolicya,
typename _Traitsa,
362 using size_type =
typename __hashtable_base::size_type;
363 using difference_type =
typename __hashtable_base::difference_type;
369 using const_local_iterator =
typename __hashtable_base::
370 const_local_iterator;
372 #if __cplusplus > 201402L
373 using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
374 using insert_return_type = _Node_insert_return<iterator, node_type>;
378 __bucket_type* _M_buckets = &_M_single_bucket;
379 size_type _M_bucket_count = 1;
380 __node_base _M_before_begin;
381 size_type _M_element_count = 0;
382 _RehashPolicy _M_rehash_policy;
390 __bucket_type _M_single_bucket =
nullptr;
393 _M_uses_single_bucket(__bucket_type* __bkts)
const
394 {
return __builtin_expect(__bkts == &_M_single_bucket,
false); }
397 _M_uses_single_bucket()
const
398 {
return _M_uses_single_bucket(_M_buckets); }
401 _M_base_alloc() {
return *
this; }
404 _M_allocate_buckets(size_type __bkt_count)
406 if (__builtin_expect(__bkt_count == 1,
false))
408 _M_single_bucket =
nullptr;
409 return &_M_single_bucket;
412 return __hashtable_alloc::_M_allocate_buckets(__bkt_count);
416 _M_deallocate_buckets(__bucket_type* __bkts, size_type __bkt_count)
418 if (_M_uses_single_bucket(__bkts))
421 __hashtable_alloc::_M_deallocate_buckets(__bkts, __bkt_count);
425 _M_deallocate_buckets()
426 { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
431 _M_bucket_begin(size_type __bkt)
const;
435 {
return static_cast<__node_type*
>(_M_before_begin._M_nxt); }
439 template<
typename _Ht>
441 _M_assign_elements(_Ht&&);
443 template<
typename _Ht,
typename _NodeGenerator>
445 _M_assign(_Ht&&,
const _NodeGenerator&);
456 _Hashtable(
const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
457 const _Equal& __eq,
const _ExtractKey& __exk,
458 const allocator_type& __a)
464 template<
bool _No_realloc = true>
465 static constexpr
bool
468 #if __cplusplus <= 201402L
469 return __and_<__bool_constant<_No_realloc>,
473 if constexpr (_No_realloc)
482 noexcept(_S_nothrow_move());
492 const _H1&,
const _H2&,
const _Hash&,
493 const _Equal&,
const _ExtractKey&,
494 const allocator_type&);
496 template<
typename _InputIterator>
497 _Hashtable(_InputIterator __first, _InputIterator __last,
498 size_type __bkt_count_hint,
499 const _H1&,
const _H2&,
const _Hash&,
500 const _Equal&,
const _ExtractKey&,
501 const allocator_type&);
506 noexcept(_S_nothrow_move())
514 noexcept(_S_nothrow_move<__node_alloc_traits::_S_always_equal()>())
516 typename __node_alloc_traits::is_always_equal{})
528 const _H1& __hf = _H1(),
529 const key_equal& __eql = key_equal(),
530 const allocator_type& __a = allocator_type())
531 :
_Hashtable(__bkt_count_hint, __hf, _H2(), _Hash(), __eql,
532 __key_extract(), __a)
535 template<
typename _InputIterator>
536 _Hashtable(_InputIterator __f, _InputIterator __l,
537 size_type __bkt_count_hint = 0,
538 const _H1& __hf = _H1(),
539 const key_equal& __eql = key_equal(),
540 const allocator_type& __a = allocator_type())
541 :
_Hashtable(__f, __l, __bkt_count_hint, __hf, _H2(), _Hash(), __eql,
542 __key_extract(), __a)
546 size_type __bkt_count_hint = 0,
547 const _H1& __hf = _H1(),
548 const key_equal& __eql = key_equal(),
549 const allocator_type& __a = allocator_type())
550 :
_Hashtable(__l.begin(), __l.end(), __bkt_count_hint,
551 __hf, _H2(), _Hash(), __eql,
552 __key_extract(), __a)
560 noexcept(__node_alloc_traits::_S_nothrow_move()
564 constexpr
bool __move_storage =
565 __node_alloc_traits::_S_propagate_on_move_assign()
566 || __node_alloc_traits::_S_always_equal();
574 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *
this);
575 _M_before_begin._M_nxt =
nullptr;
577 this->_M_insert_range(__l.begin(), __l.end(), __roan, __unique_keys());
585 noexcept(__and_<__is_nothrow_swappable<_H1>,
586 __is_nothrow_swappable<_Equal>>::value);
591 {
return iterator(_M_begin()); }
594 begin()
const noexcept
595 {
return const_iterator(_M_begin()); }
599 {
return iterator(
nullptr); }
603 {
return const_iterator(
nullptr); }
607 {
return const_iterator(_M_begin()); }
610 cend()
const noexcept
611 {
return const_iterator(
nullptr); }
614 size()
const noexcept
615 {
return _M_element_count; }
617 _GLIBCXX_NODISCARD
bool
618 empty()
const noexcept
619 {
return size() == 0; }
622 get_allocator()
const noexcept
623 {
return allocator_type(this->_M_node_allocator()); }
626 max_size()
const noexcept
627 {
return __node_alloc_traits::max_size(this->_M_node_allocator()); }
632 {
return this->_M_eq(); }
638 bucket_count()
const noexcept
639 {
return _M_bucket_count; }
642 max_bucket_count()
const noexcept
643 {
return max_size(); }
646 bucket_size(size_type __bkt)
const
650 bucket(
const key_type& __k)
const
651 {
return _M_bucket_index(__k, this->_M_hash_code(__k)); }
654 begin(size_type __bkt)
656 return local_iterator(*
this, _M_bucket_begin(__bkt),
657 __bkt, _M_bucket_count);
662 {
return local_iterator(*
this,
nullptr, __bkt, _M_bucket_count); }
665 begin(size_type __bkt)
const
667 return const_local_iterator(*
this, _M_bucket_begin(__bkt),
668 __bkt, _M_bucket_count);
672 end(size_type __bkt)
const
673 {
return const_local_iterator(*
this,
nullptr, __bkt, _M_bucket_count); }
677 cbegin(size_type __bkt)
const
679 return const_local_iterator(*
this, _M_bucket_begin(__bkt),
680 __bkt, _M_bucket_count);
684 cend(size_type __bkt)
const
685 {
return const_local_iterator(*
this,
nullptr, __bkt, _M_bucket_count); }
688 load_factor()
const noexcept
690 return static_cast<float>(size()) /
static_cast<float>(bucket_count());
699 __rehash_policy()
const
700 {
return _M_rehash_policy; }
703 __rehash_policy(
const _RehashPolicy& __pol)
704 { _M_rehash_policy = __pol; }
708 find(
const key_type& __k);
711 find(
const key_type& __k)
const;
714 count(
const key_type& __k)
const;
717 equal_range(
const key_type& __k);
720 equal_range(
const key_type& __k)
const;
726 {
return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
729 _M_bucket_index(
const key_type& __k, __hash_code __c)
const
730 {
return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
735 _M_find_before_node(size_type,
const key_type&, __hash_code)
const;
738 _M_find_node(size_type __bkt,
const key_type& __key,
739 __hash_code __c)
const
741 __node_base* __before_n = _M_find_before_node(__bkt, __key, __c);
743 return static_cast<__node_type*
>(__before_n->_M_nxt);
753 _M_remove_bucket_begin(size_type __bkt,
__node_type* __next_n,
754 size_type __next_bkt);
758 _M_get_previous_node(size_type __bkt, __node_base* __n);
764 _M_insert_unique_node(
const key_type& __k, size_type __bkt,
766 size_type __n_elt = 1);
771 _M_insert_multi_node(
__node_type* __hint,
const key_type& __k,
774 template<
typename... _Args>
776 _M_emplace(
true_type, _Args&&... __args);
778 template<
typename... _Args>
780 _M_emplace(
false_type __uk, _Args&&... __args)
781 {
return _M_emplace(
cend(), __uk, std::forward<_Args>(__args)...); }
784 template<
typename... _Args>
786 _M_emplace(const_iterator,
true_type __uk, _Args&&... __args)
787 {
return _M_emplace(__uk, std::forward<_Args>(__args)...).first; }
789 template<
typename... _Args>
791 _M_emplace(const_iterator,
false_type, _Args&&... __args);
793 template<
typename _Arg,
typename _NodeGenerator>
795 _M_insert(_Arg&&,
const _NodeGenerator&,
true_type, size_type = 1);
797 template<
typename _Arg,
typename _NodeGenerator>
799 _M_insert(_Arg&& __arg,
const _NodeGenerator& __node_gen,
802 return _M_insert(
cend(), std::forward<_Arg>(__arg), __node_gen,
807 template<
typename _Arg,
typename _NodeGenerator>
809 _M_insert(const_iterator, _Arg&& __arg,
810 const _NodeGenerator& __node_gen,
true_type __uk)
813 _M_insert(std::forward<_Arg>(__arg), __node_gen, __uk).
first;
817 template<
typename _Arg,
typename _NodeGenerator>
819 _M_insert(const_iterator, _Arg&&,
829 _M_erase(size_type __bkt, __node_base* __prev_n,
__node_type* __n);
833 template<
typename... _Args>
835 emplace(_Args&&... __args)
836 {
return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
838 template<
typename... _Args>
840 emplace_hint(const_iterator __hint, _Args&&... __args)
842 return _M_emplace(__hint, __unique_keys(),
843 std::forward<_Args>(__args)...);
850 erase(const_iterator);
855 {
return erase(const_iterator(__it)); }
858 erase(
const key_type& __k)
859 {
return _M_erase(__unique_keys(), __k); }
862 erase(const_iterator, const_iterator);
869 void rehash(size_type __bkt_count);
874 #if __cplusplus > 201402L
877 _M_reinsert_node(node_type&& __nh)
879 insert_return_type __ret;
881 __ret.position =
end();
884 __glibcxx_assert(get_allocator() == __nh.get_allocator());
886 const key_type& __k = __nh._M_key();
887 __hash_code __code = this->_M_hash_code(__k);
888 size_type __bkt = _M_bucket_index(__k, __code);
889 if (
__node_type* __n = _M_find_node(__bkt, __k, __code))
892 __ret.position = iterator(__n);
893 __ret.inserted =
false;
898 = _M_insert_unique_node(__k, __bkt, __code, __nh._M_ptr);
899 __nh._M_ptr =
nullptr;
900 __ret.inserted =
true;
908 _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
913 __glibcxx_assert(get_allocator() == __nh.get_allocator());
915 const key_type& __k = __nh._M_key();
916 auto __code = this->_M_hash_code(__k);
918 = _M_insert_multi_node(__hint._M_cur, __k, __code, __nh._M_ptr);
919 __nh._M_ptr =
nullptr;
925 _M_extract_node(
size_t __bkt, __node_base* __prev_n)
928 if (__prev_n == _M_buckets[__bkt])
929 _M_remove_bucket_begin(__bkt, __n->_M_next(),
930 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
931 else if (__n->_M_nxt)
933 size_type __next_bkt = _M_bucket_index(__n->_M_next());
934 if (__next_bkt != __bkt)
935 _M_buckets[__next_bkt] = __prev_n;
938 __prev_n->_M_nxt = __n->_M_nxt;
939 __n->_M_nxt =
nullptr;
941 return { __n, this->_M_node_allocator() };
947 extract(const_iterator __pos)
949 size_t __bkt = _M_bucket_index(__pos._M_cur);
950 return _M_extract_node(__bkt,
951 _M_get_previous_node(__bkt, __pos._M_cur));
956 extract(
const _Key& __k)
959 __hash_code __code = this->_M_hash_code(__k);
960 std::size_t __bkt = _M_bucket_index(__k, __code);
961 if (__node_base* __prev_node = _M_find_before_node(__bkt, __k, __code))
962 __nh = _M_extract_node(__bkt, __prev_node);
967 template<
typename _Compatible_Hashtable>
969 _M_merge_unique(_Compatible_Hashtable& __src) noexcept
971 static_assert(is_same_v<
typename _Compatible_Hashtable::node_type,
972 node_type>,
"Node types are compatible");
973 __glibcxx_assert(get_allocator() == __src.get_allocator());
975 auto __n_elt = __src.size();
976 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
979 const key_type& __k = this->_M_extract()(*__pos);
980 __hash_code __code = this->_M_hash_code(__k);
981 size_type __bkt = _M_bucket_index(__k, __code);
982 if (_M_find_node(__bkt, __k, __code) ==
nullptr)
984 auto __nh = __src.extract(__pos);
985 _M_insert_unique_node(__k, __bkt, __code, __nh._M_ptr,
987 __nh._M_ptr =
nullptr;
990 else if (__n_elt != 1)
996 template<
typename _Compatible_Hashtable>
998 _M_merge_multi(_Compatible_Hashtable& __src) noexcept
1000 static_assert(is_same_v<
typename _Compatible_Hashtable::node_type,
1001 node_type>,
"Node types are compatible");
1002 __glibcxx_assert(get_allocator() == __src.get_allocator());
1004 this->reserve(size() + __src.size());
1005 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1006 _M_reinsert_node_multi(
cend(), __src.extract(__i++));
1012 void _M_rehash_aux(size_type __bkt_count,
true_type);
1015 void _M_rehash_aux(size_type __bkt_count,
false_type);
1019 void _M_rehash(size_type __bkt_count,
const __rehash_state& __state);
1024 template<
typename _Key,
typename _Value,
1025 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1026 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1029 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1030 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1031 _M_bucket_begin(size_type __bkt)
const
1034 __node_base* __n = _M_buckets[__bkt];
1035 return __n ?
static_cast<__node_type*
>(__n->_M_nxt) :
nullptr;
1038 template<
typename _Key,
typename _Value,
1039 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1040 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1042 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1043 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1044 _Hashtable(size_type __bkt_count_hint,
1045 const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
1046 const _Equal& __eq,
const _ExtractKey& __exk,
1047 const allocator_type& __a)
1048 : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
1050 auto __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count_hint);
1051 if (__bkt_count > _M_bucket_count)
1053 _M_buckets = _M_allocate_buckets(__bkt_count);
1054 _M_bucket_count = __bkt_count;
1058 template<
typename _Key,
typename _Value,
1059 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1060 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1062 template<
typename _InputIterator>
1063 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1064 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1065 _Hashtable(_InputIterator __f, _InputIterator __l,
1066 size_type __bkt_count_hint,
1067 const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
1068 const _Equal& __eq,
const _ExtractKey& __exk,
1069 const allocator_type& __a)
1070 : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
1072 auto __nb_elems = __detail::__distance_fw(__f, __l);
1074 _M_rehash_policy._M_next_bkt(
1075 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
1078 if (__bkt_count > _M_bucket_count)
1080 _M_buckets = _M_allocate_buckets(__bkt_count);
1081 _M_bucket_count = __bkt_count;
1084 for (; __f != __l; ++__f)
1088 template<
typename _Key,
typename _Value,
1089 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1090 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1093 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1094 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1095 operator=(
const _Hashtable& __ht)
1101 if (__node_alloc_traits::_S_propagate_on_copy_assign())
1103 auto& __this_alloc = this->_M_node_allocator();
1104 auto& __that_alloc = __ht._M_node_allocator();
1105 if (!__node_alloc_traits::_S_always_equal()
1106 && __this_alloc != __that_alloc)
1109 this->_M_deallocate_nodes(_M_begin());
1110 _M_before_begin._M_nxt =
nullptr;
1111 _M_deallocate_buckets();
1112 _M_buckets =
nullptr;
1113 std::__alloc_on_copy(__this_alloc, __that_alloc);
1114 __hashtable_base::operator=(__ht);
1115 _M_bucket_count = __ht._M_bucket_count;
1116 _M_element_count = __ht._M_element_count;
1117 _M_rehash_policy = __ht._M_rehash_policy;
1118 __alloc_node_gen_t __alloc_node_gen(*
this);
1121 _M_assign(__ht, __alloc_node_gen);
1128 __throw_exception_again;
1132 std::__alloc_on_copy(__this_alloc, __that_alloc);
1136 _M_assign_elements(__ht);
1140 template<
typename _Key,
typename _Value,
1141 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1142 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1144 template<
typename _Ht>
1146 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1147 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1148 _M_assign_elements(_Ht&& __ht)
1150 __bucket_type* __former_buckets =
nullptr;
1151 std::size_t __former_bucket_count = _M_bucket_count;
1152 const __rehash_state& __former_state = _M_rehash_policy._M_state();
1154 if (_M_bucket_count != __ht._M_bucket_count)
1156 __former_buckets = _M_buckets;
1157 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1158 _M_bucket_count = __ht._M_bucket_count;
1161 __builtin_memset(_M_buckets, 0,
1162 _M_bucket_count *
sizeof(__bucket_type));
1166 __hashtable_base::operator=(std::forward<_Ht>(__ht));
1167 _M_element_count = __ht._M_element_count;
1168 _M_rehash_policy = __ht._M_rehash_policy;
1169 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *
this);
1170 _M_before_begin._M_nxt =
nullptr;
1171 _M_assign(std::forward<_Ht>(__ht), __roan);
1172 if (__former_buckets)
1173 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1177 if (__former_buckets)
1180 _M_deallocate_buckets();
1181 _M_rehash_policy._M_reset(__former_state);
1182 _M_buckets = __former_buckets;
1183 _M_bucket_count = __former_bucket_count;
1185 __builtin_memset(_M_buckets, 0,
1186 _M_bucket_count *
sizeof(__bucket_type));
1187 __throw_exception_again;
1191 template<
typename _Key,
typename _Value,
1192 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1193 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1195 template<
typename _Ht,
typename _NodeGenerator>
1197 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1198 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1199 _M_assign(_Ht&& __ht,
const _NodeGenerator& __node_gen)
1201 __bucket_type* __buckets =
nullptr;
1203 _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
1207 if (!__ht._M_before_begin._M_nxt)
1212 __node_type* __ht_n = __ht._M_begin();
1213 __node_type* __this_n
1214 = __node_gen(__fwd_value_for<_Ht>(__ht_n->_M_v()));
1215 this->_M_copy_code(__this_n, __ht_n);
1216 _M_before_begin._M_nxt = __this_n;
1217 _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
1220 __node_base* __prev_n = __this_n;
1221 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
1223 __this_n = __node_gen(__fwd_value_for<_Ht>(__ht_n->_M_v()));
1224 __prev_n->_M_nxt = __this_n;
1225 this->_M_copy_code(__this_n, __ht_n);
1226 size_type __bkt = _M_bucket_index(__this_n);
1227 if (!_M_buckets[__bkt])
1228 _M_buckets[__bkt] = __prev_n;
1229 __prev_n = __this_n;
1236 _M_deallocate_buckets();
1237 __throw_exception_again;
1241 template<
typename _Key,
typename _Value,
1242 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1243 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1246 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1247 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1250 _M_rehash_policy._M_reset();
1251 _M_bucket_count = 1;
1252 _M_single_bucket =
nullptr;
1253 _M_buckets = &_M_single_bucket;
1254 _M_before_begin._M_nxt =
nullptr;
1255 _M_element_count = 0;
1258 template<
typename _Key,
typename _Value,
1259 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1260 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1263 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1264 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1265 _M_move_assign(_Hashtable&& __ht,
true_type)
1267 this->_M_deallocate_nodes(_M_begin());
1268 _M_deallocate_buckets();
1269 __hashtable_base::operator=(
std::move(__ht));
1270 _M_rehash_policy = __ht._M_rehash_policy;
1271 if (!__ht._M_uses_single_bucket())
1272 _M_buckets = __ht._M_buckets;
1275 _M_buckets = &_M_single_bucket;
1276 _M_single_bucket = __ht._M_single_bucket;
1278 _M_bucket_count = __ht._M_bucket_count;
1279 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1280 _M_element_count = __ht._M_element_count;
1281 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1286 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1290 template<
typename _Key,
typename _Value,
1291 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1292 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1295 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1296 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1297 _M_move_assign(_Hashtable&& __ht,
false_type)
1299 if (__ht._M_node_allocator() == this->_M_node_allocator())
1309 template<
typename _Key,
typename _Value,
1310 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1311 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1313 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1314 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1315 _Hashtable(
const _Hashtable& __ht)
1316 : __hashtable_base(__ht),
1318 __rehash_base(__ht),
1320 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1321 __enable_default_ctor(__ht),
1322 _M_buckets(nullptr),
1323 _M_bucket_count(__ht._M_bucket_count),
1324 _M_element_count(__ht._M_element_count),
1325 _M_rehash_policy(__ht._M_rehash_policy)
1327 __alloc_node_gen_t __alloc_node_gen(*
this);
1328 _M_assign(__ht, __alloc_node_gen);
1331 template<
typename _Key,
typename _Value,
1332 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1333 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1335 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1336 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1337 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1339 noexcept(_S_nothrow_move())
1340 : __hashtable_base(__ht),
1342 __rehash_base(__ht),
1343 __hashtable_alloc(
std::
move(__a)),
1344 __enable_default_ctor(__ht),
1345 _M_buckets(__ht._M_buckets),
1346 _M_bucket_count(__ht._M_bucket_count),
1347 _M_before_begin(__ht._M_before_begin._M_nxt),
1348 _M_element_count(__ht._M_element_count),
1349 _M_rehash_policy(__ht._M_rehash_policy)
1352 if (__ht._M_uses_single_bucket())
1354 _M_buckets = &_M_single_bucket;
1355 _M_single_bucket = __ht._M_single_bucket;
1361 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
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>::
1372 _Hashtable(
const _Hashtable& __ht,
const allocator_type& __a)
1373 : __hashtable_base(__ht),
1375 __rehash_base(__ht),
1376 __hashtable_alloc(__node_alloc_type(__a)),
1377 __enable_default_ctor(__ht),
1379 _M_bucket_count(__ht._M_bucket_count),
1380 _M_element_count(__ht._M_element_count),
1381 _M_rehash_policy(__ht._M_rehash_policy)
1383 __alloc_node_gen_t __alloc_node_gen(*
this);
1384 _M_assign(__ht, __alloc_node_gen);
1387 template<
typename _Key,
typename _Value,
1388 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1389 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1391 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1392 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1393 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1395 : __hashtable_base(__ht),
1397 __rehash_base(__ht),
1398 __hashtable_alloc(
std::
move(__a)),
1399 __enable_default_ctor(__ht),
1400 _M_buckets(nullptr),
1401 _M_bucket_count(__ht._M_bucket_count),
1402 _M_element_count(__ht._M_element_count),
1403 _M_rehash_policy(__ht._M_rehash_policy)
1405 if (__ht._M_node_allocator() == this->_M_node_allocator())
1407 if (__ht._M_uses_single_bucket())
1409 _M_buckets = &_M_single_bucket;
1410 _M_single_bucket = __ht._M_single_bucket;
1413 _M_buckets = __ht._M_buckets;
1415 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1419 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1424 __alloc_node_gen_t __alloc_gen(*
this);
1426 using _Fwd_Ht =
typename
1427 conditional<__move_if_noexcept_cond<value_type>::value,
1428 const _Hashtable&, _Hashtable&&>::type;
1429 _M_assign(std::forward<_Fwd_Ht>(__ht), __alloc_gen);
1434 template<
typename _Key,
typename _Value,
1435 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1436 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1438 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1439 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1440 ~_Hashtable() noexcept
1443 _M_deallocate_buckets();
1446 template<
typename _Key,
typename _Value,
1447 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1448 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1451 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1452 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1453 swap(_Hashtable& __x)
1454 noexcept(__and_<__is_nothrow_swappable<_H1>,
1455 __is_nothrow_swappable<_Equal>>::value)
1462 std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
1463 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1466 if (this->_M_uses_single_bucket())
1468 if (!__x._M_uses_single_bucket())
1470 _M_buckets = __x._M_buckets;
1471 __x._M_buckets = &__x._M_single_bucket;
1474 else if (__x._M_uses_single_bucket())
1476 __x._M_buckets = _M_buckets;
1477 _M_buckets = &_M_single_bucket;
1480 std::swap(_M_buckets, __x._M_buckets);
1482 std::swap(_M_bucket_count, __x._M_bucket_count);
1483 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1484 std::swap(_M_element_count, __x._M_element_count);
1485 std::swap(_M_single_bucket, __x._M_single_bucket);
1490 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1493 __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
1494 = &__x._M_before_begin;
1497 template<
typename _Key,
typename _Value,
1498 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1499 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1502 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1503 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1504 find(
const key_type& __k)
1507 __hash_code __code = this->_M_hash_code(__k);
1508 std::size_t __bkt = _M_bucket_index(__k, __code);
1509 __node_type* __p = _M_find_node(__bkt, __k, __code);
1510 return __p ? iterator(__p) :
end();
1513 template<
typename _Key,
typename _Value,
1514 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1515 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1518 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1519 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1520 find(
const key_type& __k)
const
1523 __hash_code __code = this->_M_hash_code(__k);
1524 std::size_t __bkt = _M_bucket_index(__k, __code);
1525 __node_type* __p = _M_find_node(__bkt, __k, __code);
1526 return __p ? const_iterator(__p) :
end();
1529 template<
typename _Key,
typename _Value,
1530 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1531 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1534 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1535 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1536 count(
const key_type& __k)
const
1539 __hash_code __code = this->_M_hash_code(__k);
1540 std::size_t __bkt = _M_bucket_index(__k, __code);
1541 __node_type* __p = _M_bucket_begin(__bkt);
1545 std::size_t __result = 0;
1546 for (;; __p = __p->_M_next())
1548 if (this->_M_equals(__k, __code, __p))
1555 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __bkt)
1561 template<
typename _Key,
typename _Value,
1562 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1563 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1566 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1567 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1568 equal_range(
const key_type& __k)
1569 -> pair<iterator, iterator>
1571 __hash_code __code = this->_M_hash_code(__k);
1572 std::size_t __bkt = _M_bucket_index(__k, __code);
1573 __node_type* __p = _M_find_node(__bkt, __k, __code);
1577 __node_type* __p1 = __p->_M_next();
1578 while (__p1 && _M_bucket_index(__p1) == __bkt
1579 && this->_M_equals(__k, __code, __p1))
1580 __p1 = __p1->_M_next();
1582 return std::make_pair(iterator(__p), iterator(__p1));
1585 return std::make_pair(
end(),
end());
1588 template<
typename _Key,
typename _Value,
1589 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1590 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1593 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1594 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1595 equal_range(
const key_type& __k)
const
1596 -> pair<const_iterator, const_iterator>
1598 __hash_code __code = this->_M_hash_code(__k);
1599 std::size_t __bkt = _M_bucket_index(__k, __code);
1600 __node_type* __p = _M_find_node(__bkt, __k, __code);
1604 __node_type* __p1 = __p->_M_next();
1605 while (__p1 && _M_bucket_index(__p1) == __bkt
1606 && this->_M_equals(__k, __code, __p1))
1607 __p1 = __p1->_M_next();
1609 return std::make_pair(const_iterator(__p), const_iterator(__p1));
1612 return std::make_pair(
end(),
end());
1617 template<
typename _Key,
typename _Value,
1618 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1619 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1622 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1623 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1624 _M_find_before_node(size_type __bkt,
const key_type& __k,
1625 __hash_code __code)
const
1628 __node_base* __prev_p = _M_buckets[__bkt];
1632 for (__node_type* __p =
static_cast<__node_type*
>(__prev_p->_M_nxt);;
1633 __p = __p->_M_next())
1635 if (this->_M_equals(__k, __code, __p))
1638 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __bkt)
1645 template<
typename _Key,
typename _Value,
1646 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1647 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1650 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1651 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1652 _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
1654 if (_M_buckets[__bkt])
1658 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1659 _M_buckets[__bkt]->_M_nxt = __node;
1666 __node->_M_nxt = _M_before_begin._M_nxt;
1667 _M_before_begin._M_nxt = __node;
1671 _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
1672 _M_buckets[__bkt] = &_M_before_begin;
1676 template<
typename _Key,
typename _Value,
1677 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1678 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1681 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1682 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1683 _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
1684 size_type __next_bkt)
1686 if (!__next || __next_bkt != __bkt)
1691 _M_buckets[__next_bkt] = _M_buckets[__bkt];
1694 if (&_M_before_begin == _M_buckets[__bkt])
1695 _M_before_begin._M_nxt = __next;
1696 _M_buckets[__bkt] =
nullptr;
1700 template<
typename _Key,
typename _Value,
1701 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1702 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1705 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1706 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1707 _M_get_previous_node(size_type __bkt, __node_base* __n)
1710 __node_base* __prev_n = _M_buckets[__bkt];
1711 while (__prev_n->_M_nxt != __n)
1712 __prev_n = __prev_n->_M_nxt;
1716 template<
typename _Key,
typename _Value,
1717 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1718 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1720 template<
typename... _Args>
1722 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1723 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1724 _M_emplace(
true_type, _Args&&... __args)
1725 -> pair<iterator, bool>
1728 _Scoped_node __node {
this, std::forward<_Args>(__args)... };
1729 const key_type& __k = this->_M_extract()(__node._M_node->_M_v());
1730 __hash_code __code = this->_M_hash_code(__k);
1731 size_type __bkt = _M_bucket_index(__k, __code);
1732 if (__node_type* __p = _M_find_node(__bkt, __k, __code))
1734 return std::make_pair(iterator(__p),
false);
1737 auto __pos = _M_insert_unique_node(__k, __bkt, __code, __node._M_node);
1738 __node._M_node =
nullptr;
1739 return { __pos,
true };
1742 template<
typename _Key,
typename _Value,
1743 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1744 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1746 template<
typename... _Args>
1748 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1749 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1750 _M_emplace(const_iterator __hint,
false_type, _Args&&... __args)
1754 _Scoped_node __node {
this, std::forward<_Args>(__args)... };
1755 const key_type& __k = this->_M_extract()(__node._M_node->_M_v());
1757 __hash_code __code = this->_M_hash_code(__k);
1759 = _M_insert_multi_node(__hint._M_cur, __k, __code, __node._M_node);
1760 __node._M_node =
nullptr;
1764 template<
typename _Key,
typename _Value,
1765 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1766 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1769 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1770 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1771 _M_insert_unique_node(
const key_type& __k, size_type __bkt,
1772 __hash_code __code, __node_type* __node,
1776 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1778 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
1781 if (__do_rehash.
first)
1783 _M_rehash(__do_rehash.
second, __saved_state);
1784 __bkt = _M_bucket_index(__k, __code);
1787 this->_M_store_code(__node, __code);
1790 _M_insert_bucket_begin(__bkt, __node);
1792 return iterator(__node);
1795 template<
typename _Key,
typename _Value,
1796 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1797 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1800 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1801 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1802 _M_insert_multi_node(__node_type* __hint,
const key_type& __k,
1803 __hash_code __code, __node_type* __node)
1806 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1808 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1810 if (__do_rehash.
first)
1811 _M_rehash(__do_rehash.
second, __saved_state);
1813 this->_M_store_code(__node, __code);
1814 size_type __bkt = _M_bucket_index(__k, __code);
1819 = __builtin_expect(__hint !=
nullptr,
false)
1820 && this->_M_equals(__k, __code, __hint)
1822 : _M_find_before_node(__bkt, __k, __code);
1826 __node->_M_nxt = __prev->_M_nxt;
1827 __prev->_M_nxt = __node;
1828 if (__builtin_expect(__prev == __hint,
false))
1832 && !this->_M_equals(__k, __code, __node->_M_next()))
1834 size_type __next_bkt = _M_bucket_index(__node->_M_next());
1835 if (__next_bkt != __bkt)
1836 _M_buckets[__next_bkt] = __node;
1843 _M_insert_bucket_begin(__bkt, __node);
1845 return iterator(__node);
1849 template<
typename _Key,
typename _Value,
1850 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1851 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1853 template<
typename _Arg,
typename _NodeGenerator>
1855 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1856 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1857 _M_insert(_Arg&& __v,
const _NodeGenerator& __node_gen,
true_type,
1859 -> pair<iterator, bool>
1861 const key_type& __k = this->_M_extract()(__v);
1862 __hash_code __code = this->_M_hash_code(__k);
1863 size_type __bkt = _M_bucket_index(__k, __code);
1865 if (__node_type* __node = _M_find_node(__bkt, __k, __code))
1866 return { iterator(__node),
false };
1868 _Scoped_node __node{ __node_gen(std::forward<_Arg>(__v)),
this };
1870 = _M_insert_unique_node(__k, __bkt, __code, __node._M_node, __n_elt);
1871 __node._M_node =
nullptr;
1872 return { __pos,
true };
1876 template<
typename _Key,
typename _Value,
1877 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1878 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1880 template<
typename _Arg,
typename _NodeGenerator>
1882 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1883 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1884 _M_insert(const_iterator __hint, _Arg&& __v,
1885 const _NodeGenerator& __node_gen,
false_type)
1890 __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
1893 _Scoped_node __node{ __node_gen(std::forward<_Arg>(__v)),
this };
1894 const key_type& __k = this->_M_extract()(__node._M_node->_M_v());
1896 = _M_insert_multi_node(__hint._M_cur, __k, __code, __node._M_node);
1897 __node._M_node =
nullptr;
1901 template<
typename _Key,
typename _Value,
1902 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1903 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1906 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1907 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1908 erase(const_iterator __it)
1911 __node_type* __n = __it._M_cur;
1912 std::size_t __bkt = _M_bucket_index(__n);
1917 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1918 return _M_erase(__bkt, __prev_n, __n);
1921 template<
typename _Key,
typename _Value,
1922 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1923 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1926 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1927 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1928 _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
1931 if (__prev_n == _M_buckets[__bkt])
1932 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1933 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
1934 else if (__n->_M_nxt)
1936 size_type __next_bkt = _M_bucket_index(__n->_M_next());
1937 if (__next_bkt != __bkt)
1938 _M_buckets[__next_bkt] = __prev_n;
1941 __prev_n->_M_nxt = __n->_M_nxt;
1942 iterator __result(__n->_M_next());
1943 this->_M_deallocate_node(__n);
1949 template<
typename _Key,
typename _Value,
1950 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1951 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1954 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1955 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1956 _M_erase(
true_type,
const key_type& __k)
1959 __hash_code __code = this->_M_hash_code(__k);
1960 std::size_t __bkt = _M_bucket_index(__k, __code);
1963 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1968 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1969 _M_erase(__bkt, __prev_n, __n);
1973 template<
typename _Key,
typename _Value,
1974 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1975 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1978 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1979 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1983 __hash_code __code = this->_M_hash_code(__k);
1984 std::size_t __bkt = _M_bucket_index(__k, __code);
1987 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1997 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1998 __node_type* __n_last = __n;
1999 std::size_t __n_last_bkt = __bkt;
2002 __n_last = __n_last->_M_next();
2005 __n_last_bkt = _M_bucket_index(__n_last);
2007 while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
2010 size_type __result = 0;
2013 __node_type* __p = __n->_M_next();
2014 this->_M_deallocate_node(__n);
2019 while (__n != __n_last);
2021 if (__prev_n == _M_buckets[__bkt])
2022 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
2023 else if (__n_last && __n_last_bkt != __bkt)
2024 _M_buckets[__n_last_bkt] = __prev_n;
2025 __prev_n->_M_nxt = __n_last;
2029 template<
typename _Key,
typename _Value,
2030 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2031 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2034 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2035 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2036 erase(const_iterator __first, const_iterator __last)
2039 __node_type* __n = __first._M_cur;
2040 __node_type* __last_n = __last._M_cur;
2041 if (__n == __last_n)
2042 return iterator(__n);
2044 std::size_t __bkt = _M_bucket_index(__n);
2046 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
2047 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2048 std::size_t __n_bkt = __bkt;
2053 __node_type* __tmp = __n;
2054 __n = __n->_M_next();
2055 this->_M_deallocate_node(__tmp);
2059 __n_bkt = _M_bucket_index(__n);
2061 while (__n != __last_n && __n_bkt == __bkt);
2062 if (__is_bucket_begin)
2063 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2064 if (__n == __last_n)
2066 __is_bucket_begin =
true;
2070 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
2071 _M_buckets[__n_bkt] = __prev_n;
2072 __prev_n->_M_nxt = __n;
2073 return iterator(__n);
2076 template<
typename _Key,
typename _Value,
2077 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2078 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2081 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2082 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2085 this->_M_deallocate_nodes(_M_begin());
2086 __builtin_memset(_M_buckets, 0, _M_bucket_count *
sizeof(__bucket_type));
2087 _M_element_count = 0;
2088 _M_before_begin._M_nxt =
nullptr;
2091 template<
typename _Key,
typename _Value,
2092 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2093 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2096 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2097 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2098 rehash(size_type __bkt_count)
2100 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
2102 =
std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
2104 __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count);
2106 if (__bkt_count != _M_bucket_count)
2107 _M_rehash(__bkt_count, __saved_state);
2111 _M_rehash_policy._M_reset(__saved_state);
2114 template<
typename _Key,
typename _Value,
2115 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2116 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2119 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2120 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2121 _M_rehash(size_type __bkt_count,
const __rehash_state& __state)
2125 _M_rehash_aux(__bkt_count, __unique_keys());
2131 _M_rehash_policy._M_reset(__state);
2132 __throw_exception_again;
2137 template<
typename _Key,
typename _Value,
2138 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2139 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2142 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2143 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2144 _M_rehash_aux(size_type __bkt_count,
true_type)
2146 __bucket_type* __new_buckets = _M_allocate_buckets(__bkt_count);
2147 __node_type* __p = _M_begin();
2148 _M_before_begin._M_nxt =
nullptr;
2149 std::size_t __bbegin_bkt = 0;
2152 __node_type* __next = __p->_M_next();
2154 = __hash_code_base::_M_bucket_index(__p, __bkt_count);
2155 if (!__new_buckets[__bkt])
2157 __p->_M_nxt = _M_before_begin._M_nxt;
2158 _M_before_begin._M_nxt = __p;
2159 __new_buckets[__bkt] = &_M_before_begin;
2161 __new_buckets[__bbegin_bkt] = __p;
2162 __bbegin_bkt = __bkt;
2166 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2167 __new_buckets[__bkt]->_M_nxt = __p;
2172 _M_deallocate_buckets();
2173 _M_bucket_count = __bkt_count;
2174 _M_buckets = __new_buckets;
2179 template<
typename _Key,
typename _Value,
2180 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2181 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2184 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2185 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2186 _M_rehash_aux(size_type __bkt_count,
false_type)
2188 __bucket_type* __new_buckets = _M_allocate_buckets(__bkt_count);
2190 __node_type* __p = _M_begin();
2191 _M_before_begin._M_nxt =
nullptr;
2192 std::size_t __bbegin_bkt = 0;
2193 std::size_t __prev_bkt = 0;
2194 __node_type* __prev_p =
nullptr;
2195 bool __check_bucket =
false;
2199 __node_type* __next = __p->_M_next();
2201 = __hash_code_base::_M_bucket_index(__p, __bkt_count);
2203 if (__prev_p && __prev_bkt == __bkt)
2208 __p->_M_nxt = __prev_p->_M_nxt;
2209 __prev_p->_M_nxt = __p;
2216 __check_bucket =
true;
2224 if (__prev_p->_M_nxt)
2226 std::size_t __next_bkt
2227 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
2229 if (__next_bkt != __prev_bkt)
2230 __new_buckets[__next_bkt] = __prev_p;
2232 __check_bucket =
false;
2235 if (!__new_buckets[__bkt])
2237 __p->_M_nxt = _M_before_begin._M_nxt;
2238 _M_before_begin._M_nxt = __p;
2239 __new_buckets[__bkt] = &_M_before_begin;
2241 __new_buckets[__bbegin_bkt] = __p;
2242 __bbegin_bkt = __bkt;
2246 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2247 __new_buckets[__bkt]->_M_nxt = __p;
2255 if (__check_bucket && __prev_p->_M_nxt)
2257 std::size_t __next_bkt
2258 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
2260 if (__next_bkt != __prev_bkt)
2261 __new_buckets[__next_bkt] = __prev_p;
2264 _M_deallocate_buckets();
2265 _M_bucket_count = __bkt_count;
2266 _M_buckets = __new_buckets;
2269 #if __cplusplus > 201402L
2270 template<
typename,
typename,
typename>
class _Hash_merge_helper { };
2273 #if __cpp_deduction_guides >= 201606
2275 template<
typename _Hash>
2276 using _RequireNotAllocatorOrIntegral
2277 = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
2280 _GLIBCXX_END_NAMESPACE_VERSION
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
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.
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.
Define a member typedef type to one of two argument types.
is_nothrow_copy_constructible
is_nothrow_move_assignable
A mixin helper to conditionally enable or disable the default constructor.
Node iterators, used to iterate through all the hashtable.
Node const_iterators, used to iterate through all the hashtable.
Struct holding two objects of arbitrary type.
_T1 first
The first member.
_T2 second
The second member.
Uniform interface to C++98 and C++11 allocators.