OSDN Git Service

2012-01-03 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, 2012 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 _Nm, typename _Tp,
519            bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
520     struct _Hashtable_ebo_helper;
521
522   // Specialization using EBO.
523   template<int _Nm, typename _Tp>
524     struct _Hashtable_ebo_helper<_Nm, _Tp, true> : private _Tp
525     {
526       _Hashtable_ebo_helper() = default;
527       _Hashtable_ebo_helper(const _Tp& __tp) : _Tp(__tp)
528       { }
529
530       static const _Tp&
531       _S_cget(const _Hashtable_ebo_helper& __eboh)
532       { return static_cast<const _Tp&>(__eboh); }
533
534       static _Tp&
535       _S_get(_Hashtable_ebo_helper& __eboh)
536       { return static_cast<_Tp&>(__eboh); }
537     };
538
539   // Specialization not using EBO.
540   template<int _Nm, typename _Tp>
541     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
542     {
543       _Hashtable_ebo_helper() = default;
544       _Hashtable_ebo_helper(const _Tp& __tp) : _M_tp(__tp)
545       { }
546
547       static const _Tp&
548       _S_cget(const _Hashtable_ebo_helper& __eboh)
549       { return __eboh._M_tp; }
550
551       static _Tp&
552       _S_get(_Hashtable_ebo_helper& __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     : private _Hashtable_ebo_helper<0, _ExtractKey>,
587       private _Hashtable_ebo_helper<1, _Hash>
588     {
589     private:
590       typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
591       typedef _Hashtable_ebo_helper<1, _Hash> _EboHash;
592
593     protected:
594       // We need the default constructor for the local iterators.
595       _Hash_code_base() = default;
596       _Hash_code_base(const _ExtractKey& __ex,
597                       const _H1&, const _H2&, const _Hash& __h)
598         : _EboExtractKey(__ex), _EboHash(__h) { }
599
600       typedef void* _Hash_code_type;
601
602       _Hash_code_type
603       _M_hash_code(const _Key& __key) const
604       { return 0; }
605
606       std::size_t
607       _M_bucket_index(const _Key& __k, _Hash_code_type,
608                       std::size_t __n) const
609       { return _M_ranged_hash()(__k, __n); }
610
611       std::size_t
612       _M_bucket_index(const _Hash_node<_Value, false>* __p,
613                       std::size_t __n) const
614       { return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
615
616       void
617       _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
618       { }
619
620       void
621       _M_copy_code(_Hash_node<_Value, false>*,
622                    const _Hash_node<_Value, false>*) const
623       { }
624
625       void
626       _M_swap(_Hash_code_base& __x)
627       {
628         std::swap(_M_extract(), __x._M_extract());
629         std::swap(_M_ranged_hash(), __x._M_ranged_hash());
630       }
631
632     protected:
633       const _ExtractKey&
634       _M_extract() const { return _EboExtractKey::_S_cget(*this); }
635       _ExtractKey&
636       _M_extract() { return _EboExtractKey::_S_get(*this); }
637       const _Hash&
638       _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
639       _Hash&
640       _M_ranged_hash() { return _EboHash::_S_get(*this); }
641     };
642
643   // No specialization for ranged hash function while caching hash codes.
644   // That combination is meaningless, and trying to do it is an error.
645
646   // Specialization: ranged hash function, cache hash codes.  This
647   // combination is meaningless, so we provide only a declaration
648   // and no definition.
649   template<typename _Key, typename _Value, typename _ExtractKey,
650            typename _H1, typename _H2, typename _Hash>
651     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
652
653   // Specialization: hash function and range-hashing function, no
654   // caching of hash codes.
655   // Provides typedef and accessor required by TR1.
656   template<typename _Key, typename _Value, typename _ExtractKey,
657            typename _H1, typename _H2>
658     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
659                            _Default_ranged_hash, false>
660     : private _Hashtable_ebo_helper<0, _ExtractKey>,
661       private _Hashtable_ebo_helper<1, _H1>,
662       private _Hashtable_ebo_helper<2, _H2>
663     {
664     private:
665       typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
666       typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
667       typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
668
669     public:
670       typedef _H1 hasher;
671
672       hasher
673       hash_function() const
674       { return _M_h1(); }
675
676     protected:
677       // We need the default constructor for the local iterators.
678       _Hash_code_base() = default;
679       _Hash_code_base(const _ExtractKey& __ex,
680                       const _H1& __h1, const _H2& __h2,
681                       const _Default_ranged_hash&)
682       : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
683
684       typedef std::size_t _Hash_code_type;
685
686       _Hash_code_type
687       _M_hash_code(const _Key& __k) const
688       { return _M_h1()(__k); }
689
690       std::size_t
691       _M_bucket_index(const _Key&, _Hash_code_type __c,
692                       std::size_t __n) const
693       { return _M_h2()(__c, __n); }
694
695       std::size_t
696       _M_bucket_index(const _Hash_node<_Value, false>* __p,
697                       std::size_t __n) const
698       { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
699
700       void
701       _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
702       { }
703
704       void
705       _M_copy_code(_Hash_node<_Value, false>*,
706                    const _Hash_node<_Value, false>*) const
707       { }
708
709       void
710       _M_swap(_Hash_code_base& __x)
711       {
712         std::swap(_M_extract(), __x._M_extract());
713         std::swap(_M_h1(), __x._M_h1());
714         std::swap(_M_h2(), __x._M_h2());
715       }
716
717     protected:
718       const _ExtractKey&
719       _M_extract() const { return _EboExtractKey::_S_cget(*this); }
720       _ExtractKey&
721       _M_extract() { return _EboExtractKey::_S_get(*this); }
722       const _H1&
723       _M_h1() const { return _EboH1::_S_cget(*this); }
724       _H1&
725       _M_h1() { return _EboH1::_S_get(*this); }
726       const _H2&
727       _M_h2() const { return _EboH2::_S_cget(*this); }
728       _H2&
729       _M_h2() { return _EboH2::_S_get(*this); }
730     };
731
732   // Specialization: hash function and range-hashing function,
733   // caching hash codes.  H is provided but ignored.  Provides
734   // typedef and accessor required by TR1.
735   template<typename _Key, typename _Value, typename _ExtractKey,
736            typename _H1, typename _H2>
737     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
738                            _Default_ranged_hash, true>
739     : private _Hashtable_ebo_helper<0, _ExtractKey>,
740       private _Hashtable_ebo_helper<1, _H1>,
741       private _Hashtable_ebo_helper<2, _H2>
742     {
743     private:
744       typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
745       typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
746       typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
747
748     public:
749       typedef _H1 hasher;
750
751       hasher
752       hash_function() const
753       { return _M_h1(); }
754
755     protected:
756       _Hash_code_base(const _ExtractKey& __ex,
757                       const _H1& __h1, const _H2& __h2,
758                       const _Default_ranged_hash&)
759       : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
760
761       typedef std::size_t _Hash_code_type;
762
763       _Hash_code_type
764       _M_hash_code(const _Key& __k) const
765       { return _M_h1()(__k); }
766
767       std::size_t
768       _M_bucket_index(const _Key&, _Hash_code_type __c,
769                       std::size_t __n) const
770       { return _M_h2()(__c, __n); }
771
772       std::size_t
773       _M_bucket_index(const _Hash_node<_Value, true>* __p,
774                       std::size_t __n) const
775       { return _M_h2()(__p->_M_hash_code, __n); }
776
777       void
778       _M_store_code(_Hash_node<_Value, true>* __n, _Hash_code_type __c) const
779       { __n->_M_hash_code = __c; }
780
781       void
782       _M_copy_code(_Hash_node<_Value, true>* __to,
783                    const _Hash_node<_Value, true>* __from) const
784       { __to->_M_hash_code = __from->_M_hash_code; }
785
786       void
787       _M_swap(_Hash_code_base& __x)
788       {
789         std::swap(_M_extract(), __x._M_extract());
790         std::swap(_M_h1(), __x._M_h1());
791         std::swap(_M_h2(), __x._M_h2());
792       }
793
794     protected:
795       const _ExtractKey&
796       _M_extract() const { return _EboExtractKey::_S_cget(*this); }
797       _ExtractKey&
798       _M_extract() { return _EboExtractKey::_S_get(*this); }
799       const _H1&
800       _M_h1() const { return _EboH1::_S_cget(*this); }
801       _H1&
802       _M_h1() { return _EboH1::_S_get(*this); }
803       const _H2&
804       _M_h2() const { return _EboH2::_S_cget(*this); }
805       _H2&
806       _M_h2() { return _EboH2::_S_get(*this); }
807     };
808
809   template <typename _Key, typename _Value, typename _ExtractKey,
810             typename _Equal, typename _HashCodeType,
811             bool __cache_hash_code>
812   struct _Equal_helper;
813
814   template<typename _Key, typename _Value, typename _ExtractKey,
815            typename _Equal, typename _HashCodeType>
816   struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
817   {
818     static bool
819     _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
820               const _Key& __k, _HashCodeType __c,
821               _Hash_node<_Value, true>* __n)
822     { return __c == __n->_M_hash_code
823              && __eq(__k, __extract(__n->_M_v)); }
824   };
825
826   template<typename _Key, typename _Value, typename _ExtractKey,
827            typename _Equal, typename _HashCodeType>
828   struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
829   {
830     static bool
831     _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
832               const _Key& __k, _HashCodeType,
833               _Hash_node<_Value, false>* __n)
834     { return __eq(__k, __extract(__n->_M_v)); }
835   };
836
837   // Helper class adding management of _Equal functor to _Hash_code_base
838   // type.
839   template<typename _Key, typename _Value,
840            typename _ExtractKey, typename _Equal,
841            typename _H1, typename _H2, typename _Hash,
842            bool __cache_hash_code>
843   struct _Hashtable_base
844   : public  _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
845                             __cache_hash_code>,
846     private _Hashtable_ebo_helper<0, _Equal>
847   {
848   private:
849     typedef _Hashtable_ebo_helper<0, _Equal> _EboEqual;
850
851   protected:
852     typedef _Hash_code_base<_Key, _Value, _ExtractKey,
853                             _H1, _H2, _Hash, __cache_hash_code> _HCBase;
854     typedef typename _HCBase::_Hash_code_type _Hash_code_type;
855
856     _Hashtable_base(const _ExtractKey& __ex,
857                     const _H1& __h1, const _H2& __h2,
858                     const _Hash& __hash, const _Equal& __eq)
859       : _HCBase(__ex, __h1, __h2, __hash), _EboEqual(__eq) { }
860
861     bool
862     _M_equals(const _Key& __k, _Hash_code_type __c,
863               _Hash_node<_Value, __cache_hash_code>* __n) const
864     {
865       typedef _Equal_helper<_Key, _Value, _ExtractKey,
866                            _Equal, _Hash_code_type,
867                            __cache_hash_code> _EqualHelper;
868       return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
869                                      __k, __c, __n);
870     }
871
872     void
873     _M_swap(_Hashtable_base& __x)
874     {
875       _HCBase::_M_swap(__x);
876       std::swap(_M_eq(), __x._M_eq());
877     }
878
879   private:
880     const _Equal&
881     _M_eq() const { return _EboEqual::_S_cget(*this); }
882     _Equal&
883     _M_eq() { return _EboEqual::_S_get(*this); }
884   };
885
886   // Local iterators, used to iterate within a bucket but not between
887   // buckets.
888   template<typename _Key, typename _Value, typename _ExtractKey,
889            typename _H1, typename _H2, typename _Hash,
890            bool __cache_hash_code>
891     struct _Local_iterator_base;
892
893   template<typename _Key, typename _Value, typename _ExtractKey,
894            typename _H1, typename _H2, typename _Hash>
895     struct _Local_iterator_base<_Key, _Value, _ExtractKey,
896                                 _H1, _H2, _Hash, true>
897       : private _H2
898     {
899       _Local_iterator_base() = default;
900       _Local_iterator_base(_Hash_node<_Value, true>* __p,
901                            std::size_t __bkt, std::size_t __bkt_count)
902       : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
903
904       void
905       _M_incr()
906       {
907         _M_cur = _M_cur->_M_next;
908         if (_M_cur)
909           {
910             std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
911             if (__bkt != _M_bucket)
912               _M_cur = nullptr;
913           }
914       }
915
916       const _H2& _M_h2() const
917       { return *this; }
918
919       _Hash_node<_Value, true>*  _M_cur;
920       std::size_t _M_bucket;
921       std::size_t _M_bucket_count;
922     };
923
924   template<typename _Key, typename _Value, typename _ExtractKey,
925            typename _H1, typename _H2, typename _Hash>
926     struct _Local_iterator_base<_Key, _Value, _ExtractKey,
927                                 _H1, _H2, _Hash, false>
928       : private _Hash_code_base<_Key, _Value, _ExtractKey,
929                                 _H1, _H2, _Hash, false>
930     {
931       _Local_iterator_base() = default;
932       _Local_iterator_base(_Hash_node<_Value, false>* __p,
933                            std::size_t __bkt, std::size_t __bkt_count)
934       : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
935
936       void
937       _M_incr()
938       {
939         _M_cur = _M_cur->_M_next;
940         if (_M_cur)
941           {
942             std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
943             if (__bkt != _M_bucket)
944               _M_cur = nullptr;
945           }
946       }
947
948       _Hash_node<_Value, false>*  _M_cur;
949       std::size_t _M_bucket;
950       std::size_t _M_bucket_count;
951     };
952
953   template<typename _Key, typename _Value, typename _ExtractKey,
954            typename _H1, typename _H2, typename _Hash, bool __cache>
955     inline bool
956     operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
957                                           _H1, _H2, _Hash, __cache>& __x,
958                const _Local_iterator_base<_Key, _Value, _ExtractKey,
959                                           _H1, _H2, _Hash, __cache>& __y)
960     { return __x._M_cur == __y._M_cur; }
961
962   template<typename _Key, typename _Value, typename _ExtractKey,
963            typename _H1, typename _H2, typename _Hash, bool __cache>
964     inline bool
965     operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
966                                           _H1, _H2, _Hash, __cache>& __x,
967                const _Local_iterator_base<_Key, _Value, _ExtractKey,
968                                           _H1, _H2, _Hash, __cache>& __y)
969     { return __x._M_cur != __y._M_cur; }
970
971   template<typename _Key, typename _Value, typename _ExtractKey,
972            typename _H1, typename _H2, typename _Hash,
973            bool __constant_iterators, bool __cache>
974     struct _Local_iterator
975     : public _Local_iterator_base<_Key, _Value, _ExtractKey,
976                                   _H1, _H2, _Hash, __cache>
977     {
978       typedef _Value                                   value_type;
979       typedef typename std::conditional<__constant_iterators,
980                                         const _Value*, _Value*>::type
981                                                        pointer;
982       typedef typename std::conditional<__constant_iterators,
983                                         const _Value&, _Value&>::type
984                                                        reference;
985       typedef std::ptrdiff_t                           difference_type;
986       typedef std::forward_iterator_tag                iterator_category;
987
988       _Local_iterator() = default;
989
990       explicit
991       _Local_iterator(_Hash_node<_Value, __cache>* __p,
992                       std::size_t __bkt, std::size_t __bkt_count)
993       : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
994                              __cache>(__p, __bkt, __bkt_count)
995       { }
996
997       reference
998       operator*() const
999       { return this->_M_cur->_M_v; }
1000
1001       pointer
1002       operator->() const
1003       { return std::__addressof(this->_M_cur->_M_v); }
1004
1005       _Local_iterator&
1006       operator++()
1007       {
1008         this->_M_incr();
1009         return *this;
1010       }
1011
1012       _Local_iterator
1013       operator++(int)
1014       {
1015         _Local_iterator __tmp(*this);
1016         this->_M_incr();
1017         return __tmp;
1018       }
1019     };
1020
1021   template<typename _Key, typename _Value, typename _ExtractKey,
1022            typename _H1, typename _H2, typename _Hash,
1023            bool __constant_iterators, bool __cache>
1024     struct _Local_const_iterator
1025     : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1026                                   _H1, _H2, _Hash, __cache>
1027     {
1028       typedef _Value                                   value_type;
1029       typedef const _Value*                            pointer;
1030       typedef const _Value&                            reference;
1031       typedef std::ptrdiff_t                           difference_type;
1032       typedef std::forward_iterator_tag                iterator_category;
1033
1034       _Local_const_iterator() = default;
1035
1036       explicit
1037       _Local_const_iterator(_Hash_node<_Value, __cache>* __p,
1038                             std::size_t __bkt, std::size_t __bkt_count)
1039       : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1040                              __cache>(__p, __bkt, __bkt_count)
1041       { }
1042
1043       _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1044                                                   _H1, _H2, _Hash,
1045                                                   __constant_iterators,
1046                                                   __cache>& __x)
1047       : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1048                              __cache>(__x._M_cur, __x._M_bucket,
1049                                       __x._M_bucket_count)
1050       { }
1051
1052       reference
1053       operator*() const
1054       { return this->_M_cur->_M_v; }
1055
1056       pointer
1057       operator->() const
1058       { return std::__addressof(this->_M_cur->_M_v); }
1059
1060       _Local_const_iterator&
1061       operator++()
1062       {
1063         this->_M_incr();
1064         return *this;
1065       }
1066
1067       _Local_const_iterator
1068       operator++(int)
1069       {
1070         _Local_const_iterator __tmp(*this);
1071         this->_M_incr();
1072         return __tmp;
1073       }
1074     };
1075
1076
1077   // Class template _Equality_base.  This is for implementing equality
1078   // comparison for unordered containers, per N3068, by John Lakos and
1079   // Pablo Halpern.  Algorithmically, we follow closely the reference
1080   // implementations therein.
1081   template<typename _ExtractKey, bool __unique_keys,
1082            typename _Hashtable>
1083     struct _Equality_base;
1084
1085   template<typename _ExtractKey, typename _Hashtable>
1086     struct _Equality_base<_ExtractKey, true, _Hashtable>
1087     {
1088       bool _M_equal(const _Hashtable&) const;
1089     };
1090
1091   template<typename _ExtractKey, typename _Hashtable>
1092     bool
1093     _Equality_base<_ExtractKey, true, _Hashtable>::
1094     _M_equal(const _Hashtable& __other) const
1095     {
1096       const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1097
1098       if (__this->size() != __other.size())
1099         return false;
1100
1101       for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1102         {
1103           const auto __ity = __other.find(_ExtractKey()(*__itx));
1104           if (__ity == __other.end() || *__ity != *__itx)
1105             return false;
1106         }
1107       return true;
1108     }
1109
1110   template<typename _ExtractKey, typename _Hashtable>
1111     struct _Equality_base<_ExtractKey, false, _Hashtable>
1112     {
1113       bool _M_equal(const _Hashtable&) const;
1114
1115     private:
1116       template<typename _Uiterator>
1117         static bool
1118         _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
1119     };
1120
1121   // See std::is_permutation in N3068.
1122   template<typename _ExtractKey, typename _Hashtable>
1123     template<typename _Uiterator>
1124       bool
1125       _Equality_base<_ExtractKey, false, _Hashtable>::
1126       _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
1127                         _Uiterator __first2)
1128       {
1129         for (; __first1 != __last1; ++__first1, ++__first2)
1130           if (!(*__first1 == *__first2))
1131             break;
1132
1133         if (__first1 == __last1)
1134           return true;
1135
1136         _Uiterator __last2 = __first2;
1137         std::advance(__last2, std::distance(__first1, __last1));
1138
1139         for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
1140           {
1141             _Uiterator __tmp =  __first1;
1142             while (__tmp != __it1 && !(*__tmp == *__it1))
1143               ++__tmp;
1144
1145             // We've seen this one before.
1146             if (__tmp != __it1)
1147               continue;
1148
1149             std::ptrdiff_t __n2 = 0;
1150             for (__tmp = __first2; __tmp != __last2; ++__tmp)
1151               if (*__tmp == *__it1)
1152                 ++__n2;
1153
1154             if (!__n2)
1155               return false;
1156
1157             std::ptrdiff_t __n1 = 0;
1158             for (__tmp = __it1; __tmp != __last1; ++__tmp)
1159               if (*__tmp == *__it1)
1160                 ++__n1;
1161
1162             if (__n1 != __n2)
1163               return false;
1164           }
1165         return true;
1166       }
1167
1168   template<typename _ExtractKey, typename _Hashtable>
1169     bool
1170     _Equality_base<_ExtractKey, false, _Hashtable>::
1171     _M_equal(const _Hashtable& __other) const
1172     {
1173       const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1174
1175       if (__this->size() != __other.size())
1176         return false;
1177
1178       for (auto __itx = __this->begin(); __itx != __this->end();)
1179         {
1180           const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
1181           const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
1182
1183           if (std::distance(__xrange.first, __xrange.second)
1184               != std::distance(__yrange.first, __yrange.second))
1185             return false;
1186
1187           if (!_S_is_permutation(__xrange.first,
1188                                  __xrange.second,
1189                                  __yrange.first))
1190             return false;
1191
1192           __itx = __xrange.second;
1193         }
1194       return true;
1195     }
1196
1197 _GLIBCXX_END_NAMESPACE_VERSION
1198 } // namespace __detail
1199 } // namespace std
1200
1201 #endif // _HASHTABLE_POLICY_H