68 namespace std _GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
88 enum _Rb_tree_color { _S_red =
false, _S_black =
true };
90 struct _Rb_tree_node_base
92 typedef _Rb_tree_node_base* _Base_ptr;
93 typedef const _Rb_tree_node_base* _Const_Base_ptr;
95 _Rb_tree_color _M_color;
101 _S_minimum(_Base_ptr __x)
103 while (__x->_M_left != 0) __x = __x->_M_left;
107 static _Const_Base_ptr
108 _S_minimum(_Const_Base_ptr __x)
110 while (__x->_M_left != 0) __x = __x->_M_left;
115 _S_maximum(_Base_ptr __x)
117 while (__x->_M_right != 0) __x = __x->_M_right;
121 static _Const_Base_ptr
122 _S_maximum(_Const_Base_ptr __x)
124 while (__x->_M_right != 0) __x = __x->_M_right;
129 template<
typename _Val>
130 struct _Rb_tree_node :
public _Rb_tree_node_base
132 typedef _Rb_tree_node<_Val>* _Link_type;
135 #ifdef __GXX_EXPERIMENTAL_CXX0X__
136 template<
typename... _Args>
137 _Rb_tree_node(_Args&&... __args)
138 : _Rb_tree_node_base(),
139 _M_value_field(std::
forward<_Args>(__args)...) { }
143 _GLIBCXX_PURE _Rb_tree_node_base*
144 _Rb_tree_increment(_Rb_tree_node_base* __x)
throw ();
146 _GLIBCXX_PURE
const _Rb_tree_node_base*
147 _Rb_tree_increment(
const _Rb_tree_node_base* __x)
throw ();
149 _GLIBCXX_PURE _Rb_tree_node_base*
150 _Rb_tree_decrement(_Rb_tree_node_base* __x)
throw ();
152 _GLIBCXX_PURE
const _Rb_tree_node_base*
153 _Rb_tree_decrement(
const _Rb_tree_node_base* __x)
throw ();
155 template<
typename _Tp>
156 struct _Rb_tree_iterator
158 typedef _Tp value_type;
159 typedef _Tp& reference;
160 typedef _Tp* pointer;
162 typedef bidirectional_iterator_tag iterator_category;
163 typedef ptrdiff_t difference_type;
165 typedef _Rb_tree_iterator<_Tp> _Self;
166 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
167 typedef _Rb_tree_node<_Tp>* _Link_type;
173 _Rb_tree_iterator(_Link_type __x)
178 {
return static_cast<_Link_type
>(_M_node)->_M_value_field; }
182 {
return std::__addressof(static_cast<_Link_type>
183 (_M_node)->_M_value_field); }
188 _M_node = _Rb_tree_increment(_M_node);
196 _M_node = _Rb_tree_increment(_M_node);
203 _M_node = _Rb_tree_decrement(_M_node);
211 _M_node = _Rb_tree_decrement(_M_node);
216 operator==(
const _Self& __x)
const
217 {
return _M_node == __x._M_node; }
220 operator!=(
const _Self& __x)
const
221 {
return _M_node != __x._M_node; }
226 template<
typename _Tp>
227 struct _Rb_tree_const_iterator
229 typedef _Tp value_type;
230 typedef const _Tp& reference;
231 typedef const _Tp* pointer;
233 typedef _Rb_tree_iterator<_Tp> iterator;
235 typedef bidirectional_iterator_tag iterator_category;
236 typedef ptrdiff_t difference_type;
238 typedef _Rb_tree_const_iterator<_Tp> _Self;
239 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
240 typedef const _Rb_tree_node<_Tp>* _Link_type;
242 _Rb_tree_const_iterator()
246 _Rb_tree_const_iterator(_Link_type __x)
249 _Rb_tree_const_iterator(
const iterator& __it)
250 : _M_node(__it._M_node) { }
253 _M_const_cast()
const
254 {
return iterator(static_cast<typename iterator::_Link_type>
255 (const_cast<typename iterator::_Base_ptr>(_M_node))); }
259 {
return static_cast<_Link_type
>(_M_node)->_M_value_field; }
263 {
return std::__addressof(static_cast<_Link_type>
264 (_M_node)->_M_value_field); }
269 _M_node = _Rb_tree_increment(_M_node);
277 _M_node = _Rb_tree_increment(_M_node);
284 _M_node = _Rb_tree_decrement(_M_node);
292 _M_node = _Rb_tree_decrement(_M_node);
297 operator==(
const _Self& __x)
const
298 {
return _M_node == __x._M_node; }
301 operator!=(
const _Self& __x)
const
302 {
return _M_node != __x._M_node; }
307 template<
typename _Val>
309 operator==(
const _Rb_tree_iterator<_Val>& __x,
310 const _Rb_tree_const_iterator<_Val>& __y)
311 {
return __x._M_node == __y._M_node; }
313 template<
typename _Val>
315 operator!=(
const _Rb_tree_iterator<_Val>& __x,
316 const _Rb_tree_const_iterator<_Val>& __y)
317 {
return __x._M_node != __y._M_node; }
320 _Rb_tree_insert_and_rebalance(
const bool __insert_left,
321 _Rb_tree_node_base* __x,
322 _Rb_tree_node_base* __p,
323 _Rb_tree_node_base& __header)
throw ();
326 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base*
const __z,
327 _Rb_tree_node_base& __header)
throw ();
330 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
331 typename _Compare,
typename _Alloc = allocator<_Val> >
334 typedef typename _Alloc::template rebind<_Rb_tree_node<_Val> >::other
338 typedef _Rb_tree_node_base* _Base_ptr;
339 typedef const _Rb_tree_node_base* _Const_Base_ptr;
342 typedef _Key key_type;
343 typedef _Val value_type;
344 typedef value_type* pointer;
345 typedef const value_type* const_pointer;
346 typedef value_type& reference;
347 typedef const value_type& const_reference;
348 typedef _Rb_tree_node<_Val>* _Link_type;
349 typedef const _Rb_tree_node<_Val>* _Const_Link_type;
350 typedef size_t size_type;
351 typedef ptrdiff_t difference_type;
352 typedef _Alloc allocator_type;
355 _M_get_Node_allocator()
356 {
return *
static_cast<_Node_allocator*
>(&this->_M_impl); }
358 const _Node_allocator&
359 _M_get_Node_allocator()
const
360 {
return *
static_cast<const _Node_allocator*
>(&this->_M_impl); }
363 get_allocator()
const
364 {
return allocator_type(_M_get_Node_allocator()); }
369 {
return _M_impl._Node_allocator::allocate(1); }
372 _M_put_node(_Link_type __p)
373 { _M_impl._Node_allocator::deallocate(__p, 1); }
375 #ifndef __GXX_EXPERIMENTAL_CXX0X__
377 _M_create_node(
const value_type& __x)
379 _Link_type __tmp = _M_get_node();
381 { get_allocator().construct
382 (std::__addressof(__tmp->_M_value_field), __x); }
386 __throw_exception_again;
392 _M_destroy_node(_Link_type __p)
394 get_allocator().destroy(std::__addressof(__p->_M_value_field));
398 template<
typename... _Args>
400 _M_create_node(_Args&&... __args)
402 _Link_type __tmp = _M_get_node();
405 _M_get_Node_allocator().construct(__tmp,
406 std::forward<_Args>(__args)...);
411 __throw_exception_again;
417 _M_destroy_node(_Link_type __p)
419 _M_get_Node_allocator().destroy(__p);
425 _M_clone_node(_Const_Link_type __x)
427 _Link_type __tmp = _M_create_node(__x->_M_value_field);
428 __tmp->_M_color = __x->_M_color;
435 template<
typename _Key_compare,
436 bool _Is_pod_comparator = __is_pod(_Key_compare)>
437 struct _Rb_tree_impl :
public _Node_allocator
439 _Key_compare _M_key_compare;
440 _Rb_tree_node_base _M_header;
441 size_type _M_node_count;
444 : _Node_allocator(), _M_key_compare(), _M_header(),
448 _Rb_tree_impl(
const _Key_compare& __comp,
const _Node_allocator& __a)
449 : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
457 this->_M_header._M_color = _S_red;
458 this->_M_header._M_parent = 0;
459 this->_M_header._M_left = &this->_M_header;
460 this->_M_header._M_right = &this->_M_header;
464 _Rb_tree_impl<_Compare> _M_impl;
469 {
return this->_M_impl._M_header._M_parent; }
473 {
return this->_M_impl._M_header._M_parent; }
477 {
return this->_M_impl._M_header._M_left; }
481 {
return this->_M_impl._M_header._M_left; }
485 {
return this->_M_impl._M_header._M_right; }
489 {
return this->_M_impl._M_header._M_right; }
493 {
return static_cast<_Link_type
>(this->_M_impl._M_header._M_parent); }
498 return static_cast<_Const_Link_type
>
499 (this->_M_impl._M_header._M_parent);
504 {
return static_cast<_Link_type
>(&this->_M_impl._M_header); }
508 {
return static_cast<_Const_Link_type
>(&this->_M_impl._M_header); }
510 static const_reference
511 _S_value(_Const_Link_type __x)
512 {
return __x->_M_value_field; }
515 _S_key(_Const_Link_type __x)
516 {
return _KeyOfValue()(_S_value(__x)); }
519 _S_left(_Base_ptr __x)
520 {
return static_cast<_Link_type
>(__x->_M_left); }
522 static _Const_Link_type
523 _S_left(_Const_Base_ptr __x)
524 {
return static_cast<_Const_Link_type
>(__x->_M_left); }
527 _S_right(_Base_ptr __x)
528 {
return static_cast<_Link_type
>(__x->_M_right); }
530 static _Const_Link_type
531 _S_right(_Const_Base_ptr __x)
532 {
return static_cast<_Const_Link_type
>(__x->_M_right); }
534 static const_reference
535 _S_value(_Const_Base_ptr __x)
536 {
return static_cast<_Const_Link_type
>(__x)->_M_value_field; }
539 _S_key(_Const_Base_ptr __x)
540 {
return _KeyOfValue()(_S_value(__x)); }
543 _S_minimum(_Base_ptr __x)
544 {
return _Rb_tree_node_base::_S_minimum(__x); }
546 static _Const_Base_ptr
547 _S_minimum(_Const_Base_ptr __x)
548 {
return _Rb_tree_node_base::_S_minimum(__x); }
551 _S_maximum(_Base_ptr __x)
552 {
return _Rb_tree_node_base::_S_maximum(__x); }
554 static _Const_Base_ptr
555 _S_maximum(_Const_Base_ptr __x)
556 {
return _Rb_tree_node_base::_S_maximum(__x); }
559 typedef _Rb_tree_iterator<value_type> iterator;
560 typedef _Rb_tree_const_iterator<value_type> const_iterator;
566 #ifdef __GXX_EXPERIMENTAL_CXX0X__
567 template<
typename _Arg>
569 _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __y, _Arg&& __v);
571 template<
typename _Arg>
573 _M_insert_lower(_Base_ptr __x, _Base_ptr __y, _Arg&& __v);
575 template<
typename _Arg>
577 _M_insert_equal_lower(_Arg&& __x);
580 _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __y,
581 const value_type& __v);
586 _M_insert_lower(_Base_ptr __x, _Base_ptr __y,
const value_type& __v);
589 _M_insert_equal_lower(
const value_type& __x);
593 _M_copy(_Const_Link_type __x, _Link_type __p);
596 _M_erase(_Link_type __x);
599 _M_lower_bound(_Link_type __x, _Link_type __y,
603 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
604 const _Key& __k)
const;
607 _M_upper_bound(_Link_type __x, _Link_type __y,
611 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
612 const _Key& __k)
const;
618 _Rb_tree(
const _Compare& __comp,
619 const allocator_type& __a = allocator_type())
620 : _M_impl(__comp, __a) { }
622 _Rb_tree(
const _Rb_tree& __x)
623 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
625 if (__x._M_root() != 0)
627 _M_root() = _M_copy(__x._M_begin(), _M_end());
628 _M_leftmost() = _S_minimum(_M_root());
629 _M_rightmost() = _S_maximum(_M_root());
630 _M_impl._M_node_count = __x._M_impl._M_node_count;
634 #ifdef __GXX_EXPERIMENTAL_CXX0X__
635 _Rb_tree(_Rb_tree&& __x);
639 { _M_erase(_M_begin()); }
642 operator=(
const _Rb_tree& __x);
647 {
return _M_impl._M_key_compare; }
652 return iterator(static_cast<_Link_type>
653 (this->_M_impl._M_header._M_left));
659 return const_iterator(static_cast<_Const_Link_type>
660 (this->_M_impl._M_header._M_left));
665 {
return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
670 return const_iterator(static_cast<_Const_Link_type>
671 (&this->_M_impl._M_header));
676 {
return reverse_iterator(
end()); }
678 const_reverse_iterator
680 {
return const_reverse_iterator(
end()); }
684 {
return reverse_iterator(
begin()); }
686 const_reverse_iterator
688 {
return const_reverse_iterator(
begin()); }
692 {
return _M_impl._M_node_count == 0; }
696 {
return _M_impl._M_node_count; }
700 {
return _M_get_Node_allocator().max_size(); }
706 #ifdef __GXX_EXPERIMENTAL_CXX0X__
707 template<
typename _Arg>
709 _M_insert_unique(_Arg&& __x);
711 template<
typename _Arg>
713 _M_insert_equal(_Arg&& __x);
715 template<
typename _Arg>
717 _M_insert_unique_(const_iterator __position, _Arg&& __x);
719 template<
typename _Arg>
721 _M_insert_equal_(const_iterator __position, _Arg&& __x);
724 _M_insert_unique(
const value_type& __x);
727 _M_insert_equal(
const value_type& __x);
730 _M_insert_unique_(const_iterator __position,
const value_type& __x);
733 _M_insert_equal_(const_iterator __position,
const value_type& __x);
736 template<
typename _InputIterator>
738 _M_insert_unique(_InputIterator __first, _InputIterator __last);
740 template<
typename _InputIterator>
742 _M_insert_equal(_InputIterator __first, _InputIterator __last);
746 _M_erase_aux(const_iterator __position);
749 _M_erase_aux(const_iterator __first, const_iterator __last);
752 #ifdef __GXX_EXPERIMENTAL_CXX0X__
756 erase(const_iterator __position)
758 const_iterator __result = __position;
760 _M_erase_aux(__position);
761 return __result._M_const_cast();
766 erase(iterator __position)
768 iterator __result = __position;
770 _M_erase_aux(__position);
775 erase(iterator __position)
776 { _M_erase_aux(__position); }
779 erase(const_iterator __position)
780 { _M_erase_aux(__position); }
783 erase(
const key_type& __x);
785 #ifdef __GXX_EXPERIMENTAL_CXX0X__
789 erase(const_iterator __first, const_iterator __last)
791 _M_erase_aux(__first, __last);
792 return __last._M_const_cast();
796 erase(iterator __first, iterator __last)
797 { _M_erase_aux(__first, __last); }
800 erase(const_iterator __first, const_iterator __last)
801 { _M_erase_aux(__first, __last); }
804 erase(
const key_type* __first,
const key_type* __last);
809 _M_erase(_M_begin());
810 _M_leftmost() = _M_end();
812 _M_rightmost() = _M_end();
813 _M_impl._M_node_count = 0;
818 find(
const key_type& __k);
821 find(
const key_type& __k)
const;
824 count(
const key_type& __k)
const;
827 lower_bound(
const key_type& __k)
828 {
return _M_lower_bound(_M_begin(), _M_end(), __k); }
831 lower_bound(
const key_type& __k)
const
832 {
return _M_lower_bound(_M_begin(), _M_end(), __k); }
835 upper_bound(
const key_type& __k)
836 {
return _M_upper_bound(_M_begin(), _M_end(), __k); }
839 upper_bound(
const key_type& __k)
const
840 {
return _M_upper_bound(_M_begin(), _M_end(), __k); }
842 pair<iterator, iterator>
843 equal_range(
const key_type& __k);
845 pair<const_iterator, const_iterator>
846 equal_range(
const key_type& __k)
const;
853 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
854 typename _Compare,
typename _Alloc>
856 operator==(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
857 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
859 return __x.size() == __y.size()
860 &&
std::equal(__x.begin(), __x.end(), __y.begin());
863 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
864 typename _Compare,
typename _Alloc>
866 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
867 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
870 __y.begin(), __y.end());
873 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
874 typename _Compare,
typename _Alloc>
876 operator!=(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
877 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
878 {
return !(__x == __y); }
880 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
881 typename _Compare,
typename _Alloc>
883 operator>(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
884 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
885 {
return __y < __x; }
887 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
888 typename _Compare,
typename _Alloc>
890 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
891 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
892 {
return !(__y < __x); }
894 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
895 typename _Compare,
typename _Alloc>
897 operator>=(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
898 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
899 {
return !(__x < __y); }
901 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
902 typename _Compare,
typename _Alloc>
904 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
905 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
908 #ifdef __GXX_EXPERIMENTAL_CXX0X__
909 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
910 typename _Compare,
typename _Alloc>
911 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
912 _Rb_tree(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&& __x)
913 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
915 if (__x._M_root() != 0)
917 _M_root() = __x._M_root();
918 _M_leftmost() = __x._M_leftmost();
919 _M_rightmost() = __x._M_rightmost();
920 _M_root()->_M_parent = _M_end();
923 __x._M_leftmost() = __x._M_end();
924 __x._M_rightmost() = __x._M_end();
926 this->_M_impl._M_node_count = __x._M_impl._M_node_count;
927 __x._M_impl._M_node_count = 0;
932 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
933 typename _Compare,
typename _Alloc>
934 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
935 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
936 operator=(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x)
942 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
943 if (__x._M_root() != 0)
945 _M_root() = _M_copy(__x._M_begin(), _M_end());
946 _M_leftmost() = _S_minimum(_M_root());
947 _M_rightmost() = _S_maximum(_M_root());
948 _M_impl._M_node_count = __x._M_impl._M_node_count;
954 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
955 typename _Compare,
typename _Alloc>
956 #ifdef __GXX_EXPERIMENTAL_CXX0X__
957 template<
typename _Arg>
959 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
960 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
961 #ifdef __GXX_EXPERIMENTAL_CXX0X__
962 _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __p, _Arg&& __v)
964 _M_insert_(_Const_Base_ptr __x, _Const_Base_ptr __p,
const _Val& __v)
967 bool __insert_left = (__x != 0 || __p == _M_end()
968 || _M_impl._M_key_compare(_KeyOfValue()(__v),
971 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
973 _Rb_tree_insert_and_rebalance(__insert_left, __z,
974 const_cast<_Base_ptr>(__p),
975 this->_M_impl._M_header);
976 ++_M_impl._M_node_count;
977 return iterator(__z);
980 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
981 typename _Compare,
typename _Alloc>
982 #ifdef __GXX_EXPERIMENTAL_CXX0X__
983 template<
typename _Arg>
985 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
986 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
987 #ifdef __GXX_EXPERIMENTAL_CXX0X__
988 _M_insert_lower(_Base_ptr __x, _Base_ptr __p, _Arg&& __v)
990 _M_insert_lower(_Base_ptr __x, _Base_ptr __p,
const _Val& __v)
993 bool __insert_left = (__x != 0 || __p == _M_end()
994 || !_M_impl._M_key_compare(_S_key(__p),
995 _KeyOfValue()(__v)));
997 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
999 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1000 this->_M_impl._M_header);
1001 ++_M_impl._M_node_count;
1002 return iterator(__z);
1005 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1006 typename _Compare,
typename _Alloc>
1007 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1008 template<
typename _Arg>
1010 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1011 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1012 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1013 _M_insert_equal_lower(_Arg&& __v)
1015 _M_insert_equal_lower(
const _Val& __v)
1018 _Link_type __x = _M_begin();
1019 _Link_type __y = _M_end();
1023 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
1024 _S_left(__x) : _S_right(__x);
1026 return _M_insert_lower(__x, __y, _GLIBCXX_FORWARD(_Arg, __v));
1029 template<
typename _Key,
typename _Val,
typename _KoV,
1030 typename _Compare,
typename _Alloc>
1031 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1032 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1033 _M_copy(_Const_Link_type __x, _Link_type __p)
1036 _Link_type __top = _M_clone_node(__x);
1037 __top->_M_parent = __p;
1042 __top->_M_right = _M_copy(_S_right(__x), __top);
1048 _Link_type __y = _M_clone_node(__x);
1050 __y->_M_parent = __p;
1052 __y->_M_right = _M_copy(_S_right(__x), __y);
1060 __throw_exception_again;
1065 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1066 typename _Compare,
typename _Alloc>
1068 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1069 _M_erase(_Link_type __x)
1074 _M_erase(_S_right(__x));
1075 _Link_type __y = _S_left(__x);
1076 _M_destroy_node(__x);
1081 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1082 typename _Compare,
typename _Alloc>
1083 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1084 _Compare, _Alloc>::iterator
1085 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1086 _M_lower_bound(_Link_type __x, _Link_type __y,
1090 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1091 __y = __x, __x = _S_left(__x);
1093 __x = _S_right(__x);
1094 return iterator(__y);
1097 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1098 typename _Compare,
typename _Alloc>
1099 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1100 _Compare, _Alloc>::const_iterator
1101 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1102 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
1103 const _Key& __k)
const
1106 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1107 __y = __x, __x = _S_left(__x);
1109 __x = _S_right(__x);
1110 return const_iterator(__y);
1113 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1114 typename _Compare,
typename _Alloc>
1115 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1116 _Compare, _Alloc>::iterator
1117 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1118 _M_upper_bound(_Link_type __x, _Link_type __y,
1122 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1123 __y = __x, __x = _S_left(__x);
1125 __x = _S_right(__x);
1126 return iterator(__y);
1129 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1130 typename _Compare,
typename _Alloc>
1131 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1132 _Compare, _Alloc>::const_iterator
1133 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1134 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
1135 const _Key& __k)
const
1138 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1139 __y = __x, __x = _S_left(__x);
1141 __x = _S_right(__x);
1142 return const_iterator(__y);
1145 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1146 typename _Compare,
typename _Alloc>
1147 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1148 _Compare, _Alloc>::iterator,
1149 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1150 _Compare, _Alloc>::iterator>
1151 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1152 equal_range(
const _Key& __k)
1154 _Link_type __x = _M_begin();
1155 _Link_type __y = _M_end();
1158 if (_M_impl._M_key_compare(_S_key(__x), __k))
1159 __x = _S_right(__x);
1160 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1161 __y = __x, __x = _S_left(__x);
1164 _Link_type __xu(__x), __yu(__y);
1165 __y = __x, __x = _S_left(__x);
1166 __xu = _S_right(__xu);
1167 return pair<iterator,
1168 iterator>(_M_lower_bound(__x, __y, __k),
1169 _M_upper_bound(__xu, __yu, __k));
1172 return pair<iterator, iterator>(iterator(__y),
1176 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1177 typename _Compare,
typename _Alloc>
1178 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1179 _Compare, _Alloc>::const_iterator,
1180 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1181 _Compare, _Alloc>::const_iterator>
1182 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1183 equal_range(
const _Key& __k)
const
1185 _Const_Link_type __x = _M_begin();
1186 _Const_Link_type __y = _M_end();
1189 if (_M_impl._M_key_compare(_S_key(__x), __k))
1190 __x = _S_right(__x);
1191 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1192 __y = __x, __x = _S_left(__x);
1195 _Const_Link_type __xu(__x), __yu(__y);
1196 __y = __x, __x = _S_left(__x);
1197 __xu = _S_right(__xu);
1198 return pair<const_iterator,
1199 const_iterator>(_M_lower_bound(__x, __y, __k),
1200 _M_upper_bound(__xu, __yu, __k));
1203 return pair<const_iterator, const_iterator>(const_iterator(__y),
1204 const_iterator(__y));
1207 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1208 typename _Compare,
typename _Alloc>
1210 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1211 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
1215 if (__t._M_root() != 0)
1217 _M_root() = __t._M_root();
1218 _M_leftmost() = __t._M_leftmost();
1219 _M_rightmost() = __t._M_rightmost();
1220 _M_root()->_M_parent = _M_end();
1223 __t._M_leftmost() = __t._M_end();
1224 __t._M_rightmost() = __t._M_end();
1227 else if (__t._M_root() == 0)
1229 __t._M_root() = _M_root();
1230 __t._M_leftmost() = _M_leftmost();
1231 __t._M_rightmost() = _M_rightmost();
1232 __t._M_root()->_M_parent = __t._M_end();
1235 _M_leftmost() = _M_end();
1236 _M_rightmost() = _M_end();
1240 std::swap(_M_root(),__t._M_root());
1241 std::swap(_M_leftmost(),__t._M_leftmost());
1242 std::swap(_M_rightmost(),__t._M_rightmost());
1244 _M_root()->_M_parent = _M_end();
1245 __t._M_root()->_M_parent = __t._M_end();
1248 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
1249 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
1253 std::__alloc_swap<_Node_allocator>::
1254 _S_do_it(_M_get_Node_allocator(), __t._M_get_Node_allocator());
1257 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1258 typename _Compare,
typename _Alloc>
1259 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1260 template<
typename _Arg>
1262 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1263 _Compare, _Alloc>::iterator,
bool>
1264 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1265 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1266 _M_insert_unique(_Arg&& __v)
1268 _M_insert_unique(
const _Val& __v)
1271 _Link_type __x = _M_begin();
1272 _Link_type __y = _M_end();
1277 __comp = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x));
1278 __x = __comp ? _S_left(__x) : _S_right(__x);
1280 iterator __j = iterator(__y);
1284 return pair<iterator, bool>
1285 (_M_insert_(__x, __y, _GLIBCXX_FORWARD(_Arg, __v)),
true);
1289 if (_M_impl._M_key_compare(_S_key(__j._M_node), _KeyOfValue()(__v)))
1290 return pair<iterator, bool>
1291 (_M_insert_(__x, __y, _GLIBCXX_FORWARD(_Arg, __v)),
true);
1292 return pair<iterator, bool>(__j,
false);
1295 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1296 typename _Compare,
typename _Alloc>
1297 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1298 template<
typename _Arg>
1300 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1301 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1303 _M_insert_equal(_Arg&& __v)
1305 _M_insert_equal(
const _Val& __v)
1308 _Link_type __x = _M_begin();
1309 _Link_type __y = _M_end();
1313 __x = _M_impl._M_key_compare(_KeyOfValue()(__v), _S_key(__x)) ?
1314 _S_left(__x) : _S_right(__x);
1316 return _M_insert_(__x, __y, _GLIBCXX_FORWARD(_Arg, __v));
1319 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1320 typename _Compare,
typename _Alloc>
1321 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1322 template<
typename _Arg>
1324 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1325 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1326 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1327 _M_insert_unique_(const_iterator __position, _Arg&& __v)
1329 _M_insert_unique_(const_iterator __position,
const _Val& __v)
1333 if (__position._M_node == _M_end())
1336 && _M_impl._M_key_compare(_S_key(_M_rightmost()),
1337 _KeyOfValue()(__v)))
1338 return _M_insert_(0, _M_rightmost(), _GLIBCXX_FORWARD(_Arg, __v));
1340 return _M_insert_unique(_GLIBCXX_FORWARD(_Arg, __v)).first;
1342 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1343 _S_key(__position._M_node)))
1346 const_iterator __before = __position;
1347 if (__position._M_node == _M_leftmost())
1348 return _M_insert_(_M_leftmost(), _M_leftmost(),
1349 _GLIBCXX_FORWARD(_Arg, __v));
1350 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node),
1351 _KeyOfValue()(__v)))
1353 if (_S_right(__before._M_node) == 0)
1354 return _M_insert_(0, __before._M_node,
1355 _GLIBCXX_FORWARD(_Arg, __v));
1357 return _M_insert_(__position._M_node,
1359 _GLIBCXX_FORWARD(_Arg, __v));
1362 return _M_insert_unique(_GLIBCXX_FORWARD(_Arg, __v)).first;
1364 else if (_M_impl._M_key_compare(_S_key(__position._M_node),
1365 _KeyOfValue()(__v)))
1368 const_iterator __after = __position;
1369 if (__position._M_node == _M_rightmost())
1370 return _M_insert_(0, _M_rightmost(),
1371 _GLIBCXX_FORWARD(_Arg, __v));
1372 else if (_M_impl._M_key_compare(_KeyOfValue()(__v),
1373 _S_key((++__after)._M_node)))
1375 if (_S_right(__position._M_node) == 0)
1376 return _M_insert_(0, __position._M_node,
1377 _GLIBCXX_FORWARD(_Arg, __v));
1379 return _M_insert_(__after._M_node, __after._M_node,
1380 _GLIBCXX_FORWARD(_Arg, __v));
1383 return _M_insert_unique(_GLIBCXX_FORWARD(_Arg, __v)).first;
1387 return __position._M_const_cast();
1390 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1391 typename _Compare,
typename _Alloc>
1392 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1393 template<
typename _Arg>
1395 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1396 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1397 #ifdef __GXX_EXPERIMENTAL_CXX0X__
1398 _M_insert_equal_(const_iterator __position, _Arg&& __v)
1400 _M_insert_equal_(const_iterator __position,
const _Val& __v)
1404 if (__position._M_node == _M_end())
1407 && !_M_impl._M_key_compare(_KeyOfValue()(__v),
1408 _S_key(_M_rightmost())))
1409 return _M_insert_(0, _M_rightmost(),
1410 _GLIBCXX_FORWARD(_Arg, __v));
1412 return _M_insert_equal(_GLIBCXX_FORWARD(_Arg, __v));
1414 else if (!_M_impl._M_key_compare(_S_key(__position._M_node),
1415 _KeyOfValue()(__v)))
1418 const_iterator __before = __position;
1419 if (__position._M_node == _M_leftmost())
1420 return _M_insert_(_M_leftmost(), _M_leftmost(),
1421 _GLIBCXX_FORWARD(_Arg, __v));
1422 else if (!_M_impl._M_key_compare(_KeyOfValue()(__v),
1423 _S_key((--__before)._M_node)))
1425 if (_S_right(__before._M_node) == 0)
1426 return _M_insert_(0, __before._M_node,
1427 _GLIBCXX_FORWARD(_Arg, __v));
1429 return _M_insert_(__position._M_node,
1431 _GLIBCXX_FORWARD(_Arg, __v));
1434 return _M_insert_equal(_GLIBCXX_FORWARD(_Arg, __v));
1439 const_iterator __after = __position;
1440 if (__position._M_node == _M_rightmost())
1441 return _M_insert_(0, _M_rightmost(),
1442 _GLIBCXX_FORWARD(_Arg, __v));
1443 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node),
1444 _KeyOfValue()(__v)))
1446 if (_S_right(__position._M_node) == 0)
1447 return _M_insert_(0, __position._M_node,
1448 _GLIBCXX_FORWARD(_Arg, __v));
1450 return _M_insert_(__after._M_node, __after._M_node,
1451 _GLIBCXX_FORWARD(_Arg, __v));
1454 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
1458 template<
typename _Key,
typename _Val,
typename _KoV,
1459 typename _Cmp,
typename _Alloc>
1462 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1463 _M_insert_unique(_II __first, _II __last)
1465 for (; __first != __last; ++__first)
1466 _M_insert_unique_(
end(), *__first);
1469 template<
typename _Key,
typename _Val,
typename _KoV,
1470 typename _Cmp,
typename _Alloc>
1473 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1474 _M_insert_equal(_II __first, _II __last)
1476 for (; __first != __last; ++__first)
1477 _M_insert_equal_(
end(), *__first);
1480 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1481 typename _Compare,
typename _Alloc>
1483 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1484 _M_erase_aux(const_iterator __position)
1487 static_cast<_Link_type
>(_Rb_tree_rebalance_for_erase
1488 (const_cast<_Base_ptr>(__position._M_node),
1489 this->_M_impl._M_header));
1490 _M_destroy_node(__y);
1491 --_M_impl._M_node_count;
1494 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1495 typename _Compare,
typename _Alloc>
1497 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1498 _M_erase_aux(const_iterator __first, const_iterator __last)
1500 if (__first ==
begin() && __last ==
end())
1503 while (__first != __last)
1507 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1508 typename _Compare,
typename _Alloc>
1509 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1510 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1511 erase(
const _Key& __x)
1513 pair<iterator, iterator> __p = equal_range(__x);
1514 const size_type __old_size =
size();
1515 erase(__p.first, __p.second);
1516 return __old_size -
size();
1519 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1520 typename _Compare,
typename _Alloc>
1522 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1523 erase(
const _Key* __first,
const _Key* __last)
1525 while (__first != __last)
1529 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1530 typename _Compare,
typename _Alloc>
1531 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1532 _Compare, _Alloc>::iterator
1533 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1534 find(
const _Key& __k)
1536 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1537 return (__j ==
end()
1538 || _M_impl._M_key_compare(__k,
1539 _S_key(__j._M_node))) ?
end() : __j;
1542 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1543 typename _Compare,
typename _Alloc>
1544 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1545 _Compare, _Alloc>::const_iterator
1546 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1547 find(
const _Key& __k)
const
1549 const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1550 return (__j ==
end()
1551 || _M_impl._M_key_compare(__k,
1552 _S_key(__j._M_node))) ?
end() : __j;
1555 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1556 typename _Compare,
typename _Alloc>
1557 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1559 count(
const _Key& __k)
const
1561 pair<const_iterator, const_iterator> __p = equal_range(__k);
1566 _GLIBCXX_PURE
unsigned int
1567 _Rb_tree_black_count(
const _Rb_tree_node_base* __node,
1568 const _Rb_tree_node_base* __root)
throw ();
1570 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1571 typename _Compare,
typename _Alloc>
1573 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify()
const
1575 if (_M_impl._M_node_count == 0 ||
begin() ==
end())
1576 return _M_impl._M_node_count == 0 &&
begin() ==
end()
1577 && this->_M_impl._M_header._M_left == _M_end()
1578 && this->_M_impl._M_header._M_right == _M_end();
1580 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1581 for (const_iterator __it =
begin(); __it !=
end(); ++__it)
1583 _Const_Link_type __x =
static_cast<_Const_Link_type
>(__it._M_node);
1584 _Const_Link_type __L = _S_left(__x);
1585 _Const_Link_type __R = _S_right(__x);
1587 if (__x->_M_color == _S_red)
1588 if ((__L && __L->_M_color == _S_red)
1589 || (__R && __R->_M_color == _S_red))
1592 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1594 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1597 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1601 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1603 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1608 _GLIBCXX_END_NAMESPACE_VERSION