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