/home/ntakagi/work/STLport-5.1.5/stlport/stl/_unordered_map.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004
00003  * Francois Dumont
00004  *
00005  * This material is provided "as is", with absolutely no warranty expressed
00006  * or implied. Any use is at your own risk.
00007  *
00008  * Permission to use or copy this software for any purpose is hereby granted
00009  * without fee, provided the above notices are retained on all copies.
00010  * Permission to modify the code and to distribute modified code is granted,
00011  * provided the above notices are retained, and a notice that the code was
00012  * modified is included with the above copyright notice.
00013  *
00014  */
00015 
00016 /* NOTE: This is an internal header file, included by other STL headers.
00017  *   You should not attempt to use it directly.
00018  */
00019 
00020 #ifndef _STLP_INTERNAL_UNORDERED_MAP_H
00021 #define _STLP_INTERNAL_UNORDERED_MAP_H
00022 
00023 #ifndef _STLP_INTERNAL_HASHTABLE_H
00024 #  include <stl/_hashtable.h>
00025 #endif
00026 
00027 _STLP_BEGIN_NAMESPACE
00028 
00029 //Specific iterator traits creation
00030 _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMapTraitsT, traits)
00031 
00032 template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
00033           _STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Key>),
00034           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
00035 class unordered_map
00036 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00037                : public __stlport_class<unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> >
00038 #endif
00039 {
00040 private:
00041   typedef unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> _Self;
00042 public:
00043   typedef _Key key_type;
00044   typedef _Tp data_type;
00045   typedef _Tp mapped_type;
00046 #if !defined (__DMC__)
00047   typedef pair<const key_type, data_type> value_type;
00048 #else
00049   typedef pair<key_type, data_type> value_type;
00050 #endif
00051 private:
00052   //Specific iterator traits creation
00053   typedef _STLP_PRIV _UnorderedMapTraitsT<value_type> _UnorderedMapTraits;
00054 
00055 public:
00056   typedef hashtable<value_type, key_type, _HashFcn, _UnorderedMapTraits,
00057                     _STLP_SELECT1ST(value_type,  _Key), _EqualKey, _Alloc > _Ht;
00058 
00059   typedef typename _Ht::hasher hasher;
00060   typedef typename _Ht::key_equal key_equal;
00061 
00062   typedef typename _Ht::size_type size_type;
00063   typedef typename _Ht::difference_type difference_type;
00064   typedef typename _Ht::pointer pointer;
00065   typedef typename _Ht::const_pointer const_pointer;
00066   typedef typename _Ht::reference reference;
00067   typedef typename _Ht::const_reference const_reference;
00068 
00069   typedef typename _Ht::iterator iterator;
00070   typedef typename _Ht::const_iterator const_iterator;
00071   typedef typename _Ht::local_iterator local_iterator;
00072   typedef typename _Ht::const_local_iterator const_local_iterator;
00073 
00074   typedef typename _Ht::allocator_type allocator_type;
00075 
00076   hasher hash_function() const { return _M_ht.hash_funct(); }
00077   key_equal key_eq() const { return _M_ht.key_eq(); }
00078   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
00079 
00080 private:
00081   _Ht _M_ht;
00082   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00083 
00084 public:
00085   explicit unordered_map(size_type __n = 100, const hasher& __hf = hasher(),
00086                          const key_equal& __eql = key_equal(),
00087                          const allocator_type& __a = allocator_type())
00088     : _M_ht(__n, __hf, __eql, __a) {}
00089 
00090   unordered_map(__move_source<_Self> src)
00091     : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
00092 
00093 #if defined (_STLP_MEMBER_TEMPLATES)
00094   template <class _InputIterator>
00095   unordered_map(_InputIterator __f, _InputIterator __l,
00096                 size_type __n = 100, const hasher& __hf = hasher(),
00097                 const key_equal& __eql = key_equal(),
00098                 const allocator_type& __a = allocator_type())
00099     : _M_ht(__n, __hf, __eql, __a)
00100   { _M_ht.insert_unique(__f, __l); }
00101 #else
00102   unordered_map(const value_type* __f, const value_type* __l,
00103                 size_type __n = 100, const hasher& __hf = hasher(),
00104                 const key_equal& __eql = key_equal(),
00105                 const allocator_type& __a = allocator_type())
00106     : _M_ht(__n, __hf, __eql, __a)
00107   { _M_ht.insert_unique(__f, __l); }
00108 
00109   unordered_map(const_iterator __f, const_iterator __l,
00110                 size_type __n = 100, const hasher& __hf = hasher(),
00111                 const key_equal& __eql = key_equal(),
00112                 const allocator_type& __a = allocator_type())
00113     : _M_ht(__n, __hf, __eql, __a)
00114   { _M_ht.insert_unique(__f, __l); }
00115 #endif /*_STLP_MEMBER_TEMPLATES */
00116 
00117   _Self& operator = (const _Self& __other)
00118   { _M_ht = __other._M_ht; return *this; }
00119 
00120   size_type size() const { return _M_ht.size(); }
00121   size_type max_size() const { return _M_ht.max_size(); }
00122   bool empty() const { return _M_ht.empty(); }
00123   void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
00124 
00125   iterator begin() { return _M_ht.begin(); }
00126   iterator end() { return _M_ht.end(); }
00127   const_iterator begin() const { return _M_ht.begin(); }
00128   const_iterator end() const { return _M_ht.end(); }
00129 
00130   pair<iterator,bool> insert(const value_type& __obj)
00131   { return _M_ht.insert_unique(__obj); }
00132   iterator insert(const_iterator /*__hint*/, const value_type& __obj)
00133   { return _M_ht.insert_unique(__obj); }
00134 #if defined (_STLP_MEMBER_TEMPLATES)
00135   template <class _InputIterator>
00136   void insert(_InputIterator __f, _InputIterator __l)
00137 #else
00138   void insert(const value_type* __f, const value_type* __l)
00139   { _M_ht.insert_unique(__f,__l); }
00140   void insert(const_iterator __f, const_iterator __l)
00141 #endif /*_STLP_MEMBER_TEMPLATES */
00142   { _M_ht.insert_unique(__f, __l); }
00143 
00144   _STLP_TEMPLATE_FOR_CONT_EXT
00145   iterator find(const _KT& __key) { return _M_ht.find(__key); }
00146   _STLP_TEMPLATE_FOR_CONT_EXT
00147   const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
00148 
00149   _STLP_TEMPLATE_FOR_CONT_EXT
00150   _Tp& operator[](const _KT& __key) {
00151     iterator __it = _M_ht.find(__key);
00152     return (__it == _M_ht.end() ?
00153       _M_ht._M_insert(value_type(__key, _STLP_DEFAULT_CONSTRUCTED(_Tp))).second :
00154       (*__it).second );
00155   }
00156 
00157   _STLP_TEMPLATE_FOR_CONT_EXT
00158   size_type count(const _KT& __key) const { return _M_ht.count(__key); }
00159 
00160   _STLP_TEMPLATE_FOR_CONT_EXT
00161   pair<iterator, iterator> equal_range(const _KT& __key)
00162   { return _M_ht.equal_range(__key); }
00163   _STLP_TEMPLATE_FOR_CONT_EXT
00164   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
00165   { return _M_ht.equal_range(__key); }
00166 
00167   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
00168   void erase(const_iterator __it) { _M_ht.erase(__it); }
00169   void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
00170   void clear() { _M_ht.clear(); }
00171 
00172   size_type bucket_count() const { return _M_ht.bucket_count(); }
00173   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
00174   size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
00175   _STLP_TEMPLATE_FOR_CONT_EXT
00176   size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
00177   local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
00178   local_iterator end(size_type __n) { return _M_ht.end(__n); }
00179   const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
00180   const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
00181 
00182   float load_factor() const { return _M_ht.load_factor(); }
00183   float max_load_factor() const { return _M_ht.max_load_factor(); }
00184   void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
00185   void rehash(size_type __hint) { _M_ht.rehash(__hint); }
00186 };
00187 
00188 //Specific iterator traits creation
00189 _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMultimapTraitsT, traits)
00190 
00191 template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
00192           _STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Key>),
00193           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
00194 class unordered_multimap
00195 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00196                     : public __stlport_class<unordered_multimap<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> >
00197 #endif
00198 {
00199 private:
00200   typedef unordered_multimap<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> _Self;
00201 public:
00202   typedef _Key key_type;
00203   typedef _Tp data_type;
00204   typedef _Tp mapped_type;
00205 #if !defined (__DMC__)
00206   typedef pair<const key_type, data_type> value_type;
00207 #else
00208   typedef pair<key_type, data_type> value_type;
00209 #endif
00210 private:
00211   //Specific iterator traits creation
00212   typedef _STLP_PRIV _UnorderedMultimapTraitsT<value_type> _UnorderedMultimapTraits;
00213 
00214 public:
00215   typedef hashtable<value_type, key_type, _HashFcn, _UnorderedMultimapTraits,
00216                     _STLP_SELECT1ST(value_type,  _Key), _EqualKey, _Alloc > _Ht;
00217 
00218   typedef typename _Ht::hasher hasher;
00219   typedef typename _Ht::key_equal key_equal;
00220 
00221   typedef typename _Ht::size_type size_type;
00222   typedef typename _Ht::difference_type difference_type;
00223   typedef typename _Ht::pointer pointer;
00224   typedef typename _Ht::const_pointer const_pointer;
00225   typedef typename _Ht::reference reference;
00226   typedef typename _Ht::const_reference const_reference;
00227 
00228   typedef typename _Ht::iterator iterator;
00229   typedef typename _Ht::const_iterator const_iterator;
00230   typedef typename _Ht::local_iterator local_iterator;
00231   typedef typename _Ht::const_local_iterator const_local_iterator;
00232 
00233   typedef typename _Ht::allocator_type allocator_type;
00234 
00235   hasher hash_function() const { return _M_ht.hash_funct(); }
00236   key_equal key_eq() const { return _M_ht.key_eq(); }
00237   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
00238 
00239 private:
00240   _Ht _M_ht;
00241   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00242 
00243 public:
00244   explicit unordered_multimap(size_type __n = 100, const hasher& __hf = hasher(),
00245                               const key_equal& __eql = key_equal(),
00246                               const allocator_type& __a = allocator_type())
00247     : _M_ht(__n, __hf, __eql, __a) {}
00248 
00249   unordered_multimap(__move_source<_Self> src)
00250     : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
00251 
00252 #if defined (_STLP_MEMBER_TEMPLATES)
00253   template <class _InputIterator>
00254   unordered_multimap(_InputIterator __f, _InputIterator __l,
00255                      size_type __n = 100, const hasher& __hf = hasher(),
00256                      const key_equal& __eql = key_equal(),
00257                      const allocator_type& __a = allocator_type())
00258     : _M_ht(__n, __hf, __eql, __a)
00259   { _M_ht.insert_equal(__f, __l); }
00260 #else
00261   unordered_multimap(const value_type* __f, const value_type* __l,
00262                      size_type __n = 100, const hasher& __hf = hasher(),
00263                      const key_equal& __eql = key_equal(),
00264                      const allocator_type& __a = allocator_type())
00265     : _M_ht(__n, __hf, __eql, __a)
00266   { _M_ht.insert_equal(__f, __l); }
00267 
00268   unordered_multimap(const_iterator __f, const_iterator __l,
00269                      size_type __n = 100, const hasher& __hf = hasher(),
00270                      const key_equal& __eql = key_equal(),
00271                      const allocator_type& __a = allocator_type())
00272     : _M_ht(__n, __hf, __eql, __a)
00273   { _M_ht.insert_equal(__f, __l); }
00274 #endif /*_STLP_MEMBER_TEMPLATES */
00275 
00276   _Self& operator = (const _Self& __other)
00277   { _M_ht = __other._M_ht; return *this; }
00278 
00279   size_type size() const { return _M_ht.size(); }
00280   size_type max_size() const { return _M_ht.max_size(); }
00281   bool empty() const { return _M_ht.empty(); }
00282   void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
00283 
00284   iterator begin() { return _M_ht.begin(); }
00285   iterator end() { return _M_ht.end(); }
00286   const_iterator begin() const { return _M_ht.begin(); }
00287   const_iterator end() const { return _M_ht.end(); }
00288 
00289   iterator insert(const value_type& __obj)
00290   { return _M_ht.insert_equal(__obj); }
00291   iterator insert(const_iterator /*__hint*/, const value_type& __obj)
00292   { return _M_ht.insert_equal(__obj); }
00293 #if defined (_STLP_MEMBER_TEMPLATES)
00294   template <class _InputIterator>
00295   void insert(_InputIterator __f, _InputIterator __l)
00296 #else
00297   void insert(const value_type* __f, const value_type* __l)
00298   { _M_ht.insert_equal(__f,__l); }
00299   void insert(const_iterator __f, const_iterator __l)
00300 #endif /*_STLP_MEMBER_TEMPLATES */
00301   { _M_ht.insert_equal(__f, __l); }
00302 
00303   _STLP_TEMPLATE_FOR_CONT_EXT
00304   iterator find(const _KT& __key) { return _M_ht.find(__key); }
00305   _STLP_TEMPLATE_FOR_CONT_EXT
00306   const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
00307 
00308   _STLP_TEMPLATE_FOR_CONT_EXT
00309   size_type count(const _KT& __key) const { return _M_ht.count(__key); }
00310 
00311   _STLP_TEMPLATE_FOR_CONT_EXT
00312   pair<iterator, iterator> equal_range(const _KT& __key)
00313   { return _M_ht.equal_range(__key); }
00314   _STLP_TEMPLATE_FOR_CONT_EXT
00315   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
00316   { return _M_ht.equal_range(__key); }
00317 
00318   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
00319   void erase(const_iterator __it) { _M_ht.erase(__it); }
00320   void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
00321   void clear() { _M_ht.clear(); }
00322 
00323   size_type bucket_count() const { return _M_ht.bucket_count(); }
00324   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
00325   size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
00326   _STLP_TEMPLATE_FOR_CONT_EXT
00327   size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
00328   local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
00329   local_iterator end(size_type __n) { return _M_ht.end(__n); }
00330   const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
00331   const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
00332 
00333   float load_factor() const { return _M_ht.load_factor(); }
00334   float max_load_factor() const { return _M_ht.max_load_factor(); }
00335   void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
00336   void rehash(size_type __hint) { _M_ht.rehash(__hint); }
00337 };
00338 
00339 #define _STLP_TEMPLATE_HEADER template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
00340 #define _STLP_TEMPLATE_CONTAINER unordered_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>
00341 
00342 #include <stl/_relops_hash_cont.h>
00343 
00344 #undef _STLP_TEMPLATE_CONTAINER
00345 #define _STLP_TEMPLATE_CONTAINER unordered_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>
00346 
00347 #include <stl/_relops_hash_cont.h>
00348 
00349 #undef _STLP_TEMPLATE_CONTAINER
00350 #undef _STLP_TEMPLATE_HEADER
00351 
00352 // Specialization of insert_iterator so that it will work for unordered_map
00353 // and unordered_multimap.
00354 
00355 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00356 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
00357 struct __move_traits<unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > :
00358   _STLP_PRIV __move_traits_help<typename unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>::_Ht>
00359 {};
00360 
00361 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
00362 struct __move_traits<unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > :
00363   _STLP_PRIV __move_traits_help<typename unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>::_Ht>
00364 {};
00365 
00366 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
00367 class insert_iterator<unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
00368 protected:
00369   typedef unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
00370   _Container* container;
00371 public:
00372   typedef _Container          container_type;
00373   typedef output_iterator_tag iterator_category;
00374   typedef void                value_type;
00375   typedef void                difference_type;
00376   typedef void                pointer;
00377   typedef void                reference;
00378 
00379   insert_iterator(_Container& __x) : container(&__x) {}
00380   insert_iterator(_Container& __x, typename _Container::iterator)
00381     : container(&__x) {}
00382   insert_iterator<_Container>&
00383   operator=(const typename _Container::value_type& __val) {
00384     container->insert(__val);
00385     return *this;
00386   }
00387   insert_iterator<_Container>& operator*() { return *this; }
00388   insert_iterator<_Container>& operator++() { return *this; }
00389   insert_iterator<_Container>& operator++(int) { return *this; }
00390 };
00391 
00392 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
00393 class insert_iterator<unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
00394 protected:
00395   typedef unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
00396   _Container* container;
00397   typename _Container::iterator iter;
00398 public:
00399   typedef _Container          container_type;
00400   typedef output_iterator_tag iterator_category;
00401   typedef void                value_type;
00402   typedef void                difference_type;
00403   typedef void                pointer;
00404   typedef void                reference;
00405 
00406   insert_iterator(_Container& __x) : container(&__x) {}
00407   insert_iterator(_Container& __x, typename _Container::iterator)
00408     : container(&__x) {}
00409   insert_iterator<_Container>&
00410   operator=(const typename _Container::value_type& __val) {
00411     container->insert(__val);
00412     return *this;
00413   }
00414   insert_iterator<_Container>& operator*() { return *this; }
00415   insert_iterator<_Container>& operator++() { return *this; }
00416   insert_iterator<_Container>& operator++(int) { return *this; }
00417 };
00418 
00419 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
00420 
00421 _STLP_END_NAMESPACE
00422 
00423 #endif /* _STLP_INTERNAL_UNORDERED_MAP_H */
00424 
00425 // Local Variables:
00426 // mode:C++
00427 // End:



Generated on Mon Mar 10 15:32:43 2008 by  doxygen 1.5.1