_tree.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_TREE_H
  29. #define _STLP_INTERNAL_DBG_TREE_H
  30. #ifndef _STLP_DBG_ITERATOR_H
  31. # include <stl/debug/_iterator.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  34. # include <stl/_function_base.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_ALLOC_H
  37. # include <stl/_alloc.h>
  38. #endif
  39. _STLP_BEGIN_NAMESPACE
  40. _STLP_MOVE_TO_PRIV_NAMESPACE
  41. template <class _Key, class _Compare>
  42. class _DbgCompare {
  43. public:
  44. _DbgCompare() {}
  45. _DbgCompare(const _Compare& __cmp) : _M_non_dbg_cmp(__cmp) {}
  46. _DbgCompare(const _DbgCompare& __cmp) : _M_non_dbg_cmp(__cmp._M_non_dbg_cmp) {}
  47. #if !defined (_STLP_USE_CONTAINERS_EXTENSION)
  48. bool operator () (const _Key& __lhs, const _Key& __rhs) const {
  49. #else
  50. template <class _Kp1, class _Kp2>
  51. bool operator () (const _Kp1& __lhs, const _Kp2& __rhs) const {
  52. #endif
  53. if (_M_non_dbg_cmp(__lhs, __rhs)) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. _Compare non_dbg_key_comp() const { return _M_non_dbg_cmp; }
  59. private:
  60. _Compare _M_non_dbg_cmp;
  61. };
  62. #define _STLP_NON_DBG_TREE _STLP_PRIV _STLP_NON_DBG_NAME(Rb_tree) <_Key, _STLP_PRIV _DbgCompare<_Key, _Compare>, _Value, _KeyOfValue, _Traits, _Alloc>
  63. #if defined (_STLP_DEBUG_USE_DISTINCT_VALUE_TYPE_HELPERS)
  64. _STLP_MOVE_TO_STD_NAMESPACE
  65. template <class _Key, class _Compare,
  66. class _Value, class _KeyOfValue, class _Traits, class _Alloc >
  67. inline _Value*
  68. value_type(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_TREE >&)
  69. { return (_Value*)0; }
  70. template <class _Key, class _Compare,
  71. class _Value, class _KeyOfValue, class _Traits, class _Alloc >
  72. inline bidirectional_iterator_tag
  73. iterator_category(const _STLP_PRIV _DBG_iter_base< _STLP_NON_DBG_TREE >&)
  74. { return bidirectional_iterator_tag(); }
  75. _STLP_MOVE_TO_PRIV_NAMESPACE
  76. #endif
  77. template <class _Key, class _Compare,
  78. class _Value, class _KeyOfValue, class _Traits,
  79. _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Value>) >
  80. class _Rb_tree {
  81. typedef _STLP_NON_DBG_TREE _Base;
  82. typedef _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc> _Self;
  83. _Base _M_non_dbg_impl;
  84. _STLP_PRIV __owned_list _M_iter_list;
  85. public:
  86. __IMPORT_CONTAINER_TYPEDEFS(_Base)
  87. typedef typename _Base::key_type key_type;
  88. typedef typename _Traits::_NonConstTraits _NonConstIteTraits;
  89. typedef typename _Traits::_ConstTraits _ConstIteTraits;
  90. typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_NonConstIteTraits> > iterator;
  91. typedef _STLP_PRIV _DBG_iter<_Base, _STLP_PRIV _DbgTraits<_ConstIteTraits> > const_iterator;
  92. _STLP_DECLARE_BIDIRECTIONAL_REVERSE_ITERATORS;
  93. private:
  94. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  95. void _Invalidate_iterator(const iterator& __it)
  96. { _STLP_PRIV __invalidate_iterator(&_M_iter_list,__it); }
  97. void _Invalidate_iterators(const iterator& __first, const iterator& __last)
  98. { _STLP_PRIV __invalidate_range(&_M_iter_list, __first, __last); }
  99. typedef typename _Base::iterator _Base_iterator;
  100. typedef typename _Base::const_iterator _Base_const_iterator;
  101. public:
  102. _Rb_tree()
  103. : _M_non_dbg_impl(), _M_iter_list(&_M_non_dbg_impl) {}
  104. _Rb_tree(const _Compare& __comp)
  105. : _M_non_dbg_impl(__comp), _M_iter_list(&_M_non_dbg_impl) {}
  106. _Rb_tree(const _Compare& __comp, const allocator_type& __a)
  107. : _M_non_dbg_impl(__comp, __a), _M_iter_list(&_M_non_dbg_impl) {}
  108. _Rb_tree(const _Self& __x)
  109. : _M_non_dbg_impl(__x._M_non_dbg_impl), _M_iter_list(&_M_non_dbg_impl) {}
  110. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  111. _Rb_tree(__move_source<_Self> src):
  112. _M_non_dbg_impl(__move_source<_Base>(src.get()._M_non_dbg_impl)),
  113. _M_iter_list(&_M_non_dbg_impl) {
  114. # if defined (_STLP_NO_EXTENSIONS) || (_STLP_DEBUG_LEVEL == _STLP_STANDARD_DBG_LEVEL)
  115. src.get()._M_iter_list._Invalidate_all();
  116. # else
  117. src.get()._M_iter_list._Set_owner(_M_iter_list);
  118. # endif
  119. }
  120. #endif
  121. ~_Rb_tree() {}
  122. _Self& operator=(const _Self& __x) {
  123. if (this != &__x) {
  124. //Should not invalidate end iterator:
  125. _Invalidate_iterators(begin(), end());
  126. _M_non_dbg_impl = __x._M_non_dbg_impl;
  127. }
  128. return *this;
  129. }
  130. allocator_type get_allocator() const { return _M_non_dbg_impl.get_allocator(); }
  131. _Compare key_comp() const { return _M_non_dbg_impl.key_comp().non_dbg_key_comp(); }
  132. iterator begin() { return iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
  133. const_iterator begin() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.begin()); }
  134. iterator end() { return iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
  135. const_iterator end() const { return const_iterator(&_M_iter_list, _M_non_dbg_impl.end()); }
  136. reverse_iterator rbegin() { return reverse_iterator(end()); }
  137. const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
  138. reverse_iterator rend() { return reverse_iterator(begin()); }
  139. const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
  140. bool empty() const { return _M_non_dbg_impl.empty(); }
  141. size_type size() const { return _M_non_dbg_impl.size(); }
  142. size_type max_size() const { return _M_non_dbg_impl.max_size(); }
  143. _STLP_TEMPLATE_FOR_CONT_EXT
  144. size_type count(const _KT& __x) const { return _M_non_dbg_impl.count(__x); }
  145. void swap(_Self& __t) {
  146. _M_non_dbg_impl.swap(__t._M_non_dbg_impl);
  147. _M_iter_list._Swap_owners(__t._M_iter_list);
  148. }
  149. _STLP_TEMPLATE_FOR_CONT_EXT
  150. iterator find(const _KT& __k)
  151. { return iterator(&_M_iter_list, _M_non_dbg_impl.find(__k)); }
  152. _STLP_TEMPLATE_FOR_CONT_EXT
  153. const_iterator find(const _KT& __k) const
  154. { return const_iterator(&_M_iter_list, _M_non_dbg_impl.find(__k)); }
  155. _STLP_TEMPLATE_FOR_CONT_EXT
  156. iterator lower_bound(const _KT& __x)
  157. { return iterator(&_M_iter_list, _M_non_dbg_impl.lower_bound(__x)); }
  158. _STLP_TEMPLATE_FOR_CONT_EXT
  159. const_iterator lower_bound(const _KT& __x) const
  160. { return const_iterator(&_M_iter_list, _M_non_dbg_impl.lower_bound(__x)); }
  161. _STLP_TEMPLATE_FOR_CONT_EXT
  162. iterator upper_bound(const _KT& __x)
  163. { return iterator(&_M_iter_list, _M_non_dbg_impl.upper_bound(__x)); }
  164. _STLP_TEMPLATE_FOR_CONT_EXT
  165. const_iterator upper_bound(const _KT& __x) const
  166. { return const_iterator(&_M_iter_list, _M_non_dbg_impl.upper_bound(__x)); }
  167. _STLP_TEMPLATE_FOR_CONT_EXT
  168. pair<iterator,iterator> equal_range(const _KT& __x) {
  169. return pair<iterator, iterator>(iterator(&_M_iter_list, _M_non_dbg_impl.lower_bound(__x)),
  170. iterator(&_M_iter_list, _M_non_dbg_impl.upper_bound(__x)));
  171. }
  172. _STLP_TEMPLATE_FOR_CONT_EXT
  173. pair<const_iterator, const_iterator> equal_range(const _KT& __x) const {
  174. return pair<const_iterator,const_iterator>(const_iterator(&_M_iter_list, _M_non_dbg_impl.lower_bound(__x)),
  175. const_iterator(&_M_iter_list, _M_non_dbg_impl.upper_bound(__x)));
  176. }
  177. _STLP_TEMPLATE_FOR_CONT_EXT
  178. pair<iterator,iterator> equal_range_unique(const _KT& __x) {
  179. _STLP_STD::pair<_Base_iterator, _Base_iterator> __p;
  180. __p = _M_non_dbg_impl.equal_range_unique(__x);
  181. return pair<iterator, iterator>(iterator(&_M_iter_list, __p.first), iterator(&_M_iter_list, __p.second));
  182. }
  183. _STLP_TEMPLATE_FOR_CONT_EXT
  184. pair<const_iterator, const_iterator> equal_range_unique(const _KT& __x) const {
  185. _STLP_STD::pair<_Base_const_iterator, _Base_const_iterator> __p;
  186. __p = _M_non_dbg_impl.equal_range_unique(__x);
  187. return pair<const_iterator, const_iterator>(const_iterator(&_M_iter_list, __p.first),
  188. const_iterator(&_M_iter_list, __p.second));
  189. }
  190. pair<iterator,bool> insert_unique(const value_type& __x) {
  191. _STLP_STD::pair<_Base_iterator, bool> __res = _M_non_dbg_impl.insert_unique(__x);
  192. return pair<iterator, bool>(iterator(&_M_iter_list, __res.first), __res.second);
  193. }
  194. iterator insert_equal(const value_type& __x)
  195. { return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal(__x)); }
  196. iterator insert_unique(iterator __pos, const value_type& __x) {
  197. _STLP_DEBUG_CHECK(__check_if_owner(&_M_iter_list,__pos))
  198. return iterator(&_M_iter_list, _M_non_dbg_impl.insert_unique(__pos._M_iterator, __x));
  199. }
  200. iterator insert_equal(iterator __pos, const value_type& __x) {
  201. _STLP_DEBUG_CHECK(__check_if_owner(&_M_iter_list, __pos))
  202. return iterator(&_M_iter_list, _M_non_dbg_impl.insert_equal(__pos._M_iterator, __x));
  203. }
  204. #if defined (_STLP_MEMBER_TEMPLATES)
  205. template<class _InputIterator>
  206. void insert_equal(_InputIterator __first, _InputIterator __last) {
  207. _STLP_DEBUG_CHECK(__check_range(__first,__last))
  208. _M_non_dbg_impl.insert_equal(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
  209. }
  210. template<class _InputIterator>
  211. void insert_unique(_InputIterator __first, _InputIterator __last) {
  212. _STLP_DEBUG_CHECK(__check_range(__first,__last))
  213. _M_non_dbg_impl.insert_unique(_STLP_PRIV _Non_Dbg_iter(__first), _STLP_PRIV _Non_Dbg_iter(__last));
  214. }
  215. #else
  216. void insert_unique(const_iterator __first, const_iterator __last) {
  217. _STLP_DEBUG_CHECK(__check_range(__first,__last))
  218. _M_non_dbg_impl.insert_unique(__first._M_iterator, __last._M_iterator);
  219. }
  220. void insert_unique(const value_type* __first, const value_type* __last) {
  221. _STLP_DEBUG_CHECK(__check_ptr_range(__first,__last))
  222. _M_non_dbg_impl.insert_unique(__first, __last);
  223. }
  224. void insert_equal(const_iterator __first, const_iterator __last) {
  225. _STLP_DEBUG_CHECK(__check_range(__first,__last))
  226. _M_non_dbg_impl.insert_equal(__first._M_iterator, __last._M_iterator);
  227. }
  228. void insert_equal(const value_type* __first, const value_type* __last) {
  229. _STLP_DEBUG_CHECK(__check_ptr_range(__first,__last))
  230. _M_non_dbg_impl.insert_equal(__first, __last);
  231. }
  232. #endif
  233. void erase(iterator __pos) {
  234. _STLP_DEBUG_CHECK(__check_if_owner(&_M_iter_list,__pos))
  235. _STLP_DEBUG_CHECK(_Dereferenceable(__pos))
  236. _Invalidate_iterator(__pos);
  237. _M_non_dbg_impl.erase(__pos._M_iterator);
  238. }
  239. size_type erase(const key_type& __x) {
  240. pair<iterator, iterator> __p = equal_range(__x);
  241. size_type __n = _STLP_STD::distance(__p.first._M_iterator, __p.second._M_iterator);
  242. _Invalidate_iterators(__p.first, __p.second);
  243. _M_non_dbg_impl.erase(__p.first._M_iterator, __p.second._M_iterator);
  244. return __n;
  245. }
  246. size_type erase_unique(const key_type& __x) {
  247. _Base_iterator __i = _M_non_dbg_impl.find(__x);
  248. if (__i != _M_non_dbg_impl.end()) {
  249. _Invalidate_iterator(iterator(&_M_iter_list, __i));
  250. _M_non_dbg_impl.erase(__i);
  251. return 1;
  252. }
  253. return 0;
  254. }
  255. void erase(iterator __first, iterator __last) {
  256. _STLP_DEBUG_CHECK(__check_range(__first, __last, begin(), end()))
  257. _Invalidate_iterators(__first, __last);
  258. _M_non_dbg_impl.erase(__first._M_iterator, __last._M_iterator);
  259. }
  260. void erase(const key_type* __first, const key_type* __last) {
  261. while (__first != __last) erase(*__first++);
  262. }
  263. void clear() {
  264. //should not invalidate end:
  265. _Invalidate_iterators(begin(), end());
  266. _M_non_dbg_impl.clear();
  267. }
  268. };
  269. _STLP_MOVE_TO_STD_NAMESPACE
  270. _STLP_END_NAMESPACE
  271. #undef _STLP_NON_DBG_TREE
  272. #endif /* _STLP_INTERNAL_DBG_TREE_H */
  273. // Local Variables:
  274. // mode:C++
  275. // End: