libstdc++
bits/hashtable.h
Go to the documentation of this file.
1 // hashtable.h header -*- C++ -*-
2 
3 // Copyright (C) 2007-2021 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.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{unordered_map, unordered_set}
28  */
29 
30 #ifndef _HASHTABLE_H
31 #define _HASHTABLE_H 1
32 
33 #pragma GCC system_header
34 
35 #include <bits/hashtable_policy.h>
36 #if __cplusplus > 201402L
37 # include <bits/node_handle.h>
38 #endif
39 
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44  template<typename _Tp, typename _Hash>
45  using __cache_default
46  = __not_<__and_<// Do not cache for fast hasher.
47  __is_fast_hash<_Hash>,
48  // Mandatory to have erase not throwing.
49  __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
50 
51  /**
52  * Primary class template _Hashtable.
53  *
54  * @ingroup hashtable-detail
55  *
56  * @tparam _Value CopyConstructible type.
57  *
58  * @tparam _Key CopyConstructible type.
59  *
60  * @tparam _Alloc An allocator type
61  * ([lib.allocator.requirements]) whose _Alloc::value_type is
62  * _Value. As a conforming extension, we allow for
63  * _Alloc::value_type != _Value.
64  *
65  * @tparam _ExtractKey Function object that takes an object of type
66  * _Value and returns a value of type _Key.
67  *
68  * @tparam _Equal Function object that takes two objects of type k
69  * and returns a bool-like value that is true if the two objects
70  * are considered equal.
71  *
72  * @tparam _Hash The hash function. A unary function object with
73  * argument type _Key and result type size_t. Return values should
74  * be distributed over the entire range [0, numeric_limits<size_t>:::max()].
75  *
76  * @tparam _RangeHash The range-hashing function (in the terminology of
77  * Tavori and Dreizin). A binary function object whose argument
78  * types and result type are all size_t. Given arguments r and N,
79  * the return value is in the range [0, N).
80  *
81  * @tparam _Unused Not used.
82  *
83  * @tparam _RehashPolicy Policy class with three members, all of
84  * which govern the bucket count. _M_next_bkt(n) returns a bucket
85  * count no smaller than n. _M_bkt_for_elements(n) returns a
86  * bucket count appropriate for an element count of n.
87  * _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
88  * current bucket count is n_bkt and the current element count is
89  * n_elt, we need to increase the bucket count for n_ins insertions.
90  * If so, returns make_pair(true, n), where n is the new bucket count. If
91  * not, returns make_pair(false, <anything>)
92  *
93  * @tparam _Traits Compile-time class with three boolean
94  * std::integral_constant members: __cache_hash_code, __constant_iterators,
95  * __unique_keys.
96  *
97  * Each _Hashtable data structure has:
98  *
99  * - _Bucket[] _M_buckets
100  * - _Hash_node_base _M_before_begin
101  * - size_type _M_bucket_count
102  * - size_type _M_element_count
103  *
104  * with _Bucket being _Hash_node_base* and _Hash_node containing:
105  *
106  * - _Hash_node* _M_next
107  * - Tp _M_value
108  * - size_t _M_hash_code if cache_hash_code is true
109  *
110  * In terms of Standard containers the hashtable is like the aggregation of:
111  *
112  * - std::forward_list<_Node> containing the elements
113  * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
114  *
115  * The non-empty buckets contain the node before the first node in the
116  * bucket. This design makes it possible to implement something like a
117  * std::forward_list::insert_after on container insertion and
118  * std::forward_list::erase_after on container erase
119  * calls. _M_before_begin is equivalent to
120  * std::forward_list::before_begin. Empty buckets contain
121  * nullptr. Note that one of the non-empty buckets contains
122  * &_M_before_begin which is not a dereferenceable node so the
123  * node pointer in a bucket shall never be dereferenced, only its
124  * next node can be.
125  *
126  * Walking through a bucket's nodes requires a check on the hash code to
127  * see if each node is still in the bucket. Such a design assumes a
128  * quite efficient hash functor and is one of the reasons it is
129  * highly advisable to set __cache_hash_code to true.
130  *
131  * The container iterators are simply built from nodes. This way
132  * incrementing the iterator is perfectly efficient independent of
133  * how many empty buckets there are in the container.
134  *
135  * On insert we compute the element's hash code and use it to find the
136  * bucket index. If the element must be inserted in an empty bucket
137  * we add it at the beginning of the singly linked list and make the
138  * bucket point to _M_before_begin. The bucket that used to point to
139  * _M_before_begin, if any, is updated to point to its new before
140  * begin node.
141  *
142  * On erase, the simple iterator design requires using the hash
143  * functor to get the index of the bucket to update. For this
144  * reason, when __cache_hash_code is set to false the hash functor must
145  * not throw and this is enforced by a static assertion.
146  *
147  * Functionality is implemented by decomposition into base classes,
148  * where the derived _Hashtable class is used in _Map_base,
149  * _Insert, _Rehash_base, and _Equality base classes to access the
150  * "this" pointer. _Hashtable_base is used in the base classes as a
151  * non-recursive, fully-completed-type so that detailed nested type
152  * information, such as iterator type and node type, can be
153  * used. This is similar to the "Curiously Recurring Template
154  * Pattern" (CRTP) technique, but uses a reconstructed, not
155  * explicitly passed, template pattern.
156  *
157  * Base class templates are:
158  * - __detail::_Hashtable_base
159  * - __detail::_Map_base
160  * - __detail::_Insert
161  * - __detail::_Rehash_base
162  * - __detail::_Equality
163  */
164  template<typename _Key, typename _Value, typename _Alloc,
165  typename _ExtractKey, typename _Equal,
166  typename _Hash, typename _RangeHash, typename _Unused,
167  typename _RehashPolicy, typename _Traits>
169  : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
170  _Hash, _RangeHash, _Unused, _Traits>,
171  public __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
172  _Hash, _RangeHash, _Unused,
173  _RehashPolicy, _Traits>,
174  public __detail::_Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
175  _Hash, _RangeHash, _Unused,
176  _RehashPolicy, _Traits>,
177  public __detail::_Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
178  _Hash, _RangeHash, _Unused,
179  _RehashPolicy, _Traits>,
180  public __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
181  _Hash, _RangeHash, _Unused,
182  _RehashPolicy, _Traits>,
184  __alloc_rebind<_Alloc,
185  __detail::_Hash_node<_Value,
186  _Traits::__hash_cached::value>>>
187  {
188  static_assert(is_same<typename remove_cv<_Value>::type, _Value>::value,
189  "unordered container must have a non-const, non-volatile value_type");
190 #if __cplusplus > 201703L || defined __STRICT_ANSI__
192  "unordered container must have the same value_type as its allocator");
193 #endif
194 
195  using __traits_type = _Traits;
196  using __hash_cached = typename __traits_type::__hash_cached;
197  using __constant_iterators = typename __traits_type::__constant_iterators;
199  using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
200 
202 
203  using __node_value_type =
204  __detail::_Hash_node_value<_Value, __hash_cached::value>;
205  using __node_ptr = typename __hashtable_alloc::__node_ptr;
206  using __value_alloc_traits =
207  typename __hashtable_alloc::__value_alloc_traits;
208  using __node_alloc_traits =
210  using __node_base = typename __hashtable_alloc::__node_base;
211  using __node_base_ptr = typename __hashtable_alloc::__node_base_ptr;
212  using __buckets_ptr = typename __hashtable_alloc::__buckets_ptr;
213 
214  using __insert_base = __detail::_Insert<_Key, _Value, _Alloc, _ExtractKey,
215  _Equal, _Hash,
216  _RangeHash, _Unused,
217  _RehashPolicy, _Traits>;
218 
219  public:
220  typedef _Key key_type;
221  typedef _Value value_type;
222  typedef _Alloc allocator_type;
223  typedef _Equal key_equal;
224 
225  // mapped_type, if present, comes from _Map_base.
226  // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
227  typedef typename __value_alloc_traits::pointer pointer;
228  typedef typename __value_alloc_traits::const_pointer const_pointer;
229  typedef value_type& reference;
230  typedef const value_type& const_reference;
231 
232  using iterator = typename __insert_base::iterator;
233 
234  using const_iterator = typename __insert_base::const_iterator;
235 
236  using local_iterator = __detail::_Local_iterator<key_type, _Value,
237  _ExtractKey, _Hash, _RangeHash, _Unused,
238  __constant_iterators::value,
239  __hash_cached::value>;
240 
242  key_type, _Value,
243  _ExtractKey, _Hash, _RangeHash, _Unused,
244  __constant_iterators::value, __hash_cached::value>;
245 
246  private:
247  using __rehash_type = _RehashPolicy;
248  using __rehash_state = typename __rehash_type::_State;
249 
250  using __unique_keys = typename __traits_type::__unique_keys;
251 
252  using __hashtable_base = __detail::
253  _Hashtable_base<_Key, _Value, _ExtractKey,
254  _Equal, _Hash, _RangeHash, _Unused, _Traits>;
255 
256  using __hash_code_base = typename __hashtable_base::__hash_code_base;
257  using __hash_code = typename __hashtable_base::__hash_code;
258  using __ireturn_type = typename __insert_base::__ireturn_type;
259 
260  using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
261  _Equal, _Hash, _RangeHash, _Unused,
262  _RehashPolicy, _Traits>;
263 
264  using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
265  _ExtractKey, _Equal,
266  _Hash, _RangeHash, _Unused,
267  _RehashPolicy, _Traits>;
268 
269  using __eq_base = __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey,
270  _Equal, _Hash, _RangeHash, _Unused,
271  _RehashPolicy, _Traits>;
272 
273  using __reuse_or_alloc_node_gen_t =
274  __detail::_ReuseOrAllocNode<__node_alloc_type>;
275  using __alloc_node_gen_t =
276  __detail::_AllocNode<__node_alloc_type>;
277 
278  // Simple RAII type for managing a node containing an element
279  struct _Scoped_node
280  {
281  // Take ownership of a node with a constructed element.
282  _Scoped_node(__node_ptr __n, __hashtable_alloc* __h)
283  : _M_h(__h), _M_node(__n) { }
284 
285  // Allocate a node and construct an element within it.
286  template<typename... _Args>
287  _Scoped_node(__hashtable_alloc* __h, _Args&&... __args)
288  : _M_h(__h),
289  _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
290  { }
291 
292  // Destroy element and deallocate node.
293  ~_Scoped_node() { if (_M_node) _M_h->_M_deallocate_node(_M_node); };
294 
295  _Scoped_node(const _Scoped_node&) = delete;
296  _Scoped_node& operator=(const _Scoped_node&) = delete;
297 
298  __hashtable_alloc* _M_h;
299  __node_ptr _M_node;
300  };
301 
302  template<typename _Ht>
303  static constexpr
305  const value_type&, value_type&&>::type
306  __fwd_value_for(value_type& __val) noexcept
307  { return std::move(__val); }
308 
309  // Compile-time diagnostics.
310 
311  // _Hash_code_base has everything protected, so use this derived type to
312  // access it.
313  struct __hash_code_base_access : __hash_code_base
314  { using __hash_code_base::_M_bucket_index; };
315 
316  // Getting a bucket index from a node shall not throw because it is used
317  // in methods (erase, swap...) that shall not throw.
318  static_assert(noexcept(declval<const __hash_code_base_access&>()
319  ._M_bucket_index(declval<const __node_value_type&>(),
320  (std::size_t)0)),
321  "Cache the hash code or qualify your functors involved"
322  " in hash code and bucket index computation with noexcept");
323 
324  // To get bucket index we need _RangeHash not to throw.
326  "Functor used to map hash code to bucket index"
327  " must be nothrow default constructible");
328  static_assert(noexcept(
329  std::declval<const _RangeHash&>()((std::size_t)0, (std::size_t)0)),
330  "Functor used to map hash code to bucket index must be"
331  " noexcept");
332 
333  // To compute bucket index we also need _ExtratKey not to throw.
335  "_ExtractKey must be nothrow default constructible");
336  static_assert(noexcept(
337  std::declval<const _ExtractKey&>()(std::declval<_Value>())),
338  "_ExtractKey functor must be noexcept invocable");
339 
340  template<typename _Keya, typename _Valuea, typename _Alloca,
341  typename _ExtractKeya, typename _Equala,
342  typename _Hasha, typename _RangeHasha, typename _Unuseda,
343  typename _RehashPolicya, typename _Traitsa,
344  bool _Unique_keysa>
345  friend struct __detail::_Map_base;
346 
347  template<typename _Keya, typename _Valuea, typename _Alloca,
348  typename _ExtractKeya, typename _Equala,
349  typename _Hasha, typename _RangeHasha, typename _Unuseda,
350  typename _RehashPolicya, typename _Traitsa>
351  friend struct __detail::_Insert_base;
352 
353  template<typename _Keya, typename _Valuea, typename _Alloca,
354  typename _ExtractKeya, typename _Equala,
355  typename _Hasha, typename _RangeHasha, typename _Unuseda,
356  typename _RehashPolicya, typename _Traitsa,
357  bool _Constant_iteratorsa>
358  friend struct __detail::_Insert;
359 
360  template<typename _Keya, typename _Valuea, typename _Alloca,
361  typename _ExtractKeya, typename _Equala,
362  typename _Hasha, typename _RangeHasha, typename _Unuseda,
363  typename _RehashPolicya, typename _Traitsa,
364  bool _Unique_keysa>
365  friend struct __detail::_Equality;
366 
367  public:
368  using size_type = typename __hashtable_base::size_type;
369  using difference_type = typename __hashtable_base::difference_type;
370 
371 #if __cplusplus > 201402L
374 #endif
375 
376  private:
377  __buckets_ptr _M_buckets = &_M_single_bucket;
378  size_type _M_bucket_count = 1;
379  __node_base _M_before_begin;
380  size_type _M_element_count = 0;
381  _RehashPolicy _M_rehash_policy;
382 
383  // A single bucket used when only need for 1 bucket. Especially
384  // interesting in move semantic to leave hashtable with only 1 bucket
385  // which is not allocated so that we can have those operations noexcept
386  // qualified.
387  // Note that we can't leave hashtable with 0 bucket without adding
388  // numerous checks in the code to avoid 0 modulus.
389  __node_base_ptr _M_single_bucket = nullptr;
390 
391  void
392  _M_update_bbegin()
393  {
394  if (_M_begin())
395  _M_buckets[_M_bucket_index(*_M_begin())] = &_M_before_begin;
396  }
397 
398  void
399  _M_update_bbegin(__node_ptr __n)
400  {
401  _M_before_begin._M_nxt = __n;
402  _M_update_bbegin();
403  }
404 
405  bool
406  _M_uses_single_bucket(__buckets_ptr __bkts) const
407  { return __builtin_expect(__bkts == &_M_single_bucket, false); }
408 
409  bool
410  _M_uses_single_bucket() const
411  { return _M_uses_single_bucket(_M_buckets); }
412 
414  _M_base_alloc() { return *this; }
415 
416  __buckets_ptr
417  _M_allocate_buckets(size_type __bkt_count)
418  {
419  if (__builtin_expect(__bkt_count == 1, false))
420  {
421  _M_single_bucket = nullptr;
422  return &_M_single_bucket;
423  }
424 
425  return __hashtable_alloc::_M_allocate_buckets(__bkt_count);
426  }
427 
428  void
429  _M_deallocate_buckets(__buckets_ptr __bkts, size_type __bkt_count)
430  {
431  if (_M_uses_single_bucket(__bkts))
432  return;
433 
434  __hashtable_alloc::_M_deallocate_buckets(__bkts, __bkt_count);
435  }
436 
437  void
438  _M_deallocate_buckets()
439  { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
440 
441  // Gets bucket begin, deals with the fact that non-empty buckets contain
442  // their before begin node.
443  __node_ptr
444  _M_bucket_begin(size_type __bkt) const;
445 
446  __node_ptr
447  _M_begin() const
448  { return static_cast<__node_ptr>(_M_before_begin._M_nxt); }
449 
450  // Assign *this using another _Hashtable instance. Whether elements
451  // are copied or moved depends on the _Ht reference.
452  template<typename _Ht>
453  void
454  _M_assign_elements(_Ht&&);
455 
456  template<typename _Ht, typename _NodeGenerator>
457  void
458  _M_assign(_Ht&&, const _NodeGenerator&);
459 
460  void
461  _M_move_assign(_Hashtable&&, true_type);
462 
463  void
464  _M_move_assign(_Hashtable&&, false_type);
465 
466  void
467  _M_reset() noexcept;
468 
469  _Hashtable(const _Hash& __h, const _Equal& __eq,
470  const allocator_type& __a)
471  : __hashtable_base(__h, __eq),
472  __hashtable_alloc(__node_alloc_type(__a))
473  { }
474 
475  template<bool _No_realloc = true>
476  static constexpr bool
477  _S_nothrow_move()
478  {
479 #if __cplusplus <= 201402L
480  return __and_<__bool_constant<_No_realloc>,
483 #else
484  if constexpr (_No_realloc)
485  if constexpr (is_nothrow_copy_constructible<_Hash>())
487  return false;
488 #endif
489  }
490 
491  _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
492  true_type /* alloc always equal */)
493  noexcept(_S_nothrow_move());
494 
495  _Hashtable(_Hashtable&&, __node_alloc_type&&,
496  false_type /* alloc always equal */);
497 
498  template<typename _InputIterator>
499  _Hashtable(_InputIterator __first, _InputIterator __last,
500  size_type __bkt_count_hint,
501  const _Hash&, const _Equal&, const allocator_type&,
502  true_type __uks);
503 
504  template<typename _InputIterator>
505  _Hashtable(_InputIterator __first, _InputIterator __last,
506  size_type __bkt_count_hint,
507  const _Hash&, const _Equal&, const allocator_type&,
508  false_type __uks);
509 
510  public:
511  // Constructor, destructor, assignment, swap
512  _Hashtable() = default;
513 
514  _Hashtable(const _Hashtable&);
515 
516  _Hashtable(const _Hashtable&, const allocator_type&);
517 
518  explicit
519  _Hashtable(size_type __bkt_count_hint,
520  const _Hash& __hf = _Hash(),
521  const key_equal& __eql = key_equal(),
522  const allocator_type& __a = allocator_type());
523 
524  // Use delegating constructors.
525  _Hashtable(_Hashtable&& __ht)
526  noexcept(_S_nothrow_move())
527  : _Hashtable(std::move(__ht), std::move(__ht._M_node_allocator()),
528  true_type{})
529  { }
530 
531  _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
532  noexcept(_S_nothrow_move<__node_alloc_traits::_S_always_equal()>())
533  : _Hashtable(std::move(__ht), __node_alloc_type(__a),
534  typename __node_alloc_traits::is_always_equal{})
535  { }
536 
537  explicit
538  _Hashtable(const allocator_type& __a)
539  : __hashtable_alloc(__node_alloc_type(__a))
540  { }
541 
542  template<typename _InputIterator>
543  _Hashtable(_InputIterator __f, _InputIterator __l,
544  size_type __bkt_count_hint = 0,
545  const _Hash& __hf = _Hash(),
546  const key_equal& __eql = key_equal(),
547  const allocator_type& __a = allocator_type())
548  : _Hashtable(__f, __l, __bkt_count_hint, __hf, __eql, __a,
549  __unique_keys{})
550  { }
551 
553  size_type __bkt_count_hint = 0,
554  const _Hash& __hf = _Hash(),
555  const key_equal& __eql = key_equal(),
556  const allocator_type& __a = allocator_type())
557  : _Hashtable(__l.begin(), __l.end(), __bkt_count_hint,
558  __hf, __eql, __a, __unique_keys{})
559  { }
560 
561  _Hashtable&
562  operator=(const _Hashtable& __ht);
563 
564  _Hashtable&
565  operator=(_Hashtable&& __ht)
566  noexcept(__node_alloc_traits::_S_nothrow_move()
569  {
570  constexpr bool __move_storage =
571  __node_alloc_traits::_S_propagate_on_move_assign()
572  || __node_alloc_traits::_S_always_equal();
573  _M_move_assign(std::move(__ht), __bool_constant<__move_storage>());
574  return *this;
575  }
576 
577  _Hashtable&
579  {
580  __reuse_or_alloc_node_gen_t __roan(_M_begin(), *this);
581  _M_before_begin._M_nxt = nullptr;
582  clear();
583 
584  // We consider that all elements of __l are going to be inserted.
585  auto __l_bkt_count = _M_rehash_policy._M_bkt_for_elements(__l.size());
586 
587  // Do not shrink to keep potential user reservation.
588  if (_M_bucket_count < __l_bkt_count)
589  rehash(__l_bkt_count);
590 
591  this->_M_insert_range(__l.begin(), __l.end(), __roan, __unique_keys{});
592  return *this;
593  }
594 
595  ~_Hashtable() noexcept;
596 
597  void
598  swap(_Hashtable&)
599  noexcept(__and_<__is_nothrow_swappable<_Hash>,
600  __is_nothrow_swappable<_Equal>>::value);
601 
602  // Basic container operations
603  iterator
604  begin() noexcept
605  { return iterator(_M_begin()); }
606 
607  const_iterator
608  begin() const noexcept
609  { return const_iterator(_M_begin()); }
610 
611  iterator
612  end() noexcept
613  { return iterator(nullptr); }
614 
615  const_iterator
616  end() const noexcept
617  { return const_iterator(nullptr); }
618 
619  const_iterator
620  cbegin() const noexcept
621  { return const_iterator(_M_begin()); }
622 
623  const_iterator
624  cend() const noexcept
625  { return const_iterator(nullptr); }
626 
627  size_type
628  size() const noexcept
629  { return _M_element_count; }
630 
631  _GLIBCXX_NODISCARD bool
632  empty() const noexcept
633  { return size() == 0; }
634 
635  allocator_type
636  get_allocator() const noexcept
637  { return allocator_type(this->_M_node_allocator()); }
638 
639  size_type
640  max_size() const noexcept
641  { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
642 
643  // Observers
644  key_equal
645  key_eq() const
646  { return this->_M_eq(); }
647 
648  // hash_function, if present, comes from _Hash_code_base.
649 
650  // Bucket operations
651  size_type
652  bucket_count() const noexcept
653  { return _M_bucket_count; }
654 
655  size_type
656  max_bucket_count() const noexcept
657  { return max_size(); }
658 
659  size_type
660  bucket_size(size_type __bkt) const
661  { return std::distance(begin(__bkt), end(__bkt)); }
662 
663  size_type
664  bucket(const key_type& __k) const
665  { return _M_bucket_index(this->_M_hash_code(__k)); }
666 
668  begin(size_type __bkt)
669  {
670  return local_iterator(*this, _M_bucket_begin(__bkt),
671  __bkt, _M_bucket_count);
672  }
673 
675  end(size_type __bkt)
676  { return local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
677 
679  begin(size_type __bkt) const
680  {
681  return const_local_iterator(*this, _M_bucket_begin(__bkt),
682  __bkt, _M_bucket_count);
683  }
684 
686  end(size_type __bkt) const
687  { return const_local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
688 
689  // DR 691.
691  cbegin(size_type __bkt) const
692  {
693  return const_local_iterator(*this, _M_bucket_begin(__bkt),
694  __bkt, _M_bucket_count);
695  }
696 
698  cend(size_type __bkt) const
699  { return const_local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
700 
701  float
702  load_factor() const noexcept
703  {
704  return static_cast<float>(size()) / static_cast<float>(bucket_count());
705  }
706 
707  // max_load_factor, if present, comes from _Rehash_base.
708 
709  // Generalization of max_load_factor. Extension, not found in
710  // TR1. Only useful if _RehashPolicy is something other than
711  // the default.
712  const _RehashPolicy&
713  __rehash_policy() const
714  { return _M_rehash_policy; }
715 
716  void
717  __rehash_policy(const _RehashPolicy& __pol)
718  { _M_rehash_policy = __pol; }
719 
720  // Lookup.
721  iterator
722  find(const key_type& __k);
723 
724  const_iterator
725  find(const key_type& __k) const;
726 
727  size_type
728  count(const key_type& __k) const;
729 
731  equal_range(const key_type& __k);
732 
734  equal_range(const key_type& __k) const;
735 
736 #if __cplusplus > 201702L
737  template<typename _Kt,
738  typename = __has_is_transparent_t<_Hash, _Kt>,
739  typename = __has_is_transparent_t<_Equal, _Kt>>
740  iterator
741  _M_find_tr(const _Kt& __k);
742 
743  template<typename _Kt,
744  typename = __has_is_transparent_t<_Hash, _Kt>,
745  typename = __has_is_transparent_t<_Equal, _Kt>>
746  const_iterator
747  _M_find_tr(const _Kt& __k) const;
748 
749  template<typename _Kt,
750  typename = __has_is_transparent_t<_Hash, _Kt>,
751  typename = __has_is_transparent_t<_Equal, _Kt>>
752  size_type
753  _M_count_tr(const _Kt& __k) const;
754 
755  template<typename _Kt,
756  typename = __has_is_transparent_t<_Hash, _Kt>,
757  typename = __has_is_transparent_t<_Equal, _Kt>>
759  _M_equal_range_tr(const _Kt& __k);
760 
761  template<typename _Kt,
762  typename = __has_is_transparent_t<_Hash, _Kt>,
763  typename = __has_is_transparent_t<_Equal, _Kt>>
765  _M_equal_range_tr(const _Kt& __k) const;
766 #endif
767 
768  private:
769  // Bucket index computation helpers.
770  size_type
771  _M_bucket_index(const __node_value_type& __n) const noexcept
772  { return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
773 
774  size_type
775  _M_bucket_index(__hash_code __c) const
776  { return __hash_code_base::_M_bucket_index(__c, _M_bucket_count); }
777 
778  // Find and insert helper functions and types
779  // Find the node before the one matching the criteria.
780  __node_base_ptr
781  _M_find_before_node(size_type, const key_type&, __hash_code) const;
782 
783  template<typename _Kt>
784  __node_base_ptr
785  _M_find_before_node_tr(size_type, const _Kt&, __hash_code) const;
786 
787  __node_ptr
788  _M_find_node(size_type __bkt, const key_type& __key,
789  __hash_code __c) const
790  {
791  __node_base_ptr __before_n = _M_find_before_node(__bkt, __key, __c);
792  if (__before_n)
793  return static_cast<__node_ptr>(__before_n->_M_nxt);
794  return nullptr;
795  }
796 
797  template<typename _Kt>
798  __node_ptr
799  _M_find_node_tr(size_type __bkt, const _Kt& __key,
800  __hash_code __c) const
801  {
802  auto __before_n = _M_find_before_node_tr(__bkt, __key, __c);
803  if (__before_n)
804  return static_cast<__node_ptr>(__before_n->_M_nxt);
805  return nullptr;
806  }
807 
808  // Insert a node at the beginning of a bucket.
809  void
810  _M_insert_bucket_begin(size_type, __node_ptr);
811 
812  // Remove the bucket first node
813  void
814  _M_remove_bucket_begin(size_type __bkt, __node_ptr __next_n,
815  size_type __next_bkt);
816 
817  // Get the node before __n in the bucket __bkt
818  __node_base_ptr
819  _M_get_previous_node(size_type __bkt, __node_ptr __n);
820 
821  // Insert node __n with hash code __code, in bucket __bkt if no
822  // rehash (assumes no element with same key already present).
823  // Takes ownership of __n if insertion succeeds, throws otherwise.
824  iterator
825  _M_insert_unique_node(size_type __bkt, __hash_code,
826  __node_ptr __n, size_type __n_elt = 1);
827 
828  // Insert node __n with key __k and hash code __code.
829  // Takes ownership of __n if insertion succeeds, throws otherwise.
830  iterator
831  _M_insert_multi_node(__node_ptr __hint,
832  __hash_code __code, __node_ptr __n);
833 
834  template<typename... _Args>
836  _M_emplace(true_type __uks, _Args&&... __args);
837 
838  template<typename... _Args>
839  iterator
840  _M_emplace(false_type __uks, _Args&&... __args)
841  { return _M_emplace(cend(), __uks, std::forward<_Args>(__args)...); }
842 
843  // Emplace with hint, useless when keys are unique.
844  template<typename... _Args>
845  iterator
846  _M_emplace(const_iterator, true_type __uks, _Args&&... __args)
847  { return _M_emplace(__uks, std::forward<_Args>(__args)...).first; }
848 
849  template<typename... _Args>
850  iterator
851  _M_emplace(const_iterator, false_type __uks, _Args&&... __args);
852 
853  template<typename _Arg, typename _NodeGenerator>
855  _M_insert(_Arg&&, const _NodeGenerator&, true_type __uks);
856 
857  template<typename _Arg, typename _NodeGenerator>
858  iterator
859  _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
860  false_type __uks)
861  {
862  return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
863  __uks);
864  }
865 
866  // Insert with hint, not used when keys are unique.
867  template<typename _Arg, typename _NodeGenerator>
868  iterator
869  _M_insert(const_iterator, _Arg&& __arg,
870  const _NodeGenerator& __node_gen, true_type __uks)
871  {
872  return
873  _M_insert(std::forward<_Arg>(__arg), __node_gen, __uks).first;
874  }
875 
876  // Insert with hint when keys are not unique.
877  template<typename _Arg, typename _NodeGenerator>
878  iterator
879  _M_insert(const_iterator, _Arg&&,
880  const _NodeGenerator&, false_type __uks);
881 
882  size_type
883  _M_erase(true_type __uks, const key_type&);
884 
885  size_type
886  _M_erase(false_type __uks, const key_type&);
887 
888  iterator
889  _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n);
890 
891  public:
892  // Emplace
893  template<typename... _Args>
894  __ireturn_type
895  emplace(_Args&&... __args)
896  { return _M_emplace(__unique_keys{}, std::forward<_Args>(__args)...); }
897 
898  template<typename... _Args>
899  iterator
900  emplace_hint(const_iterator __hint, _Args&&... __args)
901  {
902  return _M_emplace(__hint, __unique_keys{},
903  std::forward<_Args>(__args)...);
904  }
905 
906  // Insert member functions via inheritance.
907 
908  // Erase
909  iterator
910  erase(const_iterator);
911 
912  // LWG 2059.
913  iterator
914  erase(iterator __it)
915  { return erase(const_iterator(__it)); }
916 
917  size_type
918  erase(const key_type& __k)
919  { return _M_erase(__unique_keys{}, __k); }
920 
921  iterator
922  erase(const_iterator, const_iterator);
923 
924  void
925  clear() noexcept;
926 
927  // Set number of buckets keeping it appropriate for container's number
928  // of elements.
929  void rehash(size_type __bkt_count);
930 
931  // DR 1189.
932  // reserve, if present, comes from _Rehash_base.
933 
934 #if __cplusplus > 201402L
935  /// Re-insert an extracted node into a container with unique keys.
938  {
939  insert_return_type __ret;
940  if (__nh.empty())
941  __ret.position = end();
942  else
943  {
944  __glibcxx_assert(get_allocator() == __nh.get_allocator());
945 
946  const key_type& __k = __nh._M_key();
947  __hash_code __code = this->_M_hash_code(__k);
948  size_type __bkt = _M_bucket_index(__code);
949  if (__node_ptr __n = _M_find_node(__bkt, __k, __code))
950  {
951  __ret.node = std::move(__nh);
952  __ret.position = iterator(__n);
953  __ret.inserted = false;
954  }
955  else
956  {
957  __ret.position
958  = _M_insert_unique_node(__bkt, __code, __nh._M_ptr);
959  __nh._M_ptr = nullptr;
960  __ret.inserted = true;
961  }
962  }
963  return __ret;
964  }
965 
966  /// Re-insert an extracted node into a container with equivalent keys.
967  iterator
968  _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
969  {
970  if (__nh.empty())
971  return end();
972 
973  __glibcxx_assert(get_allocator() == __nh.get_allocator());
974 
975  const key_type& __k = __nh._M_key();
976  auto __code = this->_M_hash_code(__k);
977  auto __ret
978  = _M_insert_multi_node(__hint._M_cur, __code, __nh._M_ptr);
979  __nh._M_ptr = nullptr;
980  return __ret;
981  }
982 
983  private:
984  node_type
985  _M_extract_node(size_t __bkt, __node_base_ptr __prev_n)
986  {
987  __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
988  if (__prev_n == _M_buckets[__bkt])
989  _M_remove_bucket_begin(__bkt, __n->_M_next(),
990  __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
991  else if (__n->_M_nxt)
992  {
993  size_type __next_bkt = _M_bucket_index(*__n->_M_next());
994  if (__next_bkt != __bkt)
995  _M_buckets[__next_bkt] = __prev_n;
996  }
997 
998  __prev_n->_M_nxt = __n->_M_nxt;
999  __n->_M_nxt = nullptr;
1000  --_M_element_count;
1001  return { __n, this->_M_node_allocator() };
1002  }
1003 
1004  public:
1005  // Extract a node.
1006  node_type
1007  extract(const_iterator __pos)
1008  {
1009  size_t __bkt = _M_bucket_index(*__pos._M_cur);
1010  return _M_extract_node(__bkt,
1011  _M_get_previous_node(__bkt, __pos._M_cur));
1012  }
1013 
1014  /// Extract a node.
1015  node_type
1016  extract(const _Key& __k)
1017  {
1018  node_type __nh;
1019  __hash_code __code = this->_M_hash_code(__k);
1020  std::size_t __bkt = _M_bucket_index(__code);
1021  if (__node_base_ptr __prev_node = _M_find_before_node(__bkt, __k, __code))
1022  __nh = _M_extract_node(__bkt, __prev_node);
1023  return __nh;
1024  }
1025 
1026  /// Merge from a compatible container into one with unique keys.
1027  template<typename _Compatible_Hashtable>
1028  void
1029  _M_merge_unique(_Compatible_Hashtable& __src) noexcept
1030  {
1031  static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
1032  node_type>, "Node types are compatible");
1033  __glibcxx_assert(get_allocator() == __src.get_allocator());
1034 
1035  auto __n_elt = __src.size();
1036  for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1037  {
1038  auto __pos = __i++;
1039  const key_type& __k = _ExtractKey{}(*__pos);
1040  __hash_code __code = this->_M_hash_code(__k);
1041  size_type __bkt = _M_bucket_index(__code);
1042  if (_M_find_node(__bkt, __k, __code) == nullptr)
1043  {
1044  auto __nh = __src.extract(__pos);
1045  _M_insert_unique_node(__bkt, __code, __nh._M_ptr, __n_elt);
1046  __nh._M_ptr = nullptr;
1047  __n_elt = 1;
1048  }
1049  else if (__n_elt != 1)
1050  --__n_elt;
1051  }
1052  }
1053 
1054  /// Merge from a compatible container into one with equivalent keys.
1055  template<typename _Compatible_Hashtable>
1056  void
1057  _M_merge_multi(_Compatible_Hashtable& __src) noexcept
1058  {
1059  static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
1060  node_type>, "Node types are compatible");
1061  __glibcxx_assert(get_allocator() == __src.get_allocator());
1062 
1063  this->reserve(size() + __src.size());
1064  for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1065  _M_reinsert_node_multi(cend(), __src.extract(__i++));
1066  }
1067 #endif // C++17
1068 
1069  private:
1070  // Helper rehash method used when keys are unique.
1071  void _M_rehash_aux(size_type __bkt_count, true_type __uks);
1072 
1073  // Helper rehash method used when keys can be non-unique.
1074  void _M_rehash_aux(size_type __bkt_count, false_type __uks);
1075 
1076  // Unconditionally change size of bucket array to n, restore
1077  // hash policy state to __state on exception.
1078  void _M_rehash(size_type __bkt_count, const __rehash_state& __state);
1079  };
1080 
1081 
1082  // Definitions of class template _Hashtable's out-of-line member functions.
1083  template<typename _Key, typename _Value, typename _Alloc,
1084  typename _ExtractKey, typename _Equal,
1085  typename _Hash, typename _RangeHash, typename _Unused,
1086  typename _RehashPolicy, typename _Traits>
1087  auto
1088  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1089  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1090  _M_bucket_begin(size_type __bkt) const
1091  -> __node_ptr
1092  {
1093  __node_base_ptr __n = _M_buckets[__bkt];
1094  return __n ? static_cast<__node_ptr>(__n->_M_nxt) : nullptr;
1095  }
1096 
1097  template<typename _Key, typename _Value, typename _Alloc,
1098  typename _ExtractKey, typename _Equal,
1099  typename _Hash, typename _RangeHash, typename _Unused,
1100  typename _RehashPolicy, typename _Traits>
1101  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1102  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1103  _Hashtable(size_type __bkt_count_hint,
1104  const _Hash& __h, const _Equal& __eq, const allocator_type& __a)
1105  : _Hashtable(__h, __eq, __a)
1106  {
1107  auto __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count_hint);
1108  if (__bkt_count > _M_bucket_count)
1109  {
1110  _M_buckets = _M_allocate_buckets(__bkt_count);
1111  _M_bucket_count = __bkt_count;
1112  }
1113  }
1114 
1115  template<typename _Key, typename _Value, typename _Alloc,
1116  typename _ExtractKey, typename _Equal,
1117  typename _Hash, typename _RangeHash, typename _Unused,
1118  typename _RehashPolicy, typename _Traits>
1119  template<typename _InputIterator>
1120  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1121  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1122  _Hashtable(_InputIterator __f, _InputIterator __l,
1123  size_type __bkt_count_hint,
1124  const _Hash& __h, const _Equal& __eq,
1125  const allocator_type& __a, true_type /* __uks */)
1126  : _Hashtable(__bkt_count_hint, __h, __eq, __a)
1127  {
1128  for (; __f != __l; ++__f)
1129  this->insert(*__f);
1130  }
1131 
1132  template<typename _Key, typename _Value, typename _Alloc,
1133  typename _ExtractKey, typename _Equal,
1134  typename _Hash, typename _RangeHash, typename _Unused,
1135  typename _RehashPolicy, typename _Traits>
1136  template<typename _InputIterator>
1137  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1138  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1139  _Hashtable(_InputIterator __f, _InputIterator __l,
1140  size_type __bkt_count_hint,
1141  const _Hash& __h, const _Equal& __eq,
1142  const allocator_type& __a, false_type /* __uks */)
1143  : _Hashtable(__h, __eq, __a)
1144  {
1145  auto __nb_elems = __detail::__distance_fw(__f, __l);
1146  auto __bkt_count =
1147  _M_rehash_policy._M_next_bkt(
1148  std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
1149  __bkt_count_hint));
1150 
1151  if (__bkt_count > _M_bucket_count)
1152  {
1153  _M_buckets = _M_allocate_buckets(__bkt_count);
1154  _M_bucket_count = __bkt_count;
1155  }
1156 
1157  for (; __f != __l; ++__f)
1158  this->insert(*__f);
1159  }
1160 
1161  template<typename _Key, typename _Value, typename _Alloc,
1162  typename _ExtractKey, typename _Equal,
1163  typename _Hash, typename _RangeHash, typename _Unused,
1164  typename _RehashPolicy, typename _Traits>
1165  auto
1166  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1167  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1168  operator=(const _Hashtable& __ht)
1169  -> _Hashtable&
1170  {
1171  if (&__ht == this)
1172  return *this;
1173 
1174  if (__node_alloc_traits::_S_propagate_on_copy_assign())
1175  {
1176  auto& __this_alloc = this->_M_node_allocator();
1177  auto& __that_alloc = __ht._M_node_allocator();
1178  if (!__node_alloc_traits::_S_always_equal()
1179  && __this_alloc != __that_alloc)
1180  {
1181  // Replacement allocator cannot free existing storage.
1182  this->_M_deallocate_nodes(_M_begin());
1183  _M_before_begin._M_nxt = nullptr;
1184  _M_deallocate_buckets();
1185  _M_buckets = nullptr;
1186  std::__alloc_on_copy(__this_alloc, __that_alloc);
1188  _M_bucket_count = __ht._M_bucket_count;
1189  _M_element_count = __ht._M_element_count;
1190  _M_rehash_policy = __ht._M_rehash_policy;
1191  __alloc_node_gen_t __alloc_node_gen(*this);
1192  __try
1193  {
1194  _M_assign(__ht, __alloc_node_gen);
1195  }
1196  __catch(...)
1197  {
1198  // _M_assign took care of deallocating all memory. Now we
1199  // must make sure this instance remains in a usable state.
1200  _M_reset();
1201  __throw_exception_again;
1202  }
1203  return *this;
1204  }
1205  std::__alloc_on_copy(__this_alloc, __that_alloc);
1206  }
1207 
1208  // Reuse allocated buckets and nodes.
1209  _M_assign_elements(__ht);
1210  return *this;
1211  }
1212 
1213  template<typename _Key, typename _Value, typename _Alloc,
1214  typename _ExtractKey, typename _Equal,
1215  typename _Hash, typename _RangeHash, typename _Unused,
1216  typename _RehashPolicy, typename _Traits>
1217  template<typename _Ht>
1218  void
1219  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1220  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1221  _M_assign_elements(_Ht&& __ht)
1222  {
1223  __buckets_ptr __former_buckets = nullptr;
1224  std::size_t __former_bucket_count = _M_bucket_count;
1225  const __rehash_state& __former_state = _M_rehash_policy._M_state();
1226 
1227  if (_M_bucket_count != __ht._M_bucket_count)
1228  {
1229  __former_buckets = _M_buckets;
1230  _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1231  _M_bucket_count = __ht._M_bucket_count;
1232  }
1233  else
1234  __builtin_memset(_M_buckets, 0,
1235  _M_bucket_count * sizeof(__node_base_ptr));
1236 
1237  __try
1238  {
1239  __hashtable_base::operator=(std::forward<_Ht>(__ht));
1240  _M_element_count = __ht._M_element_count;
1241  _M_rehash_policy = __ht._M_rehash_policy;
1242  __reuse_or_alloc_node_gen_t __roan(_M_begin(), *this);
1243  _M_before_begin._M_nxt = nullptr;
1244  _M_assign(std::forward<_Ht>(__ht), __roan);
1245  if (__former_buckets)
1246  _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1247  }
1248  __catch(...)
1249  {
1250  if (__former_buckets)
1251  {
1252  // Restore previous buckets.
1253  _M_deallocate_buckets();
1254  _M_rehash_policy._M_reset(__former_state);
1255  _M_buckets = __former_buckets;
1256  _M_bucket_count = __former_bucket_count;
1257  }
1258  __builtin_memset(_M_buckets, 0,
1259  _M_bucket_count * sizeof(__node_base_ptr));
1260  __throw_exception_again;
1261  }
1262  }
1263 
1264  template<typename _Key, typename _Value, typename _Alloc,
1265  typename _ExtractKey, typename _Equal,
1266  typename _Hash, typename _RangeHash, typename _Unused,
1267  typename _RehashPolicy, typename _Traits>
1268  template<typename _Ht, typename _NodeGenerator>
1269  void
1270  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1271  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1272  _M_assign(_Ht&& __ht, const _NodeGenerator& __node_gen)
1273  {
1274  __buckets_ptr __buckets = nullptr;
1275  if (!_M_buckets)
1276  _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
1277 
1278  __try
1279  {
1280  if (!__ht._M_before_begin._M_nxt)
1281  return;
1282 
1283  // First deal with the special first node pointed to by
1284  // _M_before_begin.
1285  __node_ptr __ht_n = __ht._M_begin();
1286  __node_ptr __this_n
1287  = __node_gen(__fwd_value_for<_Ht>(__ht_n->_M_v()));
1288  this->_M_copy_code(*__this_n, *__ht_n);
1289  _M_update_bbegin(__this_n);
1290 
1291  // Then deal with other nodes.
1292  __node_ptr __prev_n = __this_n;
1293  for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
1294  {
1295  __this_n = __node_gen(__fwd_value_for<_Ht>(__ht_n->_M_v()));
1296  __prev_n->_M_nxt = __this_n;
1297  this->_M_copy_code(*__this_n, *__ht_n);
1298  size_type __bkt = _M_bucket_index(*__this_n);
1299  if (!_M_buckets[__bkt])
1300  _M_buckets[__bkt] = __prev_n;
1301  __prev_n = __this_n;
1302  }
1303  }
1304  __catch(...)
1305  {
1306  clear();
1307  if (__buckets)
1308  _M_deallocate_buckets();
1309  __throw_exception_again;
1310  }
1311  }
1312 
1313  template<typename _Key, typename _Value, typename _Alloc,
1314  typename _ExtractKey, typename _Equal,
1315  typename _Hash, typename _RangeHash, typename _Unused,
1316  typename _RehashPolicy, typename _Traits>
1317  void
1318  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1319  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1320  _M_reset() noexcept
1321  {
1322  _M_rehash_policy._M_reset();
1323  _M_bucket_count = 1;
1324  _M_single_bucket = nullptr;
1325  _M_buckets = &_M_single_bucket;
1326  _M_before_begin._M_nxt = nullptr;
1327  _M_element_count = 0;
1328  }
1329 
1330  template<typename _Key, typename _Value, typename _Alloc,
1331  typename _ExtractKey, typename _Equal,
1332  typename _Hash, typename _RangeHash, typename _Unused,
1333  typename _RehashPolicy, typename _Traits>
1334  void
1335  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1336  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1337  _M_move_assign(_Hashtable&& __ht, true_type)
1338  {
1339  if (__builtin_expect(std::__addressof(__ht) == this, false))
1340  return;
1341 
1342  this->_M_deallocate_nodes(_M_begin());
1343  _M_deallocate_buckets();
1345  _M_rehash_policy = __ht._M_rehash_policy;
1346  if (!__ht._M_uses_single_bucket())
1347  _M_buckets = __ht._M_buckets;
1348  else
1349  {
1350  _M_buckets = &_M_single_bucket;
1351  _M_single_bucket = __ht._M_single_bucket;
1352  }
1353 
1354  _M_bucket_count = __ht._M_bucket_count;
1355  _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1356  _M_element_count = __ht._M_element_count;
1357  std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1358 
1359  // Fix bucket containing the _M_before_begin pointer that can't be moved.
1360  _M_update_bbegin();
1361  __ht._M_reset();
1362  }
1363 
1364  template<typename _Key, typename _Value, typename _Alloc,
1365  typename _ExtractKey, typename _Equal,
1366  typename _Hash, typename _RangeHash, typename _Unused,
1367  typename _RehashPolicy, typename _Traits>
1368  void
1369  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1370  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1371  _M_move_assign(_Hashtable&& __ht, false_type)
1372  {
1373  if (__ht._M_node_allocator() == this->_M_node_allocator())
1374  _M_move_assign(std::move(__ht), true_type{});
1375  else
1376  {
1377  // Can't move memory, move elements then.
1378  _M_assign_elements(std::move(__ht));
1379  __ht.clear();
1380  }
1381  }
1382 
1383  template<typename _Key, typename _Value, typename _Alloc,
1384  typename _ExtractKey, typename _Equal,
1385  typename _Hash, typename _RangeHash, typename _Unused,
1386  typename _RehashPolicy, typename _Traits>
1387  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1388  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1389  _Hashtable(const _Hashtable& __ht)
1390  : __hashtable_base(__ht),
1391  __map_base(__ht),
1392  __rehash_base(__ht),
1393  __hashtable_alloc(
1394  __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1395  _M_buckets(nullptr),
1396  _M_bucket_count(__ht._M_bucket_count),
1397  _M_element_count(__ht._M_element_count),
1398  _M_rehash_policy(__ht._M_rehash_policy)
1399  {
1400  __alloc_node_gen_t __alloc_node_gen(*this);
1401  _M_assign(__ht, __alloc_node_gen);
1402  }
1403 
1404  template<typename _Key, typename _Value, typename _Alloc,
1405  typename _ExtractKey, typename _Equal,
1406  typename _Hash, typename _RangeHash, typename _Unused,
1407  typename _RehashPolicy, typename _Traits>
1408  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1409  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1410  _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1411  true_type /* alloc always equal */)
1412  noexcept(_S_nothrow_move())
1413  : __hashtable_base(__ht),
1414  __map_base(__ht),
1415  __rehash_base(__ht),
1416  __hashtable_alloc(std::move(__a)),
1417  _M_buckets(__ht._M_buckets),
1418  _M_bucket_count(__ht._M_bucket_count),
1419  _M_before_begin(__ht._M_before_begin._M_nxt),
1420  _M_element_count(__ht._M_element_count),
1421  _M_rehash_policy(__ht._M_rehash_policy)
1422  {
1423  // Update buckets if __ht is using its single bucket.
1424  if (__ht._M_uses_single_bucket())
1425  {
1426  _M_buckets = &_M_single_bucket;
1427  _M_single_bucket = __ht._M_single_bucket;
1428  }
1429 
1430  // Fix bucket containing the _M_before_begin pointer that can't be moved.
1431  _M_update_bbegin();
1432 
1433  __ht._M_reset();
1434  }
1435 
1436  template<typename _Key, typename _Value, typename _Alloc,
1437  typename _ExtractKey, typename _Equal,
1438  typename _Hash, typename _RangeHash, typename _Unused,
1439  typename _RehashPolicy, typename _Traits>
1440  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1441  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1442  _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
1443  : __hashtable_base(__ht),
1444  __map_base(__ht),
1445  __rehash_base(__ht),
1446  __hashtable_alloc(__node_alloc_type(__a)),
1447  _M_buckets(),
1448  _M_bucket_count(__ht._M_bucket_count),
1449  _M_element_count(__ht._M_element_count),
1450  _M_rehash_policy(__ht._M_rehash_policy)
1451  {
1452  __alloc_node_gen_t __alloc_node_gen(*this);
1453  _M_assign(__ht, __alloc_node_gen);
1454  }
1455 
1456  template<typename _Key, typename _Value, typename _Alloc,
1457  typename _ExtractKey, typename _Equal,
1458  typename _Hash, typename _RangeHash, typename _Unused,
1459  typename _RehashPolicy, typename _Traits>
1460  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1461  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1462  _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1463  false_type /* alloc always equal */)
1464  : __hashtable_base(__ht),
1465  __map_base(__ht),
1466  __rehash_base(__ht),
1467  __hashtable_alloc(std::move(__a)),
1468  _M_buckets(nullptr),
1469  _M_bucket_count(__ht._M_bucket_count),
1470  _M_element_count(__ht._M_element_count),
1471  _M_rehash_policy(__ht._M_rehash_policy)
1472  {
1473  if (__ht._M_node_allocator() == this->_M_node_allocator())
1474  {
1475  if (__ht._M_uses_single_bucket())
1476  {
1477  _M_buckets = &_M_single_bucket;
1478  _M_single_bucket = __ht._M_single_bucket;
1479  }
1480  else
1481  _M_buckets = __ht._M_buckets;
1482 
1483  // Fix bucket containing the _M_before_begin pointer that can't be
1484  // moved.
1485  _M_update_bbegin(__ht._M_begin());
1486 
1487  __ht._M_reset();
1488  }
1489  else
1490  {
1491  __alloc_node_gen_t __alloc_gen(*this);
1492 
1493  using _Fwd_Ht = typename
1494  conditional<__move_if_noexcept_cond<value_type>::value,
1495  const _Hashtable&, _Hashtable&&>::type;
1496  _M_assign(std::forward<_Fwd_Ht>(__ht), __alloc_gen);
1497  __ht.clear();
1498  }
1499  }
1500 
1501  template<typename _Key, typename _Value, typename _Alloc,
1502  typename _ExtractKey, typename _Equal,
1503  typename _Hash, typename _RangeHash, typename _Unused,
1504  typename _RehashPolicy, typename _Traits>
1505  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1506  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1507  ~_Hashtable() noexcept
1508  {
1509  clear();
1510  _M_deallocate_buckets();
1511  }
1512 
1513  template<typename _Key, typename _Value, typename _Alloc,
1514  typename _ExtractKey, typename _Equal,
1515  typename _Hash, typename _RangeHash, typename _Unused,
1516  typename _RehashPolicy, typename _Traits>
1517  void
1518  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1519  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1520  swap(_Hashtable& __x)
1521  noexcept(__and_<__is_nothrow_swappable<_Hash>,
1522  __is_nothrow_swappable<_Equal>>::value)
1523  {
1524  // The only base class with member variables is hash_code_base.
1525  // We define _Hash_code_base::_M_swap because different
1526  // specializations have different members.
1527  this->_M_swap(__x);
1528 
1529  std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
1530  std::swap(_M_rehash_policy, __x._M_rehash_policy);
1531 
1532  // Deal properly with potentially moved instances.
1533  if (this->_M_uses_single_bucket())
1534  {
1535  if (!__x._M_uses_single_bucket())
1536  {
1537  _M_buckets = __x._M_buckets;
1538  __x._M_buckets = &__x._M_single_bucket;
1539  }
1540  }
1541  else if (__x._M_uses_single_bucket())
1542  {
1543  __x._M_buckets = _M_buckets;
1544  _M_buckets = &_M_single_bucket;
1545  }
1546  else
1547  std::swap(_M_buckets, __x._M_buckets);
1548 
1549  std::swap(_M_bucket_count, __x._M_bucket_count);
1550  std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1551  std::swap(_M_element_count, __x._M_element_count);
1552  std::swap(_M_single_bucket, __x._M_single_bucket);
1553 
1554  // Fix buckets containing the _M_before_begin pointers that can't be
1555  // swapped.
1556  _M_update_bbegin();
1557  __x._M_update_bbegin();
1558  }
1559 
1560  template<typename _Key, typename _Value, typename _Alloc,
1561  typename _ExtractKey, typename _Equal,
1562  typename _Hash, typename _RangeHash, typename _Unused,
1563  typename _RehashPolicy, typename _Traits>
1564  auto
1565  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1566  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1567  find(const key_type& __k)
1568  -> iterator
1569  {
1570  __hash_code __code = this->_M_hash_code(__k);
1571  std::size_t __bkt = _M_bucket_index(__code);
1572  return iterator(_M_find_node(__bkt, __k, __code));
1573  }
1574 
1575  template<typename _Key, typename _Value, typename _Alloc,
1576  typename _ExtractKey, typename _Equal,
1577  typename _Hash, typename _RangeHash, typename _Unused,
1578  typename _RehashPolicy, typename _Traits>
1579  auto
1580  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1581  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1582  find(const key_type& __k) const
1583  -> const_iterator
1584  {
1585  __hash_code __code = this->_M_hash_code(__k);
1586  std::size_t __bkt = _M_bucket_index(__code);
1587  return const_iterator(_M_find_node(__bkt, __k, __code));
1588  }
1589 
1590 #if __cplusplus > 201703L
1591  template<typename _Key, typename _Value, typename _Alloc,
1592  typename _ExtractKey, typename _Equal,
1593  typename _Hash, typename _RangeHash, typename _Unused,
1594  typename _RehashPolicy, typename _Traits>
1595  template<typename _Kt, typename, typename>
1596  auto
1597  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1598  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1599  _M_find_tr(const _Kt& __k)
1600  -> iterator
1601  {
1602  __hash_code __code = this->_M_hash_code_tr(__k);
1603  std::size_t __bkt = _M_bucket_index(__code);
1604  return iterator(_M_find_node_tr(__bkt, __k, __code));
1605  }
1606 
1607  template<typename _Key, typename _Value, typename _Alloc,
1608  typename _ExtractKey, typename _Equal,
1609  typename _Hash, typename _RangeHash, typename _Unused,
1610  typename _RehashPolicy, typename _Traits>
1611  template<typename _Kt, typename, typename>
1612  auto
1613  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1614  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1615  _M_find_tr(const _Kt& __k) const
1616  -> const_iterator
1617  {
1618  __hash_code __code = this->_M_hash_code_tr(__k);
1619  std::size_t __bkt = _M_bucket_index(__code);
1620  return const_iterator(_M_find_node_tr(__bkt, __k, __code));
1621  }
1622 #endif
1623 
1624  template<typename _Key, typename _Value, typename _Alloc,
1625  typename _ExtractKey, typename _Equal,
1626  typename _Hash, typename _RangeHash, typename _Unused,
1627  typename _RehashPolicy, typename _Traits>
1628  auto
1629  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1630  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1631  count(const key_type& __k) const
1632  -> size_type
1633  {
1634  auto __it = find(__k);
1635  if (!__it._M_cur)
1636  return 0;
1637 
1638  if (__unique_keys::value)
1639  return 1;
1640 
1641  // All equivalent values are next to each other, if we find a
1642  // non-equivalent value after an equivalent one it means that we won't
1643  // find any new equivalent value.
1644  size_type __result = 1;
1645  for (auto __ref = __it++;
1646  __it._M_cur && this->_M_node_equals(*__ref._M_cur, *__it._M_cur);
1647  ++__it)
1648  ++__result;
1649 
1650  return __result;
1651  }
1652 
1653 #if __cplusplus > 201703L
1654  template<typename _Key, typename _Value, typename _Alloc,
1655  typename _ExtractKey, typename _Equal,
1656  typename _Hash, typename _RangeHash, typename _Unused,
1657  typename _RehashPolicy, typename _Traits>
1658  template<typename _Kt, typename, typename>
1659  auto
1660  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1661  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1662  _M_count_tr(const _Kt& __k) const
1663  -> size_type
1664  {
1665  __hash_code __code = this->_M_hash_code_tr(__k);
1666  std::size_t __bkt = _M_bucket_index(__code);
1667  auto __n = _M_find_node_tr(__bkt, __k, __code);
1668  if (!__n)
1669  return 0;
1670 
1671  // All equivalent values are next to each other, if we find a
1672  // non-equivalent value after an equivalent one it means that we won't
1673  // find any new equivalent value.
1674  iterator __it(__n);
1675  size_type __result = 1;
1676  for (++__it;
1677  __it._M_cur && this->_M_equals_tr(__k, __code, *__it._M_cur);
1678  ++__it)
1679  ++__result;
1680 
1681  return __result;
1682  }
1683 #endif
1684 
1685  template<typename _Key, typename _Value, typename _Alloc,
1686  typename _ExtractKey, typename _Equal,
1687  typename _Hash, typename _RangeHash, typename _Unused,
1688  typename _RehashPolicy, typename _Traits>
1689  auto
1690  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1691  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1692  equal_range(const key_type& __k)
1693  -> pair<iterator, iterator>
1694  {
1695  auto __ite = find(__k);
1696  if (!__ite._M_cur)
1697  return { __ite, __ite };
1698 
1699  auto __beg = __ite++;
1700  if (__unique_keys::value)
1701  return { __beg, __ite };
1702 
1703  // All equivalent values are next to each other, if we find a
1704  // non-equivalent value after an equivalent one it means that we won't
1705  // find any new equivalent value.
1706  while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
1707  ++__ite;
1708 
1709  return { __beg, __ite };
1710  }
1711 
1712  template<typename _Key, typename _Value, typename _Alloc,
1713  typename _ExtractKey, typename _Equal,
1714  typename _Hash, typename _RangeHash, typename _Unused,
1715  typename _RehashPolicy, typename _Traits>
1716  auto
1717  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1718  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1719  equal_range(const key_type& __k) const
1720  -> pair<const_iterator, const_iterator>
1721  {
1722  auto __ite = find(__k);
1723  if (!__ite._M_cur)
1724  return { __ite, __ite };
1725 
1726  auto __beg = __ite++;
1727  if (__unique_keys::value)
1728  return { __beg, __ite };
1729 
1730  // All equivalent values are next to each other, if we find a
1731  // non-equivalent value after an equivalent one it means that we won't
1732  // find any new equivalent value.
1733  while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
1734  ++__ite;
1735 
1736  return { __beg, __ite };
1737  }
1738 
1739 #if __cplusplus > 201703L
1740  template<typename _Key, typename _Value, typename _Alloc,
1741  typename _ExtractKey, typename _Equal,
1742  typename _Hash, typename _RangeHash, typename _Unused,
1743  typename _RehashPolicy, typename _Traits>
1744  template<typename _Kt, typename, typename>
1745  auto
1746  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1747  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1748  _M_equal_range_tr(const _Kt& __k)
1749  -> pair<iterator, iterator>
1750  {
1751  __hash_code __code = this->_M_hash_code_tr(__k);
1752  std::size_t __bkt = _M_bucket_index(__code);
1753  auto __n = _M_find_node_tr(__bkt, __k, __code);
1754  iterator __ite(__n);
1755  if (!__n)
1756  return { __ite, __ite };
1757 
1758  // All equivalent values are next to each other, if we find a
1759  // non-equivalent value after an equivalent one it means that we won't
1760  // find any new equivalent value.
1761  auto __beg = __ite++;
1762  while (__ite._M_cur && this->_M_equals_tr(__k, __code, *__ite._M_cur))
1763  ++__ite;
1764 
1765  return { __beg, __ite };
1766  }
1767 
1768  template<typename _Key, typename _Value, typename _Alloc,
1769  typename _ExtractKey, typename _Equal,
1770  typename _Hash, typename _RangeHash, typename _Unused,
1771  typename _RehashPolicy, typename _Traits>
1772  template<typename _Kt, typename, typename>
1773  auto
1774  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1775  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1776  _M_equal_range_tr(const _Kt& __k) const
1777  -> pair<const_iterator, const_iterator>
1778  {
1779  __hash_code __code = this->_M_hash_code_tr(__k);
1780  std::size_t __bkt = _M_bucket_index(__code);
1781  auto __n = _M_find_node_tr(__bkt, __k, __code);
1782  const_iterator __ite(__n);
1783  if (!__n)
1784  return { __ite, __ite };
1785 
1786  // All equivalent values are next to each other, if we find a
1787  // non-equivalent value after an equivalent one it means that we won't
1788  // find any new equivalent value.
1789  auto __beg = __ite++;
1790  while (__ite._M_cur && this->_M_equals_tr(__k, __code, *__ite._M_cur))
1791  ++__ite;
1792 
1793  return { __beg, __ite };
1794  }
1795 #endif
1796 
1797  // Find the node before the one whose key compares equal to k in the bucket
1798  // bkt. Return nullptr if no node is found.
1799  template<typename _Key, typename _Value, typename _Alloc,
1800  typename _ExtractKey, typename _Equal,
1801  typename _Hash, typename _RangeHash, typename _Unused,
1802  typename _RehashPolicy, typename _Traits>
1803  auto
1804  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1805  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1806  _M_find_before_node(size_type __bkt, const key_type& __k,
1807  __hash_code __code) const
1808  -> __node_base_ptr
1809  {
1810  __node_base_ptr __prev_p = _M_buckets[__bkt];
1811  if (!__prev_p)
1812  return nullptr;
1813 
1814  for (__node_ptr __p = static_cast<__node_ptr>(__prev_p->_M_nxt);;
1815  __p = __p->_M_next())
1816  {
1817  if (this->_M_equals(__k, __code, *__p))
1818  return __prev_p;
1819 
1820  if (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt)
1821  break;
1822  __prev_p = __p;
1823  }
1824 
1825  return nullptr;
1826  }
1827 
1828  template<typename _Key, typename _Value, typename _Alloc,
1829  typename _ExtractKey, typename _Equal,
1830  typename _Hash, typename _RangeHash, typename _Unused,
1831  typename _RehashPolicy, typename _Traits>
1832  template<typename _Kt>
1833  auto
1834  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1835  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1836  _M_find_before_node_tr(size_type __bkt, const _Kt& __k,
1837  __hash_code __code) const
1838  -> __node_base_ptr
1839  {
1840  __node_base_ptr __prev_p = _M_buckets[__bkt];
1841  if (!__prev_p)
1842  return nullptr;
1843 
1844  for (__node_ptr __p = static_cast<__node_ptr>(__prev_p->_M_nxt);;
1845  __p = __p->_M_next())
1846  {
1847  if (this->_M_equals_tr(__k, __code, *__p))
1848  return __prev_p;
1849 
1850  if (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt)
1851  break;
1852  __prev_p = __p;
1853  }
1854 
1855  return nullptr;
1856  }
1857 
1858  template<typename _Key, typename _Value, typename _Alloc,
1859  typename _ExtractKey, typename _Equal,
1860  typename _Hash, typename _RangeHash, typename _Unused,
1861  typename _RehashPolicy, typename _Traits>
1862  void
1863  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1864  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1865  _M_insert_bucket_begin(size_type __bkt, __node_ptr __node)
1866  {
1867  if (_M_buckets[__bkt])
1868  {
1869  // Bucket is not empty, we just need to insert the new node
1870  // after the bucket before begin.
1871  __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1872  _M_buckets[__bkt]->_M_nxt = __node;
1873  }
1874  else
1875  {
1876  // The bucket is empty, the new node is inserted at the
1877  // beginning of the singly-linked list and the bucket will
1878  // contain _M_before_begin pointer.
1879  __node->_M_nxt = _M_before_begin._M_nxt;
1880  _M_before_begin._M_nxt = __node;
1881 
1882  if (__node->_M_nxt)
1883  // We must update former begin bucket that is pointing to
1884  // _M_before_begin.
1885  _M_buckets[_M_bucket_index(*__node->_M_next())] = __node;
1886 
1887  _M_buckets[__bkt] = &_M_before_begin;
1888  }
1889  }
1890 
1891  template<typename _Key, typename _Value, typename _Alloc,
1892  typename _ExtractKey, typename _Equal,
1893  typename _Hash, typename _RangeHash, typename _Unused,
1894  typename _RehashPolicy, typename _Traits>
1895  void
1896  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1897  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1898  _M_remove_bucket_begin(size_type __bkt, __node_ptr __next,
1899  size_type __next_bkt)
1900  {
1901  if (!__next || __next_bkt != __bkt)
1902  {
1903  // Bucket is now empty
1904  // First update next bucket if any
1905  if (__next)
1906  _M_buckets[__next_bkt] = _M_buckets[__bkt];
1907 
1908  // Second update before begin node if necessary
1909  if (&_M_before_begin == _M_buckets[__bkt])
1910  _M_before_begin._M_nxt = __next;
1911  _M_buckets[__bkt] = nullptr;
1912  }
1913  }
1914 
1915  template<typename _Key, typename _Value, typename _Alloc,
1916  typename _ExtractKey, typename _Equal,
1917  typename _Hash, typename _RangeHash, typename _Unused,
1918  typename _RehashPolicy, typename _Traits>
1919  auto
1920  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1921  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1922  _M_get_previous_node(size_type __bkt, __node_ptr __n)
1923  -> __node_base_ptr
1924  {
1925  __node_base_ptr __prev_n = _M_buckets[__bkt];
1926  while (__prev_n->_M_nxt != __n)
1927  __prev_n = __prev_n->_M_nxt;
1928  return __prev_n;
1929  }
1930 
1931  template<typename _Key, typename _Value, typename _Alloc,
1932  typename _ExtractKey, typename _Equal,
1933  typename _Hash, typename _RangeHash, typename _Unused,
1934  typename _RehashPolicy, typename _Traits>
1935  template<typename... _Args>
1936  auto
1937  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1938  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1939  _M_emplace(true_type /* __uks */, _Args&&... __args)
1940  -> pair<iterator, bool>
1941  {
1942  // First build the node to get access to the hash code
1943  _Scoped_node __node { this, std::forward<_Args>(__args)... };
1944  const key_type& __k = _ExtractKey{}(__node._M_node->_M_v());
1945  __hash_code __code = this->_M_hash_code(__k);
1946  size_type __bkt = _M_bucket_index(__code);
1947  if (__node_ptr __p = _M_find_node(__bkt, __k, __code))
1948  // There is already an equivalent node, no insertion
1949  return std::make_pair(iterator(__p), false);
1950 
1951  // Insert the node
1952  auto __pos = _M_insert_unique_node(__bkt, __code, __node._M_node);
1953  __node._M_node = nullptr;
1954  return { __pos, true };
1955  }
1956 
1957  template<typename _Key, typename _Value, typename _Alloc,
1958  typename _ExtractKey, typename _Equal,
1959  typename _Hash, typename _RangeHash, typename _Unused,
1960  typename _RehashPolicy, typename _Traits>
1961  template<typename... _Args>
1962  auto
1963  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1964  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1965  _M_emplace(const_iterator __hint, false_type /* __uks */,
1966  _Args&&... __args)
1967  -> iterator
1968  {
1969  // First build the node to get its hash code.
1970  _Scoped_node __node { this, std::forward<_Args>(__args)... };
1971  const key_type& __k = _ExtractKey{}(__node._M_node->_M_v());
1972 
1973  __hash_code __code = this->_M_hash_code(__k);
1974  auto __pos
1975  = _M_insert_multi_node(__hint._M_cur, __code, __node._M_node);
1976  __node._M_node = nullptr;
1977  return __pos;
1978  }
1979 
1980  template<typename _Key, typename _Value, typename _Alloc,
1981  typename _ExtractKey, typename _Equal,
1982  typename _Hash, typename _RangeHash, typename _Unused,
1983  typename _RehashPolicy, typename _Traits>
1984  auto
1985  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1986  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1987  _M_insert_unique_node(size_type __bkt, __hash_code __code,
1988  __node_ptr __node, size_type __n_elt)
1989  -> iterator
1990  {
1991  const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1992  std::pair<bool, std::size_t> __do_rehash
1993  = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
1994  __n_elt);
1995 
1996  if (__do_rehash.first)
1997  {
1998  _M_rehash(__do_rehash.second, __saved_state);
1999  __bkt = _M_bucket_index(__code);
2000  }
2001 
2002  this->_M_store_code(*__node, __code);
2003 
2004  // Always insert at the beginning of the bucket.
2005  _M_insert_bucket_begin(__bkt, __node);
2006  ++_M_element_count;
2007  return iterator(__node);
2008  }
2009 
2010  template<typename _Key, typename _Value, typename _Alloc,
2011  typename _ExtractKey, typename _Equal,
2012  typename _Hash, typename _RangeHash, typename _Unused,
2013  typename _RehashPolicy, typename _Traits>
2014  auto
2015  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2016  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2017  _M_insert_multi_node(__node_ptr __hint,
2018  __hash_code __code, __node_ptr __node)
2019  -> iterator
2020  {
2021  const __rehash_state& __saved_state = _M_rehash_policy._M_state();
2022  std::pair<bool, std::size_t> __do_rehash
2023  = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
2024 
2025  if (__do_rehash.first)
2026  _M_rehash(__do_rehash.second, __saved_state);
2027 
2028  this->_M_store_code(*__node, __code);
2029  const key_type& __k = _ExtractKey{}(__node->_M_v());
2030  size_type __bkt = _M_bucket_index(__code);
2031 
2032  // Find the node before an equivalent one or use hint if it exists and
2033  // if it is equivalent.
2034  __node_base_ptr __prev
2035  = __builtin_expect(__hint != nullptr, false)
2036  && this->_M_equals(__k, __code, *__hint)
2037  ? __hint
2038  : _M_find_before_node(__bkt, __k, __code);
2039 
2040  if (__prev)
2041  {
2042  // Insert after the node before the equivalent one.
2043  __node->_M_nxt = __prev->_M_nxt;
2044  __prev->_M_nxt = __node;
2045  if (__builtin_expect(__prev == __hint, false))
2046  // hint might be the last bucket node, in this case we need to
2047  // update next bucket.
2048  if (__node->_M_nxt
2049  && !this->_M_equals(__k, __code, *__node->_M_next()))
2050  {
2051  size_type __next_bkt = _M_bucket_index(*__node->_M_next());
2052  if (__next_bkt != __bkt)
2053  _M_buckets[__next_bkt] = __node;
2054  }
2055  }
2056  else
2057  // The inserted node has no equivalent in the hashtable. We must
2058  // insert the new node at the beginning of the bucket to preserve
2059  // equivalent elements' relative positions.
2060  _M_insert_bucket_begin(__bkt, __node);
2061  ++_M_element_count;
2062  return iterator(__node);
2063  }
2064 
2065  // Insert v if no element with its key is already present.
2066  template<typename _Key, typename _Value, typename _Alloc,
2067  typename _ExtractKey, typename _Equal,
2068  typename _Hash, typename _RangeHash, typename _Unused,
2069  typename _RehashPolicy, typename _Traits>
2070  template<typename _Arg, typename _NodeGenerator>
2071  auto
2072  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2073  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2074  _M_insert(_Arg&& __v, const _NodeGenerator& __node_gen,
2075  true_type /* __uks */)
2076  -> pair<iterator, bool>
2077  {
2078  const key_type& __k = _ExtractKey{}(__v);
2079  __hash_code __code = this->_M_hash_code(__k);
2080  size_type __bkt = _M_bucket_index(__code);
2081 
2082  if (__node_ptr __node = _M_find_node(__bkt, __k, __code))
2083  return { iterator(__node), false };
2084 
2085  _Scoped_node __node{ __node_gen(std::forward<_Arg>(__v)), this };
2086  auto __pos
2087  = _M_insert_unique_node(__bkt, __code, __node._M_node);
2088  __node._M_node = nullptr;
2089  return { __pos, true };
2090  }
2091 
2092  // Insert v unconditionally.
2093  template<typename _Key, typename _Value, typename _Alloc,
2094  typename _ExtractKey, typename _Equal,
2095  typename _Hash, typename _RangeHash, typename _Unused,
2096  typename _RehashPolicy, typename _Traits>
2097  template<typename _Arg, typename _NodeGenerator>
2098  auto
2099  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2100  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2101  _M_insert(const_iterator __hint, _Arg&& __v,
2102  const _NodeGenerator& __node_gen,
2103  false_type /* __uks */)
2104  -> iterator
2105  {
2106  // First compute the hash code so that we don't do anything if it
2107  // throws.
2108  __hash_code __code = this->_M_hash_code(_ExtractKey{}(__v));
2109 
2110  // Second allocate new node so that we don't rehash if it throws.
2111  _Scoped_node __node{ __node_gen(std::forward<_Arg>(__v)), this };
2112  auto __pos
2113  = _M_insert_multi_node(__hint._M_cur, __code, __node._M_node);
2114  __node._M_node = nullptr;
2115  return __pos;
2116  }
2117 
2118  template<typename _Key, typename _Value, typename _Alloc,
2119  typename _ExtractKey, typename _Equal,
2120  typename _Hash, typename _RangeHash, typename _Unused,
2121  typename _RehashPolicy, typename _Traits>
2122  auto
2123  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2124  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2125  erase(const_iterator __it)
2126  -> iterator
2127  {
2128  __node_ptr __n = __it._M_cur;
2129  std::size_t __bkt = _M_bucket_index(*__n);
2130 
2131  // Look for previous node to unlink it from the erased one, this
2132  // is why we need buckets to contain the before begin to make
2133  // this search fast.
2134  __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
2135  return _M_erase(__bkt, __prev_n, __n);
2136  }
2137 
2138  template<typename _Key, typename _Value, typename _Alloc,
2139  typename _ExtractKey, typename _Equal,
2140  typename _Hash, typename _RangeHash, typename _Unused,
2141  typename _RehashPolicy, typename _Traits>
2142  auto
2143  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2144  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2145  _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n)
2146  -> iterator
2147  {
2148  if (__prev_n == _M_buckets[__bkt])
2149  _M_remove_bucket_begin(__bkt, __n->_M_next(),
2150  __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
2151  else if (__n->_M_nxt)
2152  {
2153  size_type __next_bkt = _M_bucket_index(*__n->_M_next());
2154  if (__next_bkt != __bkt)
2155  _M_buckets[__next_bkt] = __prev_n;
2156  }
2157 
2158  __prev_n->_M_nxt = __n->_M_nxt;
2159  iterator __result(__n->_M_next());
2160  this->_M_deallocate_node(__n);
2161  --_M_element_count;
2162 
2163  return __result;
2164  }
2165 
2166  template<typename _Key, typename _Value, typename _Alloc,
2167  typename _ExtractKey, typename _Equal,
2168  typename _Hash, typename _RangeHash, typename _Unused,
2169  typename _RehashPolicy, typename _Traits>
2170  auto
2171  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2172  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2173  _M_erase(true_type /* __uks */, const key_type& __k)
2174  -> size_type
2175  {
2176  __hash_code __code = this->_M_hash_code(__k);
2177  std::size_t __bkt = _M_bucket_index(__code);
2178 
2179  // Look for the node before the first matching node.
2180  __node_base_ptr __prev_n = _M_find_before_node(__bkt, __k, __code);
2181  if (!__prev_n)
2182  return 0;
2183 
2184  // We found a matching node, erase it.
2185  __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
2186  _M_erase(__bkt, __prev_n, __n);
2187  return 1;
2188  }
2189 
2190  template<typename _Key, typename _Value, typename _Alloc,
2191  typename _ExtractKey, typename _Equal,
2192  typename _Hash, typename _RangeHash, typename _Unused,
2193  typename _RehashPolicy, typename _Traits>
2194  auto
2195  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2196  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2197  _M_erase(false_type /* __uks */, const key_type& __k)
2198  -> size_type
2199  {
2200  __hash_code __code = this->_M_hash_code(__k);
2201  std::size_t __bkt = _M_bucket_index(__code);
2202 
2203  // Look for the node before the first matching node.
2204  __node_base_ptr __prev_n = _M_find_before_node(__bkt, __k, __code);
2205  if (!__prev_n)
2206  return 0;
2207 
2208  // _GLIBCXX_RESOLVE_LIB_DEFECTS
2209  // 526. Is it undefined if a function in the standard changes
2210  // in parameters?
2211  // We use one loop to find all matching nodes and another to deallocate
2212  // them so that the key stays valid during the first loop. It might be
2213  // invalidated indirectly when destroying nodes.
2214  __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
2215  __node_ptr __n_last = __n->_M_next();
2216  while (__n_last && this->_M_node_equals(*__n, *__n_last))
2217  __n_last = __n_last->_M_next();
2218 
2219  std::size_t __n_last_bkt = __n_last ? _M_bucket_index(*__n_last) : __bkt;
2220 
2221  // Deallocate nodes.
2222  size_type __result = 0;
2223  do
2224  {
2225  __node_ptr __p = __n->_M_next();
2226  this->_M_deallocate_node(__n);
2227  __n = __p;
2228  ++__result;
2229  }
2230  while (__n != __n_last);
2231 
2232  _M_element_count -= __result;
2233  if (__prev_n == _M_buckets[__bkt])
2234  _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
2235  else if (__n_last_bkt != __bkt)
2236  _M_buckets[__n_last_bkt] = __prev_n;
2237  __prev_n->_M_nxt = __n_last;
2238  return __result;
2239  }
2240 
2241  template<typename _Key, typename _Value, typename _Alloc,
2242  typename _ExtractKey, typename _Equal,
2243  typename _Hash, typename _RangeHash, typename _Unused,
2244  typename _RehashPolicy, typename _Traits>
2245  auto
2246  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2247  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2248  erase(const_iterator __first, const_iterator __last)
2249  -> iterator
2250  {
2251  __node_ptr __n = __first._M_cur;
2252  __node_ptr __last_n = __last._M_cur;
2253  if (__n == __last_n)
2254  return iterator(__n);
2255 
2256  std::size_t __bkt = _M_bucket_index(*__n);
2257 
2258  __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
2259  bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2260  std::size_t __n_bkt = __bkt;
2261  for (;;)
2262  {
2263  do
2264  {
2265  __node_ptr __tmp = __n;
2266  __n = __n->_M_next();
2267  this->_M_deallocate_node(__tmp);
2268  --_M_element_count;
2269  if (!__n)
2270  break;
2271  __n_bkt = _M_bucket_index(*__n);
2272  }
2273  while (__n != __last_n && __n_bkt == __bkt);
2274  if (__is_bucket_begin)
2275  _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2276  if (__n == __last_n)
2277  break;
2278  __is_bucket_begin = true;
2279  __bkt = __n_bkt;
2280  }
2281 
2282  if (__n && (__n_bkt != __bkt || __is_bucket_begin))
2283  _M_buckets[__n_bkt] = __prev_n;
2284  __prev_n->_M_nxt = __n;
2285  return iterator(__n);
2286  }
2287 
2288  template<typename _Key, typename _Value, typename _Alloc,
2289  typename _ExtractKey, typename _Equal,
2290  typename _Hash, typename _RangeHash, typename _Unused,
2291  typename _RehashPolicy, typename _Traits>
2292  void
2293  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2294  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2295  clear() noexcept
2296  {
2297  this->_M_deallocate_nodes(_M_begin());
2298  __builtin_memset(_M_buckets, 0,
2299  _M_bucket_count * sizeof(__node_base_ptr));
2300  _M_element_count = 0;
2301  _M_before_begin._M_nxt = nullptr;
2302  }
2303 
2304  template<typename _Key, typename _Value, typename _Alloc,
2305  typename _ExtractKey, typename _Equal,
2306  typename _Hash, typename _RangeHash, typename _Unused,
2307  typename _RehashPolicy, typename _Traits>
2308  void
2309  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2310  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2311  rehash(size_type __bkt_count)
2312  {
2313  const __rehash_state& __saved_state = _M_rehash_policy._M_state();
2314  __bkt_count
2315  = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
2316  __bkt_count);
2317  __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count);
2318 
2319  if (__bkt_count != _M_bucket_count)
2320  _M_rehash(__bkt_count, __saved_state);
2321  else
2322  // No rehash, restore previous state to keep it consistent with
2323  // container state.
2324  _M_rehash_policy._M_reset(__saved_state);
2325  }
2326 
2327  template<typename _Key, typename _Value, typename _Alloc,
2328  typename _ExtractKey, typename _Equal,
2329  typename _Hash, typename _RangeHash, typename _Unused,
2330  typename _RehashPolicy, typename _Traits>
2331  void
2332  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2333  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2334  _M_rehash(size_type __bkt_count, const __rehash_state& __state)
2335  {
2336  __try
2337  {
2338  _M_rehash_aux(__bkt_count, __unique_keys{});
2339  }
2340  __catch(...)
2341  {
2342  // A failure here means that buckets allocation failed. We only
2343  // have to restore hash policy previous state.
2344  _M_rehash_policy._M_reset(__state);
2345  __throw_exception_again;
2346  }
2347  }
2348 
2349  // Rehash when there is no equivalent elements.
2350  template<typename _Key, typename _Value, typename _Alloc,
2351  typename _ExtractKey, typename _Equal,
2352  typename _Hash, typename _RangeHash, typename _Unused,
2353  typename _RehashPolicy, typename _Traits>
2354  void
2355  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2356  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2357  _M_rehash_aux(size_type __bkt_count, true_type /* __uks */)
2358  {
2359  __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2360  __node_ptr __p = _M_begin();
2361  _M_before_begin._M_nxt = nullptr;
2362  std::size_t __bbegin_bkt = 0;
2363  while (__p)
2364  {
2365  __node_ptr __next = __p->_M_next();
2366  std::size_t __bkt
2367  = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
2368  if (!__new_buckets[__bkt])
2369  {
2370  __p->_M_nxt = _M_before_begin._M_nxt;
2371  _M_before_begin._M_nxt = __p;
2372  __new_buckets[__bkt] = &_M_before_begin;
2373  if (__p->_M_nxt)
2374  __new_buckets[__bbegin_bkt] = __p;
2375  __bbegin_bkt = __bkt;
2376  }
2377  else
2378  {
2379  __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2380  __new_buckets[__bkt]->_M_nxt = __p;
2381  }
2382 
2383  __p = __next;
2384  }
2385 
2386  _M_deallocate_buckets();
2387  _M_bucket_count = __bkt_count;
2388  _M_buckets = __new_buckets;
2389  }
2390 
2391  // Rehash when there can be equivalent elements, preserve their relative
2392  // order.
2393  template<typename _Key, typename _Value, typename _Alloc,
2394  typename _ExtractKey, typename _Equal,
2395  typename _Hash, typename _RangeHash, typename _Unused,
2396  typename _RehashPolicy, typename _Traits>
2397  void
2398  _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2399  _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2400  _M_rehash_aux(size_type __bkt_count, false_type /* __uks */)
2401  {
2402  __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2403  __node_ptr __p = _M_begin();
2404  _M_before_begin._M_nxt = nullptr;
2405  std::size_t __bbegin_bkt = 0;
2406  std::size_t __prev_bkt = 0;
2407  __node_ptr __prev_p = nullptr;
2408  bool __check_bucket = false;
2409 
2410  while (__p)
2411  {
2412  __node_ptr __next = __p->_M_next();
2413  std::size_t __bkt
2414  = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
2415 
2416  if (__prev_p && __prev_bkt == __bkt)
2417  {
2418  // Previous insert was already in this bucket, we insert after
2419  // the previously inserted one to preserve equivalent elements
2420  // relative order.
2421  __p->_M_nxt = __prev_p->_M_nxt;
2422  __prev_p->_M_nxt = __p;
2423 
2424  // Inserting after a node in a bucket require to check that we
2425  // haven't change the bucket last node, in this case next
2426  // bucket containing its before begin node must be updated. We
2427  // schedule a check as soon as we move out of the sequence of
2428  // equivalent nodes to limit the number of checks.
2429  __check_bucket = true;
2430  }
2431  else
2432  {
2433  if (__check_bucket)
2434  {
2435  // Check if we shall update the next bucket because of
2436  // insertions into __prev_bkt bucket.
2437  if (__prev_p->_M_nxt)
2438  {
2439  std::size_t __next_bkt
2440  = __hash_code_base::_M_bucket_index(
2441  *__prev_p->_M_next(), __bkt_count);
2442  if (__next_bkt != __prev_bkt)
2443  __new_buckets[__next_bkt] = __prev_p;
2444  }
2445  __check_bucket = false;
2446  }
2447 
2448  if (!__new_buckets[__bkt])
2449  {
2450  __p->_M_nxt = _M_before_begin._M_nxt;
2451  _M_before_begin._M_nxt = __p;
2452  __new_buckets[__bkt] = &_M_before_begin;
2453  if (__p->_M_nxt)
2454  __new_buckets[__bbegin_bkt] = __p;
2455  __bbegin_bkt = __bkt;
2456  }
2457  else
2458  {
2459  __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2460  __new_buckets[__bkt]->_M_nxt = __p;
2461  }
2462  }
2463  __prev_p = __p;
2464  __prev_bkt = __bkt;
2465  __p = __next;
2466  }
2467 
2468  if (__check_bucket && __prev_p->_M_nxt)
2469  {
2470  std::size_t __next_bkt
2471  = __hash_code_base::_M_bucket_index(*__prev_p->_M_next(),
2472  __bkt_count);
2473  if (__next_bkt != __prev_bkt)
2474  __new_buckets[__next_bkt] = __prev_p;
2475  }
2476 
2477  _M_deallocate_buckets();
2478  _M_bucket_count = __bkt_count;
2479  _M_buckets = __new_buckets;
2480  }
2481 
2482 #if __cplusplus > 201402L
2483  template<typename, typename, typename> class _Hash_merge_helper { };
2484 #endif // C++17
2485 
2486 #if __cpp_deduction_guides >= 201606
2487  // Used to constrain deduction guides
2488  template<typename _Hash>
2489  using _RequireNotAllocatorOrIntegral
2490  = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
2491 #endif
2492 
2493 _GLIBCXX_END_NAMESPACE_VERSION
2494 } // namespace std
2495 
2496 #endif // _HASHTABLE_H
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1214
initializer_list
integral_constant
Definition: type_traits:57
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:78
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:245
is_nothrow_copy_constructible
Definition: type_traits:1006
is_nothrow_move_assignable
Definition: type_traits:1133
void _M_merge_unique(_Compatible_Hashtable &__src) noexcept
Merge from a compatible container into one with unique keys.
_Tp * end(valarray< _Tp > &__va)
Return an iterator pointing to one past the last element of the valarray.
Definition: valarray:1234
Struct holding two objects of arbitrary type.
Definition: stl_pair.h:211
Define a member typedef type to one of two argument types.
Definition: type_traits:92
is_nothrow_default_constructible
Definition: type_traits:984
void _M_merge_multi(_Compatible_Hashtable &__src) noexcept
Merge from a compatible container into one with equivalent keys.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
Definition: range_access.h:119
is_same
Definition: type_traits:585
_T2 second
The second member.
Definition: stl_pair.h:218
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:75
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:104
node_type extract(const _Key &__k)
Extract a node.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
Definition: range_access.h:263
_T1 first
The first member.
Definition: stl_pair.h:217
insert_return_type _M_reinsert_node(node_type &&__nh)
Re-insert an extracted node into a container with unique keys.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:254
Common iterator class.
ISO C++ entities toplevel namespace is std.
void swap(shared_lock< _Mutex > &__x, shared_lock< _Mutex > &__y) noexcept
Swap specialization for shared_lock.
Definition: shared_mutex:851
auto_ptr & operator=(auto_ptr &__a)
auto_ptr assignment operator.
Definition: auto_ptr.h:128
Node handle type for maps.
Definition: node_handle.h:221
Uniform interface to C++98 and C++11 allocators.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
Definition: range_access.h:130
iterator _M_reinsert_node_multi(const_iterator __hint, node_type &&__nh)
Re-insert an extracted node into a container with equivalent keys.
Return type of insert(node_handle&&) on unique maps/sets.
Definition: node_handle.h:363