66 #if __cplusplus >= 201103L
70 namespace std _GLIBCXX_VISIBILITY(default)
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
90 enum _Rb_tree_color { _S_red =
false, _S_black =
true };
92 struct _Rb_tree_node_base
94 typedef _Rb_tree_node_base* _Base_ptr;
95 typedef const _Rb_tree_node_base* _Const_Base_ptr;
97 _Rb_tree_color _M_color;
103 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
105 while (__x->_M_left != 0) __x = __x->_M_left;
109 static _Const_Base_ptr
110 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
112 while (__x->_M_left != 0) __x = __x->_M_left;
117 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
119 while (__x->_M_right != 0) __x = __x->_M_right;
123 static _Const_Base_ptr
124 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
126 while (__x->_M_right != 0) __x = __x->_M_right;
131 template<
typename _Val>
132 struct _Rb_tree_node :
public _Rb_tree_node_base
134 typedef _Rb_tree_node<_Val>* _Link_type;
136 #if __cplusplus < 201103L
147 __gnu_cxx::__aligned_buffer<_Val> _M_storage;
151 {
return _M_storage._M_ptr(); }
155 {
return _M_storage._M_ptr(); }
159 _GLIBCXX_PURE _Rb_tree_node_base*
160 _Rb_tree_increment(_Rb_tree_node_base* __x)
throw ();
162 _GLIBCXX_PURE
const _Rb_tree_node_base*
163 _Rb_tree_increment(
const _Rb_tree_node_base* __x)
throw ();
165 _GLIBCXX_PURE _Rb_tree_node_base*
166 _Rb_tree_decrement(_Rb_tree_node_base* __x)
throw ();
168 _GLIBCXX_PURE
const _Rb_tree_node_base*
169 _Rb_tree_decrement(
const _Rb_tree_node_base* __x)
throw ();
171 template<
typename _Tp>
172 struct _Rb_tree_iterator
174 typedef _Tp value_type;
175 typedef _Tp& reference;
176 typedef _Tp* pointer;
178 typedef bidirectional_iterator_tag iterator_category;
179 typedef ptrdiff_t difference_type;
181 typedef _Rb_tree_iterator<_Tp> _Self;
182 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr;
183 typedef _Rb_tree_node<_Tp>* _Link_type;
185 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
189 _Rb_tree_iterator(_Link_type __x) _GLIBCXX_NOEXCEPT
194 {
return *
static_cast<_Link_type
>(_M_node)->_M_valptr(); }
197 operator->() const _GLIBCXX_NOEXCEPT
198 {
return static_cast<_Link_type
> (_M_node)->_M_valptr(); }
201 operator++() _GLIBCXX_NOEXCEPT
203 _M_node = _Rb_tree_increment(_M_node);
208 operator++(
int) _GLIBCXX_NOEXCEPT
211 _M_node = _Rb_tree_increment(_M_node);
216 operator--() _GLIBCXX_NOEXCEPT
218 _M_node = _Rb_tree_decrement(_M_node);
223 operator--(
int) _GLIBCXX_NOEXCEPT
226 _M_node = _Rb_tree_decrement(_M_node);
231 operator==(
const _Self& __x)
const _GLIBCXX_NOEXCEPT
232 {
return _M_node == __x._M_node; }
235 operator!=(
const _Self& __x)
const _GLIBCXX_NOEXCEPT
236 {
return _M_node != __x._M_node; }
241 template<
typename _Tp>
242 struct _Rb_tree_const_iterator
244 typedef _Tp value_type;
245 typedef const _Tp& reference;
246 typedef const _Tp* pointer;
248 typedef _Rb_tree_iterator<_Tp> iterator;
250 typedef bidirectional_iterator_tag iterator_category;
251 typedef ptrdiff_t difference_type;
253 typedef _Rb_tree_const_iterator<_Tp> _Self;
254 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr;
255 typedef const _Rb_tree_node<_Tp>* _Link_type;
257 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
261 _Rb_tree_const_iterator(_Link_type __x) _GLIBCXX_NOEXCEPT
264 _Rb_tree_const_iterator(
const iterator& __it) _GLIBCXX_NOEXCEPT
265 : _M_node(__it._M_node) { }
268 _M_const_cast() const _GLIBCXX_NOEXCEPT
269 {
return iterator(static_cast<typename iterator::_Link_type>
270 (const_cast<typename iterator::_Base_ptr>(_M_node))); }
274 {
return *
static_cast<_Link_type
>(_M_node)->_M_valptr(); }
277 operator->() const _GLIBCXX_NOEXCEPT
278 {
return static_cast<_Link_type
>(_M_node)->_M_valptr(); }
281 operator++() _GLIBCXX_NOEXCEPT
283 _M_node = _Rb_tree_increment(_M_node);
288 operator++(
int) _GLIBCXX_NOEXCEPT
291 _M_node = _Rb_tree_increment(_M_node);
296 operator--() _GLIBCXX_NOEXCEPT
298 _M_node = _Rb_tree_decrement(_M_node);
303 operator--(
int) _GLIBCXX_NOEXCEPT
306 _M_node = _Rb_tree_decrement(_M_node);
311 operator==(
const _Self& __x)
const _GLIBCXX_NOEXCEPT
312 {
return _M_node == __x._M_node; }
315 operator!=(
const _Self& __x)
const _GLIBCXX_NOEXCEPT
316 {
return _M_node != __x._M_node; }
321 template<
typename _Val>
323 operator==(
const _Rb_tree_iterator<_Val>& __x,
324 const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
325 {
return __x._M_node == __y._M_node; }
327 template<
typename _Val>
329 operator!=(
const _Rb_tree_iterator<_Val>& __x,
330 const _Rb_tree_const_iterator<_Val>& __y) _GLIBCXX_NOEXCEPT
331 {
return __x._M_node != __y._M_node; }
334 _Rb_tree_insert_and_rebalance(
const bool __insert_left,
335 _Rb_tree_node_base* __x,
336 _Rb_tree_node_base* __p,
337 _Rb_tree_node_base& __header)
throw ();
340 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base*
const __z,
341 _Rb_tree_node_base& __header)
throw ();
344 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
345 typename _Compare,
typename _Alloc = allocator<_Val> >
349 rebind<_Rb_tree_node<_Val> >::other _Node_allocator;
354 typedef _Rb_tree_node_base* _Base_ptr;
355 typedef const _Rb_tree_node_base* _Const_Base_ptr;
358 typedef _Key key_type;
359 typedef _Val value_type;
360 typedef value_type* pointer;
361 typedef const value_type* const_pointer;
362 typedef value_type& reference;
363 typedef const value_type& const_reference;
364 typedef _Rb_tree_node<_Val>* _Link_type;
365 typedef const _Rb_tree_node<_Val>* _Const_Link_type;
366 typedef size_t size_type;
367 typedef ptrdiff_t difference_type;
368 typedef _Alloc allocator_type;
371 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
372 {
return *
static_cast<_Node_allocator*
>(&this->_M_impl); }
374 const _Node_allocator&
375 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
376 {
return *
static_cast<const _Node_allocator*
>(&this->_M_impl); }
379 get_allocator() const _GLIBCXX_NOEXCEPT
380 {
return allocator_type(_M_get_Node_allocator()); }
388 _M_put_node(_Link_type __p) _GLIBCXX_NOEXCEPT
391 #if __cplusplus < 201103L
393 _M_create_node(
const value_type& __x)
395 _Link_type __tmp = _M_get_node();
397 { get_allocator().construct(__tmp->_M_valptr(), __x); }
401 __throw_exception_again;
407 _M_destroy_node(_Link_type __p)
409 get_allocator().destroy(__p->_M_valptr());
413 template<
typename... _Args>
415 _M_create_node(_Args&&... __args)
417 _Link_type __tmp = _M_get_node();
420 ::new(__tmp) _Rb_tree_node<_Val>;
421 _Alloc_traits::construct(_M_get_Node_allocator(),
423 std::
forward<_Args>(__args)...);
428 __throw_exception_again;
434 _M_destroy_node(_Link_type __p) noexcept
436 _Alloc_traits::destroy(_M_get_Node_allocator(), __p->_M_valptr());
437 __p->~_Rb_tree_node<_Val>();
443 _M_clone_node(_Const_Link_type __x)
445 _Link_type __tmp = _M_create_node(*__x->_M_valptr());
446 __tmp->_M_color = __x->_M_color;
453 template<
typename _Key_compare,
454 bool _Is_pod_comparator = __is_pod(_Key_compare)>
455 struct _Rb_tree_impl :
public _Node_allocator
457 _Key_compare _M_key_compare;
458 _Rb_tree_node_base _M_header;
459 size_type _M_node_count;
462 : _Node_allocator(), _M_key_compare(), _M_header(),
466 _Rb_tree_impl(
const _Key_compare& __comp,
const _Node_allocator& __a)
467 : _Node_allocator(__a), _M_key_compare(__comp), _M_header(),
471 #if __cplusplus >= 201103L
472 _Rb_tree_impl(
const _Key_compare& __comp, _Node_allocator&& __a)
473 : _Node_allocator(std::move(__a)), _M_key_compare(__comp),
474 _M_header(), _M_node_count(0)
482 this->_M_header._M_color = _S_red;
483 this->_M_header._M_parent = 0;
484 this->_M_header._M_left = &this->_M_header;
485 this->_M_header._M_right = &this->_M_header;
489 _Rb_tree_impl<_Compare> _M_impl;
493 _M_root() _GLIBCXX_NOEXCEPT
494 {
return this->_M_impl._M_header._M_parent; }
497 _M_root() const _GLIBCXX_NOEXCEPT
498 {
return this->_M_impl._M_header._M_parent; }
501 _M_leftmost() _GLIBCXX_NOEXCEPT
502 {
return this->_M_impl._M_header._M_left; }
505 _M_leftmost() const _GLIBCXX_NOEXCEPT
506 {
return this->_M_impl._M_header._M_left; }
509 _M_rightmost() _GLIBCXX_NOEXCEPT
510 {
return this->_M_impl._M_header._M_right; }
513 _M_rightmost() const _GLIBCXX_NOEXCEPT
514 {
return this->_M_impl._M_header._M_right; }
517 _M_begin() _GLIBCXX_NOEXCEPT
518 {
return static_cast<_Link_type
>(this->_M_impl._M_header._M_parent); }
521 _M_begin() const _GLIBCXX_NOEXCEPT
523 return static_cast<_Const_Link_type
>
524 (this->_M_impl._M_header._M_parent);
528 _M_end() _GLIBCXX_NOEXCEPT
529 {
return static_cast<_Link_type
>(&this->_M_impl._M_header); }
532 _M_end() const _GLIBCXX_NOEXCEPT
533 {
return static_cast<_Const_Link_type
>(&this->_M_impl._M_header); }
535 static const_reference
536 _S_value(_Const_Link_type __x)
537 {
return *__x->_M_valptr(); }
540 _S_key(_Const_Link_type __x)
541 {
return _KeyOfValue()(_S_value(__x)); }
544 _S_left(_Base_ptr __x) _GLIBCXX_NOEXCEPT
545 {
return static_cast<_Link_type
>(__x->_M_left); }
547 static _Const_Link_type
548 _S_left(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
549 {
return static_cast<_Const_Link_type
>(__x->_M_left); }
552 _S_right(_Base_ptr __x) _GLIBCXX_NOEXCEPT
553 {
return static_cast<_Link_type
>(__x->_M_right); }
555 static _Const_Link_type
556 _S_right(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
557 {
return static_cast<_Const_Link_type
>(__x->_M_right); }
559 static const_reference
560 _S_value(_Const_Base_ptr __x)
561 {
return *
static_cast<_Const_Link_type
>(__x)->_M_valptr(); }
564 _S_key(_Const_Base_ptr __x)
565 {
return _KeyOfValue()(_S_value(__x)); }
568 _S_minimum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
569 {
return _Rb_tree_node_base::_S_minimum(__x); }
571 static _Const_Base_ptr
572 _S_minimum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
573 {
return _Rb_tree_node_base::_S_minimum(__x); }
576 _S_maximum(_Base_ptr __x) _GLIBCXX_NOEXCEPT
577 {
return _Rb_tree_node_base::_S_maximum(__x); }
579 static _Const_Base_ptr
580 _S_maximum(_Const_Base_ptr __x) _GLIBCXX_NOEXCEPT
581 {
return _Rb_tree_node_base::_S_maximum(__x); }
584 typedef _Rb_tree_iterator<value_type> iterator;
585 typedef _Rb_tree_const_iterator<value_type> const_iterator;
591 pair<_Base_ptr, _Base_ptr>
592 _M_get_insert_unique_pos(
const key_type& __k);
594 pair<_Base_ptr, _Base_ptr>
595 _M_get_insert_equal_pos(
const key_type& __k);
597 pair<_Base_ptr, _Base_ptr>
598 _M_get_insert_hint_unique_pos(const_iterator __pos,
599 const key_type& __k);
601 pair<_Base_ptr, _Base_ptr>
602 _M_get_insert_hint_equal_pos(const_iterator __pos,
603 const key_type& __k);
605 #if __cplusplus >= 201103L
606 template<
typename _Arg>
608 _M_insert_(_Base_ptr __x, _Base_ptr __y, _Arg&& __v);
611 _M_insert_node(_Base_ptr __x, _Base_ptr __y, _Link_type __z);
613 template<
typename _Arg>
615 _M_insert_lower(_Base_ptr __y, _Arg&& __v);
617 template<
typename _Arg>
619 _M_insert_equal_lower(_Arg&& __x);
622 _M_insert_lower_node(_Base_ptr __p, _Link_type __z);
625 _M_insert_equal_lower_node(_Link_type __z);
628 _M_insert_(_Base_ptr __x, _Base_ptr __y,
629 const value_type& __v);
634 _M_insert_lower(_Base_ptr __y,
const value_type& __v);
637 _M_insert_equal_lower(
const value_type& __x);
641 _M_copy(_Const_Link_type __x, _Link_type __p);
644 _M_erase(_Link_type __x);
647 _M_lower_bound(_Link_type __x, _Link_type __y,
651 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
652 const _Key& __k)
const;
655 _M_upper_bound(_Link_type __x, _Link_type __y,
659 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
660 const _Key& __k)
const;
666 _Rb_tree(
const _Compare& __comp,
667 const allocator_type& __a = allocator_type())
668 : _M_impl(__comp, _Node_allocator(__a)) { }
670 _Rb_tree(
const _Rb_tree& __x)
671 : _M_impl(__x._M_impl._M_key_compare,
672 _Alloc_traits::_S_select_on_copy(__x._M_get_Node_allocator()))
674 if (__x._M_root() != 0)
676 _M_root() = _M_copy(__x._M_begin(), _M_end());
677 _M_leftmost() = _S_minimum(_M_root());
678 _M_rightmost() = _S_maximum(_M_root());
679 _M_impl._M_node_count = __x._M_impl._M_node_count;
683 #if __cplusplus >= 201103L
684 _Rb_tree(
const allocator_type& __a)
685 : _M_impl(_Compare(), _Node_allocator(__a))
688 _Rb_tree(
const _Rb_tree& __x,
const allocator_type& __a)
689 : _M_impl(__x._M_impl._M_key_compare, _Node_allocator(__a))
691 if (__x._M_root() != 0)
693 _M_root() = _M_copy(__x._M_begin(), _M_end());
694 _M_leftmost() = _S_minimum(_M_root());
695 _M_rightmost() = _S_maximum(_M_root());
696 _M_impl._M_node_count = __x._M_impl._M_node_count;
700 _Rb_tree(_Rb_tree&& __x)
701 : _M_impl(__x._M_impl._M_key_compare, __x._M_get_Node_allocator())
703 if (__x._M_root() != 0)
707 _Rb_tree(_Rb_tree&& __x,
const allocator_type& __a)
708 : _Rb_tree(std::move(__x), _Node_allocator(__a))
711 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a);
714 ~_Rb_tree() _GLIBCXX_NOEXCEPT
715 { _M_erase(_M_begin()); }
718 operator=(
const _Rb_tree& __x);
723 {
return _M_impl._M_key_compare; }
726 begin() _GLIBCXX_NOEXCEPT
728 return iterator(static_cast<_Link_type>
729 (this->_M_impl._M_header._M_left));
733 begin() const _GLIBCXX_NOEXCEPT
735 return const_iterator(static_cast<_Const_Link_type>
736 (this->_M_impl._M_header._M_left));
740 end() _GLIBCXX_NOEXCEPT
741 {
return iterator(static_cast<_Link_type>(&this->_M_impl._M_header)); }
744 end() const _GLIBCXX_NOEXCEPT
746 return const_iterator(static_cast<_Const_Link_type>
747 (&this->_M_impl._M_header));
751 rbegin() _GLIBCXX_NOEXCEPT
752 {
return reverse_iterator(
end()); }
754 const_reverse_iterator
755 rbegin() const _GLIBCXX_NOEXCEPT
756 {
return const_reverse_iterator(
end()); }
759 rend() _GLIBCXX_NOEXCEPT
760 {
return reverse_iterator(
begin()); }
762 const_reverse_iterator
763 rend() const _GLIBCXX_NOEXCEPT
764 {
return const_reverse_iterator(
begin()); }
767 empty() const _GLIBCXX_NOEXCEPT
768 {
return _M_impl._M_node_count == 0; }
771 size() const _GLIBCXX_NOEXCEPT
772 {
return _M_impl._M_node_count; }
775 max_size() const _GLIBCXX_NOEXCEPT
779 #if __cplusplus >= 201103L
780 swap(_Rb_tree& __t) noexcept(_Alloc_traits::_S_nothrow_swap());
786 #if __cplusplus >= 201103L
787 template<
typename _Arg>
789 _M_insert_unique(_Arg&& __x);
791 template<
typename _Arg>
793 _M_insert_equal(_Arg&& __x);
795 template<
typename _Arg>
797 _M_insert_unique_(const_iterator __position, _Arg&& __x);
799 template<
typename _Arg>
801 _M_insert_equal_(const_iterator __position, _Arg&& __x);
803 template<
typename... _Args>
805 _M_emplace_unique(_Args&&... __args);
807 template<
typename... _Args>
809 _M_emplace_equal(_Args&&... __args);
811 template<
typename... _Args>
813 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args);
815 template<
typename... _Args>
817 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args);
820 _M_insert_unique(
const value_type& __x);
823 _M_insert_equal(
const value_type& __x);
826 _M_insert_unique_(const_iterator __position,
const value_type& __x);
829 _M_insert_equal_(const_iterator __position,
const value_type& __x);
832 template<
typename _InputIterator>
834 _M_insert_unique(_InputIterator __first, _InputIterator __last);
836 template<
typename _InputIterator>
838 _M_insert_equal(_InputIterator __first, _InputIterator __last);
842 _M_erase_aux(const_iterator __position);
845 _M_erase_aux(const_iterator __first, const_iterator __last);
848 #if __cplusplus >= 201103L
851 _GLIBCXX_ABI_TAG_CXX11
853 erase(const_iterator __position)
855 const_iterator __result = __position;
857 _M_erase_aux(__position);
858 return __result._M_const_cast();
862 _GLIBCXX_ABI_TAG_CXX11
864 erase(iterator __position)
866 iterator __result = __position;
868 _M_erase_aux(__position);
873 erase(iterator __position)
874 { _M_erase_aux(__position); }
877 erase(const_iterator __position)
878 { _M_erase_aux(__position); }
881 erase(
const key_type& __x);
883 #if __cplusplus >= 201103L
886 _GLIBCXX_ABI_TAG_CXX11
888 erase(const_iterator __first, const_iterator __last)
890 _M_erase_aux(__first, __last);
891 return __last._M_const_cast();
895 erase(iterator __first, iterator __last)
896 { _M_erase_aux(__first, __last); }
899 erase(const_iterator __first, const_iterator __last)
900 { _M_erase_aux(__first, __last); }
903 erase(
const key_type* __first,
const key_type* __last);
906 clear() _GLIBCXX_NOEXCEPT
908 _M_erase(_M_begin());
909 _M_leftmost() = _M_end();
911 _M_rightmost() = _M_end();
912 _M_impl._M_node_count = 0;
917 find(
const key_type& __k);
920 find(
const key_type& __k)
const;
923 count(
const key_type& __k)
const;
926 lower_bound(
const key_type& __k)
927 {
return _M_lower_bound(_M_begin(), _M_end(), __k); }
930 lower_bound(
const key_type& __k)
const
931 {
return _M_lower_bound(_M_begin(), _M_end(), __k); }
934 upper_bound(
const key_type& __k)
935 {
return _M_upper_bound(_M_begin(), _M_end(), __k); }
938 upper_bound(
const key_type& __k)
const
939 {
return _M_upper_bound(_M_begin(), _M_end(), __k); }
941 pair<iterator, iterator>
942 equal_range(
const key_type& __k);
944 pair<const_iterator, const_iterator>
945 equal_range(
const key_type& __k)
const;
951 #if __cplusplus >= 201103L
953 _M_move_assign(_Rb_tree&);
967 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
968 typename _Compare,
typename _Alloc>
970 operator==(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
971 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
973 return __x.size() == __y.size()
974 &&
std::equal(__x.begin(), __x.end(), __y.begin());
977 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
978 typename _Compare,
typename _Alloc>
980 operator<(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
981 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
984 __y.begin(), __y.end());
987 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
988 typename _Compare,
typename _Alloc>
990 operator!=(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
991 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
992 {
return !(__x == __y); }
994 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
995 typename _Compare,
typename _Alloc>
997 operator>(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
998 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
999 {
return __y < __x; }
1001 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1002 typename _Compare,
typename _Alloc>
1004 operator<=(const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1005 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1006 {
return !(__y < __x); }
1008 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1009 typename _Compare,
typename _Alloc>
1011 operator>=(
const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1012 const _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1013 {
return !(__x < __y); }
1015 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1016 typename _Compare,
typename _Alloc>
1018 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __x,
1019 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __y)
1022 #if __cplusplus >= 201103L
1023 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1024 typename _Compare,
typename _Alloc>
1025 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1026 _Rb_tree(_Rb_tree&& __x, _Node_allocator&& __a)
1027 : _M_impl(__x._M_impl._M_key_compare, std::move(__a))
1029 using __eq = integral_constant<bool, _Alloc_traits::_S_always_equal()>;
1030 if (__x._M_root() != 0)
1031 _M_move_data(__x, __eq());
1034 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1035 typename _Compare,
typename _Alloc>
1037 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1040 _M_root() = __x._M_root();
1041 _M_leftmost() = __x._M_leftmost();
1042 _M_rightmost() = __x._M_rightmost();
1043 _M_root()->_M_parent = _M_end();
1046 __x._M_leftmost() = __x._M_end();
1047 __x._M_rightmost() = __x._M_end();
1049 this->_M_impl._M_node_count = __x._M_impl._M_node_count;
1050 __x._M_impl._M_node_count = 0;
1053 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1054 typename _Compare,
typename _Alloc>
1056 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1059 if (_M_get_Node_allocator() == __x._M_get_Node_allocator())
1063 _M_root() = _M_copy(__x._M_begin(), _M_end());
1064 _M_leftmost() = _S_minimum(_M_root());
1065 _M_rightmost() = _S_maximum(_M_root());
1066 _M_impl._M_node_count = __x._M_impl._M_node_count;
1070 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1071 typename _Compare,
typename _Alloc>
1073 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1074 _M_move_assign(_Rb_tree& __x)
1076 if (_Alloc_traits::_S_propagate_on_move_assign()
1077 || _Alloc_traits::_S_always_equal()
1078 || _M_get_Node_allocator() == __x._M_get_Node_allocator())
1081 if (__x._M_root() != 0)
1083 std::__alloc_on_move(_M_get_Node_allocator(),
1084 __x._M_get_Node_allocator());
1091 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1092 typename _Compare,
typename _Alloc>
1093 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&
1094 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1095 operator=(
const _Rb_tree& __x)
1101 #if __cplusplus >= 201103L
1102 if (_Alloc_traits::_S_propagate_on_copy_assign())
1104 auto& __this_alloc = this->_M_get_Node_allocator();
1105 auto& __that_alloc = __x._M_get_Node_allocator();
1106 if (!_Alloc_traits::_S_always_equal()
1107 && __this_alloc != __that_alloc)
1109 std::__alloc_on_copy(__this_alloc, __that_alloc);
1113 _M_impl._M_key_compare = __x._M_impl._M_key_compare;
1114 if (__x._M_root() != 0)
1116 _M_root() = _M_copy(__x._M_begin(), _M_end());
1117 _M_leftmost() = _S_minimum(_M_root());
1118 _M_rightmost() = _S_maximum(_M_root());
1119 _M_impl._M_node_count = __x._M_impl._M_node_count;
1125 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1126 typename _Compare,
typename _Alloc>
1127 #if __cplusplus >= 201103L
1128 template<
typename _Arg>
1130 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1131 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1132 #if __cplusplus >= 201103L
1133 _M_insert_(_Base_ptr __x, _Base_ptr __p, _Arg&& __v)
1135 _M_insert_(_Base_ptr __x, _Base_ptr __p,
const _Val& __v)
1138 bool __insert_left = (__x != 0 || __p == _M_end()
1139 || _M_impl._M_key_compare(_KeyOfValue()(__v),
1142 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
1144 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1145 this->_M_impl._M_header);
1146 ++_M_impl._M_node_count;
1147 return iterator(__z);
1150 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1151 typename _Compare,
typename _Alloc>
1152 #if __cplusplus >= 201103L
1153 template<
typename _Arg>
1155 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1156 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1157 #if __cplusplus >= 201103L
1158 _M_insert_lower(_Base_ptr __p, _Arg&& __v)
1160 _M_insert_lower(_Base_ptr __p,
const _Val& __v)
1163 bool __insert_left = (__p == _M_end()
1164 || !_M_impl._M_key_compare(_S_key(__p),
1165 _KeyOfValue()(__v)));
1167 _Link_type __z = _M_create_node(_GLIBCXX_FORWARD(_Arg, __v));
1169 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1170 this->_M_impl._M_header);
1171 ++_M_impl._M_node_count;
1172 return iterator(__z);
1175 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1176 typename _Compare,
typename _Alloc>
1177 #if __cplusplus >= 201103L
1178 template<
typename _Arg>
1180 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1181 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1182 #if __cplusplus >= 201103L
1183 _M_insert_equal_lower(_Arg&& __v)
1185 _M_insert_equal_lower(
const _Val& __v)
1188 _Link_type __x = _M_begin();
1189 _Link_type __y = _M_end();
1193 __x = !_M_impl._M_key_compare(_S_key(__x), _KeyOfValue()(__v)) ?
1194 _S_left(__x) : _S_right(__x);
1196 return _M_insert_lower(__y, _GLIBCXX_FORWARD(_Arg, __v));
1199 template<
typename _Key,
typename _Val,
typename _KoV,
1200 typename _Compare,
typename _Alloc>
1201 typename _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::_Link_type
1202 _Rb_tree<_Key, _Val, _KoV, _Compare, _Alloc>::
1203 _M_copy(_Const_Link_type __x, _Link_type __p)
1206 _Link_type __top = _M_clone_node(__x);
1207 __top->_M_parent = __p;
1212 __top->_M_right = _M_copy(_S_right(__x), __top);
1218 _Link_type __y = _M_clone_node(__x);
1220 __y->_M_parent = __p;
1222 __y->_M_right = _M_copy(_S_right(__x), __y);
1230 __throw_exception_again;
1235 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1236 typename _Compare,
typename _Alloc>
1238 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1239 _M_erase(_Link_type __x)
1244 _M_erase(_S_right(__x));
1245 _Link_type __y = _S_left(__x);
1246 _M_destroy_node(__x);
1251 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1252 typename _Compare,
typename _Alloc>
1253 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1254 _Compare, _Alloc>::iterator
1255 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1256 _M_lower_bound(_Link_type __x, _Link_type __y,
1260 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1261 __y = __x, __x = _S_left(__x);
1263 __x = _S_right(__x);
1264 return iterator(__y);
1267 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1268 typename _Compare,
typename _Alloc>
1269 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1270 _Compare, _Alloc>::const_iterator
1271 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1272 _M_lower_bound(_Const_Link_type __x, _Const_Link_type __y,
1273 const _Key& __k)
const
1276 if (!_M_impl._M_key_compare(_S_key(__x), __k))
1277 __y = __x, __x = _S_left(__x);
1279 __x = _S_right(__x);
1280 return const_iterator(__y);
1283 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1284 typename _Compare,
typename _Alloc>
1285 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1286 _Compare, _Alloc>::iterator
1287 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1288 _M_upper_bound(_Link_type __x, _Link_type __y,
1292 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1293 __y = __x, __x = _S_left(__x);
1295 __x = _S_right(__x);
1296 return iterator(__y);
1299 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1300 typename _Compare,
typename _Alloc>
1301 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1302 _Compare, _Alloc>::const_iterator
1303 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1304 _M_upper_bound(_Const_Link_type __x, _Const_Link_type __y,
1305 const _Key& __k)
const
1308 if (_M_impl._M_key_compare(__k, _S_key(__x)))
1309 __y = __x, __x = _S_left(__x);
1311 __x = _S_right(__x);
1312 return const_iterator(__y);
1315 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1316 typename _Compare,
typename _Alloc>
1317 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1318 _Compare, _Alloc>::iterator,
1319 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1320 _Compare, _Alloc>::iterator>
1321 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1322 equal_range(
const _Key& __k)
1324 _Link_type __x = _M_begin();
1325 _Link_type __y = _M_end();
1328 if (_M_impl._M_key_compare(_S_key(__x), __k))
1329 __x = _S_right(__x);
1330 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1331 __y = __x, __x = _S_left(__x);
1334 _Link_type __xu(__x), __yu(__y);
1335 __y = __x, __x = _S_left(__x);
1336 __xu = _S_right(__xu);
1337 return pair<iterator,
1338 iterator>(_M_lower_bound(__x, __y, __k),
1339 _M_upper_bound(__xu, __yu, __k));
1342 return pair<iterator, iterator>(iterator(__y),
1346 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1347 typename _Compare,
typename _Alloc>
1348 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1349 _Compare, _Alloc>::const_iterator,
1350 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1351 _Compare, _Alloc>::const_iterator>
1352 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1353 equal_range(
const _Key& __k)
const
1355 _Const_Link_type __x = _M_begin();
1356 _Const_Link_type __y = _M_end();
1359 if (_M_impl._M_key_compare(_S_key(__x), __k))
1360 __x = _S_right(__x);
1361 else if (_M_impl._M_key_compare(__k, _S_key(__x)))
1362 __y = __x, __x = _S_left(__x);
1365 _Const_Link_type __xu(__x), __yu(__y);
1366 __y = __x, __x = _S_left(__x);
1367 __xu = _S_right(__xu);
1368 return pair<const_iterator,
1369 const_iterator>(_M_lower_bound(__x, __y, __k),
1370 _M_upper_bound(__xu, __yu, __k));
1373 return pair<const_iterator, const_iterator>(const_iterator(__y),
1374 const_iterator(__y));
1377 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1378 typename _Compare,
typename _Alloc>
1380 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1381 swap(_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>& __t)
1382 #if __cplusplus >= 201103L
1383 noexcept(_Alloc_traits::_S_nothrow_swap())
1388 if (__t._M_root() != 0)
1390 _M_root() = __t._M_root();
1391 _M_leftmost() = __t._M_leftmost();
1392 _M_rightmost() = __t._M_rightmost();
1393 _M_root()->_M_parent = _M_end();
1396 __t._M_leftmost() = __t._M_end();
1397 __t._M_rightmost() = __t._M_end();
1400 else if (__t._M_root() == 0)
1402 __t._M_root() = _M_root();
1403 __t._M_leftmost() = _M_leftmost();
1404 __t._M_rightmost() = _M_rightmost();
1405 __t._M_root()->_M_parent = __t._M_end();
1408 _M_leftmost() = _M_end();
1409 _M_rightmost() = _M_end();
1413 std::swap(_M_root(),__t._M_root());
1414 std::swap(_M_leftmost(),__t._M_leftmost());
1415 std::swap(_M_rightmost(),__t._M_rightmost());
1417 _M_root()->_M_parent = _M_end();
1418 __t._M_root()->_M_parent = __t._M_end();
1421 std::swap(this->_M_impl._M_node_count, __t._M_impl._M_node_count);
1422 std::swap(this->_M_impl._M_key_compare, __t._M_impl._M_key_compare);
1424 _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
1425 __t._M_get_Node_allocator());
1428 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1429 typename _Compare,
typename _Alloc>
1430 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1431 _Compare, _Alloc>::_Base_ptr,
1432 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1433 _Compare, _Alloc>::_Base_ptr>
1434 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1435 _M_get_insert_unique_pos(
const key_type& __k)
1437 typedef pair<_Base_ptr, _Base_ptr> _Res;
1438 _Link_type __x = _M_begin();
1439 _Link_type __y = _M_end();
1444 __comp = _M_impl._M_key_compare(__k, _S_key(__x));
1445 __x = __comp ? _S_left(__x) : _S_right(__x);
1447 iterator __j = iterator(__y);
1451 return _Res(__x, __y);
1455 if (_M_impl._M_key_compare(_S_key(__j._M_node), __k))
1456 return _Res(__x, __y);
1457 return _Res(__j._M_node, 0);
1460 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1461 typename _Compare,
typename _Alloc>
1462 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1463 _Compare, _Alloc>::_Base_ptr,
1464 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1465 _Compare, _Alloc>::_Base_ptr>
1466 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1467 _M_get_insert_equal_pos(
const key_type& __k)
1469 typedef pair<_Base_ptr, _Base_ptr> _Res;
1470 _Link_type __x = _M_begin();
1471 _Link_type __y = _M_end();
1475 __x = _M_impl._M_key_compare(__k, _S_key(__x)) ?
1476 _S_left(__x) : _S_right(__x);
1478 return _Res(__x, __y);
1481 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1482 typename _Compare,
typename _Alloc>
1483 #if __cplusplus >= 201103L
1484 template<
typename _Arg>
1486 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1487 _Compare, _Alloc>::iterator,
bool>
1488 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1489 #if __cplusplus >= 201103L
1490 _M_insert_unique(_Arg&& __v)
1492 _M_insert_unique(
const _Val& __v)
1495 typedef pair<iterator, bool> _Res;
1496 pair<_Base_ptr, _Base_ptr> __res
1497 = _M_get_insert_unique_pos(_KeyOfValue()(__v));
1500 return _Res(_M_insert_(__res.first, __res.second,
1501 _GLIBCXX_FORWARD(_Arg, __v)),
1504 return _Res(iterator(static_cast<_Link_type>(__res.first)),
false);
1507 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1508 typename _Compare,
typename _Alloc>
1509 #if __cplusplus >= 201103L
1510 template<
typename _Arg>
1512 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1513 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1514 #if __cplusplus >= 201103L
1515 _M_insert_equal(_Arg&& __v)
1517 _M_insert_equal(
const _Val& __v)
1520 pair<_Base_ptr, _Base_ptr> __res
1521 = _M_get_insert_equal_pos(_KeyOfValue()(__v));
1522 return _M_insert_(__res.first, __res.second, _GLIBCXX_FORWARD(_Arg, __v));
1525 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1526 typename _Compare,
typename _Alloc>
1527 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1528 _Compare, _Alloc>::_Base_ptr,
1529 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1530 _Compare, _Alloc>::_Base_ptr>
1531 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1532 _M_get_insert_hint_unique_pos(const_iterator __position,
1533 const key_type& __k)
1535 iterator __pos = __position._M_const_cast();
1536 typedef pair<_Base_ptr, _Base_ptr> _Res;
1539 if (__pos._M_node == _M_end())
1542 && _M_impl._M_key_compare(_S_key(_M_rightmost()), __k))
1543 return _Res(0, _M_rightmost());
1545 return _M_get_insert_unique_pos(__k);
1547 else if (_M_impl._M_key_compare(__k, _S_key(__pos._M_node)))
1550 iterator __before = __pos;
1551 if (__pos._M_node == _M_leftmost())
1552 return _Res(_M_leftmost(), _M_leftmost());
1553 else if (_M_impl._M_key_compare(_S_key((--__before)._M_node), __k))
1555 if (_S_right(__before._M_node) == 0)
1556 return _Res(0, __before._M_node);
1558 return _Res(__pos._M_node, __pos._M_node);
1561 return _M_get_insert_unique_pos(__k);
1563 else if (_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
1566 iterator __after = __pos;
1567 if (__pos._M_node == _M_rightmost())
1568 return _Res(0, _M_rightmost());
1569 else if (_M_impl._M_key_compare(__k, _S_key((++__after)._M_node)))
1571 if (_S_right(__pos._M_node) == 0)
1572 return _Res(0, __pos._M_node);
1574 return _Res(__after._M_node, __after._M_node);
1577 return _M_get_insert_unique_pos(__k);
1581 return _Res(__pos._M_node, 0);
1584 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1585 typename _Compare,
typename _Alloc>
1586 #if __cplusplus >= 201103L
1587 template<
typename _Arg>
1589 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1590 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1591 #if __cplusplus >= 201103L
1592 _M_insert_unique_(const_iterator __position, _Arg&& __v)
1594 _M_insert_unique_(const_iterator __position,
const _Val& __v)
1597 pair<_Base_ptr, _Base_ptr> __res
1598 = _M_get_insert_hint_unique_pos(__position, _KeyOfValue()(__v));
1601 return _M_insert_(__res.first, __res.second,
1602 _GLIBCXX_FORWARD(_Arg, __v));
1603 return iterator(static_cast<_Link_type>(__res.first));
1606 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1607 typename _Compare,
typename _Alloc>
1608 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1609 _Compare, _Alloc>::_Base_ptr,
1610 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1611 _Compare, _Alloc>::_Base_ptr>
1612 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1613 _M_get_insert_hint_equal_pos(const_iterator __position,
const key_type& __k)
1615 iterator __pos = __position._M_const_cast();
1616 typedef pair<_Base_ptr, _Base_ptr> _Res;
1619 if (__pos._M_node == _M_end())
1622 && !_M_impl._M_key_compare(__k, _S_key(_M_rightmost())))
1623 return _Res(0, _M_rightmost());
1625 return _M_get_insert_equal_pos(__k);
1627 else if (!_M_impl._M_key_compare(_S_key(__pos._M_node), __k))
1630 iterator __before = __pos;
1631 if (__pos._M_node == _M_leftmost())
1632 return _Res(_M_leftmost(), _M_leftmost());
1633 else if (!_M_impl._M_key_compare(__k, _S_key((--__before)._M_node)))
1635 if (_S_right(__before._M_node) == 0)
1636 return _Res(0, __before._M_node);
1638 return _Res(__pos._M_node, __pos._M_node);
1641 return _M_get_insert_equal_pos(__k);
1646 iterator __after = __pos;
1647 if (__pos._M_node == _M_rightmost())
1648 return _Res(0, _M_rightmost());
1649 else if (!_M_impl._M_key_compare(_S_key((++__after)._M_node), __k))
1651 if (_S_right(__pos._M_node) == 0)
1652 return _Res(0, __pos._M_node);
1654 return _Res(__after._M_node, __after._M_node);
1661 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1662 typename _Compare,
typename _Alloc>
1663 #if __cplusplus >= 201103L
1664 template<
typename _Arg>
1666 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1667 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1668 #if __cplusplus >= 201103L
1669 _M_insert_equal_(const_iterator __position, _Arg&& __v)
1671 _M_insert_equal_(const_iterator __position,
const _Val& __v)
1674 pair<_Base_ptr, _Base_ptr> __res
1675 = _M_get_insert_hint_equal_pos(__position, _KeyOfValue()(__v));
1678 return _M_insert_(__res.first, __res.second,
1679 _GLIBCXX_FORWARD(_Arg, __v));
1681 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg, __v));
1684 #if __cplusplus >= 201103L
1685 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1686 typename _Compare,
typename _Alloc>
1687 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1688 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1689 _M_insert_node(_Base_ptr __x, _Base_ptr __p, _Link_type __z)
1691 bool __insert_left = (__x != 0 || __p == _M_end()
1692 || _M_impl._M_key_compare(_S_key(__z),
1695 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1696 this->_M_impl._M_header);
1697 ++_M_impl._M_node_count;
1698 return iterator(__z);
1701 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1702 typename _Compare,
typename _Alloc>
1703 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1704 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1705 _M_insert_lower_node(_Base_ptr __p, _Link_type __z)
1707 bool __insert_left = (__p == _M_end()
1708 || !_M_impl._M_key_compare(_S_key(__p),
1711 _Rb_tree_insert_and_rebalance(__insert_left, __z, __p,
1712 this->_M_impl._M_header);
1713 ++_M_impl._M_node_count;
1714 return iterator(__z);
1717 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1718 typename _Compare,
typename _Alloc>
1719 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1720 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1721 _M_insert_equal_lower_node(_Link_type __z)
1723 _Link_type __x = _M_begin();
1724 _Link_type __y = _M_end();
1728 __x = !_M_impl._M_key_compare(_S_key(__x), _S_key(__z)) ?
1729 _S_left(__x) : _S_right(__x);
1731 return _M_insert_lower_node(__y, __z);
1734 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1735 typename _Compare,
typename _Alloc>
1736 template<
typename... _Args>
1737 pair<
typename _Rb_tree<_Key, _Val, _KeyOfValue,
1738 _Compare, _Alloc>::iterator,
bool>
1739 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1740 _M_emplace_unique(_Args&&... __args)
1742 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1746 typedef pair<iterator, bool> _Res;
1747 auto __res = _M_get_insert_unique_pos(_S_key(__z));
1749 return _Res(_M_insert_node(__res.first, __res.second, __z),
true);
1751 _M_destroy_node(__z);
1752 return _Res(iterator(static_cast<_Link_type>(__res.first)),
false);
1756 _M_destroy_node(__z);
1757 __throw_exception_again;
1761 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1762 typename _Compare,
typename _Alloc>
1763 template<
typename... _Args>
1764 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1765 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1766 _M_emplace_equal(_Args&&... __args)
1768 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1772 auto __res = _M_get_insert_equal_pos(_S_key(__z));
1773 return _M_insert_node(__res.first, __res.second, __z);
1777 _M_destroy_node(__z);
1778 __throw_exception_again;
1782 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1783 typename _Compare,
typename _Alloc>
1784 template<
typename... _Args>
1785 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1786 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1787 _M_emplace_hint_unique(const_iterator __pos, _Args&&... __args)
1789 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1793 auto __res = _M_get_insert_hint_unique_pos(__pos, _S_key(__z));
1796 return _M_insert_node(__res.first, __res.second, __z);
1798 _M_destroy_node(__z);
1799 return iterator(static_cast<_Link_type>(__res.first));
1803 _M_destroy_node(__z);
1804 __throw_exception_again;
1808 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1809 typename _Compare,
typename _Alloc>
1810 template<
typename... _Args>
1811 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator
1812 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1813 _M_emplace_hint_equal(const_iterator __pos, _Args&&... __args)
1815 _Link_type __z = _M_create_node(std::forward<_Args>(__args)...);
1819 auto __res = _M_get_insert_hint_equal_pos(__pos, _S_key(__z));
1822 return _M_insert_node(__res.first, __res.second, __z);
1824 return _M_insert_equal_lower_node(__z);
1828 _M_destroy_node(__z);
1829 __throw_exception_again;
1834 template<
typename _Key,
typename _Val,
typename _KoV,
1835 typename _Cmp,
typename _Alloc>
1838 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1839 _M_insert_unique(_II __first, _II __last)
1841 for (; __first != __last; ++__first)
1842 _M_insert_unique_(
end(), *__first);
1845 template<
typename _Key,
typename _Val,
typename _KoV,
1846 typename _Cmp,
typename _Alloc>
1849 _Rb_tree<_Key, _Val, _KoV, _Cmp, _Alloc>::
1850 _M_insert_equal(_II __first, _II __last)
1852 for (; __first != __last; ++__first)
1853 _M_insert_equal_(
end(), *__first);
1856 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1857 typename _Compare,
typename _Alloc>
1859 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1860 _M_erase_aux(const_iterator __position)
1863 static_cast<_Link_type
>(_Rb_tree_rebalance_for_erase
1864 (const_cast<_Base_ptr>(__position._M_node),
1865 this->_M_impl._M_header));
1866 _M_destroy_node(__y);
1867 --_M_impl._M_node_count;
1870 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1871 typename _Compare,
typename _Alloc>
1873 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1874 _M_erase_aux(const_iterator __first, const_iterator __last)
1876 if (__first ==
begin() && __last ==
end())
1879 while (__first != __last)
1883 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1884 typename _Compare,
typename _Alloc>
1885 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1886 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1887 erase(
const _Key& __x)
1889 pair<iterator, iterator> __p = equal_range(__x);
1890 const size_type __old_size =
size();
1891 erase(__p.first, __p.second);
1892 return __old_size -
size();
1895 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1896 typename _Compare,
typename _Alloc>
1898 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1899 erase(
const _Key* __first,
const _Key* __last)
1901 while (__first != __last)
1905 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1906 typename _Compare,
typename _Alloc>
1907 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1908 _Compare, _Alloc>::iterator
1909 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1910 find(
const _Key& __k)
1912 iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1913 return (__j ==
end()
1914 || _M_impl._M_key_compare(__k,
1915 _S_key(__j._M_node))) ?
end() : __j;
1918 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1919 typename _Compare,
typename _Alloc>
1920 typename _Rb_tree<_Key, _Val, _KeyOfValue,
1921 _Compare, _Alloc>::const_iterator
1922 _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
1923 find(
const _Key& __k)
const
1925 const_iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
1926 return (__j ==
end()
1927 || _M_impl._M_key_compare(__k,
1928 _S_key(__j._M_node))) ?
end() : __j;
1931 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1932 typename _Compare,
typename _Alloc>
1933 typename _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::size_type
1935 count(
const _Key& __k)
const
1937 pair<const_iterator, const_iterator> __p = equal_range(__k);
1942 _GLIBCXX_PURE
unsigned int
1943 _Rb_tree_black_count(
const _Rb_tree_node_base* __node,
1944 const _Rb_tree_node_base* __root)
throw ();
1946 template<
typename _Key,
typename _Val,
typename _KeyOfValue,
1947 typename _Compare,
typename _Alloc>
1949 _Rb_tree<_Key,_Val,_KeyOfValue,_Compare,_Alloc>::__rb_verify()
const
1951 if (_M_impl._M_node_count == 0 ||
begin() ==
end())
1952 return _M_impl._M_node_count == 0 &&
begin() ==
end()
1953 && this->_M_impl._M_header._M_left == _M_end()
1954 && this->_M_impl._M_header._M_right == _M_end();
1956 unsigned int __len = _Rb_tree_black_count(_M_leftmost(), _M_root());
1957 for (const_iterator __it =
begin(); __it !=
end(); ++__it)
1959 _Const_Link_type __x =
static_cast<_Const_Link_type
>(__it._M_node);
1960 _Const_Link_type __L = _S_left(__x);
1961 _Const_Link_type __R = _S_right(__x);
1963 if (__x->_M_color == _S_red)
1964 if ((__L && __L->_M_color == _S_red)
1965 || (__R && __R->_M_color == _S_red))
1968 if (__L && _M_impl._M_key_compare(_S_key(__x), _S_key(__L)))
1970 if (__R && _M_impl._M_key_compare(_S_key(__R), _S_key(__x)))
1973 if (!__L && !__R && _Rb_tree_black_count(__x, _M_root()) != __len)
1977 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
1979 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
1984 _GLIBCXX_END_NAMESPACE_VERSION
size_t count() const noexcept
Returns the number of bits which are set.
Uniform interface to C++98 and C++0x allocators.
bool lexicographical_compare(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2, _Compare __comp)
Performs dictionary comparison on ranges.
static void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.
static size_type max_size(const _Alloc &__a) noexcept
The maximum supported allocation size.
_Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
static pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
bool equal(_II1 __first1, _II1 __last1, _II2 __first2)
Tests a range for element-wise equality.
constexpr size_t size() const noexcept
Returns the total number of bits.