OSDN Git Service

2011-12-29 François Dumont <fdumont@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / hashtable_policy.h
1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
2
3 // Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /** @file bits/hashtable_policy.h
26  *  This is an internal header file, included by other library headers.
27  *  Do not attempt to use it directly.
28  *  @headername{unordered_map,unordered_set}
29  */
30
31 #ifndef _HASHTABLE_POLICY_H
32 #define _HASHTABLE_POLICY_H 1
33
34 namespace std _GLIBCXX_VISIBILITY(default)
35 {
36 namespace __detail
37 {
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
39
40   // Helper function: return distance(first, last) for forward
41   // iterators, or 0 for input iterators.
42   template<class _Iterator>
43     inline typename std::iterator_traits<_Iterator>::difference_type
44     __distance_fw(_Iterator __first, _Iterator __last,
45                   std::input_iterator_tag)
46     { return 0; }
47
48   template<class _Iterator>
49     inline typename std::iterator_traits<_Iterator>::difference_type
50     __distance_fw(_Iterator __first, _Iterator __last,
51                   std::forward_iterator_tag)
52     { return std::distance(__first, __last); }
53
54   template<class _Iterator>
55     inline typename std::iterator_traits<_Iterator>::difference_type
56     __distance_fw(_Iterator __first, _Iterator __last)
57     {
58       typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
59       return __distance_fw(__first, __last, _Tag());
60     }
61
62   // Helper type used to detect when the hash functor is noexcept qualified or
63   // not
64   template <typename _Key, typename _Hash>
65     struct __is_noexcept_hash : std::integral_constant<bool,
66         noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
67     {};
68
69   // Auxiliary types used for all instantiations of _Hashtable: nodes
70   // and iterators.
71
72   // Nodes, used to wrap elements stored in the hash table.  A policy
73   // template parameter of class template _Hashtable controls whether
74   // nodes also store a hash code. In some cases (e.g. strings) this
75   // may be a performance win.
76   template<typename _Value, bool __cache_hash_code>
77     struct _Hash_node;
78
79   template<typename _Value>
80     struct _Hash_node<_Value, true>
81     {
82       _Value       _M_v;
83       std::size_t  _M_hash_code;
84       _Hash_node*  _M_next;
85
86       template<typename... _Args>
87         _Hash_node(_Args&&... __args)
88         : _M_v(std::forward<_Args>(__args)...),
89           _M_hash_code(), _M_next() { }
90     };
91
92   template<typename _Value>
93     struct _Hash_node<_Value, false>
94     {
95       _Value       _M_v;
96       _Hash_node*  _M_next;
97
98       template<typename... _Args>
99         _Hash_node(_Args&&... __args)
100         : _M_v(std::forward<_Args>(__args)...),
101           _M_next() { }
102     };
103
104   // Node iterators, used to iterate through all the hashtable.
105   template<typename _Value, bool __cache>
106     struct _Node_iterator_base
107     {
108       _Node_iterator_base(_Hash_node<_Value, __cache>* __p)
109       : _M_cur(__p) { }
110
111       void
112       _M_incr()
113       { _M_cur = _M_cur->_M_next; }
114
115       _Hash_node<_Value, __cache>*  _M_cur;
116     };
117
118   template<typename _Value, bool __cache>
119     inline bool
120     operator==(const _Node_iterator_base<_Value, __cache>& __x,
121                const _Node_iterator_base<_Value, __cache>& __y)
122     { return __x._M_cur == __y._M_cur; }
123
124   template<typename _Value, bool __cache>
125     inline bool
126     operator!=(const _Node_iterator_base<_Value, __cache>& __x,
127                const _Node_iterator_base<_Value, __cache>& __y)
128     { return __x._M_cur != __y._M_cur; }
129
130   template<typename _Value, bool __constant_iterators, bool __cache>
131     struct _Node_iterator
132     : public _Node_iterator_base<_Value, __cache>
133     {
134       typedef _Value                                   value_type;
135       typedef typename std::conditional<__constant_iterators,
136                                         const _Value*, _Value*>::type
137                                                        pointer;
138       typedef typename std::conditional<__constant_iterators,
139                                         const _Value&, _Value&>::type
140                                                        reference;
141       typedef std::ptrdiff_t                           difference_type;
142       typedef std::forward_iterator_tag                iterator_category;
143
144       _Node_iterator()
145       : _Node_iterator_base<_Value, __cache>(0) { }
146
147       explicit
148       _Node_iterator(_Hash_node<_Value, __cache>* __p)
149       : _Node_iterator_base<_Value, __cache>(__p) { }
150
151       reference
152       operator*() const
153       { return this->_M_cur->_M_v; }
154
155       pointer
156       operator->() const
157       { return std::__addressof(this->_M_cur->_M_v); }
158
159       _Node_iterator&
160       operator++()
161       {
162         this->_M_incr();
163         return *this;
164       }
165
166       _Node_iterator
167       operator++(int)
168       {
169         _Node_iterator __tmp(*this);
170         this->_M_incr();
171         return __tmp;
172       }
173     };
174
175   template<typename _Value, bool __constant_iterators, bool __cache>
176     struct _Node_const_iterator
177     : public _Node_iterator_base<_Value, __cache>
178     {
179       typedef _Value                                   value_type;
180       typedef const _Value*                            pointer;
181       typedef const _Value&                            reference;
182       typedef std::ptrdiff_t                           difference_type;
183       typedef std::forward_iterator_tag                iterator_category;
184
185       _Node_const_iterator()
186       : _Node_iterator_base<_Value, __cache>(0) { }
187
188       explicit
189       _Node_const_iterator(_Hash_node<_Value, __cache>* __p)
190       : _Node_iterator_base<_Value, __cache>(__p) { }
191
192       _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
193                            __cache>& __x)
194       : _Node_iterator_base<_Value, __cache>(__x._M_cur) { }
195
196       reference
197       operator*() const
198       { return this->_M_cur->_M_v; }
199
200       pointer
201       operator->() const
202       { return std::__addressof(this->_M_cur->_M_v); }
203
204       _Node_const_iterator&
205       operator++()
206       {
207         this->_M_incr();
208         return *this;
209       }
210
211       _Node_const_iterator
212       operator++(int)
213       {
214         _Node_const_iterator __tmp(*this);
215         this->_M_incr();
216         return __tmp;
217       }
218     };
219
220   // Many of class template _Hashtable's template parameters are policy
221   // classes.  These are defaults for the policies.
222
223   // Default range hashing function: use division to fold a large number
224   // into the range [0, N).
225   struct _Mod_range_hashing
226   {
227     typedef std::size_t first_argument_type;
228     typedef std::size_t second_argument_type;
229     typedef std::size_t result_type;
230
231     result_type
232     operator()(first_argument_type __num, second_argument_type __den) const
233     { return __num % __den; }
234   };
235
236   // Default ranged hash function H.  In principle it should be a
237   // function object composed from objects of type H1 and H2 such that
238   // h(k, N) = h2(h1(k), N), but that would mean making extra copies of
239   // h1 and h2.  So instead we'll just use a tag to tell class template
240   // hashtable to do that composition.
241   struct _Default_ranged_hash { };
242
243   // Default value for rehash policy.  Bucket size is (usually) the
244   // smallest prime that keeps the load factor small enough.
245   struct _Prime_rehash_policy
246   {
247     _Prime_rehash_policy(float __z = 1.0)
248     : _M_max_load_factor(__z), _M_prev_resize(0), _M_next_resize(0) { }
249
250     float
251     max_load_factor() const noexcept
252     { return _M_max_load_factor; }
253
254     // Return a bucket size no smaller than n.
255     std::size_t
256     _M_next_bkt(std::size_t __n) const;
257
258     // Return a bucket count appropriate for n elements
259     std::size_t
260     _M_bkt_for_elements(std::size_t __n) const;
261
262     // __n_bkt is current bucket count, __n_elt is current element count,
263     // and __n_ins is number of elements to be inserted.  Do we need to
264     // increase bucket count?  If so, return make_pair(true, n), where n
265     // is the new bucket count.  If not, return make_pair(false, 0).
266     std::pair<bool, std::size_t>
267     _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
268                    std::size_t __n_ins) const;
269
270     typedef std::pair<std::size_t, std::size_t> _State;
271
272     _State
273     _M_state() const
274     { return std::make_pair(_M_prev_resize, _M_next_resize); }
275
276     void
277     _M_reset(const _State& __state)
278     {
279       _M_prev_resize = __state.first;
280       _M_next_resize = __state.second;
281     }
282
283     enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
284
285     float                _M_max_load_factor;
286     mutable std::size_t  _M_prev_resize;
287     mutable std::size_t  _M_next_resize;
288   };
289
290   extern const unsigned long __prime_list[];
291
292   // XXX This is a hack.  There's no good reason for any of
293   // _Prime_rehash_policy's member functions to be inline.
294
295   // Return a prime no smaller than n.
296   inline std::size_t
297   _Prime_rehash_policy::
298   _M_next_bkt(std::size_t __n) const
299   {
300     // Optimize lookups involving the first elements of __prime_list.
301     // (useful to speed-up, eg, constructors)
302     static const unsigned char __fast_bkt[12]
303       = { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 };
304
305     if (__n <= 11)
306       {
307         _M_prev_resize = 0;
308         _M_next_resize
309           = __builtin_ceil(__fast_bkt[__n] * (long double)_M_max_load_factor);
310         return __fast_bkt[__n];
311       }
312
313     const unsigned long* __p
314       = std::lower_bound(__prime_list + 5, __prime_list + _S_n_primes, __n);
315
316     // Shrink will take place only if the number of elements is small enough
317     // so that the prime number 2 steps before __p is large enough to still
318     // conform to the max load factor:
319     _M_prev_resize
320       = __builtin_floor(*(__p - 2) * (long double)_M_max_load_factor);
321
322     // Let's guaranty that a minimal grow step of 11 is used
323     if (*__p - __n < 11)
324       __p = std::lower_bound(__p, __prime_list + _S_n_primes, __n + 11);
325     _M_next_resize = __builtin_ceil(*__p * (long double)_M_max_load_factor);
326     return *__p;
327   }
328
329   // Return the smallest prime p such that alpha p >= n, where alpha
330   // is the load factor.
331   inline std::size_t
332   _Prime_rehash_policy::
333   _M_bkt_for_elements(std::size_t __n) const
334   { return _M_next_bkt(__builtin_ceil(__n / (long double)_M_max_load_factor)); }
335
336   // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
337   // If p > __n_bkt, return make_pair(true, p); otherwise return
338   // make_pair(false, 0).  In principle this isn't very different from
339   // _M_bkt_for_elements.
340
341   // The only tricky part is that we're caching the element count at
342   // which we need to rehash, so we don't have to do a floating-point
343   // multiply for every insertion.
344
345   inline std::pair<bool, std::size_t>
346   _Prime_rehash_policy::
347   _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
348                  std::size_t __n_ins) const
349   {
350     if (__n_elt + __n_ins >= _M_next_resize)
351       {
352         long double __min_bkts = (__n_elt + __n_ins)
353                                  / (long double)_M_max_load_factor;
354         if (__min_bkts >= __n_bkt)
355           return std::make_pair(true,
356                                 _M_next_bkt(__builtin_floor(__min_bkts) + 1));
357         else
358           {
359             _M_next_resize
360               = __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
361             return std::make_pair(false, 0);
362           }
363       }
364     else if (__n_elt + __n_ins < _M_prev_resize)
365       {
366         long double __min_bkts = (__n_elt + __n_ins)
367                                  / (long double)_M_max_load_factor;
368         return std::make_pair(true,
369                               _M_next_bkt(__builtin_floor(__min_bkts) + 1));
370       }
371     else
372       return std::make_pair(false, 0);
373   }
374
375   // Base classes for std::_Hashtable.  We define these base classes
376   // because in some cases we want to do different things depending
377   // on the value of a policy class.  In some cases the policy class
378   // affects which member functions and nested typedefs are defined;
379   // we handle that by specializing base class templates.  Several of
380   // the base class templates need to access other members of class
381   // template _Hashtable, so we use the "curiously recurring template
382   // pattern" for them.
383
384   // class template _Map_base.  If the hashtable has a value type of
385   // the form pair<T1, T2> and a key extraction policy that returns the
386   // first part of the pair, the hashtable gets a mapped_type typedef.
387   // If it satisfies those criteria and also has unique keys, then it
388   // also gets an operator[].
389   template<typename _Key, typename _Value, typename _Ex, bool __unique,
390            typename _Hashtable>
391     struct _Map_base { };
392
393   template<typename _Key, typename _Pair, typename _Hashtable>
394     struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, false, _Hashtable>
395     {
396       typedef typename _Pair::second_type mapped_type;
397     };
398
399   template<typename _Key, typename _Pair, typename _Hashtable>
400     struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>
401     {
402       typedef typename _Pair::second_type mapped_type;
403
404       mapped_type&
405       operator[](const _Key& __k);
406
407       mapped_type&
408       operator[](_Key&& __k);
409
410       // _GLIBCXX_RESOLVE_LIB_DEFECTS
411       // DR 761. unordered_map needs an at() member function.
412       mapped_type&
413       at(const _Key& __k);
414
415       const mapped_type&
416       at(const _Key& __k) const;
417     };
418
419   template<typename _Key, typename _Pair, typename _Hashtable>
420     typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
421                        true, _Hashtable>::mapped_type&
422     _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
423     operator[](const _Key& __k)
424     {
425       _Hashtable* __h = static_cast<_Hashtable*>(this);
426       typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
427       std::size_t __n = __h->_M_bucket_index(__k, __code);
428
429       typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
430       if (!__p)
431         return __h->_M_insert_bucket(std::make_pair(__k, mapped_type()),
432                                      __n, __code)->second;
433       return (__p->_M_v).second;
434     }
435
436   template<typename _Key, typename _Pair, typename _Hashtable>
437     typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
438                        true, _Hashtable>::mapped_type&
439     _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
440     operator[](_Key&& __k)
441     {
442       _Hashtable* __h = static_cast<_Hashtable*>(this);
443       typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
444       std::size_t __n = __h->_M_bucket_index(__k, __code);
445
446       typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
447       if (!__p)
448         return __h->_M_insert_bucket(std::make_pair(std::move(__k),
449                                                     mapped_type()),
450                                      __n, __code)->second;
451       return (__p->_M_v).second;
452     }
453
454   template<typename _Key, typename _Pair, typename _Hashtable>
455     typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
456                        true, _Hashtable>::mapped_type&
457     _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
458     at(const _Key& __k)
459     {
460       _Hashtable* __h = static_cast<_Hashtable*>(this);
461       typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
462       std::size_t __n = __h->_M_bucket_index(__k, __code);
463
464       typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
465       if (!__p)
466         __throw_out_of_range(__N("_Map_base::at"));
467       return (__p->_M_v).second;
468     }
469
470   template<typename _Key, typename _Pair, typename _Hashtable>
471     const typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
472                              true, _Hashtable>::mapped_type&
473     _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
474     at(const _Key& __k) const
475     {
476       const _Hashtable* __h = static_cast<const _Hashtable*>(this);
477       typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
478       std::size_t __n = __h->_M_bucket_index(__k, __code);
479
480       typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
481       if (!__p)
482         __throw_out_of_range(__N("_Map_base::at"));
483       return (__p->_M_v).second;
484     }
485
486   // class template _Rehash_base.  Give hashtable the max_load_factor
487   // functions and reserve iff the rehash policy is _Prime_rehash_policy.
488   template<typename _RehashPolicy, typename _Hashtable>
489     struct _Rehash_base { };
490
491   template<typename _Hashtable>
492     struct _Rehash_base<_Prime_rehash_policy, _Hashtable>
493     {
494       float
495       max_load_factor() const noexcept
496       {
497         const _Hashtable* __this = static_cast<const _Hashtable*>(this);
498         return __this->__rehash_policy().max_load_factor();
499       }
500
501       void
502       max_load_factor(float __z)
503       {
504         _Hashtable* __this = static_cast<_Hashtable*>(this);
505         __this->__rehash_policy(_Prime_rehash_policy(__z));
506       }
507
508       void
509       reserve(std::size_t __n)
510       {
511         _Hashtable* __this = static_cast<_Hashtable*>(this);
512         __this->rehash(__builtin_ceil(__n / max_load_factor()));
513       }
514     };
515
516   // Helper class using EBO when it is not forbidden, type is not final,
517   // and when it worth it, type is empty.
518   template<int _N, typename _Tp,
519            bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
520     struct _Ebo_helper;
521
522   // Specialization using EBO
523   template<int _N, typename _Tp>
524     struct _Ebo_helper<_N, _Tp, true> : _Tp
525     {
526       _Ebo_helper() = default;
527       _Ebo_helper(const _Tp& __tp) : _Tp(__tp)
528       { }
529
530       static const _Tp&
531       _S_cget(const _Ebo_helper<_N, _Tp, true>& __eboh)
532       { return static_cast<const _Tp&>(__eboh); }
533
534       static _Tp&
535       _S_get(_Ebo_helper<_N, _Tp, true>& __eboh)
536       { return static_cast<_Tp&>(__eboh); }
537     };
538
539   // Specialization not using EBO
540   template<int _N, typename _Tp>
541     struct _Ebo_helper<_N, _Tp, false>
542     {
543       _Ebo_helper() = default;
544       _Ebo_helper(const _Tp& __tp) : m_tp(__tp)
545       { }
546
547       static const _Tp&
548       _S_cget(const _Ebo_helper<_N, _Tp, false>& __eboh)
549       { return __eboh.m_tp; }
550
551       static _Tp&
552       _S_get(_Ebo_helper<_N, _Tp, false>& __eboh)
553       { return __eboh.m_tp; }
554
555     private:
556       _Tp m_tp;
557     };
558
559   // Class template _Hash_code_base.  Encapsulates two policy issues that
560   // aren't quite orthogonal.
561   //   (1) the difference between using a ranged hash function and using
562   //       the combination of a hash function and a range-hashing function.
563   //       In the former case we don't have such things as hash codes, so
564   //       we have a dummy type as placeholder.
565   //   (2) Whether or not we cache hash codes.  Caching hash codes is
566   //       meaningless if we have a ranged hash function.
567   // We also put the key extraction objects here, for convenience.
568   //
569   // Each specialization derives from one or more of the template parameters to
570   // benefit from Ebo. This is important as this type is inherited in some cases
571   // by the _Local_iterator_base type used to implement local_iterator and
572   // const_local_iterator. As with any iterator type we prefer to make it as
573   // small as possible.
574
575   // Primary template: unused except as a hook for specializations.
576   template<typename _Key, typename _Value, typename _ExtractKey,
577            typename _H1, typename _H2, typename _Hash,
578            bool __cache_hash_code>
579     struct _Hash_code_base;
580
581   // Specialization: ranged hash function, no caching hash codes.  H1
582   // and H2 are provided but ignored.  We define a dummy hash code type.
583   template<typename _Key, typename _Value, typename _ExtractKey, 
584            typename _H1, typename _H2, typename _Hash>
585     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
586       : _Ebo_helper<0, _ExtractKey>, _Ebo_helper<1, _Hash>
587     {
588     private:
589       typedef _Ebo_helper<0, _ExtractKey> _EboExtractKey;
590       typedef _Ebo_helper<1, _Hash> _EboHash;
591     protected:
592       // We need the default constructor for the local iterators.
593       _Hash_code_base() = default;
594       _Hash_code_base(const _ExtractKey& __ex,
595                       const _H1&, const _H2&, const _Hash& __h)
596         : _EboExtractKey(__ex), _EboHash(__h) { }
597
598       typedef void* _Hash_code_type;
599
600       _Hash_code_type
601       _M_hash_code(const _Key& __key) const
602       { return 0; }
603
604       std::size_t
605       _M_bucket_index(const _Key& __k, _Hash_code_type,
606                       std::size_t __n) const
607       { return _M_ranged_hash()(__k, __n); }
608
609       std::size_t
610       _M_bucket_index(const _Hash_node<_Value, false>* __p,
611                       std::size_t __n) const
612       { return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
613
614       void
615       _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
616       { }
617
618       void
619       _M_copy_code(_Hash_node<_Value, false>*,
620                    const _Hash_node<_Value, false>*) const
621       { }
622
623       void
624       _M_swap(_Hash_code_base& __x)
625       {
626         std::swap(_M_extract(), __x._M_extract());
627         std::swap(_M_ranged_hash(), __x._M_ranged_hash());
628       }
629
630     protected:
631       const _ExtractKey&
632       _M_extract() const { return _EboExtractKey::_S_cget(*this); }
633       _ExtractKey&
634       _M_extract() { return _EboExtractKey::_S_get(*this); }
635       const _Hash&
636       _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
637       _Hash&
638       _M_ranged_hash() { return _EboHash::_S_get(*this); }
639     };
640
641   // No specialization for ranged hash function while caching hash codes.
642   // That combination is meaningless, and trying to do it is an error.
643
644   // Specialization: ranged hash function, cache hash codes.  This
645   // combination is meaningless, so we provide only a declaration
646   // and no definition.
647   template<typename _Key, typename _Value, typename _ExtractKey,
648            typename _H1, typename _H2, typename _Hash>
649     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
650
651   // Specialization: hash function and range-hashing function, no
652   // caching of hash codes.
653   // Provides typedef and accessor required by TR1.
654   template<typename _Key, typename _Value, typename _ExtractKey,
655            typename _H1, typename _H2>
656     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
657                            _Default_ranged_hash, false>
658       : _Ebo_helper<0, _ExtractKey>, _Ebo_helper<1, _H1>, _Ebo_helper<2, _H2>
659     {
660     private:
661       typedef _Ebo_helper<0, _ExtractKey> _EboExtractKey;
662       typedef _Ebo_helper<1, _H1> _EboH1;
663       typedef _Ebo_helper<2, _H2> _EboH2;
664
665     public:
666       typedef _H1 hasher;
667
668       hasher
669       hash_function() const
670       { return _M_h1(); }
671
672     protected:
673       // We need the default constructor for the local iterators.
674       _Hash_code_base() = default;
675       _Hash_code_base(const _ExtractKey& __ex,
676                       const _H1& __h1, const _H2& __h2,
677                       const _Default_ranged_hash&)
678       : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
679
680       typedef std::size_t _Hash_code_type;
681
682       _Hash_code_type
683       _M_hash_code(const _Key& __k) const
684       { return _M_h1()(__k); }
685
686       std::size_t
687       _M_bucket_index(const _Key&, _Hash_code_type __c,
688                       std::size_t __n) const
689       { return _M_h2()(__c, __n); }
690
691       std::size_t
692       _M_bucket_index(const _Hash_node<_Value, false>* __p,
693                       std::size_t __n) const
694       { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
695
696       void
697       _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
698       { }
699
700       void
701       _M_copy_code(_Hash_node<_Value, false>*,
702                    const _Hash_node<_Value, false>*) const
703       { }
704
705       void
706       _M_swap(_Hash_code_base& __x)
707       {
708         std::swap(_M_extract(), __x._M_extract());
709         std::swap(_M_h1(), __x._M_h1());
710         std::swap(_M_h2(), __x._M_h2());
711       }
712
713     protected:
714       const _ExtractKey&
715       _M_extract() const { return _EboExtractKey::_S_cget(*this); }
716       _ExtractKey&
717       _M_extract() { return _EboExtractKey::_S_get(*this); }
718       const _H1&
719       _M_h1() const { return _EboH1::_S_cget(*this); }
720       _H1&
721       _M_h1() { return _EboH1::_S_get(*this); }
722       const _H2&
723       _M_h2() const { return _EboH2::_S_cget(*this); }
724       _H2&
725       _M_h2() { return _EboH2::_S_get(*this); }
726     };
727
728   // Specialization: hash function and range-hashing function,
729   // caching hash codes.  H is provided but ignored.  Provides
730   // typedef and accessor required by TR1.
731   template<typename _Key, typename _Value, typename _ExtractKey,
732            typename _H1, typename _H2>
733     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
734                            _Default_ranged_hash, true>
735       : _Ebo_helper<0, _ExtractKey>, _Ebo_helper<1, _H1>, _Ebo_helper<2, _H2>
736     {
737     private:
738       typedef _Ebo_helper<0, _ExtractKey> _EboExtractKey;
739       typedef _Ebo_helper<1, _H1> _EboH1;
740       typedef _Ebo_helper<2, _H2> _EboH2;
741
742     public:
743       typedef _H1 hasher;
744
745       hasher
746       hash_function() const
747       { return _M_h1(); }
748
749     protected:
750       _Hash_code_base(const _ExtractKey& __ex,
751                       const _H1& __h1, const _H2& __h2,
752                       const _Default_ranged_hash&)
753       : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
754
755       typedef std::size_t _Hash_code_type;
756
757       _Hash_code_type
758       _M_hash_code(const _Key& __k) const
759       { return _M_h1()(__k); }
760
761       std::size_t
762       _M_bucket_index(const _Key&, _Hash_code_type __c,
763                       std::size_t __n) const
764       { return _M_h2()(__c, __n); }
765
766       std::size_t
767       _M_bucket_index(const _Hash_node<_Value, true>* __p,
768                       std::size_t __n) const
769       { return _M_h2()(__p->_M_hash_code, __n); }
770
771       void
772       _M_store_code(_Hash_node<_Value, true>* __n, _Hash_code_type __c) const
773       { __n->_M_hash_code = __c; }
774
775       void
776       _M_copy_code(_Hash_node<_Value, true>* __to,
777                    const _Hash_node<_Value, true>* __from) const
778       { __to->_M_hash_code = __from->_M_hash_code; }
779
780       void
781       _M_swap(_Hash_code_base& __x)
782       {
783         std::swap(_M_extract(), __x._M_extract());
784         std::swap(_M_h1(), __x._M_h1());
785         std::swap(_M_h2(), __x._M_h2());
786       }
787
788     protected:
789       const _ExtractKey&
790       _M_extract() const { return _EboExtractKey::_S_cget(*this); }
791       _ExtractKey&
792       _M_extract() { return _EboExtractKey::_S_get(*this); }
793       const _H1&
794       _M_h1() const { return _EboH1::_S_cget(*this); }
795       _H1&
796       _M_h1() { return _EboH1::_S_get(*this); }
797       const _H2&
798       _M_h2() const { return _EboH2::_S_cget(*this); }
799       _H2&
800       _M_h2() { return _EboH2::_S_get(*this); }
801     };
802
803   template <typename _Key, typename _Value, typename _ExtractKey,
804             typename _Equal, typename _HashCodeType,
805             bool __cache_hash_code>
806   struct _Equal_helper;
807
808   template<typename _Key, typename _Value, typename _ExtractKey,
809            typename _Equal, typename _HashCodeType>
810   struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
811   {
812     static bool
813     _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
814               const _Key& __k, _HashCodeType __c,
815               _Hash_node<_Value, true>* __n)
816     { return __c == __n->_M_hash_code
817              && __eq(__k, __extract(__n->_M_v)); }
818   };
819
820   template<typename _Key, typename _Value, typename _ExtractKey,
821            typename _Equal, typename _HashCodeType>
822   struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
823   {
824     static bool
825     _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
826               const _Key& __k, _HashCodeType,
827               _Hash_node<_Value, false>* __n)
828     { return __eq(__k, __extract(__n->_M_v)); }
829   };
830
831   // Helper class adding management of _Equal functor to _Hash_code_base
832   // type.
833   template<typename _Key, typename _Value,
834            typename _ExtractKey, typename _Equal,
835            typename _H1, typename _H2, typename _Hash,
836            bool __cache_hash_code>
837   struct _Hashtable_base
838     : _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
839                       __cache_hash_code>,
840       _Ebo_helper<0, _Equal>
841   {
842   private:
843     typedef _Ebo_helper<0, _Equal> _EboEqual;
844
845   protected:
846     typedef _Hash_code_base<_Key, _Value, _ExtractKey,
847                             _H1, _H2, _Hash, __cache_hash_code> _HCBase;
848     typedef typename _HCBase::_Hash_code_type _Hash_code_type;
849
850     _Hashtable_base(const _ExtractKey& __ex,
851                     const _H1& __h1, const _H2& __h2,
852                     const _Hash& __hash, const _Equal& __eq)
853       : _HCBase(__ex, __h1, __h2, __hash), _EboEqual(__eq) { }
854
855     bool
856     _M_equals(const _Key& __k, _Hash_code_type __c,
857               _Hash_node<_Value, __cache_hash_code>* __n) const
858     {
859       typedef _Equal_helper<_Key, _Value, _ExtractKey,
860                            _Equal, _Hash_code_type,
861                            __cache_hash_code> _EqualHelper;
862       return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(), __k, __c, __n);
863     }
864
865     void
866     _M_swap(_Hashtable_base& __x)
867     {
868       _HCBase::_M_swap(__x);
869       std::swap(_M_eq(), __x._M_eq());
870     }
871
872   private:
873     const _Equal&
874     _M_eq() const { return _EboEqual::_S_cget(*this); }
875     _Equal&
876     _M_eq() { return _EboEqual::_S_get(*this); }
877   };
878
879   // Local iterators, used to iterate within a bucket but not between
880   // buckets.
881   template<typename _Key, typename _Value, typename _ExtractKey,
882            typename _H1, typename _H2, typename _Hash,
883            bool __cache_hash_code>
884     struct _Local_iterator_base;
885
886   template<typename _Key, typename _Value, typename _ExtractKey,
887            typename _H1, typename _H2, typename _Hash>
888     struct _Local_iterator_base<_Key, _Value, _ExtractKey,
889                                 _H1, _H2, _Hash, true>
890       : _H2
891     {
892       _Local_iterator_base() = default;
893       _Local_iterator_base(_Hash_node<_Value, true>* __p,
894                            std::size_t __bkt, std::size_t __bkt_count)
895       : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
896
897       void
898       _M_incr()
899       {
900         _M_cur = _M_cur->_M_next;
901         if (_M_cur)
902           {
903             std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
904             if (__bkt != _M_bucket)
905               _M_cur = nullptr;
906           }
907       }
908
909       const _H2& _M_h2() const
910       { return *this; }
911
912       _Hash_node<_Value, true>*  _M_cur;
913       std::size_t _M_bucket;
914       std::size_t _M_bucket_count;
915     };
916
917   template<typename _Key, typename _Value, typename _ExtractKey,
918            typename _H1, typename _H2, typename _Hash>
919     struct _Local_iterator_base<_Key, _Value, _ExtractKey,
920                                 _H1, _H2, _Hash, false>
921       : _Hash_code_base<_Key, _Value, _ExtractKey,
922                         _H1, _H2, _Hash, false>
923     {
924       _Local_iterator_base() = default;
925       _Local_iterator_base(_Hash_node<_Value, false>* __p,
926                            std::size_t __bkt, std::size_t __bkt_count)
927       : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
928
929       void
930       _M_incr()
931       {
932         _M_cur = _M_cur->_M_next;
933         if (_M_cur)
934           {
935             std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
936             if (__bkt != _M_bucket)
937               _M_cur = nullptr;
938           }
939       }
940
941       _Hash_node<_Value, false>*  _M_cur;
942       std::size_t _M_bucket;
943       std::size_t _M_bucket_count;
944     };
945
946   template<typename _Key, typename _Value, typename _ExtractKey,
947            typename _H1, typename _H2, typename _Hash, bool __cache>
948     inline bool
949     operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
950                                           _H1, _H2, _Hash, __cache>& __x,
951                const _Local_iterator_base<_Key, _Value, _ExtractKey,
952                                           _H1, _H2, _Hash, __cache>& __y)
953     { return __x._M_cur == __y._M_cur; }
954
955   template<typename _Key, typename _Value, typename _ExtractKey,
956            typename _H1, typename _H2, typename _Hash, bool __cache>
957     inline bool
958     operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
959                                           _H1, _H2, _Hash, __cache>& __x,
960                const _Local_iterator_base<_Key, _Value, _ExtractKey,
961                                           _H1, _H2, _Hash, __cache>& __y)
962     { return __x._M_cur != __y._M_cur; }
963
964   template<typename _Key, typename _Value, typename _ExtractKey,
965            typename _H1, typename _H2, typename _Hash,
966            bool __constant_iterators, bool __cache>
967     struct _Local_iterator
968     : public _Local_iterator_base<_Key, _Value, _ExtractKey,
969                                   _H1, _H2, _Hash, __cache>
970     {
971       typedef _Value                                   value_type;
972       typedef typename std::conditional<__constant_iterators,
973                                         const _Value*, _Value*>::type
974                                                        pointer;
975       typedef typename std::conditional<__constant_iterators,
976                                         const _Value&, _Value&>::type
977                                                        reference;
978       typedef std::ptrdiff_t                           difference_type;
979       typedef std::forward_iterator_tag                iterator_category;
980
981       _Local_iterator() = default;
982
983       explicit
984       _Local_iterator(_Hash_node<_Value, __cache>* __p,
985                       std::size_t __bkt, std::size_t __bkt_count)
986       : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
987                              __cache>(__p, __bkt, __bkt_count)
988       { }
989
990       reference
991       operator*() const
992       { return this->_M_cur->_M_v; }
993
994       pointer
995       operator->() const
996       { return std::__addressof(this->_M_cur->_M_v); }
997
998       _Local_iterator&
999       operator++()
1000       {
1001         this->_M_incr();
1002         return *this;
1003       }
1004
1005       _Local_iterator
1006       operator++(int)
1007       {
1008         _Local_iterator __tmp(*this);
1009         this->_M_incr();
1010         return __tmp;
1011       }
1012     };
1013
1014   template<typename _Key, typename _Value, typename _ExtractKey,
1015            typename _H1, typename _H2, typename _Hash,
1016            bool __constant_iterators, bool __cache>
1017     struct _Local_const_iterator
1018     : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1019                                   _H1, _H2, _Hash, __cache>
1020     {
1021       typedef _Value                                   value_type;
1022       typedef const _Value*                            pointer;
1023       typedef const _Value&                            reference;
1024       typedef std::ptrdiff_t                           difference_type;
1025       typedef std::forward_iterator_tag                iterator_category;
1026
1027       _Local_const_iterator() = default;
1028
1029       explicit
1030       _Local_const_iterator(_Hash_node<_Value, __cache>* __p,
1031                             std::size_t __bkt, std::size_t __bkt_count)
1032       : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1033                              __cache>(__p, __bkt, __bkt_count)
1034       { }
1035
1036       _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1037                                                   _H1, _H2, _Hash,
1038                                                   __constant_iterators,
1039                                                   __cache>& __x)
1040       : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1041                              __cache>(__x._M_cur, __x._M_bucket,
1042                                       __x._M_bucket_count)
1043       { }
1044
1045       reference
1046       operator*() const
1047       { return this->_M_cur->_M_v; }
1048
1049       pointer
1050       operator->() const
1051       { return std::__addressof(this->_M_cur->_M_v); }
1052
1053       _Local_const_iterator&
1054       operator++()
1055       {
1056         this->_M_incr();
1057         return *this;
1058       }
1059
1060       _Local_const_iterator
1061       operator++(int)
1062       {
1063         _Local_const_iterator __tmp(*this);
1064         this->_M_incr();
1065         return __tmp;
1066       }
1067     };
1068
1069
1070   // Class template _Equality_base.  This is for implementing equality
1071   // comparison for unordered containers, per N3068, by John Lakos and
1072   // Pablo Halpern.  Algorithmically, we follow closely the reference
1073   // implementations therein.
1074   template<typename _ExtractKey, bool __unique_keys,
1075            typename _Hashtable>
1076     struct _Equality_base;
1077
1078   template<typename _ExtractKey, typename _Hashtable>
1079     struct _Equality_base<_ExtractKey, true, _Hashtable>
1080     {
1081       bool _M_equal(const _Hashtable&) const;
1082     };
1083
1084   template<typename _ExtractKey, typename _Hashtable>
1085     bool
1086     _Equality_base<_ExtractKey, true, _Hashtable>::
1087     _M_equal(const _Hashtable& __other) const
1088     {
1089       const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1090
1091       if (__this->size() != __other.size())
1092         return false;
1093
1094       for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1095         {
1096           const auto __ity = __other.find(_ExtractKey()(*__itx));
1097           if (__ity == __other.end() || *__ity != *__itx)
1098             return false;
1099         }
1100       return true;
1101     }
1102
1103   template<typename _ExtractKey, typename _Hashtable>
1104     struct _Equality_base<_ExtractKey, false, _Hashtable>
1105     {
1106       bool _M_equal(const _Hashtable&) const;
1107
1108     private:
1109       template<typename _Uiterator>
1110         static bool
1111         _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
1112     };
1113
1114   // See std::is_permutation in N3068.
1115   template<typename _ExtractKey, typename _Hashtable>
1116     template<typename _Uiterator>
1117       bool
1118       _Equality_base<_ExtractKey, false, _Hashtable>::
1119       _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
1120                         _Uiterator __first2)
1121       {
1122         for (; __first1 != __last1; ++__first1, ++__first2)
1123           if (!(*__first1 == *__first2))
1124             break;
1125
1126         if (__first1 == __last1)
1127           return true;
1128
1129         _Uiterator __last2 = __first2;
1130         std::advance(__last2, std::distance(__first1, __last1));
1131
1132         for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
1133           {
1134             _Uiterator __tmp =  __first1;
1135             while (__tmp != __it1 && !(*__tmp == *__it1))
1136               ++__tmp;
1137
1138             // We've seen this one before.
1139             if (__tmp != __it1)
1140               continue;
1141
1142             std::ptrdiff_t __n2 = 0;
1143             for (__tmp = __first2; __tmp != __last2; ++__tmp)
1144               if (*__tmp == *__it1)
1145                 ++__n2;
1146
1147             if (!__n2)
1148               return false;
1149
1150             std::ptrdiff_t __n1 = 0;
1151             for (__tmp = __it1; __tmp != __last1; ++__tmp)
1152               if (*__tmp == *__it1)
1153                 ++__n1;
1154
1155             if (__n1 != __n2)
1156               return false;
1157           }
1158         return true;
1159       }
1160
1161   template<typename _ExtractKey, typename _Hashtable>
1162     bool
1163     _Equality_base<_ExtractKey, false, _Hashtable>::
1164     _M_equal(const _Hashtable& __other) const
1165     {
1166       const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1167
1168       if (__this->size() != __other.size())
1169         return false;
1170
1171       for (auto __itx = __this->begin(); __itx != __this->end();)
1172         {
1173           const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
1174           const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
1175
1176           if (std::distance(__xrange.first, __xrange.second)
1177               != std::distance(__yrange.first, __yrange.second))
1178             return false;
1179
1180           if (!_S_is_permutation(__xrange.first,
1181                                  __xrange.second,
1182                                  __yrange.first))
1183             return false;
1184
1185           __itx = __xrange.second;
1186         }
1187       return true;
1188     }
1189
1190 _GLIBCXX_END_NAMESPACE_VERSION
1191 } // namespace __detail
1192 } // namespace std
1193
1194 #endif // _HASHTABLE_POLICY_H