OSDN Git Service

2010-03-02 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / hashtable.h
1 // hashtable.h header -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009, 2010 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.h
26  *  This is an internal header file, included by other library headers.
27  *  You should not attempt to use it directly.
28  */
29
30 #ifndef _HASHTABLE_H
31 #define _HASHTABLE_H 1
32
33 #pragma GCC system_header
34
35 #include <bits/hashtable_policy.h>
36
37 namespace std
38 {
39   // Class template _Hashtable, class definition.
40   
41   // Meaning of class template _Hashtable's template parameters
42   
43   // _Key and _Value: arbitrary CopyConstructible types.
44   
45   // _Allocator: an allocator type ([lib.allocator.requirements]) whose
46   // value type is Value.  As a conforming extension, we allow for
47   // value type != Value.
48
49   // _ExtractKey: function object that takes a object of type Value
50   // and returns a value of type _Key.
51   
52   // _Equal: function object that takes two objects of type k and returns
53   // a bool-like value that is true if the two objects are considered equal.
54   
55   // _H1: the hash function.  A unary function object with argument type
56   // Key and result type size_t.  Return values should be distributed
57   // over the entire range [0, numeric_limits<size_t>:::max()].
58   
59   // _H2: the range-hashing function (in the terminology of Tavori and
60   // Dreizin).  A binary function object whose argument types and result
61   // type are all size_t.  Given arguments r and N, the return value is
62   // in the range [0, N).
63   
64   // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
65   // whose argument types are _Key and size_t and whose result type is
66   // size_t.  Given arguments k and N, the return value is in the range
67   // [0, N).  Default: hash(k, N) = h2(h1(k), N).  If _Hash is anything other
68   // than the default, _H1 and _H2 are ignored.
69   
70   // _RehashPolicy: Policy class with three members, all of which govern
71   // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
72   // than n.  _M_bkt_for_elements(n) returns a bucket count appropriate
73   // for an element count of n.  _M_need_rehash(n_bkt, n_elt, n_ins)
74   // determines whether, if the current bucket count is n_bkt and the
75   // current element count is n_elt, we need to increase the bucket
76   // count.  If so, returns make_pair(true, n), where n is the new
77   // bucket count.  If not, returns make_pair(false, <anything>).
78   
79   // ??? Right now it is hard-wired that the number of buckets never
80   // shrinks.  Should we allow _RehashPolicy to change that?
81   
82   // __cache_hash_code: bool.  true if we store the value of the hash
83   // function along with the value.  This is a time-space tradeoff.
84   // Storing it may improve lookup speed by reducing the number of times
85   // we need to call the Equal function.
86   
87   // __constant_iterators: bool.  true if iterator and const_iterator are
88   // both constant iterator types.  This is true for unordered_set and
89   // unordered_multiset, false for unordered_map and unordered_multimap.
90   
91   // __unique_keys: bool.  true if the return value of _Hashtable::count(k)
92   // is always at most one, false if it may be an arbitrary number.  This
93   // true for unordered_set and unordered_map, false for unordered_multiset
94   // and unordered_multimap.
95   
96   template<typename _Key, typename _Value, typename _Allocator,
97            typename _ExtractKey, typename _Equal,
98            typename _H1, typename _H2, typename _Hash, 
99            typename _RehashPolicy,
100            bool __cache_hash_code,
101            bool __constant_iterators,
102            bool __unique_keys>
103     class _Hashtable
104     : public __detail::_Rehash_base<_RehashPolicy,
105                                     _Hashtable<_Key, _Value, _Allocator,
106                                                _ExtractKey,
107                                                _Equal, _H1, _H2, _Hash,
108                                                _RehashPolicy,
109                                                __cache_hash_code,
110                                                __constant_iterators,
111                                                __unique_keys> >,
112       public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
113                                        _H1, _H2, _Hash, __cache_hash_code>,
114       public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
115                                  _Hashtable<_Key, _Value, _Allocator,
116                                             _ExtractKey,
117                                             _Equal, _H1, _H2, _Hash,
118                                             _RehashPolicy,
119                                             __cache_hash_code,
120                                             __constant_iterators,
121                                             __unique_keys> >
122     {
123     public:
124       typedef _Allocator                                  allocator_type;
125       typedef _Value                                      value_type;
126       typedef _Key                                        key_type;
127       typedef _Equal                                      key_equal;
128       // mapped_type, if present, comes from _Map_base.
129       // hasher, if present, comes from _Hash_code_base.
130       typedef typename _Allocator::difference_type        difference_type;
131       typedef typename _Allocator::size_type              size_type;
132       typedef typename _Allocator::pointer                pointer;
133       typedef typename _Allocator::const_pointer          const_pointer;
134       typedef typename _Allocator::reference              reference;
135       typedef typename _Allocator::const_reference        const_reference;
136       
137       typedef __detail::_Node_iterator<value_type, __constant_iterators,
138                                        __cache_hash_code>
139                                                           local_iterator;
140       typedef __detail::_Node_const_iterator<value_type,
141                                              __constant_iterators,
142                                              __cache_hash_code>
143                                                           const_local_iterator;
144
145       typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
146                                             __cache_hash_code>
147                                                           iterator;
148       typedef __detail::_Hashtable_const_iterator<value_type,
149                                                   __constant_iterators,
150                                                   __cache_hash_code>
151                                                           const_iterator;
152
153       template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2,
154                typename _Hashtable2>
155         friend struct __detail::_Map_base;
156
157     private:
158       typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
159       typedef typename _Allocator::template rebind<_Node>::other
160                                                         _Node_allocator_type;
161       typedef typename _Allocator::template rebind<_Node*>::other
162                                                         _Bucket_allocator_type;
163
164       typedef typename _Allocator::template rebind<_Value>::other
165                                                         _Value_allocator_type;
166
167       _Node_allocator_type   _M_node_allocator;
168       _Node**                _M_buckets;
169       size_type              _M_bucket_count;
170       size_type              _M_element_count;
171       _RehashPolicy          _M_rehash_policy;
172       
173       _Node*
174       _M_allocate_node(const value_type& __v);
175   
176       void
177       _M_deallocate_node(_Node* __n);
178   
179       void
180       _M_deallocate_nodes(_Node**, size_type);
181
182       _Node**
183       _M_allocate_buckets(size_type __n);
184   
185       void
186       _M_deallocate_buckets(_Node**, size_type __n);
187
188     public:                         
189       // Constructor, destructor, assignment, swap
190       _Hashtable(size_type __bucket_hint,
191                  const _H1&, const _H2&, const _Hash&,
192                  const _Equal&, const _ExtractKey&,
193                  const allocator_type&);
194   
195       template<typename _InputIterator>
196         _Hashtable(_InputIterator __first, _InputIterator __last,
197                    size_type __bucket_hint,
198                    const _H1&, const _H2&, const _Hash&, 
199                    const _Equal&, const _ExtractKey&,
200                    const allocator_type&);
201   
202       _Hashtable(const _Hashtable&);
203
204       _Hashtable(_Hashtable&&);
205       
206       _Hashtable&
207       operator=(const _Hashtable&);
208
209       ~_Hashtable();
210
211       void swap(_Hashtable&);
212
213       // Basic container operations
214       iterator
215       begin()
216       {
217         iterator __i(_M_buckets);
218         if (!__i._M_cur_node)
219           __i._M_incr_bucket();
220         return __i;
221       }
222
223       const_iterator
224       begin() const
225       {
226         const_iterator __i(_M_buckets);
227         if (!__i._M_cur_node)
228           __i._M_incr_bucket();
229         return __i;
230       }
231
232       iterator
233       end()
234       { return iterator(_M_buckets + _M_bucket_count); }
235
236       const_iterator
237       end() const
238       { return const_iterator(_M_buckets + _M_bucket_count); }
239
240       const_iterator
241       cbegin() const
242       {
243         const_iterator __i(_M_buckets);
244         if (!__i._M_cur_node)
245           __i._M_incr_bucket();
246         return __i;
247       }
248
249       const_iterator
250       cend() const
251       { return const_iterator(_M_buckets + _M_bucket_count); }
252
253       size_type
254       size() const
255       { return _M_element_count; }
256   
257       bool
258       empty() const
259       { return size() == 0; }
260
261       allocator_type
262       get_allocator() const
263       { return allocator_type(_M_node_allocator); }
264
265       _Value_allocator_type
266       _M_get_Value_allocator() const
267       { return _Value_allocator_type(_M_node_allocator); }
268
269       size_type
270       max_size() const
271       { return _M_node_allocator.max_size(); }
272
273       // Observers
274       key_equal
275       key_eq() const
276       { return this->_M_eq; }
277
278       // hash_function, if present, comes from _Hash_code_base.
279
280       // Bucket operations
281       size_type
282       bucket_count() const
283       { return _M_bucket_count; }
284   
285       size_type
286       max_bucket_count() const
287       { return max_size(); }
288   
289       size_type
290       bucket_size(size_type __n) const
291       { return std::distance(begin(__n), end(__n)); }
292   
293       size_type
294       bucket(const key_type& __k) const
295       { 
296         return this->_M_bucket_index(__k, this->_M_hash_code(__k),
297                                      bucket_count());
298       }
299
300       local_iterator
301       begin(size_type __n)
302       { return local_iterator(_M_buckets[__n]); }
303
304       local_iterator
305       end(size_type)
306       { return local_iterator(0); }
307
308       const_local_iterator
309       begin(size_type __n) const
310       { return const_local_iterator(_M_buckets[__n]); }
311
312       const_local_iterator
313       end(size_type) const
314       { return const_local_iterator(0); }
315
316       // DR 691.
317       const_local_iterator
318       cbegin(size_type __n) const
319       { return const_local_iterator(_M_buckets[__n]); }
320
321       const_local_iterator
322       cend(size_type) const
323       { return const_local_iterator(0); }
324
325       float
326       load_factor() const
327       { 
328         return static_cast<float>(size()) / static_cast<float>(bucket_count());
329       }
330
331       // max_load_factor, if present, comes from _Rehash_base.
332
333       // Generalization of max_load_factor.  Extension, not found in TR1.  Only
334       // useful if _RehashPolicy is something other than the default.
335       const _RehashPolicy&
336       __rehash_policy() const
337       { return _M_rehash_policy; }
338       
339       void 
340       __rehash_policy(const _RehashPolicy&);
341
342       // Lookup.
343       iterator
344       find(const key_type& __k);
345
346       const_iterator
347       find(const key_type& __k) const;
348
349       size_type
350       count(const key_type& __k) const;
351
352       std::pair<iterator, iterator>
353       equal_range(const key_type& __k);
354
355       std::pair<const_iterator, const_iterator>
356       equal_range(const key_type& __k) const;
357
358     private:                    // Find, insert and erase helper functions
359       // ??? This dispatching is a workaround for the fact that we don't
360       // have partial specialization of member templates; it would be
361       // better to just specialize insert on __unique_keys.  There may be a
362       // cleaner workaround.
363       typedef typename std::conditional<__unique_keys,
364                                         std::pair<iterator, bool>,
365                                         iterator>::type
366         _Insert_Return_Type;
367
368       typedef typename std::conditional<__unique_keys,
369                                         std::_Select1st<_Insert_Return_Type>,
370                                         std::_Identity<_Insert_Return_Type>
371                                    >::type
372         _Insert_Conv_Type;
373
374       _Node*
375       _M_find_node(_Node*, const key_type&,
376                    typename _Hashtable::_Hash_code_type) const;
377
378       iterator
379       _M_insert_bucket(const value_type&, size_type,
380                        typename _Hashtable::_Hash_code_type);
381
382       std::pair<iterator, bool>
383       _M_insert(const value_type&, std::true_type);
384
385       iterator
386       _M_insert(const value_type&, std::false_type);
387
388     public:                             
389       // Insert and erase
390       _Insert_Return_Type
391       insert(const value_type& __v) 
392       { return _M_insert(__v, std::integral_constant<bool,
393                          __unique_keys>()); }
394
395       iterator
396       insert(const_iterator, const value_type& __v)
397       { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
398
399       template<typename _InputIterator>
400         void
401         insert(_InputIterator __first, _InputIterator __last);
402
403       void
404       insert(initializer_list<value_type> __l)
405       { this->insert(__l.begin(), __l.end()); }
406
407       void
408       erase(const_iterator);
409
410       size_type
411       erase(const key_type&);
412
413       void
414       erase(const_iterator, const_iterator);
415
416       void
417       clear();
418
419       // Set number of buckets to be appropriate for container of n element.
420       void rehash(size_type __n);
421       
422     private:
423       // Unconditionally change size of bucket array to n.
424       void _M_rehash(size_type __n);
425     };
426
427
428   // Definitions of class template _Hashtable's out-of-line member functions.
429   template<typename _Key, typename _Value, 
430            typename _Allocator, typename _ExtractKey, typename _Equal,
431            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
432            bool __chc, bool __cit, bool __uk>
433     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
434                         _H1, _H2, _Hash, _RehashPolicy,
435                         __chc, __cit, __uk>::_Node*
436     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
437                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
438     _M_allocate_node(const value_type& __v)
439     {
440       _Node* __n = _M_node_allocator.allocate(1);
441       __try
442         {
443           _M_node_allocator.construct(__n, __v);
444           __n->_M_next = 0;
445           return __n;
446         }
447       __catch(...)
448         {
449           _M_node_allocator.deallocate(__n, 1);
450           __throw_exception_again;
451         }
452     }
453
454   template<typename _Key, typename _Value, 
455            typename _Allocator, typename _ExtractKey, typename _Equal,
456            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
457            bool __chc, bool __cit, bool __uk>
458     void
459     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
460                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
461     _M_deallocate_node(_Node* __n)
462     {
463       _M_node_allocator.destroy(__n);
464       _M_node_allocator.deallocate(__n, 1);
465     }
466
467   template<typename _Key, typename _Value, 
468            typename _Allocator, typename _ExtractKey, typename _Equal,
469            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
470            bool __chc, bool __cit, bool __uk>
471     void
472     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
473                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
474     _M_deallocate_nodes(_Node** __array, size_type __n)
475     {
476       for (size_type __i = 0; __i < __n; ++__i)
477         {
478           _Node* __p = __array[__i];
479           while (__p)
480             {
481               _Node* __tmp = __p;
482               __p = __p->_M_next;
483               _M_deallocate_node(__tmp);
484             }
485           __array[__i] = 0;
486         }
487     }
488
489   template<typename _Key, typename _Value, 
490            typename _Allocator, typename _ExtractKey, typename _Equal,
491            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
492            bool __chc, bool __cit, bool __uk>
493     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
494                         _H1, _H2, _Hash, _RehashPolicy,
495                         __chc, __cit, __uk>::_Node**
496     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
497                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
498     _M_allocate_buckets(size_type __n)
499     {
500       _Bucket_allocator_type __alloc(_M_node_allocator);
501
502       // We allocate one extra bucket to hold a sentinel, an arbitrary
503       // non-null pointer.  Iterator increment relies on this.
504       _Node** __p = __alloc.allocate(__n + 1);
505       std::fill(__p, __p + __n, (_Node*) 0);
506       __p[__n] = reinterpret_cast<_Node*>(0x1000);
507       return __p;
508     }
509
510   template<typename _Key, typename _Value, 
511            typename _Allocator, typename _ExtractKey, typename _Equal,
512            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
513            bool __chc, bool __cit, bool __uk>
514     void
515     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
516                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
517     _M_deallocate_buckets(_Node** __p, size_type __n)
518     {
519       _Bucket_allocator_type __alloc(_M_node_allocator);
520       __alloc.deallocate(__p, __n + 1);
521     }
522
523   template<typename _Key, typename _Value, 
524            typename _Allocator, typename _ExtractKey, typename _Equal,
525            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
526            bool __chc, bool __cit, bool __uk>
527     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
528                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
529     _Hashtable(size_type __bucket_hint,
530                const _H1& __h1, const _H2& __h2, const _Hash& __h,
531                const _Equal& __eq, const _ExtractKey& __exk,
532                const allocator_type& __a)
533     : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
534       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
535                                 _H1, _H2, _Hash, __chc>(__exk, __eq,
536                                                         __h1, __h2, __h),
537       __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
538       _M_node_allocator(__a),
539       _M_bucket_count(0),
540       _M_element_count(0),
541       _M_rehash_policy()
542     {
543       _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
544       _M_buckets = _M_allocate_buckets(_M_bucket_count);
545     }
546
547   template<typename _Key, typename _Value, 
548            typename _Allocator, typename _ExtractKey, typename _Equal,
549            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
550            bool __chc, bool __cit, bool __uk>
551     template<typename _InputIterator>
552       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
553                  _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
554       _Hashtable(_InputIterator __f, _InputIterator __l,
555                  size_type __bucket_hint,
556                  const _H1& __h1, const _H2& __h2, const _Hash& __h,
557                  const _Equal& __eq, const _ExtractKey& __exk,
558                  const allocator_type& __a)
559       : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
560         __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
561                                   _H1, _H2, _Hash, __chc>(__exk, __eq,
562                                                           __h1, __h2, __h),
563         __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
564         _M_node_allocator(__a),
565         _M_bucket_count(0),
566         _M_element_count(0),
567         _M_rehash_policy()
568       {
569         _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
570                                    _M_rehash_policy.
571                                    _M_bkt_for_elements(__detail::
572                                                        __distance_fw(__f,
573                                                                      __l)));
574         _M_buckets = _M_allocate_buckets(_M_bucket_count);
575         __try
576           {
577             for (; __f != __l; ++__f)
578               this->insert(*__f);
579           }
580         __catch(...)
581           {
582             clear();
583             _M_deallocate_buckets(_M_buckets, _M_bucket_count);
584             __throw_exception_again;
585           }
586       }
587   
588   template<typename _Key, typename _Value, 
589            typename _Allocator, typename _ExtractKey, typename _Equal,
590            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
591            bool __chc, bool __cit, bool __uk>
592     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
593                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
594     _Hashtable(const _Hashtable& __ht)
595     : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
596       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
597                                 _H1, _H2, _Hash, __chc>(__ht),
598       __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
599       _M_node_allocator(__ht._M_node_allocator),
600       _M_bucket_count(__ht._M_bucket_count),
601       _M_element_count(__ht._M_element_count),
602       _M_rehash_policy(__ht._M_rehash_policy)
603     {
604       _M_buckets = _M_allocate_buckets(_M_bucket_count);
605       __try
606         {
607           for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
608             {
609               _Node* __n = __ht._M_buckets[__i];
610               _Node** __tail = _M_buckets + __i;
611               while (__n)
612                 {
613                   *__tail = _M_allocate_node(__n->_M_v);
614                   this->_M_copy_code(*__tail, __n);
615                   __tail = &((*__tail)->_M_next);
616                   __n = __n->_M_next;
617                 }
618             }
619         }
620       __catch(...)
621         {
622           clear();
623           _M_deallocate_buckets(_M_buckets, _M_bucket_count);
624           __throw_exception_again;
625         }
626     }
627
628   template<typename _Key, typename _Value, 
629            typename _Allocator, typename _ExtractKey, typename _Equal,
630            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
631            bool __chc, bool __cit, bool __uk>
632     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
633                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
634     _Hashtable(_Hashtable&& __ht)
635     : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
636       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
637                                 _H1, _H2, _Hash, __chc>(__ht),
638       __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
639       _M_node_allocator(__ht._M_node_allocator),
640       _M_bucket_count(__ht._M_bucket_count),
641       _M_element_count(__ht._M_element_count),
642       _M_rehash_policy(__ht._M_rehash_policy),
643       _M_buckets(__ht._M_buckets)
644     {
645       size_type __n_bkt = __ht._M_rehash_policy._M_next_bkt(0);
646       __ht._M_buckets = __ht._M_allocate_buckets(__n_bkt);
647       __ht._M_bucket_count = __n_bkt;
648       __ht._M_element_count = 0;
649       __ht._M_rehash_policy = _RehashPolicy();
650     }
651
652   template<typename _Key, typename _Value, 
653            typename _Allocator, typename _ExtractKey, typename _Equal,
654            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
655            bool __chc, bool __cit, bool __uk>
656     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
657                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
658     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
659                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
660     operator=(const _Hashtable& __ht)
661     {
662       _Hashtable __tmp(__ht);
663       this->swap(__tmp);
664       return *this;
665     }
666
667   template<typename _Key, typename _Value, 
668            typename _Allocator, typename _ExtractKey, typename _Equal,
669            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
670            bool __chc, bool __cit, bool __uk>
671     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
672                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
673     ~_Hashtable()
674     {
675       clear();
676       _M_deallocate_buckets(_M_buckets, _M_bucket_count);
677     }
678
679   template<typename _Key, typename _Value, 
680            typename _Allocator, typename _ExtractKey, typename _Equal,
681            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
682            bool __chc, bool __cit, bool __uk>
683     void
684     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
685                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
686     swap(_Hashtable& __x)
687     {
688       // The only base class with member variables is hash_code_base.  We
689       // define _Hash_code_base::_M_swap because different specializations
690       // have different members.
691       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
692         _H1, _H2, _Hash, __chc>::_M_swap(__x);
693
694       // _GLIBCXX_RESOLVE_LIB_DEFECTS
695       // 431. Swapping containers with unequal allocators.
696       std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
697                                                         __x._M_node_allocator);
698
699       std::swap(_M_rehash_policy, __x._M_rehash_policy);
700       std::swap(_M_buckets, __x._M_buckets);
701       std::swap(_M_bucket_count, __x._M_bucket_count);
702       std::swap(_M_element_count, __x._M_element_count);
703     }
704
705   template<typename _Key, typename _Value, 
706            typename _Allocator, typename _ExtractKey, typename _Equal,
707            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
708            bool __chc, bool __cit, bool __uk>
709     void
710     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
711                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
712     __rehash_policy(const _RehashPolicy& __pol)
713     {
714       _M_rehash_policy = __pol;
715       size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
716       if (__n_bkt > _M_bucket_count)
717         _M_rehash(__n_bkt);
718     }
719
720   template<typename _Key, typename _Value, 
721            typename _Allocator, typename _ExtractKey, typename _Equal,
722            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
723            bool __chc, bool __cit, bool __uk>
724     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
725                         _H1, _H2, _Hash, _RehashPolicy,
726                         __chc, __cit, __uk>::iterator
727     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
728                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
729     find(const key_type& __k)
730     {
731       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
732       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
733       _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
734       return __p ? iterator(__p, _M_buckets + __n) : this->end();
735     }
736
737   template<typename _Key, typename _Value, 
738            typename _Allocator, typename _ExtractKey, typename _Equal,
739            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
740            bool __chc, bool __cit, bool __uk>
741     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
742                         _H1, _H2, _Hash, _RehashPolicy,
743                         __chc, __cit, __uk>::const_iterator
744     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
745                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
746     find(const key_type& __k) const
747     {
748       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
749       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
750       _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
751       return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
752     }
753
754   template<typename _Key, typename _Value, 
755            typename _Allocator, typename _ExtractKey, typename _Equal,
756            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
757            bool __chc, bool __cit, bool __uk>
758     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
759                         _H1, _H2, _Hash, _RehashPolicy,
760                         __chc, __cit, __uk>::size_type
761     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
762                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
763     count(const key_type& __k) const
764     {
765       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
766       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
767       std::size_t __result = 0;
768       for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
769         if (this->_M_compare(__k, __code, __p))
770           ++__result;
771       return __result;
772     }
773
774   template<typename _Key, typename _Value, 
775            typename _Allocator, typename _ExtractKey, typename _Equal,
776            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
777            bool __chc, bool __cit, bool __uk>
778     std::pair<typename _Hashtable<_Key, _Value, _Allocator,
779                                   _ExtractKey, _Equal, _H1,
780                                   _H2, _Hash, _RehashPolicy,
781                                   __chc, __cit, __uk>::iterator,
782               typename _Hashtable<_Key, _Value, _Allocator,
783                                   _ExtractKey, _Equal, _H1,
784                                   _H2, _Hash, _RehashPolicy,
785                                   __chc, __cit, __uk>::iterator>
786     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
787                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
788     equal_range(const key_type& __k)
789     {
790       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
791       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
792       _Node** __head = _M_buckets + __n;
793       _Node* __p = _M_find_node(*__head, __k, __code);
794       
795       if (__p)
796         {
797           _Node* __p1 = __p->_M_next;
798           for (; __p1; __p1 = __p1->_M_next)
799             if (!this->_M_compare(__k, __code, __p1))
800               break;
801
802           iterator __first(__p, __head);
803           iterator __last(__p1, __head);
804           if (!__p1)
805             __last._M_incr_bucket();
806           return std::make_pair(__first, __last);
807         }
808       else
809         return std::make_pair(this->end(), this->end());
810     }
811
812   template<typename _Key, typename _Value, 
813            typename _Allocator, typename _ExtractKey, typename _Equal,
814            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
815            bool __chc, bool __cit, bool __uk>
816     std::pair<typename _Hashtable<_Key, _Value, _Allocator,
817                                   _ExtractKey, _Equal, _H1,
818                                   _H2, _Hash, _RehashPolicy,
819                                   __chc, __cit, __uk>::const_iterator,
820               typename _Hashtable<_Key, _Value, _Allocator,
821                                   _ExtractKey, _Equal, _H1,
822                                   _H2, _Hash, _RehashPolicy,
823                                   __chc, __cit, __uk>::const_iterator>
824     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
825                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
826     equal_range(const key_type& __k) const
827     {
828       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
829       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
830       _Node** __head = _M_buckets + __n;
831       _Node* __p = _M_find_node(*__head, __k, __code);
832
833       if (__p)
834         {
835           _Node* __p1 = __p->_M_next;
836           for (; __p1; __p1 = __p1->_M_next)
837             if (!this->_M_compare(__k, __code, __p1))
838               break;
839
840           const_iterator __first(__p, __head);
841           const_iterator __last(__p1, __head);
842           if (!__p1)
843             __last._M_incr_bucket();
844           return std::make_pair(__first, __last);
845         }
846       else
847         return std::make_pair(this->end(), this->end());
848     }
849
850   // Find the node whose key compares equal to k, beginning the search
851   // at p (usually the head of a bucket).  Return nil if no node is found.
852   template<typename _Key, typename _Value, 
853            typename _Allocator, typename _ExtractKey, typename _Equal,
854            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
855            bool __chc, bool __cit, bool __uk>
856     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
857                         _Equal, _H1, _H2, _Hash, _RehashPolicy,
858                         __chc, __cit, __uk>::_Node* 
859     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
860                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
861     _M_find_node(_Node* __p, const key_type& __k,
862                 typename _Hashtable::_Hash_code_type __code) const
863     {
864       for (; __p; __p = __p->_M_next)
865         if (this->_M_compare(__k, __code, __p))
866           return __p;
867       return false;
868     }
869
870   // Insert v in bucket n (assumes no element with its key already present).
871   template<typename _Key, typename _Value, 
872            typename _Allocator, typename _ExtractKey, typename _Equal,
873            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
874            bool __chc, bool __cit, bool __uk>
875     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
876                         _H1, _H2, _Hash, _RehashPolicy,
877                         __chc, __cit, __uk>::iterator
878     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
879                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
880     _M_insert_bucket(const value_type& __v, size_type __n,
881                      typename _Hashtable::_Hash_code_type __code)
882     {
883       std::pair<bool, std::size_t> __do_rehash
884         = _M_rehash_policy._M_need_rehash(_M_bucket_count,
885                                           _M_element_count, 1);
886
887       // Allocate the new node before doing the rehash so that we don't
888       // do a rehash if the allocation throws.
889       _Node* __new_node = _M_allocate_node(__v);
890
891       __try
892         {
893           if (__do_rehash.first)
894             {
895               const key_type& __k = this->_M_extract(__v);
896               __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
897               _M_rehash(__do_rehash.second);
898             }
899
900           __new_node->_M_next = _M_buckets[__n];
901           this->_M_store_code(__new_node, __code);
902           _M_buckets[__n] = __new_node;
903           ++_M_element_count;
904           return iterator(__new_node, _M_buckets + __n);
905         }
906       __catch(...)
907         {
908           _M_deallocate_node(__new_node);
909           __throw_exception_again;
910         }
911     }
912
913   // Insert v if no element with its key is already present.
914   template<typename _Key, typename _Value, 
915            typename _Allocator, typename _ExtractKey, typename _Equal,
916            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
917            bool __chc, bool __cit, bool __uk>
918     std::pair<typename _Hashtable<_Key, _Value, _Allocator,
919                                   _ExtractKey, _Equal, _H1,
920                                   _H2, _Hash, _RehashPolicy,
921                                   __chc, __cit, __uk>::iterator, bool>
922     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
923                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
924     _M_insert(const value_type& __v, std::true_type)
925     {
926       const key_type& __k = this->_M_extract(__v);
927       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
928       size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
929
930       if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
931         return std::make_pair(iterator(__p, _M_buckets + __n), false);
932       return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
933     }
934
935   // Insert v unconditionally.
936   template<typename _Key, typename _Value, 
937            typename _Allocator, typename _ExtractKey, typename _Equal,
938            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
939            bool __chc, bool __cit, bool __uk>
940     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
941                         _H1, _H2, _Hash, _RehashPolicy,
942                         __chc, __cit, __uk>::iterator
943     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
944                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
945     _M_insert(const value_type& __v, std::false_type)
946     {
947       std::pair<bool, std::size_t> __do_rehash
948         = _M_rehash_policy._M_need_rehash(_M_bucket_count,
949                                           _M_element_count, 1);
950       if (__do_rehash.first)
951         _M_rehash(__do_rehash.second);
952  
953       const key_type& __k = this->_M_extract(__v);
954       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
955       size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
956
957       // First find the node, avoid leaking new_node if compare throws.
958       _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
959       _Node* __new_node = _M_allocate_node(__v);
960
961       if (__prev)
962         {
963           __new_node->_M_next = __prev->_M_next;
964           __prev->_M_next = __new_node;
965         }
966       else
967         {
968           __new_node->_M_next = _M_buckets[__n];
969           _M_buckets[__n] = __new_node;
970         }
971       this->_M_store_code(__new_node, __code);
972
973       ++_M_element_count;
974       return iterator(__new_node, _M_buckets + __n);
975     }
976
977   template<typename _Key, typename _Value, 
978            typename _Allocator, typename _ExtractKey, typename _Equal,
979            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
980            bool __chc, bool __cit, bool __uk>
981     template<typename _InputIterator>
982       void 
983       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
984                  _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
985       insert(_InputIterator __first, _InputIterator __last)
986       {
987         size_type __n_elt = __detail::__distance_fw(__first, __last);
988         std::pair<bool, std::size_t> __do_rehash
989           = _M_rehash_policy._M_need_rehash(_M_bucket_count,
990                                             _M_element_count, __n_elt);
991         if (__do_rehash.first)
992           _M_rehash(__do_rehash.second);
993
994         for (; __first != __last; ++__first)
995           this->insert(*__first);
996       }
997
998   template<typename _Key, typename _Value, 
999            typename _Allocator, typename _ExtractKey, typename _Equal,
1000            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1001            bool __chc, bool __cit, bool __uk>
1002     void
1003     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1004                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1005     erase(const_iterator __it)
1006     {
1007       _Node* __p = __it._M_cur_node;
1008       _Node** __b = __it._M_cur_bucket;
1009
1010       _Node* __cur = *__b;
1011       if (__cur == __p)
1012         *__b = __cur->_M_next;
1013       else
1014         {
1015           _Node* __next = __cur->_M_next;
1016           while (__next != __p)
1017             {
1018               __cur = __next;
1019               __next = __cur->_M_next;
1020             }
1021           __cur->_M_next = __next->_M_next;
1022         }
1023
1024       _M_deallocate_node(__p);
1025       --_M_element_count;
1026     }
1027
1028   template<typename _Key, typename _Value, 
1029            typename _Allocator, typename _ExtractKey, typename _Equal,
1030            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1031            bool __chc, bool __cit, bool __uk>
1032     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1033                         _H1, _H2, _Hash, _RehashPolicy,
1034                         __chc, __cit, __uk>::size_type
1035     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1036                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1037     erase(const key_type& __k)
1038     {
1039       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1040       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1041       size_type __result = 0;
1042       
1043       _Node** __slot = _M_buckets + __n;
1044       while (*__slot && !this->_M_compare(__k, __code, *__slot))
1045         __slot = &((*__slot)->_M_next);
1046
1047       _Node** __saved_slot = 0;
1048       while (*__slot && this->_M_compare(__k, __code, *__slot))
1049         {
1050           // _GLIBCXX_RESOLVE_LIB_DEFECTS
1051           // 526. Is it undefined if a function in the standard changes
1052           // in parameters?
1053           if (&this->_M_extract((*__slot)->_M_v) != &__k)
1054             {
1055               _Node* __p = *__slot;
1056               *__slot = __p->_M_next;
1057               _M_deallocate_node(__p);
1058               --_M_element_count;
1059               ++__result;
1060             }
1061           else
1062             {
1063               __saved_slot = __slot;
1064               __slot = &((*__slot)->_M_next);
1065             }
1066         }
1067
1068       if (__saved_slot)
1069         {
1070           _Node* __p = *__saved_slot;
1071           *__saved_slot = __p->_M_next;
1072           _M_deallocate_node(__p);
1073           --_M_element_count;
1074           ++__result;
1075         }
1076
1077       return __result;
1078     }
1079
1080   // ??? This could be optimized by taking advantage of the bucket
1081   // structure, but it's not clear that it's worth doing.  It probably
1082   // wouldn't even be an optimization unless the load factor is large.
1083   template<typename _Key, typename _Value, 
1084            typename _Allocator, typename _ExtractKey, typename _Equal,
1085            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1086            bool __chc, bool __cit, bool __uk>
1087     void
1088     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1089                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1090     erase(const_iterator __first, const_iterator __last)
1091     {
1092       if (__first == begin() && __last == end())
1093         clear();
1094       else
1095         while (__first != __last)
1096           erase(__first++);
1097      }
1098
1099   template<typename _Key, typename _Value, 
1100            typename _Allocator, typename _ExtractKey, typename _Equal,
1101            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1102            bool __chc, bool __cit, bool __uk>
1103     void
1104     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1105                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1106     clear()
1107     {
1108       _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1109       _M_element_count = 0;
1110     }
1111  
1112   template<typename _Key, typename _Value, 
1113            typename _Allocator, typename _ExtractKey, typename _Equal,
1114            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1115            bool __chc, bool __cit, bool __uk>
1116     void
1117     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1118                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1119     rehash(size_type __n)
1120     {
1121       _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1122                          _M_rehash_policy._M_bkt_for_elements(_M_element_count
1123                                                               + 1)));
1124     }
1125
1126   template<typename _Key, typename _Value, 
1127            typename _Allocator, typename _ExtractKey, typename _Equal,
1128            typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1129            bool __chc, bool __cit, bool __uk>
1130     void
1131     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1132                _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1133     _M_rehash(size_type __n)
1134     {
1135       _Node** __new_array = _M_allocate_buckets(__n);
1136       __try
1137         {
1138           for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1139             while (_Node* __p = _M_buckets[__i])
1140               {
1141                 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1142                 _M_buckets[__i] = __p->_M_next;
1143                 __p->_M_next = __new_array[__new_index];
1144                 __new_array[__new_index] = __p;
1145               }
1146           _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1147           _M_bucket_count = __n;
1148           _M_buckets = __new_array;
1149         }
1150       __catch(...)
1151         {
1152           // A failure here means that a hash function threw an exception.
1153           // We can't restore the previous state without calling the hash
1154           // function again, so the only sensible recovery is to delete
1155           // everything.
1156           _M_deallocate_nodes(__new_array, __n);
1157           _M_deallocate_buckets(__new_array, __n);
1158           _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1159           _M_element_count = 0;
1160           __throw_exception_again;
1161         }
1162     }
1163 }
1164
1165 #endif // _HASHTABLE_H