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