3 * Copyright (c) 1996,1997
4 * Silicon Graphics Computer Systems, Inc.
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation. Silicon Graphics makes no
11 * representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
16 * Hewlett-Packard Company
18 * Permission to use, copy, modify, distribute and sell this software
19 * and its documentation for any purpose is hereby granted without fee,
20 * provided that the above copyright notice appear in all copies and
21 * that both that copyright notice and this permission notice appear
22 * in supporting documentation. Hewlett-Packard Company makes no
23 * representations about the suitability of this software for any
24 * purpose. It is provided "as is" without express or implied warranty.
29 /* NOTE: This is an internal header file, included by other STL headers.
30 * You should not attempt to use it directly.
33 #ifndef __SGI_STL_INTERNAL_TREE_H
34 #define __SGI_STL_INTERNAL_TREE_H
38 Red-black tree class, designed for use in implementing STL
39 associative containers (set, multiset, map, and multimap). The
40 insertion and deletion algorithms are based on those in Cormen,
41 Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
44 (1) the header cell is maintained with links not only to the root
45 but also to the leftmost node of the tree, to enable constant time
46 begin(), and to the rightmost node of the tree, to enable linear time
47 performance when used with the generic set algorithms (set_union,
50 (2) when a node being deleted has two children its successor node is
51 relinked into its place, rather than copied, so that the only
52 iterators invalidated are those referring to the deleted node.
56 #include <stl_algobase.h>
57 #include <stl_alloc.h>
58 #include <stl_construct.h>
59 #include <stl_function.h>
63 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
67 typedef bool _Rb_tree_Color_type
;
68 const _Rb_tree_Color_type _S_rb_tree_red
= false;
69 const _Rb_tree_Color_type _S_rb_tree_black
= true;
71 struct _Rb_tree_node_base
73 typedef _Rb_tree_Color_type _Color_type
;
74 typedef _Rb_tree_node_base
* _Base_ptr
;
81 static _Base_ptr
_S_minimum(_Base_ptr __x
)
83 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
87 static _Base_ptr
_S_maximum(_Base_ptr __x
)
89 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
94 template <class _Value
>
95 struct _Rb_tree_node
: public _Rb_tree_node_base
97 typedef _Rb_tree_node
<_Value
>* _Link_type
;
98 _Value _M_value_field
;
102 struct _Rb_tree_base_iterator
104 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr
;
105 typedef bidirectional_iterator_tag iterator_category
;
106 typedef ptrdiff_t difference_type
;
111 if (_M_node
->_M_right
!= 0) {
112 _M_node
= _M_node
->_M_right
;
113 while (_M_node
->_M_left
!= 0)
114 _M_node
= _M_node
->_M_left
;
117 _Base_ptr __y
= _M_node
->_M_parent
;
118 while (_M_node
== __y
->_M_right
) {
120 __y
= __y
->_M_parent
;
122 if (_M_node
->_M_right
!= __y
)
129 if (_M_node
->_M_color
== _S_rb_tree_red
&&
130 _M_node
->_M_parent
->_M_parent
== _M_node
)
131 _M_node
= _M_node
->_M_right
;
132 else if (_M_node
->_M_left
!= 0) {
133 _Base_ptr __y
= _M_node
->_M_left
;
134 while (__y
->_M_right
!= 0)
139 _Base_ptr __y
= _M_node
->_M_parent
;
140 while (_M_node
== __y
->_M_left
) {
142 __y
= __y
->_M_parent
;
149 template <class _Value
, class _Ref
, class _Ptr
>
150 struct _Rb_tree_iterator
: public _Rb_tree_base_iterator
152 typedef _Value value_type
;
153 typedef _Ref reference
;
154 typedef _Ptr pointer
;
155 typedef _Rb_tree_iterator
<_Value
, _Value
&, _Value
*>
157 typedef _Rb_tree_iterator
<_Value
, const _Value
&, const _Value
*>
159 typedef _Rb_tree_iterator
<_Value
, _Ref
, _Ptr
>
161 typedef _Rb_tree_node
<_Value
>* _Link_type
;
163 _Rb_tree_iterator() {}
164 _Rb_tree_iterator(_Link_type __x
) { _M_node
= __x
; }
165 _Rb_tree_iterator(const iterator
& __it
) { _M_node
= __it
._M_node
; }
167 reference
operator*() const { return _Link_type(_M_node
)->_M_value_field
; }
168 #ifndef __SGI_STL_NO_ARROW_OPERATOR
169 pointer
operator->() const { return &(operator*()); }
170 #endif /* __SGI_STL_NO_ARROW_OPERATOR */
172 _Self
& operator++() { _M_increment(); return *this; }
173 _Self
operator++(int) {
179 _Self
& operator--() { _M_decrement(); return *this; }
180 _Self
operator--(int) {
187 inline bool operator==(const _Rb_tree_base_iterator
& __x
,
188 const _Rb_tree_base_iterator
& __y
) {
189 return __x
._M_node
== __y
._M_node
;
192 inline bool operator!=(const _Rb_tree_base_iterator
& __x
,
193 const _Rb_tree_base_iterator
& __y
) {
194 return __x
._M_node
!= __y
._M_node
;
197 #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
199 inline bidirectional_iterator_tag
200 iterator_category(const _Rb_tree_base_iterator
&) {
201 return bidirectional_iterator_tag();
204 inline _Rb_tree_base_iterator::difference_type
*
205 distance_type(const _Rb_tree_base_iterator
&) {
206 return (_Rb_tree_base_iterator::difference_type
*) 0;
209 template <class _Value
, class _Ref
, class _Ptr
>
210 inline _Value
* value_type(const _Rb_tree_iterator
<_Value
, _Ref
, _Ptr
>&) {
214 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
217 _Rb_tree_rotate_left(_Rb_tree_node_base
* __x
, _Rb_tree_node_base
*& __root
)
219 _Rb_tree_node_base
* __y
= __x
->_M_right
;
220 __x
->_M_right
= __y
->_M_left
;
221 if (__y
->_M_left
!=0)
222 __y
->_M_left
->_M_parent
= __x
;
223 __y
->_M_parent
= __x
->_M_parent
;
227 else if (__x
== __x
->_M_parent
->_M_left
)
228 __x
->_M_parent
->_M_left
= __y
;
230 __x
->_M_parent
->_M_right
= __y
;
232 __x
->_M_parent
= __y
;
236 _Rb_tree_rotate_right(_Rb_tree_node_base
* __x
, _Rb_tree_node_base
*& __root
)
238 _Rb_tree_node_base
* __y
= __x
->_M_left
;
239 __x
->_M_left
= __y
->_M_right
;
240 if (__y
->_M_right
!= 0)
241 __y
->_M_right
->_M_parent
= __x
;
242 __y
->_M_parent
= __x
->_M_parent
;
246 else if (__x
== __x
->_M_parent
->_M_right
)
247 __x
->_M_parent
->_M_right
= __y
;
249 __x
->_M_parent
->_M_left
= __y
;
251 __x
->_M_parent
= __y
;
255 _Rb_tree_rebalance(_Rb_tree_node_base
* __x
, _Rb_tree_node_base
*& __root
)
257 __x
->_M_color
= _S_rb_tree_red
;
258 while (__x
!= __root
&& __x
->_M_parent
->_M_color
== _S_rb_tree_red
) {
259 if (__x
->_M_parent
== __x
->_M_parent
->_M_parent
->_M_left
) {
260 _Rb_tree_node_base
* __y
= __x
->_M_parent
->_M_parent
->_M_right
;
261 if (__y
&& __y
->_M_color
== _S_rb_tree_red
) {
262 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
263 __y
->_M_color
= _S_rb_tree_black
;
264 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
265 __x
= __x
->_M_parent
->_M_parent
;
268 if (__x
== __x
->_M_parent
->_M_right
) {
269 __x
= __x
->_M_parent
;
270 _Rb_tree_rotate_left(__x
, __root
);
272 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
273 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
274 _Rb_tree_rotate_right(__x
->_M_parent
->_M_parent
, __root
);
278 _Rb_tree_node_base
* __y
= __x
->_M_parent
->_M_parent
->_M_left
;
279 if (__y
&& __y
->_M_color
== _S_rb_tree_red
) {
280 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
281 __y
->_M_color
= _S_rb_tree_black
;
282 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
283 __x
= __x
->_M_parent
->_M_parent
;
286 if (__x
== __x
->_M_parent
->_M_left
) {
287 __x
= __x
->_M_parent
;
288 _Rb_tree_rotate_right(__x
, __root
);
290 __x
->_M_parent
->_M_color
= _S_rb_tree_black
;
291 __x
->_M_parent
->_M_parent
->_M_color
= _S_rb_tree_red
;
292 _Rb_tree_rotate_left(__x
->_M_parent
->_M_parent
, __root
);
296 __root
->_M_color
= _S_rb_tree_black
;
299 inline _Rb_tree_node_base
*
300 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base
* __z
,
301 _Rb_tree_node_base
*& __root
,
302 _Rb_tree_node_base
*& __leftmost
,
303 _Rb_tree_node_base
*& __rightmost
)
305 _Rb_tree_node_base
* __y
= __z
;
306 _Rb_tree_node_base
* __x
= 0;
307 _Rb_tree_node_base
* __x_parent
= 0;
308 if (__y
->_M_left
== 0) // __z has at most one non-null child. y == z.
309 __x
= __y
->_M_right
; // __x might be null.
311 if (__y
->_M_right
== 0) // __z has exactly one non-null child. y == z.
312 __x
= __y
->_M_left
; // __x is not null.
313 else { // __z has two non-null children. Set __y to
314 __y
= __y
->_M_right
; // __z's successor. __x might be null.
315 while (__y
->_M_left
!= 0)
319 if (__y
!= __z
) { // relink y in place of z. y is z's successor
320 __z
->_M_left
->_M_parent
= __y
;
321 __y
->_M_left
= __z
->_M_left
;
322 if (__y
!= __z
->_M_right
) {
323 __x_parent
= __y
->_M_parent
;
324 if (__x
) __x
->_M_parent
= __y
->_M_parent
;
325 __y
->_M_parent
->_M_left
= __x
; // __y must be a child of _M_left
326 __y
->_M_right
= __z
->_M_right
;
327 __z
->_M_right
->_M_parent
= __y
;
333 else if (__z
->_M_parent
->_M_left
== __z
)
334 __z
->_M_parent
->_M_left
= __y
;
336 __z
->_M_parent
->_M_right
= __y
;
337 __y
->_M_parent
= __z
->_M_parent
;
338 __STD::swap(__y
->_M_color
, __z
->_M_color
);
340 // __y now points to node to be actually deleted
343 __x_parent
= __y
->_M_parent
;
344 if (__x
) __x
->_M_parent
= __y
->_M_parent
;
348 if (__z
->_M_parent
->_M_left
== __z
)
349 __z
->_M_parent
->_M_left
= __x
;
351 __z
->_M_parent
->_M_right
= __x
;
352 if (__leftmost
== __z
)
353 if (__z
->_M_right
== 0) // __z->_M_left must be null also
354 __leftmost
= __z
->_M_parent
;
355 // makes __leftmost == _M_header if __z == __root
357 __leftmost
= _Rb_tree_node_base::_S_minimum(__x
);
358 if (__rightmost
== __z
)
359 if (__z
->_M_left
== 0) // __z->_M_right must be null also
360 __rightmost
= __z
->_M_parent
;
361 // makes __rightmost == _M_header if __z == __root
362 else // __x == __z->_M_left
363 __rightmost
= _Rb_tree_node_base::_S_maximum(__x
);
365 if (__y
->_M_color
!= _S_rb_tree_red
) {
366 while (__x
!= __root
&& (__x
== 0 || __x
->_M_color
== _S_rb_tree_black
))
367 if (__x
== __x_parent
->_M_left
) {
368 _Rb_tree_node_base
* __w
= __x_parent
->_M_right
;
369 if (__w
->_M_color
== _S_rb_tree_red
) {
370 __w
->_M_color
= _S_rb_tree_black
;
371 __x_parent
->_M_color
= _S_rb_tree_red
;
372 _Rb_tree_rotate_left(__x_parent
, __root
);
373 __w
= __x_parent
->_M_right
;
375 if ((__w
->_M_left
== 0 ||
376 __w
->_M_left
->_M_color
== _S_rb_tree_black
) &&
377 (__w
->_M_right
== 0 ||
378 __w
->_M_right
->_M_color
== _S_rb_tree_black
)) {
379 __w
->_M_color
= _S_rb_tree_red
;
381 __x_parent
= __x_parent
->_M_parent
;
383 if (__w
->_M_right
== 0 ||
384 __w
->_M_right
->_M_color
== _S_rb_tree_black
) {
385 if (__w
->_M_left
) __w
->_M_left
->_M_color
= _S_rb_tree_black
;
386 __w
->_M_color
= _S_rb_tree_red
;
387 _Rb_tree_rotate_right(__w
, __root
);
388 __w
= __x_parent
->_M_right
;
390 __w
->_M_color
= __x_parent
->_M_color
;
391 __x_parent
->_M_color
= _S_rb_tree_black
;
392 if (__w
->_M_right
) __w
->_M_right
->_M_color
= _S_rb_tree_black
;
393 _Rb_tree_rotate_left(__x_parent
, __root
);
396 } else { // same as above, with _M_right <-> _M_left.
397 _Rb_tree_node_base
* __w
= __x_parent
->_M_left
;
398 if (__w
->_M_color
== _S_rb_tree_red
) {
399 __w
->_M_color
= _S_rb_tree_black
;
400 __x_parent
->_M_color
= _S_rb_tree_red
;
401 _Rb_tree_rotate_right(__x_parent
, __root
);
402 __w
= __x_parent
->_M_left
;
404 if ((__w
->_M_right
== 0 ||
405 __w
->_M_right
->_M_color
== _S_rb_tree_black
) &&
406 (__w
->_M_left
== 0 ||
407 __w
->_M_left
->_M_color
== _S_rb_tree_black
)) {
408 __w
->_M_color
= _S_rb_tree_red
;
410 __x_parent
= __x_parent
->_M_parent
;
412 if (__w
->_M_left
== 0 ||
413 __w
->_M_left
->_M_color
== _S_rb_tree_black
) {
414 if (__w
->_M_right
) __w
->_M_right
->_M_color
= _S_rb_tree_black
;
415 __w
->_M_color
= _S_rb_tree_red
;
416 _Rb_tree_rotate_left(__w
, __root
);
417 __w
= __x_parent
->_M_left
;
419 __w
->_M_color
= __x_parent
->_M_color
;
420 __x_parent
->_M_color
= _S_rb_tree_black
;
421 if (__w
->_M_left
) __w
->_M_left
->_M_color
= _S_rb_tree_black
;
422 _Rb_tree_rotate_right(__x_parent
, __root
);
426 if (__x
) __x
->_M_color
= _S_rb_tree_black
;
431 // Base class to encapsulate the differences between old SGI-style
432 // allocators and standard-conforming allocators. In order to avoid
433 // having an empty base class, we arbitrarily move one of rb_tree's
434 // data members into the base class.
436 #ifdef __STL_USE_STD_ALLOCATORS
438 // _Base for general standard-conforming allocators.
439 template <class _Tp
, class _Alloc
, bool _S_instanceless
>
440 class _Rb_tree_alloc_base
{
442 typedef typename _Alloc_traits
<_Tp
, _Alloc
>::allocator_type allocator_type
;
443 allocator_type
get_allocator() const { return _M_node_allocator
; }
445 _Rb_tree_alloc_base(const allocator_type
& __a
)
446 : _M_node_allocator(__a
), _M_header(0) {}
449 typename _Alloc_traits
<_Rb_tree_node
<_Tp
>, _Alloc
>::allocator_type
451 _Rb_tree_node
<_Tp
>* _M_header
;
453 _Rb_tree_node
<_Tp
>* _M_get_node()
454 { return _M_node_allocator
.allocate(1); }
455 void _M_put_node(_Rb_tree_node
<_Tp
>* __p
)
456 { _M_node_allocator
.deallocate(__p
, 1); }
459 // Specialization for instanceless allocators.
460 template <class _Tp
, class _Alloc
>
461 class _Rb_tree_alloc_base
<_Tp
, _Alloc
, true> {
463 typedef typename _Alloc_traits
<_Tp
, _Alloc
>::allocator_type allocator_type
;
464 allocator_type
get_allocator() const { return allocator_type(); }
466 _Rb_tree_alloc_base(const allocator_type
&) : _M_header(0) {}
469 _Rb_tree_node
<_Tp
>* _M_header
;
471 typedef typename _Alloc_traits
<_Rb_tree_node
<_Tp
>, _Alloc
>::_Alloc_type
474 _Rb_tree_node
<_Tp
>* _M_get_node()
475 { return _Alloc_type::allocate(1); }
476 void _M_put_node(_Rb_tree_node
<_Tp
>* __p
)
477 { _Alloc_type::deallocate(__p
, 1); }
480 template <class _Tp
, class _Alloc
>
482 : public _Rb_tree_alloc_base
<_Tp
, _Alloc
,
483 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
485 typedef _Rb_tree_alloc_base
<_Tp
, _Alloc
,
486 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
488 typedef typename
_Base::allocator_type allocator_type
;
490 _Rb_tree_base(const allocator_type
& __a
)
491 : _Base(__a
) { _M_header
= _M_get_node(); }
492 ~_Rb_tree_base() { _M_put_node(_M_header
); }
496 #else /* __STL_USE_STD_ALLOCATORS */
498 template <class _Tp
, class _Alloc
>
501 typedef _Alloc allocator_type
;
502 allocator_type
get_allocator() const { return allocator_type(); }
504 _Rb_tree_base(const allocator_type
&)
505 : _M_header(0) { _M_header
= _M_get_node(); }
506 ~_Rb_tree_base() { _M_put_node(_M_header
); }
509 _Rb_tree_node
<_Tp
>* _M_header
;
511 typedef simple_alloc
<_Rb_tree_node
<_Tp
>, _Alloc
> _Alloc_type
;
513 _Rb_tree_node
<_Tp
>* _M_get_node()
514 { return _Alloc_type::allocate(1); }
515 void _M_put_node(_Rb_tree_node
<_Tp
>* __p
)
516 { _Alloc_type::deallocate(__p
, 1); }
519 #endif /* __STL_USE_STD_ALLOCATORS */
521 template <class _Key
, class _Value
, class _KeyOfValue
, class _Compare
,
522 class _Alloc
= __STL_DEFAULT_ALLOCATOR(_Value
) >
523 class _Rb_tree
: protected _Rb_tree_base
<_Value
, _Alloc
> {
524 typedef _Rb_tree_base
<_Value
, _Alloc
> _Base
;
526 typedef _Rb_tree_node_base
* _Base_ptr
;
527 typedef _Rb_tree_node
<_Value
> _Rb_tree_node
;
528 typedef _Rb_tree_Color_type _Color_type
;
530 typedef _Key key_type
;
531 typedef _Value value_type
;
532 typedef value_type
* pointer
;
533 typedef const value_type
* const_pointer
;
534 typedef value_type
& reference
;
535 typedef const value_type
& const_reference
;
536 typedef _Rb_tree_node
* _Link_type
;
537 typedef size_t size_type
;
538 typedef ptrdiff_t difference_type
;
540 typedef typename
_Base::allocator_type allocator_type
;
541 allocator_type
get_allocator() const { return _Base::get_allocator(); }
544 #ifdef __STL_USE_NAMESPACES
545 using _Base::_M_get_node
;
546 using _Base::_M_put_node
;
547 using _Base::_M_header
;
548 #endif /* __STL_USE_NAMESPACES */
552 _Link_type
_M_create_node(const value_type
& __x
)
554 _Link_type __tmp
= _M_get_node();
556 construct(&__tmp
->_M_value_field
, __x
);
558 __STL_UNWIND(_M_put_node(__tmp
));
562 _Link_type
_M_clone_node(_Link_type __x
)
564 _Link_type __tmp
= _M_create_node(__x
->_M_value_field
);
565 __tmp
->_M_color
= __x
->_M_color
;
571 void destroy_node(_Link_type __p
)
573 destroy(&__p
->_M_value_field
);
578 size_type _M_node_count
; // keeps track of size of tree
579 _Compare _M_key_compare
;
581 _Link_type
& _M_root() const
582 { return (_Link_type
&) _M_header
->_M_parent
; }
583 _Link_type
& _M_leftmost() const
584 { return (_Link_type
&) _M_header
->_M_left
; }
585 _Link_type
& _M_rightmost() const
586 { return (_Link_type
&) _M_header
->_M_right
; }
588 static _Link_type
& _S_left(_Link_type __x
)
589 { return (_Link_type
&)(__x
->_M_left
); }
590 static _Link_type
& _S_right(_Link_type __x
)
591 { return (_Link_type
&)(__x
->_M_right
); }
592 static _Link_type
& _S_parent(_Link_type __x
)
593 { return (_Link_type
&)(__x
->_M_parent
); }
594 static reference
_S_value(_Link_type __x
)
595 { return __x
->_M_value_field
; }
596 static const _Key
& _S_key(_Link_type __x
)
597 { return _KeyOfValue()(_S_value(__x
)); }
598 static _Color_type
& _S_color(_Link_type __x
)
599 { return (_Color_type
&)(__x
->_M_color
); }
601 static _Link_type
& _S_left(_Base_ptr __x
)
602 { return (_Link_type
&)(__x
->_M_left
); }
603 static _Link_type
& _S_right(_Base_ptr __x
)
604 { return (_Link_type
&)(__x
->_M_right
); }
605 static _Link_type
& _S_parent(_Base_ptr __x
)
606 { return (_Link_type
&)(__x
->_M_parent
); }
607 static reference
_S_value(_Base_ptr __x
)
608 { return ((_Link_type
)__x
)->_M_value_field
; }
609 static const _Key
& _S_key(_Base_ptr __x
)
610 { return _KeyOfValue()(_S_value(_Link_type(__x
)));}
611 static _Color_type
& _S_color(_Base_ptr __x
)
612 { return (_Color_type
&)(_Link_type(__x
)->_M_color
); }
614 static _Link_type
_S_minimum(_Link_type __x
)
615 { return (_Link_type
) _Rb_tree_node_base::_S_minimum(__x
); }
617 static _Link_type
_S_maximum(_Link_type __x
)
618 { return (_Link_type
) _Rb_tree_node_base::_S_maximum(__x
); }
621 typedef _Rb_tree_iterator
<value_type
, reference
, pointer
> iterator
;
622 typedef _Rb_tree_iterator
<value_type
, const_reference
, const_pointer
>
625 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
626 typedef reverse_iterator
<const_iterator
> const_reverse_iterator
;
627 typedef reverse_iterator
<iterator
> reverse_iterator
;
628 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
629 typedef reverse_bidirectional_iterator
<iterator
, value_type
, reference
,
632 typedef reverse_bidirectional_iterator
<const_iterator
, value_type
,
633 const_reference
, difference_type
>
634 const_reverse_iterator
;
635 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
638 iterator
_M_insert(_Base_ptr __x
, _Base_ptr __y
, const value_type
& __v
);
639 _Link_type
_M_copy(_Link_type __x
, _Link_type __p
);
640 void _M_erase(_Link_type __x
);
643 // allocation/deallocation
645 : _Base(allocator_type()), _M_node_count(0), _M_key_compare()
646 { _M_empty_initialize(); }
648 _Rb_tree(const _Compare
& __comp
)
649 : _Base(allocator_type()), _M_node_count(0), _M_key_compare(__comp
)
650 { _M_empty_initialize(); }
652 _Rb_tree(const _Compare
& __comp
, const allocator_type
& __a
)
653 : _Base(__a
), _M_node_count(0), _M_key_compare(__comp
)
654 { _M_empty_initialize(); }
656 _Rb_tree(const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __x
)
657 : _Base(__x
.get_allocator()),
658 _M_node_count(0), _M_key_compare(__x
._M_key_compare
)
660 if (__x
._M_root() == 0)
661 _M_empty_initialize();
663 _S_color(_M_header
) = _S_rb_tree_red
;
664 _M_root() = _M_copy(__x
._M_root(), _M_header
);
665 _M_leftmost() = _S_minimum(_M_root());
666 _M_rightmost() = _S_maximum(_M_root());
668 _M_node_count
= __x
._M_node_count
;
670 ~_Rb_tree() { clear(); }
671 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>&
672 operator=(const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __x
);
675 void _M_empty_initialize() {
676 _S_color(_M_header
) = _S_rb_tree_red
; // used to distinguish header from
677 // __root, in iterator.operator++
679 _M_leftmost() = _M_header
;
680 _M_rightmost() = _M_header
;
685 _Compare
key_comp() const { return _M_key_compare
; }
686 iterator
begin() { return _M_leftmost(); }
687 const_iterator
begin() const { return _M_leftmost(); }
688 iterator
end() { return _M_header
; }
689 const_iterator
end() const { return _M_header
; }
690 reverse_iterator
rbegin() { return reverse_iterator(end()); }
691 const_reverse_iterator
rbegin() const {
692 return const_reverse_iterator(end());
694 reverse_iterator
rend() { return reverse_iterator(begin()); }
695 const_reverse_iterator
rend() const {
696 return const_reverse_iterator(begin());
698 bool empty() const { return _M_node_count
== 0; }
699 size_type
size() const { return _M_node_count
; }
700 size_type
max_size() const { return size_type(-1); }
702 void swap(_Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __t
) {
703 __STD::swap(_M_header
, __t
._M_header
);
704 __STD::swap(_M_node_count
, __t
._M_node_count
);
705 __STD::swap(_M_key_compare
, __t
._M_key_compare
);
710 pair
<iterator
,bool> insert_unique(const value_type
& __x
);
711 iterator
insert_equal(const value_type
& __x
);
713 iterator
insert_unique(iterator __position
, const value_type
& __x
);
714 iterator
insert_equal(iterator __position
, const value_type
& __x
);
716 #ifdef __STL_MEMBER_TEMPLATES
717 template <class _InputIterator
>
718 void insert_unique(_InputIterator __first
, _InputIterator __last
);
719 template <class _InputIterator
>
720 void insert_equal(_InputIterator __first
, _InputIterator __last
);
721 #else /* __STL_MEMBER_TEMPLATES */
722 void insert_unique(const_iterator __first
, const_iterator __last
);
723 void insert_unique(const value_type
* __first
, const value_type
* __last
);
724 void insert_equal(const_iterator __first
, const_iterator __last
);
725 void insert_equal(const value_type
* __first
, const value_type
* __last
);
726 #endif /* __STL_MEMBER_TEMPLATES */
728 void erase(iterator __position
);
729 size_type
erase(const key_type
& __x
);
730 void erase(iterator __first
, iterator __last
);
731 void erase(const key_type
* __first
, const key_type
* __last
);
733 if (_M_node_count
!= 0) {
735 _M_leftmost() = _M_header
;
737 _M_rightmost() = _M_header
;
744 iterator
find(const key_type
& __x
);
745 const_iterator
find(const key_type
& __x
) const;
746 size_type
count(const key_type
& __x
) const;
747 iterator
lower_bound(const key_type
& __x
);
748 const_iterator
lower_bound(const key_type
& __x
) const;
749 iterator
upper_bound(const key_type
& __x
);
750 const_iterator
upper_bound(const key_type
& __x
) const;
751 pair
<iterator
,iterator
> equal_range(const key_type
& __x
);
752 pair
<const_iterator
, const_iterator
> equal_range(const key_type
& __x
) const;
756 bool __rb_verify() const;
759 template <class _Key
, class _Value
, class _KeyOfValue
,
760 class _Compare
, class _Alloc
>
762 operator==(const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
763 const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
765 return __x
.size() == __y
.size() &&
766 equal(__x
.begin(), __x
.end(), __y
.begin());
769 template <class _Key
, class _Value
, class _KeyOfValue
,
770 class _Compare
, class _Alloc
>
772 operator<(const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
773 const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
775 return lexicographical_compare(__x
.begin(), __x
.end(),
776 __y
.begin(), __y
.end());
779 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
781 template <class _Key
, class _Value
, class _KeyOfValue
,
782 class _Compare
, class _Alloc
>
784 swap(_Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
785 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
790 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
793 template <class _Key
, class _Value
, class _KeyOfValue
,
794 class _Compare
, class _Alloc
>
795 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>&
796 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
797 ::operator=(const _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>& __x
)
800 // Note that _Key may be a constant type.
803 _M_key_compare
= __x
._M_key_compare
;
804 if (__x
._M_root() == 0) {
806 _M_leftmost() = _M_header
;
807 _M_rightmost() = _M_header
;
810 _M_root() = _M_copy(__x
._M_root(), _M_header
);
811 _M_leftmost() = _S_minimum(_M_root());
812 _M_rightmost() = _S_maximum(_M_root());
813 _M_node_count
= __x
._M_node_count
;
819 template <class _Key
, class _Value
, class _KeyOfValue
,
820 class _Compare
, class _Alloc
>
821 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
822 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
823 ::_M_insert(_Base_ptr __x_
, _Base_ptr __y_
, const _Value
& __v
)
825 _Link_type __x
= (_Link_type
) __x_
;
826 _Link_type __y
= (_Link_type
) __y_
;
829 if (__y
== _M_header
|| __x
!= 0 ||
830 _M_key_compare(_KeyOfValue()(__v
), _S_key(__y
))) {
831 __z
= _M_create_node(__v
);
832 _S_left(__y
) = __z
; // also makes _M_leftmost() = __z
833 // when __y == _M_header
834 if (__y
== _M_header
) {
836 _M_rightmost() = __z
;
838 else if (__y
== _M_leftmost())
839 _M_leftmost() = __z
; // maintain _M_leftmost() pointing to min node
842 __z
= _M_create_node(__v
);
844 if (__y
== _M_rightmost())
845 _M_rightmost() = __z
; // maintain _M_rightmost() pointing to max node
847 _S_parent(__z
) = __y
;
850 _Rb_tree_rebalance(__z
, _M_header
->_M_parent
);
852 return iterator(__z
);
855 template <class _Key
, class _Value
, class _KeyOfValue
,
856 class _Compare
, class _Alloc
>
857 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
858 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
859 ::insert_equal(const _Value
& __v
)
861 _Link_type __y
= _M_header
;
862 _Link_type __x
= _M_root();
865 __x
= _M_key_compare(_KeyOfValue()(__v
), _S_key(__x
)) ?
866 _S_left(__x
) : _S_right(__x
);
868 return _M_insert(__x
, __y
, __v
);
872 template <class _Key
, class _Value
, class _KeyOfValue
,
873 class _Compare
, class _Alloc
>
874 pair
<typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
,
876 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
877 ::insert_unique(const _Value
& __v
)
879 _Link_type __y
= _M_header
;
880 _Link_type __x
= _M_root();
884 __comp
= _M_key_compare(_KeyOfValue()(__v
), _S_key(__x
));
885 __x
= __comp
? _S_left(__x
) : _S_right(__x
);
887 iterator __j
= iterator(__y
);
890 return pair
<iterator
,bool>(_M_insert(__x
, __y
, __v
), true);
893 if (_M_key_compare(_S_key(__j
._M_node
), _KeyOfValue()(__v
)))
894 return pair
<iterator
,bool>(_M_insert(__x
, __y
, __v
), true);
895 return pair
<iterator
,bool>(__j
, false);
899 template <class _Key
, class _Val
, class _KeyOfValue
,
900 class _Compare
, class _Alloc
>
901 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
902 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>
903 ::insert_unique(iterator __position
, const _Val
& __v
)
905 if (__position
._M_node
== _M_header
->_M_left
) { // begin()
907 _M_key_compare(_KeyOfValue()(__v
), _S_key(__position
._M_node
)))
908 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
909 // first argument just needs to be non-null
911 return insert_unique(__v
).first
;
912 } else if (__position
._M_node
== _M_header
) { // end()
913 if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v
)))
914 return _M_insert(0, _M_rightmost(), __v
);
916 return insert_unique(__v
).first
;
918 iterator __before
= __position
;
920 if (_M_key_compare(_S_key(__before
._M_node
), _KeyOfValue()(__v
))
921 && _M_key_compare(_KeyOfValue()(__v
), _S_key(__position
._M_node
))) {
922 if (_S_right(__before
._M_node
) == 0)
923 return _M_insert(0, __before
._M_node
, __v
);
925 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
926 // first argument just needs to be non-null
928 return insert_unique(__v
).first
;
932 template <class _Key
, class _Val
, class _KeyOfValue
,
933 class _Compare
, class _Alloc
>
934 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
935 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>
936 ::insert_equal(iterator __position
, const _Val
& __v
)
938 if (__position
._M_node
== _M_header
->_M_left
) { // begin()
940 _M_key_compare(_KeyOfValue()(__v
), _S_key(__position
._M_node
)))
941 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
942 // first argument just needs to be non-null
944 return insert_equal(__v
);
945 } else if (__position
._M_node
== _M_header
) {// end()
946 if (!_M_key_compare(_KeyOfValue()(__v
), _S_key(_M_rightmost())))
947 return _M_insert(0, _M_rightmost(), __v
);
949 return insert_equal(__v
);
951 iterator __before
= __position
;
953 if (!_M_key_compare(_KeyOfValue()(__v
), _S_key(__before
._M_node
))
954 && !_M_key_compare(_S_key(__position
._M_node
), _KeyOfValue()(__v
))) {
955 if (_S_right(__before
._M_node
) == 0)
956 return _M_insert(0, __before
._M_node
, __v
);
958 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
959 // first argument just needs to be non-null
961 return insert_equal(__v
);
965 #ifdef __STL_MEMBER_TEMPLATES
967 template <class _Key
, class _Val
, class _KoV
, class _Cmp
, class _Alloc
>
969 void _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>
970 ::insert_equal(_II __first
, _II __last
)
972 for ( ; __first
!= __last
; ++__first
)
973 insert_equal(*__first
);
976 template <class _Key
, class _Val
, class _KoV
, class _Cmp
, class _Alloc
>
978 void _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>
979 ::insert_unique(_II __first
, _II __last
) {
980 for ( ; __first
!= __last
; ++__first
)
981 insert_unique(*__first
);
984 #else /* __STL_MEMBER_TEMPLATES */
986 template <class _Key
, class _Val
, class _KoV
, class _Cmp
, class _Alloc
>
988 _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>
989 ::insert_equal(const _Val
* __first
, const _Val
* __last
)
991 for ( ; __first
!= __last
; ++__first
)
992 insert_equal(*__first
);
995 template <class _Key
, class _Val
, class _KoV
, class _Cmp
, class _Alloc
>
997 _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>
998 ::insert_equal(const_iterator __first
, const_iterator __last
)
1000 for ( ; __first
!= __last
; ++__first
)
1001 insert_equal(*__first
);
1004 template <class _Key
, class _Val
, class _KoV
, class _Cmp
, class _Alloc
>
1006 _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>
1007 ::insert_unique(const _Val
* __first
, const _Val
* __last
)
1009 for ( ; __first
!= __last
; ++__first
)
1010 insert_unique(*__first
);
1013 template <class _Key
, class _Val
, class _KoV
, class _Cmp
, class _Alloc
>
1014 void _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>
1015 ::insert_unique(const_iterator __first
, const_iterator __last
)
1017 for ( ; __first
!= __last
; ++__first
)
1018 insert_unique(*__first
);
1021 #endif /* __STL_MEMBER_TEMPLATES */
1023 template <class _Key
, class _Value
, class _KeyOfValue
,
1024 class _Compare
, class _Alloc
>
1025 inline void _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1026 ::erase(iterator __position
)
1029 (_Link_type
) _Rb_tree_rebalance_for_erase(__position
._M_node
,
1030 _M_header
->_M_parent
,
1032 _M_header
->_M_right
);
1037 template <class _Key
, class _Value
, class _KeyOfValue
,
1038 class _Compare
, class _Alloc
>
1039 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::size_type
1040 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::erase(const _Key
& __x
)
1042 pair
<iterator
,iterator
> __p
= equal_range(__x
);
1044 distance(__p
.first
, __p
.second
, __n
);
1045 erase(__p
.first
, __p
.second
);
1049 template <class _Key
, class _Val
, class _KoV
, class _Compare
, class _Alloc
>
1050 typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::_Link_type
1051 _Rb_tree
<_Key
,_Val
,_KoV
,_Compare
,_Alloc
>
1052 ::_M_copy(_Link_type __x
, _Link_type __p
)
1054 // structural copy. __x and __p must be non-null.
1055 _Link_type __top
= _M_clone_node(__x
);
1056 __top
->_M_parent
= __p
;
1060 __top
->_M_right
= _M_copy(_S_right(__x
), __top
);
1065 _Link_type __y
= _M_clone_node(__x
);
1067 __y
->_M_parent
= __p
;
1069 __y
->_M_right
= _M_copy(_S_right(__x
), __y
);
1074 __STL_UNWIND(_M_erase(__top
));
1079 template <class _Key
, class _Value
, class _KeyOfValue
,
1080 class _Compare
, class _Alloc
>
1081 void _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1082 ::_M_erase(_Link_type __x
)
1084 // erase without rebalancing
1086 _M_erase(_S_right(__x
));
1087 _Link_type __y
= _S_left(__x
);
1093 template <class _Key
, class _Value
, class _KeyOfValue
,
1094 class _Compare
, class _Alloc
>
1095 void _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1096 ::erase(iterator __first
, iterator __last
)
1098 if (__first
== begin() && __last
== end())
1101 while (__first
!= __last
) erase(__first
++);
1104 template <class _Key
, class _Value
, class _KeyOfValue
,
1105 class _Compare
, class _Alloc
>
1106 void _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1107 ::erase(const _Key
* __first
, const _Key
* __last
)
1109 while (__first
!= __last
) erase(*__first
++);
1112 template <class _Key
, class _Value
, class _KeyOfValue
,
1113 class _Compare
, class _Alloc
>
1114 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
1115 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::find(const _Key
& __k
)
1117 _Link_type __y
= _M_header
; // Last node which is not less than __k.
1118 _Link_type __x
= _M_root(); // Current node.
1121 if (!_M_key_compare(_S_key(__x
), __k
))
1122 __y
= __x
, __x
= _S_left(__x
);
1124 __x
= _S_right(__x
);
1126 iterator __j
= iterator(__y
);
1127 return (__j
== end() || _M_key_compare(__k
, _S_key(__j
._M_node
))) ?
1131 template <class _Key
, class _Value
, class _KeyOfValue
,
1132 class _Compare
, class _Alloc
>
1133 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::const_iterator
1134 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::find(const _Key
& __k
) const
1136 _Link_type __y
= _M_header
; /* Last node which is not less than __k. */
1137 _Link_type __x
= _M_root(); /* Current node. */
1140 if (!_M_key_compare(_S_key(__x
), __k
))
1141 __y
= __x
, __x
= _S_left(__x
);
1143 __x
= _S_right(__x
);
1145 const_iterator __j
= const_iterator(__y
);
1146 return (__j
== end() || _M_key_compare(__k
, _S_key(__j
._M_node
))) ?
1150 template <class _Key
, class _Value
, class _KeyOfValue
,
1151 class _Compare
, class _Alloc
>
1152 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::size_type
1153 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1154 ::count(const _Key
& __k
) const
1156 pair
<const_iterator
, const_iterator
> __p
= equal_range(__k
);
1158 distance(__p
.first
, __p
.second
, __n
);
1162 template <class _Key
, class _Value
, class _KeyOfValue
,
1163 class _Compare
, class _Alloc
>
1164 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
1165 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1166 ::lower_bound(const _Key
& __k
)
1168 _Link_type __y
= _M_header
; /* Last node which is not less than __k. */
1169 _Link_type __x
= _M_root(); /* Current node. */
1172 if (!_M_key_compare(_S_key(__x
), __k
))
1173 __y
= __x
, __x
= _S_left(__x
);
1175 __x
= _S_right(__x
);
1177 return iterator(__y
);
1180 template <class _Key
, class _Value
, class _KeyOfValue
,
1181 class _Compare
, class _Alloc
>
1182 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::const_iterator
1183 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1184 ::lower_bound(const _Key
& __k
) const
1186 _Link_type __y
= _M_header
; /* Last node which is not less than __k. */
1187 _Link_type __x
= _M_root(); /* Current node. */
1190 if (!_M_key_compare(_S_key(__x
), __k
))
1191 __y
= __x
, __x
= _S_left(__x
);
1193 __x
= _S_right(__x
);
1195 return const_iterator(__y
);
1198 template <class _Key
, class _Value
, class _KeyOfValue
,
1199 class _Compare
, class _Alloc
>
1200 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
1201 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1202 ::upper_bound(const _Key
& __k
)
1204 _Link_type __y
= _M_header
; /* Last node which is greater than __k. */
1205 _Link_type __x
= _M_root(); /* Current node. */
1208 if (_M_key_compare(__k
, _S_key(__x
)))
1209 __y
= __x
, __x
= _S_left(__x
);
1211 __x
= _S_right(__x
);
1213 return iterator(__y
);
1216 template <class _Key
, class _Value
, class _KeyOfValue
,
1217 class _Compare
, class _Alloc
>
1218 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::const_iterator
1219 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1220 ::upper_bound(const _Key
& __k
) const
1222 _Link_type __y
= _M_header
; /* Last node which is greater than __k. */
1223 _Link_type __x
= _M_root(); /* Current node. */
1226 if (_M_key_compare(__k
, _S_key(__x
)))
1227 __y
= __x
, __x
= _S_left(__x
);
1229 __x
= _S_right(__x
);
1231 return const_iterator(__y
);
1234 template <class _Key
, class _Value
, class _KeyOfValue
,
1235 class _Compare
, class _Alloc
>
1237 pair
<typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
,
1238 typename _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
>
1239 _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>
1240 ::equal_range(const _Key
& __k
)
1242 return pair
<iterator
, iterator
>(lower_bound(__k
), upper_bound(__k
));
1245 template <class _Key
, class _Value
, class _KoV
, class _Compare
, class _Alloc
>
1247 pair
<typename _Rb_tree
<_Key
, _Value
, _KoV
, _Compare
, _Alloc
>::const_iterator
,
1248 typename _Rb_tree
<_Key
, _Value
, _KoV
, _Compare
, _Alloc
>::const_iterator
>
1249 _Rb_tree
<_Key
, _Value
, _KoV
, _Compare
, _Alloc
>
1250 ::equal_range(const _Key
& __k
) const
1252 return pair
<const_iterator
,const_iterator
>(lower_bound(__k
),
1257 __black_count(_Rb_tree_node_base
* __node
, _Rb_tree_node_base
* __root
)
1262 int __bc
= __node
->_M_color
== _S_rb_tree_black
? 1 : 0;
1263 if (__node
== __root
)
1266 return __bc
+ __black_count(__node
->_M_parent
, __root
);
1270 template <class _Key
, class _Value
, class _KeyOfValue
,
1271 class _Compare
, class _Alloc
>
1272 bool _Rb_tree
<_Key
,_Value
,_KeyOfValue
,_Compare
,_Alloc
>::__rb_verify() const
1274 if (_M_node_count
== 0 || begin() == end())
1275 return _M_node_count
== 0 && begin() == end() &&
1276 _M_header
->_M_left
== _M_header
&& _M_header
->_M_right
== _M_header
;
1278 int __len
= __black_count(_M_leftmost(), _M_root());
1279 for (const_iterator __it
= begin(); __it
!= end(); ++__it
) {
1280 _Link_type __x
= (_Link_type
) __it
._M_node
;
1281 _Link_type __L
= _S_left(__x
);
1282 _Link_type __R
= _S_right(__x
);
1284 if (__x
->_M_color
== _S_rb_tree_red
)
1285 if ((__L
&& __L
->_M_color
== _S_rb_tree_red
) ||
1286 (__R
&& __R
->_M_color
== _S_rb_tree_red
))
1289 if (__L
&& _M_key_compare(_S_key(__x
), _S_key(__L
)))
1291 if (__R
&& _M_key_compare(_S_key(__R
), _S_key(__x
)))
1294 if (!__L
&& !__R
&& __black_count(__x
, _M_root()) != __len
)
1298 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1300 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1306 // Class rb_tree is not part of the C++ standard. It is provided for
1307 // compatibility with the HP STL.
1309 template <class _Key
, class _Value
, class _KeyOfValue
, class _Compare
,
1310 class _Alloc
= __STL_DEFAULT_ALLOCATOR(_Value
) >
1311 struct rb_tree
: public _Rb_tree
<_Key
, _Value
, _KeyOfValue
, _Compare
, _Alloc
>
1313 typedef _Rb_tree
<_Key
, _Value
, _KeyOfValue
, _Compare
, _Alloc
> _Base
;
1314 typedef typename
_Base::allocator_type allocator_type
;
1316 rb_tree(const _Compare
& __comp
= _Compare(),
1317 const allocator_type
& __a
= allocator_type())
1318 : _Base(__comp
, __a
) {}
1323 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
1324 #pragma reset woff 1375
1329 #endif /* __SGI_STL_INTERNAL_TREE_H */