/home/ntakagi/work/STLport-5.1.5/stlport/stl/_unordered_set.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_SET_H
00021 #define _STLP_INTERNAL_UNORDERED_SET_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(UnorderedSetTraitsT, Const_traits)
00031 
00032 template <class _Value, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Value>),
00033           _STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Value>),
00034           _STLP_DEFAULT_ALLOCATOR_SELECT(_Value) >
00035 class unordered_set
00036 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00037                : public __stlport_class<unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> >
00038 #endif
00039 {
00040   typedef unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> _Self;
00041   //Specific iterator traits creation
00042   typedef _STLP_PRIV _UnorderedSetTraitsT<_Value> _UnorderedSetTraits;
00043 public:
00044   typedef hashtable<_Value, _Value, _HashFcn,
00045                     _UnorderedSetTraits, _STLP_PRIV _Identity<_Value>, _EqualKey, _Alloc> _Ht;
00046 public:
00047   typedef typename _Ht::key_type key_type;
00048   typedef typename _Ht::value_type value_type;
00049   typedef typename _Ht::hasher hasher;
00050   typedef typename _Ht::key_equal key_equal;
00051 
00052   typedef typename _Ht::size_type size_type;
00053   typedef typename _Ht::difference_type difference_type;
00054   typedef typename _Ht::pointer         pointer;
00055   typedef typename _Ht::const_pointer   const_pointer;
00056   typedef typename _Ht::reference       reference;
00057   typedef typename _Ht::const_reference const_reference;
00058 
00059   typedef typename _Ht::iterator iterator;
00060   typedef typename _Ht::const_iterator const_iterator;
00061   typedef typename _Ht::local_iterator local_iterator;
00062   typedef typename _Ht::const_local_iterator const_local_iterator;
00063 
00064   typedef typename _Ht::allocator_type allocator_type;
00065 
00066   hasher hash_function() const { return _M_ht.hash_funct(); }
00067   key_equal key_eq() const { return _M_ht.key_eq(); }
00068   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
00069 
00070 private:
00071   _Ht _M_ht;
00072   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00073 
00074 public:
00075   explicit unordered_set(size_type __n = 100, const hasher& __hf = hasher(),
00076                          const key_equal& __eql = key_equal(),
00077                          const allocator_type& __a = allocator_type())
00078     : _M_ht(__n, __hf, __eql, __a) {}
00079 
00080   unordered_set(__move_source<_Self> src)
00081     : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
00082 
00083 #if defined (_STLP_MEMBER_TEMPLATES)
00084   template <class _InputIterator>
00085   unordered_set(_InputIterator __f, _InputIterator __l,
00086                 size_type __n = 100, const hasher& __hf = hasher(),
00087                 const key_equal& __eql = key_equal(),
00088                 const allocator_type& __a = allocator_type())
00089     : _M_ht(__n, __hf, __eql, __a)
00090   { _M_ht.insert_unique(__f, __l); }
00091 #else
00092   unordered_set(const value_type* __f, const value_type* __l,
00093                 size_type __n = 100, const hasher& __hf = hasher(),
00094                 const key_equal& __eql = key_equal(),
00095                 const allocator_type& __a = allocator_type())
00096     : _M_ht(__n, __hf, __eql, __a)
00097   { _M_ht.insert_unique(__f, __l); }
00098 
00099   unordered_set(const_iterator __f, const_iterator __l,
00100                 size_type __n = 100, const hasher& __hf = hasher(),
00101                 const key_equal& __eql = key_equal(),
00102                 const allocator_type& __a = allocator_type())
00103     : _M_ht(__n, __hf, __eql, __a)
00104   { _M_ht.insert_unique(__f, __l); }
00105 #endif /*_STLP_MEMBER_TEMPLATES */
00106 
00107   _Self& operator = (const _Self& __other)
00108   { _M_ht = __other._M_ht; return *this; }
00109 
00110   size_type size() const { return _M_ht.size(); }
00111   size_type max_size() const { return _M_ht.max_size(); }
00112   bool empty() const { return _M_ht.empty(); }
00113   void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
00114 
00115   iterator begin() { return _M_ht.begin(); }
00116   iterator end() { return _M_ht.end(); }
00117   const_iterator begin() const { return _M_ht.begin(); }
00118   const_iterator end() const { return _M_ht.end(); }
00119 
00120   pair<iterator, bool> insert(const value_type& __obj)
00121   { return _M_ht.insert_unique(__obj); }
00122   iterator insert(const_iterator /*__hint*/, const value_type& __obj)
00123   { return _M_ht.insert_unique(__obj); }
00124 #if defined (_STLP_MEMBER_TEMPLATES)
00125   template <class _InputIterator>
00126   void insert(_InputIterator __f, _InputIterator __l)
00127 #else
00128   void insert(const_iterator __f, const_iterator __l)
00129   {_M_ht.insert_unique(__f, __l); }
00130   void insert(const value_type* __f, const value_type* __l)
00131 #endif
00132   { _M_ht.insert_unique(__f,__l); }
00133 
00134   _STLP_TEMPLATE_FOR_CONT_EXT
00135   iterator find(const _KT& __key) { return _M_ht.find(__key); }
00136   _STLP_TEMPLATE_FOR_CONT_EXT
00137   const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
00138 
00139   _STLP_TEMPLATE_FOR_CONT_EXT
00140   size_type count(const _KT& __key) const { return _M_ht.count(__key); }
00141 
00142   _STLP_TEMPLATE_FOR_CONT_EXT
00143   pair<iterator, iterator> equal_range(const _KT& __key)
00144   { return _M_ht.equal_range(__key); }
00145   _STLP_TEMPLATE_FOR_CONT_EXT
00146   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
00147   { return _M_ht.equal_range(__key); }
00148 
00149   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
00150   void erase(const_iterator __it) { _M_ht.erase(__it); }
00151   void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
00152   void clear() { _M_ht.clear(); }
00153 
00154   size_type bucket_count() const { return _M_ht.bucket_count(); }
00155   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
00156   size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
00157   _STLP_TEMPLATE_FOR_CONT_EXT
00158   size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
00159   local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
00160   local_iterator end(size_type __n) { return _M_ht.end(__n); }
00161   const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
00162   const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
00163 
00164   float load_factor() const { return _M_ht.load_factor(); }
00165   float max_load_factor() const { return _M_ht.max_load_factor(); }
00166   void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
00167   void rehash(size_type __hint) { _M_ht.rehash(__hint); }
00168 };
00169 
00170 //Specific iterator traits creation
00171 _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMultisetTraitsT, Const_traits)
00172 
00173 template <class _Value, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Value>),
00174           _STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Value>),
00175           _STLP_DEFAULT_ALLOCATOR_SELECT(_Value) >
00176 class unordered_multiset
00177 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
00178                : public __stlport_class<unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> >
00179 #endif
00180 {
00181   typedef unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> _Self;
00182   //Specific iterator traits creation
00183   typedef _STLP_PRIV _UnorderedMultisetTraitsT<_Value> _UnorderedMultisetTraits;
00184 public:
00185   typedef hashtable<_Value, _Value, _HashFcn,
00186                     _UnorderedMultisetTraits, _STLP_PRIV _Identity<_Value>, _EqualKey, _Alloc> _Ht;
00187 
00188   typedef typename _Ht::key_type key_type;
00189   typedef typename _Ht::value_type value_type;
00190   typedef typename _Ht::hasher hasher;
00191   typedef typename _Ht::key_equal key_equal;
00192 
00193   typedef typename _Ht::size_type size_type;
00194   typedef typename _Ht::difference_type difference_type;
00195   typedef typename _Ht::pointer       pointer;
00196   typedef typename _Ht::const_pointer const_pointer;
00197   typedef typename _Ht::reference reference;
00198   typedef typename _Ht::const_reference const_reference;
00199 
00200   typedef typename _Ht::iterator iterator;
00201   typedef typename _Ht::const_iterator const_iterator;
00202   typedef typename _Ht::local_iterator local_iterator;
00203   typedef typename _Ht::const_local_iterator const_local_iterator;
00204 
00205   typedef typename _Ht::allocator_type allocator_type;
00206 
00207   hasher hash_function() const { return _M_ht.hash_funct(); }
00208   key_equal key_eq() const { return _M_ht.key_eq(); }
00209   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
00210 
00211 private:
00212   _Ht _M_ht;
00213   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00214 
00215 public:
00216   explicit unordered_multiset(size_type __n = 100, const hasher& __hf = hasher(),
00217                               const key_equal& __eql = key_equal(),
00218                               const allocator_type& __a = allocator_type())
00219     : _M_ht(__n, __hf, __eql, __a) {}
00220 
00221   unordered_multiset(__move_source<_Self> src)
00222     : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
00223 
00224 #if defined (_STLP_MEMBER_TEMPLATES)
00225   template <class _InputIterator>
00226   unordered_multiset(_InputIterator __f, _InputIterator __l,
00227                      size_type __n = 100, const hasher& __hf = hasher(),
00228                      const key_equal& __eql = key_equal(),
00229                      const allocator_type& __a = allocator_type())
00230     : _M_ht(__n, __hf, __eql, __a)
00231   { _M_ht.insert_equal(__f, __l); }
00232 #else
00233   unordered_multiset(const value_type* __f, const value_type* __l,
00234                      size_type __n = 100, const hasher& __hf = hasher(),
00235                      const key_equal& __eql = key_equal(),
00236                      const allocator_type& __a = allocator_type())
00237     : _M_ht(__n, __hf, __eql, __a)
00238   { _M_ht.insert_equal(__f, __l); }
00239 
00240   unordered_multiset(const_iterator __f, const_iterator __l,
00241                      size_type __n = 100, const hasher& __hf = hasher(),
00242                      const key_equal& __eql = key_equal(),
00243                      const allocator_type& __a = allocator_type())
00244     : _M_ht(__n, __hf, __eql, __a)
00245   { _M_ht.insert_equal(__f, __l); }
00246 #endif /*_STLP_MEMBER_TEMPLATES */
00247 
00248   _Self& operator = (const _Self& __other)
00249   { _M_ht = __other._M_ht; return *this; }
00250 
00251   size_type size() const { return _M_ht.size(); }
00252   size_type max_size() const { return _M_ht.max_size(); }
00253   bool empty() const { return _M_ht.empty(); }
00254   void swap(_Self& hs) { _M_ht.swap(hs._M_ht); }
00255 
00256   iterator begin() { return _M_ht.begin(); }
00257   iterator end() { return _M_ht.end(); }
00258   const_iterator begin() const { return _M_ht.begin(); }
00259   const_iterator end() const { return _M_ht.end(); }
00260 
00261   iterator insert(const value_type& __obj)
00262   { return _M_ht.insert_equal(__obj); }
00263   iterator insert(const_iterator /*__hint*/, const value_type& __obj)
00264   { return _M_ht.insert_equal(__obj); }
00265 #if defined (_STLP_MEMBER_TEMPLATES)
00266   template <class _InputIterator>
00267   void insert(_InputIterator __f, _InputIterator __l)
00268 #else
00269   void insert(const value_type* __f, const value_type* __l)
00270   { _M_ht.insert_equal(__f,__l); }
00271   void insert(const_iterator __f, const_iterator __l)
00272 #endif /*_STLP_MEMBER_TEMPLATES */
00273   { _M_ht.insert_equal(__f, __l); }
00274 
00275   _STLP_TEMPLATE_FOR_CONT_EXT
00276   iterator find(const _KT& __key) { return _M_ht.find(__key); }
00277   _STLP_TEMPLATE_FOR_CONT_EXT
00278   const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
00279 
00280   _STLP_TEMPLATE_FOR_CONT_EXT
00281   size_type count(const _KT& __key) const { return _M_ht.count(__key); }
00282 
00283   _STLP_TEMPLATE_FOR_CONT_EXT
00284   pair<iterator, iterator> equal_range(const _KT& __key)
00285   { return _M_ht.equal_range(__key); }
00286   _STLP_TEMPLATE_FOR_CONT_EXT
00287   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
00288   { return _M_ht.equal_range(__key); }
00289 
00290   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
00291   void erase(const_iterator __it) { _M_ht.erase(__it); }
00292   void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
00293   void clear() { _M_ht.clear(); }
00294 
00295   size_type bucket_count() const { return _M_ht.bucket_count(); }
00296   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
00297   size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
00298   _STLP_TEMPLATE_FOR_CONT_EXT
00299   size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
00300   local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
00301   local_iterator end(size_type __n) { return _M_ht.end(__n); }
00302   const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
00303   const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
00304 
00305   float load_factor() const { return _M_ht.load_factor(); }
00306   float max_load_factor() const { return _M_ht.max_load_factor(); }
00307   void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
00308   void rehash(size_type __hint) { _M_ht.rehash(__hint); }
00309 };
00310 
00311 #define _STLP_TEMPLATE_HEADER template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
00312 #define _STLP_TEMPLATE_CONTAINER unordered_set<_Value,_HashFcn,_EqualKey,_Alloc>
00313 
00314 #include <stl/_relops_hash_cont.h>
00315 
00316 #undef _STLP_TEMPLATE_CONTAINER
00317 #define _STLP_TEMPLATE_CONTAINER unordered_multiset<_Value,_HashFcn,_EqualKey,_Alloc>
00318 #include <stl/_relops_hash_cont.h>
00319 
00320 #undef _STLP_TEMPLATE_CONTAINER
00321 #undef _STLP_TEMPLATE_HEADER
00322 
00323 // Specialization of insert_iterator so that it will work for unordered_set
00324 // and unordered_multiset.
00325 
00326 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
00327 template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
00328 struct __move_traits<unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> > :
00329   _STLP_PRIV __move_traits_aux<typename unordered_set<_Value, _HashFcn, _EqualKey, _Alloc>::_Ht>
00330 {};
00331 
00332 template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
00333 struct __move_traits<unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> > :
00334   _STLP_PRIV __move_traits_aux<typename unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc>::_Ht>
00335 {};
00336 
00337 template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
00338 class insert_iterator<unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> > {
00339 protected:
00340   typedef unordered_set<_Value, _HashFcn, _EqualKey, _Alloc> _Container;
00341   _Container* container;
00342 public:
00343   typedef _Container          container_type;
00344   typedef output_iterator_tag iterator_category;
00345   typedef void                value_type;
00346   typedef void                difference_type;
00347   typedef void                pointer;
00348   typedef void                reference;
00349 
00350   insert_iterator(_Container& __x) : container(&__x) {}
00351   insert_iterator(_Container& __x, typename _Container::iterator)
00352     : container(&__x) {}
00353   insert_iterator<_Container>&
00354   operator=(const typename _Container::value_type& __val) {
00355     container->insert(__val);
00356     return *this;
00357   }
00358   insert_iterator<_Container>& operator*() { return *this; }
00359   insert_iterator<_Container>& operator++() { return *this; }
00360   insert_iterator<_Container>& operator++(int) { return *this; }
00361 };
00362 
00363 template <class _Value, class _HashFcn, class _EqualKey, class _Alloc>
00364 class insert_iterator<unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> > {
00365 protected:
00366   typedef unordered_multiset<_Value, _HashFcn, _EqualKey, _Alloc> _Container;
00367   _Container* container;
00368   typename _Container::iterator iter;
00369 public:
00370   typedef _Container          container_type;
00371   typedef output_iterator_tag iterator_category;
00372   typedef void                value_type;
00373   typedef void                difference_type;
00374   typedef void                pointer;
00375   typedef void                reference;
00376 
00377   insert_iterator(_Container& __x) : container(&__x) {}
00378   insert_iterator(_Container& __x, typename _Container::iterator)
00379     : container(&__x) {}
00380   insert_iterator<_Container>&
00381   operator=(const typename _Container::value_type& __val) {
00382     container->insert(__val);
00383     return *this;
00384   }
00385   insert_iterator<_Container>& operator*() { return *this; }
00386   insert_iterator<_Container>& operator++() { return *this; }
00387   insert_iterator<_Container>& operator++(int) { return *this; }
00388 };
00389 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
00390 
00391 _STLP_END_NAMESPACE
00392 
00393 #endif /* _STLP_INTERNAL_UNORDERED_SET_H */
00394 
00395 // Local Variables:
00396 // mode:C++
00397 // End:
00398 



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