1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
33 * Copyright (c) 1996,1997
34 * Silicon Graphics Computer Systems, Inc.
36 * Permission to use, copy, modify, distribute and sell this software
37 * and its documentation for any purpose is hereby granted without fee,
38 * provided that the above copyright notice appear in all copies and
39 * that both that copyright notice and this permission notice appear
40 * in supporting documentation. Silicon Graphics makes no
41 * representations about the suitability of this software for any
42 * purpose. It is provided "as is" without express or implied warranty.
46 * Hewlett-Packard Company
48 * Permission to use, copy, modify, distribute and sell this software
49 * and its documentation for any purpose is hereby granted without fee,
50 * provided that the above copyright notice appear in all copies and
51 * that both that copyright notice and this permission notice appear
52 * in supporting documentation. Hewlett-Packard Company makes no
53 * representations about the suitability of this software for any
54 * purpose. It is provided "as is" without express or implied warranty.
60 * This is an internal header file, included by other library headers.
61 * You should not attempt to use it directly.
67 #include <bits/stl_algobase.h>
68 #include <bits/allocator.h>
69 #include <bits/stl_function.h>
70 #include <bits/cpp_type_traits.h>
72 _GLIBCXX_BEGIN_NAMESPACE(std
)
74 // Red-black tree class, designed for use in implementing STL
75 // associative containers (set, multiset, map, and multimap). The
76 // insertion and deletion algorithms are based on those in Cormen,
77 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
80 // (1) the header cell is maintained with links not only to the root
81 // but also to the leftmost node of the tree, to enable constant
82 // time begin(), and to the rightmost node of the tree, to enable
83 // linear time performance when used with the generic set algorithms
86 // (2) when a node being deleted has two children its successor node
87 // is relinked into its place, rather than copied, so that the only
88 // iterators invalidated are those referring to the deleted node.
90 enum _Rb_tree_color
{ _S_red
= false, _S_black
= true };
92 struct _Rb_tree_node_base
94 typedef _Rb_tree_node_base
* _Base_ptr
;
95 typedef const _Rb_tree_node_base
* _Const_Base_ptr
;
97 _Rb_tree_color _M_color
;
103 _S_minimum(_Base_ptr __x
)
105 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
109 static _Const_Base_ptr
110 _S_minimum(_Const_Base_ptr __x
)
112 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
117 _S_maximum(_Base_ptr __x
)
119 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
123 static _Const_Base_ptr
124 _S_maximum(_Const_Base_ptr __x
)
126 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
131 template<typename _Val
>
132 struct _Rb_tree_node
: public _Rb_tree_node_base
134 typedef _Rb_tree_node
<_Val
>* _Link_type
;
139 _Rb_tree_increment(_Rb_tree_node_base
* __x
);
141 const _Rb_tree_node_base
*
142 _Rb_tree_increment(const _Rb_tree_node_base
* __x
);
145 _Rb_tree_decrement(_Rb_tree_node_base
* __x
);
147 const _Rb_tree_node_base
*
148 _Rb_tree_decrement(const _Rb_tree_node_base
* __x
);
150 template<typename _Tp
>
151 struct _Rb_tree_iterator
153 typedef _Tp value_type
;
154 typedef _Tp
& reference
;
155 typedef _Tp
* pointer
;
157 typedef bidirectional_iterator_tag iterator_category
;
158 typedef ptrdiff_t difference_type
;
160 typedef _Rb_tree_iterator
<_Tp
> _Self
;
161 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr
;
162 typedef _Rb_tree_node
<_Tp
>* _Link_type
;
168 _Rb_tree_iterator(_Link_type __x
)
173 { return static_cast<_Link_type
>(_M_node
)->_M_value_field
; }
177 { return &static_cast<_Link_type
>(_M_node
)->_M_value_field
; }
182 _M_node
= _Rb_tree_increment(_M_node
);
190 _M_node
= _Rb_tree_increment(_M_node
);
197 _M_node
= _Rb_tree_decrement(_M_node
);
205 _M_node
= _Rb_tree_decrement(_M_node
);
210 operator==(const _Self
& __x
) const
211 { return _M_node
== __x
._M_node
; }
214 operator!=(const _Self
& __x
) const
215 { return _M_node
!= __x
._M_node
; }
220 template<typename _Tp
>
221 struct _Rb_tree_const_iterator
223 typedef _Tp value_type
;
224 typedef const _Tp
& reference
;
225 typedef const _Tp
* pointer
;
227 typedef _Rb_tree_iterator
<_Tp
> iterator
;
229 typedef bidirectional_iterator_tag iterator_category
;
230 typedef ptrdiff_t difference_type
;
232 typedef _Rb_tree_const_iterator
<_Tp
> _Self
;
233 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr
;
234 typedef const _Rb_tree_node
<_Tp
>* _Link_type
;
236 _Rb_tree_const_iterator()
240 _Rb_tree_const_iterator(_Link_type __x
)
243 _Rb_tree_const_iterator(const iterator
& __it
)
244 : _M_node(__it
._M_node
) { }
248 { return static_cast<_Link_type
>(_M_node
)->_M_value_field
; }
252 { return &static_cast<_Link_type
>(_M_node
)->_M_value_field
; }
257 _M_node
= _Rb_tree_increment(_M_node
);
265 _M_node
= _Rb_tree_increment(_M_node
);
272 _M_node
= _Rb_tree_decrement(_M_node
);
280 _M_node
= _Rb_tree_decrement(_M_node
);
285 operator==(const _Self
& __x
) const
286 { return _M_node
== __x
._M_node
; }
289 operator!=(const _Self
& __x
) const
290 { return _M_node
!= __x
._M_node
; }
295 template<typename _Val
>
297 operator==(const _Rb_tree_iterator
<_Val
>& __x
,
298 const _Rb_tree_const_iterator
<_Val
>& __y
)
299 { return __x
._M_node
== __y
._M_node
; }
301 template<typename _Val
>
303 operator!=(const _Rb_tree_iterator
<_Val
>& __x
,
304 const _Rb_tree_const_iterator
<_Val
>& __y
)
305 { return __x
._M_node
!= __y
._M_node
; }
308 _Rb_tree_insert_and_rebalance(const bool __insert_left
,
309 _Rb_tree_node_base
* __x
,
310 _Rb_tree_node_base
* __p
,
311 _Rb_tree_node_base
& __header
);
314 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base
* const __z
,
315 _Rb_tree_node_base
& __header
);
318 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
319 typename _Compare
, typename _Alloc
= allocator
<_Val
> >
322 typedef typename
_Alloc::template rebind
<_Rb_tree_node
<_Val
> >::other
326 typedef _Rb_tree_node_base
* _Base_ptr
;
327 typedef const _Rb_tree_node_base
* _Const_Base_ptr
;
330 typedef _Key key_type
;
331 typedef _Val value_type
;
332 typedef value_type
* pointer
;
333 typedef const value_type
* const_pointer
;
334 typedef value_type
& reference
;
335 typedef const value_type
& const_reference
;
336 typedef _Rb_tree_node
<_Val
>* _Link_type
;
337 typedef const _Rb_tree_node
<_Val
>* _Const_Link_type
;
338 typedef size_t size_type
;
339 typedef ptrdiff_t difference_type
;
340 typedef _Alloc allocator_type
;
343 _M_get_Node_allocator()
344 { return *static_cast<_Node_allocator
*>(&this->_M_impl
); }
346 const _Node_allocator
&
347 _M_get_Node_allocator() const
348 { return *static_cast<const _Node_allocator
*>(&this->_M_impl
); }
351 get_allocator() const
352 { return allocator_type(_M_get_Node_allocator()); }
357 { return _M_impl
._Node_allocator::allocate(1); }
360 _M_put_node(_Link_type __p
)
361 { _M_impl
._Node_allocator::deallocate(__p
, 1); }
364 _M_create_node(const value_type
& __x
)
366 _Link_type __tmp
= _M_get_node();
368 { get_allocator().construct(&__tmp
->_M_value_field
, __x
); }
372 __throw_exception_again
;
378 _M_clone_node(_Const_Link_type __x
)
380 _Link_type __tmp
= _M_create_node(__x
->_M_value_field
);
381 __tmp
->_M_color
= __x
->_M_color
;
388 _M_destroy_node(_Link_type __p
)
390 get_allocator().destroy(&__p
->_M_value_field
);
395 template<typename _Key_compare
,
396 bool _Is_pod_comparator
= __is_pod(_Key_compare
)>
397 struct _Rb_tree_impl
: public _Node_allocator
399 _Key_compare _M_key_compare
;
400 _Rb_tree_node_base _M_header
;
401 size_type _M_node_count
; // Keeps track of size of tree.
404 : _Node_allocator(), _M_key_compare(), _M_header(),
408 _Rb_tree_impl(const _Key_compare
& __comp
, const _Node_allocator
& __a
)
409 : _Node_allocator(__a
), _M_key_compare(__comp
), _M_header(),
417 this->_M_header
._M_color
= _S_red
;
418 this->_M_header
._M_parent
= 0;
419 this->_M_header
._M_left
= &this->_M_header
;
420 this->_M_header
._M_right
= &this->_M_header
;
424 // Specialization for _Comparison types that are not capable of
425 // being base classes / super classes.
426 template<typename _Key_compare
>
427 struct _Rb_tree_impl
<_Key_compare
, true> : public _Node_allocator
429 _Key_compare _M_key_compare
;
430 _Rb_tree_node_base _M_header
;
431 size_type _M_node_count
; // Keeps track of size of tree.
434 : _Node_allocator(), _M_key_compare(), _M_header(),
438 _Rb_tree_impl(const _Key_compare
& __comp
, const _Node_allocator
& __a
)
439 : _Node_allocator(__a
), _M_key_compare(__comp
), _M_header(),
447 this->_M_header
._M_color
= _S_red
;
448 this->_M_header
._M_parent
= 0;
449 this->_M_header
._M_left
= &this->_M_header
;
450 this->_M_header
._M_right
= &this->_M_header
;
454 _Rb_tree_impl
<_Compare
> _M_impl
;
459 { return this->_M_impl
._M_header
._M_parent
; }
463 { return this->_M_impl
._M_header
._M_parent
; }
467 { return this->_M_impl
._M_header
._M_left
; }
471 { return this->_M_impl
._M_header
._M_left
; }
475 { return this->_M_impl
._M_header
._M_right
; }
479 { return this->_M_impl
._M_header
._M_right
; }
483 { return static_cast<_Link_type
>(this->_M_impl
._M_header
._M_parent
); }
488 return static_cast<_Const_Link_type
>
489 (this->_M_impl
._M_header
._M_parent
);
494 { return static_cast<_Link_type
>(&this->_M_impl
._M_header
); }
498 { return static_cast<_Const_Link_type
>(&this->_M_impl
._M_header
); }
500 static const_reference
501 _S_value(_Const_Link_type __x
)
502 { return __x
->_M_value_field
; }
505 _S_key(_Const_Link_type __x
)
506 { return _KeyOfValue()(_S_value(__x
)); }
509 _S_left(_Base_ptr __x
)
510 { return static_cast<_Link_type
>(__x
->_M_left
); }
512 static _Const_Link_type
513 _S_left(_Const_Base_ptr __x
)
514 { return static_cast<_Const_Link_type
>(__x
->_M_left
); }
517 _S_right(_Base_ptr __x
)
518 { return static_cast<_Link_type
>(__x
->_M_right
); }
520 static _Const_Link_type
521 _S_right(_Const_Base_ptr __x
)
522 { return static_cast<_Const_Link_type
>(__x
->_M_right
); }
524 static const_reference
525 _S_value(_Const_Base_ptr __x
)
526 { return static_cast<_Const_Link_type
>(__x
)->_M_value_field
; }
529 _S_key(_Const_Base_ptr __x
)
530 { return _KeyOfValue()(_S_value(__x
)); }
533 _S_minimum(_Base_ptr __x
)
534 { return _Rb_tree_node_base::_S_minimum(__x
); }
536 static _Const_Base_ptr
537 _S_minimum(_Const_Base_ptr __x
)
538 { return _Rb_tree_node_base::_S_minimum(__x
); }
541 _S_maximum(_Base_ptr __x
)
542 { return _Rb_tree_node_base::_S_maximum(__x
); }
544 static _Const_Base_ptr
545 _S_maximum(_Const_Base_ptr __x
)
546 { return _Rb_tree_node_base::_S_maximum(__x
); }
549 typedef _Rb_tree_iterator
<value_type
> iterator
;
550 typedef _Rb_tree_const_iterator
<value_type
> const_iterator
;
552 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
553 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
557 _M_insert_(_Const_Base_ptr __x
, _Const_Base_ptr __y
,
558 const value_type
& __v
);
560 // _GLIBCXX_RESOLVE_LIB_DEFECTS
561 // 233. Insertion hints in associative containers.
563 _M_insert_lower(_Base_ptr __x
, _Base_ptr __y
, const value_type
& __v
);
566 _M_insert_equal_lower(const value_type
& __x
);
569 _M_copy(_Const_Link_type __x
, _Link_type __p
);
572 _M_erase(_Link_type __x
);
575 _M_lower_bound(_Link_type __x
, _Link_type __y
,
579 _M_lower_bound(_Const_Link_type __x
, _Const_Link_type __y
,
580 const _Key
& __k
) const;
583 _M_upper_bound(_Link_type __x
, _Link_type __y
,
587 _M_upper_bound(_Const_Link_type __x
, _Const_Link_type __y
,
588 const _Key
& __k
) const;
591 // allocation/deallocation
594 _Rb_tree(const _Compare
& __comp
,
595 const allocator_type
& __a
= allocator_type())
596 : _M_impl(__comp
, __a
) { }
598 _Rb_tree(const _Rb_tree
& __x
)
599 : _M_impl(__x
._M_impl
._M_key_compare
, __x
._M_get_Node_allocator())
601 if (__x
._M_root() != 0)
603 _M_root() = _M_copy(__x
._M_begin(), _M_end());
604 _M_leftmost() = _S_minimum(_M_root());
605 _M_rightmost() = _S_maximum(_M_root());
606 _M_impl
._M_node_count
= __x
._M_impl
._M_node_count
;
611 { _M_erase(_M_begin()); }
614 operator=(const _Rb_tree
& __x
);
619 { return _M_impl
._M_key_compare
; }
624 return iterator(static_cast<_Link_type
>
625 (this->_M_impl
._M_header
._M_left
));
631 return const_iterator(static_cast<_Const_Link_type
>
632 (this->_M_impl
._M_header
._M_left
));
637 { return iterator(static_cast<_Link_type
>(&this->_M_impl
._M_header
)); }
642 return const_iterator(static_cast<_Const_Link_type
>
643 (&this->_M_impl
._M_header
));
648 { return reverse_iterator(end()); }
650 const_reverse_iterator
652 { return const_reverse_iterator(end()); }
656 { return reverse_iterator(begin()); }
658 const_reverse_iterator
660 { return const_reverse_iterator(begin()); }
664 { return _M_impl
._M_node_count
== 0; }
668 { return _M_impl
._M_node_count
; }
672 { return get_allocator().max_size(); }
675 #ifdef __GXX_EXPERIMENTAL_CXX0X__
676 swap(_Rb_tree
&& __t
);
683 _M_insert_unique(const value_type
& __x
);
686 _M_insert_equal(const value_type
& __x
);
689 _M_insert_unique_(const_iterator __position
, const value_type
& __x
);
692 _M_insert_equal_(const_iterator __position
, const value_type
& __x
);
694 template<typename _InputIterator
>
696 _M_insert_unique(_InputIterator __first
, _InputIterator __last
);
698 template<typename _InputIterator
>
700 _M_insert_equal(_InputIterator __first
, _InputIterator __last
);
703 erase(iterator __position
);
706 erase(const_iterator __position
);
709 erase(const key_type
& __x
);
712 erase(iterator __first
, iterator __last
);
715 erase(const_iterator __first
, const_iterator __last
);
718 erase(const key_type
* __first
, const key_type
* __last
);
723 _M_erase(_M_begin());
724 _M_leftmost() = _M_end();
726 _M_rightmost() = _M_end();
727 _M_impl
._M_node_count
= 0;
732 find(const key_type
& __k
);
735 find(const key_type
& __k
) const;
738 count(const key_type
& __k
) const;
741 lower_bound(const key_type
& __k
)
742 { return _M_lower_bound(_M_begin(), _M_end(), __k
); }
745 lower_bound(const key_type
& __k
) const
746 { return _M_lower_bound(_M_begin(), _M_end(), __k
); }
749 upper_bound(const key_type
& __k
)
750 { return _M_upper_bound(_M_begin(), _M_end(), __k
); }
753 upper_bound(const key_type
& __k
) const
754 { return _M_upper_bound(_M_begin(), _M_end(), __k
); }
756 pair
<iterator
, iterator
>
757 equal_range(const key_type
& __k
);
759 pair
<const_iterator
, const_iterator
>
760 equal_range(const key_type
& __k
) const;
767 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
768 typename _Compare
, typename _Alloc
>
770 operator==(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
771 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
773 return __x
.size() == __y
.size()
774 && std::equal(__x
.begin(), __x
.end(), __y
.begin());
777 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
778 typename _Compare
, typename _Alloc
>
780 operator<(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
781 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
783 return std::lexicographical_compare(__x
.begin(), __x
.end(),
784 __y
.begin(), __y
.end());
787 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
788 typename _Compare
, typename _Alloc
>
790 operator!=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
791 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
792 { return !(__x
== __y
); }
794 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
795 typename _Compare
, typename _Alloc
>
797 operator>(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
798 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
799 { return __y
< __x
; }
801 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
802 typename _Compare
, typename _Alloc
>
804 operator<=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
805 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
806 { return !(__y
< __x
); }
808 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
809 typename _Compare
, typename _Alloc
>
811 operator>=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
812 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
813 { return !(__x
< __y
); }
815 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
816 typename _Compare
, typename _Alloc
>
818 swap(_Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
819 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
822 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
823 typename _Compare
, typename _Alloc
>
824 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>&
825 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
826 operator=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
)
830 // Note that _Key may be a constant type.
832 _M_impl
._M_key_compare
= __x
._M_impl
._M_key_compare
;
833 if (__x
._M_root() != 0)
835 _M_root() = _M_copy(__x
._M_begin(), _M_end());
836 _M_leftmost() = _S_minimum(_M_root());
837 _M_rightmost() = _S_maximum(_M_root());
838 _M_impl
._M_node_count
= __x
._M_impl
._M_node_count
;
844 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
845 typename _Compare
, typename _Alloc
>
846 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
847 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
848 _M_insert_(_Const_Base_ptr __x
, _Const_Base_ptr __p
, const _Val
& __v
)
850 bool __insert_left
= (__x
!= 0 || __p
== _M_end()
851 || _M_impl
._M_key_compare(_KeyOfValue()(__v
),
854 _Link_type __z
= _M_create_node(__v
);
856 _Rb_tree_insert_and_rebalance(__insert_left
, __z
,
857 const_cast<_Base_ptr
>(__p
),
858 this->_M_impl
._M_header
);
859 ++_M_impl
._M_node_count
;
860 return iterator(__z
);
863 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
864 typename _Compare
, typename _Alloc
>
865 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
866 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
867 _M_insert_lower(_Base_ptr __x
, _Base_ptr __p
, const _Val
& __v
)
869 bool __insert_left
= (__x
!= 0 || __p
== _M_end()
870 || !_M_impl
._M_key_compare(_S_key(__p
),
871 _KeyOfValue()(__v
)));
873 _Link_type __z
= _M_create_node(__v
);
875 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
876 this->_M_impl
._M_header
);
877 ++_M_impl
._M_node_count
;
878 return iterator(__z
);
881 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
882 typename _Compare
, typename _Alloc
>
883 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
884 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
885 _M_insert_equal_lower(const _Val
& __v
)
887 _Link_type __x
= _M_begin();
888 _Link_type __y
= _M_end();
892 __x
= !_M_impl
._M_key_compare(_S_key(__x
), _KeyOfValue()(__v
)) ?
893 _S_left(__x
) : _S_right(__x
);
895 return _M_insert_lower(__x
, __y
, __v
);
898 template<typename _Key
, typename _Val
, typename _KoV
,
899 typename _Compare
, typename _Alloc
>
900 typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::_Link_type
901 _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::
902 _M_copy(_Const_Link_type __x
, _Link_type __p
)
904 // Structural copy. __x and __p must be non-null.
905 _Link_type __top
= _M_clone_node(__x
);
906 __top
->_M_parent
= __p
;
911 __top
->_M_right
= _M_copy(_S_right(__x
), __top
);
917 _Link_type __y
= _M_clone_node(__x
);
919 __y
->_M_parent
= __p
;
921 __y
->_M_right
= _M_copy(_S_right(__x
), __y
);
929 __throw_exception_again
;
934 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
935 typename _Compare
, typename _Alloc
>
937 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
938 _M_erase(_Link_type __x
)
940 // Erase without rebalancing.
943 _M_erase(_S_right(__x
));
944 _Link_type __y
= _S_left(__x
);
945 _M_destroy_node(__x
);
950 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
951 typename _Compare
, typename _Alloc
>
952 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
953 _Compare
, _Alloc
>::iterator
954 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
955 _M_lower_bound(_Link_type __x
, _Link_type __y
,
959 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
960 __y
= __x
, __x
= _S_left(__x
);
963 return iterator(__y
);
966 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
967 typename _Compare
, typename _Alloc
>
968 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
969 _Compare
, _Alloc
>::const_iterator
970 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
971 _M_lower_bound(_Const_Link_type __x
, _Const_Link_type __y
,
972 const _Key
& __k
) const
975 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
976 __y
= __x
, __x
= _S_left(__x
);
979 return const_iterator(__y
);
982 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
983 typename _Compare
, typename _Alloc
>
984 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
985 _Compare
, _Alloc
>::iterator
986 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
987 _M_upper_bound(_Link_type __x
, _Link_type __y
,
991 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
992 __y
= __x
, __x
= _S_left(__x
);
995 return iterator(__y
);
998 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
999 typename _Compare
, typename _Alloc
>
1000 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1001 _Compare
, _Alloc
>::const_iterator
1002 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1003 _M_upper_bound(_Const_Link_type __x
, _Const_Link_type __y
,
1004 const _Key
& __k
) const
1007 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1008 __y
= __x
, __x
= _S_left(__x
);
1010 __x
= _S_right(__x
);
1011 return const_iterator(__y
);
1014 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1015 typename _Compare
, typename _Alloc
>
1016 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1017 _Compare
, _Alloc
>::iterator
,
1018 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1019 _Compare
, _Alloc
>::iterator
>
1020 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1021 equal_range(const _Key
& __k
)
1023 _Link_type __x
= _M_begin();
1024 _Link_type __y
= _M_end();
1027 if (_M_impl
._M_key_compare(_S_key(__x
), __k
))
1028 __x
= _S_right(__x
);
1029 else if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1030 __y
= __x
, __x
= _S_left(__x
);
1033 _Link_type
__xu(__x
), __yu(__y
);
1034 __y
= __x
, __x
= _S_left(__x
);
1035 __xu
= _S_right(__xu
);
1036 return pair
<iterator
,
1037 iterator
>(_M_lower_bound(__x
, __y
, __k
),
1038 _M_upper_bound(__xu
, __yu
, __k
));
1041 return pair
<iterator
, iterator
>(iterator(__y
),
1045 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1046 typename _Compare
, typename _Alloc
>
1047 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1048 _Compare
, _Alloc
>::const_iterator
,
1049 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1050 _Compare
, _Alloc
>::const_iterator
>
1051 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1052 equal_range(const _Key
& __k
) const
1054 _Const_Link_type __x
= _M_begin();
1055 _Const_Link_type __y
= _M_end();
1058 if (_M_impl
._M_key_compare(_S_key(__x
), __k
))
1059 __x
= _S_right(__x
);
1060 else if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1061 __y
= __x
, __x
= _S_left(__x
);
1064 _Const_Link_type
__xu(__x
), __yu(__y
);
1065 __y
= __x
, __x
= _S_left(__x
);
1066 __xu
= _S_right(__xu
);
1067 return pair
<const_iterator
,
1068 const_iterator
>(_M_lower_bound(__x
, __y
, __k
),
1069 _M_upper_bound(__xu
, __yu
, __k
));
1072 return pair
<const_iterator
, const_iterator
>(const_iterator(__y
),
1073 const_iterator(__y
));
1076 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1077 typename _Compare
, typename _Alloc
>
1079 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1080 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1081 swap(_Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>&& __t
)
1083 swap(_Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __t
)
1088 if (__t
._M_root() != 0)
1090 _M_root() = __t
._M_root();
1091 _M_leftmost() = __t
._M_leftmost();
1092 _M_rightmost() = __t
._M_rightmost();
1093 _M_root()->_M_parent
= _M_end();
1096 __t
._M_leftmost() = __t
._M_end();
1097 __t
._M_rightmost() = __t
._M_end();
1100 else if (__t
._M_root() == 0)
1102 __t
._M_root() = _M_root();
1103 __t
._M_leftmost() = _M_leftmost();
1104 __t
._M_rightmost() = _M_rightmost();
1105 __t
._M_root()->_M_parent
= __t
._M_end();
1108 _M_leftmost() = _M_end();
1109 _M_rightmost() = _M_end();
1113 std::swap(_M_root(),__t
._M_root());
1114 std::swap(_M_leftmost(),__t
._M_leftmost());
1115 std::swap(_M_rightmost(),__t
._M_rightmost());
1117 _M_root()->_M_parent
= _M_end();
1118 __t
._M_root()->_M_parent
= __t
._M_end();
1120 // No need to swap header's color as it does not change.
1121 std::swap(this->_M_impl
._M_node_count
, __t
._M_impl
._M_node_count
);
1122 std::swap(this->_M_impl
._M_key_compare
, __t
._M_impl
._M_key_compare
);
1124 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1125 // 431. Swapping containers with unequal allocators.
1126 std::__alloc_swap
<_Node_allocator
>::
1127 _S_do_it(_M_get_Node_allocator(), __t
._M_get_Node_allocator());
1130 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1131 typename _Compare
, typename _Alloc
>
1132 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1133 _Compare
, _Alloc
>::iterator
, bool>
1134 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1135 _M_insert_unique(const _Val
& __v
)
1137 _Link_type __x
= _M_begin();
1138 _Link_type __y
= _M_end();
1143 __comp
= _M_impl
._M_key_compare(_KeyOfValue()(__v
), _S_key(__x
));
1144 __x
= __comp
? _S_left(__x
) : _S_right(__x
);
1146 iterator __j
= iterator(__y
);
1150 return pair
<iterator
, bool>(_M_insert_(__x
, __y
, __v
), true);
1154 if (_M_impl
._M_key_compare(_S_key(__j
._M_node
), _KeyOfValue()(__v
)))
1155 return pair
<iterator
, bool>(_M_insert_(__x
, __y
, __v
), true);
1156 return pair
<iterator
, bool>(__j
, false);
1159 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1160 typename _Compare
, typename _Alloc
>
1161 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1162 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1163 _M_insert_equal(const _Val
& __v
)
1165 _Link_type __x
= _M_begin();
1166 _Link_type __y
= _M_end();
1170 __x
= _M_impl
._M_key_compare(_KeyOfValue()(__v
), _S_key(__x
)) ?
1171 _S_left(__x
) : _S_right(__x
);
1173 return _M_insert_(__x
, __y
, __v
);
1176 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1177 typename _Compare
, typename _Alloc
>
1178 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1179 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1180 _M_insert_unique_(const_iterator __position
, const _Val
& __v
)
1183 if (__position
._M_node
== _M_end())
1186 && _M_impl
._M_key_compare(_S_key(_M_rightmost()),
1187 _KeyOfValue()(__v
)))
1188 return _M_insert_(0, _M_rightmost(), __v
);
1190 return _M_insert_unique(__v
).first
;
1192 else if (_M_impl
._M_key_compare(_KeyOfValue()(__v
),
1193 _S_key(__position
._M_node
)))
1195 // First, try before...
1196 const_iterator __before
= __position
;
1197 if (__position
._M_node
== _M_leftmost()) // begin()
1198 return _M_insert_(_M_leftmost(), _M_leftmost(), __v
);
1199 else if (_M_impl
._M_key_compare(_S_key((--__before
)._M_node
),
1200 _KeyOfValue()(__v
)))
1202 if (_S_right(__before
._M_node
) == 0)
1203 return _M_insert_(0, __before
._M_node
, __v
);
1205 return _M_insert_(__position
._M_node
,
1206 __position
._M_node
, __v
);
1209 return _M_insert_unique(__v
).first
;
1211 else if (_M_impl
._M_key_compare(_S_key(__position
._M_node
),
1212 _KeyOfValue()(__v
)))
1214 // ... then try after.
1215 const_iterator __after
= __position
;
1216 if (__position
._M_node
== _M_rightmost())
1217 return _M_insert_(0, _M_rightmost(), __v
);
1218 else if (_M_impl
._M_key_compare(_KeyOfValue()(__v
),
1219 _S_key((++__after
)._M_node
)))
1221 if (_S_right(__position
._M_node
) == 0)
1222 return _M_insert_(0, __position
._M_node
, __v
);
1224 return _M_insert_(__after
._M_node
, __after
._M_node
, __v
);
1227 return _M_insert_unique(__v
).first
;
1231 return iterator(static_cast<_Link_type
>
1232 (const_cast<_Base_ptr
>(__position
._M_node
)));
1235 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1236 typename _Compare
, typename _Alloc
>
1237 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1238 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1239 _M_insert_equal_(const_iterator __position
, const _Val
& __v
)
1242 if (__position
._M_node
== _M_end())
1245 && !_M_impl
._M_key_compare(_KeyOfValue()(__v
),
1246 _S_key(_M_rightmost())))
1247 return _M_insert_(0, _M_rightmost(), __v
);
1249 return _M_insert_equal(__v
);
1251 else if (!_M_impl
._M_key_compare(_S_key(__position
._M_node
),
1252 _KeyOfValue()(__v
)))
1254 // First, try before...
1255 const_iterator __before
= __position
;
1256 if (__position
._M_node
== _M_leftmost()) // begin()
1257 return _M_insert_(_M_leftmost(), _M_leftmost(), __v
);
1258 else if (!_M_impl
._M_key_compare(_KeyOfValue()(__v
),
1259 _S_key((--__before
)._M_node
)))
1261 if (_S_right(__before
._M_node
) == 0)
1262 return _M_insert_(0, __before
._M_node
, __v
);
1264 return _M_insert_(__position
._M_node
,
1265 __position
._M_node
, __v
);
1268 return _M_insert_equal(__v
);
1272 // ... then try after.
1273 const_iterator __after
= __position
;
1274 if (__position
._M_node
== _M_rightmost())
1275 return _M_insert_(0, _M_rightmost(), __v
);
1276 else if (!_M_impl
._M_key_compare(_S_key((++__after
)._M_node
),
1277 _KeyOfValue()(__v
)))
1279 if (_S_right(__position
._M_node
) == 0)
1280 return _M_insert_(0, __position
._M_node
, __v
);
1282 return _M_insert_(__after
._M_node
, __after
._M_node
, __v
);
1285 return _M_insert_equal_lower(__v
);
1289 template<typename _Key
, typename _Val
, typename _KoV
,
1290 typename _Cmp
, typename _Alloc
>
1293 _Rb_tree
<_Key
, _Val
, _KoV
, _Cmp
, _Alloc
>::
1294 _M_insert_unique(_II __first
, _II __last
)
1296 for (; __first
!= __last
; ++__first
)
1297 _M_insert_unique_(end(), *__first
);
1300 template<typename _Key
, typename _Val
, typename _KoV
,
1301 typename _Cmp
, typename _Alloc
>
1304 _Rb_tree
<_Key
, _Val
, _KoV
, _Cmp
, _Alloc
>::
1305 _M_insert_equal(_II __first
, _II __last
)
1307 for (; __first
!= __last
; ++__first
)
1308 _M_insert_equal_(end(), *__first
);
1311 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1312 typename _Compare
, typename _Alloc
>
1314 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1315 erase(iterator __position
)
1318 static_cast<_Link_type
>(_Rb_tree_rebalance_for_erase
1319 (__position
._M_node
,
1320 this->_M_impl
._M_header
));
1321 _M_destroy_node(__y
);
1322 --_M_impl
._M_node_count
;
1325 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1326 typename _Compare
, typename _Alloc
>
1328 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1329 erase(const_iterator __position
)
1332 static_cast<_Link_type
>(_Rb_tree_rebalance_for_erase
1333 (const_cast<_Base_ptr
>(__position
._M_node
),
1334 this->_M_impl
._M_header
));
1335 _M_destroy_node(__y
);
1336 --_M_impl
._M_node_count
;
1339 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1340 typename _Compare
, typename _Alloc
>
1341 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::size_type
1342 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1343 erase(const _Key
& __x
)
1345 pair
<iterator
, iterator
> __p
= equal_range(__x
);
1346 const size_type __old_size
= size();
1347 erase(__p
.first
, __p
.second
);
1348 return __old_size
- size();
1351 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1352 typename _Compare
, typename _Alloc
>
1354 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1355 erase(iterator __first
, iterator __last
)
1357 if (__first
== begin() && __last
== end())
1360 while (__first
!= __last
)
1364 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1365 typename _Compare
, typename _Alloc
>
1367 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1368 erase(const_iterator __first
, const_iterator __last
)
1370 if (__first
== begin() && __last
== end())
1373 while (__first
!= __last
)
1377 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1378 typename _Compare
, typename _Alloc
>
1380 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1381 erase(const _Key
* __first
, const _Key
* __last
)
1383 while (__first
!= __last
)
1387 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1388 typename _Compare
, typename _Alloc
>
1389 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1390 _Compare
, _Alloc
>::iterator
1391 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1392 find(const _Key
& __k
)
1394 iterator __j
= _M_lower_bound(_M_begin(), _M_end(), __k
);
1395 return (__j
== end()
1396 || _M_impl
._M_key_compare(__k
,
1397 _S_key(__j
._M_node
))) ? end() : __j
;
1400 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1401 typename _Compare
, typename _Alloc
>
1402 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1403 _Compare
, _Alloc
>::const_iterator
1404 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1405 find(const _Key
& __k
) const
1407 const_iterator __j
= _M_lower_bound(_M_begin(), _M_end(), __k
);
1408 return (__j
== end()
1409 || _M_impl
._M_key_compare(__k
,
1410 _S_key(__j
._M_node
))) ? end() : __j
;
1413 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1414 typename _Compare
, typename _Alloc
>
1415 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::size_type
1416 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1417 count(const _Key
& __k
) const
1419 pair
<const_iterator
, const_iterator
> __p
= equal_range(__k
);
1420 const size_type __n
= std::distance(__p
.first
, __p
.second
);
1425 _Rb_tree_black_count(const _Rb_tree_node_base
* __node
,
1426 const _Rb_tree_node_base
* __root
);
1428 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1429 typename _Compare
, typename _Alloc
>
1431 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::__rb_verify() const
1433 if (_M_impl
._M_node_count
== 0 || begin() == end())
1434 return _M_impl
._M_node_count
== 0 && begin() == end()
1435 && this->_M_impl
._M_header
._M_left
== _M_end()
1436 && this->_M_impl
._M_header
._M_right
== _M_end();
1438 unsigned int __len
= _Rb_tree_black_count(_M_leftmost(), _M_root());
1439 for (const_iterator __it
= begin(); __it
!= end(); ++__it
)
1441 _Const_Link_type __x
= static_cast<_Const_Link_type
>(__it
._M_node
);
1442 _Const_Link_type __L
= _S_left(__x
);
1443 _Const_Link_type __R
= _S_right(__x
);
1445 if (__x
->_M_color
== _S_red
)
1446 if ((__L
&& __L
->_M_color
== _S_red
)
1447 || (__R
&& __R
->_M_color
== _S_red
))
1450 if (__L
&& _M_impl
._M_key_compare(_S_key(__x
), _S_key(__L
)))
1452 if (__R
&& _M_impl
._M_key_compare(_S_key(__R
), _S_key(__x
)))
1455 if (!__L
&& !__R
&& _Rb_tree_black_count(__x
, _M_root()) != __len
)
1459 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1461 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1466 _GLIBCXX_END_NAMESPACE