OSDN Git Service

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