1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Copyright (c) 1996,1997
33 * Silicon Graphics Computer Systems, Inc.
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Silicon Graphics makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
45 * Hewlett-Packard Company
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Hewlett-Packard Company makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
59 * This is an internal header file, included by other library headers.
60 * You should not attempt to use it directly.
68 Red-black tree class, designed for use in implementing STL
69 associative containers (set, multiset, map, and multimap). The
70 insertion and deletion algorithms are based on those in Cormen,
71 Leiserson, and Rivest, Introduction to Algorithms (MIT Press, 1990),
74 (1) the header cell is maintained with links not only to the root
75 but also to the leftmost node of the tree, to enable constant time
76 begin(), and to the rightmost node of the tree, to enable linear time
77 performance when used with the generic set algorithms (set_union,
80 (2) when a node being deleted has two children its successor node is
81 relinked into its place, rather than copied, so that the only
82 iterators invalidated are those referring to the deleted node.
86 #include <bits/stl_algobase.h>
87 #include <bits/allocator.h>
88 #include <bits/stl_construct.h>
89 #include <bits/stl_function.h>
93 enum _Rb_tree_color
{ _S_red
= false, _S_black
= true };
95 struct _Rb_tree_node_base
97 typedef _Rb_tree_node_base
* _Base_ptr
;
99 _Rb_tree_color _M_color
;
105 _S_minimum(_Base_ptr __x
)
107 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
112 _S_maximum(_Base_ptr __x
)
114 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
119 template<typename _Val
>
120 struct _Rb_tree_node
: public _Rb_tree_node_base
122 typedef _Rb_tree_node
<_Val
>* _Link_type
;
126 struct _Rb_tree_base_iterator
128 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr
;
129 typedef bidirectional_iterator_tag iterator_category
;
130 typedef ptrdiff_t difference_type
;
141 template<typename _Val
, typename _Ref
, typename _Ptr
>
142 struct _Rb_tree_iterator
: public _Rb_tree_base_iterator
144 typedef _Val value_type
;
145 typedef _Ref reference
;
146 typedef _Ptr pointer
;
147 typedef _Rb_tree_iterator
<_Val
, _Val
&, _Val
*> iterator
;
148 typedef _Rb_tree_iterator
<_Val
, const _Val
&, const _Val
*>
150 typedef _Rb_tree_iterator
<_Val
, _Ref
, _Ptr
> _Self
;
151 typedef _Rb_tree_node
<_Val
>* _Link_type
;
153 _Rb_tree_iterator() {}
154 _Rb_tree_iterator(_Rb_tree_node_base
* __x
) { _M_node
= __x
; }
155 _Rb_tree_iterator(const iterator
& __it
) { _M_node
= __it
._M_node
; }
158 operator*() const { return _Link_type(_M_node
)->_M_value_field
; }
161 operator->() const { return &(operator*()); }
179 operator--() { _M_decrement(); return *this; }
190 template<typename _Val
, typename _Ref
, typename _Ptr
>
192 operator==(const _Rb_tree_iterator
<_Val
, _Ref
, _Ptr
>& __x
,
193 const _Rb_tree_iterator
<_Val
, _Ref
, _Ptr
>& __y
)
194 { return __x
._M_node
== __y
._M_node
; }
196 template<typename _Val
>
198 operator==(const _Rb_tree_iterator
<_Val
, const _Val
&, const _Val
*>& __x
,
199 const _Rb_tree_iterator
<_Val
, _Val
&, _Val
*>& __y
)
200 { return __x
._M_node
== __y
._M_node
; }
202 template<typename _Val
>
204 operator==(const _Rb_tree_iterator
<_Val
, _Val
&, _Val
*>& __x
,
205 const _Rb_tree_iterator
<_Val
, const _Val
&, const _Val
*>& __y
)
206 { return __x
._M_node
== __y
._M_node
; }
208 template<typename _Val
, typename _Ref
, typename _Ptr
>
210 operator!=(const _Rb_tree_iterator
<_Val
, _Ref
, _Ptr
>& __x
,
211 const _Rb_tree_iterator
<_Val
, _Ref
, _Ptr
>& __y
)
212 { return __x
._M_node
!= __y
._M_node
; }
214 template<typename _Val
>
216 operator!=(const _Rb_tree_iterator
<_Val
, const _Val
&, const _Val
*>& __x
,
217 const _Rb_tree_iterator
<_Val
, _Val
&, _Val
*>& __y
)
218 { return __x
._M_node
!= __y
._M_node
; }
220 template<typename _Val
>
222 operator!=(const _Rb_tree_iterator
<_Val
, _Val
&, _Val
*>& __x
,
223 const _Rb_tree_iterator
<_Val
, const _Val
&, const _Val
*>& __y
)
224 { return __x
._M_node
!= __y
._M_node
; }
227 _Rb_tree_rotate_left(_Rb_tree_node_base
* const __x
, _Rb_tree_node_base
*& __root
);
230 _Rb_tree_rotate_right(_Rb_tree_node_base
* const __x
, _Rb_tree_node_base
*& __root
);
233 _Rb_tree_rebalance(_Rb_tree_node_base
* __x
, _Rb_tree_node_base
*& __root
);
236 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base
* const __z
,
237 _Rb_tree_node_base
& __header
);
239 // Base class to encapsulate the differences between old SGI-style
240 // allocators and standard-conforming allocators. In order to avoid
241 // having an empty base class, we arbitrarily move one of rb_tree's
242 // data members into the base class.
244 // _Base for general standard-conforming allocators.
245 template<typename _Tp
, typename _Alloc
, bool _S_instanceless
>
246 class _Rb_tree_alloc_base
249 typedef typename _Alloc_traits
<_Tp
, _Alloc
>::allocator_type allocator_type
;
252 get_allocator() const { return _M_node_allocator
; }
254 _Rb_tree_alloc_base(const allocator_type
& __a
)
255 : _M_node_allocator(__a
) {}
258 typename _Alloc_traits
<_Rb_tree_node
<_Tp
>, _Alloc
>::allocator_type
261 _Rb_tree_node_base _M_header
;
264 _M_get_node() { return _M_node_allocator
.allocate(1); }
267 _M_put_node(_Rb_tree_node
<_Tp
>* __p
)
268 { _M_node_allocator
.deallocate(__p
, 1); }
271 // Specialization for instanceless allocators.
272 template<typename _Tp
, typename _Alloc
>
273 class _Rb_tree_alloc_base
<_Tp
, _Alloc
, true>
276 typedef typename _Alloc_traits
<_Tp
, _Alloc
>::allocator_type allocator_type
;
277 allocator_type
get_allocator() const { return allocator_type(); }
279 _Rb_tree_alloc_base(const allocator_type
&) {}
282 _Rb_tree_node_base _M_header
;
284 typedef typename _Alloc_traits
<_Rb_tree_node
<_Tp
>, _Alloc
>::_Alloc_type
288 _M_get_node() { return _Alloc_type::allocate(1); }
291 _M_put_node(_Rb_tree_node
<_Tp
>* __p
) { _Alloc_type::deallocate(__p
, 1); }
294 template<typename _Tp
, typename _Alloc
>
295 struct _Rb_tree_base
: public _Rb_tree_alloc_base
<_Tp
, _Alloc
,
296 _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
>
298 typedef _Rb_tree_alloc_base
<_Tp
,
299 _Alloc
, _Alloc_traits
<_Tp
, _Alloc
>::_S_instanceless
> _Base
;
300 typedef typename
_Base::allocator_type allocator_type
;
302 _Rb_tree_base(const allocator_type
& __a
)
307 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
308 typename _Compare
, typename _Alloc
= allocator
<_Val
> >
309 class _Rb_tree
: protected _Rb_tree_base
<_Val
, _Alloc
>
311 typedef _Rb_tree_base
<_Val
, _Alloc
> _Base
;
314 typedef _Rb_tree_node_base
* _Base_ptr
;
315 typedef _Rb_tree_node
<_Val
> _Rb_tree_node
;
318 typedef _Key key_type
;
319 typedef _Val value_type
;
320 typedef value_type
* pointer
;
321 typedef const value_type
* const_pointer
;
322 typedef value_type
& reference
;
323 typedef const value_type
& const_reference
;
324 typedef _Rb_tree_node
* _Link_type
;
325 typedef size_t size_type
;
326 typedef ptrdiff_t difference_type
;
328 typedef typename
_Base::allocator_type allocator_type
;
329 allocator_type
get_allocator() const { return _Base::get_allocator(); }
332 using _Base::_M_get_node
;
333 using _Base::_M_put_node
;
334 using _Base::_M_header
;
337 _M_create_node(const value_type
& __x
)
339 _Link_type __tmp
= this->_M_get_node();
341 { std::_Construct(&__tmp
->_M_value_field
, __x
); }
345 __throw_exception_again
;
351 _M_clone_node(_Link_type __x
)
353 _Link_type __tmp
= _M_create_node(__x
->_M_value_field
);
354 __tmp
->_M_color
= __x
->_M_color
;
361 destroy_node(_Link_type __p
)
363 std::_Destroy(&__p
->_M_value_field
);
367 size_type _M_node_count
; // keeps track of size of tree
368 _Compare _M_key_compare
;
371 _M_root() const { return (_Link_type
&) this->_M_header
._M_parent
; }
374 _M_leftmost() const { return (_Link_type
&) this->_M_header
._M_left
; }
377 _M_rightmost() const { return (_Link_type
&) this->_M_header
._M_right
; }
380 _M_end() const { return (_Link_type
) &this->_M_header
; }
383 _S_left(_Link_type __x
) { return (_Link_type
&)(__x
->_M_left
); }
386 _S_right(_Link_type __x
) { return (_Link_type
&)(__x
->_M_right
); }
389 _S_parent(_Link_type __x
) { return (_Link_type
&)(__x
->_M_parent
); }
392 _S_value(_Link_type __x
) { return __x
->_M_value_field
; }
395 _S_key(_Link_type __x
) { return _KeyOfValue()(_S_value(__x
)); }
398 _S_left(_Base_ptr __x
) { return (_Link_type
&)(__x
->_M_left
); }
401 _S_right(_Base_ptr __x
) { return (_Link_type
&)(__x
->_M_right
); }
404 _S_parent(_Base_ptr __x
) { return (_Link_type
&)(__x
->_M_parent
); }
407 _S_value(_Base_ptr __x
) { return ((_Link_type
)__x
)->_M_value_field
; }
410 _S_key(_Base_ptr __x
) { return _KeyOfValue()(_S_value(_Link_type(__x
)));}
412 static _Rb_tree_color
&
413 _S_color(_Base_ptr __x
) { return __x
->_M_color
; }
416 _S_minimum(_Link_type __x
)
417 { return (_Link_type
) _Rb_tree_node_base::_S_minimum(__x
); }
420 _S_maximum(_Link_type __x
)
421 { return (_Link_type
) _Rb_tree_node_base::_S_maximum(__x
); }
424 typedef _Rb_tree_iterator
<value_type
, reference
, pointer
> iterator
;
425 typedef _Rb_tree_iterator
<value_type
, const_reference
, const_pointer
>
428 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
429 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
433 _M_insert(_Base_ptr __x
, _Base_ptr __y
, const value_type
& __v
);
436 _M_copy(_Link_type __x
, _Link_type __p
);
439 _M_erase(_Link_type __x
);
442 // allocation/deallocation
444 : _Base(allocator_type()), _M_node_count(0), _M_key_compare()
445 { _M_empty_initialize(); }
447 _Rb_tree(const _Compare
& __comp
)
448 : _Base(allocator_type()), _M_node_count(0), _M_key_compare(__comp
)
449 { _M_empty_initialize(); }
451 _Rb_tree(const _Compare
& __comp
, const allocator_type
& __a
)
452 : _Base(__a
), _M_node_count(0), _M_key_compare(__comp
)
453 { _M_empty_initialize(); }
455 _Rb_tree(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
)
456 : _Base(__x
.get_allocator()), _M_node_count(0),
457 _M_key_compare(__x
._M_key_compare
)
459 if (__x
._M_root() == 0)
460 _M_empty_initialize();
463 _S_color(&this->_M_header
) = _S_red
;
464 _M_root() = _M_copy(__x
._M_root(), _M_end());
465 _M_leftmost() = _S_minimum(_M_root());
466 _M_rightmost() = _S_maximum(_M_root());
468 _M_node_count
= __x
._M_node_count
;
471 ~_Rb_tree() { clear(); }
473 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>&
474 operator=(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
);
477 void _M_empty_initialize()
479 // Used to distinguish header from __root, in iterator.operator++.
480 _S_color(&this->_M_header
) = _S_red
;
482 _M_leftmost() = _M_end();
483 _M_rightmost() = _M_end();
489 key_comp() const { return _M_key_compare
; }
492 begin() { return _M_leftmost(); }
495 begin() const { return _M_leftmost(); }
498 end() { return &this->_M_header
; }
501 end() const { return const_cast<_Base_ptr
>(&this->_M_header
); }
504 rbegin() { return reverse_iterator(end()); }
506 const_reverse_iterator
507 rbegin() const { return const_reverse_iterator(end()); }
510 rend() { return reverse_iterator(begin()); }
512 const_reverse_iterator
513 rend() const { return const_reverse_iterator(begin()); }
516 empty() const { return _M_node_count
== 0; }
519 size() const { return _M_node_count
; }
522 max_size() const { return size_type(-1); }
525 swap(_Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __t
);
529 insert_unique(const value_type
& __x
);
532 insert_equal(const value_type
& __x
);
535 insert_unique(iterator __position
, const value_type
& __x
);
538 insert_equal(iterator __position
, const value_type
& __x
);
540 template<typename _InputIterator
>
542 insert_unique(_InputIterator __first
, _InputIterator __last
);
544 template<typename _InputIterator
>
546 insert_equal(_InputIterator __first
, _InputIterator __last
);
549 erase(iterator __position
);
552 erase(const key_type
& __x
);
555 erase(iterator __first
, iterator __last
);
558 erase(const key_type
* __first
, const key_type
* __last
);
563 if (_M_node_count
!= 0)
566 _M_leftmost() = _M_end();
568 _M_rightmost() = _M_end();
575 find(const key_type
& __x
);
578 find(const key_type
& __x
) const;
581 count(const key_type
& __x
) const;
584 lower_bound(const key_type
& __x
);
587 lower_bound(const key_type
& __x
) const;
590 upper_bound(const key_type
& __x
);
593 upper_bound(const key_type
& __x
) const;
595 pair
<iterator
,iterator
>
596 equal_range(const key_type
& __x
);
598 pair
<const_iterator
, const_iterator
>
599 equal_range(const key_type
& __x
) const;
606 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
607 typename _Compare
, typename _Alloc
>
609 operator==(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
610 const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
612 return __x
.size() == __y
.size() &&
613 equal(__x
.begin(), __x
.end(), __y
.begin());
616 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
617 typename _Compare
, typename _Alloc
>
619 operator<(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
620 const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
622 return lexicographical_compare(__x
.begin(), __x
.end(),
623 __y
.begin(), __y
.end());
626 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
627 typename _Compare
, typename _Alloc
>
629 operator!=(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
630 const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
631 { return !(__x
== __y
); }
633 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
634 typename _Compare
, typename _Alloc
>
636 operator>(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
637 const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
638 { return __y
< __x
; }
640 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
641 typename _Compare
, typename _Alloc
>
643 operator<=(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
644 const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
645 { return !(__y
< __x
); }
647 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
648 typename _Compare
, typename _Alloc
>
650 operator>=(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
651 const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
652 { return !(__x
< __y
); }
654 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
655 typename _Compare
, typename _Alloc
>
657 swap(_Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
,
658 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __y
)
661 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
662 typename _Compare
, typename _Alloc
>
663 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>&
664 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
665 operator=(const _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __x
)
669 // Note that _Key may be a constant type.
672 _M_key_compare
= __x
._M_key_compare
;
673 if (__x
._M_root() == 0)
676 _M_leftmost() = _M_end();
677 _M_rightmost() = _M_end();
681 _M_root() = _M_copy(__x
._M_root(), _M_end());
682 _M_leftmost() = _S_minimum(_M_root());
683 _M_rightmost() = _S_maximum(_M_root());
684 _M_node_count
= __x
._M_node_count
;
690 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
691 typename _Compare
, typename _Alloc
>
692 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
693 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
694 _M_insert(_Base_ptr __x_
, _Base_ptr __y_
, const _Val
& __v
)
696 _Link_type __x
= (_Link_type
) __x_
;
697 _Link_type __y
= (_Link_type
) __y_
;
700 if (__y
== &this->_M_header
|| __x
!= 0 ||
701 _M_key_compare(_KeyOfValue()(__v
), _S_key(__y
)))
703 __z
= _M_create_node(__v
);
704 _S_left(__y
) = __z
; // also makes _M_leftmost() = __z
705 // when __y == &_M_header
706 if (__y
== &this->_M_header
)
709 _M_rightmost() = __z
;
711 else if (__y
== _M_leftmost())
712 _M_leftmost() = __z
; // maintain _M_leftmost() pointing to min node
716 __z
= _M_create_node(__v
);
718 // Maintain _M_rightmost() pointing to max node.
719 if (__y
== _M_rightmost())
720 _M_rightmost() = __z
;
722 _S_parent(__z
) = __y
;
725 _Rb_tree_rebalance(__z
, this->_M_header
._M_parent
);
727 return iterator(__z
);
730 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
731 typename _Compare
, typename _Alloc
>
732 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
733 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
734 insert_equal(const _Val
& __v
)
736 _Link_type __y
= _M_end();
737 _Link_type __x
= _M_root();
741 __x
= _M_key_compare(_KeyOfValue()(__v
), _S_key(__x
)) ?
742 _S_left(__x
) : _S_right(__x
);
744 return _M_insert(__x
, __y
, __v
);
747 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
748 typename _Compare
, typename _Alloc
>
750 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
751 swap(_Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>& __t
)
755 if (__t
._M_root() != 0)
757 _M_root() = __t
._M_root();
758 _M_leftmost() = __t
._M_leftmost();
759 _M_rightmost() = __t
._M_rightmost();
760 _M_root()->_M_parent
= _M_end();
763 __t
._M_leftmost() = __t
._M_end();
764 __t
._M_rightmost() = __t
._M_end();
767 else if (__t
._M_root() == 0)
769 __t
._M_root() = _M_root();
770 __t
._M_leftmost() = _M_leftmost();
771 __t
._M_rightmost() = _M_rightmost();
772 __t
._M_root()->_M_parent
= __t
._M_end();
775 _M_leftmost() = _M_end();
776 _M_rightmost() = _M_end();
780 std::swap(_M_root(),__t
._M_root());
781 std::swap(_M_leftmost(),__t
._M_leftmost());
782 std::swap(_M_rightmost(),__t
._M_rightmost());
784 _M_root()->_M_parent
= _M_end();
785 __t
._M_root()->_M_parent
= __t
._M_end();
787 // No need to swap header's color as it does not change.
788 std::swap(this->_M_node_count
, __t
._M_node_count
);
789 std::swap(this->_M_key_compare
, __t
._M_key_compare
);
792 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
793 typename _Compare
, typename _Alloc
>
794 pair
<typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
,
796 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
797 insert_unique(const _Val
& __v
)
799 _Link_type __y
= _M_end();
800 _Link_type __x
= _M_root();
805 __comp
= _M_key_compare(_KeyOfValue()(__v
), _S_key(__x
));
806 __x
= __comp
? _S_left(__x
) : _S_right(__x
);
808 iterator __j
= iterator(__y
);
811 return pair
<iterator
,bool>(_M_insert(__x
, __y
, __v
), true);
814 if (_M_key_compare(_S_key(__j
._M_node
), _KeyOfValue()(__v
)))
815 return pair
<iterator
,bool>(_M_insert(__x
, __y
, __v
), true);
816 return pair
<iterator
,bool>(__j
, false);
820 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
821 typename _Compare
, typename _Alloc
>
822 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
823 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
824 insert_unique(iterator __position
, const _Val
& __v
)
826 if (__position
._M_node
== this->_M_header
._M_left
)
830 _M_key_compare(_KeyOfValue()(__v
), _S_key(__position
._M_node
)))
831 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
832 // first argument just needs to be non-null
834 return insert_unique(__v
).first
;
836 else if (__position
._M_node
== &this->_M_header
)
839 if (_M_key_compare(_S_key(_M_rightmost()), _KeyOfValue()(__v
)))
840 return _M_insert(0, _M_rightmost(), __v
);
842 return insert_unique(__v
).first
;
846 iterator __before
= __position
;
848 if (_M_key_compare(_S_key(__before
._M_node
), _KeyOfValue()(__v
))
849 && _M_key_compare(_KeyOfValue()(__v
),_S_key(__position
._M_node
)))
851 if (_S_right(__before
._M_node
) == 0)
852 return _M_insert(0, __before
._M_node
, __v
);
854 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
855 // first argument just needs to be non-null
858 return insert_unique(__v
).first
;
862 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
863 typename _Compare
, typename _Alloc
>
864 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
865 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
866 insert_equal(iterator __position
, const _Val
& __v
)
868 if (__position
._M_node
== this->_M_header
._M_left
)
872 !_M_key_compare(_S_key(__position
._M_node
), _KeyOfValue()(__v
)))
873 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
874 // first argument just needs to be non-null
876 return insert_equal(__v
);
878 else if (__position
._M_node
== &this->_M_header
)
881 if (!_M_key_compare(_KeyOfValue()(__v
), _S_key(_M_rightmost())))
882 return _M_insert(0, _M_rightmost(), __v
);
884 return insert_equal(__v
);
888 iterator __before
= __position
;
890 if (!_M_key_compare(_KeyOfValue()(__v
), _S_key(__before
._M_node
))
891 && !_M_key_compare(_S_key(__position
._M_node
),
894 if (_S_right(__before
._M_node
) == 0)
895 return _M_insert(0, __before
._M_node
, __v
);
897 return _M_insert(__position
._M_node
, __position
._M_node
, __v
);
898 // first argument just needs to be non-null
901 return insert_equal(__v
);
905 template<typename _Key
, typename _Val
, typename _KoV
,
906 typename _Cmp
, typename _Alloc
>
909 _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>::
910 insert_equal(_II __first
, _II __last
)
912 for ( ; __first
!= __last
; ++__first
)
913 insert_equal(*__first
);
916 template<typename _Key
, typename _Val
, typename _KoV
,
917 typename _Cmp
, typename _Alloc
>
920 _Rb_tree
<_Key
,_Val
,_KoV
,_Cmp
,_Alloc
>::
921 insert_unique(_II __first
, _II __last
)
923 for ( ; __first
!= __last
; ++__first
)
924 insert_unique(*__first
);
927 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
928 typename _Compare
, typename _Alloc
>
930 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::erase(iterator __position
)
933 (_Link_type
) _Rb_tree_rebalance_for_erase(__position
._M_node
,
939 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
940 typename _Compare
, typename _Alloc
>
941 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::size_type
942 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::erase(const _Key
& __x
)
944 pair
<iterator
,iterator
> __p
= equal_range(__x
);
945 size_type __n
= std::distance(__p
.first
, __p
.second
);
946 erase(__p
.first
, __p
.second
);
950 template<typename _Key
, typename _Val
, typename _KoV
,
951 typename _Compare
, typename _Alloc
>
952 typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::_Link_type
953 _Rb_tree
<_Key
,_Val
,_KoV
,_Compare
,_Alloc
>::
954 _M_copy(_Link_type __x
, _Link_type __p
)
956 // Structural copy. __x and __p must be non-null.
957 _Link_type __top
= _M_clone_node(__x
);
958 __top
->_M_parent
= __p
;
963 __top
->_M_right
= _M_copy(_S_right(__x
), __top
);
969 _Link_type __y
= _M_clone_node(__x
);
971 __y
->_M_parent
= __p
;
973 __y
->_M_right
= _M_copy(_S_right(__x
), __y
);
981 __throw_exception_again
;
986 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
987 typename _Compare
, typename _Alloc
>
989 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::_M_erase(_Link_type __x
)
991 // Erase without rebalancing.
994 _M_erase(_S_right(__x
));
995 _Link_type __y
= _S_left(__x
);
1001 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1002 typename _Compare
, typename _Alloc
>
1004 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1005 erase(iterator __first
, iterator __last
)
1007 if (__first
== begin() && __last
== end())
1010 while (__first
!= __last
) erase(__first
++);
1013 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1014 typename _Compare
, typename _Alloc
>
1016 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1017 erase(const _Key
* __first
, const _Key
* __last
)
1019 while (__first
!= __last
)
1023 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1024 typename _Compare
, typename _Alloc
>
1025 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
1026 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::find(const _Key
& __k
)
1028 _Link_type __y
= _M_end(); // Last node which is not less than __k.
1029 _Link_type __x
= _M_root(); // Current node.
1032 if (!_M_key_compare(_S_key(__x
), __k
))
1033 __y
= __x
, __x
= _S_left(__x
);
1035 __x
= _S_right(__x
);
1037 iterator __j
= iterator(__y
);
1038 return (__j
== end() || _M_key_compare(__k
, _S_key(__j
._M_node
))) ?
1042 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1043 typename _Compare
, typename _Alloc
>
1044 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::const_iterator
1045 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1046 find(const _Key
& __k
) const
1048 _Link_type __y
= _M_end(); // Last node which is not less than __k.
1049 _Link_type __x
= _M_root(); // Current node.
1053 if (!_M_key_compare(_S_key(__x
), __k
))
1054 __y
= __x
, __x
= _S_left(__x
);
1056 __x
= _S_right(__x
);
1058 const_iterator __j
= const_iterator(__y
);
1059 return (__j
== end() || _M_key_compare(__k
, _S_key(__j
._M_node
))) ?
1063 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1064 typename _Compare
, typename _Alloc
>
1065 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::size_type
1066 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1067 count(const _Key
& __k
) const
1069 pair
<const_iterator
, const_iterator
> __p
= equal_range(__k
);
1070 size_type __n
= std::distance(__p
.first
, __p
.second
);
1074 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1075 typename _Compare
, typename _Alloc
>
1076 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
1077 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1078 lower_bound(const _Key
& __k
)
1080 _Link_type __y
= _M_end(); // Last node which is not less than __k
1081 _Link_type __x
= _M_root(); // Current node.
1084 if (!_M_key_compare(_S_key(__x
), __k
))
1085 __y
= __x
, __x
= _S_left(__x
);
1087 __x
= _S_right(__x
);
1089 return iterator(__y
);
1092 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1093 typename _Compare
, typename _Alloc
>
1094 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::const_iterator
1095 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1096 lower_bound(const _Key
& __k
) const
1098 _Link_type __y
= _M_end(); // Last node which is not less than __k.
1099 _Link_type __x
= _M_root(); // Current node.
1102 if (!_M_key_compare(_S_key(__x
), __k
))
1103 __y
= __x
, __x
= _S_left(__x
);
1105 __x
= _S_right(__x
);
1107 return const_iterator(__y
);
1110 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1111 typename _Compare
, typename _Alloc
>
1112 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
1113 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1114 upper_bound(const _Key
& __k
)
1116 _Link_type __y
= _M_end(); // Last node which is greater than __k.
1117 _Link_type __x
= _M_root(); // Current node.
1120 if (_M_key_compare(__k
, _S_key(__x
)))
1121 __y
= __x
, __x
= _S_left(__x
);
1123 __x
= _S_right(__x
);
1125 return iterator(__y
);
1128 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1129 typename _Compare
, typename _Alloc
>
1130 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::const_iterator
1131 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1132 upper_bound(const _Key
& __k
) const
1134 _Link_type __y
= _M_end(); // Last node which is greater than __k.
1135 _Link_type __x
= _M_root(); // Current node.
1138 if (_M_key_compare(__k
, _S_key(__x
)))
1139 __y
= __x
, __x
= _S_left(__x
);
1141 __x
= _S_right(__x
);
1143 return const_iterator(__y
);
1146 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1147 typename _Compare
, typename _Alloc
>
1149 pair
<typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
,
1150 typename _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::iterator
>
1151 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::
1152 equal_range(const _Key
& __k
)
1153 { return pair
<iterator
, iterator
>(lower_bound(__k
), upper_bound(__k
)); }
1155 template<typename _Key
, typename _Val
, typename _KoV
,
1156 typename _Compare
, typename _Alloc
>
1158 pair
<typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::const_iterator
,
1159 typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::const_iterator
>
1160 _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>
1161 ::equal_range(const _Key
& __k
) const
1163 return pair
<const_iterator
,const_iterator
>(lower_bound(__k
),
1168 __black_count(_Rb_tree_node_base
* __node
, _Rb_tree_node_base
* __root
)
1175 if (__node
->_M_color
== _S_black
)
1177 if (__node
== __root
)
1179 __node
= __node
->_M_parent
;
1185 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1186 typename _Compare
, typename _Alloc
>
1188 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::__rb_verify() const
1190 if (_M_node_count
== 0 || begin() == end())
1191 return _M_node_count
== 0 && begin() == end() &&
1192 this->_M_header
._M_left
== &this->_M_header
&&
1193 this->_M_header
._M_right
== &this->_M_header
;
1195 int __len
= __black_count(_M_leftmost(), _M_root());
1196 for (const_iterator __it
= begin(); __it
!= end(); ++__it
)
1198 _Link_type __x
= (_Link_type
) __it
._M_node
;
1199 _Link_type __L
= _S_left(__x
);
1200 _Link_type __R
= _S_right(__x
);
1202 if (__x
->_M_color
== _S_red
)
1203 if ((__L
&& __L
->_M_color
== _S_red
)
1204 || (__R
&& __R
->_M_color
== _S_red
))
1207 if (__L
&& _M_key_compare(_S_key(__x
), _S_key(__L
)))
1209 if (__R
&& _M_key_compare(_S_key(__R
), _S_key(__x
)))
1212 if (!__L
&& !__R
&& __black_count(__x
, _M_root()) != __len
)
1216 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1218 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))