_hashtable.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_DBG_HASHTABLE_H
  29. #define _STLP_INTERNAL_DBG_HASHTABLE_H
  30. // Hashtable class, used to implement the hashed associative containers
  31. // hash_set, hash_map, hash_multiset, and hash_multimap,
  32. // unordered_set, unordered_map, unordered_multiset, unordered_multimap
  33. #ifndef _STLP_DBG_ITERATOR_H
  34. # include <stl/debug/_iterator.h>
  35. #endif
  36. _STLP_BEGIN_NAMESPACE
  37. _STLP_MOVE_TO_PRIV_NAMESPACE
  38. template <class _Key, class _Equal>
  39. class _DbgEqual {
  40. public:
  41. _DbgEqual() {}
  42. _DbgEqual(const _Equal& __eq) : _M_non_dbg_eq(__eq) {}
  43. _DbgEqual(const _DbgEqual& __eq) : _M_non_dbg_eq(__eq._M_non_dbg_eq) {}
  44. #if !defined (_STLP_USE_CONTAINERS_EXTENSION)
  45. bool operator () (const _Key& __lhs, const _Key& __rhs) const
  46. #else
  47. template <class _Kp1, class _Kp2>
  48. bool operator () (const _Kp1& __lhs, const _Kp2& __rhs) const
  49. #endif
  50. {
  51. #if !defined (_STLP_USE_CONTAINERS_EXTENSION)
  52. _STLP_VERBOSE_ASSERT(_M_non_dbg_eq(__rhs, __lhs) == _M_non_dbg_eq(__lhs, __rhs), _StlMsg_INVALID_EQUIVALENT_PREDICATE)
  53. #endif
  54. return _M_non_dbg_eq(__lhs, __rhs) ? true : false;
  55. }
  56. _Equal non_dbg_key_eq() const { return _M_non_dbg_eq; }
  57. private:
  58. _Equal _M_non_dbg_eq;
  59. };
  60. _STLP_MOVE_TO_STD_NAMESPACE
  61. #define _STLP_NON_DBG_HT \
  62. _STLP_PRIV _STLP_NON_DBG_NAME(hashtable) <_Val, _Key, _HF, _Traits, _ExK, _STLP_PRIV _DbgEqual<_Key, _EqK>, _All>
  63. #if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
  64. template <class _Val, class _Key, class _HF,
  65. class _ExK, class _EqK, class _All>
  66. inline _Val*
  67. value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_HT >&)
  68. { return (_Val*)0; }
  69. template <class _Val, class _Key, class _HF,
  70. class _ExK, class _EqK, class _All>
  71. inline forward_iterator_tag
  72. iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_HT >&)
  73. { return forward_iterator_tag(); }
  74. #endif
  75. template <class _Val, class _Key, class _HF,
  76. class _Traits, class _ExK, class _EqK, class _All>
  77. class hashtable {
  78. typedef hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All> _Self;
  79. typedef _STLP_NON_DBG_HT _Base;
  80. typedef typename _Traits::_NonConstTraits _NonConstTraits;
  81. typedef typename _Traits::_ConstTraits _ConstTraits;
  82. typedef typename _Traits::_NonConstLocalTraits _NonConstLocalTraits;
  83. typedef typename _Traits::_ConstLocalTraits _ConstLocalTraits;
  84. _Base _M_non_dbg_impl;
  85. _STLP_PRIV __owned_list _M_iter_list;
  86. public:
  87. typedef _Key key_type;
  88. typedef _HF hasher;
  89. typedef _EqK key_equal;
  90. __IMPORT_CONTAINER_TYPEDEFS(_Base)
  91. typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_NonConstTraits> > iterator;
  92. typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_ConstTraits> > const_iterator;
  93. //typedef _STLP_PRIV _DBG_iter<_Base, _DbgTraits<_NonConstLocalTraits> > local_iterator;
  94. typedef iterator local_iterator;
  95. //typedef _STLP_PRIV _DBG_iter<_Base, _DbgTraits<_ConstLocalTraits> > const_local_iterator;
  96. typedef const_iterator const_local_iterator;
  97. typedef typename _Base::iterator _Base_iterator;
  98. typedef typename _Base::const_iterator _Base_const_iterator;
  99. hasher hash_funct() const { return _M_non_dbg_impl.hash_funct(); }
  100. key_equal key_eq() const { return _M_non_dbg_impl.key_eq().non_dbg_key_eq(); }
  101. private:
  102. void _Invalidate_iterator(const const_iterator& __it)
  103. { _STLP_PRIV __invalidate_iterator(&_M_iter_list, __it); }
  104. void _Invalidate_iterators(const const_iterator& __first, const const_iterator& __last)
  105. { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); }
  106. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  107. public:
  108. allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); }
  109. hashtable(size_type __n,
  110. const _HF& __hf,
  111. const _EqK& __eql,
  112. const _ExK& __ext,
  113. const allocator_type& __a = allocator_type())
  114. : _M_non_dbg_impl(__n, __hf, __eql, __ext, __a),
  115. _M_iter_list(&_M_non_dbg_impl) {}
  116. hashtable(size_type __n,
  117. const _HF& __hf,
  118. const _EqK& __eql,
  119. const allocator_type& __a = allocator_type())
  120. : _M_non_dbg_impl(__n, __hf, __eql, __a),
  121. _M_iter_list(&_M_non_dbg_impl) {}
  122. hashtable(const _Self& __ht)
  123. : _M_non_dbg_impl(__ht._M_non_dbg_impl),
  124. _M_iter_list(&_M_non_dbg_impl) {}
  125. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  126. hashtable(__move_source<_Self> src)
  127. : _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)),
  128. _M_iter_list(&_M_non_dbg_impl) {
  129. # if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
  130. src.get()._M_iter_list._Invalidate_all();
  131. # else
  132. src.get()._M_iter_list._Set_owner(_M_iter_list);
  133. # endif
  134. }
  135. #endif
  136. size_type size() const { return _M_non_dbg_impl.size(); }
  137. size_type max_size() const { return _M_non_dbg_impl.max_size(); }
  138. bool empty() const { return _M_non_dbg_impl.empty(); }
  139. _Self& operator=(const _Self& __ht) {
  140. if (this != &__ht) {
  141. //Should not invalidate end iterator
  142. _Invalidate_iterators(begin(), end());
  143. _M_non_dbg_impl = __ht._M_non_dbg_impl;
  144. }
  145. return *this;
  146. }
  147. void swap(_Self& __ht) {
  148. _M_iter_list._Swap_owners(__ht._M_iter_list);
  149. _M_non_dbg_impl.swap(__ht._M_non_dbg_impl);
  150. }
  151. iterator begin() { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
  152. iterator end() { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
  153. local_iterator begin(size_type __n) {
  154. //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
  155. _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
  156. return local_iterator(&_M_iter_list, _M_non_dbg_impl.begin(__n));
  157. }
  158. local_iterator end(size_type __n) {
  159. //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
  160. _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
  161. return local_iterator(&_M_iter_list, _M_non_dbg_impl.end(__n));
  162. }
  163. const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
  164. const_iterator end() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
  165. const_local_iterator begin(size_type __n) const {
  166. //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
  167. _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
  168. return const_local_iterator(&_M_iter_list, _M_non_dbg_impl.begin(__n));
  169. }
  170. const_local_iterator end(size_type __n) const {
  171. //TODO: Add checks for iterator locality -> avoids comparison between different bucket iterators
  172. _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
  173. return const_local_iterator(&_M_iter_list, _M_non_dbg_impl.end(__n));
  174. }
  175. pair<iterator, bool> insert_unique(const value_type& __obj) {
  176. pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique(__obj);
  177. return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second);
  178. }
  179. iterator insert_equal(const value_type& __obj)
  180. { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal(__obj)); }
  181. pair<iterator, bool> insert_unique_noresize(const value_type& __obj) {
  182. pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique_noresize(__obj);
  183. return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second);
  184. }
  185. iterator insert_equal_noresize(const value_type& __obj)
  186. { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal_noresize(__obj)); }
  187. #if defined (_STLP_MEMBER_TEMPLATES)
  188. template <class _InputIterator>
  189. void insert_unique(_InputIterator __f, _InputIterator __l) {
  190. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
  191. _M_non_dbg_impl.insert_unique(_STLP_PRIV _Non_Dbg_iter(__f), _STLP_PRIV _Non_Dbg_iter(__l));
  192. }
  193. template <class _InputIterator>
  194. void insert_equal(_InputIterator __f, _InputIterator __l){
  195. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
  196. _M_non_dbg_impl.insert_equal(_STLP_PRIV _Non_Dbg_iter(__f), _STLP_PRIV _Non_Dbg_iter(__l));
  197. }
  198. #else
  199. void insert_unique(const value_type* __f, const value_type* __l) {
  200. _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__f, __l))
  201. _M_non_dbg_impl.insert_unique(__f, __l);
  202. }
  203. void insert_equal(const value_type* __f, const value_type* __l) {
  204. _STLP_DEBUG_CHECK(_STLP_PRIV __check_ptr_range(__f, __l))
  205. _M_non_dbg_impl.insert_equal(__f, __l);
  206. }
  207. void insert_unique(const_iterator __f, const_iterator __l) {
  208. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
  209. _M_non_dbg_impl.insert_unique(__f._M_iterator, __l._M_iterator);
  210. }
  211. void insert_equal(const_iterator __f, const_iterator __l) {
  212. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__f, __l))
  213. _M_non_dbg_impl.insert_equal(__f._M_iterator, __l._M_iterator);
  214. }
  215. #endif
  216. _STLP_TEMPLATE_FOR_CONT_EXT
  217. iterator find(const _KT& __key)
  218. { return iterator(&_M_iter_list, _M_non_dbg_impl.find(__key)); }
  219. _STLP_TEMPLATE_FOR_CONT_EXT
  220. const_iterator find(const _KT& __key) const
  221. { return const_iterator(&_M_iter_list, _M_non_dbg_impl.find(__key)); }
  222. _STLP_TEMPLATE_FOR_CONT_EXT
  223. size_type count(const _KT& __key) const { return _M_non_dbg_impl.count(__key); }
  224. _STLP_TEMPLATE_FOR_CONT_EXT
  225. pair<iterator, iterator> equal_range(const _KT& __key) {
  226. pair<_Base_iterator, _Base_iterator> __res = _M_non_dbg_impl.equal_range(__key);
  227. return pair<iterator,iterator> (iterator(&_M_iter_list,__res.first),
  228. iterator(&_M_iter_list,__res.second));
  229. }
  230. _STLP_TEMPLATE_FOR_CONT_EXT
  231. pair<const_iterator, const_iterator> equal_range(const _KT& __key) const {
  232. pair <_Base_const_iterator, _Base_const_iterator> __res = _M_non_dbg_impl.equal_range(__key);
  233. return pair<const_iterator,const_iterator> (const_iterator(&_M_iter_list,__res.first),
  234. const_iterator(&_M_iter_list,__res.second));
  235. }
  236. size_type erase(const key_type& __key) {
  237. pair<iterator, iterator> __p = equal_range(__key);
  238. size_type __n = _STLP_STD::distance(__p.first, __p.second);
  239. _Invalidate_iterators(__p.first, __p.second);
  240. _M_non_dbg_impl.erase(__p.first._M_iterator, __p.second._M_iterator);
  241. return __n;
  242. }
  243. void erase(const const_iterator& __it) {
  244. _STLP_DEBUG_CHECK(_STLP_PRIV _Dereferenceable(__it))
  245. _STLP_DEBUG_CHECK(_STLP_PRIV __check_if_owner(&_M_iter_list, __it))
  246. _Invalidate_iterator(__it);
  247. _M_non_dbg_impl.erase(__it._M_iterator);
  248. }
  249. void erase(const_iterator __first, const_iterator __last) {
  250. _STLP_DEBUG_CHECK(_STLP_PRIV __check_range(__first, __last,
  251. const_iterator(begin()), const_iterator(end())))
  252. _Invalidate_iterators(__first, __last);
  253. _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator);
  254. }
  255. void rehash(size_type __num_buckets_hint) { _M_non_dbg_impl.rehash(__num_buckets_hint); }
  256. void resize(size_type __num_elements_hint) { _M_non_dbg_impl.resize(__num_elements_hint); }
  257. void clear() {
  258. _Invalidate_iterators(begin(), end());
  259. _M_non_dbg_impl.clear();
  260. }
  261. reference _M_insert(const value_type& __obj) { return _M_non_dbg_impl._M_insert(__obj); }
  262. size_type bucket_count() const { return _M_non_dbg_impl.bucket_count(); }
  263. size_type max_bucket_count() const { return _M_non_dbg_impl.max_bucket_count(); }
  264. size_type elems_in_bucket(size_type __n) const {
  265. _STLP_VERBOSE_ASSERT((__n < bucket_count()), _StlMsg_INVALID_ARGUMENT)
  266. return _M_non_dbg_impl.elems_in_bucket(__n);
  267. }
  268. _STLP_TEMPLATE_FOR_CONT_EXT
  269. size_type bucket(const _KT& __k) const { return _M_non_dbg_impl.bucket(__k); }
  270. float load_factor() const { return _M_non_dbg_impl.load_factor(); }
  271. float max_load_factor() const { return _M_non_dbg_impl.max_load_factor(); }
  272. void max_load_factor(float __z) {
  273. _STLP_VERBOSE_ASSERT((__z > 0.0f), _StlMsg_INVALID_ARGUMENT)
  274. _M_non_dbg_impl.max_load_factor(__z);
  275. }
  276. };
  277. _STLP_END_NAMESPACE
  278. #undef _STLP_NON_DBG_HT
  279. #endif /* _STLP_INTERNAL_HASHTABLE_H */
  280. // Local Variables:
  281. // mode:C++
  282. // End: