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