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