62#pragma GCC system_header
71#if __cplusplus >= 201103L
74#ifdef __glibcxx_node_extract
78#if __cplusplus < 201103L
79# undef _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
80# define _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE 0
81#elif ! defined _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
82# define _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE 1
85namespace std _GLIBCXX_VISIBILITY(default)
87_GLIBCXX_BEGIN_NAMESPACE_VERSION
105 enum _Rb_tree_color { _S_red =
false, _S_black =
true };
107 struct _Rb_tree_node_base
109 typedef _Rb_tree_node_base* _Base_ptr;
111 _Rb_tree_color _M_color;
117 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
119 while (__x->_M_left != 0) __x = __x->_M_left;
124 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
126 while (__x->_M_right != 0) __x = __x->_M_right;
134 _M_base_ptr() const _GLIBCXX_NOEXCEPT
135 {
return const_cast<_Rb_tree_node_base*
>(
this); }
139 template<
typename _Key_compare>
140 struct _Rb_tree_key_compare
142 _Key_compare _M_key_compare;
144 _Rb_tree_key_compare()
145 _GLIBCXX_NOEXCEPT_IF(
146 is_nothrow_default_constructible<_Key_compare>::value)
150 _Rb_tree_key_compare(
const _Key_compare& __comp)
151 : _M_key_compare(__comp)
154#if __cplusplus >= 201103L
156 _Rb_tree_key_compare(
const _Rb_tree_key_compare&) =
default;
158 _Rb_tree_key_compare(_Rb_tree_key_compare&& __x)
159 noexcept(is_nothrow_copy_constructible<_Key_compare>::value)
160 : _M_key_compare(__x._M_key_compare)
166 struct _Rb_tree_header
168 _Rb_tree_node_base _M_header;
169 size_t _M_node_count;
171 _Rb_tree_header() _GLIBCXX_NOEXCEPT
173 _M_header._M_color = _S_red;
177#if __cplusplus >= 201103L
178 _Rb_tree_header(_Rb_tree_header&& __x)
noexcept
180 if (__x._M_header._M_parent !=
nullptr)
184 _M_header._M_color = _S_red;
191 _M_move_data(_Rb_tree_header& __from)
193 _M_header._M_color = __from._M_header._M_color;
194 _M_header._M_parent = __from._M_header._M_parent;
195 _M_header._M_left = __from._M_header._M_left;
196 _M_header._M_right = __from._M_header._M_right;
197 _M_header._M_parent->_M_parent = &_M_header;
198 _M_node_count = __from._M_node_count;
206 _M_header._M_parent = 0;
207 _M_header._M_left = &_M_header;
208 _M_header._M_right = &_M_header;
213 template<
typename _Val>
214 struct _Rb_tree_node :
public _Rb_tree_node_base
216#if __cplusplus < 201103L
227 __gnu_cxx::__aligned_membuf<_Val> _M_storage;
231 {
return _M_storage._M_ptr(); }
235 {
return _M_storage._M_ptr(); }
239 _M_node_ptr() _GLIBCXX_NOEXCEPT
243#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
246 template<
typename _Vo
idPtr>
249 using _Base_ptr = __ptr_rebind<_VoidPtr, _Node_base>;
251 _Rb_tree_color _M_color;
257 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
259 while (__x->_M_left) __x = __x->_M_left;
264 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
266 while (__x->_M_right) __x = __x->_M_right;
274 _M_base_ptr() const noexcept
276 return pointer_traits<_Base_ptr>::pointer_to
277 (*
const_cast<_Node_base*
>(
this));
282 template<
typename _NodeBase>
286 using _Base_ptr =
typename _NodeBase::_Base_ptr;
290 size_t _M_node_count;
294 _M_header._M_color = _S_red;
298 _Header(_Header&& __x)
noexcept
300 if (__x._M_header._M_parent)
304 _M_header._M_color = _S_red;
310 _M_move_data(_Header& __from)
312 _M_header._M_color = __from._M_header._M_color;
313 _M_header._M_parent = __from._M_header._M_parent;
314 _M_header._M_left = __from._M_header._M_left;
315 _M_header._M_right = __from._M_header._M_right;
316 _M_header._M_parent->_M_parent = _M_header._M_base_ptr();
317 _M_node_count = __from._M_node_count;
325 _M_header._M_parent =
nullptr;
326 _M_header._M_left = _M_header._M_right = _M_header._M_base_ptr();
331 template<
typename _ValPtr>
332 struct _Node :
public __rb_tree::_Node_base<__ptr_rebind<_ValPtr, void>>
334 using value_type =
typename pointer_traits<_ValPtr>::element_type;
339 _Node(_Node&&) =
delete;
341 union _Uninit_storage
343 _Uninit_storage() noexcept { }
344 ~_Uninit_storage() { }
348 _Uninit_storage _M_u;
359 _M_node_ptr() noexcept
360 {
return pointer_traits<_Node_ptr>::pointer_to(*
this); }
365 _GLIBCXX_PURE _Rb_tree_node_base*
366 _Rb_tree_increment(_Rb_tree_node_base* __x)
throw ();
368 _GLIBCXX_PURE _Rb_tree_node_base*
369 _Rb_tree_decrement(_Rb_tree_node_base* __x)
throw ();
371 template<
typename _Tp>
372 struct _Rb_tree_iterator
374 typedef _Tp value_type;
375 typedef _Tp& reference;
376 typedef _Tp* pointer;
378 typedef bidirectional_iterator_tag iterator_category;
379 typedef ptrdiff_t difference_type;
381 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
382 typedef _Rb_tree_node<_Tp>* _Node_ptr;
384 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
388 _Rb_tree_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
392 operator*() const _GLIBCXX_NOEXCEPT
393 {
return *
static_cast<_Node_ptr
>(_M_node)->_M_valptr(); }
396 operator->() const _GLIBCXX_NOEXCEPT
397 {
return static_cast<_Node_ptr
>(_M_node)->_M_valptr(); }
400 operator++() _GLIBCXX_NOEXCEPT
402 _M_node = _Rb_tree_increment(_M_node);
407 operator++(
int) _GLIBCXX_NOEXCEPT
409 _Rb_tree_iterator __tmp = *
this;
410 _M_node = _Rb_tree_increment(_M_node);
415 operator--() _GLIBCXX_NOEXCEPT
417 _M_node = _Rb_tree_decrement(_M_node);
422 operator--(
int) _GLIBCXX_NOEXCEPT
424 _Rb_tree_iterator __tmp = *
this;
425 _M_node = _Rb_tree_decrement(_M_node);
430 operator==(
const _Rb_tree_iterator& __x,
431 const _Rb_tree_iterator& __y) _GLIBCXX_NOEXCEPT
432 {
return __x._M_node == __y._M_node; }
434#if ! __cpp_lib_three_way_comparison
436 operator!=(
const _Rb_tree_iterator& __x,
437 const _Rb_tree_iterator& __y) _GLIBCXX_NOEXCEPT
438 {
return __x._M_node != __y._M_node; }
444 template<
typename _Tp>
445 struct _Rb_tree_const_iterator
447 typedef _Tp value_type;
448 typedef const _Tp& reference;
449 typedef const _Tp* pointer;
451 typedef _Rb_tree_iterator<_Tp> iterator;
453 typedef bidirectional_iterator_tag iterator_category;
454 typedef ptrdiff_t difference_type;
456 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
457 typedef const _Rb_tree_node<_Tp>* _Node_ptr;
459 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
463 _Rb_tree_const_iterator(_Base_ptr __x) _GLIBCXX_NOEXCEPT
466 _Rb_tree_const_iterator(
const iterator& __it) _GLIBCXX_NOEXCEPT
467 : _M_node(__it._M_node) { }
470 operator*() const _GLIBCXX_NOEXCEPT
471 {
return *
static_cast<_Node_ptr
>(_M_node)->_M_valptr(); }
474 operator->() const _GLIBCXX_NOEXCEPT
475 {
return static_cast<_Node_ptr
>(_M_node)->_M_valptr(); }
477 _Rb_tree_const_iterator&
478 operator++() _GLIBCXX_NOEXCEPT
480 _M_node = _Rb_tree_increment(_M_node);
484 _Rb_tree_const_iterator
485 operator++(
int) _GLIBCXX_NOEXCEPT
487 _Rb_tree_const_iterator __tmp = *
this;
488 _M_node = _Rb_tree_increment(_M_node);
492 _Rb_tree_const_iterator&
493 operator--() _GLIBCXX_NOEXCEPT
495 _M_node = _Rb_tree_decrement(_M_node);
499 _Rb_tree_const_iterator
500 operator--(
int) _GLIBCXX_NOEXCEPT
502 _Rb_tree_const_iterator __tmp = *
this;
503 _M_node = _Rb_tree_decrement(_M_node);
508 operator==(
const _Rb_tree_const_iterator& __x,
509 const _Rb_tree_const_iterator& __y) _GLIBCXX_NOEXCEPT
510 {
return __x._M_node == __y._M_node; }
512#if ! __cpp_lib_three_way_comparison
514 operator!=(
const _Rb_tree_const_iterator& __x,
515 const _Rb_tree_const_iterator& __y) _GLIBCXX_NOEXCEPT
516 {
return __x._M_node != __y._M_node; }
522 __attribute__((__nonnull__))
524 _Rb_tree_insert_and_rebalance(
const bool __insert_left,
525 _Rb_tree_node_base* __x,
526 _Rb_tree_node_base* __p,
527 _Rb_tree_node_base& __header)
throw ();
529 __attribute__((__nonnull__,__returns_nonnull__))
531 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base*
const __z,
532 _Rb_tree_node_base& __header)
throw ();
536#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
537 template<
bool _Const,
typename _ValPtr>
540 template<
typename _Tp>
541 using __maybe_const = __conditional_t<_Const, const _Tp, _Tp>;
543 using __ptr_traits = pointer_traits<_ValPtr>;
544 using value_type =
typename __ptr_traits::element_type;
545 using reference = __maybe_const<value_type>&;
546 using pointer = __maybe_const<value_type>*;
548 using iterator_category = bidirectional_iterator_tag;
549 using difference_type = ptrdiff_t;
551 using _Node = __rb_tree::_Node<_ValPtr>;
552 using _Node_base = __rb_tree::_Node_base<__ptr_rebind<_ValPtr, void>>;
553 using _Base_ptr =
typename _Node_base::_Base_ptr;
559 _Iterator(_Base_ptr __x) noexcept
562 _Iterator(
const _Iterator&) =
default;
563 _Iterator& operator=(
const _Iterator&) =
default;
565#ifdef __glibcxx_concepts
567 _Iterator(
const _Iterator<false, _ValPtr>& __it)
requires _Const
569 template<
bool _OtherConst,
570 typename = __enable_if_t<_Const && !_OtherConst>>
572 _Iterator(
const _Iterator<_OtherConst, _ValPtr>& __it)
574 : _M_node(__it._M_node) { }
578 operator*() const noexcept
579 {
return *
static_cast<_Node&
>(*_M_node)._M_valptr(); }
583 operator->() const noexcept
584 {
return static_cast<_Node&
>(*_M_node)._M_valptr(); }
586 _GLIBCXX14_CONSTEXPR _Iterator&
587 operator++() noexcept
589 if (_M_node->_M_right)
591 _M_node = _M_node->_M_right;
592 while (_M_node->_M_left)
593 _M_node = _M_node->_M_left;
597 _Base_ptr __y = _M_node->_M_parent;
598 while (_M_node == __y->_M_right)
601 __y = __y->_M_parent;
603 if (_M_node->_M_right != __y)
610 _GLIBCXX14_CONSTEXPR _Iterator
611 operator++(
int)
noexcept
613 _Iterator __tmp(this->_M_node);
618 _GLIBCXX14_CONSTEXPR _Iterator&
619 operator--() noexcept
621 if (_M_node->_M_color == _S_red
622 && _M_node->_M_parent->_M_parent == _M_node)
623 _M_node = _M_node->_M_right;
624 else if (_M_node->_M_left)
626 _Base_ptr __y = _M_node->_M_left;
627 while (__y->_M_right)
633 _Base_ptr __y = _M_node->_M_parent;
634 while (_M_node == __y->_M_left)
637 __y = __y->_M_parent;
644 _GLIBCXX14_CONSTEXPR _Iterator
645 operator--(
int)
noexcept
647 _Iterator __tmp(this->_M_node);
654 operator==(
const _Iterator& __x,
const _Iterator& __y) _GLIBCXX_NOEXCEPT
655 {
return __x._M_node == __y._M_node; }
657#if ! __cpp_lib_three_way_comparison
660 operator!=(
const _Iterator& __x,
const _Iterator& __y) _GLIBCXX_NOEXCEPT
661 {
return __x._M_node != __y._M_node; }
669 template<
typename _Val,
typename _Ptr>
672#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE <= 9000
676 template<
typename _Val>
677 struct _Node_traits<_Val, _Val*>
679 typedef _Rb_tree_node<_Val> _Node;
680 typedef _Node* _Node_ptr;
681 typedef _Rb_tree_node_base _Node_base;
682 typedef _Node_base* _Base_ptr;
683 typedef _Rb_tree_header _Header_t;
684 typedef _Rb_tree_iterator<_Val> _Iterator;
685 typedef _Rb_tree_const_iterator<_Val> _Const_iterator;
687 __attribute__((__nonnull__))
689 _S_insert_and_rebalance(
const bool __insert_left,
690 _Node_base* __x, _Node_base* __p,
691 _Node_base& __header) _GLIBCXX_USE_NOEXCEPT
693 return _Rb_tree_insert_and_rebalance(__insert_left, __x, __p, __header);
696 __attribute__((__nonnull__,__returns_nonnull__))
698 _S_rebalance_for_erase(_Node_base*
const __z,
699 _Node_base& __header) _GLIBCXX_USE_NOEXCEPT
700 {
return _Rb_tree_rebalance_for_erase(__z, __header); }
704#if ! _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
706 template<
typename _Val,
typename _Ptr>
708 : _Node_traits<_Val, _Val*>
712 template<
typename _Val,
typename _ValPtr>
715 using _Node = __rb_tree::_Node<_ValPtr>;
717 using _Node_base = __rb_tree::_Node_base<__ptr_rebind<_ValPtr, void>>;
719 using _Header_t = __rb_tree::_Header<_Node_base>;
720 using _Iterator = __rb_tree::_Iterator<false, _ValPtr>;
721 using _Const_iterator = __rb_tree::_Iterator<true, _ValPtr>;
724 _Rotate_left(_Base_ptr __x, _Base_ptr& __root)
726 const _Base_ptr __y = __x->_M_right;
728 __x->_M_right = __y->_M_left;
730 __y->_M_left->_M_parent = __x;
731 __y->_M_parent = __x->_M_parent;
735 else if (__x == __x->_M_parent->_M_left)
736 __x->_M_parent->_M_left = __y;
738 __x->_M_parent->_M_right = __y;
740 __x->_M_parent = __y;
744 _Rotate_right(_Base_ptr __x, _Base_ptr& __root)
746 const _Base_ptr __y = __x->_M_left;
748 __x->_M_left = __y->_M_right;
750 __y->_M_right->_M_parent = __x;
751 __y->_M_parent = __x->_M_parent;
755 else if (__x == __x->_M_parent->_M_right)
756 __x->_M_parent->_M_right = __y;
758 __x->_M_parent->_M_left = __y;
760 __x->_M_parent = __y;
764 _S_insert_and_rebalance(
const bool __insert_left,
765 _Base_ptr __x, _Base_ptr __p,
766 _Node_base& __header)
768 _Base_ptr& __root = __header._M_parent;
771 __x->_M_parent = __p;
772 __x->_M_left = __x->_M_right =
nullptr;
773 __x->_M_color = _S_red;
785 __header._M_parent = __x;
786 __header._M_right = __x;
788 else if (__p == __header._M_left)
789 __header._M_left = __x;
795 if (__p == __header._M_right)
796 __header._M_right = __x;
800 && __x->_M_parent->_M_color == _S_red)
802 const _Base_ptr __xpp = __x->_M_parent->_M_parent;
804 if (__x->_M_parent == __xpp->_M_left)
806 const _Base_ptr __y = __xpp->_M_right;
807 if (__y && __y->_M_color == _S_red)
809 __x->_M_parent->_M_color = _S_black;
810 __y->_M_color = _S_black;
811 __xpp->_M_color = _S_red;
816 if (__x == __x->_M_parent->_M_right)
818 __x = __x->_M_parent;
819 _Rotate_left(__x, __root);
821 __x->_M_parent->_M_color = _S_black;
822 __xpp->_M_color = _S_red;
823 _Rotate_right(__xpp, __root);
828 const _Base_ptr __y = __xpp->_M_left;
829 if (__y && __y->_M_color == _S_red)
831 __x->_M_parent->_M_color = _S_black;
832 __y->_M_color = _S_black;
833 __xpp->_M_color = _S_red;
838 if (__x == __x->_M_parent->_M_left)
840 __x = __x->_M_parent;
841 _Rotate_right(__x, __root);
843 __x->_M_parent->_M_color = _S_black;
844 __xpp->_M_color = _S_red;
845 _Rotate_left(__xpp, __root);
849 __root->_M_color = _S_black;
853 _S_rebalance_for_erase(_Base_ptr __z, _Node_base& __header)
855 _Base_ptr& __root = __header._M_parent;
856 _Base_ptr& __leftmost = __header._M_left;
857 _Base_ptr& __rightmost = __header._M_right;
860 _Base_ptr __x_parent{};
878 __z->_M_left->_M_parent = __y;
879 __y->_M_left = __z->_M_left;
880 if (__y != __z->_M_right)
882 __x_parent = __y->_M_parent;
884 __x->_M_parent = __y->_M_parent;
885 __y->_M_parent->_M_left = __x;
886 __y->_M_right = __z->_M_right;
887 __z->_M_right->_M_parent = __y;
893 else if (__z->_M_parent->_M_left == __z)
894 __z->_M_parent->_M_left = __y;
896 __z->_M_parent->_M_right = __y;
897 __y->_M_parent = __z->_M_parent;
898 std::swap(__y->_M_color, __z->_M_color);
904 __x_parent = __y->_M_parent;
906 __x->_M_parent = __y->_M_parent;
910 if (__z->_M_parent->_M_left == __z)
911 __z->_M_parent->_M_left = __x;
913 __z->_M_parent->_M_right = __x;
914 if (__leftmost == __z)
917 __leftmost = __z->_M_parent;
920 __leftmost = _Node_base::_S_minimum(__x);
922 if (__rightmost == __z)
924 if (__z->_M_left == 0)
925 __rightmost = __z->_M_parent;
928 __rightmost = _Node_base::_S_maximum(__x);
931 if (__y->_M_color != _S_red)
933 while (__x != __root && (__x == 0 || __x->_M_color == _S_black))
934 if (__x == __x_parent->_M_left)
936 _Base_ptr __w = __x_parent->_M_right;
937 if (__w->_M_color == _S_red)
939 __w->_M_color = _S_black;
940 __x_parent->_M_color = _S_red;
941 _Rotate_left(__x_parent, __root);
942 __w = __x_parent->_M_right;
944 if ((!__w->_M_left || __w->_M_left->_M_color == _S_black) &&
945 (!__w->_M_right || __w->_M_right->_M_color == _S_black))
947 __w->_M_color = _S_red;
949 __x_parent = __x_parent->_M_parent;
953 if (!__w->_M_right || __w->_M_right->_M_color == _S_black)
955 __w->_M_left->_M_color = _S_black;
956 __w->_M_color = _S_red;
957 _Rotate_right(__w, __root);
958 __w = __x_parent->_M_right;
960 __w->_M_color = __x_parent->_M_color;
961 __x_parent->_M_color = _S_black;
963 __w->_M_right->_M_color = _S_black;
964 _Rotate_left(__x_parent, __root);
971 _Base_ptr __w = __x_parent->_M_left;
972 if (__w->_M_color == _S_red)
974 __w->_M_color = _S_black;
975 __x_parent->_M_color = _S_red;
976 _Rotate_right(__x_parent, __root);
977 __w = __x_parent->_M_left;
979 if ((!__w->_M_right || __w->_M_right->_M_color == _S_black) &&
980 (!__w->_M_left || __w->_M_left->_M_color == _S_black))
982 __w->_M_color = _S_red;
984 __x_parent = __x_parent->_M_parent;
988 if (!__w->_M_left || __w->_M_left->_M_color == _S_black)
990 __w->_M_right->_M_color = _S_black;
991 __w->_M_color = _S_red;
992 _Rotate_left(__w, __root);
993 __w = __x_parent->_M_left;
995 __w->_M_color = __x_parent->_M_color;
996 __x_parent->_M_color = _S_black;
998 __w->_M_left->_M_color = _S_black;
999 _Rotate_right(__x_parent, __root);
1004 __x->_M_color = _S_black;
1013#ifdef __glibcxx_node_extract
1014 template<
typename _Tree1,
typename _Cmp2>
1015 struct _Rb_tree_merge_helper { };
1018 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1022 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
1023 rebind<_Val>::other _Val_alloc_type;
1025 typedef __gnu_cxx::__alloc_traits<_Val_alloc_type> _Val_alloc_traits;
1026 typedef typename _Val_alloc_traits::pointer _ValPtr;
1027 typedef __rb_tree::_Node_traits<_Val, _ValPtr> _Node_traits;
1029 typedef typename _Node_traits::_Node_base _Node_base;
1030 typedef typename _Node_traits::_Node _Node;
1032 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
1033 rebind<_Node>::other _Node_allocator;
1035 typedef __gnu_cxx::__alloc_traits<_Node_allocator> _Node_alloc_traits;
1038 typedef typename _Node_traits::_Base_ptr _Base_ptr;
1039 typedef typename _Node_traits::_Node_ptr _Node_ptr;
1044 struct _Reuse_or_alloc_node
1046 _Reuse_or_alloc_node(_Rb_tree& __t)
1047 : _M_root(__t._M_root()), _M_nodes(__t._M_rightmost()), _M_t(__t)
1051 _M_root->_M_parent = _Base_ptr();
1053 if (_M_nodes->_M_left)
1054 _M_nodes = _M_nodes->_M_left;
1057 _M_nodes = _Base_ptr();
1060#pragma GCC diagnostic push
1061#pragma GCC diagnostic ignored "-Wc++11-extensions"
1062 _Reuse_or_alloc_node(
const _Reuse_or_alloc_node&) =
delete;
1063#pragma GCC diagnostic pop
1065 ~_Reuse_or_alloc_node()
1068 _M_t._M_erase(
static_cast<_Node&
>(*_M_root)._M_node_ptr());
1071 template<
typename _Arg>
1073 operator()(_GLIBCXX_FWDREF(_Arg) __arg)
1075 _Base_ptr
__base = _M_extract();
1078 _Node_ptr __node =
static_cast<_Node&
>(*__base)._M_node_ptr();
1079 _M_t._M_destroy_node(__node);
1080 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
1084 return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
1094 _Base_ptr __node = _M_nodes;
1095 _M_nodes = _M_nodes->_M_parent;
1098 if (_M_nodes->_M_right == __node)
1100 _M_nodes->_M_right = _Base_ptr();
1102 if (_M_nodes->_M_left)
1104 _M_nodes = _M_nodes->_M_left;
1106 while (_M_nodes->_M_right)
1107 _M_nodes = _M_nodes->_M_right;
1109 if (_M_nodes->_M_left)
1110 _M_nodes = _M_nodes->_M_left;
1114 _M_nodes->_M_left = _Base_ptr();
1117 _M_root = _Base_ptr();
1131 _Alloc_node(_Rb_tree& __t)
1134 template<
typename _Arg>
1136 operator()(_GLIBCXX_FWDREF(_Arg) __arg)
const
1137 {
return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
1144 typedef _Key key_type;
1145 typedef _Val value_type;
1146 typedef value_type* pointer;
1147 typedef const value_type* const_pointer;
1148 typedef value_type& reference;
1149 typedef const value_type& const_reference;
1150 typedef size_t size_type;
1151 typedef ptrdiff_t difference_type;
1152 typedef _Alloc allocator_type;
1155 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
1156 {
return this->_M_impl; }
1158 const _Node_allocator&
1159 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
1160 {
return this->_M_impl; }
1163 get_allocator() const _GLIBCXX_NOEXCEPT
1164 {
return allocator_type(_M_get_Node_allocator()); }
1170#if __cplusplus < 201102L || _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
1173#pragma GCC diagnostic push
1174#pragma GCC diagnostic ignored "-Wc++17-extensions"
1175 using __alloc_pointer =
typename _Node_alloc_traits::pointer;
1176 if constexpr (is_same<_Node_ptr, __alloc_pointer>::value)
1182 return std::__to_address(__ptr);
1184#pragma GCC diagnostic pop
1189 _M_put_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1191#if __cplusplus < 201102L || _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
1194#pragma GCC diagnostic push
1195#pragma GCC diagnostic ignored "-Wc++17-extensions"
1196 using __alloc_pointer =
typename _Node_alloc_traits::pointer;
1197 if constexpr (is_same<_Node_ptr, __alloc_pointer>::value)
1203 auto __ap = pointer_traits<__alloc_pointer>::pointer_to(*__p);
1206#pragma GCC diagnostic pop
1210#if __cplusplus < 201103L
1212 _M_construct_node(_Node_ptr __node,
const value_type& __x)
1215 { get_allocator().construct(__node->_M_valptr(), __x); }
1218 _M_put_node(__node);
1219 __throw_exception_again;
1224 _M_create_node(
const value_type& __x)
1226 _Node_ptr __tmp = _M_get_node();
1227 _M_construct_node(__tmp, __x);
1231 template<
typename... _Args>
1233 _M_construct_node(_Node_ptr __node, _Args&&... __args)
1238 _Node_alloc_traits::construct(_M_get_Node_allocator(),
1239 __node->_M_valptr(),
1245 _M_put_node(__node);
1246 __throw_exception_again;
1250 template<
typename... _Args>
1252 _M_create_node(_Args&&... __args)
1254 _Node_ptr __tmp = _M_get_node();
1261 _M_destroy_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1263#if __cplusplus < 201103L
1264 get_allocator().destroy(__p->_M_valptr());
1266 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
1272 _M_drop_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1274 _M_destroy_node(__p);
1278 template<
bool _MoveValue,
typename _NodeGen>
1280 _M_clone_node(_Node_ptr __x, _NodeGen& __node_gen)
1282#if __cplusplus >= 201103L
1283 using _Vp = __conditional_t<_MoveValue,
1288 = __node_gen(_GLIBCXX_FORWARD(_Vp, *__x->_M_valptr()));
1289 __tmp->_M_color = __x->_M_color;
1290 __tmp->_M_left = __tmp->_M_right = _Base_ptr();
1295 typedef typename _Node_traits::_Header_t _Header_t;
1297#if _GLIBCXX_INLINE_VERSION
1298 template<
typename _Key_compare>
1301 template<
typename _Key_compare,
1302 bool = __is_pod(_Key_compare)>
1304 struct _Rb_tree_impl
1305 :
public _Node_allocator
1306 ,
public _Rb_tree_key_compare<_Key_compare>
1309 typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
1312 _GLIBCXX_NOEXCEPT_IF(
1313 is_nothrow_default_constructible<_Node_allocator>::value
1314 && is_nothrow_default_constructible<_Base_key_compare>::value )
1318 _Rb_tree_impl(
const _Rb_tree_impl& __x)
1319 : _Node_allocator(_Node_alloc_traits::_S_select_on_copy(__x))
1320 , _Base_key_compare(__x._M_key_compare)
1324#if __cplusplus < 201103L
1325 _Rb_tree_impl(
const _Key_compare& __comp,
const _Node_allocator& __a)
1326 : _Node_allocator(__a), _Base_key_compare(__comp)
1329 _Rb_tree_impl(_Rb_tree_impl&&)
1330 noexcept( is_nothrow_move_constructible<_Base_key_compare>::value )
1334 _Rb_tree_impl(_Node_allocator&& __a)
1335 : _Node_allocator(std::
move(__a))
1338 _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
1339 : _Node_allocator(std::
move(__a)),
1340 _Base_key_compare(std::
move(__x)),
1341 _Header_t(std::
move(__x))
1344 _Rb_tree_impl(
const _Key_compare& __comp, _Node_allocator&& __a)
1345 : _Node_allocator(std::
move(__a)), _Base_key_compare(__comp)
1350 _Rb_tree_impl<_Compare> _M_impl;
1354 _M_root() _GLIBCXX_NOEXCEPT
1355 {
return this->_M_impl._M_header._M_parent; }
1358 _M_root() const _GLIBCXX_NOEXCEPT
1359 {
return this->_M_impl._M_header._M_parent; }
1362 _M_leftmost() _GLIBCXX_NOEXCEPT
1363 {
return this->_M_impl._M_header._M_left; }
1366 _M_leftmost() const _GLIBCXX_NOEXCEPT
1367 {
return this->_M_impl._M_header._M_left; }
1370 _M_rightmost() _GLIBCXX_NOEXCEPT
1371 {
return this->_M_impl._M_header._M_right; }
1374 _M_rightmost() const _GLIBCXX_NOEXCEPT
1375 {
return this->_M_impl._M_header._M_right; }
1378 _M_begin() const _GLIBCXX_NOEXCEPT
1379 {
return this->_M_impl._M_header._M_parent; }
1382 _M_begin_node() const _GLIBCXX_NOEXCEPT
1384 _Base_ptr __begin = this->_M_impl._M_header._M_parent;
1386 ?
static_cast<_Node&
>(*__begin)._M_node_ptr()
1391 _M_end() const _GLIBCXX_NOEXCEPT
1392 {
return this->_M_impl._M_header._M_base_ptr(); }
1396 template<
typename _Key1,
typename _Key2>
1398 _M_key_compare(
const _Key1& __k1,
const _Key2& __k2)
const
1400#if __cplusplus >= 201103L
1403 __is_invocable<const _Compare&, const _Key&, const _Key&>::value,
1404 "comparison object must be invocable with arguments of key_type"
1407 return _M_impl._M_key_compare(__k1, __k2);
1411 _S_key(
const _Node& __node)
1412 {
return _KeyOfValue()(*__node._M_valptr()); }
1415 _S_key(_Base_ptr __x)
1416 {
return _S_key(
static_cast<const _Node&
>(*__x)); }
1419 _S_key(_Node_ptr __x)
1420 {
return _S_key(*__x); }
1423 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
1424 {
return __x->_M_left; }
1427 _S_left(_Node_ptr __x)
1430 ?
static_cast<_Node&
>(*__x->_M_left)._M_node_ptr()
1435 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
1436 {
return __x->_M_right; }
1439 _S_right(_Node_ptr __x) _GLIBCXX_NOEXCEPT
1441 return __x->_M_right
1442 ?
static_cast<_Node&
>(*__x->_M_right)._M_node_ptr()
1447 typedef typename _Node_traits::_Iterator iterator;
1448 typedef typename _Node_traits::_Const_iterator const_iterator;
1450 typedef std::reverse_iterator<iterator> reverse_iterator;
1451 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1453#ifdef __glibcxx_node_extract
1454 using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
1455 using insert_return_type = _Node_insert_return<
1456 __conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
1461 _M_get_insert_unique_pos(
const key_type& __k);
1464 _M_get_insert_equal_pos(
const key_type& __k);
1467 _M_get_insert_hint_unique_pos(const_iterator __pos,
1468 const key_type& __k);
1471 _M_get_insert_hint_equal_pos(const_iterator __pos,
1472 const key_type& __k);
1474#ifdef __glibcxx_associative_heterogeneous_insertion
1475 template <
typename... _Args>
1477 _M_emplace_here(
bool __place_left, _Base_ptr __node, _Args&&... __args);
1479 template <
typename _Kt>
1481 _M_get_insert_unique_pos_tr(
const _Kt& __k);
1483 template <
typename _Kt>
1485 _M_get_insert_hint_unique_pos_tr(const_iterator,
const _Kt& __k);
1489#if __cplusplus >= 201103L
1490 template<
typename _Arg,
typename _NodeGen>
1492 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
1495 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Node_ptr __z);
1497 template<
typename _Arg>
1499 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
1501 template<
typename _Arg>
1503 _M_insert_equal_lower(_Arg&& __x);
1506 _M_insert_lower_node(_Base_ptr __p, _Node_ptr __z);
1509 _M_insert_equal_lower_node(_Node_ptr __z);
1511 template<
typename _NodeGen>
1513 _M_insert_(_Base_ptr __x, _Base_ptr __y,
1514 const value_type& __v, _NodeGen&);
1519 _M_insert_lower(_Base_ptr __y,
const value_type& __v);
1522 _M_insert_equal_lower(
const value_type& __x);
1525 enum { __as_lvalue, __as_rvalue };
1527 template<
bool _MoveValues,
typename _NodeGen>
1529 _M_copy(_Node_ptr, _Base_ptr, _NodeGen&);
1531 template<
bool _MoveValues,
typename _NodeGen>
1533 _M_copy(
const _Rb_tree& __x, _NodeGen& __gen)
1536 _M_copy<_MoveValues>(__x._M_begin_node(), _M_end(), __gen);
1537 _M_leftmost() = _Node_base::_S_minimum(__root);
1538 _M_rightmost() = _Node_base::_S_maximum(__root);
1539 _M_impl._M_node_count = __x._M_impl._M_node_count;
1544 _M_copy(
const _Rb_tree& __x)
1546 _Alloc_node __an(*
this);
1547 return _M_copy<__as_lvalue>(__x, __an);
1551 _M_erase(_Node_ptr __x);
1554 _M_lower_bound(_Base_ptr __x, _Base_ptr __y,
1555 const _Key& __k)
const;
1557 template <
typename _Kt>
1559 _M_lower_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const;
1562 _M_upper_bound(_Base_ptr __x, _Base_ptr __y,
1563 const _Key& __k)
const;
1565 template <
typename _Kt>
1567 _M_upper_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const;
1571#if __cplusplus < 201103L
1574 _Rb_tree() =
default;
1577 _Rb_tree(
const _Compare& __comp,
1578 const allocator_type& __a = allocator_type())
1579 : _M_impl(__comp, _Node_allocator(__a)) { }
1581 _Rb_tree(
const _Rb_tree& __x)
1582 : _M_impl(__x._M_impl)
1585 _M_root() = _M_copy(__x);
1588#if __cplusplus >= 201103L
1589 _Rb_tree(
const allocator_type& __a)
1590 : _M_impl(_Node_allocator(__a))
1593 _Rb_tree(
const _Rb_tree& __x,
const allocator_type& __a)
1594 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
1597 _M_root() = _M_copy(__x);
1600 _Rb_tree(_Rb_tree&&) =
default;
1602 _Rb_tree(_Rb_tree&& __x,
const allocator_type& __a)
1603 : _Rb_tree(std::
move(__x), _Node_allocator(__a))
1607 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a,
true_type)
1608 noexcept(is_nothrow_default_constructible<_Compare>::value)
1609 : _M_impl(std::
move(__x._M_impl), std::
move(__a))
1612 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a,
false_type)
1613 : _M_impl(__x._M_impl._M_key_compare, std::
move(__a))
1620 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
1624 : _Rb_tree(std::
move(__x), std::
move(__a),
1625 typename _Node_alloc_traits::is_always_equal{})
1629 ~_Rb_tree() _GLIBCXX_NOEXCEPT
1630 { _M_erase(_M_begin_node()); }
1633 operator=(
const _Rb_tree& __x);
1638 {
return _M_impl._M_key_compare; }
1641 begin() _GLIBCXX_NOEXCEPT
1642 {
return iterator(this->_M_impl._M_header._M_left); }
1645 begin() const _GLIBCXX_NOEXCEPT
1646 {
return const_iterator(this->_M_impl._M_header._M_left); }
1649 end() _GLIBCXX_NOEXCEPT
1650 {
return iterator(_M_end()); }
1653 end() const _GLIBCXX_NOEXCEPT
1654 {
return const_iterator(_M_end()); }
1657 rbegin() _GLIBCXX_NOEXCEPT
1658 {
return reverse_iterator(end()); }
1660 const_reverse_iterator
1661 rbegin() const _GLIBCXX_NOEXCEPT
1662 {
return const_reverse_iterator(end()); }
1665 rend() _GLIBCXX_NOEXCEPT
1666 {
return reverse_iterator(begin()); }
1668 const_reverse_iterator
1669 rend() const _GLIBCXX_NOEXCEPT
1670 {
return const_reverse_iterator(begin()); }
1672 _GLIBCXX_NODISCARD
bool
1673 empty() const _GLIBCXX_NOEXCEPT
1674 {
return _M_impl._M_node_count == 0; }
1677 size() const _GLIBCXX_NOEXCEPT
1678 {
return _M_impl._M_node_count; }
1681 max_size() const _GLIBCXX_NOEXCEPT
1686 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value);
1689#if __cplusplus >= 201103L
1690 template<
typename _Arg>
1692 _M_insert_unique(_Arg&& __x);
1694 template<
typename _Arg>
1696 _M_insert_equal(_Arg&& __x);
1698 template<
typename _Arg,
typename _NodeGen>
1700 _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1702 template<
typename _Arg>
1704 _M_insert_unique_(const_iterator __pos, _Arg&& __x)
1706 _Alloc_node __an(*
this);
1710 template<
typename _Arg,
typename _NodeGen>
1712 _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1714 template<
typename _Arg>
1716 _M_insert_equal_(const_iterator __pos, _Arg&& __x)
1718 _Alloc_node __an(*
this);
1722 template<
typename... _Args>
1724 _M_emplace_unique(_Args&&... __args);
1726 template<
typename... _Args>
1728 _M_emplace_equal(_Args&&... __args);
1730 template<
typename... _Args>
1732 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
1734 template<
typename... _Args>
1736 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
1738 template<
typename _Iter>
1739 using __same_value_type
1740 = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
1742 template<
typename _InputIterator>
1743 __enable_if_t<__same_value_type<_InputIterator>::value>
1744 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1746 _Alloc_node __an(*
this);
1747 for (; __first != __last; ++__first)
1748 _M_insert_unique_(end(), *__first, __an);
1751 template<
typename _InputIterator>
1752 __enable_if_t<!__same_value_type<_InputIterator>::value>
1753 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1755 for (; __first != __last; ++__first)
1756 _M_emplace_unique(*__first);
1759 template<
typename _InputIterator>
1760 __enable_if_t<__same_value_type<_InputIterator>::value>
1761 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1763 _Alloc_node __an(*
this);
1764 for (; __first != __last; ++__first)
1765 _M_insert_equal_(end(), *__first, __an);
1768 template<
typename _InputIterator>
1769 __enable_if_t<!__same_value_type<_InputIterator>::value>
1770 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1772 for (; __first != __last; ++__first)
1773 _M_emplace_equal(*__first);
1777 _M_insert_unique(
const value_type& __x);
1780 _M_insert_equal(
const value_type& __x);
1782 template<
typename _NodeGen>
1784 _M_insert_unique_(const_iterator __pos,
const value_type& __x,
1788 _M_insert_unique_(const_iterator __pos,
const value_type& __x)
1790 _Alloc_node __an(*
this);
1791 return _M_insert_unique_(__pos, __x, __an);
1794 template<
typename _NodeGen>
1796 _M_insert_equal_(const_iterator __pos,
const value_type& __x,
1799 _M_insert_equal_(const_iterator __pos,
const value_type& __x)
1801 _Alloc_node __an(*
this);
1802 return _M_insert_equal_(__pos, __x, __an);
1805 template<
typename _InputIterator>
1807 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1809 _Alloc_node __an(*
this);
1810 for (; __first != __last; ++__first)
1811 _M_insert_unique_(end(), *__first, __an);
1814 template<
typename _InputIterator>
1816 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1818 _Alloc_node __an(*
this);
1819 for (; __first != __last; ++__first)
1820 _M_insert_equal_(end(), *__first, __an);
1826 _M_erase_aux(const_iterator __position);
1829 _M_erase_aux(const_iterator __first, const_iterator __last);
1832#if __cplusplus >= 201103L
1835 _GLIBCXX_ABI_TAG_CXX11
1837 erase(const_iterator __position)
1839 __glibcxx_assert(__position != end());
1840 const_iterator __result = __position;
1842 _M_erase_aux(__position);
1843 return iterator(__result._M_node);
1847 _GLIBCXX_ABI_TAG_CXX11
1849 erase(iterator __position)
1851 __glibcxx_assert(__position != end());
1852 iterator __result = __position;
1854 _M_erase_aux(__position);
1859 erase(iterator __position)
1861 __glibcxx_assert(__position != end());
1862 _M_erase_aux(__position);
1866 erase(const_iterator __position)
1868 __glibcxx_assert(__position != end());
1869 _M_erase_aux(__position);
1874 erase(
const key_type& __x);
1876 template <
typename _Kt>
1878 _M_erase_tr(
const _Kt& __x);
1881 _M_erase_unique(
const key_type& __x);
1883#if __cplusplus >= 201103L
1886 _GLIBCXX_ABI_TAG_CXX11
1888 erase(const_iterator __first, const_iterator __last)
1890 _M_erase_aux(__first, __last);
1891 return iterator(__last._M_node);
1895 erase(iterator __first, iterator __last)
1896 { _M_erase_aux(__first, __last); }
1899 erase(const_iterator __first, const_iterator __last)
1900 { _M_erase_aux(__first, __last); }
1904 clear() _GLIBCXX_NOEXCEPT
1906 _M_erase(_M_begin_node());
1912 find(
const key_type& __k);
1915 find(
const key_type& __k)
const;
1918 count(
const key_type& __k)
const;
1921 lower_bound(
const key_type& __k)
1922 {
return iterator(_M_lower_bound(_M_begin(), _M_end(), __k)); }
1925 lower_bound(
const key_type& __k)
const
1927 return const_iterator
1928 (_M_lower_bound(_M_begin(), _M_end(), __k));
1932 upper_bound(
const key_type& __k)
1933 {
return iterator(_M_upper_bound(_M_begin(), _M_end(), __k)); }
1936 upper_bound(
const key_type& __k)
const
1938 return const_iterator
1939 (_M_upper_bound(_M_begin(), _M_end(), __k));
1943 equal_range(
const key_type& __k);
1946 equal_range(
const key_type& __k)
const;
1948#ifdef __glibcxx_generic_associative_lookup
1949 template<
typename _Kt,
1950 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1952 _M_find_tr(
const _Kt& __k)
1954 const _Rb_tree* __const_this =
this;
1955 return iterator(__const_this->_M_find_tr(__k)._M_node);
1958 template<
typename _Kt,
1959 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1961 _M_find_tr(
const _Kt& __k)
const
1963 const_iterator __j(_M_lower_bound_tr(__k));
1964 if (__j != end() && _M_key_compare(__k, _S_key(__j._M_node)))
1969 template<
typename _Kt,
1970 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1972 _M_count_tr(
const _Kt& __k)
const
1974 auto __p = _M_equal_range_tr(__k);
1978 template<
typename _Kt,
1979 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1981 _M_lower_bound_tr(
const _Kt& __k)
const
1983 auto __x = _M_begin();
1984 auto __y = _M_end();
1986 if (!_M_key_compare(_S_key(__x), __k))
1992 __x = _S_right(__x);
1996 template<
typename _Kt,
1997 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1999 _M_upper_bound_tr(
const _Kt& __k)
const
2001 auto __x = _M_begin();
2002 auto __y = _M_end();
2004 if (_M_key_compare(__k, _S_key(__x)))
2010 __x = _S_right(__x);
2014 template<
typename _Kt,
2015 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
2017 _M_equal_range_tr(
const _Kt& __k)
2019 const _Rb_tree* __const_this =
this;
2020 auto __ret = __const_this->_M_equal_range_tr(__k);
2022 { iterator(__ret.first._M_node), iterator(__ret.second._M_node) };
2025 template<
typename _Kt,
2026 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
2028 _M_equal_range_tr(
const _Kt& __k)
const
2030 auto __x = _M_begin();
2031 auto __y = _M_end();
2034 if (_M_key_compare(_S_key(__x), __k))
2035 __x = _S_right(__x);
2036 else if (_M_key_compare(__k, _S_key(__x)))
2047 __xu = _S_right(__xu);
2048 return { const_iterator(_M_lower_bound_tr(__x, __y, __k)),
2049 const_iterator(_M_upper_bound_tr(__xu, __yu, __k)) };
2052 return { const_iterator(__y), const_iterator(__y) };
2058 __rb_verify()
const;
2060#if __cplusplus >= 201103L
2062 operator=(_Rb_tree&&)
2063 noexcept(_Node_alloc_traits::_S_nothrow_move()
2064 && is_nothrow_move_assignable<_Compare>::value);
2066 template<typename _Iterator>
2068 _M_assign_unique(_Iterator, _Iterator);
2070 template<typename _Iterator>
2072 _M_assign_equal(_Iterator, _Iterator);
2078 { _M_impl._M_move_data(__x._M_impl); }
2095#ifdef __glibcxx_node_extract
2097 _S_adapt(
typename _Node_alloc_traits::pointer __ptr)
2099#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
2102#pragma GCC diagnostic push
2103#pragma GCC diagnostic ignored "-Wc++17-extensions"
2104 using __alloc_ptr =
typename _Node_alloc_traits::pointer;
2105 if constexpr (is_same<_Node_ptr, __alloc_ptr>::value)
2108 return std::__to_address(__ptr);
2109#pragma GCC diagnostic pop
2116 _M_reinsert_node_unique(node_type&& __nh)
2118 insert_return_type __ret;
2120 __ret.position = end();
2123 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2125 auto __res = _M_get_insert_unique_pos(__nh._M_key());
2129 = _M_insert_node(__res.first, __res.second,
2130 _S_adapt(__nh._M_ptr));
2132 __ret.inserted =
true;
2137 __ret.position = iterator(__res.first);
2138 __ret.inserted =
false;
2146 _M_reinsert_node_equal(node_type&& __nh)
2153 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2154 auto __res = _M_get_insert_equal_pos(__nh._M_key());
2156 __ret = _M_insert_node(__res.first, __res.second,
2157 _S_adapt(__nh._M_ptr));
2159 __ret = _M_insert_equal_lower_node(_S_adapt(__nh._M_ptr));
2167 _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
2174 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2175 auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
2178 __ret = _M_insert_node(__res.first, __res.second,
2179 _S_adapt(__nh._M_ptr));
2183 __ret = iterator(__res.first);
2190 _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
2197 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2198 auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
2200 __ret = _M_insert_node(__res.first, __res.second,
2201 _S_adapt(__nh._M_ptr));
2203 __ret = _M_insert_equal_lower_node(_S_adapt(__nh._M_ptr));
2211 extract(const_iterator __pos)
2213 auto __ptr = _Node_traits::_S_rebalance_for_erase
2214 (__pos._M_node, _M_impl._M_header);
2215 --_M_impl._M_node_count;
2216 auto __node_ptr =
static_cast<_Node&
>(*__ptr)._M_node_ptr();
2217#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
2218 return { __node_ptr, _M_get_Node_allocator() };
2220#pragma GCC diagnostic push
2221#pragma GCC diagnostic ignored "-Wc++17-extensions"
2222 using __alloc_ptr =
typename _Node_alloc_traits::pointer;
2223 if constexpr (is_same<_Node_ptr, __alloc_ptr>::value)
2224 return { __node_ptr, _M_get_Node_allocator() };
2227 auto __ap = pointer_traits<__alloc_ptr>::pointer_to(*__node_ptr);
2228 return { __ap, _M_get_Node_allocator() };
2230#pragma GCC diagnostic pop
2236 extract(
const key_type& __k)
2239 auto __pos = find(__k);
2241 __nh = extract(const_iterator(__pos));
2245 template <
typename _Kt>
2247 _M_extract_tr(
const _Kt& __k)
2250 auto __pos = _M_find_tr(__k);
2252 __nh = extract(const_iterator(__pos));
2256 template<
typename _Compare2>
2257 using _Compatible_tree
2258 = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
2260 template<
typename,
typename>
2261 friend struct _Rb_tree_merge_helper;
2264 template<
typename _Compare2>
2266 _M_merge_unique(_Compatible_tree<_Compare2>& __src)
noexcept
2268 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
2269 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
2272 auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
2275 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
2276 auto __ptr = _Node_traits::_S_rebalance_for_erase
2277 (__pos._M_node, __src_impl._M_header);
2278 --__src_impl._M_node_count;
2279 auto __node_ptr =
static_cast<_Node&
>(*__ptr)._M_node_ptr();
2280 _M_insert_node(__res.first, __res.second, __node_ptr);
2286 template<
typename _Compare2>
2288 _M_merge_equal(_Compatible_tree<_Compare2>& __src)
noexcept
2290 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
2291 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
2294 auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
2297 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
2298 auto __ptr = _Node_traits::_S_rebalance_for_erase
2299 (__pos._M_node, __src_impl._M_header);
2300 --__src_impl._M_node_count;
2301 auto __node_ptr =
static_cast<_Node&
>(*__ptr)._M_node_ptr();
2302 _M_insert_node(__res.first, __res.second, __node_ptr);
2309 operator==(
const _Rb_tree& __x,
const _Rb_tree& __y)
2311 return __x.size() == __y.size()
2312 && std::equal(__x.begin(), __x.end(), __y.begin());
2315#if __cpp_lib_three_way_comparison
2317 operator<=>(
const _Rb_tree& __x,
const _Rb_tree& __y)
2319 if constexpr (
requires {
typename __detail::__synth3way_t<_Val>; })
2321 __y.begin(), __y.end(),
2322 __detail::__synth3way);
2326 operator<(
const _Rb_tree& __x,
const _Rb_tree& __y)
2328 return std::lexicographical_compare(__x.begin(), __x.end(),
2329 __y.begin(), __y.end());
2334#if __cplusplus >= 201103L
2338 template<
typename... _Args>
2339 _Auto_node(_Rb_tree& __t, _Args&&... __args)
2341 _M_node(__t._M_create_node(std::
forward<_Args>(__args)...))
2347 _M_t._M_drop_node(_M_node);
2350 _Auto_node(_Auto_node&& __n)
2351 : _M_t(__n._M_t), _M_node(__n._M_node)
2352 { __n._M_node =
nullptr; }
2356 {
return _S_key(_M_node); }
2361 auto __it = _M_t._M_insert_node(__p.first, __p.second, _M_node);
2367 _M_insert_equal_lower()
2369 auto __it = _M_t._M_insert_equal_lower_node(_M_node);
2380 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2381 typename _Compare,
typename _Alloc>
2383 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
2384 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
2387#if __cplusplus >= 201103L
2388 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2389 typename _Compare,
typename _Alloc>
2391 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2394 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
2398 constexpr bool __move = !__move_if_noexcept_cond<value_type>::value;
2399 _Alloc_node __an(*
this);
2400 _M_root() = _M_copy<__move>(__x, __an);
2401#pragma GCC diagnostic push
2402#pragma GCC diagnostic ignored "-Wc++17-extensions"
2403 if constexpr (__move)
2405#pragma GCC diagnostic pop
2409 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2410 typename _Compare,
typename _Alloc>
2412 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2413 _M_move_assign(_Rb_tree& __x,
true_type)
2418 std::__alloc_on_move(_M_get_Node_allocator(),
2419 __x._M_get_Node_allocator());
2422 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2423 typename _Compare,
typename _Alloc>
2425 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2428 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
2429 return _M_move_assign(__x,
true_type{});
2433 _Reuse_or_alloc_node __roan(*
this);
2437 _M_root() = _M_copy<__as_rvalue>(__x, __roan);
2442 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2443 typename _Compare,
typename _Alloc>
2444 inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
2445 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2446 operator=(_Rb_tree&& __x)
2447 noexcept(_Node_alloc_traits::_S_nothrow_move()
2450 _M_impl._M_key_compare =
std::move(__x._M_impl._M_key_compare);
2452 __bool_constant<_Node_alloc_traits::_S_nothrow_move()>());
2456 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2457 typename _Compare,
typename _Alloc>
2458 template<
typename _Iterator>
2460 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2461 _M_assign_unique(_Iterator __first, _Iterator __last)
2463 _Reuse_or_alloc_node __roan(*
this);
2465 for (; __first != __last; ++__first)
2466 _M_insert_unique_(
end(), *__first, __roan);
2469 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2470 typename _Compare,
typename _Alloc>
2471 template<
typename _Iterator>
2473 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2474 _M_assign_equal(_Iterator __first, _Iterator __last)
2476 _Reuse_or_alloc_node __roan(*
this);
2478 for (; __first != __last; ++__first)
2479 _M_insert_equal_(
end(), *__first, __roan);
2483 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2484 typename _Compare,
typename _Alloc>
2485 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
2486 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2487 operator=(
const _Rb_tree& __x)
2492#if __cplusplus >= 201103L
2493 if (_Node_alloc_traits::_S_propagate_on_copy_assign())
2495 auto& __this_alloc = this->_M_get_Node_allocator();
2496 auto& __that_alloc = __x._M_get_Node_allocator();
2497 if (!_Node_alloc_traits::_S_always_equal()
2498 && __this_alloc != __that_alloc)
2503 std::__alloc_on_copy(__this_alloc, __that_alloc);
2508 _Reuse_or_alloc_node __roan(*
this);
2510 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
2512 _M_root() = _M_copy<__as_lvalue>(__x, __roan);
2518 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2519 typename _Compare,
typename _Alloc>
2520#if __cplusplus >= 201103L
2521 template<
typename _Arg,
typename _NodeGen>
2523 template<
typename _NodeGen>
2525 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2526 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2527 _M_insert_(_Base_ptr __x, _Base_ptr __p,
2528#
if __cplusplus >= 201103L
2533 _NodeGen& __node_gen)
2535 bool __insert_left = (__x || __p == _M_end()
2536 || _M_key_compare(_KeyOfValue()(__v),
2540 __node_gen(_GLIBCXX_FORWARD(_Arg, __v))->_M_base_ptr();
2542 _Node_traits::_S_insert_and_rebalance
2543 (__insert_left, __z, __p, this->_M_impl._M_header);
2544 ++_M_impl._M_node_count;
2548 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2549 typename _Compare,
typename _Alloc>
2550#if __cplusplus >= 201103L
2551 template<
typename _Arg>
2553 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2554 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2555#if __cplusplus >= 201103L
2556 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
2558 _M_insert_lower(_Base_ptr __p,
const _Val& __v)
2561 bool __insert_left = (__p == _M_end()
2562 || !_M_key_compare(_S_key(__p),
2563 _KeyOfValue()(__v)));
2566 _M_create_node(_GLIBCXX_FORWARD(_Arg, __v))->_M_base_ptr();
2567 _Node_traits::_S_insert_and_rebalance
2568 (__insert_left, __z, __p, this->_M_impl._M_header);
2569 ++_M_impl._M_node_count;
2573 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2574 typename _Compare,
typename _Alloc>
2575#if __cplusplus >= 201103L
2576 template<
typename _Arg>
2578 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2579 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2580#if __cplusplus >= 201103L
2581 _M_insert_equal_lower(_Arg&& __v)
2583 _M_insert_equal_lower(
const _Val& __v)
2586 _Base_ptr __x = _M_begin();
2587 _Base_ptr __y = _M_end();
2591 __x = !_M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
2592 _S_left(__x) : _S_right(__x);
2594 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
2597 template<
typename _Key,
typename _Val,
typename _KoV,
2598 typename _Compare,
typename _Alloc>
2599 template<
bool _MoveValues,
typename _NodeGen>
2600 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Base_ptr
2601 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
2602 _M_copy(_Node_ptr __x, _Base_ptr __p, _NodeGen& __node_gen)
2605 _Node_ptr __top = _M_clone_node<_MoveValues>(__x, __node_gen);
2606 _Base_ptr __top_base = __top->_M_base_ptr();
2607 __top->_M_parent = __p;
2613 _M_copy<_MoveValues>(_S_right(__x), __top_base, __node_gen);
2620 _M_clone_node<_MoveValues>(__x, __node_gen)->_M_base_ptr();
2622 __y->_M_parent = __p;
2624 __y->_M_right = _M_copy<_MoveValues>(_S_right(__x),
2633 __throw_exception_again;
2638 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2639 typename _Compare,
typename _Alloc>
2641 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2642 _M_erase(_Node_ptr __x)
2647 _M_erase(_S_right(__x));
2648 _Node_ptr __y = _S_left(__x);
2654 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2655 typename _Compare,
typename _Alloc>
2656 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2657 _Compare, _Alloc>::_Base_ptr
2658 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2659 _M_lower_bound(_Base_ptr __x, _Base_ptr __y,
2660 const _Key& __k)
const
2663 if (!_M_key_compare(_S_key(__x), __k))
2664 __y = __x, __x = _S_left(__x);
2666 __x = _S_right(__x);
2670 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2671 typename _Compare,
typename _Alloc>
2672 template <
typename _Kt>
2673 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr
2674 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2675 _M_lower_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const
2678 if (!_M_key_compare(_S_key(__x), __k))
2679 __y = __x, __x = _S_left(__x);
2681 __x = _S_right(__x);
2685 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2686 typename _Compare,
typename _Alloc>
2687 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2688 _Compare, _Alloc>::_Base_ptr
2689 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2690 _M_upper_bound(_Base_ptr __x, _Base_ptr __y,
2691 const _Key& __k)
const
2694 if (_M_key_compare(__k, _S_key(__x)))
2695 __y = __x, __x = _S_left(__x);
2697 __x = _S_right(__x);
2701 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2702 typename _Compare,
typename _Alloc>
2703 template <
typename _Kt>
2704 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr
2705 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2706 _M_upper_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const
2709 if (_M_key_compare(__k, _S_key(__x)))
2710 __y = __x, __x = _S_left(__x);
2712 __x = _S_right(__x);
2716 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2717 typename _Compare,
typename _Alloc>
2718 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2719 _Compare, _Alloc>::iterator,
2720 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2721 _Compare, _Alloc>::iterator>
2722 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2723 equal_range(
const _Key& __k)
2727 _Base_ptr __x = _M_begin();
2728 _Base_ptr __y = _M_end();
2731 if (_M_key_compare(_S_key(__x), __k))
2732 __x = _S_right(__x);
2733 else if (_M_key_compare(__k, _S_key(__x)))
2734 __y = __x, __x = _S_left(__x);
2737 _Base_ptr __xu(__x);
2738 _Base_ptr __yu(__y);
2739 __y = __x, __x = _S_left(__x);
2740 __xu = _S_right(__xu);
2741 return _Ret(
iterator(_M_lower_bound(__x, __y, __k)),
2742 iterator(_M_upper_bound(__xu, __yu, __k)));
2748 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2749 typename _Compare,
typename _Alloc>
2750 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2751 _Compare, _Alloc>::const_iterator,
2752 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2753 _Compare, _Alloc>::const_iterator>
2754 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2755 equal_range(
const _Key& __k)
const
2759 _Base_ptr __x = _M_begin();
2760 _Base_ptr __y = _M_end();
2763 if (_M_key_compare(_S_key(__x), __k))
2764 __x = _S_right(__x);
2765 else if (_M_key_compare(__k, _S_key(__x)))
2766 __y = __x, __x = _S_left(__x);
2769 _Base_ptr __xu(__x);
2770 _Base_ptr __yu(__y);
2771 __y = __x, __x = _S_left(__x);
2772 __xu = _S_right(__xu);
2773 return _Ret(const_iterator(_M_lower_bound(__x, __y, __k)),
2774 const_iterator(_M_upper_bound(__xu, __yu, __k)));
2777 return _Ret(const_iterator(__y), const_iterator(__y));
2780 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2781 typename _Compare,
typename _Alloc>
2783 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2785 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
2790 _M_impl._M_move_data(__t._M_impl);
2792 else if (!__t._M_root())
2793 __t._M_impl._M_move_data(_M_impl);
2796 std::swap(_M_root(),__t._M_root());
2797 std::swap(_M_leftmost(),__t._M_leftmost());
2798 std::swap(_M_rightmost(),__t._M_rightmost());
2800 _M_root()->_M_parent = _M_end();
2801 __t._M_root()->_M_parent = __t._M_end();
2802 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
2807 swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
2809 _Node_alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2810 __t._M_get_Node_allocator());
2813 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2814 typename _Compare,
typename _Alloc>
2815 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2816 _Compare, _Alloc>::_Base_ptr,
2817 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2818 _Compare, _Alloc>::_Base_ptr>
2819 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2820 _M_get_insert_unique_pos(
const key_type& __k)
2823 _Base_ptr __x = _M_begin();
2824 _Base_ptr __y = _M_end();
2829 __comp = _M_key_compare(__k, _S_key(__x));
2830 __x = __comp ? _S_left(__x) : _S_right(__x);
2836 return _Res(__x, __y);
2840 if (_M_key_compare(_S_key(__j._M_node), __k))
2841 return _Res(__x, __y);
2842 return _Res(__j._M_node, _Base_ptr());
2845 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2846 typename _Compare,
typename _Alloc>
2847 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2848 _Compare, _Alloc>::_Base_ptr,
2849 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2850 _Compare, _Alloc>::_Base_ptr>
2851 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2852 _M_get_insert_equal_pos(
const key_type& __k)
2855 _Base_ptr __x = _M_begin();
2856 _Base_ptr __y = _M_end();
2860 __x = _M_key_compare(__k, _S_key(__x)) ? _S_left(__x) : _S_right(__x);
2862 return _Res(__x, __y);
2865#ifdef __glibcxx_associative_heterogeneous_insertion
2870 template <
typename _Key,
typename _Val,
typename _KeyOfValue,
2871 typename _Compare,
typename _Alloc>
2872 template <
typename _Kt>
2874 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2875 _M_get_insert_unique_pos_tr(
const _Kt& __k)
2879 return { _M_end(), _M_end() };
2881 _Base_ptr __x = _M_begin(), __y = __x;
2882 bool __k_le_y =
false;
2886 __k_le_y = ! _M_key_compare(_S_key(__x), __k);
2887 __x = __k_le_y ? _S_left(__x) : _S_right(__x);
2900 if (__y == _M_rightmost())
2904 if (_M_key_compare(__k, _S_key(__j._M_node)))
2907 return { __y, __y };
2911 return { __j._M_node, {} };
2915 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2916 typename _Compare,
typename _Alloc>
2917#if __cplusplus >= 201103L
2918 template<
typename _Arg>
2920 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2921 _Compare, _Alloc>::iterator,
bool>
2922 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2923#if __cplusplus >= 201103L
2924 _M_insert_unique(_Arg&& __v)
2926 _M_insert_unique(
const _Val& __v)
2931 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
2935 _Alloc_node __an(*
this);
2936 return _Res(_M_insert_(__res.first, __res.second,
2937 _GLIBCXX_FORWARD(_Arg, __v), __an),
2941 return _Res(
iterator(__res.first),
false);
2944 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2945 typename _Compare,
typename _Alloc>
2946#if __cplusplus >= 201103L
2947 template<
typename _Arg>
2949 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2950 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2951#if __cplusplus >= 201103L
2952 _M_insert_equal(_Arg&& __v)
2954 _M_insert_equal(
const _Val& __v)
2958 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
2959 _Alloc_node __an(*
this);
2960 return _M_insert_(__res.first, __res.second,
2961 _GLIBCXX_FORWARD(_Arg, __v), __an);
2964 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2965 typename _Compare,
typename _Alloc>
2966 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2967 _Compare, _Alloc>::_Base_ptr,
2968 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2969 _Compare, _Alloc>::_Base_ptr>
2970 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2971 _M_get_insert_hint_unique_pos(const_iterator __position,
2972 const key_type& __k)
2977 if (__position._M_node == _M_end())
2979 if (
size() > 0 && _M_key_compare(_S_key(_M_rightmost()), __k))
2980 return _Res(_Base_ptr(), _M_rightmost());
2982 return _M_get_insert_unique_pos(__k);
2984 else if (_M_key_compare(__k, _S_key(__position._M_node)))
2987 iterator __before(__position._M_node);
2988 if (__position._M_node == _M_leftmost())
2989 return _Res(_M_leftmost(), _M_leftmost());
2990 else if (_M_key_compare(_S_key((--__before)._M_node), __k))
2992 if (!_S_right(__before._M_node))
2993 return _Res(_Base_ptr(), __before._M_node);
2995 return _Res(__position._M_node, __position._M_node);
2998 return _M_get_insert_unique_pos(__k);
3000 else if (_M_key_compare(_S_key(__position._M_node), __k))
3003 iterator __after(__position._M_node);
3004 if (__position._M_node == _M_rightmost())
3005 return _Res(_Base_ptr(), _M_rightmost());
3006 else if (_M_key_compare(__k, _S_key((++__after)._M_node)))
3008 if (!_S_right(__position._M_node))
3009 return _Res(_Base_ptr(), __position._M_node);
3011 return _Res(__after._M_node, __after._M_node);
3014 return _M_get_insert_unique_pos(__k);
3018 return _Res(__position._M_node, _Base_ptr());
3021#ifdef __glibcxx_associative_heterogeneous_insertion
3022 template <
typename _Key,
typename _Val,
typename _KeyOfValue,
3023 typename _Compare,
typename _Alloc>
3024 template <
typename _Kt>
3026 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3027 _M_get_insert_hint_unique_pos_tr(const_iterator __hint,
const _Kt& __k)
3030 auto __node =__hint._M_node;
3031 if (__node == _M_end())
3033 if (
size() > 0 && _M_key_compare(_S_key(_M_rightmost()), __k))
3034 return { {}, _M_rightmost() };
3035 return _M_get_insert_unique_pos_tr(__k);
3037 if (_M_key_compare(__k, _S_key(__node)))
3039 if (__node == _M_leftmost())
3040 return { _M_leftmost(), _M_leftmost() };
3043 if (_M_key_compare(_S_key(__before._M_node), __k))
3045 if (!_S_right(__before._M_node))
3046 return { {}, __before._M_node };
3047 return { __node, __node };
3049 return _M_get_insert_unique_pos_tr(__k);
3051 if (_M_key_compare(_S_key(__node), __k))
3053 if (__node == _M_rightmost())
3054 return { {}, _M_rightmost() };
3057 if (_M_key_compare(__k, _S_key(__after._M_node)))
3059 if (!_S_right(__node))
3060 return { {}, __node };
3061 return { __after._M_node, __after._M_node };
3063 return _M_get_insert_unique_pos_tr(__k);
3067 if (__node == _M_leftmost() ||
3068 _M_key_compare(_S_key((--__before)._M_node), __k))
3069 {
return { __node, {} }; }
3070 return _M_get_insert_unique_pos_tr(__k);
3074 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3075 typename _Compare,
typename _Alloc>
3076#if __cplusplus >= 201103L
3077 template<
typename _Arg,
typename _NodeGen>
3079 template<
typename _NodeGen>
3081 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
3082 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3083 _M_insert_unique_(const_iterator __position,
3084#
if __cplusplus >= 201103L
3089 _NodeGen& __node_gen)
3092 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
3095 return _M_insert_(__res.first, __res.second,
3096 _GLIBCXX_FORWARD(_Arg, __v),
3101 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3102 typename _Compare,
typename _Alloc>
3103 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
3104 _Compare, _Alloc>::_Base_ptr,
3105 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3106 _Compare, _Alloc>::_Base_ptr>
3107 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3108 _M_get_insert_hint_equal_pos(const_iterator __position,
const key_type& __k)
3113 if (__position._M_node == _M_end())
3116 && !_M_key_compare(__k, _S_key(_M_rightmost())))
3117 return _Res(_Base_ptr(), _M_rightmost());
3119 return _M_get_insert_equal_pos(__k);
3121 else if (!_M_key_compare(_S_key(__position._M_node), __k))
3124 iterator __before(__position._M_node);
3125 if (__position._M_node == _M_leftmost())
3126 return _Res(_M_leftmost(), _M_leftmost());
3127 else if (!_M_key_compare(__k, _S_key((--__before)._M_node)))
3129 if (!_S_right(__before._M_node))
3130 return _Res(_Base_ptr(), __before._M_node);
3132 return _Res(__position._M_node, __position._M_node);
3135 return _M_get_insert_equal_pos(__k);
3140 iterator __after(__position._M_node);
3141 if (__position._M_node == _M_rightmost())
3142 return _Res(_Base_ptr(), _M_rightmost());
3143 else if (!_M_key_compare(_S_key((++__after)._M_node), __k))
3145 if (!_S_right(__position._M_node))
3146 return _Res(_Base_ptr(), __position._M_node);
3148 return _Res(__after._M_node, __after._M_node);
3151 return _Res(_Base_ptr(), _Base_ptr());
3155 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3156 typename _Compare,
typename _Alloc>
3157#if __cplusplus >= 201103L
3158 template<
typename _Arg,
typename _NodeGen>
3160 template<
typename _NodeGen>
3162 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
3163 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3164 _M_insert_equal_(const_iterator __position,
3165#
if __cplusplus >= 201103L
3170 _NodeGen& __node_gen)
3173 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
3176 return _M_insert_(__res.first, __res.second,
3177 _GLIBCXX_FORWARD(_Arg, __v),
3180 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
3183#if __cplusplus >= 201103L
3184 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3185 typename _Compare,
typename _Alloc>
3187 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3188 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Node_ptr __z)
3191 bool __insert_left = (__x || __p == _M_end()
3192 || _M_key_compare(_S_key(__z), _S_key(__p)));
3194 _Base_ptr __base_z = __z->_M_base_ptr();
3195 _Node_traits::_S_insert_and_rebalance
3196 (__insert_left, __base_z, __p, this->_M_impl._M_header);
3197 ++_M_impl._M_node_count;
3201 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3202 typename _Compare,
typename _Alloc>
3204 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3205 _M_insert_lower_node(_Base_ptr __p, _Node_ptr __z)
3208 bool __insert_left = (__p == _M_end()
3209 || !_M_key_compare(_S_key(__p), _S_key(__z)));
3211 _Base_ptr __base_z = __z->_M_base_ptr();
3212 _Node_traits::_S_insert_and_rebalance
3213 (__insert_left, __base_z, __p, this->_M_impl._M_header);
3214 ++_M_impl._M_node_count;
3218 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3219 typename _Compare,
typename _Alloc>
3221 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3222 _M_insert_equal_lower_node(_Node_ptr __z)
3225 _Base_ptr __x = _M_begin();
3226 _Base_ptr __y = _M_end();
3230 __x = !_M_key_compare(_S_key(__x), _S_key(__z)) ?
3231 _S_left(__x) : _S_right(__x);
3233 return _M_insert_lower_node(__y, __z);
3236 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3237 typename _Compare,
typename _Alloc>
3238 template<
typename... _Args>
3240 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3241 _M_emplace_unique(_Args&&... __args)
3245 auto __res = _M_get_insert_unique_pos(__z._M_key());
3247 return {__z._M_insert(__res),
true};
3248 return {
iterator(__res.first),
false};
3251 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3252 typename _Compare,
typename _Alloc>
3253 template<
typename... _Args>
3255 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3256 _M_emplace_equal(_Args&&... __args)
3260 auto __res = _M_get_insert_equal_pos(__z._M_key());
3261 return __z._M_insert(__res);
3264 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3265 typename _Compare,
typename _Alloc>
3266 template<
typename... _Args>
3268 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3269 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
3273 auto __res = _M_get_insert_hint_unique_pos(__pos, __z._M_key());
3275 return __z._M_insert(__res);
3279 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3280 typename _Compare,
typename _Alloc>
3281 template<
typename... _Args>
3283 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3284 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
3288 auto __res = _M_get_insert_hint_equal_pos(__pos, __z._M_key());
3290 return __z._M_insert(__res);
3291 return __z._M_insert_equal_lower();
3294#ifdef __glibcxx_associative_heterogeneous_insertion
3295 template <
typename _Key,
typename _Val,
typename _KeyOfValue,
3296 typename _Compare,
typename _Alloc>
3297 template <
typename... _Args>
3299 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3300 _M_emplace_here(
bool __place_left, _Base_ptr __node, _Args&&... __args)
3304 _Base_ptr __base_z = __z._M_node->_M_base_ptr();
3305 _Node_traits::_S_insert_and_rebalance(
3306 __place_left, __base_z, __node, _M_impl._M_header);
3307 __z._M_node =
nullptr;
3308 ++_M_impl._M_node_count;
3316 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3317 typename _Compare,
typename _Alloc>
3319 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3320 _M_erase_aux(const_iterator __position)
3322 _Base_ptr __y = _Node_traits::_S_rebalance_for_erase
3323 (__position._M_node, this->_M_impl._M_header);
3324 _M_drop_node(
static_cast<_Node&
>(*__y)._M_node_ptr());
3325 --_M_impl._M_node_count;
3328 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3329 typename _Compare,
typename _Alloc>
3331 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3332 _M_erase_aux(const_iterator __first, const_iterator __last)
3334 if (__first ==
begin() && __last ==
end())
3337 while (__first != __last)
3338 _M_erase_aux(__first++);
3341 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3342 typename _Compare,
typename _Alloc>
3343 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3344 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3345 erase(
const _Key& __x)
3349 _M_erase_aux(__p.first, __p.second);
3350 return __old_size -
size();
3353 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3354 typename _Compare,
typename _Alloc>
3355 template <
typename _Kt>
3356 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3357 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3358 _M_erase_tr(
const _Kt& __x)
3362 _M_erase_aux(__p.first, __p.second);
3363 return __old_size -
size();
3366 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3367 typename _Compare,
typename _Alloc>
3368 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3369 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3370 _M_erase_unique(
const _Key& __x)
3380 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3381 typename _Compare,
typename _Alloc>
3382 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3383 _Compare, _Alloc>::iterator
3384 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3385 find(
const _Key& __k)
3387 iterator __j(_M_lower_bound(_M_begin(), _M_end(), __k));
3388 return (__j ==
end()
3389 || _M_key_compare(__k, _S_key(__j._M_node))) ?
end() : __j;
3392 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3393 typename _Compare,
typename _Alloc>
3394 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3395 _Compare, _Alloc>::const_iterator
3396 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3397 find(
const _Key& __k)
const
3399 const_iterator __j(_M_lower_bound(_M_begin(), _M_end(), __k));
3400 return (__j ==
end()
3401 || _M_key_compare(__k, _S_key(__j._M_node))) ?
end() : __j;
3404 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3405 typename _Compare,
typename _Alloc>
3406 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3407 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3408 count(
const _Key& __k)
const
3415 _GLIBCXX_PURE
unsigned int
3416 _Rb_tree_black_count(
const _Rb_tree_node_base* __node,
3417 const _Rb_tree_node_base* __root)
throw ();
3419 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3420 typename _Compare,
typename _Alloc>
3422 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify()
const
3424 if (_M_impl._M_node_count == 0 ||
begin() ==
end())
3425 return _M_impl._M_node_count == 0 &&
begin() ==
end()
3426 && this->_M_impl._M_header._M_left == _M_end()
3427 && this->_M_impl._M_header._M_right == _M_end();
3429 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
3430 for (const_iterator __it =
begin(); __it !=
end(); ++__it)
3432 _Base_ptr __x = __it._M_node;
3433 _Base_ptr __L = _S_left(__x);
3434 _Base_ptr __R = _S_right(__x);
3436 if (__x->_M_color == _S_red)
3437 if ((__L && __L->_M_color == _S_red)
3438 || (__R && __R->_M_color == _S_red))
3441 if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
3443 if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
3446 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
3450 if (_M_leftmost() != _Node_base::_S_minimum(_M_root()))
3452 if (_M_rightmost() != _Node_base::_S_maximum(_M_root()))
3457#ifdef __glibcxx_node_extract
3459 template<
typename _Key,
typename _Val,
typename _Sel,
typename _Cmp1,
3460 typename _Alloc,
typename _Cmp2>
3461 struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
3465 friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
3468 _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
3469 {
return __tree._M_impl; }
3473#ifdef __glibcxx_associative_heterogeneous_erasure
3474template <
typename _Kt,
typename _Container>
3475 concept __heterogeneous_tree_key =
3476 __transparent_comparator<typename _Container::key_compare> &&
3477 __heterogeneous_key<_Kt, _Container>;
3480_GLIBCXX_END_NAMESPACE_VERSION
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
constexpr auto lexicographical_compare_three_way(_InputIter1 __first1, _InputIter1 __last1, _InputIter2 __first2, _InputIter2 __last2, _Comp __comp) -> decltype(__comp(*__first1, *__first2))
Performs dictionary comparison on ranges.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
typename pointer_traits< _Ptr >::template rebind< _Tp > __ptr_rebind
Convenience alias for rebinding pointers.
constexpr auto end(_Container &__cont) noexcept(noexcept(__cont.end())) -> decltype(__cont.end())
Return an iterator pointing to one past the last element of the container.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto begin(_Container &__cont) noexcept(noexcept(__cont.begin())) -> decltype(__cont.begin())
Return an iterator pointing to the first element of the container.
constexpr _Iterator __base(_Iterator __it)
is_nothrow_move_assignable
The standard allocator, as per C++03 [20.4.1].
Struct holding two objects (or references) of arbitrary type.
static constexpr pointer allocate(_Node_allocator &__a, size_type __n)
static constexpr void deallocate(_Node_allocator &__a, pointer __p, size_type __n)
static constexpr size_type max_size(const _Node_allocator &__a) noexcept