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