libstdc++
bits/hashtable.h
Go to the documentation of this file.
00001 // hashtable.h header -*- C++ -*-
00002 
00003 // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/hashtable.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{unordered_map, unordered_set}
00028  */
00029 
00030 #ifndef _HASHTABLE_H
00031 #define _HASHTABLE_H 1
00032 
00033 #pragma GCC system_header
00034 
00035 #include <bits/hashtable_policy.h>
00036 
00037 namespace std _GLIBCXX_VISIBILITY(default)
00038 {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041   // Class template _Hashtable, class definition.
00042 
00043   // Meaning of class template _Hashtable's template parameters
00044 
00045   // _Key and _Value: arbitrary CopyConstructible types.
00046 
00047   // _Allocator: an allocator type ([lib.allocator.requirements]) whose
00048   // value type is Value.  As a conforming extension, we allow for
00049   // value type != Value.
00050 
00051   // _ExtractKey: function object that takes a object of type Value
00052   // and returns a value of type _Key.
00053 
00054   // _Equal: function object that takes two objects of type k and returns
00055   // a bool-like value that is true if the two objects are considered equal.
00056 
00057   // _H1: the hash function.  A unary function object with argument type
00058   // Key and result type size_t.  Return values should be distributed
00059   // over the entire range [0, numeric_limits<size_t>:::max()].
00060 
00061   // _H2: the range-hashing function (in the terminology of Tavori and
00062   // Dreizin).  A binary function object whose argument types and result
00063   // type are all size_t.  Given arguments r and N, the return value is
00064   // in the range [0, N).
00065 
00066   // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
00067   // whose argument types are _Key and size_t and whose result type is
00068   // size_t.  Given arguments k and N, the return value is in the range
00069   // [0, N).  Default: hash(k, N) = h2(h1(k), N).  If _Hash is anything other
00070   // than the default, _H1 and _H2 are ignored.
00071 
00072   // _RehashPolicy: Policy class with three members, all of which govern
00073   // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
00074   // than n.  _M_bkt_for_elements(n) returns a bucket count appropriate
00075   // for an element count of n.  _M_need_rehash(n_bkt, n_elt, n_ins)
00076   // determines whether, if the current bucket count is n_bkt and the
00077   // current element count is n_elt, we need to increase the bucket
00078   // count.  If so, returns make_pair(true, n), where n is the new
00079   // bucket count.  If not, returns make_pair(false, <anything>).
00080 
00081   // ??? Right now it is hard-wired that the number of buckets never
00082   // shrinks.  Should we allow _RehashPolicy to change that?
00083 
00084   // __cache_hash_code: bool.  true if we store the value of the hash
00085   // function along with the value.  This is a time-space tradeoff.
00086   // Storing it may improve lookup speed by reducing the number of times
00087   // we need to call the Equal function.
00088 
00089   // __constant_iterators: bool.  true if iterator and const_iterator are
00090   // both constant iterator types.  This is true for unordered_set and
00091   // unordered_multiset, false for unordered_map and unordered_multimap.
00092 
00093   // __unique_keys: bool.  true if the return value of _Hashtable::count(k)
00094   // is always at most one, false if it may be an arbitrary number.  This
00095   // true for unordered_set and unordered_map, false for unordered_multiset
00096   // and unordered_multimap.
00097 
00098   template<typename _Key, typename _Value, typename _Allocator,
00099        typename _ExtractKey, typename _Equal,
00100        typename _H1, typename _H2, typename _Hash,
00101        typename _RehashPolicy,
00102        bool __cache_hash_code,
00103        bool __constant_iterators,
00104        bool __unique_keys>
00105     class _Hashtable
00106     : public __detail::_Rehash_base<_RehashPolicy,
00107                     _Hashtable<_Key, _Value, _Allocator,
00108                            _ExtractKey,
00109                            _Equal, _H1, _H2, _Hash,
00110                            _RehashPolicy,
00111                            __cache_hash_code,
00112                            __constant_iterators,
00113                            __unique_keys> >,
00114       public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
00115                        _H1, _H2, _Hash, __cache_hash_code>,
00116       public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
00117                  _Hashtable<_Key, _Value, _Allocator,
00118                         _ExtractKey,
00119                         _Equal, _H1, _H2, _Hash,
00120                         _RehashPolicy,
00121                         __cache_hash_code,
00122                         __constant_iterators,
00123                         __unique_keys> >,
00124       public __detail::_Equality_base<_ExtractKey, __unique_keys,
00125                       _Hashtable<_Key, _Value, _Allocator,
00126                          _ExtractKey,
00127                          _Equal, _H1, _H2, _Hash,
00128                          _RehashPolicy,
00129                          __cache_hash_code,
00130                          __constant_iterators,
00131                          __unique_keys> >
00132     {
00133     public:
00134       typedef _Allocator                                  allocator_type;
00135       typedef _Value                                      value_type;
00136       typedef _Key                                        key_type;
00137       typedef _Equal                                      key_equal;
00138       // mapped_type, if present, comes from _Map_base.
00139       // hasher, if present, comes from _Hash_code_base.
00140       typedef typename _Allocator::pointer                pointer;
00141       typedef typename _Allocator::const_pointer          const_pointer;
00142       typedef typename _Allocator::reference              reference;
00143       typedef typename _Allocator::const_reference        const_reference;
00144 
00145       typedef std::size_t                                 size_type;
00146       typedef std::ptrdiff_t                              difference_type;
00147       typedef __detail::_Node_iterator<value_type, __constant_iterators,
00148                        __cache_hash_code>
00149                               local_iterator;
00150       typedef __detail::_Node_const_iterator<value_type,
00151                          __constant_iterators,
00152                          __cache_hash_code>
00153                               const_local_iterator;
00154 
00155       typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
00156                         __cache_hash_code>
00157                               iterator;
00158       typedef __detail::_Hashtable_const_iterator<value_type,
00159                           __constant_iterators,
00160                           __cache_hash_code>
00161                               const_iterator;
00162 
00163       template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2,
00164            typename _Hashtable2>
00165     friend struct __detail::_Map_base;
00166 
00167     private:
00168       typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
00169       typedef typename _Allocator::template rebind<_Node>::other
00170                             _Node_allocator_type;
00171       typedef typename _Allocator::template rebind<_Node*>::other
00172                             _Bucket_allocator_type;
00173 
00174       typedef typename _Allocator::template rebind<_Value>::other
00175                             _Value_allocator_type;
00176 
00177       _Node_allocator_type   _M_node_allocator;
00178       _Node**                _M_buckets;
00179       size_type              _M_bucket_count;
00180       size_type              _M_begin_bucket_index; // First non-empty bucket.
00181       size_type              _M_element_count;
00182       _RehashPolicy          _M_rehash_policy;
00183 
00184       template<typename... _Args>
00185     _Node*
00186     _M_allocate_node(_Args&&... __args);
00187 
00188       void
00189       _M_deallocate_node(_Node* __n);
00190 
00191       void
00192       _M_deallocate_nodes(_Node**, size_type);
00193 
00194       _Node**
00195       _M_allocate_buckets(size_type __n);
00196 
00197       void
00198       _M_deallocate_buckets(_Node**, size_type __n);
00199 
00200     public:
00201       // Constructor, destructor, assignment, swap
00202       _Hashtable(size_type __bucket_hint,
00203          const _H1&, const _H2&, const _Hash&,
00204          const _Equal&, const _ExtractKey&,
00205          const allocator_type&);
00206 
00207       template<typename _InputIterator>
00208     _Hashtable(_InputIterator __first, _InputIterator __last,
00209            size_type __bucket_hint,
00210            const _H1&, const _H2&, const _Hash&,
00211            const _Equal&, const _ExtractKey&,
00212            const allocator_type&);
00213 
00214       _Hashtable(const _Hashtable&);
00215 
00216       _Hashtable(_Hashtable&&);
00217 
00218       _Hashtable&
00219       operator=(const _Hashtable& __ht)
00220       {
00221     _Hashtable __tmp(__ht);
00222     this->swap(__tmp);
00223     return *this;
00224       }
00225 
00226       _Hashtable&
00227       operator=(_Hashtable&& __ht)
00228       {
00229     // NB: DR 1204.
00230     // NB: DR 675.
00231     this->clear();
00232     this->swap(__ht);
00233     return *this;
00234       }
00235 
00236       ~_Hashtable();
00237 
00238       void swap(_Hashtable&);
00239 
00240       // Basic container operations
00241       iterator
00242       begin()
00243       { return iterator(_M_buckets + _M_begin_bucket_index); }
00244 
00245       const_iterator
00246       begin() const
00247       { return const_iterator(_M_buckets + _M_begin_bucket_index); }
00248 
00249       iterator
00250       end()
00251       { return iterator(_M_buckets + _M_bucket_count); }
00252 
00253       const_iterator
00254       end() const
00255       { return const_iterator(_M_buckets + _M_bucket_count); }
00256 
00257       const_iterator
00258       cbegin() const
00259       { return const_iterator(_M_buckets + _M_begin_bucket_index); }
00260 
00261       const_iterator
00262       cend() const
00263       { return const_iterator(_M_buckets + _M_bucket_count); }
00264 
00265       size_type
00266       size() const
00267       { return _M_element_count; }
00268 
00269       bool
00270       empty() const
00271       { return size() == 0; }
00272 
00273       allocator_type
00274       get_allocator() const
00275       { return allocator_type(_M_node_allocator); }
00276 
00277       size_type
00278       max_size() const
00279       { return _M_node_allocator.max_size(); }
00280 
00281       // Observers
00282       key_equal
00283       key_eq() const
00284       { return this->_M_eq; }
00285 
00286       // hash_function, if present, comes from _Hash_code_base.
00287 
00288       // Bucket operations
00289       size_type
00290       bucket_count() const
00291       { return _M_bucket_count; }
00292 
00293       size_type
00294       max_bucket_count() const
00295       { return max_size(); }
00296 
00297       size_type
00298       bucket_size(size_type __n) const
00299       { return std::distance(begin(__n), end(__n)); }
00300 
00301       size_type
00302       bucket(const key_type& __k) const
00303       {
00304     return this->_M_bucket_index(__k, this->_M_hash_code(__k),
00305                      bucket_count());
00306       }
00307 
00308       local_iterator
00309       begin(size_type __n)
00310       { return local_iterator(_M_buckets[__n]); }
00311 
00312       local_iterator
00313       end(size_type)
00314       { return local_iterator(0); }
00315 
00316       const_local_iterator
00317       begin(size_type __n) const
00318       { return const_local_iterator(_M_buckets[__n]); }
00319 
00320       const_local_iterator
00321       end(size_type) const
00322       { return const_local_iterator(0); }
00323 
00324       // DR 691.
00325       const_local_iterator
00326       cbegin(size_type __n) const
00327       { return const_local_iterator(_M_buckets[__n]); }
00328 
00329       const_local_iterator
00330       cend(size_type) const
00331       { return const_local_iterator(0); }
00332 
00333       float
00334       load_factor() const
00335       {
00336     return static_cast<float>(size()) / static_cast<float>(bucket_count());
00337       }
00338 
00339       // max_load_factor, if present, comes from _Rehash_base.
00340 
00341       // Generalization of max_load_factor.  Extension, not found in TR1.  Only
00342       // useful if _RehashPolicy is something other than the default.
00343       const _RehashPolicy&
00344       __rehash_policy() const
00345       { return _M_rehash_policy; }
00346 
00347       void
00348       __rehash_policy(const _RehashPolicy&);
00349 
00350       // Lookup.
00351       iterator
00352       find(const key_type& __k);
00353 
00354       const_iterator
00355       find(const key_type& __k) const;
00356 
00357       size_type
00358       count(const key_type& __k) const;
00359 
00360       std::pair<iterator, iterator>
00361       equal_range(const key_type& __k);
00362 
00363       std::pair<const_iterator, const_iterator>
00364       equal_range(const key_type& __k) const;
00365 
00366     private:
00367       // Find and insert helper functions and types
00368       _Node*
00369       _M_find_node(_Node*, const key_type&,
00370            typename _Hashtable::_Hash_code_type) const;
00371 
00372       template<typename _Arg>
00373     iterator
00374     _M_insert_bucket(_Arg&&, size_type,
00375              typename _Hashtable::_Hash_code_type);
00376 
00377       template<typename _Arg>
00378     std::pair<iterator, bool>
00379     _M_insert(_Arg&&, std::true_type);
00380 
00381       template<typename _Arg>
00382     iterator
00383     _M_insert(_Arg&&, std::false_type);
00384 
00385       typedef typename std::conditional<__unique_keys,
00386                     std::pair<iterator, bool>,
00387                     iterator>::type
00388     _Insert_Return_Type;
00389 
00390       typedef typename std::conditional<__unique_keys,
00391                     std::_Select1st<_Insert_Return_Type>,
00392                     std::_Identity<_Insert_Return_Type>
00393                    >::type
00394     _Insert_Conv_Type;
00395 
00396     public:
00397       // Insert and erase
00398       _Insert_Return_Type
00399       insert(const value_type& __v)
00400       { return _M_insert(__v, std::integral_constant<bool, __unique_keys>()); }
00401 
00402       iterator
00403       insert(const_iterator, const value_type& __v)
00404       { return _Insert_Conv_Type()(insert(__v)); }
00405 
00406       _Insert_Return_Type
00407       insert(value_type&& __v)
00408       { return _M_insert(std::move(__v),
00409              std::integral_constant<bool, __unique_keys>()); }
00410 
00411       iterator
00412       insert(const_iterator, value_type&& __v)
00413       { return _Insert_Conv_Type()(insert(std::move(__v))); }
00414 
00415       template<typename _Pair, typename = typename
00416            std::enable_if<!__constant_iterators
00417                   && std::is_convertible<_Pair,
00418                              value_type>::value>::type>
00419     _Insert_Return_Type
00420     insert(_Pair&& __v)
00421     { return _M_insert(std::forward<_Pair>(__v),
00422                std::integral_constant<bool, __unique_keys>()); }
00423 
00424       template<typename _Pair, typename = typename
00425            std::enable_if<!__constant_iterators
00426                   && std::is_convertible<_Pair,
00427                              value_type>::value>::type>
00428     iterator
00429     insert(const_iterator, _Pair&& __v)
00430     { return _Insert_Conv_Type()(insert(std::forward<_Pair>(__v))); }
00431 
00432       template<typename _InputIterator>
00433     void
00434     insert(_InputIterator __first, _InputIterator __last);
00435 
00436       void
00437       insert(initializer_list<value_type> __l)
00438       { this->insert(__l.begin(), __l.end()); }
00439 
00440       iterator
00441       erase(const_iterator);
00442 
00443       size_type
00444       erase(const key_type&);
00445 
00446       iterator
00447       erase(const_iterator, const_iterator);
00448 
00449       void
00450       clear();
00451 
00452       // Set number of buckets to be appropriate for container of n element.
00453       void rehash(size_type __n);
00454 
00455       // DR 1189.
00456       // reserve, if present, comes from _Rehash_base.
00457 
00458     private:
00459       // Unconditionally change size of bucket array to n.
00460       void _M_rehash(size_type __n);
00461     };
00462 
00463 
00464   // Definitions of class template _Hashtable's out-of-line member functions.
00465   template<typename _Key, typename _Value,
00466        typename _Allocator, typename _ExtractKey, typename _Equal,
00467        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00468        bool __chc, bool __cit, bool __uk>
00469     template<typename... _Args>
00470       typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00471               _H1, _H2, _Hash, _RehashPolicy,
00472               __chc, __cit, __uk>::_Node*
00473       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00474          _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00475       _M_allocate_node(_Args&&... __args)
00476       {
00477     _Node* __n = _M_node_allocator.allocate(1);
00478     __try
00479       {
00480         _M_node_allocator.construct(__n, std::forward<_Args>(__args)...);
00481         __n->_M_next = 0;
00482         return __n;
00483       }
00484     __catch(...)
00485       {
00486         _M_node_allocator.deallocate(__n, 1);
00487         __throw_exception_again;
00488       }
00489       }
00490 
00491   template<typename _Key, typename _Value,
00492        typename _Allocator, typename _ExtractKey, typename _Equal,
00493        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00494        bool __chc, bool __cit, bool __uk>
00495     void
00496     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00497            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00498     _M_deallocate_node(_Node* __n)
00499     {
00500       _M_node_allocator.destroy(__n);
00501       _M_node_allocator.deallocate(__n, 1);
00502     }
00503 
00504   template<typename _Key, typename _Value,
00505        typename _Allocator, typename _ExtractKey, typename _Equal,
00506        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00507        bool __chc, bool __cit, bool __uk>
00508     void
00509     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00510            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00511     _M_deallocate_nodes(_Node** __array, size_type __n)
00512     {
00513       for (size_type __i = 0; __i < __n; ++__i)
00514     {
00515       _Node* __p = __array[__i];
00516       while (__p)
00517         {
00518           _Node* __tmp = __p;
00519           __p = __p->_M_next;
00520           _M_deallocate_node(__tmp);
00521         }
00522       __array[__i] = 0;
00523     }
00524     }
00525 
00526   template<typename _Key, typename _Value,
00527        typename _Allocator, typename _ExtractKey, typename _Equal,
00528        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00529        bool __chc, bool __cit, bool __uk>
00530     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00531             _H1, _H2, _Hash, _RehashPolicy,
00532             __chc, __cit, __uk>::_Node**
00533     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00534            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00535     _M_allocate_buckets(size_type __n)
00536     {
00537       _Bucket_allocator_type __alloc(_M_node_allocator);
00538 
00539       // We allocate one extra bucket to hold a sentinel, an arbitrary
00540       // non-null pointer.  Iterator increment relies on this.
00541       _Node** __p = __alloc.allocate(__n + 1);
00542       std::fill(__p, __p + __n, (_Node*) 0);
00543       __p[__n] = reinterpret_cast<_Node*>(0x1000);
00544       return __p;
00545     }
00546 
00547   template<typename _Key, typename _Value,
00548        typename _Allocator, typename _ExtractKey, typename _Equal,
00549        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00550        bool __chc, bool __cit, bool __uk>
00551     void
00552     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00553            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00554     _M_deallocate_buckets(_Node** __p, size_type __n)
00555     {
00556       _Bucket_allocator_type __alloc(_M_node_allocator);
00557       __alloc.deallocate(__p, __n + 1);
00558     }
00559 
00560   template<typename _Key, typename _Value,
00561        typename _Allocator, typename _ExtractKey, typename _Equal,
00562        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00563        bool __chc, bool __cit, bool __uk>
00564     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00565            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00566     _Hashtable(size_type __bucket_hint,
00567            const _H1& __h1, const _H2& __h2, const _Hash& __h,
00568            const _Equal& __eq, const _ExtractKey& __exk,
00569            const allocator_type& __a)
00570     : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
00571       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
00572                 _H1, _H2, _Hash, __chc>(__exk, __eq,
00573                             __h1, __h2, __h),
00574       __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
00575       _M_node_allocator(__a),
00576       _M_bucket_count(0),
00577       _M_element_count(0),
00578       _M_rehash_policy()
00579     {
00580       _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
00581       _M_buckets = _M_allocate_buckets(_M_bucket_count);
00582       _M_begin_bucket_index = _M_bucket_count;
00583     }
00584 
00585   template<typename _Key, typename _Value,
00586        typename _Allocator, typename _ExtractKey, typename _Equal,
00587        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00588        bool __chc, bool __cit, bool __uk>
00589     template<typename _InputIterator>
00590       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00591          _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00592       _Hashtable(_InputIterator __f, _InputIterator __l,
00593          size_type __bucket_hint,
00594          const _H1& __h1, const _H2& __h2, const _Hash& __h,
00595          const _Equal& __eq, const _ExtractKey& __exk,
00596          const allocator_type& __a)
00597       : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
00598     __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
00599                   _H1, _H2, _Hash, __chc>(__exk, __eq,
00600                               __h1, __h2, __h),
00601     __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
00602     _M_node_allocator(__a),
00603     _M_bucket_count(0),
00604     _M_element_count(0),
00605     _M_rehash_policy()
00606       {
00607     _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
00608                    _M_rehash_policy.
00609                    _M_bkt_for_elements(__detail::
00610                                __distance_fw(__f,
00611                                      __l)));
00612     _M_buckets = _M_allocate_buckets(_M_bucket_count);
00613     _M_begin_bucket_index = _M_bucket_count;
00614     __try
00615       {
00616         for (; __f != __l; ++__f)
00617           this->insert(*__f);
00618       }
00619     __catch(...)
00620       {
00621         clear();
00622         _M_deallocate_buckets(_M_buckets, _M_bucket_count);
00623         __throw_exception_again;
00624       }
00625       }
00626 
00627   template<typename _Key, typename _Value,
00628        typename _Allocator, typename _ExtractKey, typename _Equal,
00629        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00630        bool __chc, bool __cit, bool __uk>
00631     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00632            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00633     _Hashtable(const _Hashtable& __ht)
00634     : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
00635       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
00636                 _H1, _H2, _Hash, __chc>(__ht),
00637       __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
00638       _M_node_allocator(__ht._M_node_allocator),
00639       _M_bucket_count(__ht._M_bucket_count),
00640       _M_begin_bucket_index(__ht._M_begin_bucket_index),
00641       _M_element_count(__ht._M_element_count),
00642       _M_rehash_policy(__ht._M_rehash_policy)
00643     {
00644       _M_buckets = _M_allocate_buckets(_M_bucket_count);
00645       __try
00646     {
00647       for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
00648         {
00649           _Node* __n = __ht._M_buckets[__i];
00650           _Node** __tail = _M_buckets + __i;
00651           while (__n)
00652         {
00653           *__tail = _M_allocate_node(__n->_M_v);
00654           this->_M_copy_code(*__tail, __n);
00655           __tail = &((*__tail)->_M_next);
00656           __n = __n->_M_next;
00657         }
00658         }
00659     }
00660       __catch(...)
00661     {
00662       clear();
00663       _M_deallocate_buckets(_M_buckets, _M_bucket_count);
00664       __throw_exception_again;
00665     }
00666     }
00667 
00668   template<typename _Key, typename _Value,
00669        typename _Allocator, typename _ExtractKey, typename _Equal,
00670        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00671        bool __chc, bool __cit, bool __uk>
00672     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00673            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00674     _Hashtable(_Hashtable&& __ht)
00675     : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
00676       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
00677                 _H1, _H2, _Hash, __chc>(__ht),
00678       __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
00679       _M_node_allocator(__ht._M_node_allocator),
00680       _M_buckets(__ht._M_buckets),
00681       _M_bucket_count(__ht._M_bucket_count),
00682       _M_begin_bucket_index(__ht._M_begin_bucket_index),
00683       _M_element_count(__ht._M_element_count),
00684       _M_rehash_policy(__ht._M_rehash_policy)
00685     {
00686       __ht._M_rehash_policy = _RehashPolicy();
00687       __ht._M_bucket_count = __ht._M_rehash_policy._M_next_bkt(0);
00688       __ht._M_buckets = __ht._M_allocate_buckets(__ht._M_bucket_count);
00689       __ht._M_begin_bucket_index = __ht._M_bucket_count;
00690       __ht._M_element_count = 0;
00691     }
00692 
00693   template<typename _Key, typename _Value,
00694        typename _Allocator, typename _ExtractKey, typename _Equal,
00695        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00696        bool __chc, bool __cit, bool __uk>
00697     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00698            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00699     ~_Hashtable()
00700     {
00701       clear();
00702       _M_deallocate_buckets(_M_buckets, _M_bucket_count);
00703     }
00704 
00705   template<typename _Key, typename _Value,
00706        typename _Allocator, typename _ExtractKey, typename _Equal,
00707        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00708        bool __chc, bool __cit, bool __uk>
00709     void
00710     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00711            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00712     swap(_Hashtable& __x)
00713     {
00714       // The only base class with member variables is hash_code_base.  We
00715       // define _Hash_code_base::_M_swap because different specializations
00716       // have different members.
00717       __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
00718     _H1, _H2, _Hash, __chc>::_M_swap(__x);
00719 
00720       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00721       // 431. Swapping containers with unequal allocators.
00722       std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
00723                             __x._M_node_allocator);
00724 
00725       std::swap(_M_rehash_policy, __x._M_rehash_policy);
00726       std::swap(_M_buckets, __x._M_buckets);
00727       std::swap(_M_bucket_count, __x._M_bucket_count);
00728       std::swap(_M_begin_bucket_index, __x._M_begin_bucket_index);
00729       std::swap(_M_element_count, __x._M_element_count);
00730     }
00731 
00732   template<typename _Key, typename _Value,
00733        typename _Allocator, typename _ExtractKey, typename _Equal,
00734        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00735        bool __chc, bool __cit, bool __uk>
00736     void
00737     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00738            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00739     __rehash_policy(const _RehashPolicy& __pol)
00740     {
00741       _M_rehash_policy = __pol;
00742       size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
00743       if (__n_bkt > _M_bucket_count)
00744     _M_rehash(__n_bkt);
00745     }
00746 
00747   template<typename _Key, typename _Value,
00748        typename _Allocator, typename _ExtractKey, typename _Equal,
00749        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00750        bool __chc, bool __cit, bool __uk>
00751     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00752             _H1, _H2, _Hash, _RehashPolicy,
00753             __chc, __cit, __uk>::iterator
00754     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00755            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00756     find(const key_type& __k)
00757     {
00758       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00759       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00760       _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
00761       return __p ? iterator(__p, _M_buckets + __n) : this->end();
00762     }
00763 
00764   template<typename _Key, typename _Value,
00765        typename _Allocator, typename _ExtractKey, typename _Equal,
00766        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00767        bool __chc, bool __cit, bool __uk>
00768     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00769             _H1, _H2, _Hash, _RehashPolicy,
00770             __chc, __cit, __uk>::const_iterator
00771     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00772            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00773     find(const key_type& __k) const
00774     {
00775       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00776       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00777       _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
00778       return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
00779     }
00780 
00781   template<typename _Key, typename _Value,
00782        typename _Allocator, typename _ExtractKey, typename _Equal,
00783        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00784        bool __chc, bool __cit, bool __uk>
00785     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00786             _H1, _H2, _Hash, _RehashPolicy,
00787             __chc, __cit, __uk>::size_type
00788     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00789            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00790     count(const key_type& __k) const
00791     {
00792       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00793       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00794       std::size_t __result = 0;
00795       for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
00796     if (this->_M_compare(__k, __code, __p))
00797       ++__result;
00798       return __result;
00799     }
00800 
00801   template<typename _Key, typename _Value,
00802        typename _Allocator, typename _ExtractKey, typename _Equal,
00803        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00804        bool __chc, bool __cit, bool __uk>
00805     std::pair<typename _Hashtable<_Key, _Value, _Allocator,
00806                   _ExtractKey, _Equal, _H1,
00807                   _H2, _Hash, _RehashPolicy,
00808                   __chc, __cit, __uk>::iterator,
00809           typename _Hashtable<_Key, _Value, _Allocator,
00810                   _ExtractKey, _Equal, _H1,
00811                   _H2, _Hash, _RehashPolicy,
00812                   __chc, __cit, __uk>::iterator>
00813     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00814            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00815     equal_range(const key_type& __k)
00816     {
00817       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00818       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00819       _Node** __head = _M_buckets + __n;
00820       _Node* __p = _M_find_node(*__head, __k, __code);
00821 
00822       if (__p)
00823     {
00824       _Node* __p1 = __p->_M_next;
00825       for (; __p1; __p1 = __p1->_M_next)
00826         if (!this->_M_compare(__k, __code, __p1))
00827           break;
00828 
00829       iterator __first(__p, __head);
00830       iterator __last(__p1, __head);
00831       if (!__p1)
00832         __last._M_incr_bucket();
00833       return std::make_pair(__first, __last);
00834     }
00835       else
00836     return std::make_pair(this->end(), this->end());
00837     }
00838 
00839   template<typename _Key, typename _Value,
00840        typename _Allocator, typename _ExtractKey, typename _Equal,
00841        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00842        bool __chc, bool __cit, bool __uk>
00843     std::pair<typename _Hashtable<_Key, _Value, _Allocator,
00844                   _ExtractKey, _Equal, _H1,
00845                   _H2, _Hash, _RehashPolicy,
00846                   __chc, __cit, __uk>::const_iterator,
00847           typename _Hashtable<_Key, _Value, _Allocator,
00848                   _ExtractKey, _Equal, _H1,
00849                   _H2, _Hash, _RehashPolicy,
00850                   __chc, __cit, __uk>::const_iterator>
00851     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00852            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00853     equal_range(const key_type& __k) const
00854     {
00855       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00856       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00857       _Node** __head = _M_buckets + __n;
00858       _Node* __p = _M_find_node(*__head, __k, __code);
00859 
00860       if (__p)
00861     {
00862       _Node* __p1 = __p->_M_next;
00863       for (; __p1; __p1 = __p1->_M_next)
00864         if (!this->_M_compare(__k, __code, __p1))
00865           break;
00866 
00867       const_iterator __first(__p, __head);
00868       const_iterator __last(__p1, __head);
00869       if (!__p1)
00870         __last._M_incr_bucket();
00871       return std::make_pair(__first, __last);
00872     }
00873       else
00874     return std::make_pair(this->end(), this->end());
00875     }
00876 
00877   // Find the node whose key compares equal to k, beginning the search
00878   // at p (usually the head of a bucket).  Return nil if no node is found.
00879   template<typename _Key, typename _Value,
00880        typename _Allocator, typename _ExtractKey, typename _Equal,
00881        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00882        bool __chc, bool __cit, bool __uk>
00883     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
00884             _Equal, _H1, _H2, _Hash, _RehashPolicy,
00885             __chc, __cit, __uk>::_Node*
00886     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00887            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00888     _M_find_node(_Node* __p, const key_type& __k,
00889         typename _Hashtable::_Hash_code_type __code) const
00890     {
00891       for (; __p; __p = __p->_M_next)
00892     if (this->_M_compare(__k, __code, __p))
00893       return __p;
00894       return false;
00895     }
00896 
00897   // Insert v in bucket n (assumes no element with its key already present).
00898   template<typename _Key, typename _Value,
00899        typename _Allocator, typename _ExtractKey, typename _Equal,
00900        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00901        bool __chc, bool __cit, bool __uk>
00902     template<typename _Arg>
00903       typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00904               _H1, _H2, _Hash, _RehashPolicy,
00905               __chc, __cit, __uk>::iterator
00906       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00907          _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00908       _M_insert_bucket(_Arg&& __v, size_type __n,
00909                typename _Hashtable::_Hash_code_type __code)
00910       {
00911     std::pair<bool, std::size_t> __do_rehash
00912       = _M_rehash_policy._M_need_rehash(_M_bucket_count,
00913                         _M_element_count, 1);
00914 
00915     if (__do_rehash.first)
00916       {
00917         const key_type& __k = this->_M_extract(__v);
00918         __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
00919       }
00920 
00921     // Allocate the new node before doing the rehash so that we don't
00922     // do a rehash if the allocation throws.
00923     _Node* __new_node = _M_allocate_node(std::forward<_Arg>(__v));
00924 
00925     __try
00926       {
00927         if (__do_rehash.first)
00928           _M_rehash(__do_rehash.second);
00929 
00930         __new_node->_M_next = _M_buckets[__n];
00931         this->_M_store_code(__new_node, __code);
00932         _M_buckets[__n] = __new_node;
00933         ++_M_element_count;
00934         if (__n < _M_begin_bucket_index)
00935           _M_begin_bucket_index = __n;
00936         return iterator(__new_node, _M_buckets + __n);
00937       }
00938     __catch(...)
00939       {
00940         _M_deallocate_node(__new_node);
00941         __throw_exception_again;
00942       }
00943       }
00944 
00945   // Insert v if no element with its key is already present.
00946   template<typename _Key, typename _Value,
00947        typename _Allocator, typename _ExtractKey, typename _Equal,
00948        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00949        bool __chc, bool __cit, bool __uk>
00950     template<typename _Arg>
00951       std::pair<typename _Hashtable<_Key, _Value, _Allocator,
00952                     _ExtractKey, _Equal, _H1,
00953                     _H2, _Hash, _RehashPolicy,
00954                     __chc, __cit, __uk>::iterator, bool>
00955       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00956          _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00957       _M_insert(_Arg&& __v, std::true_type)
00958       {
00959     const key_type& __k = this->_M_extract(__v);
00960     typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00961     size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00962 
00963     if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
00964       return std::make_pair(iterator(__p, _M_buckets + __n), false);
00965     return std::make_pair(_M_insert_bucket(std::forward<_Arg>(__v),
00966                   __n, __code), true);
00967       }
00968 
00969   // Insert v unconditionally.
00970   template<typename _Key, typename _Value,
00971        typename _Allocator, typename _ExtractKey, typename _Equal,
00972        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
00973        bool __chc, bool __cit, bool __uk>
00974     template<typename _Arg>
00975       typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00976               _H1, _H2, _Hash, _RehashPolicy,
00977               __chc, __cit, __uk>::iterator
00978       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
00979          _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
00980       _M_insert(_Arg&& __v, std::false_type)
00981       {
00982     std::pair<bool, std::size_t> __do_rehash
00983       = _M_rehash_policy._M_need_rehash(_M_bucket_count,
00984                         _M_element_count, 1);
00985     if (__do_rehash.first)
00986       _M_rehash(__do_rehash.second);
00987 
00988     const key_type& __k = this->_M_extract(__v);
00989     typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
00990     size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
00991 
00992     // First find the node, avoid leaking new_node if compare throws.
00993     _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
00994     _Node* __new_node = _M_allocate_node(std::forward<_Arg>(__v));
00995 
00996     if (__prev)
00997       {
00998         __new_node->_M_next = __prev->_M_next;
00999         __prev->_M_next = __new_node;
01000       }
01001     else
01002       {
01003         __new_node->_M_next = _M_buckets[__n];
01004         _M_buckets[__n] = __new_node;
01005         if (__n < _M_begin_bucket_index)
01006           _M_begin_bucket_index = __n;
01007       }
01008     this->_M_store_code(__new_node, __code);
01009 
01010     ++_M_element_count;
01011     return iterator(__new_node, _M_buckets + __n);
01012       }
01013 
01014   template<typename _Key, typename _Value,
01015        typename _Allocator, typename _ExtractKey, typename _Equal,
01016        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01017        bool __chc, bool __cit, bool __uk>
01018     template<typename _InputIterator>
01019       void
01020       _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01021          _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01022       insert(_InputIterator __first, _InputIterator __last)
01023       {
01024     size_type __n_elt = __detail::__distance_fw(__first, __last);
01025     std::pair<bool, std::size_t> __do_rehash
01026       = _M_rehash_policy._M_need_rehash(_M_bucket_count,
01027                         _M_element_count, __n_elt);
01028     if (__do_rehash.first)
01029       _M_rehash(__do_rehash.second);
01030 
01031     for (; __first != __last; ++__first)
01032       this->insert(*__first);
01033       }
01034 
01035   template<typename _Key, typename _Value,
01036        typename _Allocator, typename _ExtractKey, typename _Equal,
01037        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01038        bool __chc, bool __cit, bool __uk>
01039     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01040             _H1, _H2, _Hash, _RehashPolicy,
01041             __chc, __cit, __uk>::iterator
01042     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01043            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01044     erase(const_iterator __it)
01045     {
01046       iterator __result(__it._M_cur_node, __it._M_cur_bucket);
01047       ++__result;
01048 
01049       _Node* __cur = *__it._M_cur_bucket;
01050       if (__cur == __it._M_cur_node)
01051     {
01052       *__it._M_cur_bucket = __cur->_M_next;
01053 
01054       // If _M_begin_bucket_index no longer indexes the first non-empty
01055       // bucket - its single node is being erased - update it.
01056       if (!_M_buckets[_M_begin_bucket_index])
01057         _M_begin_bucket_index = __result._M_cur_bucket - _M_buckets;
01058     }
01059       else
01060     {
01061       _Node* __next = __cur->_M_next;
01062       while (__next != __it._M_cur_node)
01063         {
01064           __cur = __next;
01065           __next = __cur->_M_next;
01066         }
01067       __cur->_M_next = __next->_M_next;
01068     }
01069 
01070       _M_deallocate_node(__it._M_cur_node);
01071       --_M_element_count;
01072 
01073       return __result;
01074     }
01075 
01076   template<typename _Key, typename _Value,
01077        typename _Allocator, typename _ExtractKey, typename _Equal,
01078        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01079        bool __chc, bool __cit, bool __uk>
01080     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01081             _H1, _H2, _Hash, _RehashPolicy,
01082             __chc, __cit, __uk>::size_type
01083     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01084            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01085     erase(const key_type& __k)
01086     {
01087       typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
01088       std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
01089       size_type __result = 0;
01090 
01091       _Node** __slot = _M_buckets + __n;
01092       while (*__slot && !this->_M_compare(__k, __code, *__slot))
01093     __slot = &((*__slot)->_M_next);
01094 
01095       _Node** __saved_slot = 0;
01096       while (*__slot && this->_M_compare(__k, __code, *__slot))
01097     {
01098       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01099       // 526. Is it undefined if a function in the standard changes
01100       // in parameters?
01101       if (std::__addressof(this->_M_extract((*__slot)->_M_v))
01102           != std::__addressof(__k))
01103         {
01104           _Node* __p = *__slot;
01105           *__slot = __p->_M_next;
01106           _M_deallocate_node(__p);
01107           --_M_element_count;
01108           ++__result;
01109         }
01110       else
01111         {
01112           __saved_slot = __slot;
01113           __slot = &((*__slot)->_M_next);
01114         }
01115     }
01116 
01117       if (__saved_slot)
01118     {
01119       _Node* __p = *__saved_slot;
01120       *__saved_slot = __p->_M_next;
01121       _M_deallocate_node(__p);
01122       --_M_element_count;
01123       ++__result;
01124     }
01125 
01126       // If the entire bucket indexed by _M_begin_bucket_index has been
01127       // erased look forward for the first non-empty bucket.
01128       if (!_M_buckets[_M_begin_bucket_index])
01129     {
01130       if (!_M_element_count)
01131         _M_begin_bucket_index = _M_bucket_count;
01132       else
01133         {
01134           ++_M_begin_bucket_index;
01135           while (!_M_buckets[_M_begin_bucket_index])
01136         ++_M_begin_bucket_index;
01137         }
01138     }
01139 
01140       return __result;
01141     }
01142 
01143   // ??? This could be optimized by taking advantage of the bucket
01144   // structure, but it's not clear that it's worth doing.  It probably
01145   // wouldn't even be an optimization unless the load factor is large.
01146   template<typename _Key, typename _Value,
01147        typename _Allocator, typename _ExtractKey, typename _Equal,
01148        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01149        bool __chc, bool __cit, bool __uk>
01150     typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01151             _H1, _H2, _Hash, _RehashPolicy,
01152             __chc, __cit, __uk>::iterator
01153     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01154            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01155     erase(const_iterator __first, const_iterator __last)
01156     {
01157        while (__first != __last)
01158      __first = this->erase(__first);
01159       return iterator(__last._M_cur_node, __last._M_cur_bucket);
01160     }
01161 
01162   template<typename _Key, typename _Value,
01163        typename _Allocator, typename _ExtractKey, typename _Equal,
01164        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01165        bool __chc, bool __cit, bool __uk>
01166     void
01167     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01168            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01169     clear()
01170     {
01171       _M_deallocate_nodes(_M_buckets, _M_bucket_count);
01172       _M_element_count = 0;
01173       _M_begin_bucket_index = _M_bucket_count;
01174     }
01175 
01176   template<typename _Key, typename _Value,
01177        typename _Allocator, typename _ExtractKey, typename _Equal,
01178        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01179        bool __chc, bool __cit, bool __uk>
01180     void
01181     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01182            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01183     rehash(size_type __n)
01184     {
01185       _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
01186              _M_rehash_policy._M_bkt_for_elements(_M_element_count
01187                                   + 1)));
01188     }
01189 
01190   template<typename _Key, typename _Value,
01191        typename _Allocator, typename _ExtractKey, typename _Equal,
01192        typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
01193        bool __chc, bool __cit, bool __uk>
01194     void
01195     _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
01196            _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
01197     _M_rehash(size_type __n)
01198     {
01199       _Node** __new_array = _M_allocate_buckets(__n);
01200       __try
01201     {
01202       _M_begin_bucket_index = __n;
01203       for (size_type __i = 0; __i < _M_bucket_count; ++__i)
01204         while (_Node* __p = _M_buckets[__i])
01205           {
01206         std::size_t __new_index = this->_M_bucket_index(__p, __n);
01207         _M_buckets[__i] = __p->_M_next;
01208         __p->_M_next = __new_array[__new_index];
01209         __new_array[__new_index] = __p;
01210         if (__new_index < _M_begin_bucket_index)
01211           _M_begin_bucket_index = __new_index;
01212           }
01213       _M_deallocate_buckets(_M_buckets, _M_bucket_count);
01214       _M_bucket_count = __n;
01215       _M_buckets = __new_array;
01216     }
01217       __catch(...)
01218     {
01219       // A failure here means that a hash function threw an exception.
01220       // We can't restore the previous state without calling the hash
01221       // function again, so the only sensible recovery is to delete
01222       // everything.
01223       _M_deallocate_nodes(__new_array, __n);
01224       _M_deallocate_buckets(__new_array, __n);
01225       _M_deallocate_nodes(_M_buckets, _M_bucket_count);
01226       _M_element_count = 0;
01227       _M_begin_bucket_index = _M_bucket_count;
01228       __throw_exception_again;
01229     }
01230     }
01231 
01232 _GLIBCXX_END_NAMESPACE_VERSION
01233 } // namespace std
01234 
01235 #endif // _HASHTABLE_H