1 // RB tree implementation -*- C++ -*-
3 // Copyright (C) 2001-2024 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 #ifdef _GLIBCXX_SYSHDR
62 #pragma GCC system_header
65 #include <bits/stl_algobase.h>
66 #include <bits/allocator.h>
67 #include <bits/stl_function.h>
68 #include <bits/cpp_type_traits.h>
69 #include <ext/alloc_traits.h>
70 #if __cplusplus >= 201103L
71 # include <ext/aligned_buffer.h>
73 #if __cplusplus > 201402L
74 # include <bits/node_handle.h>
77 namespace std
_GLIBCXX_VISIBILITY(default)
79 _GLIBCXX_BEGIN_NAMESPACE_VERSION
81 // Red-black tree class, designed for use in implementing STL
82 // associative containers (set, multiset, map, and multimap). The
83 // insertion and deletion algorithms are based on those in Cormen,
84 // Leiserson, and Rivest, Introduction to Algorithms (MIT Press,
87 // (1) the header cell is maintained with links not only to the root
88 // but also to the leftmost node of the tree, to enable constant
89 // time begin(), and to the rightmost node of the tree, to enable
90 // linear time performance when used with the generic set algorithms
93 // (2) when a node being deleted has two children its successor node
94 // is relinked into its place, rather than copied, so that the only
95 // iterators invalidated are those referring to the deleted node.
97 enum _Rb_tree_color
{ _S_red
= false, _S_black
= true };
99 struct _Rb_tree_node_base
101 typedef _Rb_tree_node_base
* _Base_ptr
;
102 typedef const _Rb_tree_node_base
* _Const_Base_ptr
;
104 _Rb_tree_color _M_color
;
110 _S_minimum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
112 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
116 static _Const_Base_ptr
117 _S_minimum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
119 while (__x
->_M_left
!= 0) __x
= __x
->_M_left
;
124 _S_maximum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
126 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
130 static _Const_Base_ptr
131 _S_maximum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
133 while (__x
->_M_right
!= 0) __x
= __x
->_M_right
;
138 // Helper type offering value initialization guarantee on the compare functor.
139 template<typename _Key_compare
>
140 struct _Rb_tree_key_compare
142 _Key_compare _M_key_compare
;
144 _Rb_tree_key_compare()
145 _GLIBCXX_NOEXCEPT_IF(
146 is_nothrow_default_constructible
<_Key_compare
>::value
)
150 _Rb_tree_key_compare(const _Key_compare
& __comp
)
151 : _M_key_compare(__comp
)
154 #if __cplusplus >= 201103L
155 // Copy constructor added for consistency with C++98 mode.
156 _Rb_tree_key_compare(const _Rb_tree_key_compare
&) = default;
158 _Rb_tree_key_compare(_Rb_tree_key_compare
&& __x
)
159 noexcept(is_nothrow_copy_constructible
<_Key_compare
>::value
)
160 : _M_key_compare(__x
._M_key_compare
)
165 // Helper type to manage default initialization of node count and header.
166 struct _Rb_tree_header
168 _Rb_tree_node_base _M_header
;
169 size_t _M_node_count
; // Keeps track of size of tree.
171 _Rb_tree_header() _GLIBCXX_NOEXCEPT
173 _M_header
._M_color
= _S_red
;
177 #if __cplusplus >= 201103L
178 _Rb_tree_header(_Rb_tree_header
&& __x
) noexcept
180 if (__x
._M_header
._M_parent
!= nullptr)
184 _M_header
._M_color
= _S_red
;
191 _M_move_data(_Rb_tree_header
& __from
)
193 _M_header
._M_color
= __from
._M_header
._M_color
;
194 _M_header
._M_parent
= __from
._M_header
._M_parent
;
195 _M_header
._M_left
= __from
._M_header
._M_left
;
196 _M_header
._M_right
= __from
._M_header
._M_right
;
197 _M_header
._M_parent
->_M_parent
= &_M_header
;
198 _M_node_count
= __from
._M_node_count
;
206 _M_header
._M_parent
= 0;
207 _M_header
._M_left
= &_M_header
;
208 _M_header
._M_right
= &_M_header
;
213 template<typename _Val
>
214 struct _Rb_tree_node
: public _Rb_tree_node_base
216 typedef _Rb_tree_node
<_Val
>* _Link_type
;
218 #if __cplusplus < 201103L
223 { return std::__addressof(_M_value_field
); }
227 { return std::__addressof(_M_value_field
); }
229 __gnu_cxx::__aligned_membuf
<_Val
> _M_storage
;
233 { return _M_storage
._M_ptr(); }
237 { return _M_storage
._M_ptr(); }
241 _GLIBCXX_PURE _Rb_tree_node_base
*
242 _Rb_tree_increment(_Rb_tree_node_base
* __x
) throw ();
244 _GLIBCXX_PURE
const _Rb_tree_node_base
*
245 _Rb_tree_increment(const _Rb_tree_node_base
* __x
) throw ();
247 _GLIBCXX_PURE _Rb_tree_node_base
*
248 _Rb_tree_decrement(_Rb_tree_node_base
* __x
) throw ();
250 _GLIBCXX_PURE
const _Rb_tree_node_base
*
251 _Rb_tree_decrement(const _Rb_tree_node_base
* __x
) throw ();
253 template<typename _Tp
>
254 struct _Rb_tree_iterator
256 typedef _Tp value_type
;
257 typedef _Tp
& reference
;
258 typedef _Tp
* pointer
;
260 typedef bidirectional_iterator_tag iterator_category
;
261 typedef ptrdiff_t difference_type
;
263 typedef _Rb_tree_iterator
<_Tp
> _Self
;
264 typedef _Rb_tree_node_base::_Base_ptr _Base_ptr
;
265 typedef _Rb_tree_node
<_Tp
>* _Link_type
;
267 _Rb_tree_iterator() _GLIBCXX_NOEXCEPT
271 _Rb_tree_iterator(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
275 operator*() const _GLIBCXX_NOEXCEPT
276 { return *static_cast<_Link_type
>(_M_node
)->_M_valptr(); }
279 operator->() const _GLIBCXX_NOEXCEPT
280 { return static_cast<_Link_type
> (_M_node
)->_M_valptr(); }
283 operator++() _GLIBCXX_NOEXCEPT
285 _M_node
= _Rb_tree_increment(_M_node
);
290 operator++(int) _GLIBCXX_NOEXCEPT
293 _M_node
= _Rb_tree_increment(_M_node
);
298 operator--() _GLIBCXX_NOEXCEPT
300 _M_node
= _Rb_tree_decrement(_M_node
);
305 operator--(int) _GLIBCXX_NOEXCEPT
308 _M_node
= _Rb_tree_decrement(_M_node
);
313 operator==(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
314 { return __x
._M_node
== __y
._M_node
; }
316 #if ! __cpp_lib_three_way_comparison
318 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
319 { return __x
._M_node
!= __y
._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 _Self
& __y
) _GLIBCXX_NOEXCEPT
395 { return __x
._M_node
== __y
._M_node
; }
397 #if ! __cpp_lib_three_way_comparison
399 operator!=(const _Self
& __x
, const _Self
& __y
) _GLIBCXX_NOEXCEPT
400 { return __x
._M_node
!= __y
._M_node
; }
406 __attribute__((__nonnull__
))
408 _Rb_tree_insert_and_rebalance(const bool __insert_left
,
409 _Rb_tree_node_base
* __x
,
410 _Rb_tree_node_base
* __p
,
411 _Rb_tree_node_base
& __header
) throw ();
413 __attribute__((__nonnull__
,__returns_nonnull__
))
415 _Rb_tree_rebalance_for_erase(_Rb_tree_node_base
* const __z
,
416 _Rb_tree_node_base
& __header
) throw ();
418 #if __cplusplus > 201402L
419 template<typename _Tree1
, typename _Cmp2
>
420 struct _Rb_tree_merge_helper
{ };
423 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
424 typename _Compare
, typename _Alloc
= allocator
<_Val
> >
427 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
428 rebind
<_Rb_tree_node
<_Val
> >::other _Node_allocator
;
430 typedef __gnu_cxx::__alloc_traits
<_Node_allocator
> _Alloc_traits
;
433 typedef _Rb_tree_node_base
* _Base_ptr
;
434 typedef const _Rb_tree_node_base
* _Const_Base_ptr
;
435 typedef _Rb_tree_node
<_Val
>* _Link_type
;
436 typedef const _Rb_tree_node
<_Val
>* _Const_Link_type
;
439 // Functor recycling a pool of nodes and using allocation once the pool
441 struct _Reuse_or_alloc_node
443 _Reuse_or_alloc_node(_Rb_tree
& __t
)
444 : _M_root(__t
._M_root()), _M_nodes(__t
._M_rightmost()), _M_t(__t
)
448 _M_root
->_M_parent
= 0;
450 if (_M_nodes
->_M_left
)
451 _M_nodes
= _M_nodes
->_M_left
;
457 #if __cplusplus >= 201103L
458 _Reuse_or_alloc_node(const _Reuse_or_alloc_node
&) = delete;
461 ~_Reuse_or_alloc_node()
462 { _M_t
._M_erase(static_cast<_Link_type
>(_M_root
)); }
464 template<typename _Arg
>
466 operator()(_GLIBCXX_FWDREF(_Arg
) __arg
)
468 _Link_type __node
= static_cast<_Link_type
>(_M_extract());
471 _M_t
._M_destroy_node(__node
);
472 _M_t
._M_construct_node(__node
, _GLIBCXX_FORWARD(_Arg
, __arg
));
476 return _M_t
._M_create_node(_GLIBCXX_FORWARD(_Arg
, __arg
));
486 _Base_ptr __node
= _M_nodes
;
487 _M_nodes
= _M_nodes
->_M_parent
;
490 if (_M_nodes
->_M_right
== __node
)
492 _M_nodes
->_M_right
= 0;
494 if (_M_nodes
->_M_left
)
496 _M_nodes
= _M_nodes
->_M_left
;
498 while (_M_nodes
->_M_right
)
499 _M_nodes
= _M_nodes
->_M_right
;
501 if (_M_nodes
->_M_left
)
502 _M_nodes
= _M_nodes
->_M_left
;
505 else // __node is on the left.
506 _M_nodes
->_M_left
= 0;
519 // Functor similar to the previous one but without any pool of nodes to
523 _Alloc_node(_Rb_tree
& __t
)
526 template<typename _Arg
>
528 operator()(_GLIBCXX_FWDREF(_Arg
) __arg
) const
529 { return _M_t
._M_create_node(_GLIBCXX_FORWARD(_Arg
, __arg
)); }
536 typedef _Key key_type
;
537 typedef _Val value_type
;
538 typedef value_type
* pointer
;
539 typedef const value_type
* const_pointer
;
540 typedef value_type
& reference
;
541 typedef const value_type
& const_reference
;
542 typedef size_t size_type
;
543 typedef ptrdiff_t difference_type
;
544 typedef _Alloc allocator_type
;
547 _M_get_Node_allocator() _GLIBCXX_NOEXCEPT
548 { return this->_M_impl
; }
550 const _Node_allocator
&
551 _M_get_Node_allocator() const _GLIBCXX_NOEXCEPT
552 { return this->_M_impl
; }
555 get_allocator() const _GLIBCXX_NOEXCEPT
556 { return allocator_type(_M_get_Node_allocator()); }
561 { return _Alloc_traits::allocate(_M_get_Node_allocator(), 1); }
564 _M_put_node(_Link_type __p
) _GLIBCXX_NOEXCEPT
565 { _Alloc_traits::deallocate(_M_get_Node_allocator(), __p
, 1); }
567 #if __cplusplus < 201103L
569 _M_construct_node(_Link_type __node
, const value_type
& __x
)
572 { get_allocator().construct(__node
->_M_valptr(), __x
); }
576 __throw_exception_again
;
581 _M_create_node(const value_type
& __x
)
583 _Link_type __tmp
= _M_get_node();
584 _M_construct_node(__tmp
, __x
);
588 template<typename
... _Args
>
590 _M_construct_node(_Link_type __node
, _Args
&&... __args
)
594 ::new(__node
) _Rb_tree_node
<_Val
>;
595 _Alloc_traits::construct(_M_get_Node_allocator(),
597 std::forward
<_Args
>(__args
)...);
601 __node
->~_Rb_tree_node
<_Val
>();
603 __throw_exception_again
;
607 template<typename
... _Args
>
609 _M_create_node(_Args
&&... __args
)
611 _Link_type __tmp
= _M_get_node();
612 _M_construct_node(__tmp
, std::forward
<_Args
>(__args
)...);
618 _M_destroy_node(_Link_type __p
) _GLIBCXX_NOEXCEPT
620 #if __cplusplus < 201103L
621 get_allocator().destroy(__p
->_M_valptr());
623 _Alloc_traits::destroy(_M_get_Node_allocator(), __p
->_M_valptr());
624 __p
->~_Rb_tree_node
<_Val
>();
629 _M_drop_node(_Link_type __p
) _GLIBCXX_NOEXCEPT
631 _M_destroy_node(__p
);
635 template<bool _MoveValue
, typename _NodeGen
>
637 _M_clone_node(_Link_type __x
, _NodeGen
& __node_gen
)
639 #if __cplusplus >= 201103L
640 using _Vp
= __conditional_t
<_MoveValue
,
645 = __node_gen(_GLIBCXX_FORWARD(_Vp
, *__x
->_M_valptr()));
646 __tmp
->_M_color
= __x
->_M_color
;
653 #if _GLIBCXX_INLINE_VERSION
654 template<typename _Key_compare
>
656 // Unused _Is_pod_comparator is kept as it is part of mangled name.
657 template<typename _Key_compare
,
658 bool /* _Is_pod_comparator */ = __is_pod(_Key_compare
)>
661 : public _Node_allocator
662 , public _Rb_tree_key_compare
<_Key_compare
>
663 , public _Rb_tree_header
665 typedef _Rb_tree_key_compare
<_Key_compare
> _Base_key_compare
;
668 _GLIBCXX_NOEXCEPT_IF(
669 is_nothrow_default_constructible
<_Node_allocator
>::value
670 && is_nothrow_default_constructible
<_Base_key_compare
>::value
)
674 _Rb_tree_impl(const _Rb_tree_impl
& __x
)
675 : _Node_allocator(_Alloc_traits::_S_select_on_copy(__x
))
676 , _Base_key_compare(__x
._M_key_compare
)
680 #if __cplusplus < 201103L
681 _Rb_tree_impl(const _Key_compare
& __comp
, const _Node_allocator
& __a
)
682 : _Node_allocator(__a
), _Base_key_compare(__comp
)
685 _Rb_tree_impl(_Rb_tree_impl
&&)
686 noexcept( is_nothrow_move_constructible
<_Base_key_compare
>::value
)
690 _Rb_tree_impl(_Node_allocator
&& __a
)
691 : _Node_allocator(std::move(__a
))
694 _Rb_tree_impl(_Rb_tree_impl
&& __x
, _Node_allocator
&& __a
)
695 : _Node_allocator(std::move(__a
)),
696 _Base_key_compare(std::move(__x
)),
697 _Rb_tree_header(std::move(__x
))
700 _Rb_tree_impl(const _Key_compare
& __comp
, _Node_allocator
&& __a
)
701 : _Node_allocator(std::move(__a
)), _Base_key_compare(__comp
)
706 _Rb_tree_impl
<_Compare
> _M_impl
;
710 _M_root() _GLIBCXX_NOEXCEPT
711 { return this->_M_impl
._M_header
._M_parent
; }
714 _M_root() const _GLIBCXX_NOEXCEPT
715 { return this->_M_impl
._M_header
._M_parent
; }
718 _M_leftmost() _GLIBCXX_NOEXCEPT
719 { return this->_M_impl
._M_header
._M_left
; }
722 _M_leftmost() const _GLIBCXX_NOEXCEPT
723 { return this->_M_impl
._M_header
._M_left
; }
726 _M_rightmost() _GLIBCXX_NOEXCEPT
727 { return this->_M_impl
._M_header
._M_right
; }
730 _M_rightmost() const _GLIBCXX_NOEXCEPT
731 { return this->_M_impl
._M_header
._M_right
; }
734 _M_mbegin() const _GLIBCXX_NOEXCEPT
735 { return static_cast<_Link_type
>(this->_M_impl
._M_header
._M_parent
); }
738 _M_begin() _GLIBCXX_NOEXCEPT
739 { return _M_mbegin(); }
742 _M_begin() const _GLIBCXX_NOEXCEPT
744 return static_cast<_Const_Link_type
>
745 (this->_M_impl
._M_header
._M_parent
);
749 _M_end() _GLIBCXX_NOEXCEPT
750 { return &this->_M_impl
._M_header
; }
753 _M_end() const _GLIBCXX_NOEXCEPT
754 { return &this->_M_impl
._M_header
; }
757 _S_key(_Const_Link_type __x
)
759 #if __cplusplus >= 201103L
760 // If we're asking for the key we're presumably using the comparison
761 // object, and so this is a good place to sanity check it.
762 static_assert(__is_invocable
<_Compare
&, const _Key
&, const _Key
&>{},
763 "comparison object must be invocable "
764 "with two arguments of key type");
765 # if __cplusplus >= 201703L
766 // _GLIBCXX_RESOLVE_LIB_DEFECTS
767 // 2542. Missing const requirements for associative containers
768 if constexpr (__is_invocable
<_Compare
&, const _Key
&, const _Key
&>{})
770 is_invocable_v
<const _Compare
&, const _Key
&, const _Key
&>,
771 "comparison object must be invocable as const");
775 return _KeyOfValue()(*__x
->_M_valptr());
779 _S_left(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
780 { return static_cast<_Link_type
>(__x
->_M_left
); }
782 static _Const_Link_type
783 _S_left(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
784 { return static_cast<_Const_Link_type
>(__x
->_M_left
); }
787 _S_right(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
788 { return static_cast<_Link_type
>(__x
->_M_right
); }
790 static _Const_Link_type
791 _S_right(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
792 { return static_cast<_Const_Link_type
>(__x
->_M_right
); }
795 _S_key(_Const_Base_ptr __x
)
796 { return _S_key(static_cast<_Const_Link_type
>(__x
)); }
799 _S_minimum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
800 { return _Rb_tree_node_base::_S_minimum(__x
); }
802 static _Const_Base_ptr
803 _S_minimum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
804 { return _Rb_tree_node_base::_S_minimum(__x
); }
807 _S_maximum(_Base_ptr __x
) _GLIBCXX_NOEXCEPT
808 { return _Rb_tree_node_base::_S_maximum(__x
); }
810 static _Const_Base_ptr
811 _S_maximum(_Const_Base_ptr __x
) _GLIBCXX_NOEXCEPT
812 { return _Rb_tree_node_base::_S_maximum(__x
); }
815 typedef _Rb_tree_iterator
<value_type
> iterator
;
816 typedef _Rb_tree_const_iterator
<value_type
> const_iterator
;
818 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
819 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
821 #if __cplusplus > 201402L
822 using node_type
= _Node_handle
<_Key
, _Val
, _Node_allocator
>;
823 using insert_return_type
= _Node_insert_return
<
824 __conditional_t
<is_same_v
<_Key
, _Val
>, const_iterator
, iterator
>,
828 pair
<_Base_ptr
, _Base_ptr
>
829 _M_get_insert_unique_pos(const key_type
& __k
);
831 pair
<_Base_ptr
, _Base_ptr
>
832 _M_get_insert_equal_pos(const key_type
& __k
);
834 pair
<_Base_ptr
, _Base_ptr
>
835 _M_get_insert_hint_unique_pos(const_iterator __pos
,
836 const key_type
& __k
);
838 pair
<_Base_ptr
, _Base_ptr
>
839 _M_get_insert_hint_equal_pos(const_iterator __pos
,
840 const key_type
& __k
);
843 #if __cplusplus >= 201103L
844 template<typename _Arg
, typename _NodeGen
>
846 _M_insert_(_Base_ptr __x
, _Base_ptr __y
, _Arg
&& __v
, _NodeGen
&);
849 _M_insert_node(_Base_ptr __x
, _Base_ptr __y
, _Link_type __z
);
851 template<typename _Arg
>
853 _M_insert_lower(_Base_ptr __y
, _Arg
&& __v
);
855 template<typename _Arg
>
857 _M_insert_equal_lower(_Arg
&& __x
);
860 _M_insert_lower_node(_Base_ptr __p
, _Link_type __z
);
863 _M_insert_equal_lower_node(_Link_type __z
);
865 template<typename _NodeGen
>
867 _M_insert_(_Base_ptr __x
, _Base_ptr __y
,
868 const value_type
& __v
, _NodeGen
&);
870 // _GLIBCXX_RESOLVE_LIB_DEFECTS
871 // 233. Insertion hints in associative containers.
873 _M_insert_lower(_Base_ptr __y
, const value_type
& __v
);
876 _M_insert_equal_lower(const value_type
& __x
);
879 enum { __as_lvalue
, __as_rvalue
};
881 template<bool _MoveValues
, typename _NodeGen
>
883 _M_copy(_Link_type
, _Base_ptr
, _NodeGen
&);
885 template<bool _MoveValues
, typename _NodeGen
>
887 _M_copy(const _Rb_tree
& __x
, _NodeGen
& __gen
)
890 _M_copy
<_MoveValues
>(__x
._M_mbegin(), _M_end(), __gen
);
891 _M_leftmost() = _S_minimum(__root
);
892 _M_rightmost() = _S_maximum(__root
);
893 _M_impl
._M_node_count
= __x
._M_impl
._M_node_count
;
898 _M_copy(const _Rb_tree
& __x
)
900 _Alloc_node
__an(*this);
901 return _M_copy
<__as_lvalue
>(__x
, __an
);
905 _M_erase(_Link_type __x
);
908 _M_lower_bound(_Link_type __x
, _Base_ptr __y
,
912 _M_lower_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
913 const _Key
& __k
) const;
916 _M_upper_bound(_Link_type __x
, _Base_ptr __y
,
920 _M_upper_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
921 const _Key
& __k
) const;
924 // allocation/deallocation
925 #if __cplusplus < 201103L
928 _Rb_tree() = default;
931 _Rb_tree(const _Compare
& __comp
,
932 const allocator_type
& __a
= allocator_type())
933 : _M_impl(__comp
, _Node_allocator(__a
)) { }
935 _Rb_tree(const _Rb_tree
& __x
)
936 : _M_impl(__x
._M_impl
)
938 if (__x
._M_root() != 0)
939 _M_root() = _M_copy(__x
);
942 #if __cplusplus >= 201103L
943 _Rb_tree(const allocator_type
& __a
)
944 : _M_impl(_Node_allocator(__a
))
947 _Rb_tree(const _Rb_tree
& __x
, const allocator_type
& __a
)
948 : _M_impl(__x
._M_impl
._M_key_compare
, _Node_allocator(__a
))
950 if (__x
._M_root() != nullptr)
951 _M_root() = _M_copy(__x
);
954 _Rb_tree(_Rb_tree
&&) = default;
956 _Rb_tree(_Rb_tree
&& __x
, const allocator_type
& __a
)
957 : _Rb_tree(std::move(__x
), _Node_allocator(__a
))
961 _Rb_tree(_Rb_tree
&& __x
, _Node_allocator
&& __a
, true_type
)
962 noexcept(is_nothrow_default_constructible
<_Compare
>::value
)
963 : _M_impl(std::move(__x
._M_impl
), std::move(__a
))
966 _Rb_tree(_Rb_tree
&& __x
, _Node_allocator
&& __a
, false_type
)
967 : _M_impl(__x
._M_impl
._M_key_compare
, std::move(__a
))
969 if (__x
._M_root() != nullptr)
970 _M_move_data(__x
, false_type
{});
974 _Rb_tree(_Rb_tree
&& __x
, _Node_allocator
&& __a
)
976 _Rb_tree(std::declval
<_Rb_tree
&&>(), std::declval
<_Node_allocator
&&>(),
977 std::declval
<typename
_Alloc_traits::is_always_equal
>())) )
978 : _Rb_tree(std::move(__x
), std::move(__a
),
979 typename
_Alloc_traits::is_always_equal
{})
983 ~_Rb_tree() _GLIBCXX_NOEXCEPT
984 { _M_erase(_M_begin()); }
987 operator=(const _Rb_tree
& __x
);
992 { return _M_impl
._M_key_compare
; }
995 begin() _GLIBCXX_NOEXCEPT
996 { return iterator(this->_M_impl
._M_header
._M_left
); }
999 begin() const _GLIBCXX_NOEXCEPT
1000 { return const_iterator(this->_M_impl
._M_header
._M_left
); }
1003 end() _GLIBCXX_NOEXCEPT
1004 { return iterator(&this->_M_impl
._M_header
); }
1007 end() const _GLIBCXX_NOEXCEPT
1008 { return const_iterator(&this->_M_impl
._M_header
); }
1011 rbegin() _GLIBCXX_NOEXCEPT
1012 { return reverse_iterator(end()); }
1014 const_reverse_iterator
1015 rbegin() const _GLIBCXX_NOEXCEPT
1016 { return const_reverse_iterator(end()); }
1019 rend() _GLIBCXX_NOEXCEPT
1020 { return reverse_iterator(begin()); }
1022 const_reverse_iterator
1023 rend() const _GLIBCXX_NOEXCEPT
1024 { return const_reverse_iterator(begin()); }
1026 _GLIBCXX_NODISCARD
bool
1027 empty() const _GLIBCXX_NOEXCEPT
1028 { return _M_impl
._M_node_count
== 0; }
1031 size() const _GLIBCXX_NOEXCEPT
1032 { return _M_impl
._M_node_count
; }
1035 max_size() const _GLIBCXX_NOEXCEPT
1036 { return _Alloc_traits::max_size(_M_get_Node_allocator()); }
1040 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Compare
>::value
);
1043 #if __cplusplus >= 201103L
1044 template<typename _Arg
>
1045 pair
<iterator
, bool>
1046 _M_insert_unique(_Arg
&& __x
);
1048 template<typename _Arg
>
1050 _M_insert_equal(_Arg
&& __x
);
1052 template<typename _Arg
, typename _NodeGen
>
1054 _M_insert_unique_(const_iterator __pos
, _Arg
&& __x
, _NodeGen
&);
1056 template<typename _Arg
>
1058 _M_insert_unique_(const_iterator __pos
, _Arg
&& __x
)
1060 _Alloc_node
__an(*this);
1061 return _M_insert_unique_(__pos
, std::forward
<_Arg
>(__x
), __an
);
1064 template<typename _Arg
, typename _NodeGen
>
1066 _M_insert_equal_(const_iterator __pos
, _Arg
&& __x
, _NodeGen
&);
1068 template<typename _Arg
>
1070 _M_insert_equal_(const_iterator __pos
, _Arg
&& __x
)
1072 _Alloc_node
__an(*this);
1073 return _M_insert_equal_(__pos
, std::forward
<_Arg
>(__x
), __an
);
1076 template<typename
... _Args
>
1077 pair
<iterator
, bool>
1078 _M_emplace_unique(_Args
&&... __args
);
1080 template<typename
... _Args
>
1082 _M_emplace_equal(_Args
&&... __args
);
1084 template<typename
... _Args
>
1086 _M_emplace_hint_unique(const_iterator __pos
, _Args
&&... __args
);
1088 template<typename
... _Args
>
1090 _M_emplace_hint_equal(const_iterator __pos
, _Args
&&... __args
);
1092 template<typename _Iter
>
1093 using __same_value_type
1094 = is_same
<value_type
, typename iterator_traits
<_Iter
>::value_type
>;
1096 template<typename _InputIterator
>
1097 __enable_if_t
<__same_value_type
<_InputIterator
>::value
>
1098 _M_insert_range_unique(_InputIterator __first
, _InputIterator __last
)
1100 _Alloc_node
__an(*this);
1101 for (; __first
!= __last
; ++__first
)
1102 _M_insert_unique_(end(), *__first
, __an
);
1105 template<typename _InputIterator
>
1106 __enable_if_t
<!__same_value_type
<_InputIterator
>::value
>
1107 _M_insert_range_unique(_InputIterator __first
, _InputIterator __last
)
1109 for (; __first
!= __last
; ++__first
)
1110 _M_emplace_unique(*__first
);
1113 template<typename _InputIterator
>
1114 __enable_if_t
<__same_value_type
<_InputIterator
>::value
>
1115 _M_insert_range_equal(_InputIterator __first
, _InputIterator __last
)
1117 _Alloc_node
__an(*this);
1118 for (; __first
!= __last
; ++__first
)
1119 _M_insert_equal_(end(), *__first
, __an
);
1122 template<typename _InputIterator
>
1123 __enable_if_t
<!__same_value_type
<_InputIterator
>::value
>
1124 _M_insert_range_equal(_InputIterator __first
, _InputIterator __last
)
1126 for (; __first
!= __last
; ++__first
)
1127 _M_emplace_equal(*__first
);
1130 pair
<iterator
, bool>
1131 _M_insert_unique(const value_type
& __x
);
1134 _M_insert_equal(const value_type
& __x
);
1136 template<typename _NodeGen
>
1138 _M_insert_unique_(const_iterator __pos
, const value_type
& __x
,
1142 _M_insert_unique_(const_iterator __pos
, const value_type
& __x
)
1144 _Alloc_node
__an(*this);
1145 return _M_insert_unique_(__pos
, __x
, __an
);
1148 template<typename _NodeGen
>
1150 _M_insert_equal_(const_iterator __pos
, const value_type
& __x
,
1153 _M_insert_equal_(const_iterator __pos
, const value_type
& __x
)
1155 _Alloc_node
__an(*this);
1156 return _M_insert_equal_(__pos
, __x
, __an
);
1159 template<typename _InputIterator
>
1161 _M_insert_range_unique(_InputIterator __first
, _InputIterator __last
)
1163 _Alloc_node
__an(*this);
1164 for (; __first
!= __last
; ++__first
)
1165 _M_insert_unique_(end(), *__first
, __an
);
1168 template<typename _InputIterator
>
1170 _M_insert_range_equal(_InputIterator __first
, _InputIterator __last
)
1172 _Alloc_node
__an(*this);
1173 for (; __first
!= __last
; ++__first
)
1174 _M_insert_equal_(end(), *__first
, __an
);
1180 _M_erase_aux(const_iterator __position
);
1183 _M_erase_aux(const_iterator __first
, const_iterator __last
);
1186 #if __cplusplus >= 201103L
1187 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1188 // DR 130. Associative erase should return an iterator.
1189 _GLIBCXX_ABI_TAG_CXX11
1191 erase(const_iterator __position
)
1193 __glibcxx_assert(__position
!= end());
1194 const_iterator __result
= __position
;
1196 _M_erase_aux(__position
);
1197 return __result
._M_const_cast();
1201 _GLIBCXX_ABI_TAG_CXX11
1203 erase(iterator __position
)
1205 __glibcxx_assert(__position
!= end());
1206 iterator __result
= __position
;
1208 _M_erase_aux(__position
);
1213 erase(iterator __position
)
1215 __glibcxx_assert(__position
!= end());
1216 _M_erase_aux(__position
);
1220 erase(const_iterator __position
)
1222 __glibcxx_assert(__position
!= end());
1223 _M_erase_aux(__position
);
1228 erase(const key_type
& __x
);
1230 #if __cplusplus >= 201103L
1231 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1232 // DR 130. Associative erase should return an iterator.
1233 _GLIBCXX_ABI_TAG_CXX11
1235 erase(const_iterator __first
, const_iterator __last
)
1237 _M_erase_aux(__first
, __last
);
1238 return __last
._M_const_cast();
1242 erase(iterator __first
, iterator __last
)
1243 { _M_erase_aux(__first
, __last
); }
1246 erase(const_iterator __first
, const_iterator __last
)
1247 { _M_erase_aux(__first
, __last
); }
1251 clear() _GLIBCXX_NOEXCEPT
1253 _M_erase(_M_begin());
1259 find(const key_type
& __k
);
1262 find(const key_type
& __k
) const;
1265 count(const key_type
& __k
) const;
1268 lower_bound(const key_type
& __k
)
1269 { return _M_lower_bound(_M_begin(), _M_end(), __k
); }
1272 lower_bound(const key_type
& __k
) const
1273 { return _M_lower_bound(_M_begin(), _M_end(), __k
); }
1276 upper_bound(const key_type
& __k
)
1277 { return _M_upper_bound(_M_begin(), _M_end(), __k
); }
1280 upper_bound(const key_type
& __k
) const
1281 { return _M_upper_bound(_M_begin(), _M_end(), __k
); }
1283 pair
<iterator
, iterator
>
1284 equal_range(const key_type
& __k
);
1286 pair
<const_iterator
, const_iterator
>
1287 equal_range(const key_type
& __k
) const;
1289 #if __cplusplus >= 201402L
1290 template<typename _Kt
,
1291 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1293 _M_find_tr(const _Kt
& __k
)
1295 const _Rb_tree
* __const_this
= this;
1296 return __const_this
->_M_find_tr(__k
)._M_const_cast();
1299 template<typename _Kt
,
1300 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1302 _M_find_tr(const _Kt
& __k
) const
1304 auto __j
= _M_lower_bound_tr(__k
);
1305 if (__j
!= end() && _M_impl
._M_key_compare(__k
, _S_key(__j
._M_node
)))
1310 template<typename _Kt
,
1311 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1313 _M_count_tr(const _Kt
& __k
) const
1315 auto __p
= _M_equal_range_tr(__k
);
1316 return std::distance(__p
.first
, __p
.second
);
1319 template<typename _Kt
,
1320 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1322 _M_lower_bound_tr(const _Kt
& __k
)
1324 const _Rb_tree
* __const_this
= this;
1325 return __const_this
->_M_lower_bound_tr(__k
)._M_const_cast();
1328 template<typename _Kt
,
1329 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1331 _M_lower_bound_tr(const _Kt
& __k
) const
1333 auto __x
= _M_begin();
1334 auto __y
= _M_end();
1336 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
1342 __x
= _S_right(__x
);
1343 return const_iterator(__y
);
1346 template<typename _Kt
,
1347 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1349 _M_upper_bound_tr(const _Kt
& __k
)
1351 const _Rb_tree
* __const_this
= this;
1352 return __const_this
->_M_upper_bound_tr(__k
)._M_const_cast();
1355 template<typename _Kt
,
1356 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1358 _M_upper_bound_tr(const _Kt
& __k
) const
1360 auto __x
= _M_begin();
1361 auto __y
= _M_end();
1363 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1369 __x
= _S_right(__x
);
1370 return const_iterator(__y
);
1373 template<typename _Kt
,
1374 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1375 pair
<iterator
, iterator
>
1376 _M_equal_range_tr(const _Kt
& __k
)
1378 const _Rb_tree
* __const_this
= this;
1379 auto __ret
= __const_this
->_M_equal_range_tr(__k
);
1380 return { __ret
.first
._M_const_cast(), __ret
.second
._M_const_cast() };
1383 template<typename _Kt
,
1384 typename _Req
= __has_is_transparent_t
<_Compare
, _Kt
>>
1385 pair
<const_iterator
, const_iterator
>
1386 _M_equal_range_tr(const _Kt
& __k
) const
1388 auto __low
= _M_lower_bound_tr(__k
);
1389 auto __high
= __low
;
1390 auto& __cmp
= _M_impl
._M_key_compare
;
1391 while (__high
!= end() && !__cmp(__k
, _S_key(__high
._M_node
)))
1393 return { __low
, __high
};
1399 __rb_verify() const;
1401 #if __cplusplus >= 201103L
1403 operator=(_Rb_tree
&&)
1404 noexcept(_Alloc_traits::_S_nothrow_move()
1405 && is_nothrow_move_assignable
<_Compare
>::value
);
1407 template<typename _Iterator
>
1409 _M_assign_unique(_Iterator
, _Iterator
);
1411 template<typename _Iterator
>
1413 _M_assign_equal(_Iterator
, _Iterator
);
1416 // Move elements from container with equal allocator.
1418 _M_move_data(_Rb_tree
& __x
, true_type
)
1419 { _M_impl
._M_move_data(__x
._M_impl
); }
1421 // Move elements from container with possibly non-equal allocator,
1422 // which might result in a copy not a move.
1424 _M_move_data(_Rb_tree
&, false_type
);
1426 // Move assignment from container with equal allocator.
1428 _M_move_assign(_Rb_tree
&, true_type
);
1430 // Move assignment from container with possibly non-equal allocator,
1431 // which might result in a copy not a move.
1433 _M_move_assign(_Rb_tree
&, false_type
);
1436 #if __glibcxx_node_extract // >= C++17
1438 /// Re-insert an extracted node.
1440 _M_reinsert_node_unique(node_type
&& __nh
)
1442 insert_return_type __ret
;
1444 __ret
.position
= end();
1447 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1449 auto __res
= _M_get_insert_unique_pos(__nh
._M_key());
1453 = _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1455 __ret
.inserted
= true;
1459 __ret
.node
= std::move(__nh
);
1460 __ret
.position
= iterator(__res
.first
);
1461 __ret
.inserted
= false;
1467 /// Re-insert an extracted node.
1469 _M_reinsert_node_equal(node_type
&& __nh
)
1476 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1477 auto __res
= _M_get_insert_equal_pos(__nh
._M_key());
1479 __ret
= _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1481 __ret
= _M_insert_equal_lower_node(__nh
._M_ptr
);
1487 /// Re-insert an extracted node.
1489 _M_reinsert_node_hint_unique(const_iterator __hint
, node_type
&& __nh
)
1496 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1497 auto __res
= _M_get_insert_hint_unique_pos(__hint
, __nh
._M_key());
1500 __ret
= _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1504 __ret
= iterator(__res
.first
);
1509 /// Re-insert an extracted node.
1511 _M_reinsert_node_hint_equal(const_iterator __hint
, node_type
&& __nh
)
1518 __glibcxx_assert(_M_get_Node_allocator() == *__nh
._M_alloc
);
1519 auto __res
= _M_get_insert_hint_equal_pos(__hint
, __nh
._M_key());
1521 __ret
= _M_insert_node(__res
.first
, __res
.second
, __nh
._M_ptr
);
1523 __ret
= _M_insert_equal_lower_node(__nh
._M_ptr
);
1531 extract(const_iterator __pos
)
1533 auto __ptr
= _Rb_tree_rebalance_for_erase(
1534 __pos
._M_const_cast()._M_node
, _M_impl
._M_header
);
1535 --_M_impl
._M_node_count
;
1536 return { static_cast<_Link_type
>(__ptr
), _M_get_Node_allocator() };
1541 extract(const key_type
& __k
)
1544 auto __pos
= find(__k
);
1546 __nh
= extract(const_iterator(__pos
));
1550 template<typename _Compare2
>
1551 using _Compatible_tree
1552 = _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare2
, _Alloc
>;
1554 template<typename
, typename
>
1555 friend struct _Rb_tree_merge_helper
;
1557 /// Merge from a compatible container into one with unique keys.
1558 template<typename _Compare2
>
1560 _M_merge_unique(_Compatible_tree
<_Compare2
>& __src
) noexcept
1562 using _Merge_helper
= _Rb_tree_merge_helper
<_Rb_tree
, _Compare2
>;
1563 for (auto __i
= __src
.begin(), __end
= __src
.end(); __i
!= __end
;)
1566 auto __res
= _M_get_insert_unique_pos(_KeyOfValue()(*__pos
));
1569 auto& __src_impl
= _Merge_helper::_S_get_impl(__src
);
1570 auto __ptr
= _Rb_tree_rebalance_for_erase(
1571 __pos
._M_node
, __src_impl
._M_header
);
1572 --__src_impl
._M_node_count
;
1573 _M_insert_node(__res
.first
, __res
.second
,
1574 static_cast<_Link_type
>(__ptr
));
1579 /// Merge from a compatible container into one with equivalent keys.
1580 template<typename _Compare2
>
1582 _M_merge_equal(_Compatible_tree
<_Compare2
>& __src
) noexcept
1584 using _Merge_helper
= _Rb_tree_merge_helper
<_Rb_tree
, _Compare2
>;
1585 for (auto __i
= __src
.begin(), __end
= __src
.end(); __i
!= __end
;)
1588 auto __res
= _M_get_insert_equal_pos(_KeyOfValue()(*__pos
));
1591 auto& __src_impl
= _Merge_helper::_S_get_impl(__src
);
1592 auto __ptr
= _Rb_tree_rebalance_for_erase(
1593 __pos
._M_node
, __src_impl
._M_header
);
1594 --__src_impl
._M_node_count
;
1595 _M_insert_node(__res
.first
, __res
.second
,
1596 static_cast<_Link_type
>(__ptr
));
1600 #endif // C++17 node_extract
1603 operator==(const _Rb_tree
& __x
, const _Rb_tree
& __y
)
1605 return __x
.size() == __y
.size()
1606 && std::equal(__x
.begin(), __x
.end(), __y
.begin());
1609 #if __cpp_lib_three_way_comparison
1611 operator<=>(const _Rb_tree
& __x
, const _Rb_tree
& __y
)
1613 if constexpr (requires
{ typename
__detail::__synth3way_t
<_Val
>; })
1614 return std::lexicographical_compare_three_way(__x
.begin(), __x
.end(),
1615 __y
.begin(), __y
.end(),
1616 __detail::__synth3way
);
1620 operator<(const _Rb_tree
& __x
, const _Rb_tree
& __y
)
1622 return std::lexicographical_compare(__x
.begin(), __x
.end(),
1623 __y
.begin(), __y
.end());
1628 #if __cplusplus >= 201103L
1629 // An RAII _Node handle
1632 template<typename
... _Args
>
1633 _Auto_node(_Rb_tree
& __t
, _Args
&&... __args
)
1635 _M_node(__t
._M_create_node(std::forward
<_Args
>(__args
)...))
1641 _M_t
._M_drop_node(_M_node
);
1644 _Auto_node(_Auto_node
&& __n
)
1645 : _M_t(__n
._M_t
), _M_node(__n
._M_node
)
1646 { __n
._M_node
= nullptr; }
1650 { return _S_key(_M_node
); }
1653 _M_insert(pair
<_Base_ptr
, _Base_ptr
> __p
)
1655 auto __it
= _M_t
._M_insert_node(__p
.first
, __p
.second
, _M_node
);
1661 _M_insert_equal_lower()
1663 auto __it
= _M_t
._M_insert_equal_lower_node(_M_node
);
1674 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1675 typename _Compare
, typename _Alloc
>
1677 swap(_Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __x
,
1678 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>& __y
)
1681 #if __cplusplus >= 201103L
1682 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1683 typename _Compare
, typename _Alloc
>
1685 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1686 _M_move_data(_Rb_tree
& __x
, false_type
)
1688 if (_M_get_Node_allocator() == __x
._M_get_Node_allocator())
1689 _M_move_data(__x
, true_type());
1692 constexpr bool __move
= !__move_if_noexcept_cond
<value_type
>::value
;
1693 _Alloc_node
__an(*this);
1694 _M_root() = _M_copy
<__move
>(__x
, __an
);
1695 if _GLIBCXX17_CONSTEXPR (__move
)
1700 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1701 typename _Compare
, typename _Alloc
>
1703 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1704 _M_move_assign(_Rb_tree
& __x
, true_type
)
1707 if (__x
._M_root() != nullptr)
1708 _M_move_data(__x
, true_type());
1709 std::__alloc_on_move(_M_get_Node_allocator(),
1710 __x
._M_get_Node_allocator());
1713 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1714 typename _Compare
, typename _Alloc
>
1716 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1717 _M_move_assign(_Rb_tree
& __x
, false_type
)
1719 if (_M_get_Node_allocator() == __x
._M_get_Node_allocator())
1720 return _M_move_assign(__x
, true_type
{});
1722 // Try to move each node reusing existing nodes and copying __x nodes
1724 _Reuse_or_alloc_node
__roan(*this);
1726 if (__x
._M_root() != nullptr)
1728 _M_root() = _M_copy
<__as_rvalue
>(__x
, __roan
);
1733 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1734 typename _Compare
, typename _Alloc
>
1735 inline _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>&
1736 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1737 operator=(_Rb_tree
&& __x
)
1738 noexcept(_Alloc_traits::_S_nothrow_move()
1739 && is_nothrow_move_assignable
<_Compare
>::value
)
1741 _M_impl
._M_key_compare
= std::move(__x
._M_impl
._M_key_compare
);
1742 _M_move_assign(__x
, __bool_constant
<_Alloc_traits::_S_nothrow_move()>());
1746 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1747 typename _Compare
, typename _Alloc
>
1748 template<typename _Iterator
>
1750 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1751 _M_assign_unique(_Iterator __first
, _Iterator __last
)
1753 _Reuse_or_alloc_node
__roan(*this);
1755 for (; __first
!= __last
; ++__first
)
1756 _M_insert_unique_(end(), *__first
, __roan
);
1759 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1760 typename _Compare
, typename _Alloc
>
1761 template<typename _Iterator
>
1763 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1764 _M_assign_equal(_Iterator __first
, _Iterator __last
)
1766 _Reuse_or_alloc_node
__roan(*this);
1768 for (; __first
!= __last
; ++__first
)
1769 _M_insert_equal_(end(), *__first
, __roan
);
1773 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1774 typename _Compare
, typename _Alloc
>
1775 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>&
1776 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1777 operator=(const _Rb_tree
& __x
)
1779 if (this != std::__addressof(__x
))
1781 // Note that _Key may be a constant type.
1782 #if __cplusplus >= 201103L
1783 if (_Alloc_traits::_S_propagate_on_copy_assign())
1785 auto& __this_alloc
= this->_M_get_Node_allocator();
1786 auto& __that_alloc
= __x
._M_get_Node_allocator();
1787 if (!_Alloc_traits::_S_always_equal()
1788 && __this_alloc
!= __that_alloc
)
1790 // Replacement allocator cannot free existing storage, we need
1791 // to erase nodes first.
1793 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
1798 _Reuse_or_alloc_node
__roan(*this);
1800 _M_impl
._M_key_compare
= __x
._M_impl
._M_key_compare
;
1801 if (__x
._M_root() != 0)
1802 _M_root() = _M_copy
<__as_lvalue
>(__x
, __roan
);
1808 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1809 typename _Compare
, typename _Alloc
>
1810 #if __cplusplus >= 201103L
1811 template<typename _Arg
, typename _NodeGen
>
1813 template<typename _NodeGen
>
1815 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1816 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1817 _M_insert_(_Base_ptr __x
, _Base_ptr __p
,
1818 #if __cplusplus >= 201103L
1823 _NodeGen
& __node_gen
)
1825 bool __insert_left
= (__x
!= 0 || __p
== _M_end()
1826 || _M_impl
._M_key_compare(_KeyOfValue()(__v
),
1829 _Link_type __z
= __node_gen(_GLIBCXX_FORWARD(_Arg
, __v
));
1831 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
1832 this->_M_impl
._M_header
);
1833 ++_M_impl
._M_node_count
;
1834 return iterator(__z
);
1837 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1838 typename _Compare
, typename _Alloc
>
1839 #if __cplusplus >= 201103L
1840 template<typename _Arg
>
1842 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1843 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1844 #if __cplusplus >= 201103L
1845 _M_insert_lower(_Base_ptr __p
, _Arg
&& __v
)
1847 _M_insert_lower(_Base_ptr __p
, const _Val
& __v
)
1850 bool __insert_left
= (__p
== _M_end()
1851 || !_M_impl
._M_key_compare(_S_key(__p
),
1852 _KeyOfValue()(__v
)));
1854 _Link_type __z
= _M_create_node(_GLIBCXX_FORWARD(_Arg
, __v
));
1856 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
1857 this->_M_impl
._M_header
);
1858 ++_M_impl
._M_node_count
;
1859 return iterator(__z
);
1862 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1863 typename _Compare
, typename _Alloc
>
1864 #if __cplusplus >= 201103L
1865 template<typename _Arg
>
1867 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
1868 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1869 #if __cplusplus >= 201103L
1870 _M_insert_equal_lower(_Arg
&& __v
)
1872 _M_insert_equal_lower(const _Val
& __v
)
1875 _Link_type __x
= _M_begin();
1876 _Base_ptr __y
= _M_end();
1880 __x
= !_M_impl
._M_key_compare(_S_key(__x
), _KeyOfValue()(__v
)) ?
1881 _S_left(__x
) : _S_right(__x
);
1883 return _M_insert_lower(__y
, _GLIBCXX_FORWARD(_Arg
, __v
));
1886 template<typename _Key
, typename _Val
, typename _KoV
,
1887 typename _Compare
, typename _Alloc
>
1888 template<bool _MoveValues
, typename _NodeGen
>
1889 typename _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::_Link_type
1890 _Rb_tree
<_Key
, _Val
, _KoV
, _Compare
, _Alloc
>::
1891 _M_copy(_Link_type __x
, _Base_ptr __p
, _NodeGen
& __node_gen
)
1893 // Structural copy. __x and __p must be non-null.
1894 _Link_type __top
= _M_clone_node
<_MoveValues
>(__x
, __node_gen
);
1895 __top
->_M_parent
= __p
;
1901 _M_copy
<_MoveValues
>(_S_right(__x
), __top
, __node_gen
);
1907 _Link_type __y
= _M_clone_node
<_MoveValues
>(__x
, __node_gen
);
1909 __y
->_M_parent
= __p
;
1911 __y
->_M_right
= _M_copy
<_MoveValues
>(_S_right(__x
),
1920 __throw_exception_again
;
1925 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1926 typename _Compare
, typename _Alloc
>
1928 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1929 _M_erase(_Link_type __x
)
1931 // Erase without rebalancing.
1934 _M_erase(_S_right(__x
));
1935 _Link_type __y
= _S_left(__x
);
1941 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1942 typename _Compare
, typename _Alloc
>
1943 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1944 _Compare
, _Alloc
>::iterator
1945 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1946 _M_lower_bound(_Link_type __x
, _Base_ptr __y
,
1950 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
1951 __y
= __x
, __x
= _S_left(__x
);
1953 __x
= _S_right(__x
);
1954 return iterator(__y
);
1957 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1958 typename _Compare
, typename _Alloc
>
1959 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1960 _Compare
, _Alloc
>::const_iterator
1961 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1962 _M_lower_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
1963 const _Key
& __k
) const
1966 if (!_M_impl
._M_key_compare(_S_key(__x
), __k
))
1967 __y
= __x
, __x
= _S_left(__x
);
1969 __x
= _S_right(__x
);
1970 return const_iterator(__y
);
1973 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1974 typename _Compare
, typename _Alloc
>
1975 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1976 _Compare
, _Alloc
>::iterator
1977 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1978 _M_upper_bound(_Link_type __x
, _Base_ptr __y
,
1982 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1983 __y
= __x
, __x
= _S_left(__x
);
1985 __x
= _S_right(__x
);
1986 return iterator(__y
);
1989 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
1990 typename _Compare
, typename _Alloc
>
1991 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
1992 _Compare
, _Alloc
>::const_iterator
1993 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
1994 _M_upper_bound(_Const_Link_type __x
, _Const_Base_ptr __y
,
1995 const _Key
& __k
) const
1998 if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
1999 __y
= __x
, __x
= _S_left(__x
);
2001 __x
= _S_right(__x
);
2002 return const_iterator(__y
);
2005 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2006 typename _Compare
, typename _Alloc
>
2007 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2008 _Compare
, _Alloc
>::iterator
,
2009 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2010 _Compare
, _Alloc
>::iterator
>
2011 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2012 equal_range(const _Key
& __k
)
2014 _Link_type __x
= _M_begin();
2015 _Base_ptr __y
= _M_end();
2018 if (_M_impl
._M_key_compare(_S_key(__x
), __k
))
2019 __x
= _S_right(__x
);
2020 else if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
2021 __y
= __x
, __x
= _S_left(__x
);
2024 _Link_type
__xu(__x
);
2025 _Base_ptr
__yu(__y
);
2026 __y
= __x
, __x
= _S_left(__x
);
2027 __xu
= _S_right(__xu
);
2028 return pair
<iterator
,
2029 iterator
>(_M_lower_bound(__x
, __y
, __k
),
2030 _M_upper_bound(__xu
, __yu
, __k
));
2033 return pair
<iterator
, iterator
>(iterator(__y
),
2037 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2038 typename _Compare
, typename _Alloc
>
2039 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2040 _Compare
, _Alloc
>::const_iterator
,
2041 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2042 _Compare
, _Alloc
>::const_iterator
>
2043 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2044 equal_range(const _Key
& __k
) const
2046 _Const_Link_type __x
= _M_begin();
2047 _Const_Base_ptr __y
= _M_end();
2050 if (_M_impl
._M_key_compare(_S_key(__x
), __k
))
2051 __x
= _S_right(__x
);
2052 else if (_M_impl
._M_key_compare(__k
, _S_key(__x
)))
2053 __y
= __x
, __x
= _S_left(__x
);
2056 _Const_Link_type
__xu(__x
);
2057 _Const_Base_ptr
__yu(__y
);
2058 __y
= __x
, __x
= _S_left(__x
);
2059 __xu
= _S_right(__xu
);
2060 return pair
<const_iterator
,
2061 const_iterator
>(_M_lower_bound(__x
, __y
, __k
),
2062 _M_upper_bound(__xu
, __yu
, __k
));
2065 return pair
<const_iterator
, const_iterator
>(const_iterator(__y
),
2066 const_iterator(__y
));
2069 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2070 typename _Compare
, typename _Alloc
>
2072 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2074 _GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable
<_Compare
>::value
)
2078 if (__t
._M_root() != 0)
2079 _M_impl
._M_move_data(__t
._M_impl
);
2081 else if (__t
._M_root() == 0)
2082 __t
._M_impl
._M_move_data(_M_impl
);
2085 std::swap(_M_root(),__t
._M_root());
2086 std::swap(_M_leftmost(),__t
._M_leftmost());
2087 std::swap(_M_rightmost(),__t
._M_rightmost());
2089 _M_root()->_M_parent
= _M_end();
2090 __t
._M_root()->_M_parent
= __t
._M_end();
2091 std::swap(this->_M_impl
._M_node_count
, __t
._M_impl
._M_node_count
);
2093 // No need to swap header's color as it does not change.
2094 std::swap(this->_M_impl
._M_key_compare
, __t
._M_impl
._M_key_compare
);
2096 _Alloc_traits::_S_on_swap(_M_get_Node_allocator(),
2097 __t
._M_get_Node_allocator());
2100 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2101 typename _Compare
, typename _Alloc
>
2102 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2103 _Compare
, _Alloc
>::_Base_ptr
,
2104 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2105 _Compare
, _Alloc
>::_Base_ptr
>
2106 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2107 _M_get_insert_unique_pos(const key_type
& __k
)
2109 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2110 _Link_type __x
= _M_begin();
2111 _Base_ptr __y
= _M_end();
2116 __comp
= _M_impl
._M_key_compare(__k
, _S_key(__x
));
2117 __x
= __comp
? _S_left(__x
) : _S_right(__x
);
2119 iterator __j
= iterator(__y
);
2123 return _Res(__x
, __y
);
2127 if (_M_impl
._M_key_compare(_S_key(__j
._M_node
), __k
))
2128 return _Res(__x
, __y
);
2129 return _Res(__j
._M_node
, 0);
2132 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2133 typename _Compare
, typename _Alloc
>
2134 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2135 _Compare
, _Alloc
>::_Base_ptr
,
2136 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2137 _Compare
, _Alloc
>::_Base_ptr
>
2138 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2139 _M_get_insert_equal_pos(const key_type
& __k
)
2141 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2142 _Link_type __x
= _M_begin();
2143 _Base_ptr __y
= _M_end();
2147 __x
= _M_impl
._M_key_compare(__k
, _S_key(__x
)) ?
2148 _S_left(__x
) : _S_right(__x
);
2150 return _Res(__x
, __y
);
2153 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2154 typename _Compare
, typename _Alloc
>
2155 #if __cplusplus >= 201103L
2156 template<typename _Arg
>
2158 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2159 _Compare
, _Alloc
>::iterator
, bool>
2160 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2161 #if __cplusplus >= 201103L
2162 _M_insert_unique(_Arg
&& __v
)
2164 _M_insert_unique(const _Val
& __v
)
2167 typedef pair
<iterator
, bool> _Res
;
2168 pair
<_Base_ptr
, _Base_ptr
> __res
2169 = _M_get_insert_unique_pos(_KeyOfValue()(__v
));
2173 _Alloc_node
__an(*this);
2174 return _Res(_M_insert_(__res
.first
, __res
.second
,
2175 _GLIBCXX_FORWARD(_Arg
, __v
), __an
),
2179 return _Res(iterator(__res
.first
), false);
2182 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2183 typename _Compare
, typename _Alloc
>
2184 #if __cplusplus >= 201103L
2185 template<typename _Arg
>
2187 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2188 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2189 #if __cplusplus >= 201103L
2190 _M_insert_equal(_Arg
&& __v
)
2192 _M_insert_equal(const _Val
& __v
)
2195 pair
<_Base_ptr
, _Base_ptr
> __res
2196 = _M_get_insert_equal_pos(_KeyOfValue()(__v
));
2197 _Alloc_node
__an(*this);
2198 return _M_insert_(__res
.first
, __res
.second
,
2199 _GLIBCXX_FORWARD(_Arg
, __v
), __an
);
2202 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2203 typename _Compare
, typename _Alloc
>
2204 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2205 _Compare
, _Alloc
>::_Base_ptr
,
2206 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2207 _Compare
, _Alloc
>::_Base_ptr
>
2208 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2209 _M_get_insert_hint_unique_pos(const_iterator __position
,
2210 const key_type
& __k
)
2212 iterator __pos
= __position
._M_const_cast();
2213 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2216 if (__pos
._M_node
== _M_end())
2219 && _M_impl
._M_key_compare(_S_key(_M_rightmost()), __k
))
2220 return _Res(0, _M_rightmost());
2222 return _M_get_insert_unique_pos(__k
);
2224 else if (_M_impl
._M_key_compare(__k
, _S_key(__pos
._M_node
)))
2226 // First, try before...
2227 iterator __before
= __pos
;
2228 if (__pos
._M_node
== _M_leftmost()) // begin()
2229 return _Res(_M_leftmost(), _M_leftmost());
2230 else if (_M_impl
._M_key_compare(_S_key((--__before
)._M_node
), __k
))
2232 if (_S_right(__before
._M_node
) == 0)
2233 return _Res(0, __before
._M_node
);
2235 return _Res(__pos
._M_node
, __pos
._M_node
);
2238 return _M_get_insert_unique_pos(__k
);
2240 else if (_M_impl
._M_key_compare(_S_key(__pos
._M_node
), __k
))
2242 // ... then try after.
2243 iterator __after
= __pos
;
2244 if (__pos
._M_node
== _M_rightmost())
2245 return _Res(0, _M_rightmost());
2246 else if (_M_impl
._M_key_compare(__k
, _S_key((++__after
)._M_node
)))
2248 if (_S_right(__pos
._M_node
) == 0)
2249 return _Res(0, __pos
._M_node
);
2251 return _Res(__after
._M_node
, __after
._M_node
);
2254 return _M_get_insert_unique_pos(__k
);
2258 return _Res(__pos
._M_node
, 0);
2261 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2262 typename _Compare
, typename _Alloc
>
2263 #if __cplusplus >= 201103L
2264 template<typename _Arg
, typename _NodeGen
>
2266 template<typename _NodeGen
>
2268 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2269 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2270 _M_insert_unique_(const_iterator __position
,
2271 #if __cplusplus >= 201103L
2276 _NodeGen
& __node_gen
)
2278 pair
<_Base_ptr
, _Base_ptr
> __res
2279 = _M_get_insert_hint_unique_pos(__position
, _KeyOfValue()(__v
));
2282 return _M_insert_(__res
.first
, __res
.second
,
2283 _GLIBCXX_FORWARD(_Arg
, __v
),
2285 return iterator(__res
.first
);
2288 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2289 typename _Compare
, typename _Alloc
>
2290 pair
<typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2291 _Compare
, _Alloc
>::_Base_ptr
,
2292 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2293 _Compare
, _Alloc
>::_Base_ptr
>
2294 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2295 _M_get_insert_hint_equal_pos(const_iterator __position
, const key_type
& __k
)
2297 iterator __pos
= __position
._M_const_cast();
2298 typedef pair
<_Base_ptr
, _Base_ptr
> _Res
;
2301 if (__pos
._M_node
== _M_end())
2304 && !_M_impl
._M_key_compare(__k
, _S_key(_M_rightmost())))
2305 return _Res(0, _M_rightmost());
2307 return _M_get_insert_equal_pos(__k
);
2309 else if (!_M_impl
._M_key_compare(_S_key(__pos
._M_node
), __k
))
2311 // First, try before...
2312 iterator __before
= __pos
;
2313 if (__pos
._M_node
== _M_leftmost()) // begin()
2314 return _Res(_M_leftmost(), _M_leftmost());
2315 else if (!_M_impl
._M_key_compare(__k
, _S_key((--__before
)._M_node
)))
2317 if (_S_right(__before
._M_node
) == 0)
2318 return _Res(0, __before
._M_node
);
2320 return _Res(__pos
._M_node
, __pos
._M_node
);
2323 return _M_get_insert_equal_pos(__k
);
2327 // ... then try after.
2328 iterator __after
= __pos
;
2329 if (__pos
._M_node
== _M_rightmost())
2330 return _Res(0, _M_rightmost());
2331 else if (!_M_impl
._M_key_compare(_S_key((++__after
)._M_node
), __k
))
2333 if (_S_right(__pos
._M_node
) == 0)
2334 return _Res(0, __pos
._M_node
);
2336 return _Res(__after
._M_node
, __after
._M_node
);
2343 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2344 typename _Compare
, typename _Alloc
>
2345 #if __cplusplus >= 201103L
2346 template<typename _Arg
, typename _NodeGen
>
2348 template<typename _NodeGen
>
2350 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::iterator
2351 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2352 _M_insert_equal_(const_iterator __position
,
2353 #if __cplusplus >= 201103L
2358 _NodeGen
& __node_gen
)
2360 pair
<_Base_ptr
, _Base_ptr
> __res
2361 = _M_get_insert_hint_equal_pos(__position
, _KeyOfValue()(__v
));
2364 return _M_insert_(__res
.first
, __res
.second
,
2365 _GLIBCXX_FORWARD(_Arg
, __v
),
2368 return _M_insert_equal_lower(_GLIBCXX_FORWARD(_Arg
, __v
));
2371 #if __cplusplus >= 201103L
2372 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2373 typename _Compare
, typename _Alloc
>
2375 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2376 _M_insert_node(_Base_ptr __x
, _Base_ptr __p
, _Link_type __z
)
2379 bool __insert_left
= (__x
!= 0 || __p
== _M_end()
2380 || _M_impl
._M_key_compare(_S_key(__z
),
2383 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
2384 this->_M_impl
._M_header
);
2385 ++_M_impl
._M_node_count
;
2386 return iterator(__z
);
2389 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2390 typename _Compare
, typename _Alloc
>
2392 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2393 _M_insert_lower_node(_Base_ptr __p
, _Link_type __z
)
2396 bool __insert_left
= (__p
== _M_end()
2397 || !_M_impl
._M_key_compare(_S_key(__p
),
2400 _Rb_tree_insert_and_rebalance(__insert_left
, __z
, __p
,
2401 this->_M_impl
._M_header
);
2402 ++_M_impl
._M_node_count
;
2403 return iterator(__z
);
2406 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2407 typename _Compare
, typename _Alloc
>
2409 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2410 _M_insert_equal_lower_node(_Link_type __z
)
2413 _Link_type __x
= _M_begin();
2414 _Base_ptr __y
= _M_end();
2418 __x
= !_M_impl
._M_key_compare(_S_key(__x
), _S_key(__z
)) ?
2419 _S_left(__x
) : _S_right(__x
);
2421 return _M_insert_lower_node(__y
, __z
);
2424 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2425 typename _Compare
, typename _Alloc
>
2426 template<typename
... _Args
>
2428 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2429 _M_emplace_unique(_Args
&&... __args
)
2430 -> pair
<iterator
, bool>
2432 _Auto_node
__z(*this, std::forward
<_Args
>(__args
)...);
2433 auto __res
= _M_get_insert_unique_pos(__z
._M_key());
2435 return {__z
._M_insert(__res
), true};
2436 return {iterator(__res
.first
), false};
2439 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2440 typename _Compare
, typename _Alloc
>
2441 template<typename
... _Args
>
2443 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2444 _M_emplace_equal(_Args
&&... __args
)
2447 _Auto_node
__z(*this, std::forward
<_Args
>(__args
)...);
2448 auto __res
= _M_get_insert_equal_pos(__z
._M_key());
2449 return __z
._M_insert(__res
);
2452 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2453 typename _Compare
, typename _Alloc
>
2454 template<typename
... _Args
>
2456 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2457 _M_emplace_hint_unique(const_iterator __pos
, _Args
&&... __args
)
2460 _Auto_node
__z(*this, std::forward
<_Args
>(__args
)...);
2461 auto __res
= _M_get_insert_hint_unique_pos(__pos
, __z
._M_key());
2463 return __z
._M_insert(__res
);
2464 return iterator(__res
.first
);
2467 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2468 typename _Compare
, typename _Alloc
>
2469 template<typename
... _Args
>
2471 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2472 _M_emplace_hint_equal(const_iterator __pos
, _Args
&&... __args
)
2475 _Auto_node
__z(*this, std::forward
<_Args
>(__args
)...);
2476 auto __res
= _M_get_insert_hint_equal_pos(__pos
, __z
._M_key());
2478 return __z
._M_insert(__res
);
2479 return __z
._M_insert_equal_lower();
2484 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2485 typename _Compare
, typename _Alloc
>
2487 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2488 _M_erase_aux(const_iterator __position
)
2491 static_cast<_Link_type
>(_Rb_tree_rebalance_for_erase
2492 (const_cast<_Base_ptr
>(__position
._M_node
),
2493 this->_M_impl
._M_header
));
2495 --_M_impl
._M_node_count
;
2498 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2499 typename _Compare
, typename _Alloc
>
2501 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2502 _M_erase_aux(const_iterator __first
, const_iterator __last
)
2504 if (__first
== begin() && __last
== end())
2507 while (__first
!= __last
)
2508 _M_erase_aux(__first
++);
2511 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2512 typename _Compare
, typename _Alloc
>
2513 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::size_type
2514 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2515 erase(const _Key
& __x
)
2517 pair
<iterator
, iterator
> __p
= equal_range(__x
);
2518 const size_type __old_size
= size();
2519 _M_erase_aux(__p
.first
, __p
.second
);
2520 return __old_size
- size();
2523 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2524 typename _Compare
, typename _Alloc
>
2525 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2526 _Compare
, _Alloc
>::iterator
2527 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2528 find(const _Key
& __k
)
2530 iterator __j
= _M_lower_bound(_M_begin(), _M_end(), __k
);
2531 return (__j
== end()
2532 || _M_impl
._M_key_compare(__k
,
2533 _S_key(__j
._M_node
))) ? end() : __j
;
2536 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2537 typename _Compare
, typename _Alloc
>
2538 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
,
2539 _Compare
, _Alloc
>::const_iterator
2540 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2541 find(const _Key
& __k
) const
2543 const_iterator __j
= _M_lower_bound(_M_begin(), _M_end(), __k
);
2544 return (__j
== end()
2545 || _M_impl
._M_key_compare(__k
,
2546 _S_key(__j
._M_node
))) ? end() : __j
;
2549 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2550 typename _Compare
, typename _Alloc
>
2551 typename _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::size_type
2552 _Rb_tree
<_Key
, _Val
, _KeyOfValue
, _Compare
, _Alloc
>::
2553 count(const _Key
& __k
) const
2555 pair
<const_iterator
, const_iterator
> __p
= equal_range(__k
);
2556 const size_type __n
= std::distance(__p
.first
, __p
.second
);
2560 _GLIBCXX_PURE
unsigned int
2561 _Rb_tree_black_count(const _Rb_tree_node_base
* __node
,
2562 const _Rb_tree_node_base
* __root
) throw ();
2564 template<typename _Key
, typename _Val
, typename _KeyOfValue
,
2565 typename _Compare
, typename _Alloc
>
2567 _Rb_tree
<_Key
,_Val
,_KeyOfValue
,_Compare
,_Alloc
>::__rb_verify() const
2569 if (_M_impl
._M_node_count
== 0 || begin() == end())
2570 return _M_impl
._M_node_count
== 0 && begin() == end()
2571 && this->_M_impl
._M_header
._M_left
== _M_end()
2572 && this->_M_impl
._M_header
._M_right
== _M_end();
2574 unsigned int __len
= _Rb_tree_black_count(_M_leftmost(), _M_root());
2575 for (const_iterator __it
= begin(); __it
!= end(); ++__it
)
2577 _Const_Link_type __x
= static_cast<_Const_Link_type
>(__it
._M_node
);
2578 _Const_Link_type __L
= _S_left(__x
);
2579 _Const_Link_type __R
= _S_right(__x
);
2581 if (__x
->_M_color
== _S_red
)
2582 if ((__L
&& __L
->_M_color
== _S_red
)
2583 || (__R
&& __R
->_M_color
== _S_red
))
2586 if (__L
&& _M_impl
._M_key_compare(_S_key(__x
), _S_key(__L
)))
2588 if (__R
&& _M_impl
._M_key_compare(_S_key(__R
), _S_key(__x
)))
2591 if (!__L
&& !__R
&& _Rb_tree_black_count(__x
, _M_root()) != __len
)
2595 if (_M_leftmost() != _Rb_tree_node_base::_S_minimum(_M_root()))
2597 if (_M_rightmost() != _Rb_tree_node_base::_S_maximum(_M_root()))
2602 #if __cplusplus > 201402L
2603 // Allow access to internals of compatible _Rb_tree specializations.
2604 template<typename _Key
, typename _Val
, typename _Sel
, typename _Cmp1
,
2605 typename _Alloc
, typename _Cmp2
>
2606 struct _Rb_tree_merge_helper
<_Rb_tree
<_Key
, _Val
, _Sel
, _Cmp1
, _Alloc
>,
2610 friend class _Rb_tree
<_Key
, _Val
, _Sel
, _Cmp1
, _Alloc
>;
2613 _S_get_impl(_Rb_tree
<_Key
, _Val
, _Sel
, _Cmp2
, _Alloc
>& __tree
)
2614 { return __tree
._M_impl
; }
2618 _GLIBCXX_END_NAMESPACE_VERSION