Home | History | Annotate | Download | only in bits
      1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
      2 
      3 // Copyright (C) 2010-2014 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file bits/hashtable_policy.h
     26  *  This is an internal header file, included by other library headers.
     27  *  Do not attempt to use it directly.
     28  *  @headername{unordered_map,unordered_set}
     29  */
     30 
     31 #ifndef _HASHTABLE_POLICY_H
     32 #define _HASHTABLE_POLICY_H 1
     33 
     34 namespace std _GLIBCXX_VISIBILITY(default)
     35 {
     36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     37 
     38   template<typename _Key, typename _Value, typename _Alloc,
     39 	   typename _ExtractKey, typename _Equal,
     40 	   typename _H1, typename _H2, typename _Hash,
     41 	   typename _RehashPolicy, typename _Traits>
     42     class _Hashtable;
     43 
     44 _GLIBCXX_END_NAMESPACE_VERSION
     45 
     46 namespace __detail
     47 {
     48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     49 
     50   /**
     51    *  @defgroup hashtable-detail Base and Implementation Classes
     52    *  @ingroup unordered_associative_containers
     53    *  @{
     54    */
     55   template<typename _Key, typename _Value,
     56 	   typename _ExtractKey, typename _Equal,
     57 	   typename _H1, typename _H2, typename _Hash, typename _Traits>
     58     struct _Hashtable_base;
     59 
     60   // Helper function: return distance(first, last) for forward
     61   // iterators, or 0 for input iterators.
     62   template<class _Iterator>
     63     inline typename std::iterator_traits<_Iterator>::difference_type
     64     __distance_fw(_Iterator __first, _Iterator __last,
     65 		  std::input_iterator_tag)
     66     { return 0; }
     67 
     68   template<class _Iterator>
     69     inline typename std::iterator_traits<_Iterator>::difference_type
     70     __distance_fw(_Iterator __first, _Iterator __last,
     71 		  std::forward_iterator_tag)
     72     { return std::distance(__first, __last); }
     73 
     74   template<class _Iterator>
     75     inline typename std::iterator_traits<_Iterator>::difference_type
     76     __distance_fw(_Iterator __first, _Iterator __last)
     77     {
     78       typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
     79       return __distance_fw(__first, __last, _Tag());
     80     }
     81 
     82   // Helper type used to detect whether the hash functor is noexcept.
     83   template <typename _Key, typename _Hash>
     84     struct __is_noexcept_hash : std::integral_constant<bool,
     85 	noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
     86     { };
     87 
     88   struct _Identity
     89   {
     90     template<typename _Tp>
     91       _Tp&&
     92       operator()(_Tp&& __x) const
     93       { return std::forward<_Tp>(__x); }
     94   };
     95 
     96   struct _Select1st
     97   {
     98     template<typename _Tp>
     99       auto
    100       operator()(_Tp&& __x) const
    101       -> decltype(std::get<0>(std::forward<_Tp>(__x)))
    102       { return std::get<0>(std::forward<_Tp>(__x)); }
    103   };
    104 
    105   template<typename _NodeAlloc>
    106     struct _Hashtable_alloc;
    107 
    108   // Functor recycling a pool of nodes and using allocation once the pool is
    109   // empty.
    110   template<typename _NodeAlloc>
    111     struct _ReuseOrAllocNode
    112     {
    113     private:
    114       using __node_alloc_type = _NodeAlloc;
    115       using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
    116       using __value_alloc_type = typename __hashtable_alloc::__value_alloc_type;
    117       using __value_alloc_traits =
    118 	typename __hashtable_alloc::__value_alloc_traits;
    119       using __node_alloc_traits =
    120 	typename __hashtable_alloc::__node_alloc_traits;
    121       using __node_type = typename __hashtable_alloc::__node_type;
    122 
    123     public:
    124       _ReuseOrAllocNode(__node_type* __nodes, __hashtable_alloc& __h)
    125 	: _M_nodes(__nodes), _M_h(__h) { }
    126       _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete;
    127 
    128       ~_ReuseOrAllocNode()
    129       { _M_h._M_deallocate_nodes(_M_nodes); }
    130 
    131       template<typename _Arg>
    132 	__node_type*
    133 	operator()(_Arg&& __arg) const
    134 	{
    135 	  if (_M_nodes)
    136 	    {
    137 	      __node_type* __node = _M_nodes;
    138 	      _M_nodes = _M_nodes->_M_next();
    139 	      __node->_M_nxt = nullptr;
    140 	      __value_alloc_type __a(_M_h._M_node_allocator());
    141 	      __value_alloc_traits::destroy(__a, __node->_M_valptr());
    142 	      __try
    143 		{
    144 		  __value_alloc_traits::construct(__a, __node->_M_valptr(),
    145 						  std::forward<_Arg>(__arg));
    146 		}
    147 	      __catch(...)
    148 		{
    149 		  __node->~__node_type();
    150 		  __node_alloc_traits::deallocate(_M_h._M_node_allocator(),
    151 						  __node, 1);
    152 		  __throw_exception_again;
    153 		}
    154 	      return __node;
    155 	    }
    156 	  return _M_h._M_allocate_node(std::forward<_Arg>(__arg));
    157 	}
    158 
    159     private:
    160       mutable __node_type* _M_nodes;
    161       __hashtable_alloc& _M_h;
    162     };
    163 
    164   // Functor similar to the previous one but without any pool of nodes to
    165   // recycle.
    166   template<typename _NodeAlloc>
    167     struct _AllocNode
    168     {
    169     private:
    170       using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
    171       using __node_type = typename __hashtable_alloc::__node_type;
    172 
    173     public:
    174       _AllocNode(__hashtable_alloc& __h)
    175 	: _M_h(__h) { }
    176 
    177       template<typename _Arg>
    178 	__node_type*
    179 	operator()(_Arg&& __arg) const
    180 	{ return _M_h._M_allocate_node(std::forward<_Arg>(__arg)); }
    181 
    182     private:
    183       __hashtable_alloc& _M_h;
    184     };
    185 
    186   // Auxiliary types used for all instantiations of _Hashtable nodes
    187   // and iterators.
    188 
    189   /**
    190    *  struct _Hashtable_traits
    191    *
    192    *  Important traits for hash tables.
    193    *
    194    *  @tparam _Cache_hash_code  Boolean value. True if the value of
    195    *  the hash function is stored along with the value. This is a
    196    *  time-space tradeoff.  Storing it may improve lookup speed by
    197    *  reducing the number of times we need to call the _Equal
    198    *  function.
    199    *
    200    *  @tparam _Constant_iterators  Boolean value. True if iterator and
    201    *  const_iterator are both constant iterator types. This is true
    202    *  for unordered_set and unordered_multiset, false for
    203    *  unordered_map and unordered_multimap.
    204    *
    205    *  @tparam _Unique_keys  Boolean value. True if the return value
    206    *  of _Hashtable::count(k) is always at most one, false if it may
    207    *  be an arbitrary number. This is true for unordered_set and
    208    *  unordered_map, false for unordered_multiset and
    209    *  unordered_multimap.
    210    */
    211   template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
    212     struct _Hashtable_traits
    213     {
    214       template<bool _Cond>
    215 	using __bool_constant = integral_constant<bool, _Cond>;
    216 
    217       using __hash_cached = __bool_constant<_Cache_hash_code>;
    218       using __constant_iterators = __bool_constant<_Constant_iterators>;
    219       using __unique_keys = __bool_constant<_Unique_keys>;
    220     };
    221 
    222   /**
    223    *  struct _Hash_node_base
    224    *
    225    *  Nodes, used to wrap elements stored in the hash table.  A policy
    226    *  template parameter of class template _Hashtable controls whether
    227    *  nodes also store a hash code. In some cases (e.g. strings) this
    228    *  may be a performance win.
    229    */
    230   struct _Hash_node_base
    231   {
    232     _Hash_node_base* _M_nxt;
    233 
    234     _Hash_node_base() noexcept : _M_nxt() { }
    235 
    236     _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
    237   };
    238 
    239   /**
    240    *  struct _Hash_node_value_base
    241    *
    242    *  Node type with the value to store.
    243    */
    244   template<typename _Value>
    245     struct _Hash_node_value_base : _Hash_node_base
    246     {
    247       typedef _Value value_type;
    248 
    249       __gnu_cxx::__aligned_buffer<_Value> _M_storage;
    250 
    251       _Value*
    252       _M_valptr() noexcept
    253       { return _M_storage._M_ptr(); }
    254 
    255       const _Value*
    256       _M_valptr() const noexcept
    257       { return _M_storage._M_ptr(); }
    258 
    259       _Value&
    260       _M_v() noexcept
    261       { return *_M_valptr(); }
    262 
    263       const _Value&
    264       _M_v() const noexcept
    265       { return *_M_valptr(); }
    266     };
    267 
    268   /**
    269    *  Primary template struct _Hash_node.
    270    */
    271   template<typename _Value, bool _Cache_hash_code>
    272     struct _Hash_node;
    273 
    274   /**
    275    *  Specialization for nodes with caches, struct _Hash_node.
    276    *
    277    *  Base class is __detail::_Hash_node_value_base.
    278    */
    279   template<typename _Value>
    280     struct _Hash_node<_Value, true> : _Hash_node_value_base<_Value>
    281     {
    282       std::size_t  _M_hash_code;
    283 
    284       _Hash_node*
    285       _M_next() const noexcept
    286       { return static_cast<_Hash_node*>(this->_M_nxt); }
    287     };
    288 
    289   /**
    290    *  Specialization for nodes without caches, struct _Hash_node.
    291    *
    292    *  Base class is __detail::_Hash_node_value_base.
    293    */
    294   template<typename _Value>
    295     struct _Hash_node<_Value, false> : _Hash_node_value_base<_Value>
    296     {
    297       _Hash_node*
    298       _M_next() const noexcept
    299       { return static_cast<_Hash_node*>(this->_M_nxt); }
    300     };
    301 
    302   /// Base class for node iterators.
    303   template<typename _Value, bool _Cache_hash_code>
    304     struct _Node_iterator_base
    305     {
    306       using __node_type = _Hash_node<_Value, _Cache_hash_code>;
    307 
    308       __node_type*  _M_cur;
    309 
    310       _Node_iterator_base(__node_type* __p) noexcept
    311       : _M_cur(__p) { }
    312 
    313       void
    314       _M_incr() noexcept
    315       { _M_cur = _M_cur->_M_next(); }
    316     };
    317 
    318   template<typename _Value, bool _Cache_hash_code>
    319     inline bool
    320     operator==(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
    321 	       const _Node_iterator_base<_Value, _Cache_hash_code >& __y)
    322     noexcept
    323     { return __x._M_cur == __y._M_cur; }
    324 
    325   template<typename _Value, bool _Cache_hash_code>
    326     inline bool
    327     operator!=(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
    328 	       const _Node_iterator_base<_Value, _Cache_hash_code>& __y)
    329     noexcept
    330     { return __x._M_cur != __y._M_cur; }
    331 
    332   /// Node iterators, used to iterate through all the hashtable.
    333   template<typename _Value, bool __constant_iterators, bool __cache>
    334     struct _Node_iterator
    335     : public _Node_iterator_base<_Value, __cache>
    336     {
    337     private:
    338       using __base_type = _Node_iterator_base<_Value, __cache>;
    339       using __node_type = typename __base_type::__node_type;
    340 
    341     public:
    342       typedef _Value					value_type;
    343       typedef std::ptrdiff_t				difference_type;
    344       typedef std::forward_iterator_tag			iterator_category;
    345 
    346       using pointer = typename std::conditional<__constant_iterators,
    347 						const _Value*, _Value*>::type;
    348 
    349       using reference = typename std::conditional<__constant_iterators,
    350 						  const _Value&, _Value&>::type;
    351 
    352       _Node_iterator() noexcept
    353       : __base_type(0) { }
    354 
    355       explicit
    356       _Node_iterator(__node_type* __p) noexcept
    357       : __base_type(__p) { }
    358 
    359       reference
    360       operator*() const noexcept
    361       { return this->_M_cur->_M_v(); }
    362 
    363       pointer
    364       operator->() const noexcept
    365       { return this->_M_cur->_M_valptr(); }
    366 
    367       _Node_iterator&
    368       operator++() noexcept
    369       {
    370 	this->_M_incr();
    371 	return *this;
    372       }
    373 
    374       _Node_iterator
    375       operator++(int) noexcept
    376       {
    377 	_Node_iterator __tmp(*this);
    378 	this->_M_incr();
    379 	return __tmp;
    380       }
    381     };
    382 
    383   /// Node const_iterators, used to iterate through all the hashtable.
    384   template<typename _Value, bool __constant_iterators, bool __cache>
    385     struct _Node_const_iterator
    386     : public _Node_iterator_base<_Value, __cache>
    387     {
    388     private:
    389       using __base_type = _Node_iterator_base<_Value, __cache>;
    390       using __node_type = typename __base_type::__node_type;
    391 
    392     public:
    393       typedef _Value					value_type;
    394       typedef std::ptrdiff_t				difference_type;
    395       typedef std::forward_iterator_tag			iterator_category;
    396 
    397       typedef const _Value*				pointer;
    398       typedef const _Value&				reference;
    399 
    400       _Node_const_iterator() noexcept
    401       : __base_type(0) { }
    402 
    403       explicit
    404       _Node_const_iterator(__node_type* __p) noexcept
    405       : __base_type(__p) { }
    406 
    407       _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
    408 			   __cache>& __x) noexcept
    409       : __base_type(__x._M_cur) { }
    410 
    411       reference
    412       operator*() const noexcept
    413       { return this->_M_cur->_M_v(); }
    414 
    415       pointer
    416       operator->() const noexcept
    417       { return this->_M_cur->_M_valptr(); }
    418 
    419       _Node_const_iterator&
    420       operator++() noexcept
    421       {
    422 	this->_M_incr();
    423 	return *this;
    424       }
    425 
    426       _Node_const_iterator
    427       operator++(int) noexcept
    428       {
    429 	_Node_const_iterator __tmp(*this);
    430 	this->_M_incr();
    431 	return __tmp;
    432       }
    433     };
    434 
    435   // Many of class template _Hashtable's template parameters are policy
    436   // classes.  These are defaults for the policies.
    437 
    438   /// Default range hashing function: use division to fold a large number
    439   /// into the range [0, N).
    440   struct _Mod_range_hashing
    441   {
    442     typedef std::size_t first_argument_type;
    443     typedef std::size_t second_argument_type;
    444     typedef std::size_t result_type;
    445 
    446     result_type
    447     operator()(first_argument_type __num,
    448 	       second_argument_type __den) const noexcept
    449     { return __num % __den; }
    450   };
    451 
    452   /// Default ranged hash function H.  In principle it should be a
    453   /// function object composed from objects of type H1 and H2 such that
    454   /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
    455   /// h1 and h2.  So instead we'll just use a tag to tell class template
    456   /// hashtable to do that composition.
    457   struct _Default_ranged_hash { };
    458 
    459   /// Default value for rehash policy.  Bucket size is (usually) the
    460   /// smallest prime that keeps the load factor small enough.
    461   struct _Prime_rehash_policy
    462   {
    463     _Prime_rehash_policy(float __z = 1.0)
    464     : _M_max_load_factor(__z), _M_next_resize(0) { }
    465 
    466     float
    467     max_load_factor() const noexcept
    468     { return _M_max_load_factor; }
    469 
    470     // Return a bucket size no smaller than n.
    471     std::size_t
    472     _M_next_bkt(std::size_t __n) const;
    473 
    474     // Return a bucket count appropriate for n elements
    475     std::size_t
    476     _M_bkt_for_elements(std::size_t __n) const
    477     { return __builtin_ceil(__n / (long double)_M_max_load_factor); }
    478 
    479     // __n_bkt is current bucket count, __n_elt is current element count,
    480     // and __n_ins is number of elements to be inserted.  Do we need to
    481     // increase bucket count?  If so, return make_pair(true, n), where n
    482     // is the new bucket count.  If not, return make_pair(false, 0).
    483     std::pair<bool, std::size_t>
    484     _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
    485 		   std::size_t __n_ins) const;
    486 
    487     typedef std::size_t _State;
    488 
    489     _State
    490     _M_state() const
    491     { return _M_next_resize; }
    492 
    493     void
    494     _M_reset() noexcept
    495     { _M_next_resize = 0; }
    496 
    497     void
    498     _M_reset(_State __state)
    499     { _M_next_resize = __state; }
    500 
    501     enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
    502 
    503     static const std::size_t _S_growth_factor = 2;
    504 
    505     float		_M_max_load_factor;
    506     mutable std::size_t	_M_next_resize;
    507   };
    508 
    509   // Base classes for std::_Hashtable.  We define these base classes
    510   // because in some cases we want to do different things depending on
    511   // the value of a policy class.  In some cases the policy class
    512   // affects which member functions and nested typedefs are defined;
    513   // we handle that by specializing base class templates.  Several of
    514   // the base class templates need to access other members of class
    515   // template _Hashtable, so we use a variant of the "Curiously
    516   // Recurring Template Pattern" (CRTP) technique.
    517 
    518   /**
    519    *  Primary class template _Map_base.
    520    *
    521    *  If the hashtable has a value type of the form pair<T1, T2> and a
    522    *  key extraction policy (_ExtractKey) that returns the first part
    523    *  of the pair, the hashtable gets a mapped_type typedef.  If it
    524    *  satisfies those criteria and also has unique keys, then it also
    525    *  gets an operator[].
    526    */
    527   template<typename _Key, typename _Value, typename _Alloc,
    528 	   typename _ExtractKey, typename _Equal,
    529 	   typename _H1, typename _H2, typename _Hash,
    530 	   typename _RehashPolicy, typename _Traits,
    531 	   bool _Unique_keys = _Traits::__unique_keys::value>
    532     struct _Map_base { };
    533 
    534   /// Partial specialization, __unique_keys set to false.
    535   template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
    536 	   typename _H1, typename _H2, typename _Hash,
    537 	   typename _RehashPolicy, typename _Traits>
    538     struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    539 		     _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
    540     {
    541       using mapped_type = typename std::tuple_element<1, _Pair>::type;
    542     };
    543 
    544   /// Partial specialization, __unique_keys set to true.
    545   template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
    546 	   typename _H1, typename _H2, typename _Hash,
    547 	   typename _RehashPolicy, typename _Traits>
    548     struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    549 		     _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
    550     {
    551     private:
    552       using __hashtable_base = __detail::_Hashtable_base<_Key, _Pair,
    553 							 _Select1st,
    554 							_Equal, _H1, _H2, _Hash,
    555 							  _Traits>;
    556 
    557       using __hashtable = _Hashtable<_Key, _Pair, _Alloc,
    558 				     _Select1st, _Equal,
    559 				     _H1, _H2, _Hash, _RehashPolicy, _Traits>;
    560 
    561       using __hash_code = typename __hashtable_base::__hash_code;
    562       using __node_type = typename __hashtable_base::__node_type;
    563 
    564     public:
    565       using key_type = typename __hashtable_base::key_type;
    566       using iterator = typename __hashtable_base::iterator;
    567       using mapped_type = typename std::tuple_element<1, _Pair>::type;
    568 
    569       mapped_type&
    570       operator[](const key_type& __k);
    571 
    572       mapped_type&
    573       operator[](key_type&& __k);
    574 
    575       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    576       // DR 761. unordered_map needs an at() member function.
    577       mapped_type&
    578       at(const key_type& __k);
    579 
    580       const mapped_type&
    581       at(const key_type& __k) const;
    582     };
    583 
    584   template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
    585 	   typename _H1, typename _H2, typename _Hash,
    586 	   typename _RehashPolicy, typename _Traits>
    587     typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    588 		       _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
    589 		       ::mapped_type&
    590     _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    591 	      _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
    592     operator[](const key_type& __k)
    593     {
    594       __hashtable* __h = static_cast<__hashtable*>(this);
    595       __hash_code __code = __h->_M_hash_code(__k);
    596       std::size_t __n = __h->_M_bucket_index(__k, __code);
    597       __node_type* __p = __h->_M_find_node(__n, __k, __code);
    598 
    599       if (!__p)
    600 	{
    601 	  __p = __h->_M_allocate_node(std::piecewise_construct,
    602 				      std::tuple<const key_type&>(__k),
    603 				      std::tuple<>());
    604 	  return __h->_M_insert_unique_node(__n, __code, __p)->second;
    605 	}
    606 
    607       return __p->_M_v().second;
    608     }
    609 
    610   template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
    611 	   typename _H1, typename _H2, typename _Hash,
    612 	   typename _RehashPolicy, typename _Traits>
    613     typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    614 		       _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
    615 		       ::mapped_type&
    616     _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    617 	      _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
    618     operator[](key_type&& __k)
    619     {
    620       __hashtable* __h = static_cast<__hashtable*>(this);
    621       __hash_code __code = __h->_M_hash_code(__k);
    622       std::size_t __n = __h->_M_bucket_index(__k, __code);
    623       __node_type* __p = __h->_M_find_node(__n, __k, __code);
    624 
    625       if (!__p)
    626 	{
    627 	  __p = __h->_M_allocate_node(std::piecewise_construct,
    628 				      std::forward_as_tuple(std::move(__k)),
    629 				      std::tuple<>());
    630 	  return __h->_M_insert_unique_node(__n, __code, __p)->second;
    631 	}
    632 
    633       return __p->_M_v().second;
    634     }
    635 
    636   template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
    637 	   typename _H1, typename _H2, typename _Hash,
    638 	   typename _RehashPolicy, typename _Traits>
    639     typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    640 		       _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
    641 		       ::mapped_type&
    642     _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    643 	      _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
    644     at(const key_type& __k)
    645     {
    646       __hashtable* __h = static_cast<__hashtable*>(this);
    647       __hash_code __code = __h->_M_hash_code(__k);
    648       std::size_t __n = __h->_M_bucket_index(__k, __code);
    649       __node_type* __p = __h->_M_find_node(__n, __k, __code);
    650 
    651       if (!__p)
    652 	__throw_out_of_range(__N("_Map_base::at"));
    653       return __p->_M_v().second;
    654     }
    655 
    656   template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
    657 	   typename _H1, typename _H2, typename _Hash,
    658 	   typename _RehashPolicy, typename _Traits>
    659     const typename _Map_base<_Key, _Pair, _Alloc, _Select1st,
    660 			     _Equal, _H1, _H2, _Hash, _RehashPolicy,
    661 			     _Traits, true>::mapped_type&
    662     _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
    663 	      _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
    664     at(const key_type& __k) const
    665     {
    666       const __hashtable* __h = static_cast<const __hashtable*>(this);
    667       __hash_code __code = __h->_M_hash_code(__k);
    668       std::size_t __n = __h->_M_bucket_index(__k, __code);
    669       __node_type* __p = __h->_M_find_node(__n, __k, __code);
    670 
    671       if (!__p)
    672 	__throw_out_of_range(__N("_Map_base::at"));
    673       return __p->_M_v().second;
    674     }
    675 
    676   /**
    677    *  Primary class template _Insert_base.
    678    *
    679    *  insert member functions appropriate to all _Hashtables.
    680    */
    681   template<typename _Key, typename _Value, typename _Alloc,
    682 	   typename _ExtractKey, typename _Equal,
    683 	   typename _H1, typename _H2, typename _Hash,
    684 	   typename _RehashPolicy, typename _Traits>
    685     struct _Insert_base
    686     {
    687     protected:
    688       using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
    689 				     _Equal, _H1, _H2, _Hash,
    690 				     _RehashPolicy, _Traits>;
    691 
    692       using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
    693 					       _Equal, _H1, _H2, _Hash,
    694 					       _Traits>;
    695 
    696       using value_type = typename __hashtable_base::value_type;
    697       using iterator = typename __hashtable_base::iterator;
    698       using const_iterator =  typename __hashtable_base::const_iterator;
    699       using size_type = typename __hashtable_base::size_type;
    700 
    701       using __unique_keys = typename __hashtable_base::__unique_keys;
    702       using __ireturn_type = typename __hashtable_base::__ireturn_type;
    703       using __node_type = _Hash_node<_Value, _Traits::__hash_cached::value>;
    704       using __node_alloc_type =
    705 	typename __alloctr_rebind<_Alloc, __node_type>::__type;
    706       using __node_gen_type = _AllocNode<__node_alloc_type>;
    707 
    708       __hashtable&
    709       _M_conjure_hashtable()
    710       { return *(static_cast<__hashtable*>(this)); }
    711 
    712       template<typename _InputIterator, typename _NodeGetter>
    713 	void
    714 	_M_insert_range(_InputIterator __first, _InputIterator __last,
    715 			const _NodeGetter&);
    716 
    717     public:
    718       __ireturn_type
    719       insert(const value_type& __v)
    720       {
    721 	__hashtable& __h = _M_conjure_hashtable();
    722 	__node_gen_type __node_gen(__h);
    723 	return __h._M_insert(__v, __node_gen, __unique_keys());
    724       }
    725 
    726       iterator
    727       insert(const_iterator __hint, const value_type& __v)
    728       {
    729 	__hashtable& __h = _M_conjure_hashtable();
    730 	__node_gen_type __node_gen(__h);
    731 	return __h._M_insert(__hint, __v, __node_gen, __unique_keys());
    732       }
    733 
    734       void
    735       insert(initializer_list<value_type> __l)
    736       { this->insert(__l.begin(), __l.end()); }
    737 
    738       template<typename _InputIterator>
    739 	void
    740 	insert(_InputIterator __first, _InputIterator __last)
    741 	{
    742 	  __hashtable& __h = _M_conjure_hashtable();
    743 	  __node_gen_type __node_gen(__h);
    744 	  return _M_insert_range(__first, __last, __node_gen);
    745 	}
    746     };
    747 
    748   template<typename _Key, typename _Value, typename _Alloc,
    749 	   typename _ExtractKey, typename _Equal,
    750 	   typename _H1, typename _H2, typename _Hash,
    751 	   typename _RehashPolicy, typename _Traits>
    752     template<typename _InputIterator, typename _NodeGetter>
    753       void
    754       _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
    755 		    _RehashPolicy, _Traits>::
    756       _M_insert_range(_InputIterator __first, _InputIterator __last,
    757 		      const _NodeGetter& __node_gen)
    758       {
    759 	using __rehash_type = typename __hashtable::__rehash_type;
    760 	using __rehash_state = typename __hashtable::__rehash_state;
    761 	using pair_type = std::pair<bool, std::size_t>;
    762 
    763 	size_type __n_elt = __detail::__distance_fw(__first, __last);
    764 
    765 	__hashtable& __h = _M_conjure_hashtable();
    766 	__rehash_type& __rehash = __h._M_rehash_policy;
    767 	const __rehash_state& __saved_state = __rehash._M_state();
    768 	pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count,
    769 							__h._M_element_count,
    770 							__n_elt);
    771 
    772 	if (__do_rehash.first)
    773 	  __h._M_rehash(__do_rehash.second, __saved_state);
    774 
    775 	for (; __first != __last; ++__first)
    776 	  __h._M_insert(*__first, __node_gen, __unique_keys());
    777       }
    778 
    779   /**
    780    *  Primary class template _Insert.
    781    *
    782    *  Select insert member functions appropriate to _Hashtable policy choices.
    783    */
    784   template<typename _Key, typename _Value, typename _Alloc,
    785 	   typename _ExtractKey, typename _Equal,
    786 	   typename _H1, typename _H2, typename _Hash,
    787 	   typename _RehashPolicy, typename _Traits,
    788 	   bool _Constant_iterators = _Traits::__constant_iterators::value,
    789 	   bool _Unique_keys = _Traits::__unique_keys::value>
    790     struct _Insert;
    791 
    792   /// Specialization.
    793   template<typename _Key, typename _Value, typename _Alloc,
    794 	   typename _ExtractKey, typename _Equal,
    795 	   typename _H1, typename _H2, typename _Hash,
    796 	   typename _RehashPolicy, typename _Traits>
    797     struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
    798 		   _RehashPolicy, _Traits, true, true>
    799     : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
    800 			   _H1, _H2, _Hash, _RehashPolicy, _Traits>
    801     {
    802       using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
    803 					_Equal, _H1, _H2, _Hash,
    804 					_RehashPolicy, _Traits>;
    805       using value_type = typename __base_type::value_type;
    806       using iterator = typename __base_type::iterator;
    807       using const_iterator =  typename __base_type::const_iterator;
    808 
    809       using __unique_keys = typename __base_type::__unique_keys;
    810       using __hashtable = typename __base_type::__hashtable;
    811       using __node_gen_type = typename __base_type::__node_gen_type;
    812 
    813       using __base_type::insert;
    814 
    815       std::pair<iterator, bool>
    816       insert(value_type&& __v)
    817       {
    818 	__hashtable& __h = this->_M_conjure_hashtable();
    819 	__node_gen_type __node_gen(__h);
    820 	return __h._M_insert(std::move(__v), __node_gen, __unique_keys());
    821       }
    822 
    823       iterator
    824       insert(const_iterator __hint, value_type&& __v)
    825       {
    826 	__hashtable& __h = this->_M_conjure_hashtable();
    827 	__node_gen_type __node_gen(__h);
    828 	return __h._M_insert(__hint, std::move(__v), __node_gen,
    829 			     __unique_keys());
    830       }
    831     };
    832 
    833   /// Specialization.
    834   template<typename _Key, typename _Value, typename _Alloc,
    835 	   typename _ExtractKey, typename _Equal,
    836 	   typename _H1, typename _H2, typename _Hash,
    837 	   typename _RehashPolicy, typename _Traits>
    838     struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
    839 		   _RehashPolicy, _Traits, true, false>
    840     : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
    841 			   _H1, _H2, _Hash, _RehashPolicy, _Traits>
    842     {
    843       using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
    844 					_Equal, _H1, _H2, _Hash,
    845 					_RehashPolicy, _Traits>;
    846       using value_type = typename __base_type::value_type;
    847       using iterator = typename __base_type::iterator;
    848       using const_iterator =  typename __base_type::const_iterator;
    849 
    850       using __unique_keys = typename __base_type::__unique_keys;
    851       using __hashtable = typename __base_type::__hashtable;
    852       using __node_gen_type = typename __base_type::__node_gen_type;
    853 
    854       using __base_type::insert;
    855 
    856       iterator
    857       insert(value_type&& __v)
    858       {
    859 	__hashtable& __h = this->_M_conjure_hashtable();
    860 	__node_gen_type __node_gen(__h);
    861 	return __h._M_insert(std::move(__v), __node_gen, __unique_keys());
    862       }
    863 
    864       iterator
    865       insert(const_iterator __hint, value_type&& __v)
    866       {
    867 	__hashtable& __h = this->_M_conjure_hashtable();
    868 	__node_gen_type __node_gen(__h);
    869 	return __h._M_insert(__hint, std::move(__v), __node_gen,
    870 			     __unique_keys());
    871       }
    872     };
    873 
    874   /// Specialization.
    875   template<typename _Key, typename _Value, typename _Alloc,
    876 	   typename _ExtractKey, typename _Equal,
    877 	   typename _H1, typename _H2, typename _Hash,
    878 	   typename _RehashPolicy, typename _Traits, bool _Unique_keys>
    879     struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
    880 		   _RehashPolicy, _Traits, false, _Unique_keys>
    881     : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
    882 			   _H1, _H2, _Hash, _RehashPolicy, _Traits>
    883     {
    884       using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
    885 				       _Equal, _H1, _H2, _Hash,
    886 				       _RehashPolicy, _Traits>;
    887       using value_type = typename __base_type::value_type;
    888       using iterator = typename __base_type::iterator;
    889       using const_iterator =  typename __base_type::const_iterator;
    890 
    891       using __unique_keys = typename __base_type::__unique_keys;
    892       using __hashtable = typename __base_type::__hashtable;
    893       using __ireturn_type = typename __base_type::__ireturn_type;
    894 
    895       using __base_type::insert;
    896 
    897       template<typename _Pair>
    898 	using __is_cons = std::is_constructible<value_type, _Pair&&>;
    899 
    900       template<typename _Pair>
    901 	using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
    902 
    903       template<typename _Pair>
    904 	using _IFconsp = typename _IFcons<_Pair>::type;
    905 
    906       template<typename _Pair, typename = _IFconsp<_Pair>>
    907 	__ireturn_type
    908 	insert(_Pair&& __v)
    909 	{
    910 	  __hashtable& __h = this->_M_conjure_hashtable();
    911 	  return __h._M_emplace(__unique_keys(), std::forward<_Pair>(__v));
    912 	}
    913 
    914       template<typename _Pair, typename = _IFconsp<_Pair>>
    915 	iterator
    916 	insert(const_iterator __hint, _Pair&& __v)
    917 	{
    918 	  __hashtable& __h = this->_M_conjure_hashtable();
    919 	  return __h._M_emplace(__hint, __unique_keys(),
    920 				std::forward<_Pair>(__v));
    921 	}
    922    };
    923 
    924   /**
    925    *  Primary class template  _Rehash_base.
    926    *
    927    *  Give hashtable the max_load_factor functions and reserve iff the
    928    *  rehash policy is _Prime_rehash_policy.
    929   */
    930   template<typename _Key, typename _Value, typename _Alloc,
    931 	   typename _ExtractKey, typename _Equal,
    932 	   typename _H1, typename _H2, typename _Hash,
    933 	   typename _RehashPolicy, typename _Traits>
    934     struct _Rehash_base;
    935 
    936   /// Specialization.
    937   template<typename _Key, typename _Value, typename _Alloc,
    938 	   typename _ExtractKey, typename _Equal,
    939 	   typename _H1, typename _H2, typename _Hash, typename _Traits>
    940     struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
    941 			_H1, _H2, _Hash, _Prime_rehash_policy, _Traits>
    942     {
    943       using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
    944 				     _Equal, _H1, _H2, _Hash,
    945 				     _Prime_rehash_policy, _Traits>;
    946 
    947       float
    948       max_load_factor() const noexcept
    949       {
    950 	const __hashtable* __this = static_cast<const __hashtable*>(this);
    951 	return __this->__rehash_policy().max_load_factor();
    952       }
    953 
    954       void
    955       max_load_factor(float __z)
    956       {
    957 	__hashtable* __this = static_cast<__hashtable*>(this);
    958 	__this->__rehash_policy(_Prime_rehash_policy(__z));
    959       }
    960 
    961       void
    962       reserve(std::size_t __n)
    963       {
    964 	__hashtable* __this = static_cast<__hashtable*>(this);
    965 	__this->rehash(__builtin_ceil(__n / max_load_factor()));
    966       }
    967     };
    968 
    969   /**
    970    *  Primary class template _Hashtable_ebo_helper.
    971    *
    972    *  Helper class using EBO when it is not forbidden (the type is not
    973    *  final) and when it is worth it (the type is empty.)
    974    */
    975   template<int _Nm, typename _Tp,
    976 	   bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
    977     struct _Hashtable_ebo_helper;
    978 
    979   /// Specialization using EBO.
    980   template<int _Nm, typename _Tp>
    981     struct _Hashtable_ebo_helper<_Nm, _Tp, true>
    982     : private _Tp
    983     {
    984       _Hashtable_ebo_helper() = default;
    985 
    986       template<typename _OtherTp>
    987 	_Hashtable_ebo_helper(_OtherTp&& __tp)
    988 	  : _Tp(std::forward<_OtherTp>(__tp))
    989 	{ }
    990 
    991       static const _Tp&
    992       _S_cget(const _Hashtable_ebo_helper& __eboh)
    993       { return static_cast<const _Tp&>(__eboh); }
    994 
    995       static _Tp&
    996       _S_get(_Hashtable_ebo_helper& __eboh)
    997       { return static_cast<_Tp&>(__eboh); }
    998     };
    999 
   1000   /// Specialization not using EBO.
   1001   template<int _Nm, typename _Tp>
   1002     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
   1003     {
   1004       _Hashtable_ebo_helper() = default;
   1005 
   1006       template<typename _OtherTp>
   1007 	_Hashtable_ebo_helper(_OtherTp&& __tp)
   1008 	  : _M_tp(std::forward<_OtherTp>(__tp))
   1009 	{ }
   1010 
   1011       static const _Tp&
   1012       _S_cget(const _Hashtable_ebo_helper& __eboh)
   1013       { return __eboh._M_tp; }
   1014 
   1015       static _Tp&
   1016       _S_get(_Hashtable_ebo_helper& __eboh)
   1017       { return __eboh._M_tp; }
   1018 
   1019     private:
   1020       _Tp _M_tp;
   1021     };
   1022 
   1023   /**
   1024    *  Primary class template _Local_iterator_base.
   1025    *
   1026    *  Base class for local iterators, used to iterate within a bucket
   1027    *  but not between buckets.
   1028    */
   1029   template<typename _Key, typename _Value, typename _ExtractKey,
   1030 	   typename _H1, typename _H2, typename _Hash,
   1031 	   bool __cache_hash_code>
   1032     struct _Local_iterator_base;
   1033 
   1034   /**
   1035    *  Primary class template _Hash_code_base.
   1036    *
   1037    *  Encapsulates two policy issues that aren't quite orthogonal.
   1038    *   (1) the difference between using a ranged hash function and using
   1039    *       the combination of a hash function and a range-hashing function.
   1040    *       In the former case we don't have such things as hash codes, so
   1041    *       we have a dummy type as placeholder.
   1042    *   (2) Whether or not we cache hash codes.  Caching hash codes is
   1043    *       meaningless if we have a ranged hash function.
   1044    *
   1045    *  We also put the key extraction objects here, for convenience.
   1046    *  Each specialization derives from one or more of the template
   1047    *  parameters to benefit from Ebo. This is important as this type
   1048    *  is inherited in some cases by the _Local_iterator_base type used
   1049    *  to implement local_iterator and const_local_iterator. As with
   1050    *  any iterator type we prefer to make it as small as possible.
   1051    *
   1052    *  Primary template is unused except as a hook for specializations.
   1053    */
   1054   template<typename _Key, typename _Value, typename _ExtractKey,
   1055 	   typename _H1, typename _H2, typename _Hash,
   1056 	   bool __cache_hash_code>
   1057     struct _Hash_code_base;
   1058 
   1059   /// Specialization: ranged hash function, no caching hash codes.  H1
   1060   /// and H2 are provided but ignored.  We define a dummy hash code type.
   1061   template<typename _Key, typename _Value, typename _ExtractKey,
   1062 	   typename _H1, typename _H2, typename _Hash>
   1063     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
   1064     : private _Hashtable_ebo_helper<0, _ExtractKey>,
   1065       private _Hashtable_ebo_helper<1, _Hash>
   1066     {
   1067     private:
   1068       using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
   1069       using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>;
   1070 
   1071     protected:
   1072       typedef void* 					__hash_code;
   1073       typedef _Hash_node<_Value, false>			__node_type;
   1074 
   1075       // We need the default constructor for the local iterators.
   1076       _Hash_code_base() = default;
   1077 
   1078       _Hash_code_base(const _ExtractKey& __ex, const _H1&, const _H2&,
   1079 		      const _Hash& __h)
   1080       : __ebo_extract_key(__ex), __ebo_hash(__h) { }
   1081 
   1082       __hash_code
   1083       _M_hash_code(const _Key& __key) const
   1084       { return 0; }
   1085 
   1086       std::size_t
   1087       _M_bucket_index(const _Key& __k, __hash_code, std::size_t __n) const
   1088       { return _M_ranged_hash()(__k, __n); }
   1089 
   1090       std::size_t
   1091       _M_bucket_index(const __node_type* __p, std::size_t __n) const
   1092 	noexcept( noexcept(declval<const _Hash&>()(declval<const _Key&>(),
   1093 						   (std::size_t)0)) )
   1094       { return _M_ranged_hash()(_M_extract()(__p->_M_v()), __n); }
   1095 
   1096       void
   1097       _M_store_code(__node_type*, __hash_code) const
   1098       { }
   1099 
   1100       void
   1101       _M_copy_code(__node_type*, const __node_type*) const
   1102       { }
   1103 
   1104       void
   1105       _M_swap(_Hash_code_base& __x)
   1106       {
   1107 	std::swap(_M_extract(), __x._M_extract());
   1108 	std::swap(_M_ranged_hash(), __x._M_ranged_hash());
   1109       }
   1110 
   1111       const _ExtractKey&
   1112       _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
   1113 
   1114       _ExtractKey&
   1115       _M_extract() { return __ebo_extract_key::_S_get(*this); }
   1116 
   1117       const _Hash&
   1118       _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
   1119 
   1120       _Hash&
   1121       _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
   1122     };
   1123 
   1124   // No specialization for ranged hash function while caching hash codes.
   1125   // That combination is meaningless, and trying to do it is an error.
   1126 
   1127   /// Specialization: ranged hash function, cache hash codes.  This
   1128   /// combination is meaningless, so we provide only a declaration
   1129   /// and no definition.
   1130   template<typename _Key, typename _Value, typename _ExtractKey,
   1131 	   typename _H1, typename _H2, typename _Hash>
   1132     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
   1133 
   1134   /// Specialization: hash function and range-hashing function, no
   1135   /// caching of hash codes.
   1136   /// Provides typedef and accessor required by C++ 11.
   1137   template<typename _Key, typename _Value, typename _ExtractKey,
   1138 	   typename _H1, typename _H2>
   1139     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
   1140 			   _Default_ranged_hash, false>
   1141     : private _Hashtable_ebo_helper<0, _ExtractKey>,
   1142       private _Hashtable_ebo_helper<1, _H1>,
   1143       private _Hashtable_ebo_helper<2, _H2>
   1144     {
   1145     private:
   1146       using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
   1147       using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
   1148       using __ebo_h2 = _Hashtable_ebo_helper<2, _H2>;
   1149 
   1150       // Gives the local iterator implementation access to _M_bucket_index().
   1151       friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2,
   1152 					 _Default_ranged_hash, false>;
   1153 
   1154     public:
   1155       typedef _H1 					hasher;
   1156 
   1157       hasher
   1158       hash_function() const
   1159       { return _M_h1(); }
   1160 
   1161     protected:
   1162       typedef std::size_t 				__hash_code;
   1163       typedef _Hash_node<_Value, false>			__node_type;
   1164 
   1165       // We need the default constructor for the local iterators.
   1166       _Hash_code_base() = default;
   1167 
   1168       _Hash_code_base(const _ExtractKey& __ex,
   1169 		      const _H1& __h1, const _H2& __h2,
   1170 		      const _Default_ranged_hash&)
   1171       : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
   1172 
   1173       __hash_code
   1174       _M_hash_code(const _Key& __k) const
   1175       { return _M_h1()(__k); }
   1176 
   1177       std::size_t
   1178       _M_bucket_index(const _Key&, __hash_code __c, std::size_t __n) const
   1179       { return _M_h2()(__c, __n); }
   1180 
   1181       std::size_t
   1182       _M_bucket_index(const __node_type* __p, std::size_t __n) const
   1183 	noexcept( noexcept(declval<const _H1&>()(declval<const _Key&>()))
   1184 		  && noexcept(declval<const _H2&>()((__hash_code)0,
   1185 						    (std::size_t)0)) )
   1186       { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v())), __n); }
   1187 
   1188       void
   1189       _M_store_code(__node_type*, __hash_code) const
   1190       { }
   1191 
   1192       void
   1193       _M_copy_code(__node_type*, const __node_type*) const
   1194       { }
   1195 
   1196       void
   1197       _M_swap(_Hash_code_base& __x)
   1198       {
   1199 	std::swap(_M_extract(), __x._M_extract());
   1200 	std::swap(_M_h1(), __x._M_h1());
   1201 	std::swap(_M_h2(), __x._M_h2());
   1202       }
   1203 
   1204       const _ExtractKey&
   1205       _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
   1206 
   1207       _ExtractKey&
   1208       _M_extract() { return __ebo_extract_key::_S_get(*this); }
   1209 
   1210       const _H1&
   1211       _M_h1() const { return __ebo_h1::_S_cget(*this); }
   1212 
   1213       _H1&
   1214       _M_h1() { return __ebo_h1::_S_get(*this); }
   1215 
   1216       const _H2&
   1217       _M_h2() const { return __ebo_h2::_S_cget(*this); }
   1218 
   1219       _H2&
   1220       _M_h2() { return __ebo_h2::_S_get(*this); }
   1221     };
   1222 
   1223   /// Specialization: hash function and range-hashing function,
   1224   /// caching hash codes.  H is provided but ignored.  Provides
   1225   /// typedef and accessor required by C++ 11.
   1226   template<typename _Key, typename _Value, typename _ExtractKey,
   1227 	   typename _H1, typename _H2>
   1228     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
   1229 			   _Default_ranged_hash, true>
   1230     : private _Hashtable_ebo_helper<0, _ExtractKey>,
   1231       private _Hashtable_ebo_helper<1, _H1>,
   1232       private _Hashtable_ebo_helper<2, _H2>
   1233     {
   1234     private:
   1235       // Gives the local iterator implementation access to _M_h2().
   1236       friend struct _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2,
   1237 					 _Default_ranged_hash, true>;
   1238 
   1239       using __ebo_extract_key = _Hashtable_ebo_helper<0, _ExtractKey>;
   1240       using __ebo_h1 = _Hashtable_ebo_helper<1, _H1>;
   1241       using __ebo_h2 = _Hashtable_ebo_helper<2, _H2>;
   1242 
   1243     public:
   1244       typedef _H1 					hasher;
   1245 
   1246       hasher
   1247       hash_function() const
   1248       { return _M_h1(); }
   1249 
   1250     protected:
   1251       typedef std::size_t 				__hash_code;
   1252       typedef _Hash_node<_Value, true>			__node_type;
   1253 
   1254       _Hash_code_base(const _ExtractKey& __ex,
   1255 		      const _H1& __h1, const _H2& __h2,
   1256 		      const _Default_ranged_hash&)
   1257       : __ebo_extract_key(__ex), __ebo_h1(__h1), __ebo_h2(__h2) { }
   1258 
   1259       __hash_code
   1260       _M_hash_code(const _Key& __k) const
   1261       { return _M_h1()(__k); }
   1262 
   1263       std::size_t
   1264       _M_bucket_index(const _Key&, __hash_code __c,
   1265 		      std::size_t __n) const
   1266       { return _M_h2()(__c, __n); }
   1267 
   1268       std::size_t
   1269       _M_bucket_index(const __node_type* __p, std::size_t __n) const
   1270 	noexcept( noexcept(declval<const _H2&>()((__hash_code)0,
   1271 						 (std::size_t)0)) )
   1272       { return _M_h2()(__p->_M_hash_code, __n); }
   1273 
   1274       void
   1275       _M_store_code(__node_type* __n, __hash_code __c) const
   1276       { __n->_M_hash_code = __c; }
   1277 
   1278       void
   1279       _M_copy_code(__node_type* __to, const __node_type* __from) const
   1280       { __to->_M_hash_code = __from->_M_hash_code; }
   1281 
   1282       void
   1283       _M_swap(_Hash_code_base& __x)
   1284       {
   1285 	std::swap(_M_extract(), __x._M_extract());
   1286 	std::swap(_M_h1(), __x._M_h1());
   1287 	std::swap(_M_h2(), __x._M_h2());
   1288       }
   1289 
   1290       const _ExtractKey&
   1291       _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
   1292 
   1293       _ExtractKey&
   1294       _M_extract() { return __ebo_extract_key::_S_get(*this); }
   1295 
   1296       const _H1&
   1297       _M_h1() const { return __ebo_h1::_S_cget(*this); }
   1298 
   1299       _H1&
   1300       _M_h1() { return __ebo_h1::_S_get(*this); }
   1301 
   1302       const _H2&
   1303       _M_h2() const { return __ebo_h2::_S_cget(*this); }
   1304 
   1305       _H2&
   1306       _M_h2() { return __ebo_h2::_S_get(*this); }
   1307     };
   1308 
   1309   /**
   1310    *  Primary class template _Equal_helper.
   1311    *
   1312    */
   1313   template <typename _Key, typename _Value, typename _ExtractKey,
   1314 	    typename _Equal, typename _HashCodeType,
   1315 	    bool __cache_hash_code>
   1316   struct _Equal_helper;
   1317 
   1318   /// Specialization.
   1319   template<typename _Key, typename _Value, typename _ExtractKey,
   1320 	   typename _Equal, typename _HashCodeType>
   1321   struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
   1322   {
   1323     static bool
   1324     _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
   1325 	      const _Key& __k, _HashCodeType __c, _Hash_node<_Value, true>* __n)
   1326     { return __c == __n->_M_hash_code && __eq(__k, __extract(__n->_M_v())); }
   1327   };
   1328 
   1329   /// Specialization.
   1330   template<typename _Key, typename _Value, typename _ExtractKey,
   1331 	   typename _Equal, typename _HashCodeType>
   1332   struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
   1333   {
   1334     static bool
   1335     _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
   1336 	      const _Key& __k, _HashCodeType, _Hash_node<_Value, false>* __n)
   1337     { return __eq(__k, __extract(__n->_M_v())); }
   1338   };
   1339 
   1340 
   1341   /// Partial specialization used when nodes contain a cached hash code.
   1342   template<typename _Key, typename _Value, typename _ExtractKey,
   1343 	   typename _H1, typename _H2, typename _Hash>
   1344     struct _Local_iterator_base<_Key, _Value, _ExtractKey,
   1345 				_H1, _H2, _Hash, true>
   1346     : private _Hashtable_ebo_helper<0, _H2>
   1347     {
   1348     protected:
   1349       using __base_type = _Hashtable_ebo_helper<0, _H2>;
   1350       using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
   1351 					       _H1, _H2, _Hash, true>;
   1352 
   1353       _Local_iterator_base() = default;
   1354       _Local_iterator_base(const __hash_code_base& __base,
   1355 			   _Hash_node<_Value, true>* __p,
   1356 			   std::size_t __bkt, std::size_t __bkt_count)
   1357       : __base_type(__base._M_h2()),
   1358 	_M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
   1359 
   1360       void
   1361       _M_incr()
   1362       {
   1363 	_M_cur = _M_cur->_M_next();
   1364 	if (_M_cur)
   1365 	  {
   1366 	    std::size_t __bkt
   1367 	      = __base_type::_S_get(*this)(_M_cur->_M_hash_code,
   1368 					   _M_bucket_count);
   1369 	    if (__bkt != _M_bucket)
   1370 	      _M_cur = nullptr;
   1371 	  }
   1372       }
   1373 
   1374       _Hash_node<_Value, true>*  _M_cur;
   1375       std::size_t _M_bucket;
   1376       std::size_t _M_bucket_count;
   1377 
   1378     public:
   1379       const void*
   1380       _M_curr() const { return _M_cur; }  // for equality ops
   1381 
   1382       std::size_t
   1383       _M_get_bucket() const { return _M_bucket; }  // for debug mode
   1384     };
   1385 
   1386   // Uninitialized storage for a _Hash_code_base.
   1387   // This type is DefaultConstructible and Assignable even if the
   1388   // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
   1389   // can be DefaultConstructible and Assignable.
   1390   template<typename _Tp, bool _IsEmpty = std::is_empty<_Tp>::value>
   1391     struct _Hash_code_storage
   1392     {
   1393       __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
   1394 
   1395       _Tp*
   1396       _M_h() { return _M_storage._M_ptr(); }
   1397 
   1398       const _Tp*
   1399       _M_h() const { return _M_storage._M_ptr(); }
   1400     };
   1401 
   1402   // Empty partial specialization for empty _Hash_code_base types.
   1403   template<typename _Tp>
   1404     struct _Hash_code_storage<_Tp, true>
   1405     {
   1406       static_assert( std::is_empty<_Tp>::value, "Type must be empty" );
   1407 
   1408       // As _Tp is an empty type there will be no bytes written/read through
   1409       // the cast pointer, so no strict-aliasing violation.
   1410       _Tp*
   1411       _M_h() { return reinterpret_cast<_Tp*>(this); }
   1412 
   1413       const _Tp*
   1414       _M_h() const { return reinterpret_cast<const _Tp*>(this); }
   1415     };
   1416 
   1417   template<typename _Key, typename _Value, typename _ExtractKey,
   1418 	   typename _H1, typename _H2, typename _Hash>
   1419     using __hash_code_for_local_iter
   1420       = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey,
   1421 					   _H1, _H2, _Hash, false>>;
   1422 
   1423   // Partial specialization used when hash codes are not cached
   1424   template<typename _Key, typename _Value, typename _ExtractKey,
   1425 	   typename _H1, typename _H2, typename _Hash>
   1426     struct _Local_iterator_base<_Key, _Value, _ExtractKey,
   1427 				_H1, _H2, _Hash, false>
   1428     : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _H1, _H2, _Hash>
   1429     {
   1430     protected:
   1431       using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
   1432 					       _H1, _H2, _Hash, false>;
   1433 
   1434       _Local_iterator_base() : _M_bucket_count(-1) { }
   1435 
   1436       _Local_iterator_base(const __hash_code_base& __base,
   1437 			   _Hash_node<_Value, false>* __p,
   1438 			   std::size_t __bkt, std::size_t __bkt_count)
   1439       : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
   1440       { _M_init(__base); }
   1441 
   1442       ~_Local_iterator_base()
   1443       {
   1444 	if (_M_bucket_count != -1)
   1445 	  _M_destroy();
   1446       }
   1447 
   1448       _Local_iterator_base(const _Local_iterator_base& __iter)
   1449       : _M_cur(__iter._M_cur), _M_bucket(__iter._M_bucket),
   1450         _M_bucket_count(__iter._M_bucket_count)
   1451       {
   1452 	if (_M_bucket_count != -1)
   1453 	  _M_init(*__iter._M_h());
   1454       }
   1455 
   1456       _Local_iterator_base&
   1457       operator=(const _Local_iterator_base& __iter)
   1458       {
   1459 	if (_M_bucket_count != -1)
   1460 	  _M_destroy();
   1461 	_M_cur = __iter._M_cur;
   1462 	_M_bucket = __iter._M_bucket;
   1463 	_M_bucket_count = __iter._M_bucket_count;
   1464 	if (_M_bucket_count != -1)
   1465 	  _M_init(*__iter._M_h());
   1466 	return *this;
   1467       }
   1468 
   1469       void
   1470       _M_incr()
   1471       {
   1472 	_M_cur = _M_cur->_M_next();
   1473 	if (_M_cur)
   1474 	  {
   1475 	    std::size_t __bkt = this->_M_h()->_M_bucket_index(_M_cur,
   1476 							      _M_bucket_count);
   1477 	    if (__bkt != _M_bucket)
   1478 	      _M_cur = nullptr;
   1479 	  }
   1480       }
   1481 
   1482       _Hash_node<_Value, false>*  _M_cur;
   1483       std::size_t _M_bucket;
   1484       std::size_t _M_bucket_count;
   1485 
   1486       void
   1487       _M_init(const __hash_code_base& __base)
   1488       { ::new(this->_M_h()) __hash_code_base(__base); }
   1489 
   1490       void
   1491       _M_destroy() { this->_M_h()->~__hash_code_base(); }
   1492 
   1493     public:
   1494       const void*
   1495       _M_curr() const { return _M_cur; }  // for equality ops and debug mode
   1496 
   1497       std::size_t
   1498       _M_get_bucket() const { return _M_bucket; }  // for debug mode
   1499     };
   1500 
   1501   template<typename _Key, typename _Value, typename _ExtractKey,
   1502 	   typename _H1, typename _H2, typename _Hash, bool __cache>
   1503     inline bool
   1504     operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
   1505 					  _H1, _H2, _Hash, __cache>& __x,
   1506 	       const _Local_iterator_base<_Key, _Value, _ExtractKey,
   1507 					  _H1, _H2, _Hash, __cache>& __y)
   1508     { return __x._M_curr() == __y._M_curr(); }
   1509 
   1510   template<typename _Key, typename _Value, typename _ExtractKey,
   1511 	   typename _H1, typename _H2, typename _Hash, bool __cache>
   1512     inline bool
   1513     operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
   1514 					  _H1, _H2, _Hash, __cache>& __x,
   1515 	       const _Local_iterator_base<_Key, _Value, _ExtractKey,
   1516 					  _H1, _H2, _Hash, __cache>& __y)
   1517     { return __x._M_curr() != __y._M_curr(); }
   1518 
   1519   /// local iterators
   1520   template<typename _Key, typename _Value, typename _ExtractKey,
   1521 	   typename _H1, typename _H2, typename _Hash,
   1522 	   bool __constant_iterators, bool __cache>
   1523     struct _Local_iterator
   1524     : public _Local_iterator_base<_Key, _Value, _ExtractKey,
   1525 				  _H1, _H2, _Hash, __cache>
   1526     {
   1527     private:
   1528       using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
   1529 					       _H1, _H2, _Hash, __cache>;
   1530       using __hash_code_base = typename __base_type::__hash_code_base;
   1531     public:
   1532       typedef _Value					value_type;
   1533       typedef typename std::conditional<__constant_iterators,
   1534 					const _Value*, _Value*>::type
   1535 						       pointer;
   1536       typedef typename std::conditional<__constant_iterators,
   1537 					const _Value&, _Value&>::type
   1538 						       reference;
   1539       typedef std::ptrdiff_t				difference_type;
   1540       typedef std::forward_iterator_tag			iterator_category;
   1541 
   1542       _Local_iterator() = default;
   1543 
   1544       _Local_iterator(const __hash_code_base& __base,
   1545 		      _Hash_node<_Value, __cache>* __p,
   1546 		      std::size_t __bkt, std::size_t __bkt_count)
   1547 	: __base_type(__base, __p, __bkt, __bkt_count)
   1548       { }
   1549 
   1550       reference
   1551       operator*() const
   1552       { return this->_M_cur->_M_v(); }
   1553 
   1554       pointer
   1555       operator->() const
   1556       { return this->_M_cur->_M_valptr(); }
   1557 
   1558       _Local_iterator&
   1559       operator++()
   1560       {
   1561 	this->_M_incr();
   1562 	return *this;
   1563       }
   1564 
   1565       _Local_iterator
   1566       operator++(int)
   1567       {
   1568 	_Local_iterator __tmp(*this);
   1569 	this->_M_incr();
   1570 	return __tmp;
   1571       }
   1572     };
   1573 
   1574   /// local const_iterators
   1575   template<typename _Key, typename _Value, typename _ExtractKey,
   1576 	   typename _H1, typename _H2, typename _Hash,
   1577 	   bool __constant_iterators, bool __cache>
   1578     struct _Local_const_iterator
   1579     : public _Local_iterator_base<_Key, _Value, _ExtractKey,
   1580 				  _H1, _H2, _Hash, __cache>
   1581     {
   1582     private:
   1583       using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
   1584 					       _H1, _H2, _Hash, __cache>;
   1585       using __hash_code_base = typename __base_type::__hash_code_base;
   1586 
   1587     public:
   1588       typedef _Value					value_type;
   1589       typedef const _Value*				pointer;
   1590       typedef const _Value&				reference;
   1591       typedef std::ptrdiff_t				difference_type;
   1592       typedef std::forward_iterator_tag			iterator_category;
   1593 
   1594       _Local_const_iterator() = default;
   1595 
   1596       _Local_const_iterator(const __hash_code_base& __base,
   1597 			    _Hash_node<_Value, __cache>* __p,
   1598 			    std::size_t __bkt, std::size_t __bkt_count)
   1599 	: __base_type(__base, __p, __bkt, __bkt_count)
   1600       { }
   1601 
   1602       _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
   1603 						  _H1, _H2, _Hash,
   1604 						  __constant_iterators,
   1605 						  __cache>& __x)
   1606 	: __base_type(__x)
   1607       { }
   1608 
   1609       reference
   1610       operator*() const
   1611       { return this->_M_cur->_M_v(); }
   1612 
   1613       pointer
   1614       operator->() const
   1615       { return this->_M_cur->_M_valptr(); }
   1616 
   1617       _Local_const_iterator&
   1618       operator++()
   1619       {
   1620 	this->_M_incr();
   1621 	return *this;
   1622       }
   1623 
   1624       _Local_const_iterator
   1625       operator++(int)
   1626       {
   1627 	_Local_const_iterator __tmp(*this);
   1628 	this->_M_incr();
   1629 	return __tmp;
   1630       }
   1631     };
   1632 
   1633   /**
   1634    *  Primary class template _Hashtable_base.
   1635    *
   1636    *  Helper class adding management of _Equal functor to
   1637    *  _Hash_code_base type.
   1638    *
   1639    *  Base class templates are:
   1640    *    - __detail::_Hash_code_base
   1641    *    - __detail::_Hashtable_ebo_helper
   1642    */
   1643   template<typename _Key, typename _Value,
   1644 	   typename _ExtractKey, typename _Equal,
   1645 	   typename _H1, typename _H2, typename _Hash, typename _Traits>
   1646   struct _Hashtable_base
   1647   : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
   1648 			   _Traits::__hash_cached::value>,
   1649     private _Hashtable_ebo_helper<0, _Equal>
   1650   {
   1651   public:
   1652     typedef _Key					key_type;
   1653     typedef _Value					value_type;
   1654     typedef _Equal					key_equal;
   1655     typedef std::size_t					size_type;
   1656     typedef std::ptrdiff_t				difference_type;
   1657 
   1658     using __traits_type = _Traits;
   1659     using __hash_cached = typename __traits_type::__hash_cached;
   1660     using __constant_iterators = typename __traits_type::__constant_iterators;
   1661     using __unique_keys = typename __traits_type::__unique_keys;
   1662 
   1663     using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
   1664 					     _H1, _H2, _Hash,
   1665 					     __hash_cached::value>;
   1666 
   1667     using __hash_code = typename __hash_code_base::__hash_code;
   1668     using __node_type = typename __hash_code_base::__node_type;
   1669 
   1670     using iterator = __detail::_Node_iterator<value_type,
   1671 					      __constant_iterators::value,
   1672 					      __hash_cached::value>;
   1673 
   1674     using const_iterator = __detail::_Node_const_iterator<value_type,
   1675 						   __constant_iterators::value,
   1676 						   __hash_cached::value>;
   1677 
   1678     using local_iterator = __detail::_Local_iterator<key_type, value_type,
   1679 						  _ExtractKey, _H1, _H2, _Hash,
   1680 						  __constant_iterators::value,
   1681 						     __hash_cached::value>;
   1682 
   1683     using const_local_iterator = __detail::_Local_const_iterator<key_type,
   1684 								 value_type,
   1685 					_ExtractKey, _H1, _H2, _Hash,
   1686 					__constant_iterators::value,
   1687 					__hash_cached::value>;
   1688 
   1689     using __ireturn_type = typename std::conditional<__unique_keys::value,
   1690 						     std::pair<iterator, bool>,
   1691 						     iterator>::type;
   1692   private:
   1693     using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
   1694     using _EqualHelper =  _Equal_helper<_Key, _Value, _ExtractKey, _Equal,
   1695 					__hash_code, __hash_cached::value>;
   1696 
   1697   protected:
   1698     _Hashtable_base(const _ExtractKey& __ex, const _H1& __h1, const _H2& __h2,
   1699 		    const _Hash& __hash, const _Equal& __eq)
   1700     : __hash_code_base(__ex, __h1, __h2, __hash), _EqualEBO(__eq)
   1701     { }
   1702 
   1703     bool
   1704     _M_equals(const _Key& __k, __hash_code __c, __node_type* __n) const
   1705     {
   1706       return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
   1707 				     __k, __c, __n);
   1708     }
   1709 
   1710     void
   1711     _M_swap(_Hashtable_base& __x)
   1712     {
   1713       __hash_code_base::_M_swap(__x);
   1714       std::swap(_M_eq(), __x._M_eq());
   1715     }
   1716 
   1717     const _Equal&
   1718     _M_eq() const { return _EqualEBO::_S_cget(*this); }
   1719 
   1720     _Equal&
   1721     _M_eq() { return _EqualEBO::_S_get(*this); }
   1722   };
   1723 
   1724   /**
   1725    *  struct _Equality_base.
   1726    *
   1727    *  Common types and functions for class _Equality.
   1728    */
   1729   struct _Equality_base
   1730   {
   1731   protected:
   1732     template<typename _Uiterator>
   1733       static bool
   1734       _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
   1735   };
   1736 
   1737   // See std::is_permutation in N3068.
   1738   template<typename _Uiterator>
   1739     bool
   1740     _Equality_base::
   1741     _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
   1742 		      _Uiterator __first2)
   1743     {
   1744       for (; __first1 != __last1; ++__first1, ++__first2)
   1745 	if (!(*__first1 == *__first2))
   1746 	  break;
   1747 
   1748       if (__first1 == __last1)
   1749 	return true;
   1750 
   1751       _Uiterator __last2 = __first2;
   1752       std::advance(__last2, std::distance(__first1, __last1));
   1753 
   1754       for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
   1755 	{
   1756 	  _Uiterator __tmp =  __first1;
   1757 	  while (__tmp != __it1 && !bool(*__tmp == *__it1))
   1758 	    ++__tmp;
   1759 
   1760 	  // We've seen this one before.
   1761 	  if (__tmp != __it1)
   1762 	    continue;
   1763 
   1764 	  std::ptrdiff_t __n2 = 0;
   1765 	  for (__tmp = __first2; __tmp != __last2; ++__tmp)
   1766 	    if (*__tmp == *__it1)
   1767 	      ++__n2;
   1768 
   1769 	  if (!__n2)
   1770 	    return false;
   1771 
   1772 	  std::ptrdiff_t __n1 = 0;
   1773 	  for (__tmp = __it1; __tmp != __last1; ++__tmp)
   1774 	    if (*__tmp == *__it1)
   1775 	      ++__n1;
   1776 
   1777 	  if (__n1 != __n2)
   1778 	    return false;
   1779 	}
   1780       return true;
   1781     }
   1782 
   1783   /**
   1784    *  Primary class template  _Equality.
   1785    *
   1786    *  This is for implementing equality comparison for unordered
   1787    *  containers, per N3068, by John Lakos and Pablo Halpern.
   1788    *  Algorithmically, we follow closely the reference implementations
   1789    *  therein.
   1790    */
   1791   template<typename _Key, typename _Value, typename _Alloc,
   1792 	   typename _ExtractKey, typename _Equal,
   1793 	   typename _H1, typename _H2, typename _Hash,
   1794 	   typename _RehashPolicy, typename _Traits,
   1795 	   bool _Unique_keys = _Traits::__unique_keys::value>
   1796     struct _Equality;
   1797 
   1798   /// Specialization.
   1799   template<typename _Key, typename _Value, typename _Alloc,
   1800 	   typename _ExtractKey, typename _Equal,
   1801 	   typename _H1, typename _H2, typename _Hash,
   1802 	   typename _RehashPolicy, typename _Traits>
   1803     struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
   1804 		     _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
   1805     {
   1806       using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
   1807 				     _H1, _H2, _Hash, _RehashPolicy, _Traits>;
   1808 
   1809       bool
   1810       _M_equal(const __hashtable&) const;
   1811     };
   1812 
   1813   template<typename _Key, typename _Value, typename _Alloc,
   1814 	   typename _ExtractKey, typename _Equal,
   1815 	   typename _H1, typename _H2, typename _Hash,
   1816 	   typename _RehashPolicy, typename _Traits>
   1817     bool
   1818     _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
   1819 	      _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
   1820     _M_equal(const __hashtable& __other) const
   1821     {
   1822       const __hashtable* __this = static_cast<const __hashtable*>(this);
   1823 
   1824       if (__this->size() != __other.size())
   1825 	return false;
   1826 
   1827       for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
   1828 	{
   1829 	  const auto __ity = __other.find(_ExtractKey()(*__itx));
   1830 	  if (__ity == __other.end() || !bool(*__ity == *__itx))
   1831 	    return false;
   1832 	}
   1833       return true;
   1834     }
   1835 
   1836   /// Specialization.
   1837   template<typename _Key, typename _Value, typename _Alloc,
   1838 	   typename _ExtractKey, typename _Equal,
   1839 	   typename _H1, typename _H2, typename _Hash,
   1840 	   typename _RehashPolicy, typename _Traits>
   1841     struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
   1842 		     _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
   1843     : public _Equality_base
   1844     {
   1845       using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
   1846 				     _H1, _H2, _Hash, _RehashPolicy, _Traits>;
   1847 
   1848       bool
   1849       _M_equal(const __hashtable&) const;
   1850     };
   1851 
   1852   template<typename _Key, typename _Value, typename _Alloc,
   1853 	   typename _ExtractKey, typename _Equal,
   1854 	   typename _H1, typename _H2, typename _Hash,
   1855 	   typename _RehashPolicy, typename _Traits>
   1856     bool
   1857     _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
   1858 	      _H1, _H2, _Hash, _RehashPolicy, _Traits, false>::
   1859     _M_equal(const __hashtable& __other) const
   1860     {
   1861       const __hashtable* __this = static_cast<const __hashtable*>(this);
   1862 
   1863       if (__this->size() != __other.size())
   1864 	return false;
   1865 
   1866       for (auto __itx = __this->begin(); __itx != __this->end();)
   1867 	{
   1868 	  const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
   1869 	  const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
   1870 
   1871 	  if (std::distance(__xrange.first, __xrange.second)
   1872 	      != std::distance(__yrange.first, __yrange.second))
   1873 	    return false;
   1874 
   1875 	  if (!_S_is_permutation(__xrange.first, __xrange.second,
   1876 				 __yrange.first))
   1877 	    return false;
   1878 
   1879 	  __itx = __xrange.second;
   1880 	}
   1881       return true;
   1882     }
   1883 
   1884   /**
   1885    * This type deals with all allocation and keeps an allocator instance through
   1886    * inheritance to benefit from EBO when possible.
   1887    */
   1888   template<typename _NodeAlloc>
   1889     struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc>
   1890     {
   1891     private:
   1892       using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>;
   1893     public:
   1894       using __node_type = typename _NodeAlloc::value_type;
   1895       using __node_alloc_type = _NodeAlloc;
   1896       // Use __gnu_cxx to benefit from _S_always_equal and al.
   1897       using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
   1898 
   1899       using __value_type = typename __node_type::value_type;
   1900       using __value_alloc_type =
   1901 	typename __alloctr_rebind<__node_alloc_type, __value_type>::__type;
   1902       using __value_alloc_traits = std::allocator_traits<__value_alloc_type>;
   1903 
   1904       using __node_base = __detail::_Hash_node_base;
   1905       using __bucket_type = __node_base*;
   1906       using __bucket_alloc_type =
   1907 	typename __alloctr_rebind<__node_alloc_type, __bucket_type>::__type;
   1908       using __bucket_alloc_traits = std::allocator_traits<__bucket_alloc_type>;
   1909 
   1910       _Hashtable_alloc(const _Hashtable_alloc&) = default;
   1911       _Hashtable_alloc(_Hashtable_alloc&&) = default;
   1912 
   1913       template<typename _Alloc>
   1914 	_Hashtable_alloc(_Alloc&& __a)
   1915 	  : __ebo_node_alloc(std::forward<_Alloc>(__a))
   1916 	{ }
   1917 
   1918       __node_alloc_type&
   1919       _M_node_allocator()
   1920       { return __ebo_node_alloc::_S_get(*this); }
   1921 
   1922       const __node_alloc_type&
   1923       _M_node_allocator() const
   1924       { return __ebo_node_alloc::_S_cget(*this); }
   1925 
   1926       template<typename... _Args>
   1927 	__node_type*
   1928 	_M_allocate_node(_Args&&... __args);
   1929 
   1930       void
   1931       _M_deallocate_node(__node_type* __n);
   1932 
   1933       // Deallocate the linked list of nodes pointed to by __n
   1934       void
   1935       _M_deallocate_nodes(__node_type* __n);
   1936 
   1937       __bucket_type*
   1938       _M_allocate_buckets(std::size_t __n);
   1939 
   1940       void
   1941       _M_deallocate_buckets(__bucket_type*, std::size_t __n);
   1942     };
   1943 
   1944   // Definitions of class template _Hashtable_alloc's out-of-line member
   1945   // functions.
   1946   template<typename _NodeAlloc>
   1947     template<typename... _Args>
   1948       typename _Hashtable_alloc<_NodeAlloc>::__node_type*
   1949       _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
   1950       {
   1951 	auto __nptr = __node_alloc_traits::allocate(_M_node_allocator(), 1);
   1952 	__node_type* __n = std::__addressof(*__nptr);
   1953 	__try
   1954 	  {
   1955 	    __value_alloc_type __a(_M_node_allocator());
   1956 	    ::new ((void*)__n) __node_type;
   1957 	    __value_alloc_traits::construct(__a, __n->_M_valptr(),
   1958 					    std::forward<_Args>(__args)...);
   1959 	    return __n;
   1960 	  }
   1961 	__catch(...)
   1962 	  {
   1963 	    __node_alloc_traits::deallocate(_M_node_allocator(), __nptr, 1);
   1964 	    __throw_exception_again;
   1965 	  }
   1966       }
   1967 
   1968   template<typename _NodeAlloc>
   1969     void
   1970     _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_type* __n)
   1971     {
   1972       typedef typename __node_alloc_traits::pointer _Ptr;
   1973       auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
   1974       __value_alloc_type __a(_M_node_allocator());
   1975       __value_alloc_traits::destroy(__a, __n->_M_valptr());
   1976       __n->~__node_type();
   1977       __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
   1978     }
   1979 
   1980   template<typename _NodeAlloc>
   1981     void
   1982     _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_type* __n)
   1983     {
   1984       while (__n)
   1985 	{
   1986 	  __node_type* __tmp = __n;
   1987 	  __n = __n->_M_next();
   1988 	  _M_deallocate_node(__tmp);
   1989 	}
   1990     }
   1991 
   1992   template<typename _NodeAlloc>
   1993     typename _Hashtable_alloc<_NodeAlloc>::__bucket_type*
   1994     _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __n)
   1995     {
   1996       __bucket_alloc_type __alloc(_M_node_allocator());
   1997 
   1998       auto __ptr = __bucket_alloc_traits::allocate(__alloc, __n);
   1999       __bucket_type* __p = std::__addressof(*__ptr);
   2000       __builtin_memset(__p, 0, __n * sizeof(__bucket_type));
   2001       return __p;
   2002     }
   2003 
   2004   template<typename _NodeAlloc>
   2005     void
   2006     _Hashtable_alloc<_NodeAlloc>::_M_deallocate_buckets(__bucket_type* __bkts,
   2007 							std::size_t __n)
   2008     {
   2009       typedef typename __bucket_alloc_traits::pointer _Ptr;
   2010       auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
   2011       __bucket_alloc_type __alloc(_M_node_allocator());
   2012       __bucket_alloc_traits::deallocate(__alloc, __ptr, __n);
   2013     }
   2014 
   2015  //@} hashtable-detail
   2016 _GLIBCXX_END_NAMESPACE_VERSION
   2017 } // namespace __detail
   2018 } // namespace std
   2019 
   2020 #endif // _HASHTABLE_POLICY_H
   2021