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