unordered_map.h

Go to the documentation of this file.
00001 // unordered_map implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2010 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/unordered_map.h
00026  *  This is an internal header file, included by other library headers.
00027  *  You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _UNORDERED_MAP_H
00031 #define _UNORDERED_MAP_H
00032 
00033 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
00034 
00035   // XXX When we get typedef templates these class definitions
00036   // will be unnecessary.
00037   template<class _Key, class _Tp,
00038        class _Hash = hash<_Key>,
00039        class _Pred = std::equal_to<_Key>,
00040        class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
00041        bool __cache_hash_code = false>
00042     class __unordered_map
00043     : public _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
00044             std::_Select1st<std::pair<const _Key, _Tp> >, _Pred, 
00045             _Hash, __detail::_Mod_range_hashing,
00046             __detail::_Default_ranged_hash,
00047             __detail::_Prime_rehash_policy,
00048             __cache_hash_code, false, true>
00049     {
00050       typedef _Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc,
00051              std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
00052              _Hash, __detail::_Mod_range_hashing,
00053              __detail::_Default_ranged_hash,
00054              __detail::_Prime_rehash_policy,
00055              __cache_hash_code, false, true>
00056         _Base;
00057 
00058     public:
00059       typedef typename _Base::size_type       size_type;
00060       typedef typename _Base::hasher          hasher;
00061       typedef typename _Base::key_equal       key_equal;
00062       typedef typename _Base::allocator_type  allocator_type;
00063 
00064       explicit
00065       __unordered_map(size_type __n = 10,
00066               const hasher& __hf = hasher(),
00067               const key_equal& __eql = key_equal(),
00068               const allocator_type& __a = allocator_type())
00069       : _Base(__n, __hf, __detail::_Mod_range_hashing(),
00070           __detail::_Default_ranged_hash(),
00071           __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
00072       { }
00073 
00074       template<typename _InputIterator>
00075         __unordered_map(_InputIterator __f, _InputIterator __l, 
00076             size_type __n = 10,
00077             const hasher& __hf = hasher(), 
00078             const key_equal& __eql = key_equal(), 
00079             const allocator_type& __a = allocator_type())
00080     : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
00081         __detail::_Default_ranged_hash(),
00082         __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
00083     { }
00084 
00085       __unordered_map(const __unordered_map& __x) = default;
00086 
00087       __unordered_map(__unordered_map&& __x)
00088       : _Base(std::forward<_Base>(__x)) { }
00089     };
00090   
00091   template<class _Key, class _Tp,
00092        class _Hash = hash<_Key>,
00093        class _Pred = std::equal_to<_Key>,
00094        class _Alloc = std::allocator<std::pair<const _Key, _Tp> >,
00095        bool __cache_hash_code = false>
00096     class __unordered_multimap
00097     : public _Hashtable<_Key, std::pair<const _Key, _Tp>,
00098             _Alloc,
00099             std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
00100             _Hash, __detail::_Mod_range_hashing,
00101             __detail::_Default_ranged_hash,
00102             __detail::_Prime_rehash_policy,
00103             __cache_hash_code, false, false>
00104     {
00105       typedef _Hashtable<_Key, std::pair<const _Key, _Tp>,
00106              _Alloc,
00107              std::_Select1st<std::pair<const _Key, _Tp> >, _Pred,
00108              _Hash, __detail::_Mod_range_hashing,
00109              __detail::_Default_ranged_hash,
00110              __detail::_Prime_rehash_policy,
00111              __cache_hash_code, false, false>
00112         _Base;
00113 
00114     public:
00115       typedef typename _Base::size_type       size_type;
00116       typedef typename _Base::hasher          hasher;
00117       typedef typename _Base::key_equal       key_equal;
00118       typedef typename _Base::allocator_type  allocator_type;
00119       
00120       explicit
00121       __unordered_multimap(size_type __n = 10,
00122                const hasher& __hf = hasher(),
00123                const key_equal& __eql = key_equal(),
00124                const allocator_type& __a = allocator_type())
00125       : _Base(__n, __hf, __detail::_Mod_range_hashing(),
00126           __detail::_Default_ranged_hash(),
00127           __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
00128       { }
00129 
00130 
00131       template<typename _InputIterator>
00132         __unordered_multimap(_InputIterator __f, _InputIterator __l, 
00133                  typename _Base::size_type __n = 0,
00134                  const hasher& __hf = hasher(), 
00135                  const key_equal& __eql = key_equal(), 
00136                  const allocator_type& __a = allocator_type())
00137     : _Base(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
00138         __detail::_Default_ranged_hash(),
00139         __eql, std::_Select1st<std::pair<const _Key, _Tp> >(), __a)
00140         { }
00141 
00142       __unordered_multimap(const __unordered_multimap& __x) = default;
00143 
00144       __unordered_multimap(__unordered_multimap&& __x)
00145       : _Base(std::forward<_Base>(__x)) { }
00146     };
00147 
00148   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
00149        bool __cache_hash_code>
00150     inline void
00151     swap(__unordered_map<_Key, _Tp, _Hash, _Pred,
00152      _Alloc, __cache_hash_code>& __x,
00153      __unordered_map<_Key, _Tp, _Hash, _Pred,
00154      _Alloc, __cache_hash_code>& __y)
00155     { __x.swap(__y); }
00156 
00157   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
00158        bool __cache_hash_code>
00159     inline void
00160     swap(__unordered_multimap<_Key, _Tp, _Hash, _Pred,
00161      _Alloc, __cache_hash_code>& __x,
00162      __unordered_multimap<_Key, _Tp, _Hash, _Pred,
00163      _Alloc, __cache_hash_code>& __y)
00164     { __x.swap(__y); }
00165 
00166   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
00167        bool __cache_hash_code>
00168     inline bool
00169     operator==(const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
00170            __cache_hash_code>& __x,
00171            const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
00172            __cache_hash_code>& __y)
00173     { return __x._M_equal(__y); }
00174 
00175   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
00176        bool __cache_hash_code>
00177     inline bool
00178     operator!=(const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
00179            __cache_hash_code>& __x,
00180            const __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc,
00181            __cache_hash_code>& __y)
00182     { return !(__x == __y); }
00183 
00184   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
00185        bool __cache_hash_code>
00186     inline bool
00187     operator==(const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
00188            __cache_hash_code>& __x,
00189            const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
00190            __cache_hash_code>& __y)
00191     { return __x._M_equal(__y); }
00192 
00193   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc,
00194        bool __cache_hash_code>
00195     inline bool
00196     operator!=(const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
00197            __cache_hash_code>& __x,
00198            const __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc,
00199            __cache_hash_code>& __y)
00200     { return !(__x == __y); }
00201 
00202   /**
00203    *  @brief A standard container composed of unique keys (containing
00204    *  at most one of each key value) that associates values of another type
00205    *  with the keys.
00206    *
00207    *  @ingroup unordered_associative_containers
00208    *
00209    *  Meets the requirements of a <a href="tables.html#65">container</a>, and
00210    *  <a href="tables.html#xx">unordered associative container</a>
00211    *
00212    *  @param  Key  Type of key objects.
00213    *  @param  Tp  Type of mapped objects.
00214    *  @param  Hash  Hashing function object type, defaults to hash<Value>.
00215    *  @param  Pred  Predicate function object type, defaults to equal_to<Value>.
00216    *  @param  Alloc  Allocator type, defaults to allocator<Key>.
00217    *
00218    * The resulting value type of the container is std::pair<const Key, Tp>.
00219    */
00220   template<class _Key, class _Tp,
00221        class _Hash = hash<_Key>,
00222        class _Pred = std::equal_to<_Key>,
00223        class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
00224     class unordered_map
00225     : public __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>
00226     {
00227       typedef __unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>  _Base;
00228 
00229     public:
00230       typedef typename _Base::value_type      value_type;
00231       typedef typename _Base::size_type       size_type;
00232       typedef typename _Base::hasher          hasher;
00233       typedef typename _Base::key_equal       key_equal;
00234       typedef typename _Base::allocator_type  allocator_type;
00235 
00236       explicit
00237       unordered_map(size_type __n = 10,
00238             const hasher& __hf = hasher(),
00239             const key_equal& __eql = key_equal(),
00240             const allocator_type& __a = allocator_type())
00241       : _Base(__n, __hf, __eql, __a)
00242       { }
00243 
00244       template<typename _InputIterator>
00245         unordered_map(_InputIterator __f, _InputIterator __l, 
00246               size_type __n = 10,
00247               const hasher& __hf = hasher(), 
00248               const key_equal& __eql = key_equal(), 
00249               const allocator_type& __a = allocator_type())
00250     : _Base(__f, __l, __n, __hf, __eql, __a)
00251         { }
00252 
00253       unordered_map(const unordered_map& __x) = default;
00254 
00255       unordered_map(unordered_map&& __x)
00256       : _Base(std::forward<_Base>(__x)) { }
00257 
00258       unordered_map(initializer_list<value_type> __l,
00259             size_type __n = 10,
00260             const hasher& __hf = hasher(),
00261             const key_equal& __eql = key_equal(),
00262             const allocator_type& __a = allocator_type())
00263     : _Base(__l.begin(), __l.end(), __n, __hf, __eql, __a)
00264       { }
00265 
00266       unordered_map&
00267       operator=(const unordered_map& __x) = default;
00268 
00269       unordered_map&
00270       operator=(unordered_map&& __x)
00271       {
00272     // NB: DR 1204.
00273     // NB: DR 675.
00274     this->clear();
00275     this->swap(__x);
00276     return *this;   
00277       }
00278 
00279       unordered_map&
00280       operator=(initializer_list<value_type> __l)
00281       {
00282     this->clear();
00283     this->insert(__l.begin(), __l.end());
00284     return *this;
00285       }
00286     };
00287   
00288   /**
00289    *  @brief A standard container composed of equivalent keys
00290    *  (possibly containing multiple of each key value) that associates
00291    *  values of another type with the keys.
00292    *
00293    *  @ingroup unordered_associative_containers
00294    *
00295    *  Meets the requirements of a <a href="tables.html#65">container</a>, and
00296    *  <a href="tables.html#xx">unordered associative container</a>
00297    *
00298    *  @param  Key  Type of key objects.
00299    *  @param  Tp  Type of mapped objects.
00300    *  @param  Hash  Hashing function object type, defaults to hash<Value>.
00301    *  @param  Pred  Predicate function object type, defaults to equal_to<Value>.
00302    *  @param  Alloc  Allocator type, defaults to allocator<Key>.
00303    *
00304    * The resulting value type of the container is std::pair<const Key, Tp>.
00305    */
00306   template<class _Key, class _Tp,
00307        class _Hash = hash<_Key>,
00308        class _Pred = std::equal_to<_Key>,
00309        class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
00310     class unordered_multimap
00311     : public __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>
00312     {
00313       typedef __unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>  _Base;
00314 
00315     public:
00316       typedef typename _Base::value_type      value_type;
00317       typedef typename _Base::size_type       size_type;
00318       typedef typename _Base::hasher          hasher;
00319       typedef typename _Base::key_equal       key_equal;
00320       typedef typename _Base::allocator_type  allocator_type;
00321       
00322       explicit
00323       unordered_multimap(size_type __n = 10,
00324              const hasher& __hf = hasher(),
00325              const key_equal& __eql = key_equal(),
00326              const allocator_type& __a = allocator_type())
00327       : _Base(__n, __hf, __eql, __a)
00328       { }
00329 
00330 
00331       template<typename _InputIterator>
00332         unordered_multimap(_InputIterator __f, _InputIterator __l, 
00333                typename _Base::size_type __n = 0,
00334                const hasher& __hf = hasher(), 
00335                const key_equal& __eql = key_equal(), 
00336                const allocator_type& __a = allocator_type())
00337     : _Base(__f, __l, __n, __hf, __eql, __a)
00338         { }
00339 
00340       unordered_multimap(const unordered_multimap& __x) = default;
00341 
00342       unordered_multimap(unordered_multimap&& __x)
00343       : _Base(std::forward<_Base>(__x)) { }
00344 
00345       unordered_multimap(initializer_list<value_type> __l,
00346              size_type __n = 10,
00347              const hasher& __hf = hasher(),
00348              const key_equal& __eql = key_equal(),
00349              const allocator_type& __a = allocator_type())
00350     : _Base(__l.begin(), __l.end(), __n, __hf, __eql, __a)
00351       { }
00352 
00353       unordered_multimap&
00354       operator=(const unordered_multimap& __x) = default;
00355 
00356       unordered_multimap&
00357       operator=(unordered_multimap&& __x)
00358       {
00359     // NB: DR 1204.
00360     // NB: DR 675.
00361     this->clear();
00362     this->swap(__x);
00363     return *this;   
00364       }
00365 
00366       unordered_multimap&
00367       operator=(initializer_list<value_type> __l)
00368       {
00369     this->clear();
00370     this->insert(__l.begin(), __l.end());
00371     return *this;
00372       }
00373     };
00374 
00375   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
00376     inline void
00377     swap(unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00378      unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00379     { __x.swap(__y); }
00380 
00381   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
00382     inline void
00383     swap(unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00384      unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00385     { __x.swap(__y); }
00386 
00387   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
00388     inline bool
00389     operator==(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00390            const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00391     { return __x._M_equal(__y); }
00392 
00393   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
00394     inline bool
00395     operator!=(const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00396            const unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00397     { return !(__x == __y); }
00398 
00399   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
00400     inline bool
00401     operator==(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00402            const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00403     { return __x._M_equal(__y); }
00404 
00405   template<class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
00406     inline bool
00407     operator!=(const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
00408            const unordered_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
00409     { return !(__x == __y); }
00410 
00411 _GLIBCXX_END_NESTED_NAMESPACE
00412 
00413 #endif /* _UNORDERED_MAP_H */