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