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-2022 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/stl_algobase.h> // for std::min, std::is_permutation.
36 #include <ext/numeric_traits.h> // for __gnu_cxx::__int_traits
37 
38 namespace std _GLIBCXX_VISIBILITY(default)
39 {
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 /// @cond undocumented
42 
43  template<typename _Key, typename _Value, typename _Alloc,
44  typename _ExtractKey, typename _Equal,
45  typename _Hash, typename _RangeHash, typename _Unused,
46  typename _RehashPolicy, typename _Traits>
47  class _Hashtable;
48 
49 namespace __detail
50 {
51  /**
52  * @defgroup hashtable-detail Base and Implementation Classes
53  * @ingroup unordered_associative_containers
54  * @{
55  */
56  template<typename _Key, typename _Value, typename _ExtractKey,
57  typename _Equal, typename _Hash, typename _RangeHash,
58  typename _Unused, typename _Traits>
59  struct _Hashtable_base;
60 
61  // Helper function: return distance(first, last) for forward
62  // iterators, or 0/1 for input iterators.
63  template<typename _Iterator>
65  __distance_fw(_Iterator __first, _Iterator __last,
67  { return __first != __last ? 1 : 0; }
68 
69  template<typename _Iterator>
71  __distance_fw(_Iterator __first, _Iterator __last,
73  { return std::distance(__first, __last); }
74 
75  template<typename _Iterator>
77  __distance_fw(_Iterator __first, _Iterator __last)
78  { return __distance_fw(__first, __last,
79  std::__iterator_category(__first)); }
80 
81  struct _Identity
82  {
83  template<typename _Tp>
84  _Tp&&
85  operator()(_Tp&& __x) const noexcept
86  { return std::forward<_Tp>(__x); }
87  };
88 
89  struct _Select1st
90  {
91  template<typename _Pair>
92  struct __1st_type;
93 
94  template<typename _Tp, typename _Up>
95  struct __1st_type<pair<_Tp, _Up>>
96  { using type = _Tp; };
97 
98  template<typename _Tp, typename _Up>
99  struct __1st_type<const pair<_Tp, _Up>>
100  { using type = const _Tp; };
101 
102  template<typename _Pair>
103  struct __1st_type<_Pair&>
104  { using type = typename __1st_type<_Pair>::type&; };
105 
106  template<typename _Tp>
107  typename __1st_type<_Tp>::type&&
108  operator()(_Tp&& __x) const noexcept
109  { return std::forward<_Tp>(__x).first; }
110  };
111 
112  template<typename _ExKey>
113  struct _NodeBuilder;
114 
115  template<>
116  struct _NodeBuilder<_Select1st>
117  {
118  template<typename _Kt, typename _Arg, typename _NodeGenerator>
119  static auto
120  _S_build(_Kt&& __k, _Arg&& __arg, const _NodeGenerator& __node_gen)
121  -> typename _NodeGenerator::__node_type*
122  {
123  return __node_gen(std::forward<_Kt>(__k),
124  std::forward<_Arg>(__arg).second);
125  }
126  };
127 
128  template<>
129  struct _NodeBuilder<_Identity>
130  {
131  template<typename _Kt, typename _Arg, typename _NodeGenerator>
132  static auto
133  _S_build(_Kt&& __k, _Arg&&, const _NodeGenerator& __node_gen)
134  -> typename _NodeGenerator::__node_type*
135  { return __node_gen(std::forward<_Kt>(__k)); }
136  };
137 
138  template<typename _NodeAlloc>
139  struct _Hashtable_alloc;
140 
141  // Functor recycling a pool of nodes and using allocation once the pool is
142  // empty.
143  template<typename _NodeAlloc>
144  struct _ReuseOrAllocNode
145  {
146  private:
147  using __node_alloc_type = _NodeAlloc;
148  using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
149  using __node_alloc_traits =
150  typename __hashtable_alloc::__node_alloc_traits;
151 
152  public:
153  using __node_type = typename __hashtable_alloc::__node_type;
154 
155  _ReuseOrAllocNode(__node_type* __nodes, __hashtable_alloc& __h)
156  : _M_nodes(__nodes), _M_h(__h) { }
157  _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete;
158 
159  ~_ReuseOrAllocNode()
160  { _M_h._M_deallocate_nodes(_M_nodes); }
161 
162  template<typename... _Args>
163  __node_type*
164  operator()(_Args&&... __args) const
165  {
166  if (_M_nodes)
167  {
168  __node_type* __node = _M_nodes;
169  _M_nodes = _M_nodes->_M_next();
170  __node->_M_nxt = nullptr;
171  auto& __a = _M_h._M_node_allocator();
172  __node_alloc_traits::destroy(__a, __node->_M_valptr());
173  __try
174  {
175  __node_alloc_traits::construct(__a, __node->_M_valptr(),
176  std::forward<_Args>(__args)...);
177  }
178  __catch(...)
179  {
180  _M_h._M_deallocate_node_ptr(__node);
181  __throw_exception_again;
182  }
183  return __node;
184  }
185  return _M_h._M_allocate_node(std::forward<_Args>(__args)...);
186  }
187 
188  private:
189  mutable __node_type* _M_nodes;
190  __hashtable_alloc& _M_h;
191  };
192 
193  // Functor similar to the previous one but without any pool of nodes to
194  // recycle.
195  template<typename _NodeAlloc>
196  struct _AllocNode
197  {
198  private:
199  using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
200 
201  public:
202  using __node_type = typename __hashtable_alloc::__node_type;
203 
204  _AllocNode(__hashtable_alloc& __h)
205  : _M_h(__h) { }
206 
207  template<typename... _Args>
208  __node_type*
209  operator()(_Args&&... __args) const
210  { return _M_h._M_allocate_node(std::forward<_Args>(__args)...); }
211 
212  private:
213  __hashtable_alloc& _M_h;
214  };
215 
216  // Auxiliary types used for all instantiations of _Hashtable nodes
217  // and iterators.
218 
219  /**
220  * struct _Hashtable_traits
221  *
222  * Important traits for hash tables.
223  *
224  * @tparam _Cache_hash_code Boolean value. True if the value of
225  * the hash function is stored along with the value. This is a
226  * time-space tradeoff. Storing it may improve lookup speed by
227  * reducing the number of times we need to call the _Hash or _Equal
228  * functors.
229  *
230  * @tparam _Constant_iterators Boolean value. True if iterator and
231  * const_iterator are both constant iterator types. This is true
232  * for unordered_set and unordered_multiset, false for
233  * unordered_map and unordered_multimap.
234  *
235  * @tparam _Unique_keys Boolean value. True if the return value
236  * of _Hashtable::count(k) is always at most one, false if it may
237  * be an arbitrary number. This is true for unordered_set and
238  * unordered_map, false for unordered_multiset and
239  * unordered_multimap.
240  */
241  template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
242  struct _Hashtable_traits
243  {
244  using __hash_cached = __bool_constant<_Cache_hash_code>;
245  using __constant_iterators = __bool_constant<_Constant_iterators>;
246  using __unique_keys = __bool_constant<_Unique_keys>;
247  };
248 
249  /**
250  * struct _Hashtable_hash_traits
251  *
252  * Important traits for hash tables depending on associated hasher.
253  *
254  */
255  template<typename _Hash>
256  struct _Hashtable_hash_traits
257  {
258  static constexpr std::size_t
259  __small_size_threshold() noexcept
260  { return std::__is_fast_hash<_Hash>::value ? 0 : 20; }
261  };
262 
263  /**
264  * struct _Hash_node_base
265  *
266  * Nodes, used to wrap elements stored in the hash table. A policy
267  * template parameter of class template _Hashtable controls whether
268  * nodes also store a hash code. In some cases (e.g. strings) this
269  * may be a performance win.
270  */
271  struct _Hash_node_base
272  {
273  _Hash_node_base* _M_nxt;
274 
275  _Hash_node_base() noexcept : _M_nxt() { }
276 
277  _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
278  };
279 
280  /**
281  * struct _Hash_node_value_base
282  *
283  * Node type with the value to store.
284  */
285  template<typename _Value>
286  struct _Hash_node_value_base
287  {
288  typedef _Value value_type;
289 
290  __gnu_cxx::__aligned_buffer<_Value> _M_storage;
291 
292  _Value*
293  _M_valptr() noexcept
294  { return _M_storage._M_ptr(); }
295 
296  const _Value*
297  _M_valptr() const noexcept
298  { return _M_storage._M_ptr(); }
299 
300  _Value&
301  _M_v() noexcept
302  { return *_M_valptr(); }
303 
304  const _Value&
305  _M_v() const noexcept
306  { return *_M_valptr(); }
307  };
308 
309  /**
310  * Primary template struct _Hash_node_code_cache.
311  */
312  template<bool _Cache_hash_code>
313  struct _Hash_node_code_cache
314  { };
315 
316  /**
317  * Specialization for node with cache, struct _Hash_node_code_cache.
318  */
319  template<>
320  struct _Hash_node_code_cache<true>
321  { std::size_t _M_hash_code; };
322 
323  template<typename _Value, bool _Cache_hash_code>
324  struct _Hash_node_value
325  : _Hash_node_value_base<_Value>
326  , _Hash_node_code_cache<_Cache_hash_code>
327  { };
328 
329  /**
330  * Primary template struct _Hash_node.
331  */
332  template<typename _Value, bool _Cache_hash_code>
333  struct _Hash_node
334  : _Hash_node_base
335  , _Hash_node_value<_Value, _Cache_hash_code>
336  {
337  _Hash_node*
338  _M_next() const noexcept
339  { return static_cast<_Hash_node*>(this->_M_nxt); }
340  };
341 
342  /// Base class for node iterators.
343  template<typename _Value, bool _Cache_hash_code>
344  struct _Node_iterator_base
345  {
346  using __node_type = _Hash_node<_Value, _Cache_hash_code>;
347 
348  __node_type* _M_cur;
349 
350  _Node_iterator_base() : _M_cur(nullptr) { }
351  _Node_iterator_base(__node_type* __p) noexcept
352  : _M_cur(__p) { }
353 
354  void
355  _M_incr() noexcept
356  { _M_cur = _M_cur->_M_next(); }
357 
358  friend bool
359  operator==(const _Node_iterator_base& __x, const _Node_iterator_base& __y)
360  noexcept
361  { return __x._M_cur == __y._M_cur; }
362 
363 #if __cpp_impl_three_way_comparison < 201907L
364  friend bool
365  operator!=(const _Node_iterator_base& __x, const _Node_iterator_base& __y)
366  noexcept
367  { return __x._M_cur != __y._M_cur; }
368 #endif
369  };
370 
371  /// Node iterators, used to iterate through all the hashtable.
372  template<typename _Value, bool __constant_iterators, bool __cache>
373  struct _Node_iterator
374  : public _Node_iterator_base<_Value, __cache>
375  {
376  private:
377  using __base_type = _Node_iterator_base<_Value, __cache>;
378  using __node_type = typename __base_type::__node_type;
379 
380  public:
381  using value_type = _Value;
382  using difference_type = std::ptrdiff_t;
383  using iterator_category = std::forward_iterator_tag;
384 
385  using pointer = __conditional_t<__constant_iterators,
386  const value_type*, value_type*>;
387 
388  using reference = __conditional_t<__constant_iterators,
389  const value_type&, value_type&>;
390 
391  _Node_iterator() = default;
392 
393  explicit
394  _Node_iterator(__node_type* __p) noexcept
395  : __base_type(__p) { }
396 
397  reference
398  operator*() const noexcept
399  { return this->_M_cur->_M_v(); }
400 
401  pointer
402  operator->() const noexcept
403  { return this->_M_cur->_M_valptr(); }
404 
405  _Node_iterator&
406  operator++() noexcept
407  {
408  this->_M_incr();
409  return *this;
410  }
411 
412  _Node_iterator
413  operator++(int) noexcept
414  {
415  _Node_iterator __tmp(*this);
416  this->_M_incr();
417  return __tmp;
418  }
419  };
420 
421  /// Node const_iterators, used to iterate through all the hashtable.
422  template<typename _Value, bool __constant_iterators, bool __cache>
423  struct _Node_const_iterator
424  : public _Node_iterator_base<_Value, __cache>
425  {
426  private:
427  using __base_type = _Node_iterator_base<_Value, __cache>;
428  using __node_type = typename __base_type::__node_type;
429 
430  public:
431  typedef _Value value_type;
432  typedef std::ptrdiff_t difference_type;
433  typedef std::forward_iterator_tag iterator_category;
434 
435  typedef const value_type* pointer;
436  typedef const value_type& reference;
437 
438  _Node_const_iterator() = default;
439 
440  explicit
441  _Node_const_iterator(__node_type* __p) noexcept
442  : __base_type(__p) { }
443 
444  _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
445  __cache>& __x) noexcept
446  : __base_type(__x._M_cur) { }
447 
448  reference
449  operator*() const noexcept
450  { return this->_M_cur->_M_v(); }
451 
452  pointer
453  operator->() const noexcept
454  { return this->_M_cur->_M_valptr(); }
455 
456  _Node_const_iterator&
457  operator++() noexcept
458  {
459  this->_M_incr();
460  return *this;
461  }
462 
463  _Node_const_iterator
464  operator++(int) noexcept
465  {
466  _Node_const_iterator __tmp(*this);
467  this->_M_incr();
468  return __tmp;
469  }
470  };
471 
472  // Many of class template _Hashtable's template parameters are policy
473  // classes. These are defaults for the policies.
474 
475  /// Default range hashing function: use division to fold a large number
476  /// into the range [0, N).
477  struct _Mod_range_hashing
478  {
479  typedef std::size_t first_argument_type;
480  typedef std::size_t second_argument_type;
481  typedef std::size_t result_type;
482 
483  result_type
484  operator()(first_argument_type __num,
485  second_argument_type __den) const noexcept
486  { return __num % __den; }
487  };
488 
489  /// Default ranged hash function H. In principle it should be a
490  /// function object composed from objects of type H1 and H2 such that
491  /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
492  /// h1 and h2. So instead we'll just use a tag to tell class template
493  /// hashtable to do that composition.
494  struct _Default_ranged_hash { };
495 
496  /// Default value for rehash policy. Bucket size is (usually) the
497  /// smallest prime that keeps the load factor small enough.
498  struct _Prime_rehash_policy
499  {
500  using __has_load_factor = true_type;
501 
502  _Prime_rehash_policy(float __z = 1.0) noexcept
503  : _M_max_load_factor(__z), _M_next_resize(0) { }
504 
505  float
506  max_load_factor() const noexcept
507  { return _M_max_load_factor; }
508 
509  // Return a bucket size no smaller than n.
510  std::size_t
511  _M_next_bkt(std::size_t __n) const;
512 
513  // Return a bucket count appropriate for n elements
514  std::size_t
515  _M_bkt_for_elements(std::size_t __n) const
516  { return __builtin_ceil(__n / (double)_M_max_load_factor); }
517 
518  // __n_bkt is current bucket count, __n_elt is current element count,
519  // and __n_ins is number of elements to be inserted. Do we need to
520  // increase bucket count? If so, return make_pair(true, n), where n
521  // is the new bucket count. If not, return make_pair(false, 0).
523  _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
524  std::size_t __n_ins) const;
525 
526  typedef std::size_t _State;
527 
528  _State
529  _M_state() const
530  { return _M_next_resize; }
531 
532  void
533  _M_reset() noexcept
534  { _M_next_resize = 0; }
535 
536  void
537  _M_reset(_State __state)
538  { _M_next_resize = __state; }
539 
540  static const std::size_t _S_growth_factor = 2;
541 
542  float _M_max_load_factor;
543  mutable std::size_t _M_next_resize;
544  };
545 
546  /// Range hashing function assuming that second arg is a power of 2.
547  struct _Mask_range_hashing
548  {
549  typedef std::size_t first_argument_type;
550  typedef std::size_t second_argument_type;
551  typedef std::size_t result_type;
552 
553  result_type
554  operator()(first_argument_type __num,
555  second_argument_type __den) const noexcept
556  { return __num & (__den - 1); }
557  };
558 
559  /// Compute closest power of 2 not less than __n
560  inline std::size_t
561  __clp2(std::size_t __n) noexcept
562  {
564  // Equivalent to return __n ? std::bit_ceil(__n) : 0;
565  if (__n < 2)
566  return __n;
567  const unsigned __lz = sizeof(size_t) > sizeof(long)
568  ? __builtin_clzll(__n - 1ull)
569  : __builtin_clzl(__n - 1ul);
570  // Doing two shifts avoids undefined behaviour when __lz == 0.
571  return (size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
572  }
573 
574  /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
575  /// operations.
576  struct _Power2_rehash_policy
577  {
578  using __has_load_factor = true_type;
579 
580  _Power2_rehash_policy(float __z = 1.0) noexcept
581  : _M_max_load_factor(__z), _M_next_resize(0) { }
582 
583  float
584  max_load_factor() const noexcept
585  { return _M_max_load_factor; }
586 
587  // Return a bucket size no smaller than n (as long as n is not above the
588  // highest power of 2).
589  std::size_t
590  _M_next_bkt(std::size_t __n) noexcept
591  {
592  if (__n == 0)
593  // Special case on container 1st initialization with 0 bucket count
594  // hint. We keep _M_next_resize to 0 to make sure that next time we
595  // want to add an element allocation will take place.
596  return 1;
597 
598  const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
599  const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
600  std::size_t __res = __clp2(__n);
601 
602  if (__res == 0)
603  __res = __max_bkt;
604  else if (__res == 1)
605  // If __res is 1 we force it to 2 to make sure there will be an
606  // allocation so that nothing need to be stored in the initial
607  // single bucket
608  __res = 2;
609 
610  if (__res == __max_bkt)
611  // Set next resize to the max value so that we never try to rehash again
612  // as we already reach the biggest possible bucket number.
613  // Note that it might result in max_load_factor not being respected.
614  _M_next_resize = size_t(-1);
615  else
616  _M_next_resize
617  = __builtin_floor(__res * (double)_M_max_load_factor);
618 
619  return __res;
620  }
621 
622  // Return a bucket count appropriate for n elements
623  std::size_t
624  _M_bkt_for_elements(std::size_t __n) const noexcept
625  { return __builtin_ceil(__n / (double)_M_max_load_factor); }
626 
627  // __n_bkt is current bucket count, __n_elt is current element count,
628  // and __n_ins is number of elements to be inserted. Do we need to
629  // increase bucket count? If so, return make_pair(true, n), where n
630  // is the new bucket count. If not, return make_pair(false, 0).
632  _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
633  std::size_t __n_ins) noexcept
634  {
635  if (__n_elt + __n_ins > _M_next_resize)
636  {
637  // If _M_next_resize is 0 it means that we have nothing allocated so
638  // far and that we start inserting elements. In this case we start
639  // with an initial bucket size of 11.
640  double __min_bkts
641  = std::max<std::size_t>(__n_elt + __n_ins, _M_next_resize ? 0 : 11)
642  / (double)_M_max_load_factor;
643  if (__min_bkts >= __n_bkt)
644  return { true,
645  _M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
646  __n_bkt * _S_growth_factor)) };
647 
648  _M_next_resize
649  = __builtin_floor(__n_bkt * (double)_M_max_load_factor);
650  return { false, 0 };
651  }
652  else
653  return { false, 0 };
654  }
655 
656  typedef std::size_t _State;
657 
658  _State
659  _M_state() const noexcept
660  { return _M_next_resize; }
661 
662  void
663  _M_reset() noexcept
664  { _M_next_resize = 0; }
665 
666  void
667  _M_reset(_State __state) noexcept
668  { _M_next_resize = __state; }
669 
670  static const std::size_t _S_growth_factor = 2;
671 
672  float _M_max_load_factor;
673  std::size_t _M_next_resize;
674  };
675 
676  // Base classes for std::_Hashtable. We define these base classes
677  // because in some cases we want to do different things depending on
678  // the value of a policy class. In some cases the policy class
679  // affects which member functions and nested typedefs are defined;
680  // we handle that by specializing base class templates. Several of
681  // the base class templates need to access other members of class
682  // template _Hashtable, so we use a variant of the "Curiously
683  // Recurring Template Pattern" (CRTP) technique.
684 
685  /**
686  * Primary class template _Map_base.
687  *
688  * If the hashtable has a value type of the form pair<const T1, T2> and
689  * a key extraction policy (_ExtractKey) that returns the first part
690  * of the pair, the hashtable gets a mapped_type typedef. If it
691  * satisfies those criteria and also has unique keys, then it also
692  * gets an operator[].
693  */
694  template<typename _Key, typename _Value, typename _Alloc,
695  typename _ExtractKey, typename _Equal,
696  typename _Hash, typename _RangeHash, typename _Unused,
697  typename _RehashPolicy, typename _Traits,
698  bool _Unique_keys = _Traits::__unique_keys::value>
699  struct _Map_base { };
700 
701  /// Partial specialization, __unique_keys set to false, std::pair value type.
702  template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
703  typename _Hash, typename _RangeHash, typename _Unused,
704  typename _RehashPolicy, typename _Traits>
705  struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
706  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
707  {
708  using mapped_type = _Val;
709  };
710 
711  /// Partial specialization, __unique_keys set to true.
712  template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
713  typename _Hash, typename _RangeHash, typename _Unused,
714  typename _RehashPolicy, typename _Traits>
715  struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
716  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
717  {
718  private:
719  using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
720  _Select1st, _Equal, _Hash,
721  _RangeHash, _Unused,
722  _Traits>;
723 
724  using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
725  _Select1st, _Equal, _Hash, _RangeHash,
726  _Unused, _RehashPolicy, _Traits>;
727 
728  using __hash_code = typename __hashtable_base::__hash_code;
729 
730  public:
731  using key_type = typename __hashtable_base::key_type;
732  using mapped_type = _Val;
733 
734  mapped_type&
735  operator[](const key_type& __k);
736 
737  mapped_type&
738  operator[](key_type&& __k);
739 
740  // _GLIBCXX_RESOLVE_LIB_DEFECTS
741  // DR 761. unordered_map needs an at() member function.
742  mapped_type&
743  at(const key_type& __k)
744  {
745  auto __ite = static_cast<__hashtable*>(this)->find(__k);
746  if (!__ite._M_cur)
747  __throw_out_of_range(__N("unordered_map::at"));
748  return __ite->second;
749  }
750 
751  const mapped_type&
752  at(const key_type& __k) const
753  {
754  auto __ite = static_cast<const __hashtable*>(this)->find(__k);
755  if (!__ite._M_cur)
756  __throw_out_of_range(__N("unordered_map::at"));
757  return __ite->second;
758  }
759  };
760 
761  template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
762  typename _Hash, typename _RangeHash, typename _Unused,
763  typename _RehashPolicy, typename _Traits>
764  auto
765  _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
766  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
767  operator[](const key_type& __k)
768  -> mapped_type&
769  {
770  __hashtable* __h = static_cast<__hashtable*>(this);
771  __hash_code __code = __h->_M_hash_code(__k);
772  std::size_t __bkt = __h->_M_bucket_index(__code);
773  if (auto __node = __h->_M_find_node(__bkt, __k, __code))
774  return __node->_M_v().second;
775 
776  typename __hashtable::_Scoped_node __node {
777  __h,
780  std::tuple<>()
781  };
782  auto __pos
783  = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
784  __node._M_node = nullptr;
785  return __pos->second;
786  }
787 
788  template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
789  typename _Hash, typename _RangeHash, typename _Unused,
790  typename _RehashPolicy, typename _Traits>
791  auto
792  _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
793  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
794  operator[](key_type&& __k)
795  -> mapped_type&
796  {
797  __hashtable* __h = static_cast<__hashtable*>(this);
798  __hash_code __code = __h->_M_hash_code(__k);
799  std::size_t __bkt = __h->_M_bucket_index(__code);
800  if (auto __node = __h->_M_find_node(__bkt, __k, __code))
801  return __node->_M_v().second;
802 
803  typename __hashtable::_Scoped_node __node {
804  __h,
807  std::tuple<>()
808  };
809  auto __pos
810  = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
811  __node._M_node = nullptr;
812  return __pos->second;
813  }
814 
815  /**
816  * Primary class template _Insert_base.
817  *
818  * Defines @c insert member functions appropriate to all _Hashtables.
819  */
820  template<typename _Key, typename _Value, typename _Alloc,
821  typename _ExtractKey, typename _Equal,
822  typename _Hash, typename _RangeHash, typename _Unused,
823  typename _RehashPolicy, typename _Traits>
824  struct _Insert_base
825  {
826  protected:
827  using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
828  _Equal, _Hash, _RangeHash,
829  _Unused, _Traits>;
830 
831  using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
832  _Hash, _RangeHash,
833  _Unused, _RehashPolicy, _Traits>;
834 
835  using __hash_cached = typename _Traits::__hash_cached;
836  using __constant_iterators = typename _Traits::__constant_iterators;
837 
838  using __hashtable_alloc = _Hashtable_alloc<
839  __alloc_rebind<_Alloc, _Hash_node<_Value,
840  __hash_cached::value>>>;
841 
842  using value_type = typename __hashtable_base::value_type;
843  using size_type = typename __hashtable_base::size_type;
844 
845  using __unique_keys = typename _Traits::__unique_keys;
846  using __node_alloc_type = typename __hashtable_alloc::__node_alloc_type;
847  using __node_gen_type = _AllocNode<__node_alloc_type>;
848 
849  __hashtable&
850  _M_conjure_hashtable()
851  { return *(static_cast<__hashtable*>(this)); }
852 
853  template<typename _InputIterator, typename _NodeGetter>
854  void
855  _M_insert_range(_InputIterator __first, _InputIterator __last,
856  const _NodeGetter&, true_type __uks);
857 
858  template<typename _InputIterator, typename _NodeGetter>
859  void
860  _M_insert_range(_InputIterator __first, _InputIterator __last,
861  const _NodeGetter&, false_type __uks);
862 
863  public:
864  using iterator = _Node_iterator<_Value, __constant_iterators::value,
865  __hash_cached::value>;
866 
867  using const_iterator = _Node_const_iterator<_Value,
868  __constant_iterators::value,
869  __hash_cached::value>;
870 
871  using __ireturn_type = __conditional_t<__unique_keys::value,
873  iterator>;
874 
875  __ireturn_type
876  insert(const value_type& __v)
877  {
878  __hashtable& __h = _M_conjure_hashtable();
879  __node_gen_type __node_gen(__h);
880  return __h._M_insert(__v, __node_gen, __unique_keys{});
881  }
882 
883  iterator
884  insert(const_iterator __hint, const value_type& __v)
885  {
886  __hashtable& __h = _M_conjure_hashtable();
887  __node_gen_type __node_gen(__h);
888  return __h._M_insert(__hint, __v, __node_gen, __unique_keys{});
889  }
890 
891  template<typename _KType, typename... _Args>
893  try_emplace(const_iterator, _KType&& __k, _Args&&... __args)
894  {
895  __hashtable& __h = _M_conjure_hashtable();
896  auto __code = __h._M_hash_code(__k);
897  std::size_t __bkt = __h._M_bucket_index(__code);
898  if (auto __node = __h._M_find_node(__bkt, __k, __code))
899  return { iterator(__node), false };
900 
901  typename __hashtable::_Scoped_node __node {
902  &__h,
904  std::forward_as_tuple(std::forward<_KType>(__k)),
905  std::forward_as_tuple(std::forward<_Args>(__args)...)
906  };
907  auto __it
908  = __h._M_insert_unique_node(__bkt, __code, __node._M_node);
909  __node._M_node = nullptr;
910  return { __it, true };
911  }
912 
913  void
914  insert(initializer_list<value_type> __l)
915  { this->insert(__l.begin(), __l.end()); }
916 
917  template<typename _InputIterator>
918  void
919  insert(_InputIterator __first, _InputIterator __last)
920  {
921  __hashtable& __h = _M_conjure_hashtable();
922  __node_gen_type __node_gen(__h);
923  return _M_insert_range(__first, __last, __node_gen, __unique_keys{});
924  }
925  };
926 
927  template<typename _Key, typename _Value, typename _Alloc,
928  typename _ExtractKey, typename _Equal,
929  typename _Hash, typename _RangeHash, typename _Unused,
930  typename _RehashPolicy, typename _Traits>
931  template<typename _InputIterator, typename _NodeGetter>
932  void
933  _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
934  _Hash, _RangeHash, _Unused,
935  _RehashPolicy, _Traits>::
936  _M_insert_range(_InputIterator __first, _InputIterator __last,
937  const _NodeGetter& __node_gen, true_type __uks)
938  {
939  __hashtable& __h = _M_conjure_hashtable();
940  for (; __first != __last; ++__first)
941  __h._M_insert(*__first, __node_gen, __uks);
942  }
943 
944  template<typename _Key, typename _Value, typename _Alloc,
945  typename _ExtractKey, typename _Equal,
946  typename _Hash, typename _RangeHash, typename _Unused,
947  typename _RehashPolicy, typename _Traits>
948  template<typename _InputIterator, typename _NodeGetter>
949  void
950  _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
951  _Hash, _RangeHash, _Unused,
952  _RehashPolicy, _Traits>::
953  _M_insert_range(_InputIterator __first, _InputIterator __last,
954  const _NodeGetter& __node_gen, false_type __uks)
955  {
956  using __rehash_type = typename __hashtable::__rehash_type;
957  using __rehash_state = typename __hashtable::__rehash_state;
958  using pair_type = std::pair<bool, std::size_t>;
959 
960  size_type __n_elt = __detail::__distance_fw(__first, __last);
961  if (__n_elt == 0)
962  return;
963 
964  __hashtable& __h = _M_conjure_hashtable();
965  __rehash_type& __rehash = __h._M_rehash_policy;
966  const __rehash_state& __saved_state = __rehash._M_state();
967  pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count,
968  __h._M_element_count,
969  __n_elt);
970 
971  if (__do_rehash.first)
972  __h._M_rehash(__do_rehash.second, __saved_state);
973 
974  for (; __first != __last; ++__first)
975  __h._M_insert(*__first, __node_gen, __uks);
976  }
977 
978  /**
979  * Primary class template _Insert.
980  *
981  * Defines @c insert member functions that depend on _Hashtable policies,
982  * via partial specializations.
983  */
984  template<typename _Key, typename _Value, typename _Alloc,
985  typename _ExtractKey, typename _Equal,
986  typename _Hash, typename _RangeHash, typename _Unused,
987  typename _RehashPolicy, typename _Traits,
988  bool _Constant_iterators = _Traits::__constant_iterators::value>
989  struct _Insert;
990 
991  /// Specialization.
992  template<typename _Key, typename _Value, typename _Alloc,
993  typename _ExtractKey, typename _Equal,
994  typename _Hash, typename _RangeHash, typename _Unused,
995  typename _RehashPolicy, typename _Traits>
996  struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
997  _Hash, _RangeHash, _Unused,
998  _RehashPolicy, _Traits, true>
999  : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1000  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>
1001  {
1002  using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
1003  _Equal, _Hash, _RangeHash, _Unused,
1004  _RehashPolicy, _Traits>;
1005 
1006  using value_type = typename __base_type::value_type;
1007  using iterator = typename __base_type::iterator;
1008  using const_iterator = typename __base_type::const_iterator;
1009  using __ireturn_type = typename __base_type::__ireturn_type;
1010 
1011  using __unique_keys = typename __base_type::__unique_keys;
1012  using __hashtable = typename __base_type::__hashtable;
1013  using __node_gen_type = typename __base_type::__node_gen_type;
1014 
1015  using __base_type::insert;
1016 
1017  __ireturn_type
1018  insert(value_type&& __v)
1019  {
1020  __hashtable& __h = this->_M_conjure_hashtable();
1021  __node_gen_type __node_gen(__h);
1022  return __h._M_insert(std::move(__v), __node_gen, __unique_keys{});
1023  }
1024 
1025  iterator
1026  insert(const_iterator __hint, value_type&& __v)
1027  {
1028  __hashtable& __h = this->_M_conjure_hashtable();
1029  __node_gen_type __node_gen(__h);
1030  return __h._M_insert(__hint, std::move(__v), __node_gen,
1031  __unique_keys{});
1032  }
1033  };
1034 
1035  /// Specialization.
1036  template<typename _Key, typename _Value, typename _Alloc,
1037  typename _ExtractKey, typename _Equal,
1038  typename _Hash, typename _RangeHash, typename _Unused,
1039  typename _RehashPolicy, typename _Traits>
1040  struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1041  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
1042  : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1043  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>
1044  {
1045  using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
1046  _Equal, _Hash, _RangeHash, _Unused,
1047  _RehashPolicy, _Traits>;
1048  using value_type = typename __base_type::value_type;
1049  using iterator = typename __base_type::iterator;
1050  using const_iterator = typename __base_type::const_iterator;
1051 
1052  using __unique_keys = typename __base_type::__unique_keys;
1053  using __hashtable = typename __base_type::__hashtable;
1054  using __ireturn_type = typename __base_type::__ireturn_type;
1055 
1056  using __base_type::insert;
1057 
1058  template<typename _Pair>
1060 
1061  template<typename _Pair>
1062  using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
1063 
1064  template<typename _Pair>
1065  using _IFconsp = typename _IFcons<_Pair>::type;
1066 
1067  template<typename _Pair, typename = _IFconsp<_Pair>>
1068  __ireturn_type
1069  insert(_Pair&& __v)
1070  {
1071  __hashtable& __h = this->_M_conjure_hashtable();
1072  return __h._M_emplace(__unique_keys{}, std::forward<_Pair>(__v));
1073  }
1074 
1075  template<typename _Pair, typename = _IFconsp<_Pair>>
1076  iterator
1077  insert(const_iterator __hint, _Pair&& __v)
1078  {
1079  __hashtable& __h = this->_M_conjure_hashtable();
1080  return __h._M_emplace(__hint, __unique_keys{},
1081  std::forward<_Pair>(__v));
1082  }
1083  };
1084 
1085  template<typename _Policy>
1086  using __has_load_factor = typename _Policy::__has_load_factor;
1087 
1088  /**
1089  * Primary class template _Rehash_base.
1090  *
1091  * Give hashtable the max_load_factor functions and reserve iff the
1092  * rehash policy supports it.
1093  */
1094  template<typename _Key, typename _Value, typename _Alloc,
1095  typename _ExtractKey, typename _Equal,
1096  typename _Hash, typename _RangeHash, typename _Unused,
1097  typename _RehashPolicy, typename _Traits,
1098  typename =
1099  __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
1100  struct _Rehash_base;
1101 
1102  /// Specialization when rehash policy doesn't provide load factor management.
1103  template<typename _Key, typename _Value, typename _Alloc,
1104  typename _ExtractKey, typename _Equal,
1105  typename _Hash, typename _RangeHash, typename _Unused,
1106  typename _RehashPolicy, typename _Traits>
1107  struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1108  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1109  false_type /* Has load factor */>
1110  {
1111  };
1112 
1113  /// Specialization when rehash policy provide load factor management.
1114  template<typename _Key, typename _Value, typename _Alloc,
1115  typename _ExtractKey, typename _Equal,
1116  typename _Hash, typename _RangeHash, typename _Unused,
1117  typename _RehashPolicy, typename _Traits>
1118  struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1119  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1120  true_type /* Has load factor */>
1121  {
1122  private:
1123  using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1124  _Equal, _Hash, _RangeHash, _Unused,
1125  _RehashPolicy, _Traits>;
1126 
1127  public:
1128  float
1129  max_load_factor() const noexcept
1130  {
1131  const __hashtable* __this = static_cast<const __hashtable*>(this);
1132  return __this->__rehash_policy().max_load_factor();
1133  }
1134 
1135  void
1136  max_load_factor(float __z)
1137  {
1138  __hashtable* __this = static_cast<__hashtable*>(this);
1139  __this->__rehash_policy(_RehashPolicy(__z));
1140  }
1141 
1142  void
1143  reserve(std::size_t __n)
1144  {
1145  __hashtable* __this = static_cast<__hashtable*>(this);
1146  __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
1147  }
1148  };
1149 
1150  /**
1151  * Primary class template _Hashtable_ebo_helper.
1152  *
1153  * Helper class using EBO when it is not forbidden (the type is not
1154  * final) and when it is worth it (the type is empty.)
1155  */
1156  template<int _Nm, typename _Tp,
1157  bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
1158  struct _Hashtable_ebo_helper;
1159 
1160  /// Specialization using EBO.
1161  template<int _Nm, typename _Tp>
1162  struct _Hashtable_ebo_helper<_Nm, _Tp, true>
1163  : private _Tp
1164  {
1165  _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { }
1166 
1167  template<typename _OtherTp>
1168  _Hashtable_ebo_helper(_OtherTp&& __tp)
1169  : _Tp(std::forward<_OtherTp>(__tp))
1170  { }
1171 
1172  const _Tp& _M_cget() const { return static_cast<const _Tp&>(*this); }
1173  _Tp& _M_get() { return static_cast<_Tp&>(*this); }
1174  };
1175 
1176  /// Specialization not using EBO.
1177  template<int _Nm, typename _Tp>
1178  struct _Hashtable_ebo_helper<_Nm, _Tp, false>
1179  {
1180  _Hashtable_ebo_helper() = default;
1181 
1182  template<typename _OtherTp>
1183  _Hashtable_ebo_helper(_OtherTp&& __tp)
1184  : _M_tp(std::forward<_OtherTp>(__tp))
1185  { }
1186 
1187  const _Tp& _M_cget() const { return _M_tp; }
1188  _Tp& _M_get() { return _M_tp; }
1189 
1190  private:
1191  _Tp _M_tp{};
1192  };
1193 
1194  /**
1195  * Primary class template _Local_iterator_base.
1196  *
1197  * Base class for local iterators, used to iterate within a bucket
1198  * but not between buckets.
1199  */
1200  template<typename _Key, typename _Value, typename _ExtractKey,
1201  typename _Hash, typename _RangeHash, typename _Unused,
1202  bool __cache_hash_code>
1203  struct _Local_iterator_base;
1204 
1205  /**
1206  * Primary class template _Hash_code_base.
1207  *
1208  * Encapsulates two policy issues that aren't quite orthogonal.
1209  * (1) the difference between using a ranged hash function and using
1210  * the combination of a hash function and a range-hashing function.
1211  * In the former case we don't have such things as hash codes, so
1212  * we have a dummy type as placeholder.
1213  * (2) Whether or not we cache hash codes. Caching hash codes is
1214  * meaningless if we have a ranged hash function.
1215  *
1216  * We also put the key extraction objects here, for convenience.
1217  * Each specialization derives from one or more of the template
1218  * parameters to benefit from Ebo. This is important as this type
1219  * is inherited in some cases by the _Local_iterator_base type used
1220  * to implement local_iterator and const_local_iterator. As with
1221  * any iterator type we prefer to make it as small as possible.
1222  */
1223  template<typename _Key, typename _Value, typename _ExtractKey,
1224  typename _Hash, typename _RangeHash, typename _Unused,
1225  bool __cache_hash_code>
1226  struct _Hash_code_base
1227  : private _Hashtable_ebo_helper<1, _Hash>
1228  {
1229  private:
1230  using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>;
1231 
1232  // Gives the local iterator implementation access to _M_bucket_index().
1233  friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1234  _Hash, _RangeHash, _Unused, false>;
1235 
1236  public:
1237  typedef _Hash hasher;
1238 
1239  hasher
1240  hash_function() const
1241  { return _M_hash(); }
1242 
1243  protected:
1244  typedef std::size_t __hash_code;
1245 
1246  // We need the default constructor for the local iterators and _Hashtable
1247  // default constructor.
1248  _Hash_code_base() = default;
1249 
1250  _Hash_code_base(const _Hash& __hash) : __ebo_hash(__hash) { }
1251 
1252  __hash_code
1253  _M_hash_code(const _Key& __k) const
1254  {
1255  static_assert(__is_invocable<const _Hash&, const _Key&>{},
1256  "hash function must be invocable with an argument of key type");
1257  return _M_hash()(__k);
1258  }
1259 
1260  template<typename _Kt>
1261  __hash_code
1262  _M_hash_code_tr(const _Kt& __k) const
1263  {
1264  static_assert(__is_invocable<const _Hash&, const _Kt&>{},
1265  "hash function must be invocable with an argument of key type");
1266  return _M_hash()(__k);
1267  }
1268 
1269  __hash_code
1270  _M_hash_code(const _Hash&,
1271  const _Hash_node_value<_Value, true>& __n) const
1272  { return __n._M_hash_code; }
1273 
1274  // Compute hash code using _Hash as __n _M_hash_code, if present, was
1275  // computed using _H2.
1276  template<typename _H2>
1277  __hash_code
1278  _M_hash_code(const _H2&,
1279  const _Hash_node_value<_Value, __cache_hash_code>& __n) const
1280  { return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1281 
1282  __hash_code
1283  _M_hash_code(const _Hash_node_value<_Value, false>& __n) const
1284  { return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1285 
1286  __hash_code
1287  _M_hash_code(const _Hash_node_value<_Value, true>& __n) const
1288  { return __n._M_hash_code; }
1289 
1290  std::size_t
1291  _M_bucket_index(__hash_code __c, std::size_t __bkt_count) const
1292  { return _RangeHash{}(__c, __bkt_count); }
1293 
1294  std::size_t
1295  _M_bucket_index(const _Hash_node_value<_Value, false>& __n,
1296  std::size_t __bkt_count) const
1297  noexcept( noexcept(declval<const _Hash&>()(declval<const _Key&>()))
1298  && noexcept(declval<const _RangeHash&>()((__hash_code)0,
1299  (std::size_t)0)) )
1300  {
1301  return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
1302  __bkt_count);
1303  }
1304 
1305  std::size_t
1306  _M_bucket_index(const _Hash_node_value<_Value, true>& __n,
1307  std::size_t __bkt_count) const
1308  noexcept( noexcept(declval<const _RangeHash&>()((__hash_code)0,
1309  (std::size_t)0)) )
1310  { return _RangeHash{}(__n._M_hash_code, __bkt_count); }
1311 
1312  void
1313  _M_store_code(_Hash_node_code_cache<false>&, __hash_code) const
1314  { }
1315 
1316  void
1317  _M_copy_code(_Hash_node_code_cache<false>&,
1318  const _Hash_node_code_cache<false>&) const
1319  { }
1320 
1321  void
1322  _M_store_code(_Hash_node_code_cache<true>& __n, __hash_code __c) const
1323  { __n._M_hash_code = __c; }
1324 
1325  void
1326  _M_copy_code(_Hash_node_code_cache<true>& __to,
1327  const _Hash_node_code_cache<true>& __from) const
1328  { __to._M_hash_code = __from._M_hash_code; }
1329 
1330  void
1331  _M_swap(_Hash_code_base& __x)
1332  { std::swap(__ebo_hash::_M_get(), __x.__ebo_hash::_M_get()); }
1333 
1334  const _Hash&
1335  _M_hash() const { return __ebo_hash::_M_cget(); }
1336  };
1337 
1338  /// Partial specialization used when nodes contain a cached hash code.
1339  template<typename _Key, typename _Value, typename _ExtractKey,
1340  typename _Hash, typename _RangeHash, typename _Unused>
1341  struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1342  _Hash, _RangeHash, _Unused, true>
1343  : public _Node_iterator_base<_Value, true>
1344  {
1345  protected:
1346  using __base_node_iter = _Node_iterator_base<_Value, true>;
1347  using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1348  _Hash, _RangeHash, _Unused, true>;
1349 
1350  _Local_iterator_base() = default;
1351  _Local_iterator_base(const __hash_code_base&,
1352  _Hash_node<_Value, true>* __p,
1353  std::size_t __bkt, std::size_t __bkt_count)
1354  : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1355  { }
1356 
1357  void
1358  _M_incr()
1359  {
1360  __base_node_iter::_M_incr();
1361  if (this->_M_cur)
1362  {
1363  std::size_t __bkt
1364  = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
1365  if (__bkt != _M_bucket)
1366  this->_M_cur = nullptr;
1367  }
1368  }
1369 
1370  std::size_t _M_bucket;
1371  std::size_t _M_bucket_count;
1372 
1373  public:
1374  std::size_t
1375  _M_get_bucket() const { return _M_bucket; } // for debug mode
1376  };
1377 
1378  // Uninitialized storage for a _Hash_code_base.
1379  // This type is DefaultConstructible and Assignable even if the
1380  // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1381  // can be DefaultConstructible and Assignable.
1382  template<typename _Tp, bool _IsEmpty = std::is_empty<_Tp>::value>
1383  struct _Hash_code_storage
1384  {
1385  __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
1386 
1387  _Tp*
1388  _M_h() { return _M_storage._M_ptr(); }
1389 
1390  const _Tp*
1391  _M_h() const { return _M_storage._M_ptr(); }
1392  };
1393 
1394  // Empty partial specialization for empty _Hash_code_base types.
1395  template<typename _Tp>
1396  struct _Hash_code_storage<_Tp, true>
1397  {
1398  static_assert( std::is_empty<_Tp>::value, "Type must be empty" );
1399 
1400  // As _Tp is an empty type there will be no bytes written/read through
1401  // the cast pointer, so no strict-aliasing violation.
1402  _Tp*
1403  _M_h() { return reinterpret_cast<_Tp*>(this); }
1404 
1405  const _Tp*
1406  _M_h() const { return reinterpret_cast<const _Tp*>(this); }
1407  };
1408 
1409  template<typename _Key, typename _Value, typename _ExtractKey,
1410  typename _Hash, typename _RangeHash, typename _Unused>
1411  using __hash_code_for_local_iter
1412  = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey,
1413  _Hash, _RangeHash, _Unused, false>>;
1414 
1415  // Partial specialization used when hash codes are not cached
1416  template<typename _Key, typename _Value, typename _ExtractKey,
1417  typename _Hash, typename _RangeHash, typename _Unused>
1418  struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1419  _Hash, _RangeHash, _Unused, false>
1420  : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1421  _Unused>
1422  , _Node_iterator_base<_Value, false>
1423  {
1424  protected:
1425  using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1426  _Hash, _RangeHash, _Unused, false>;
1427  using __node_iter_base = _Node_iterator_base<_Value, false>;
1428 
1429  _Local_iterator_base() : _M_bucket_count(-1) { }
1430 
1431  _Local_iterator_base(const __hash_code_base& __base,
1432  _Hash_node<_Value, false>* __p,
1433  std::size_t __bkt, std::size_t __bkt_count)
1434  : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1435  { _M_init(__base); }
1436 
1437  ~_Local_iterator_base()
1438  {
1439  if (_M_bucket_count != size_t(-1))
1440  _M_destroy();
1441  }
1442 
1443  _Local_iterator_base(const _Local_iterator_base& __iter)
1444  : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket)
1445  , _M_bucket_count(__iter._M_bucket_count)
1446  {
1447  if (_M_bucket_count != size_t(-1))
1448  _M_init(*__iter._M_h());
1449  }
1450 
1451  _Local_iterator_base&
1452  operator=(const _Local_iterator_base& __iter)
1453  {
1454  if (_M_bucket_count != -1)
1455  _M_destroy();
1456  this->_M_cur = __iter._M_cur;
1457  _M_bucket = __iter._M_bucket;
1458  _M_bucket_count = __iter._M_bucket_count;
1459  if (_M_bucket_count != -1)
1460  _M_init(*__iter._M_h());
1461  return *this;
1462  }
1463 
1464  void
1465  _M_incr()
1466  {
1467  __node_iter_base::_M_incr();
1468  if (this->_M_cur)
1469  {
1470  std::size_t __bkt = this->_M_h()->_M_bucket_index(*this->_M_cur,
1471  _M_bucket_count);
1472  if (__bkt != _M_bucket)
1473  this->_M_cur = nullptr;
1474  }
1475  }
1476 
1477  std::size_t _M_bucket;
1478  std::size_t _M_bucket_count;
1479 
1480  void
1481  _M_init(const __hash_code_base& __base)
1482  { ::new(this->_M_h()) __hash_code_base(__base); }
1483 
1484  void
1485  _M_destroy() { this->_M_h()->~__hash_code_base(); }
1486 
1487  public:
1488  std::size_t
1489  _M_get_bucket() const { return _M_bucket; } // for debug mode
1490  };
1491 
1492  /// local iterators
1493  template<typename _Key, typename _Value, typename _ExtractKey,
1494  typename _Hash, typename _RangeHash, typename _Unused,
1495  bool __constant_iterators, bool __cache>
1496  struct _Local_iterator
1497  : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1498  _Hash, _RangeHash, _Unused, __cache>
1499  {
1500  private:
1501  using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1502  _Hash, _RangeHash, _Unused, __cache>;
1503  using __hash_code_base = typename __base_type::__hash_code_base;
1504 
1505  public:
1506  using value_type = _Value;
1507  using pointer = __conditional_t<__constant_iterators,
1508  const value_type*, value_type*>;
1509  using reference = __conditional_t<__constant_iterators,
1510  const value_type&, value_type&>;
1511  using difference_type = ptrdiff_t;
1512  using iterator_category = forward_iterator_tag;
1513 
1514  _Local_iterator() = default;
1515 
1516  _Local_iterator(const __hash_code_base& __base,
1517  _Hash_node<_Value, __cache>* __n,
1518  std::size_t __bkt, std::size_t __bkt_count)
1519  : __base_type(__base, __n, __bkt, __bkt_count)
1520  { }
1521 
1522  reference
1523  operator*() const
1524  { return this->_M_cur->_M_v(); }
1525 
1526  pointer
1527  operator->() const
1528  { return this->_M_cur->_M_valptr(); }
1529 
1530  _Local_iterator&
1531  operator++()
1532  {
1533  this->_M_incr();
1534  return *this;
1535  }
1536 
1537  _Local_iterator
1538  operator++(int)
1539  {
1540  _Local_iterator __tmp(*this);
1541  this->_M_incr();
1542  return __tmp;
1543  }
1544  };
1545 
1546  /// local const_iterators
1547  template<typename _Key, typename _Value, typename _ExtractKey,
1548  typename _Hash, typename _RangeHash, typename _Unused,
1549  bool __constant_iterators, bool __cache>
1550  struct _Local_const_iterator
1551  : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1552  _Hash, _RangeHash, _Unused, __cache>
1553  {
1554  private:
1555  using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1556  _Hash, _RangeHash, _Unused, __cache>;
1557  using __hash_code_base = typename __base_type::__hash_code_base;
1558 
1559  public:
1560  typedef _Value value_type;
1561  typedef const value_type* pointer;
1562  typedef const value_type& reference;
1563  typedef std::ptrdiff_t difference_type;
1564  typedef std::forward_iterator_tag iterator_category;
1565 
1566  _Local_const_iterator() = default;
1567 
1568  _Local_const_iterator(const __hash_code_base& __base,
1569  _Hash_node<_Value, __cache>* __n,
1570  std::size_t __bkt, std::size_t __bkt_count)
1571  : __base_type(__base, __n, __bkt, __bkt_count)
1572  { }
1573 
1574  _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1575  _Hash, _RangeHash, _Unused,
1576  __constant_iterators,
1577  __cache>& __x)
1578  : __base_type(__x)
1579  { }
1580 
1581  reference
1582  operator*() const
1583  { return this->_M_cur->_M_v(); }
1584 
1585  pointer
1586  operator->() const
1587  { return this->_M_cur->_M_valptr(); }
1588 
1589  _Local_const_iterator&
1590  operator++()
1591  {
1592  this->_M_incr();
1593  return *this;
1594  }
1595 
1596  _Local_const_iterator
1597  operator++(int)
1598  {
1599  _Local_const_iterator __tmp(*this);
1600  this->_M_incr();
1601  return __tmp;
1602  }
1603  };
1604 
1605  /**
1606  * Primary class template _Hashtable_base.
1607  *
1608  * Helper class adding management of _Equal functor to
1609  * _Hash_code_base type.
1610  *
1611  * Base class templates are:
1612  * - __detail::_Hash_code_base
1613  * - __detail::_Hashtable_ebo_helper
1614  */
1615  template<typename _Key, typename _Value, typename _ExtractKey,
1616  typename _Equal, typename _Hash, typename _RangeHash,
1617  typename _Unused, typename _Traits>
1618  struct _Hashtable_base
1619  : public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1620  _Unused, _Traits::__hash_cached::value>,
1621  private _Hashtable_ebo_helper<0, _Equal>
1622  {
1623  public:
1624  typedef _Key key_type;
1625  typedef _Value value_type;
1626  typedef _Equal key_equal;
1627  typedef std::size_t size_type;
1628  typedef std::ptrdiff_t difference_type;
1629 
1630  using __traits_type = _Traits;
1631  using __hash_cached = typename __traits_type::__hash_cached;
1632 
1633  using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1634  _Hash, _RangeHash, _Unused,
1635  __hash_cached::value>;
1636 
1637  using __hash_code = typename __hash_code_base::__hash_code;
1638 
1639  private:
1640  using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
1641 
1642  static bool
1643  _S_equals(__hash_code, const _Hash_node_code_cache<false>&)
1644  { return true; }
1645 
1646  static bool
1647  _S_node_equals(const _Hash_node_code_cache<false>&,
1648  const _Hash_node_code_cache<false>&)
1649  { return true; }
1650 
1651  static bool
1652  _S_equals(__hash_code __c, const _Hash_node_code_cache<true>& __n)
1653  { return __c == __n._M_hash_code; }
1654 
1655  static bool
1656  _S_node_equals(const _Hash_node_code_cache<true>& __lhn,
1657  const _Hash_node_code_cache<true>& __rhn)
1658  { return __lhn._M_hash_code == __rhn._M_hash_code; }
1659 
1660  protected:
1661  _Hashtable_base() = default;
1662 
1663  _Hashtable_base(const _Hash& __hash, const _Equal& __eq)
1664  : __hash_code_base(__hash), _EqualEBO(__eq)
1665  { }
1666 
1667  bool
1668  _M_key_equals(const _Key& __k,
1669  const _Hash_node_value<_Value,
1670  __hash_cached::value>& __n) const
1671  {
1672  static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
1673  "key equality predicate must be invocable with two arguments of "
1674  "key type");
1675  return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1676  }
1677 
1678  template<typename _Kt>
1679  bool
1680  _M_key_equals_tr(const _Kt& __k,
1681  const _Hash_node_value<_Value,
1682  __hash_cached::value>& __n) const
1683  {
1684  static_assert(
1685  __is_invocable<const _Equal&, const _Kt&, const _Key&>{},
1686  "key equality predicate must be invocable with two arguments of "
1687  "key type");
1688  return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1689  }
1690 
1691  bool
1692  _M_equals(const _Key& __k, __hash_code __c,
1693  const _Hash_node_value<_Value, __hash_cached::value>& __n) const
1694  { return _S_equals(__c, __n) && _M_key_equals(__k, __n); }
1695 
1696  template<typename _Kt>
1697  bool
1698  _M_equals_tr(const _Kt& __k, __hash_code __c,
1699  const _Hash_node_value<_Value,
1700  __hash_cached::value>& __n) const
1701  { return _S_equals(__c, __n) && _M_key_equals_tr(__k, __n); }
1702 
1703  bool
1704  _M_node_equals(
1705  const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1706  const _Hash_node_value<_Value, __hash_cached::value>& __rhn) const
1707  {
1708  return _S_node_equals(__lhn, __rhn)
1709  && _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1710  }
1711 
1712  void
1713  _M_swap(_Hashtable_base& __x)
1714  {
1715  __hash_code_base::_M_swap(__x);
1716  std::swap(_EqualEBO::_M_get(), __x._EqualEBO::_M_get());
1717  }
1718 
1719  const _Equal&
1720  _M_eq() const { return _EqualEBO::_M_cget(); }
1721  };
1722 
1723  /**
1724  * Primary class template _Equality.
1725  *
1726  * This is for implementing equality comparison for unordered
1727  * containers, per N3068, by John Lakos and Pablo Halpern.
1728  * Algorithmically, we follow closely the reference implementations
1729  * therein.
1730  */
1731  template<typename _Key, typename _Value, typename _Alloc,
1732  typename _ExtractKey, typename _Equal,
1733  typename _Hash, typename _RangeHash, typename _Unused,
1734  typename _RehashPolicy, typename _Traits,
1735  bool _Unique_keys = _Traits::__unique_keys::value>
1736  struct _Equality;
1737 
1738  /// unordered_map and unordered_set specializations.
1739  template<typename _Key, typename _Value, typename _Alloc,
1740  typename _ExtractKey, typename _Equal,
1741  typename _Hash, typename _RangeHash, typename _Unused,
1742  typename _RehashPolicy, typename _Traits>
1743  struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1744  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
1745  {
1746  using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1747  _Hash, _RangeHash, _Unused,
1748  _RehashPolicy, _Traits>;
1749 
1750  bool
1751  _M_equal(const __hashtable&) const;
1752  };
1753 
1754  template<typename _Key, typename _Value, typename _Alloc,
1755  typename _ExtractKey, typename _Equal,
1756  typename _Hash, typename _RangeHash, typename _Unused,
1757  typename _RehashPolicy, typename _Traits>
1758  bool
1759  _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1760  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
1761  _M_equal(const __hashtable& __other) const
1762  {
1763  using __node_type = typename __hashtable::__node_type;
1764  const __hashtable* __this = static_cast<const __hashtable*>(this);
1765  if (__this->size() != __other.size())
1766  return false;
1767 
1768  for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1769  {
1770  std::size_t __ybkt = __other._M_bucket_index(*__itx._M_cur);
1771  auto __prev_n = __other._M_buckets[__ybkt];
1772  if (!__prev_n)
1773  return false;
1774 
1775  for (__node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);;
1776  __n = __n->_M_next())
1777  {
1778  if (__n->_M_v() == *__itx)
1779  break;
1780 
1781  if (!__n->_M_nxt
1782  || __other._M_bucket_index(*__n->_M_next()) != __ybkt)
1783  return false;
1784  }
1785  }
1786 
1787  return true;
1788  }
1789 
1790  /// unordered_multiset and unordered_multimap specializations.
1791  template<typename _Key, typename _Value, typename _Alloc,
1792  typename _ExtractKey, typename _Equal,
1793  typename _Hash, typename _RangeHash, typename _Unused,
1794  typename _RehashPolicy, typename _Traits>
1795  struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1796  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
1797  {
1798  using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1799  _Hash, _RangeHash, _Unused,
1800  _RehashPolicy, _Traits>;
1801 
1802  bool
1803  _M_equal(const __hashtable&) const;
1804  };
1805 
1806  template<typename _Key, typename _Value, typename _Alloc,
1807  typename _ExtractKey, typename _Equal,
1808  typename _Hash, typename _RangeHash, typename _Unused,
1809  typename _RehashPolicy, typename _Traits>
1810  bool
1811  _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1812  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>::
1813  _M_equal(const __hashtable& __other) const
1814  {
1815  using __node_type = typename __hashtable::__node_type;
1816  const __hashtable* __this = static_cast<const __hashtable*>(this);
1817  if (__this->size() != __other.size())
1818  return false;
1819 
1820  for (auto __itx = __this->begin(); __itx != __this->end();)
1821  {
1822  std::size_t __x_count = 1;
1823  auto __itx_end = __itx;
1824  for (++__itx_end; __itx_end != __this->end()
1825  && __this->key_eq()(_ExtractKey{}(*__itx),
1826  _ExtractKey{}(*__itx_end));
1827  ++__itx_end)
1828  ++__x_count;
1829 
1830  std::size_t __ybkt = __other._M_bucket_index(*__itx._M_cur);
1831  auto __y_prev_n = __other._M_buckets[__ybkt];
1832  if (!__y_prev_n)
1833  return false;
1834 
1835  __node_type* __y_n = static_cast<__node_type*>(__y_prev_n->_M_nxt);
1836  for (;;)
1837  {
1838  if (__this->key_eq()(_ExtractKey{}(__y_n->_M_v()),
1839  _ExtractKey{}(*__itx)))
1840  break;
1841 
1842  auto __y_ref_n = __y_n;
1843  for (__y_n = __y_n->_M_next(); __y_n; __y_n = __y_n->_M_next())
1844  if (!__other._M_node_equals(*__y_ref_n, *__y_n))
1845  break;
1846 
1847  if (!__y_n || __other._M_bucket_index(*__y_n) != __ybkt)
1848  return false;
1849  }
1850 
1851  typename __hashtable::const_iterator __ity(__y_n);
1852  for (auto __ity_end = __ity; __ity_end != __other.end(); ++__ity_end)
1853  if (--__x_count == 0)
1854  break;
1855 
1856  if (__x_count != 0)
1857  return false;
1858 
1859  if (!std::is_permutation(__itx, __itx_end, __ity))
1860  return false;
1861 
1862  __itx = __itx_end;
1863  }
1864  return true;
1865  }
1866 
1867  /**
1868  * This type deals with all allocation and keeps an allocator instance
1869  * through inheritance to benefit from EBO when possible.
1870  */
1871  template<typename _NodeAlloc>
1872  struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc>
1873  {
1874  private:
1875  using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>;
1876 
1877  template<typename>
1878  struct __get_value_type;
1879  template<typename _Val, bool _Cache_hash_code>
1880  struct __get_value_type<_Hash_node<_Val, _Cache_hash_code>>
1881  { using type = _Val; };
1882 
1883  public:
1884  using __node_type = typename _NodeAlloc::value_type;
1885  using __node_alloc_type = _NodeAlloc;
1886  // Use __gnu_cxx to benefit from _S_always_equal and al.
1887  using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
1888 
1889  using __value_alloc_traits = typename __node_alloc_traits::template
1890  rebind_traits<typename __get_value_type<__node_type>::type>;
1891 
1892  using __node_ptr = __node_type*;
1893  using __node_base = _Hash_node_base;
1894  using __node_base_ptr = __node_base*;
1895  using __buckets_alloc_type =
1896  __alloc_rebind<__node_alloc_type, __node_base_ptr>;
1897  using __buckets_alloc_traits = std::allocator_traits<__buckets_alloc_type>;
1898  using __buckets_ptr = __node_base_ptr*;
1899 
1900  _Hashtable_alloc() = default;
1901  _Hashtable_alloc(const _Hashtable_alloc&) = default;
1902  _Hashtable_alloc(_Hashtable_alloc&&) = default;
1903 
1904  template<typename _Alloc>
1905  _Hashtable_alloc(_Alloc&& __a)
1906  : __ebo_node_alloc(std::forward<_Alloc>(__a))
1907  { }
1908 
1909  __node_alloc_type&
1910  _M_node_allocator()
1911  { return __ebo_node_alloc::_M_get(); }
1912 
1913  const __node_alloc_type&
1914  _M_node_allocator() const
1915  { return __ebo_node_alloc::_M_cget(); }
1916 
1917  // Allocate a node and construct an element within it.
1918  template<typename... _Args>
1919  __node_ptr
1920  _M_allocate_node(_Args&&... __args);
1921 
1922  // Destroy the element within a node and deallocate the node.
1923  void
1924  _M_deallocate_node(__node_ptr __n);
1925 
1926  // Deallocate a node.
1927  void
1928  _M_deallocate_node_ptr(__node_ptr __n);
1929 
1930  // Deallocate the linked list of nodes pointed to by __n.
1931  // The elements within the nodes are destroyed.
1932  void
1933  _M_deallocate_nodes(__node_ptr __n);
1934 
1935  __buckets_ptr
1936  _M_allocate_buckets(std::size_t __bkt_count);
1937 
1938  void
1939  _M_deallocate_buckets(__buckets_ptr, std::size_t __bkt_count);
1940  };
1941 
1942  // Definitions of class template _Hashtable_alloc's out-of-line member
1943  // functions.
1944  template<typename _NodeAlloc>
1945  template<typename... _Args>
1946  auto
1947  _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
1948  -> __node_ptr
1949  {
1950  auto __nptr = __node_alloc_traits::allocate(_M_node_allocator(), 1);
1951  __node_ptr __n = std::__to_address(__nptr);
1952  __try
1953  {
1954  ::new ((void*)__n) __node_type;
1955  __node_alloc_traits::construct(_M_node_allocator(),
1956  __n->_M_valptr(),
1957  std::forward<_Args>(__args)...);
1958  return __n;
1959  }
1960  __catch(...)
1961  {
1962  __node_alloc_traits::deallocate(_M_node_allocator(), __nptr, 1);
1963  __throw_exception_again;
1964  }
1965  }
1966 
1967  template<typename _NodeAlloc>
1968  void
1969  _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
1970  {
1971  __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
1972  _M_deallocate_node_ptr(__n);
1973  }
1974 
1975  template<typename _NodeAlloc>
1976  void
1977  _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
1978  {
1979  typedef typename __node_alloc_traits::pointer _Ptr;
1980  auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
1981  __n->~__node_type();
1982  __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
1983  }
1984 
1985  template<typename _NodeAlloc>
1986  void
1987  _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
1988  {
1989  while (__n)
1990  {
1991  __node_ptr __tmp = __n;
1992  __n = __n->_M_next();
1993  _M_deallocate_node(__tmp);
1994  }
1995  }
1996 
1997  template<typename _NodeAlloc>
1998  auto
1999  _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __bkt_count)
2000  -> __buckets_ptr
2001  {
2002  __buckets_alloc_type __alloc(_M_node_allocator());
2003 
2004  auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count);
2005  __buckets_ptr __p = std::__to_address(__ptr);
2006  __builtin_memset(__p, 0, __bkt_count * sizeof(__node_base_ptr));
2007  return __p;
2008  }
2009 
2010  template<typename _NodeAlloc>
2011  void
2012  _Hashtable_alloc<_NodeAlloc>::
2013  _M_deallocate_buckets(__buckets_ptr __bkts,
2014  std::size_t __bkt_count)
2015  {
2016  typedef typename __buckets_alloc_traits::pointer _Ptr;
2017  auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
2018  __buckets_alloc_type __alloc(_M_node_allocator());
2019  __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count);
2020  }
2021 
2022  ///@} hashtable-detail
2023 } // namespace __detail
2024 /// @endcond
2025 _GLIBCXX_END_NAMESPACE_VERSION
2026 } // namespace std
2027 
2028 #endif // _HASHTABLE_POLICY_H
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition: complex:392
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:82
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:85
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition: stl_pair.h:83
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
std::forward_as_tuple
Definition: tuple:1589
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition: move.h:77
void swap(any &__x, any &__y) noexcept
Exchange the states of two any objects.
Definition: any:429
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
constexpr _Iterator __base(_Iterator __it)
Primary class template, tuple.
Definition: tuple:610
is_empty
Definition: type_traits:782
is_constructible
Definition: type_traits:979
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:2229
Uniform interface to all allocator types.
Traits class for iterators.
Uniform interface to all pointer-like types.
Definition: ptr_traits.h:192
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:201
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Uniform interface to C++98 and C++11 allocators.