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
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
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
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
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)
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)
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(),
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
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;
313 [[__gnu__::__always_inline__]]
316 {
return _M_storage._M_ptr(); }
318 [[__gnu__::__always_inline__]]
320 _M_valptr() const noexcept
321 {
return _M_storage._M_ptr(); }
323 [[__gnu__::__always_inline__]]
326 {
return *_M_valptr(); }
328 [[__gnu__::__always_inline__]]
330 _M_v() const noexcept
331 {
return *_M_valptr(); }
337 template<
bool _Cache_hash_code>
338 struct _Hash_node_code_cache
345 struct _Hash_node_code_cache<true>
346 {
size_t _M_hash_code; };
348 template<
typename _Value,
bool _Cache_hash_code>
349 struct _Hash_node_value
350 : _Hash_node_value_base<_Value>
351 , _Hash_node_code_cache<_Cache_hash_code>
357 template<
typename _Value,
bool _Cache_hash_code>
360 , _Hash_node_value<_Value, _Cache_hash_code>
363 _M_next() const noexcept
364 {
return static_cast<_Hash_node*
>(this->_M_nxt); }
368 template<
typename _Value,
bool _Cache_hash_code>
369 struct _Node_iterator_base
371 using __node_type = _Hash_node<_Value, _Cache_hash_code>;
375 _Node_iterator_base() : _M_cur(nullptr) { }
376 _Node_iterator_base(__node_type* __p) noexcept
381 { _M_cur = _M_cur->_M_next(); }
384 operator==(
const _Node_iterator_base& __x,
const _Node_iterator_base& __y)
386 {
return __x._M_cur == __y._M_cur; }
388#if __cpp_impl_three_way_comparison < 201907L
390 operator!=(
const _Node_iterator_base& __x,
const _Node_iterator_base& __y)
392 {
return __x._M_cur != __y._M_cur; }
397 template<
typename _Value,
bool __constant_iterators,
bool __cache>
398 struct _Node_iterator
399 :
public _Node_iterator_base<_Value, __cache>
402 using __base_type = _Node_iterator_base<_Value, __cache>;
403 using __node_type =
typename __base_type::__node_type;
406 using value_type = _Value;
407 using difference_type = ptrdiff_t;
408 using iterator_category = forward_iterator_tag;
410 using pointer = __conditional_t<__constant_iterators,
411 const value_type*, value_type*>;
413 using reference = __conditional_t<__constant_iterators,
414 const value_type&, value_type&>;
416 _Node_iterator() =
default;
419 _Node_iterator(__node_type* __p) noexcept
420 : __base_type(__p) { }
424 {
return this->_M_cur->_M_v(); }
427 operator->() const noexcept
428 {
return this->_M_cur->_M_valptr(); }
431 operator++() noexcept
438 operator++(
int)
noexcept
440 _Node_iterator __tmp(*
this);
445#if __cpp_impl_three_way_comparison >= 201907L
447 operator==(
const _Node_iterator&,
const _Node_iterator&) =
default;
450 operator==(
const _Node_iterator& __x,
const _Node_iterator& __y)
noexcept
452 const __base_type& __bx = __x;
453 const __base_type& __by = __y;
458 operator!=(
const _Node_iterator& __x,
const _Node_iterator& __y)
noexcept
459 {
return !(__x == __y); }
464 template<
typename _Value,
bool __constant_iterators,
bool __cache>
465 struct _Node_const_iterator
466 :
public _Node_iterator_base<_Value, __cache>
469 using __base_type = _Node_iterator_base<_Value, __cache>;
470 using __node_type =
typename __base_type::__node_type;
474 = _Node_iterator<_Value, __constant_iterators, __cache>;
477 using value_type = _Value;
478 using difference_type = ptrdiff_t;
479 using iterator_category = forward_iterator_tag;
481 using pointer =
const value_type*;
482 using reference =
const value_type&;
484 _Node_const_iterator() =
default;
487 _Node_const_iterator(__node_type* __p) noexcept
488 : __base_type(__p) { }
490 _Node_const_iterator(
const __iterator& __x) noexcept
491 : __base_type(__x._M_cur) { }
495 {
return this->_M_cur->_M_v(); }
498 operator->() const noexcept
499 {
return this->_M_cur->_M_valptr(); }
501 _Node_const_iterator&
502 operator++() noexcept
509 operator++(
int)
noexcept
511 _Node_const_iterator __tmp(*
this);
516#if __cpp_impl_three_way_comparison >= 201907L
518 operator==(
const _Node_const_iterator&,
519 const _Node_const_iterator&) =
default;
522 operator==(
const _Node_const_iterator& __x,
const __iterator& __y)
524 const __base_type& __bx = __x;
525 const __base_type& __by = __y;
530 operator==(
const _Node_const_iterator& __x,
531 const _Node_const_iterator& __y)
noexcept
533 const __base_type& __bx = __x;
534 const __base_type& __by = __y;
539 operator!=(
const _Node_const_iterator& __x,
540 const _Node_const_iterator& __y)
noexcept
541 {
return !(__x == __y); }
544 operator==(
const _Node_const_iterator& __x,
545 const __iterator& __y)
noexcept
547 const __base_type& __bx = __x;
548 const __base_type& __by = __y;
553 operator!=(
const _Node_const_iterator& __x,
554 const __iterator& __y)
noexcept
555 {
return !(__x == __y); }
558 operator==(
const __iterator& __x,
559 const _Node_const_iterator& __y)
noexcept
561 const __base_type& __bx = __x;
562 const __base_type& __by = __y;
567 operator!=(
const __iterator& __x,
568 const _Node_const_iterator& __y)
noexcept
569 {
return !(__x == __y); }
578 struct _Mod_range_hashing
581 operator()(
size_t __num,
size_t __den)
const noexcept
582 {
return __num % __den; }
590 struct _Default_ranged_hash { };
594 struct _Prime_rehash_policy
596 using __has_load_factor = true_type;
598 _Prime_rehash_policy(
float __z = 1.0) noexcept
599 : _M_max_load_factor(__z), _M_next_resize(0) { }
602 max_load_factor() const noexcept
603 {
return _M_max_load_factor; }
608 _M_next_bkt(
size_t __n)
const;
612 _M_bkt_for_elements(
size_t __n)
const
613 {
return __builtin_ceil(__n / (
double)_M_max_load_factor); }
621 _M_need_rehash(
size_t __n_bkt,
size_t __n_elt,
622 size_t __n_ins)
const;
624 using _State = size_t;
628 {
return _M_next_resize; }
632 { _M_next_resize = 0; }
635 _M_reset(_State __state)
636 { _M_next_resize = __state; }
638 static const size_t _S_growth_factor = 2;
640 float _M_max_load_factor;
643 mutable size_t _M_next_resize;
647 struct _Mask_range_hashing
650 operator()(
size_t __num,
size_t __den)
const noexcept
651 {
return __num & (__den - 1); }
656 __clp2(
size_t __n)
noexcept
662 const unsigned __lz =
sizeof(size_t) >
sizeof(
long)
663 ? __builtin_clzll(__n - 1ull)
664 : __builtin_clzl(__n - 1ul);
666 return (
size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
671 struct _Power2_rehash_policy
673 using __has_load_factor = true_type;
675 _Power2_rehash_policy(
float __z = 1.0) noexcept
676 : _M_max_load_factor(__z), _M_next_resize(0) { }
679 max_load_factor() const noexcept
680 {
return _M_max_load_factor; }
685 _M_next_bkt(
size_t __n)
noexcept
694 const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
695 size_t __res = __clp2(__n);
705 if (__res == __max_bkt)
709 _M_next_resize = size_t(-1);
712 = __builtin_floor(__res * (
double)_M_max_load_factor);
719 _M_bkt_for_elements(
size_t __n)
const noexcept
720 {
return __builtin_ceil(__n / (
double)_M_max_load_factor); }
727 _M_need_rehash(
size_t __n_bkt,
size_t __n_elt,
size_t __n_ins)
noexcept
729 if (__n_elt + __n_ins > _M_next_resize)
736 / (double)_M_max_load_factor;
737 if (__min_bkts >= __n_bkt)
740 __n_bkt * _S_growth_factor)) };
743 = __builtin_floor(__n_bkt * (
double)_M_max_load_factor);
750 using _State = size_t;
753 _M_state() const noexcept
754 {
return _M_next_resize; }
758 { _M_next_resize = 0; }
761 _M_reset(_State __state)
noexcept
762 { _M_next_resize = __state; }
764 static const size_t _S_growth_factor = 2;
766 float _M_max_load_factor;
767 size_t _M_next_resize;
770 template<
typename _RehashPolicy>
771 struct _RehashStateGuard
773 _RehashPolicy* _M_guarded_obj;
774 typename _RehashPolicy::_State _M_prev_state;
776 _RehashStateGuard(_RehashPolicy& __policy)
778 , _M_prev_state(__policy._M_state())
780 _RehashStateGuard(
const _RehashStateGuard&) =
delete;
785 _M_guarded_obj->_M_reset(_M_prev_state);
807 template<
typename _Key,
typename _Value,
typename _Alloc,
808 typename _ExtractKey,
typename _Equal,
809 typename _Hash,
typename _RangeHash,
typename _Unused,
810 typename _RehashPolicy,
typename _Traits,
811 bool _Unique_keys = _Traits::__unique_keys::value>
812 struct _Map_base { };
815 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
816 typename _Hash,
typename _RangeHash,
typename _Unused,
817 typename _RehashPolicy,
typename _Traits>
818 struct _Map_base<_Key,
pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
819 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
821 using mapped_type = _Val;
825 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
826 typename _Hash,
typename _RangeHash,
typename _Unused,
827 typename _RehashPolicy,
typename _Traits>
828 struct _Map_base<_Key,
pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
829 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
832 using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
833 _Select1st, _Equal, _Hash,
837 using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
838 _Select1st, _Equal, _Hash, _RangeHash,
839 _Unused, _RehashPolicy, _Traits>;
841 using __hash_code =
typename __hashtable_base::__hash_code;
844 using key_type =
typename __hashtable_base::key_type;
845 using mapped_type = _Val;
848 operator[](
const key_type& __k);
851 operator[](key_type&& __k);
856 at(
const key_type& __k)
858 auto __ite =
static_cast<__hashtable*
>(
this)->find(__k);
860 __throw_out_of_range(__N(
"unordered_map::at"));
861 return __ite->second;
865 at(
const key_type& __k)
const
867 auto __ite =
static_cast<const __hashtable*
>(
this)->find(__k);
869 __throw_out_of_range(__N(
"unordered_map::at"));
870 return __ite->second;
874 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
875 typename _Hash,
typename _RangeHash,
typename _Unused,
876 typename _RehashPolicy,
typename _Traits>
878 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
879 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
true>::
880 operator[](
const key_type& __k)
883 __hashtable* __h =
static_cast<__hashtable*
>(
this);
884 __hash_code __code = __h->_M_hash_code(__k);
885 size_t __bkt = __h->_M_bucket_index(__code);
886 if (
auto __node = __h->_M_find_node(__bkt, __k, __code))
887 return __node->_M_v().second;
889 typename __hashtable::_Scoped_node __node {
896 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
897 __node._M_node =
nullptr;
898 return __pos->second;
901 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
902 typename _Hash,
typename _RangeHash,
typename _Unused,
903 typename _RehashPolicy,
typename _Traits>
905 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
906 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
true>::
907 operator[](key_type&& __k)
910 __hashtable* __h =
static_cast<__hashtable*
>(
this);
911 __hash_code __code = __h->_M_hash_code(__k);
912 size_t __bkt = __h->_M_bucket_index(__code);
913 if (
auto __node = __h->_M_find_node(__bkt, __k, __code))
914 return __node->_M_v().second;
916 typename __hashtable::_Scoped_node __node {
923 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
924 __node._M_node =
nullptr;
925 return __pos->second;
929 template<
typename _Key,
typename _Val,
typename _Alloc,
typename _Equal,
930 typename _Hash,
typename _RangeHash,
typename _Unused,
931 typename _RehashPolicy,
typename _Traits,
bool __uniq>
932 struct _Map_base<const _Key,
pair<const _Key, _Val>,
933 _Alloc, _Select1st, _Equal, _Hash,
934 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
935 : _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal, _Hash,
936 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
939 template<
typename _Policy>
940 using __has_load_factor =
typename _Policy::__has_load_factor;
948 template<
typename _Key,
typename _Value,
typename _Alloc,
949 typename _ExtractKey,
typename _Equal,
950 typename _Hash,
typename _RangeHash,
typename _Unused,
951 typename _RehashPolicy,
typename _Traits,
953 __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
957 template<
typename _Key,
typename _Value,
typename _Alloc,
958 typename _ExtractKey,
typename _Equal,
959 typename _Hash,
typename _RangeHash,
typename _Unused,
960 typename _RehashPolicy,
typename _Traits>
961 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
962 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
968 template<
typename _Key,
typename _Value,
typename _Alloc,
969 typename _ExtractKey,
typename _Equal,
970 typename _Hash,
typename _RangeHash,
typename _Unused,
971 typename _RehashPolicy,
typename _Traits>
972 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
973 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
977 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
978 _Equal, _Hash, _RangeHash, _Unused,
979 _RehashPolicy, _Traits>;
983 max_load_factor() const noexcept
985 const __hashtable* __this =
static_cast<const __hashtable*
>(
this);
986 return __this->__rehash_policy().max_load_factor();
990 max_load_factor(
float __z)
992 __hashtable* __this =
static_cast<__hashtable*
>(
this);
993 __this->__rehash_policy(_RehashPolicy(__z));
999 __hashtable* __this =
static_cast<__hashtable*
>(
this);
1000 __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
1009 template<
typename _Tp,
1010 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
1011 struct _Hashtable_ebo_helper
1013 [[__no_unique_address__]] _Tp _M_obj;
1016#if ! _GLIBCXX_INLINE_VERSION
1019 template<
typename _Tp>
1020 struct _Hashtable_ebo_helper<_Tp, false>
1032 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1033 typename _Hash,
typename _RangeHash,
typename _Unused,
1034 bool __cache_hash_code>
1035 struct _Local_iterator_base;
1038 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1039 typename _Hash,
typename _RangeHash,
typename _Unused,
1041 struct _Hash_code_base
1044 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1045 _Hash, _RangeHash, _Unused, false>;
1047 using hasher = _Hash;
1050 hash_function()
const
1051 {
return _M_hash._M_obj; }
1054 [[__no_unique_address__]] _Hashtable_ebo_helper<_Hash> _M_hash{};
1056 using __hash_code = size_t;
1060 _Hash_code_base() =
default;
1062 _Hash_code_base(
const _Hash& __hash) : _M_hash{__hash} { }
1065 _M_hash_code(
const _Key& __k)
const
1067 static_assert(__is_invocable<const _Hash&, const _Key&>{},
1068 "hash function must be invocable with an argument of key type");
1069 return _M_hash._M_obj(__k);
1072 template<
typename _Kt>
1074 _M_hash_code_tr(
const _Kt& __k)
const
1076 static_assert(__is_invocable<const _Hash&, const _Kt&>{},
1077 "hash function must be invocable with an argument of key type");
1078 return _M_hash._M_obj(__k);
1082 _M_hash_code(
const _Hash_node_value<_Value, false>& __n)
const
1083 {
return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1086 _M_hash_code(
const _Hash_node_value<_Value, true>& __n)
const
1087 {
return __n._M_hash_code; }
1090 _M_bucket_index(__hash_code __c,
size_t __bkt_count)
const
1091 {
return _RangeHash{}(__c, __bkt_count); }
1094 _M_bucket_index(
const _Hash_node_value<_Value, false>& __n,
1095 size_t __bkt_count)
const
1096 noexcept(
noexcept(declval<const _Hash&>()(declval<const _Key&>())) )
1098 return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
1103 _M_bucket_index(
const _Hash_node_value<_Value, true>& __n,
1104 size_t __bkt_count)
const noexcept
1105 {
return _RangeHash{}(__n._M_hash_code, __bkt_count); }
1108 _M_store_code(_Hash_node_code_cache<false>&, __hash_code)
const
1112 _M_copy_code(_Hash_node_code_cache<false>&,
1113 const _Hash_node_code_cache<false>&)
const
1117 _M_store_code(_Hash_node_code_cache<true>& __n, __hash_code __c)
const
1118 { __n._M_hash_code = __c; }
1121 _M_copy_code(_Hash_node_code_cache<true>& __to,
1122 const _Hash_node_code_cache<true>& __from)
const
1123 { __to._M_hash_code = __from._M_hash_code; }
1127 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1128 typename _Hash,
typename _RangeHash,
typename _Unused>
1129 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1130 _Hash, _RangeHash, _Unused, true>
1131 :
public _Node_iterator_base<_Value, true>
1134 using __base_node_iter = _Node_iterator_base<_Value, true>;
1135 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1136 _Hash, _RangeHash, _Unused,
true>;
1138 _Local_iterator_base() =
default;
1140 _Local_iterator_base(
const __hash_code_base&,
1141 _Hash_node<_Value, true>* __p,
1142 size_t __bkt,
size_t __bkt_count)
1143 : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1149 __base_node_iter::_M_incr();
1153 = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
1154 if (__bkt != _M_bucket)
1155 this->_M_cur =
nullptr;
1159 size_t _M_bucket = 0;
1160 size_t _M_bucket_count = 0;
1164 _M_get_bucket()
const {
return _M_bucket; }
1170 template<
typename _Hash>
1171 struct _Hash_obj_storage
1173 union _Uninit_storage
1175 _Uninit_storage() noexcept { }
1176 ~_Uninit_storage() { }
1178 [[__no_unique_address__]] _Hash _M_h;
1181 [[__no_unique_address__]] _Uninit_storage _M_u;
1185 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1186 typename _Hash,
typename _RangeHash,
typename _Unused>
1187 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1188 _Hash, _RangeHash, _Unused, false>
1189 : _Hash_obj_storage<_Hash>, _Node_iterator_base<_Value, false>
1192 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1193 _Hash, _RangeHash, _Unused,
false>;
1194 using __hash_obj_storage = _Hash_obj_storage<_Hash>;
1195 using __node_iter_base = _Node_iterator_base<_Value, false>;
1197 _Local_iterator_base() =
default;
1199 _Local_iterator_base(
const __hash_code_base& __base,
1200 _Hash_node<_Value, false>* __p,
1201 size_t __bkt,
size_t __bkt_count)
1202 : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1203 { _M_init(
__base._M_hash._M_obj); }
1205 ~_Local_iterator_base()
1207 if (_M_bucket_count !=
size_t(-1))
1211 _Local_iterator_base(
const _Local_iterator_base& __iter)
1212 : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket)
1213 , _M_bucket_count(__iter._M_bucket_count)
1215 if (_M_bucket_count !=
size_t(-1))
1216 _M_init(__iter._M_h());
1219 _Local_iterator_base&
1220 operator=(
const _Local_iterator_base& __iter)
1222 if (_M_bucket_count !=
size_t(-1))
1224 this->_M_cur = __iter._M_cur;
1225 _M_bucket = __iter._M_bucket;
1226 _M_bucket_count = __iter._M_bucket_count;
1227 if (_M_bucket_count !=
size_t(-1))
1228 _M_init(__iter._M_h());
1235 __node_iter_base::_M_incr();
1238 const auto __code = _M_h()(_ExtractKey{}(this->_M_cur->_M_v()));
1239 size_t __bkt = _RangeHash{}(__code, _M_bucket_count);
1240 if (__bkt != _M_bucket)
1241 this->_M_cur =
nullptr;
1245 size_t _M_bucket = 0;
1246 size_t _M_bucket_count = -1;
1249 _M_init(
const _Hash& __h)
1253 _M_destroy() { __hash_obj_storage::_M_u._M_h.~_Hash(); }
1256 _M_h()
const {
return __hash_obj_storage::_M_u._M_h; }
1260 _M_get_bucket()
const {
return _M_bucket; }
1264 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1265 typename _Hash,
typename _RangeHash,
typename _Unused,
1266 bool __constant_iterators,
bool __cache>
1267 struct _Local_iterator
1268 :
public _Local_iterator_base<_Key, _Value, _ExtractKey,
1269 _Hash, _RangeHash, _Unused, __cache>
1272 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1273 _Hash, _RangeHash, _Unused, __cache>;
1274 using __hash_code_base =
typename __base_type::__hash_code_base;
1277 using value_type = _Value;
1278 using pointer = __conditional_t<__constant_iterators,
1279 const value_type*, value_type*>;
1280 using reference = __conditional_t<__constant_iterators,
1281 const value_type&, value_type&>;
1282 using difference_type = ptrdiff_t;
1283 using iterator_category = forward_iterator_tag;
1285 _Local_iterator() =
default;
1287 _Local_iterator(
const __hash_code_base& __base,
1288 _Hash_node<_Value, __cache>* __n,
1289 size_t __bkt,
size_t __bkt_count)
1290 : __base_type(
__base, __n, __bkt, __bkt_count)
1295 {
return this->_M_cur->_M_v(); }
1299 {
return this->_M_cur->_M_valptr(); }
1311 _Local_iterator __tmp(*
this);
1318 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1319 typename _Hash,
typename _RangeHash,
typename _Unused,
1320 bool __constant_iterators,
bool __cache>
1321 struct _Local_const_iterator
1322 :
public _Local_iterator_base<_Key, _Value, _ExtractKey,
1323 _Hash, _RangeHash, _Unused, __cache>
1326 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1327 _Hash, _RangeHash, _Unused, __cache>;
1328 using __hash_code_base =
typename __base_type::__hash_code_base;
1331 using value_type = _Value;
1332 using pointer =
const value_type*;
1333 using reference =
const value_type&;
1334 using difference_type = ptrdiff_t;
1335 using iterator_category = forward_iterator_tag;
1337 _Local_const_iterator() =
default;
1339 _Local_const_iterator(
const __hash_code_base& __base,
1340 _Hash_node<_Value, __cache>* __n,
1341 size_t __bkt,
size_t __bkt_count)
1342 : __base_type(
__base, __n, __bkt, __bkt_count)
1345 _Local_const_iterator(
const _Local_iterator<_Key, _Value, _ExtractKey,
1346 _Hash, _RangeHash, _Unused,
1347 __constant_iterators,
1354 {
return this->_M_cur->_M_v(); }
1358 {
return this->_M_cur->_M_valptr(); }
1360 _Local_const_iterator&
1367 _Local_const_iterator
1370 _Local_const_iterator __tmp(*
this);
1385 template<
typename _Key,
typename _Value,
typename _ExtractKey,
1386 typename _Equal,
typename _Hash,
typename _RangeHash,
1387 typename _Unused,
typename _Traits>
1388 struct _Hashtable_base
1389 :
public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1390 _Unused, _Traits::__hash_cached::value>
1393 using key_type = _Key;
1394 using value_type = _Value;
1395 using key_equal = _Equal;
1396 using size_type = size_t;
1397 using difference_type = ptrdiff_t;
1399 using __traits_type = _Traits;
1400 using __hash_cached =
typename __traits_type::__hash_cached;
1402 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1403 _Hash, _RangeHash, _Unused,
1404 __hash_cached::value>;
1406 using __hash_code =
typename __hash_code_base::__hash_code;
1409 [[__no_unique_address__]] _Hashtable_ebo_helper<_Equal> _M_equal{};
1411 _Hashtable_base() =
default;
1413 _Hashtable_base(
const _Hash& __hash,
const _Equal& __eq)
1414 : __hash_code_base(__hash), _M_equal{__eq}
1418 _M_key_equals(
const _Key& __k,
1419 const _Hash_node_value<_Value,
1420 __hash_cached::value>& __n)
const
1422 static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
1423 "key equality predicate must be invocable with two arguments of "
1425 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1428 template<
typename _Kt>
1430 _M_key_equals_tr(
const _Kt& __k,
1431 const _Hash_node_value<_Value,
1432 __hash_cached::value>& __n)
const
1435 __is_invocable<const _Equal&, const _Kt&, const _Key&>{},
1436 "key equality predicate must be invocable with the argument type "
1437 "and the key type");
1438 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1441#pragma GCC diagnostic push
1442#pragma GCC diagnostic ignored "-Wc++17-extensions"
1444 _M_equals(
const _Key& __k, __hash_code __c,
1445 const _Hash_node_value<_Value, __hash_cached::value>& __n)
const
1447 if constexpr (__hash_cached::value)
1448 if (__c != __n._M_hash_code)
1451 return _M_key_equals(__k, __n);
1454 template<
typename _Kt>
1456 _M_equals_tr(
const _Kt& __k, __hash_code __c,
1457 const _Hash_node_value<_Value,
1458 __hash_cached::value>& __n)
const
1460 if constexpr (__hash_cached::value)
1461 if (__c != __n._M_hash_code)
1464 return _M_key_equals_tr(__k, __n);
1469 const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1470 const _Hash_node_value<_Value, __hash_cached::value>& __rhn)
const
1472 if constexpr (__hash_cached::value)
1473 if (__lhn._M_hash_code != __rhn._M_hash_code)
1476 return _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1478#pragma GCC diagnostic pop
1481 _M_eq() const noexcept {
return _M_equal._M_obj; }
1487 template<
typename _NodeAlloc>
1488 struct _Hashtable_alloc
1491 [[__no_unique_address__]] _Hashtable_ebo_helper<_NodeAlloc> _M_alloc{};
1494 struct __get_value_type;
1495 template<
typename _Val,
bool _Cache_hash_code>
1496 struct __get_value_type<_Hash_node<_Val, _Cache_hash_code>>
1497 {
using type = _Val; };
1500 using __node_type =
typename _NodeAlloc::value_type;
1501 using __node_alloc_type = _NodeAlloc;
1505 using __value_alloc_traits =
typename __node_alloc_traits::template
1506 rebind_traits<typename __get_value_type<__node_type>::type>;
1508 using __node_ptr = __node_type*;
1509 using __node_base = _Hash_node_base;
1510 using __node_base_ptr = __node_base*;
1511 using __buckets_alloc_type =
1512 __alloc_rebind<__node_alloc_type, __node_base_ptr>;
1514 using __buckets_ptr = __node_base_ptr*;
1516 _Hashtable_alloc() =
default;
1517 _Hashtable_alloc(
const _Hashtable_alloc&) =
default;
1518 _Hashtable_alloc(_Hashtable_alloc&&) =
default;
1520 template<
typename _Alloc>
1521 _Hashtable_alloc(_Alloc&& __a)
1527 {
return _M_alloc._M_obj; }
1529 const __node_alloc_type&
1530 _M_node_allocator()
const
1531 {
return _M_alloc._M_obj; }
1534 template<
typename... _Args>
1536 _M_allocate_node(_Args&&... __args);
1540 _M_deallocate_node(__node_ptr __n);
1544 _M_deallocate_node_ptr(__node_ptr __n);
1549 _M_deallocate_nodes(__node_ptr __n);
1552 _M_allocate_buckets(
size_t __bkt_count);
1555 _M_deallocate_buckets(__buckets_ptr,
size_t __bkt_count);
1560 template<
typename _NodeAlloc>
1561 template<
typename... _Args>
1563 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
1566 auto& __alloc = _M_node_allocator();
1567 auto __nptr = __node_alloc_traits::allocate(__alloc, 1);
1568 __node_ptr __n = std::__to_address(__nptr);
1571 ::new ((
void*)__n) __node_type;
1572 __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
1578 __n->~__node_type();
1579 __node_alloc_traits::deallocate(__alloc, __nptr, 1);
1580 __throw_exception_again;
1584 template<
typename _NodeAlloc>
1586 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
1588 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
1589 _M_deallocate_node_ptr(__n);
1592 template<
typename _NodeAlloc>
1594 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
1596 using _Ptr =
typename __node_alloc_traits::pointer;
1598 __n->~__node_type();
1599 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
1602 template<
typename _NodeAlloc>
1604 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
1608 __node_ptr __tmp = __n;
1609 __n = __n->_M_next();
1610 _M_deallocate_node(__tmp);
1614 template<
typename _NodeAlloc>
1616 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(
size_t __bkt_count)
1619 __buckets_alloc_type __alloc(_M_node_allocator());
1621 auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count);
1622 __buckets_ptr __p = std::__to_address(__ptr);
1623 __builtin_memset(__p, 0, __bkt_count *
sizeof(__node_base_ptr));
1627 template<
typename _NodeAlloc>
1629 _Hashtable_alloc<_NodeAlloc>::
1630 _M_deallocate_buckets(__buckets_ptr __bkts,
size_t __bkt_count)
1632 using _Ptr =
typename __buckets_alloc_traits::pointer;
1634 __buckets_alloc_type __alloc(_M_node_allocator());
1635 __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count);
1641_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
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)
__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.