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#if __cplusplus >= 201103L
1061 _Reuse_or_alloc_node(
const _Reuse_or_alloc_node&) =
delete;
1064 ~_Reuse_or_alloc_node()
1067 _M_t._M_erase(
static_cast<_Node&
>(*_M_root)._M_node_ptr());
1070 template<
typename _Arg>
1072 operator()(_GLIBCXX_FWDREF(_Arg) __arg)
1074 _Base_ptr
__base = _M_extract();
1077 _Node_ptr __node =
static_cast<_Node&
>(*__base)._M_node_ptr();
1078 _M_t._M_destroy_node(__node);
1079 _M_t._M_construct_node(__node, _GLIBCXX_FORWARD(_Arg, __arg));
1083 return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg));
1093 _Base_ptr __node = _M_nodes;
1094 _M_nodes = _M_nodes->_M_parent;
1097 if (_M_nodes->_M_right == __node)
1099 _M_nodes->_M_right = _Base_ptr();
1101 if (_M_nodes->_M_left)
1103 _M_nodes = _M_nodes->_M_left;
1105 while (_M_nodes->_M_right)
1106 _M_nodes = _M_nodes->_M_right;
1108 if (_M_nodes->_M_left)
1109 _M_nodes = _M_nodes->_M_left;
1113 _M_nodes->_M_left = _Base_ptr();
1116 _M_root = _Base_ptr();
1130 _Alloc_node(_Rb_tree& __t)
1133 template<
typename _Arg>
1135 operator()(_GLIBCXX_FWDREF(_Arg) __arg)
const
1136 {
return _M_t._M_create_node(_GLIBCXX_FORWARD(_Arg, __arg)); }
1143 typedef _Key key_type;
1144 typedef _Val value_type;
1145 typedef value_type* pointer;
1146 typedef const value_type* const_pointer;
1147 typedef value_type& reference;
1148 typedef const value_type& const_reference;
1149 typedef size_t size_type;
1150 typedef ptrdiff_t difference_type;
1151 typedef _Alloc allocator_type;
1154 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
1155 {
return this->_M_impl; }
1157 const _Node_allocator&
1158 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
1159 {
return this->_M_impl; }
1162 get_allocator() const _GLIBCXX_NOEXCEPT
1163 {
return allocator_type(_M_get_Node_allocator()); }
1169#if __cplusplus < 201102L || _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
1172#pragma GCC diagnostic push
1173#pragma GCC diagnostic ignored "-Wc++17-extensions"
1174 using __alloc_pointer =
typename _Node_alloc_traits::pointer;
1175 if constexpr (is_same<_Node_ptr, __alloc_pointer>::value)
1181 return std::__to_address(__ptr);
1183#pragma GCC diagnostic pop
1188 _M_put_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1190#if __cplusplus < 201102L || _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
1193#pragma GCC diagnostic push
1194#pragma GCC diagnostic ignored "-Wc++17-extensions"
1195 using __alloc_pointer =
typename _Node_alloc_traits::pointer;
1196 if constexpr (is_same<_Node_ptr, __alloc_pointer>::value)
1202 auto __ap = pointer_traits<__alloc_pointer>::pointer_to(*__p);
1205#pragma GCC diagnostic pop
1209#if __cplusplus < 201103L
1211 _M_construct_node(_Node_ptr __node,
const value_type& __x)
1214 { get_allocator().construct(__node->_M_valptr(), __x); }
1217 _M_put_node(__node);
1218 __throw_exception_again;
1223 _M_create_node(
const value_type& __x)
1225 _Node_ptr __tmp = _M_get_node();
1226 _M_construct_node(__tmp, __x);
1230 template<
typename... _Args>
1232 _M_construct_node(_Node_ptr __node, _Args&&... __args)
1237 _Node_alloc_traits::construct(_M_get_Node_allocator(),
1238 __node->_M_valptr(),
1244 _M_put_node(__node);
1245 __throw_exception_again;
1249 template<
typename... _Args>
1251 _M_create_node(_Args&&... __args)
1253 _Node_ptr __tmp = _M_get_node();
1260 _M_destroy_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1262#if __cplusplus < 201103L
1263 get_allocator().destroy(__p->_M_valptr());
1265 _Node_alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
1271 _M_drop_node(_Node_ptr __p) _GLIBCXX_NOEXCEPT
1273 _M_destroy_node(__p);
1277 template<
bool _MoveValue,
typename _NodeGen>
1279 _M_clone_node(_Node_ptr __x, _NodeGen& __node_gen)
1281#if __cplusplus >= 201103L
1282 using _Vp = __conditional_t<_MoveValue,
1287 = __node_gen(_GLIBCXX_FORWARD(_Vp, *__x->_M_valptr()));
1288 __tmp->_M_color = __x->_M_color;
1289 __tmp->_M_left = __tmp->_M_right = _Base_ptr();
1294 typedef typename _Node_traits::_Header_t _Header_t;
1296#if _GLIBCXX_INLINE_VERSION
1297 template<
typename _Key_compare>
1300 template<
typename _Key_compare,
1301 bool = __is_pod(_Key_compare)>
1303 struct _Rb_tree_impl
1304 :
public _Node_allocator
1305 ,
public _Rb_tree_key_compare<_Key_compare>
1308 typedef _Rb_tree_key_compare<_Key_compare> _Base_key_compare;
1311 _GLIBCXX_NOEXCEPT_IF(
1312 is_nothrow_default_constructible<_Node_allocator>::value
1313 && is_nothrow_default_constructible<_Base_key_compare>::value )
1317 _Rb_tree_impl(
const _Rb_tree_impl& __x)
1318 : _Node_allocator(_Node_alloc_traits::_S_select_on_copy(__x))
1319 , _Base_key_compare(__x._M_key_compare)
1323#if __cplusplus < 201103L
1324 _Rb_tree_impl(
const _Key_compare& __comp,
const _Node_allocator& __a)
1325 : _Node_allocator(__a), _Base_key_compare(__comp)
1328 _Rb_tree_impl(_Rb_tree_impl&&)
1329 noexcept( is_nothrow_move_constructible<_Base_key_compare>::value )
1333 _Rb_tree_impl(_Node_allocator&& __a)
1334 : _Node_allocator(std::
move(__a))
1337 _Rb_tree_impl(_Rb_tree_impl&& __x, _Node_allocator&& __a)
1338 : _Node_allocator(std::
move(__a)),
1339 _Base_key_compare(std::
move(__x)),
1340 _Header_t(std::
move(__x))
1343 _Rb_tree_impl(
const _Key_compare& __comp, _Node_allocator&& __a)
1344 : _Node_allocator(std::
move(__a)), _Base_key_compare(__comp)
1349 _Rb_tree_impl<_Compare> _M_impl;
1353 _M_root() _GLIBCXX_NOEXCEPT
1354 {
return this->_M_impl._M_header._M_parent; }
1357 _M_root() const _GLIBCXX_NOEXCEPT
1358 {
return this->_M_impl._M_header._M_parent; }
1361 _M_leftmost() _GLIBCXX_NOEXCEPT
1362 {
return this->_M_impl._M_header._M_left; }
1365 _M_leftmost() const _GLIBCXX_NOEXCEPT
1366 {
return this->_M_impl._M_header._M_left; }
1369 _M_rightmost() _GLIBCXX_NOEXCEPT
1370 {
return this->_M_impl._M_header._M_right; }
1373 _M_rightmost() const _GLIBCXX_NOEXCEPT
1374 {
return this->_M_impl._M_header._M_right; }
1377 _M_begin() const _GLIBCXX_NOEXCEPT
1378 {
return this->_M_impl._M_header._M_parent; }
1381 _M_begin_node() const _GLIBCXX_NOEXCEPT
1383 _Base_ptr __begin = this->_M_impl._M_header._M_parent;
1385 ?
static_cast<_Node&
>(*__begin)._M_node_ptr()
1390 _M_end() const _GLIBCXX_NOEXCEPT
1391 {
return this->_M_impl._M_header._M_base_ptr(); }
1395 template<
typename _Key1,
typename _Key2>
1397 _M_key_compare(
const _Key1& __k1,
const _Key2& __k2)
const
1399#if __cplusplus >= 201103L
1402 __is_invocable<const _Compare&, const _Key&, const _Key&>::value,
1403 "comparison object must be invocable with arguments of key_type"
1406 return _M_impl._M_key_compare(__k1, __k2);
1410 _S_key(
const _Node& __node)
1411 {
return _KeyOfValue()(*__node._M_valptr()); }
1414 _S_key(_Base_ptr __x)
1415 {
return _S_key(
static_cast<const _Node&
>(*__x)); }
1418 _S_key(_Node_ptr __x)
1419 {
return _S_key(*__x); }
1422 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
1423 {
return __x->_M_left; }
1426 _S_left(_Node_ptr __x)
1429 ?
static_cast<_Node&
>(*__x->_M_left)._M_node_ptr()
1434 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
1435 {
return __x->_M_right; }
1438 _S_right(_Node_ptr __x) _GLIBCXX_NOEXCEPT
1440 return __x->_M_right
1441 ?
static_cast<_Node&
>(*__x->_M_right)._M_node_ptr()
1446 typedef typename _Node_traits::_Iterator iterator;
1447 typedef typename _Node_traits::_Const_iterator const_iterator;
1449 typedef std::reverse_iterator<iterator> reverse_iterator;
1450 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
1452#ifdef __glibcxx_node_extract
1453 using node_type = _Node_handle<_Key, _Val, _Node_allocator>;
1454 using insert_return_type = _Node_insert_return<
1455 __conditional_t<is_same_v<_Key, _Val>, const_iterator, iterator>,
1460 _M_get_insert_unique_pos(
const key_type& __k);
1463 _M_get_insert_equal_pos(
const key_type& __k);
1466 _M_get_insert_hint_unique_pos(const_iterator __pos,
1467 const key_type& __k);
1470 _M_get_insert_hint_equal_pos(const_iterator __pos,
1471 const key_type& __k);
1473#ifdef __glibcxx_associative_heterogeneous_insertion
1474 template <
typename... _Args>
1476 _M_emplace_here(
bool __place_left, _Base_ptr __node, _Args&&... __args);
1478 template <
typename _Kt>
1480 _M_get_insert_unique_pos_tr(
const _Kt& __k);
1482 template <
typename _Kt>
1484 _M_get_insert_hint_unique_pos_tr(const_iterator,
const _Kt& __k);
1488#if __cplusplus >= 201103L
1489 template<
typename _Arg,
typename _NodeGen>
1491 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v, _NodeGen&);
1494 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Node_ptr __z);
1496 template<
typename _Arg>
1498 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
1500 template<
typename _Arg>
1502 _M_insert_equal_lower(_Arg&& __x);
1505 _M_insert_lower_node(_Base_ptr __p, _Node_ptr __z);
1508 _M_insert_equal_lower_node(_Node_ptr __z);
1510 template<
typename _NodeGen>
1512 _M_insert_(_Base_ptr __x, _Base_ptr __y,
1513 const value_type& __v, _NodeGen&);
1518 _M_insert_lower(_Base_ptr __y,
const value_type& __v);
1521 _M_insert_equal_lower(
const value_type& __x);
1524 enum { __as_lvalue, __as_rvalue };
1526 template<
bool _MoveValues,
typename _NodeGen>
1528 _M_copy(_Node_ptr, _Base_ptr, _NodeGen&);
1530 template<
bool _MoveValues,
typename _NodeGen>
1532 _M_copy(
const _Rb_tree& __x, _NodeGen& __gen)
1535 _M_copy<_MoveValues>(__x._M_begin_node(), _M_end(), __gen);
1536 _M_leftmost() = _Node_base::_S_minimum(__root);
1537 _M_rightmost() = _Node_base::_S_maximum(__root);
1538 _M_impl._M_node_count = __x._M_impl._M_node_count;
1543 _M_copy(
const _Rb_tree& __x)
1545 _Alloc_node __an(*
this);
1546 return _M_copy<__as_lvalue>(__x, __an);
1550 _M_erase(_Node_ptr __x);
1553 _M_lower_bound(_Base_ptr __x, _Base_ptr __y,
1554 const _Key& __k)
const;
1556 template <
typename _Kt>
1558 _M_lower_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const;
1561 _M_upper_bound(_Base_ptr __x, _Base_ptr __y,
1562 const _Key& __k)
const;
1564 template <
typename _Kt>
1566 _M_upper_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const;
1570#if __cplusplus < 201103L
1573 _Rb_tree() =
default;
1576 _Rb_tree(
const _Compare& __comp,
1577 const allocator_type& __a = allocator_type())
1578 : _M_impl(__comp, _Node_allocator(__a)) { }
1580 _Rb_tree(
const _Rb_tree& __x)
1581 : _M_impl(__x._M_impl)
1584 _M_root() = _M_copy(__x);
1587#if __cplusplus >= 201103L
1588 _Rb_tree(
const allocator_type& __a)
1589 : _M_impl(_Node_allocator(__a))
1592 _Rb_tree(
const _Rb_tree& __x,
const allocator_type& __a)
1593 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
1596 _M_root() = _M_copy(__x);
1599 _Rb_tree(_Rb_tree&&) =
default;
1601 _Rb_tree(_Rb_tree&& __x,
const allocator_type& __a)
1602 : _Rb_tree(std::
move(__x), _Node_allocator(__a))
1606 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a,
true_type)
1607 noexcept(is_nothrow_default_constructible<_Compare>::value)
1608 : _M_impl(std::
move(__x._M_impl), std::
move(__a))
1611 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a,
false_type)
1612 : _M_impl(__x._M_impl._M_key_compare, std::
move(__a))
1619 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
1623 : _Rb_tree(std::
move(__x), std::
move(__a),
1624 typename _Node_alloc_traits::is_always_equal{})
1628 ~_Rb_tree() _GLIBCXX_NOEXCEPT
1629 { _M_erase(_M_begin_node()); }
1632 operator=(
const _Rb_tree& __x);
1637 {
return _M_impl._M_key_compare; }
1640 begin() _GLIBCXX_NOEXCEPT
1641 {
return iterator(this->_M_impl._M_header._M_left); }
1644 begin() const _GLIBCXX_NOEXCEPT
1645 {
return const_iterator(this->_M_impl._M_header._M_left); }
1648 end() _GLIBCXX_NOEXCEPT
1649 {
return iterator(_M_end()); }
1652 end() const _GLIBCXX_NOEXCEPT
1653 {
return const_iterator(_M_end()); }
1656 rbegin() _GLIBCXX_NOEXCEPT
1657 {
return reverse_iterator(end()); }
1659 const_reverse_iterator
1660 rbegin() const _GLIBCXX_NOEXCEPT
1661 {
return const_reverse_iterator(end()); }
1664 rend() _GLIBCXX_NOEXCEPT
1665 {
return reverse_iterator(begin()); }
1667 const_reverse_iterator
1668 rend() const _GLIBCXX_NOEXCEPT
1669 {
return const_reverse_iterator(begin()); }
1671 _GLIBCXX_NODISCARD
bool
1672 empty() const _GLIBCXX_NOEXCEPT
1673 {
return _M_impl._M_node_count == 0; }
1676 size() const _GLIBCXX_NOEXCEPT
1677 {
return _M_impl._M_node_count; }
1680 max_size() const _GLIBCXX_NOEXCEPT
1685 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value);
1688#if __cplusplus >= 201103L
1689 template<
typename _Arg>
1691 _M_insert_unique(_Arg&& __x);
1693 template<
typename _Arg>
1695 _M_insert_equal(_Arg&& __x);
1697 template<
typename _Arg,
typename _NodeGen>
1699 _M_insert_unique_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1701 template<
typename _Arg>
1703 _M_insert_unique_(const_iterator __pos, _Arg&& __x)
1705 _Alloc_node __an(*
this);
1709 template<
typename _Arg,
typename _NodeGen>
1711 _M_insert_equal_(const_iterator __pos, _Arg&& __x, _NodeGen&);
1713 template<
typename _Arg>
1715 _M_insert_equal_(const_iterator __pos, _Arg&& __x)
1717 _Alloc_node __an(*
this);
1721 template<
typename... _Args>
1723 _M_emplace_unique(_Args&&... __args);
1725 template<
typename... _Args>
1727 _M_emplace_equal(_Args&&... __args);
1729 template<
typename... _Args>
1731 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
1733 template<
typename... _Args>
1735 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
1737 template<
typename _Iter>
1738 using __same_value_type
1739 = is_same<value_type, typename iterator_traits<_Iter>::value_type>;
1741 template<
typename _InputIterator>
1742 __enable_if_t<__same_value_type<_InputIterator>::value>
1743 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1745 _Alloc_node __an(*
this);
1746 for (; __first != __last; ++__first)
1747 _M_insert_unique_(end(), *__first, __an);
1750 template<
typename _InputIterator>
1751 __enable_if_t<!__same_value_type<_InputIterator>::value>
1752 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1754 for (; __first != __last; ++__first)
1755 _M_emplace_unique(*__first);
1758 template<
typename _InputIterator>
1759 __enable_if_t<__same_value_type<_InputIterator>::value>
1760 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1762 _Alloc_node __an(*
this);
1763 for (; __first != __last; ++__first)
1764 _M_insert_equal_(end(), *__first, __an);
1767 template<
typename _InputIterator>
1768 __enable_if_t<!__same_value_type<_InputIterator>::value>
1769 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1771 for (; __first != __last; ++__first)
1772 _M_emplace_equal(*__first);
1776 _M_insert_unique(
const value_type& __x);
1779 _M_insert_equal(
const value_type& __x);
1781 template<
typename _NodeGen>
1783 _M_insert_unique_(const_iterator __pos,
const value_type& __x,
1787 _M_insert_unique_(const_iterator __pos,
const value_type& __x)
1789 _Alloc_node __an(*
this);
1790 return _M_insert_unique_(__pos, __x, __an);
1793 template<
typename _NodeGen>
1795 _M_insert_equal_(const_iterator __pos,
const value_type& __x,
1798 _M_insert_equal_(const_iterator __pos,
const value_type& __x)
1800 _Alloc_node __an(*
this);
1801 return _M_insert_equal_(__pos, __x, __an);
1804 template<
typename _InputIterator>
1806 _M_insert_range_unique(_InputIterator __first, _InputIterator __last)
1808 _Alloc_node __an(*
this);
1809 for (; __first != __last; ++__first)
1810 _M_insert_unique_(end(), *__first, __an);
1813 template<
typename _InputIterator>
1815 _M_insert_range_equal(_InputIterator __first, _InputIterator __last)
1817 _Alloc_node __an(*
this);
1818 for (; __first != __last; ++__first)
1819 _M_insert_equal_(end(), *__first, __an);
1825 _M_erase_aux(const_iterator __position);
1828 _M_erase_aux(const_iterator __first, const_iterator __last);
1831#if __cplusplus >= 201103L
1834 _GLIBCXX_ABI_TAG_CXX11
1836 erase(const_iterator __position)
1838 __glibcxx_assert(__position != end());
1839 const_iterator __result = __position;
1841 _M_erase_aux(__position);
1842 return iterator(__result._M_node);
1846 _GLIBCXX_ABI_TAG_CXX11
1848 erase(iterator __position)
1850 __glibcxx_assert(__position != end());
1851 iterator __result = __position;
1853 _M_erase_aux(__position);
1858 erase(iterator __position)
1860 __glibcxx_assert(__position != end());
1861 _M_erase_aux(__position);
1865 erase(const_iterator __position)
1867 __glibcxx_assert(__position != end());
1868 _M_erase_aux(__position);
1873 erase(
const key_type& __x);
1875 template <
typename _Kt>
1877 _M_erase_tr(
const _Kt& __x);
1880 _M_erase_unique(
const key_type& __x);
1882#if __cplusplus >= 201103L
1885 _GLIBCXX_ABI_TAG_CXX11
1887 erase(const_iterator __first, const_iterator __last)
1889 _M_erase_aux(__first, __last);
1890 return iterator(__last._M_node);
1894 erase(iterator __first, iterator __last)
1895 { _M_erase_aux(__first, __last); }
1898 erase(const_iterator __first, const_iterator __last)
1899 { _M_erase_aux(__first, __last); }
1903 clear() _GLIBCXX_NOEXCEPT
1905 _M_erase(_M_begin_node());
1911 find(
const key_type& __k);
1914 find(
const key_type& __k)
const;
1917 count(
const key_type& __k)
const;
1920 lower_bound(
const key_type& __k)
1921 {
return iterator(_M_lower_bound(_M_begin(), _M_end(), __k)); }
1924 lower_bound(
const key_type& __k)
const
1926 return const_iterator
1927 (_M_lower_bound(_M_begin(), _M_end(), __k));
1931 upper_bound(
const key_type& __k)
1932 {
return iterator(_M_upper_bound(_M_begin(), _M_end(), __k)); }
1935 upper_bound(
const key_type& __k)
const
1937 return const_iterator
1938 (_M_upper_bound(_M_begin(), _M_end(), __k));
1942 equal_range(
const key_type& __k);
1945 equal_range(
const key_type& __k)
const;
1947#ifdef __glibcxx_generic_associative_lookup
1948 template<
typename _Kt,
1949 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1951 _M_find_tr(
const _Kt& __k)
1953 const _Rb_tree* __const_this =
this;
1954 return iterator(__const_this->_M_find_tr(__k)._M_node);
1957 template<
typename _Kt,
1958 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1960 _M_find_tr(
const _Kt& __k)
const
1962 const_iterator __j(_M_lower_bound_tr(__k));
1963 if (__j != end() && _M_key_compare(__k, _S_key(__j._M_node)))
1968 template<
typename _Kt,
1969 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1971 _M_count_tr(
const _Kt& __k)
const
1973 auto __p = _M_equal_range_tr(__k);
1977 template<
typename _Kt,
1978 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1980 _M_lower_bound_tr(
const _Kt& __k)
const
1982 auto __x = _M_begin();
1983 auto __y = _M_end();
1985 if (!_M_key_compare(_S_key(__x), __k))
1991 __x = _S_right(__x);
1995 template<
typename _Kt,
1996 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
1998 _M_upper_bound_tr(
const _Kt& __k)
const
2000 auto __x = _M_begin();
2001 auto __y = _M_end();
2003 if (_M_key_compare(__k, _S_key(__x)))
2009 __x = _S_right(__x);
2013 template<
typename _Kt,
2014 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
2016 _M_equal_range_tr(
const _Kt& __k)
2018 const _Rb_tree* __const_this =
this;
2019 auto __ret = __const_this->_M_equal_range_tr(__k);
2021 { iterator(__ret.first._M_node), iterator(__ret.second._M_node) };
2024 template<
typename _Kt,
2025 typename _Req = __has_is_transparent_t<_Compare, _Kt>>
2027 _M_equal_range_tr(
const _Kt& __k)
const
2029 auto __x = _M_begin();
2030 auto __y = _M_end();
2033 if (_M_key_compare(_S_key(__x), __k))
2034 __x = _S_right(__x);
2035 else if (_M_key_compare(__k, _S_key(__x)))
2046 __xu = _S_right(__xu);
2047 return { const_iterator(_M_lower_bound_tr(__x, __y, __k)),
2048 const_iterator(_M_upper_bound_tr(__xu, __yu, __k)) };
2051 return { const_iterator(__y), const_iterator(__y) };
2057 __rb_verify()
const;
2059#if __cplusplus >= 201103L
2061 operator=(_Rb_tree&&)
2062 noexcept(_Node_alloc_traits::_S_nothrow_move()
2063 && is_nothrow_move_assignable<_Compare>::value);
2065 template<typename _Iterator>
2067 _M_assign_unique(_Iterator, _Iterator);
2069 template<typename _Iterator>
2071 _M_assign_equal(_Iterator, _Iterator);
2077 { _M_impl._M_move_data(__x._M_impl); }
2094#ifdef __glibcxx_node_extract
2096 _S_adapt(
typename _Node_alloc_traits::pointer __ptr)
2098#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
2101#pragma GCC diagnostic push
2102#pragma GCC diagnostic ignored "-Wc++17-extensions"
2103 using __alloc_ptr =
typename _Node_alloc_traits::pointer;
2104 if constexpr (is_same<_Node_ptr, __alloc_ptr>::value)
2107 return std::__to_address(__ptr);
2108#pragma GCC diagnostic pop
2115 _M_reinsert_node_unique(node_type&& __nh)
2117 insert_return_type __ret;
2119 __ret.position = end();
2122 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2124 auto __res = _M_get_insert_unique_pos(__nh._M_key());
2128 = _M_insert_node(__res.first, __res.second,
2129 _S_adapt(__nh._M_ptr));
2131 __ret.inserted =
true;
2136 __ret.position = iterator(__res.first);
2137 __ret.inserted =
false;
2145 _M_reinsert_node_equal(node_type&& __nh)
2152 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2153 auto __res = _M_get_insert_equal_pos(__nh._M_key());
2155 __ret = _M_insert_node(__res.first, __res.second,
2156 _S_adapt(__nh._M_ptr));
2158 __ret = _M_insert_equal_lower_node(_S_adapt(__nh._M_ptr));
2166 _M_reinsert_node_hint_unique(const_iterator __hint, node_type&& __nh)
2173 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2174 auto __res = _M_get_insert_hint_unique_pos(__hint, __nh._M_key());
2177 __ret = _M_insert_node(__res.first, __res.second,
2178 _S_adapt(__nh._M_ptr));
2182 __ret = iterator(__res.first);
2189 _M_reinsert_node_hint_equal(const_iterator __hint, node_type&& __nh)
2196 __glibcxx_assert(_M_get_Node_allocator() == *__nh._M_alloc);
2197 auto __res = _M_get_insert_hint_equal_pos(__hint, __nh._M_key());
2199 __ret = _M_insert_node(__res.first, __res.second,
2200 _S_adapt(__nh._M_ptr));
2202 __ret = _M_insert_equal_lower_node(_S_adapt(__nh._M_ptr));
2210 extract(const_iterator __pos)
2212 auto __ptr = _Node_traits::_S_rebalance_for_erase
2213 (__pos._M_node, _M_impl._M_header);
2214 --_M_impl._M_node_count;
2215 auto __node_ptr =
static_cast<_Node&
>(*__ptr)._M_node_ptr();
2216#if _GLIBCXX_USE_ALLOC_PTR_FOR_RB_TREE
2217 return { __node_ptr, _M_get_Node_allocator() };
2219#pragma GCC diagnostic push
2220#pragma GCC diagnostic ignored "-Wc++17-extensions"
2221 using __alloc_ptr =
typename _Node_alloc_traits::pointer;
2222 if constexpr (is_same<_Node_ptr, __alloc_ptr>::value)
2223 return { __node_ptr, _M_get_Node_allocator() };
2226 auto __ap = pointer_traits<__alloc_ptr>::pointer_to(*__node_ptr);
2227 return { __ap, _M_get_Node_allocator() };
2229#pragma GCC diagnostic pop
2235 extract(
const key_type& __k)
2238 auto __pos = find(__k);
2240 __nh = extract(const_iterator(__pos));
2244 template <
typename _Kt>
2246 _M_extract_tr(
const _Kt& __k)
2249 auto __pos = _M_find_tr(__k);
2251 __nh = extract(const_iterator(__pos));
2255 template<
typename _Compare2>
2256 using _Compatible_tree
2257 = _Rb_tree<_Key, _Val, _KeyOfValue, _Compare2, _Alloc>;
2259 template<
typename,
typename>
2260 friend struct _Rb_tree_merge_helper;
2263 template<
typename _Compare2>
2265 _M_merge_unique(_Compatible_tree<_Compare2>& __src)
noexcept
2267 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
2268 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
2271 auto __res = _M_get_insert_unique_pos(_KeyOfValue()(*__pos));
2274 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
2275 auto __ptr = _Node_traits::_S_rebalance_for_erase
2276 (__pos._M_node, __src_impl._M_header);
2277 --__src_impl._M_node_count;
2278 auto __node_ptr =
static_cast<_Node&
>(*__ptr)._M_node_ptr();
2279 _M_insert_node(__res.first, __res.second, __node_ptr);
2285 template<
typename _Compare2>
2287 _M_merge_equal(_Compatible_tree<_Compare2>& __src)
noexcept
2289 using _Merge_helper = _Rb_tree_merge_helper<_Rb_tree, _Compare2>;
2290 for (
auto __i = __src.begin(), __end = __src.end(); __i != __end;)
2293 auto __res = _M_get_insert_equal_pos(_KeyOfValue()(*__pos));
2296 auto& __src_impl = _Merge_helper::_S_get_impl(__src);
2297 auto __ptr = _Node_traits::_S_rebalance_for_erase
2298 (__pos._M_node, __src_impl._M_header);
2299 --__src_impl._M_node_count;
2300 auto __node_ptr =
static_cast<_Node&
>(*__ptr)._M_node_ptr();
2301 _M_insert_node(__res.first, __res.second, __node_ptr);
2308 operator==(
const _Rb_tree& __x,
const _Rb_tree& __y)
2310 return __x.size() == __y.size()
2311 && std::equal(__x.begin(), __x.end(), __y.begin());
2314#if __cpp_lib_three_way_comparison
2316 operator<=>(
const _Rb_tree& __x,
const _Rb_tree& __y)
2318 if constexpr (
requires {
typename __detail::__synth3way_t<_Val>; })
2320 __y.begin(), __y.end(),
2321 __detail::__synth3way);
2325 operator<(
const _Rb_tree& __x,
const _Rb_tree& __y)
2327 return std::lexicographical_compare(__x.begin(), __x.end(),
2328 __y.begin(), __y.end());
2333#if __cplusplus >= 201103L
2337 template<
typename... _Args>
2338 _Auto_node(_Rb_tree& __t, _Args&&... __args)
2340 _M_node(__t._M_create_node(std::
forward<_Args>(__args)...))
2346 _M_t._M_drop_node(_M_node);
2349 _Auto_node(_Auto_node&& __n)
2350 : _M_t(__n._M_t), _M_node(__n._M_node)
2351 { __n._M_node =
nullptr; }
2355 {
return _S_key(_M_node); }
2360 auto __it = _M_t._M_insert_node(__p.first, __p.second, _M_node);
2366 _M_insert_equal_lower()
2368 auto __it = _M_t._M_insert_equal_lower_node(_M_node);
2379 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2380 typename _Compare,
typename _Alloc>
2382 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
2383 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
2386#if __cplusplus >= 201103L
2387 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2388 typename _Compare,
typename _Alloc>
2390 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2393 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
2397 constexpr bool __move = !__move_if_noexcept_cond<value_type>::value;
2398 _Alloc_node __an(*
this);
2399 _M_root() = _M_copy<__move>(__x, __an);
2400#pragma GCC diagnostic push
2401#pragma GCC diagnostic ignored "-Wc++17-extensions"
2402 if constexpr (__move)
2404#pragma GCC diagnostic pop
2408 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2409 typename _Compare,
typename _Alloc>
2411 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2412 _M_move_assign(_Rb_tree& __x,
true_type)
2417 std::__alloc_on_move(_M_get_Node_allocator(),
2418 __x._M_get_Node_allocator());
2421 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2422 typename _Compare,
typename _Alloc>
2424 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2427 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
2428 return _M_move_assign(__x,
true_type{});
2432 _Reuse_or_alloc_node __roan(*
this);
2436 _M_root() = _M_copy<__as_rvalue>(__x, __roan);
2441 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2442 typename _Compare,
typename _Alloc>
2443 inline _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
2444 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2445 operator=(_Rb_tree&& __x)
2446 noexcept(_Node_alloc_traits::_S_nothrow_move()
2449 _M_impl._M_key_compare =
std::move(__x._M_impl._M_key_compare);
2451 __bool_constant<_Node_alloc_traits::_S_nothrow_move()>());
2455 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2456 typename _Compare,
typename _Alloc>
2457 template<
typename _Iterator>
2459 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2460 _M_assign_unique(_Iterator __first, _Iterator __last)
2462 _Reuse_or_alloc_node __roan(*
this);
2464 for (; __first != __last; ++__first)
2465 _M_insert_unique_(
end(), *__first, __roan);
2468 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2469 typename _Compare,
typename _Alloc>
2470 template<
typename _Iterator>
2472 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2473 _M_assign_equal(_Iterator __first, _Iterator __last)
2475 _Reuse_or_alloc_node __roan(*
this);
2477 for (; __first != __last; ++__first)
2478 _M_insert_equal_(
end(), *__first, __roan);
2482 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2483 typename _Compare,
typename _Alloc>
2484 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
2485 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2486 operator=(
const _Rb_tree& __x)
2491#if __cplusplus >= 201103L
2492 if (_Node_alloc_traits::_S_propagate_on_copy_assign())
2494 auto& __this_alloc = this->_M_get_Node_allocator();
2495 auto& __that_alloc = __x._M_get_Node_allocator();
2496 if (!_Node_alloc_traits::_S_always_equal()
2497 && __this_alloc != __that_alloc)
2502 std::__alloc_on_copy(__this_alloc, __that_alloc);
2507 _Reuse_or_alloc_node __roan(*
this);
2509 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
2511 _M_root() = _M_copy<__as_lvalue>(__x, __roan);
2517 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2518 typename _Compare,
typename _Alloc>
2519#if __cplusplus >= 201103L
2520 template<
typename _Arg,
typename _NodeGen>
2522 template<
typename _NodeGen>
2524 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2525 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2526 _M_insert_(_Base_ptr __x, _Base_ptr __p,
2527#
if __cplusplus >= 201103L
2532 _NodeGen& __node_gen)
2534 bool __insert_left = (__x || __p == _M_end()
2535 || _M_key_compare(_KeyOfValue()(__v),
2539 __node_gen(_GLIBCXX_FORWARD(_Arg, __v))->_M_base_ptr();
2541 _Node_traits::_S_insert_and_rebalance
2542 (__insert_left, __z, __p, this->_M_impl._M_header);
2543 ++_M_impl._M_node_count;
2547 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2548 typename _Compare,
typename _Alloc>
2549#if __cplusplus >= 201103L
2550 template<
typename _Arg>
2552 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2553 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2554#if __cplusplus >= 201103L
2555 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
2557 _M_insert_lower(_Base_ptr __p,
const _Val& __v)
2560 bool __insert_left = (__p == _M_end()
2561 || !_M_key_compare(_S_key(__p),
2562 _KeyOfValue()(__v)));
2565 _M_create_node(_GLIBCXX_FORWARD(_Arg, __v))->_M_base_ptr();
2566 _Node_traits::_S_insert_and_rebalance
2567 (__insert_left, __z, __p, this->_M_impl._M_header);
2568 ++_M_impl._M_node_count;
2572 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2573 typename _Compare,
typename _Alloc>
2574#if __cplusplus >= 201103L
2575 template<
typename _Arg>
2577 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2578 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2579#if __cplusplus >= 201103L
2580 _M_insert_equal_lower(_Arg&& __v)
2582 _M_insert_equal_lower(
const _Val& __v)
2585 _Base_ptr __x = _M_begin();
2586 _Base_ptr __y = _M_end();
2590 __x = !_M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
2591 _S_left(__x) : _S_right(__x);
2593 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
2596 template<
typename _Key,
typename _Val,
typename _KoV,
2597 typename _Compare,
typename _Alloc>
2598 template<
bool _MoveValues,
typename _NodeGen>
2599 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Base_ptr
2600 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
2601 _M_copy(_Node_ptr __x, _Base_ptr __p, _NodeGen& __node_gen)
2604 _Node_ptr __top = _M_clone_node<_MoveValues>(__x, __node_gen);
2605 _Base_ptr __top_base = __top->_M_base_ptr();
2606 __top->_M_parent = __p;
2612 _M_copy<_MoveValues>(_S_right(__x), __top_base, __node_gen);
2619 _M_clone_node<_MoveValues>(__x, __node_gen)->_M_base_ptr();
2621 __y->_M_parent = __p;
2623 __y->_M_right = _M_copy<_MoveValues>(_S_right(__x),
2632 __throw_exception_again;
2637 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2638 typename _Compare,
typename _Alloc>
2640 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2641 _M_erase(_Node_ptr __x)
2646 _M_erase(_S_right(__x));
2647 _Node_ptr __y = _S_left(__x);
2653 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2654 typename _Compare,
typename _Alloc>
2655 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2656 _Compare, _Alloc>::_Base_ptr
2657 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2658 _M_lower_bound(_Base_ptr __x, _Base_ptr __y,
2659 const _Key& __k)
const
2662 if (!_M_key_compare(_S_key(__x), __k))
2663 __y = __x, __x = _S_left(__x);
2665 __x = _S_right(__x);
2669 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2670 typename _Compare,
typename _Alloc>
2671 template <
typename _Kt>
2672 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr
2673 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2674 _M_lower_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const
2677 if (!_M_key_compare(_S_key(__x), __k))
2678 __y = __x, __x = _S_left(__x);
2680 __x = _S_right(__x);
2684 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2685 typename _Compare,
typename _Alloc>
2686 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2687 _Compare, _Alloc>::_Base_ptr
2688 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2689 _M_upper_bound(_Base_ptr __x, _Base_ptr __y,
2690 const _Key& __k)
const
2693 if (_M_key_compare(__k, _S_key(__x)))
2694 __y = __x, __x = _S_left(__x);
2696 __x = _S_right(__x);
2700 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2701 typename _Compare,
typename _Alloc>
2702 template <
typename _Kt>
2703 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Base_ptr
2704 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2705 _M_upper_bound_tr(_Base_ptr __x, _Base_ptr __y,
const _Kt& __k)
const
2708 if (_M_key_compare(__k, _S_key(__x)))
2709 __y = __x, __x = _S_left(__x);
2711 __x = _S_right(__x);
2715 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2716 typename _Compare,
typename _Alloc>
2717 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2718 _Compare, _Alloc>::iterator,
2719 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2720 _Compare, _Alloc>::iterator>
2721 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2722 equal_range(
const _Key& __k)
2726 _Base_ptr __x = _M_begin();
2727 _Base_ptr __y = _M_end();
2730 if (_M_key_compare(_S_key(__x), __k))
2731 __x = _S_right(__x);
2732 else if (_M_key_compare(__k, _S_key(__x)))
2733 __y = __x, __x = _S_left(__x);
2736 _Base_ptr __xu(__x);
2737 _Base_ptr __yu(__y);
2738 __y = __x, __x = _S_left(__x);
2739 __xu = _S_right(__xu);
2740 return _Ret(
iterator(_M_lower_bound(__x, __y, __k)),
2741 iterator(_M_upper_bound(__xu, __yu, __k)));
2747 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2748 typename _Compare,
typename _Alloc>
2749 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2750 _Compare, _Alloc>::const_iterator,
2751 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2752 _Compare, _Alloc>::const_iterator>
2753 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2754 equal_range(
const _Key& __k)
const
2758 _Base_ptr __x = _M_begin();
2759 _Base_ptr __y = _M_end();
2762 if (_M_key_compare(_S_key(__x), __k))
2763 __x = _S_right(__x);
2764 else if (_M_key_compare(__k, _S_key(__x)))
2765 __y = __x, __x = _S_left(__x);
2768 _Base_ptr __xu(__x);
2769 _Base_ptr __yu(__y);
2770 __y = __x, __x = _S_left(__x);
2771 __xu = _S_right(__xu);
2772 return _Ret(const_iterator(_M_lower_bound(__x, __y, __k)),
2773 const_iterator(_M_upper_bound(__xu, __yu, __k)));
2776 return _Ret(const_iterator(__y), const_iterator(__y));
2779 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2780 typename _Compare,
typename _Alloc>
2782 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2784 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
2789 _M_impl._M_move_data(__t._M_impl);
2791 else if (!__t._M_root())
2792 __t._M_impl._M_move_data(_M_impl);
2795 std::swap(_M_root(),__t._M_root());
2796 std::swap(_M_leftmost(),__t._M_leftmost());
2797 std::swap(_M_rightmost(),__t._M_rightmost());
2799 _M_root()->_M_parent = _M_end();
2800 __t._M_root()->_M_parent = __t._M_end();
2801 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
2806 swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
2808 _Node_alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2809 __t._M_get_Node_allocator());
2812 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2813 typename _Compare,
typename _Alloc>
2814 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2815 _Compare, _Alloc>::_Base_ptr,
2816 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2817 _Compare, _Alloc>::_Base_ptr>
2818 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2819 _M_get_insert_unique_pos(
const key_type& __k)
2822 _Base_ptr __x = _M_begin();
2823 _Base_ptr __y = _M_end();
2828 __comp = _M_key_compare(__k, _S_key(__x));
2829 __x = __comp ? _S_left(__x) : _S_right(__x);
2835 return _Res(__x, __y);
2839 if (_M_key_compare(_S_key(__j._M_node), __k))
2840 return _Res(__x, __y);
2841 return _Res(__j._M_node, _Base_ptr());
2844 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2845 typename _Compare,
typename _Alloc>
2846 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2847 _Compare, _Alloc>::_Base_ptr,
2848 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2849 _Compare, _Alloc>::_Base_ptr>
2850 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2851 _M_get_insert_equal_pos(
const key_type& __k)
2854 _Base_ptr __x = _M_begin();
2855 _Base_ptr __y = _M_end();
2859 __x = _M_key_compare(__k, _S_key(__x)) ? _S_left(__x) : _S_right(__x);
2861 return _Res(__x, __y);
2864#ifdef __glibcxx_associative_heterogeneous_insertion
2869 template <
typename _Key,
typename _Val,
typename _KeyOfValue,
2870 typename _Compare,
typename _Alloc>
2871 template <
typename _Kt>
2873 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2874 _M_get_insert_unique_pos_tr(
const _Kt& __k)
2878 return { _M_end(), _M_end() };
2880 _Base_ptr __x = _M_begin(), __y = __x;
2881 bool __k_le_y =
false;
2885 __k_le_y = ! _M_key_compare(_S_key(__x), __k);
2886 __x = __k_le_y ? _S_left(__x) : _S_right(__x);
2899 if (__y == _M_rightmost())
2903 if (_M_key_compare(__k, _S_key(__j._M_node)))
2906 return { __y, __y };
2910 return { __j._M_node, {} };
2914 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2915 typename _Compare,
typename _Alloc>
2916#if __cplusplus >= 201103L
2917 template<
typename _Arg>
2919 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2920 _Compare, _Alloc>::iterator,
bool>
2921 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2922#if __cplusplus >= 201103L
2923 _M_insert_unique(_Arg&& __v)
2925 _M_insert_unique(
const _Val& __v)
2930 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
2934 _Alloc_node __an(*
this);
2935 return _Res(_M_insert_(__res.first, __res.second,
2936 _GLIBCXX_FORWARD(_Arg, __v), __an),
2940 return _Res(
iterator(__res.first),
false);
2943 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2944 typename _Compare,
typename _Alloc>
2945#if __cplusplus >= 201103L
2946 template<
typename _Arg>
2948 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
2949 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2950#if __cplusplus >= 201103L
2951 _M_insert_equal(_Arg&& __v)
2953 _M_insert_equal(
const _Val& __v)
2957 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
2958 _Alloc_node __an(*
this);
2959 return _M_insert_(__res.first, __res.second,
2960 _GLIBCXX_FORWARD(_Arg, __v), __an);
2963 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
2964 typename _Compare,
typename _Alloc>
2965 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
2966 _Compare, _Alloc>::_Base_ptr,
2967 typename _Rb_tree<_Key, _Val, _KeyOfValue,
2968 _Compare, _Alloc>::_Base_ptr>
2969 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
2970 _M_get_insert_hint_unique_pos(const_iterator __position,
2971 const key_type& __k)
2976 if (__position._M_node == _M_end())
2978 if (
size() > 0 && _M_key_compare(_S_key(_M_rightmost()), __k))
2979 return _Res(_Base_ptr(), _M_rightmost());
2981 return _M_get_insert_unique_pos(__k);
2983 else if (_M_key_compare(__k, _S_key(__position._M_node)))
2986 iterator __before(__position._M_node);
2987 if (__position._M_node == _M_leftmost())
2988 return _Res(_M_leftmost(), _M_leftmost());
2989 else if (_M_key_compare(_S_key((--__before)._M_node), __k))
2991 if (!_S_right(__before._M_node))
2992 return _Res(_Base_ptr(), __before._M_node);
2994 return _Res(__position._M_node, __position._M_node);
2997 return _M_get_insert_unique_pos(__k);
2999 else if (_M_key_compare(_S_key(__position._M_node), __k))
3002 iterator __after(__position._M_node);
3003 if (__position._M_node == _M_rightmost())
3004 return _Res(_Base_ptr(), _M_rightmost());
3005 else if (_M_key_compare(__k, _S_key((++__after)._M_node)))
3007 if (!_S_right(__position._M_node))
3008 return _Res(_Base_ptr(), __position._M_node);
3010 return _Res(__after._M_node, __after._M_node);
3013 return _M_get_insert_unique_pos(__k);
3017 return _Res(__position._M_node, _Base_ptr());
3020#ifdef __glibcxx_associative_heterogeneous_insertion
3021 template <
typename _Key,
typename _Val,
typename _KeyOfValue,
3022 typename _Compare,
typename _Alloc>
3023 template <
typename _Kt>
3025 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3026 _M_get_insert_hint_unique_pos_tr(const_iterator __hint,
const _Kt& __k)
3029 auto __node =__hint._M_node;
3030 if (__node == _M_end())
3032 if (
size() > 0 && _M_key_compare(_S_key(_M_rightmost()), __k))
3033 return { {}, _M_rightmost() };
3034 return _M_get_insert_unique_pos_tr(__k);
3036 if (_M_key_compare(__k, _S_key(__node)))
3038 if (__node == _M_leftmost())
3039 return { _M_leftmost(), _M_leftmost() };
3042 if (_M_key_compare(_S_key(__before._M_node), __k))
3044 if (!_S_right(__before._M_node))
3045 return { {}, __before._M_node };
3046 return { __node, __node };
3048 return _M_get_insert_unique_pos_tr(__k);
3050 if (_M_key_compare(_S_key(__node), __k))
3052 if (__node == _M_rightmost())
3053 return { {}, _M_rightmost() };
3056 if (_M_key_compare(__k, _S_key(__after._M_node)))
3058 if (!_S_right(__node))
3059 return { {}, __node };
3060 return { __after._M_node, __after._M_node };
3062 return _M_get_insert_unique_pos_tr(__k);
3066 if (__node == _M_leftmost() ||
3067 _M_key_compare(_S_key((--__before)._M_node), __k))
3068 {
return { __node, {} }; }
3069 return _M_get_insert_unique_pos_tr(__k);
3073 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3074 typename _Compare,
typename _Alloc>
3075#if __cplusplus >= 201103L
3076 template<
typename _Arg,
typename _NodeGen>
3078 template<
typename _NodeGen>
3080 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
3081 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3082 _M_insert_unique_(const_iterator __position,
3083#
if __cplusplus >= 201103L
3088 _NodeGen& __node_gen)
3091 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
3094 return _M_insert_(__res.first, __res.second,
3095 _GLIBCXX_FORWARD(_Arg, __v),
3100 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3101 typename _Compare,
typename _Alloc>
3102 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
3103 _Compare, _Alloc>::_Base_ptr,
3104 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3105 _Compare, _Alloc>::_Base_ptr>
3106 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3107 _M_get_insert_hint_equal_pos(const_iterator __position,
const key_type& __k)
3112 if (__position._M_node == _M_end())
3115 && !_M_key_compare(__k, _S_key(_M_rightmost())))
3116 return _Res(_Base_ptr(), _M_rightmost());
3118 return _M_get_insert_equal_pos(__k);
3120 else if (!_M_key_compare(_S_key(__position._M_node), __k))
3123 iterator __before(__position._M_node);
3124 if (__position._M_node == _M_leftmost())
3125 return _Res(_M_leftmost(), _M_leftmost());
3126 else if (!_M_key_compare(__k, _S_key((--__before)._M_node)))
3128 if (!_S_right(__before._M_node))
3129 return _Res(_Base_ptr(), __before._M_node);
3131 return _Res(__position._M_node, __position._M_node);
3134 return _M_get_insert_equal_pos(__k);
3139 iterator __after(__position._M_node);
3140 if (__position._M_node == _M_rightmost())
3141 return _Res(_Base_ptr(), _M_rightmost());
3142 else if (!_M_key_compare(_S_key((++__after)._M_node), __k))
3144 if (!_S_right(__position._M_node))
3145 return _Res(_Base_ptr(), __position._M_node);
3147 return _Res(__after._M_node, __after._M_node);
3150 return _Res(_Base_ptr(), _Base_ptr());
3154 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3155 typename _Compare,
typename _Alloc>
3156#if __cplusplus >= 201103L
3157 template<
typename _Arg,
typename _NodeGen>
3159 template<
typename _NodeGen>
3161 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
3162 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3163 _M_insert_equal_(const_iterator __position,
3164#
if __cplusplus >= 201103L
3169 _NodeGen& __node_gen)
3172 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
3175 return _M_insert_(__res.first, __res.second,
3176 _GLIBCXX_FORWARD(_Arg, __v),
3179 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
3182#if __cplusplus >= 201103L
3183 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3184 typename _Compare,
typename _Alloc>
3186 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3187 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Node_ptr __z)
3190 bool __insert_left = (__x || __p == _M_end()
3191 || _M_key_compare(_S_key(__z), _S_key(__p)));
3193 _Base_ptr __base_z = __z->_M_base_ptr();
3194 _Node_traits::_S_insert_and_rebalance
3195 (__insert_left, __base_z, __p, this->_M_impl._M_header);
3196 ++_M_impl._M_node_count;
3200 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3201 typename _Compare,
typename _Alloc>
3203 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3204 _M_insert_lower_node(_Base_ptr __p, _Node_ptr __z)
3207 bool __insert_left = (__p == _M_end()
3208 || !_M_key_compare(_S_key(__p), _S_key(__z)));
3210 _Base_ptr __base_z = __z->_M_base_ptr();
3211 _Node_traits::_S_insert_and_rebalance
3212 (__insert_left, __base_z, __p, this->_M_impl._M_header);
3213 ++_M_impl._M_node_count;
3217 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3218 typename _Compare,
typename _Alloc>
3220 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3221 _M_insert_equal_lower_node(_Node_ptr __z)
3224 _Base_ptr __x = _M_begin();
3225 _Base_ptr __y = _M_end();
3229 __x = !_M_key_compare(_S_key(__x), _S_key(__z)) ?
3230 _S_left(__x) : _S_right(__x);
3232 return _M_insert_lower_node(__y, __z);
3235 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3236 typename _Compare,
typename _Alloc>
3237 template<
typename... _Args>
3239 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3240 _M_emplace_unique(_Args&&... __args)
3244 auto __res = _M_get_insert_unique_pos(__z._M_key());
3246 return {__z._M_insert(__res),
true};
3247 return {
iterator(__res.first),
false};
3250 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3251 typename _Compare,
typename _Alloc>
3252 template<
typename... _Args>
3254 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3255 _M_emplace_equal(_Args&&... __args)
3259 auto __res = _M_get_insert_equal_pos(__z._M_key());
3260 return __z._M_insert(__res);
3263 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3264 typename _Compare,
typename _Alloc>
3265 template<
typename... _Args>
3267 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3268 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
3272 auto __res = _M_get_insert_hint_unique_pos(__pos, __z._M_key());
3274 return __z._M_insert(__res);
3278 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3279 typename _Compare,
typename _Alloc>
3280 template<
typename... _Args>
3282 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3283 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
3287 auto __res = _M_get_insert_hint_equal_pos(__pos, __z._M_key());
3289 return __z._M_insert(__res);
3290 return __z._M_insert_equal_lower();
3293#ifdef __glibcxx_associative_heterogeneous_insertion
3294 template <
typename _Key,
typename _Val,
typename _KeyOfValue,
3295 typename _Compare,
typename _Alloc>
3296 template <
typename... _Args>
3298 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3299 _M_emplace_here(
bool __place_left, _Base_ptr __node, _Args&&... __args)
3303 _Base_ptr __base_z = __z._M_node->_M_base_ptr();
3304 _Node_traits::_S_insert_and_rebalance(
3305 __place_left, __base_z, __node, _M_impl._M_header);
3306 __z._M_node =
nullptr;
3307 ++_M_impl._M_node_count;
3315 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3316 typename _Compare,
typename _Alloc>
3318 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3319 _M_erase_aux(const_iterator __position)
3321 _Base_ptr __y = _Node_traits::_S_rebalance_for_erase
3322 (__position._M_node, this->_M_impl._M_header);
3323 _M_drop_node(
static_cast<_Node&
>(*__y)._M_node_ptr());
3324 --_M_impl._M_node_count;
3327 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3328 typename _Compare,
typename _Alloc>
3330 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3331 _M_erase_aux(const_iterator __first, const_iterator __last)
3333 if (__first ==
begin() && __last ==
end())
3336 while (__first != __last)
3337 _M_erase_aux(__first++);
3340 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3341 typename _Compare,
typename _Alloc>
3342 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3343 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3344 erase(
const _Key& __x)
3348 _M_erase_aux(__p.first, __p.second);
3349 return __old_size -
size();
3352 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3353 typename _Compare,
typename _Alloc>
3354 template <
typename _Kt>
3355 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3356 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3357 _M_erase_tr(
const _Kt& __x)
3361 _M_erase_aux(__p.first, __p.second);
3362 return __old_size -
size();
3365 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3366 typename _Compare,
typename _Alloc>
3367 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3368 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3369 _M_erase_unique(
const _Key& __x)
3379 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3380 typename _Compare,
typename _Alloc>
3381 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3382 _Compare, _Alloc>::iterator
3383 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3384 find(
const _Key& __k)
3386 iterator __j(_M_lower_bound(_M_begin(), _M_end(), __k));
3387 return (__j ==
end()
3388 || _M_key_compare(__k, _S_key(__j._M_node))) ?
end() : __j;
3391 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3392 typename _Compare,
typename _Alloc>
3393 typename _Rb_tree<_Key, _Val, _KeyOfValue,
3394 _Compare, _Alloc>::const_iterator
3395 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3396 find(
const _Key& __k)
const
3398 const_iterator __j(_M_lower_bound(_M_begin(), _M_end(), __k));
3399 return (__j ==
end()
3400 || _M_key_compare(__k, _S_key(__j._M_node))) ?
end() : __j;
3403 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3404 typename _Compare,
typename _Alloc>
3405 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
3406 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
3407 count(
const _Key& __k)
const
3414 _GLIBCXX_PURE
unsigned int
3415 _Rb_tree_black_count(
const _Rb_tree_node_base* __node,
3416 const _Rb_tree_node_base* __root)
throw ();
3418 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
3419 typename _Compare,
typename _Alloc>
3421 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify()
const
3423 if (_M_impl._M_node_count == 0 ||
begin() ==
end())
3424 return _M_impl._M_node_count == 0 &&
begin() ==
end()
3425 && this->_M_impl._M_header._M_left == _M_end()
3426 && this->_M_impl._M_header._M_right == _M_end();
3428 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
3429 for (const_iterator __it =
begin(); __it !=
end(); ++__it)
3431 _Base_ptr __x = __it._M_node;
3432 _Base_ptr __L = _S_left(__x);
3433 _Base_ptr __R = _S_right(__x);
3435 if (__x->_M_color == _S_red)
3436 if ((__L && __L->_M_color == _S_red)
3437 || (__R && __R->_M_color == _S_red))
3440 if (__L && _M_key_compare(_S_key(__x), _S_key(__L)))
3442 if (__R && _M_key_compare(_S_key(__R), _S_key(__x)))
3445 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
3449 if (_M_leftmost() != _Node_base::_S_minimum(_M_root()))
3451 if (_M_rightmost() != _Node_base::_S_maximum(_M_root()))
3456#ifdef __glibcxx_node_extract
3458 template<
typename _Key,
typename _Val,
typename _Sel,
typename _Cmp1,
3459 typename _Alloc,
typename _Cmp2>
3460 struct _Rb_tree_merge_helper<_Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>,
3464 friend class _Rb_tree<_Key, _Val, _Sel, _Cmp1, _Alloc>;
3467 _S_get_impl(_Rb_tree<_Key, _Val, _Sel, _Cmp2, _Alloc>& __tree)
3468 {
return __tree._M_impl; }
3472#ifdef __glibcxx_associative_heterogeneous_erasure
3473template <
typename _Kt,
typename _Container>
3474 concept __heterogeneous_tree_key =
3475 __transparent_comparator<typename _Container::key_compare> &&
3476 __heterogeneous_key<_Kt, _Container>;
3479_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