OSDN Git Service

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