libstdc++
hashtable.h
Go to the documentation of this file.
00001 // hashtable.h header -*- C++ -*-
00002 
00003 // Copyright (C) 2007-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/hashtable.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{unordered_map, unordered_set}
00028  */
00029 
00030 #ifndef _HASHTABLE_H
00031 #define _HASHTABLE_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <bits/hashtable_policy.h>
00036 #if __cplusplus > 201402L
00037 # include <bits/node_handle.h>
00038 #endif
00039 
00040 namespace std _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043 
00044   template<typename _Tp, typename _Hash>
00045     using __cache_default
00046       =  __not_<__and_<// Do not cache for fast hasher.
00047                        __is_fast_hash<_Hash>,
00048                        // Mandatory to have erase not throwing.
00049                        __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
00050 
00051   /**
00052    *  Primary class template _Hashtable.
00053    *
00054    *  @ingroup hashtable-detail
00055    *
00056    *  @tparam _Value  CopyConstructible type.
00057    *
00058    *  @tparam _Key    CopyConstructible type.
00059    *
00060    *  @tparam _Alloc  An allocator type
00061    *  ([lib.allocator.requirements]) whose _Alloc::value_type is
00062    *  _Value.  As a conforming extension, we allow for
00063    *  _Alloc::value_type != _Value.
00064    *
00065    *  @tparam _ExtractKey  Function object that takes an object of type
00066    *  _Value and returns a value of type _Key.
00067    *
00068    *  @tparam _Equal  Function object that takes two objects of type k
00069    *  and returns a bool-like value that is true if the two objects
00070    *  are considered equal.
00071    *
00072    *  @tparam _H1  The hash function. A unary function object with
00073    *  argument type _Key and result type size_t. Return values should
00074    *  be distributed over the entire range [0, numeric_limits<size_t>:::max()].
00075    *
00076    *  @tparam _H2  The range-hashing function (in the terminology of
00077    *  Tavori and Dreizin).  A binary function object whose argument
00078    *  types and result type are all size_t.  Given arguments r and N,
00079    *  the return value is in the range [0, N).
00080    *
00081    *  @tparam _Hash  The ranged hash function (Tavori and Dreizin). A
00082    *  binary function whose argument types are _Key and size_t and
00083    *  whose result type is size_t.  Given arguments k and N, the
00084    *  return value is in the range [0, N).  Default: hash(k, N) =
00085    *  h2(h1(k), N).  If _Hash is anything other than the default, _H1
00086    *  and _H2 are ignored.
00087    *
00088    *  @tparam _RehashPolicy  Policy class with three members, all of
00089    *  which govern the bucket count. _M_next_bkt(n) returns a bucket
00090    *  count no smaller than n.  _M_bkt_for_elements(n) returns a
00091    *  bucket count appropriate for an element count of n.
00092    *  _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
00093    *  current bucket count is n_bkt and the current element count is
00094    *  n_elt, we need to increase the bucket count.  If so, returns
00095    *  make_pair(true, n), where n is the new bucket count.  If not,
00096    *  returns make_pair(false, <anything>)
00097    *
00098    *  @tparam _Traits  Compile-time class with three boolean
00099    *  std::integral_constant members:  __cache_hash_code, __constant_iterators,
00100    *   __unique_keys.
00101    *
00102    *  Each _Hashtable data structure has:
00103    *
00104    *  - _Bucket[]       _M_buckets
00105    *  - _Hash_node_base _M_before_begin
00106    *  - size_type       _M_bucket_count
00107    *  - size_type       _M_element_count
00108    *
00109    *  with _Bucket being _Hash_node* and _Hash_node containing:
00110    *
00111    *  - _Hash_node*   _M_next
00112    *  - Tp            _M_value
00113    *  - size_t        _M_hash_code if cache_hash_code is true
00114    *
00115    *  In terms of Standard containers the hashtable is like the aggregation of:
00116    *
00117    *  - std::forward_list<_Node> containing the elements
00118    *  - std::vector<std::forward_list<_Node>::iterator> representing the buckets
00119    *
00120    *  The non-empty buckets contain the node before the first node in the
00121    *  bucket. This design makes it possible to implement something like a
00122    *  std::forward_list::insert_after on container insertion and
00123    *  std::forward_list::erase_after on container erase
00124    *  calls. _M_before_begin is equivalent to
00125    *  std::forward_list::before_begin. Empty buckets contain
00126    *  nullptr.  Note that one of the non-empty buckets contains
00127    *  &_M_before_begin which is not a dereferenceable node so the
00128    *  node pointer in a bucket shall never be dereferenced, only its
00129    *  next node can be.
00130    *
00131    *  Walking through a bucket's nodes requires a check on the hash code to
00132    *  see if each node is still in the bucket. Such a design assumes a
00133    *  quite efficient hash functor and is one of the reasons it is
00134    *  highly advisable to set __cache_hash_code to true.
00135    *
00136    *  The container iterators are simply built from nodes. This way
00137    *  incrementing the iterator is perfectly efficient independent of
00138    *  how many empty buckets there are in the container.
00139    *
00140    *  On insert we compute the element's hash code and use it to find the
00141    *  bucket index. If the element must be inserted in an empty bucket
00142    *  we add it at the beginning of the singly linked list and make the
00143    *  bucket point to _M_before_begin. The bucket that used to point to
00144    *  _M_before_begin, if any, is updated to point to its new before
00145    *  begin node.
00146    *
00147    *  On erase, the simple iterator design requires using the hash
00148    *  functor to get the index of the bucket to update. For this
00149    *  reason, when __cache_hash_code is set to false the hash functor must
00150    *  not throw and this is enforced by a static assertion.
00151    *
00152    *  Functionality is implemented by decomposition into base classes,
00153    *  where the derived _Hashtable class is used in _Map_base,
00154    *  _Insert, _Rehash_base, and _Equality base classes to access the
00155    *  "this" pointer. _Hashtable_base is used in the base classes as a
00156    *  non-recursive, fully-completed-type so that detailed nested type
00157    *  information, such as iterator type and node type, can be
00158    *  used. This is similar to the "Curiously Recurring Template
00159    *  Pattern" (CRTP) technique, but uses a reconstructed, not
00160    *  explicitly passed, template pattern.
00161    *
00162    *  Base class templates are: 
00163    *    - __detail::_Hashtable_base
00164    *    - __detail::_Map_base
00165    *    - __detail::_Insert
00166    *    - __detail::_Rehash_base
00167    *    - __detail::_Equality
00168    */
00169   template<typename _Key, typename _Value, typename _Alloc,
00170            typename _ExtractKey, typename _Equal,
00171            typename _H1, typename _H2, typename _Hash,
00172            typename _RehashPolicy, typename _Traits>
00173     class _Hashtable
00174     : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
00175                                        _H1, _H2, _Hash, _Traits>,
00176       public __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00177                                  _H1, _H2, _Hash, _RehashPolicy, _Traits>,
00178       public __detail::_Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00179                                _H1, _H2, _Hash, _RehashPolicy, _Traits>,
00180       public __detail::_Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00181                                     _H1, _H2, _Hash, _RehashPolicy, _Traits>,
00182       public __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00183                                  _H1, _H2, _Hash, _RehashPolicy, _Traits>,
00184       private __detail::_Hashtable_alloc<
00185         __alloc_rebind<_Alloc,
00186                        __detail::_Hash_node<_Value,
00187                                             _Traits::__hash_cached::value>>>
00188     {
00189       static_assert(is_same<typename remove_cv<_Value>::type, _Value>::value,
00190           "unordered container must have a non-const, non-volatile value_type");
00191 #ifdef __STRICT_ANSI__
00192       static_assert(is_same<typename _Alloc::value_type, _Value>{},
00193           "unordered container must have the same value_type as its allocator");
00194 #endif
00195 
00196       using __traits_type = _Traits;
00197       using __hash_cached = typename __traits_type::__hash_cached;
00198       using __node_type = __detail::_Hash_node<_Value, __hash_cached::value>;
00199       using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
00200 
00201       using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
00202 
00203       using __value_alloc_traits =
00204         typename __hashtable_alloc::__value_alloc_traits;
00205       using __node_alloc_traits =
00206         typename __hashtable_alloc::__node_alloc_traits;
00207       using __node_base = typename __hashtable_alloc::__node_base;
00208       using __bucket_type = typename __hashtable_alloc::__bucket_type;
00209 
00210     public:
00211       typedef _Key                                              key_type;
00212       typedef _Value                                            value_type;
00213       typedef _Alloc                                            allocator_type;
00214       typedef _Equal                                            key_equal;
00215 
00216       // mapped_type, if present, comes from _Map_base.
00217       // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
00218       typedef typename __value_alloc_traits::pointer            pointer;
00219       typedef typename __value_alloc_traits::const_pointer      const_pointer;
00220       typedef value_type&                                       reference;
00221       typedef const value_type&                                 const_reference;
00222 
00223     private:
00224       using __rehash_type = _RehashPolicy;
00225       using __rehash_state = typename __rehash_type::_State;
00226 
00227       using __constant_iterators = typename __traits_type::__constant_iterators;
00228       using __unique_keys = typename __traits_type::__unique_keys;
00229 
00230       using __key_extract = typename std::conditional<
00231                                              __constant_iterators::value,
00232                                              __detail::_Identity,
00233                                              __detail::_Select1st>::type;
00234 
00235       using __hashtable_base = __detail::
00236 			       _Hashtable_base<_Key, _Value, _ExtractKey,
00237                                               _Equal, _H1, _H2, _Hash, _Traits>;
00238 
00239       using __hash_code_base =  typename __hashtable_base::__hash_code_base;
00240       using __hash_code =  typename __hashtable_base::__hash_code;
00241       using __ireturn_type = typename __hashtable_base::__ireturn_type;
00242 
00243       using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
00244                                              _Equal, _H1, _H2, _Hash,
00245                                              _RehashPolicy, _Traits>;
00246 
00247       using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
00248                                                    _ExtractKey, _Equal,
00249                                                    _H1, _H2, _Hash,
00250                                                    _RehashPolicy, _Traits>;
00251 
00252       using __eq_base = __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey,
00253                                             _Equal, _H1, _H2, _Hash,
00254                                             _RehashPolicy, _Traits>;
00255 
00256       using __reuse_or_alloc_node_type =
00257         __detail::_ReuseOrAllocNode<__node_alloc_type>;
00258 
00259       // Metaprogramming for picking apart hash caching.
00260       template<typename _Cond>
00261         using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
00262 
00263       template<typename _Cond>
00264         using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
00265 
00266       // Compile-time diagnostics.
00267 
00268       // _Hash_code_base has everything protected, so use this derived type to
00269       // access it.
00270       struct __hash_code_base_access : __hash_code_base
00271       { using __hash_code_base::_M_bucket_index; };
00272 
00273       // Getting a bucket index from a node shall not throw because it is used
00274       // in methods (erase, swap...) that shall not throw.
00275       static_assert(noexcept(declval<const __hash_code_base_access&>()
00276                              ._M_bucket_index((const __node_type*)nullptr,
00277                                               (std::size_t)0)),
00278                     "Cache the hash code or qualify your functors involved"
00279                     " in hash code and bucket index computation with noexcept");
00280 
00281       // Following two static assertions are necessary to guarantee
00282       // that local_iterator will be default constructible.
00283 
00284       // When hash codes are cached local iterator inherits from H2 functor
00285       // which must then be default constructible.
00286       static_assert(__if_hash_cached<is_default_constructible<_H2>>::value,
00287                     "Functor used to map hash code to bucket index"
00288                     " must be default constructible");
00289 
00290       template<typename _Keya, typename _Valuea, typename _Alloca,
00291                typename _ExtractKeya, typename _Equala,
00292                typename _H1a, typename _H2a, typename _Hasha,
00293                typename _RehashPolicya, typename _Traitsa,
00294                bool _Unique_keysa>
00295         friend struct __detail::_Map_base;
00296 
00297       template<typename _Keya, typename _Valuea, typename _Alloca,
00298                typename _ExtractKeya, typename _Equala,
00299                typename _H1a, typename _H2a, typename _Hasha,
00300                typename _RehashPolicya, typename _Traitsa>
00301         friend struct __detail::_Insert_base;
00302 
00303       template<typename _Keya, typename _Valuea, typename _Alloca,
00304                typename _ExtractKeya, typename _Equala,
00305                typename _H1a, typename _H2a, typename _Hasha,
00306                typename _RehashPolicya, typename _Traitsa,
00307                bool _Constant_iteratorsa>
00308         friend struct __detail::_Insert;
00309 
00310     public:
00311       using size_type = typename __hashtable_base::size_type;
00312       using difference_type = typename __hashtable_base::difference_type;
00313 
00314       using iterator = typename __hashtable_base::iterator;
00315       using const_iterator = typename __hashtable_base::const_iterator;
00316 
00317       using local_iterator = typename __hashtable_base::local_iterator;
00318       using const_local_iterator = typename __hashtable_base::
00319                                    const_local_iterator;
00320 
00321 #if __cplusplus > 201402L
00322       using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
00323       using insert_return_type = _Node_insert_return<iterator, node_type>;
00324 #endif
00325 
00326     private:
00327       __bucket_type*            _M_buckets              = &_M_single_bucket;
00328       size_type                 _M_bucket_count         = 1;
00329       __node_base               _M_before_begin;
00330       size_type                 _M_element_count        = 0;
00331       _RehashPolicy             _M_rehash_policy;
00332 
00333       // A single bucket used when only need for 1 bucket. Especially
00334       // interesting in move semantic to leave hashtable with only 1 buckets
00335       // which is not allocated so that we can have those operations noexcept
00336       // qualified.
00337       // Note that we can't leave hashtable with 0 bucket without adding
00338       // numerous checks in the code to avoid 0 modulus.
00339       __bucket_type             _M_single_bucket        = nullptr;
00340 
00341       bool
00342       _M_uses_single_bucket(__bucket_type* __bkts) const
00343       { return __builtin_expect(__bkts == &_M_single_bucket, false); }
00344 
00345       bool
00346       _M_uses_single_bucket() const
00347       { return _M_uses_single_bucket(_M_buckets); }
00348 
00349       __hashtable_alloc&
00350       _M_base_alloc() { return *this; }
00351 
00352       __bucket_type*
00353       _M_allocate_buckets(size_type __n)
00354       {
00355         if (__builtin_expect(__n == 1, false))
00356           {
00357             _M_single_bucket = nullptr;
00358             return &_M_single_bucket;
00359           }
00360 
00361         return __hashtable_alloc::_M_allocate_buckets(__n);
00362       }
00363 
00364       void
00365       _M_deallocate_buckets(__bucket_type* __bkts, size_type __n)
00366       {
00367         if (_M_uses_single_bucket(__bkts))
00368           return;
00369 
00370         __hashtable_alloc::_M_deallocate_buckets(__bkts, __n);
00371       }
00372 
00373       void
00374       _M_deallocate_buckets()
00375       { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
00376 
00377       // Gets bucket begin, deals with the fact that non-empty buckets contain
00378       // their before begin node.
00379       __node_type*
00380       _M_bucket_begin(size_type __bkt) const;
00381 
00382       __node_type*
00383       _M_begin() const
00384       { return static_cast<__node_type*>(_M_before_begin._M_nxt); }
00385 
00386       // Assign *this using another _Hashtable instance. Either elements
00387       // are copy or move depends on the _NodeGenerator.
00388       template<typename _Ht, typename _NodeGenerator>
00389         void
00390         _M_assign_elements(_Ht&&, const _NodeGenerator&);
00391 
00392       template<typename _NodeGenerator>
00393         void
00394         _M_assign(const _Hashtable&, const _NodeGenerator&);
00395 
00396       void
00397       _M_move_assign(_Hashtable&&, std::true_type);
00398 
00399       void
00400       _M_move_assign(_Hashtable&&, std::false_type);
00401 
00402       void
00403       _M_reset() noexcept;
00404 
00405       _Hashtable(const _H1& __h1, const _H2& __h2, const _Hash& __h,
00406                  const _Equal& __eq, const _ExtractKey& __exk,
00407                  const allocator_type& __a)
00408         : __hashtable_base(__exk, __h1, __h2, __h, __eq),
00409           __hashtable_alloc(__node_alloc_type(__a))
00410       { }
00411 
00412     public:
00413       // Constructor, destructor, assignment, swap
00414       _Hashtable() = default;
00415       _Hashtable(size_type __bucket_hint,
00416                  const _H1&, const _H2&, const _Hash&,
00417                  const _Equal&, const _ExtractKey&,
00418                  const allocator_type&);
00419 
00420       template<typename _InputIterator>
00421         _Hashtable(_InputIterator __first, _InputIterator __last,
00422                    size_type __bucket_hint,
00423                    const _H1&, const _H2&, const _Hash&,
00424                    const _Equal&, const _ExtractKey&,
00425                    const allocator_type&);
00426 
00427       _Hashtable(const _Hashtable&);
00428 
00429       _Hashtable(_Hashtable&&) noexcept;
00430 
00431       _Hashtable(const _Hashtable&, const allocator_type&);
00432 
00433       _Hashtable(_Hashtable&&, const allocator_type&);
00434 
00435       // Use delegating constructors.
00436       explicit
00437       _Hashtable(const allocator_type& __a)
00438         : __hashtable_alloc(__node_alloc_type(__a))
00439       { }
00440 
00441       explicit
00442       _Hashtable(size_type __n,
00443                  const _H1& __hf = _H1(),
00444                  const key_equal& __eql = key_equal(),
00445                  const allocator_type& __a = allocator_type())
00446       : _Hashtable(__n, __hf, _H2(), _Hash(), __eql,
00447                    __key_extract(), __a)
00448       { }
00449 
00450       template<typename _InputIterator>
00451         _Hashtable(_InputIterator __f, _InputIterator __l,
00452                    size_type __n = 0,
00453                    const _H1& __hf = _H1(),
00454                    const key_equal& __eql = key_equal(),
00455                    const allocator_type& __a = allocator_type())
00456         : _Hashtable(__f, __l, __n, __hf, _H2(), _Hash(), __eql,
00457                      __key_extract(), __a)
00458         { }
00459 
00460       _Hashtable(initializer_list<value_type> __l,
00461                  size_type __n = 0,
00462                  const _H1& __hf = _H1(),
00463                  const key_equal& __eql = key_equal(),
00464                  const allocator_type& __a = allocator_type())
00465       : _Hashtable(__l.begin(), __l.end(), __n, __hf, _H2(), _Hash(), __eql,
00466                    __key_extract(), __a)
00467       { }
00468 
00469       _Hashtable&
00470       operator=(const _Hashtable& __ht);
00471 
00472       _Hashtable&
00473       operator=(_Hashtable&& __ht)
00474       noexcept(__node_alloc_traits::_S_nothrow_move()
00475                && is_nothrow_move_assignable<_H1>::value
00476                && is_nothrow_move_assignable<_Equal>::value)
00477       {
00478         constexpr bool __move_storage =
00479           __node_alloc_traits::_S_propagate_on_move_assign()
00480           || __node_alloc_traits::_S_always_equal();
00481         _M_move_assign(std::move(__ht), __bool_constant<__move_storage>());
00482         return *this;
00483       }
00484 
00485       _Hashtable&
00486       operator=(initializer_list<value_type> __l)
00487       {
00488         __reuse_or_alloc_node_type __roan(_M_begin(), *this);
00489         _M_before_begin._M_nxt = nullptr;
00490         clear();
00491         this->_M_insert_range(__l.begin(), __l.end(), __roan, __unique_keys());
00492         return *this;
00493       }
00494 
00495       ~_Hashtable() noexcept;
00496 
00497       void
00498       swap(_Hashtable&)
00499       noexcept(__and_<__is_nothrow_swappable<_H1>,
00500                           __is_nothrow_swappable<_Equal>>::value);
00501 
00502       // Basic container operations
00503       iterator
00504       begin() noexcept
00505       { return iterator(_M_begin()); }
00506 
00507       const_iterator
00508       begin() const noexcept
00509       { return const_iterator(_M_begin()); }
00510 
00511       iterator
00512       end() noexcept
00513       { return iterator(nullptr); }
00514 
00515       const_iterator
00516       end() const noexcept
00517       { return const_iterator(nullptr); }
00518 
00519       const_iterator
00520       cbegin() const noexcept
00521       { return const_iterator(_M_begin()); }
00522 
00523       const_iterator
00524       cend() const noexcept
00525       { return const_iterator(nullptr); }
00526 
00527       size_type
00528       size() const noexcept
00529       { return _M_element_count; }
00530 
00531       _GLIBCXX_NODISCARD bool
00532       empty() const noexcept
00533       { return size() == 0; }
00534 
00535       allocator_type
00536       get_allocator() const noexcept
00537       { return allocator_type(this->_M_node_allocator()); }
00538 
00539       size_type
00540       max_size() const noexcept
00541       { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
00542 
00543       // Observers
00544       key_equal
00545       key_eq() const
00546       { return this->_M_eq(); }
00547 
00548       // hash_function, if present, comes from _Hash_code_base.
00549 
00550       // Bucket operations
00551       size_type
00552       bucket_count() const noexcept
00553       { return _M_bucket_count; }
00554 
00555       size_type
00556       max_bucket_count() const noexcept
00557       { return max_size(); }
00558 
00559       size_type
00560       bucket_size(size_type __n) const
00561       { return std::distance(begin(__n), end(__n)); }
00562 
00563       size_type
00564       bucket(const key_type& __k) const
00565       { return _M_bucket_index(__k, this->_M_hash_code(__k)); }
00566 
00567       local_iterator
00568       begin(size_type __n)
00569       {
00570         return local_iterator(*this, _M_bucket_begin(__n),
00571                               __n, _M_bucket_count);
00572       }
00573 
00574       local_iterator
00575       end(size_type __n)
00576       { return local_iterator(*this, nullptr, __n, _M_bucket_count); }
00577 
00578       const_local_iterator
00579       begin(size_type __n) const
00580       {
00581         return const_local_iterator(*this, _M_bucket_begin(__n),
00582                                     __n, _M_bucket_count);
00583       }
00584 
00585       const_local_iterator
00586       end(size_type __n) const
00587       { return const_local_iterator(*this, nullptr, __n, _M_bucket_count); }
00588 
00589       // DR 691.
00590       const_local_iterator
00591       cbegin(size_type __n) const
00592       {
00593         return const_local_iterator(*this, _M_bucket_begin(__n),
00594                                     __n, _M_bucket_count);
00595       }
00596 
00597       const_local_iterator
00598       cend(size_type __n) const
00599       { return const_local_iterator(*this, nullptr, __n, _M_bucket_count); }
00600 
00601       float
00602       load_factor() const noexcept
00603       {
00604         return static_cast<float>(size()) / static_cast<float>(bucket_count());
00605       }
00606 
00607       // max_load_factor, if present, comes from _Rehash_base.
00608 
00609       // Generalization of max_load_factor.  Extension, not found in
00610       // TR1.  Only useful if _RehashPolicy is something other than
00611       // the default.
00612       const _RehashPolicy&
00613       __rehash_policy() const
00614       { return _M_rehash_policy; }
00615 
00616       void
00617       __rehash_policy(const _RehashPolicy& __pol)
00618       { _M_rehash_policy = __pol; }
00619 
00620       // Lookup.
00621       iterator
00622       find(const key_type& __k);
00623 
00624       const_iterator
00625       find(const key_type& __k) const;
00626 
00627       size_type
00628       count(const key_type& __k) const;
00629 
00630       std::pair<iterator, iterator>
00631       equal_range(const key_type& __k);
00632 
00633       std::pair<const_iterator, const_iterator>
00634       equal_range(const key_type& __k) const;
00635 
00636     protected:
00637       // Bucket index computation helpers.
00638       size_type
00639       _M_bucket_index(__node_type* __n) const noexcept
00640       { return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
00641 
00642       size_type
00643       _M_bucket_index(const key_type& __k, __hash_code __c) const
00644       { return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
00645 
00646       // Find and insert helper functions and types
00647       // Find the node before the one matching the criteria.
00648       __node_base*
00649       _M_find_before_node(size_type, const key_type&, __hash_code) const;
00650 
00651       __node_type*
00652       _M_find_node(size_type __bkt, const key_type& __key,
00653                    __hash_code __c) const
00654       {
00655         __node_base* __before_n = _M_find_before_node(__bkt, __key, __c);
00656         if (__before_n)
00657           return static_cast<__node_type*>(__before_n->_M_nxt);
00658         return nullptr;
00659       }
00660 
00661       // Insert a node at the beginning of a bucket.
00662       void
00663       _M_insert_bucket_begin(size_type, __node_type*);
00664 
00665       // Remove the bucket first node
00666       void
00667       _M_remove_bucket_begin(size_type __bkt, __node_type* __next_n,
00668                              size_type __next_bkt);
00669 
00670       // Get the node before __n in the bucket __bkt
00671       __node_base*
00672       _M_get_previous_node(size_type __bkt, __node_base* __n);
00673 
00674       // Insert node with hash code __code, in bucket bkt if no rehash (assumes
00675       // no element with its key already present). Take ownership of the node,
00676       // deallocate it on exception.
00677       iterator
00678       _M_insert_unique_node(size_type __bkt, __hash_code __code,
00679                             __node_type* __n, size_type __n_elt = 1);
00680 
00681       // Insert node with hash code __code. Take ownership of the node,
00682       // deallocate it on exception.
00683       iterator
00684       _M_insert_multi_node(__node_type* __hint,
00685                            __hash_code __code, __node_type* __n);
00686 
00687       template<typename... _Args>
00688         std::pair<iterator, bool>
00689         _M_emplace(std::true_type, _Args&&... __args);
00690 
00691       template<typename... _Args>
00692         iterator
00693         _M_emplace(std::false_type __uk, _Args&&... __args)
00694         { return _M_emplace(cend(), __uk, std::forward<_Args>(__args)...); }
00695 
00696       // Emplace with hint, useless when keys are unique.
00697       template<typename... _Args>
00698         iterator
00699         _M_emplace(const_iterator, std::true_type __uk, _Args&&... __args)
00700         { return _M_emplace(__uk, std::forward<_Args>(__args)...).first; }
00701 
00702       template<typename... _Args>
00703         iterator
00704         _M_emplace(const_iterator, std::false_type, _Args&&... __args);
00705 
00706       template<typename _Arg, typename _NodeGenerator>
00707         std::pair<iterator, bool>
00708         _M_insert(_Arg&&, const _NodeGenerator&, true_type, size_type = 1);
00709 
00710       template<typename _Arg, typename _NodeGenerator>
00711         iterator
00712         _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
00713                   false_type __uk)
00714         {
00715           return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
00716                            __uk);
00717         }
00718 
00719       // Insert with hint, not used when keys are unique.
00720       template<typename _Arg, typename _NodeGenerator>
00721         iterator
00722         _M_insert(const_iterator, _Arg&& __arg,
00723                   const _NodeGenerator& __node_gen, true_type __uk)
00724         {
00725           return
00726             _M_insert(std::forward<_Arg>(__arg), __node_gen, __uk).first;
00727         }
00728 
00729       // Insert with hint when keys are not unique.
00730       template<typename _Arg, typename _NodeGenerator>
00731         iterator
00732         _M_insert(const_iterator, _Arg&&,
00733                   const _NodeGenerator&, false_type);
00734 
00735       size_type
00736       _M_erase(std::true_type, const key_type&);
00737 
00738       size_type
00739       _M_erase(std::false_type, const key_type&);
00740 
00741       iterator
00742       _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n);
00743 
00744     public:
00745       // Emplace
00746       template<typename... _Args>
00747         __ireturn_type
00748         emplace(_Args&&... __args)
00749         { return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
00750 
00751       template<typename... _Args>
00752         iterator
00753         emplace_hint(const_iterator __hint, _Args&&... __args)
00754         {
00755           return _M_emplace(__hint, __unique_keys(),
00756                             std::forward<_Args>(__args)...);
00757         }
00758 
00759       // Insert member functions via inheritance.
00760 
00761       // Erase
00762       iterator
00763       erase(const_iterator);
00764 
00765       // LWG 2059.
00766       iterator
00767       erase(iterator __it)
00768       { return erase(const_iterator(__it)); }
00769 
00770       size_type
00771       erase(const key_type& __k)
00772       { return _M_erase(__unique_keys(), __k); }
00773 
00774       iterator
00775       erase(const_iterator, const_iterator);
00776 
00777       void
00778       clear() noexcept;
00779 
00780       // Set number of buckets to be appropriate for container of n element.
00781       void rehash(size_type __n);
00782 
00783       // DR 1189.
00784       // reserve, if present, comes from _Rehash_base.
00785 
00786 #if __cplusplus > 201402L
00787       /// Re-insert an extracted node into a container with unique keys.
00788       insert_return_type
00789       _M_reinsert_node(node_type&& __nh)
00790       {
00791         insert_return_type __ret;
00792         if (__nh.empty())
00793           __ret.position = end();
00794         else
00795           {
00796             __glibcxx_assert(get_allocator() == __nh.get_allocator());
00797 
00798             const key_type& __k = __nh._M_key();
00799             __hash_code __code = this->_M_hash_code(__k);
00800             size_type __bkt = _M_bucket_index(__k, __code);
00801             if (__node_type* __n = _M_find_node(__bkt, __k, __code))
00802               {
00803                 __ret.node = std::move(__nh);
00804                 __ret.position = iterator(__n);
00805                 __ret.inserted = false;
00806               }
00807             else
00808               {
00809                 __ret.position
00810                   = _M_insert_unique_node(__bkt, __code, __nh._M_ptr);
00811                 __nh._M_ptr = nullptr;
00812                 __ret.inserted = true;
00813               }
00814           }
00815         return __ret;
00816       }
00817 
00818       /// Re-insert an extracted node into a container with equivalent keys.
00819       iterator
00820       _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
00821       {
00822         iterator __ret;
00823         if (__nh.empty())
00824           __ret = end();
00825         else
00826           {
00827             __glibcxx_assert(get_allocator() == __nh.get_allocator());
00828 
00829             auto __code = this->_M_hash_code(__nh._M_key());
00830             auto __node = std::exchange(__nh._M_ptr, nullptr);
00831             // FIXME: this deallocates the node on exception.
00832             __ret = _M_insert_multi_node(__hint._M_cur, __code, __node);
00833           }
00834         return __ret;
00835       }
00836 
00837       /// Extract a node.
00838       node_type
00839       extract(const_iterator __pos)
00840       {
00841         __node_type* __n = __pos._M_cur;
00842         size_t __bkt = _M_bucket_index(__n);
00843 
00844         // Look for previous node to unlink it from the erased one, this
00845         // is why we need buckets to contain the before begin to make
00846         // this search fast.
00847         __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
00848 
00849         if (__prev_n == _M_buckets[__bkt])
00850           _M_remove_bucket_begin(__bkt, __n->_M_next(),
00851              __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
00852         else if (__n->_M_nxt)
00853           {
00854             size_type __next_bkt = _M_bucket_index(__n->_M_next());
00855             if (__next_bkt != __bkt)
00856               _M_buckets[__next_bkt] = __prev_n;
00857           }
00858 
00859         __prev_n->_M_nxt = __n->_M_nxt;
00860         __n->_M_nxt = nullptr;
00861         --_M_element_count;
00862         return { __n, this->_M_node_allocator() };
00863       }
00864 
00865       /// Extract a node.
00866       node_type
00867       extract(const _Key& __k)
00868       {
00869         node_type __nh;
00870         auto __pos = find(__k);
00871         if (__pos != end())
00872           __nh = extract(const_iterator(__pos));
00873         return __nh;
00874       }
00875 
00876       /// Merge from a compatible container into one with unique keys.
00877       template<typename _Compatible_Hashtable>
00878         void
00879         _M_merge_unique(_Compatible_Hashtable& __src) noexcept
00880         {
00881           static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
00882               node_type>, "Node types are compatible");
00883           __glibcxx_assert(get_allocator() == __src.get_allocator());
00884 
00885           auto __n_elt = __src.size();
00886           for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
00887             {
00888               auto __pos = __i++;
00889               const key_type& __k = this->_M_extract()(__pos._M_cur->_M_v());
00890               __hash_code __code = this->_M_hash_code(__k);
00891               size_type __bkt = _M_bucket_index(__k, __code);
00892               if (_M_find_node(__bkt, __k, __code) == nullptr)
00893                 {
00894                   auto __nh = __src.extract(__pos);
00895                   _M_insert_unique_node(__bkt, __code, __nh._M_ptr, __n_elt);
00896                   __nh._M_ptr = nullptr;
00897                   __n_elt = 1;
00898                 }
00899               else if (__n_elt != 1)
00900                 --__n_elt;
00901             }
00902         }
00903 
00904       /// Merge from a compatible container into one with equivalent keys.
00905       template<typename _Compatible_Hashtable>
00906         void
00907         _M_merge_multi(_Compatible_Hashtable& __src) noexcept
00908         {
00909           static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
00910               node_type>, "Node types are compatible");
00911           __glibcxx_assert(get_allocator() == __src.get_allocator());
00912 
00913           this->reserve(size() + __src.size());
00914           for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
00915             _M_reinsert_node_multi(cend(), __src.extract(__i++));
00916         }
00917 #endif // C++17
00918 
00919     private:
00920       // Helper rehash method used when keys are unique.
00921       void _M_rehash_aux(size_type __n, std::true_type);
00922 
00923       // Helper rehash method used when keys can be non-unique.
00924       void _M_rehash_aux(size_type __n, std::false_type);
00925 
00926       // Unconditionally change size of bucket array to n, restore
00927       // hash policy state to __state on exception.
00928       void _M_rehash(size_type __n, const __rehash_state& __state);
00929     };
00930 
00931 
00932   // Definitions of class template _Hashtable's out-of-line member functions.
00933   template<typename _Key, typename _Value,
00934            typename _Alloc, typename _ExtractKey, typename _Equal,
00935            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00936            typename _Traits>
00937     auto
00938     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00939                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
00940     _M_bucket_begin(size_type __bkt) const
00941     -> __node_type*
00942     {
00943       __node_base* __n = _M_buckets[__bkt];
00944       return __n ? static_cast<__node_type*>(__n->_M_nxt) : nullptr;
00945     }
00946 
00947   template<typename _Key, typename _Value,
00948            typename _Alloc, typename _ExtractKey, typename _Equal,
00949            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00950            typename _Traits>
00951     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00952                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
00953     _Hashtable(size_type __bucket_hint,
00954                const _H1& __h1, const _H2& __h2, const _Hash& __h,
00955                const _Equal& __eq, const _ExtractKey& __exk,
00956                const allocator_type& __a)
00957       : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
00958     {
00959       auto __bkt = _M_rehash_policy._M_next_bkt(__bucket_hint);
00960       if (__bkt > _M_bucket_count)
00961         {
00962           _M_buckets = _M_allocate_buckets(__bkt);
00963           _M_bucket_count = __bkt;
00964         }
00965     }
00966 
00967   template<typename _Key, typename _Value,
00968            typename _Alloc, typename _ExtractKey, typename _Equal,
00969            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00970            typename _Traits>
00971     template<typename _InputIterator>
00972       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
00973                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
00974       _Hashtable(_InputIterator __f, _InputIterator __l,
00975                  size_type __bucket_hint,
00976                  const _H1& __h1, const _H2& __h2, const _Hash& __h,
00977                  const _Equal& __eq, const _ExtractKey& __exk,
00978                  const allocator_type& __a)
00979         : _Hashtable(__h1, __h2, __h, __eq, __exk, __a)
00980       {
00981         auto __nb_elems = __detail::__distance_fw(__f, __l);
00982         auto __bkt_count =
00983           _M_rehash_policy._M_next_bkt(
00984             std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
00985                      __bucket_hint));
00986 
00987         if (__bkt_count > _M_bucket_count)
00988           {
00989             _M_buckets = _M_allocate_buckets(__bkt_count);
00990             _M_bucket_count = __bkt_count;
00991           }
00992 
00993         for (; __f != __l; ++__f)
00994           this->insert(*__f);
00995       }
00996 
00997   template<typename _Key, typename _Value,
00998            typename _Alloc, typename _ExtractKey, typename _Equal,
00999            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01000            typename _Traits>
01001     auto
01002     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01003                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01004     operator=(const _Hashtable& __ht)
01005     -> _Hashtable&
01006     {
01007       if (&__ht == this)
01008         return *this;
01009 
01010       if (__node_alloc_traits::_S_propagate_on_copy_assign())
01011         {
01012           auto& __this_alloc = this->_M_node_allocator();
01013           auto& __that_alloc = __ht._M_node_allocator();
01014           if (!__node_alloc_traits::_S_always_equal()
01015               && __this_alloc != __that_alloc)
01016             {
01017               // Replacement allocator cannot free existing storage.
01018               this->_M_deallocate_nodes(_M_begin());
01019               _M_before_begin._M_nxt = nullptr;
01020               _M_deallocate_buckets();
01021               _M_buckets = nullptr;
01022               std::__alloc_on_copy(__this_alloc, __that_alloc);
01023               __hashtable_base::operator=(__ht);
01024               _M_bucket_count = __ht._M_bucket_count;
01025               _M_element_count = __ht._M_element_count;
01026               _M_rehash_policy = __ht._M_rehash_policy;
01027               __try
01028                 {
01029                   _M_assign(__ht,
01030                             [this](const __node_type* __n)
01031                             { return this->_M_allocate_node(__n->_M_v()); });
01032                 }
01033               __catch(...)
01034                 {
01035                   // _M_assign took care of deallocating all memory. Now we
01036                   // must make sure this instance remains in a usable state.
01037                   _M_reset();
01038                   __throw_exception_again;
01039                 }
01040               return *this;
01041             }
01042           std::__alloc_on_copy(__this_alloc, __that_alloc);
01043         }
01044 
01045       // Reuse allocated buckets and nodes.
01046       _M_assign_elements(__ht,
01047         [](const __reuse_or_alloc_node_type& __roan, const __node_type* __n)
01048         { return __roan(__n->_M_v()); });
01049       return *this;
01050     }
01051 
01052   template<typename _Key, typename _Value,
01053            typename _Alloc, typename _ExtractKey, typename _Equal,
01054            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01055            typename _Traits>
01056     template<typename _Ht, typename _NodeGenerator>
01057       void
01058       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01059                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01060       _M_assign_elements(_Ht&& __ht, const _NodeGenerator& __node_gen)
01061       {
01062         __bucket_type* __former_buckets = nullptr;
01063         std::size_t __former_bucket_count = _M_bucket_count;
01064         const __rehash_state& __former_state = _M_rehash_policy._M_state();
01065 
01066         if (_M_bucket_count != __ht._M_bucket_count)
01067           {
01068             __former_buckets = _M_buckets;
01069             _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
01070             _M_bucket_count = __ht._M_bucket_count;
01071           }
01072         else
01073           __builtin_memset(_M_buckets, 0,
01074                            _M_bucket_count * sizeof(__bucket_type));
01075 
01076         __try
01077           {
01078             __hashtable_base::operator=(std::forward<_Ht>(__ht));
01079             _M_element_count = __ht._M_element_count;
01080             _M_rehash_policy = __ht._M_rehash_policy;
01081             __reuse_or_alloc_node_type __roan(_M_begin(), *this);
01082             _M_before_begin._M_nxt = nullptr;
01083             _M_assign(__ht,
01084                       [&__node_gen, &__roan](__node_type* __n)
01085                       { return __node_gen(__roan, __n); });
01086             if (__former_buckets)
01087               _M_deallocate_buckets(__former_buckets, __former_bucket_count);
01088           }
01089         __catch(...)
01090           {
01091             if (__former_buckets)
01092               {
01093                 // Restore previous buckets.
01094                 _M_deallocate_buckets();
01095                 _M_rehash_policy._M_reset(__former_state);
01096                 _M_buckets = __former_buckets;
01097                 _M_bucket_count = __former_bucket_count;
01098               }
01099             __builtin_memset(_M_buckets, 0,
01100                              _M_bucket_count * sizeof(__bucket_type));
01101             __throw_exception_again;
01102           }
01103       }
01104 
01105   template<typename _Key, typename _Value,
01106            typename _Alloc, typename _ExtractKey, typename _Equal,
01107            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01108            typename _Traits>
01109     template<typename _NodeGenerator>
01110       void
01111       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01112                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01113       _M_assign(const _Hashtable& __ht, const _NodeGenerator& __node_gen)
01114       {
01115         __bucket_type* __buckets = nullptr;
01116         if (!_M_buckets)
01117           _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
01118 
01119         __try
01120           {
01121             if (!__ht._M_before_begin._M_nxt)
01122               return;
01123 
01124             // First deal with the special first node pointed to by
01125             // _M_before_begin.
01126             __node_type* __ht_n = __ht._M_begin();
01127             __node_type* __this_n = __node_gen(__ht_n);
01128             this->_M_copy_code(__this_n, __ht_n);
01129             _M_before_begin._M_nxt = __this_n;
01130             _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
01131 
01132             // Then deal with other nodes.
01133             __node_base* __prev_n = __this_n;
01134             for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
01135               {
01136                 __this_n = __node_gen(__ht_n);
01137                 __prev_n->_M_nxt = __this_n;
01138                 this->_M_copy_code(__this_n, __ht_n);
01139                 size_type __bkt = _M_bucket_index(__this_n);
01140                 if (!_M_buckets[__bkt])
01141                   _M_buckets[__bkt] = __prev_n;
01142                 __prev_n = __this_n;
01143               }
01144           }
01145         __catch(...)
01146           {
01147             clear();
01148             if (__buckets)
01149               _M_deallocate_buckets();
01150             __throw_exception_again;
01151           }
01152       }
01153 
01154   template<typename _Key, typename _Value,
01155            typename _Alloc, typename _ExtractKey, typename _Equal,
01156            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01157            typename _Traits>
01158     void
01159     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01160                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01161     _M_reset() noexcept
01162     {
01163       _M_rehash_policy._M_reset();
01164       _M_bucket_count = 1;
01165       _M_single_bucket = nullptr;
01166       _M_buckets = &_M_single_bucket;
01167       _M_before_begin._M_nxt = nullptr;
01168       _M_element_count = 0;
01169     }
01170 
01171   template<typename _Key, typename _Value,
01172            typename _Alloc, typename _ExtractKey, typename _Equal,
01173            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01174            typename _Traits>
01175     void
01176     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01177                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01178     _M_move_assign(_Hashtable&& __ht, std::true_type)
01179     {
01180       this->_M_deallocate_nodes(_M_begin());
01181       _M_deallocate_buckets();
01182       __hashtable_base::operator=(std::move(__ht));
01183       _M_rehash_policy = __ht._M_rehash_policy;
01184       if (!__ht._M_uses_single_bucket())
01185         _M_buckets = __ht._M_buckets;
01186       else
01187         {
01188           _M_buckets = &_M_single_bucket;
01189           _M_single_bucket = __ht._M_single_bucket;
01190         }
01191       _M_bucket_count = __ht._M_bucket_count;
01192       _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
01193       _M_element_count = __ht._M_element_count;
01194       std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
01195 
01196       // Fix buckets containing the _M_before_begin pointers that can't be
01197       // moved.
01198       if (_M_begin())
01199         _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
01200       __ht._M_reset();
01201     }
01202 
01203   template<typename _Key, typename _Value,
01204            typename _Alloc, typename _ExtractKey, typename _Equal,
01205            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01206            typename _Traits>
01207     void
01208     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01209                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01210     _M_move_assign(_Hashtable&& __ht, std::false_type)
01211     {
01212       if (__ht._M_node_allocator() == this->_M_node_allocator())
01213         _M_move_assign(std::move(__ht), std::true_type());
01214       else
01215         {
01216           // Can't move memory, move elements then.
01217           _M_assign_elements(std::move(__ht),
01218                 [](const __reuse_or_alloc_node_type& __roan, __node_type* __n)
01219                 { return __roan(std::move_if_noexcept(__n->_M_v())); });
01220           __ht.clear();
01221         }
01222     }
01223 
01224   template<typename _Key, typename _Value,
01225            typename _Alloc, typename _ExtractKey, typename _Equal,
01226            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01227            typename _Traits>
01228     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01229                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01230     _Hashtable(const _Hashtable& __ht)
01231     : __hashtable_base(__ht),
01232       __map_base(__ht),
01233       __rehash_base(__ht),
01234       __hashtable_alloc(
01235         __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
01236       _M_buckets(nullptr),
01237       _M_bucket_count(__ht._M_bucket_count),
01238       _M_element_count(__ht._M_element_count),
01239       _M_rehash_policy(__ht._M_rehash_policy)
01240     {
01241       _M_assign(__ht,
01242                 [this](const __node_type* __n)
01243                 { return this->_M_allocate_node(__n->_M_v()); });
01244     }
01245 
01246   template<typename _Key, typename _Value,
01247            typename _Alloc, typename _ExtractKey, typename _Equal,
01248            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01249            typename _Traits>
01250     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01251                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01252     _Hashtable(_Hashtable&& __ht) noexcept
01253     : __hashtable_base(__ht),
01254       __map_base(__ht),
01255       __rehash_base(__ht),
01256       __hashtable_alloc(std::move(__ht._M_base_alloc())),
01257       _M_buckets(__ht._M_buckets),
01258       _M_bucket_count(__ht._M_bucket_count),
01259       _M_before_begin(__ht._M_before_begin._M_nxt),
01260       _M_element_count(__ht._M_element_count),
01261       _M_rehash_policy(__ht._M_rehash_policy)
01262     {
01263       // Update, if necessary, buckets if __ht is using its single bucket.
01264       if (__ht._M_uses_single_bucket())
01265         {
01266           _M_buckets = &_M_single_bucket;
01267           _M_single_bucket = __ht._M_single_bucket;
01268         }
01269 
01270       // Update, if necessary, bucket pointing to before begin that hasn't
01271       // moved.
01272       if (_M_begin())
01273         _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
01274 
01275       __ht._M_reset();
01276     }
01277 
01278   template<typename _Key, typename _Value,
01279            typename _Alloc, typename _ExtractKey, typename _Equal,
01280            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01281            typename _Traits>
01282     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01283                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01284     _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
01285     : __hashtable_base(__ht),
01286       __map_base(__ht),
01287       __rehash_base(__ht),
01288       __hashtable_alloc(__node_alloc_type(__a)),
01289       _M_buckets(),
01290       _M_bucket_count(__ht._M_bucket_count),
01291       _M_element_count(__ht._M_element_count),
01292       _M_rehash_policy(__ht._M_rehash_policy)
01293     {
01294       _M_assign(__ht,
01295                 [this](const __node_type* __n)
01296                 { return this->_M_allocate_node(__n->_M_v()); });
01297     }
01298 
01299   template<typename _Key, typename _Value,
01300            typename _Alloc, typename _ExtractKey, typename _Equal,
01301            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01302            typename _Traits>
01303     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01304                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01305     _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
01306     : __hashtable_base(__ht),
01307       __map_base(__ht),
01308       __rehash_base(__ht),
01309       __hashtable_alloc(__node_alloc_type(__a)),
01310       _M_buckets(nullptr),
01311       _M_bucket_count(__ht._M_bucket_count),
01312       _M_element_count(__ht._M_element_count),
01313       _M_rehash_policy(__ht._M_rehash_policy)
01314     {
01315       if (__ht._M_node_allocator() == this->_M_node_allocator())
01316         {
01317           if (__ht._M_uses_single_bucket())
01318             {
01319               _M_buckets = &_M_single_bucket;
01320               _M_single_bucket = __ht._M_single_bucket;
01321             }
01322           else
01323             _M_buckets = __ht._M_buckets;
01324 
01325           _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
01326           // Update, if necessary, bucket pointing to before begin that hasn't
01327           // moved.
01328           if (_M_begin())
01329             _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
01330           __ht._M_reset();
01331         }
01332       else
01333         {
01334           _M_assign(__ht,
01335                     [this](__node_type* __n)
01336                     {
01337                       return this->_M_allocate_node(
01338                                         std::move_if_noexcept(__n->_M_v()));
01339                     });
01340           __ht.clear();
01341         }
01342     }
01343 
01344   template<typename _Key, typename _Value,
01345            typename _Alloc, typename _ExtractKey, typename _Equal,
01346            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01347            typename _Traits>
01348     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01349                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01350     ~_Hashtable() noexcept
01351     {
01352       clear();
01353       _M_deallocate_buckets();
01354     }
01355 
01356   template<typename _Key, typename _Value,
01357            typename _Alloc, typename _ExtractKey, typename _Equal,
01358            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01359            typename _Traits>
01360     void
01361     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01362                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01363     swap(_Hashtable& __x)
01364     noexcept(__and_<__is_nothrow_swappable<_H1>,
01365                         __is_nothrow_swappable<_Equal>>::value)
01366     {
01367       // The only base class with member variables is hash_code_base.
01368       // We define _Hash_code_base::_M_swap because different
01369       // specializations have different members.
01370       this->_M_swap(__x);
01371 
01372       std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
01373       std::swap(_M_rehash_policy, __x._M_rehash_policy);
01374 
01375       // Deal properly with potentially moved instances.
01376       if (this->_M_uses_single_bucket())
01377         {
01378           if (!__x._M_uses_single_bucket())
01379             {
01380               _M_buckets = __x._M_buckets;
01381               __x._M_buckets = &__x._M_single_bucket;
01382             }
01383         }
01384       else if (__x._M_uses_single_bucket())
01385         {
01386           __x._M_buckets = _M_buckets;
01387           _M_buckets = &_M_single_bucket;
01388         }       
01389       else
01390         std::swap(_M_buckets, __x._M_buckets);
01391 
01392       std::swap(_M_bucket_count, __x._M_bucket_count);
01393       std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
01394       std::swap(_M_element_count, __x._M_element_count);
01395       std::swap(_M_single_bucket, __x._M_single_bucket);
01396 
01397       // Fix buckets containing the _M_before_begin pointers that can't be
01398       // swapped.
01399       if (_M_begin())
01400         _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
01401 
01402       if (__x._M_begin())
01403         __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
01404           = &__x._M_before_begin;
01405     }
01406 
01407   template<typename _Key, typename _Value,
01408            typename _Alloc, typename _ExtractKey, typename _Equal,
01409            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01410            typename _Traits>
01411     auto
01412     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01413                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01414     find(const key_type& __k)
01415     -> iterator
01416     {
01417       __hash_code __code = this->_M_hash_code(__k);
01418       std::size_t __n = _M_bucket_index(__k, __code);
01419       __node_type* __p = _M_find_node(__n, __k, __code);
01420       return __p ? iterator(__p) : end();
01421     }
01422 
01423   template<typename _Key, typename _Value,
01424            typename _Alloc, typename _ExtractKey, typename _Equal,
01425            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01426            typename _Traits>
01427     auto
01428     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01429                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01430     find(const key_type& __k) const
01431     -> const_iterator
01432     {
01433       __hash_code __code = this->_M_hash_code(__k);
01434       std::size_t __n = _M_bucket_index(__k, __code);
01435       __node_type* __p = _M_find_node(__n, __k, __code);
01436       return __p ? const_iterator(__p) : end();
01437     }
01438 
01439   template<typename _Key, typename _Value,
01440            typename _Alloc, typename _ExtractKey, typename _Equal,
01441            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01442            typename _Traits>
01443     auto
01444     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01445                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01446     count(const key_type& __k) const
01447     -> size_type
01448     {
01449       __hash_code __code = this->_M_hash_code(__k);
01450       std::size_t __n = _M_bucket_index(__k, __code);
01451       __node_type* __p = _M_bucket_begin(__n);
01452       if (!__p)
01453         return 0;
01454 
01455       std::size_t __result = 0;
01456       for (;; __p = __p->_M_next())
01457         {
01458           if (this->_M_equals(__k, __code, __p))
01459             ++__result;
01460           else if (__result)
01461             // All equivalent values are next to each other, if we
01462             // found a non-equivalent value after an equivalent one it
01463             // means that we won't find any new equivalent value.
01464             break;
01465           if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
01466             break;
01467         }
01468       return __result;
01469     }
01470 
01471   template<typename _Key, typename _Value,
01472            typename _Alloc, typename _ExtractKey, typename _Equal,
01473            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01474            typename _Traits>
01475     auto
01476     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01477                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01478     equal_range(const key_type& __k)
01479     -> pair<iterator, iterator>
01480     {
01481       __hash_code __code = this->_M_hash_code(__k);
01482       std::size_t __n = _M_bucket_index(__k, __code);
01483       __node_type* __p = _M_find_node(__n, __k, __code);
01484 
01485       if (__p)
01486         {
01487           __node_type* __p1 = __p->_M_next();
01488           while (__p1 && _M_bucket_index(__p1) == __n
01489                  && this->_M_equals(__k, __code, __p1))
01490             __p1 = __p1->_M_next();
01491 
01492           return std::make_pair(iterator(__p), iterator(__p1));
01493         }
01494       else
01495         return std::make_pair(end(), end());
01496     }
01497 
01498   template<typename _Key, typename _Value,
01499            typename _Alloc, typename _ExtractKey, typename _Equal,
01500            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01501            typename _Traits>
01502     auto
01503     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01504                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01505     equal_range(const key_type& __k) const
01506     -> pair<const_iterator, const_iterator>
01507     {
01508       __hash_code __code = this->_M_hash_code(__k);
01509       std::size_t __n = _M_bucket_index(__k, __code);
01510       __node_type* __p = _M_find_node(__n, __k, __code);
01511 
01512       if (__p)
01513         {
01514           __node_type* __p1 = __p->_M_next();
01515           while (__p1 && _M_bucket_index(__p1) == __n
01516                  && this->_M_equals(__k, __code, __p1))
01517             __p1 = __p1->_M_next();
01518 
01519           return std::make_pair(const_iterator(__p), const_iterator(__p1));
01520         }
01521       else
01522         return std::make_pair(end(), end());
01523     }
01524 
01525   // Find the node whose key compares equal to k in the bucket n.
01526   // Return nullptr if no node is found.
01527   template<typename _Key, typename _Value,
01528            typename _Alloc, typename _ExtractKey, typename _Equal,
01529            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01530            typename _Traits>
01531     auto
01532     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01533                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01534     _M_find_before_node(size_type __n, const key_type& __k,
01535                         __hash_code __code) const
01536     -> __node_base*
01537     {
01538       __node_base* __prev_p = _M_buckets[__n];
01539       if (!__prev_p)
01540         return nullptr;
01541 
01542       for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
01543            __p = __p->_M_next())
01544         {
01545           if (this->_M_equals(__k, __code, __p))
01546             return __prev_p;
01547 
01548           if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
01549             break;
01550           __prev_p = __p;
01551         }
01552       return nullptr;
01553     }
01554 
01555   template<typename _Key, typename _Value,
01556            typename _Alloc, typename _ExtractKey, typename _Equal,
01557            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01558            typename _Traits>
01559     void
01560     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01561                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01562     _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
01563     {
01564       if (_M_buckets[__bkt])
01565         {
01566           // Bucket is not empty, we just need to insert the new node
01567           // after the bucket before begin.
01568           __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
01569           _M_buckets[__bkt]->_M_nxt = __node;
01570         }
01571       else
01572         {
01573           // The bucket is empty, the new node is inserted at the
01574           // beginning of the singly-linked list and the bucket will
01575           // contain _M_before_begin pointer.
01576           __node->_M_nxt = _M_before_begin._M_nxt;
01577           _M_before_begin._M_nxt = __node;
01578           if (__node->_M_nxt)
01579             // We must update former begin bucket that is pointing to
01580             // _M_before_begin.
01581             _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
01582           _M_buckets[__bkt] = &_M_before_begin;
01583         }
01584     }
01585 
01586   template<typename _Key, typename _Value,
01587            typename _Alloc, typename _ExtractKey, typename _Equal,
01588            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01589            typename _Traits>
01590     void
01591     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01592                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01593     _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
01594                            size_type __next_bkt)
01595     {
01596       if (!__next || __next_bkt != __bkt)
01597         {
01598           // Bucket is now empty
01599           // First update next bucket if any
01600           if (__next)
01601             _M_buckets[__next_bkt] = _M_buckets[__bkt];
01602 
01603           // Second update before begin node if necessary
01604           if (&_M_before_begin == _M_buckets[__bkt])
01605             _M_before_begin._M_nxt = __next;
01606           _M_buckets[__bkt] = nullptr;
01607         }
01608     }
01609 
01610   template<typename _Key, typename _Value,
01611            typename _Alloc, typename _ExtractKey, typename _Equal,
01612            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01613            typename _Traits>
01614     auto
01615     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01616                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01617     _M_get_previous_node(size_type __bkt, __node_base* __n)
01618     -> __node_base*
01619     {
01620       __node_base* __prev_n = _M_buckets[__bkt];
01621       while (__prev_n->_M_nxt != __n)
01622         __prev_n = __prev_n->_M_nxt;
01623       return __prev_n;
01624     }
01625 
01626   template<typename _Key, typename _Value,
01627            typename _Alloc, typename _ExtractKey, typename _Equal,
01628            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01629            typename _Traits>
01630     template<typename... _Args>
01631       auto
01632       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01633                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01634       _M_emplace(std::true_type, _Args&&... __args)
01635       -> pair<iterator, bool>
01636       {
01637         // First build the node to get access to the hash code
01638         __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
01639         const key_type& __k = this->_M_extract()(__node->_M_v());
01640         __hash_code __code;
01641         __try
01642           {
01643             __code = this->_M_hash_code(__k);
01644           }
01645         __catch(...)
01646           {
01647             this->_M_deallocate_node(__node);
01648             __throw_exception_again;
01649           }
01650 
01651         size_type __bkt = _M_bucket_index(__k, __code);
01652         if (__node_type* __p = _M_find_node(__bkt, __k, __code))
01653           {
01654             // There is already an equivalent node, no insertion
01655             this->_M_deallocate_node(__node);
01656             return std::make_pair(iterator(__p), false);
01657           }
01658 
01659         // Insert the node
01660         return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
01661                               true);
01662       }
01663 
01664   template<typename _Key, typename _Value,
01665            typename _Alloc, typename _ExtractKey, typename _Equal,
01666            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01667            typename _Traits>
01668     template<typename... _Args>
01669       auto
01670       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01671                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01672       _M_emplace(const_iterator __hint, std::false_type, _Args&&... __args)
01673       -> iterator
01674       {
01675         // First build the node to get its hash code.
01676         __node_type* __node =
01677           this->_M_allocate_node(std::forward<_Args>(__args)...);
01678 
01679         __hash_code __code;
01680         __try
01681           {
01682             __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
01683           }
01684         __catch(...)
01685           {
01686             this->_M_deallocate_node(__node);
01687             __throw_exception_again;
01688           }
01689 
01690         return _M_insert_multi_node(__hint._M_cur, __code, __node);
01691       }
01692 
01693   template<typename _Key, typename _Value,
01694            typename _Alloc, typename _ExtractKey, typename _Equal,
01695            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01696            typename _Traits>
01697     auto
01698     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01699                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01700     _M_insert_unique_node(size_type __bkt, __hash_code __code,
01701                           __node_type* __node, size_type __n_elt)
01702     -> iterator
01703     {
01704       const __rehash_state& __saved_state = _M_rehash_policy._M_state();
01705       std::pair<bool, std::size_t> __do_rehash
01706         = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
01707                                           __n_elt);
01708 
01709       __try
01710         {
01711           if (__do_rehash.first)
01712             {
01713               _M_rehash(__do_rehash.second, __saved_state);
01714               __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
01715             }
01716 
01717           this->_M_store_code(__node, __code);
01718 
01719           // Always insert at the beginning of the bucket.
01720           _M_insert_bucket_begin(__bkt, __node);
01721           ++_M_element_count;
01722           return iterator(__node);
01723         }
01724       __catch(...)
01725         {
01726           this->_M_deallocate_node(__node);
01727           __throw_exception_again;
01728         }
01729     }
01730 
01731   // Insert node, in bucket bkt if no rehash (assumes no element with its key
01732   // already present). Take ownership of the node, deallocate it on exception.
01733   template<typename _Key, typename _Value,
01734            typename _Alloc, typename _ExtractKey, typename _Equal,
01735            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01736            typename _Traits>
01737     auto
01738     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01739                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01740     _M_insert_multi_node(__node_type* __hint, __hash_code __code,
01741                          __node_type* __node)
01742     -> iterator
01743     {
01744       const __rehash_state& __saved_state = _M_rehash_policy._M_state();
01745       std::pair<bool, std::size_t> __do_rehash
01746         = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
01747 
01748       __try
01749         {
01750           if (__do_rehash.first)
01751             _M_rehash(__do_rehash.second, __saved_state);
01752 
01753           this->_M_store_code(__node, __code);
01754           const key_type& __k = this->_M_extract()(__node->_M_v());
01755           size_type __bkt = _M_bucket_index(__k, __code);
01756 
01757           // Find the node before an equivalent one or use hint if it exists and
01758           // if it is equivalent.
01759           __node_base* __prev
01760             = __builtin_expect(__hint != nullptr, false)
01761               && this->_M_equals(__k, __code, __hint)
01762                 ? __hint
01763                 : _M_find_before_node(__bkt, __k, __code);
01764           if (__prev)
01765             {
01766               // Insert after the node before the equivalent one.
01767               __node->_M_nxt = __prev->_M_nxt;
01768               __prev->_M_nxt = __node;
01769               if (__builtin_expect(__prev == __hint, false))
01770                 // hint might be the last bucket node, in this case we need to
01771                 // update next bucket.
01772                 if (__node->_M_nxt
01773                     && !this->_M_equals(__k, __code, __node->_M_next()))
01774                   {
01775                     size_type __next_bkt = _M_bucket_index(__node->_M_next());
01776                     if (__next_bkt != __bkt)
01777                       _M_buckets[__next_bkt] = __node;
01778                   }
01779             }
01780           else
01781             // The inserted node has no equivalent in the
01782             // hashtable. We must insert the new node at the
01783             // beginning of the bucket to preserve equivalent
01784             // elements' relative positions.
01785             _M_insert_bucket_begin(__bkt, __node);
01786           ++_M_element_count;
01787           return iterator(__node);
01788         }
01789       __catch(...)
01790         {
01791           this->_M_deallocate_node(__node);
01792           __throw_exception_again;
01793         }
01794     }
01795 
01796   // Insert v if no element with its key is already present.
01797   template<typename _Key, typename _Value,
01798            typename _Alloc, typename _ExtractKey, typename _Equal,
01799            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01800            typename _Traits>
01801     template<typename _Arg, typename _NodeGenerator>
01802       auto
01803       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01804                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01805       _M_insert(_Arg&& __v, const _NodeGenerator& __node_gen, true_type,
01806                 size_type __n_elt)
01807       -> pair<iterator, bool>
01808       {
01809         const key_type& __k = this->_M_extract()(__v);
01810         __hash_code __code = this->_M_hash_code(__k);
01811         size_type __bkt = _M_bucket_index(__k, __code);
01812 
01813         __node_type* __n = _M_find_node(__bkt, __k, __code);
01814         if (__n)
01815           return std::make_pair(iterator(__n), false);
01816 
01817         __n = __node_gen(std::forward<_Arg>(__v));
01818         return { _M_insert_unique_node(__bkt, __code, __n, __n_elt), true };
01819       }
01820 
01821   // Insert v unconditionally.
01822   template<typename _Key, typename _Value,
01823            typename _Alloc, typename _ExtractKey, typename _Equal,
01824            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01825            typename _Traits>
01826     template<typename _Arg, typename _NodeGenerator>
01827       auto
01828       _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01829                  _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01830       _M_insert(const_iterator __hint, _Arg&& __v,
01831                 const _NodeGenerator& __node_gen, false_type)
01832       -> iterator
01833       {
01834         // First compute the hash code so that we don't do anything if it
01835         // throws.
01836         __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
01837 
01838         // Second allocate new node so that we don't rehash if it throws.
01839         __node_type* __node = __node_gen(std::forward<_Arg>(__v));
01840 
01841         return _M_insert_multi_node(__hint._M_cur, __code, __node);
01842       }
01843 
01844   template<typename _Key, typename _Value,
01845            typename _Alloc, typename _ExtractKey, typename _Equal,
01846            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01847            typename _Traits>
01848     auto
01849     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01850                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01851     erase(const_iterator __it)
01852     -> iterator
01853     {
01854       __node_type* __n = __it._M_cur;
01855       std::size_t __bkt = _M_bucket_index(__n);
01856 
01857       // Look for previous node to unlink it from the erased one, this
01858       // is why we need buckets to contain the before begin to make
01859       // this search fast.
01860       __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
01861       return _M_erase(__bkt, __prev_n, __n);
01862     }
01863 
01864   template<typename _Key, typename _Value,
01865            typename _Alloc, typename _ExtractKey, typename _Equal,
01866            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01867            typename _Traits>
01868     auto
01869     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01870                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01871     _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
01872     -> iterator
01873     {
01874       if (__prev_n == _M_buckets[__bkt])
01875         _M_remove_bucket_begin(__bkt, __n->_M_next(),
01876            __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
01877       else if (__n->_M_nxt)
01878         {
01879           size_type __next_bkt = _M_bucket_index(__n->_M_next());
01880           if (__next_bkt != __bkt)
01881             _M_buckets[__next_bkt] = __prev_n;
01882         }
01883 
01884       __prev_n->_M_nxt = __n->_M_nxt;
01885       iterator __result(__n->_M_next());
01886       this->_M_deallocate_node(__n);
01887       --_M_element_count;
01888 
01889       return __result;
01890     }
01891 
01892   template<typename _Key, typename _Value,
01893            typename _Alloc, typename _ExtractKey, typename _Equal,
01894            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01895            typename _Traits>
01896     auto
01897     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01898                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01899     _M_erase(std::true_type, const key_type& __k)
01900     -> size_type
01901     {
01902       __hash_code __code = this->_M_hash_code(__k);
01903       std::size_t __bkt = _M_bucket_index(__k, __code);
01904 
01905       // Look for the node before the first matching node.
01906       __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
01907       if (!__prev_n)
01908         return 0;
01909 
01910       // We found a matching node, erase it.
01911       __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
01912       _M_erase(__bkt, __prev_n, __n);
01913       return 1;
01914     }
01915 
01916   template<typename _Key, typename _Value,
01917            typename _Alloc, typename _ExtractKey, typename _Equal,
01918            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01919            typename _Traits>
01920     auto
01921     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01922                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01923     _M_erase(std::false_type, const key_type& __k)
01924     -> size_type
01925     {
01926       __hash_code __code = this->_M_hash_code(__k);
01927       std::size_t __bkt = _M_bucket_index(__k, __code);
01928 
01929       // Look for the node before the first matching node.
01930       __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
01931       if (!__prev_n)
01932         return 0;
01933 
01934       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01935       // 526. Is it undefined if a function in the standard changes
01936       // in parameters?
01937       // We use one loop to find all matching nodes and another to deallocate
01938       // them so that the key stays valid during the first loop. It might be
01939       // invalidated indirectly when destroying nodes.
01940       __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
01941       __node_type* __n_last = __n;
01942       std::size_t __n_last_bkt = __bkt;
01943       do
01944         {
01945           __n_last = __n_last->_M_next();
01946           if (!__n_last)
01947             break;
01948           __n_last_bkt = _M_bucket_index(__n_last);
01949         }
01950       while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
01951 
01952       // Deallocate nodes.
01953       size_type __result = 0;
01954       do
01955         {
01956           __node_type* __p = __n->_M_next();
01957           this->_M_deallocate_node(__n);
01958           __n = __p;
01959           ++__result;
01960           --_M_element_count;
01961         }
01962       while (__n != __n_last);
01963 
01964       if (__prev_n == _M_buckets[__bkt])
01965         _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
01966       else if (__n_last && __n_last_bkt != __bkt)
01967         _M_buckets[__n_last_bkt] = __prev_n;
01968       __prev_n->_M_nxt = __n_last;
01969       return __result;
01970     }
01971 
01972   template<typename _Key, typename _Value,
01973            typename _Alloc, typename _ExtractKey, typename _Equal,
01974            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01975            typename _Traits>
01976     auto
01977     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
01978                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
01979     erase(const_iterator __first, const_iterator __last)
01980     -> iterator
01981     {
01982       __node_type* __n = __first._M_cur;
01983       __node_type* __last_n = __last._M_cur;
01984       if (__n == __last_n)
01985         return iterator(__n);
01986 
01987       std::size_t __bkt = _M_bucket_index(__n);
01988 
01989       __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
01990       bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
01991       std::size_t __n_bkt = __bkt;
01992       for (;;)
01993         {
01994           do
01995             {
01996               __node_type* __tmp = __n;
01997               __n = __n->_M_next();
01998               this->_M_deallocate_node(__tmp);
01999               --_M_element_count;
02000               if (!__n)
02001                 break;
02002               __n_bkt = _M_bucket_index(__n);
02003             }
02004           while (__n != __last_n && __n_bkt == __bkt);
02005           if (__is_bucket_begin)
02006             _M_remove_bucket_begin(__bkt, __n, __n_bkt);
02007           if (__n == __last_n)
02008             break;
02009           __is_bucket_begin = true;
02010           __bkt = __n_bkt;
02011         }
02012 
02013       if (__n && (__n_bkt != __bkt || __is_bucket_begin))
02014         _M_buckets[__n_bkt] = __prev_n;
02015       __prev_n->_M_nxt = __n;
02016       return iterator(__n);
02017     }
02018 
02019   template<typename _Key, typename _Value,
02020            typename _Alloc, typename _ExtractKey, typename _Equal,
02021            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
02022            typename _Traits>
02023     void
02024     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
02025                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
02026     clear() noexcept
02027     {
02028       this->_M_deallocate_nodes(_M_begin());
02029       __builtin_memset(_M_buckets, 0, _M_bucket_count * sizeof(__bucket_type));
02030       _M_element_count = 0;
02031       _M_before_begin._M_nxt = nullptr;
02032     }
02033 
02034   template<typename _Key, typename _Value,
02035            typename _Alloc, typename _ExtractKey, typename _Equal,
02036            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
02037            typename _Traits>
02038     void
02039     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
02040                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
02041     rehash(size_type __n)
02042     {
02043       const __rehash_state& __saved_state = _M_rehash_policy._M_state();
02044       std::size_t __buckets
02045         = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
02046                    __n);
02047       __buckets = _M_rehash_policy._M_next_bkt(__buckets);
02048 
02049       if (__buckets != _M_bucket_count)
02050         _M_rehash(__buckets, __saved_state);
02051       else
02052         // No rehash, restore previous state to keep a consistent state.
02053         _M_rehash_policy._M_reset(__saved_state);
02054     }
02055 
02056   template<typename _Key, typename _Value,
02057            typename _Alloc, typename _ExtractKey, typename _Equal,
02058            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
02059            typename _Traits>
02060     void
02061     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
02062                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
02063     _M_rehash(size_type __n, const __rehash_state& __state)
02064     {
02065       __try
02066         {
02067           _M_rehash_aux(__n, __unique_keys());
02068         }
02069       __catch(...)
02070         {
02071           // A failure here means that buckets allocation failed.  We only
02072           // have to restore hash policy previous state.
02073           _M_rehash_policy._M_reset(__state);
02074           __throw_exception_again;
02075         }
02076     }
02077 
02078   // Rehash when there is no equivalent elements.
02079   template<typename _Key, typename _Value,
02080            typename _Alloc, typename _ExtractKey, typename _Equal,
02081            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
02082            typename _Traits>
02083     void
02084     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
02085                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
02086     _M_rehash_aux(size_type __n, std::true_type)
02087     {
02088       __bucket_type* __new_buckets = _M_allocate_buckets(__n);
02089       __node_type* __p = _M_begin();
02090       _M_before_begin._M_nxt = nullptr;
02091       std::size_t __bbegin_bkt = 0;
02092       while (__p)
02093         {
02094           __node_type* __next = __p->_M_next();
02095           std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
02096           if (!__new_buckets[__bkt])
02097             {
02098               __p->_M_nxt = _M_before_begin._M_nxt;
02099               _M_before_begin._M_nxt = __p;
02100               __new_buckets[__bkt] = &_M_before_begin;
02101               if (__p->_M_nxt)
02102                 __new_buckets[__bbegin_bkt] = __p;
02103               __bbegin_bkt = __bkt;
02104             }
02105           else
02106             {
02107               __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
02108               __new_buckets[__bkt]->_M_nxt = __p;
02109             }
02110           __p = __next;
02111         }
02112 
02113       _M_deallocate_buckets();
02114       _M_bucket_count = __n;
02115       _M_buckets = __new_buckets;
02116     }
02117 
02118   // Rehash when there can be equivalent elements, preserve their relative
02119   // order.
02120   template<typename _Key, typename _Value,
02121            typename _Alloc, typename _ExtractKey, typename _Equal,
02122            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
02123            typename _Traits>
02124     void
02125     _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
02126                _H1, _H2, _Hash, _RehashPolicy, _Traits>::
02127     _M_rehash_aux(size_type __n, std::false_type)
02128     {
02129       __bucket_type* __new_buckets = _M_allocate_buckets(__n);
02130 
02131       __node_type* __p = _M_begin();
02132       _M_before_begin._M_nxt = nullptr;
02133       std::size_t __bbegin_bkt = 0;
02134       std::size_t __prev_bkt = 0;
02135       __node_type* __prev_p = nullptr;
02136       bool __check_bucket = false;
02137 
02138       while (__p)
02139         {
02140           __node_type* __next = __p->_M_next();
02141           std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
02142 
02143           if (__prev_p && __prev_bkt == __bkt)
02144             {
02145               // Previous insert was already in this bucket, we insert after
02146               // the previously inserted one to preserve equivalent elements
02147               // relative order.
02148               __p->_M_nxt = __prev_p->_M_nxt;
02149               __prev_p->_M_nxt = __p;
02150 
02151               // Inserting after a node in a bucket require to check that we
02152               // haven't change the bucket last node, in this case next
02153               // bucket containing its before begin node must be updated. We
02154               // schedule a check as soon as we move out of the sequence of
02155               // equivalent nodes to limit the number of checks.
02156               __check_bucket = true;
02157             }
02158           else
02159             {
02160               if (__check_bucket)
02161                 {
02162                   // Check if we shall update the next bucket because of
02163                   // insertions into __prev_bkt bucket.
02164                   if (__prev_p->_M_nxt)
02165                     {
02166                       std::size_t __next_bkt
02167                         = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
02168                                                             __n);
02169                       if (__next_bkt != __prev_bkt)
02170                         __new_buckets[__next_bkt] = __prev_p;
02171                     }
02172                   __check_bucket = false;
02173                 }
02174 
02175               if (!__new_buckets[__bkt])
02176                 {
02177                   __p->_M_nxt = _M_before_begin._M_nxt;
02178                   _M_before_begin._M_nxt = __p;
02179                   __new_buckets[__bkt] = &_M_before_begin;
02180                   if (__p->_M_nxt)
02181                     __new_buckets[__bbegin_bkt] = __p;
02182                   __bbegin_bkt = __bkt;
02183                 }
02184               else
02185                 {
02186                   __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
02187                   __new_buckets[__bkt]->_M_nxt = __p;
02188                 }
02189             }
02190           __prev_p = __p;
02191           __prev_bkt = __bkt;
02192           __p = __next;
02193         }
02194 
02195       if (__check_bucket && __prev_p->_M_nxt)
02196         {
02197           std::size_t __next_bkt
02198             = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
02199           if (__next_bkt != __prev_bkt)
02200             __new_buckets[__next_bkt] = __prev_p;
02201         }
02202 
02203       _M_deallocate_buckets();
02204       _M_bucket_count = __n;
02205       _M_buckets = __new_buckets;
02206     }
02207 
02208 #if __cplusplus > 201402L
02209   template<typename, typename, typename> class _Hash_merge_helper { };
02210 #endif // C++17
02211 
02212 #if __cpp_deduction_guides >= 201606
02213   // Used to constrain deduction guides
02214   template<typename _Hash>
02215     using _RequireNotAllocatorOrIntegral
02216       = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
02217 #endif
02218 
02219 _GLIBCXX_END_NAMESPACE_VERSION
02220 } // namespace std
02221 
02222 #endif // _HASHTABLE_H