31 #define _HASHTABLE_H 1
33 #pragma GCC system_header
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 template<
typename _Tp,
typename _Hash>
44 __is_fast_hash<_Hash>,
46 __detail::__is_noexcept_hash<_Tp, _Hash>>>;
166 template<
typename _Key,
typename _Value,
typename _Alloc,
167 typename _ExtractKey,
typename _Equal,
168 typename _H1,
typename _H2,
typename _Hash,
169 typename _RehashPolicy,
typename _Traits>
172 _H1, _H2, _Hash, _Traits>,
174 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
176 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
178 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
180 _H1, _H2, _Hash, _RehashPolicy, _Traits>,
182 typename __alloctr_rebind<_Alloc,
183 __detail::_Hash_node<_Value,
184 _Traits::__hash_cached::value> >::__type>
186 using __traits_type = _Traits;
187 using __hash_cached =
typename __traits_type::__hash_cached;
189 using __node_alloc_type =
190 typename __alloctr_rebind<_Alloc, __node_type>::__type;
194 using __value_alloc_traits =
196 using __node_alloc_traits =
202 typedef _Key key_type;
203 typedef _Value value_type;
204 typedef _Alloc allocator_type;
205 typedef _Equal key_equal;
209 typedef typename __value_alloc_traits::pointer pointer;
210 typedef typename __value_alloc_traits::const_pointer const_pointer;
211 typedef value_type& reference;
212 typedef const value_type& const_reference;
215 using __rehash_type = _RehashPolicy;
216 using __rehash_state =
typename __rehash_type::_State;
218 using __constant_iterators =
typename __traits_type::__constant_iterators;
219 using __unique_keys =
typename __traits_type::__unique_keys;
221 using __key_extract =
typename std::conditional<
222 __constant_iterators::value,
224 __detail::_Select1st>::type;
228 _Equal, _H1, _H2, _Hash, _Traits>;
231 using __hash_code =
typename __hashtable_base::__hash_code;
232 using __ireturn_type =
typename __hashtable_base::__ireturn_type;
235 _Equal, _H1, _H2, _Hash,
236 _RehashPolicy, _Traits>;
241 _RehashPolicy, _Traits>;
244 _Equal, _H1, _H2, _Hash,
245 _RehashPolicy, _Traits>;
247 using __reuse_or_alloc_node_type =
248 __detail::_ReuseOrAllocNode<__node_alloc_type>;
251 template<
typename _Cond>
252 using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
254 template<
typename _Cond>
255 using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
261 struct __hash_code_base_access : __hash_code_base
262 {
using __hash_code_base::_M_bucket_index; };
266 static_assert(noexcept(declval<const __hash_code_base_access&>()
269 "Cache the hash code or qualify your functors involved"
270 " in hash code and bucket index computation with noexcept");
277 static_assert(__if_hash_cached<is_default_constructible<_H2>>::value,
278 "Functor used to map hash code to bucket index"
279 " must be default constructible");
281 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
282 typename _ExtractKeya,
typename _Equala,
283 typename _H1a,
typename _H2a,
typename _Hasha,
284 typename _RehashPolicya,
typename _Traitsa,
288 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
289 typename _ExtractKeya,
typename _Equala,
290 typename _H1a,
typename _H2a,
typename _Hasha,
291 typename _RehashPolicya,
typename _Traitsa>
294 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
295 typename _ExtractKeya,
typename _Equala,
296 typename _H1a,
typename _H2a,
typename _Hasha,
297 typename _RehashPolicya,
typename _Traitsa,
298 bool _Constant_iteratorsa,
bool _Unique_keysa>
302 using size_type =
typename __hashtable_base::size_type;
303 using difference_type =
typename __hashtable_base::difference_type;
313 __bucket_type* _M_buckets;
314 size_type _M_bucket_count;
315 __node_base _M_before_begin;
316 size_type _M_element_count;
317 _RehashPolicy _M_rehash_policy;
325 __bucket_type _M_single_bucket;
328 _M_uses_single_bucket(__bucket_type* __bkts)
const
329 {
return __builtin_expect(__bkts == &_M_single_bucket,
false); }
332 _M_uses_single_bucket()
const
333 {
return _M_uses_single_bucket(_M_buckets); }
336 _M_base_alloc() {
return *
this; }
339 _M_allocate_buckets(size_type __n)
341 if (__builtin_expect(__n == 1,
false))
343 _M_single_bucket =
nullptr;
344 return &_M_single_bucket;
347 return __hashtable_alloc::_M_allocate_buckets(__n);
351 _M_deallocate_buckets(__bucket_type* __bkts, size_type __n)
353 if (_M_uses_single_bucket(__bkts))
356 __hashtable_alloc::_M_deallocate_buckets(__bkts, __n);
360 _M_deallocate_buckets()
361 { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
366 _M_bucket_begin(size_type __bkt)
const;
370 {
return static_cast<__node_type*
>(_M_before_begin._M_nxt); }
372 template<
typename _NodeGenerator>
374 _M_assign(
const _Hashtable&,
const _NodeGenerator&);
380 _M_move_assign(
_Hashtable&&, std::false_type);
388 const _H1&,
const _H2&,
const _Hash&,
389 const _Equal&,
const _ExtractKey&,
390 const allocator_type&);
392 template<
typename _InputIterator>
393 _Hashtable(_InputIterator __first, _InputIterator __last,
394 size_type __bucket_hint,
395 const _H1&,
const _H2&,
const _Hash&,
396 const _Equal&,
const _ExtractKey&,
397 const allocator_type&);
410 :
_Hashtable(10, _H1(), _H2(), _Hash(), key_equal(),
411 __key_extract(), __a)
416 const _H1& __hf = _H1(),
417 const key_equal& __eql = key_equal(),
418 const allocator_type& __a = allocator_type())
419 :
_Hashtable(__n, __hf, _H2(), _Hash(), __eql,
420 __key_extract(), __a)
423 template<
typename _InputIterator>
424 _Hashtable(_InputIterator __f, _InputIterator __l,
426 const _H1& __hf = _H1(),
427 const key_equal& __eql = key_equal(),
428 const allocator_type& __a = allocator_type())
429 :
_Hashtable(__f, __l, __n, __hf, _H2(), _Hash(), __eql,
430 __key_extract(), __a)
435 const _H1& __hf = _H1(),
436 const key_equal& __eql = key_equal(),
437 const allocator_type& __a = allocator_type())
438 :
_Hashtable(__l.begin(), __l.end(), __n, __hf, _H2(), _Hash(), __eql,
439 __key_extract(), __a)
447 noexcept(__node_alloc_traits::_S_nothrow_move())
449 constexpr
bool __move_storage =
450 __node_alloc_traits::_S_propagate_on_move_assign()
451 || __node_alloc_traits::_S_always_equal();
453 integral_constant<bool, __move_storage>());
458 operator=(initializer_list<value_type> __l)
460 __reuse_or_alloc_node_type __roan(_M_begin(), *
this);
461 _M_before_begin._M_nxt =
nullptr;
463 this->_M_insert_range(__l.begin(), __l.end(), __roan);
471 noexcept(__node_alloc_traits::_S_nothrow_swap());
479 begin()
const noexcept
491 cbegin()
const noexcept
495 cend()
const noexcept
499 size()
const noexcept
500 {
return _M_element_count; }
503 empty()
const noexcept
504 {
return size() == 0; }
507 get_allocator()
const noexcept
508 {
return allocator_type(this->_M_node_allocator()); }
511 max_size()
const noexcept
512 {
return __node_alloc_traits::max_size(this->_M_node_allocator()); }
517 {
return this->_M_eq(); }
523 bucket_count()
const noexcept
524 {
return _M_bucket_count; }
527 max_bucket_count()
const noexcept
528 {
return max_size(); }
531 bucket_size(size_type __n)
const
535 bucket(
const key_type& __k)
const
536 {
return _M_bucket_index(__k, this->_M_hash_code(__k)); }
542 __n, _M_bucket_count);
550 begin(size_type __n)
const
553 __n, _M_bucket_count);
557 end(size_type __n)
const
562 cbegin(size_type __n)
const
565 __n, _M_bucket_count);
569 cend(size_type __n)
const
573 load_factor()
const noexcept
575 return static_cast<float>(size()) / static_cast<float>(bucket_count());
584 __rehash_policy()
const
585 {
return _M_rehash_policy; }
588 __rehash_policy(
const _RehashPolicy&);
592 find(
const key_type& __k);
595 find(
const key_type& __k)
const;
598 count(
const key_type& __k)
const;
601 equal_range(
const key_type& __k);
604 equal_range(
const key_type& __k)
const;
610 {
return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
613 _M_bucket_index(
const key_type& __k, __hash_code __c)
const
614 {
return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
619 _M_find_before_node(size_type,
const key_type&, __hash_code)
const;
622 _M_find_node(size_type __bkt,
const key_type& __key,
623 __hash_code __c)
const
625 __node_base* __before_n = _M_find_before_node(__bkt, __key, __c);
627 return static_cast<__node_type*
>(__before_n->_M_nxt);
637 _M_remove_bucket_begin(size_type __bkt,
__node_type* __next_n,
638 size_type __next_bkt);
642 _M_get_previous_node(size_type __bkt, __node_base* __n);
648 _M_insert_unique_node(size_type __bkt, __hash_code __code,
657 template<
typename... _Args>
659 _M_emplace(std::true_type, _Args&&... __args);
661 template<
typename... _Args>
663 _M_emplace(std::false_type __uk, _Args&&... __args)
664 {
return _M_emplace(cend(), __uk, std::forward<_Args>(__args)...); }
667 template<
typename... _Args>
669 _M_emplace(
const_iterator, std::true_type __uk, _Args&&... __args)
670 {
return _M_emplace(__uk, std::forward<_Args>(__args)...).first; }
672 template<
typename... _Args>
676 template<
typename _Arg,
typename _NodeGenerator>
678 _M_insert(_Arg&&,
const _NodeGenerator&, std::true_type);
680 template<
typename _Arg,
typename _NodeGenerator>
682 _M_insert(_Arg&& __arg,
const _NodeGenerator& __node_gen,
683 std::false_type __uk)
685 return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
690 template<
typename _Arg,
typename _NodeGenerator>
692 _M_insert(
const_iterator, _Arg&& __arg,
const _NodeGenerator& __node_gen,
696 _M_insert(std::forward<_Arg>(__arg), __node_gen, __uk).
first;
700 template<
typename _Arg,
typename _NodeGenerator>
702 _M_insert(
const_iterator, _Arg&&,
const _NodeGenerator&, std::false_type);
705 _M_erase(std::true_type,
const key_type&);
708 _M_erase(std::false_type,
const key_type&);
711 _M_erase(size_type __bkt, __node_base* __prev_n,
__node_type* __n);
715 template<
typename... _Args>
717 emplace(_Args&&... __args)
718 {
return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
720 template<
typename... _Args>
724 return _M_emplace(__hint, __unique_keys(),
725 std::forward<_Args>(__args)...);
740 erase(
const key_type& __k)
741 {
return _M_erase(__unique_keys(), __k); }
750 void rehash(size_type __n);
757 void _M_rehash_aux(size_type __n, std::true_type);
760 void _M_rehash_aux(size_type __n, std::false_type);
764 void _M_rehash(size_type __n,
const __rehash_state& __state);
769 template<
typename _Key,
typename _Value,
770 typename _Alloc,
typename _ExtractKey,
typename _Equal,
771 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
773 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
774 _Equal, _H1, _H2, _Hash, _RehashPolicy,
775 _Traits>::__node_type*
776 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
777 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
778 _M_bucket_begin(size_type __bkt)
const
780 __node_base* __n = _M_buckets[__bkt];
781 return __n ?
static_cast<__node_type*
>(__n->_M_nxt) :
nullptr;
784 template<
typename _Key,
typename _Value,
785 typename _Alloc,
typename _ExtractKey,
typename _Equal,
786 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
788 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
789 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
790 _Hashtable(size_type __bucket_hint,
791 const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
792 const _Equal& __eq,
const _ExtractKey& __exk,
793 const allocator_type& __a)
794 : __hashtable_base(__exk, __h1, __h2, __h, __eq),
797 __hashtable_alloc(__node_alloc_type(__a)),
801 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
802 _M_buckets = _M_allocate_buckets(_M_bucket_count);
805 template<
typename _Key,
typename _Value,
806 typename _Alloc,
typename _ExtractKey,
typename _Equal,
807 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
809 template<
typename _InputIterator>
810 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
811 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
812 _Hashtable(_InputIterator __f, _InputIterator __l,
813 size_type __bucket_hint,
814 const _H1& __h1,
const _H2& __h2,
const _Hash& __h,
815 const _Equal& __eq,
const _ExtractKey& __exk,
816 const allocator_type& __a)
817 : __hashtable_base(__exk, __h1, __h2, __h, __eq),
820 __hashtable_alloc(__node_alloc_type(__a)),
824 auto __nb_elems = __detail::__distance_fw(__f, __l);
826 _M_rehash_policy._M_next_bkt(
827 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
830 _M_buckets = _M_allocate_buckets(_M_bucket_count);
833 for (; __f != __l; ++__f)
839 _M_deallocate_buckets();
840 __throw_exception_again;
844 template<
typename _Key,
typename _Value,
845 typename _Alloc,
typename _ExtractKey,
typename _Equal,
846 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
848 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
849 _H1, _H2, _Hash, _RehashPolicy, _Traits>&
850 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
851 _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(
852 const _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
853 _H1, _H2, _Hash, _RehashPolicy, _Traits>& __ht)
858 if (__node_alloc_traits::_S_propagate_on_copy_assign())
860 auto& __this_alloc = this->_M_node_allocator();
861 auto& __that_alloc = __ht._M_node_allocator();
862 if (!__node_alloc_traits::_S_always_equal()
863 && __this_alloc != __that_alloc)
866 this->_M_deallocate_nodes(_M_begin());
867 _M_before_begin._M_nxt =
nullptr;
868 _M_deallocate_buckets();
869 _M_buckets =
nullptr;
870 std::__alloc_on_copy(__this_alloc, __that_alloc);
871 __hashtable_base::operator=(__ht);
872 _M_bucket_count = __ht._M_bucket_count;
873 _M_element_count = __ht._M_element_count;
874 _M_rehash_policy = __ht._M_rehash_policy;
878 [
this](
const __node_type* __n)
879 {
return this->_M_allocate_node(__n->_M_v()); });
886 __throw_exception_again;
890 std::__alloc_on_copy(__this_alloc, __that_alloc);
894 __bucket_type* __former_buckets =
nullptr;
895 std::size_t __former_bucket_count = _M_bucket_count;
896 const __rehash_state& __former_state = _M_rehash_policy._M_state();
898 if (_M_bucket_count != __ht._M_bucket_count)
900 __former_buckets = _M_buckets;
901 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
902 _M_bucket_count = __ht._M_bucket_count;
905 __builtin_memset(_M_buckets, 0,
906 _M_bucket_count *
sizeof(__bucket_type));
910 __hashtable_base::operator=(__ht);
911 _M_element_count = __ht._M_element_count;
912 _M_rehash_policy = __ht._M_rehash_policy;
913 __reuse_or_alloc_node_type __roan(_M_begin(), *
this);
914 _M_before_begin._M_nxt =
nullptr;
916 [&__roan](
const __node_type* __n)
917 {
return __roan(__n->_M_v()); });
918 if (__former_buckets)
919 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
923 if (__former_buckets)
926 _M_deallocate_buckets();
927 _M_rehash_policy._M_reset(__former_state);
928 _M_buckets = __former_buckets;
929 _M_bucket_count = __former_bucket_count;
931 __builtin_memset(_M_buckets, 0,
932 _M_bucket_count *
sizeof(__bucket_type));
933 __throw_exception_again;
938 template<
typename _Key,
typename _Value,
939 typename _Alloc,
typename _ExtractKey,
typename _Equal,
940 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
942 template<
typename _NodeGenerator>
944 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
945 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
946 _M_assign(
const _Hashtable& __ht,
const _NodeGenerator& __node_gen)
948 __bucket_type* __buckets =
nullptr;
950 _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
954 if (!__ht._M_before_begin._M_nxt)
959 __node_type* __ht_n = __ht._M_begin();
960 __node_type* __this_n = __node_gen(__ht_n);
961 this->_M_copy_code(__this_n, __ht_n);
962 _M_before_begin._M_nxt = __this_n;
963 _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
966 __node_base* __prev_n = __this_n;
967 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
969 __this_n = __node_gen(__ht_n);
970 __prev_n->_M_nxt = __this_n;
971 this->_M_copy_code(__this_n, __ht_n);
972 size_type __bkt = _M_bucket_index(__this_n);
973 if (!_M_buckets[__bkt])
974 _M_buckets[__bkt] = __prev_n;
982 _M_deallocate_buckets();
983 __throw_exception_again;
987 template<
typename _Key,
typename _Value,
988 typename _Alloc,
typename _ExtractKey,
typename _Equal,
989 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
992 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
993 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
996 _M_rehash_policy._M_reset();
998 _M_single_bucket =
nullptr;
999 _M_buckets = &_M_single_bucket;
1000 _M_before_begin._M_nxt =
nullptr;
1001 _M_element_count = 0;
1004 template<
typename _Key,
typename _Value,
1005 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1006 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1009 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1010 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1011 _M_move_assign(_Hashtable&& __ht, std::true_type)
1013 this->_M_deallocate_nodes(_M_begin());
1014 _M_deallocate_buckets();
1015 __hashtable_base::operator=(
std::move(__ht));
1016 _M_rehash_policy = __ht._M_rehash_policy;
1017 if (!__ht._M_uses_single_bucket())
1018 _M_buckets = __ht._M_buckets;
1021 _M_buckets = &_M_single_bucket;
1022 _M_single_bucket = __ht._M_single_bucket;
1024 _M_bucket_count = __ht._M_bucket_count;
1025 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1026 _M_element_count = __ht._M_element_count;
1027 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1032 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1036 template<
typename _Key,
typename _Value,
1037 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1038 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1041 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1042 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1043 _M_move_assign(_Hashtable&& __ht, std::false_type)
1045 if (__ht._M_node_allocator() == this->_M_node_allocator())
1046 _M_move_assign(
std::move(__ht), std::true_type());
1050 __bucket_type* __former_buckets =
nullptr;
1051 size_type __former_bucket_count = _M_bucket_count;
1052 const __rehash_state& __former_state = _M_rehash_policy._M_state();
1054 if (_M_bucket_count != __ht._M_bucket_count)
1056 __former_buckets = _M_buckets;
1057 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1058 _M_bucket_count = __ht._M_bucket_count;
1061 __builtin_memset(_M_buckets, 0,
1062 _M_bucket_count *
sizeof(__bucket_type));
1066 __hashtable_base::operator=(
std::move(__ht));
1067 _M_element_count = __ht._M_element_count;
1068 _M_rehash_policy = __ht._M_rehash_policy;
1069 __reuse_or_alloc_node_type __roan(_M_begin(), *
this);
1070 _M_before_begin._M_nxt =
nullptr;
1072 [&__roan](__node_type* __n)
1078 if (__former_buckets)
1080 _M_deallocate_buckets();
1081 _M_rehash_policy._M_reset(__former_state);
1082 _M_buckets = __former_buckets;
1083 _M_bucket_count = __former_bucket_count;
1085 __builtin_memset(_M_buckets, 0,
1086 _M_bucket_count *
sizeof(__bucket_type));
1087 __throw_exception_again;
1092 template<
typename _Key,
typename _Value,
1093 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1094 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1096 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1097 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1098 _Hashtable(
const _Hashtable& __ht)
1099 : __hashtable_base(__ht),
1101 __rehash_base(__ht),
1103 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1105 _M_bucket_count(__ht._M_bucket_count),
1106 _M_element_count(__ht._M_element_count),
1107 _M_rehash_policy(__ht._M_rehash_policy)
1110 [
this](
const __node_type* __n)
1111 {
return this->_M_allocate_node(__n->_M_v()); });
1114 template<
typename _Key,
typename _Value,
1115 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1116 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1118 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1119 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1120 _Hashtable(_Hashtable&& __ht) noexcept
1121 : __hashtable_base(__ht),
1123 __rehash_base(__ht),
1124 __hashtable_alloc(
std::
move(__ht._M_base_alloc())),
1125 _M_buckets(__ht._M_buckets),
1126 _M_bucket_count(__ht._M_bucket_count),
1127 _M_before_begin(__ht._M_before_begin._M_nxt),
1128 _M_element_count(__ht._M_element_count),
1129 _M_rehash_policy(__ht._M_rehash_policy)
1132 if (__ht._M_uses_single_bucket())
1134 _M_buckets = &_M_single_bucket;
1135 _M_single_bucket = __ht._M_single_bucket;
1141 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1146 template<
typename _Key,
typename _Value,
1147 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1148 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1150 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1151 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1152 _Hashtable(
const _Hashtable& __ht,
const allocator_type& __a)
1153 : __hashtable_base(__ht),
1155 __rehash_base(__ht),
1156 __hashtable_alloc(__node_alloc_type(__a)),
1158 _M_bucket_count(__ht._M_bucket_count),
1159 _M_element_count(__ht._M_element_count),
1160 _M_rehash_policy(__ht._M_rehash_policy)
1163 [
this](
const __node_type* __n)
1164 {
return this->_M_allocate_node(__n->_M_v()); });
1167 template<
typename _Key,
typename _Value,
1168 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1169 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1171 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1172 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1173 _Hashtable(_Hashtable&& __ht,
const allocator_type& __a)
1174 : __hashtable_base(__ht),
1176 __rehash_base(__ht),
1177 __hashtable_alloc(__node_alloc_type(__a)),
1179 _M_bucket_count(__ht._M_bucket_count),
1180 _M_element_count(__ht._M_element_count),
1181 _M_rehash_policy(__ht._M_rehash_policy)
1183 if (__ht._M_node_allocator() == this->_M_node_allocator())
1185 if (__ht._M_uses_single_bucket())
1187 _M_buckets = &_M_single_bucket;
1188 _M_single_bucket = __ht._M_single_bucket;
1191 _M_buckets = __ht._M_buckets;
1193 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1197 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1203 [
this](__node_type* __n)
1205 return this->_M_allocate_node(
1212 template<
typename _Key,
typename _Value,
1213 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1214 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1216 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1217 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1218 ~_Hashtable() noexcept
1222 _M_deallocate_buckets();
1225 template<
typename _Key,
typename _Value,
1226 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1227 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1230 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1231 _H1, _H2, _Hash, _RehashPolicy, _Traits>
::
1232 swap(_Hashtable& __x)
1233 noexcept(__node_alloc_traits::_S_nothrow_swap())
1240 std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
1241 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1244 if (this->_M_uses_single_bucket())
1246 if (!__x._M_uses_single_bucket())
1248 _M_buckets = __x._M_buckets;
1249 __x._M_buckets = &__x._M_single_bucket;
1252 else if (__x._M_uses_single_bucket())
1254 __x._M_buckets = _M_buckets;
1255 _M_buckets = &_M_single_bucket;
1260 std::swap(_M_bucket_count, __x._M_bucket_count);
1261 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1262 std::swap(_M_element_count, __x._M_element_count);
1263 std::swap(_M_single_bucket, __x._M_single_bucket);
1268 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1271 __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
1272 = &__x._M_before_begin;
1275 template<
typename _Key,
typename _Value,
1276 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1277 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1280 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1281 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1282 __rehash_policy(
const _RehashPolicy& __pol)
1285 __pol._M_need_rehash(_M_bucket_count, _M_element_count, 0);
1286 if (__do_rehash.first)
1287 _M_rehash(__do_rehash.second, _M_rehash_policy._M_state());
1288 _M_rehash_policy = __pol;
1291 template<
typename _Key,
typename _Value,
1292 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1293 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1295 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1296 _H1, _H2, _Hash, _RehashPolicy,
1298 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1299 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1300 find(
const key_type& __k)
1302 __hash_code __code = this->_M_hash_code(__k);
1303 std::size_t __n = _M_bucket_index(__k, __code);
1304 __node_type* __p = _M_find_node(__n, __k, __code);
1305 return __p ? iterator(__p) :
end();
1308 template<
typename _Key,
typename _Value,
1309 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1310 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1312 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1313 _H1, _H2, _Hash, _RehashPolicy,
1314 _Traits>::const_iterator
1315 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1316 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1317 find(
const key_type& __k)
const
1319 __hash_code __code = this->_M_hash_code(__k);
1320 std::size_t __n = _M_bucket_index(__k, __code);
1321 __node_type* __p = _M_find_node(__n, __k, __code);
1322 return __p ? const_iterator(__p) :
end();
1325 template<
typename _Key,
typename _Value,
1326 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1327 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1329 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1330 _H1, _H2, _Hash, _RehashPolicy,
1332 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1333 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1334 count(
const key_type& __k)
const
1336 __hash_code __code = this->_M_hash_code(__k);
1337 std::size_t __n = _M_bucket_index(__k, __code);
1338 __node_type* __p = _M_bucket_begin(__n);
1342 std::size_t __result = 0;
1343 for (;; __p = __p->_M_next())
1345 if (this->_M_equals(__k, __code, __p))
1352 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1358 template<
typename _Key,
typename _Value,
1359 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1360 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1362 std::pair<
typename _Hashtable<_Key, _Value, _Alloc,
1363 _ExtractKey, _Equal, _H1,
1364 _H2, _Hash, _RehashPolicy,
1366 typename _Hashtable<_Key, _Value, _Alloc,
1367 _ExtractKey, _Equal, _H1,
1368 _H2, _Hash, _RehashPolicy,
1370 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1371 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1372 equal_range(
const key_type& __k)
1374 __hash_code __code = this->_M_hash_code(__k);
1375 std::size_t __n = _M_bucket_index(__k, __code);
1376 __node_type* __p = _M_find_node(__n, __k, __code);
1380 __node_type* __p1 = __p->_M_next();
1381 while (__p1 && _M_bucket_index(__p1) == __n
1382 && this->_M_equals(__k, __code, __p1))
1383 __p1 = __p1->_M_next();
1391 template<
typename _Key,
typename _Value,
1392 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1393 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1395 std::pair<
typename _Hashtable<_Key, _Value, _Alloc,
1396 _ExtractKey, _Equal, _H1,
1397 _H2, _Hash, _RehashPolicy,
1398 _Traits>::const_iterator,
1399 typename _Hashtable<_Key, _Value, _Alloc,
1400 _ExtractKey, _Equal, _H1,
1401 _H2, _Hash, _RehashPolicy,
1402 _Traits>::const_iterator>
1403 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1404 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1405 equal_range(
const key_type& __k)
const
1407 __hash_code __code = this->_M_hash_code(__k);
1408 std::size_t __n = _M_bucket_index(__k, __code);
1409 __node_type* __p = _M_find_node(__n, __k, __code);
1413 __node_type* __p1 = __p->_M_next();
1414 while (__p1 && _M_bucket_index(__p1) == __n
1415 && this->_M_equals(__k, __code, __p1))
1416 __p1 = __p1->_M_next();
1418 return std::make_pair(const_iterator(__p), const_iterator(__p1));
1426 template<
typename _Key,
typename _Value,
1427 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1428 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1430 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1431 _Equal, _H1, _H2, _Hash, _RehashPolicy,
1432 _Traits>::__node_base*
1433 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1434 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1435 _M_find_before_node(size_type __n,
const key_type& __k,
1436 __hash_code __code)
const
1438 __node_base* __prev_p = _M_buckets[__n];
1442 for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
1443 __p = __p->_M_next())
1445 if (this->_M_equals(__k, __code, __p))
1448 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1455 template<
typename _Key,
typename _Value,
1456 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1457 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1460 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1461 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1462 _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
1464 if (_M_buckets[__bkt])
1468 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1469 _M_buckets[__bkt]->_M_nxt = __node;
1476 __node->_M_nxt = _M_before_begin._M_nxt;
1477 _M_before_begin._M_nxt = __node;
1481 _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
1482 _M_buckets[__bkt] = &_M_before_begin;
1486 template<
typename _Key,
typename _Value,
1487 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1488 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1491 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1492 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1493 _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
1494 size_type __next_bkt)
1496 if (!__next || __next_bkt != __bkt)
1501 _M_buckets[__next_bkt] = _M_buckets[__bkt];
1504 if (&_M_before_begin == _M_buckets[__bkt])
1505 _M_before_begin._M_nxt = __next;
1506 _M_buckets[__bkt] =
nullptr;
1510 template<
typename _Key,
typename _Value,
1511 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1512 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1514 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1515 _Equal, _H1, _H2, _Hash, _RehashPolicy,
1516 _Traits>::__node_base*
1517 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1518 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1519 _M_get_previous_node(size_type __bkt, __node_base* __n)
1521 __node_base* __prev_n = _M_buckets[__bkt];
1522 while (__prev_n->_M_nxt != __n)
1523 __prev_n = __prev_n->_M_nxt;
1527 template<
typename _Key,
typename _Value,
1528 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1529 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1531 template<
typename... _Args>
1532 std::pair<
typename _Hashtable<_Key, _Value, _Alloc,
1533 _ExtractKey, _Equal, _H1,
1534 _H2, _Hash, _RehashPolicy,
1535 _Traits>::iterator,
bool>
1536 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1537 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1538 _M_emplace(std::true_type, _Args&&... __args)
1541 __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
1542 const key_type& __k = this->_M_extract()(__node->_M_v());
1546 __code = this->_M_hash_code(__k);
1550 this->_M_deallocate_node(__node);
1551 __throw_exception_again;
1554 size_type __bkt = _M_bucket_index(__k, __code);
1555 if (__node_type* __p = _M_find_node(__bkt, __k, __code))
1558 this->_M_deallocate_node(__node);
1563 return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
1567 template<
typename _Key,
typename _Value,
1568 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1569 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1571 template<
typename... _Args>
1572 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1573 _H1, _H2, _Hash, _RehashPolicy,
1575 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1576 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1577 _M_emplace(const_iterator __hint, std::false_type, _Args&&... __args)
1580 __node_type* __node =
1581 this->_M_allocate_node(std::forward<_Args>(__args)...);
1586 __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
1590 this->_M_deallocate_node(__node);
1591 __throw_exception_again;
1594 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1597 template<
typename _Key,
typename _Value,
1598 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1599 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1601 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1602 _H1, _H2, _Hash, _RehashPolicy,
1604 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1605 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1606 _M_insert_unique_node(size_type __bkt, __hash_code __code,
1607 __node_type* __node)
1609 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1611 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1615 if (__do_rehash.
first)
1617 _M_rehash(__do_rehash.
second, __saved_state);
1618 __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
1621 this->_M_store_code(__node, __code);
1624 _M_insert_bucket_begin(__bkt, __node);
1626 return iterator(__node);
1630 this->_M_deallocate_node(__node);
1631 __throw_exception_again;
1637 template<
typename _Key,
typename _Value,
1638 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1639 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1641 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1642 _H1, _H2, _Hash, _RehashPolicy,
1644 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1645 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1646 _M_insert_multi_node(__node_type* __hint, __hash_code __code,
1647 __node_type* __node)
1649 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1651 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1655 if (__do_rehash.
first)
1656 _M_rehash(__do_rehash.
second, __saved_state);
1658 this->_M_store_code(__node, __code);
1659 const key_type& __k = this->_M_extract()(__node->_M_v());
1660 size_type __bkt = _M_bucket_index(__k, __code);
1665 = __builtin_expect(__hint !=
nullptr,
false)
1666 && this->_M_equals(__k, __code, __hint)
1668 : _M_find_before_node(__bkt, __k, __code);
1672 __node->_M_nxt = __prev->_M_nxt;
1673 __prev->_M_nxt = __node;
1674 if (__builtin_expect(__prev == __hint,
false))
1678 && !this->_M_equals(__k, __code, __node->_M_next()))
1680 size_type __next_bkt = _M_bucket_index(__node->_M_next());
1681 if (__next_bkt != __bkt)
1682 _M_buckets[__next_bkt] = __node;
1690 _M_insert_bucket_begin(__bkt, __node);
1692 return iterator(__node);
1696 this->_M_deallocate_node(__node);
1697 __throw_exception_again;
1702 template<
typename _Key,
typename _Value,
1703 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1704 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1706 template<
typename _Arg,
typename _NodeGenerator>
1707 std::pair<
typename _Hashtable<_Key, _Value, _Alloc,
1708 _ExtractKey, _Equal, _H1,
1709 _H2, _Hash, _RehashPolicy,
1710 _Traits>::iterator,
bool>
1711 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1712 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1713 _M_insert(_Arg&& __v,
const _NodeGenerator& __node_gen, std::true_type)
1715 const key_type& __k = this->_M_extract()(__v);
1716 __hash_code __code = this->_M_hash_code(__k);
1717 size_type __bkt = _M_bucket_index(__k, __code);
1719 __node_type* __n = _M_find_node(__bkt, __k, __code);
1723 __n = __node_gen(std::forward<_Arg>(__v));
1724 return std::make_pair(_M_insert_unique_node(__bkt, __code, __n),
true);
1728 template<
typename _Key,
typename _Value,
1729 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1730 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1732 template<
typename _Arg,
typename _NodeGenerator>
1733 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1734 _H1, _H2, _Hash, _RehashPolicy,
1736 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1737 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1738 _M_insert(const_iterator __hint, _Arg&& __v,
1739 const _NodeGenerator& __node_gen,
1744 __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
1747 __node_type* __node = __node_gen(std::forward<_Arg>(__v));
1749 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1752 template<
typename _Key,
typename _Value,
1753 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1754 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1756 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1757 _H1, _H2, _Hash, _RehashPolicy,
1759 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1760 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1761 erase(const_iterator __it)
1763 __node_type* __n = __it._M_cur;
1764 std::size_t __bkt = _M_bucket_index(__n);
1769 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1770 return _M_erase(__bkt, __prev_n, __n);
1773 template<
typename _Key,
typename _Value,
1774 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1775 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1777 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1778 _H1, _H2, _Hash, _RehashPolicy,
1780 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1781 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1782 _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
1784 if (__prev_n == _M_buckets[__bkt])
1785 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1786 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
1787 else if (__n->_M_nxt)
1789 size_type __next_bkt = _M_bucket_index(__n->_M_next());
1790 if (__next_bkt != __bkt)
1791 _M_buckets[__next_bkt] = __prev_n;
1794 __prev_n->_M_nxt = __n->_M_nxt;
1795 iterator __result(__n->_M_next());
1796 this->_M_deallocate_node(__n);
1802 template<
typename _Key,
typename _Value,
1803 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1804 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1806 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1807 _H1, _H2, _Hash, _RehashPolicy,
1809 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1810 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1811 _M_erase(std::true_type,
const key_type& __k)
1813 __hash_code __code = this->_M_hash_code(__k);
1814 std::size_t __bkt = _M_bucket_index(__k, __code);
1817 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1822 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1823 _M_erase(__bkt, __prev_n, __n);
1827 template<
typename _Key,
typename _Value,
1828 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1829 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1831 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1832 _H1, _H2, _Hash, _RehashPolicy,
1834 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1835 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1836 _M_erase(std::false_type,
const key_type& __k)
1838 __hash_code __code = this->_M_hash_code(__k);
1839 std::size_t __bkt = _M_bucket_index(__k, __code);
1842 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1852 __node_type* __n =
static_cast<__node_type*
>(__prev_n->_M_nxt);
1853 __node_type* __n_last = __n;
1854 std::size_t __n_last_bkt = __bkt;
1857 __n_last = __n_last->_M_next();
1860 __n_last_bkt = _M_bucket_index(__n_last);
1862 while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
1865 size_type __result = 0;
1868 __node_type* __p = __n->_M_next();
1869 this->_M_deallocate_node(__n);
1874 while (__n != __n_last);
1876 if (__prev_n == _M_buckets[__bkt])
1877 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
1878 else if (__n_last && __n_last_bkt != __bkt)
1879 _M_buckets[__n_last_bkt] = __prev_n;
1880 __prev_n->_M_nxt = __n_last;
1884 template<
typename _Key,
typename _Value,
1885 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1886 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1888 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1889 _H1, _H2, _Hash, _RehashPolicy,
1891 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1892 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1893 erase(const_iterator __first, const_iterator __last)
1895 __node_type* __n = __first._M_cur;
1896 __node_type* __last_n = __last._M_cur;
1897 if (__n == __last_n)
1898 return iterator(__n);
1900 std::size_t __bkt = _M_bucket_index(__n);
1902 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1903 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
1904 std::size_t __n_bkt = __bkt;
1909 __node_type* __tmp = __n;
1910 __n = __n->_M_next();
1911 this->_M_deallocate_node(__tmp);
1915 __n_bkt = _M_bucket_index(__n);
1917 while (__n != __last_n && __n_bkt == __bkt);
1918 if (__is_bucket_begin)
1919 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
1920 if (__n == __last_n)
1922 __is_bucket_begin =
true;
1926 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
1927 _M_buckets[__n_bkt] = __prev_n;
1928 __prev_n->_M_nxt = __n;
1929 return iterator(__n);
1932 template<
typename _Key,
typename _Value,
1933 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1934 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1937 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1938 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1941 this->_M_deallocate_nodes(_M_begin());
1942 __builtin_memset(_M_buckets, 0, _M_bucket_count *
sizeof(__bucket_type));
1943 _M_element_count = 0;
1944 _M_before_begin._M_nxt =
nullptr;
1947 template<
typename _Key,
typename _Value,
1948 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1949 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1952 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1953 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1954 rehash(size_type __n)
1956 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1957 std::size_t __buckets
1958 =
std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
1960 __buckets = _M_rehash_policy._M_next_bkt(__buckets);
1962 if (__buckets != _M_bucket_count)
1963 _M_rehash(__buckets, __saved_state);
1966 _M_rehash_policy._M_reset(__saved_state);
1969 template<
typename _Key,
typename _Value,
1970 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1971 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1974 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1975 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1976 _M_rehash(size_type __n,
const __rehash_state& __state)
1980 _M_rehash_aux(__n, __unique_keys());
1986 _M_rehash_policy._M_reset(__state);
1987 __throw_exception_again;
1992 template<
typename _Key,
typename _Value,
1993 typename _Alloc,
typename _ExtractKey,
typename _Equal,
1994 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
1997 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1998 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1999 _M_rehash_aux(size_type __n, std::true_type)
2001 __bucket_type* __new_buckets = _M_allocate_buckets(__n);
2002 __node_type* __p = _M_begin();
2003 _M_before_begin._M_nxt =
nullptr;
2004 std::size_t __bbegin_bkt = 0;
2007 __node_type* __next = __p->_M_next();
2008 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2009 if (!__new_buckets[__bkt])
2011 __p->_M_nxt = _M_before_begin._M_nxt;
2012 _M_before_begin._M_nxt = __p;
2013 __new_buckets[__bkt] = &_M_before_begin;
2015 __new_buckets[__bbegin_bkt] = __p;
2016 __bbegin_bkt = __bkt;
2020 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2021 __new_buckets[__bkt]->_M_nxt = __p;
2026 _M_deallocate_buckets();
2027 _M_bucket_count = __n;
2028 _M_buckets = __new_buckets;
2033 template<
typename _Key,
typename _Value,
2034 typename _Alloc,
typename _ExtractKey,
typename _Equal,
2035 typename _H1,
typename _H2,
typename _Hash,
typename _RehashPolicy,
2038 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2039 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2040 _M_rehash_aux(size_type __n, std::false_type)
2042 __bucket_type* __new_buckets = _M_allocate_buckets(__n);
2044 __node_type* __p = _M_begin();
2045 _M_before_begin._M_nxt =
nullptr;
2046 std::size_t __bbegin_bkt = 0;
2047 std::size_t __prev_bkt = 0;
2048 __node_type* __prev_p =
nullptr;
2049 bool __check_bucket =
false;
2053 __node_type* __next = __p->_M_next();
2054 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2056 if (__prev_p && __prev_bkt == __bkt)
2061 __p->_M_nxt = __prev_p->_M_nxt;
2062 __prev_p->_M_nxt = __p;
2069 __check_bucket =
true;
2077 if (__prev_p->_M_nxt)
2079 std::size_t __next_bkt
2080 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
2082 if (__next_bkt != __prev_bkt)
2083 __new_buckets[__next_bkt] = __prev_p;
2085 __check_bucket =
false;
2088 if (!__new_buckets[__bkt])
2090 __p->_M_nxt = _M_before_begin._M_nxt;
2091 _M_before_begin._M_nxt = __p;
2092 __new_buckets[__bkt] = &_M_before_begin;
2094 __new_buckets[__bbegin_bkt] = __p;
2095 __bbegin_bkt = __bkt;
2099 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2100 __new_buckets[__bkt]->_M_nxt = __p;
2108 if (__check_bucket && __prev_p->_M_nxt)
2110 std::size_t __next_bkt
2111 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
2112 if (__next_bkt != __prev_bkt)
2113 __new_buckets[__next_bkt] = __prev_p;
2116 _M_deallocate_buckets();
2117 _M_bucket_count = __n;
2118 _M_buckets = __new_buckets;
2121 _GLIBCXX_END_NAMESPACE_VERSION
2124 #endif // _HASHTABLE_H
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
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.
Uniform interface to C++98 and C++0x allocators.
auto end(_Container &__cont) -> decltype(__cont.end())
Return an iterator pointing to one past the last element of the container.
Uniform interface to all allocator types.
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
auto begin(_Container &__cont) -> decltype(__cont.begin())
Return an iterator pointing to the first element of the container.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
_T1 first
second_type is the second bound type
ISO C++ entities toplevel namespace is std.
Node const_iterators, used to iterate through all the hashtable.
Struct holding two objects of arbitrary type.
void swap(_Tp &, _Tp &) noexcept(__and_< is_nothrow_move_constructible< _Tp >, is_nothrow_move_assignable< _Tp >>::value)
Swaps two values.
_T2 second
first is a copy of the first object
Node iterators, used to iterate through all the hashtable.
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.