34#pragma GCC system_header
41#if __cplusplus > 201402L
45#pragma GCC diagnostic push
46#pragma GCC diagnostic ignored "-Wc++11-extensions"
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
53 template<
typename _Tp,
typename _Hash>
58 __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
63 template<
typename _Equal,
typename _Hash,
typename _Allocator>
64 using _Hashtable_enable_default_ctor
65 = _Enable_default_constructor<__and_<is_default_constructible<_Equal>,
68 __detail::_Hash_node_base>;
185 template<
typename _Key,
typename _Value,
typename _Alloc,
186 typename _ExtractKey,
typename _Equal,
187 typename _Hash,
typename _RangeHash,
typename _Unused,
188 typename _RehashPolicy,
typename _Traits>
190 :
public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
191 _Hash, _RangeHash, _Unused, _Traits>,
192 public __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
193 _Hash, _RangeHash, _Unused,
194 _RehashPolicy, _Traits>,
195 public __detail::_Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
196 _Hash, _RangeHash, _Unused,
197 _RehashPolicy, _Traits>,
198 private __detail::_Hashtable_alloc<
199 __alloc_rebind<_Alloc,
200 __detail::_Hash_node<_Value,
201 _Traits::__hash_cached::value>>>,
202 private _Hashtable_enable_default_ctor<_Equal, _Hash, _Alloc>
204 static_assert(is_same<typename remove_cv<_Value>::type, _Value>::value,
205 "unordered container must have a non-const, non-volatile value_type");
206#if __cplusplus > 201703L || defined __STRICT_ANSI__
207 static_assert(is_same<typename _Alloc::value_type, _Value>{},
208 "unordered container must have the same value_type as its allocator");
210 static_assert(is_copy_constructible<_Hash>::value,
211 "hash function must be copy constructible");
213 using __traits_type = _Traits;
214 using __hash_cached =
typename __traits_type::__hash_cached;
215 using __constant_iterators =
typename __traits_type::__constant_iterators;
216 using __node_type = __detail::_Hash_node<_Value, __hash_cached::value>;
217 using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
219 using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
221 using __node_value_type =
222 __detail::_Hash_node_value<_Value, __hash_cached::value>;
223 using __node_ptr =
typename __hashtable_alloc::__node_ptr;
224 using __value_alloc_traits =
225 typename __hashtable_alloc::__value_alloc_traits;
226 using __node_alloc_traits =
227 typename __hashtable_alloc::__node_alloc_traits;
228 using __node_base =
typename __hashtable_alloc::__node_base;
229 using __node_base_ptr =
typename __hashtable_alloc::__node_base_ptr;
230 using __buckets_ptr =
typename __hashtable_alloc::__buckets_ptr;
232 using __enable_default_ctor
233 = _Hashtable_enable_default_ctor<_Equal, _Hash, _Alloc>;
234 using __rehash_guard_t
235 = __detail::_RehashStateGuard<_RehashPolicy>;
238 typedef _Key key_type;
239 typedef _Value value_type;
240 typedef _Alloc allocator_type;
241 typedef _Equal key_equal;
245 typedef typename __value_alloc_traits::pointer pointer;
246 typedef typename __value_alloc_traits::const_pointer const_pointer;
247 typedef value_type& reference;
248 typedef const value_type& const_reference;
251 = __detail::_Node_iterator<_Value, __constant_iterators::value,
252 __hash_cached::value>;
255 = __detail::_Node_const_iterator<_Value, __constant_iterators::value,
256 __hash_cached::value>;
258 using local_iterator = __detail::_Local_iterator<key_type, _Value,
259 _ExtractKey, _Hash, _RangeHash, _Unused,
260 __constant_iterators::value,
261 __hash_cached::value>;
263 using const_local_iterator = __detail::_Local_const_iterator<
265 _ExtractKey, _Hash, _RangeHash, _Unused,
266 __constant_iterators::value, __hash_cached::value>;
269 using __rehash_type = _RehashPolicy;
271 using __unique_keys =
typename __traits_type::__unique_keys;
273 using __hashtable_base = __detail::
274 _Hashtable_base<_Key, _Value, _ExtractKey,
275 _Equal, _Hash, _RangeHash, _Unused, _Traits>;
277 using __hash_code_base =
typename __hashtable_base::__hash_code_base;
278 using __hash_code =
typename __hashtable_base::__hash_code;
279 using __ireturn_type = __conditional_t<__unique_keys::value,
283 using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
284 _Equal, _Hash, _RangeHash, _Unused,
285 _RehashPolicy, _Traits>;
287 using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
289 _Hash, _RangeHash, _Unused,
290 _RehashPolicy, _Traits>;
292 using __node_builder_t = __detail::_NodeBuilder<_ExtractKey>;
298 _Scoped_node(__node_ptr __n, __hashtable_alloc* __h)
299 : _M_h(__h), _M_node(__n) { }
302 template<
typename... _Args>
303 _Scoped_node(__hashtable_alloc* __h, _Args&&... __args)
305 _M_node(__h->_M_allocate_node(std::
forward<_Args>(__args)...))
309 ~_Scoped_node() {
if (_M_node) _M_h->_M_deallocate_node(_M_node); };
311 _Scoped_node(
const _Scoped_node&) =
delete;
312 _Scoped_node& operator=(
const _Scoped_node&) =
delete;
314 __hashtable_alloc* _M_h;
322 struct __hash_code_base_access : __hash_code_base
323 {
using __hash_code_base::_M_bucket_index; };
326 static_assert(is_nothrow_default_constructible<_RangeHash>::value,
327 "Functor used to map hash code to bucket index"
328 " must be nothrow default constructible");
329 static_assert(
noexcept(
331 "Functor used to map hash code to bucket index must be"
335 static_assert(is_nothrow_default_constructible<_ExtractKey>::value,
336 "_ExtractKey must be nothrow default constructible");
337 static_assert(
noexcept(
339 "_ExtractKey functor must be noexcept invocable");
341 template<
typename _Keya,
typename _Valuea,
typename _Alloca,
342 typename _ExtractKeya,
typename _Equala,
343 typename _Hasha,
typename _RangeHasha,
typename _Unuseda,
344 typename _RehashPolicya,
typename _Traitsa,
346 friend struct __detail::_Map_base;
349 using size_type =
typename __hashtable_base::size_type;
350 using difference_type =
typename __hashtable_base::difference_type;
352#if __cplusplus > 201402L
353 using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
354 using insert_return_type = _Node_insert_return<iterator, node_type>;
358 __buckets_ptr _M_buckets = &_M_single_bucket;
359 size_type _M_bucket_count = 1;
360 __node_base _M_before_begin;
361 size_type _M_element_count = 0;
362 _RehashPolicy _M_rehash_policy;
370 __node_base_ptr _M_single_bucket =
nullptr;
375 if (
auto __begin = _M_begin())
376 _M_buckets[_M_bucket_index(*__begin)] = &_M_before_begin;
380 _M_update_bbegin(__node_ptr __n)
382 _M_before_begin._M_nxt = __n;
387 _M_uses_single_bucket(__buckets_ptr __bkts)
const
388 {
return __builtin_expect(__bkts == &_M_single_bucket,
false); }
391 _M_uses_single_bucket()
const
392 {
return _M_uses_single_bucket(_M_buckets); }
394 static constexpr size_t
395 __small_size_threshold() noexcept
398 __detail::_Hashtable_hash_traits<_Hash>::__small_size_threshold();
402 _M_base_alloc() {
return *
this; }
405 _M_allocate_buckets(size_type __bkt_count)
407 if (__builtin_expect(__bkt_count == 1,
false))
409 _M_single_bucket =
nullptr;
410 return &_M_single_bucket;
413 return __hashtable_alloc::_M_allocate_buckets(__bkt_count);
417 _M_deallocate_buckets(__buckets_ptr __bkts, size_type __bkt_count)
419 if (_M_uses_single_bucket(__bkts))
422 __hashtable_alloc::_M_deallocate_buckets(__bkts, __bkt_count);
426 _M_deallocate_buckets()
427 { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
432 _M_bucket_begin(size_type __bkt)
const
434 __node_base_ptr __n = _M_buckets[__bkt];
435 return __n ?
static_cast<__node_ptr
>(__n->_M_nxt) : nullptr;
440 {
return static_cast<__node_ptr
>(_M_before_begin._M_nxt); }
444 template<
typename _Ht>
446 _M_assign_elements(_Ht&&);
448 template<
typename _Ht>
450 _M_assign(_Ht&& __ht)
452 __detail::_AllocNode<__node_alloc_type> __alloc_node_gen(*
this);
456 template<
typename _Ht,
typename _NodeGenerator>
458 _M_assign(_Ht&&, _NodeGenerator&);
461 _M_move_assign(_Hashtable&&, true_type);
464 _M_move_assign(_Hashtable&&, false_type);
469 _Hashtable(const _Hash& __h, const _Equal& __eq,
470 const allocator_type& __a)
471 : __hashtable_base(__h, __eq),
472 __hashtable_alloc(__node_alloc_type(__a)),
473 __enable_default_ctor(_Enable_default_constructor_tag{})
476 template<
bool _No_realloc = true>
477 static constexpr bool
480#if __cpp_constexpr >= 201304
481# pragma GCC diagnostic push
482# pragma GCC diagnostic ignored "-Wc++17-extensions"
483 if constexpr (_No_realloc)
484 if constexpr (is_nothrow_copy_constructible<_Hash>::value)
485 return is_nothrow_copy_constructible<_Equal>::value;
487# pragma GCC diagnostic pop
489 return __and_<__bool_constant<_No_realloc>,
490 is_nothrow_copy_constructible<_Hash>,
491 is_nothrow_copy_constructible<_Equal>>::value;
495 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
497 noexcept(_S_nothrow_move());
499 _Hashtable(_Hashtable&&, __node_alloc_type&&,
502 template<
typename _InputIterator>
503 _Hashtable(_InputIterator __first, _InputIterator __last,
504 size_type __bkt_count_hint,
505 const _Hash&,
const _Equal&,
const allocator_type&,
508 template<
typename _InputIterator>
509 _Hashtable(_InputIterator __first, _InputIterator __last,
510 size_type __bkt_count_hint,
511 const _Hash&,
const _Equal&,
const allocator_type&,
516 _Hashtable() =
default;
518 _Hashtable(
const _Hashtable&);
520 _Hashtable(
const _Hashtable&,
const allocator_type&);
523 _Hashtable(size_type __bkt_count_hint,
524 const _Hash& __hf = _Hash(),
525 const key_equal& __eql = key_equal(),
526 const allocator_type& __a = allocator_type());
529 _Hashtable(_Hashtable&& __ht)
530 noexcept(_S_nothrow_move())
531 : _Hashtable(std::
move(__ht), std::
move(__ht._M_node_allocator()),
535 _Hashtable(_Hashtable&& __ht,
const allocator_type& __a)
536 noexcept(_S_nothrow_move<__node_alloc_traits::_S_always_equal()>())
537 : _Hashtable(std::
move(__ht), __node_alloc_type(__a),
538 typename __node_alloc_traits::is_always_equal{})
542 _Hashtable(
const allocator_type& __a)
543 : __hashtable_alloc(__node_alloc_type(__a)),
544 __enable_default_ctor(_Enable_default_constructor_tag{})
547 template<
typename _InputIterator>
548 _Hashtable(_InputIterator __f, _InputIterator __l,
549 size_type __bkt_count_hint = 0,
550 const _Hash& __hf = _Hash(),
551 const key_equal& __eql = key_equal(),
552 const allocator_type& __a = allocator_type())
553 : _Hashtable(__f, __l, __bkt_count_hint, __hf, __eql, __a,
557 _Hashtable(initializer_list<value_type> __l,
558 size_type __bkt_count_hint = 0,
559 const _Hash& __hf = _Hash(),
560 const key_equal& __eql = key_equal(),
561 const allocator_type& __a = allocator_type())
562 : _Hashtable(__l.
begin(), __l.
end(), __bkt_count_hint,
563 __hf, __eql, __a, __unique_keys{})
567 operator=(
const _Hashtable& __ht);
570 operator=(_Hashtable&& __ht)
571 noexcept(__node_alloc_traits::_S_nothrow_move()
572 && is_nothrow_move_assignable<_Hash>::value
573 && is_nothrow_move_assignable<_Equal>::value)
575 constexpr bool __move_storage =
576 __node_alloc_traits::_S_propagate_on_move_assign()
577 || __node_alloc_traits::_S_always_equal();
578 _M_move_assign(
std::move(__ht), __bool_constant<__move_storage>());
582#pragma GCC diagnostic push
583#pragma GCC diagnostic ignored "-Wc++17-extensions"
585 operator=(initializer_list<value_type> __l)
587 using __reuse_or_alloc_node_gen_t =
588 __detail::_ReuseOrAllocNode<__node_alloc_type>;
590 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *
this);
591 _M_before_begin._M_nxt =
nullptr;
595 auto __l_bkt_count = _M_rehash_policy._M_bkt_for_elements(__l.size());
599 if (_M_bucket_count < __l_bkt_count)
600 rehash(__l_bkt_count);
604 for (
auto& __e : __l)
606 const key_type& __k = _ExtractKey{}(__e);
608 if constexpr (__unique_keys::value)
610 if (
auto __loc = _M_locate(__k))
614 __code = __loc._M_hash_code;
615 __bkt = __loc._M_bucket_index;
620 __code = this->_M_hash_code(__k);
621 __bkt = _M_bucket_index(__code);
624 _M_insert_unique_node(__bkt, __code, __roan(__e));
629#pragma GCC diagnostic pop
631 ~_Hashtable() noexcept;
635 noexcept(__and_<__is_nothrow_swappable<_Hash>,
636 __is_nothrow_swappable<_Equal>>::value);
641 {
return iterator(_M_begin()); }
644 begin() const noexcept
645 {
return const_iterator(_M_begin()); }
649 {
return iterator(
nullptr); }
653 {
return const_iterator(
nullptr); }
657 {
return const_iterator(_M_begin()); }
660 cend() const noexcept
661 {
return const_iterator(
nullptr); }
664 size() const noexcept
665 {
return _M_element_count; }
667 _GLIBCXX_NODISCARD
bool
668 empty() const noexcept
669 {
return size() == 0; }
672 get_allocator() const noexcept
673 {
return allocator_type(this->_M_node_allocator()); }
676 max_size() const noexcept
677 {
return __node_alloc_traits::max_size(this->_M_node_allocator()); }
682 {
return this->_M_eq(); }
688 bucket_count() const noexcept
689 {
return _M_bucket_count; }
692 max_bucket_count() const noexcept
693 {
return max_size(); }
696 bucket_size(size_type __bkt)
const
700 bucket(
const key_type& __k)
const
701 {
return _M_bucket_index(this->_M_hash_code(__k)); }
704 begin(size_type __bkt)
706 return local_iterator(*
this, _M_bucket_begin(__bkt),
707 __bkt, _M_bucket_count);
712 {
return local_iterator(*
this,
nullptr, __bkt, _M_bucket_count); }
715 begin(size_type __bkt)
const
717 return const_local_iterator(*
this, _M_bucket_begin(__bkt),
718 __bkt, _M_bucket_count);
722 end(size_type __bkt)
const
723 {
return const_local_iterator(*
this,
nullptr, __bkt, _M_bucket_count); }
727 cbegin(size_type __bkt)
const
729 return const_local_iterator(*
this, _M_bucket_begin(__bkt),
730 __bkt, _M_bucket_count);
734 cend(size_type __bkt)
const
735 {
return const_local_iterator(*
this,
nullptr, __bkt, _M_bucket_count); }
738 load_factor() const noexcept
740 return static_cast<float>(
size()) /
static_cast<float>(bucket_count());
749 __rehash_policy()
const
750 {
return _M_rehash_policy; }
753 __rehash_policy(
const _RehashPolicy& __pol)
754 { _M_rehash_policy = __pol; }
758 find(
const key_type& __k);
761 find(
const key_type& __k)
const;
764 count(
const key_type& __k)
const;
767 equal_range(
const key_type& __k);
770 equal_range(
const key_type& __k)
const;
772#ifdef __glibcxx_generic_unordered_lookup
773 template<
typename _Kt,
774 typename = __has_is_transparent_t<_Hash, _Kt>,
775 typename = __has_is_transparent_t<_Equal, _Kt>>
777 _M_find_tr(
const _Kt& __k);
779 template<
typename _Kt,
780 typename = __has_is_transparent_t<_Hash, _Kt>,
781 typename = __has_is_transparent_t<_Equal, _Kt>>
783 _M_find_tr(
const _Kt& __k)
const;
785 template<
typename _Kt,
786 typename = __has_is_transparent_t<_Hash, _Kt>,
787 typename = __has_is_transparent_t<_Equal, _Kt>>
789 _M_count_tr(
const _Kt& __k)
const;
791 template<
typename _Kt,
792 typename = __has_is_transparent_t<_Hash, _Kt>,
793 typename = __has_is_transparent_t<_Equal, _Kt>>
794 pair<iterator, iterator>
795 _M_equal_range_tr(
const _Kt& __k);
797 template<
typename _Kt,
798 typename = __has_is_transparent_t<_Hash, _Kt>,
799 typename = __has_is_transparent_t<_Equal, _Kt>>
800 pair<const_iterator, const_iterator>
801 _M_equal_range_tr(
const _Kt& __k)
const;
804 void _M_rehash_insert(size_type __n);
809 _M_bucket_index(
const __node_value_type& __n)
const noexcept
810 {
return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
813 _M_bucket_index(__hash_code __c)
const
814 {
return __hash_code_base::_M_bucket_index(__c, _M_bucket_count); }
816#pragma GCC diagnostic push
817#pragma GCC diagnostic ignored "-Wc++17-extensions"
822 _M_hash_code_ext(
const __node_value_type& __from)
const
824 if constexpr (__and_<__hash_cached, is_empty<_Hash>>::value)
825 return __from._M_hash_code;
827 return this->_M_hash_code(_ExtractKey{}(__from._M_v()));
833 _M_bucket_index_ext(
const __node_value_type& __from)
const
834 {
return _RangeHash{}(_M_hash_code_ext(__from), _M_bucket_count); }
837 _M_copy_code(__node_value_type& __to,
838 const __node_value_type& __from)
const
840 if constexpr (__hash_cached::value)
841 __to._M_hash_code = _M_hash_code_ext(__from);
845 _M_store_code(__node_value_type& __to, __hash_code __code)
const
847 if constexpr (__hash_cached::value)
848 __to._M_hash_code = __code;
850#pragma GCC diagnostic pop
856 _M_find_before_node(size_type,
const key_type&, __hash_code)
const;
858 template<
typename _Kt>
860 _M_find_before_node_tr(size_type,
const _Kt&, __hash_code)
const;
864 struct __location_type
867 explicit operator bool() const noexcept
868 {
return static_cast<bool>(_M_before); }
871 explicit operator iterator() const noexcept
872 {
return iterator(_M_node()); }
875 explicit operator const_iterator() const noexcept
876 {
return const_iterator(_M_node()); }
879 __node_ptr _M_node()
const
882 return static_cast<__node_ptr
>(_M_before->_M_nxt);
886 __node_base_ptr _M_before{};
887 __hash_code _M_hash_code{};
888 size_type _M_bucket_index = size_type(-1);
906 _M_locate(
const key_type& __k)
const;
909 _M_find_node(size_type __bkt,
const key_type& __key,
910 __hash_code __c)
const
912 if (__node_base_ptr __before_n = _M_find_before_node(__bkt, __key, __c))
913 return static_cast<__node_ptr
>(__before_n->_M_nxt);
917 template<
typename _Kt>
919 _M_find_node_tr(size_type __bkt,
const _Kt& __key,
920 __hash_code __c)
const
922 if (
auto __before_n = _M_find_before_node_tr(__bkt, __key, __c))
923 return static_cast<__node_ptr
>(__before_n->_M_nxt);
929 _M_insert_bucket_begin(size_type __bkt, __node_ptr __node)
931 if (_M_buckets[__bkt])
935 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
936 _M_buckets[__bkt]->_M_nxt = __node;
943 __node->_M_nxt = _M_before_begin._M_nxt;
944 _M_before_begin._M_nxt = __node;
949 _M_buckets[_M_bucket_index(*__node->_M_next())] = __node;
951 _M_buckets[__bkt] = &_M_before_begin;
957 _M_remove_bucket_begin(size_type __bkt, __node_ptr __next_n,
958 size_type __next_bkt)
961 _M_buckets[__bkt] =
nullptr;
962 else if (__next_bkt != __bkt)
964 _M_buckets[__next_bkt] = _M_buckets[__bkt];
965 _M_buckets[__bkt] =
nullptr;
971 _M_get_previous_node(size_type __bkt, __node_ptr __n);
973 pair<__node_ptr, __hash_code>
974 _M_compute_hash_code(__node_ptr __hint,
const key_type& __k)
const;
983 _M_insert_unique_node(size_type __bkt, __hash_code,
984 __node_ptr __n, size_type __n_elt = 1);
989 _M_insert_multi_node(__node_ptr __hint,
990 __hash_code __code, __node_ptr __n);
992 template<
typename... _Args>
994 _M_emplace_uniq(_Args&&... __args);
996#pragma GCC diagnostic push
997#pragma GCC diagnostic ignored "-Wc++14-extensions"
998 template<
typename _Arg,
typename _DArg = __remove_cvref_t<_Arg>,
999 typename = _ExtractKey>
1000 static constexpr bool __is_key_type =
false;
1002 template<
typename _Arg>
1003 static constexpr bool
1004 __is_key_type<_Arg, key_type, __detail::_Identity> =
true;
1006 template<
typename _Arg,
typename _Arg1,
typename _Arg2>
1007 static constexpr bool
1008 __is_key_type<_Arg, pair<_Arg1, _Arg2>, __detail::_Select1st>
1009 = is_same<__remove_cvref_t<_Arg1>, key_type>::value;
1010#pragma GCC diagnostic pop
1012 template<
typename... _Args>
1014 _M_emplace_multi(const_iterator, _Args&&... __args);
1017 _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n);
1019 template<
typename _InputIterator>
1021 _M_insert_range_multi(_InputIterator __first, _InputIterator __last);
1024#pragma GCC diagnostic push
1025#pragma GCC diagnostic ignored "-Wc++17-extensions"
1027 template<
typename... _Args>
1029 emplace(_Args&&... __args)
1031 if constexpr (__unique_keys::value)
1037 template<
typename... _Args>
1039 emplace_hint(const_iterator __hint, _Args&&... __args)
1041 if constexpr (__unique_keys::value)
1049 insert(
const value_type& __v)
1051 if constexpr (__unique_keys::value)
1052 return _M_emplace_uniq(__v);
1054 return _M_emplace_multi(
cend(), __v);
1058 insert(const_iterator __hint,
const value_type& __v)
1060 if constexpr (__unique_keys::value)
1061 return _M_emplace_uniq(__v).first;
1063 return _M_emplace_multi(__hint, __v);
1067 insert(value_type&& __v)
1069 if constexpr (__unique_keys::value)
1076 insert(const_iterator __hint, value_type&& __v)
1078 if constexpr (__unique_keys::value)
1079 return _M_emplace_uniq(
std::move(__v)).first;
1081 return _M_emplace_multi(__hint,
std::move(__v));
1084#ifdef __glibcxx_unordered_map_try_emplace
1085 template<
typename _KType,
typename... _Args>
1087 try_emplace(const_iterator, _KType&& __k, _Args&&... __args)
1091 if (
auto __loc = _M_locate(__k))
1092 return { iterator(__loc),
false };
1095 __code = __loc._M_hash_code;
1096 __bkt = __loc._M_bucket_index;
1099 _Scoped_node __node {
1105 auto __it = _M_insert_unique_node(__bkt, __code, __node._M_node);
1106 __node._M_node =
nullptr;
1107 return { __it,
true };
1112 insert(initializer_list<value_type> __l)
1113 { this->insert(__l.begin(), __l.end()); }
1115 template<
typename _InputIterator>
1117 insert(_InputIterator __first, _InputIterator __last)
1119 if constexpr (__unique_keys::value)
1120 for (; __first != __last; ++__first)
1121 _M_emplace_uniq(*__first);
1123 return _M_insert_range_multi(__first, __last);
1127 template<
typename _Pair,
1128 typename = _Require<__not_<is_same<_Key, _Value>>,
1129 is_constructible<value_type, _Pair&&>>>
1133 if constexpr (__unique_keys::value)
1140 template<
typename _Pair,
1141 typename = _Require<__not_<is_same<_Key, _Value>>,
1142 is_constructible<value_type, _Pair&&>>>
1144 insert(const_iterator __hint, _Pair&& __v)
1146 if constexpr (__unique_keys::value)
1151#pragma GCC diagnostic pop
1155 erase(const_iterator);
1160 erase(iterator __it)
1161 {
return erase(const_iterator(__it)); }
1164 erase(
const key_type& __k);
1167 erase(const_iterator, const_iterator);
1174 void rehash(size_type __bkt_count);
1179#if __glibcxx_node_extract
1182 _M_reinsert_node(node_type&& __nh)
1184 insert_return_type __ret;
1186 __ret.position =
end();
1189 __glibcxx_assert(get_allocator() == __nh.get_allocator());
1191 if (
auto __loc = _M_locate(__nh._M_key()))
1194 __ret.position = iterator(__loc);
1195 __ret.inserted =
false;
1199 auto __code = __loc._M_hash_code;
1200 auto __bkt = __loc._M_bucket_index;
1202 = _M_insert_unique_node(__bkt, __code, __nh._M_ptr);
1203 __ret.inserted =
true;
1212 _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
1217 __glibcxx_assert(get_allocator() == __nh.get_allocator());
1219 const key_type& __k = __nh._M_key();
1220 auto __code = this->_M_hash_code(__k);
1222 = _M_insert_multi_node(__hint._M_cur, __code, __nh._M_ptr);
1229 _M_extract_node(
size_t __bkt, __node_base_ptr __prev_n)
1231 __node_ptr __n =
static_cast<__node_ptr
>(__prev_n->_M_nxt);
1232 if (__prev_n == _M_buckets[__bkt])
1233 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1234 __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
1235 else if (__n->_M_nxt)
1237 size_type __next_bkt = _M_bucket_index(*__n->_M_next());
1238 if (__next_bkt != __bkt)
1239 _M_buckets[__next_bkt] = __prev_n;
1242 __prev_n->_M_nxt = __n->_M_nxt;
1243 __n->_M_nxt =
nullptr;
1245 return { __n, this->_M_node_allocator() };
1252 template<
typename _H2>
1254 _M_src_hash_code(
const _H2&,
const __node_value_type& __src_n)
const
1256 if constexpr (__and_<__hash_cached,
1257 is_same<_H2, _Hash>, is_empty<_Hash>>::value)
1259 return __src_n._M_hash_code;
1261 return this->_M_hash_code(_ExtractKey{}(__src_n._M_v()));
1267 extract(const_iterator __pos)
1269 size_t __bkt = _M_bucket_index(*__pos._M_cur);
1270 return _M_extract_node(__bkt,
1271 _M_get_previous_node(__bkt, __pos._M_cur));
1276 extract(
const _Key& __k)
1279 __hash_code __code = this->_M_hash_code(__k);
1280 std::size_t __bkt = _M_bucket_index(__code);
1281 if (__node_base_ptr __prev_node = _M_find_before_node(__bkt, __k, __code))
1282 __nh = _M_extract_node(__bkt, __prev_node);
1288 _M_merge_unique(_Hashtable& __src)
1290 __glibcxx_assert(get_allocator() == __src.get_allocator());
1292 using _PTr = pointer_traits<__node_base_ptr>;
1294 auto __n_elt = __src.size();
1295 size_type __first = 1;
1298 auto __prev = _PTr::pointer_to(__src._M_before_begin);
1301 const auto __next = __prev->_M_nxt;
1302 const auto& __node =
static_cast<__node_type&
>(*__next);
1303 const key_type& __k = _ExtractKey{}(__node._M_v());
1304 const auto __loc = _M_locate(__k);
1311 auto __src_bkt = __src._M_bucket_index(__node);
1312 auto __nh = __src._M_extract_node(__src_bkt, __prev);
1313 _M_insert_unique_node(__loc._M_bucket_index, __loc._M_hash_code,
1314 __nh._M_ptr, __first * __n_elt + 1);
1321 template<
typename _Compatible_Hashtable>
1323 _M_merge_unique(_Compatible_Hashtable& __src)
1325 static_assert(is_same_v<
typename _Compatible_Hashtable::node_type,
1326 node_type>,
"Node types are compatible");
1327 __glibcxx_assert(get_allocator() == __src.get_allocator());
1329 auto __n_elt = __src.size();
1330 size_type __first = 1;
1333 for (
auto __i = __src.cbegin(), __end = __src.cend(); __i != __end;)
1337 const key_type& __k = _ExtractKey{}(*__pos);
1338 const auto __loc = _M_locate(__k);
1342 auto __nh = __src.extract(__pos);
1343 _M_insert_unique_node(__loc._M_bucket_index,
1344 __loc._M_hash_code, __nh._M_ptr,
1345 __first * __n_elt + 1);
1353 _M_merge_multi(_Hashtable& __src)
1355 __glibcxx_assert(get_allocator() == __src.get_allocator());
1357 if (__src.size() == 0) [[__unlikely__]]
1360 using _PTr = pointer_traits<__node_base_ptr>;
1362 __node_ptr __hint =
nullptr;
1363 this->reserve(
size() + __src.size());
1366 auto __prev = _PTr::pointer_to(__src._M_before_begin);
1369 const auto& __node =
static_cast<__node_type&
>(*__prev->_M_nxt);
1371 auto __code = _M_hash_code_ext(__node);
1373 size_type __src_bkt = __src._M_bucket_index(__node);
1374 auto __nh = __src._M_extract_node(__src_bkt, __prev);
1375 __hint = _M_insert_multi_node(__hint, __code, __nh._M_ptr)._M_cur;
1378 while (__prev->_M_nxt !=
nullptr);
1382 template<
typename _Compatible_Hashtable>
1384 _M_merge_multi(_Compatible_Hashtable& __src)
1386 static_assert(is_same_v<
typename _Compatible_Hashtable::node_type,
1387 node_type>,
"Node types are compatible");
1388 __glibcxx_assert(get_allocator() == __src.get_allocator());
1390 __node_ptr __hint =
nullptr;
1391 this->reserve(
size() + __src.size());
1394 for (
auto __i = __src.cbegin(), __end = __src.cend(); __i != __end;)
1398 = _M_src_hash_code(__src.hash_function(), *__pos._M_cur);
1399 auto __nh = __src.extract(__pos);
1400 __hint = _M_insert_multi_node(__hint, __code, __nh._M_ptr)._M_cur;
1407 _M_equal(
const _Hashtable& __other)
const;
1411 void _M_rehash(size_type __bkt_count, true_type __uks);
1414 void _M_rehash(size_type __bkt_count, false_type __uks);
1418 template<
typename _Key,
typename _Value,
typename _Alloc,
1419 typename _ExtractKey,
typename _Equal,
1420 typename _Hash,
typename _RangeHash,
typename _Unused,
1421 typename _RehashPolicy,
typename _Traits>
1422 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1423 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1425 const _Hash& __h,
const _Equal& __eq,
const allocator_type& __a)
1426 : _Hashtable(__h, __eq, __a)
1428 auto __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count_hint);
1429 if (__bkt_count > _M_bucket_count)
1431 _M_buckets = _M_allocate_buckets(__bkt_count);
1432 _M_bucket_count = __bkt_count;
1436 template<
typename _Key,
typename _Value,
typename _Alloc,
1437 typename _ExtractKey,
typename _Equal,
1438 typename _Hash,
typename _RangeHash,
typename _Unused,
1439 typename _RehashPolicy,
typename _Traits>
1440 template<
typename _InputIterator>
1442 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1443 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1444 _Hashtable(_InputIterator __f, _InputIterator __l,
1445 size_type __bkt_count_hint,
1446 const _Hash& __h,
const _Equal& __eq,
1447 const allocator_type& __a, true_type )
1448 : _Hashtable(__bkt_count_hint, __h, __eq, __a)
1449 { this->insert(__f, __l); }
1451 template<
typename _Key,
typename _Value,
typename _Alloc,
1452 typename _ExtractKey,
typename _Equal,
1453 typename _Hash,
typename _RangeHash,
typename _Unused,
1454 typename _RehashPolicy,
typename _Traits>
1455 template<
typename _InputIterator>
1456 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1457 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1458 _Hashtable(_InputIterator __f, _InputIterator __l,
1459 size_type __bkt_count_hint,
1460 const _Hash& __h,
const _Equal& __eq,
1461 const allocator_type& __a, false_type __uks)
1462 : _Hashtable(__h, __eq, __a)
1464 auto __nb_elems = __detail::__distance_fw(__f, __l);
1466 _M_rehash_policy._M_next_bkt(
1467 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
1470 if (__bkt_count > _M_bucket_count)
1472 _M_buckets = _M_allocate_buckets(__bkt_count);
1473 _M_bucket_count = __bkt_count;
1476 for (; __f != __l; ++__f)
1477 _M_emplace_multi(
cend(), *__f);
1480 template<
typename _Key,
typename _Value,
typename _Alloc,
1481 typename _ExtractKey,
typename _Equal,
1482 typename _Hash,
typename _RangeHash,
typename _Unused,
1483 typename _RehashPolicy,
typename _Traits>
1485 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1486 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1487 operator=(
const _Hashtable& __ht)
1493 if (__node_alloc_traits::_S_propagate_on_copy_assign())
1495 auto& __this_alloc = this->_M_node_allocator();
1496 auto& __that_alloc = __ht._M_node_allocator();
1497 if (!__node_alloc_traits::_S_always_equal()
1498 && __this_alloc != __that_alloc)
1501 this->_M_deallocate_nodes(_M_begin());
1502 _M_before_begin._M_nxt =
nullptr;
1503 _M_deallocate_buckets();
1504 _M_buckets =
nullptr;
1505 std::__alloc_on_copy(__this_alloc, __that_alloc);
1506 __hashtable_base::operator=(__ht);
1507 _M_bucket_count = __ht._M_bucket_count;
1508 _M_element_count = __ht._M_element_count;
1509 _M_rehash_policy = __ht._M_rehash_policy;
1513 ~_Guard() {
if (_M_ht) _M_ht->_M_reset(); }
1518 _Guard __guard{
this};
1520 __guard._M_ht =
nullptr;
1523 std::__alloc_on_copy(__this_alloc, __that_alloc);
1527 _M_assign_elements(__ht);
1531 template<
typename _Key,
typename _Value,
typename _Alloc,
1532 typename _ExtractKey,
typename _Equal,
1533 typename _Hash,
typename _RangeHash,
typename _Unused,
1534 typename _RehashPolicy,
typename _Traits>
1535 template<
typename _Ht>
1537 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1538 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1539 _M_assign_elements(_Ht&& __ht)
1541 using __reuse_or_alloc_node_gen_t =
1542 __detail::_ReuseOrAllocNode<__node_alloc_type>;
1544 __buckets_ptr __former_buckets =
nullptr;
1545 std::size_t __former_bucket_count = _M_bucket_count;
1546 __rehash_guard_t __rehash_guard(_M_rehash_policy);
1548 if (_M_bucket_count != __ht._M_bucket_count)
1550 __former_buckets = _M_buckets;
1551 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1552 _M_bucket_count = __ht._M_bucket_count;
1555 std::fill_n(_M_buckets, _M_bucket_count,
nullptr);
1560 _M_element_count = __ht._M_element_count;
1561 _M_rehash_policy = __ht._M_rehash_policy;
1562 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *
this);
1563 _M_before_begin._M_nxt =
nullptr;
1565 if (__former_buckets)
1566 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1567 __rehash_guard._M_guarded_obj =
nullptr;
1571 if (__former_buckets)
1574 _M_deallocate_buckets();
1575 _M_buckets = __former_buckets;
1576 _M_bucket_count = __former_bucket_count;
1578 std::fill_n(_M_buckets, _M_bucket_count,
nullptr);
1579 __throw_exception_again;
1583 template<
typename _Key,
typename _Value,
typename _Alloc,
1584 typename _ExtractKey,
typename _Equal,
1585 typename _Hash,
typename _RangeHash,
typename _Unused,
1586 typename _RehashPolicy,
typename _Traits>
1587 template<
typename _Ht,
typename _NodeGenerator>
1589 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1590 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1591 _M_assign(_Ht&& __ht, _NodeGenerator& __node_gen)
1600 if (_M_dealloc_buckets)
1601 _M_ht->_M_deallocate_buckets();
1604 _Hashtable* _M_ht =
nullptr;
1605 bool _M_dealloc_buckets =
false;
1611 _M_buckets = _M_allocate_buckets(_M_bucket_count);
1612 __guard._M_dealloc_buckets =
true;
1615 if (!__ht._M_before_begin._M_nxt)
1618 __guard._M_ht =
this;
1620 using _FromVal = __conditional_t<is_lvalue_reference<_Ht>::value,
1621 const value_type&, value_type&&>;
1625 __node_ptr __ht_n = __ht._M_begin();
1627 = __node_gen(
static_cast<_FromVal
>(__ht_n->_M_v()));
1628 _M_copy_code(*__this_n, *__ht_n);
1629 _M_update_bbegin(__this_n);
1632 __node_ptr __prev_n = __this_n;
1633 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
1635 __this_n = __node_gen(
static_cast<_FromVal
>(__ht_n->_M_v()));
1636 __prev_n->_M_nxt = __this_n;
1637 _M_copy_code(*__this_n, *__ht_n);
1638 size_type __bkt = _M_bucket_index(*__this_n);
1639 if (!_M_buckets[__bkt])
1640 _M_buckets[__bkt] = __prev_n;
1641 __prev_n = __this_n;
1643 __guard._M_ht =
nullptr;
1646 template<
typename _Key,
typename _Value,
typename _Alloc,
1647 typename _ExtractKey,
typename _Equal,
1648 typename _Hash,
typename _RangeHash,
typename _Unused,
1649 typename _RehashPolicy,
typename _Traits>
1651 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1652 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1655 _M_rehash_policy._M_reset();
1656 _M_bucket_count = 1;
1657 _M_single_bucket =
nullptr;
1658 _M_buckets = &_M_single_bucket;
1659 _M_before_begin._M_nxt =
nullptr;
1660 _M_element_count = 0;
1663 template<
typename _Key,
typename _Value,
typename _Alloc,
1664 typename _ExtractKey,
typename _Equal,
1665 typename _Hash,
typename _RangeHash,
typename _Unused,
1666 typename _RehashPolicy,
typename _Traits>
1668 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1669 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1670 _M_move_assign(_Hashtable&& __ht, true_type)
1675 this->_M_deallocate_nodes(_M_begin());
1676 _M_deallocate_buckets();
1677 __hashtable_base::operator=(
std::move(__ht));
1678 _M_rehash_policy = __ht._M_rehash_policy;
1679 if (!__ht._M_uses_single_bucket())
1680 _M_buckets = __ht._M_buckets;
1683 _M_buckets = &_M_single_bucket;
1684 _M_single_bucket = __ht._M_single_bucket;
1687 _M_bucket_count = __ht._M_bucket_count;
1688 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1689 _M_element_count = __ht._M_element_count;
1690 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1697 template<
typename _Key,
typename _Value,
typename _Alloc,
1698 typename _ExtractKey,
typename _Equal,
1699 typename _Hash,
typename _RangeHash,
typename _Unused,
1700 typename _RehashPolicy,
typename _Traits>
1702 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1703 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1704 _M_move_assign(_Hashtable&& __ht, false_type)
1706 if (__ht._M_node_allocator() == this->_M_node_allocator())
1707 _M_move_assign(
std::move(__ht), true_type{});
1716 template<
typename _Key,
typename _Value,
typename _Alloc,
1717 typename _ExtractKey,
typename _Equal,
1718 typename _Hash,
typename _RangeHash,
typename _Unused,
1719 typename _RehashPolicy,
typename _Traits>
1721 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1722 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1723 _Hashtable(
const _Hashtable& __ht)
1724 : __hashtable_base(__ht),
1726 __rehash_base(__ht),
1728 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1729 __enable_default_ctor(__ht),
1730 _M_buckets(nullptr),
1731 _M_bucket_count(__ht._M_bucket_count),
1732 _M_element_count(__ht._M_element_count),
1733 _M_rehash_policy(__ht._M_rehash_policy)
1738 template<
typename _Key,
typename _Value,
typename _Alloc,
1739 typename _ExtractKey,
typename _Equal,
1740 typename _Hash,
typename _RangeHash,
typename _Unused,
1741 typename _RehashPolicy,
typename _Traits>
1742 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1743 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1744 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1746 noexcept(_S_nothrow_move())
1747 : __hashtable_base(__ht),
1749 __rehash_base(__ht),
1750 __hashtable_alloc(
std::
move(__a)),
1751 __enable_default_ctor(__ht),
1752 _M_buckets(__ht._M_buckets),
1753 _M_bucket_count(__ht._M_bucket_count),
1754 _M_before_begin(__ht._M_before_begin._M_nxt),
1755 _M_element_count(__ht._M_element_count),
1756 _M_rehash_policy(__ht._M_rehash_policy)
1759 if (__ht._M_uses_single_bucket())
1761 _M_buckets = &_M_single_bucket;
1762 _M_single_bucket = __ht._M_single_bucket;
1771 template<
typename _Key,
typename _Value,
typename _Alloc,
1772 typename _ExtractKey,
typename _Equal,
1773 typename _Hash,
typename _RangeHash,
typename _Unused,
1774 typename _RehashPolicy,
typename _Traits>
1776 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1777 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1778 _Hashtable(
const _Hashtable& __ht,
const allocator_type& __a)
1779 : __hashtable_base(__ht),
1781 __rehash_base(__ht),
1782 __hashtable_alloc(__node_alloc_type(__a)),
1783 __enable_default_ctor(__ht),
1785 _M_bucket_count(__ht._M_bucket_count),
1786 _M_element_count(__ht._M_element_count),
1787 _M_rehash_policy(__ht._M_rehash_policy)
1792 template<
typename _Key,
typename _Value,
typename _Alloc,
1793 typename _ExtractKey,
typename _Equal,
1794 typename _Hash,
typename _RangeHash,
typename _Unused,
1795 typename _RehashPolicy,
typename _Traits>
1796 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1797 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1798 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1800 : __hashtable_base(__ht),
1802 __rehash_base(__ht),
1803 __hashtable_alloc(
std::
move(__a)),
1804 __enable_default_ctor(__ht),
1805 _M_buckets(nullptr),
1806 _M_bucket_count(__ht._M_bucket_count),
1807 _M_element_count(__ht._M_element_count),
1808 _M_rehash_policy(__ht._M_rehash_policy)
1810 if (__ht._M_node_allocator() == this->_M_node_allocator())
1812 if (__ht._M_uses_single_bucket())
1814 _M_buckets = &_M_single_bucket;
1815 _M_single_bucket = __ht._M_single_bucket;
1818 _M_buckets = __ht._M_buckets;
1822 _M_update_bbegin(__ht._M_begin());
1828 using _Fwd_Ht = __conditional_t<
1829 __move_if_noexcept_cond<value_type>::value,
1830 const _Hashtable&, _Hashtable&&>;
1836 template<
typename _Key,
typename _Value,
typename _Alloc,
1837 typename _ExtractKey,
typename _Equal,
1838 typename _Hash,
typename _RangeHash,
typename _Unused,
1839 typename _RehashPolicy,
typename _Traits>
1840 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1841 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1842 ~_Hashtable() noexcept
1849 static_assert(
noexcept(declval<const __hash_code_base_access&>()
1850 ._M_bucket_index(declval<const __node_value_type&>(),
1852 "Cache the hash code or qualify your functors involved"
1853 " in hash code and bucket index computation with noexcept");
1855 this->_M_deallocate_nodes(_M_begin());
1856 _M_deallocate_buckets();
1859 template<
typename _Key,
typename _Value,
typename _Alloc,
1860 typename _ExtractKey,
typename _Equal,
1861 typename _Hash,
typename _RangeHash,
typename _Unused,
1862 typename _RehashPolicy,
typename _Traits>
1864 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1865 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1866 swap(_Hashtable& __x)
1867 noexcept(__and_<__is_nothrow_swappable<_Hash>,
1868 __is_nothrow_swappable<_Equal>>::value)
1871 swap(__hash_code_base::_M_hash._M_obj,
1872 __x.__hash_code_base::_M_hash._M_obj);
1873 swap(__hashtable_base::_M_equal._M_obj,
1874 __x.__hashtable_base::_M_equal._M_obj);
1876#pragma GCC diagnostic push
1877#pragma GCC diagnostic ignored "-Wc++17-extensions"
1878 if constexpr (__node_alloc_traits::propagate_on_container_swap::value)
1879 swap(this->_M_node_allocator(), __x._M_node_allocator());
1880#pragma GCC diagnostic pop
1882 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1885 if (this->_M_uses_single_bucket())
1887 if (!__x._M_uses_single_bucket())
1889 _M_buckets = __x._M_buckets;
1890 __x._M_buckets = &__x._M_single_bucket;
1893 else if (__x._M_uses_single_bucket())
1895 __x._M_buckets = _M_buckets;
1896 _M_buckets = &_M_single_bucket;
1899 std::swap(_M_buckets, __x._M_buckets);
1901 std::swap(_M_bucket_count, __x._M_bucket_count);
1902 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1903 std::swap(_M_element_count, __x._M_element_count);
1904 std::swap(_M_single_bucket, __x._M_single_bucket);
1909 __x._M_update_bbegin();
1912 template<
typename _Key,
typename _Value,
typename _Alloc,
1913 typename _ExtractKey,
typename _Equal,
1914 typename _Hash,
typename _RangeHash,
typename _Unused,
1915 typename _RehashPolicy,
typename _Traits>
1917 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1918 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1919 find(
const key_type& __k)
1921 {
return iterator(_M_locate(__k)); }
1923 template<
typename _Key,
typename _Value,
typename _Alloc,
1924 typename _ExtractKey,
typename _Equal,
1925 typename _Hash,
typename _RangeHash,
typename _Unused,
1926 typename _RehashPolicy,
typename _Traits>
1928 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1929 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1930 find(
const key_type& __k)
const
1932 {
return const_iterator(_M_locate(__k)); }
1934#if __cplusplus > 201703L
1935 template<
typename _Key,
typename _Value,
typename _Alloc,
1936 typename _ExtractKey,
typename _Equal,
1937 typename _Hash,
typename _RangeHash,
typename _Unused,
1938 typename _RehashPolicy,
typename _Traits>
1939 template<
typename _Kt,
typename,
typename>
1941 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1942 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1943 _M_find_tr(
const _Kt& __k)
1946 if (
size() <= __small_size_threshold())
1948 for (
auto __n = _M_begin(); __n; __n = __n->_M_next())
1949 if (this->_M_key_equals_tr(__k, *__n))
1950 return iterator(__n);
1954 __hash_code __code = this->_M_hash_code_tr(__k);
1955 std::size_t __bkt = _M_bucket_index(__code);
1956 return iterator(_M_find_node_tr(__bkt, __k, __code));
1959 template<
typename _Key,
typename _Value,
typename _Alloc,
1960 typename _ExtractKey,
typename _Equal,
1961 typename _Hash,
typename _RangeHash,
typename _Unused,
1962 typename _RehashPolicy,
typename _Traits>
1963 template<
typename _Kt,
typename,
typename>
1965 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1966 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1967 _M_find_tr(
const _Kt& __k)
const
1970 if (
size() <= __small_size_threshold())
1972 for (
auto __n = _M_begin(); __n; __n = __n->_M_next())
1973 if (this->_M_key_equals_tr(__k, *__n))
1974 return const_iterator(__n);
1978 __hash_code __code = this->_M_hash_code_tr(__k);
1979 std::size_t __bkt = _M_bucket_index(__code);
1980 return const_iterator(_M_find_node_tr(__bkt, __k, __code));
1984 template<
typename _Key,
typename _Value,
typename _Alloc,
1985 typename _ExtractKey,
typename _Equal,
1986 typename _Hash,
typename _RangeHash,
typename _Unused,
1987 typename _RehashPolicy,
typename _Traits>
1989 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1990 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1991 count(
const key_type& __k)
const
1994 auto __it = find(__k);
1998 if (__unique_keys::value)
2001 size_type __result = 1;
2002 for (
auto __ref = __it++;
2003 __it._M_cur && this->_M_node_equals(*__ref._M_cur, *__it._M_cur);
2010#if __cplusplus > 201703L
2011 template<
typename _Key,
typename _Value,
typename _Alloc,
2012 typename _ExtractKey,
typename _Equal,
2013 typename _Hash,
typename _RangeHash,
typename _Unused,
2014 typename _RehashPolicy,
typename _Traits>
2015 template<
typename _Kt,
typename,
typename>
2017 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2018 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2019 _M_count_tr(
const _Kt& __k)
const
2022 if (
size() <= __small_size_threshold())
2024 size_type __result = 0;
2025 for (
auto __n = _M_begin(); __n; __n = __n->_M_next())
2027 if (this->_M_key_equals_tr(__k, *__n))
2040 __hash_code __code = this->_M_hash_code_tr(__k);
2041 std::size_t __bkt = _M_bucket_index(__code);
2042 auto __n = _M_find_node_tr(__bkt, __k, __code);
2047 size_type __result = 1;
2049 __it._M_cur && this->_M_equals_tr(__k, __code, *__it._M_cur);
2057 template<
typename _Key,
typename _Value,
typename _Alloc,
2058 typename _ExtractKey,
typename _Equal,
2059 typename _Hash,
typename _RangeHash,
typename _Unused,
2060 typename _RehashPolicy,
typename _Traits>
2062 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2063 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2064 equal_range(
const key_type& __k)
2065 -> pair<iterator, iterator>
2067 auto __ite = find(__k);
2069 return { __ite, __ite };
2071 auto __beg = __ite++;
2072 if (__unique_keys::value)
2073 return { __beg, __ite };
2075 while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
2078 return { __beg, __ite };
2081 template<
typename _Key,
typename _Value,
typename _Alloc,
2082 typename _ExtractKey,
typename _Equal,
2083 typename _Hash,
typename _RangeHash,
typename _Unused,
2084 typename _RehashPolicy,
typename _Traits>
2086 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2087 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2088 equal_range(
const key_type& __k)
const
2089 -> pair<const_iterator, const_iterator>
2091 auto __ite = find(__k);
2093 return { __ite, __ite };
2095 auto __beg = __ite++;
2096 if (__unique_keys::value)
2097 return { __beg, __ite };
2099 while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
2102 return { __beg, __ite };
2105#if __cplusplus > 201703L
2106 template<
typename _Key,
typename _Value,
typename _Alloc,
2107 typename _ExtractKey,
typename _Equal,
2108 typename _Hash,
typename _RangeHash,
typename _Unused,
2109 typename _RehashPolicy,
typename _Traits>
2110 template<
typename _Kt,
typename,
typename>
2112 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2113 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2114 _M_equal_range_tr(
const _Kt& __k)
2115 -> pair<iterator, iterator>
2117 if (
size() <= __small_size_threshold())
2119 __node_ptr __n, __beg =
nullptr;
2120 for (__n = _M_begin(); __n; __n = __n->_M_next())
2122 if (this->_M_key_equals_tr(__k, *__n))
2133 return { iterator(__beg), iterator(__n) };
2136 __hash_code __code = this->_M_hash_code_tr(__k);
2137 std::size_t __bkt = _M_bucket_index(__code);
2138 auto __n = _M_find_node_tr(__bkt, __k, __code);
2139 iterator __ite(__n);
2141 return { __ite, __ite };
2143 auto __beg = __ite++;
2144 while (__ite._M_cur && this->_M_equals_tr(__k, __code, *__ite._M_cur))
2147 return { __beg, __ite };
2150 template<
typename _Key,
typename _Value,
typename _Alloc,
2151 typename _ExtractKey,
typename _Equal,
2152 typename _Hash,
typename _RangeHash,
typename _Unused,
2153 typename _RehashPolicy,
typename _Traits>
2154 template<
typename _Kt,
typename,
typename>
2156 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2157 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2158 _M_equal_range_tr(
const _Kt& __k)
const
2159 -> pair<const_iterator, const_iterator>
2161 if (
size() <= __small_size_threshold())
2163 __node_ptr __n, __beg =
nullptr;
2164 for (__n = _M_begin(); __n; __n = __n->_M_next())
2166 if (this->_M_key_equals_tr(__k, *__n))
2177 return { const_iterator(__beg), const_iterator(__n) };
2180 __hash_code __code = this->_M_hash_code_tr(__k);
2181 std::size_t __bkt = _M_bucket_index(__code);
2182 auto __n = _M_find_node_tr(__bkt, __k, __code);
2183 const_iterator __ite(__n);
2185 return { __ite, __ite };
2187 auto __beg = __ite++;
2188 while (__ite._M_cur && this->_M_equals_tr(__k, __code, *__ite._M_cur))
2191 return { __beg, __ite };
2197 template<
typename _Key,
typename _Value,
typename _Alloc,
2198 typename _ExtractKey,
typename _Equal,
2199 typename _Hash,
typename _RangeHash,
typename _Unused,
2200 typename _RehashPolicy,
typename _Traits>
2202 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2203 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2204 _M_find_before_node(size_type __bkt,
const key_type& __k,
2205 __hash_code __code)
const
2208 __node_base_ptr __prev_p = _M_buckets[__bkt];
2212 for (__node_ptr __p =
static_cast<__node_ptr
>(__prev_p->_M_nxt);;
2213 __p = __p->_M_next())
2215 if (this->_M_equals(__k, __code, *__p))
2218 if (__builtin_expect (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt, 0))
2226 template<
typename _Key,
typename _Value,
typename _Alloc,
2227 typename _ExtractKey,
typename _Equal,
2228 typename _Hash,
typename _RangeHash,
typename _Unused,
2229 typename _RehashPolicy,
typename _Traits>
2230 template<
typename _Kt>
2232 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2233 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2234 _M_find_before_node_tr(size_type __bkt,
const _Kt& __k,
2235 __hash_code __code)
const
2238 __node_base_ptr __prev_p = _M_buckets[__bkt];
2242 for (__node_ptr __p =
static_cast<__node_ptr
>(__prev_p->_M_nxt);;
2243 __p = __p->_M_next())
2245 if (this->_M_equals_tr(__k, __code, *__p))
2248 if (__builtin_expect (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt, 0))
2256 template<
typename _Key,
typename _Value,
typename _Alloc,
2257 typename _ExtractKey,
typename _Equal,
2258 typename _Hash,
typename _RangeHash,
typename _Unused,
2259 typename _RehashPolicy,
typename _Traits>
2261 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2262 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2263 _M_locate(
const key_type& __k)
const
2266 __location_type __loc;
2267 const auto __size =
size();
2269 if (__size <= __small_size_threshold())
2271 __loc._M_before = pointer_traits<__node_base_ptr>::
2272 pointer_to(
const_cast<__node_base&
>(_M_before_begin));
2273 while (__loc._M_before->_M_nxt)
2275 if (this->_M_key_equals(__k, *__loc._M_node()))
2277 __loc._M_before = __loc._M_before->_M_nxt;
2279 __loc._M_before =
nullptr;
2282 __loc._M_hash_code = this->_M_hash_code(__k);
2283 __loc._M_bucket_index = _M_bucket_index(__loc._M_hash_code);
2285 if (__size > __small_size_threshold())
2286 __loc._M_before = _M_find_before_node(__loc._M_bucket_index, __k,
2287 __loc._M_hash_code);
2292 template<
typename _Key,
typename _Value,
typename _Alloc,
2293 typename _ExtractKey,
typename _Equal,
2294 typename _Hash,
typename _RangeHash,
typename _Unused,
2295 typename _RehashPolicy,
typename _Traits>
2297 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2298 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2299 _M_get_previous_node(size_type __bkt, __node_ptr __n)
2302 __node_base_ptr __prev_n = _M_buckets[__bkt];
2303 while (__prev_n->_M_nxt != __n)
2304 __prev_n = __prev_n->_M_nxt;
2308#pragma GCC diagnostic push
2309#pragma GCC diagnostic ignored "-Wc++17-extensions"
2310 template<
typename _Key,
typename _Value,
typename _Alloc,
2311 typename _ExtractKey,
typename _Equal,
2312 typename _Hash,
typename _RangeHash,
typename _Unused,
2313 typename _RehashPolicy,
typename _Traits>
2314 template<
typename... _Args>
2316 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2317 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2318 _M_emplace_uniq(_Args&&... __args)
2319 -> pair<iterator, bool>
2321 const key_type* __kp =
nullptr;
2323 if constexpr (
sizeof...(_Args) == 1)
2325 if constexpr (__is_key_type<_Args...>)
2327 const auto& __key = _ExtractKey{}(__args...);
2331 else if constexpr (
sizeof...(_Args) == 2)
2333 if constexpr (__is_key_type<
pair<
const _Args&...>>)
2335 pair<
const _Args&...> __refs(__args...);
2336 const auto& __key = _ExtractKey{}(__refs);
2341 _Scoped_node __node { __node_ptr(),
this };
2342 __hash_code __code = 0;
2343 size_type __bkt = 0;
2345 if (__kp ==
nullptr)
2350 const key_type& __key = _ExtractKey{}(__node._M_node->_M_v());
2354 if (
auto __loc = _M_locate(*__kp))
2356 return { iterator(__loc),
false };
2359 __code = __loc._M_hash_code;
2360 __bkt = __loc._M_bucket_index;
2363 if (!__node._M_node)
2368 auto __pos = _M_insert_unique_node(__bkt, __code, __node._M_node);
2369 __node._M_node =
nullptr;
2370 return { __pos,
true };
2372#pragma GCC diagnostic pop
2374 template<
typename _Key,
typename _Value,
typename _Alloc,
2375 typename _ExtractKey,
typename _Equal,
2376 typename _Hash,
typename _RangeHash,
typename _Unused,
2377 typename _RehashPolicy,
typename _Traits>
2378 template<
typename... _Args>
2380 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2381 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2382 _M_emplace_multi(const_iterator __hint, _Args&&... __args)
2387 const key_type& __k = _ExtractKey{}(__node._M_node->_M_v());
2389 auto __res = this->_M_compute_hash_code(__hint._M_cur, __k);
2391 = _M_insert_multi_node(__res.first, __res.second, __node._M_node);
2392 __node._M_node =
nullptr;
2396 template<
typename _Key,
typename _Value,
typename _Alloc,
2397 typename _ExtractKey,
typename _Equal,
2398 typename _Hash,
typename _RangeHash,
typename _Unused,
2399 typename _RehashPolicy,
typename _Traits>
2401 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2402 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2403 _M_rehash_insert(size_type __n)
2409 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2410 __pair_type __do_rehash
2411 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, __n);
2413 if (__do_rehash.first)
2414 _M_rehash(__do_rehash.second, false_type{});
2416 __rehash_guard._M_guarded_obj =
nullptr;
2420 template<
typename _Key,
typename _Value,
typename _Alloc,
2421 typename _ExtractKey,
typename _Equal,
2422 typename _Hash,
typename _RangeHash,
typename _Unused,
2423 typename _RehashPolicy,
typename _Traits>
2424 template<
typename _InputIterator>
2426 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2427 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2428 _M_insert_range_multi(_InputIterator __first, _InputIterator __last)
2430 _M_rehash_insert(__detail::__distance_fw(__first, __last));
2431 for (; __first != __last; ++__first)
2432 _M_emplace_multi(
cend(), *__first);
2435 template<
typename _Key,
typename _Value,
typename _Alloc,
2436 typename _ExtractKey,
typename _Equal,
2437 typename _Hash,
typename _RangeHash,
typename _Unused,
2438 typename _RehashPolicy,
typename _Traits>
2440 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2441 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2442 _M_compute_hash_code(__node_ptr __hint,
const key_type& __k)
const
2443 -> pair<__node_ptr, __hash_code>
2445 if (
size() <= __small_size_threshold())
2449 for (
auto __it = __hint; __it; __it = __it->_M_next())
2450 if (this->_M_key_equals(__k, *__it))
2451 return { __it, this->_M_hash_code(*__it) };
2454 for (
auto __it = _M_begin(); __it != __hint; __it = __it->_M_next())
2455 if (this->_M_key_equals(__k, *__it))
2456 return { __it, this->_M_hash_code(*__it) };
2461 return { __hint, this->_M_hash_code(__k) };
2464 template<
typename _Key,
typename _Value,
typename _Alloc,
2465 typename _ExtractKey,
typename _Equal,
2466 typename _Hash,
typename _RangeHash,
typename _Unused,
2467 typename _RehashPolicy,
typename _Traits>
2469 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2470 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2471 _M_insert_unique_node(size_type __bkt, __hash_code __code,
2472 __node_ptr __node, size_type __n_elt)
2475 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2477 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
2480 if (__do_rehash.
first)
2482 _M_rehash(__do_rehash.
second, true_type{});
2483 __bkt = _M_bucket_index(__code);
2486 __rehash_guard._M_guarded_obj =
nullptr;
2487 _M_store_code(*__node, __code);
2490 _M_insert_bucket_begin(__bkt, __node);
2492 return iterator(__node);
2495 template<
typename _Key,
typename _Value,
typename _Alloc,
2496 typename _ExtractKey,
typename _Equal,
2497 typename _Hash,
typename _RangeHash,
typename _Unused,
2498 typename _RehashPolicy,
typename _Traits>
2500 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2501 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2502 _M_insert_multi_node(__node_ptr __hint,
2503 __hash_code __code, __node_ptr __node)
2506 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2508 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
2510 if (__do_rehash.
first)
2511 _M_rehash(__do_rehash.
second, false_type{});
2513 __rehash_guard._M_guarded_obj =
nullptr;
2514 _M_store_code(*__node, __code);
2515 const key_type& __k = _ExtractKey{}(__node->_M_v());
2516 size_type __bkt = _M_bucket_index(__code);
2520 __node_base_ptr __prev
2521 = __builtin_expect(__hint !=
nullptr,
false)
2522 && this->_M_equals(__k, __code, *__hint)
2524 : _M_find_before_node(__bkt, __k, __code);
2529 __node->_M_nxt = __prev->_M_nxt;
2530 __prev->_M_nxt = __node;
2531 if (__builtin_expect(__prev == __hint,
false))
2535 && !this->_M_equals(__k, __code, *__node->_M_next()))
2537 size_type __next_bkt = _M_bucket_index(*__node->_M_next());
2538 if (__next_bkt != __bkt)
2539 _M_buckets[__next_bkt] = __node;
2546 _M_insert_bucket_begin(__bkt, __node);
2548 return iterator(__node);
2551 template<
typename _Key,
typename _Value,
typename _Alloc,
2552 typename _ExtractKey,
typename _Equal,
2553 typename _Hash,
typename _RangeHash,
typename _Unused,
2554 typename _RehashPolicy,
typename _Traits>
2556 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2557 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2558 erase(const_iterator __it)
2561 __node_ptr __n = __it._M_cur;
2562 std::size_t __bkt = _M_bucket_index(*__n);
2567 __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
2568 return _M_erase(__bkt, __prev_n, __n);
2571 template<
typename _Key,
typename _Value,
typename _Alloc,
2572 typename _ExtractKey,
typename _Equal,
2573 typename _Hash,
typename _RangeHash,
typename _Unused,
2574 typename _RehashPolicy,
typename _Traits>
2576 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2577 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2578 _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n)
2581 if (__prev_n == _M_buckets[__bkt])
2582 _M_remove_bucket_begin(__bkt, __n->_M_next(),
2583 __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
2584 else if (__n->_M_nxt)
2586 size_type __next_bkt = _M_bucket_index(*__n->_M_next());
2587 if (__next_bkt != __bkt)
2588 _M_buckets[__next_bkt] = __prev_n;
2591 __prev_n->_M_nxt = __n->_M_nxt;
2592 iterator __result(__n->_M_next());
2593 this->_M_deallocate_node(__n);
2599#pragma GCC diagnostic push
2600#pragma GCC diagnostic ignored "-Wc++17-extensions"
2601 template<
typename _Key,
typename _Value,
typename _Alloc,
2602 typename _ExtractKey,
typename _Equal,
2603 typename _Hash,
typename _RangeHash,
typename _Unused,
2604 typename _RehashPolicy,
typename _Traits>
2606 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2607 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2608 erase(
const key_type& __k)
2611 auto __loc = _M_locate(__k);
2615 __node_base_ptr __prev_n = __loc._M_before;
2616 __node_ptr __n = __loc._M_node();
2617 auto __bkt = __loc._M_bucket_index;
2618 if (__bkt == size_type(-1))
2619 __bkt = _M_bucket_index(*__n);
2621 if constexpr (__unique_keys::value)
2623 _M_erase(__bkt, __prev_n, __n);
2634 __node_ptr __n_last = __n->_M_next();
2635 while (__n_last && this->_M_node_equals(*__n, *__n_last))
2636 __n_last = __n_last->_M_next();
2638 std::size_t __n_last_bkt
2639 = __n_last ? _M_bucket_index(*__n_last) : __bkt;
2642 size_type __result = 0;
2645 __node_ptr __p = __n->_M_next();
2646 this->_M_deallocate_node(__n);
2650 while (__n != __n_last);
2652 _M_element_count -= __result;
2653 if (__prev_n == _M_buckets[__bkt])
2654 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
2655 else if (__n_last_bkt != __bkt)
2656 _M_buckets[__n_last_bkt] = __prev_n;
2657 __prev_n->_M_nxt = __n_last;
2661#pragma GCC diagnostic pop
2663 template<
typename _Key,
typename _Value,
typename _Alloc,
2664 typename _ExtractKey,
typename _Equal,
2665 typename _Hash,
typename _RangeHash,
typename _Unused,
2666 typename _RehashPolicy,
typename _Traits>
2668 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2669 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2670 erase(const_iterator __first, const_iterator __last)
2673 __node_ptr __n = __first._M_cur;
2674 __node_ptr __last_n = __last._M_cur;
2675 if (__n == __last_n)
2676 return iterator(__n);
2678 std::size_t __bkt = _M_bucket_index(*__n);
2680 __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
2681 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2682 std::size_t __n_bkt = __bkt;
2687 __node_ptr __tmp = __n;
2688 __n = __n->_M_next();
2689 this->_M_deallocate_node(__tmp);
2693 __n_bkt = _M_bucket_index(*__n);
2695 while (__n != __last_n && __n_bkt == __bkt);
2696 if (__is_bucket_begin)
2697 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2698 if (__n == __last_n)
2700 __is_bucket_begin =
true;
2704 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
2705 _M_buckets[__n_bkt] = __prev_n;
2706 __prev_n->_M_nxt = __n;
2707 return iterator(__n);
2710 template<
typename _Key,
typename _Value,
typename _Alloc,
2711 typename _ExtractKey,
typename _Equal,
2712 typename _Hash,
typename _RangeHash,
typename _Unused,
2713 typename _RehashPolicy,
typename _Traits>
2715 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2716 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2719 this->_M_deallocate_nodes(_M_begin());
2720 std::fill_n(_M_buckets, _M_bucket_count,
nullptr);
2721 _M_element_count = 0;
2722 _M_before_begin._M_nxt =
nullptr;
2725 template<
typename _Key,
typename _Value,
typename _Alloc,
2726 typename _ExtractKey,
typename _Equal,
2727 typename _Hash,
typename _RangeHash,
typename _Unused,
2728 typename _RehashPolicy,
typename _Traits>
2730 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2731 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2732 rehash(size_type __bkt_count)
2734 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2736 =
std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
2738 __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count);
2740 if (__bkt_count != _M_bucket_count)
2742 _M_rehash(__bkt_count, __unique_keys{});
2743 __rehash_guard._M_guarded_obj =
nullptr;
2748 template<
typename _Key,
typename _Value,
typename _Alloc,
2749 typename _ExtractKey,
typename _Equal,
2750 typename _Hash,
typename _RangeHash,
typename _Unused,
2751 typename _RehashPolicy,
typename _Traits>
2753 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2754 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2755 _M_rehash(size_type __bkt_count, true_type )
2757 __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2758 __node_ptr __p = _M_begin();
2759 _M_before_begin._M_nxt =
nullptr;
2760 std::size_t __bbegin_bkt = 0;
2763 __node_ptr __next = __p->_M_next();
2765 = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
2766 if (!__new_buckets[__bkt])
2768 __p->_M_nxt = _M_before_begin._M_nxt;
2769 _M_before_begin._M_nxt = __p;
2770 __new_buckets[__bkt] = &_M_before_begin;
2772 __new_buckets[__bbegin_bkt] = __p;
2773 __bbegin_bkt = __bkt;
2777 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2778 __new_buckets[__bkt]->_M_nxt = __p;
2784 _M_deallocate_buckets();
2785 _M_bucket_count = __bkt_count;
2786 _M_buckets = __new_buckets;
2791 template<
typename _Key,
typename _Value,
typename _Alloc,
2792 typename _ExtractKey,
typename _Equal,
2793 typename _Hash,
typename _RangeHash,
typename _Unused,
2794 typename _RehashPolicy,
typename _Traits>
2796 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2797 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2798 _M_rehash(size_type __bkt_count, false_type )
2800 __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2801 __node_ptr __p = _M_begin();
2802 _M_before_begin._M_nxt =
nullptr;
2803 std::size_t __bbegin_bkt = 0;
2804 std::size_t __prev_bkt = 0;
2805 __node_ptr __prev_p =
nullptr;
2806 bool __check_bucket =
false;
2810 __node_ptr __next = __p->_M_next();
2812 = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
2814 if (__prev_p && __prev_bkt == __bkt)
2819 __p->_M_nxt = __prev_p->_M_nxt;
2820 __prev_p->_M_nxt = __p;
2827 __check_bucket =
true;
2835 if (__prev_p->_M_nxt)
2837 std::size_t __next_bkt
2838 = __hash_code_base::_M_bucket_index(
2839 *__prev_p->_M_next(), __bkt_count);
2840 if (__next_bkt != __prev_bkt)
2841 __new_buckets[__next_bkt] = __prev_p;
2843 __check_bucket =
false;
2846 if (!__new_buckets[__bkt])
2848 __p->_M_nxt = _M_before_begin._M_nxt;
2849 _M_before_begin._M_nxt = __p;
2850 __new_buckets[__bkt] = &_M_before_begin;
2852 __new_buckets[__bbegin_bkt] = __p;
2853 __bbegin_bkt = __bkt;
2857 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2858 __new_buckets[__bkt]->_M_nxt = __p;
2866 if (__check_bucket && __prev_p->_M_nxt)
2868 std::size_t __next_bkt
2869 = __hash_code_base::_M_bucket_index(*__prev_p->_M_next(),
2871 if (__next_bkt != __prev_bkt)
2872 __new_buckets[__next_bkt] = __prev_p;
2875 _M_deallocate_buckets();
2876 _M_bucket_count = __bkt_count;
2877 _M_buckets = __new_buckets;
2880#pragma GCC diagnostic push
2881#pragma GCC diagnostic ignored "-Wc++17-extensions"
2886 template<
typename _Key,
typename _Value,
typename _Alloc,
2887 typename _ExtractKey,
typename _Equal,
2888 typename _Hash,
typename _RangeHash,
typename _Unused,
2889 typename _RehashPolicy,
typename _Traits>
2891 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2892 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2893 _M_equal(
const _Hashtable& __other)
const
2895 if (
size() != __other.size())
2898 if constexpr (__unique_keys::value)
2899 for (
auto __x_n = _M_begin(); __x_n; __x_n = __x_n->_M_next())
2901 std::size_t __ybkt = __other._M_bucket_index_ext(*__x_n);
2902 auto __prev_n = __other._M_buckets[__ybkt];
2906 for (__node_ptr __n =
static_cast<__node_ptr
>(__prev_n->_M_nxt);;
2907 __n = __n->_M_next())
2909 if (__n->_M_v() == __x_n->_M_v())
2913 || __other._M_bucket_index(*__n->_M_next()) != __ybkt)
2918 for (
auto __x_n = _M_begin(); __x_n;)
2920 std::size_t __x_count = 1;
2921 auto __x_n_end = __x_n->_M_next();
2923 && key_eq()(_ExtractKey{}(__x_n->_M_v()),
2924 _ExtractKey{}(__x_n_end->_M_v()));
2925 __x_n_end = __x_n_end->_M_next())
2928 std::size_t __ybkt = __other._M_bucket_index_ext(*__x_n);
2929 auto __y_prev_n = __other._M_buckets[__ybkt];
2933 __node_ptr __y_n =
static_cast<__node_ptr
>(__y_prev_n->_M_nxt);
2936 if (key_eq()(_ExtractKey{}(__y_n->_M_v()),
2937 _ExtractKey{}(__x_n->_M_v())))
2940 auto __y_ref_n = __y_n;
2941 for (__y_n = __y_n->_M_next(); __y_n; __y_n = __y_n->_M_next())
2942 if (!__other._M_node_equals(*__y_ref_n, *__y_n))
2945 if (!__y_n || __other._M_bucket_index(*__y_n) != __ybkt)
2949 auto __y_n_end = __y_n;
2950 for (; __y_n_end; __y_n_end = __y_n_end->_M_next())
2951 if (--__x_count == 0)
2957 const_iterator __itx(__x_n), __itx_end(__x_n_end);
2958 const_iterator __ity(__y_n);
2959 if (!std::is_permutation(__itx, __itx_end, __ity))
2967#pragma GCC diagnostic pop
2969#if __cplusplus > 201402L
2970 template<
typename,
typename,
typename>
class _Hash_merge_helper { };
2973#if __cpp_deduction_guides >= 201606
2975 template<
typename _Hash>
2976 using _RequireNotAllocatorOrIntegral
2977 = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
2981_GLIBCXX_END_NAMESPACE_VERSION
2984#pragma GCC diagnostic pop
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
_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 empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a 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.
Struct holding two objects of arbitrary type.
_T1 first
The first member.
_T2 second
The second member.