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