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 #if defined (_STLP_DEBUG)
39 # define _Rb_tree _STLP_NON_DBG_NAME(Rb_tree)
42 // fbp: these defines are for outline methods definitions.
43 // needed for definitions to be portable. Should not be used in method bodies.
44 #if defined (_STLP_NESTED_TYPE_PARAM_BUG)
45 # define __iterator__ _Rb_tree_iterator<_Value, _STLP_HEADER_TYPENAME _Traits::_NonConstTraits>
46 # define __size_type__ size_t
47 # define iterator __iterator__
49 # define __iterator__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc>::iterator
50 # define __size_type__ _STLP_TYPENAME_ON_RETURN_TYPE _Rb_tree<_Key, _Compare, _Value, _KeyOfValue, _Traits, _Alloc>::size_type
55 _STLP_MOVE_TO_PRIV_NAMESPACE
57 #if defined (_STLP_EXPOSE_GLOBALS_IMPLEMENTATION)
59 template <class _Dummy
> void _STLP_CALL
60 _Rb_global
<_Dummy
>::_Rotate_left(_Rb_tree_node_base
* __x
,
61 _Rb_tree_node_base
*& __root
) {
62 _Rb_tree_node_base
* __y
= __x
->_M_right
;
63 __x
->_M_right
= __y
->_M_left
;
64 if (__y
->_M_left
!= 0)
65 __y
->_M_left
->_M_parent
= __x
;
66 __y
->_M_parent
= __x
->_M_parent
;
70 else if (__x
== __x
->_M_parent
->_M_left
)
71 __x
->_M_parent
->_M_left
= __y
;
73 __x
->_M_parent
->_M_right
= __y
;
78 template <class _Dummy
> void _STLP_CALL
79 _Rb_global
<_Dummy
>::_Rotate_right(_Rb_tree_node_base
* __x
,
80 _Rb_tree_node_base
*& __root
) {
81 _Rb_tree_node_base
* __y
= __x
->_M_left
;
82 __x
->_M_left
= __y
->_M_right
;
83 if (__y
->_M_right
!= 0)
84 __y
->_M_right
->_M_parent
= __x
;
85 __y
->_M_parent
= __x
->_M_parent
;
89 else if (__x
== __x
->_M_parent
->_M_right
)
90 __x
->_M_parent
->_M_right
= __y
;
92 __x
->_M_parent
->_M_left
= __y
;
97 template <class _Dummy
> void _STLP_CALL
98 _Rb_global
<_Dummy
>::_Rebalance(_Rb_tree_node_base
* __x
,
99 _Rb_tree_node_base
*& __root
) {
100 __x
->_M_color
= _S_rb_tree_red
;
101 while (__x
!= __root
&& __x
->_M_parent
->_M_color
== _S_rb_tree_red
) {
102 if (__x
->_M_parent
== __x
->_M_parent
->_M_parent
->_M_left
) {
103 _Rb_tree_node_base
* __y
= __x
->_M_parent
->_M_parent
->_M_right
;
104 if (__y
&& __y
->_M_color
== _S_rb_tree_red
) {
105 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
106 __y
->_M_color
= _S_rb_tree_black
;
107 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
108 __x
= __x
->_M_parent
->_M_parent
;
111 if (__x
== __x
->_M_parent
->_M_right
) {
112 __x
= __x
->_M_parent
;
113 _Rotate_left(__x
, __root
);
115 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
116 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
117 _Rotate_right(__x
->_M_parent
->_M_parent
, __root
);
121 _Rb_tree_node_base
* __y
= __x
->_M_parent
->_M_parent
->_M_left
;
122 if (__y
&& __y
->_M_color
== _S_rb_tree_red
) {
123 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
124 __y
->_M_color
= _S_rb_tree_black
;
125 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
126 __x
= __x
->_M_parent
->_M_parent
;
129 if (__x
== __x
->_M_parent
->_M_left
) {
130 __x
= __x
->_M_parent
;
131 _Rotate_right(__x
, __root
);
133 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
134 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
135 _Rotate_left(__x
->_M_parent
->_M_parent
, __root
);
139 __root
->_M_color
= _S_rb_tree_black
;
142 template <class _Dummy
> _Rb_tree_node_base
* _STLP_CALL
143 _Rb_global
<_Dummy
>::_Rebalance_for_erase(_Rb_tree_node_base
* __z
,
144 _Rb_tree_node_base
*& __root
,
145 _Rb_tree_node_base
*& __leftmost
,
146 _Rb_tree_node_base
*& __rightmost
) {
147 _Rb_tree_node_base
* __y
= __z
;
148 _Rb_tree_node_base
* __x
;
149 _Rb_tree_node_base
* __x_parent
;
151 if (__y
->_M_left
== 0) // __z has at most one non-null child. y == z.
152 __x
= __y
->_M_right
; // __x might be null.
154 if (__y
->_M_right
== 0) // __z has exactly one non-null child. y == z.
155 __x
= __y
->_M_left
; // __x is not null.
156 else { // __z has two non-null children. Set __y to
157 __y
= _Rb_tree_node_base::_S_minimum(__y
->_M_right
); // __z's successor. __x might be null.
162 if (__y
!= __z
) { // relink y in place of z. y is z's successor
163 __z
->_M_left
->_M_parent
= __y
;
164 __y
->_M_left
= __z
->_M_left
;
165 if (__y
!= __z
->_M_right
) {
166 __x_parent
= __y
->_M_parent
;
167 if (__x
) __x
->_M_parent
= __y
->_M_parent
;
168 __y
->_M_parent
->_M_left
= __x
; // __y must be a child of _M_left
169 __y
->_M_right
= __z
->_M_right
;
170 __z
->_M_right
->_M_parent
= __y
;
176 else if (__z
->_M_parent
->_M_left
== __z
)
177 __z
->_M_parent
->_M_left
= __y
;
179 __z
->_M_parent
->_M_right
= __y
;
180 __y
->_M_parent
= __z
->_M_parent
;
181 _STLP_STD::swap(__y
->_M_color
, __z
->_M_color
);
183 // __y now points to node to be actually deleted
186 __x_parent
= __y
->_M_parent
;
187 if (__x
) __x
->_M_parent
= __y
->_M_parent
;
191 if (__z
->_M_parent
->_M_left
== __z
)
192 __z
->_M_parent
->_M_left
= __x
;
194 __z
->_M_parent
->_M_right
= __x
;
197 if (__leftmost
== __z
) {
198 if (__z
->_M_right
== 0) // __z->_M_left must be null also
199 __leftmost
= __z
->_M_parent
;
200 // makes __leftmost == _M_header if __z == __root
202 __leftmost
= _Rb_tree_node_base::_S_minimum(__x
);
204 if (__rightmost
== __z
) {
205 if (__z
->_M_left
== 0) // __z->_M_right must be null also
206 __rightmost
= __z
->_M_parent
;
207 // makes __rightmost == _M_header if __z == __root
208 else // __x == __z->_M_left
209 __rightmost
= _Rb_tree_node_base::_S_maximum(__x
);
213 if (__y
->_M_color
!= _S_rb_tree_red
) {
214 while (__x
!= __root
&& (__x
== 0 || __x
->_M_color
== _S_rb_tree_black
))
215 if (__x
== __x_parent
->_M_left
) {
216 _Rb_tree_node_base
* __w
= __x_parent
->_M_right
;
217 if (__w
->_M_color
== _S_rb_tree_red
) {
218 __w
->_M_color
= _S_rb_tree_black
;
219 __x_parent
->_M_color
= _S_rb_tree_red
;
220 _Rotate_left(__x_parent
, __root
);
221 __w
= __x_parent
->_M_right
;
223 if ((__w
->_M_left
== 0 ||
224 __w
->_M_left
->_M_color
== _S_rb_tree_black
) && (__w
->_M_right
== 0 ||
225 __w
->_M_right
->_M_color
== _S_rb_tree_black
)) {
226 __w
->_M_color
= _S_rb_tree_red
;
228 __x_parent
= __x_parent
->_M_parent
;
230 if (__w
->_M_right
== 0 ||
231 __w
->_M_right
->_M_color
== _S_rb_tree_black
) {
232 if (__w
->_M_left
) __w
->_M_left
->_M_color
= _S_rb_tree_black
;
233 __w
->_M_color
= _S_rb_tree_red
;
234 _Rotate_right(__w
, __root
);
235 __w
= __x_parent
->_M_right
;
237 __w
->_M_color
= __x_parent
->_M_color
;
238 __x_parent
->_M_color
= _S_rb_tree_black
;
239 if (__w
->_M_right
) __w
->_M_right
->_M_color
= _S_rb_tree_black
;
240 _Rotate_left(__x_parent
, __root
);
243 } else { // same as above, with _M_right <-> _M_left.
244 _Rb_tree_node_base
* __w
= __x_parent
->_M_left
;
245 if (__w
->_M_color
== _S_rb_tree_red
) {
246 __w
->_M_color
= _S_rb_tree_black
;
247 __x_parent
->_M_color
= _S_rb_tree_red
;
248 _Rotate_right(__x_parent
, __root
);
249 __w
= __x_parent
->_M_left
;
251 if ((__w
->_M_right
== 0 ||
252 __w
->_M_right
->_M_color
== _S_rb_tree_black
) && (__w
->_M_left
== 0 ||
253 __w
->_M_left
->_M_color
== _S_rb_tree_black
)) {
254 __w
->_M_color
= _S_rb_tree_red
;
256 __x_parent
= __x_parent
->_M_parent
;
258 if (__w
->_M_left
== 0 ||
259 __w
->_M_left
->_M_color
== _S_rb_tree_black
) {
260 if (__w
->_M_right
) __w
->_M_right
->_M_color
= _S_rb_tree_black
;
261 __w
->_M_color
= _S_rb_tree_red
;
262 _Rotate_left(__w
, __root
);
263 __w
= __x_parent
->_M_left
;
265 __w
->_M_color
= __x_parent
->_M_color
;
266 __x_parent
->_M_color
= _S_rb_tree_black
;
267 if (__w
->_M_left
) __w
->_M_left
->_M_color
= _S_rb_tree_black
;
268 _Rotate_right(__x_parent
, __root
);
272 if (__x
) __x
->_M_color
= _S_rb_tree_black
;
277 template <class _Dummy
> _Rb_tree_node_base
* _STLP_CALL
278 _Rb_global
<_Dummy
>::_M_decrement(_Rb_tree_node_base
* _M_node
) {
279 if (_M_node
->_M_color
== _S_rb_tree_red
&& _M_node
->_M_parent
->_M_parent
== _M_node
)
280 _M_node
= _M_node
->_M_right
;
281 else if (_M_node
->_M_left
!= 0) {
282 _M_node
= _Rb_tree_node_base::_S_maximum(_M_node
->_M_left
);
285 _Base_ptr __y
= _M_node
->_M_parent
;
286 while (_M_node
== __y
->_M_left
) {
288 __y
= __y
->_M_parent
;
295 template <class _Dummy
> _Rb_tree_node_base
* _STLP_CALL
296 _Rb_global
<_Dummy
>::_M_increment(_Rb_tree_node_base
* _M_node
) {
297 if (_M_node
->_M_right
!= 0) {
298 _M_node
= _Rb_tree_node_base::_S_minimum(_M_node
->_M_right
);
301 _Base_ptr __y
= _M_node
->_M_parent
;
302 while (_M_node
== __y
->_M_right
) {
304 __y
= __y
->_M_parent
;
306 // check special case: This is necessary if _M_node is the
307 // _M_head and the tree contains only a single node __y. In
308 // that case parent, left and right all point to __y!
309 if (_M_node
->_M_right
!= __y
)
315 #endif /* _STLP_EXPOSE_GLOBALS_IMPLEMENTATION */
318 template <class _Key
, class _Compare
,
319 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
320 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
>&
321 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::operator=(
322 const _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
>& __x
) {
324 // Note that _Key may be a constant type.
327 _M_key_compare
= __x
._M_key_compare
;
328 if (__x
._M_root() == 0) {
330 _M_leftmost() = &this->_M_header
._M_data
;
331 _M_rightmost() = &this->_M_header
._M_data
;
334 _M_root() = _M_copy(__x
._M_root(), &this->_M_header
._M_data
);
335 _M_leftmost() = _S_minimum(_M_root());
336 _M_rightmost() = _S_maximum(_M_root());
337 _M_node_count
= __x
._M_node_count
;
343 // CRP 7/10/00 inserted argument __on_right, which is another hint (meant to
344 // act like __on_left and ignore a portion of the if conditions -- specify
345 // __on_right != 0 to bypass comparison as false or __on_left != 0 to bypass
346 // comparison as true)
347 template <class _Key
, class _Compare
,
348 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
350 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::_M_insert(_Rb_tree_node_base
* __parent
,
352 _Rb_tree_node_base
* __on_left
,
353 _Rb_tree_node_base
* __on_right
) {
354 // We do not create the node here as, depending on tests, we might call
355 // _M_key_compare that can throw an exception.
356 _Base_ptr __new_node
;
358 if ( __parent
== &this->_M_header
._M_data
) {
359 __new_node
= _M_create_node(__val
);
360 _S_left(__parent
) = __new_node
; // also makes _M_leftmost() = __new_node
361 _M_root() = __new_node
;
362 _M_rightmost() = __new_node
;
364 else if ( __on_right
== 0 && // If __on_right != 0, the remainder fails to false
365 ( __on_left
!= 0 || // If __on_left != 0, the remainder succeeds to true
366 _M_key_compare( _KeyOfValue()(__val
), _S_key(__parent
) ) ) ) {
367 __new_node
= _M_create_node(__val
);
368 _S_left(__parent
) = __new_node
;
369 if (__parent
== _M_leftmost())
370 _M_leftmost() = __new_node
; // maintain _M_leftmost() pointing to min node
373 __new_node
= _M_create_node(__val
);
374 _S_right(__parent
) = __new_node
;
375 if (__parent
== _M_rightmost())
376 _M_rightmost() = __new_node
; // maintain _M_rightmost() pointing to max node
378 _S_parent(__new_node
) = __parent
;
379 _Rb_global_inst::_Rebalance(__new_node
, this->_M_header
._M_data
._M_parent
);
381 return iterator(__new_node
);
384 template <class _Key
, class _Compare
,
385 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
387 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::insert_equal(const _Value
& __val
) {
388 _Base_ptr __y
= &this->_M_header
._M_data
;
389 _Base_ptr __x
= _M_root();
392 if (_M_key_compare(_KeyOfValue()(__val
), _S_key(__x
))) {
398 return _M_insert(__y
, __val
, __x
);
402 template <class _Key
, class _Compare
,
403 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
404 pair
<__iterator__
, bool>
405 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::insert_unique(const _Value
& __val
) {
406 _Base_ptr __y
= &this->_M_header
._M_data
;
407 _Base_ptr __x
= _M_root();
411 __comp
= _M_key_compare(_KeyOfValue()(__val
), _S_key(__x
));
412 __x
= __comp
? _S_left(__x
) : _S_right(__x
);
414 iterator __j
= iterator(__y
);
417 return pair
<iterator
,bool>(_M_insert(__y
, __val
, /* __x*/ __y
), true);
421 if (_M_key_compare(_S_key(__j
._M_node
), _KeyOfValue()(__val
))) {
422 return pair
<iterator
,bool>(_M_insert(__y
, __val
, __x
), true);
424 return pair
<iterator
,bool>(__j
, false);
427 // Modifications CRP 7/10/00 as noted to improve conformance and
429 template <class _Key
, class _Compare
,
430 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
432 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::insert_unique(iterator __position
,
433 const _Value
& __val
) {
434 if (__position
._M_node
== this->_M_header
._M_data
._M_left
) { // begin()
436 // if the container is empty, fall back on insert_unique.
438 return insert_unique(__val
).first
;
440 if (_M_key_compare(_KeyOfValue()(__val
), _S_key(__position
._M_node
))) {
441 return _M_insert(__position
._M_node
, __val
, __position
._M_node
);
443 // first argument just needs to be non-null
445 bool __comp_pos_v
= _M_key_compare( _S_key(__position
._M_node
), _KeyOfValue()(__val
) );
447 if (__comp_pos_v
== false) // compare > and compare < both false so compare equal
449 //Below __comp_pos_v == true
451 // Standard-conformance - does the insertion point fall immediately AFTER
453 iterator __after
= __position
;
456 // Check for only one member -- in that case, __position points to itself,
457 // and attempting to increment will cause an infinite loop.
458 if (__after
._M_node
== &this->_M_header
._M_data
)
459 // Check guarantees exactly one member, so comparison was already
460 // performed and we know the result; skip repeating it in _M_insert
461 // by specifying a non-zero fourth argument.
462 return _M_insert(__position
._M_node
, __val
, 0, __position
._M_node
);
466 // Optimization to catch insert-equivalent -- save comparison results,
467 // and we get this for free.
468 if (_M_key_compare( _KeyOfValue()(__val
), _S_key(__after
._M_node
) )) {
469 if (_S_right(__position
._M_node
) == 0)
470 return _M_insert(__position
._M_node
, __val
, 0, __position
._M_node
);
472 return _M_insert(__after
._M_node
, __val
, __after
._M_node
);
475 return insert_unique(__val
).first
;
479 else if (__position
._M_node
== &this->_M_header
._M_data
) { // end()
480 if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__val
))) {
481 // pass along to _M_insert that it can skip comparing
482 // v, Key ; since compare Key, v was true, compare v, Key must be false.
483 return _M_insert(_M_rightmost(), __val
, 0, __position
._M_node
); // Last argument only needs to be non-null
486 return insert_unique(__val
).first
;
489 iterator __before
= __position
;
492 bool __comp_v_pos
= _M_key_compare(_KeyOfValue()(__val
), _S_key(__position
._M_node
));
495 && _M_key_compare( _S_key(__before
._M_node
), _KeyOfValue()(__val
) )) {
497 if (_S_right(__before
._M_node
) == 0)
498 return _M_insert(__before
._M_node
, __val
, 0, __before
._M_node
); // Last argument only needs to be non-null
500 return _M_insert(__position
._M_node
, __val
, __position
._M_node
);
501 // first argument just needs to be non-null
504 // Does the insertion point fall immediately AFTER the hint?
505 iterator __after
= __position
;
507 // Optimization to catch equivalent cases and avoid unnecessary comparisons
508 bool __comp_pos_v
= !__comp_v_pos
; // Stored this result earlier
509 // If the earlier comparison was true, this comparison doesn't need to be
510 // performed because it must be false. However, if the earlier comparison
511 // was false, we need to perform this one because in the equal case, both will
514 __comp_pos_v
= _M_key_compare(_S_key(__position
._M_node
), _KeyOfValue()(__val
));
517 if ( (!__comp_v_pos
) // comp_v_pos true implies comp_v_pos false
519 && (__after
._M_node
== &this->_M_header
._M_data
||
520 _M_key_compare( _KeyOfValue()(__val
), _S_key(__after
._M_node
) ))) {
521 if (_S_right(__position
._M_node
) == 0)
522 return _M_insert(__position
._M_node
, __val
, 0, __position
._M_node
);
524 return _M_insert(__after
._M_node
, __val
, __after
._M_node
);
526 // Test for equivalent case
527 if (__comp_v_pos
== __comp_pos_v
)
530 return insert_unique(__val
).first
;
536 template <class _Key
, class _Compare
,
537 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
539 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::insert_equal(iterator __position
,
540 const _Value
& __val
) {
541 if (__position
._M_node
== this->_M_header
._M_data
._M_left
) { // begin()
543 // Check for zero members
545 return insert_equal(__val
);
547 if (!_M_key_compare(_S_key(__position
._M_node
), _KeyOfValue()(__val
)))
548 return _M_insert(__position
._M_node
, __val
, __position
._M_node
);
550 // Check for only one member
551 if (__position
._M_node
->_M_left
== __position
._M_node
)
552 // Unlike insert_unique, can't avoid doing a comparison here.
553 return _M_insert(__position
._M_node
, __val
);
556 // Standard-conformance - does the insertion point fall immediately AFTER
558 iterator __after
= __position
;
561 // Already know that compare(pos, v) must be true!
562 // Therefore, we want to know if compare(after, v) is false.
563 // (i.e., we now pos < v, now we want to know if v <= after)
564 // If not, invalid hint.
565 if ( __after
._M_node
== &this->_M_header
._M_data
||
566 !_M_key_compare( _S_key(__after
._M_node
), _KeyOfValue()(__val
) ) ) {
567 if (_S_right(__position
._M_node
) == 0)
568 return _M_insert(__position
._M_node
, __val
, 0, __position
._M_node
);
570 return _M_insert(__after
._M_node
, __val
, __after
._M_node
);
572 else { // Invalid hint
573 return insert_equal(__val
);
577 else if (__position
._M_node
== &this->_M_header
._M_data
) { // end()
578 if (!_M_key_compare(_KeyOfValue()(__val
), _S_key(_M_rightmost())))
579 return _M_insert(_M_rightmost(), __val
, 0, __position
._M_node
); // Last argument only needs to be non-null
581 return insert_equal(__val
);
585 iterator __before
= __position
;
587 // store the result of the comparison between pos and v so
588 // that we don't have to do it again later. Note that this reverses the shortcut
589 // on the if, possibly harming efficiency in comparisons; I think the harm will
590 // be negligible, and to do what I want to do (save the result of a comparison so
591 // that it can be re-used) there is no alternative. Test here is for before <= v <= pos.
592 bool __comp_pos_v
= _M_key_compare(_S_key(__position
._M_node
), _KeyOfValue()(__val
));
594 !_M_key_compare(_KeyOfValue()(__val
), _S_key(__before
._M_node
))) {
595 if (_S_right(__before
._M_node
) == 0)
596 return _M_insert(__before
._M_node
, __val
, 0, __before
._M_node
); // Last argument only needs to be non-null
598 return _M_insert(__position
._M_node
, __val
, __position
._M_node
);
601 // Does the insertion point fall immediately AFTER the hint?
602 // Test for pos < v <= after
603 iterator __after
= __position
;
607 ( __after
._M_node
== &this->_M_header
._M_data
||
608 !_M_key_compare( _S_key(__after
._M_node
), _KeyOfValue()(__val
) ) ) ) {
609 if (_S_right(__position
._M_node
) == 0)
610 return _M_insert(__position
._M_node
, __val
, 0, __position
._M_node
);
612 return _M_insert(__after
._M_node
, __val
, __after
._M_node
);
614 else { // Invalid hint
615 return insert_equal(__val
);
621 template <class _Key
, class _Compare
,
622 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
624 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
> ::_M_copy(_Rb_tree_node_base
* __x
,
625 _Rb_tree_node_base
* __p
) {
626 // structural copy. __x and __p must be non-null.
627 _Base_ptr __top
= _M_clone_node(__x
);
628 _S_parent(__top
) = __p
;
632 _S_right(__top
) = _M_copy(_S_right(__x
), __top
);
637 _Base_ptr __y
= _M_clone_node(__x
);
639 _S_parent(__y
) = __p
;
641 _S_right(__y
) = _M_copy(_S_right(__x
), __y
);
646 _STLP_UNWIND(_M_erase(__top
))
651 // this has to stay out-of-line : it's recursive
652 template <class _Key
, class _Compare
,
653 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
655 _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
>::_M_erase(_Rb_tree_node_base
*__x
) {
656 // erase without rebalancing
658 _M_erase(_S_right(__x
));
659 _Base_ptr __y
= _S_left(__x
);
660 _STLP_STD::_Destroy(&_S_value(__x
));
661 this->_M_header
.deallocate(__STATIC_CAST(_Link_type
, __x
),1);
666 #if defined (_STLP_DEBUG)
668 __black_count(_Rb_tree_node_base
* __node
, _Rb_tree_node_base
* __root
) {
672 int __bc
= __node
->_M_color
== _S_rb_tree_black
? 1 : 0;
673 if (__node
== __root
)
676 return __bc
+ __black_count(__node
->_M_parent
, __root
);
680 template <class _Key
, class _Compare
,
681 class _Value
, class _KeyOfValue
, class _Traits
, class _Alloc
>
682 bool _Rb_tree
<_Key
,_Compare
,_Value
,_KeyOfValue
,_Traits
,_Alloc
>::__rb_verify() const {
683 if (_M_node_count
== 0 || begin() == end())
684 return ((_M_node_count
== 0) &&
685 (begin() == end()) &&
686 (this->_M_header
._M_data
._M_left
== &this->_M_header
._M_data
) &&
687 (this->_M_header
._M_data
._M_right
== &this->_M_header
._M_data
));
689 int __len
= __black_count(_M_leftmost(), _M_root());
690 for (const_iterator __it
= begin(); __it
!= end(); ++__it
) {
691 _Base_ptr __x
= __it
._M_node
;
692 _Base_ptr __L
= _S_left(__x
);
693 _Base_ptr __R
= _S_right(__x
);
695 if (__x
->_M_color
== _S_rb_tree_red
)
696 if ((__L
&& __L
->_M_color
== _S_rb_tree_red
) ||
697 (__R
&& __R
->_M_color
== _S_rb_tree_red
))
700 if (__L
&& _M_key_compare(_S_key(__x
), _S_key(__L
)))
702 if (__R
&& _M_key_compare(_S_key(__R
), _S_key(__x
)))
705 if (!__L
&& !__R
&& __black_count(__x
, _M_root()) != __len
)
709 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
711 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
716 #endif /* _STLP_DEBUG */
718 _STLP_MOVE_TO_STD_NAMESPACE
726 #endif /* _STLP_TREE_C */