31#ifndef _HASHTABLE_POLICY_H
32#define _HASHTABLE_POLICY_H 1
43namespace std _GLIBCXX_VISIBILITY(default)
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
48 template<
typename _Key,
typename _Value,
typename _Alloc,
49 typename _ExtractKey,
typename _Equal,
50 typename _Hash,
typename _RangeHash,
typename _Unused,
51 typename _RehashPolicy,
typename _Traits>
61 template<
typename _Key,
typename _Value,
typename _ExtractKey,
62 typename _Equal,
typename _Hash,
typename _RangeHash,
63 typename _Unused,
typename _Traits>
64 struct _Hashtable_base;
66#pragma GCC diagnostic push
67#pragma GCC diagnostic ignored "-Wc++17-extensions"
70 template<
typename _Iterator>
71 inline typename std::iterator_traits<_Iterator>::difference_type
72 __distance_fw(_Iterator __first, _Iterator __last)
74 using _Cat =
typename std::iterator_traits<_Iterator>::iterator_category;
75 if constexpr (is_convertible<_Cat, forward_iterator_tag>::value)
78 return __first != __last ? 1 : 0;
80#pragma GCC diagnostic pop
84 template<
typename _Tp>
86 operator()(_Tp&& __x)
const noexcept
92 template<
typename _Pair>
95 template<
typename _Tp,
typename _Up>
96 struct __1st_type<
pair<_Tp, _Up>>
97 {
using type = _Tp; };
99 template<
typename _Tp,
typename _Up>
100 struct __1st_type<const
pair<_Tp, _Up>>
101 {
using type =
const _Tp; };
103 template<
typename _Pair>
104 struct __1st_type<_Pair&>
105 {
using type =
typename __1st_type<_Pair>::type&; };
107 template<
typename _Tp>
108 typename __1st_type<_Tp>::type&&
109 operator()(_Tp&& __x)
const noexcept
113 template<
typename _ExKey>
117 struct _NodeBuilder<_Select1st>
119 template<
typename _Kt,
typename _Arg,
typename _NodeGenerator>
121 _S_build(_Kt&& __k, _Arg&& __arg, _NodeGenerator& __node_gen)
122 ->
typename _NodeGenerator::__node_ptr
130 struct _NodeBuilder<_Identity>
132 template<
typename _Kt,
typename _Arg,
typename _NodeGenerator>
134 _S_build(_Kt&& __k, _Arg&&, _NodeGenerator& __node_gen)
135 ->
typename _NodeGenerator::__node_ptr
139 template<
typename _HashtableAlloc,
typename _NodePtr>
142 _HashtableAlloc& _M_h;
148 _M_h._M_deallocate_node_ptr(_M_ptr);
152 template<
typename _NodeAlloc>
153 struct _Hashtable_alloc;
157 template<
typename _NodeAlloc>
158 struct _ReuseOrAllocNode
161 using __node_alloc_type = _NodeAlloc;
162 using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
163 using __node_alloc_traits =
164 typename __hashtable_alloc::__node_alloc_traits;
167 using __node_ptr =
typename __hashtable_alloc::__node_ptr;
169 _ReuseOrAllocNode(__node_ptr __nodes, __hashtable_alloc& __h)
170 : _M_nodes(__nodes), _M_h(__h) { }
171 _ReuseOrAllocNode(
const _ReuseOrAllocNode&) =
delete;
174 { _M_h._M_deallocate_nodes(_M_nodes); }
176#pragma GCC diagnostic push
177#pragma GCC diagnostic ignored "-Wc++17-extensions"
178 template<
typename _Arg>
180 operator()(_Arg&& __arg)
185 using value_type =
typename _NodeAlloc::value_type::value_type;
187 __node_ptr __node = _M_nodes;
188 if constexpr (is_assignable<value_type&, _Arg>::value)
191 _M_nodes = _M_nodes->_M_next();
192 __node->_M_nxt =
nullptr;
196 _M_nodes = _M_nodes->_M_next();
197 __node->_M_nxt =
nullptr;
198 auto& __a = _M_h._M_node_allocator();
199 __node_alloc_traits::destroy(__a, __node->_M_valptr());
200 _NodePtrGuard<__hashtable_alloc, __node_ptr>
201 __guard{ _M_h, __node };
202 __node_alloc_traits::construct(__a, __node->_M_valptr(),
204 __guard._M_ptr =
nullptr;
208#pragma GCC diagnostic pop
212 __hashtable_alloc& _M_h;
217 template<
typename _NodeAlloc>
221 using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
224 using __node_ptr =
typename __hashtable_alloc::__node_ptr;
226 _AllocNode(__hashtable_alloc& __h)
229 template<
typename... _Args>
231 operator()(_Args&&... __args)
const
235 __hashtable_alloc& _M_h;
263 template<
bool _Cache_hash_code,
bool _Constant_iterators,
bool _Unique_keys>
264 struct _Hashtable_traits
266 using __hash_cached = __bool_constant<_Cache_hash_code>;
267 using __constant_iterators = __bool_constant<_Constant_iterators>;
268 using __unique_keys = __bool_constant<_Unique_keys>;
277 template<
typename _Hash>
278 struct _Hashtable_hash_traits
280 static constexpr size_t
281 __small_size_threshold() noexcept
282 {
return std::__is_fast_hash<_Hash>::value ? 0 : 20; }
293 struct _Hash_node_base
295 _Hash_node_base* _M_nxt;
297 _Hash_node_base() noexcept : _M_nxt() { }
299 _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
307 template<
typename _Value>
308 struct _Hash_node_value_base
310 using value_type = _Value;
312 __gnu_cxx::__aligned_buffer<_Value> _M_storage;
316 [[__gnu__::__always_inline__]]
319 {
return _M_storage._M_ptr(); }
321 [[__gnu__::__always_inline__]]
323 _M_valptr() const noexcept
324 {
return _M_storage._M_ptr(); }
326 [[__gnu__::__always_inline__]]
329 {
return *_M_valptr(); }
331 [[__gnu__::__always_inline__]]
333 _M_v() const noexcept
334 {
return *_M_valptr(); }
340 template<
bool _Cache_hash_code>
341 struct _Hash_node_code_cache
348 struct _Hash_node_code_cache<true>
349 {
size_t _M_hash_code; };
351 template<
typename _Value,
bool _Cache_hash_code>
352 struct _Hash_node_value
353 : _Hash_node_value_base<_Value>
354 , _Hash_node_code_cache<_Cache_hash_code>
360 template<
typename _Value,
bool _Cache_hash_code>
363 , _Hash_node_value<_Value, _Cache_hash_code>
366 _M_next() const noexcept
367 {
return static_cast<_Hash_node*
>(this->_M_nxt); }
371 template<
typename _Value,
bool _Cache_hash_code>
372 struct _Node_iterator_base
374 using __node_type = _Hash_node<_Value, _Cache_hash_code>;
378 _Node_iterator_base() : _M_cur(nullptr) { }
379 _Node_iterator_base(__node_type* __p) noexcept
384 { _M_cur = _M_cur->_M_next(); }
387 operator==(
const _Node_iterator_base& __x,
const _Node_iterator_base& __y)
389 {
return __x._M_cur == __y._M_cur; }
391#if __cpp_impl_three_way_comparison < 201907L
393 operator!=(
const _Node_iterator_base& __x,
const _Node_iterator_base& __y)
395 {
return __x._M_cur != __y._M_cur; }
400 template<
typename _Value,
bool __constant_iterators,
bool __cache>
401 struct _Node_iterator
402 :
public _Node_iterator_base<_Value, __cache>
405 using __base_type = _Node_iterator_base<_Value, __cache>;
406 using __node_type =
typename __base_type::__node_type;
409 using value_type = _Value;
410 using difference_type = ptrdiff_t;
411 using iterator_category = forward_iterator_tag;
413 using pointer = __conditional_t<__constant_iterators,
414 const value_type*, value_type*>;
416 using reference = __conditional_t<__constant_iterators,
417 const value_type&, value_type&>;
419 _Node_iterator() =
default;
422 _Node_iterator(__node_type* __p) noexcept
423 : __base_type(__p) { }
427 {
return this->_M_cur->_M_v(); }
430 operator->() const noexcept
431 {
return this->_M_cur->_M_valptr(); }
434 operator++() noexcept
441 operator++(
int)
noexcept
443 _Node_iterator __tmp(*
this);
448#if __cpp_impl_three_way_comparison >= 201907L
450 operator==(
const _Node_iterator&,
const _Node_iterator&) =
default;
453 operator==(
const _Node_iterator& __x,
const _Node_iterator& __y)
noexcept
455 const __base_type& __bx = __x;
456 const __base_type& __by = __y;
461 operator!=(
const _Node_iterator& __x,
const _Node_iterator& __y)
noexcept
462 {
return !(__x == __y); }
467 template<
typename _Value,
bool __constant_iterators,
bool __cache>
468 struct _Node_const_iterator
469 :
public _Node_iterator_base<_Value, __cache>
472 using __base_type = _Node_iterator_base<_Value, __cache>;
473 using __node_type =
typename __base_type::__node_type;
477 = _Node_iterator<_Value, __constant_iterators, __cache>;
480 using value_type = _Value;
481 using difference_type = ptrdiff_t;
482 using iterator_category = forward_iterator_tag;
484 using pointer =
const value_type*;
485 using reference =
const value_type&;
487 _Node_const_iterator() =
default;
490 _Node_const_iterator(__node_type* __p) noexcept
491 : __base_type(__p) { }
493 _Node_const_iterator(
const __iterator& __x) noexcept
494 : __base_type(__x._M_cur) { }
498 {
return this->_M_cur->_M_v(); }
501 operator->() const noexcept
502 {
return this->_M_cur->_M_valptr(); }
504 _Node_const_iterator&
505 operator++() noexcept
512 operator++(
int)
noexcept
514 _Node_const_iterator __tmp(*
this);
519#if __cpp_impl_three_way_comparison >= 201907L
521 operator==(
const _Node_const_iterator&,
522 const _Node_const_iterator&) =
default;
525 operator==(
const _Node_const_iterator& __x,
const __iterator& __y)
527 const __base_type& __bx = __x;
528 const __base_type& __by = __y;
533 operator==(
const _Node_const_iterator& __x,
534 const _Node_const_iterator& __y)
noexcept
536 const __base_type& __bx = __x;
537 const __base_type& __by = __y;
542 operator!=(
const _Node_const_iterator& __x,
543 const _Node_const_iterator& __y)
noexcept
544 {
return !(__x == __y); }
547 operator==(
const _Node_const_iterator& __x,
548 const __iterator& __y)
noexcept
550 const __base_type& __bx = __x;
551 const __base_type& __by = __y;
556 operator!=(
const _Node_const_iterator& __x,
557 const __iterator& __y)
noexcept
558 {
return !(__x == __y); }
561 operator==(
const __iterator& __x,
562 const _Node_const_iterator& __y)
noexcept
564 const __base_type& __bx = __x;
565 const __base_type& __by = __y;
570 operator!=(
const __iterator& __x,
571 const _Node_const_iterator& __y)
noexcept
572 {
return !(__x == __y); }
581 struct _Mod_range_hashing
584 operator()(
size_t __num,
size_t __den)
const noexcept
585 {
return __num % __den; }
593 struct _Default_ranged_hash { };
597 struct _Prime_rehash_policy
601 _Prime_rehash_policy(
float __z = 1.0) noexcept
602 : _M_max_load_factor(__z), _M_next_resize(0) { }
605 max_load_factor() const noexcept
606 {
return _M_max_load_factor; }
611 _M_next_bkt(
size_t __n)
const;
615 _M_bkt_for_elements(
size_t __n)
const
616 {
return __builtin_ceil(__n / (
double)_M_max_load_factor); }
624 _M_need_rehash(
size_t __n_bkt,
size_t __n_elt,
625 size_t __n_ins)
const;
627 using _State = size_t;
631 {
return _M_next_resize; }
635 { _M_next_resize = 0; }
638 _M_reset(_State __state)
639 { _M_next_resize = __state; }
641 static const size_t _S_growth_factor = 2;
643 float _M_max_load_factor;
646 mutable size_t _M_next_resize;
650 struct _Mask_range_hashing
653 operator()(
size_t __num,
size_t __den)
const noexcept
654 {
return __num & (__den - 1); }
659 __clp2(
size_t __n)
noexcept
665 const unsigned __lz =
sizeof(size_t) >
sizeof(
long)
666 ? __builtin_clzll(__n - 1ull)
667 : __builtin_clzl(__n - 1ul);
669 return (
size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
674 struct _Power2_rehash_policy
678 _Power2_rehash_policy(
float __z = 1.0) noexcept
679 : _M_max_load_factor(__z), _M_next_resize(0) { }
682 max_load_factor() const noexcept
683 {
return _M_max_load_factor; }
688 _M_next_bkt(
size_t __n)
noexcept
697 const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
698 size_t __res = __clp2(__n);
708 if (__res == __max_bkt)
712 _M_next_resize = size_t(-1);
715 = __builtin_floor(__res * (
double)_M_max_load_factor);
722 _M_bkt_for_elements(
size_t __n)
const noexcept
723 {
return __builtin_ceil(__n / (
double)_M_max_load_factor); }
730 _M_need_rehash(
size_t __n_bkt,
size_t __n_elt,
size_t __n_ins)
noexcept
732 if (__n_elt + __n_ins > _M_next_resize)
739 / (double)_M_max_load_factor;
740 if (__min_bkts >= __n_bkt)
743 __n_bkt * _S_growth_factor)) };
746 = __builtin_floor(__n_bkt * (
double)_M_max_load_factor);
753 using _State = size_t;
756 _M_state() const noexcept
757 {
return _M_next_resize; }
761 { _M_next_resize = 0; }
764 _M_reset(_State __state)
noexcept
765 { _M_next_resize = __state; }
767 static const size_t _S_growth_factor = 2;
769 float _M_max_load_factor;
770 size_t _M_next_resize;
773 template<
typename _RehashPolicy>
774 struct _RehashStateGuard
776 _RehashPolicy* _M_guarded_obj;
777 typename _RehashPolicy::_State _M_prev_state;
779 _RehashStateGuard(_RehashPolicy& __policy)
781 , _M_prev_state(__policy._M_state())
783 _RehashStateGuard(
const _RehashStateGuard&) =
delete;
788 _M_guarded_obj->_M_reset(_M_prev_state);
810 template<
typename _Key,
typename _Value,
typename _Alloc,
811 typename _ExtractKey,
typename _Equal,
812 typename _Hash,
typename _RangeHash,
typename _Unused,
813 typename _RehashPolicy,
typename _Traits,
814 bool _Unique_keys = _Traits::__unique_keys::value>
815 struct _Map_base { };
818 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
819 typename _Hash,
typename _RangeHash,
typename _Unused,
820 typename _RehashPolicy,
typename _Traits>
821 struct _Map_base<_Key,
pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
822 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
824 using mapped_type = _Val;
828 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
829 typename _Hash,
typename _RangeHash,
typename _Unused,
830 typename _RehashPolicy,
typename _Traits>
831 struct _Map_base<_Key,
pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
832 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
835 using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
836 _Select1st, _Equal, _Hash,
840 using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
841 _Select1st, _Equal, _Hash, _RangeHash,
842 _Unused, _RehashPolicy, _Traits>;
844 using __hash_code =
typename __hashtable_base::__hash_code;
847 using key_type =
typename __hashtable_base::key_type;
848 using mapped_type = _Val;
851 operator[](
const key_type& __k);
854 operator[](key_type&& __k);
859 at(
const key_type& __k)
861 auto __ite =
static_cast<__hashtable*
>(
this)->find(__k);
863 __throw_out_of_range(__N(
"unordered_map::at"));
864 return __ite->second;
868 at(
const key_type& __k)
const
870 auto __ite =
static_cast<const __hashtable*
>(
this)->find(__k);
872 __throw_out_of_range(__N(
"unordered_map::at"));
873 return __ite->second;
876 template <
typename _Kt>
878 _M_at_tr(
const _Kt& __k)
880 auto __ite =
static_cast<__hashtable*
>(
this)->_M_find_tr(__k);
882 __throw_out_of_range(__N(
"unordered_map::at"));
883 return __ite->second;
886 template <
typename _Kt>
888 _M_at_tr(
const _Kt& __k)
const
890 auto __ite =
static_cast<const __hashtable*
>(
this)->_M_find_tr(__k);
892 __throw_out_of_range(__N(
"unordered_map::at"));
893 return __ite->second;
897 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
898 typename _Hash,
typename _RangeHash,
typename _Unused,
899 typename _RehashPolicy,
typename _Traits>
901 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
902 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
true>::
903 operator[](
const key_type& __k)
906 __hashtable* __h =
static_cast<__hashtable*
>(
this);
907 __hash_code __code = __h->_M_hash_code(__k);
908 size_t __bkt = __h->_M_bucket_index(__code);
909 if (
auto __node = __h->_M_find_node(__bkt, __k, __code))
910 return __node->_M_v().second;
912 typename __hashtable::_Scoped_node __node {
915 std::tuple<const key_type&>(__k),
919 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
920 __node._M_node =
nullptr;
921 return __pos->second;
924 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
925 typename _Hash,
typename _RangeHash,
typename _Unused,
926 typename _RehashPolicy,
typename _Traits>
928 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
929 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
true>::
930 operator[](key_type&& __k)
933 __hashtable* __h =
static_cast<__hashtable*
>(
this);
934 __hash_code __code = __h->_M_hash_code(__k);
935 size_t __bkt = __h->_M_bucket_index(__code);
936 if (
auto __node = __h->_M_find_node(__bkt, __k, __code))
937 return __node->_M_v().second;
939 typename __hashtable::_Scoped_node __node {
946 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
947 __node._M_node =
nullptr;
948 return __pos->second;
952 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
953 typename _Hash,
typename _RangeHash,
typename _Unused,
954 typename _RehashPolicy,
typename _Traits,
bool __uniq>
955 struct _Map_base<const _Key,
pair<const _Key, _Val>,
956 _Alloc, _Select1st, _Equal, _Hash,
957 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
958 : _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal, _Hash,
959 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
962 template<
typename _Policy>
963 using __has_load_factor =
typename _Policy::__has_load_factor;
971 template<
typename _Key,
typename _Value,
typename _Alloc,
972 typename _ExtractKey,
typename _Equal,
973 typename _Hash,
typename _RangeHash,
typename _Unused,
974 typename _RehashPolicy,
typename _Traits,
976 __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
980 template<
typename _Key,
typename _Value,
typename _Alloc,
981 typename _ExtractKey,
typename _Equal,
982 typename _Hash,
typename _RangeHash,
typename _Unused,
983 typename _RehashPolicy,
typename _Traits>
984 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
985 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
991 template<
typename _Key,
typename _Value,
typename _Alloc,
992 typename _ExtractKey,
typename _Equal,
993 typename _Hash,
typename _RangeHash,
typename _Unused,
994 typename _RehashPolicy,
typename _Traits>
995 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
996 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1000 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1001 _Equal, _Hash, _RangeHash, _Unused,
1002 _RehashPolicy, _Traits>;
1006 max_load_factor() const noexcept
1008 const __hashtable* __this =
static_cast<const __hashtable*
>(
this);
1009 return __this->__rehash_policy().max_load_factor();
1013 max_load_factor(
float __z)
1015 __hashtable* __this =
static_cast<__hashtable*
>(
this);
1016 __this->__rehash_policy(_RehashPolicy(__z));
1022 __hashtable* __this =
static_cast<__hashtable*
>(
this);
1023 __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
1032 template<
typename _Tp,
1033 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
1034 struct _Hashtable_ebo_helper
1036 [[__no_unique_address__]] _Tp _M_obj;
1039#if ! _GLIBCXX_INLINE_VERSION
1042 template<
typename _Tp>
1043 struct _Hashtable_ebo_helper<_Tp, false>
1055 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1056 typename _Hash,
typename _RangeHash,
typename _Unused,
1057 bool __cache_hash_code>
1058 struct _Local_iterator_base;
1061 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1062 typename _Hash,
typename _RangeHash,
typename _Unused,
1064 struct _Hash_code_base
1067 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1068 _Hash, _RangeHash, _Unused, false>;
1070 using hasher = _Hash;
1073 hash_function()
const
1074 {
return _M_hash._M_obj; }
1077 [[__no_unique_address__]] _Hashtable_ebo_helper<_Hash> _M_hash{};
1079 using __hash_code = size_t;
1083 _Hash_code_base() =
default;
1085 _Hash_code_base(
const _Hash& __hash) : _M_hash{__hash} { }
1088 _M_hash_code(
const _Key& __k)
const
1090 static_assert(__is_invocable<const _Hash&, const _Key&>{},
1091 "hash function must be invocable with an argument of key type");
1092 return _M_hash._M_obj(__k);
1095 template<
typename _Kt>
1097 _M_hash_code_tr(
const _Kt& __k)
const
1099 static_assert(__is_invocable<const _Hash&, const _Kt&>{},
1100 "hash function must be invocable with an argument of key type");
1101 return _M_hash._M_obj(__k);
1105 _M_hash_code(
const _Hash_node_value<_Value, false>& __n)
const
1106 {
return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1109 _M_hash_code(
const _Hash_node_value<_Value, true>& __n)
const
1110 {
return __n._M_hash_code; }
1113 _M_bucket_index(__hash_code __c,
size_t __bkt_count)
const
1114 {
return _RangeHash{}(__c, __bkt_count); }
1117 _M_bucket_index(
const _Hash_node_value<_Value, false>& __n,
1118 size_t __bkt_count)
const
1119 noexcept(
noexcept(declval<const _Hash&>()(declval<const _Key&>())) )
1121 return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
1126 _M_bucket_index(
const _Hash_node_value<_Value, true>& __n,
1127 size_t __bkt_count)
const noexcept
1128 {
return _RangeHash{}(__n._M_hash_code, __bkt_count); }
1132 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1133 typename _Hash,
typename _RangeHash,
typename _Unused>
1134 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1135 _Hash, _RangeHash, _Unused, true>
1136 :
public _Node_iterator_base<_Value, true>
1139 using __base_node_iter = _Node_iterator_base<_Value, true>;
1140 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1141 _Hash, _RangeHash, _Unused,
true>;
1143 _Local_iterator_base() =
default;
1145 _Local_iterator_base(
const __hash_code_base&,
1146 _Hash_node<_Value, true>* __p,
1147 size_t __bkt,
size_t __bkt_count)
1148 : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1154 __base_node_iter::_M_incr();
1158 = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
1159 if (__bkt != _M_bucket)
1160 this->_M_cur =
nullptr;
1164 size_t _M_bucket = 0;
1165 size_t _M_bucket_count = 0;
1169 _M_get_bucket()
const {
return _M_bucket; }
1175 template<
typename _Hash>
1176 struct _Hash_obj_storage
1178 union _Uninit_storage
1180 _Uninit_storage() noexcept { }
1181 ~_Uninit_storage() { }
1183 [[__no_unique_address__]] _Hash _M_h;
1186 [[__no_unique_address__]] _Uninit_storage _M_u;
1190 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1191 typename _Hash,
typename _RangeHash,
typename _Unused>
1192 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1193 _Hash, _RangeHash, _Unused, false>
1194 : _Hash_obj_storage<_Hash>, _Node_iterator_base<_Value, false>
1197 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1198 _Hash, _RangeHash, _Unused,
false>;
1199 using __hash_obj_storage = _Hash_obj_storage<_Hash>;
1200 using __node_iter_base = _Node_iterator_base<_Value, false>;
1202 _Local_iterator_base() =
default;
1204 _Local_iterator_base(
const __hash_code_base& __base,
1205 _Hash_node<_Value, false>* __p,
1206 size_t __bkt,
size_t __bkt_count)
1207 : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1208 { _M_init(
__base._M_hash._M_obj); }
1210 ~_Local_iterator_base()
1212 if (_M_bucket_count !=
size_t(-1))
1216 _Local_iterator_base(
const _Local_iterator_base& __iter)
1217 : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket)
1218 , _M_bucket_count(__iter._M_bucket_count)
1220 if (_M_bucket_count !=
size_t(-1))
1221 _M_init(__iter._M_h());
1224 _Local_iterator_base&
1225 operator=(
const _Local_iterator_base& __iter)
1227 if (_M_bucket_count !=
size_t(-1))
1229 this->_M_cur = __iter._M_cur;
1230 _M_bucket = __iter._M_bucket;
1231 _M_bucket_count = __iter._M_bucket_count;
1232 if (_M_bucket_count !=
size_t(-1))
1233 _M_init(__iter._M_h());
1240 __node_iter_base::_M_incr();
1243 const auto __code = _M_h()(_ExtractKey{}(this->_M_cur->_M_v()));
1244 size_t __bkt = _RangeHash{}(__code, _M_bucket_count);
1245 if (__bkt != _M_bucket)
1246 this->_M_cur =
nullptr;
1250 size_t _M_bucket = 0;
1251 size_t _M_bucket_count = -1;
1254 _M_init(
const _Hash& __h)
1258 _M_destroy() { __hash_obj_storage::_M_u._M_h.~_Hash(); }
1261 _M_h()
const {
return __hash_obj_storage::_M_u._M_h; }
1265 _M_get_bucket()
const {
return _M_bucket; }
1269 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1270 typename _Hash,
typename _RangeHash,
typename _Unused,
1271 bool __constant_iterators,
bool __cache>
1272 struct _Local_iterator
1273 :
public _Local_iterator_base<_Key, _Value, _ExtractKey,
1274 _Hash, _RangeHash, _Unused, __cache>
1277 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1278 _Hash, _RangeHash, _Unused, __cache>;
1279 using __hash_code_base =
typename __base_type::__hash_code_base;
1282 using value_type = _Value;
1283 using pointer = __conditional_t<__constant_iterators,
1284 const value_type*, value_type*>;
1285 using reference = __conditional_t<__constant_iterators,
1286 const value_type&, value_type&>;
1287 using difference_type = ptrdiff_t;
1288 using iterator_category = forward_iterator_tag;
1290 _Local_iterator() =
default;
1292 _Local_iterator(
const __hash_code_base& __base,
1293 _Hash_node<_Value, __cache>* __n,
1294 size_t __bkt,
size_t __bkt_count)
1295 : __base_type(
__base, __n, __bkt, __bkt_count)
1300 {
return this->_M_cur->_M_v(); }
1304 {
return this->_M_cur->_M_valptr(); }
1316 _Local_iterator __tmp(*
this);
1323 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1324 typename _Hash,
typename _RangeHash,
typename _Unused,
1325 bool __constant_iterators,
bool __cache>
1326 struct _Local_const_iterator
1327 :
public _Local_iterator_base<_Key, _Value, _ExtractKey,
1328 _Hash, _RangeHash, _Unused, __cache>
1331 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1332 _Hash, _RangeHash, _Unused, __cache>;
1333 using __hash_code_base =
typename __base_type::__hash_code_base;
1336 using value_type = _Value;
1337 using pointer =
const value_type*;
1338 using reference =
const value_type&;
1339 using difference_type = ptrdiff_t;
1340 using iterator_category = forward_iterator_tag;
1342 _Local_const_iterator() =
default;
1344 _Local_const_iterator(
const __hash_code_base& __base,
1345 _Hash_node<_Value, __cache>* __n,
1346 size_t __bkt,
size_t __bkt_count)
1347 : __base_type(
__base, __n, __bkt, __bkt_count)
1350 _Local_const_iterator(
const _Local_iterator<_Key, _Value, _ExtractKey,
1351 _Hash, _RangeHash, _Unused,
1352 __constant_iterators,
1359 {
return this->_M_cur->_M_v(); }
1363 {
return this->_M_cur->_M_valptr(); }
1365 _Local_const_iterator&
1372 _Local_const_iterator
1375 _Local_const_iterator __tmp(*
this);
1390 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1391 typename _Equal,
typename _Hash,
typename _RangeHash,
1392 typename _Unused,
typename _Traits>
1393 struct _Hashtable_base
1394 :
public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1395 _Unused, _Traits::__hash_cached::value>
1398 using key_type = _Key;
1399 using value_type = _Value;
1400 using key_equal = _Equal;
1401 using size_type = size_t;
1402 using difference_type = ptrdiff_t;
1404 using __traits_type = _Traits;
1405 using __hash_cached =
typename __traits_type::__hash_cached;
1407 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1408 _Hash, _RangeHash, _Unused,
1409 __hash_cached::value>;
1411 using __hash_code =
typename __hash_code_base::__hash_code;
1414 [[__no_unique_address__]] _Hashtable_ebo_helper<_Equal> _M_equal{};
1416 _Hashtable_base() =
default;
1418 _Hashtable_base(
const _Hash& __hash,
const _Equal& __eq)
1419 : __hash_code_base(__hash), _M_equal{__eq}
1423 _M_key_equals(
const _Key& __k,
1424 const _Hash_node_value<_Value,
1425 __hash_cached::value>& __n)
const
1427 static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
1428 "key equality predicate must be invocable with two arguments of "
1430 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1433 template<
typename _Kt>
1435 _M_key_equals_tr(
const _Kt& __k,
1436 const _Hash_node_value<_Value, __hash_cached::value>& __n)
const
1439 __is_invocable<const _Equal&, const _Kt&, const _Key&>{},
1440 "key equality predicate must be invocable with the argument type "
1441 "and the key type");
1442 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1445#pragma GCC diagnostic push
1446#pragma GCC diagnostic ignored "-Wc++17-extensions"
1448 _M_equals(
const _Key& __k, __hash_code __c,
1449 const _Hash_node_value<_Value, __hash_cached::value>& __n)
const
1451 if constexpr (__hash_cached::value)
1452 if (__c != __n._M_hash_code)
1455 return _M_key_equals(__k, __n);
1458 template<
typename _Kt>
1460 _M_equals_tr(
const _Kt& __k, __hash_code __c,
1461 const _Hash_node_value<_Value, __hash_cached::value>& __n)
const
1463 if constexpr (__hash_cached::value)
1464 if (__c != __n._M_hash_code)
1467 return _M_key_equals_tr(__k, __n);
1472 const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1473 const _Hash_node_value<_Value, __hash_cached::value>& __rhn)
const
1475 if constexpr (__hash_cached::value)
1476 if (__lhn._M_hash_code != __rhn._M_hash_code)
1479 return _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1481#pragma GCC diagnostic pop
1484 _M_eq() const noexcept {
return _M_equal._M_obj; }
1490 template<
typename _NodeAlloc>
1491 struct _Hashtable_alloc
1494 [[__no_unique_address__]] _Hashtable_ebo_helper<_NodeAlloc> _M_alloc{};
1497 struct __get_value_type;
1498 template<
typename _Val,
bool _Cache_hash_code>
1499 struct __get_value_type<_Hash_node<_Val, _Cache_hash_code>>
1500 {
using type = _Val; };
1503 using __node_type =
typename _NodeAlloc::value_type;
1504 using __node_alloc_type = _NodeAlloc;
1506 using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
1508 using __value_alloc_traits =
typename __node_alloc_traits::template
1509 rebind_traits<typename __get_value_type<__node_type>::type>;
1511 using __node_ptr = __node_type*;
1512 using __node_base = _Hash_node_base;
1513 using __node_base_ptr = __node_base*;
1514 using __buckets_alloc_type =
1515 __alloc_rebind<__node_alloc_type, __node_base_ptr>;
1516 using __buckets_alloc_traits = std::allocator_traits<__buckets_alloc_type>;
1517 using __buckets_ptr = __node_base_ptr*;
1519 _Hashtable_alloc() =
default;
1520 _Hashtable_alloc(
const _Hashtable_alloc&) =
default;
1521 _Hashtable_alloc(_Hashtable_alloc&&) =
default;
1523 template<
typename _Alloc>
1524 _Hashtable_alloc(_Alloc&& __a)
1525 : _M_alloc{std::
forward<_Alloc>(__a)}
1530 {
return _M_alloc._M_obj; }
1532 const __node_alloc_type&
1533 _M_node_allocator()
const
1534 {
return _M_alloc._M_obj; }
1537 template<
typename... _Args>
1539 _M_allocate_node(_Args&&... __args);
1543 _M_deallocate_node(__node_ptr __n);
1547 _M_deallocate_node_ptr(__node_ptr __n);
1552 _M_deallocate_nodes(__node_ptr __n);
1555 _M_allocate_buckets(
size_t __bkt_count);
1558 _M_deallocate_buckets(__buckets_ptr,
size_t __bkt_count);
1563 template<
typename _NodeAlloc>
1564 template<
typename... _Args>
1566 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
1569 auto& __alloc = _M_node_allocator();
1570 auto __nptr = __node_alloc_traits::allocate(__alloc, 1);
1571 __node_ptr __n = std::__to_address(__nptr);
1574 ::new ((
void*)__n) __node_type;
1575 __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
1581 __n->~__node_type();
1582 __node_alloc_traits::deallocate(__alloc, __nptr, 1);
1583 __throw_exception_again;
1587 template<
typename _NodeAlloc>
1589 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
1591 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
1592 _M_deallocate_node_ptr(__n);
1595 template<
typename _NodeAlloc>
1597 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
1599 using _Ptr =
typename __node_alloc_traits::pointer;
1600 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
1601 __n->~__node_type();
1602 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
1605 template<
typename _NodeAlloc>
1607 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
1611 __node_ptr __tmp = __n;
1612 __n = __n->_M_next();
1613 _M_deallocate_node(__tmp);
1617 template<
typename _NodeAlloc>
1619 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(
size_t __bkt_count)
1622 __buckets_alloc_type __alloc(_M_node_allocator());
1624 auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count);
1625 __buckets_ptr __p = std::__to_address(__ptr);
1626 __builtin_memset(__p, 0, __bkt_count *
sizeof(__node_base_ptr));
1630 template<
typename _NodeAlloc>
1632 _Hashtable_alloc<_NodeAlloc>::
1633 _M_deallocate_buckets(__buckets_ptr __bkts,
size_t __bkt_count)
1635 using _Ptr =
typename __buckets_alloc_traits::pointer;
1636 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
1637 __buckets_alloc_type __alloc(_M_node_allocator());
1638 __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count);
1644_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
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.
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr const _Tp & min(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 void _Construct(_Tp *__p, _Args &&... __args)
Implementation details not part of the namespace std interface.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
constexpr _Iterator __base(_Iterator __it)