31#ifndef _HASHTABLE_POLICY_H
32#define _HASHTABLE_POLICY_H 1
42namespace std _GLIBCXX_VISIBILITY(default)
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<
typename _Key,
typename _Value,
typename _Alloc,
48 typename _ExtractKey,
typename _Equal,
49 typename _Hash,
typename _RangeHash,
typename _Unused,
50 typename _RehashPolicy,
typename _Traits>
60 template<
typename _Key,
typename _Value,
typename _ExtractKey,
61 typename _Equal,
typename _Hash,
typename _RangeHash,
62 typename _Unused,
typename _Traits>
63 struct _Hashtable_base;
65#pragma GCC diagnostic push
66#pragma GCC diagnostic ignored "-Wc++17-extensions"
69 template<
typename _Iterator>
71 __distance_fw(_Iterator __first, _Iterator __last)
74 if constexpr (is_convertible<_Cat, forward_iterator_tag>::value)
77 return __first != __last ? 1 : 0;
79#pragma GCC diagnostic pop
83 template<
typename _Tp>
85 operator()(_Tp&& __x)
const noexcept
86 {
return std::forward<_Tp>(__x); }
91 template<
typename _Pair>
94 template<
typename _Tp,
typename _Up>
95 struct __1st_type<pair<_Tp, _Up>>
96 {
using type = _Tp; };
98 template<
typename _Tp,
typename _Up>
99 struct __1st_type<const pair<_Tp, _Up>>
100 {
using type =
const _Tp; };
102 template<
typename _Pair>
103 struct __1st_type<_Pair&>
104 {
using type =
typename __1st_type<_Pair>::type&; };
106 template<
typename _Tp>
107 typename __1st_type<_Tp>::type&&
108 operator()(_Tp&& __x)
const noexcept
109 {
return std::forward<_Tp>(__x).first; }
112 template<
typename _ExKey>
116 struct _NodeBuilder<_Select1st>
118 template<
typename _Kt,
typename _Arg,
typename _NodeGenerator>
120 _S_build(_Kt&& __k, _Arg&& __arg, _NodeGenerator& __node_gen)
121 ->
typename _NodeGenerator::__node_ptr
123 return __node_gen(std::forward<_Kt>(__k),
124 std::forward<_Arg>(__arg).second);
129 struct _NodeBuilder<_Identity>
131 template<
typename _Kt,
typename _Arg,
typename _NodeGenerator>
133 _S_build(_Kt&& __k, _Arg&&, _NodeGenerator& __node_gen)
134 ->
typename _NodeGenerator::__node_ptr
135 {
return __node_gen(std::forward<_Kt>(__k)); }
138 template<
typename _HashtableAlloc,
typename _NodePtr>
141 _HashtableAlloc& _M_h;
147 _M_h._M_deallocate_node_ptr(_M_ptr);
151 template<
typename _NodeAlloc>
152 struct _Hashtable_alloc;
156 template<
typename _NodeAlloc>
157 struct _ReuseOrAllocNode
160 using __node_alloc_type = _NodeAlloc;
161 using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
162 using __node_alloc_traits =
163 typename __hashtable_alloc::__node_alloc_traits;
166 using __node_ptr =
typename __hashtable_alloc::__node_ptr;
168 _ReuseOrAllocNode(__node_ptr __nodes, __hashtable_alloc& __h)
169 : _M_nodes(__nodes), _M_h(__h) { }
170 _ReuseOrAllocNode(
const _ReuseOrAllocNode&) =
delete;
173 { _M_h._M_deallocate_nodes(_M_nodes); }
175#pragma GCC diagnostic push
176#pragma GCC diagnostic ignored "-Wc++17-extensions"
177 template<
typename _Arg>
179 operator()(_Arg&& __arg)
182 return _M_h._M_allocate_node(std::forward<_Arg>(__arg));
184 using value_type =
typename _NodeAlloc::value_type::value_type;
186 __node_ptr __node = _M_nodes;
187 if constexpr (is_assignable<value_type&, _Arg>::value)
189 __node->_M_v() = std::forward<_Arg>(__arg);
190 _M_nodes = _M_nodes->_M_next();
191 __node->_M_nxt =
nullptr;
195 _M_nodes = _M_nodes->_M_next();
196 __node->_M_nxt =
nullptr;
197 auto& __a = _M_h._M_node_allocator();
198 __node_alloc_traits::destroy(__a, __node->_M_valptr());
199 _NodePtrGuard<__hashtable_alloc, __node_ptr>
200 __guard{ _M_h, __node };
201 __node_alloc_traits::construct(__a, __node->_M_valptr(),
202 std::forward<_Arg>(__arg));
203 __guard._M_ptr =
nullptr;
207#pragma GCC diagnostic pop
211 __hashtable_alloc& _M_h;
216 template<
typename _NodeAlloc>
220 using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
223 using __node_ptr =
typename __hashtable_alloc::__node_ptr;
225 _AllocNode(__hashtable_alloc& __h)
228 template<
typename... _Args>
230 operator()(_Args&&... __args)
const
231 {
return _M_h._M_allocate_node(std::forward<_Args>(__args)...); }
234 __hashtable_alloc& _M_h;
262 template<
bool _Cache_hash_code,
bool _Constant_iterators,
bool _Unique_keys>
263 struct _Hashtable_traits
265 using __hash_cached = __bool_constant<_Cache_hash_code>;
266 using __constant_iterators = __bool_constant<_Constant_iterators>;
267 using __unique_keys = __bool_constant<_Unique_keys>;
276 template<
typename _Hash>
277 struct _Hashtable_hash_traits
279 static constexpr size_t
280 __small_size_threshold() noexcept
292 struct _Hash_node_base
294 _Hash_node_base* _M_nxt;
296 _Hash_node_base() noexcept : _M_nxt() { }
298 _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
306 template<
typename _Value>
307 struct _Hash_node_value_base
309 using value_type = _Value;
311 __gnu_cxx::__aligned_buffer<_Value> _M_storage;
315 [[__gnu__::__always_inline__]]
318 {
return _M_storage._M_ptr(); }
320 [[__gnu__::__always_inline__]]
322 _M_valptr() const noexcept
323 {
return _M_storage._M_ptr(); }
325 [[__gnu__::__always_inline__]]
328 {
return *_M_valptr(); }
330 [[__gnu__::__always_inline__]]
332 _M_v() const noexcept
333 {
return *_M_valptr(); }
339 template<
bool _Cache_hash_code>
340 struct _Hash_node_code_cache
347 struct _Hash_node_code_cache<true>
348 {
size_t _M_hash_code; };
350 template<
typename _Value,
bool _Cache_hash_code>
351 struct _Hash_node_value
352 : _Hash_node_value_base<_Value>
353 , _Hash_node_code_cache<_Cache_hash_code>
359 template<
typename _Value,
bool _Cache_hash_code>
362 , _Hash_node_value<_Value, _Cache_hash_code>
365 _M_next() const noexcept
366 {
return static_cast<_Hash_node*
>(this->_M_nxt); }
370 template<
typename _Value,
bool _Cache_hash_code>
371 struct _Node_iterator_base
373 using __node_type = _Hash_node<_Value, _Cache_hash_code>;
377 _Node_iterator_base() : _M_cur(nullptr) { }
378 _Node_iterator_base(__node_type* __p) noexcept
383 { _M_cur = _M_cur->_M_next(); }
386 operator==(
const _Node_iterator_base& __x,
const _Node_iterator_base& __y)
388 {
return __x._M_cur == __y._M_cur; }
390#if __cpp_impl_three_way_comparison < 201907L
392 operator!=(
const _Node_iterator_base& __x,
const _Node_iterator_base& __y)
394 {
return __x._M_cur != __y._M_cur; }
399 template<
typename _Value,
bool __constant_iterators,
bool __cache>
400 struct _Node_iterator
401 :
public _Node_iterator_base<_Value, __cache>
404 using __base_type = _Node_iterator_base<_Value, __cache>;
405 using __node_type =
typename __base_type::__node_type;
408 using value_type = _Value;
409 using difference_type = ptrdiff_t;
410 using iterator_category = forward_iterator_tag;
412 using pointer = __conditional_t<__constant_iterators,
413 const value_type*, value_type*>;
415 using reference = __conditional_t<__constant_iterators,
416 const value_type&, value_type&>;
418 _Node_iterator() =
default;
421 _Node_iterator(__node_type* __p) noexcept
422 : __base_type(__p) { }
426 {
return this->_M_cur->_M_v(); }
429 operator->() const noexcept
430 {
return this->_M_cur->_M_valptr(); }
433 operator++() noexcept
440 operator++(
int)
noexcept
442 _Node_iterator __tmp(*
this);
447#if __cpp_impl_three_way_comparison >= 201907L
449 operator==(
const _Node_iterator&,
const _Node_iterator&) =
default;
452 operator==(
const _Node_iterator& __x,
const _Node_iterator& __y)
noexcept
454 const __base_type& __bx = __x;
455 const __base_type& __by = __y;
460 operator!=(
const _Node_iterator& __x,
const _Node_iterator& __y)
noexcept
461 {
return !(__x == __y); }
466 template<
typename _Value,
bool __constant_iterators,
bool __cache>
467 struct _Node_const_iterator
468 :
public _Node_iterator_base<_Value, __cache>
471 using __base_type = _Node_iterator_base<_Value, __cache>;
472 using __node_type =
typename __base_type::__node_type;
476 = _Node_iterator<_Value, __constant_iterators, __cache>;
479 using value_type = _Value;
480 using difference_type = ptrdiff_t;
481 using iterator_category = forward_iterator_tag;
483 using pointer =
const value_type*;
484 using reference =
const value_type&;
486 _Node_const_iterator() =
default;
489 _Node_const_iterator(__node_type* __p) noexcept
490 : __base_type(__p) { }
492 _Node_const_iterator(
const __iterator& __x) noexcept
493 : __base_type(__x._M_cur) { }
497 {
return this->_M_cur->_M_v(); }
500 operator->() const noexcept
501 {
return this->_M_cur->_M_valptr(); }
503 _Node_const_iterator&
504 operator++() noexcept
511 operator++(
int)
noexcept
513 _Node_const_iterator __tmp(*
this);
518#if __cpp_impl_three_way_comparison >= 201907L
520 operator==(
const _Node_const_iterator&,
521 const _Node_const_iterator&) =
default;
524 operator==(
const _Node_const_iterator& __x,
const __iterator& __y)
526 const __base_type& __bx = __x;
527 const __base_type& __by = __y;
532 operator==(
const _Node_const_iterator& __x,
533 const _Node_const_iterator& __y)
noexcept
535 const __base_type& __bx = __x;
536 const __base_type& __by = __y;
541 operator!=(
const _Node_const_iterator& __x,
542 const _Node_const_iterator& __y)
noexcept
543 {
return !(__x == __y); }
546 operator==(
const _Node_const_iterator& __x,
547 const __iterator& __y)
noexcept
549 const __base_type& __bx = __x;
550 const __base_type& __by = __y;
555 operator!=(
const _Node_const_iterator& __x,
556 const __iterator& __y)
noexcept
557 {
return !(__x == __y); }
560 operator==(
const __iterator& __x,
561 const _Node_const_iterator& __y)
noexcept
563 const __base_type& __bx = __x;
564 const __base_type& __by = __y;
569 operator!=(
const __iterator& __x,
570 const _Node_const_iterator& __y)
noexcept
571 {
return !(__x == __y); }
580 struct _Mod_range_hashing
583 operator()(
size_t __num,
size_t __den)
const noexcept
584 {
return __num % __den; }
592 struct _Default_ranged_hash { };
596 struct _Prime_rehash_policy
600 _Prime_rehash_policy(
float __z = 1.0) noexcept
601 : _M_max_load_factor(__z), _M_next_resize(0) { }
604 max_load_factor() const noexcept
605 {
return _M_max_load_factor; }
610 _M_next_bkt(
size_t __n)
const;
614 _M_bkt_for_elements(
size_t __n)
const
615 {
return __builtin_ceil(__n / (
double)_M_max_load_factor); }
623 _M_need_rehash(
size_t __n_bkt,
size_t __n_elt,
624 size_t __n_ins)
const;
626 using _State = size_t;
630 {
return _M_next_resize; }
634 { _M_next_resize = 0; }
637 _M_reset(_State __state)
638 { _M_next_resize = __state; }
640 static const size_t _S_growth_factor = 2;
642 float _M_max_load_factor;
645 mutable size_t _M_next_resize;
649 struct _Mask_range_hashing
652 operator()(
size_t __num,
size_t __den)
const noexcept
653 {
return __num & (__den - 1); }
658 __clp2(
size_t __n)
noexcept
664 const unsigned __lz =
sizeof(size_t) >
sizeof(
long)
665 ? __builtin_clzll(__n - 1ull)
666 : __builtin_clzl(__n - 1ul);
668 return (
size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
673 struct _Power2_rehash_policy
677 _Power2_rehash_policy(
float __z = 1.0) noexcept
678 : _M_max_load_factor(__z), _M_next_resize(0) { }
681 max_load_factor() const noexcept
682 {
return _M_max_load_factor; }
687 _M_next_bkt(
size_t __n)
noexcept
695 const auto __max_width = std::min<size_t>(
sizeof(
size_t), 8);
696 const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
697 size_t __res = __clp2(__n);
707 if (__res == __max_bkt)
711 _M_next_resize = size_t(-1);
714 = __builtin_floor(__res * (
double)_M_max_load_factor);
721 _M_bkt_for_elements(
size_t __n)
const noexcept
722 {
return __builtin_ceil(__n / (
double)_M_max_load_factor); }
729 _M_need_rehash(
size_t __n_bkt,
size_t __n_elt,
size_t __n_ins)
noexcept
731 if (__n_elt + __n_ins > _M_next_resize)
737 = std::max<size_t>(__n_elt + __n_ins, _M_next_resize ? 0 : 11)
738 / (double)_M_max_load_factor;
739 if (__min_bkts >= __n_bkt)
741 _M_next_bkt(std::max<size_t>(__builtin_floor(__min_bkts) + 1,
742 __n_bkt * _S_growth_factor)) };
745 = __builtin_floor(__n_bkt * (
double)_M_max_load_factor);
752 using _State = size_t;
755 _M_state() const noexcept
756 {
return _M_next_resize; }
760 { _M_next_resize = 0; }
763 _M_reset(_State __state)
noexcept
764 { _M_next_resize = __state; }
766 static const size_t _S_growth_factor = 2;
768 float _M_max_load_factor;
769 size_t _M_next_resize;
772 template<
typename _RehashPolicy>
773 struct _RehashStateGuard
775 _RehashPolicy* _M_guarded_obj;
776 typename _RehashPolicy::_State _M_prev_state;
778 _RehashStateGuard(_RehashPolicy& __policy)
780 , _M_prev_state(__policy._M_state())
782 _RehashStateGuard(
const _RehashStateGuard&) =
delete;
787 _M_guarded_obj->_M_reset(_M_prev_state);
809 template<
typename _Key,
typename _Value,
typename _Alloc,
810 typename _ExtractKey,
typename _Equal,
811 typename _Hash,
typename _RangeHash,
typename _Unused,
812 typename _RehashPolicy,
typename _Traits,
813 bool _Unique_keys = _Traits::__unique_keys::value>
814 struct _Map_base { };
817 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
818 typename _Hash,
typename _RangeHash,
typename _Unused,
819 typename _RehashPolicy,
typename _Traits>
820 struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
821 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
823 using mapped_type = _Val;
827 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
828 typename _Hash,
typename _RangeHash,
typename _Unused,
829 typename _RehashPolicy,
typename _Traits>
830 struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
831 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
834 using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
835 _Select1st, _Equal, _Hash,
839 using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
840 _Select1st, _Equal, _Hash, _RangeHash,
841 _Unused, _RehashPolicy, _Traits>;
843 using __hash_code =
typename __hashtable_base::__hash_code;
846 using key_type =
typename __hashtable_base::key_type;
847 using mapped_type = _Val;
850 operator[](
const key_type& __k);
853 operator[](key_type&& __k);
858 at(
const key_type& __k)
860 auto __ite =
static_cast<__hashtable*
>(
this)->find(__k);
862 __throw_out_of_range(__N(
"unordered_map::at"));
863 return __ite->second;
867 at(
const key_type& __k)
const
869 auto __ite =
static_cast<const __hashtable*
>(
this)->find(__k);
871 __throw_out_of_range(__N(
"unordered_map::at"));
872 return __ite->second;
876 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
877 typename _Hash,
typename _RangeHash,
typename _Unused,
878 typename _RehashPolicy,
typename _Traits>
880 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
881 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
true>::
882 operator[](
const key_type& __k)
885 __hashtable* __h =
static_cast<__hashtable*
>(
this);
886 __hash_code __code = __h->_M_hash_code(__k);
887 size_t __bkt = __h->_M_bucket_index(__code);
888 if (
auto __node = __h->_M_find_node(__bkt, __k, __code))
889 return __node->_M_v().second;
891 typename __hashtable::_Scoped_node __node {
898 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
899 __node._M_node =
nullptr;
900 return __pos->second;
903 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
904 typename _Hash,
typename _RangeHash,
typename _Unused,
905 typename _RehashPolicy,
typename _Traits>
907 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
908 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
true>::
909 operator[](key_type&& __k)
912 __hashtable* __h =
static_cast<__hashtable*
>(
this);
913 __hash_code __code = __h->_M_hash_code(__k);
914 size_t __bkt = __h->_M_bucket_index(__code);
915 if (
auto __node = __h->_M_find_node(__bkt, __k, __code))
916 return __node->_M_v().second;
918 typename __hashtable::_Scoped_node __node {
925 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
926 __node._M_node =
nullptr;
927 return __pos->second;
931 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
932 typename _Hash,
typename _RangeHash,
typename _Unused,
933 typename _RehashPolicy,
typename _Traits,
bool __uniq>
934 struct _Map_base<const _Key, pair<const _Key, _Val>,
935 _Alloc, _Select1st, _Equal, _Hash,
936 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
937 : _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal, _Hash,
938 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
941 template<
typename _Policy>
942 using __has_load_factor =
typename _Policy::__has_load_factor;
950 template<
typename _Key,
typename _Value,
typename _Alloc,
951 typename _ExtractKey,
typename _Equal,
952 typename _Hash,
typename _RangeHash,
typename _Unused,
953 typename _RehashPolicy,
typename _Traits,
955 __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
959 template<
typename _Key,
typename _Value,
typename _Alloc,
960 typename _ExtractKey,
typename _Equal,
961 typename _Hash,
typename _RangeHash,
typename _Unused,
962 typename _RehashPolicy,
typename _Traits>
963 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
964 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
970 template<
typename _Key,
typename _Value,
typename _Alloc,
971 typename _ExtractKey,
typename _Equal,
972 typename _Hash,
typename _RangeHash,
typename _Unused,
973 typename _RehashPolicy,
typename _Traits>
974 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
975 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
979 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
980 _Equal, _Hash, _RangeHash, _Unused,
981 _RehashPolicy, _Traits>;
985 max_load_factor() const noexcept
987 const __hashtable* __this =
static_cast<const __hashtable*
>(
this);
988 return __this->__rehash_policy().max_load_factor();
992 max_load_factor(
float __z)
994 __hashtable* __this =
static_cast<__hashtable*
>(
this);
995 __this->__rehash_policy(_RehashPolicy(__z));
1001 __hashtable* __this =
static_cast<__hashtable*
>(
this);
1002 __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
1011 template<
typename _Tp,
1012 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
1013 struct _Hashtable_ebo_helper
1015 [[__no_unique_address__]] _Tp _M_obj;
1018#if ! _GLIBCXX_INLINE_VERSION
1021 template<
typename _Tp>
1022 struct _Hashtable_ebo_helper<_Tp, false>
1034 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1035 typename _Hash,
typename _RangeHash,
typename _Unused,
1036 bool __cache_hash_code>
1037 struct _Local_iterator_base;
1040 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1041 typename _Hash,
typename _RangeHash,
typename _Unused,
1043 struct _Hash_code_base
1046 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1047 _Hash, _RangeHash, _Unused, false>;
1049 using hasher = _Hash;
1052 hash_function()
const
1053 {
return _M_hash._M_obj; }
1056 [[__no_unique_address__]] _Hashtable_ebo_helper<_Hash> _M_hash{};
1058 using __hash_code = size_t;
1062 _Hash_code_base() =
default;
1064 _Hash_code_base(
const _Hash& __hash) : _M_hash{__hash} { }
1067 _M_hash_code(
const _Key& __k)
const
1069 static_assert(__is_invocable<const _Hash&, const _Key&>{},
1070 "hash function must be invocable with an argument of key type");
1071 return _M_hash._M_obj(__k);
1074 template<
typename _Kt>
1076 _M_hash_code_tr(
const _Kt& __k)
const
1078 static_assert(__is_invocable<const _Hash&, const _Kt&>{},
1079 "hash function must be invocable with an argument of key type");
1080 return _M_hash._M_obj(__k);
1084 _M_hash_code(
const _Hash_node_value<_Value, false>& __n)
const
1085 {
return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1088 _M_hash_code(
const _Hash_node_value<_Value, true>& __n)
const
1089 {
return __n._M_hash_code; }
1092 _M_bucket_index(__hash_code __c,
size_t __bkt_count)
const
1093 {
return _RangeHash{}(__c, __bkt_count); }
1096 _M_bucket_index(
const _Hash_node_value<_Value, false>& __n,
1097 size_t __bkt_count)
const
1098 noexcept(
noexcept(declval<const _Hash&>()(declval<const _Key&>())) )
1100 return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
1105 _M_bucket_index(
const _Hash_node_value<_Value, true>& __n,
1106 size_t __bkt_count)
const noexcept
1107 {
return _RangeHash{}(__n._M_hash_code, __bkt_count); }
1110 _M_store_code(_Hash_node_code_cache<false>&, __hash_code)
const
1114 _M_copy_code(_Hash_node_code_cache<false>&,
1115 const _Hash_node_code_cache<false>&)
const
1119 _M_store_code(_Hash_node_code_cache<true>& __n, __hash_code __c)
const
1120 { __n._M_hash_code = __c; }
1123 _M_copy_code(_Hash_node_code_cache<true>& __to,
1124 const _Hash_node_code_cache<true>& __from)
const
1125 { __to._M_hash_code = __from._M_hash_code; }
1129 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1130 typename _Hash,
typename _RangeHash,
typename _Unused>
1131 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1132 _Hash, _RangeHash, _Unused, true>
1133 :
public _Node_iterator_base<_Value, true>
1136 using __base_node_iter = _Node_iterator_base<_Value, true>;
1137 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1138 _Hash, _RangeHash, _Unused,
true>;
1140 _Local_iterator_base() =
default;
1142 _Local_iterator_base(
const __hash_code_base&,
1143 _Hash_node<_Value, true>* __p,
1144 size_t __bkt,
size_t __bkt_count)
1145 : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1151 __base_node_iter::_M_incr();
1155 = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
1156 if (__bkt != _M_bucket)
1157 this->_M_cur =
nullptr;
1161 size_t _M_bucket = 0;
1162 size_t _M_bucket_count = 0;
1166 _M_get_bucket()
const {
return _M_bucket; }
1172 template<
typename _Hash>
1173 struct _Hash_obj_storage
1175 union _Uninit_storage
1177 _Uninit_storage() noexcept { }
1178 ~_Uninit_storage() { }
1180 [[__no_unique_address__]] _Hash _M_h;
1183 [[__no_unique_address__]] _Uninit_storage _M_u;
1187 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1188 typename _Hash,
typename _RangeHash,
typename _Unused>
1189 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1190 _Hash, _RangeHash, _Unused, false>
1191 : _Hash_obj_storage<_Hash>, _Node_iterator_base<_Value, false>
1194 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1195 _Hash, _RangeHash, _Unused,
false>;
1196 using __hash_obj_storage = _Hash_obj_storage<_Hash>;
1197 using __node_iter_base = _Node_iterator_base<_Value, false>;
1199 _Local_iterator_base() =
default;
1201 _Local_iterator_base(
const __hash_code_base& __base,
1202 _Hash_node<_Value, false>* __p,
1203 size_t __bkt,
size_t __bkt_count)
1204 : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1205 { _M_init(
__base._M_hash._M_obj); }
1207 ~_Local_iterator_base()
1209 if (_M_bucket_count !=
size_t(-1))
1213 _Local_iterator_base(
const _Local_iterator_base& __iter)
1214 : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket)
1215 , _M_bucket_count(__iter._M_bucket_count)
1217 if (_M_bucket_count !=
size_t(-1))
1218 _M_init(__iter._M_h());
1221 _Local_iterator_base&
1222 operator=(
const _Local_iterator_base& __iter)
1224 if (_M_bucket_count !=
size_t(-1))
1226 this->_M_cur = __iter._M_cur;
1227 _M_bucket = __iter._M_bucket;
1228 _M_bucket_count = __iter._M_bucket_count;
1229 if (_M_bucket_count !=
size_t(-1))
1230 _M_init(__iter._M_h());
1237 __node_iter_base::_M_incr();
1240 const auto __code = _M_h()(_ExtractKey{}(this->_M_cur->_M_v()));
1241 size_t __bkt = _RangeHash{}(__code, _M_bucket_count);
1242 if (__bkt != _M_bucket)
1243 this->_M_cur =
nullptr;
1247 size_t _M_bucket = 0;
1248 size_t _M_bucket_count = -1;
1251 _M_init(
const _Hash& __h)
1255 _M_destroy() { __hash_obj_storage::_M_u._M_h.~_Hash(); }
1258 _M_h()
const {
return __hash_obj_storage::_M_u._M_h; }
1262 _M_get_bucket()
const {
return _M_bucket; }
1266 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1267 typename _Hash,
typename _RangeHash,
typename _Unused,
1268 bool __constant_iterators,
bool __cache>
1269 struct _Local_iterator
1270 :
public _Local_iterator_base<_Key, _Value, _ExtractKey,
1271 _Hash, _RangeHash, _Unused, __cache>
1274 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1275 _Hash, _RangeHash, _Unused, __cache>;
1276 using __hash_code_base =
typename __base_type::__hash_code_base;
1279 using value_type = _Value;
1280 using pointer = __conditional_t<__constant_iterators,
1281 const value_type*, value_type*>;
1282 using reference = __conditional_t<__constant_iterators,
1283 const value_type&, value_type&>;
1284 using difference_type = ptrdiff_t;
1285 using iterator_category = forward_iterator_tag;
1287 _Local_iterator() =
default;
1289 _Local_iterator(
const __hash_code_base& __base,
1290 _Hash_node<_Value, __cache>* __n,
1291 size_t __bkt,
size_t __bkt_count)
1292 : __base_type(
__base, __n, __bkt, __bkt_count)
1297 {
return this->_M_cur->_M_v(); }
1301 {
return this->_M_cur->_M_valptr(); }
1313 _Local_iterator __tmp(*
this);
1320 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1321 typename _Hash,
typename _RangeHash,
typename _Unused,
1322 bool __constant_iterators,
bool __cache>
1323 struct _Local_const_iterator
1324 :
public _Local_iterator_base<_Key, _Value, _ExtractKey,
1325 _Hash, _RangeHash, _Unused, __cache>
1328 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1329 _Hash, _RangeHash, _Unused, __cache>;
1330 using __hash_code_base =
typename __base_type::__hash_code_base;
1333 using value_type = _Value;
1334 using pointer =
const value_type*;
1335 using reference =
const value_type&;
1336 using difference_type = ptrdiff_t;
1337 using iterator_category = forward_iterator_tag;
1339 _Local_const_iterator() =
default;
1341 _Local_const_iterator(
const __hash_code_base& __base,
1342 _Hash_node<_Value, __cache>* __n,
1343 size_t __bkt,
size_t __bkt_count)
1344 : __base_type(
__base, __n, __bkt, __bkt_count)
1347 _Local_const_iterator(
const _Local_iterator<_Key, _Value, _ExtractKey,
1348 _Hash, _RangeHash, _Unused,
1349 __constant_iterators,
1356 {
return this->_M_cur->_M_v(); }
1360 {
return this->_M_cur->_M_valptr(); }
1362 _Local_const_iterator&
1369 _Local_const_iterator
1372 _Local_const_iterator __tmp(*
this);
1387 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1388 typename _Equal,
typename _Hash,
typename _RangeHash,
1389 typename _Unused,
typename _Traits>
1390 struct _Hashtable_base
1391 :
public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1392 _Unused, _Traits::__hash_cached::value>
1395 using key_type = _Key;
1396 using value_type = _Value;
1397 using key_equal = _Equal;
1398 using size_type = size_t;
1399 using difference_type = ptrdiff_t;
1401 using __traits_type = _Traits;
1402 using __hash_cached =
typename __traits_type::__hash_cached;
1404 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1405 _Hash, _RangeHash, _Unused,
1406 __hash_cached::value>;
1408 using __hash_code =
typename __hash_code_base::__hash_code;
1411 [[__no_unique_address__]] _Hashtable_ebo_helper<_Equal> _M_equal{};
1413 _Hashtable_base() =
default;
1415 _Hashtable_base(
const _Hash& __hash,
const _Equal& __eq)
1416 : __hash_code_base(__hash), _M_equal{__eq}
1420 _M_key_equals(
const _Key& __k,
1421 const _Hash_node_value<_Value,
1422 __hash_cached::value>& __n)
const
1424 static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
1425 "key equality predicate must be invocable with two arguments of "
1427 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1430 template<
typename _Kt>
1432 _M_key_equals_tr(
const _Kt& __k,
1433 const _Hash_node_value<_Value,
1434 __hash_cached::value>& __n)
const
1437 __is_invocable<const _Equal&, const _Kt&, const _Key&>{},
1438 "key equality predicate must be invocable with the argument type "
1439 "and the key type");
1440 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1443#pragma GCC diagnostic push
1444#pragma GCC diagnostic ignored "-Wc++17-extensions"
1446 _M_equals(
const _Key& __k, __hash_code __c,
1447 const _Hash_node_value<_Value, __hash_cached::value>& __n)
const
1449 if constexpr (__hash_cached::value)
1450 if (__c != __n._M_hash_code)
1453 return _M_key_equals(__k, __n);
1456 template<
typename _Kt>
1458 _M_equals_tr(
const _Kt& __k, __hash_code __c,
1459 const _Hash_node_value<_Value,
1460 __hash_cached::value>& __n)
const
1462 if constexpr (__hash_cached::value)
1463 if (__c != __n._M_hash_code)
1466 return _M_key_equals_tr(__k, __n);
1471 const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1472 const _Hash_node_value<_Value, __hash_cached::value>& __rhn)
const
1474 if constexpr (__hash_cached::value)
1475 if (__lhn._M_hash_code != __rhn._M_hash_code)
1478 return _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1480#pragma GCC diagnostic pop
1483 _M_eq() const noexcept {
return _M_equal._M_obj; }
1489 template<
typename _NodeAlloc>
1490 struct _Hashtable_alloc
1493 [[__no_unique_address__]] _Hashtable_ebo_helper<_NodeAlloc> _M_alloc{};
1496 struct __get_value_type;
1497 template<
typename _Val,
bool _Cache_hash_code>
1498 struct __get_value_type<_Hash_node<_Val, _Cache_hash_code>>
1499 {
using type = _Val; };
1502 using __node_type =
typename _NodeAlloc::value_type;
1503 using __node_alloc_type = _NodeAlloc;
1507 using __value_alloc_traits =
typename __node_alloc_traits::template
1508 rebind_traits<typename __get_value_type<__node_type>::type>;
1510 using __node_ptr = __node_type*;
1511 using __node_base = _Hash_node_base;
1512 using __node_base_ptr = __node_base*;
1513 using __buckets_alloc_type =
1514 __alloc_rebind<__node_alloc_type, __node_base_ptr>;
1516 using __buckets_ptr = __node_base_ptr*;
1518 _Hashtable_alloc() =
default;
1519 _Hashtable_alloc(
const _Hashtable_alloc&) =
default;
1520 _Hashtable_alloc(_Hashtable_alloc&&) =
default;
1522 template<
typename _Alloc>
1523 _Hashtable_alloc(_Alloc&& __a)
1529 {
return _M_alloc._M_obj; }
1531 const __node_alloc_type&
1532 _M_node_allocator()
const
1533 {
return _M_alloc._M_obj; }
1536 template<
typename... _Args>
1538 _M_allocate_node(_Args&&... __args);
1542 _M_deallocate_node(__node_ptr __n);
1546 _M_deallocate_node_ptr(__node_ptr __n);
1551 _M_deallocate_nodes(__node_ptr __n);
1554 _M_allocate_buckets(
size_t __bkt_count);
1557 _M_deallocate_buckets(__buckets_ptr,
size_t __bkt_count);
1562 template<
typename _NodeAlloc>
1563 template<
typename... _Args>
1565 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
1568 auto& __alloc = _M_node_allocator();
1569 auto __nptr = __node_alloc_traits::allocate(__alloc, 1);
1570 __node_ptr __n = std::__to_address(__nptr);
1573 ::new ((
void*)__n) __node_type;
1574 __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
1575 std::forward<_Args>(__args)...);
1580 __n->~__node_type();
1581 __node_alloc_traits::deallocate(__alloc, __nptr, 1);
1582 __throw_exception_again;
1586 template<
typename _NodeAlloc>
1588 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
1590 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
1591 _M_deallocate_node_ptr(__n);
1594 template<
typename _NodeAlloc>
1596 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
1598 using _Ptr =
typename __node_alloc_traits::pointer;
1600 __n->~__node_type();
1601 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
1604 template<
typename _NodeAlloc>
1606 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
1610 __node_ptr __tmp = __n;
1611 __n = __n->_M_next();
1612 _M_deallocate_node(__tmp);
1616 template<
typename _NodeAlloc>
1618 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(
size_t __bkt_count)
1621 __buckets_alloc_type __alloc(_M_node_allocator());
1623 auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count);
1624 __buckets_ptr __p = std::__to_address(__ptr);
1625 __builtin_memset(__p, 0, __bkt_count *
sizeof(__node_base_ptr));
1629 template<
typename _NodeAlloc>
1631 _Hashtable_alloc<_NodeAlloc>::
1632 _M_deallocate_buckets(__buckets_ptr __bkts,
size_t __bkt_count)
1634 using _Ptr =
typename __buckets_alloc_traits::pointer;
1636 __buckets_alloc_type __alloc(_M_node_allocator());
1637 __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count);
1643_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.
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.
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)
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
constexpr _Iterator __base(_Iterator __it)
Primary class template, tuple.
Uniform interface to all allocator types.
Uniform interface to all pointer-like types.
Struct holding two objects of arbitrary type.
Traits class for iterators.
Uniform interface to C++98 and C++11 allocators.