_tree.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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_TREE_H
  29. #define _STLP_INTERNAL_TREE_H
  30. /*
  31. Red-black tree class, designed for use in implementing STL
  32. associative containers (set, multiset, map, and multimap). The
  33. insertion and deletion algorithms are based on those in Cormen,
  34. Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
  35. except that
  36. (1) the header cell is maintained with links not only to the root
  37. but also to the leftmost node of the tree, to enable constant time
  38. begin(), and to the rightmost node of the tree, to enable linear time
  39. performance when used with the generic set algorithms (set_union,
  40. etc.);
  41. (2) when a node being deleted has two children its successor node is
  42. relinked into its place, rather than copied, so that the only
  43. iterators invalidated are those referring to the deleted node.
  44. */
  45. #ifndef _STLP_INTERNAL_ALGOBASE_H
  46. # include <stl/_algobase.h>
  47. #endif
  48. #ifndef _STLP_INTERNAL_ALLOC_H
  49. # include <stl/_alloc.h>
  50. #endif
  51. #ifndef _STLP_INTERNAL_ITERATOR_H
  52. # include <stl/_iterator.h>
  53. #endif
  54. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  55. # include <stl/_construct.h>
  56. #endif
  57. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  58. # include <stl/_function_base.h>
  59. #endif
  60. _STLP_BEGIN_NAMESPACE
  61. _STLP_MOVE_TO_PRIV_NAMESPACE
  62. typedef bool _Rb_tree_Color_type;
  63. //const _Rb_tree_Color_type _S_rb_tree_red = false;
  64. //const _Rb_tree_Color_type _S_rb_tree_black = true;
  65. #define _S_rb_tree_red false
  66. #define _S_rb_tree_black true
  67. struct _Rb_tree_node_base {
  68. typedef _Rb_tree_Color_type _Color_type;
  69. typedef _Rb_tree_node_base* _Base_ptr;
  70. _Color_type _M_color;
  71. _Base_ptr _M_parent;
  72. _Base_ptr _M_left;
  73. _Base_ptr _M_right;
  74. static _Base_ptr _STLP_CALL _S_minimum(_Base_ptr __x) {
  75. while (__x->_M_left != 0) __x = __x->_M_left;
  76. return __x;
  77. }
  78. static _Base_ptr _STLP_CALL _S_maximum(_Base_ptr __x) {
  79. while (__x->_M_right != 0) __x = __x->_M_right;
  80. return __x;
  81. }
  82. };
  83. template <class _Value>
  84. struct _Rb_tree_node : public _Rb_tree_node_base {
  85. _Value _M_value_field;
  86. __TRIVIAL_STUFF(_Rb_tree_node)
  87. };
  88. struct _Rb_tree_base_iterator;
  89. template <class _Dummy>
  90. class _Rb_global {
  91. public:
  92. typedef _Rb_tree_node_base* _Base_ptr;
  93. // those used to be global functions
  94. static void _STLP_CALL _Rebalance(_Base_ptr __x, _Base_ptr& __root);
  95. static _Base_ptr _STLP_CALL _Rebalance_for_erase(_Base_ptr __z,
  96. _Base_ptr& __root,
  97. _Base_ptr& __leftmost,
  98. _Base_ptr& __rightmost);
  99. // those are from _Rb_tree_base_iterator - moved here to reduce code bloat
  100. // moved here to reduce code bloat without templatizing _Rb_tree_base_iterator
  101. static _Base_ptr _STLP_CALL _M_increment (_Base_ptr);
  102. static _Base_ptr _STLP_CALL _M_decrement (_Base_ptr);
  103. static void _STLP_CALL _Rotate_left (_Base_ptr __x, _Base_ptr& __root);
  104. static void _STLP_CALL _Rotate_right(_Base_ptr __x, _Base_ptr& __root);
  105. };
  106. # if defined (_STLP_USE_TEMPLATE_EXPORT)
  107. _STLP_EXPORT_TEMPLATE_CLASS _Rb_global<bool>;
  108. # endif
  109. typedef _Rb_global<bool> _Rb_global_inst;
  110. struct _Rb_tree_base_iterator {
  111. typedef _Rb_tree_node_base* _Base_ptr;
  112. typedef bidirectional_iterator_tag iterator_category;
  113. typedef ptrdiff_t difference_type;
  114. _Base_ptr _M_node;
  115. _Rb_tree_base_iterator() : _M_node(0) {}
  116. _Rb_tree_base_iterator(_Base_ptr __x) : _M_node(__x) {}
  117. };
  118. template <class _Value, class _Traits>
  119. struct _Rb_tree_iterator : public _Rb_tree_base_iterator {
  120. typedef _Value value_type;
  121. typedef typename _Traits::reference reference;
  122. typedef typename _Traits::pointer pointer;
  123. typedef _Rb_tree_iterator<_Value, _Traits> _Self;
  124. typedef _Rb_tree_node_base* _Base_ptr;
  125. typedef _Rb_tree_node<_Value>* _Link_type;
  126. typedef typename _Traits::_NonConstTraits _NonConstTraits;
  127. typedef _Rb_tree_iterator<_Value, _NonConstTraits> iterator;
  128. typedef typename _Traits::_ConstTraits _ConstTraits;
  129. typedef _Rb_tree_iterator<_Value, _ConstTraits> const_iterator;
  130. _Rb_tree_iterator() {}
  131. #if !defined (_STLP_DEBUG)
  132. /* In STL debug mode we need this constructor implicit for the pointer
  133. * specialization implementation.
  134. */
  135. explicit
  136. #endif
  137. _Rb_tree_iterator(_Base_ptr __x) : _Rb_tree_base_iterator(__x) {}
  138. //copy constructor for iterator and constructor from iterator for const_iterator
  139. _Rb_tree_iterator(const iterator& __it) : _Rb_tree_base_iterator(__it._M_node) {}
  140. reference operator*() const {
  141. return __STATIC_CAST(_Link_type, _M_node)->_M_value_field;
  142. }
  143. _STLP_DEFINE_ARROW_OPERATOR
  144. _Self& operator++() {
  145. _M_node = _Rb_global_inst::_M_increment(_M_node);
  146. return *this;
  147. }
  148. _Self operator++(int) {
  149. _Self __tmp = *this;
  150. ++(*this);
  151. return __tmp;
  152. }
  153. _Self& operator--() {
  154. _M_node = _Rb_global_inst::_M_decrement(_M_node);
  155. return *this;
  156. }
  157. _Self operator--(int) {
  158. _Self __tmp = *this;
  159. --(*this);
  160. return __tmp;
  161. }
  162. bool operator == (const_iterator __rhs) const {
  163. return _M_node == __rhs._M_node;
  164. }
  165. bool operator != (const_iterator __rhs) const {
  166. return _M_node != __rhs._M_node;
  167. }
  168. };
  169. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  170. _STLP_MOVE_TO_STD_NAMESPACE
  171. template <class _Value, class _Traits>
  172. struct __type_traits<_STLP_PRIV _Rb_tree_iterator<_Value, _Traits> > {
  173. typedef __false_type has_trivial_default_constructor;
  174. typedef __true_type has_trivial_copy_constructor;
  175. typedef __true_type has_trivial_assignment_operator;
  176. typedef __true_type has_trivial_destructor;
  177. typedef __false_type is_POD_type;
  178. };
  179. _STLP_MOVE_TO_PRIV_NAMESPACE
  180. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  181. #if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
  182. _STLP_MOVE_TO_STD_NAMESPACE
  183. template <class _Value, class _Traits>
  184. inline _Value* value_type(const _STLP_PRIV _Rb_tree_iterator<_Value, _Traits>&)
  185. { return (_Value*)0; }
  186. inline bidirectional_iterator_tag iterator_category(const _STLP_PRIV _Rb_tree_base_iterator&)
  187. { return bidirectional_iterator_tag(); }
  188. inline ptrdiff_t* distance_type(const _STLP_PRIV _Rb_tree_base_iterator&)
  189. { return (ptrdiff_t*) 0; }
  190. _STLP_MOVE_TO_PRIV_NAMESPACE
  191. #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
  192. // Base class to help EH
  193. template <class _Tp, class _Alloc>
  194. class _Rb_tree_base {
  195. public:
  196. typedef _Rb_tree_node_base _Node_base;
  197. typedef _Rb_tree_node<_Tp> _Node;
  198. _STLP_FORCE_ALLOCATORS(_Tp, _Alloc)
  199. typedef _Alloc allocator_type;
  200. private:
  201. typedef _Rb_tree_base<_Tp, _Alloc> _Self;
  202. typedef typename _Alloc_traits<_Node, _Alloc>::allocator_type _M_node_allocator_type;
  203. typedef _STLP_alloc_proxy<_Node_base, _Node, _M_node_allocator_type> _AllocProxy;
  204. public:
  205. allocator_type get_allocator() const {
  206. return _STLP_CONVERT_ALLOCATOR(_M_header, _Tp);
  207. }
  208. protected:
  209. _Rb_tree_base(const allocator_type& __a) :
  210. _M_header(_STLP_CONVERT_ALLOCATOR(__a, _Node), _Node_base() ) {
  211. _M_empty_initialize();
  212. }
  213. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  214. _Rb_tree_base(__move_source<_Self> src) :
  215. _M_header(__move_source<_AllocProxy>(src.get()._M_header)) {
  216. _M_rebind(&src.get()._M_header._M_data);
  217. src.get()._M_empty_initialize();
  218. }
  219. #endif
  220. void _M_empty_initialize() {
  221. _M_header._M_data._M_color = _S_rb_tree_red; // used to distinguish header from
  222. // __root, in iterator.operator++
  223. _M_header._M_data._M_parent = 0;
  224. _M_header._M_data._M_left = &_M_header._M_data;
  225. _M_header._M_data._M_right = &_M_header._M_data;
  226. }
  227. void _M_rebind(_Node_base *__static_node) {
  228. if (_M_header._M_data._M_parent != 0) {
  229. _M_header._M_data._M_parent->_M_parent = &_M_header._M_data;
  230. }
  231. if (_M_header._M_data._M_right == __static_node) {
  232. _M_header._M_data._M_right = &_M_header._M_data;
  233. }
  234. if (_M_header._M_data._M_left == __static_node) {
  235. _M_header._M_data._M_left = &_M_header._M_data;
  236. }
  237. }
  238. _AllocProxy _M_header;
  239. };
  240. #if defined (_STLP_DEBUG)
  241. # define _Rb_tree _STLP_NON_DBG_NAME(Rb_tree)
  242. #endif
  243. template <class _Key, class _Compare,
  244. class _Value, class _KeyOfValue, class _Traits,
  245. _STLP_DFL_TMPL_PARAM(_Alloc, allocator<_Value>) >
  246. class _Rb_tree : public _Rb_tree_base<_Value, _Alloc> {
  247. typedef _Rb_tree_base<_Value, _Alloc> _Base;
  248. typedef _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc> _Self;
  249. protected:
  250. typedef _Rb_tree_node_base * _Base_ptr;
  251. typedef _Rb_tree_node<_Value> _Node;
  252. typedef _Node* _Link_type;
  253. typedef _Rb_tree_Color_type _Color_type;
  254. public:
  255. typedef _Key key_type;
  256. typedef _Value value_type;
  257. typedef typename _Traits::pointer pointer;
  258. typedef const value_type* const_pointer;
  259. typedef typename _Traits::reference reference;
  260. typedef const value_type& const_reference;
  261. typedef size_t size_type;
  262. typedef ptrdiff_t difference_type;
  263. typedef bidirectional_iterator_tag _Iterator_category;
  264. typedef typename _Base::allocator_type allocator_type;
  265. protected:
  266. _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
  267. _Base_ptr _M_create_node(const value_type& __x) {
  268. _Link_type __tmp = this->_M_header.allocate(1);
  269. _STLP_TRY {
  270. _Copy_Construct(&__tmp->_M_value_field, __x);
  271. }
  272. _STLP_UNWIND(this->_M_header.deallocate(__tmp,1))
  273. _S_left(__tmp) = 0;
  274. _S_right(__tmp) = 0;
  275. return __tmp;
  276. }
  277. _Base_ptr _M_clone_node(_Base_ptr __x) {
  278. _Base_ptr __tmp = _M_create_node(_S_value(__x));
  279. _S_color(__tmp) = _S_color(__x);
  280. return __tmp;
  281. }
  282. size_type _M_node_count; // keeps track of size of tree
  283. _Compare _M_key_compare;
  284. _Base_ptr _M_root() const
  285. { return this->_M_header._M_data._M_parent; }
  286. _Base_ptr _M_leftmost() const
  287. { return this->_M_header._M_data._M_left; }
  288. _Base_ptr _M_rightmost() const
  289. { return this->_M_header._M_data._M_right; }
  290. _Base_ptr& _M_root()
  291. { return this->_M_header._M_data._M_parent; }
  292. _Base_ptr& _M_leftmost()
  293. { return this->_M_header._M_data._M_left; }
  294. _Base_ptr& _M_rightmost()
  295. { return this->_M_header._M_data._M_right; }
  296. static _Base_ptr& _STLP_CALL _S_left(_Base_ptr __x)
  297. { return __x->_M_left; }
  298. static _Base_ptr& _STLP_CALL _S_right(_Base_ptr __x)
  299. { return __x->_M_right; }
  300. static _Base_ptr& _STLP_CALL _S_parent(_Base_ptr __x)
  301. { return __x->_M_parent; }
  302. static value_type& _STLP_CALL _S_value(_Base_ptr __x)
  303. { return __STATIC_CAST(_Link_type, __x)->_M_value_field; }
  304. static const _Key& _STLP_CALL _S_key(_Base_ptr __x)
  305. { return _KeyOfValue()(_S_value(__x));}
  306. static _Color_type& _STLP_CALL _S_color(_Base_ptr __x)
  307. { return (_Color_type&)(__x->_M_color); }
  308. static _Base_ptr _STLP_CALL _S_minimum(_Base_ptr __x)
  309. { return _Rb_tree_node_base::_S_minimum(__x); }
  310. static _Base_ptr _STLP_CALL _S_maximum(_Base_ptr __x)
  311. { return _Rb_tree_node_base::_S_maximum(__x); }
  312. public:
  313. typedef typename _Traits::_NonConstTraits _NonConstTraits;
  314. typedef typename _Traits::_ConstTraits _ConstTraits;
  315. typedef _Rb_tree_iterator<value_type, _NonConstTraits> iterator;
  316. typedef _Rb_tree_iterator<value_type, _ConstTraits> const_iterator;
  317. _STLP_DECLARE_BIDIRECTIONAL_REVERSE_ITERATORS;
  318. private:
  319. iterator _M_insert(_Base_ptr __parent, const value_type& __val, _Base_ptr __on_left = 0, _Base_ptr __on_right = 0);
  320. _Base_ptr _M_copy(_Base_ptr __x, _Base_ptr __p);
  321. void _M_erase(_Base_ptr __x);
  322. public:
  323. // allocation/deallocation
  324. _Rb_tree()
  325. : _Rb_tree_base<_Value, _Alloc>(allocator_type()), _M_node_count(0), _M_key_compare(_Compare())
  326. {}
  327. _Rb_tree(const _Compare& __comp)
  328. : _Rb_tree_base<_Value, _Alloc>(allocator_type()), _M_node_count(0), _M_key_compare(__comp)
  329. {}
  330. _Rb_tree(const _Compare& __comp, const allocator_type& __a)
  331. : _Rb_tree_base<_Value, _Alloc>(__a), _M_node_count(0), _M_key_compare(__comp)
  332. {}
  333. _Rb_tree(const _Self& __x)
  334. : _Rb_tree_base<_Value, _Alloc>(__x.get_allocator()),
  335. _M_node_count(0), _M_key_compare(__x._M_key_compare) {
  336. if (__x._M_root() != 0) {
  337. _S_color(&this->_M_header._M_data) = _S_rb_tree_red;
  338. _M_root() = _M_copy(__x._M_root(), &this->_M_header._M_data);
  339. _M_leftmost() = _S_minimum(_M_root());
  340. _M_rightmost() = _S_maximum(_M_root());
  341. }
  342. _M_node_count = __x._M_node_count;
  343. }
  344. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  345. _Rb_tree(__move_source<_Self> src)
  346. : _Rb_tree_base<_Value, _Alloc>(__move_source<_Base>(src.get())),
  347. _M_node_count(src.get()._M_node_count),
  348. _M_key_compare(_AsMoveSource(src.get()._M_key_compare))
  349. { src.get()._M_node_count = 0; }
  350. #endif
  351. ~_Rb_tree() { clear(); }
  352. _Self& operator=(const _Self& __x);
  353. public:
  354. // accessors:
  355. _Compare key_comp() const { return _M_key_compare; }
  356. iterator begin() { return iterator(_M_leftmost()); }
  357. const_iterator begin() const { return const_iterator(_M_leftmost()); }
  358. iterator end() { return iterator(&this->_M_header._M_data); }
  359. const_iterator end() const { return const_iterator(__CONST_CAST(_Base_ptr, &this->_M_header._M_data)); }
  360. reverse_iterator rbegin() { return reverse_iterator(end()); }
  361. const_reverse_iterator rbegin() const
  362. { return const_reverse_iterator(end()); }
  363. reverse_iterator rend() { return reverse_iterator(begin()); }
  364. const_reverse_iterator rend() const
  365. { return const_reverse_iterator(begin()); }
  366. bool empty() const { return _M_node_count == 0; }
  367. size_type size() const { return _M_node_count; }
  368. size_type max_size() const { return size_type(-1); }
  369. void swap(_Self& __t) {
  370. if (__t.empty()) {
  371. if (this->empty()) return;
  372. __t._M_header.swap(this->_M_header);
  373. __t._M_rebind(&this->_M_header._M_data);
  374. this->_M_empty_initialize();
  375. }
  376. else if (this->empty()) {
  377. __t.swap(*this);
  378. return;
  379. }
  380. else {
  381. this->_M_header.swap(__t._M_header);
  382. this->_M_rebind(&__t._M_header._M_data);
  383. __t._M_rebind(&this->_M_header._M_data);
  384. }
  385. _STLP_STD::swap(_M_node_count, __t._M_node_count);
  386. _STLP_STD::swap(_M_key_compare, __t._M_key_compare);
  387. }
  388. public:
  389. // insert/erase
  390. pair<iterator,bool> insert_unique(const value_type& __x);
  391. iterator insert_equal(const value_type& __x);
  392. iterator insert_unique(iterator __pos, const value_type& __x);
  393. iterator insert_equal(iterator __pos, const value_type& __x);
  394. #if defined (_STLP_MEMBER_TEMPLATES)
  395. template<class _II> void insert_equal(_II __first, _II __last) {
  396. for ( ; __first != __last; ++__first)
  397. insert_equal(*__first);
  398. }
  399. template<class _II> void insert_unique(_II __first, _II __last) {
  400. for ( ; __first != __last; ++__first)
  401. insert_unique(*__first);
  402. }
  403. #else
  404. void insert_unique(const_iterator __first, const_iterator __last) {
  405. for ( ; __first != __last; ++__first)
  406. insert_unique(*__first);
  407. }
  408. void insert_unique(const value_type* __first, const value_type* __last) {
  409. for ( ; __first != __last; ++__first)
  410. insert_unique(*__first);
  411. }
  412. void insert_equal(const_iterator __first, const_iterator __last) {
  413. for ( ; __first != __last; ++__first)
  414. insert_equal(*__first);
  415. }
  416. void insert_equal(const value_type* __first, const value_type* __last) {
  417. for ( ; __first != __last; ++__first)
  418. insert_equal(*__first);
  419. }
  420. #endif
  421. void erase(iterator __pos) {
  422. _Base_ptr __x = _Rb_global_inst::_Rebalance_for_erase(__pos._M_node,
  423. this->_M_header._M_data._M_parent,
  424. this->_M_header._M_data._M_left,
  425. this->_M_header._M_data._M_right);
  426. _STLP_STD::_Destroy(&_S_value(__x));
  427. this->_M_header.deallocate(__STATIC_CAST(_Link_type, __x), 1);
  428. --_M_node_count;
  429. }
  430. size_type erase(const key_type& __x) {
  431. pair<iterator,iterator> __p = equal_range(__x);
  432. size_type __n = _STLP_STD::distance(__p.first, __p.second);
  433. erase(__p.first, __p.second);
  434. return __n;
  435. }
  436. size_type erase_unique(const key_type& __x) {
  437. iterator __i = find(__x);
  438. if (__i._M_node != &this->_M_header._M_data) {
  439. erase(__i);
  440. return 1;
  441. }
  442. return 0;
  443. }
  444. void erase(iterator __first, iterator __last) {
  445. if (__first._M_node == this->_M_header._M_data._M_left && // begin()
  446. __last._M_node == &this->_M_header._M_data) // end()
  447. clear();
  448. else
  449. while (__first != __last) erase(__first++);
  450. }
  451. void erase(const key_type* __first, const key_type* __last) {
  452. while (__first != __last) erase(*__first++);
  453. }
  454. void clear() {
  455. if (_M_node_count != 0) {
  456. _M_erase(_M_root());
  457. _M_leftmost() = &this->_M_header._M_data;
  458. _M_root() = 0;
  459. _M_rightmost() = &this->_M_header._M_data;
  460. _M_node_count = 0;
  461. }
  462. }
  463. public:
  464. // set operations:
  465. _STLP_TEMPLATE_FOR_CONT_EXT
  466. iterator find(const _KT& __k) { return iterator(_M_find(__k)); }
  467. _STLP_TEMPLATE_FOR_CONT_EXT
  468. const_iterator find(const _KT& __k) const { return const_iterator(_M_find(__k)); }
  469. private:
  470. _STLP_TEMPLATE_FOR_CONT_EXT
  471. _Base_ptr _M_find(const _KT& __k) const {
  472. _Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); // Last node which is not less than __k.
  473. _Base_ptr __x = _M_root(); // Current node.
  474. while (__x != 0)
  475. if (!_M_key_compare(_S_key(__x), __k))
  476. __y = __x, __x = _S_left(__x);
  477. else
  478. __x = _S_right(__x);
  479. if (__y != &this->_M_header._M_data) {
  480. if (_M_key_compare(__k, _S_key(__y))) {
  481. __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data);
  482. }
  483. }
  484. return __y;
  485. }
  486. _STLP_TEMPLATE_FOR_CONT_EXT
  487. _Base_ptr _M_lower_bound(const _KT& __k) const {
  488. _Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); /* Last node which is not less than __k. */
  489. _Base_ptr __x = _M_root(); /* Current node. */
  490. while (__x != 0)
  491. if (!_M_key_compare(_S_key(__x), __k))
  492. __y = __x, __x = _S_left(__x);
  493. else
  494. __x = _S_right(__x);
  495. return __y;
  496. }
  497. _STLP_TEMPLATE_FOR_CONT_EXT
  498. _Base_ptr _M_upper_bound(const _KT& __k) const {
  499. _Base_ptr __y = __CONST_CAST(_Base_ptr, &this->_M_header._M_data); /* Last node which is greater than __k. */
  500. _Base_ptr __x = _M_root(); /* Current node. */
  501. while (__x != 0)
  502. if (_M_key_compare(__k, _S_key(__x)))
  503. __y = __x, __x = _S_left(__x);
  504. else
  505. __x = _S_right(__x);
  506. return __y;
  507. }
  508. public:
  509. _STLP_TEMPLATE_FOR_CONT_EXT
  510. size_type count(const _KT& __x) const {
  511. pair<const_iterator, const_iterator> __p = equal_range(__x);
  512. return _STLP_STD::distance(__p.first, __p.second);
  513. }
  514. _STLP_TEMPLATE_FOR_CONT_EXT
  515. iterator lower_bound(const _KT& __x) { return iterator(_M_lower_bound(__x)); }
  516. _STLP_TEMPLATE_FOR_CONT_EXT
  517. const_iterator lower_bound(const _KT& __x) const { return const_iterator(_M_lower_bound(__x)); }
  518. _STLP_TEMPLATE_FOR_CONT_EXT
  519. iterator upper_bound(const _KT& __x) { return iterator(_M_upper_bound(__x)); }
  520. _STLP_TEMPLATE_FOR_CONT_EXT
  521. const_iterator upper_bound(const _KT& __x) const { return const_iterator(_M_upper_bound(__x)); }
  522. _STLP_TEMPLATE_FOR_CONT_EXT
  523. pair<iterator,iterator> equal_range(const _KT& __x)
  524. { return pair<iterator, iterator>(lower_bound(__x), upper_bound(__x)); }
  525. _STLP_TEMPLATE_FOR_CONT_EXT
  526. pair<const_iterator, const_iterator> equal_range(const _KT& __x) const
  527. { return pair<const_iterator, const_iterator>(lower_bound(__x), upper_bound(__x)); }
  528. _STLP_TEMPLATE_FOR_CONT_EXT
  529. pair<iterator,iterator> equal_range_unique(const _KT& __x) {
  530. pair<iterator, iterator> __p;
  531. __p.second = lower_bound(__x);
  532. if (__p.second._M_node != &this->_M_header._M_data &&
  533. !_M_key_compare(__x, _S_key(__p.second._M_node))) {
  534. __p.first = __p.second++;
  535. }
  536. else {
  537. __p.first = __p.second;
  538. }
  539. return __p;
  540. }
  541. _STLP_TEMPLATE_FOR_CONT_EXT
  542. pair<const_iterator, const_iterator> equal_range_unique(const _KT& __x) const {
  543. pair<const_iterator, const_iterator> __p;
  544. __p.second = lower_bound(__x);
  545. if (__p.second._M_node != &this->_M_header._M_data &&
  546. !_M_key_compare(__x, _S_key(__p.second._M_node))) {
  547. __p.first = __p.second++;
  548. }
  549. else {
  550. __p.first = __p.second;
  551. }
  552. return __p;
  553. }
  554. #if defined (_STLP_DEBUG)
  555. public:
  556. // Debugging.
  557. bool __rb_verify() const;
  558. #endif //_STLP_DEBUG
  559. };
  560. #if defined (_STLP_DEBUG)
  561. # undef _Rb_tree
  562. #endif
  563. _STLP_MOVE_TO_STD_NAMESPACE
  564. _STLP_END_NAMESPACE
  565. #if !defined (_STLP_LINK_TIME_INSTANTIATION)
  566. # include <stl/_tree.c>
  567. #endif
  568. #if defined (_STLP_DEBUG)
  569. # include <stl/debug/_tree.h>
  570. #endif
  571. _STLP_BEGIN_NAMESPACE
  572. #define _STLP_TEMPLATE_HEADER template <class _Key, class _Compare, class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  573. #define _STLP_TEMPLATE_CONTAINER _STLP_PRIV _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>
  574. #include <stl/_relops_cont.h>
  575. #undef _STLP_TEMPLATE_CONTAINER
  576. #undef _STLP_TEMPLATE_HEADER
  577. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_NO_MOVE_SEMANTIC)
  578. template <class _Key, class _Compare, class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  579. struct __move_traits<_STLP_PRIV _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc> >
  580. : _STLP_PRIV __move_traits_help2<_Compare, _Alloc> {};
  581. #endif
  582. _STLP_END_NAMESPACE
  583. #endif /* _STLP_INTERNAL_TREE_H */
  584. // Local Variables:
  585. // mode:C++
  586. // End: