5 * Hewlett-Packard Company
7 * Copyright (c) 1996,1997
8 * Silicon Graphics Computer Systems, Inc.
11 * Moscow Center for SPARC Technology
16 * This material is provided "as is", with absolutely no warranty expressed
17 * or implied. Any use is at your own risk.
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.
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).
34 #ifndef _STLP_INTERNAL_TREE_H
35 # include <stl/_tree.h>
38 // fbp: these defines are for outline methods definitions.
39 // needed for definitions to be portable. Should not be used in method bodies.
40 # if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
41 # define __iterator__ _Rb_tree_iterator<_Value, _Nonconst_traits<_Value> >
42 # define __size_type__ size_t
43 # define iterator __iterator__
45 # define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>::iterator
46 # define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc>::size_type
49 #if defined ( _STLP_DEBUG)
50 # define _Rb_tree __WORKAROUND_DBG_RENAME(Rb_tree)
55 # if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
57 template <class _Dummy> void _STLP_CALL
58 _Rb_global<_Dummy>::_Rotate_left(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
60 _Rb_tree_node_base* __y = __x->_M_right;
61 __x->_M_right = __y->_M_left;
63 __y->_M_left->_M_parent = __x;
64 __y->_M_parent = __x->_M_parent;
68 else if (__x == __x->_M_parent->_M_left)
69 __x->_M_parent->_M_left = __y;
71 __x->_M_parent->_M_right = __y;
76 template <class _Dummy> void _STLP_CALL
77 _Rb_global<_Dummy>::_Rotate_right(_Rb_tree_node_base* __x, _Rb_tree_node_base*& __root)
79 _Rb_tree_node_base* __y = __x->_M_left;
80 __x->_M_left = __y->_M_right;
81 if (__y->_M_right != 0)
82 __y->_M_right->_M_parent = __x;
83 __y->_M_parent = __x->_M_parent;
87 else if (__x == __x->_M_parent->_M_right)
88 __x->_M_parent->_M_right = __y;
90 __x->_M_parent->_M_left = __y;
95 template <class _Dummy> void _STLP_CALL
96 _Rb_global<_Dummy>::_Rebalance(_Rb_tree_node_base* __x,
97 _Rb_tree_node_base*& __root)
99 __x->_M_color = _S_rb_tree_red;
100 while (__x != __root && __x->_M_parent->_M_color == _S_rb_tree_red) {
101 if (__x->_M_parent == __x->_M_parent->_M_parent->_M_left) {
102 _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_right;
103 if (__y && __y->_M_color == _S_rb_tree_red) {
104 __x->_M_parent->_M_color = _S_rb_tree_black;
105 __y->_M_color = _S_rb_tree_black;
106 __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
107 __x = __x->_M_parent->_M_parent;
110 if (__x == __x->_M_parent->_M_right) {
111 __x = __x->_M_parent;
112 _Rotate_left(__x, __root);
114 __x->_M_parent->_M_color = _S_rb_tree_black;
115 __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
116 _Rotate_right(__x->_M_parent->_M_parent, __root);
120 _Rb_tree_node_base* __y = __x->_M_parent->_M_parent->_M_left;
121 if (__y && __y->_M_color == _S_rb_tree_red) {
122 __x->_M_parent->_M_color = _S_rb_tree_black;
123 __y->_M_color = _S_rb_tree_black;
124 __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
125 __x = __x->_M_parent->_M_parent;
128 if (__x == __x->_M_parent->_M_left) {
129 __x = __x->_M_parent;
130 _Rotate_right(__x, __root);
132 __x->_M_parent->_M_color = _S_rb_tree_black;
133 __x->_M_parent->_M_parent->_M_color = _S_rb_tree_red;
134 _Rotate_left(__x->_M_parent->_M_parent, __root);
138 __root->_M_color = _S_rb_tree_black;
141 template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
142 _Rb_global<_Dummy>::_Rebalance_for_erase(_Rb_tree_node_base* __z,
143 _Rb_tree_node_base*& __root,
144 _Rb_tree_node_base*& __leftmost,
145 _Rb_tree_node_base*& __rightmost)
147 _Rb_tree_node_base* __y = __z;
148 _Rb_tree_node_base* __x = 0;
149 _Rb_tree_node_base* __x_parent = 0;
150 if (__y->_M_left == 0) // __z has at most one non-null child. y == z.
151 __x = __y->_M_right; // __x might be null.
153 if (__y->_M_right == 0) // __z has exactly one non-null child. y == z.
154 __x = __y->_M_left; // __x is not null.
155 else { // __z has two non-null children. Set __y to
156 __y = __y->_M_right; // __z's successor. __x might be null.
157 while (__y->_M_left != 0)
161 if (__y != __z) { // relink y in place of z. y is z's successor
162 __z->_M_left->_M_parent = __y;
163 __y->_M_left = __z->_M_left;
164 if (__y != __z->_M_right) {
165 __x_parent = __y->_M_parent;
166 if (__x) __x->_M_parent = __y->_M_parent;
167 __y->_M_parent->_M_left = __x; // __y must be a child of _M_left
168 __y->_M_right = __z->_M_right;
169 __z->_M_right->_M_parent = __y;
175 else if (__z->_M_parent->_M_left == __z)
176 __z->_M_parent->_M_left = __y;
178 __z->_M_parent->_M_right = __y;
179 __y->_M_parent = __z->_M_parent;
180 _STLP_STD::swap(__y->_M_color, __z->_M_color);
182 // __y now points to node to be actually deleted
185 __x_parent = __y->_M_parent;
186 if (__x) __x->_M_parent = __y->_M_parent;
190 if (__z->_M_parent->_M_left == __z)
191 __z->_M_parent->_M_left = __x;
193 __z->_M_parent->_M_right = __x;
194 if (__leftmost == __z)
195 if (__z->_M_right == 0) // __z->_M_left must be null also
196 __leftmost = __z->_M_parent;
197 // makes __leftmost == _M_header if __z == __root
199 __leftmost = _Rb_tree_node_base::_S_minimum(__x);
200 if (__rightmost == __z)
201 if (__z->_M_left == 0) // __z->_M_right must be null also
202 __rightmost = __z->_M_parent;
203 // makes __rightmost == _M_header if __z == __root
204 else // __x == __z->_M_left
205 __rightmost = _Rb_tree_node_base::_S_maximum(__x);
207 if (__y->_M_color != _S_rb_tree_red) {
208 while (__x != __root && (__x == 0 || __x->_M_color == _S_rb_tree_black))
209 if (__x == __x_parent->_M_left) {
210 _Rb_tree_node_base* __w = __x_parent->_M_right;
211 if (__w->_M_color == _S_rb_tree_red) {
212 __w->_M_color = _S_rb_tree_black;
213 __x_parent->_M_color = _S_rb_tree_red;
214 _Rotate_left(__x_parent, __root);
215 __w = __x_parent->_M_right;
217 if ((__w->_M_left == 0 ||
218 __w->_M_left->_M_color == _S_rb_tree_black) && (__w->_M_right == 0 ||
219 __w->_M_right->_M_color == _S_rb_tree_black)) {
220 __w->_M_color = _S_rb_tree_red;
222 __x_parent = __x_parent->_M_parent;
224 if (__w->_M_right == 0 ||
225 __w->_M_right->_M_color == _S_rb_tree_black) {
226 if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
227 __w->_M_color = _S_rb_tree_red;
228 _Rotate_right(__w, __root);
229 __w = __x_parent->_M_right;
231 __w->_M_color = __x_parent->_M_color;
232 __x_parent->_M_color = _S_rb_tree_black;
233 if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
234 _Rotate_left(__x_parent, __root);
237 } else { // same as above, with _M_right <-> _M_left.
238 _Rb_tree_node_base* __w = __x_parent->_M_left;
239 if (__w->_M_color == _S_rb_tree_red) {
240 __w->_M_color = _S_rb_tree_black;
241 __x_parent->_M_color = _S_rb_tree_red;
242 _Rotate_right(__x_parent, __root);
243 __w = __x_parent->_M_left;
245 if ((__w->_M_right == 0 ||
246 __w->_M_right->_M_color == _S_rb_tree_black) && (__w->_M_left == 0 ||
247 __w->_M_left->_M_color == _S_rb_tree_black)) {
248 __w->_M_color = _S_rb_tree_red;
250 __x_parent = __x_parent->_M_parent;
252 if (__w->_M_left == 0 ||
253 __w->_M_left->_M_color == _S_rb_tree_black) {
254 if (__w->_M_right) __w->_M_right->_M_color = _S_rb_tree_black;
255 __w->_M_color = _S_rb_tree_red;
256 _Rotate_left(__w, __root);
257 __w = __x_parent->_M_left;
259 __w->_M_color = __x_parent->_M_color;
260 __x_parent->_M_color = _S_rb_tree_black;
261 if (__w->_M_left) __w->_M_left->_M_color = _S_rb_tree_black;
262 _Rotate_right(__x_parent, __root);
266 if (__x) __x->_M_color = _S_rb_tree_black;
271 template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
272 _Rb_global<_Dummy>::_M_decrement(_Rb_tree_node_base* _M_node)
274 if (_M_node->_M_color == _S_rb_tree_red && _M_node->_M_parent->_M_parent == _M_node)
275 _M_node = _M_node->_M_right;
276 else if (_M_node->_M_left != 0) {
277 _Base_ptr __y = _M_node->_M_left;
278 while (__y->_M_right != 0)
283 _Base_ptr __y = _M_node->_M_parent;
284 while (_M_node == __y->_M_left) {
286 __y = __y->_M_parent;
293 template <class _Dummy> _Rb_tree_node_base* _STLP_CALL
294 _Rb_global<_Dummy>::_M_increment(_Rb_tree_node_base* _M_node)
296 if (_M_node->_M_right != 0) {
297 _M_node = _M_node->_M_right;
298 while (_M_node->_M_left != 0)
299 _M_node = _M_node->_M_left;
302 _Base_ptr __y = _M_node->_M_parent;
303 while (_M_node == __y->_M_right) {
305 __y = __y->_M_parent;
307 if (_M_node->_M_right != __y)
313 #endif /* defined (__BUILDING_STLPORT) || ! defined (_STLP_OWN_IOSTREAMS) */
316 template <class _Key, class _Value, class _KeyOfValue,
317 class _Compare, class _Alloc> _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::operator=(const _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>& __x)
320 // Note that _Key may be a constant type.
323 _M_key_compare = __x._M_key_compare;
324 if (__x._M_root() == 0) {
326 _M_leftmost() = this->_M_header._M_data;
327 _M_rightmost() = this->_M_header._M_data;
330 _M_root() = _M_copy(__x._M_root(), this->_M_header._M_data);
331 _M_leftmost() = _S_minimum(_M_root());
332 _M_rightmost() = _S_maximum(_M_root());
333 _M_node_count = __x._M_node_count;
339 // CRP 7/10/00 inserted argument __w_, which is another hint (meant to
340 // act like __x_ and ignore a portion of the if conditions -- specify
341 // __w_ != 0 to bypass comparison as false or __x_ != 0 to bypass
342 // comparison as true)
343 template <class _Key, class _Value, class _KeyOfValue,
344 class _Compare, class _Alloc> __iterator__
345 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::_M_insert(_Rb_tree_node_base* __x_, _Rb_tree_node_base* __y_, const _Value& __v,
346 _Rb_tree_node_base* __w_)
348 _Link_type __w = (_Link_type) __w_;
349 _Link_type __x = (_Link_type) __x_;
350 _Link_type __y = (_Link_type) __y_;
353 if ( __y == this->_M_header._M_data ||
354 ( __w == 0 && // If w != 0, the remainder fails to false
355 ( __x != 0 || // If x != 0, the remainder succeeds to true
356 _M_key_compare( _KeyOfValue()(__v), _S_key(__y) ) )
360 __z = _M_create_node(__v);
361 _S_left(__y) = __z; // also makes _M_leftmost() = __z
362 // when __y == _M_header
363 if (__y == this->_M_header._M_data) {
365 _M_rightmost() = __z;
367 else if (__y == _M_leftmost())
368 _M_leftmost() = __z; // maintain _M_leftmost() pointing to min node
371 __z = _M_create_node(__v);
373 if (__y == _M_rightmost())
374 _M_rightmost() = __z; // maintain _M_rightmost() pointing to max node
376 _S_parent(__z) = __y;
379 _Rb_global_inst::_Rebalance(__z, this->_M_header._M_data->_M_parent);
381 return iterator(__z);
384 template <class _Key, class _Value, class _KeyOfValue,
385 class _Compare, class _Alloc> __iterator__
386 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_equal(const _Value& __v)
388 _Link_type __y = this->_M_header._M_data;
389 _Link_type __x = _M_root();
392 __x = _M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
393 _S_left(__x) : _S_right(__x);
395 return _M_insert(__x, __y, __v);
399 template <class _Key, class _Value, class _KeyOfValue,
400 class _Compare, class _Alloc> pair< _Rb_tree_iterator<_Value, _Nonconst_traits<_Value> >, bool> _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_unique(const _Value& __v)
402 _Link_type __y = this->_M_header._M_data;
403 _Link_type __x = _M_root();
407 __comp = _M_key_compare(_KeyOfValue()(__v), _S_key(__x));
408 __x = __comp ? _S_left(__x) : _S_right(__x);
410 iterator __j = iterator(__y);
413 return pair<iterator,bool>(_M_insert(/* __x*/ __y, __y, __v), true);
416 if (_M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
417 return pair<iterator,bool>(_M_insert(__x, __y, __v), true);
418 return pair<iterator,bool>(__j, false);
421 // Modifications CRP 7/10/00 as noted to improve conformance and
423 template <class _Key, class _Value, class _KeyOfValue,
424 class _Compare, class _Alloc> __iterator__
425 _Rb_tree<_Key, _Value, _KeyOfValue, _Compare, _Alloc> ::insert_unique(iterator __position, const _Value& __v)
427 if (__position._M_node == this->_M_header._M_data->_M_left) { // begin()
429 // if the container is empty, fall back on insert_unique.
431 return insert_unique(__v).first;
433 if ( _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node)))
434 return _M_insert(__position._M_node, __position._M_node, __v);
435 // first argument just needs to be non-null
438 bool __comp_pos_v = _M_key_compare( _S_key(__position._M_node), _KeyOfValue()(__v) );
440 if (__comp_pos_v == false) // compare > and compare < both false so compare equal
442 //Below __comp_pos_v == true
444 // Standard-conformance - does the insertion point fall immediately AFTER
446 iterator __after = __position;
449 // Check for only one member -- in that case, __position points to itself,
450 // and attempting to increment will cause an infinite loop.
451 if (__after._M_node == this->_M_header._M_data)
452 // Check guarantees exactly one member, so comparison was already
453 // performed and we know the result; skip repeating it in _M_insert
454 // by specifying a non-zero fourth argument.
455 return _M_insert(0, __position._M_node, __v, __position._M_node);
460 // Optimization to catch insert-equivalent -- save comparison results,
461 // and we get this for free.
462 if(_M_key_compare( _KeyOfValue()(__v), _S_key(__after._M_node) )) {
463 if (_S_right(__position._M_node) == 0)
464 return _M_insert(0, __position._M_node, __v, __position._M_node);
466 return _M_insert(__after._M_node, __after._M_node, __v);
468 return insert_unique(__v).first;
472 } else if (__position._M_node == this->_M_header._M_data) { // end()
473 if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v)))
474 // pass along to _M_insert that it can skip comparing
475 // v, Key ; since compare Key, v was true, compare v, Key must be false.
476 return _M_insert(0, _M_rightmost(), __v, __position._M_node); // Last argument only needs to be non-null
478 return insert_unique(__v).first;
480 iterator __before = __position;
483 bool __comp_v_pos = _M_key_compare(_KeyOfValue()(__v), _S_key(__position._M_node));
486 && _M_key_compare( _S_key(__before._M_node), _KeyOfValue()(__v) )) {
488 if (_S_right(__before._M_node) == 0)
489 return _M_insert(0, __before._M_node, __v, __before._M_node); // Last argument only needs to be non-null
491 return _M_insert(__position._M_node, __position._M_node, __v);
492 // first argument just needs to be non-null
495 // Does the insertion point fall immediately AFTER the hint?
496 iterator __after = __position;
499 // Optimization to catch equivalent cases and avoid unnecessary comparisons
500 bool __comp_pos_v = !__comp_v_pos; // Stored this result earlier
501 // If the earlier comparison was true, this comparison doesn't need to be
502 // performed because it must be false. However, if the earlier comparison
503 // was false, we need to perform this one because in the equal case, both will
505 if (!__comp_v_pos) __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v));
507 if ( (!__comp_v_pos) // comp_v_pos true implies comp_v_pos false
509 && (__after._M_node == this->_M_header._M_data ||
510 _M_key_compare( _KeyOfValue()(__v), _S_key(__after._M_node) ))) {
512 if (_S_right(__position._M_node) == 0)
513 return _M_insert(0, __position._M_node, __v, __position._M_node);
515 return _M_insert(__after._M_node, __after._M_node, __v);
517 // Test for equivalent case
518 if (__comp_v_pos == __comp_pos_v)
521 return insert_unique(__v).first;
528 template <class _Key, class _Value, class _KeyOfValue,
529 class _Compare, class _Alloc> __iterator__
530 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::insert_equal(iterator __position, const _Value& __v)
532 if (__position._M_node == this->_M_header._M_data->_M_left) { // begin()
534 // Check for zero members
536 return insert_equal(__v);
538 if (!_M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v)))
539 return _M_insert(__position._M_node, __position._M_node, __v);
541 // Check for only one member
542 if (__position._M_node->_M_left == __position._M_node)
543 // Unlike insert_unique, can't avoid doing a comparison here.
544 return _M_insert(0, __position._M_node, __v);
547 // Standard-conformance - does the insertion point fall immediately AFTER
549 iterator __after = __position;
552 // Already know that compare(pos, v) must be true!
553 // Therefore, we want to know if compare(after, v) is false.
554 // (i.e., we now pos < v, now we want to know if v <= after)
555 // If not, invalid hint.
556 if ( __after._M_node==this->_M_header._M_data ||
557 !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__v) ) ) {
558 if (_S_right(__position._M_node) == 0)
559 return _M_insert(0, __position._M_node, __v, __position._M_node);
561 return _M_insert(__after._M_node, __after._M_node, __v);
562 } else // Invalid hint
563 return insert_equal(__v);
565 } else if (__position._M_node == this->_M_header._M_data) {// end()
566 if (!_M_key_compare(_KeyOfValue()(__v), _S_key(_M_rightmost())))
567 return _M_insert(0, _M_rightmost(), __v, __position._M_node); // Last argument only needs to be non-null
569 return insert_equal(__v);
571 iterator __before = __position;
573 // store the result of the comparison between pos and v so
574 // that we don't have to do it again later. Note that this reverses the shortcut
575 // on the if, possibly harming efficiency in comparisons; I think the harm will
576 // be negligible, and to do what I want to do (save the result of a comparison so
577 // that it can be re-used) there is no alternative. Test here is for before <= v <= pos.
578 bool __comp_pos_v = _M_key_compare(_S_key(__position._M_node), _KeyOfValue()(__v));
580 && !_M_key_compare(_KeyOfValue()(__v), _S_key(__before._M_node))) {
581 if (_S_right(__before._M_node) == 0)
582 return _M_insert(0, __before._M_node, __v, __before._M_node); // Last argument only needs to be non-null
584 return _M_insert(__position._M_node, __position._M_node, __v);
586 // Does the insertion point fall immediately AFTER the hint?
587 // Test for pos < v <= after
588 iterator __after = __position;
592 && ( __after._M_node==this->_M_header._M_data
593 || !_M_key_compare( _S_key(__after._M_node), _KeyOfValue()(__v) ) ) ) {
594 if (_S_right(__position._M_node) == 0)
595 return _M_insert(0, __position._M_node, __v, __position._M_node);
597 return _M_insert(__after._M_node, __after._M_node, __v);
598 } else // Invalid hint
599 return insert_equal(__v);
604 template <class _Key, class _Value, class _KeyOfValue, class _Compare, class _Alloc> _Rb_tree_node<_Value>*
605 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::_M_copy(_Rb_tree_node<_Value>* __x, _Rb_tree_node<_Value>* __p)
607 // structural copy. __x and __p must be non-null.
608 _STLP_LEAVE_VOLATILE _Link_type __top = _M_clone_node(__x);
609 __top->_M_parent = __p;
613 __top->_M_right = _M_copy(_S_right(__x), __top);
618 _Link_type __y = _M_clone_node(__x);
620 __y->_M_parent = __p;
622 __y->_M_right = _M_copy(_S_right(__x), __y);
627 _STLP_UNWIND(_M_erase(__top));
632 // this has to stay out-of-line : it's recursive
633 template <class _Key, class _Value, class _KeyOfValue,
634 class _Compare, class _Alloc> void
635 _Rb_tree<_Key,_Value,_KeyOfValue,
636 _Compare,_Alloc>::_M_erase(_Rb_tree_node<_Value>* __x)
638 // erase without rebalancing
640 _M_erase(_S_right(__x));
641 _Link_type __y = _S_left(__x);
642 _STLP_STD::_Destroy(&__x->_M_value_field);
643 this->_M_header.deallocate(__x,1);
648 template <class _Key, class _Value, class _KeyOfValue,
649 class _Compare, class _Alloc> __size_type__
650 _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc> ::count(const _Key& __k) const
652 pair<const_iterator, const_iterator> __p = equal_range(__k);
653 size_type __n = distance(__p.first, __p.second);
658 __black_count(_Rb_tree_node_base* __node, _Rb_tree_node_base* __root)
663 int __bc = __node->_M_color == _S_rb_tree_black ? 1 : 0;
664 if (__node == __root)
667 return __bc + __black_count(__node->_M_parent, __root);
671 template <class _Key, class _Value, class _KeyOfValue,
672 class _Compare, class _Alloc> bool _Rb_tree<_Key,_Value,_KeyOfValue,_Compare,_Alloc>::__rb_verify() const
674 if (_M_node_count == 0 || begin() == end())
675 return _M_node_count == 0 && begin() == end() && this->_M_header._M_data->_M_left == this->_M_header._M_data
676 && this->_M_header._M_data->_M_right == this->_M_header._M_data;
678 int __len = __black_count(_M_leftmost(), _M_root());
679 for (const_iterator __it = begin(); __it != end(); ++__it) {
680 _Link_type __x = (_Link_type) __it._M_node;
681 _Link_type __L = _S_left(__x);
682 _Link_type __R = _S_right(__x);
684 if (__x->_M_color == _S_rb_tree_red)
685 if ((__L && __L->_M_color == _S_rb_tree_red) ||
686 (__R && __R->_M_color == _S_rb_tree_red))
689 if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
691 if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
694 if (!__L && !__R && __black_count(__x, _M_root()) != __len)
698 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
700 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
709 # undef __size_type__
711 #endif /* _STLP_TREE_C */