_tree.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. *
  3. *
  4. * Copyright (c) 1994
  5. * Hewlett-Packard Company
  6. *
  7. * Copyright (c) 1996,1997
  8. * Silicon Graphics Computer Systems, Inc.
  9. *
  10. * Copyright (c) 1997
  11. * Moscow Center for SPARC Technology
  12. *
  13. * Copyright (c) 1999
  14. * Boris Fomitchev
  15. *
  16. * This material is provided "as is", with absolutely no warranty expressed
  17. * or implied. Any use is at your own risk.
  18. *
  19. * Permission to use or copy this software for any purpose is hereby granted
  20. * without fee, provided the above notices are retained on all copies.
  21. * Permission to modify the code and to distribute modified code is granted,
  22. * provided the above notices are retained, and a notice that the code was
  23. * modified is included with the above copyright notice.
  24. *
  25. * Modified CRP 7/10/00 for improved conformance / efficiency on insert_unique /
  26. * insert_equal with valid hint -- efficiency is improved all around, and it is
  27. * should now be standard conforming for complexity on insert point immediately
  28. * after hint (amortized constant time).
  29. *
  30. */
  31. #ifndef _STLP_TREE_C
  32. #define _STLP_TREE_C
  33. #ifndef _STLP_INTERNAL_TREE_H
  34. # include <stl/_tree.h>
  35. #endif
  36. #if defined (_STLP_DEBUG)
  37. # define _Rb_tree _STLP_NON_DBG_NAME(Rb_tree)
  38. #endif
  39. // fbp: these defines are for outline methods definitions.
  40. // needed for definitions to be portable. Should not be used in method bodies.
  41. #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
  42. # define __iterator__ _Rb_tree_iterator<_Value, _STLP_HEADER_TYPENAME _Traits::_NonConstTraits>
  43. # define __size_type__ size_t
  44. # define iterator __iterator__
  45. #else
  46. # define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc>::iterator
  47. # define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc>::size_type
  48. #endif
  49. _STLP_BEGIN_NAMESPACE
  50. _STLP_MOVE_TO_PRIV_NAMESPACE
  51. #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
  52. template <class _Dummy> void _STLP_CALL
  53. _Rb_global<_Dummy>::_Rotate_left(_Rb_tree_node_base* __x,
  54. _Rb_tree_node_base*& __root) {
  55. _Rb_tree_node_base* __y = __x->_M_right;
  56. __x->_M_right = __y->_M_left;
  57. if (__y->_M_left != 0)
  58. __y->_M_left->_M_parent = __x;
  59. __y->_M_parent = __x->_M_parent;
  60. if (__x == __root)
  61. __root = __y;
  62. else if (__x == __x->_M_parent->_M_left)
  63. __x->_M_parent->_M_left = __y;
  64. else
  65. __x->_M_parent->_M_right = __y;
  66. __y->_M_left = __x;
  67. __x->_M_parent = __y;
  68. }
  69. template <class _Dummy> void _STLP_CALL
  70. _Rb_global<_Dummy>::_Rotate_right(_Rb_tree_node_base* __x,
  71. _Rb_tree_node_base*& __root) {
  72. _Rb_tree_node_base* __y = __x->_M_left;
  73. __x->_M_left = __y->_M_right;
  74. if (__y->_M_right != 0)
  75. __y->_M_right->_M_parent = __x;
  76. __y->_M_parent = __x->_M_parent;
  77. if (__x == __root)
  78. __root = __y;
  79. else if (__x == __x->_M_parent->_M_right)
  80. __x->_M_parent->_M_right = __y;
  81. else
  82. __x->_M_parent->_M_left = __y;
  83. __y->_M_right = __x;
  84. __x->_M_parent = __y;
  85. }
  86. template <class _Dummy> void _STLP_CALL
  87. _Rb_global<_Dummy>::_Rebalance(_Rb_tree_node_base* __x,
  88. _Rb_tree_node_base*& __root) {
  89. __x->_M_color = _S_rb_tree_red;
  90. while (__x != __root && __x->_M_parent->_M_color == _S_rb_tree_red) {
  91. if (__x->_M_parent == __x->_M_parent->_M_parent->_M_left) {
  92. _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_right;
  93. if (__y && __y->_M_color == _S_rb_tree_red) {
  94. __x->_M_parent->_M_color = _S_rb_tree_black;
  95. __y->_M_color = _S_rb_tree_black;
  96. __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
  97. __x = __x->_M_parent->_M_parent;
  98. }
  99. else {
  100. if (__x == __x->_M_parent->_M_right) {
  101. __x = __x->_M_parent;
  102. _Rotate_left(__x, __root);
  103. }
  104. __x->_M_parent->_M_color = _S_rb_tree_black;
  105. __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
  106. _Rotate_right(__x->_M_parent->_M_parent, __root);
  107. }
  108. }
  109. else {
  110. _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_left;
  111. if (__y && __y->_M_color == _S_rb_tree_red) {
  112. __x->_M_parent->_M_color = _S_rb_tree_black;
  113. __y->_M_color = _S_rb_tree_black;
  114. __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
  115. __x = __x->_M_parent->_M_parent;
  116. }
  117. else {
  118. if (__x == __x->_M_parent->_M_left) {
  119. __x = __x->_M_parent;
  120. _Rotate_right(__x, __root);
  121. }
  122. __x->_M_parent->_M_color = _S_rb_tree_black;
  123. __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
  124. _Rotate_left(__x->_M_parent->_M_parent, __root);
  125. }
  126. }
  127. }
  128. __root->_M_color = _S_rb_tree_black;
  129. }
  130. template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
  131. _Rb_global<_Dummy>::_Rebalance_for_erase(_Rb_tree_node_base* __z,
  132. _Rb_tree_node_base*& __root,
  133. _Rb_tree_node_base*& __leftmost,
  134. _Rb_tree_node_base*& __rightmost) {
  135. _Rb_tree_node_base* __y = __z;
  136. _Rb_tree_node_base* __x;
  137. _Rb_tree_node_base* __x_parent;
  138. if (__y->_M_left == 0) // __z has at most one non-null child. y == z.
  139. __x = __y->_M_right; // __x might be null.
  140. else {
  141. if (__y->_M_right == 0) // __z has exactly one non-null child. y == z.
  142. __x = __y->_M_left; // __x is not null.
  143. else { // __z has two non-null children. Set __y to
  144. __y = _Rb_tree_node_base::_S_minimum(__y->_M_right); // __z's successor. __x might be null.
  145. __x = __y->_M_right;
  146. }
  147. }
  148. if (__y != __z) { // relink y in place of z. y is z's successor
  149. __z->_M_left->_M_parent = __y;
  150. __y->_M_left = __z->_M_left;
  151. if (__y != __z->_M_right) {
  152. __x_parent = __y->_M_parent;
  153. if (__x) __x->_M_parent = __y->_M_parent;
  154. __y->_M_parent->_M_left = __x; // __y must be a child of _M_left
  155. __y->_M_right = __z->_M_right;
  156. __z->_M_right->_M_parent = __y;
  157. }
  158. else
  159. __x_parent = __y;
  160. if (__root == __z)
  161. __root = __y;
  162. else if (__z->_M_parent->_M_left == __z)
  163. __z->_M_parent->_M_left = __y;
  164. else
  165. __z->_M_parent->_M_right = __y;
  166. __y->_M_parent = __z->_M_parent;
  167. _STLP_STD::swap(__y->_M_color, __z->_M_color);
  168. __y = __z;
  169. // __y now points to node to be actually deleted
  170. }
  171. else { // __y == __z
  172. __x_parent = __y->_M_parent;
  173. if (__x) __x->_M_parent = __y->_M_parent;
  174. if (__root == __z)
  175. __root = __x;
  176. else {
  177. if (__z->_M_parent->_M_left == __z)
  178. __z->_M_parent->_M_left = __x;
  179. else
  180. __z->_M_parent->_M_right = __x;
  181. }
  182. if (__leftmost == __z) {
  183. if (__z->_M_right == 0) // __z->_M_left must be null also
  184. __leftmost = __z->_M_parent;
  185. // makes __leftmost == _M_header if __z == __root
  186. else
  187. __leftmost = _Rb_tree_node_base::_S_minimum(__x);
  188. }
  189. if (__rightmost == __z) {
  190. if (__z->_M_left == 0) // __z->_M_right must be null also
  191. __rightmost = __z->_M_parent;
  192. // makes __rightmost == _M_header if __z == __root
  193. else // __x == __z->_M_left
  194. __rightmost = _Rb_tree_node_base::_S_maximum(__x);
  195. }
  196. }
  197. if (__y->_M_color != _S_rb_tree_red) {
  198. while (__x != __root && (__x == 0 || __x->_M_color == _S_rb_tree_black))
  199. if (__x == __x_parent->_M_left) {
  200. _Rb_tree_node_base* __w = __x_parent->_M_right;
  201. if (__w->_M_color == _S_rb_tree_red) {
  202. __w->_M_color = _S_rb_tree_black;
  203. __x_parent->_M_color = _S_rb_tree_red;
  204. _Rotate_left(__x_parent, __root);
  205. __w = __x_parent->_M_right;
  206. }
  207. if ((__w->_M_left == 0 ||
  208. __w->_M_left->_M_color == _S_rb_tree_black) && (__w->_M_right == 0 ||
  209. __w->_M_right->_M_color == _S_rb_tree_black)) {
  210. __w->_M_color = _S_rb_tree_red;
  211. __x = __x_parent;
  212. __x_parent = __x_parent->_M_parent;
  213. } else {
  214. if (__w->_M_right == 0 ||
  215. __w->_M_right->_M_color == _S_rb_tree_black) {
  216. if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
  217. __w->_M_color = _S_rb_tree_red;
  218. _Rotate_right(__w, __root);
  219. __w = __x_parent->_M_right;
  220. }
  221. __w->_M_color = __x_parent->_M_color;
  222. __x_parent->_M_color = _S_rb_tree_black;
  223. if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
  224. _Rotate_left(__x_parent, __root);
  225. break;
  226. }
  227. } else { // same as above, with _M_right <-> _M_left.
  228. _Rb_tree_node_base* __w = __x_parent->_M_left;
  229. if (__w->_M_color == _S_rb_tree_red) {
  230. __w->_M_color = _S_rb_tree_black;
  231. __x_parent->_M_color = _S_rb_tree_red;
  232. _Rotate_right(__x_parent, __root);
  233. __w = __x_parent->_M_left;
  234. }
  235. if ((__w->_M_right == 0 ||
  236. __w->_M_right->_M_color == _S_rb_tree_black) && (__w->_M_left == 0 ||
  237. __w->_M_left->_M_color == _S_rb_tree_black)) {
  238. __w->_M_color = _S_rb_tree_red;
  239. __x = __x_parent;
  240. __x_parent = __x_parent->_M_parent;
  241. } else {
  242. if (__w->_M_left == 0 ||
  243. __w->_M_left->_M_color == _S_rb_tree_black) {
  244. if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
  245. __w->_M_color = _S_rb_tree_red;
  246. _Rotate_left(__w, __root);
  247. __w = __x_parent->_M_left;
  248. }
  249. __w->_M_color = __x_parent->_M_color;
  250. __x_parent->_M_color = _S_rb_tree_black;
  251. if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
  252. _Rotate_right(__x_parent, __root);
  253. break;
  254. }
  255. }
  256. if (__x) __x->_M_color = _S_rb_tree_black;
  257. }
  258. return __y;
  259. }
  260. template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
  261. _Rb_global<_Dummy>::_M_decrement(_Rb_tree_node_base* _M_node) {
  262. if (_M_node->_M_color == _S_rb_tree_red && _M_node->_M_parent->_M_parent == _M_node)
  263. _M_node = _M_node->_M_right;
  264. else if (_M_node->_M_left != 0) {
  265. _M_node = _Rb_tree_node_base::_S_maximum(_M_node->_M_left);
  266. }
  267. else {
  268. _Base_ptr __y = _M_node->_M_parent;
  269. while (_M_node == __y->_M_left) {
  270. _M_node = __y;
  271. __y = __y->_M_parent;
  272. }
  273. _M_node = __y;
  274. }
  275. return _M_node;
  276. }
  277. template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
  278. _Rb_global<_Dummy>::_M_increment(_Rb_tree_node_base* _M_node) {
  279. if (_M_node->_M_right != 0) {
  280. _M_node = _Rb_tree_node_base::_S_minimum(_M_node->_M_right);
  281. }
  282. else {
  283. _Base_ptr __y = _M_node->_M_parent;
  284. while (_M_node == __y->_M_right) {
  285. _M_node = __y;
  286. __y = __y->_M_parent;
  287. }
  288. // check special case: This is necessary if _M_node is the
  289. // _M_head and the tree contains only a single node __y. In
  290. // that case parent, left and right all point to __y!
  291. if (_M_node->_M_right != __y)
  292. _M_node = __y;
  293. }
  294. return _M_node;
  295. }
  296. #endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
  297. template <class _Key, class _Compare,
  298. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  299. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>&
  300. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::operator=(
  301. const _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>& __x) {
  302. if (this != &__x) {
  303. // Note that _Key may be a constant type.
  304. clear();
  305. _M_node_count = 0;
  306. _M_key_compare = __x._M_key_compare;
  307. if (__x._M_root() == 0) {
  308. _M_root() = 0;
  309. _M_leftmost() = &this->_M_header._M_data;
  310. _M_rightmost() = &this->_M_header._M_data;
  311. }
  312. else {
  313. _M_root() = _M_copy(__x._M_root(), &this->_M_header._M_data);
  314. _M_leftmost() = _S_minimum(_M_root());
  315. _M_rightmost() = _S_maximum(_M_root());
  316. _M_node_count = __x._M_node_count;
  317. }
  318. }
  319. return *this;
  320. }
  321. // CRP 7/10/00 inserted argument __on_right, which is another hint (meant to
  322. // act like __on_left and ignore a portion of the if conditions -- specify
  323. // __on_right != 0 to bypass comparison as false or __on_left != 0 to bypass
  324. // comparison as true)
  325. template <class _Key, class _Compare,
  326. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  327. __iterator__
  328. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::_M_insert(_Rb_tree_node_base * __parent,
  329. const _Value& __val,
  330. _Rb_tree_node_base * __on_left,
  331. _Rb_tree_node_base * __on_right) {
  332. // We do not create the node here as, depending on tests, we might call
  333. // _M_key_compare that can throw an exception.
  334. _Base_ptr __new_node;
  335. if ( __parent == &this->_M_header._M_data ) {
  336. __new_node = _M_create_node(__val);
  337. _S_left(__parent) = __new_node; // also makes _M_leftmost() = __new_node
  338. _M_root() = __new_node;
  339. _M_rightmost() = __new_node;
  340. }
  341. else if ( __on_right == 0 && // If __on_right != 0, the remainder fails to false
  342. ( __on_left != 0 || // If __on_left != 0, the remainder succeeds to true
  343. _M_key_compare( _KeyOfValue()(__val), _S_key(__parent) ) ) ) {
  344. __new_node = _M_create_node(__val);
  345. _S_left(__parent) = __new_node;
  346. if (__parent == _M_leftmost())
  347. _M_leftmost() = __new_node; // maintain _M_leftmost() pointing to min node
  348. }
  349. else {
  350. __new_node = _M_create_node(__val);
  351. _S_right(__parent) = __new_node;
  352. if (__parent == _M_rightmost())
  353. _M_rightmost() = __new_node; // maintain _M_rightmost() pointing to max node
  354. }
  355. _S_parent(__new_node) = __parent;
  356. _Rb_global_inst::_Rebalance(__new_node, this->_M_header._M_data._M_parent);
  357. ++_M_node_count;
  358. return iterator(__new_node);
  359. }
  360. template <class _Key, class _Compare,
  361. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  362. __iterator__
  363. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_equal(const _Value& __val) {
  364. _Base_ptr __y = &this->_M_header._M_data;
  365. _Base_ptr __x = _M_root();
  366. while (__x != 0) {
  367. __y = __x;
  368. if (_M_key_compare(_KeyOfValue()(__val), _S_key(__x))) {
  369. __x = _S_left(__x);
  370. }
  371. else
  372. __x = _S_right(__x);
  373. }
  374. return _M_insert(__y, __val, __x);
  375. }
  376. template <class _Key, class _Compare,
  377. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  378. pair<__iterator__, bool>
  379. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_unique(const _Value& __val) {
  380. _Base_ptr __y = &this->_M_header._M_data;
  381. _Base_ptr __x = _M_root();
  382. bool __comp = true;
  383. while (__x != 0) {
  384. __y = __x;
  385. __comp = _M_key_compare(_KeyOfValue()(__val), _S_key(__x));
  386. __x = __comp ? _S_left(__x) : _S_right(__x);
  387. }
  388. iterator __j = iterator(__y);
  389. if (__comp) {
  390. if (__j == begin())
  391. return pair<iterator,bool>(_M_insert(__y, __val, /* __x*/ __y), true);
  392. else
  393. --__j;
  394. }
  395. if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__val))) {
  396. return pair<iterator,bool>(_M_insert(__y, __val, __x), true);
  397. }
  398. return pair<iterator,bool>(__j, false);
  399. }
  400. // Modifications CRP 7/10/00 as noted to improve conformance and
  401. // efficiency.
  402. template <class _Key, class _Compare,
  403. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  404. __iterator__
  405. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_unique(iterator __position,
  406. const _Value& __val) {
  407. if (__position._M_node == this->_M_header._M_data._M_left) { // begin()
  408. // if the container is empty, fall back on insert_unique.
  409. if (empty())
  410. return insert_unique(__val).first;
  411. if (_M_key_compare(_KeyOfValue()(__val), _S_key(__position._M_node))) {
  412. return _M_insert(__position._M_node, __val, __position._M_node);
  413. }
  414. // first argument just needs to be non-null
  415. else {
  416. bool __comp_pos_v = _M_key_compare( _S_key(__position._M_node), _KeyOfValue()(__val) );
  417. if (__comp_pos_v == false) // compare > and compare < both false so compare equal
  418. return __position;
  419. //Below __comp_pos_v == true
  420. // Standard-conformance - does the insertion point fall immediately AFTER
  421. // the hint?
  422. iterator __after = __position;
  423. ++__after;
  424. // Check for only one member -- in that case, __position points to itself,
  425. // and attempting to increment will cause an infinite loop.
  426. if (__after._M_node == &this->_M_header._M_data)
  427. // Check guarantees exactly one member, so comparison was already
  428. // performed and we know the result; skip repeating it in _M_insert
  429. // by specifying a non-zero fourth argument.
  430. return _M_insert(__position._M_node, __val, 0, __position._M_node);
  431. // All other cases:
  432. // Optimization to catch insert-equivalent -- save comparison results,
  433. // and we get this for free.
  434. if (_M_key_compare( _KeyOfValue()(__val), _S_key(__after._M_node) )) {
  435. if (_S_right(__position._M_node) == 0)
  436. return _M_insert(__position._M_node, __val, 0, __position._M_node);
  437. else
  438. return _M_insert(__after._M_node, __val, __after._M_node);
  439. }
  440. else {
  441. return insert_unique(__val).first;
  442. }
  443. }
  444. }
  445. else if (__position._M_node == &this->_M_header._M_data) { // end()
  446. if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__val))) {
  447. // pass along to _M_insert that it can skip comparing
  448. // v, Key ; since compare Key, v was true, compare v, Key must be false.
  449. return _M_insert(_M_rightmost(), __val, 0, __position._M_node); // Last argument only needs to be non-null
  450. }
  451. else
  452. return insert_unique(__val).first;
  453. }
  454. else {
  455. iterator __before = __position;
  456. --__before;
  457. bool __comp_v_pos = _M_key_compare(_KeyOfValue()(__val), _S_key(__position._M_node));
  458. if (__comp_v_pos
  459. && _M_key_compare( _S_key(__before._M_node), _KeyOfValue()(__val) )) {
  460. if (_S_right(__before._M_node) == 0)
  461. return _M_insert(__before._M_node, __val, 0, __before._M_node); // Last argument only needs to be non-null
  462. else
  463. return _M_insert(__position._M_node, __val, __position._M_node);
  464. // first argument just needs to be non-null
  465. }
  466. else {
  467. // Does the insertion point fall immediately AFTER the hint?
  468. iterator __after = __position;
  469. ++__after;
  470. // Optimization to catch equivalent cases and avoid unnecessary comparisons
  471. bool __comp_pos_v = !__comp_v_pos; // Stored this result earlier
  472. // If the earlier comparison was true, this comparison doesn't need to be
  473. // performed because it must be false. However, if the earlier comparison
  474. // was false, we need to perform this one because in the equal case, both will
  475. // be false.
  476. if (!__comp_v_pos) {
  477. __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__val));
  478. }
  479. if ( (!__comp_v_pos) // comp_v_pos true implies comp_v_pos false
  480. && __comp_pos_v
  481. && (__after._M_node == &this->_M_header._M_data ||
  482. _M_key_compare( _KeyOfValue()(__val), _S_key(__after._M_node) ))) {
  483. if (_S_right(__position._M_node) == 0)
  484. return _M_insert(__position._M_node, __val, 0, __position._M_node);
  485. else
  486. return _M_insert(__after._M_node, __val, __after._M_node);
  487. } else {
  488. // Test for equivalent case
  489. if (__comp_v_pos == __comp_pos_v)
  490. return __position;
  491. else
  492. return insert_unique(__val).first;
  493. }
  494. }
  495. }
  496. }
  497. template <class _Key, class _Compare,
  498. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  499. __iterator__
  500. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::insert_equal(iterator __position,
  501. const _Value& __val) {
  502. if (__position._M_node == this->_M_header._M_data._M_left) { // begin()
  503. // Check for zero members
  504. if (size() <= 0)
  505. return insert_equal(__val);
  506. if (!_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__val)))
  507. return _M_insert(__position._M_node, __val, __position._M_node);
  508. else {
  509. // Check for only one member
  510. if (__position._M_node->_M_left == __position._M_node)
  511. // Unlike insert_unique, can't avoid doing a comparison here.
  512. return _M_insert(__position._M_node, __val);
  513. // All other cases:
  514. // Standard-conformance - does the insertion point fall immediately AFTER
  515. // the hint?
  516. iterator __after = __position;
  517. ++__after;
  518. // Already know that compare(pos, v) must be true!
  519. // Therefore, we want to know if compare(after, v) is false.
  520. // (i.e., we now pos < v, now we want to know if v <= after)
  521. // If not, invalid hint.
  522. if ( __after._M_node == &this->_M_header._M_data ||
  523. !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__val) ) ) {
  524. if (_S_right(__position._M_node) == 0)
  525. return _M_insert(__position._M_node, __val, 0, __position._M_node);
  526. else
  527. return _M_insert(__after._M_node, __val, __after._M_node);
  528. }
  529. else { // Invalid hint
  530. return insert_equal(__val);
  531. }
  532. }
  533. }
  534. else if (__position._M_node == &this->_M_header._M_data) { // end()
  535. if (!_M_key_compare(_KeyOfValue()(__val), _S_key(_M_rightmost())))
  536. return _M_insert(_M_rightmost(), __val, 0, __position._M_node); // Last argument only needs to be non-null
  537. else {
  538. return insert_equal(__val);
  539. }
  540. }
  541. else {
  542. iterator __before = __position;
  543. --__before;
  544. // store the result of the comparison between pos and v so
  545. // that we don't have to do it again later. Note that this reverses the shortcut
  546. // on the if, possibly harming efficiency in comparisons; I think the harm will
  547. // be negligible, and to do what I want to do (save the result of a comparison so
  548. // that it can be re-used) there is no alternative. Test here is for before <= v <= pos.
  549. bool __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__val));
  550. if (!__comp_pos_v &&
  551. !_M_key_compare(_KeyOfValue()(__val), _S_key(__before._M_node))) {
  552. if (_S_right(__before._M_node) == 0)
  553. return _M_insert(__before._M_node, __val, 0, __before._M_node); // Last argument only needs to be non-null
  554. else
  555. return _M_insert(__position._M_node, __val, __position._M_node);
  556. }
  557. else {
  558. // Does the insertion point fall immediately AFTER the hint?
  559. // Test for pos < v <= after
  560. iterator __after = __position;
  561. ++__after;
  562. if (__comp_pos_v &&
  563. ( __after._M_node == &this->_M_header._M_data ||
  564. !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__val) ) ) ) {
  565. if (_S_right(__position._M_node) == 0)
  566. return _M_insert(__position._M_node, __val, 0, __position._M_node);
  567. else
  568. return _M_insert(__after._M_node, __val, __after._M_node);
  569. }
  570. else { // Invalid hint
  571. return insert_equal(__val);
  572. }
  573. }
  574. }
  575. }
  576. template <class _Key, class _Compare,
  577. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  578. _Rb_tree_node_base*
  579. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc> ::_M_copy(_Rb_tree_node_base* __x,
  580. _Rb_tree_node_base* __p) {
  581. // structural copy. __x and __p must be non-null.
  582. _Base_ptr __top = _M_clone_node(__x);
  583. _S_parent(__top) = __p;
  584. _STLP_TRY {
  585. if (_S_right(__x))
  586. _S_right(__top) = _M_copy(_S_right(__x), __top);
  587. __p = __top;
  588. __x = _S_left(__x);
  589. while (__x != 0) {
  590. _Base_ptr __y = _M_clone_node(__x);
  591. _S_left(__p) = __y;
  592. _S_parent(__y) = __p;
  593. if (_S_right(__x))
  594. _S_right(__y) = _M_copy(_S_right(__x), __y);
  595. __p = __y;
  596. __x = _S_left(__x);
  597. }
  598. }
  599. _STLP_UNWIND(_M_erase(__top))
  600. return __top;
  601. }
  602. // this has to stay out-of-line : it's recursive
  603. template <class _Key, class _Compare,
  604. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  605. void
  606. _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::_M_erase(_Rb_tree_node_base *__x) {
  607. // erase without rebalancing
  608. while (__x != 0) {
  609. _M_erase(_S_right(__x));
  610. _Base_ptr __y = _S_left(__x);
  611. _STLP_STD::_Destroy(&_S_value(__x));
  612. this->_M_header.deallocate(__STATIC_CAST(_Link_type, __x),1);
  613. __x = __y;
  614. }
  615. }
  616. #if defined (_STLP_DEBUG)
  617. inline int
  618. __black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root) {
  619. if (__node == 0)
  620. return 0;
  621. else {
  622. int __bc = __node->_M_color == _S_rb_tree_black ? 1 : 0;
  623. if (__node == __root)
  624. return __bc;
  625. else
  626. return __bc + __black_count(__node->_M_parent, __root);
  627. }
  628. }
  629. template <class _Key, class _Compare,
  630. class _Value, class _KeyOfValue, class _Traits, class _Alloc>
  631. bool _Rb_tree<_Key,_Compare,_Value,_KeyOfValue,_Traits,_Alloc>::__rb_verify() const {
  632. if (_M_node_count == 0 || begin() == end())
  633. return ((_M_node_count == 0) &&
  634. (begin() == end()) &&
  635. (this->_M_header._M_data._M_left == &this->_M_header._M_data) &&
  636. (this->_M_header._M_data._M_right == &this->_M_header._M_data));
  637. int __len = __black_count(_M_leftmost(), _M_root());
  638. for (const_iterator __it = begin(); __it != end(); ++__it) {
  639. _Base_ptr __x = __it._M_node;
  640. _Base_ptr __L = _S_left(__x);
  641. _Base_ptr __R = _S_right(__x);
  642. if (__x->_M_color == _S_rb_tree_red)
  643. if ((__L && __L->_M_color == _S_rb_tree_red) ||
  644. (__R && __R->_M_color == _S_rb_tree_red))
  645. return false;
  646. if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
  647. return false;
  648. if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
  649. return false;
  650. if (!__L && !__R && __black_count(__x, _M_root()) != __len)
  651. return false;
  652. }
  653. if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
  654. return false;
  655. if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
  656. return false;
  657. return true;
  658. }
  659. #endif /* _STLP_DEBUG */
  660. _STLP_MOVE_TO_STD_NAMESPACE
  661. _STLP_END_NAMESPACE
  662. #undef _Rb_tree
  663. #undef __iterator__
  664. #undef iterator
  665. #undef __size_type__
  666. #endif /* _STLP_TREE_C */
  667. // Local Variables:
  668. // mode:C++
  669. // End: