libstdc++
hashtable_policy.h
Go to the documentation of this file.
1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
2 
3 // Copyright (C) 2010-2026 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/hashtable_policy.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly.
28  * @headername{unordered_map,unordered_set}
29  */
30 
31 #ifndef _HASHTABLE_POLICY_H
32 #define _HASHTABLE_POLICY_H 1
33 
34 #include <tuple> // for std::tuple, std::forward_as_tuple
35 #include <bits/stdexcept_throw.h>
36 #include <bits/functional_hash.h> // for __is_fast_hash
37 #include <bits/stl_algobase.h> // for std::min
38 #include <bits/stl_pair.h> // for std::pair
39 #include <ext/aligned_buffer.h> // for __gnu_cxx::__aligned_buffer
40 #include <ext/alloc_traits.h> // for std::__alloc_rebind
41 #include <ext/numeric_traits.h> // for __gnu_cxx::__int_traits
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 /// @cond undocumented
47 
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>
52  class _Hashtable;
53 
54 namespace __detail
55 {
56  /**
57  * @defgroup hashtable-detail Base and Implementation Classes
58  * @ingroup unordered_associative_containers
59  * @{
60  */
61  template<typename _Key, typename _Value, typename _ExtractKey,
62  typename _Equal, typename _Hash, typename _RangeHash,
63  typename _Unused, typename _Traits>
64  struct _Hashtable_base;
65 
66 #pragma GCC diagnostic push
67 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
68  // Helper function: return distance(first, last) for forward
69  // iterators, or 0/1 for input iterators.
70  template<typename _Iterator>
72  __distance_fw(_Iterator __first, _Iterator __last)
73  {
75  if constexpr (is_convertible<_Cat, forward_iterator_tag>::value)
76  return std::distance(__first, __last);
77  else
78  return __first != __last ? 1 : 0;
79  }
80 #pragma GCC diagnostic pop
81 
82  struct _Identity
83  {
84  template<typename _Tp>
85  _Tp&&
86  operator()(_Tp&& __x) const noexcept
87  { return std::forward<_Tp>(__x); }
88  };
89 
90  struct _Select1st
91  {
92  template<typename _Pair>
93  struct __1st_type;
94 
95  template<typename _Tp, typename _Up>
96  struct __1st_type<pair<_Tp, _Up>>
97  { using type = _Tp; };
98 
99  template<typename _Tp, typename _Up>
100  struct __1st_type<const pair<_Tp, _Up>>
101  { using type = const _Tp; };
102 
103  template<typename _Pair>
104  struct __1st_type<_Pair&>
105  { using type = typename __1st_type<_Pair>::type&; };
106 
107  template<typename _Tp>
108  typename __1st_type<_Tp>::type&&
109  operator()(_Tp&& __x) const noexcept
110  { return std::forward<_Tp>(__x).first; }
111  };
112 
113  template<typename _ExKey>
114  struct _NodeBuilder;
115 
116  template<>
117  struct _NodeBuilder<_Select1st>
118  {
119  template<typename _Kt, typename _Arg, typename _NodeGenerator>
120  static auto
121  _S_build(_Kt&& __k, _Arg&& __arg, _NodeGenerator& __node_gen)
122  -> typename _NodeGenerator::__node_ptr
123  {
124  return __node_gen(std::forward<_Kt>(__k),
125  std::forward<_Arg>(__arg).second);
126  }
127  };
128 
129  template<>
130  struct _NodeBuilder<_Identity>
131  {
132  template<typename _Kt, typename _Arg, typename _NodeGenerator>
133  static auto
134  _S_build(_Kt&& __k, _Arg&&, _NodeGenerator& __node_gen)
135  -> typename _NodeGenerator::__node_ptr
136  { return __node_gen(std::forward<_Kt>(__k)); }
137  };
138 
139  template<typename _HashtableAlloc, typename _NodePtr>
140  struct _NodePtrGuard
141  {
142  _HashtableAlloc& _M_h;
143  _NodePtr _M_ptr;
144 
145  ~_NodePtrGuard()
146  {
147  if (_M_ptr)
148  _M_h._M_deallocate_node_ptr(_M_ptr);
149  }
150  };
151 
152  template<typename _NodeAlloc>
153  struct _Hashtable_alloc;
154 
155  // Functor recycling a pool of nodes and using allocation once the pool is
156  // empty.
157  template<typename _NodeAlloc>
158  struct _ReuseOrAllocNode
159  {
160  private:
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;
165 
166  public:
167  using __node_ptr = typename __hashtable_alloc::__node_ptr;
168 
169  _ReuseOrAllocNode(__node_ptr __nodes, __hashtable_alloc& __h)
170  : _M_nodes(__nodes), _M_h(__h) { }
171  _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete;
172 
173  ~_ReuseOrAllocNode()
174  { _M_h._M_deallocate_nodes(_M_nodes); }
175 
176 #pragma GCC diagnostic push
177 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
178  template<typename _Arg>
179  __node_ptr
180  operator()(_Arg&& __arg)
181  {
182  if (!_M_nodes)
183  return _M_h._M_allocate_node(std::forward<_Arg>(__arg));
184 
185  using value_type = typename _NodeAlloc::value_type::value_type;
186 
187  __node_ptr __node = _M_nodes;
188  if constexpr (is_assignable<value_type&, _Arg>::value)
189  {
190  __node->_M_v() = std::forward<_Arg>(__arg);
191  _M_nodes = _M_nodes->_M_next();
192  __node->_M_nxt = nullptr;
193  }
194  else
195  {
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(),
203  std::forward<_Arg>(__arg));
204  __guard._M_ptr = nullptr;
205  }
206  return __node;
207  }
208 #pragma GCC diagnostic pop
209 
210  private:
211  __node_ptr _M_nodes;
212  __hashtable_alloc& _M_h;
213  };
214 
215  // Functor similar to the previous one but without any pool of nodes to
216  // recycle.
217  template<typename _NodeAlloc>
218  struct _AllocNode
219  {
220  private:
221  using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
222 
223  public:
224  using __node_ptr = typename __hashtable_alloc::__node_ptr;
225 
226  _AllocNode(__hashtable_alloc& __h)
227  : _M_h(__h) { }
228 
229  template<typename... _Args>
230  __node_ptr
231  operator()(_Args&&... __args) const
232  { return _M_h._M_allocate_node(std::forward<_Args>(__args)...); }
233 
234  private:
235  __hashtable_alloc& _M_h;
236  };
237 
238  // Auxiliary types used for all instantiations of _Hashtable nodes
239  // and iterators.
240 
241  /**
242  * struct _Hashtable_traits
243  *
244  * Important traits for hash tables.
245  *
246  * @tparam _Cache_hash_code Boolean value. True if the value of
247  * the hash function is stored along with the value. This is a
248  * time-space tradeoff. Storing it may improve lookup speed by
249  * reducing the number of times we need to call the _Hash or _Equal
250  * functors.
251  *
252  * @tparam _Constant_iterators Boolean value. True if iterator and
253  * const_iterator are both constant iterator types. This is true
254  * for unordered_set and unordered_multiset, false for
255  * unordered_map and unordered_multimap.
256  *
257  * @tparam _Unique_keys Boolean value. True if the return value
258  * of _Hashtable::count(k) is always at most one, false if it may
259  * be an arbitrary number. This is true for unordered_set and
260  * unordered_map, false for unordered_multiset and
261  * unordered_multimap.
262  */
263  template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
264  struct _Hashtable_traits
265  {
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>;
269  };
270 
271  /**
272  * struct _Hashtable_hash_traits
273  *
274  * Important traits for hash tables depending on associated hasher.
275  *
276  */
277  template<typename _Hash>
278  struct _Hashtable_hash_traits
279  {
280  static constexpr size_t
281  __small_size_threshold() noexcept
282  { return std::__is_fast_hash<_Hash>::value ? 0 : 20; }
283  };
284 
285  /**
286  * struct _Hash_node_base
287  *
288  * Nodes, used to wrap elements stored in the hash table. A policy
289  * template parameter of class template _Hashtable controls whether
290  * nodes also store a hash code. In some cases (e.g. strings) this
291  * may be a performance win.
292  */
293  struct _Hash_node_base
294  {
295  _Hash_node_base* _M_nxt;
296 
297  _Hash_node_base() noexcept : _M_nxt() { }
298 
299  _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
300  };
301 
302  /**
303  * struct _Hash_node_value_base
304  *
305  * Node type with the value to store.
306  */
307  template<typename _Value>
308  struct _Hash_node_value_base
309  {
310  using value_type = _Value;
311 
312  __gnu_cxx::__aligned_buffer<_Value> _M_storage;
313 
314  // These member functions must be always_inline, see PR 111050
315 
316  [[__gnu__::__always_inline__]]
317  _Value*
318  _M_valptr() noexcept
319  { return _M_storage._M_ptr(); }
320 
321  [[__gnu__::__always_inline__]]
322  const _Value*
323  _M_valptr() const noexcept
324  { return _M_storage._M_ptr(); }
325 
326  [[__gnu__::__always_inline__]]
327  _Value&
328  _M_v() noexcept
329  { return *_M_valptr(); }
330 
331  [[__gnu__::__always_inline__]]
332  const _Value&
333  _M_v() const noexcept
334  { return *_M_valptr(); }
335  };
336 
337  /**
338  * Primary template struct _Hash_node_code_cache.
339  */
340  template<bool _Cache_hash_code>
341  struct _Hash_node_code_cache
342  { };
343 
344  /**
345  * Specialization for node with cache, struct _Hash_node_code_cache.
346  */
347  template<>
348  struct _Hash_node_code_cache<true>
349  { size_t _M_hash_code; };
350 
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>
355  { };
356 
357  /**
358  * Primary template struct _Hash_node.
359  */
360  template<typename _Value, bool _Cache_hash_code>
361  struct _Hash_node
362  : _Hash_node_base
363  , _Hash_node_value<_Value, _Cache_hash_code>
364  {
365  _Hash_node*
366  _M_next() const noexcept
367  { return static_cast<_Hash_node*>(this->_M_nxt); }
368  };
369 
370  /// Base class for node iterators.
371  template<typename _Value, bool _Cache_hash_code>
372  struct _Node_iterator_base
373  {
374  using __node_type = _Hash_node<_Value, _Cache_hash_code>;
375 
376  __node_type* _M_cur;
377 
378  _Node_iterator_base() : _M_cur(nullptr) { }
379  _Node_iterator_base(__node_type* __p) noexcept
380  : _M_cur(__p) { }
381 
382  void
383  _M_incr() noexcept
384  { _M_cur = _M_cur->_M_next(); }
385 
386  friend bool
387  operator==(const _Node_iterator_base& __x, const _Node_iterator_base& __y)
388  noexcept
389  { return __x._M_cur == __y._M_cur; }
390 
391 #if __cpp_impl_three_way_comparison < 201907L
392  friend bool
393  operator!=(const _Node_iterator_base& __x, const _Node_iterator_base& __y)
394  noexcept
395  { return __x._M_cur != __y._M_cur; }
396 #endif
397  };
398 
399  /// Node iterators, used to iterate through all the hashtable.
400  template<typename _Value, bool __constant_iterators, bool __cache>
401  struct _Node_iterator
402  : public _Node_iterator_base<_Value, __cache>
403  {
404  private:
405  using __base_type = _Node_iterator_base<_Value, __cache>;
406  using __node_type = typename __base_type::__node_type;
407 
408  public:
409  using value_type = _Value;
410  using difference_type = ptrdiff_t;
411  using iterator_category = forward_iterator_tag;
412 
413  using pointer = __conditional_t<__constant_iterators,
414  const value_type*, value_type*>;
415 
416  using reference = __conditional_t<__constant_iterators,
417  const value_type&, value_type&>;
418 
419  _Node_iterator() = default;
420 
421  explicit
422  _Node_iterator(__node_type* __p) noexcept
423  : __base_type(__p) { }
424 
425  reference
426  operator*() const noexcept
427  { return this->_M_cur->_M_v(); }
428 
429  pointer
430  operator->() const noexcept
431  { return this->_M_cur->_M_valptr(); }
432 
433  _Node_iterator&
434  operator++() noexcept
435  {
436  this->_M_incr();
437  return *this;
438  }
439 
440  _Node_iterator
441  operator++(int) noexcept
442  {
443  _Node_iterator __tmp(*this);
444  this->_M_incr();
445  return __tmp;
446  }
447 
448 #if __cpp_impl_three_way_comparison >= 201907L
449  friend bool
450  operator==(const _Node_iterator&, const _Node_iterator&) = default;
451 #else
452  friend bool
453  operator==(const _Node_iterator& __x, const _Node_iterator& __y) noexcept
454  {
455  const __base_type& __bx = __x;
456  const __base_type& __by = __y;
457  return __bx == __by;
458  }
459 
460  friend bool
461  operator!=(const _Node_iterator& __x, const _Node_iterator& __y) noexcept
462  { return !(__x == __y); }
463 #endif
464  };
465 
466  /// Node const_iterators, used to iterate through all the hashtable.
467  template<typename _Value, bool __constant_iterators, bool __cache>
468  struct _Node_const_iterator
469  : public _Node_iterator_base<_Value, __cache>
470  {
471  private:
472  using __base_type = _Node_iterator_base<_Value, __cache>;
473  using __node_type = typename __base_type::__node_type;
474 
475  // The corresponding non-const iterator.
476  using __iterator
477  = _Node_iterator<_Value, __constant_iterators, __cache>;
478 
479  public:
480  using value_type = _Value;
481  using difference_type = ptrdiff_t;
482  using iterator_category = forward_iterator_tag;
483 
484  using pointer = const value_type*;
485  using reference = const value_type&;
486 
487  _Node_const_iterator() = default;
488 
489  explicit
490  _Node_const_iterator(__node_type* __p) noexcept
491  : __base_type(__p) { }
492 
493  _Node_const_iterator(const __iterator& __x) noexcept
494  : __base_type(__x._M_cur) { }
495 
496  reference
497  operator*() const noexcept
498  { return this->_M_cur->_M_v(); }
499 
500  pointer
501  operator->() const noexcept
502  { return this->_M_cur->_M_valptr(); }
503 
504  _Node_const_iterator&
505  operator++() noexcept
506  {
507  this->_M_incr();
508  return *this;
509  }
510 
511  _Node_const_iterator
512  operator++(int) noexcept
513  {
514  _Node_const_iterator __tmp(*this);
515  this->_M_incr();
516  return __tmp;
517  }
518 
519 #if __cpp_impl_three_way_comparison >= 201907L
520  friend bool
521  operator==(const _Node_const_iterator&,
522  const _Node_const_iterator&) = default;
523 
524  friend bool
525  operator==(const _Node_const_iterator& __x, const __iterator& __y)
526  {
527  const __base_type& __bx = __x;
528  const __base_type& __by = __y;
529  return __bx == __by;
530  }
531 #else
532  friend bool
533  operator==(const _Node_const_iterator& __x,
534  const _Node_const_iterator& __y) noexcept
535  {
536  const __base_type& __bx = __x;
537  const __base_type& __by = __y;
538  return __bx == __by;
539  }
540 
541  friend bool
542  operator!=(const _Node_const_iterator& __x,
543  const _Node_const_iterator& __y) noexcept
544  { return !(__x == __y); }
545 
546  friend bool
547  operator==(const _Node_const_iterator& __x,
548  const __iterator& __y) noexcept
549  {
550  const __base_type& __bx = __x;
551  const __base_type& __by = __y;
552  return __bx == __by;
553  }
554 
555  friend bool
556  operator!=(const _Node_const_iterator& __x,
557  const __iterator& __y) noexcept
558  { return !(__x == __y); }
559 
560  friend bool
561  operator==(const __iterator& __x,
562  const _Node_const_iterator& __y) noexcept
563  {
564  const __base_type& __bx = __x;
565  const __base_type& __by = __y;
566  return __bx == __by;
567  }
568 
569  friend bool
570  operator!=(const __iterator& __x,
571  const _Node_const_iterator& __y) noexcept
572  { return !(__x == __y); }
573 #endif
574  };
575 
576  // Many of class template _Hashtable's template parameters are policy
577  // classes. These are defaults for the policies.
578 
579  /// Default range hashing function: use division to fold a large number
580  /// into the range [0, N).
581  struct _Mod_range_hashing
582  {
583  size_t
584  operator()(size_t __num, size_t __den) const noexcept
585  { return __num % __den; }
586  };
587 
588  /// Default ranged hash function H. In principle it should be a
589  /// function object composed from objects of type H1 and H2 such that
590  /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
591  /// h1 and h2. So instead we'll just use a tag to tell class template
592  /// hashtable to do that composition.
593  struct _Default_ranged_hash { };
594 
595  /// Default value for rehash policy. Bucket size is (usually) the
596  /// smallest prime that keeps the load factor small enough.
597  struct _Prime_rehash_policy
598  {
599  using __has_load_factor = true_type;
600 
601  _Prime_rehash_policy(float __z = 1.0) noexcept
602  : _M_max_load_factor(__z), _M_next_resize(0) { }
603 
604  float
605  max_load_factor() const noexcept
606  { return _M_max_load_factor; }
607 
608  // Return a bucket size no smaller than n.
609  // TODO: 'const' qualifier is kept for abi compatibility reason.
610  size_t
611  _M_next_bkt(size_t __n) const;
612 
613  // Return a bucket count appropriate for n elements
614  size_t
615  _M_bkt_for_elements(size_t __n) const
616  { return __builtin_ceil(__n / (double)_M_max_load_factor); }
617 
618  // __n_bkt is current bucket count, __n_elt is current element count,
619  // and __n_ins is number of elements to be inserted. Do we need to
620  // increase bucket count? If so, return make_pair(true, n), where n
621  // is the new bucket count. If not, return make_pair(false, 0).
622  // TODO: 'const' qualifier is kept for abi compatibility reason.
624  _M_need_rehash(size_t __n_bkt, size_t __n_elt,
625  size_t __n_ins) const;
626 
627  using _State = size_t;
628 
629  _State
630  _M_state() const
631  { return _M_next_resize; }
632 
633  void
634  _M_reset() noexcept
635  { _M_next_resize = 0; }
636 
637  void
638  _M_reset(_State __state)
639  { _M_next_resize = __state; }
640 
641  static const size_t _S_growth_factor = 2;
642 
643  float _M_max_load_factor;
644 
645  // TODO: 'mutable' kept for abi compatibility reason.
646  mutable size_t _M_next_resize;
647  };
648 
649  /// Range hashing function assuming that second arg is a power of 2.
650  struct _Mask_range_hashing
651  {
652  size_t
653  operator()(size_t __num, size_t __den) const noexcept
654  { return __num & (__den - 1); }
655  };
656 
657  /// Compute closest power of 2 not less than __n
658  inline size_t
659  __clp2(size_t __n) noexcept
660  {
662  // Equivalent to return __n ? std::bit_ceil(__n) : 0;
663  if (__n < 2)
664  return __n;
665  const unsigned __lz = sizeof(size_t) > sizeof(long)
666  ? __builtin_clzll(__n - 1ull)
667  : __builtin_clzl(__n - 1ul);
668  // Doing two shifts avoids undefined behaviour when __lz == 0.
669  return (size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
670  }
671 
672  /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
673  /// operations.
674  struct _Power2_rehash_policy
675  {
676  using __has_load_factor = true_type;
677 
678  _Power2_rehash_policy(float __z = 1.0) noexcept
679  : _M_max_load_factor(__z), _M_next_resize(0) { }
680 
681  float
682  max_load_factor() const noexcept
683  { return _M_max_load_factor; }
684 
685  // Return a bucket size no smaller than n (as long as n is not above the
686  // highest power of 2).
687  size_t
688  _M_next_bkt(size_t __n) noexcept
689  {
690  if (__n == 0)
691  // Special case on container 1st initialization with 0 bucket count
692  // hint. We keep _M_next_resize to 0 to make sure that next time we
693  // want to add an element allocation will take place.
694  return 1;
695 
696  const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
697  const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
698  size_t __res = __clp2(__n);
699 
700  if (__res == 0)
701  __res = __max_bkt;
702  else if (__res == 1)
703  // If __res is 1 we force it to 2 to make sure there will be an
704  // allocation so that nothing need to be stored in the initial
705  // single bucket
706  __res = 2;
707 
708  if (__res == __max_bkt)
709  // Set next resize to the max value so that we never try to rehash again
710  // as we already reach the biggest possible bucket number.
711  // Note that it might result in max_load_factor not being respected.
712  _M_next_resize = size_t(-1);
713  else
714  _M_next_resize
715  = __builtin_floor(__res * (double)_M_max_load_factor);
716 
717  return __res;
718  }
719 
720  // Return a bucket count appropriate for n elements
721  size_t
722  _M_bkt_for_elements(size_t __n) const noexcept
723  { return __builtin_ceil(__n / (double)_M_max_load_factor); }
724 
725  // __n_bkt is current bucket count, __n_elt is current element count,
726  // and __n_ins is number of elements to be inserted. Do we need to
727  // increase bucket count? If so, return make_pair(true, n), where n
728  // is the new bucket count. If not, return make_pair(false, 0).
730  _M_need_rehash(size_t __n_bkt, size_t __n_elt, size_t __n_ins) noexcept
731  {
732  if (__n_elt + __n_ins > _M_next_resize)
733  {
734  // If _M_next_resize is 0 it means that we have nothing allocated so
735  // far and that we start inserting elements. In this case we start
736  // with an initial bucket size of 11.
737  double __min_bkts
738  = std::max<size_t>(__n_elt + __n_ins, _M_next_resize ? 0 : 11)
739  / (double)_M_max_load_factor;
740  if (__min_bkts >= __n_bkt)
741  return { true,
742  _M_next_bkt(std::max<size_t>(__builtin_floor(__min_bkts) + 1,
743  __n_bkt * _S_growth_factor)) };
744 
745  _M_next_resize
746  = __builtin_floor(__n_bkt * (double)_M_max_load_factor);
747  return { false, 0 };
748  }
749  else
750  return { false, 0 };
751  }
752 
753  using _State = size_t;
754 
755  _State
756  _M_state() const noexcept
757  { return _M_next_resize; }
758 
759  void
760  _M_reset() noexcept
761  { _M_next_resize = 0; }
762 
763  void
764  _M_reset(_State __state) noexcept
765  { _M_next_resize = __state; }
766 
767  static const size_t _S_growth_factor = 2;
768 
769  float _M_max_load_factor;
770  size_t _M_next_resize;
771  };
772 
773  template<typename _RehashPolicy>
774  struct _RehashStateGuard
775  {
776  _RehashPolicy* _M_guarded_obj;
777  typename _RehashPolicy::_State _M_prev_state;
778 
779  _RehashStateGuard(_RehashPolicy& __policy)
780  : _M_guarded_obj(std::__addressof(__policy))
781  , _M_prev_state(__policy._M_state())
782  { }
783  _RehashStateGuard(const _RehashStateGuard&) = delete;
784 
785  ~_RehashStateGuard()
786  {
787  if (_M_guarded_obj)
788  _M_guarded_obj->_M_reset(_M_prev_state);
789  }
790  };
791 
792  // Base classes for std::_Hashtable. We define these base classes
793  // because in some cases we want to do different things depending on
794  // the value of a policy class. In some cases the policy class
795  // affects which member functions and nested typedefs are defined;
796  // we handle that by specializing base class templates. Several of
797  // the base class templates need to access other members of class
798  // template _Hashtable, so we use a variant of the "Curiously
799  // Recurring Template Pattern" (CRTP) technique.
800 
801  /**
802  * Primary class template _Map_base.
803  *
804  * If the hashtable has a value type of the form pair<const T1, T2> and
805  * a key extraction policy (_ExtractKey) that returns the first part
806  * of the pair, the hashtable gets a mapped_type typedef. If it
807  * satisfies those criteria and also has unique keys, then it also
808  * gets an operator[].
809  */
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 { };
816 
817  /// Partial specialization, __unique_keys set to false, std::pair value type.
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>
823  {
824  using mapped_type = _Val;
825  };
826 
827  /// Partial specialization, __unique_keys set to true.
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>
833  {
834  private:
835  using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
836  _Select1st, _Equal, _Hash,
837  _RangeHash, _Unused,
838  _Traits>;
839 
840  using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
841  _Select1st, _Equal, _Hash, _RangeHash,
842  _Unused, _RehashPolicy, _Traits>;
843 
844  using __hash_code = typename __hashtable_base::__hash_code;
845 
846  public:
847  using key_type = typename __hashtable_base::key_type;
848  using mapped_type = _Val;
849 
850  mapped_type&
851  operator[](const key_type& __k);
852 
853  mapped_type&
854  operator[](key_type&& __k);
855 
856  // _GLIBCXX_RESOLVE_LIB_DEFECTS
857  // DR 761. unordered_map needs an at() member function.
858  mapped_type&
859  at(const key_type& __k)
860  {
861  auto __ite = static_cast<__hashtable*>(this)->find(__k);
862  if (!__ite._M_cur)
863  __throw_out_of_range(__N("unordered_map::at"));
864  return __ite->second;
865  }
866 
867  const mapped_type&
868  at(const key_type& __k) const
869  {
870  auto __ite = static_cast<const __hashtable*>(this)->find(__k);
871  if (!__ite._M_cur)
872  __throw_out_of_range(__N("unordered_map::at"));
873  return __ite->second;
874  }
875 
876  template <typename _Kt>
877  mapped_type&
878  _M_at_tr(const _Kt& __k)
879  {
880  auto __ite = static_cast<__hashtable*>(this)->_M_find_tr(__k);
881  if (!__ite._M_cur)
882  __throw_out_of_range(__N("unordered_map::at"));
883  return __ite->second;
884  }
885 
886  template <typename _Kt>
887  const mapped_type&
888  _M_at_tr(const _Kt& __k) const
889  {
890  auto __ite = static_cast<const __hashtable*>(this)->_M_find_tr(__k);
891  if (!__ite._M_cur)
892  __throw_out_of_range(__N("unordered_map::at"));
893  return __ite->second;
894  }
895  };
896 
897  template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
898  typename _Hash, typename _RangeHash, typename _Unused,
899  typename _RehashPolicy, typename _Traits>
900  auto
901  _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
902  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
903  operator[](const key_type& __k)
904  -> mapped_type&
905  {
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;
911 
912  typename __hashtable::_Scoped_node __node {
913  __h,
916  std::tuple<>()
917  };
918  auto __pos
919  = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
920  __node._M_node = nullptr;
921  return __pos->second;
922  }
923 
924  template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
925  typename _Hash, typename _RangeHash, typename _Unused,
926  typename _RehashPolicy, typename _Traits>
927  auto
928  _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
929  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
930  operator[](key_type&& __k)
931  -> mapped_type&
932  {
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;
938 
939  typename __hashtable::_Scoped_node __node {
940  __h,
943  std::tuple<>()
944  };
945  auto __pos
946  = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
947  __node._M_node = nullptr;
948  return __pos->second;
949  }
950 
951  // Partial specialization for unordered_map<const T, U>, see PR 104174.
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>
960  { };
961 
962  template<typename _Policy>
963  using __has_load_factor = typename _Policy::__has_load_factor;
964 
965  /**
966  * Primary class template _Rehash_base.
967  *
968  * Give hashtable the max_load_factor functions and reserve iff the
969  * rehash policy supports it.
970  */
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,
975  typename =
976  __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
977  struct _Rehash_base;
978 
979  /// Specialization when rehash policy doesn't provide load factor management.
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,
986  false_type /* Has load factor */>
987  {
988  };
989 
990  /// Specialization when rehash policy provide load factor management.
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,
997  true_type /* Has load factor */>
998  {
999  private:
1000  using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1001  _Equal, _Hash, _RangeHash, _Unused,
1002  _RehashPolicy, _Traits>;
1003 
1004  public:
1005  float
1006  max_load_factor() const noexcept
1007  {
1008  const __hashtable* __this = static_cast<const __hashtable*>(this);
1009  return __this->__rehash_policy().max_load_factor();
1010  }
1011 
1012  void
1013  max_load_factor(float __z)
1014  {
1015  __hashtable* __this = static_cast<__hashtable*>(this);
1016  __this->__rehash_policy(_RehashPolicy(__z));
1017  }
1018 
1019  void
1020  reserve(size_t __n)
1021  {
1022  __hashtable* __this = static_cast<__hashtable*>(this);
1023  __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
1024  }
1025  };
1026 
1027  /**
1028  * Primary class template _Hashtable_ebo_helper.
1029  *
1030  * Helper class using [[no_unique_address]] to reduce object size.
1031  */
1032  template<typename _Tp,
1033  bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
1034  struct _Hashtable_ebo_helper
1035  {
1036  [[__no_unique_address__]] _Tp _M_obj;
1037  };
1038 
1039 #if ! _GLIBCXX_INLINE_VERSION
1040  // For ABI compatibility reasons, [[no_unique_address]] is only used
1041  // for empty non-final types.
1042  template<typename _Tp>
1043  struct _Hashtable_ebo_helper<_Tp, false>
1044  {
1045  _Tp _M_obj;
1046  };
1047 #endif
1048 
1049  /**
1050  * Primary class template _Local_iterator_base.
1051  *
1052  * Base class for local iterators, used to iterate within a bucket
1053  * but not between buckets.
1054  */
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;
1059 
1060  // Wraps the _Hash object and provides some utility functions for using it.
1061  template<typename _Key, typename _Value, typename _ExtractKey,
1062  typename _Hash, typename _RangeHash, typename _Unused,
1063  bool /* __cache_hash_code */>
1064  struct _Hash_code_base
1065  {
1066  // Gives the local iterator implementation access to _M_bucket_index().
1067  friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1068  _Hash, _RangeHash, _Unused, false>;
1069  public:
1070  using hasher = _Hash;
1071 
1072  hasher
1073  hash_function() const
1074  { return _M_hash._M_obj; }
1075 
1076  protected:
1077  [[__no_unique_address__]] _Hashtable_ebo_helper<_Hash> _M_hash{};
1078 
1079  using __hash_code = size_t;
1080 
1081  // We need the default constructor for the local iterators and _Hashtable
1082  // default constructor.
1083  _Hash_code_base() = default;
1084 
1085  _Hash_code_base(const _Hash& __hash) : _M_hash{__hash} { }
1086 
1087  __hash_code
1088  _M_hash_code(const _Key& __k) const
1089  {
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);
1093  }
1094 
1095  template<typename _Kt>
1096  __hash_code
1097  _M_hash_code_tr(const _Kt& __k) const
1098  {
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);
1102  }
1103 
1104  __hash_code
1105  _M_hash_code(const _Hash_node_value<_Value, false>& __n) const
1106  { return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1107 
1108  __hash_code
1109  _M_hash_code(const _Hash_node_value<_Value, true>& __n) const
1110  { return __n._M_hash_code; }
1111 
1112  size_t
1113  _M_bucket_index(__hash_code __c, size_t __bkt_count) const
1114  { return _RangeHash{}(__c, __bkt_count); }
1115 
1116  size_t
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&>())) )
1120  {
1121  return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
1122  __bkt_count);
1123  }
1124 
1125  size_t
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); }
1129  };
1130 
1131  /// Partial specialization used when nodes contain a cached hash code.
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>
1137  {
1138  protected:
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>;
1142 
1143  _Local_iterator_base() = default;
1144 
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)
1149  { }
1150 
1151  void
1152  _M_incr()
1153  {
1154  __base_node_iter::_M_incr();
1155  if (this->_M_cur)
1156  {
1157  size_t __bkt
1158  = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
1159  if (__bkt != _M_bucket)
1160  this->_M_cur = nullptr;
1161  }
1162  }
1163 
1164  size_t _M_bucket = 0;
1165  size_t _M_bucket_count = 0;
1166 
1167  public:
1168  size_t
1169  _M_get_bucket() const { return _M_bucket; } // for debug mode
1170  };
1171 
1172  // Uninitialized storage for a _Hash object in a local iterator.
1173  // This type is DefaultConstructible even if the _Hash type isn't,
1174  // so that _Local_iterator_base<..., false> can be DefaultConstructible.
1175  template<typename _Hash>
1176  struct _Hash_obj_storage
1177  {
1178  union _Uninit_storage
1179  {
1180  _Uninit_storage() noexcept { }
1181  ~_Uninit_storage() { }
1182 
1183  [[__no_unique_address__]] _Hash _M_h;
1184  };
1185 
1186  [[__no_unique_address__]] _Uninit_storage _M_u;
1187  };
1188 
1189  // Partial specialization used when hash codes are not cached
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>
1195  {
1196  protected:
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>;
1201 
1202  _Local_iterator_base() = default;
1203 
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); }
1209 
1210  ~_Local_iterator_base()
1211  {
1212  if (_M_bucket_count != size_t(-1))
1213  _M_destroy();
1214  }
1215 
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)
1219  {
1220  if (_M_bucket_count != size_t(-1))
1221  _M_init(__iter._M_h());
1222  }
1223 
1224  _Local_iterator_base&
1225  operator=(const _Local_iterator_base& __iter)
1226  {
1227  if (_M_bucket_count != size_t(-1))
1228  _M_destroy();
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());
1234  return *this;
1235  }
1236 
1237  void
1238  _M_incr()
1239  {
1240  __node_iter_base::_M_incr();
1241  if (this->_M_cur)
1242  {
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;
1247  }
1248  }
1249 
1250  size_t _M_bucket = 0;
1251  size_t _M_bucket_count = -1;
1252 
1253  void
1254  _M_init(const _Hash& __h)
1255  { std::_Construct(std::__addressof(__hash_obj_storage::_M_u._M_h), __h); }
1256 
1257  void
1258  _M_destroy() { __hash_obj_storage::_M_u._M_h.~_Hash(); }
1259 
1260  const _Hash&
1261  _M_h() const { return __hash_obj_storage::_M_u._M_h; }
1262 
1263  public:
1264  size_t
1265  _M_get_bucket() const { return _M_bucket; } // for debug mode
1266  };
1267 
1268  /// local iterators
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>
1275  {
1276  private:
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;
1280 
1281  public:
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;
1289 
1290  _Local_iterator() = default;
1291 
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)
1296  { }
1297 
1298  reference
1299  operator*() const
1300  { return this->_M_cur->_M_v(); }
1301 
1302  pointer
1303  operator->() const
1304  { return this->_M_cur->_M_valptr(); }
1305 
1306  _Local_iterator&
1307  operator++()
1308  {
1309  this->_M_incr();
1310  return *this;
1311  }
1312 
1313  _Local_iterator
1314  operator++(int)
1315  {
1316  _Local_iterator __tmp(*this);
1317  this->_M_incr();
1318  return __tmp;
1319  }
1320  };
1321 
1322  /// local const_iterators
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>
1329  {
1330  private:
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;
1334 
1335  public:
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;
1341 
1342  _Local_const_iterator() = default;
1343 
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)
1348  { }
1349 
1350  _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1351  _Hash, _RangeHash, _Unused,
1352  __constant_iterators,
1353  __cache>& __x)
1354  : __base_type(__x)
1355  { }
1356 
1357  reference
1358  operator*() const
1359  { return this->_M_cur->_M_v(); }
1360 
1361  pointer
1362  operator->() const
1363  { return this->_M_cur->_M_valptr(); }
1364 
1365  _Local_const_iterator&
1366  operator++()
1367  {
1368  this->_M_incr();
1369  return *this;
1370  }
1371 
1372  _Local_const_iterator
1373  operator++(int)
1374  {
1375  _Local_const_iterator __tmp(*this);
1376  this->_M_incr();
1377  return __tmp;
1378  }
1379  };
1380 
1381  /**
1382  * Primary class template _Hashtable_base.
1383  *
1384  * Helper class adding management of _Equal functor to
1385  * _Hash_code_base type.
1386  *
1387  * Base class templates are:
1388  * - __detail::_Hash_code_base
1389  */
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>
1396  {
1397  public:
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;
1403 
1404  using __traits_type = _Traits;
1405  using __hash_cached = typename __traits_type::__hash_cached;
1406 
1407  using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1408  _Hash, _RangeHash, _Unused,
1409  __hash_cached::value>;
1410 
1411  using __hash_code = typename __hash_code_base::__hash_code;
1412 
1413  protected:
1414  [[__no_unique_address__]] _Hashtable_ebo_helper<_Equal> _M_equal{};
1415 
1416  _Hashtable_base() = default;
1417 
1418  _Hashtable_base(const _Hash& __hash, const _Equal& __eq)
1419  : __hash_code_base(__hash), _M_equal{__eq}
1420  { }
1421 
1422  bool
1423  _M_key_equals(const _Key& __k,
1424  const _Hash_node_value<_Value,
1425  __hash_cached::value>& __n) const
1426  {
1427  static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
1428  "key equality predicate must be invocable with two arguments of "
1429  "key type");
1430  return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1431  }
1432 
1433  template<typename _Kt>
1434  bool
1435  _M_key_equals_tr(const _Kt& __k,
1436  const _Hash_node_value<_Value, __hash_cached::value>& __n) const
1437  {
1438  static_assert(
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()));
1443  }
1444 
1445 #pragma GCC diagnostic push
1446 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1447  bool
1448  _M_equals(const _Key& __k, __hash_code __c,
1449  const _Hash_node_value<_Value, __hash_cached::value>& __n) const
1450  {
1451  if constexpr (__hash_cached::value)
1452  if (__c != __n._M_hash_code)
1453  return false;
1454 
1455  return _M_key_equals(__k, __n);
1456  }
1457 
1458  template<typename _Kt>
1459  bool
1460  _M_equals_tr(const _Kt& __k, __hash_code __c,
1461  const _Hash_node_value<_Value, __hash_cached::value>& __n) const
1462  {
1463  if constexpr (__hash_cached::value)
1464  if (__c != __n._M_hash_code)
1465  return false;
1466 
1467  return _M_key_equals_tr(__k, __n);
1468  }
1469 
1470  bool
1471  _M_node_equals(
1472  const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1473  const _Hash_node_value<_Value, __hash_cached::value>& __rhn) const
1474  {
1475  if constexpr (__hash_cached::value)
1476  if (__lhn._M_hash_code != __rhn._M_hash_code)
1477  return false;
1478 
1479  return _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1480  }
1481 #pragma GCC diagnostic pop
1482 
1483  const _Equal&
1484  _M_eq() const noexcept { return _M_equal._M_obj; }
1485  };
1486 
1487  /**
1488  * This type deals with all allocation and keeps an allocator instance.
1489  */
1490  template<typename _NodeAlloc>
1491  struct _Hashtable_alloc
1492  {
1493  private:
1494  [[__no_unique_address__]] _Hashtable_ebo_helper<_NodeAlloc> _M_alloc{};
1495 
1496  template<typename>
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; };
1501 
1502  public:
1503  using __node_type = typename _NodeAlloc::value_type;
1504  using __node_alloc_type = _NodeAlloc;
1505  // Use __gnu_cxx to benefit from _S_always_equal and al.
1506  using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
1507 
1508  using __value_alloc_traits = typename __node_alloc_traits::template
1509  rebind_traits<typename __get_value_type<__node_type>::type>;
1510 
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*;
1518 
1519  _Hashtable_alloc() = default;
1520  _Hashtable_alloc(const _Hashtable_alloc&) = default;
1521  _Hashtable_alloc(_Hashtable_alloc&&) = default;
1522 
1523  template<typename _Alloc>
1524  _Hashtable_alloc(_Alloc&& __a)
1525  : _M_alloc{std::forward<_Alloc>(__a)}
1526  { }
1527 
1528  __node_alloc_type&
1529  _M_node_allocator()
1530  { return _M_alloc._M_obj; }
1531 
1532  const __node_alloc_type&
1533  _M_node_allocator() const
1534  { return _M_alloc._M_obj; }
1535 
1536  // Allocate a node and construct an element within it.
1537  template<typename... _Args>
1538  __node_ptr
1539  _M_allocate_node(_Args&&... __args);
1540 
1541  // Destroy the element within a node and deallocate the node.
1542  void
1543  _M_deallocate_node(__node_ptr __n);
1544 
1545  // Deallocate a node.
1546  void
1547  _M_deallocate_node_ptr(__node_ptr __n);
1548 
1549  // Deallocate the linked list of nodes pointed to by __n.
1550  // The elements within the nodes are destroyed.
1551  void
1552  _M_deallocate_nodes(__node_ptr __n);
1553 
1554  __buckets_ptr
1555  _M_allocate_buckets(size_t __bkt_count);
1556 
1557  void
1558  _M_deallocate_buckets(__buckets_ptr, size_t __bkt_count);
1559  };
1560 
1561  // Definitions of class template _Hashtable_alloc's out-of-line member
1562  // functions.
1563  template<typename _NodeAlloc>
1564  template<typename... _Args>
1565  auto
1566  _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
1567  -> __node_ptr
1568  {
1569  auto& __alloc = _M_node_allocator();
1570  auto __nptr = __node_alloc_traits::allocate(__alloc, 1);
1571  __node_ptr __n = std::__to_address(__nptr);
1572  __try
1573  {
1574  ::new ((void*)__n) __node_type;
1575  __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
1576  std::forward<_Args>(__args)...);
1577  return __n;
1578  }
1579  __catch(...)
1580  {
1581  __n->~__node_type();
1582  __node_alloc_traits::deallocate(__alloc, __nptr, 1);
1583  __throw_exception_again;
1584  }
1585  }
1586 
1587  template<typename _NodeAlloc>
1588  void
1589  _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
1590  {
1591  __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
1592  _M_deallocate_node_ptr(__n);
1593  }
1594 
1595  template<typename _NodeAlloc>
1596  void
1597  _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
1598  {
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);
1603  }
1604 
1605  template<typename _NodeAlloc>
1606  void
1607  _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
1608  {
1609  while (__n)
1610  {
1611  __node_ptr __tmp = __n;
1612  __n = __n->_M_next();
1613  _M_deallocate_node(__tmp);
1614  }
1615  }
1616 
1617  template<typename _NodeAlloc>
1618  auto
1619  _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(size_t __bkt_count)
1620  -> __buckets_ptr
1621  {
1622  __buckets_alloc_type __alloc(_M_node_allocator());
1623 
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));
1627  return __p;
1628  }
1629 
1630  template<typename _NodeAlloc>
1631  void
1632  _Hashtable_alloc<_NodeAlloc>::
1633  _M_deallocate_buckets(__buckets_ptr __bkts, size_t __bkt_count)
1634  {
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);
1639  }
1640 
1641  ///@} hashtable-detail
1642 } // namespace __detail
1643 /// @endcond
1644 _GLIBCXX_END_NAMESPACE_VERSION
1645 } // namespace std
1646 
1647 #endif // _HASHTABLE_POLICY_H
Primary class template, tuple.
Definition: tuple:69
Uniform interface to all allocator types.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition: tuple:2733
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Uniform interface to C++98 and C++11 allocators.
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:119
constexpr void _Construct(_Tp *__p, _Args &&... __args)
Uniform interface to all pointer-like types.
Definition: ptr_traits.h:177
ISO C++ entities toplevel namespace is std.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:122
Struct holding two objects of arbitrary type.
Definition: simd.h:306
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:52
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition: stl_pair.h:82
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:434
constexpr _Iterator __base(_Iterator __it)
Traits class for iterators.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.