/home/ntakagi/work/STLport-5.1.5/stlport/stl/debug/_hashtable.h

Go to the documentation of this file.
00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996,1997
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #ifndef _STLP_INTERNAL_DBG_HASHTABLE_H
00031 #define _STLP_INTERNAL_DBG_HASHTABLE_H
00032 
00033 // Hashtable class, used to implement the hashed associative containers
00034 // hash_set, hash_map, hash_multiset, and hash_multimap,
00035 // unordered_set, unordered_map, unordered_multiset, unordered_multimap
00036 
00037 #ifndef _STLP_DBG_ITERATOR_H
00038 #  include <stl/debug/_iterator.h>
00039 #endif
00040 
00041 _STLP_BEGIN_NAMESPACE
00042 
00043 _STLP_MOVE_TO_PRIV_NAMESPACE
00044 
00045 template <class _Key, class _Equal>
00046 class _DbgEqual {
00047 public:
00048   _DbgEqual() {}
00049   _DbgEqual(const _Equal& __eq) : _M_non_dbg_eq(__eq) {}
00050   _DbgEqual(const _DbgEqual& __eq) : _M_non_dbg_eq(__eq._M_non_dbg_eq) {}
00051 
00052 #if !defined (_STLP_USE_CONTAINERS_EXTENSION)
00053   bool operator () (const _Key& __lhs, const _Key& __rhs) const
00054 #else
00055   template <class _Kp1, class _Kp2>
00056   bool operator () (const _Kp1& __lhs, const _Kp2& __rhs) const
00057 #endif
00058       {
00059 #if !defined (_STLP_USE_CONTAINERS_EXTENSION)
00060         _STLP_VERBOSE_ASSERT(_M_non_dbg_eq(__rhs, __lhs) == _M_non_dbg_eq(__lhs, __rhs), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
00061 #endif
00062         return _M_non_dbg_eq(__lhs, __rhs) ? true : false;
00063       }
00064 
00065   _Equal non_dbg_key_eq() const { return _M_non_dbg_eq; }
00066 private:
00067   _Equal _M_non_dbg_eq;
00068 };
00069 
00070 _STLP_MOVE_TO_STD_NAMESPACE
00071 
00072 #define _STLP_NON_DBG_HT \
00073 _STLP_PRIV _STLP_NON_DBG_NAME(hashtable) <_Val, _Key, _HF, _Traits, _ExK, _STLP_PRIV _DbgEqual<_Key, _EqK>, _All>
00074 
00075 #if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
00076 template <class _Val, class _Key, class _HF,
00077           class _ExK, class _EqK, class _All>
00078 inline _Val*
00079 value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_HT >&)
00080 { return (_Val*)0; }
00081 
00082 template <class _Val, class _Key, class _HF,
00083           class _ExK, class _EqK, class _All>
00084 inline forward_iterator_tag
00085 iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_HT >&)
00086 { return forward_iterator_tag(); }
00087 #endif
00088 
00089 template <class _Val, class _Key, class _HF,
00090           class _Traits, class _ExK, class _EqK, class _All>
00091 class hashtable {
00092   typedef hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All> _Self;
00093   typedef _STLP_NON_DBG_HT _Base;
00094 
00095   typedef typename _Traits::_NonConstTraits _NonConstTraits;
00096   typedef typename _Traits::_ConstTraits _ConstTraits;
00097   typedef typename _Traits::_NonConstLocalTraits _NonConstLocalTraits;
00098   typedef typename _Traits::_ConstLocalTraits _ConstLocalTraits;
00099 
00100   _Base _M_non_dbg_impl;
00101   _STLP_PRIV __owned_list _M_iter_list;
00102 
00103 public:
00104   typedef _Key key_type;
00105   typedef _HF hasher;
00106   typedef _EqK key_equal;
00107 
00108   __IMPORT_CONTAINER_TYPEDEFS(_Base)
00109 
00110   typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_NonConstTraits> > iterator;
00111   typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_ConstTraits> >    const_iterator;
00112   //typedef _STLP_PRIV _DBG_iter<_Base, _DbgTraits<_NonConstLocalTraits> > local_iterator;
00113   typedef iterator local_iterator;
00114   //typedef _STLP_PRIV _DBG_iter<_Base, _DbgTraits<_ConstLocalTraits> >    const_local_iterator;
00115   typedef const_iterator const_local_iterator;
00116 
00117   typedef typename _Base::iterator _Base_iterator;
00118   typedef typename _Base::const_iterator _Base_const_iterator;
00119 
00120   hasher hash_funct() const { return _M_non_dbg_impl.hash_funct(); }
00121   key_equal key_eq() const { return _M_non_dbg_impl.key_eq().non_dbg_key_eq(); }
00122 
00123 private:
00124   void _Invalidate_iterator(const const_iterator& __it)
00125   { _STLP_PRIV __invalidate_iterator(&_M_iter_list, __it); }
00126   void _Invalidate_iterators(const const_iterator& __first, const const_iterator& __last)
00127   { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); }
00128 
00129   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
00130 
00131 public:
00132   allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); }
00133 
00134   hashtable(size_type __n,
00135             const _HF&  __hf,
00136             const _EqK& __eql,
00137             const _ExK& __ext,
00138             const allocator_type& __a = allocator_type())
00139     : _M_non_dbg_impl(__n, __hf, __eql, __ext, __a),
00140       _M_iter_list(&_M_non_dbg_impl) {}
00141 
00142   hashtable(size_type __n,
00143             const _HF&    __hf,
00144             const _EqK&   __eql,
00145             const allocator_type& __a = allocator_type())
00146     : _M_non_dbg_impl(__n, __hf, __eql, __a),
00147       _M_iter_list(&_M_non_dbg_impl) {}
00148 
00149   hashtable(const _Self& __ht)
00150     : _M_non_dbg_impl(__ht._M_non_dbg_impl),
00151       _M_iter_list(&_M_non_dbg_impl) {}
00152 
00153   hashtable(__move_source<_Self> src)
00154     : _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)),
00155       _M_iter_list(&_M_non_dbg_impl) {
00156 #if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
00157     src.get()._M_iter_list._Invalidate_all();
00158 #else
00159     src.get()._M_iter_list._Set_owner(_M_iter_list);
00160 #endif
00161   }
00162 
00163   size_type size() const { return _M_non_dbg_impl.size(); }
00164   size_type max_size() const { return _M_non_dbg_impl.max_size(); }
00165   bool empty() const { return _M_non_dbg_impl.empty(); }
00166 
00167   _Self& operator=(const _Self& __ht) {
00168     if (this != &__ht) {
00169       //Should not invalidate end iterator
00170       _Invalidate_iterators(begin(), end());
00171       _M_non_dbg_impl = __ht._M_non_dbg_impl;
00172     }
00173     return *this;
00174   }
00175 
00176   void swap(_Self& __ht) {
00177    _M_iter_list._Swap_owners(__ht._M_iter_list);
00178    _M_non_dbg_impl.swap(__ht._M_non_dbg_impl);
00179   }
00180 
00181   iterator begin() { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
00182   iterator end()   { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
00183   local_iterator begin(size_type __n) {
00184     //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
00185     _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
00186     return local_iterator(&_M_iter_list, _M_non_dbg_impl.begin(__n));
00187   }
00188   local_iterator end(size_type __n) {
00189     //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
00190     _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
00191     return local_iterator(&_M_iter_list, _M_non_dbg_impl.end(__n));
00192   }
00193 
00194   const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
00195   const_iterator end() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
00196   const_local_iterator begin(size_type __n) const {
00197     //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
00198     _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
00199     return const_local_iterator(&_M_iter_list, _M_non_dbg_impl.begin(__n));
00200   }
00201   const_local_iterator end(size_type __n) const {
00202     //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
00203     _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
00204     return const_local_iterator(&_M_iter_list, _M_non_dbg_impl.end(__n));
00205   }
00206 
00207   pair<iterator, bool> insert_unique(const value_type& __obj) {
00208     pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique(__obj);
00209     return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second);
00210   }
00211 
00212   iterator insert_equal(const value_type& __obj)
00213   { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal(__obj)); }
00214 
00215   pair<iterator, bool> insert_unique_noresize(const value_type& __obj) {
00216     pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique_noresize(__obj);
00217     return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second);
00218   }
00219 
00220   iterator insert_equal_noresize(const value_type& __obj)
00221   { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal_noresize(__obj)); }
00222 
00223 #if defined (_STLP_MEMBER_TEMPLATES)
00224   template <class _InputIterator>
00225   void insert_unique(_InputIterator __f, _InputIterator __l) {
00226     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
00227     _M_non_dbg_impl.insert_unique(_STLP_PRIV _Non_Dbg_iter(__f), _STLP_PRIV _Non_Dbg_iter(__l));
00228   }
00229 
00230   template <class _InputIterator>
00231   void insert_equal(_InputIterator __f, _InputIterator __l){
00232     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
00233     _M_non_dbg_impl.insert_equal(_STLP_PRIV _Non_Dbg_iter(__f), _STLP_PRIV _Non_Dbg_iter(__l));
00234   }
00235 
00236 #else
00237   void insert_unique(const value_type* __f, const value_type* __l) {
00238     _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__f, __l))
00239     _M_non_dbg_impl.insert_unique(__f, __l);
00240   }
00241 
00242   void insert_equal(const value_type* __f, const value_type* __l) {
00243     _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__f, __l))
00244     _M_non_dbg_impl.insert_equal(__f, __l);
00245   }
00246 
00247   void insert_unique(const_iterator __f, const_iterator __l) {
00248     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
00249     _M_non_dbg_impl.insert_unique(__f._M_iterator, __l._M_iterator);
00250   }
00251 
00252   void insert_equal(const_iterator __f, const_iterator __l) {
00253     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
00254     _M_non_dbg_impl.insert_equal(__f._M_iterator, __l._M_iterator);
00255   }
00256 #endif
00257 
00258   _STLP_TEMPLATE_FOR_CONT_EXT
00259   iterator find(const _KT& __key)
00260   { return iterator(&_M_iter_list, _M_non_dbg_impl.find(__key)); }
00261   _STLP_TEMPLATE_FOR_CONT_EXT
00262   const_iterator find(const _KT& __key) const
00263   { return const_iterator(&_M_iter_list, _M_non_dbg_impl.find(__key)); }
00264 
00265   _STLP_TEMPLATE_FOR_CONT_EXT
00266   size_type count(const _KT& __key) const { return _M_non_dbg_impl.count(__key); }
00267 
00268   _STLP_TEMPLATE_FOR_CONT_EXT
00269   pair<iterator, iterator> equal_range(const _KT& __key) {
00270     pair<_Base_iterator, _Base_iterator> __res = _M_non_dbg_impl.equal_range(__key);
00271     return pair<iterator,iterator> (iterator(&_M_iter_list,__res.first),
00272                                     iterator(&_M_iter_list,__res.second));
00273   }
00274 
00275   _STLP_TEMPLATE_FOR_CONT_EXT
00276   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const {
00277     pair <_Base_const_iterator, _Base_const_iterator> __res = _M_non_dbg_impl.equal_range(__key);
00278     return pair<const_iterator,const_iterator> (const_iterator(&_M_iter_list,__res.first),
00279                                                 const_iterator(&_M_iter_list,__res.second));
00280   }
00281 
00282   size_type erase(const key_type& __key) {
00283     pair<_Base_iterator, _Base_iterator> __p = _M_non_dbg_impl.equal_range(__key);
00284     size_type __n = _STLP_STD::distance(__p.first, __p.second);
00285     _Invalidate_iterators(const_iterator(&_M_iter_list, __p.first), const_iterator(&_M_iter_list, __p.second));
00286     _M_non_dbg_impl.erase(__p.first, __p.second);
00287     return __n;
00288   }
00289 
00290   void erase(const const_iterator& __it) {
00291     _STLP_DEBUG_CHECK(_STLP_PRIV _Dereferenceable(__it))
00292     _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __it))
00293     _Invalidate_iterator(__it);
00294     _M_non_dbg_impl.erase(__it._M_iterator);
00295   }
00296   void erase(const_iterator __first, const_iterator __last) {
00297     _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last,
00298                                                const_iterator(begin()), const_iterator(end())))
00299     _Invalidate_iterators(__first, __last);
00300     _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator);
00301   }
00302 
00303   void rehash(size_type __num_buckets_hint) { _M_non_dbg_impl.rehash(__num_buckets_hint); }
00304   void resize(size_type __num_elements_hint) { _M_non_dbg_impl.resize(__num_elements_hint); }
00305 
00306   void clear() {
00307     _Invalidate_iterators(begin(), end());
00308     _M_non_dbg_impl.clear();
00309   }
00310 
00311   reference _M_insert(const value_type& __obj) { return _M_non_dbg_impl._M_insert(__obj); }
00312 
00313   size_type bucket_count() const { return _M_non_dbg_impl.bucket_count(); }
00314   size_type max_bucket_count() const { return _M_non_dbg_impl.max_bucket_count(); }
00315   size_type elems_in_bucket(size_type __n) const {
00316     _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
00317     return _M_non_dbg_impl.elems_in_bucket(__n);
00318   }
00319   _STLP_TEMPLATE_FOR_CONT_EXT
00320   size_type bucket(const _KT& __k) const { return _M_non_dbg_impl.bucket(__k); }
00321 
00322   float load_factor() const { return _M_non_dbg_impl.load_factor(); }
00323   float max_load_factor() const { return _M_non_dbg_impl.max_load_factor(); }
00324   void max_load_factor(float __z) {
00325     _STLP_VERBOSE_ASSERT((__z > 0.0f), _StlMsg_INVALID_ARGUMENT)
00326     _M_non_dbg_impl.max_load_factor(__z);
00327   }
00328 };
00329 
00330 _STLP_END_NAMESPACE
00331 
00332 #undef _STLP_NON_DBG_HT
00333 
00334 #endif /* _STLP_INTERNAL_HASHTABLE_H */
00335 
00336 // Local Variables:
00337 // mode:C++
00338 // End:



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