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