_hashtable.h revision 9720d5f59b9c1f5d1b9ecbc9173dbcb71bd557be
1/* 2 * 3 * Copyright (c) 1994 4 * Hewlett-Packard Company 5 * 6 * Copyright (c) 1996,1997 7 * Silicon Graphics Computer Systems, Inc. 8 * 9 * Copyright (c) 1997 10 * Moscow Center for SPARC Technology 11 * 12 * Copyright (c) 1999 13 * Boris Fomitchev 14 * 15 * This material is provided "as is", with absolutely no warranty expressed 16 * or implied. Any use is at your own risk. 17 * 18 * Permission to use or copy this software for any purpose is hereby granted 19 * without fee, provided the above notices are retained on all copies. 20 * Permission to modify the code and to distribute modified code is granted, 21 * provided the above notices are retained, and a notice that the code was 22 * modified is included with the above copyright notice. 23 * 24 */ 25 26/* NOTE: This is an internal header file, included by other STL headers. 27 * You should not attempt to use it directly. 28 */ 29 30#ifndef _STLP_INTERNAL_DBG_HASHTABLE_H 31#define _STLP_INTERNAL_DBG_HASHTABLE_H 32 33// Hashtable class, used to implement the hashed associative containers 34// hash_set, hash_map, hash_multiset, and hash_multimap, 35// unordered_set, unordered_map, unordered_multiset, unordered_multimap 36 37#ifndef _STLP_DBG_ITERATOR_H 38# include <stl/debug/_iterator.h> 39#endif 40 41_STLP_BEGIN_NAMESPACE 42 43_STLP_MOVE_TO_PRIV_NAMESPACE 44 45template <class _Key, class _Equal> 46class _DbgEqual { 47public: 48 _DbgEqual() {} 49 _DbgEqual(const _Equal& __eq) : _M_non_dbg_eq(__eq) {} 50 _DbgEqual(const _DbgEqual& __eq) : _M_non_dbg_eq(__eq._M_non_dbg_eq) {} 51 52#if !defined (_STLP_USE_CONTAINERS_EXTENSION) 53 bool operator () (const _Key& __lhs, const _Key& __rhs) const 54#else 55 template <class _Kp1, class _Kp2> 56 bool operator () (const _Kp1& __lhs, const _Kp2& __rhs) const 57#endif 58 { 59#if !defined (_STLP_USE_CONTAINERS_EXTENSION) 60 _STLP_VERBOSE_ASSERT(_M_non_dbg_eq(__rhs, __lhs) == _M_non_dbg_eq(__lhs, __rhs), _StlMsg_INVALID_EQUIVALENT_PREDICATE) 61#endif 62 return _M_non_dbg_eq(__lhs, __rhs) ? true : false; 63 } 64 65 _Equal non_dbg_key_eq() const { return _M_non_dbg_eq; } 66private: 67 _Equal _M_non_dbg_eq; 68}; 69 70_STLP_MOVE_TO_STD_NAMESPACE 71 72#define _STLP_NON_DBG_HT \ 73_STLP_PRIV _STLP_NON_DBG_NAME(hashtable) <_Val, _Key, _HF, _Traits, _ExK, _STLP_PRIV _DbgEqual<_Key, _EqK>, _All> 74 75#if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS) 76template <class _Val, class _Key, class _HF, 77 class _ExK, class _EqK, class _All> 78inline _Val* 79value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_HT >&) 80{ return (_Val*)0; } 81 82template <class _Val, class _Key, class _HF, 83 class _ExK, class _EqK, class _All> 84inline forward_iterator_tag 85iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_HT >&) 86{ return forward_iterator_tag(); } 87#endif 88 89template <class _Val, class _Key, class _HF, 90 class _Traits, class _ExK, class _EqK, class _All> 91class hashtable { 92 typedef hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All> _Self; 93 typedef _STLP_NON_DBG_HT _Base; 94 95 typedef typename _Traits::_NonConstTraits _NonConstTraits; 96 typedef typename _Traits::_ConstTraits _ConstTraits; 97 typedef typename _Traits::_NonConstLocalTraits _NonConstLocalTraits; 98 typedef typename _Traits::_ConstLocalTraits _ConstLocalTraits; 99 100 _Base _M_non_dbg_impl; 101 _STLP_PRIV __owned_list _M_iter_list; 102 103public: 104 typedef _Key key_type; 105 typedef _HF hasher; 106 typedef _EqK key_equal; 107 108 __IMPORT_CONTAINER_TYPEDEFS(_Base) 109 110 typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_NonConstTraits> > iterator; 111 typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_ConstTraits> > const_iterator; 112 //typedef _STLP_PRIV _DBG_iter<_Base, _DbgTraits<_NonConstLocalTraits> > local_iterator; 113 typedef iterator local_iterator; 114 //typedef _STLP_PRIV _DBG_iter<_Base, _DbgTraits<_ConstLocalTraits> > const_local_iterator; 115 typedef const_iterator const_local_iterator; 116 117 typedef typename _Base::iterator _Base_iterator; 118 typedef typename _Base::const_iterator _Base_const_iterator; 119 120 hasher hash_funct() const { return _M_non_dbg_impl.hash_funct(); } 121 key_equal key_eq() const { return _M_non_dbg_impl.key_eq().non_dbg_key_eq(); } 122 123private: 124 void _Invalidate_iterator(const const_iterator& __it) 125 { _STLP_PRIV __invalidate_iterator(&_M_iter_list, __it); } 126 void _Invalidate_iterators(const const_iterator& __first, const const_iterator& __last) 127 { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); } 128 129 _STLP_KEY_TYPE_FOR_CONT_EXT(key_type) 130 131public: 132 allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); } 133 134 hashtable(size_type __n, 135 const _HF& __hf, 136 const _EqK& __eql, 137 const _ExK& __ext, 138 const allocator_type& __a = allocator_type()) 139 : _M_non_dbg_impl(__n, __hf, __eql, __ext, __a), 140 _M_iter_list(&_M_non_dbg_impl) {} 141 142 hashtable(size_type __n, 143 const _HF& __hf, 144 const _EqK& __eql, 145 const allocator_type& __a = allocator_type()) 146 : _M_non_dbg_impl(__n, __hf, __eql, __a), 147 _M_iter_list(&_M_non_dbg_impl) {} 148 149 hashtable(const _Self& __ht) 150 : _M_non_dbg_impl(__ht._M_non_dbg_impl), 151 _M_iter_list(&_M_non_dbg_impl) {} 152 153 hashtable(__move_source<_Self> src) 154 : _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)), 155 _M_iter_list(&_M_non_dbg_impl) { 156#if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL) 157 src.get()._M_iter_list._Invalidate_all(); 158#else 159 src.get()._M_iter_list._Set_owner(_M_iter_list); 160#endif 161 } 162 163 size_type size() const { return _M_non_dbg_impl.size(); } 164 size_type max_size() const { return _M_non_dbg_impl.max_size(); } 165 bool empty() const { return _M_non_dbg_impl.empty(); } 166 167 _Self& operator=(const _Self& __ht) { 168 if (this != &__ht) { 169 //Should not invalidate end iterator 170 _Invalidate_iterators(begin(), end()); 171 _M_non_dbg_impl = __ht._M_non_dbg_impl; 172 } 173 return *this; 174 } 175 176 void swap(_Self& __ht) { 177 _M_iter_list._Swap_owners(__ht._M_iter_list); 178 _M_non_dbg_impl.swap(__ht._M_non_dbg_impl); 179 } 180 181 iterator begin() { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); } 182 iterator end() { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); } 183 local_iterator begin(size_type __n) { 184 //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators 185 _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT) 186 return local_iterator(&_M_iter_list, _M_non_dbg_impl.begin(__n)); 187 } 188 local_iterator end(size_type __n) { 189 //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators 190 _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT) 191 return local_iterator(&_M_iter_list, _M_non_dbg_impl.end(__n)); 192 } 193 194 const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); } 195 const_iterator end() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); } 196 const_local_iterator begin(size_type __n) const { 197 //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators 198 _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT) 199 return const_local_iterator(&_M_iter_list, _M_non_dbg_impl.begin(__n)); 200 } 201 const_local_iterator end(size_type __n) const { 202 //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators 203 _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT) 204 return const_local_iterator(&_M_iter_list, _M_non_dbg_impl.end(__n)); 205 } 206 207 pair<iterator, bool> insert_unique(const value_type& __obj) { 208 pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique(__obj); 209 return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second); 210 } 211 212 iterator insert_equal(const value_type& __obj) 213 { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal(__obj)); } 214 215 pair<iterator, bool> insert_unique_noresize(const value_type& __obj) { 216 pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique_noresize(__obj); 217 return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second); 218 } 219 220 iterator insert_equal_noresize(const value_type& __obj) 221 { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal_noresize(__obj)); } 222 223#if defined (_STLP_MEMBER_TEMPLATES) 224 template <class _InputIterator> 225 void insert_unique(_InputIterator __f, _InputIterator __l) { 226 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l)) 227 _M_non_dbg_impl.insert_unique(_STLP_PRIV _Non_Dbg_iter(__f), _STLP_PRIV _Non_Dbg_iter(__l)); 228 } 229 230 template <class _InputIterator> 231 void insert_equal(_InputIterator __f, _InputIterator __l){ 232 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l)) 233 _M_non_dbg_impl.insert_equal(_STLP_PRIV _Non_Dbg_iter(__f), _STLP_PRIV _Non_Dbg_iter(__l)); 234 } 235 236#else 237 void insert_unique(const value_type* __f, const value_type* __l) { 238 _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__f, __l)) 239 _M_non_dbg_impl.insert_unique(__f, __l); 240 } 241 242 void insert_equal(const value_type* __f, const value_type* __l) { 243 _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__f, __l)) 244 _M_non_dbg_impl.insert_equal(__f, __l); 245 } 246 247 void insert_unique(const_iterator __f, const_iterator __l) { 248 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l)) 249 _M_non_dbg_impl.insert_unique(__f._M_iterator, __l._M_iterator); 250 } 251 252 void insert_equal(const_iterator __f, const_iterator __l) { 253 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l)) 254 _M_non_dbg_impl.insert_equal(__f._M_iterator, __l._M_iterator); 255 } 256#endif 257 258 _STLP_TEMPLATE_FOR_CONT_EXT 259 iterator find(const _KT& __key) 260 { return iterator(&_M_iter_list, _M_non_dbg_impl.find(__key)); } 261 _STLP_TEMPLATE_FOR_CONT_EXT 262 const_iterator find(const _KT& __key) const 263 { return const_iterator(&_M_iter_list, _M_non_dbg_impl.find(__key)); } 264 265 _STLP_TEMPLATE_FOR_CONT_EXT 266 size_type count(const _KT& __key) const { return _M_non_dbg_impl.count(__key); } 267 268 _STLP_TEMPLATE_FOR_CONT_EXT 269 pair<iterator, iterator> equal_range(const _KT& __key) { 270 pair<_Base_iterator, _Base_iterator> __res = _M_non_dbg_impl.equal_range(__key); 271 return pair<iterator,iterator> (iterator(&_M_iter_list,__res.first), 272 iterator(&_M_iter_list,__res.second)); 273 } 274 275 _STLP_TEMPLATE_FOR_CONT_EXT 276 pair<const_iterator, const_iterator> equal_range(const _KT& __key) const { 277 pair <_Base_const_iterator, _Base_const_iterator> __res = _M_non_dbg_impl.equal_range(__key); 278 return pair<const_iterator,const_iterator> (const_iterator(&_M_iter_list,__res.first), 279 const_iterator(&_M_iter_list,__res.second)); 280 } 281 282 size_type erase(const key_type& __key) { 283 pair<_Base_iterator, _Base_iterator> __p = _M_non_dbg_impl.equal_range(__key); 284 size_type __n = _STLP_STD::distance(__p.first, __p.second); 285 _Invalidate_iterators(const_iterator(&_M_iter_list, __p.first), const_iterator(&_M_iter_list, __p.second)); 286 _M_non_dbg_impl.erase(__p.first, __p.second); 287 return __n; 288 } 289 290 void erase(const const_iterator& __it) { 291 _STLP_DEBUG_CHECK(_STLP_PRIV _Dereferenceable(__it)) 292 _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __it)) 293 _Invalidate_iterator(__it); 294 _M_non_dbg_impl.erase(__it._M_iterator); 295 } 296 void erase(const_iterator __first, const_iterator __last) { 297 _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last, 298 const_iterator(begin()), const_iterator(end()))) 299 _Invalidate_iterators(__first, __last); 300 _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator); 301 } 302 303 void rehash(size_type __num_buckets_hint) { _M_non_dbg_impl.rehash(__num_buckets_hint); } 304 void resize(size_type __num_elements_hint) { _M_non_dbg_impl.resize(__num_elements_hint); } 305 306 void clear() { 307 _Invalidate_iterators(begin(), end()); 308 _M_non_dbg_impl.clear(); 309 } 310 311 reference _M_insert(const value_type& __obj) { return _M_non_dbg_impl._M_insert(__obj); } 312 313 size_type bucket_count() const { return _M_non_dbg_impl.bucket_count(); } 314 size_type max_bucket_count() const { return _M_non_dbg_impl.max_bucket_count(); } 315 size_type elems_in_bucket(size_type __n) const { 316 _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT) 317 return _M_non_dbg_impl.elems_in_bucket(__n); 318 } 319 _STLP_TEMPLATE_FOR_CONT_EXT 320 size_type bucket(const _KT& __k) const { return _M_non_dbg_impl.bucket(__k); } 321 322 float load_factor() const { return _M_non_dbg_impl.load_factor(); } 323 float max_load_factor() const { return _M_non_dbg_impl.max_load_factor(); } 324 void max_load_factor(float __z) { 325 _STLP_VERBOSE_ASSERT((__z > 0.0f), _StlMsg_INVALID_ARGUMENT) 326 _M_non_dbg_impl.max_load_factor(__z); 327 } 328}; 329 330_STLP_END_NAMESPACE 331 332#undef _STLP_NON_DBG_HT 333 334#endif /* _STLP_INTERNAL_HASHTABLE_H */ 335 336// Local Variables: 337// mode:C++ 338// End: 339