1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
27 * Copyright (c) 1996,1997
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Hewlett-Packard Company
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Hewlett-Packard Company makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_tree.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{map,set}
61 #pragma GCC system_header
63 #include <bits/stl_algobase.h>
64 #include <bits/allocator.h>
65 #include <bits/stl_function.h>
66 #include <bits/cpp_type_traits.h>
67 #include <ext/alloc_traits.h>
68 #if __cplusplus >= 201103L
69 # include <ext/aligned_buffer.h>
71 #if __cplusplus > 201402L
72 # include <bits/node_handle.h>
75 namespace std
_GLIBCXX_VISIBILITY(default)
77 _GLIBCXX_BEGIN_NAMESPACE_VERSION
79 #if __cplusplus > 201103L
80 # define __cpp_lib_generic_associative_lookup 201304
83 // Red-black tree class, designed for use in implementing STL
84 // associative containers (set, multiset, map, and multimap). The
85 // insertion and deletion algorithms are based on those in Cormen,
86 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
89 // (1) the header cell is maintained with links not only to the root
90 // but also to the leftmost node of the tree, to enable constant
91 // time begin(), and to the rightmost node of the tree, to enable
92 // linear time performance when used with the generic set algorithms
95 // (2) when a node being deleted has two children its successor node
96 // is relinked into its place, rather than copied, so that the only
97 // iterators invalidated are those referring to the deleted node.
99 enum _Rb_tree_color
{ _S_red
= false, _S_black
= true };
101 struct _Rb_tree_node_base
103 typedef _Rb_tree_node_base
* _Base_ptr
;
104 typedef const _Rb_tree_node_base
* _Const_Base_ptr
;
106 _Rb_tree_color _M_color
;
112 _S_minimum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
114 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
118 static _Const_Base_ptr
119 _S_minimum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
121 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
126 _S_maximum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
128 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
132 static _Const_Base_ptr
133 _S_maximum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
135 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
140 // Helper type offering value initialization guarantee on the compare functor.
141 template<typename _Key_compare
>
142 struct _Rb_tree_key_compare
144 _Key_compare _M_key_compare
;
146 _Rb_tree_key_compare()
147 _GLIBCXX_NOEXCEPT_IF(
148 is_nothrow_default_constructible
<_Key_compare
>::value
)
152 _Rb_tree_key_compare(const _Key_compare
& __comp
)
153 : _M_key_compare(__comp
)
156 #if __cplusplus >= 201103L
157 // Copy constructor added for consistency with C++98 mode.
158 _Rb_tree_key_compare(const _Rb_tree_key_compare
&) = default;
160 _Rb_tree_key_compare(_Rb_tree_key_compare
&& __x
)
161 noexcept(is_nothrow_copy_constructible
<_Key_compare
>::value
)
162 : _M_key_compare(__x
._M_key_compare
)
167 // Helper type to manage default initialization of node count and header.
168 struct _Rb_tree_header
170 _Rb_tree_node_base _M_header
;
171 size_t _M_node_count
; // Keeps track of size of tree.
173 _Rb_tree_header() _GLIBCXX_NOEXCEPT
175 _M_header
._M_color
= _S_red
;
179 #if __cplusplus >= 201103L
180 _Rb_tree_header(_Rb_tree_header
&& __x
) noexcept
182 if (__x
._M_header
._M_parent
!= nullptr)
186 _M_header
._M_color
= _S_red
;
193 _M_move_data(_Rb_tree_header
& __from
)
195 _M_header
._M_color
= __from
._M_header
._M_color
;
196 _M_header
._M_parent
= __from
._M_header
._M_parent
;
197 _M_header
._M_left
= __from
._M_header
._M_left
;
198 _M_header
._M_right
= __from
._M_header
._M_right
;
199 _M_header
._M_parent
->_M_parent
= &_M_header
;
200 _M_node_count
= __from
._M_node_count
;
208 _M_header
._M_parent
= 0;
209 _M_header
._M_left
= &_M_header
;
210 _M_header
._M_right
= &_M_header
;
215 template<typename _Val
>
216 struct _Rb_tree_node
: public _Rb_tree_node_base
218 typedef _Rb_tree_node
<_Val
>* _Link_type
;
220 #if __cplusplus < 201103L
225 { return std::__addressof(_M_value_field
); }
229 { return std::__addressof(_M_value_field
); }
231 __gnu_cxx::__aligned_membuf
<_Val
> _M_storage
;
235 { return _M_storage
._M_ptr(); }
239 { return _M_storage
._M_ptr(); }
243 _GLIBCXX_PURE _Rb_tree_node_base
*
244 _Rb_tree_increment(_Rb_tree_node_base
* __x
) throw ();
246 _GLIBCXX_PURE
const _Rb_tree_node_base
*
247 _Rb_tree_increment(const _Rb_tree_node_base
* __x
) throw ();
249 _GLIBCXX_PURE _Rb_tree_node_base
*
250 _Rb_tree_decrement(_Rb_tree_node_base
* __x
) throw ();
252 _GLIBCXX_PURE
const _Rb_tree_node_base
*
253 _Rb_tree_decrement(const _Rb_tree_node_base
* __x
) throw ();
255 template<typename _Tp
>
256 struct _Rb_tree_iterator
258 typedef _Tp value_type
;
259 typedef _Tp
& reference
;
260 typedef _Tp
* pointer
;
262 typedef bidirectional_iterator_tag iterator_category
;
263 typedef ptrdiff_t difference_type
;
265 typedef _Rb_tree_iterator
<_Tp
> _Self
;
266 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr
;
267 typedef _Rb_tree_node
<_Tp
>* _Link_type
;
269 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
273 _Rb_tree_iterator(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
277 operator*() const _GLIBCXX_NOEXCEPT
278 { return *static_cast<_Link_type
>(_M_node
)->_M_valptr(); }
281 operator->() const _GLIBCXX_NOEXCEPT
282 { return static_cast<_Link_type
> (_M_node
)->_M_valptr(); }
285 operator++() _GLIBCXX_NOEXCEPT
287 _M_node
= _Rb_tree_increment(_M_node
);
292 operator++(int) _GLIBCXX_NOEXCEPT
295 _M_node
= _Rb_tree_increment(_M_node
);
300 operator--() _GLIBCXX_NOEXCEPT
302 _M_node
= _Rb_tree_decrement(_M_node
);
307 operator--(int) _GLIBCXX_NOEXCEPT
310 _M_node
= _Rb_tree_decrement(_M_node
);
315 operator==(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
316 { return _M_node
== __x
._M_node
; }
319 operator!=(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
320 { return _M_node
!= __x
._M_node
; }
325 template<typename _Tp
>
326 struct _Rb_tree_const_iterator
328 typedef _Tp value_type
;
329 typedef const _Tp
& reference
;
330 typedef const _Tp
* pointer
;
332 typedef _Rb_tree_iterator
<_Tp
> iterator
;
334 typedef bidirectional_iterator_tag iterator_category
;
335 typedef ptrdiff_t difference_type
;
337 typedef _Rb_tree_const_iterator
<_Tp
> _Self
;
338 typedef _Rb_tree_node_base::_Const_Base_ptr _Base_ptr
;
339 typedef const _Rb_tree_node
<_Tp
>* _Link_type
;
341 _Rb_tree_const_iterator() _GLIBCXX_NOEXCEPT
345 _Rb_tree_const_iterator(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
348 _Rb_tree_const_iterator(const iterator
& __it
) _GLIBCXX_NOEXCEPT
349 : _M_node(__it
._M_node
) { }
352 _M_const_cast() const _GLIBCXX_NOEXCEPT
353 { return iterator(const_cast<typename
iterator::_Base_ptr
>(_M_node
)); }
356 operator*() const _GLIBCXX_NOEXCEPT
357 { return *static_cast<_Link_type
>(_M_node
)->_M_valptr(); }
360 operator->() const _GLIBCXX_NOEXCEPT
361 { return static_cast<_Link_type
>(_M_node
)->_M_valptr(); }
364 operator++() _GLIBCXX_NOEXCEPT
366 _M_node
= _Rb_tree_increment(_M_node
);
371 operator++(int) _GLIBCXX_NOEXCEPT
374 _M_node
= _Rb_tree_increment(_M_node
);
379 operator--() _GLIBCXX_NOEXCEPT
381 _M_node
= _Rb_tree_decrement(_M_node
);
386 operator--(int) _GLIBCXX_NOEXCEPT
389 _M_node
= _Rb_tree_decrement(_M_node
);
394 operator==(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
395 { return _M_node
== __x
._M_node
; }
398 operator!=(const _Self
& __x
) const _GLIBCXX_NOEXCEPT
399 { return _M_node
!= __x
._M_node
; }
404 template<typename _Val
>
406 operator==(const _Rb_tree_iterator
<_Val
>& __x
,
407 const _Rb_tree_const_iterator
<_Val
>& __y
) _GLIBCXX_NOEXCEPT
408 { return __x
._M_node
== __y
._M_node
; }
410 template<typename _Val
>
412 operator!=(const _Rb_tree_iterator
<_Val
>& __x
,
413 const _Rb_tree_const_iterator
<_Val
>& __y
) _GLIBCXX_NOEXCEPT
414 { return __x
._M_node
!= __y
._M_node
; }
417 _Rb_tree_insert_and_rebalance(const bool __insert_left
,
418 _Rb_tree_node_base
* __x
,
419 _Rb_tree_node_base
* __p
,
420 _Rb_tree_node_base
& __header
) throw ();
423 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base
* const __z
,
424 _Rb_tree_node_base
& __header
) throw ();
426 #if __cplusplus > 201103L
427 template<typename _Cmp
, typename _SfinaeType
, typename
= __void_t
<>>
428 struct __has_is_transparent
431 template<typename _Cmp
, typename _SfinaeType
>
432 struct __has_is_transparent
<_Cmp
, _SfinaeType
,
433 __void_t
<typename
_Cmp::is_transparent
>>
434 { typedef void type
; };
437 #if __cplusplus > 201402L
438 template<typename _Tree1
, typename _Cmp2
>
439 struct _Rb_tree_merge_helper
{ };
442 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
443 typename _Compare
, typename _Alloc
= allocator
<_Val
> >
446 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
447 rebind
<_Rb_tree_node
<_Val
> >::other _Node_allocator
;
449 typedef __gnu_cxx::__alloc_traits
<_Node_allocator
> _Alloc_traits
;
452 typedef _Rb_tree_node_base
* _Base_ptr
;
453 typedef const _Rb_tree_node_base
* _Const_Base_ptr
;
454 typedef _Rb_tree_node
<_Val
>* _Link_type
;
455 typedef const _Rb_tree_node
<_Val
>* _Const_Link_type
;
458 // Functor recycling a pool of nodes and using allocation once the pool
460 struct _Reuse_or_alloc_node
462 _Reuse_or_alloc_node(_Rb_tree
& __t
)
463 : _M_root(__t
._M_root()), _M_nodes(__t
._M_rightmost()), _M_t(__t
)
467 _M_root
->_M_parent
= 0;
469 if (_M_nodes
->_M_left
)
470 _M_nodes
= _M_nodes
->_M_left
;
476 #if __cplusplus >= 201103L
477 _Reuse_or_alloc_node(const _Reuse_or_alloc_node
&) = delete;
480 ~_Reuse_or_alloc_node()
481 { _M_t
._M_erase(static_cast<_Link_type
>(_M_root
)); }
483 template<typename _Arg
>
485 #if __cplusplus < 201103L
486 operator()(const _Arg
& __arg
)
488 operator()(_Arg
&& __arg
)
491 _Link_type __node
= static_cast<_Link_type
>(_M_extract());
494 _M_t
._M_destroy_node(__node
);
495 _M_t
._M_construct_node(__node
, _GLIBCXX_FORWARD(_Arg
, __arg
));
499 return _M_t
._M_create_node(_GLIBCXX_FORWARD(_Arg
, __arg
));
509 _Base_ptr __node
= _M_nodes
;
510 _M_nodes
= _M_nodes
->_M_parent
;
513 if (_M_nodes
->_M_right
== __node
)
515 _M_nodes
->_M_right
= 0;
517 if (_M_nodes
->_M_left
)
519 _M_nodes
= _M_nodes
->_M_left
;
521 while (_M_nodes
->_M_right
)
522 _M_nodes
= _M_nodes
->_M_right
;
524 if (_M_nodes
->_M_left
)
525 _M_nodes
= _M_nodes
->_M_left
;
528 else // __node is on the left.
529 _M_nodes
->_M_left
= 0;
542 // Functor similar to the previous one but without any pool of nodes to
546 _Alloc_node(_Rb_tree
& __t
)
549 template<typename _Arg
>
551 #if __cplusplus < 201103L
552 operator()(const _Arg
& __arg
) const
554 operator()(_Arg
&& __arg
) const
556 { return _M_t
._M_create_node(_GLIBCXX_FORWARD(_Arg
, __arg
)); }
563 typedef _Key key_type
;
564 typedef _Val value_type
;
565 typedef value_type
* pointer
;
566 typedef const value_type
* const_pointer
;
567 typedef value_type
& reference
;
568 typedef const value_type
& const_reference
;
569 typedef size_t size_type
;
570 typedef ptrdiff_t difference_type
;
571 typedef _Alloc allocator_type
;
574 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
575 { return *static_cast<_Node_allocator
*>(&this->_M_impl
); }
577 const _Node_allocator
&
578 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
579 { return *static_cast<const _Node_allocator
*>(&this->_M_impl
); }
582 get_allocator() const _GLIBCXX_NOEXCEPT
583 { return allocator_type(_M_get_Node_allocator()); }
588 { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
591 _M_put_node(_Link_type __p
) _GLIBCXX_NOEXCEPT
592 { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p
, 1); }
594 #if __cplusplus < 201103L
596 _M_construct_node(_Link_type __node
, const value_type
& __x
)
599 { get_allocator().construct(__node
->_M_valptr(), __x
); }
603 __throw_exception_again
;
608 _M_create_node(const value_type
& __x
)
610 _Link_type __tmp
= _M_get_node();
611 _M_construct_node(__tmp
, __x
);
616 _M_destroy_node(_Link_type __p
)
617 { get_allocator().destroy(__p
->_M_valptr()); }
619 template<typename
... _Args
>
621 _M_construct_node(_Link_type __node
, _Args
&&... __args
)
625 ::new(__node
) _Rb_tree_node
<_Val
>;
626 _Alloc_traits::construct(_M_get_Node_allocator(),
628 std::forward
<_Args
>(__args
)...);
632 __node
->~_Rb_tree_node
<_Val
>();
634 __throw_exception_again
;
638 template<typename
... _Args
>
640 _M_create_node(_Args
&&... __args
)
642 _Link_type __tmp
= _M_get_node();
643 _M_construct_node(__tmp
, std::forward
<_Args
>(__args
)...);
648 _M_destroy_node(_Link_type __p
) noexcept
650 _Alloc_traits::destroy(_M_get_Node_allocator(), __p
->_M_valptr());
651 __p
->~_Rb_tree_node
<_Val
>();
656 _M_drop_node(_Link_type __p
) _GLIBCXX_NOEXCEPT
658 _M_destroy_node(__p
);
662 template<typename _NodeGen
>
664 _M_clone_node(_Const_Link_type __x
, _NodeGen
& __node_gen
)
666 _Link_type __tmp
= __node_gen(*__x
->_M_valptr());
667 __tmp
->_M_color
= __x
->_M_color
;
674 // Unused _Is_pod_comparator is kept as it is part of mangled name.
675 template<typename _Key_compare
,
676 bool /* _Is_pod_comparator */ = __is_pod(_Key_compare
)>
678 : public _Node_allocator
679 , public _Rb_tree_key_compare
<_Key_compare
>
680 , public _Rb_tree_header
682 typedef _Rb_tree_key_compare
<_Key_compare
> _Base_key_compare
;
684 #if __cplusplus < 201103L
688 _Rb_tree_impl() = default;
689 _Rb_tree_impl(_Rb_tree_impl
&&) = default;
692 _Rb_tree_impl(const _Rb_tree_impl
& __x
)
693 : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x
))
694 , _Base_key_compare(__x
._M_key_compare
)
697 #if __cplusplus < 201103L
698 _Rb_tree_impl(const _Key_compare
& __comp
, const _Node_allocator
& __a
)
699 : _Node_allocator(__a
), _Base_key_compare(__comp
)
702 _Rb_tree_impl(const _Key_compare
& __comp
, _Node_allocator
&& __a
)
703 : _Node_allocator(std::move(__a
)), _Base_key_compare(__comp
)
708 _Rb_tree_impl
<_Compare
> _M_impl
;
712 _M_root() _GLIBCXX_NOEXCEPT
713 { return this->_M_impl
._M_header
._M_parent
; }
716 _M_root() const _GLIBCXX_NOEXCEPT
717 { return this->_M_impl
._M_header
._M_parent
; }
720 _M_leftmost() _GLIBCXX_NOEXCEPT
721 { return this->_M_impl
._M_header
._M_left
; }
724 _M_leftmost() const _GLIBCXX_NOEXCEPT
725 { return this->_M_impl
._M_header
._M_left
; }
728 _M_rightmost() _GLIBCXX_NOEXCEPT
729 { return this->_M_impl
._M_header
._M_right
; }
732 _M_rightmost() const _GLIBCXX_NOEXCEPT
733 { return this->_M_impl
._M_header
._M_right
; }
736 _M_begin() _GLIBCXX_NOEXCEPT
737 { return static_cast<_Link_type
>(this->_M_impl
._M_header
._M_parent
); }
740 _M_begin() const _GLIBCXX_NOEXCEPT
742 return static_cast<_Const_Link_type
>
743 (this->_M_impl
._M_header
._M_parent
);
747 _M_end() _GLIBCXX_NOEXCEPT
748 { return &this->_M_impl
._M_header
; }
751 _M_end() const _GLIBCXX_NOEXCEPT
752 { return &this->_M_impl
._M_header
; }
754 static const_reference
755 _S_value(_Const_Link_type __x
)
756 { return *__x
->_M_valptr(); }
759 _S_key(_Const_Link_type __x
)
760 { return _KeyOfValue()(_S_value(__x
)); }
763 _S_left(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
764 { return static_cast<_Link_type
>(__x
->_M_left
); }
766 static _Const_Link_type
767 _S_left(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
768 { return static_cast<_Const_Link_type
>(__x
->_M_left
); }
771 _S_right(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
772 { return static_cast<_Link_type
>(__x
->_M_right
); }
774 static _Const_Link_type
775 _S_right(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
776 { return static_cast<_Const_Link_type
>(__x
->_M_right
); }
778 static const_reference
779 _S_value(_Const_Base_ptr __x
)
780 { return *static_cast<_Const_Link_type
>(__x
)->_M_valptr(); }
783 _S_key(_Const_Base_ptr __x
)
784 { return _KeyOfValue()(_S_value(__x
)); }
787 _S_minimum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
788 { return _Rb_tree_node_base::_S_minimum(__x
); }
790 static _Const_Base_ptr
791 _S_minimum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
792 { return _Rb_tree_node_base::_S_minimum(__x
); }
795 _S_maximum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
796 { return _Rb_tree_node_base::_S_maximum(__x
); }
798 static _Const_Base_ptr
799 _S_maximum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
800 { return _Rb_tree_node_base::_S_maximum(__x
); }
803 typedef _Rb_tree_iterator
<value_type
> iterator
;
804 typedef _Rb_tree_const_iterator
<value_type
> const_iterator
;
806 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
807 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
809 #if __cplusplus > 201402L
810 using node_type
= _Node_handle
<_Key
, _Val
, _Node_allocator
>;
811 using insert_return_type
= _Node_insert_return
<iterator
, node_type
>;
814 pair
<_Base_ptr
, _Base_ptr
>
815 _M_get_insert_unique_pos(const key_type
& __k
);
817 pair
<_Base_ptr
, _Base_ptr
>
818 _M_get_insert_equal_pos(const key_type
& __k
);
820 pair
<_Base_ptr
, _Base_ptr
>
821 _M_get_insert_hint_unique_pos(const_iterator __pos
,
822 const key_type
& __k
);
824 pair
<_Base_ptr
, _Base_ptr
>
825 _M_get_insert_hint_equal_pos(const_iterator __pos
,
826 const key_type
& __k
);
829 #if __cplusplus >= 201103L
830 template<typename _Arg
, typename _NodeGen
>
832 _M_insert_(_Base_ptr __x
, _Base_ptr __y
, _Arg
&& __v
, _NodeGen
&);
835 _M_insert_node(_Base_ptr __x
, _Base_ptr __y
, _Link_type __z
);
837 template<typename _Arg
>
839 _M_insert_lower(_Base_ptr __y
, _Arg
&& __v
);
841 template<typename _Arg
>
843 _M_insert_equal_lower(_Arg
&& __x
);
846 _M_insert_lower_node(_Base_ptr __p
, _Link_type __z
);
849 _M_insert_equal_lower_node(_Link_type __z
);
851 template<typename _NodeGen
>
853 _M_insert_(_Base_ptr __x
, _Base_ptr __y
,
854 const value_type
& __v
, _NodeGen
&);
856 // _GLIBCXX_RESOLVE_LIB_DEFECTS
857 // 233. Insertion hints in associative containers.
859 _M_insert_lower(_Base_ptr __y
, const value_type
& __v
);
862 _M_insert_equal_lower(const value_type
& __x
);
865 template<typename _NodeGen
>
867 _M_copy(_Const_Link_type __x
, _Base_ptr __p
, _NodeGen
&);
869 template<typename _NodeGen
>
871 _M_copy(const _Rb_tree
& __x
, _NodeGen
& __gen
)
873 _Link_type __root
= _M_copy(__x
._M_begin(), _M_end(), __gen
);
874 _M_leftmost() = _S_minimum(__root
);
875 _M_rightmost() = _S_maximum(__root
);
876 _M_impl
._M_node_count
= __x
._M_impl
._M_node_count
;
881 _M_copy(const _Rb_tree
& __x
)
883 _Alloc_node
__an(*this);
884 return _M_copy(__x
, __an
);
888 _M_erase(_Link_type __x
);
891 _M_lower_bound(_Link_type __x
, _Base_ptr __y
,
895 _M_lower_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
896 const _Key
& __k
) const;
899 _M_upper_bound(_Link_type __x
, _Base_ptr __y
,
903 _M_upper_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
904 const _Key
& __k
) const;
907 // allocation/deallocation
908 #if __cplusplus < 201103L
911 _Rb_tree() = default;
914 _Rb_tree(const _Compare
& __comp
,
915 const allocator_type
& __a
= allocator_type())
916 : _M_impl(__comp
, _Node_allocator(__a
)) { }
918 _Rb_tree(const _Rb_tree
& __x
)
919 : _M_impl(__x
._M_impl
)
921 if (__x
._M_root() != 0)
922 _M_root() = _M_copy(__x
);
925 #if __cplusplus >= 201103L
926 _Rb_tree(const allocator_type
& __a
)
927 : _M_impl(_Compare(), _Node_allocator(__a
))
930 _Rb_tree(const _Rb_tree
& __x
, const allocator_type
& __a
)
931 : _M_impl(__x
._M_impl
._M_key_compare
, _Node_allocator(__a
))
933 if (__x
._M_root() != nullptr)
934 _M_root() = _M_copy(__x
);
937 _Rb_tree(_Rb_tree
&&) = default;
939 _Rb_tree(_Rb_tree
&& __x
, const allocator_type
& __a
)
940 : _Rb_tree(std::move(__x
), _Node_allocator(__a
))
943 _Rb_tree(_Rb_tree
&& __x
, _Node_allocator
&& __a
);
946 ~_Rb_tree() _GLIBCXX_NOEXCEPT
947 { _M_erase(_M_begin()); }
950 operator=(const _Rb_tree
& __x
);
955 { return _M_impl
._M_key_compare
; }
958 begin() _GLIBCXX_NOEXCEPT
959 { return iterator(this->_M_impl
._M_header
._M_left
); }
962 begin() const _GLIBCXX_NOEXCEPT
963 { return const_iterator(this->_M_impl
._M_header
._M_left
); }
966 end() _GLIBCXX_NOEXCEPT
967 { return iterator(&this->_M_impl
._M_header
); }
970 end() const _GLIBCXX_NOEXCEPT
971 { return const_iterator(&this->_M_impl
._M_header
); }
974 rbegin() _GLIBCXX_NOEXCEPT
975 { return reverse_iterator(end()); }
977 const_reverse_iterator
978 rbegin() const _GLIBCXX_NOEXCEPT
979 { return const_reverse_iterator(end()); }
982 rend() _GLIBCXX_NOEXCEPT
983 { return reverse_iterator(begin()); }
985 const_reverse_iterator
986 rend() const _GLIBCXX_NOEXCEPT
987 { return const_reverse_iterator(begin()); }
990 empty() const _GLIBCXX_NOEXCEPT
991 { return _M_impl
._M_node_count
== 0; }
994 size() const _GLIBCXX_NOEXCEPT
995 { return _M_impl
._M_node_count
; }
998 max_size() const _GLIBCXX_NOEXCEPT
999 { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
1003 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Compare
>::value
);
1006 #if __cplusplus >= 201103L
1007 template<typename _Arg
>
1008 pair
<iterator
, bool>
1009 _M_insert_unique(_Arg
&& __x
);
1011 template<typename _Arg
>
1013 _M_insert_equal(_Arg
&& __x
);
1015 template<typename _Arg
, typename _NodeGen
>
1017 _M_insert_unique_(const_iterator __pos
, _Arg
&& __x
, _NodeGen
&);
1019 template<typename _Arg
>
1021 _M_insert_unique_(const_iterator __pos
, _Arg
&& __x
)
1023 _Alloc_node
__an(*this);
1024 return _M_insert_unique_(__pos
, std::forward
<_Arg
>(__x
), __an
);
1027 template<typename _Arg
, typename _NodeGen
>
1029 _M_insert_equal_(const_iterator __pos
, _Arg
&& __x
, _NodeGen
&);
1031 template<typename _Arg
>
1033 _M_insert_equal_(const_iterator __pos
, _Arg
&& __x
)
1035 _Alloc_node
__an(*this);
1036 return _M_insert_equal_(__pos
, std::forward
<_Arg
>(__x
), __an
);
1039 template<typename
... _Args
>
1040 pair
<iterator
, bool>
1041 _M_emplace_unique(_Args
&&... __args
);
1043 template<typename
... _Args
>
1045 _M_emplace_equal(_Args
&&... __args
);
1047 template<typename
... _Args
>
1049 _M_emplace_hint_unique(const_iterator __pos
, _Args
&&... __args
);
1051 template<typename
... _Args
>
1053 _M_emplace_hint_equal(const_iterator __pos
, _Args
&&... __args
);
1055 pair
<iterator
, bool>
1056 _M_insert_unique(const value_type
& __x
);
1059 _M_insert_equal(const value_type
& __x
);
1061 template<typename _NodeGen
>
1063 _M_insert_unique_(const_iterator __pos
, const value_type
& __x
,
1067 _M_insert_unique_(const_iterator __pos
, const value_type
& __x
)
1069 _Alloc_node
__an(*this);
1070 return _M_insert_unique_(__pos
, __x
, __an
);
1073 template<typename _NodeGen
>
1075 _M_insert_equal_(const_iterator __pos
, const value_type
& __x
,
1078 _M_insert_equal_(const_iterator __pos
, const value_type
& __x
)
1080 _Alloc_node
__an(*this);
1081 return _M_insert_equal_(__pos
, __x
, __an
);
1085 template<typename _InputIterator
>
1087 _M_insert_unique(_InputIterator __first
, _InputIterator __last
);
1089 template<typename _InputIterator
>
1091 _M_insert_equal(_InputIterator __first
, _InputIterator __last
);
1095 _M_erase_aux(const_iterator __position
);
1098 _M_erase_aux(const_iterator __first
, const_iterator __last
);
1101 #if __cplusplus >= 201103L
1102 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1103 // DR 130. Associative erase should return an iterator.
1104 _GLIBCXX_ABI_TAG_CXX11
1106 erase(const_iterator __position
)
1108 __glibcxx_assert(__position
!= end());
1109 const_iterator __result
= __position
;
1111 _M_erase_aux(__position
);
1112 return __result
._M_const_cast();
1116 _GLIBCXX_ABI_TAG_CXX11
1118 erase(iterator __position
)
1120 __glibcxx_assert(__position
!= end());
1121 iterator __result
= __position
;
1123 _M_erase_aux(__position
);
1128 erase(iterator __position
)
1130 __glibcxx_assert(__position
!= end());
1131 _M_erase_aux(__position
);
1135 erase(const_iterator __position
)
1137 __glibcxx_assert(__position
!= end());
1138 _M_erase_aux(__position
);
1142 erase(const key_type
& __x
);
1144 #if __cplusplus >= 201103L
1145 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1146 // DR 130. Associative erase should return an iterator.
1147 _GLIBCXX_ABI_TAG_CXX11
1149 erase(const_iterator __first
, const_iterator __last
)
1151 _M_erase_aux(__first
, __last
);
1152 return __last
._M_const_cast();
1156 erase(iterator __first
, iterator __last
)
1157 { _M_erase_aux(__first
, __last
); }
1160 erase(const_iterator __first
, const_iterator __last
)
1161 { _M_erase_aux(__first
, __last
); }
1164 erase(const key_type
* __first
, const key_type
* __last
);
1167 clear() _GLIBCXX_NOEXCEPT
1169 _M_erase(_M_begin());
1175 find(const key_type
& __k
);
1178 find(const key_type
& __k
) const;
1181 count(const key_type
& __k
) const;
1184 lower_bound(const key_type
& __k
)
1185 { return _M_lower_bound(_M_begin(), _M_end(), __k
); }
1188 lower_bound(const key_type
& __k
) const
1189 { return _M_lower_bound(_M_begin(), _M_end(), __k
); }
1192 upper_bound(const key_type
& __k
)
1193 { return _M_upper_bound(_M_begin(), _M_end(), __k
); }
1196 upper_bound(const key_type
& __k
) const
1197 { return _M_upper_bound(_M_begin(), _M_end(), __k
); }
1199 pair
<iterator
, iterator
>
1200 equal_range(const key_type
& __k
);
1202 pair
<const_iterator
, const_iterator
>
1203 equal_range(const key_type
& __k
) const;
1205 #if __cplusplus > 201103L
1206 template<typename _Kt
,
1208 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1210 _M_find_tr(const _Kt
& __k
)
1212 const _Rb_tree
* __const_this
= this;
1213 return __const_this
->_M_find_tr(__k
)._M_const_cast();
1216 template<typename _Kt
,
1218 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1220 _M_find_tr(const _Kt
& __k
) const
1222 auto __j
= _M_lower_bound_tr(__k
);
1223 if (__j
!= end() && _M_impl
._M_key_compare(__k
, _S_key(__j
._M_node
)))
1228 template<typename _Kt
,
1230 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1232 _M_count_tr(const _Kt
& __k
) const
1234 auto __p
= _M_equal_range_tr(__k
);
1235 return std::distance(__p
.first
, __p
.second
);
1238 template<typename _Kt
,
1240 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1242 _M_lower_bound_tr(const _Kt
& __k
)
1244 const _Rb_tree
* __const_this
= this;
1245 return __const_this
->_M_lower_bound_tr(__k
)._M_const_cast();
1248 template<typename _Kt
,
1250 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1252 _M_lower_bound_tr(const _Kt
& __k
) const
1254 auto __x
= _M_begin();
1255 auto __y
= _M_end();
1257 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
1263 __x
= _S_right(__x
);
1264 return const_iterator(__y
);
1267 template<typename _Kt
,
1269 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1271 _M_upper_bound_tr(const _Kt
& __k
)
1273 const _Rb_tree
* __const_this
= this;
1274 return __const_this
->_M_upper_bound_tr(__k
)._M_const_cast();
1277 template<typename _Kt
,
1279 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1281 _M_upper_bound_tr(const _Kt
& __k
) const
1283 auto __x
= _M_begin();
1284 auto __y
= _M_end();
1286 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1292 __x
= _S_right(__x
);
1293 return const_iterator(__y
);
1296 template<typename _Kt
,
1298 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1299 pair
<iterator
, iterator
>
1300 _M_equal_range_tr(const _Kt
& __k
)
1302 const _Rb_tree
* __const_this
= this;
1303 auto __ret
= __const_this
->_M_equal_range_tr(__k
);
1304 return { __ret
.first
._M_const_cast(), __ret
.second
._M_const_cast() };
1307 template<typename _Kt
,
1309 typename __has_is_transparent
<_Compare
, _Kt
>::type
>
1310 pair
<const_iterator
, const_iterator
>
1311 _M_equal_range_tr(const _Kt
& __k
) const
1313 auto __low
= _M_lower_bound_tr(__k
);
1314 auto __high
= __low
;
1315 auto& __cmp
= _M_impl
._M_key_compare
;
1316 while (__high
!= end() && !__cmp(__k
, _S_key(__high
._M_node
)))
1318 return { __low
, __high
};
1324 __rb_verify() const;
1326 #if __cplusplus >= 201103L
1328 operator=(_Rb_tree
&&)
1329 noexcept(_Alloc_traits::_S_nothrow_move()
1330 && is_nothrow_move_assignable
<_Compare
>::value
);
1332 template<typename _Iterator
>
1334 _M_assign_unique(_Iterator
, _Iterator
);
1336 template<typename _Iterator
>
1338 _M_assign_equal(_Iterator
, _Iterator
);
1341 // Move elements from container with equal allocator.
1343 _M_move_data(_Rb_tree
& __x
, std::true_type
)
1344 { _M_impl
._M_move_data(__x
._M_impl
); }
1346 // Move elements from container with possibly non-equal allocator,
1347 // which might result in a copy not a move.
1349 _M_move_data(_Rb_tree
&, std::false_type
);
1351 // Move assignment from container with equal allocator.
1353 _M_move_assign(_Rb_tree
&, std::true_type
);
1355 // Move assignment from container with possibly non-equal allocator,
1356 // which might result in a copy not a move.
1358 _M_move_assign(_Rb_tree
&, std::false_type
);
1361 #if __cplusplus > 201402L
1363 /// Re-insert an extracted node.
1365 _M_reinsert_node_unique(node_type
&& __nh
)
1367 insert_return_type __ret
;
1369 __ret
.position
= end();
1372 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1374 auto __res
= _M_get_insert_unique_pos(__nh
._M_key());
1378 = _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1379 __nh
._M_ptr
= nullptr;
1380 __ret
.inserted
= true;
1384 __ret
.node
= std::move(__nh
);
1385 __ret
.position
= iterator(__res
.first
);
1386 __ret
.inserted
= false;
1392 /// Re-insert an extracted node.
1394 _M_reinsert_node_equal(node_type
&& __nh
)
1401 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1402 auto __res
= _M_get_insert_equal_pos(__nh
._M_key());
1404 __ret
= _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1406 __ret
= _M_insert_equal_lower_node(__nh
._M_ptr
);
1407 __nh
._M_ptr
= nullptr;
1412 /// Re-insert an extracted node.
1414 _M_reinsert_node_hint_unique(const_iterator __hint
, node_type
&& __nh
)
1421 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1422 auto __res
= _M_get_insert_hint_unique_pos(__hint
, __nh
._M_key());
1425 __ret
= _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1426 __nh
._M_ptr
= nullptr;
1429 __ret
= iterator(__res
.first
);
1434 /// Re-insert an extracted node.
1436 _M_reinsert_node_hint_equal(const_iterator __hint
, node_type
&& __nh
)
1443 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1444 auto __res
= _M_get_insert_hint_equal_pos(__hint
, __nh
._M_key());
1446 __ret
= _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1448 __ret
= _M_insert_equal_lower_node(__nh
._M_ptr
);
1449 __nh
._M_ptr
= nullptr;
1456 extract(const_iterator __pos
)
1458 auto __ptr
= _Rb_tree_rebalance_for_erase(
1459 __pos
._M_const_cast()._M_node
, _M_impl
._M_header
);
1460 --_M_impl
._M_node_count
;
1461 return { static_cast<_Link_type
>(__ptr
), _M_get_Node_allocator() };
1466 extract(const key_type
& __k
)
1469 auto __pos
= find(__k
);
1471 __nh
= extract(const_iterator(__pos
));
1475 template<typename _Compare2
>
1476 using _Compatible_tree
1477 = _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare2
, _Alloc
>;
1479 template<typename
, typename
>
1480 friend class _Rb_tree_merge_helper
;
1482 /// Merge from a compatible container into one with unique keys.
1483 template<typename _Compare2
>
1485 _M_merge_unique(_Compatible_tree
<_Compare2
>& __src
) noexcept
1487 using _Merge_helper
= _Rb_tree_merge_helper
<_Rb_tree
, _Compare2
>;
1488 for (auto __i
= __src
.begin(), __end
= __src
.end(); __i
!= __end
;)
1491 auto __res
= _M_get_insert_unique_pos(_KeyOfValue()(*__pos
));
1494 auto& __src_impl
= _Merge_helper::_S_get_impl(__src
);
1495 auto __ptr
= _Rb_tree_rebalance_for_erase(
1496 __pos
._M_node
, __src_impl
._M_header
);
1497 --__src_impl
._M_node_count
;
1498 _M_insert_node(__res
.first
, __res
.second
,
1499 static_cast<_Link_type
>(__ptr
));
1504 /// Merge from a compatible container into one with equivalent keys.
1505 template<typename _Compare2
>
1507 _M_merge_equal(_Compatible_tree
<_Compare2
>& __src
) noexcept
1509 using _Merge_helper
= _Rb_tree_merge_helper
<_Rb_tree
, _Compare2
>;
1510 for (auto __i
= __src
.begin(), __end
= __src
.end(); __i
!= __end
;)
1513 auto __res
= _M_get_insert_equal_pos(_KeyOfValue()(*__pos
));
1516 auto& __src_impl
= _Merge_helper::_S_get_impl(__src
);
1517 auto __ptr
= _Rb_tree_rebalance_for_erase(
1518 __pos
._M_node
, __src_impl
._M_header
);
1519 --__src_impl
._M_node_count
;
1520 _M_insert_node(__res
.first
, __res
.second
,
1521 static_cast<_Link_type
>(__ptr
));
1528 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1529 typename _Compare
, typename _Alloc
>
1531 operator==(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1532 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1534 return __x
.size() == __y
.size()
1535 && std::equal(__x
.begin(), __x
.end(), __y
.begin());
1538 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1539 typename _Compare
, typename _Alloc
>
1541 operator<(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1542 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1544 return std::lexicographical_compare(__x
.begin(), __x
.end(),
1545 __y
.begin(), __y
.end());
1548 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1549 typename _Compare
, typename _Alloc
>
1551 operator!=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1552 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1553 { return !(__x
== __y
); }
1555 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1556 typename _Compare
, typename _Alloc
>
1558 operator>(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1559 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1560 { return __y
< __x
; }
1562 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1563 typename _Compare
, typename _Alloc
>
1565 operator<=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1566 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1567 { return !(__y
< __x
); }
1569 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1570 typename _Compare
, typename _Alloc
>
1572 operator>=(const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1573 const _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1574 { return !(__x
< __y
); }
1576 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1577 typename _Compare
, typename _Alloc
>
1579 swap(_Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1580 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1583 #if __cplusplus >= 201103L
1584 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1585 typename _Compare
, typename _Alloc
>
1586 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1587 _Rb_tree(_Rb_tree
&& __x
, _Node_allocator
&& __a
)
1588 : _M_impl(__x
._M_impl
._M_key_compare
, std::move(__a
))
1590 using __eq
= typename
_Alloc_traits::is_always_equal
;
1591 if (__x
._M_root() != nullptr)
1592 _M_move_data(__x
, __eq());
1595 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1596 typename _Compare
, typename _Alloc
>
1598 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1599 _M_move_data(_Rb_tree
& __x
, std::false_type
)
1601 if (_M_get_Node_allocator() == __x
._M_get_Node_allocator())
1602 _M_move_data(__x
, std::true_type());
1605 _Alloc_node
__an(*this);
1607 [&__an
](const value_type
& __cval
)
1609 auto& __val
= const_cast<value_type
&>(__cval
);
1610 return __an(std::move_if_noexcept(__val
));
1612 _M_root() = _M_copy(__x
, __lbd
);
1616 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1617 typename _Compare
, typename _Alloc
>
1619 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1620 _M_move_assign(_Rb_tree
& __x
, true_type
)
1623 if (__x
._M_root() != nullptr)
1624 _M_move_data(__x
, std::true_type());
1625 std::__alloc_on_move(_M_get_Node_allocator(),
1626 __x
._M_get_Node_allocator());
1629 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1630 typename _Compare
, typename _Alloc
>
1632 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1633 _M_move_assign(_Rb_tree
& __x
, false_type
)
1635 if (_M_get_Node_allocator() == __x
._M_get_Node_allocator())
1636 return _M_move_assign(__x
, true_type
{});
1638 // Try to move each node reusing existing nodes and copying __x nodes
1640 _Reuse_or_alloc_node
__roan(*this);
1642 if (__x
._M_root() != nullptr)
1645 [&__roan
](const value_type
& __cval
)
1647 auto& __val
= const_cast<value_type
&>(__cval
);
1648 return __roan(std::move_if_noexcept(__val
));
1650 _M_root() = _M_copy(__x
, __lbd
);
1655 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1656 typename _Compare
, typename _Alloc
>
1657 inline _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>&
1658 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1659 operator=(_Rb_tree
&& __x
)
1660 noexcept(_Alloc_traits::_S_nothrow_move()
1661 && is_nothrow_move_assignable
<_Compare
>::value
)
1663 _M_impl
._M_key_compare
= std::move(__x
._M_impl
._M_key_compare
);
1664 _M_move_assign(__x
, __bool_constant
<_Alloc_traits::_S_nothrow_move()>());
1668 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1669 typename _Compare
, typename _Alloc
>
1670 template<typename _Iterator
>
1672 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1673 _M_assign_unique(_Iterator __first
, _Iterator __last
)
1675 _Reuse_or_alloc_node
__roan(*this);
1677 for (; __first
!= __last
; ++__first
)
1678 _M_insert_unique_(end(), *__first
, __roan
);
1681 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1682 typename _Compare
, typename _Alloc
>
1683 template<typename _Iterator
>
1685 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1686 _M_assign_equal(_Iterator __first
, _Iterator __last
)
1688 _Reuse_or_alloc_node
__roan(*this);
1690 for (; __first
!= __last
; ++__first
)
1691 _M_insert_equal_(end(), *__first
, __roan
);
1695 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1696 typename _Compare
, typename _Alloc
>
1697 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>&
1698 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1699 operator=(const _Rb_tree
& __x
)
1703 // Note that _Key may be a constant type.
1704 #if __cplusplus >= 201103L
1705 if (_Alloc_traits::_S_propagate_on_copy_assign())
1707 auto& __this_alloc
= this->_M_get_Node_allocator();
1708 auto& __that_alloc
= __x
._M_get_Node_allocator();
1709 if (!_Alloc_traits::_S_always_equal()
1710 && __this_alloc
!= __that_alloc
)
1712 // Replacement allocator cannot free existing storage, we need
1713 // to erase nodes first.
1715 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
1720 _Reuse_or_alloc_node
__roan(*this);
1722 _M_impl
._M_key_compare
= __x
._M_impl
._M_key_compare
;
1723 if (__x
._M_root() != 0)
1724 _M_root() = _M_copy(__x
, __roan
);
1730 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1731 typename _Compare
, typename _Alloc
>
1732 #if __cplusplus >= 201103L
1733 template<typename _Arg
, typename _NodeGen
>
1735 template<typename _NodeGen
>
1737 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1738 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1739 _M_insert_(_Base_ptr __x
, _Base_ptr __p
,
1740 #if __cplusplus >= 201103L
1745 _NodeGen
& __node_gen
)
1747 bool __insert_left
= (__x
!= 0 || __p
== _M_end()
1748 || _M_impl
._M_key_compare(_KeyOfValue()(__v
),
1751 _Link_type __z
= __node_gen(_GLIBCXX_FORWARD(_Arg
, __v
));
1753 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
1754 this->_M_impl
._M_header
);
1755 ++_M_impl
._M_node_count
;
1756 return iterator(__z
);
1759 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1760 typename _Compare
, typename _Alloc
>
1761 #if __cplusplus >= 201103L
1762 template<typename _Arg
>
1764 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1765 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1766 #if __cplusplus >= 201103L
1767 _M_insert_lower(_Base_ptr __p
, _Arg
&& __v
)
1769 _M_insert_lower(_Base_ptr __p
, const _Val
& __v
)
1772 bool __insert_left
= (__p
== _M_end()
1773 || !_M_impl
._M_key_compare(_S_key(__p
),
1774 _KeyOfValue()(__v
)));
1776 _Link_type __z
= _M_create_node(_GLIBCXX_FORWARD(_Arg
, __v
));
1778 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
1779 this->_M_impl
._M_header
);
1780 ++_M_impl
._M_node_count
;
1781 return iterator(__z
);
1784 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1785 typename _Compare
, typename _Alloc
>
1786 #if __cplusplus >= 201103L
1787 template<typename _Arg
>
1789 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1790 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1791 #if __cplusplus >= 201103L
1792 _M_insert_equal_lower(_Arg
&& __v
)
1794 _M_insert_equal_lower(const _Val
& __v
)
1797 _Link_type __x
= _M_begin();
1798 _Base_ptr __y
= _M_end();
1802 __x
= !_M_impl
._M_key_compare(_S_key(__x
), _KeyOfValue()(__v
)) ?
1803 _S_left(__x
) : _S_right(__x
);
1805 return _M_insert_lower(__y
, _GLIBCXX_FORWARD(_Arg
, __v
));
1808 template<typename _Key
, typename _Val
, typename _KoV
,
1809 typename _Compare
, typename _Alloc
>
1810 template<typename _NodeGen
>
1811 typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::_Link_type
1812 _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::
1813 _M_copy(_Const_Link_type __x
, _Base_ptr __p
, _NodeGen
& __node_gen
)
1815 // Structural copy. __x and __p must be non-null.
1816 _Link_type __top
= _M_clone_node(__x
, __node_gen
);
1817 __top
->_M_parent
= __p
;
1822 __top
->_M_right
= _M_copy(_S_right(__x
), __top
, __node_gen
);
1828 _Link_type __y
= _M_clone_node(__x
, __node_gen
);
1830 __y
->_M_parent
= __p
;
1832 __y
->_M_right
= _M_copy(_S_right(__x
), __y
, __node_gen
);
1840 __throw_exception_again
;
1845 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1846 typename _Compare
, typename _Alloc
>
1848 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1849 _M_erase(_Link_type __x
)
1851 // Erase without rebalancing.
1854 _M_erase(_S_right(__x
));
1855 _Link_type __y
= _S_left(__x
);
1861 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1862 typename _Compare
, typename _Alloc
>
1863 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1864 _Compare
, _Alloc
>::iterator
1865 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1866 _M_lower_bound(_Link_type __x
, _Base_ptr __y
,
1870 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
1871 __y
= __x
, __x
= _S_left(__x
);
1873 __x
= _S_right(__x
);
1874 return iterator(__y
);
1877 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1878 typename _Compare
, typename _Alloc
>
1879 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1880 _Compare
, _Alloc
>::const_iterator
1881 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1882 _M_lower_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
1883 const _Key
& __k
) const
1886 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
1887 __y
= __x
, __x
= _S_left(__x
);
1889 __x
= _S_right(__x
);
1890 return const_iterator(__y
);
1893 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1894 typename _Compare
, typename _Alloc
>
1895 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1896 _Compare
, _Alloc
>::iterator
1897 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1898 _M_upper_bound(_Link_type __x
, _Base_ptr __y
,
1902 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1903 __y
= __x
, __x
= _S_left(__x
);
1905 __x
= _S_right(__x
);
1906 return iterator(__y
);
1909 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1910 typename _Compare
, typename _Alloc
>
1911 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1912 _Compare
, _Alloc
>::const_iterator
1913 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1914 _M_upper_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
1915 const _Key
& __k
) const
1918 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1919 __y
= __x
, __x
= _S_left(__x
);
1921 __x
= _S_right(__x
);
1922 return const_iterator(__y
);
1925 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1926 typename _Compare
, typename _Alloc
>
1927 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1928 _Compare
, _Alloc
>::iterator
,
1929 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1930 _Compare
, _Alloc
>::iterator
>
1931 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1932 equal_range(const _Key
& __k
)
1934 _Link_type __x
= _M_begin();
1935 _Base_ptr __y
= _M_end();
1938 if (_M_impl
._M_key_compare(_S_key(__x
), __k
))
1939 __x
= _S_right(__x
);
1940 else if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1941 __y
= __x
, __x
= _S_left(__x
);
1944 _Link_type
__xu(__x
);
1945 _Base_ptr
__yu(__y
);
1946 __y
= __x
, __x
= _S_left(__x
);
1947 __xu
= _S_right(__xu
);
1948 return pair
<iterator
,
1949 iterator
>(_M_lower_bound(__x
, __y
, __k
),
1950 _M_upper_bound(__xu
, __yu
, __k
));
1953 return pair
<iterator
, iterator
>(iterator(__y
),
1957 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1958 typename _Compare
, typename _Alloc
>
1959 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1960 _Compare
, _Alloc
>::const_iterator
,
1961 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1962 _Compare
, _Alloc
>::const_iterator
>
1963 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1964 equal_range(const _Key
& __k
) const
1966 _Const_Link_type __x
= _M_begin();
1967 _Const_Base_ptr __y
= _M_end();
1970 if (_M_impl
._M_key_compare(_S_key(__x
), __k
))
1971 __x
= _S_right(__x
);
1972 else if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1973 __y
= __x
, __x
= _S_left(__x
);
1976 _Const_Link_type
__xu(__x
);
1977 _Const_Base_ptr
__yu(__y
);
1978 __y
= __x
, __x
= _S_left(__x
);
1979 __xu
= _S_right(__xu
);
1980 return pair
<const_iterator
,
1981 const_iterator
>(_M_lower_bound(__x
, __y
, __k
),
1982 _M_upper_bound(__xu
, __yu
, __k
));
1985 return pair
<const_iterator
, const_iterator
>(const_iterator(__y
),
1986 const_iterator(__y
));
1989 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1990 typename _Compare
, typename _Alloc
>
1992 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1994 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Compare
>::value
)
1998 if (__t
._M_root() != 0)
1999 _M_impl
._M_move_data(__t
._M_impl
);
2001 else if (__t
._M_root() == 0)
2002 __t
._M_impl
._M_move_data(_M_impl
);
2005 std::swap(_M_root(),__t
._M_root());
2006 std::swap(_M_leftmost(),__t
._M_leftmost());
2007 std::swap(_M_rightmost(),__t
._M_rightmost());
2009 _M_root()->_M_parent
= _M_end();
2010 __t
._M_root()->_M_parent
= __t
._M_end();
2011 std::swap(this->_M_impl
._M_node_count
, __t
._M_impl
._M_node_count
);
2013 // No need to swap header's color as it does not change.
2014 std::swap(this->_M_impl
._M_key_compare
, __t
._M_impl
._M_key_compare
);
2016 _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2017 __t
._M_get_Node_allocator());
2020 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2021 typename _Compare
, typename _Alloc
>
2022 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2023 _Compare
, _Alloc
>::_Base_ptr
,
2024 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2025 _Compare
, _Alloc
>::_Base_ptr
>
2026 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2027 _M_get_insert_unique_pos(const key_type
& __k
)
2029 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2030 _Link_type __x
= _M_begin();
2031 _Base_ptr __y
= _M_end();
2036 __comp
= _M_impl
._M_key_compare(__k
, _S_key(__x
));
2037 __x
= __comp
? _S_left(__x
) : _S_right(__x
);
2039 iterator __j
= iterator(__y
);
2043 return _Res(__x
, __y
);
2047 if (_M_impl
._M_key_compare(_S_key(__j
._M_node
), __k
))
2048 return _Res(__x
, __y
);
2049 return _Res(__j
._M_node
, 0);
2052 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2053 typename _Compare
, typename _Alloc
>
2054 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2055 _Compare
, _Alloc
>::_Base_ptr
,
2056 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2057 _Compare
, _Alloc
>::_Base_ptr
>
2058 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2059 _M_get_insert_equal_pos(const key_type
& __k
)
2061 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2062 _Link_type __x
= _M_begin();
2063 _Base_ptr __y
= _M_end();
2067 __x
= _M_impl
._M_key_compare(__k
, _S_key(__x
)) ?
2068 _S_left(__x
) : _S_right(__x
);
2070 return _Res(__x
, __y
);
2073 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2074 typename _Compare
, typename _Alloc
>
2075 #if __cplusplus >= 201103L
2076 template<typename _Arg
>
2078 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2079 _Compare
, _Alloc
>::iterator
, bool>
2080 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2081 #if __cplusplus >= 201103L
2082 _M_insert_unique(_Arg
&& __v
)
2084 _M_insert_unique(const _Val
& __v
)
2087 typedef pair
<iterator
, bool> _Res
;
2088 pair
<_Base_ptr
, _Base_ptr
> __res
2089 = _M_get_insert_unique_pos(_KeyOfValue()(__v
));
2093 _Alloc_node
__an(*this);
2094 return _Res(_M_insert_(__res
.first
, __res
.second
,
2095 _GLIBCXX_FORWARD(_Arg
, __v
), __an
),
2099 return _Res(iterator(__res
.first
), false);
2102 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2103 typename _Compare
, typename _Alloc
>
2104 #if __cplusplus >= 201103L
2105 template<typename _Arg
>
2107 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2108 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2109 #if __cplusplus >= 201103L
2110 _M_insert_equal(_Arg
&& __v
)
2112 _M_insert_equal(const _Val
& __v
)
2115 pair
<_Base_ptr
, _Base_ptr
> __res
2116 = _M_get_insert_equal_pos(_KeyOfValue()(__v
));
2117 _Alloc_node
__an(*this);
2118 return _M_insert_(__res
.first
, __res
.second
,
2119 _GLIBCXX_FORWARD(_Arg
, __v
), __an
);
2122 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2123 typename _Compare
, typename _Alloc
>
2124 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2125 _Compare
, _Alloc
>::_Base_ptr
,
2126 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2127 _Compare
, _Alloc
>::_Base_ptr
>
2128 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2129 _M_get_insert_hint_unique_pos(const_iterator __position
,
2130 const key_type
& __k
)
2132 iterator __pos
= __position
._M_const_cast();
2133 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2136 if (__pos
._M_node
== _M_end())
2139 && _M_impl
._M_key_compare(_S_key(_M_rightmost()), __k
))
2140 return _Res(0, _M_rightmost());
2142 return _M_get_insert_unique_pos(__k
);
2144 else if (_M_impl
._M_key_compare(__k
, _S_key(__pos
._M_node
)))
2146 // First, try before...
2147 iterator __before
= __pos
;
2148 if (__pos
._M_node
== _M_leftmost()) // begin()
2149 return _Res(_M_leftmost(), _M_leftmost());
2150 else if (_M_impl
._M_key_compare(_S_key((--__before
)._M_node
), __k
))
2152 if (_S_right(__before
._M_node
) == 0)
2153 return _Res(0, __before
._M_node
);
2155 return _Res(__pos
._M_node
, __pos
._M_node
);
2158 return _M_get_insert_unique_pos(__k
);
2160 else if (_M_impl
._M_key_compare(_S_key(__pos
._M_node
), __k
))
2162 // ... then try after.
2163 iterator __after
= __pos
;
2164 if (__pos
._M_node
== _M_rightmost())
2165 return _Res(0, _M_rightmost());
2166 else if (_M_impl
._M_key_compare(__k
, _S_key((++__after
)._M_node
)))
2168 if (_S_right(__pos
._M_node
) == 0)
2169 return _Res(0, __pos
._M_node
);
2171 return _Res(__after
._M_node
, __after
._M_node
);
2174 return _M_get_insert_unique_pos(__k
);
2178 return _Res(__pos
._M_node
, 0);
2181 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2182 typename _Compare
, typename _Alloc
>
2183 #if __cplusplus >= 201103L
2184 template<typename _Arg
, typename _NodeGen
>
2186 template<typename _NodeGen
>
2188 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2189 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2190 _M_insert_unique_(const_iterator __position
,
2191 #if __cplusplus >= 201103L
2196 _NodeGen
& __node_gen
)
2198 pair
<_Base_ptr
, _Base_ptr
> __res
2199 = _M_get_insert_hint_unique_pos(__position
, _KeyOfValue()(__v
));
2202 return _M_insert_(__res
.first
, __res
.second
,
2203 _GLIBCXX_FORWARD(_Arg
, __v
),
2205 return iterator(__res
.first
);
2208 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2209 typename _Compare
, typename _Alloc
>
2210 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2211 _Compare
, _Alloc
>::_Base_ptr
,
2212 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2213 _Compare
, _Alloc
>::_Base_ptr
>
2214 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2215 _M_get_insert_hint_equal_pos(const_iterator __position
, const key_type
& __k
)
2217 iterator __pos
= __position
._M_const_cast();
2218 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2221 if (__pos
._M_node
== _M_end())
2224 && !_M_impl
._M_key_compare(__k
, _S_key(_M_rightmost())))
2225 return _Res(0, _M_rightmost());
2227 return _M_get_insert_equal_pos(__k
);
2229 else if (!_M_impl
._M_key_compare(_S_key(__pos
._M_node
), __k
))
2231 // First, try before...
2232 iterator __before
= __pos
;
2233 if (__pos
._M_node
== _M_leftmost()) // begin()
2234 return _Res(_M_leftmost(), _M_leftmost());
2235 else if (!_M_impl
._M_key_compare(__k
, _S_key((--__before
)._M_node
)))
2237 if (_S_right(__before
._M_node
) == 0)
2238 return _Res(0, __before
._M_node
);
2240 return _Res(__pos
._M_node
, __pos
._M_node
);
2243 return _M_get_insert_equal_pos(__k
);
2247 // ... then try after.
2248 iterator __after
= __pos
;
2249 if (__pos
._M_node
== _M_rightmost())
2250 return _Res(0, _M_rightmost());
2251 else if (!_M_impl
._M_key_compare(_S_key((++__after
)._M_node
), __k
))
2253 if (_S_right(__pos
._M_node
) == 0)
2254 return _Res(0, __pos
._M_node
);
2256 return _Res(__after
._M_node
, __after
._M_node
);
2263 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2264 typename _Compare
, typename _Alloc
>
2265 #if __cplusplus >= 201103L
2266 template<typename _Arg
, typename _NodeGen
>
2268 template<typename _NodeGen
>
2270 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2271 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2272 _M_insert_equal_(const_iterator __position
,
2273 #if __cplusplus >= 201103L
2278 _NodeGen
& __node_gen
)
2280 pair
<_Base_ptr
, _Base_ptr
> __res
2281 = _M_get_insert_hint_equal_pos(__position
, _KeyOfValue()(__v
));
2284 return _M_insert_(__res
.first
, __res
.second
,
2285 _GLIBCXX_FORWARD(_Arg
, __v
),
2288 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg
, __v
));
2291 #if __cplusplus >= 201103L
2292 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2293 typename _Compare
, typename _Alloc
>
2294 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2295 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2296 _M_insert_node(_Base_ptr __x
, _Base_ptr __p
, _Link_type __z
)
2298 bool __insert_left
= (__x
!= 0 || __p
== _M_end()
2299 || _M_impl
._M_key_compare(_S_key(__z
),
2302 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
2303 this->_M_impl
._M_header
);
2304 ++_M_impl
._M_node_count
;
2305 return iterator(__z
);
2308 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2309 typename _Compare
, typename _Alloc
>
2310 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2311 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2312 _M_insert_lower_node(_Base_ptr __p
, _Link_type __z
)
2314 bool __insert_left
= (__p
== _M_end()
2315 || !_M_impl
._M_key_compare(_S_key(__p
),
2318 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
2319 this->_M_impl
._M_header
);
2320 ++_M_impl
._M_node_count
;
2321 return iterator(__z
);
2324 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2325 typename _Compare
, typename _Alloc
>
2326 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2327 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2328 _M_insert_equal_lower_node(_Link_type __z
)
2330 _Link_type __x
= _M_begin();
2331 _Base_ptr __y
= _M_end();
2335 __x
= !_M_impl
._M_key_compare(_S_key(__x
), _S_key(__z
)) ?
2336 _S_left(__x
) : _S_right(__x
);
2338 return _M_insert_lower_node(__y
, __z
);
2341 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2342 typename _Compare
, typename _Alloc
>
2343 template<typename
... _Args
>
2344 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2345 _Compare
, _Alloc
>::iterator
, bool>
2346 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2347 _M_emplace_unique(_Args
&&... __args
)
2349 _Link_type __z
= _M_create_node(std::forward
<_Args
>(__args
)...);
2353 typedef pair
<iterator
, bool> _Res
;
2354 auto __res
= _M_get_insert_unique_pos(_S_key(__z
));
2356 return _Res(_M_insert_node(__res
.first
, __res
.second
, __z
), true);
2359 return _Res(iterator(__res
.first
), false);
2364 __throw_exception_again
;
2368 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2369 typename _Compare
, typename _Alloc
>
2370 template<typename
... _Args
>
2371 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2372 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2373 _M_emplace_equal(_Args
&&... __args
)
2375 _Link_type __z
= _M_create_node(std::forward
<_Args
>(__args
)...);
2379 auto __res
= _M_get_insert_equal_pos(_S_key(__z
));
2380 return _M_insert_node(__res
.first
, __res
.second
, __z
);
2385 __throw_exception_again
;
2389 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2390 typename _Compare
, typename _Alloc
>
2391 template<typename
... _Args
>
2392 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2393 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2394 _M_emplace_hint_unique(const_iterator __pos
, _Args
&&... __args
)
2396 _Link_type __z
= _M_create_node(std::forward
<_Args
>(__args
)...);
2400 auto __res
= _M_get_insert_hint_unique_pos(__pos
, _S_key(__z
));
2403 return _M_insert_node(__res
.first
, __res
.second
, __z
);
2406 return iterator(__res
.first
);
2411 __throw_exception_again
;
2415 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2416 typename _Compare
, typename _Alloc
>
2417 template<typename
... _Args
>
2418 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2419 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2420 _M_emplace_hint_equal(const_iterator __pos
, _Args
&&... __args
)
2422 _Link_type __z
= _M_create_node(std::forward
<_Args
>(__args
)...);
2426 auto __res
= _M_get_insert_hint_equal_pos(__pos
, _S_key(__z
));
2429 return _M_insert_node(__res
.first
, __res
.second
, __z
);
2431 return _M_insert_equal_lower_node(__z
);
2436 __throw_exception_again
;
2441 template<typename _Key
, typename _Val
, typename _KoV
,
2442 typename _Cmp
, typename _Alloc
>
2445 _Rb_tree
<_Key
, _Val
, _KoV
, _Cmp
, _Alloc
>::
2446 _M_insert_unique(_II __first
, _II __last
)
2448 _Alloc_node
__an(*this);
2449 for (; __first
!= __last
; ++__first
)
2450 _M_insert_unique_(end(), *__first
, __an
);
2453 template<typename _Key
, typename _Val
, typename _KoV
,
2454 typename _Cmp
, typename _Alloc
>
2457 _Rb_tree
<_Key
, _Val
, _KoV
, _Cmp
, _Alloc
>::
2458 _M_insert_equal(_II __first
, _II __last
)
2460 _Alloc_node
__an(*this);
2461 for (; __first
!= __last
; ++__first
)
2462 _M_insert_equal_(end(), *__first
, __an
);
2465 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2466 typename _Compare
, typename _Alloc
>
2468 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2469 _M_erase_aux(const_iterator __position
)
2472 static_cast<_Link_type
>(_Rb_tree_rebalance_for_erase
2473 (const_cast<_Base_ptr
>(__position
._M_node
),
2474 this->_M_impl
._M_header
));
2476 --_M_impl
._M_node_count
;
2479 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2480 typename _Compare
, typename _Alloc
>
2482 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2483 _M_erase_aux(const_iterator __first
, const_iterator __last
)
2485 if (__first
== begin() && __last
== end())
2488 while (__first
!= __last
)
2489 _M_erase_aux(__first
++);
2492 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2493 typename _Compare
, typename _Alloc
>
2494 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::size_type
2495 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2496 erase(const _Key
& __x
)
2498 pair
<iterator
, iterator
> __p
= equal_range(__x
);
2499 const size_type __old_size
= size();
2500 _M_erase_aux(__p
.first
, __p
.second
);
2501 return __old_size
- size();
2504 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2505 typename _Compare
, typename _Alloc
>
2507 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2508 erase(const _Key
* __first
, const _Key
* __last
)
2510 while (__first
!= __last
)
2514 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2515 typename _Compare
, typename _Alloc
>
2516 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2517 _Compare
, _Alloc
>::iterator
2518 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2519 find(const _Key
& __k
)
2521 iterator __j
= _M_lower_bound(_M_begin(), _M_end(), __k
);
2522 return (__j
== end()
2523 || _M_impl
._M_key_compare(__k
,
2524 _S_key(__j
._M_node
))) ? end() : __j
;
2527 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2528 typename _Compare
, typename _Alloc
>
2529 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2530 _Compare
, _Alloc
>::const_iterator
2531 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2532 find(const _Key
& __k
) const
2534 const_iterator __j
= _M_lower_bound(_M_begin(), _M_end(), __k
);
2535 return (__j
== end()
2536 || _M_impl
._M_key_compare(__k
,
2537 _S_key(__j
._M_node
))) ? end() : __j
;
2540 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2541 typename _Compare
, typename _Alloc
>
2542 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::size_type
2543 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2544 count(const _Key
& __k
) const
2546 pair
<const_iterator
, const_iterator
> __p
= equal_range(__k
);
2547 const size_type __n
= std::distance(__p
.first
, __p
.second
);
2551 _GLIBCXX_PURE
unsigned int
2552 _Rb_tree_black_count(const _Rb_tree_node_base
* __node
,
2553 const _Rb_tree_node_base
* __root
) throw ();
2555 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2556 typename _Compare
, typename _Alloc
>
2558 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::__rb_verify() const
2560 if (_M_impl
._M_node_count
== 0 || begin() == end())
2561 return _M_impl
._M_node_count
== 0 && begin() == end()
2562 && this->_M_impl
._M_header
._M_left
== _M_end()
2563 && this->_M_impl
._M_header
._M_right
== _M_end();
2565 unsigned int __len
= _Rb_tree_black_count(_M_leftmost(), _M_root());
2566 for (const_iterator __it
= begin(); __it
!= end(); ++__it
)
2568 _Const_Link_type __x
= static_cast<_Const_Link_type
>(__it
._M_node
);
2569 _Const_Link_type __L
= _S_left(__x
);
2570 _Const_Link_type __R
= _S_right(__x
);
2572 if (__x
->_M_color
== _S_red
)
2573 if ((__L
&& __L
->_M_color
== _S_red
)
2574 || (__R
&& __R
->_M_color
== _S_red
))
2577 if (__L
&& _M_impl
._M_key_compare(_S_key(__x
), _S_key(__L
)))
2579 if (__R
&& _M_impl
._M_key_compare(_S_key(__R
), _S_key(__x
)))
2582 if (!__L
&& !__R
&& _Rb_tree_black_count(__x
, _M_root()) != __len
)
2586 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
2588 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
2593 #if __cplusplus > 201402L
2594 // Allow access to internals of compatible _Rb_tree specializations.
2595 template<typename _Key
, typename _Val
, typename _Sel
, typename _Cmp1
,
2596 typename _Alloc
, typename _Cmp2
>
2597 struct _Rb_tree_merge_helper
<_Rb_tree
<_Key
, _Val
, _Sel
, _Cmp1
, _Alloc
>,
2601 friend class _Rb_tree
<_Key
, _Val
, _Sel
, _Cmp1
, _Alloc
>;
2604 _S_get_impl(_Rb_tree
<_Key
, _Val
, _Sel
, _Cmp2
, _Alloc
>& __tree
)
2605 { return __tree
._M_impl
; }
2609 _GLIBCXX_END_NAMESPACE_VERSION