xref: /aosp_15_r20/external/libcxx/include/__tree (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard Worker// -*- C++ -*-
2*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
3*58b9f456SAndroid Build Coastguard Worker//
4*58b9f456SAndroid Build Coastguard Worker//                     The LLVM Compiler Infrastructure
5*58b9f456SAndroid Build Coastguard Worker//
6*58b9f456SAndroid Build Coastguard Worker// This file is dual licensed under the MIT and the University of Illinois Open
7*58b9f456SAndroid Build Coastguard Worker// Source Licenses. See LICENSE.TXT for details.
8*58b9f456SAndroid Build Coastguard Worker//
9*58b9f456SAndroid Build Coastguard Worker//===----------------------------------------------------------------------===//
10*58b9f456SAndroid Build Coastguard Worker
11*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP___TREE
12*58b9f456SAndroid Build Coastguard Worker#define _LIBCPP___TREE
13*58b9f456SAndroid Build Coastguard Worker
14*58b9f456SAndroid Build Coastguard Worker#include <__config>
15*58b9f456SAndroid Build Coastguard Worker#include <iterator>
16*58b9f456SAndroid Build Coastguard Worker#include <memory>
17*58b9f456SAndroid Build Coastguard Worker#include <stdexcept>
18*58b9f456SAndroid Build Coastguard Worker#include <algorithm>
19*58b9f456SAndroid Build Coastguard Worker
20*58b9f456SAndroid Build Coastguard Worker#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21*58b9f456SAndroid Build Coastguard Worker#pragma GCC system_header
22*58b9f456SAndroid Build Coastguard Worker#endif
23*58b9f456SAndroid Build Coastguard Worker
24*58b9f456SAndroid Build Coastguard Worker_LIBCPP_PUSH_MACROS
25*58b9f456SAndroid Build Coastguard Worker#include <__undef_macros>
26*58b9f456SAndroid Build Coastguard Worker
27*58b9f456SAndroid Build Coastguard Worker
28*58b9f456SAndroid Build Coastguard Worker_LIBCPP_BEGIN_NAMESPACE_STD
29*58b9f456SAndroid Build Coastguard Worker
30*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator> class __tree;
31*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _NodePtr, class _DiffType>
32*58b9f456SAndroid Build Coastguard Worker    class _LIBCPP_TEMPLATE_VIS __tree_iterator;
33*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _ConstNodePtr, class _DiffType>
34*58b9f456SAndroid Build Coastguard Worker    class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
35*58b9f456SAndroid Build Coastguard Worker
36*58b9f456SAndroid Build Coastguard Workertemplate <class _Pointer> class __tree_end_node;
37*58b9f456SAndroid Build Coastguard Workertemplate <class _VoidPtr> class __tree_node_base;
38*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _VoidPtr> class __tree_node;
39*58b9f456SAndroid Build Coastguard Worker
40*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class _Value>
41*58b9f456SAndroid Build Coastguard Workerstruct __value_type;
42*58b9f456SAndroid Build Coastguard Worker
43*58b9f456SAndroid Build Coastguard Workertemplate <class _Allocator> class __map_node_destructor;
44*58b9f456SAndroid Build Coastguard Workertemplate <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
45*58b9f456SAndroid Build Coastguard Workertemplate <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
46*58b9f456SAndroid Build Coastguard Worker
47*58b9f456SAndroid Build Coastguard Worker/*
48*58b9f456SAndroid Build Coastguard Worker
49*58b9f456SAndroid Build Coastguard Worker_NodePtr algorithms
50*58b9f456SAndroid Build Coastguard Worker
51*58b9f456SAndroid Build Coastguard WorkerThe algorithms taking _NodePtr are red black tree algorithms.  Those
52*58b9f456SAndroid Build Coastguard Workeralgorithms taking a parameter named __root should assume that __root
53*58b9f456SAndroid Build Coastguard Workerpoints to a proper red black tree (unless otherwise specified).
54*58b9f456SAndroid Build Coastguard Worker
55*58b9f456SAndroid Build Coastguard WorkerEach algorithm herein assumes that __root->__parent_ points to a non-null
56*58b9f456SAndroid Build Coastguard Workerstructure which has a member __left_ which points back to __root.  No other
57*58b9f456SAndroid Build Coastguard Workermember is read or written to at __root->__parent_.
58*58b9f456SAndroid Build Coastguard Worker
59*58b9f456SAndroid Build Coastguard Worker__root->__parent_ will be referred to below (in comments only) as end_node.
60*58b9f456SAndroid Build Coastguard Workerend_node->__left_ is an externably accessible lvalue for __root, and can be
61*58b9f456SAndroid Build Coastguard Workerchanged by node insertion and removal (without explicit reference to end_node).
62*58b9f456SAndroid Build Coastguard Worker
63*58b9f456SAndroid Build Coastguard WorkerAll nodes (with the exception of end_node), even the node referred to as
64*58b9f456SAndroid Build Coastguard Worker__root, have a non-null __parent_ field.
65*58b9f456SAndroid Build Coastguard Worker
66*58b9f456SAndroid Build Coastguard Worker*/
67*58b9f456SAndroid Build Coastguard Worker
68*58b9f456SAndroid Build Coastguard Worker// Returns:  true if __x is a left child of its parent, else false
69*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x != nullptr.
70*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
71*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
72*58b9f456SAndroid Build Coastguard Workerbool
73*58b9f456SAndroid Build Coastguard Worker__tree_is_left_child(_NodePtr __x) _NOEXCEPT
74*58b9f456SAndroid Build Coastguard Worker{
75*58b9f456SAndroid Build Coastguard Worker    return __x == __x->__parent_->__left_;
76*58b9f456SAndroid Build Coastguard Worker}
77*58b9f456SAndroid Build Coastguard Worker
78*58b9f456SAndroid Build Coastguard Worker// Determines if the subtree rooted at __x is a proper red black subtree.  If
79*58b9f456SAndroid Build Coastguard Worker//    __x is a proper subtree, returns the black height (null counts as 1).  If
80*58b9f456SAndroid Build Coastguard Worker//    __x is an improper subtree, returns 0.
81*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
82*58b9f456SAndroid Build Coastguard Workerunsigned
83*58b9f456SAndroid Build Coastguard Worker__tree_sub_invariant(_NodePtr __x)
84*58b9f456SAndroid Build Coastguard Worker{
85*58b9f456SAndroid Build Coastguard Worker    if (__x == nullptr)
86*58b9f456SAndroid Build Coastguard Worker        return 1;
87*58b9f456SAndroid Build Coastguard Worker    // parent consistency checked by caller
88*58b9f456SAndroid Build Coastguard Worker    // check __x->__left_ consistency
89*58b9f456SAndroid Build Coastguard Worker    if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
90*58b9f456SAndroid Build Coastguard Worker        return 0;
91*58b9f456SAndroid Build Coastguard Worker    // check __x->__right_ consistency
92*58b9f456SAndroid Build Coastguard Worker    if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
93*58b9f456SAndroid Build Coastguard Worker        return 0;
94*58b9f456SAndroid Build Coastguard Worker    // check __x->__left_ != __x->__right_ unless both are nullptr
95*58b9f456SAndroid Build Coastguard Worker    if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
96*58b9f456SAndroid Build Coastguard Worker        return 0;
97*58b9f456SAndroid Build Coastguard Worker    // If this is red, neither child can be red
98*58b9f456SAndroid Build Coastguard Worker    if (!__x->__is_black_)
99*58b9f456SAndroid Build Coastguard Worker    {
100*58b9f456SAndroid Build Coastguard Worker        if (__x->__left_ && !__x->__left_->__is_black_)
101*58b9f456SAndroid Build Coastguard Worker            return 0;
102*58b9f456SAndroid Build Coastguard Worker        if (__x->__right_ && !__x->__right_->__is_black_)
103*58b9f456SAndroid Build Coastguard Worker            return 0;
104*58b9f456SAndroid Build Coastguard Worker    }
105*58b9f456SAndroid Build Coastguard Worker    unsigned __h = __tree_sub_invariant(__x->__left_);
106*58b9f456SAndroid Build Coastguard Worker    if (__h == 0)
107*58b9f456SAndroid Build Coastguard Worker        return 0;  // invalid left subtree
108*58b9f456SAndroid Build Coastguard Worker    if (__h != __tree_sub_invariant(__x->__right_))
109*58b9f456SAndroid Build Coastguard Worker        return 0;  // invalid or different height right subtree
110*58b9f456SAndroid Build Coastguard Worker    return __h + __x->__is_black_;  // return black height of this node
111*58b9f456SAndroid Build Coastguard Worker}
112*58b9f456SAndroid Build Coastguard Worker
113*58b9f456SAndroid Build Coastguard Worker// Determines if the red black tree rooted at __root is a proper red black tree.
114*58b9f456SAndroid Build Coastguard Worker//    __root == nullptr is a proper tree.  Returns true is __root is a proper
115*58b9f456SAndroid Build Coastguard Worker//    red black tree, else returns false.
116*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
117*58b9f456SAndroid Build Coastguard Workerbool
118*58b9f456SAndroid Build Coastguard Worker__tree_invariant(_NodePtr __root)
119*58b9f456SAndroid Build Coastguard Worker{
120*58b9f456SAndroid Build Coastguard Worker    if (__root == nullptr)
121*58b9f456SAndroid Build Coastguard Worker        return true;
122*58b9f456SAndroid Build Coastguard Worker    // check __x->__parent_ consistency
123*58b9f456SAndroid Build Coastguard Worker    if (__root->__parent_ == nullptr)
124*58b9f456SAndroid Build Coastguard Worker        return false;
125*58b9f456SAndroid Build Coastguard Worker    if (!__tree_is_left_child(__root))
126*58b9f456SAndroid Build Coastguard Worker        return false;
127*58b9f456SAndroid Build Coastguard Worker    // root must be black
128*58b9f456SAndroid Build Coastguard Worker    if (!__root->__is_black_)
129*58b9f456SAndroid Build Coastguard Worker        return false;
130*58b9f456SAndroid Build Coastguard Worker    // do normal node checks
131*58b9f456SAndroid Build Coastguard Worker    return __tree_sub_invariant(__root) != 0;
132*58b9f456SAndroid Build Coastguard Worker}
133*58b9f456SAndroid Build Coastguard Worker
134*58b9f456SAndroid Build Coastguard Worker// Returns:  pointer to the left-most node under __x.
135*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x != nullptr.
136*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
137*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
138*58b9f456SAndroid Build Coastguard Worker_NodePtr
139*58b9f456SAndroid Build Coastguard Worker__tree_min(_NodePtr __x) _NOEXCEPT
140*58b9f456SAndroid Build Coastguard Worker{
141*58b9f456SAndroid Build Coastguard Worker    while (__x->__left_ != nullptr)
142*58b9f456SAndroid Build Coastguard Worker        __x = __x->__left_;
143*58b9f456SAndroid Build Coastguard Worker    return __x;
144*58b9f456SAndroid Build Coastguard Worker}
145*58b9f456SAndroid Build Coastguard Worker
146*58b9f456SAndroid Build Coastguard Worker// Returns:  pointer to the right-most node under __x.
147*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x != nullptr.
148*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
149*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
150*58b9f456SAndroid Build Coastguard Worker_NodePtr
151*58b9f456SAndroid Build Coastguard Worker__tree_max(_NodePtr __x) _NOEXCEPT
152*58b9f456SAndroid Build Coastguard Worker{
153*58b9f456SAndroid Build Coastguard Worker    while (__x->__right_ != nullptr)
154*58b9f456SAndroid Build Coastguard Worker        __x = __x->__right_;
155*58b9f456SAndroid Build Coastguard Worker    return __x;
156*58b9f456SAndroid Build Coastguard Worker}
157*58b9f456SAndroid Build Coastguard Worker
158*58b9f456SAndroid Build Coastguard Worker// Returns:  pointer to the next in-order node after __x.
159*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x != nullptr.
160*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
161*58b9f456SAndroid Build Coastguard Worker_NodePtr
162*58b9f456SAndroid Build Coastguard Worker__tree_next(_NodePtr __x) _NOEXCEPT
163*58b9f456SAndroid Build Coastguard Worker{
164*58b9f456SAndroid Build Coastguard Worker    if (__x->__right_ != nullptr)
165*58b9f456SAndroid Build Coastguard Worker        return __tree_min(__x->__right_);
166*58b9f456SAndroid Build Coastguard Worker    while (!__tree_is_left_child(__x))
167*58b9f456SAndroid Build Coastguard Worker        __x = __x->__parent_unsafe();
168*58b9f456SAndroid Build Coastguard Worker    return __x->__parent_unsafe();
169*58b9f456SAndroid Build Coastguard Worker}
170*58b9f456SAndroid Build Coastguard Worker
171*58b9f456SAndroid Build Coastguard Workertemplate <class _EndNodePtr, class _NodePtr>
172*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
173*58b9f456SAndroid Build Coastguard Worker_EndNodePtr
174*58b9f456SAndroid Build Coastguard Worker__tree_next_iter(_NodePtr __x) _NOEXCEPT
175*58b9f456SAndroid Build Coastguard Worker{
176*58b9f456SAndroid Build Coastguard Worker    if (__x->__right_ != nullptr)
177*58b9f456SAndroid Build Coastguard Worker        return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
178*58b9f456SAndroid Build Coastguard Worker    while (!__tree_is_left_child(__x))
179*58b9f456SAndroid Build Coastguard Worker        __x = __x->__parent_unsafe();
180*58b9f456SAndroid Build Coastguard Worker    return static_cast<_EndNodePtr>(__x->__parent_);
181*58b9f456SAndroid Build Coastguard Worker}
182*58b9f456SAndroid Build Coastguard Worker
183*58b9f456SAndroid Build Coastguard Worker// Returns:  pointer to the previous in-order node before __x.
184*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x != nullptr.
185*58b9f456SAndroid Build Coastguard Worker// Note: __x may be the end node.
186*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr, class _EndNodePtr>
187*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
188*58b9f456SAndroid Build Coastguard Worker_NodePtr
189*58b9f456SAndroid Build Coastguard Worker__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
190*58b9f456SAndroid Build Coastguard Worker{
191*58b9f456SAndroid Build Coastguard Worker    if (__x->__left_ != nullptr)
192*58b9f456SAndroid Build Coastguard Worker        return __tree_max(__x->__left_);
193*58b9f456SAndroid Build Coastguard Worker    _NodePtr __xx = static_cast<_NodePtr>(__x);
194*58b9f456SAndroid Build Coastguard Worker    while (__tree_is_left_child(__xx))
195*58b9f456SAndroid Build Coastguard Worker        __xx = __xx->__parent_unsafe();
196*58b9f456SAndroid Build Coastguard Worker    return __xx->__parent_unsafe();
197*58b9f456SAndroid Build Coastguard Worker}
198*58b9f456SAndroid Build Coastguard Worker
199*58b9f456SAndroid Build Coastguard Worker// Returns:  pointer to a node which has no children
200*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x != nullptr.
201*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
202*58b9f456SAndroid Build Coastguard Worker_NodePtr
203*58b9f456SAndroid Build Coastguard Worker__tree_leaf(_NodePtr __x) _NOEXCEPT
204*58b9f456SAndroid Build Coastguard Worker{
205*58b9f456SAndroid Build Coastguard Worker    while (true)
206*58b9f456SAndroid Build Coastguard Worker    {
207*58b9f456SAndroid Build Coastguard Worker        if (__x->__left_ != nullptr)
208*58b9f456SAndroid Build Coastguard Worker        {
209*58b9f456SAndroid Build Coastguard Worker            __x = __x->__left_;
210*58b9f456SAndroid Build Coastguard Worker            continue;
211*58b9f456SAndroid Build Coastguard Worker        }
212*58b9f456SAndroid Build Coastguard Worker        if (__x->__right_ != nullptr)
213*58b9f456SAndroid Build Coastguard Worker        {
214*58b9f456SAndroid Build Coastguard Worker            __x = __x->__right_;
215*58b9f456SAndroid Build Coastguard Worker            continue;
216*58b9f456SAndroid Build Coastguard Worker        }
217*58b9f456SAndroid Build Coastguard Worker        break;
218*58b9f456SAndroid Build Coastguard Worker    }
219*58b9f456SAndroid Build Coastguard Worker    return __x;
220*58b9f456SAndroid Build Coastguard Worker}
221*58b9f456SAndroid Build Coastguard Worker
222*58b9f456SAndroid Build Coastguard Worker// Effects:  Makes __x->__right_ the subtree root with __x as its left child
223*58b9f456SAndroid Build Coastguard Worker//           while preserving in-order order.
224*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x->__right_ != nullptr
225*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
226*58b9f456SAndroid Build Coastguard Workervoid
227*58b9f456SAndroid Build Coastguard Worker__tree_left_rotate(_NodePtr __x) _NOEXCEPT
228*58b9f456SAndroid Build Coastguard Worker{
229*58b9f456SAndroid Build Coastguard Worker    _NodePtr __y = __x->__right_;
230*58b9f456SAndroid Build Coastguard Worker    __x->__right_ = __y->__left_;
231*58b9f456SAndroid Build Coastguard Worker    if (__x->__right_ != nullptr)
232*58b9f456SAndroid Build Coastguard Worker        __x->__right_->__set_parent(__x);
233*58b9f456SAndroid Build Coastguard Worker    __y->__parent_ = __x->__parent_;
234*58b9f456SAndroid Build Coastguard Worker    if (__tree_is_left_child(__x))
235*58b9f456SAndroid Build Coastguard Worker        __x->__parent_->__left_ = __y;
236*58b9f456SAndroid Build Coastguard Worker    else
237*58b9f456SAndroid Build Coastguard Worker        __x->__parent_unsafe()->__right_ = __y;
238*58b9f456SAndroid Build Coastguard Worker    __y->__left_ = __x;
239*58b9f456SAndroid Build Coastguard Worker    __x->__set_parent(__y);
240*58b9f456SAndroid Build Coastguard Worker}
241*58b9f456SAndroid Build Coastguard Worker
242*58b9f456SAndroid Build Coastguard Worker// Effects:  Makes __x->__left_ the subtree root with __x as its right child
243*58b9f456SAndroid Build Coastguard Worker//           while preserving in-order order.
244*58b9f456SAndroid Build Coastguard Worker// Precondition:  __x->__left_ != nullptr
245*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
246*58b9f456SAndroid Build Coastguard Workervoid
247*58b9f456SAndroid Build Coastguard Worker__tree_right_rotate(_NodePtr __x) _NOEXCEPT
248*58b9f456SAndroid Build Coastguard Worker{
249*58b9f456SAndroid Build Coastguard Worker    _NodePtr __y = __x->__left_;
250*58b9f456SAndroid Build Coastguard Worker    __x->__left_ = __y->__right_;
251*58b9f456SAndroid Build Coastguard Worker    if (__x->__left_ != nullptr)
252*58b9f456SAndroid Build Coastguard Worker        __x->__left_->__set_parent(__x);
253*58b9f456SAndroid Build Coastguard Worker    __y->__parent_ = __x->__parent_;
254*58b9f456SAndroid Build Coastguard Worker    if (__tree_is_left_child(__x))
255*58b9f456SAndroid Build Coastguard Worker        __x->__parent_->__left_ = __y;
256*58b9f456SAndroid Build Coastguard Worker    else
257*58b9f456SAndroid Build Coastguard Worker        __x->__parent_unsafe()->__right_ = __y;
258*58b9f456SAndroid Build Coastguard Worker    __y->__right_ = __x;
259*58b9f456SAndroid Build Coastguard Worker    __x->__set_parent(__y);
260*58b9f456SAndroid Build Coastguard Worker}
261*58b9f456SAndroid Build Coastguard Worker
262*58b9f456SAndroid Build Coastguard Worker// Effects:  Rebalances __root after attaching __x to a leaf.
263*58b9f456SAndroid Build Coastguard Worker// Precondition:  __root != nulptr && __x != nullptr.
264*58b9f456SAndroid Build Coastguard Worker//                __x has no children.
265*58b9f456SAndroid Build Coastguard Worker//                __x == __root or == a direct or indirect child of __root.
266*58b9f456SAndroid Build Coastguard Worker//                If __x were to be unlinked from __root (setting __root to
267*58b9f456SAndroid Build Coastguard Worker//                  nullptr if __root == __x), __tree_invariant(__root) == true.
268*58b9f456SAndroid Build Coastguard Worker// Postcondition: __tree_invariant(end_node->__left_) == true.  end_node->__left_
269*58b9f456SAndroid Build Coastguard Worker//                may be different than the value passed in as __root.
270*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
271*58b9f456SAndroid Build Coastguard Workervoid
272*58b9f456SAndroid Build Coastguard Worker__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
273*58b9f456SAndroid Build Coastguard Worker{
274*58b9f456SAndroid Build Coastguard Worker    __x->__is_black_ = __x == __root;
275*58b9f456SAndroid Build Coastguard Worker    while (__x != __root && !__x->__parent_unsafe()->__is_black_)
276*58b9f456SAndroid Build Coastguard Worker    {
277*58b9f456SAndroid Build Coastguard Worker        // __x->__parent_ != __root because __x->__parent_->__is_black == false
278*58b9f456SAndroid Build Coastguard Worker        if (__tree_is_left_child(__x->__parent_unsafe()))
279*58b9f456SAndroid Build Coastguard Worker        {
280*58b9f456SAndroid Build Coastguard Worker            _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
281*58b9f456SAndroid Build Coastguard Worker            if (__y != nullptr && !__y->__is_black_)
282*58b9f456SAndroid Build Coastguard Worker            {
283*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
284*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = true;
285*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
286*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = __x == __root;
287*58b9f456SAndroid Build Coastguard Worker                __y->__is_black_ = true;
288*58b9f456SAndroid Build Coastguard Worker            }
289*58b9f456SAndroid Build Coastguard Worker            else
290*58b9f456SAndroid Build Coastguard Worker            {
291*58b9f456SAndroid Build Coastguard Worker                if (!__tree_is_left_child(__x))
292*58b9f456SAndroid Build Coastguard Worker                {
293*58b9f456SAndroid Build Coastguard Worker                    __x = __x->__parent_unsafe();
294*58b9f456SAndroid Build Coastguard Worker                    __tree_left_rotate(__x);
295*58b9f456SAndroid Build Coastguard Worker                }
296*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
297*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = true;
298*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
299*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = false;
300*58b9f456SAndroid Build Coastguard Worker                __tree_right_rotate(__x);
301*58b9f456SAndroid Build Coastguard Worker                break;
302*58b9f456SAndroid Build Coastguard Worker            }
303*58b9f456SAndroid Build Coastguard Worker        }
304*58b9f456SAndroid Build Coastguard Worker        else
305*58b9f456SAndroid Build Coastguard Worker        {
306*58b9f456SAndroid Build Coastguard Worker            _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
307*58b9f456SAndroid Build Coastguard Worker            if (__y != nullptr && !__y->__is_black_)
308*58b9f456SAndroid Build Coastguard Worker            {
309*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
310*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = true;
311*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
312*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = __x == __root;
313*58b9f456SAndroid Build Coastguard Worker                __y->__is_black_ = true;
314*58b9f456SAndroid Build Coastguard Worker            }
315*58b9f456SAndroid Build Coastguard Worker            else
316*58b9f456SAndroid Build Coastguard Worker            {
317*58b9f456SAndroid Build Coastguard Worker                if (__tree_is_left_child(__x))
318*58b9f456SAndroid Build Coastguard Worker                {
319*58b9f456SAndroid Build Coastguard Worker                    __x = __x->__parent_unsafe();
320*58b9f456SAndroid Build Coastguard Worker                    __tree_right_rotate(__x);
321*58b9f456SAndroid Build Coastguard Worker                }
322*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
323*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = true;
324*58b9f456SAndroid Build Coastguard Worker                __x = __x->__parent_unsafe();
325*58b9f456SAndroid Build Coastguard Worker                __x->__is_black_ = false;
326*58b9f456SAndroid Build Coastguard Worker                __tree_left_rotate(__x);
327*58b9f456SAndroid Build Coastguard Worker                break;
328*58b9f456SAndroid Build Coastguard Worker            }
329*58b9f456SAndroid Build Coastguard Worker        }
330*58b9f456SAndroid Build Coastguard Worker    }
331*58b9f456SAndroid Build Coastguard Worker}
332*58b9f456SAndroid Build Coastguard Worker
333*58b9f456SAndroid Build Coastguard Worker// Precondition:  __root != nullptr && __z != nullptr.
334*58b9f456SAndroid Build Coastguard Worker//                __tree_invariant(__root) == true.
335*58b9f456SAndroid Build Coastguard Worker//                __z == __root or == a direct or indirect child of __root.
336*58b9f456SAndroid Build Coastguard Worker// Effects:  unlinks __z from the tree rooted at __root, rebalancing as needed.
337*58b9f456SAndroid Build Coastguard Worker// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
338*58b9f456SAndroid Build Coastguard Worker//                nor any of its children refer to __z.  end_node->__left_
339*58b9f456SAndroid Build Coastguard Worker//                may be different than the value passed in as __root.
340*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr>
341*58b9f456SAndroid Build Coastguard Workervoid
342*58b9f456SAndroid Build Coastguard Worker__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
343*58b9f456SAndroid Build Coastguard Worker{
344*58b9f456SAndroid Build Coastguard Worker    // __z will be removed from the tree.  Client still needs to destruct/deallocate it
345*58b9f456SAndroid Build Coastguard Worker    // __y is either __z, or if __z has two children, __tree_next(__z).
346*58b9f456SAndroid Build Coastguard Worker    // __y will have at most one child.
347*58b9f456SAndroid Build Coastguard Worker    // __y will be the initial hole in the tree (make the hole at a leaf)
348*58b9f456SAndroid Build Coastguard Worker    _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
349*58b9f456SAndroid Build Coastguard Worker                    __z : __tree_next(__z);
350*58b9f456SAndroid Build Coastguard Worker    // __x is __y's possibly null single child
351*58b9f456SAndroid Build Coastguard Worker    _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
352*58b9f456SAndroid Build Coastguard Worker    // __w is __x's possibly null uncle (will become __x's sibling)
353*58b9f456SAndroid Build Coastguard Worker    _NodePtr __w = nullptr;
354*58b9f456SAndroid Build Coastguard Worker    // link __x to __y's parent, and find __w
355*58b9f456SAndroid Build Coastguard Worker    if (__x != nullptr)
356*58b9f456SAndroid Build Coastguard Worker        __x->__parent_ = __y->__parent_;
357*58b9f456SAndroid Build Coastguard Worker    if (__tree_is_left_child(__y))
358*58b9f456SAndroid Build Coastguard Worker    {
359*58b9f456SAndroid Build Coastguard Worker        __y->__parent_->__left_ = __x;
360*58b9f456SAndroid Build Coastguard Worker        if (__y != __root)
361*58b9f456SAndroid Build Coastguard Worker            __w = __y->__parent_unsafe()->__right_;
362*58b9f456SAndroid Build Coastguard Worker        else
363*58b9f456SAndroid Build Coastguard Worker            __root = __x;  // __w == nullptr
364*58b9f456SAndroid Build Coastguard Worker    }
365*58b9f456SAndroid Build Coastguard Worker    else
366*58b9f456SAndroid Build Coastguard Worker    {
367*58b9f456SAndroid Build Coastguard Worker        __y->__parent_unsafe()->__right_ = __x;
368*58b9f456SAndroid Build Coastguard Worker        // __y can't be root if it is a right child
369*58b9f456SAndroid Build Coastguard Worker        __w = __y->__parent_->__left_;
370*58b9f456SAndroid Build Coastguard Worker    }
371*58b9f456SAndroid Build Coastguard Worker    bool __removed_black = __y->__is_black_;
372*58b9f456SAndroid Build Coastguard Worker    // If we didn't remove __z, do so now by splicing in __y for __z,
373*58b9f456SAndroid Build Coastguard Worker    //    but copy __z's color.  This does not impact __x or __w.
374*58b9f456SAndroid Build Coastguard Worker    if (__y != __z)
375*58b9f456SAndroid Build Coastguard Worker    {
376*58b9f456SAndroid Build Coastguard Worker        // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
377*58b9f456SAndroid Build Coastguard Worker        __y->__parent_ = __z->__parent_;
378*58b9f456SAndroid Build Coastguard Worker        if (__tree_is_left_child(__z))
379*58b9f456SAndroid Build Coastguard Worker            __y->__parent_->__left_ = __y;
380*58b9f456SAndroid Build Coastguard Worker        else
381*58b9f456SAndroid Build Coastguard Worker            __y->__parent_unsafe()->__right_ = __y;
382*58b9f456SAndroid Build Coastguard Worker        __y->__left_ = __z->__left_;
383*58b9f456SAndroid Build Coastguard Worker        __y->__left_->__set_parent(__y);
384*58b9f456SAndroid Build Coastguard Worker        __y->__right_ = __z->__right_;
385*58b9f456SAndroid Build Coastguard Worker        if (__y->__right_ != nullptr)
386*58b9f456SAndroid Build Coastguard Worker            __y->__right_->__set_parent(__y);
387*58b9f456SAndroid Build Coastguard Worker        __y->__is_black_ = __z->__is_black_;
388*58b9f456SAndroid Build Coastguard Worker        if (__root == __z)
389*58b9f456SAndroid Build Coastguard Worker            __root = __y;
390*58b9f456SAndroid Build Coastguard Worker    }
391*58b9f456SAndroid Build Coastguard Worker    // There is no need to rebalance if we removed a red, or if we removed
392*58b9f456SAndroid Build Coastguard Worker    //     the last node.
393*58b9f456SAndroid Build Coastguard Worker    if (__removed_black && __root != nullptr)
394*58b9f456SAndroid Build Coastguard Worker    {
395*58b9f456SAndroid Build Coastguard Worker        // Rebalance:
396*58b9f456SAndroid Build Coastguard Worker        // __x has an implicit black color (transferred from the removed __y)
397*58b9f456SAndroid Build Coastguard Worker        //    associated with it, no matter what its color is.
398*58b9f456SAndroid Build Coastguard Worker        // If __x is __root (in which case it can't be null), it is supposed
399*58b9f456SAndroid Build Coastguard Worker        //    to be black anyway, and if it is doubly black, then the double
400*58b9f456SAndroid Build Coastguard Worker        //    can just be ignored.
401*58b9f456SAndroid Build Coastguard Worker        // If __x is red (in which case it can't be null), then it can absorb
402*58b9f456SAndroid Build Coastguard Worker        //    the implicit black just by setting its color to black.
403*58b9f456SAndroid Build Coastguard Worker        // Since __y was black and only had one child (which __x points to), __x
404*58b9f456SAndroid Build Coastguard Worker        //   is either red with no children, else null, otherwise __y would have
405*58b9f456SAndroid Build Coastguard Worker        //   different black heights under left and right pointers.
406*58b9f456SAndroid Build Coastguard Worker        // if (__x == __root || __x != nullptr && !__x->__is_black_)
407*58b9f456SAndroid Build Coastguard Worker        if (__x != nullptr)
408*58b9f456SAndroid Build Coastguard Worker            __x->__is_black_ = true;
409*58b9f456SAndroid Build Coastguard Worker        else
410*58b9f456SAndroid Build Coastguard Worker        {
411*58b9f456SAndroid Build Coastguard Worker            //  Else __x isn't root, and is "doubly black", even though it may
412*58b9f456SAndroid Build Coastguard Worker            //     be null.  __w can not be null here, else the parent would
413*58b9f456SAndroid Build Coastguard Worker            //     see a black height >= 2 on the __x side and a black height
414*58b9f456SAndroid Build Coastguard Worker            //     of 1 on the __w side (__w must be a non-null black or a red
415*58b9f456SAndroid Build Coastguard Worker            //     with a non-null black child).
416*58b9f456SAndroid Build Coastguard Worker            while (true)
417*58b9f456SAndroid Build Coastguard Worker            {
418*58b9f456SAndroid Build Coastguard Worker                if (!__tree_is_left_child(__w))  // if x is left child
419*58b9f456SAndroid Build Coastguard Worker                {
420*58b9f456SAndroid Build Coastguard Worker                    if (!__w->__is_black_)
421*58b9f456SAndroid Build Coastguard Worker                    {
422*58b9f456SAndroid Build Coastguard Worker                        __w->__is_black_ = true;
423*58b9f456SAndroid Build Coastguard Worker                        __w->__parent_unsafe()->__is_black_ = false;
424*58b9f456SAndroid Build Coastguard Worker                        __tree_left_rotate(__w->__parent_unsafe());
425*58b9f456SAndroid Build Coastguard Worker                        // __x is still valid
426*58b9f456SAndroid Build Coastguard Worker                        // reset __root only if necessary
427*58b9f456SAndroid Build Coastguard Worker                        if (__root == __w->__left_)
428*58b9f456SAndroid Build Coastguard Worker                            __root = __w;
429*58b9f456SAndroid Build Coastguard Worker                        // reset sibling, and it still can't be null
430*58b9f456SAndroid Build Coastguard Worker                        __w = __w->__left_->__right_;
431*58b9f456SAndroid Build Coastguard Worker                    }
432*58b9f456SAndroid Build Coastguard Worker                    // __w->__is_black_ is now true, __w may have null children
433*58b9f456SAndroid Build Coastguard Worker                    if ((__w->__left_  == nullptr || __w->__left_->__is_black_) &&
434*58b9f456SAndroid Build Coastguard Worker                        (__w->__right_ == nullptr || __w->__right_->__is_black_))
435*58b9f456SAndroid Build Coastguard Worker                    {
436*58b9f456SAndroid Build Coastguard Worker                        __w->__is_black_ = false;
437*58b9f456SAndroid Build Coastguard Worker                        __x = __w->__parent_unsafe();
438*58b9f456SAndroid Build Coastguard Worker                        // __x can no longer be null
439*58b9f456SAndroid Build Coastguard Worker                        if (__x == __root || !__x->__is_black_)
440*58b9f456SAndroid Build Coastguard Worker                        {
441*58b9f456SAndroid Build Coastguard Worker                            __x->__is_black_ = true;
442*58b9f456SAndroid Build Coastguard Worker                            break;
443*58b9f456SAndroid Build Coastguard Worker                        }
444*58b9f456SAndroid Build Coastguard Worker                        // reset sibling, and it still can't be null
445*58b9f456SAndroid Build Coastguard Worker                        __w = __tree_is_left_child(__x) ?
446*58b9f456SAndroid Build Coastguard Worker                                    __x->__parent_unsafe()->__right_ :
447*58b9f456SAndroid Build Coastguard Worker                                    __x->__parent_->__left_;
448*58b9f456SAndroid Build Coastguard Worker                        // continue;
449*58b9f456SAndroid Build Coastguard Worker                    }
450*58b9f456SAndroid Build Coastguard Worker                    else  // __w has a red child
451*58b9f456SAndroid Build Coastguard Worker                    {
452*58b9f456SAndroid Build Coastguard Worker                        if (__w->__right_ == nullptr || __w->__right_->__is_black_)
453*58b9f456SAndroid Build Coastguard Worker                        {
454*58b9f456SAndroid Build Coastguard Worker                            // __w left child is non-null and red
455*58b9f456SAndroid Build Coastguard Worker                            __w->__left_->__is_black_ = true;
456*58b9f456SAndroid Build Coastguard Worker                            __w->__is_black_ = false;
457*58b9f456SAndroid Build Coastguard Worker                            __tree_right_rotate(__w);
458*58b9f456SAndroid Build Coastguard Worker                            // __w is known not to be root, so root hasn't changed
459*58b9f456SAndroid Build Coastguard Worker                            // reset sibling, and it still can't be null
460*58b9f456SAndroid Build Coastguard Worker                            __w = __w->__parent_unsafe();
461*58b9f456SAndroid Build Coastguard Worker                        }
462*58b9f456SAndroid Build Coastguard Worker                        // __w has a right red child, left child may be null
463*58b9f456SAndroid Build Coastguard Worker                        __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
464*58b9f456SAndroid Build Coastguard Worker                        __w->__parent_unsafe()->__is_black_ = true;
465*58b9f456SAndroid Build Coastguard Worker                        __w->__right_->__is_black_ = true;
466*58b9f456SAndroid Build Coastguard Worker                        __tree_left_rotate(__w->__parent_unsafe());
467*58b9f456SAndroid Build Coastguard Worker                        break;
468*58b9f456SAndroid Build Coastguard Worker                    }
469*58b9f456SAndroid Build Coastguard Worker                }
470*58b9f456SAndroid Build Coastguard Worker                else
471*58b9f456SAndroid Build Coastguard Worker                {
472*58b9f456SAndroid Build Coastguard Worker                    if (!__w->__is_black_)
473*58b9f456SAndroid Build Coastguard Worker                    {
474*58b9f456SAndroid Build Coastguard Worker                        __w->__is_black_ = true;
475*58b9f456SAndroid Build Coastguard Worker                        __w->__parent_unsafe()->__is_black_ = false;
476*58b9f456SAndroid Build Coastguard Worker                        __tree_right_rotate(__w->__parent_unsafe());
477*58b9f456SAndroid Build Coastguard Worker                        // __x is still valid
478*58b9f456SAndroid Build Coastguard Worker                        // reset __root only if necessary
479*58b9f456SAndroid Build Coastguard Worker                        if (__root == __w->__right_)
480*58b9f456SAndroid Build Coastguard Worker                            __root = __w;
481*58b9f456SAndroid Build Coastguard Worker                        // reset sibling, and it still can't be null
482*58b9f456SAndroid Build Coastguard Worker                        __w = __w->__right_->__left_;
483*58b9f456SAndroid Build Coastguard Worker                    }
484*58b9f456SAndroid Build Coastguard Worker                    // __w->__is_black_ is now true, __w may have null children
485*58b9f456SAndroid Build Coastguard Worker                    if ((__w->__left_  == nullptr || __w->__left_->__is_black_) &&
486*58b9f456SAndroid Build Coastguard Worker                        (__w->__right_ == nullptr || __w->__right_->__is_black_))
487*58b9f456SAndroid Build Coastguard Worker                    {
488*58b9f456SAndroid Build Coastguard Worker                        __w->__is_black_ = false;
489*58b9f456SAndroid Build Coastguard Worker                        __x = __w->__parent_unsafe();
490*58b9f456SAndroid Build Coastguard Worker                        // __x can no longer be null
491*58b9f456SAndroid Build Coastguard Worker                        if (!__x->__is_black_ || __x == __root)
492*58b9f456SAndroid Build Coastguard Worker                        {
493*58b9f456SAndroid Build Coastguard Worker                            __x->__is_black_ = true;
494*58b9f456SAndroid Build Coastguard Worker                            break;
495*58b9f456SAndroid Build Coastguard Worker                        }
496*58b9f456SAndroid Build Coastguard Worker                        // reset sibling, and it still can't be null
497*58b9f456SAndroid Build Coastguard Worker                        __w = __tree_is_left_child(__x) ?
498*58b9f456SAndroid Build Coastguard Worker                                    __x->__parent_unsafe()->__right_ :
499*58b9f456SAndroid Build Coastguard Worker                                    __x->__parent_->__left_;
500*58b9f456SAndroid Build Coastguard Worker                        // continue;
501*58b9f456SAndroid Build Coastguard Worker                    }
502*58b9f456SAndroid Build Coastguard Worker                    else  // __w has a red child
503*58b9f456SAndroid Build Coastguard Worker                    {
504*58b9f456SAndroid Build Coastguard Worker                        if (__w->__left_ == nullptr || __w->__left_->__is_black_)
505*58b9f456SAndroid Build Coastguard Worker                        {
506*58b9f456SAndroid Build Coastguard Worker                            // __w right child is non-null and red
507*58b9f456SAndroid Build Coastguard Worker                            __w->__right_->__is_black_ = true;
508*58b9f456SAndroid Build Coastguard Worker                            __w->__is_black_ = false;
509*58b9f456SAndroid Build Coastguard Worker                            __tree_left_rotate(__w);
510*58b9f456SAndroid Build Coastguard Worker                            // __w is known not to be root, so root hasn't changed
511*58b9f456SAndroid Build Coastguard Worker                            // reset sibling, and it still can't be null
512*58b9f456SAndroid Build Coastguard Worker                            __w = __w->__parent_unsafe();
513*58b9f456SAndroid Build Coastguard Worker                        }
514*58b9f456SAndroid Build Coastguard Worker                        // __w has a left red child, right child may be null
515*58b9f456SAndroid Build Coastguard Worker                        __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
516*58b9f456SAndroid Build Coastguard Worker                        __w->__parent_unsafe()->__is_black_ = true;
517*58b9f456SAndroid Build Coastguard Worker                        __w->__left_->__is_black_ = true;
518*58b9f456SAndroid Build Coastguard Worker                        __tree_right_rotate(__w->__parent_unsafe());
519*58b9f456SAndroid Build Coastguard Worker                        break;
520*58b9f456SAndroid Build Coastguard Worker                    }
521*58b9f456SAndroid Build Coastguard Worker                }
522*58b9f456SAndroid Build Coastguard Worker            }
523*58b9f456SAndroid Build Coastguard Worker        }
524*58b9f456SAndroid Build Coastguard Worker    }
525*58b9f456SAndroid Build Coastguard Worker}
526*58b9f456SAndroid Build Coastguard Worker
527*58b9f456SAndroid Build Coastguard Worker// node traits
528*58b9f456SAndroid Build Coastguard Worker
529*58b9f456SAndroid Build Coastguard Worker
530*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
531*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp>
532*58b9f456SAndroid Build Coastguard Workerstruct __is_tree_value_type_imp : false_type {};
533*58b9f456SAndroid Build Coastguard Worker
534*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class _Value>
535*58b9f456SAndroid Build Coastguard Workerstruct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
536*58b9f456SAndroid Build Coastguard Worker
537*58b9f456SAndroid Build Coastguard Workertemplate <class ..._Args>
538*58b9f456SAndroid Build Coastguard Workerstruct __is_tree_value_type : false_type {};
539*58b9f456SAndroid Build Coastguard Worker
540*58b9f456SAndroid Build Coastguard Workertemplate <class _One>
541*58b9f456SAndroid Build Coastguard Workerstruct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
542*58b9f456SAndroid Build Coastguard Worker#endif
543*58b9f456SAndroid Build Coastguard Worker
544*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp>
545*58b9f456SAndroid Build Coastguard Workerstruct __tree_key_value_types {
546*58b9f456SAndroid Build Coastguard Worker  typedef _Tp key_type;
547*58b9f456SAndroid Build Coastguard Worker  typedef _Tp __node_value_type;
548*58b9f456SAndroid Build Coastguard Worker  typedef _Tp __container_value_type;
549*58b9f456SAndroid Build Coastguard Worker  static const bool __is_map = false;
550*58b9f456SAndroid Build Coastguard Worker
551*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
552*58b9f456SAndroid Build Coastguard Worker  static key_type const& __get_key(_Tp const& __v) {
553*58b9f456SAndroid Build Coastguard Worker    return __v;
554*58b9f456SAndroid Build Coastguard Worker  }
555*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
556*58b9f456SAndroid Build Coastguard Worker  static __container_value_type const& __get_value(__node_value_type const& __v) {
557*58b9f456SAndroid Build Coastguard Worker    return __v;
558*58b9f456SAndroid Build Coastguard Worker  }
559*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
560*58b9f456SAndroid Build Coastguard Worker  static __container_value_type* __get_ptr(__node_value_type& __n) {
561*58b9f456SAndroid Build Coastguard Worker    return _VSTD::addressof(__n);
562*58b9f456SAndroid Build Coastguard Worker  }
563*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
564*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
565*58b9f456SAndroid Build Coastguard Worker  static __container_value_type&& __move(__node_value_type& __v) {
566*58b9f456SAndroid Build Coastguard Worker    return _VSTD::move(__v);
567*58b9f456SAndroid Build Coastguard Worker  }
568*58b9f456SAndroid Build Coastguard Worker#endif
569*58b9f456SAndroid Build Coastguard Worker};
570*58b9f456SAndroid Build Coastguard Worker
571*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class _Tp>
572*58b9f456SAndroid Build Coastguard Workerstruct __tree_key_value_types<__value_type<_Key, _Tp> > {
573*58b9f456SAndroid Build Coastguard Worker  typedef _Key                                         key_type;
574*58b9f456SAndroid Build Coastguard Worker  typedef _Tp                                          mapped_type;
575*58b9f456SAndroid Build Coastguard Worker  typedef __value_type<_Key, _Tp>                      __node_value_type;
576*58b9f456SAndroid Build Coastguard Worker  typedef pair<const _Key, _Tp>                        __container_value_type;
577*58b9f456SAndroid Build Coastguard Worker  typedef __container_value_type                       __map_value_type;
578*58b9f456SAndroid Build Coastguard Worker  static const bool __is_map = true;
579*58b9f456SAndroid Build Coastguard Worker
580*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
581*58b9f456SAndroid Build Coastguard Worker  static key_type const&
582*58b9f456SAndroid Build Coastguard Worker  __get_key(__node_value_type const& __t) {
583*58b9f456SAndroid Build Coastguard Worker    return __t.__get_value().first;
584*58b9f456SAndroid Build Coastguard Worker  }
585*58b9f456SAndroid Build Coastguard Worker
586*58b9f456SAndroid Build Coastguard Worker  template <class _Up>
587*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
588*58b9f456SAndroid Build Coastguard Worker  static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
589*58b9f456SAndroid Build Coastguard Worker      key_type const&>::type
590*58b9f456SAndroid Build Coastguard Worker  __get_key(_Up& __t) {
591*58b9f456SAndroid Build Coastguard Worker    return __t.first;
592*58b9f456SAndroid Build Coastguard Worker  }
593*58b9f456SAndroid Build Coastguard Worker
594*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
595*58b9f456SAndroid Build Coastguard Worker  static __container_value_type const&
596*58b9f456SAndroid Build Coastguard Worker  __get_value(__node_value_type const& __t) {
597*58b9f456SAndroid Build Coastguard Worker    return __t.__get_value();
598*58b9f456SAndroid Build Coastguard Worker  }
599*58b9f456SAndroid Build Coastguard Worker
600*58b9f456SAndroid Build Coastguard Worker  template <class _Up>
601*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
602*58b9f456SAndroid Build Coastguard Worker  static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
603*58b9f456SAndroid Build Coastguard Worker      __container_value_type const&>::type
604*58b9f456SAndroid Build Coastguard Worker  __get_value(_Up& __t) {
605*58b9f456SAndroid Build Coastguard Worker    return __t;
606*58b9f456SAndroid Build Coastguard Worker  }
607*58b9f456SAndroid Build Coastguard Worker
608*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
609*58b9f456SAndroid Build Coastguard Worker  static __container_value_type* __get_ptr(__node_value_type& __n) {
610*58b9f456SAndroid Build Coastguard Worker    return _VSTD::addressof(__n.__get_value());
611*58b9f456SAndroid Build Coastguard Worker  }
612*58b9f456SAndroid Build Coastguard Worker
613*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
614*58b9f456SAndroid Build Coastguard Worker  _LIBCPP_INLINE_VISIBILITY
615*58b9f456SAndroid Build Coastguard Worker  static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
616*58b9f456SAndroid Build Coastguard Worker    return __v.__move();
617*58b9f456SAndroid Build Coastguard Worker  }
618*58b9f456SAndroid Build Coastguard Worker#endif
619*58b9f456SAndroid Build Coastguard Worker};
620*58b9f456SAndroid Build Coastguard Worker
621*58b9f456SAndroid Build Coastguard Workertemplate <class _VoidPtr>
622*58b9f456SAndroid Build Coastguard Workerstruct __tree_node_base_types {
623*58b9f456SAndroid Build Coastguard Worker  typedef _VoidPtr                                               __void_pointer;
624*58b9f456SAndroid Build Coastguard Worker
625*58b9f456SAndroid Build Coastguard Worker  typedef __tree_node_base<__void_pointer>                      __node_base_type;
626*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
627*58b9f456SAndroid Build Coastguard Worker                                                             __node_base_pointer;
628*58b9f456SAndroid Build Coastguard Worker
629*58b9f456SAndroid Build Coastguard Worker  typedef __tree_end_node<__node_base_pointer>                  __end_node_type;
630*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
631*58b9f456SAndroid Build Coastguard Worker                                                             __end_node_pointer;
632*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
633*58b9f456SAndroid Build Coastguard Worker  typedef __end_node_pointer __parent_pointer;
634*58b9f456SAndroid Build Coastguard Worker#else
635*58b9f456SAndroid Build Coastguard Worker  typedef typename conditional<
636*58b9f456SAndroid Build Coastguard Worker      is_pointer<__end_node_pointer>::value,
637*58b9f456SAndroid Build Coastguard Worker        __end_node_pointer,
638*58b9f456SAndroid Build Coastguard Worker        __node_base_pointer>::type __parent_pointer;
639*58b9f456SAndroid Build Coastguard Worker#endif
640*58b9f456SAndroid Build Coastguard Worker
641*58b9f456SAndroid Build Coastguard Workerprivate:
642*58b9f456SAndroid Build Coastguard Worker  static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
643*58b9f456SAndroid Build Coastguard Worker                  "_VoidPtr does not point to unqualified void type");
644*58b9f456SAndroid Build Coastguard Worker};
645*58b9f456SAndroid Build Coastguard Worker
646*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
647*58b9f456SAndroid Build Coastguard Worker         bool = _KVTypes::__is_map>
648*58b9f456SAndroid Build Coastguard Workerstruct __tree_map_pointer_types {};
649*58b9f456SAndroid Build Coastguard Worker
650*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _AllocPtr, class _KVTypes>
651*58b9f456SAndroid Build Coastguard Workerstruct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
652*58b9f456SAndroid Build Coastguard Worker  typedef typename _KVTypes::__map_value_type   _Mv;
653*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
654*58b9f456SAndroid Build Coastguard Worker                                                       __map_value_type_pointer;
655*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
656*58b9f456SAndroid Build Coastguard Worker                                                 __const_map_value_type_pointer;
657*58b9f456SAndroid Build Coastguard Worker};
658*58b9f456SAndroid Build Coastguard Worker
659*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
660*58b9f456SAndroid Build Coastguard Workerstruct __tree_node_types;
661*58b9f456SAndroid Build Coastguard Worker
662*58b9f456SAndroid Build Coastguard Workertemplate <class _NodePtr, class _Tp, class _VoidPtr>
663*58b9f456SAndroid Build Coastguard Workerstruct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
664*58b9f456SAndroid Build Coastguard Worker    : public __tree_node_base_types<_VoidPtr>,
665*58b9f456SAndroid Build Coastguard Worker             __tree_key_value_types<_Tp>,
666*58b9f456SAndroid Build Coastguard Worker             __tree_map_pointer_types<_Tp, _VoidPtr>
667*58b9f456SAndroid Build Coastguard Worker{
668*58b9f456SAndroid Build Coastguard Worker  typedef __tree_node_base_types<_VoidPtr> __base;
669*58b9f456SAndroid Build Coastguard Worker  typedef __tree_key_value_types<_Tp>      __key_base;
670*58b9f456SAndroid Build Coastguard Worker  typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
671*58b9f456SAndroid Build Coastguard Workerpublic:
672*58b9f456SAndroid Build Coastguard Worker
673*58b9f456SAndroid Build Coastguard Worker  typedef typename pointer_traits<_NodePtr>::element_type       __node_type;
674*58b9f456SAndroid Build Coastguard Worker  typedef _NodePtr                                              __node_pointer;
675*58b9f456SAndroid Build Coastguard Worker
676*58b9f456SAndroid Build Coastguard Worker  typedef _Tp                                                 __node_value_type;
677*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
678*58b9f456SAndroid Build Coastguard Worker                                                      __node_value_type_pointer;
679*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
680*58b9f456SAndroid Build Coastguard Worker                                                __const_node_value_type_pointer;
681*58b9f456SAndroid Build Coastguard Worker#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
682*58b9f456SAndroid Build Coastguard Worker  typedef typename __base::__end_node_pointer __iter_pointer;
683*58b9f456SAndroid Build Coastguard Worker#else
684*58b9f456SAndroid Build Coastguard Worker  typedef typename conditional<
685*58b9f456SAndroid Build Coastguard Worker      is_pointer<__node_pointer>::value,
686*58b9f456SAndroid Build Coastguard Worker        typename __base::__end_node_pointer,
687*58b9f456SAndroid Build Coastguard Worker        __node_pointer>::type __iter_pointer;
688*58b9f456SAndroid Build Coastguard Worker#endif
689*58b9f456SAndroid Build Coastguard Workerprivate:
690*58b9f456SAndroid Build Coastguard Worker    static_assert(!is_const<__node_type>::value,
691*58b9f456SAndroid Build Coastguard Worker                "_NodePtr should never be a pointer to const");
692*58b9f456SAndroid Build Coastguard Worker    static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
693*58b9f456SAndroid Build Coastguard Worker                          _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
694*58b9f456SAndroid Build Coastguard Worker};
695*58b9f456SAndroid Build Coastguard Worker
696*58b9f456SAndroid Build Coastguard Workertemplate <class _ValueTp, class _VoidPtr>
697*58b9f456SAndroid Build Coastguard Workerstruct __make_tree_node_types {
698*58b9f456SAndroid Build Coastguard Worker  typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
699*58b9f456SAndroid Build Coastguard Worker                                                                        _NodePtr;
700*58b9f456SAndroid Build Coastguard Worker  typedef __tree_node_types<_NodePtr> type;
701*58b9f456SAndroid Build Coastguard Worker};
702*58b9f456SAndroid Build Coastguard Worker
703*58b9f456SAndroid Build Coastguard Worker// node
704*58b9f456SAndroid Build Coastguard Worker
705*58b9f456SAndroid Build Coastguard Workertemplate <class _Pointer>
706*58b9f456SAndroid Build Coastguard Workerclass __tree_end_node
707*58b9f456SAndroid Build Coastguard Worker{
708*58b9f456SAndroid Build Coastguard Workerpublic:
709*58b9f456SAndroid Build Coastguard Worker    typedef _Pointer pointer;
710*58b9f456SAndroid Build Coastguard Worker    pointer __left_;
711*58b9f456SAndroid Build Coastguard Worker
712*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
713*58b9f456SAndroid Build Coastguard Worker    __tree_end_node() _NOEXCEPT : __left_() {}
714*58b9f456SAndroid Build Coastguard Worker};
715*58b9f456SAndroid Build Coastguard Worker
716*58b9f456SAndroid Build Coastguard Workertemplate <class _VoidPtr>
717*58b9f456SAndroid Build Coastguard Workerclass __tree_node_base
718*58b9f456SAndroid Build Coastguard Worker    : public __tree_node_base_types<_VoidPtr>::__end_node_type
719*58b9f456SAndroid Build Coastguard Worker{
720*58b9f456SAndroid Build Coastguard Worker    typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
721*58b9f456SAndroid Build Coastguard Worker
722*58b9f456SAndroid Build Coastguard Workerpublic:
723*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeBaseTypes::__node_base_pointer pointer;
724*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
725*58b9f456SAndroid Build Coastguard Worker
726*58b9f456SAndroid Build Coastguard Worker    pointer          __right_;
727*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent_;
728*58b9f456SAndroid Build Coastguard Worker    bool __is_black_;
729*58b9f456SAndroid Build Coastguard Worker
730*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
731*58b9f456SAndroid Build Coastguard Worker    pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
732*58b9f456SAndroid Build Coastguard Worker
733*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
734*58b9f456SAndroid Build Coastguard Worker    void __set_parent(pointer __p) {
735*58b9f456SAndroid Build Coastguard Worker        __parent_ = static_cast<__parent_pointer>(__p);
736*58b9f456SAndroid Build Coastguard Worker    }
737*58b9f456SAndroid Build Coastguard Worker
738*58b9f456SAndroid Build Coastguard Workerprivate:
739*58b9f456SAndroid Build Coastguard Worker  ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
740*58b9f456SAndroid Build Coastguard Worker  __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
741*58b9f456SAndroid Build Coastguard Worker  __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
742*58b9f456SAndroid Build Coastguard Worker};
743*58b9f456SAndroid Build Coastguard Worker
744*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _VoidPtr>
745*58b9f456SAndroid Build Coastguard Workerclass __tree_node
746*58b9f456SAndroid Build Coastguard Worker    : public __tree_node_base<_VoidPtr>
747*58b9f456SAndroid Build Coastguard Worker{
748*58b9f456SAndroid Build Coastguard Workerpublic:
749*58b9f456SAndroid Build Coastguard Worker    typedef _Tp __node_value_type;
750*58b9f456SAndroid Build Coastguard Worker
751*58b9f456SAndroid Build Coastguard Worker    __node_value_type __value_;
752*58b9f456SAndroid Build Coastguard Worker
753*58b9f456SAndroid Build Coastguard Workerprivate:
754*58b9f456SAndroid Build Coastguard Worker  ~__tree_node() _LIBCPP_EQUAL_DELETE;
755*58b9f456SAndroid Build Coastguard Worker  __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
756*58b9f456SAndroid Build Coastguard Worker  __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
757*58b9f456SAndroid Build Coastguard Worker};
758*58b9f456SAndroid Build Coastguard Worker
759*58b9f456SAndroid Build Coastguard Worker
760*58b9f456SAndroid Build Coastguard Workertemplate <class _Allocator>
761*58b9f456SAndroid Build Coastguard Workerclass __tree_node_destructor
762*58b9f456SAndroid Build Coastguard Worker{
763*58b9f456SAndroid Build Coastguard Worker    typedef _Allocator                                      allocator_type;
764*58b9f456SAndroid Build Coastguard Worker    typedef allocator_traits<allocator_type>                __alloc_traits;
765*58b9f456SAndroid Build Coastguard Worker
766*58b9f456SAndroid Build Coastguard Workerpublic:
767*58b9f456SAndroid Build Coastguard Worker    typedef typename __alloc_traits::pointer                pointer;
768*58b9f456SAndroid Build Coastguard Workerprivate:
769*58b9f456SAndroid Build Coastguard Worker    typedef __tree_node_types<pointer> _NodeTypes;
770*58b9f456SAndroid Build Coastguard Worker    allocator_type& __na_;
771*58b9f456SAndroid Build Coastguard Worker
772*58b9f456SAndroid Build Coastguard Worker    __tree_node_destructor& operator=(const __tree_node_destructor&);
773*58b9f456SAndroid Build Coastguard Worker
774*58b9f456SAndroid Build Coastguard Workerpublic:
775*58b9f456SAndroid Build Coastguard Worker    bool __value_constructed;
776*58b9f456SAndroid Build Coastguard Worker
777*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
778*58b9f456SAndroid Build Coastguard Worker    explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
779*58b9f456SAndroid Build Coastguard Worker        : __na_(__na),
780*58b9f456SAndroid Build Coastguard Worker          __value_constructed(__val)
781*58b9f456SAndroid Build Coastguard Worker        {}
782*58b9f456SAndroid Build Coastguard Worker
783*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
784*58b9f456SAndroid Build Coastguard Worker    void operator()(pointer __p) _NOEXCEPT
785*58b9f456SAndroid Build Coastguard Worker    {
786*58b9f456SAndroid Build Coastguard Worker        if (__value_constructed)
787*58b9f456SAndroid Build Coastguard Worker            __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
788*58b9f456SAndroid Build Coastguard Worker        if (__p)
789*58b9f456SAndroid Build Coastguard Worker            __alloc_traits::deallocate(__na_, __p, 1);
790*58b9f456SAndroid Build Coastguard Worker    }
791*58b9f456SAndroid Build Coastguard Worker
792*58b9f456SAndroid Build Coastguard Worker    template <class> friend class __map_node_destructor;
793*58b9f456SAndroid Build Coastguard Worker};
794*58b9f456SAndroid Build Coastguard Worker
795*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
796*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeType, class _Alloc>
797*58b9f456SAndroid Build Coastguard Workerstruct __generic_container_node_destructor;
798*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _VoidPtr, class _Alloc>
799*58b9f456SAndroid Build Coastguard Workerstruct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc>
800*58b9f456SAndroid Build Coastguard Worker    : __tree_node_destructor<_Alloc>
801*58b9f456SAndroid Build Coastguard Worker{
802*58b9f456SAndroid Build Coastguard Worker    using __tree_node_destructor<_Alloc>::__tree_node_destructor;
803*58b9f456SAndroid Build Coastguard Worker};
804*58b9f456SAndroid Build Coastguard Worker#endif
805*58b9f456SAndroid Build Coastguard Worker
806*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _NodePtr, class _DiffType>
807*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_TEMPLATE_VIS __tree_iterator
808*58b9f456SAndroid Build Coastguard Worker{
809*58b9f456SAndroid Build Coastguard Worker    typedef __tree_node_types<_NodePtr>                     _NodeTypes;
810*58b9f456SAndroid Build Coastguard Worker    typedef _NodePtr                                        __node_pointer;
811*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_base_pointer        __node_base_pointer;
812*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__end_node_pointer         __end_node_pointer;
813*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__iter_pointer             __iter_pointer;
814*58b9f456SAndroid Build Coastguard Worker    typedef pointer_traits<__node_pointer> __pointer_traits;
815*58b9f456SAndroid Build Coastguard Worker
816*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __ptr_;
817*58b9f456SAndroid Build Coastguard Worker
818*58b9f456SAndroid Build Coastguard Workerpublic:
819*58b9f456SAndroid Build Coastguard Worker    typedef bidirectional_iterator_tag                     iterator_category;
820*58b9f456SAndroid Build Coastguard Worker    typedef _Tp                                            value_type;
821*58b9f456SAndroid Build Coastguard Worker    typedef _DiffType                                      difference_type;
822*58b9f456SAndroid Build Coastguard Worker    typedef value_type&                                    reference;
823*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_value_type_pointer pointer;
824*58b9f456SAndroid Build Coastguard Worker
825*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
826*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 11
827*58b9f456SAndroid Build Coastguard Worker    : __ptr_(nullptr)
828*58b9f456SAndroid Build Coastguard Worker#endif
829*58b9f456SAndroid Build Coastguard Worker    {}
830*58b9f456SAndroid Build Coastguard Worker
831*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY reference operator*() const
832*58b9f456SAndroid Build Coastguard Worker        {return __get_np()->__value_;}
833*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY pointer operator->() const
834*58b9f456SAndroid Build Coastguard Worker        {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
835*58b9f456SAndroid Build Coastguard Worker
836*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
837*58b9f456SAndroid Build Coastguard Worker    __tree_iterator& operator++() {
838*58b9f456SAndroid Build Coastguard Worker      __ptr_ = static_cast<__iter_pointer>(
839*58b9f456SAndroid Build Coastguard Worker          __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
840*58b9f456SAndroid Build Coastguard Worker      return *this;
841*58b9f456SAndroid Build Coastguard Worker    }
842*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
843*58b9f456SAndroid Build Coastguard Worker    __tree_iterator operator++(int)
844*58b9f456SAndroid Build Coastguard Worker        {__tree_iterator __t(*this); ++(*this); return __t;}
845*58b9f456SAndroid Build Coastguard Worker
846*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
847*58b9f456SAndroid Build Coastguard Worker    __tree_iterator& operator--() {
848*58b9f456SAndroid Build Coastguard Worker      __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
849*58b9f456SAndroid Build Coastguard Worker          static_cast<__end_node_pointer>(__ptr_)));
850*58b9f456SAndroid Build Coastguard Worker      return *this;
851*58b9f456SAndroid Build Coastguard Worker    }
852*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
853*58b9f456SAndroid Build Coastguard Worker    __tree_iterator operator--(int)
854*58b9f456SAndroid Build Coastguard Worker        {__tree_iterator __t(*this); --(*this); return __t;}
855*58b9f456SAndroid Build Coastguard Worker
856*58b9f456SAndroid Build Coastguard Worker    friend _LIBCPP_INLINE_VISIBILITY
857*58b9f456SAndroid Build Coastguard Worker        bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
858*58b9f456SAndroid Build Coastguard Worker        {return __x.__ptr_ == __y.__ptr_;}
859*58b9f456SAndroid Build Coastguard Worker    friend _LIBCPP_INLINE_VISIBILITY
860*58b9f456SAndroid Build Coastguard Worker        bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
861*58b9f456SAndroid Build Coastguard Worker        {return !(__x == __y);}
862*58b9f456SAndroid Build Coastguard Worker
863*58b9f456SAndroid Build Coastguard Workerprivate:
864*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
865*58b9f456SAndroid Build Coastguard Worker    explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
866*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
867*58b9f456SAndroid Build Coastguard Worker    explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
868*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
869*58b9f456SAndroid Build Coastguard Worker    __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
870*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class __tree;
871*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
872*58b9f456SAndroid Build Coastguard Worker    template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
873*58b9f456SAndroid Build Coastguard Worker    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
874*58b9f456SAndroid Build Coastguard Worker    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
875*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
876*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
877*58b9f456SAndroid Build Coastguard Worker};
878*58b9f456SAndroid Build Coastguard Worker
879*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _NodePtr, class _DiffType>
880*58b9f456SAndroid Build Coastguard Workerclass _LIBCPP_TEMPLATE_VIS __tree_const_iterator
881*58b9f456SAndroid Build Coastguard Worker{
882*58b9f456SAndroid Build Coastguard Worker    typedef __tree_node_types<_NodePtr>                     _NodeTypes;
883*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_pointer             __node_pointer;
884*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_base_pointer        __node_base_pointer;
885*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__end_node_pointer         __end_node_pointer;
886*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__iter_pointer             __iter_pointer;
887*58b9f456SAndroid Build Coastguard Worker    typedef pointer_traits<__node_pointer> __pointer_traits;
888*58b9f456SAndroid Build Coastguard Worker
889*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __ptr_;
890*58b9f456SAndroid Build Coastguard Worker
891*58b9f456SAndroid Build Coastguard Workerpublic:
892*58b9f456SAndroid Build Coastguard Worker    typedef bidirectional_iterator_tag                           iterator_category;
893*58b9f456SAndroid Build Coastguard Worker    typedef _Tp                                                  value_type;
894*58b9f456SAndroid Build Coastguard Worker    typedef _DiffType                                            difference_type;
895*58b9f456SAndroid Build Coastguard Worker    typedef const value_type&                                    reference;
896*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
897*58b9f456SAndroid Build Coastguard Worker
898*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
899*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 11
900*58b9f456SAndroid Build Coastguard Worker    : __ptr_(nullptr)
901*58b9f456SAndroid Build Coastguard Worker#endif
902*58b9f456SAndroid Build Coastguard Worker    {}
903*58b9f456SAndroid Build Coastguard Worker
904*58b9f456SAndroid Build Coastguard Workerprivate:
905*58b9f456SAndroid Build Coastguard Worker    typedef __tree_iterator<value_type, __node_pointer, difference_type>
906*58b9f456SAndroid Build Coastguard Worker                                                           __non_const_iterator;
907*58b9f456SAndroid Build Coastguard Workerpublic:
908*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
909*58b9f456SAndroid Build Coastguard Worker    __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
910*58b9f456SAndroid Build Coastguard Worker        : __ptr_(__p.__ptr_) {}
911*58b9f456SAndroid Build Coastguard Worker
912*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY reference operator*() const
913*58b9f456SAndroid Build Coastguard Worker        {return __get_np()->__value_;}
914*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY pointer operator->() const
915*58b9f456SAndroid Build Coastguard Worker        {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
916*58b9f456SAndroid Build Coastguard Worker
917*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
918*58b9f456SAndroid Build Coastguard Worker    __tree_const_iterator& operator++() {
919*58b9f456SAndroid Build Coastguard Worker      __ptr_ = static_cast<__iter_pointer>(
920*58b9f456SAndroid Build Coastguard Worker          __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
921*58b9f456SAndroid Build Coastguard Worker      return *this;
922*58b9f456SAndroid Build Coastguard Worker    }
923*58b9f456SAndroid Build Coastguard Worker
924*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
925*58b9f456SAndroid Build Coastguard Worker    __tree_const_iterator operator++(int)
926*58b9f456SAndroid Build Coastguard Worker        {__tree_const_iterator __t(*this); ++(*this); return __t;}
927*58b9f456SAndroid Build Coastguard Worker
928*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
929*58b9f456SAndroid Build Coastguard Worker    __tree_const_iterator& operator--() {
930*58b9f456SAndroid Build Coastguard Worker      __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
931*58b9f456SAndroid Build Coastguard Worker          static_cast<__end_node_pointer>(__ptr_)));
932*58b9f456SAndroid Build Coastguard Worker      return *this;
933*58b9f456SAndroid Build Coastguard Worker    }
934*58b9f456SAndroid Build Coastguard Worker
935*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
936*58b9f456SAndroid Build Coastguard Worker    __tree_const_iterator operator--(int)
937*58b9f456SAndroid Build Coastguard Worker        {__tree_const_iterator __t(*this); --(*this); return __t;}
938*58b9f456SAndroid Build Coastguard Worker
939*58b9f456SAndroid Build Coastguard Worker    friend _LIBCPP_INLINE_VISIBILITY
940*58b9f456SAndroid Build Coastguard Worker        bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
941*58b9f456SAndroid Build Coastguard Worker        {return __x.__ptr_ == __y.__ptr_;}
942*58b9f456SAndroid Build Coastguard Worker    friend _LIBCPP_INLINE_VISIBILITY
943*58b9f456SAndroid Build Coastguard Worker        bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
944*58b9f456SAndroid Build Coastguard Worker        {return !(__x == __y);}
945*58b9f456SAndroid Build Coastguard Worker
946*58b9f456SAndroid Build Coastguard Workerprivate:
947*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
948*58b9f456SAndroid Build Coastguard Worker    explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
949*58b9f456SAndroid Build Coastguard Worker        : __ptr_(__p) {}
950*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
951*58b9f456SAndroid Build Coastguard Worker    explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
952*58b9f456SAndroid Build Coastguard Worker        : __ptr_(__p) {}
953*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
954*58b9f456SAndroid Build Coastguard Worker    __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
955*58b9f456SAndroid Build Coastguard Worker
956*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class __tree;
957*58b9f456SAndroid Build Coastguard Worker    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
958*58b9f456SAndroid Build Coastguard Worker    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
959*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
960*58b9f456SAndroid Build Coastguard Worker    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
961*58b9f456SAndroid Build Coastguard Worker    template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
962*58b9f456SAndroid Build Coastguard Worker
963*58b9f456SAndroid Build Coastguard Worker};
964*58b9f456SAndroid Build Coastguard Worker
965*58b9f456SAndroid Build Coastguard Workertemplate<class _Tp, class _Compare>
966*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
967*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_DIAGNOSE_WARNING(!std::__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
968*58b9f456SAndroid Build Coastguard Worker        "the specified comparator type does not provide a const call operator")
969*58b9f456SAndroid Build Coastguard Worker#endif
970*58b9f456SAndroid Build Coastguard Workerint __diagnose_non_const_comparator();
971*58b9f456SAndroid Build Coastguard Worker
972*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
973*58b9f456SAndroid Build Coastguard Workerclass __tree
974*58b9f456SAndroid Build Coastguard Worker{
975*58b9f456SAndroid Build Coastguard Workerpublic:
976*58b9f456SAndroid Build Coastguard Worker    typedef _Tp                                      value_type;
977*58b9f456SAndroid Build Coastguard Worker    typedef _Compare                                 value_compare;
978*58b9f456SAndroid Build Coastguard Worker    typedef _Allocator                               allocator_type;
979*58b9f456SAndroid Build Coastguard Worker
980*58b9f456SAndroid Build Coastguard Workerprivate:
981*58b9f456SAndroid Build Coastguard Worker    typedef allocator_traits<allocator_type>         __alloc_traits;
982*58b9f456SAndroid Build Coastguard Worker    typedef typename __make_tree_node_types<value_type,
983*58b9f456SAndroid Build Coastguard Worker        typename __alloc_traits::void_pointer>::type
984*58b9f456SAndroid Build Coastguard Worker                                                    _NodeTypes;
985*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::key_type           key_type;
986*58b9f456SAndroid Build Coastguard Workerpublic:
987*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_value_type      __node_value_type;
988*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__container_value_type __container_value_type;
989*58b9f456SAndroid Build Coastguard Worker
990*58b9f456SAndroid Build Coastguard Worker    typedef typename __alloc_traits::pointer         pointer;
991*58b9f456SAndroid Build Coastguard Worker    typedef typename __alloc_traits::const_pointer   const_pointer;
992*58b9f456SAndroid Build Coastguard Worker    typedef typename __alloc_traits::size_type       size_type;
993*58b9f456SAndroid Build Coastguard Worker    typedef typename __alloc_traits::difference_type difference_type;
994*58b9f456SAndroid Build Coastguard Worker
995*58b9f456SAndroid Build Coastguard Workerpublic:
996*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__void_pointer        __void_pointer;
997*58b9f456SAndroid Build Coastguard Worker
998*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_type           __node;
999*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_pointer        __node_pointer;
1000*58b9f456SAndroid Build Coastguard Worker
1001*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_base_type      __node_base;
1002*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__node_base_pointer   __node_base_pointer;
1003*58b9f456SAndroid Build Coastguard Worker
1004*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__end_node_type       __end_node_t;
1005*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__end_node_pointer    __end_node_ptr;
1006*58b9f456SAndroid Build Coastguard Worker
1007*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__parent_pointer      __parent_pointer;
1008*58b9f456SAndroid Build Coastguard Worker    typedef typename _NodeTypes::__iter_pointer        __iter_pointer;
1009*58b9f456SAndroid Build Coastguard Worker
1010*58b9f456SAndroid Build Coastguard Worker    typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
1011*58b9f456SAndroid Build Coastguard Worker    typedef allocator_traits<__node_allocator>         __node_traits;
1012*58b9f456SAndroid Build Coastguard Worker
1013*58b9f456SAndroid Build Coastguard Workerprivate:
1014*58b9f456SAndroid Build Coastguard Worker    // check for sane allocator pointer rebinding semantics. Rebinding the
1015*58b9f456SAndroid Build Coastguard Worker    // allocator for a new pointer type should be exactly the same as rebinding
1016*58b9f456SAndroid Build Coastguard Worker    // the pointer using 'pointer_traits'.
1017*58b9f456SAndroid Build Coastguard Worker    static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1018*58b9f456SAndroid Build Coastguard Worker                  "Allocator does not rebind pointers in a sane manner.");
1019*58b9f456SAndroid Build Coastguard Worker    typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1020*58b9f456SAndroid Build Coastguard Worker        __node_base_allocator;
1021*58b9f456SAndroid Build Coastguard Worker    typedef allocator_traits<__node_base_allocator> __node_base_traits;
1022*58b9f456SAndroid Build Coastguard Worker    static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1023*58b9f456SAndroid Build Coastguard Worker                 "Allocator does not rebind pointers in a sane manner.");
1024*58b9f456SAndroid Build Coastguard Worker
1025*58b9f456SAndroid Build Coastguard Workerprivate:
1026*58b9f456SAndroid Build Coastguard Worker    __iter_pointer                                     __begin_node_;
1027*58b9f456SAndroid Build Coastguard Worker    __compressed_pair<__end_node_t, __node_allocator>  __pair1_;
1028*58b9f456SAndroid Build Coastguard Worker    __compressed_pair<size_type, value_compare>        __pair3_;
1029*58b9f456SAndroid Build Coastguard Worker
1030*58b9f456SAndroid Build Coastguard Workerpublic:
1031*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1032*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __end_node() _NOEXCEPT
1033*58b9f456SAndroid Build Coastguard Worker    {
1034*58b9f456SAndroid Build Coastguard Worker        return static_cast<__iter_pointer>(
1035*58b9f456SAndroid Build Coastguard Worker                pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1036*58b9f456SAndroid Build Coastguard Worker        );
1037*58b9f456SAndroid Build Coastguard Worker    }
1038*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1039*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __end_node() const _NOEXCEPT
1040*58b9f456SAndroid Build Coastguard Worker    {
1041*58b9f456SAndroid Build Coastguard Worker        return static_cast<__iter_pointer>(
1042*58b9f456SAndroid Build Coastguard Worker            pointer_traits<__end_node_ptr>::pointer_to(
1043*58b9f456SAndroid Build Coastguard Worker                const_cast<__end_node_t&>(__pair1_.first())
1044*58b9f456SAndroid Build Coastguard Worker            )
1045*58b9f456SAndroid Build Coastguard Worker        );
1046*58b9f456SAndroid Build Coastguard Worker    }
1047*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1048*58b9f456SAndroid Build Coastguard Worker          __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
1049*58b9f456SAndroid Build Coastguard Workerprivate:
1050*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1051*58b9f456SAndroid Build Coastguard Worker    const __node_allocator& __node_alloc() const _NOEXCEPT
1052*58b9f456SAndroid Build Coastguard Worker        {return __pair1_.second();}
1053*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1054*58b9f456SAndroid Build Coastguard Worker          __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
1055*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1056*58b9f456SAndroid Build Coastguard Worker    const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
1057*58b9f456SAndroid Build Coastguard Workerpublic:
1058*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1059*58b9f456SAndroid Build Coastguard Worker    allocator_type __alloc() const _NOEXCEPT
1060*58b9f456SAndroid Build Coastguard Worker        {return allocator_type(__node_alloc());}
1061*58b9f456SAndroid Build Coastguard Workerprivate:
1062*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1063*58b9f456SAndroid Build Coastguard Worker          size_type& size() _NOEXCEPT {return __pair3_.first();}
1064*58b9f456SAndroid Build Coastguard Workerpublic:
1065*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1066*58b9f456SAndroid Build Coastguard Worker    const size_type& size() const _NOEXCEPT {return __pair3_.first();}
1067*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1068*58b9f456SAndroid Build Coastguard Worker          value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
1069*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1070*58b9f456SAndroid Build Coastguard Worker    const value_compare& value_comp() const _NOEXCEPT
1071*58b9f456SAndroid Build Coastguard Worker        {return __pair3_.second();}
1072*58b9f456SAndroid Build Coastguard Workerpublic:
1073*58b9f456SAndroid Build Coastguard Worker
1074*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1075*58b9f456SAndroid Build Coastguard Worker    __node_pointer __root() const _NOEXCEPT
1076*58b9f456SAndroid Build Coastguard Worker        {return static_cast<__node_pointer>(__end_node()->__left_);}
1077*58b9f456SAndroid Build Coastguard Worker
1078*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer* __root_ptr() const _NOEXCEPT {
1079*58b9f456SAndroid Build Coastguard Worker        return _VSTD::addressof(__end_node()->__left_);
1080*58b9f456SAndroid Build Coastguard Worker    }
1081*58b9f456SAndroid Build Coastguard Worker
1082*58b9f456SAndroid Build Coastguard Worker    typedef __tree_iterator<value_type, __node_pointer, difference_type>             iterator;
1083*58b9f456SAndroid Build Coastguard Worker    typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
1084*58b9f456SAndroid Build Coastguard Worker
1085*58b9f456SAndroid Build Coastguard Worker    explicit __tree(const value_compare& __comp)
1086*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1087*58b9f456SAndroid Build Coastguard Worker            is_nothrow_default_constructible<__node_allocator>::value &&
1088*58b9f456SAndroid Build Coastguard Worker            is_nothrow_copy_constructible<value_compare>::value);
1089*58b9f456SAndroid Build Coastguard Worker    explicit __tree(const allocator_type& __a);
1090*58b9f456SAndroid Build Coastguard Worker    __tree(const value_compare& __comp, const allocator_type& __a);
1091*58b9f456SAndroid Build Coastguard Worker    __tree(const __tree& __t);
1092*58b9f456SAndroid Build Coastguard Worker    __tree& operator=(const __tree& __t);
1093*58b9f456SAndroid Build Coastguard Worker    template <class _InputIterator>
1094*58b9f456SAndroid Build Coastguard Worker        void __assign_unique(_InputIterator __first, _InputIterator __last);
1095*58b9f456SAndroid Build Coastguard Worker    template <class _InputIterator>
1096*58b9f456SAndroid Build Coastguard Worker        void __assign_multi(_InputIterator __first, _InputIterator __last);
1097*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
1098*58b9f456SAndroid Build Coastguard Worker    __tree(__tree&& __t)
1099*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1100*58b9f456SAndroid Build Coastguard Worker            is_nothrow_move_constructible<__node_allocator>::value &&
1101*58b9f456SAndroid Build Coastguard Worker            is_nothrow_move_constructible<value_compare>::value);
1102*58b9f456SAndroid Build Coastguard Worker    __tree(__tree&& __t, const allocator_type& __a);
1103*58b9f456SAndroid Build Coastguard Worker    __tree& operator=(__tree&& __t)
1104*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1105*58b9f456SAndroid Build Coastguard Worker            __node_traits::propagate_on_container_move_assignment::value &&
1106*58b9f456SAndroid Build Coastguard Worker            is_nothrow_move_assignable<value_compare>::value &&
1107*58b9f456SAndroid Build Coastguard Worker            is_nothrow_move_assignable<__node_allocator>::value);
1108*58b9f456SAndroid Build Coastguard Worker#endif // _LIBCPP_CXX03_LANG
1109*58b9f456SAndroid Build Coastguard Worker
1110*58b9f456SAndroid Build Coastguard Worker    ~__tree();
1111*58b9f456SAndroid Build Coastguard Worker
1112*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1113*58b9f456SAndroid Build Coastguard Worker          iterator begin()  _NOEXCEPT {return       iterator(__begin_node());}
1114*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1115*58b9f456SAndroid Build Coastguard Worker    const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
1116*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1117*58b9f456SAndroid Build Coastguard Worker          iterator end() _NOEXCEPT {return       iterator(__end_node());}
1118*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1119*58b9f456SAndroid Build Coastguard Worker    const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
1120*58b9f456SAndroid Build Coastguard Worker
1121*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1122*58b9f456SAndroid Build Coastguard Worker    size_type max_size() const _NOEXCEPT
1123*58b9f456SAndroid Build Coastguard Worker        {return std::min<size_type>(
1124*58b9f456SAndroid Build Coastguard Worker                __node_traits::max_size(__node_alloc()),
1125*58b9f456SAndroid Build Coastguard Worker                numeric_limits<difference_type >::max());}
1126*58b9f456SAndroid Build Coastguard Worker
1127*58b9f456SAndroid Build Coastguard Worker    void clear() _NOEXCEPT;
1128*58b9f456SAndroid Build Coastguard Worker
1129*58b9f456SAndroid Build Coastguard Worker    void swap(__tree& __t)
1130*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER <= 11
1131*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1132*58b9f456SAndroid Build Coastguard Worker            __is_nothrow_swappable<value_compare>::value
1133*58b9f456SAndroid Build Coastguard Worker            && (!__node_traits::propagate_on_container_swap::value ||
1134*58b9f456SAndroid Build Coastguard Worker                 __is_nothrow_swappable<__node_allocator>::value)
1135*58b9f456SAndroid Build Coastguard Worker            );
1136*58b9f456SAndroid Build Coastguard Worker#else
1137*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1138*58b9f456SAndroid Build Coastguard Worker#endif
1139*58b9f456SAndroid Build Coastguard Worker
1140*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
1141*58b9f456SAndroid Build Coastguard Worker    template <class _Key, class ..._Args>
1142*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool>
1143*58b9f456SAndroid Build Coastguard Worker    __emplace_unique_key_args(_Key const&, _Args&&... __args);
1144*58b9f456SAndroid Build Coastguard Worker    template <class _Key, class ..._Args>
1145*58b9f456SAndroid Build Coastguard Worker    iterator
1146*58b9f456SAndroid Build Coastguard Worker    __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
1147*58b9f456SAndroid Build Coastguard Worker
1148*58b9f456SAndroid Build Coastguard Worker    template <class... _Args>
1149*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1150*58b9f456SAndroid Build Coastguard Worker
1151*58b9f456SAndroid Build Coastguard Worker    template <class... _Args>
1152*58b9f456SAndroid Build Coastguard Worker    iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
1153*58b9f456SAndroid Build Coastguard Worker
1154*58b9f456SAndroid Build Coastguard Worker    template <class... _Args>
1155*58b9f456SAndroid Build Coastguard Worker    iterator __emplace_multi(_Args&&... __args);
1156*58b9f456SAndroid Build Coastguard Worker
1157*58b9f456SAndroid Build Coastguard Worker    template <class... _Args>
1158*58b9f456SAndroid Build Coastguard Worker    iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1159*58b9f456SAndroid Build Coastguard Worker
1160*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1161*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1162*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1163*58b9f456SAndroid Build Coastguard Worker        return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1164*58b9f456SAndroid Build Coastguard Worker                                            __can_extract_key<_Pp, key_type>());
1165*58b9f456SAndroid Build Coastguard Worker    }
1166*58b9f456SAndroid Build Coastguard Worker
1167*58b9f456SAndroid Build Coastguard Worker    template <class _First, class _Second>
1168*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1169*58b9f456SAndroid Build Coastguard Worker    typename enable_if<
1170*58b9f456SAndroid Build Coastguard Worker        __can_extract_map_key<_First, key_type, __container_value_type>::value,
1171*58b9f456SAndroid Build Coastguard Worker        pair<iterator, bool>
1172*58b9f456SAndroid Build Coastguard Worker    >::type __emplace_unique(_First&& __f, _Second&& __s) {
1173*58b9f456SAndroid Build Coastguard Worker        return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1174*58b9f456SAndroid Build Coastguard Worker                                              _VSTD::forward<_Second>(__s));
1175*58b9f456SAndroid Build Coastguard Worker    }
1176*58b9f456SAndroid Build Coastguard Worker
1177*58b9f456SAndroid Build Coastguard Worker    template <class... _Args>
1178*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1179*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1180*58b9f456SAndroid Build Coastguard Worker        return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1181*58b9f456SAndroid Build Coastguard Worker    }
1182*58b9f456SAndroid Build Coastguard Worker
1183*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1184*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1185*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool>
1186*58b9f456SAndroid Build Coastguard Worker    __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1187*58b9f456SAndroid Build Coastguard Worker      return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1188*58b9f456SAndroid Build Coastguard Worker    }
1189*58b9f456SAndroid Build Coastguard Worker
1190*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1191*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1192*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool>
1193*58b9f456SAndroid Build Coastguard Worker    __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1194*58b9f456SAndroid Build Coastguard Worker      return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1195*58b9f456SAndroid Build Coastguard Worker    }
1196*58b9f456SAndroid Build Coastguard Worker
1197*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1198*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1199*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool>
1200*58b9f456SAndroid Build Coastguard Worker    __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1201*58b9f456SAndroid Build Coastguard Worker      return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1202*58b9f456SAndroid Build Coastguard Worker    }
1203*58b9f456SAndroid Build Coastguard Worker
1204*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1205*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1206*58b9f456SAndroid Build Coastguard Worker    iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1207*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1208*58b9f456SAndroid Build Coastguard Worker                                            __can_extract_key<_Pp, key_type>());
1209*58b9f456SAndroid Build Coastguard Worker    }
1210*58b9f456SAndroid Build Coastguard Worker
1211*58b9f456SAndroid Build Coastguard Worker    template <class _First, class _Second>
1212*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1213*58b9f456SAndroid Build Coastguard Worker    typename enable_if<
1214*58b9f456SAndroid Build Coastguard Worker        __can_extract_map_key<_First, key_type, __container_value_type>::value,
1215*58b9f456SAndroid Build Coastguard Worker        iterator
1216*58b9f456SAndroid Build Coastguard Worker    >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1217*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_unique_key_args(__p, __f,
1218*58b9f456SAndroid Build Coastguard Worker                                              _VSTD::forward<_First>(__f),
1219*58b9f456SAndroid Build Coastguard Worker                                              _VSTD::forward<_Second>(__s));
1220*58b9f456SAndroid Build Coastguard Worker    }
1221*58b9f456SAndroid Build Coastguard Worker
1222*58b9f456SAndroid Build Coastguard Worker    template <class... _Args>
1223*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1224*58b9f456SAndroid Build Coastguard Worker    iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1225*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1226*58b9f456SAndroid Build Coastguard Worker    }
1227*58b9f456SAndroid Build Coastguard Worker
1228*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1229*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1230*58b9f456SAndroid Build Coastguard Worker    iterator
1231*58b9f456SAndroid Build Coastguard Worker    __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1232*58b9f456SAndroid Build Coastguard Worker      return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1233*58b9f456SAndroid Build Coastguard Worker    }
1234*58b9f456SAndroid Build Coastguard Worker
1235*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1236*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1237*58b9f456SAndroid Build Coastguard Worker    iterator
1238*58b9f456SAndroid Build Coastguard Worker    __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1239*58b9f456SAndroid Build Coastguard Worker      return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1240*58b9f456SAndroid Build Coastguard Worker    }
1241*58b9f456SAndroid Build Coastguard Worker
1242*58b9f456SAndroid Build Coastguard Worker    template <class _Pp>
1243*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1244*58b9f456SAndroid Build Coastguard Worker    iterator
1245*58b9f456SAndroid Build Coastguard Worker    __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1246*58b9f456SAndroid Build Coastguard Worker      return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1247*58b9f456SAndroid Build Coastguard Worker    }
1248*58b9f456SAndroid Build Coastguard Worker
1249*58b9f456SAndroid Build Coastguard Worker#else
1250*58b9f456SAndroid Build Coastguard Worker    template <class _Key, class _Args>
1251*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1252*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1253*58b9f456SAndroid Build Coastguard Worker    template <class _Key, class _Args>
1254*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1255*58b9f456SAndroid Build Coastguard Worker    iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
1256*58b9f456SAndroid Build Coastguard Worker#endif
1257*58b9f456SAndroid Build Coastguard Worker
1258*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1259*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1260*58b9f456SAndroid Build Coastguard Worker        return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1261*58b9f456SAndroid Build Coastguard Worker    }
1262*58b9f456SAndroid Build Coastguard Worker
1263*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1264*58b9f456SAndroid Build Coastguard Worker    iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1265*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1266*58b9f456SAndroid Build Coastguard Worker    }
1267*58b9f456SAndroid Build Coastguard Worker
1268*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_CXX03_LANG
1269*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1270*58b9f456SAndroid Build Coastguard Worker    iterator __insert_multi(const __container_value_type& __v);
1271*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1272*58b9f456SAndroid Build Coastguard Worker    iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1273*58b9f456SAndroid Build Coastguard Worker#else
1274*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1275*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1276*58b9f456SAndroid Build Coastguard Worker        return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1277*58b9f456SAndroid Build Coastguard Worker    }
1278*58b9f456SAndroid Build Coastguard Worker
1279*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1280*58b9f456SAndroid Build Coastguard Worker    iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1281*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1282*58b9f456SAndroid Build Coastguard Worker    }
1283*58b9f456SAndroid Build Coastguard Worker
1284*58b9f456SAndroid Build Coastguard Worker    template <class _Vp, class = typename enable_if<
1285*58b9f456SAndroid Build Coastguard Worker            !is_same<typename __unconstref<_Vp>::type,
1286*58b9f456SAndroid Build Coastguard Worker                     __container_value_type
1287*58b9f456SAndroid Build Coastguard Worker            >::value
1288*58b9f456SAndroid Build Coastguard Worker        >::type>
1289*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1290*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __insert_unique(_Vp&& __v) {
1291*58b9f456SAndroid Build Coastguard Worker        return __emplace_unique(_VSTD::forward<_Vp>(__v));
1292*58b9f456SAndroid Build Coastguard Worker    }
1293*58b9f456SAndroid Build Coastguard Worker
1294*58b9f456SAndroid Build Coastguard Worker    template <class _Vp, class = typename enable_if<
1295*58b9f456SAndroid Build Coastguard Worker            !is_same<typename __unconstref<_Vp>::type,
1296*58b9f456SAndroid Build Coastguard Worker                     __container_value_type
1297*58b9f456SAndroid Build Coastguard Worker            >::value
1298*58b9f456SAndroid Build Coastguard Worker        >::type>
1299*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1300*58b9f456SAndroid Build Coastguard Worker    iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1301*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1302*58b9f456SAndroid Build Coastguard Worker    }
1303*58b9f456SAndroid Build Coastguard Worker
1304*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1305*58b9f456SAndroid Build Coastguard Worker    iterator __insert_multi(__container_value_type&& __v) {
1306*58b9f456SAndroid Build Coastguard Worker        return __emplace_multi(_VSTD::move(__v));
1307*58b9f456SAndroid Build Coastguard Worker    }
1308*58b9f456SAndroid Build Coastguard Worker
1309*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1310*58b9f456SAndroid Build Coastguard Worker    iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1311*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_multi(__p, _VSTD::move(__v));
1312*58b9f456SAndroid Build Coastguard Worker    }
1313*58b9f456SAndroid Build Coastguard Worker
1314*58b9f456SAndroid Build Coastguard Worker    template <class _Vp>
1315*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1316*58b9f456SAndroid Build Coastguard Worker    iterator __insert_multi(_Vp&& __v) {
1317*58b9f456SAndroid Build Coastguard Worker        return __emplace_multi(_VSTD::forward<_Vp>(__v));
1318*58b9f456SAndroid Build Coastguard Worker    }
1319*58b9f456SAndroid Build Coastguard Worker
1320*58b9f456SAndroid Build Coastguard Worker    template <class _Vp>
1321*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1322*58b9f456SAndroid Build Coastguard Worker    iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1323*58b9f456SAndroid Build Coastguard Worker        return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1324*58b9f456SAndroid Build Coastguard Worker    }
1325*58b9f456SAndroid Build Coastguard Worker
1326*58b9f456SAndroid Build Coastguard Worker#endif // !_LIBCPP_CXX03_LANG
1327*58b9f456SAndroid Build Coastguard Worker
1328*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1329*58b9f456SAndroid Build Coastguard Worker    pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1330*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1331*58b9f456SAndroid Build Coastguard Worker    iterator             __node_insert_unique(const_iterator __p,
1332*58b9f456SAndroid Build Coastguard Worker                                              __node_pointer __nd);
1333*58b9f456SAndroid Build Coastguard Worker
1334*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1335*58b9f456SAndroid Build Coastguard Worker    iterator __node_insert_multi(__node_pointer __nd);
1336*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1337*58b9f456SAndroid Build Coastguard Worker    iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1338*58b9f456SAndroid Build Coastguard Worker
1339*58b9f456SAndroid Build Coastguard Worker
1340*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY iterator
1341*58b9f456SAndroid Build Coastguard Worker    __remove_node_pointer(__node_pointer) _NOEXCEPT;
1342*58b9f456SAndroid Build Coastguard Worker
1343*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
1344*58b9f456SAndroid Build Coastguard Worker    template <class _NodeHandle, class _InsertReturnType>
1345*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1346*58b9f456SAndroid Build Coastguard Worker    _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1347*58b9f456SAndroid Build Coastguard Worker    template <class _NodeHandle>
1348*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1349*58b9f456SAndroid Build Coastguard Worker    iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
1350*58b9f456SAndroid Build Coastguard Worker    template <class _Tree>
1351*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1352*58b9f456SAndroid Build Coastguard Worker    void __node_handle_merge_unique(_Tree& __source);
1353*58b9f456SAndroid Build Coastguard Worker
1354*58b9f456SAndroid Build Coastguard Worker    template <class _NodeHandle>
1355*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1356*58b9f456SAndroid Build Coastguard Worker    iterator __node_handle_insert_multi(_NodeHandle&&);
1357*58b9f456SAndroid Build Coastguard Worker    template <class _NodeHandle>
1358*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1359*58b9f456SAndroid Build Coastguard Worker    iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
1360*58b9f456SAndroid Build Coastguard Worker    template <class _Tree>
1361*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1362*58b9f456SAndroid Build Coastguard Worker    void __node_handle_merge_multi(_Tree& __source);
1363*58b9f456SAndroid Build Coastguard Worker
1364*58b9f456SAndroid Build Coastguard Worker
1365*58b9f456SAndroid Build Coastguard Worker    template <class _NodeHandle>
1366*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1367*58b9f456SAndroid Build Coastguard Worker    _NodeHandle __node_handle_extract(key_type const&);
1368*58b9f456SAndroid Build Coastguard Worker    template <class _NodeHandle>
1369*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1370*58b9f456SAndroid Build Coastguard Worker    _NodeHandle __node_handle_extract(const_iterator);
1371*58b9f456SAndroid Build Coastguard Worker#endif
1372*58b9f456SAndroid Build Coastguard Worker
1373*58b9f456SAndroid Build Coastguard Worker    iterator erase(const_iterator __p);
1374*58b9f456SAndroid Build Coastguard Worker    iterator erase(const_iterator __f, const_iterator __l);
1375*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1376*58b9f456SAndroid Build Coastguard Worker        size_type __erase_unique(const _Key& __k);
1377*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1378*58b9f456SAndroid Build Coastguard Worker        size_type __erase_multi(const _Key& __k);
1379*58b9f456SAndroid Build Coastguard Worker
1380*58b9f456SAndroid Build Coastguard Worker    void __insert_node_at(__parent_pointer     __parent,
1381*58b9f456SAndroid Build Coastguard Worker                          __node_base_pointer& __child,
1382*58b9f456SAndroid Build Coastguard Worker                          __node_base_pointer __new_node) _NOEXCEPT;
1383*58b9f456SAndroid Build Coastguard Worker
1384*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1385*58b9f456SAndroid Build Coastguard Worker        iterator find(const _Key& __v);
1386*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1387*58b9f456SAndroid Build Coastguard Worker        const_iterator find(const _Key& __v) const;
1388*58b9f456SAndroid Build Coastguard Worker
1389*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1390*58b9f456SAndroid Build Coastguard Worker        size_type __count_unique(const _Key& __k) const;
1391*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1392*58b9f456SAndroid Build Coastguard Worker        size_type __count_multi(const _Key& __k) const;
1393*58b9f456SAndroid Build Coastguard Worker
1394*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1395*58b9f456SAndroid Build Coastguard Worker        _LIBCPP_INLINE_VISIBILITY
1396*58b9f456SAndroid Build Coastguard Worker        iterator lower_bound(const _Key& __v)
1397*58b9f456SAndroid Build Coastguard Worker            {return __lower_bound(__v, __root(), __end_node());}
1398*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1399*58b9f456SAndroid Build Coastguard Worker        iterator __lower_bound(const _Key& __v,
1400*58b9f456SAndroid Build Coastguard Worker                               __node_pointer __root,
1401*58b9f456SAndroid Build Coastguard Worker                               __iter_pointer __result);
1402*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1403*58b9f456SAndroid Build Coastguard Worker        _LIBCPP_INLINE_VISIBILITY
1404*58b9f456SAndroid Build Coastguard Worker        const_iterator lower_bound(const _Key& __v) const
1405*58b9f456SAndroid Build Coastguard Worker            {return __lower_bound(__v, __root(), __end_node());}
1406*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1407*58b9f456SAndroid Build Coastguard Worker        const_iterator __lower_bound(const _Key& __v,
1408*58b9f456SAndroid Build Coastguard Worker                                     __node_pointer __root,
1409*58b9f456SAndroid Build Coastguard Worker                                     __iter_pointer __result) const;
1410*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1411*58b9f456SAndroid Build Coastguard Worker        _LIBCPP_INLINE_VISIBILITY
1412*58b9f456SAndroid Build Coastguard Worker        iterator upper_bound(const _Key& __v)
1413*58b9f456SAndroid Build Coastguard Worker            {return __upper_bound(__v, __root(), __end_node());}
1414*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1415*58b9f456SAndroid Build Coastguard Worker        iterator __upper_bound(const _Key& __v,
1416*58b9f456SAndroid Build Coastguard Worker                               __node_pointer __root,
1417*58b9f456SAndroid Build Coastguard Worker                               __iter_pointer __result);
1418*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1419*58b9f456SAndroid Build Coastguard Worker        _LIBCPP_INLINE_VISIBILITY
1420*58b9f456SAndroid Build Coastguard Worker        const_iterator upper_bound(const _Key& __v) const
1421*58b9f456SAndroid Build Coastguard Worker            {return __upper_bound(__v, __root(), __end_node());}
1422*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1423*58b9f456SAndroid Build Coastguard Worker        const_iterator __upper_bound(const _Key& __v,
1424*58b9f456SAndroid Build Coastguard Worker                                     __node_pointer __root,
1425*58b9f456SAndroid Build Coastguard Worker                                     __iter_pointer __result) const;
1426*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1427*58b9f456SAndroid Build Coastguard Worker        pair<iterator, iterator>
1428*58b9f456SAndroid Build Coastguard Worker        __equal_range_unique(const _Key& __k);
1429*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1430*58b9f456SAndroid Build Coastguard Worker        pair<const_iterator, const_iterator>
1431*58b9f456SAndroid Build Coastguard Worker        __equal_range_unique(const _Key& __k) const;
1432*58b9f456SAndroid Build Coastguard Worker
1433*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1434*58b9f456SAndroid Build Coastguard Worker        pair<iterator, iterator>
1435*58b9f456SAndroid Build Coastguard Worker        __equal_range_multi(const _Key& __k);
1436*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1437*58b9f456SAndroid Build Coastguard Worker        pair<const_iterator, const_iterator>
1438*58b9f456SAndroid Build Coastguard Worker        __equal_range_multi(const _Key& __k) const;
1439*58b9f456SAndroid Build Coastguard Worker
1440*58b9f456SAndroid Build Coastguard Worker    typedef __tree_node_destructor<__node_allocator> _Dp;
1441*58b9f456SAndroid Build Coastguard Worker    typedef unique_ptr<__node, _Dp> __node_holder;
1442*58b9f456SAndroid Build Coastguard Worker
1443*58b9f456SAndroid Build Coastguard Worker    __node_holder remove(const_iterator __p) _NOEXCEPT;
1444*58b9f456SAndroid Build Coastguard Workerprivate:
1445*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer&
1446*58b9f456SAndroid Build Coastguard Worker        __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1447*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer&
1448*58b9f456SAndroid Build Coastguard Worker        __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1449*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer&
1450*58b9f456SAndroid Build Coastguard Worker        __find_leaf(const_iterator __hint,
1451*58b9f456SAndroid Build Coastguard Worker                    __parent_pointer& __parent, const key_type& __v);
1452*58b9f456SAndroid Build Coastguard Worker    // FIXME: Make this function const qualified. Unfortunetly doing so
1453*58b9f456SAndroid Build Coastguard Worker    // breaks existing code which uses non-const callable comparators.
1454*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1455*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer&
1456*58b9f456SAndroid Build Coastguard Worker        __find_equal(__parent_pointer& __parent, const _Key& __v);
1457*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1458*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1459*58b9f456SAndroid Build Coastguard Worker    __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1460*58b9f456SAndroid Build Coastguard Worker      return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1461*58b9f456SAndroid Build Coastguard Worker    }
1462*58b9f456SAndroid Build Coastguard Worker    template <class _Key>
1463*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer&
1464*58b9f456SAndroid Build Coastguard Worker        __find_equal(const_iterator __hint, __parent_pointer& __parent,
1465*58b9f456SAndroid Build Coastguard Worker                     __node_base_pointer& __dummy,
1466*58b9f456SAndroid Build Coastguard Worker                     const _Key& __v);
1467*58b9f456SAndroid Build Coastguard Worker
1468*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
1469*58b9f456SAndroid Build Coastguard Worker    template <class ..._Args>
1470*58b9f456SAndroid Build Coastguard Worker    __node_holder __construct_node(_Args&& ...__args);
1471*58b9f456SAndroid Build Coastguard Worker#else
1472*58b9f456SAndroid Build Coastguard Worker    __node_holder __construct_node(const __container_value_type& __v);
1473*58b9f456SAndroid Build Coastguard Worker#endif
1474*58b9f456SAndroid Build Coastguard Worker
1475*58b9f456SAndroid Build Coastguard Worker    void destroy(__node_pointer __nd) _NOEXCEPT;
1476*58b9f456SAndroid Build Coastguard Worker
1477*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1478*58b9f456SAndroid Build Coastguard Worker    void __copy_assign_alloc(const __tree& __t)
1479*58b9f456SAndroid Build Coastguard Worker        {__copy_assign_alloc(__t, integral_constant<bool,
1480*58b9f456SAndroid Build Coastguard Worker             __node_traits::propagate_on_container_copy_assignment::value>());}
1481*58b9f456SAndroid Build Coastguard Worker
1482*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1483*58b9f456SAndroid Build Coastguard Worker    void __copy_assign_alloc(const __tree& __t, true_type)
1484*58b9f456SAndroid Build Coastguard Worker        {
1485*58b9f456SAndroid Build Coastguard Worker        if (__node_alloc() != __t.__node_alloc())
1486*58b9f456SAndroid Build Coastguard Worker            clear();
1487*58b9f456SAndroid Build Coastguard Worker        __node_alloc() = __t.__node_alloc();
1488*58b9f456SAndroid Build Coastguard Worker        }
1489*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1490*58b9f456SAndroid Build Coastguard Worker    void __copy_assign_alloc(const __tree&, false_type) {}
1491*58b9f456SAndroid Build Coastguard Worker
1492*58b9f456SAndroid Build Coastguard Worker    void __move_assign(__tree& __t, false_type);
1493*58b9f456SAndroid Build Coastguard Worker    void __move_assign(__tree& __t, true_type)
1494*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1495*58b9f456SAndroid Build Coastguard Worker                   is_nothrow_move_assignable<__node_allocator>::value);
1496*58b9f456SAndroid Build Coastguard Worker
1497*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1498*58b9f456SAndroid Build Coastguard Worker    void __move_assign_alloc(__tree& __t)
1499*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1500*58b9f456SAndroid Build Coastguard Worker            !__node_traits::propagate_on_container_move_assignment::value ||
1501*58b9f456SAndroid Build Coastguard Worker            is_nothrow_move_assignable<__node_allocator>::value)
1502*58b9f456SAndroid Build Coastguard Worker        {__move_assign_alloc(__t, integral_constant<bool,
1503*58b9f456SAndroid Build Coastguard Worker             __node_traits::propagate_on_container_move_assignment::value>());}
1504*58b9f456SAndroid Build Coastguard Worker
1505*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1506*58b9f456SAndroid Build Coastguard Worker    void __move_assign_alloc(__tree& __t, true_type)
1507*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1508*58b9f456SAndroid Build Coastguard Worker        {__node_alloc() = _VSTD::move(__t.__node_alloc());}
1509*58b9f456SAndroid Build Coastguard Worker    _LIBCPP_INLINE_VISIBILITY
1510*58b9f456SAndroid Build Coastguard Worker    void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
1511*58b9f456SAndroid Build Coastguard Worker
1512*58b9f456SAndroid Build Coastguard Worker    __node_pointer __detach();
1513*58b9f456SAndroid Build Coastguard Worker    static __node_pointer __detach(__node_pointer);
1514*58b9f456SAndroid Build Coastguard Worker
1515*58b9f456SAndroid Build Coastguard Worker    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1516*58b9f456SAndroid Build Coastguard Worker    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
1517*58b9f456SAndroid Build Coastguard Worker};
1518*58b9f456SAndroid Build Coastguard Worker
1519*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1520*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
1521*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1522*58b9f456SAndroid Build Coastguard Worker            is_nothrow_default_constructible<__node_allocator>::value &&
1523*58b9f456SAndroid Build Coastguard Worker            is_nothrow_copy_constructible<value_compare>::value)
1524*58b9f456SAndroid Build Coastguard Worker    : __pair3_(0, __comp)
1525*58b9f456SAndroid Build Coastguard Worker{
1526*58b9f456SAndroid Build Coastguard Worker    __begin_node() = __end_node();
1527*58b9f456SAndroid Build Coastguard Worker}
1528*58b9f456SAndroid Build Coastguard Worker
1529*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1530*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1531*58b9f456SAndroid Build Coastguard Worker    : __begin_node_(__iter_pointer()),
1532*58b9f456SAndroid Build Coastguard Worker      __pair1_(__second_tag(), __node_allocator(__a)),
1533*58b9f456SAndroid Build Coastguard Worker      __pair3_(0)
1534*58b9f456SAndroid Build Coastguard Worker{
1535*58b9f456SAndroid Build Coastguard Worker    __begin_node() = __end_node();
1536*58b9f456SAndroid Build Coastguard Worker}
1537*58b9f456SAndroid Build Coastguard Worker
1538*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1539*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1540*58b9f456SAndroid Build Coastguard Worker                                           const allocator_type& __a)
1541*58b9f456SAndroid Build Coastguard Worker    : __begin_node_(__iter_pointer()),
1542*58b9f456SAndroid Build Coastguard Worker      __pair1_(__second_tag(), __node_allocator(__a)),
1543*58b9f456SAndroid Build Coastguard Worker      __pair3_(0, __comp)
1544*58b9f456SAndroid Build Coastguard Worker{
1545*58b9f456SAndroid Build Coastguard Worker    __begin_node() = __end_node();
1546*58b9f456SAndroid Build Coastguard Worker}
1547*58b9f456SAndroid Build Coastguard Worker
1548*58b9f456SAndroid Build Coastguard Worker// Precondition:  size() != 0
1549*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1550*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1551*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__detach()
1552*58b9f456SAndroid Build Coastguard Worker{
1553*58b9f456SAndroid Build Coastguard Worker    __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
1554*58b9f456SAndroid Build Coastguard Worker    __begin_node() = __end_node();
1555*58b9f456SAndroid Build Coastguard Worker    __end_node()->__left_->__parent_ = nullptr;
1556*58b9f456SAndroid Build Coastguard Worker    __end_node()->__left_ = nullptr;
1557*58b9f456SAndroid Build Coastguard Worker    size() = 0;
1558*58b9f456SAndroid Build Coastguard Worker    // __cache->__left_ == nullptr
1559*58b9f456SAndroid Build Coastguard Worker    if (__cache->__right_ != nullptr)
1560*58b9f456SAndroid Build Coastguard Worker        __cache = static_cast<__node_pointer>(__cache->__right_);
1561*58b9f456SAndroid Build Coastguard Worker    // __cache->__left_ == nullptr
1562*58b9f456SAndroid Build Coastguard Worker    // __cache->__right_ == nullptr
1563*58b9f456SAndroid Build Coastguard Worker    return __cache;
1564*58b9f456SAndroid Build Coastguard Worker}
1565*58b9f456SAndroid Build Coastguard Worker
1566*58b9f456SAndroid Build Coastguard Worker// Precondition:  __cache != nullptr
1567*58b9f456SAndroid Build Coastguard Worker//    __cache->left_ == nullptr
1568*58b9f456SAndroid Build Coastguard Worker//    __cache->right_ == nullptr
1569*58b9f456SAndroid Build Coastguard Worker//    This is no longer a red-black tree
1570*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1571*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1572*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1573*58b9f456SAndroid Build Coastguard Worker{
1574*58b9f456SAndroid Build Coastguard Worker    if (__cache->__parent_ == nullptr)
1575*58b9f456SAndroid Build Coastguard Worker        return nullptr;
1576*58b9f456SAndroid Build Coastguard Worker    if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
1577*58b9f456SAndroid Build Coastguard Worker    {
1578*58b9f456SAndroid Build Coastguard Worker        __cache->__parent_->__left_ = nullptr;
1579*58b9f456SAndroid Build Coastguard Worker        __cache = static_cast<__node_pointer>(__cache->__parent_);
1580*58b9f456SAndroid Build Coastguard Worker        if (__cache->__right_ == nullptr)
1581*58b9f456SAndroid Build Coastguard Worker            return __cache;
1582*58b9f456SAndroid Build Coastguard Worker        return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1583*58b9f456SAndroid Build Coastguard Worker    }
1584*58b9f456SAndroid Build Coastguard Worker    // __cache is right child
1585*58b9f456SAndroid Build Coastguard Worker    __cache->__parent_unsafe()->__right_ = nullptr;
1586*58b9f456SAndroid Build Coastguard Worker    __cache = static_cast<__node_pointer>(__cache->__parent_);
1587*58b9f456SAndroid Build Coastguard Worker    if (__cache->__left_ == nullptr)
1588*58b9f456SAndroid Build Coastguard Worker        return __cache;
1589*58b9f456SAndroid Build Coastguard Worker    return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1590*58b9f456SAndroid Build Coastguard Worker}
1591*58b9f456SAndroid Build Coastguard Worker
1592*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1593*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>&
1594*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1595*58b9f456SAndroid Build Coastguard Worker{
1596*58b9f456SAndroid Build Coastguard Worker    if (this != &__t)
1597*58b9f456SAndroid Build Coastguard Worker    {
1598*58b9f456SAndroid Build Coastguard Worker        value_comp() = __t.value_comp();
1599*58b9f456SAndroid Build Coastguard Worker        __copy_assign_alloc(__t);
1600*58b9f456SAndroid Build Coastguard Worker        __assign_multi(__t.begin(), __t.end());
1601*58b9f456SAndroid Build Coastguard Worker    }
1602*58b9f456SAndroid Build Coastguard Worker    return *this;
1603*58b9f456SAndroid Build Coastguard Worker}
1604*58b9f456SAndroid Build Coastguard Worker
1605*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1606*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator>
1607*58b9f456SAndroid Build Coastguard Workervoid
1608*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1609*58b9f456SAndroid Build Coastguard Worker{
1610*58b9f456SAndroid Build Coastguard Worker    typedef iterator_traits<_InputIterator> _ITraits;
1611*58b9f456SAndroid Build Coastguard Worker    typedef typename _ITraits::value_type _ItValueType;
1612*58b9f456SAndroid Build Coastguard Worker    static_assert((is_same<_ItValueType, __container_value_type>::value),
1613*58b9f456SAndroid Build Coastguard Worker                  "__assign_unique may only be called with the containers value type");
1614*58b9f456SAndroid Build Coastguard Worker
1615*58b9f456SAndroid Build Coastguard Worker    if (size() != 0)
1616*58b9f456SAndroid Build Coastguard Worker    {
1617*58b9f456SAndroid Build Coastguard Worker        __node_pointer __cache = __detach();
1618*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NO_EXCEPTIONS
1619*58b9f456SAndroid Build Coastguard Worker        try
1620*58b9f456SAndroid Build Coastguard Worker        {
1621*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NO_EXCEPTIONS
1622*58b9f456SAndroid Build Coastguard Worker            for (; __cache != nullptr && __first != __last; ++__first)
1623*58b9f456SAndroid Build Coastguard Worker            {
1624*58b9f456SAndroid Build Coastguard Worker                __cache->__value_ = *__first;
1625*58b9f456SAndroid Build Coastguard Worker                __node_pointer __next = __detach(__cache);
1626*58b9f456SAndroid Build Coastguard Worker                __node_insert_unique(__cache);
1627*58b9f456SAndroid Build Coastguard Worker                __cache = __next;
1628*58b9f456SAndroid Build Coastguard Worker            }
1629*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NO_EXCEPTIONS
1630*58b9f456SAndroid Build Coastguard Worker        }
1631*58b9f456SAndroid Build Coastguard Worker        catch (...)
1632*58b9f456SAndroid Build Coastguard Worker        {
1633*58b9f456SAndroid Build Coastguard Worker            while (__cache->__parent_ != nullptr)
1634*58b9f456SAndroid Build Coastguard Worker                __cache = static_cast<__node_pointer>(__cache->__parent_);
1635*58b9f456SAndroid Build Coastguard Worker            destroy(__cache);
1636*58b9f456SAndroid Build Coastguard Worker            throw;
1637*58b9f456SAndroid Build Coastguard Worker        }
1638*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NO_EXCEPTIONS
1639*58b9f456SAndroid Build Coastguard Worker        if (__cache != nullptr)
1640*58b9f456SAndroid Build Coastguard Worker        {
1641*58b9f456SAndroid Build Coastguard Worker            while (__cache->__parent_ != nullptr)
1642*58b9f456SAndroid Build Coastguard Worker                __cache = static_cast<__node_pointer>(__cache->__parent_);
1643*58b9f456SAndroid Build Coastguard Worker            destroy(__cache);
1644*58b9f456SAndroid Build Coastguard Worker        }
1645*58b9f456SAndroid Build Coastguard Worker    }
1646*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first)
1647*58b9f456SAndroid Build Coastguard Worker        __insert_unique(*__first);
1648*58b9f456SAndroid Build Coastguard Worker}
1649*58b9f456SAndroid Build Coastguard Worker
1650*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1651*58b9f456SAndroid Build Coastguard Workertemplate <class _InputIterator>
1652*58b9f456SAndroid Build Coastguard Workervoid
1653*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1654*58b9f456SAndroid Build Coastguard Worker{
1655*58b9f456SAndroid Build Coastguard Worker    typedef iterator_traits<_InputIterator> _ITraits;
1656*58b9f456SAndroid Build Coastguard Worker    typedef typename _ITraits::value_type _ItValueType;
1657*58b9f456SAndroid Build Coastguard Worker    static_assert((is_same<_ItValueType, __container_value_type>::value ||
1658*58b9f456SAndroid Build Coastguard Worker                  is_same<_ItValueType, __node_value_type>::value),
1659*58b9f456SAndroid Build Coastguard Worker                  "__assign_multi may only be called with the containers value type"
1660*58b9f456SAndroid Build Coastguard Worker                  " or the nodes value type");
1661*58b9f456SAndroid Build Coastguard Worker    if (size() != 0)
1662*58b9f456SAndroid Build Coastguard Worker    {
1663*58b9f456SAndroid Build Coastguard Worker        __node_pointer __cache = __detach();
1664*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NO_EXCEPTIONS
1665*58b9f456SAndroid Build Coastguard Worker        try
1666*58b9f456SAndroid Build Coastguard Worker        {
1667*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NO_EXCEPTIONS
1668*58b9f456SAndroid Build Coastguard Worker            for (; __cache != nullptr && __first != __last; ++__first)
1669*58b9f456SAndroid Build Coastguard Worker            {
1670*58b9f456SAndroid Build Coastguard Worker                __cache->__value_ = *__first;
1671*58b9f456SAndroid Build Coastguard Worker                __node_pointer __next = __detach(__cache);
1672*58b9f456SAndroid Build Coastguard Worker                __node_insert_multi(__cache);
1673*58b9f456SAndroid Build Coastguard Worker                __cache = __next;
1674*58b9f456SAndroid Build Coastguard Worker            }
1675*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NO_EXCEPTIONS
1676*58b9f456SAndroid Build Coastguard Worker        }
1677*58b9f456SAndroid Build Coastguard Worker        catch (...)
1678*58b9f456SAndroid Build Coastguard Worker        {
1679*58b9f456SAndroid Build Coastguard Worker            while (__cache->__parent_ != nullptr)
1680*58b9f456SAndroid Build Coastguard Worker                __cache = static_cast<__node_pointer>(__cache->__parent_);
1681*58b9f456SAndroid Build Coastguard Worker            destroy(__cache);
1682*58b9f456SAndroid Build Coastguard Worker            throw;
1683*58b9f456SAndroid Build Coastguard Worker        }
1684*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NO_EXCEPTIONS
1685*58b9f456SAndroid Build Coastguard Worker        if (__cache != nullptr)
1686*58b9f456SAndroid Build Coastguard Worker        {
1687*58b9f456SAndroid Build Coastguard Worker            while (__cache->__parent_ != nullptr)
1688*58b9f456SAndroid Build Coastguard Worker                __cache = static_cast<__node_pointer>(__cache->__parent_);
1689*58b9f456SAndroid Build Coastguard Worker            destroy(__cache);
1690*58b9f456SAndroid Build Coastguard Worker        }
1691*58b9f456SAndroid Build Coastguard Worker    }
1692*58b9f456SAndroid Build Coastguard Worker    for (; __first != __last; ++__first)
1693*58b9f456SAndroid Build Coastguard Worker        __insert_multi(_NodeTypes::__get_value(*__first));
1694*58b9f456SAndroid Build Coastguard Worker}
1695*58b9f456SAndroid Build Coastguard Worker
1696*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1697*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1698*58b9f456SAndroid Build Coastguard Worker    : __begin_node_(__iter_pointer()),
1699*58b9f456SAndroid Build Coastguard Worker      __pair1_(__second_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1700*58b9f456SAndroid Build Coastguard Worker      __pair3_(0, __t.value_comp())
1701*58b9f456SAndroid Build Coastguard Worker{
1702*58b9f456SAndroid Build Coastguard Worker    __begin_node() = __end_node();
1703*58b9f456SAndroid Build Coastguard Worker}
1704*58b9f456SAndroid Build Coastguard Worker
1705*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
1706*58b9f456SAndroid Build Coastguard Worker
1707*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1708*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
1709*58b9f456SAndroid Build Coastguard Worker    _NOEXCEPT_(
1710*58b9f456SAndroid Build Coastguard Worker        is_nothrow_move_constructible<__node_allocator>::value &&
1711*58b9f456SAndroid Build Coastguard Worker        is_nothrow_move_constructible<value_compare>::value)
1712*58b9f456SAndroid Build Coastguard Worker    : __begin_node_(_VSTD::move(__t.__begin_node_)),
1713*58b9f456SAndroid Build Coastguard Worker      __pair1_(_VSTD::move(__t.__pair1_)),
1714*58b9f456SAndroid Build Coastguard Worker      __pair3_(_VSTD::move(__t.__pair3_))
1715*58b9f456SAndroid Build Coastguard Worker{
1716*58b9f456SAndroid Build Coastguard Worker    if (size() == 0)
1717*58b9f456SAndroid Build Coastguard Worker        __begin_node() = __end_node();
1718*58b9f456SAndroid Build Coastguard Worker    else
1719*58b9f456SAndroid Build Coastguard Worker    {
1720*58b9f456SAndroid Build Coastguard Worker        __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1721*58b9f456SAndroid Build Coastguard Worker        __t.__begin_node() = __t.__end_node();
1722*58b9f456SAndroid Build Coastguard Worker        __t.__end_node()->__left_ = nullptr;
1723*58b9f456SAndroid Build Coastguard Worker        __t.size() = 0;
1724*58b9f456SAndroid Build Coastguard Worker    }
1725*58b9f456SAndroid Build Coastguard Worker}
1726*58b9f456SAndroid Build Coastguard Worker
1727*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1728*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1729*58b9f456SAndroid Build Coastguard Worker    : __pair1_(__second_tag(), __node_allocator(__a)),
1730*58b9f456SAndroid Build Coastguard Worker      __pair3_(0, _VSTD::move(__t.value_comp()))
1731*58b9f456SAndroid Build Coastguard Worker{
1732*58b9f456SAndroid Build Coastguard Worker    if (__a == __t.__alloc())
1733*58b9f456SAndroid Build Coastguard Worker    {
1734*58b9f456SAndroid Build Coastguard Worker        if (__t.size() == 0)
1735*58b9f456SAndroid Build Coastguard Worker            __begin_node() = __end_node();
1736*58b9f456SAndroid Build Coastguard Worker        else
1737*58b9f456SAndroid Build Coastguard Worker        {
1738*58b9f456SAndroid Build Coastguard Worker            __begin_node() = __t.__begin_node();
1739*58b9f456SAndroid Build Coastguard Worker            __end_node()->__left_ = __t.__end_node()->__left_;
1740*58b9f456SAndroid Build Coastguard Worker            __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1741*58b9f456SAndroid Build Coastguard Worker            size() = __t.size();
1742*58b9f456SAndroid Build Coastguard Worker            __t.__begin_node() = __t.__end_node();
1743*58b9f456SAndroid Build Coastguard Worker            __t.__end_node()->__left_ = nullptr;
1744*58b9f456SAndroid Build Coastguard Worker            __t.size() = 0;
1745*58b9f456SAndroid Build Coastguard Worker        }
1746*58b9f456SAndroid Build Coastguard Worker    }
1747*58b9f456SAndroid Build Coastguard Worker    else
1748*58b9f456SAndroid Build Coastguard Worker    {
1749*58b9f456SAndroid Build Coastguard Worker        __begin_node() = __end_node();
1750*58b9f456SAndroid Build Coastguard Worker    }
1751*58b9f456SAndroid Build Coastguard Worker}
1752*58b9f456SAndroid Build Coastguard Worker
1753*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1754*58b9f456SAndroid Build Coastguard Workervoid
1755*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1756*58b9f456SAndroid Build Coastguard Worker    _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1757*58b9f456SAndroid Build Coastguard Worker               is_nothrow_move_assignable<__node_allocator>::value)
1758*58b9f456SAndroid Build Coastguard Worker{
1759*58b9f456SAndroid Build Coastguard Worker    destroy(static_cast<__node_pointer>(__end_node()->__left_));
1760*58b9f456SAndroid Build Coastguard Worker    __begin_node_ = __t.__begin_node_;
1761*58b9f456SAndroid Build Coastguard Worker    __pair1_.first() = __t.__pair1_.first();
1762*58b9f456SAndroid Build Coastguard Worker    __move_assign_alloc(__t);
1763*58b9f456SAndroid Build Coastguard Worker    __pair3_ = _VSTD::move(__t.__pair3_);
1764*58b9f456SAndroid Build Coastguard Worker    if (size() == 0)
1765*58b9f456SAndroid Build Coastguard Worker        __begin_node() = __end_node();
1766*58b9f456SAndroid Build Coastguard Worker    else
1767*58b9f456SAndroid Build Coastguard Worker    {
1768*58b9f456SAndroid Build Coastguard Worker        __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1769*58b9f456SAndroid Build Coastguard Worker        __t.__begin_node() = __t.__end_node();
1770*58b9f456SAndroid Build Coastguard Worker        __t.__end_node()->__left_ = nullptr;
1771*58b9f456SAndroid Build Coastguard Worker        __t.size() = 0;
1772*58b9f456SAndroid Build Coastguard Worker    }
1773*58b9f456SAndroid Build Coastguard Worker}
1774*58b9f456SAndroid Build Coastguard Worker
1775*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1776*58b9f456SAndroid Build Coastguard Workervoid
1777*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1778*58b9f456SAndroid Build Coastguard Worker{
1779*58b9f456SAndroid Build Coastguard Worker    if (__node_alloc() == __t.__node_alloc())
1780*58b9f456SAndroid Build Coastguard Worker        __move_assign(__t, true_type());
1781*58b9f456SAndroid Build Coastguard Worker    else
1782*58b9f456SAndroid Build Coastguard Worker    {
1783*58b9f456SAndroid Build Coastguard Worker        value_comp() = _VSTD::move(__t.value_comp());
1784*58b9f456SAndroid Build Coastguard Worker        const_iterator __e = end();
1785*58b9f456SAndroid Build Coastguard Worker        if (size() != 0)
1786*58b9f456SAndroid Build Coastguard Worker        {
1787*58b9f456SAndroid Build Coastguard Worker            __node_pointer __cache = __detach();
1788*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NO_EXCEPTIONS
1789*58b9f456SAndroid Build Coastguard Worker            try
1790*58b9f456SAndroid Build Coastguard Worker            {
1791*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NO_EXCEPTIONS
1792*58b9f456SAndroid Build Coastguard Worker                while (__cache != nullptr && __t.size() != 0)
1793*58b9f456SAndroid Build Coastguard Worker                {
1794*58b9f456SAndroid Build Coastguard Worker                    __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
1795*58b9f456SAndroid Build Coastguard Worker                    __node_pointer __next = __detach(__cache);
1796*58b9f456SAndroid Build Coastguard Worker                    __node_insert_multi(__cache);
1797*58b9f456SAndroid Build Coastguard Worker                    __cache = __next;
1798*58b9f456SAndroid Build Coastguard Worker                }
1799*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_NO_EXCEPTIONS
1800*58b9f456SAndroid Build Coastguard Worker            }
1801*58b9f456SAndroid Build Coastguard Worker            catch (...)
1802*58b9f456SAndroid Build Coastguard Worker            {
1803*58b9f456SAndroid Build Coastguard Worker                while (__cache->__parent_ != nullptr)
1804*58b9f456SAndroid Build Coastguard Worker                    __cache = static_cast<__node_pointer>(__cache->__parent_);
1805*58b9f456SAndroid Build Coastguard Worker                destroy(__cache);
1806*58b9f456SAndroid Build Coastguard Worker                throw;
1807*58b9f456SAndroid Build Coastguard Worker            }
1808*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_NO_EXCEPTIONS
1809*58b9f456SAndroid Build Coastguard Worker            if (__cache != nullptr)
1810*58b9f456SAndroid Build Coastguard Worker            {
1811*58b9f456SAndroid Build Coastguard Worker                while (__cache->__parent_ != nullptr)
1812*58b9f456SAndroid Build Coastguard Worker                    __cache = static_cast<__node_pointer>(__cache->__parent_);
1813*58b9f456SAndroid Build Coastguard Worker                destroy(__cache);
1814*58b9f456SAndroid Build Coastguard Worker            }
1815*58b9f456SAndroid Build Coastguard Worker        }
1816*58b9f456SAndroid Build Coastguard Worker        while (__t.size() != 0)
1817*58b9f456SAndroid Build Coastguard Worker            __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
1818*58b9f456SAndroid Build Coastguard Worker    }
1819*58b9f456SAndroid Build Coastguard Worker}
1820*58b9f456SAndroid Build Coastguard Worker
1821*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1822*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>&
1823*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
1824*58b9f456SAndroid Build Coastguard Worker    _NOEXCEPT_(
1825*58b9f456SAndroid Build Coastguard Worker        __node_traits::propagate_on_container_move_assignment::value &&
1826*58b9f456SAndroid Build Coastguard Worker        is_nothrow_move_assignable<value_compare>::value &&
1827*58b9f456SAndroid Build Coastguard Worker        is_nothrow_move_assignable<__node_allocator>::value)
1828*58b9f456SAndroid Build Coastguard Worker
1829*58b9f456SAndroid Build Coastguard Worker{
1830*58b9f456SAndroid Build Coastguard Worker    __move_assign(__t, integral_constant<bool,
1831*58b9f456SAndroid Build Coastguard Worker                  __node_traits::propagate_on_container_move_assignment::value>());
1832*58b9f456SAndroid Build Coastguard Worker    return *this;
1833*58b9f456SAndroid Build Coastguard Worker}
1834*58b9f456SAndroid Build Coastguard Worker
1835*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_CXX03_LANG
1836*58b9f456SAndroid Build Coastguard Worker
1837*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1838*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::~__tree()
1839*58b9f456SAndroid Build Coastguard Worker{
1840*58b9f456SAndroid Build Coastguard Worker    static_assert((is_copy_constructible<value_compare>::value),
1841*58b9f456SAndroid Build Coastguard Worker                 "Comparator must be copy-constructible.");
1842*58b9f456SAndroid Build Coastguard Worker  destroy(__root());
1843*58b9f456SAndroid Build Coastguard Worker}
1844*58b9f456SAndroid Build Coastguard Worker
1845*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1846*58b9f456SAndroid Build Coastguard Workervoid
1847*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
1848*58b9f456SAndroid Build Coastguard Worker{
1849*58b9f456SAndroid Build Coastguard Worker    if (__nd != nullptr)
1850*58b9f456SAndroid Build Coastguard Worker    {
1851*58b9f456SAndroid Build Coastguard Worker        destroy(static_cast<__node_pointer>(__nd->__left_));
1852*58b9f456SAndroid Build Coastguard Worker        destroy(static_cast<__node_pointer>(__nd->__right_));
1853*58b9f456SAndroid Build Coastguard Worker        __node_allocator& __na = __node_alloc();
1854*58b9f456SAndroid Build Coastguard Worker        __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
1855*58b9f456SAndroid Build Coastguard Worker        __node_traits::deallocate(__na, __nd, 1);
1856*58b9f456SAndroid Build Coastguard Worker    }
1857*58b9f456SAndroid Build Coastguard Worker}
1858*58b9f456SAndroid Build Coastguard Worker
1859*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1860*58b9f456SAndroid Build Coastguard Workervoid
1861*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1862*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER <= 11
1863*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(
1864*58b9f456SAndroid Build Coastguard Worker            __is_nothrow_swappable<value_compare>::value
1865*58b9f456SAndroid Build Coastguard Worker            && (!__node_traits::propagate_on_container_swap::value ||
1866*58b9f456SAndroid Build Coastguard Worker                 __is_nothrow_swappable<__node_allocator>::value)
1867*58b9f456SAndroid Build Coastguard Worker            )
1868*58b9f456SAndroid Build Coastguard Worker#else
1869*58b9f456SAndroid Build Coastguard Worker        _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1870*58b9f456SAndroid Build Coastguard Worker#endif
1871*58b9f456SAndroid Build Coastguard Worker{
1872*58b9f456SAndroid Build Coastguard Worker    using _VSTD::swap;
1873*58b9f456SAndroid Build Coastguard Worker    swap(__begin_node_, __t.__begin_node_);
1874*58b9f456SAndroid Build Coastguard Worker    swap(__pair1_.first(), __t.__pair1_.first());
1875*58b9f456SAndroid Build Coastguard Worker    __swap_allocator(__node_alloc(), __t.__node_alloc());
1876*58b9f456SAndroid Build Coastguard Worker    __pair3_.swap(__t.__pair3_);
1877*58b9f456SAndroid Build Coastguard Worker    if (size() == 0)
1878*58b9f456SAndroid Build Coastguard Worker        __begin_node() = __end_node();
1879*58b9f456SAndroid Build Coastguard Worker    else
1880*58b9f456SAndroid Build Coastguard Worker        __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1881*58b9f456SAndroid Build Coastguard Worker    if (__t.size() == 0)
1882*58b9f456SAndroid Build Coastguard Worker        __t.__begin_node() = __t.__end_node();
1883*58b9f456SAndroid Build Coastguard Worker    else
1884*58b9f456SAndroid Build Coastguard Worker        __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
1885*58b9f456SAndroid Build Coastguard Worker}
1886*58b9f456SAndroid Build Coastguard Worker
1887*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1888*58b9f456SAndroid Build Coastguard Workervoid
1889*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
1890*58b9f456SAndroid Build Coastguard Worker{
1891*58b9f456SAndroid Build Coastguard Worker    destroy(__root());
1892*58b9f456SAndroid Build Coastguard Worker    size() = 0;
1893*58b9f456SAndroid Build Coastguard Worker    __begin_node() = __end_node();
1894*58b9f456SAndroid Build Coastguard Worker    __end_node()->__left_ = nullptr;
1895*58b9f456SAndroid Build Coastguard Worker}
1896*58b9f456SAndroid Build Coastguard Worker
1897*58b9f456SAndroid Build Coastguard Worker// Find lower_bound place to insert
1898*58b9f456SAndroid Build Coastguard Worker// Set __parent to parent of null leaf
1899*58b9f456SAndroid Build Coastguard Worker// Return reference to null leaf
1900*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1901*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1902*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
1903*58b9f456SAndroid Build Coastguard Worker                                                   const key_type& __v)
1904*58b9f456SAndroid Build Coastguard Worker{
1905*58b9f456SAndroid Build Coastguard Worker    __node_pointer __nd = __root();
1906*58b9f456SAndroid Build Coastguard Worker    if (__nd != nullptr)
1907*58b9f456SAndroid Build Coastguard Worker    {
1908*58b9f456SAndroid Build Coastguard Worker        while (true)
1909*58b9f456SAndroid Build Coastguard Worker        {
1910*58b9f456SAndroid Build Coastguard Worker            if (value_comp()(__nd->__value_, __v))
1911*58b9f456SAndroid Build Coastguard Worker            {
1912*58b9f456SAndroid Build Coastguard Worker                if (__nd->__right_ != nullptr)
1913*58b9f456SAndroid Build Coastguard Worker                    __nd = static_cast<__node_pointer>(__nd->__right_);
1914*58b9f456SAndroid Build Coastguard Worker                else
1915*58b9f456SAndroid Build Coastguard Worker                {
1916*58b9f456SAndroid Build Coastguard Worker                    __parent = static_cast<__parent_pointer>(__nd);
1917*58b9f456SAndroid Build Coastguard Worker                    return __nd->__right_;
1918*58b9f456SAndroid Build Coastguard Worker                }
1919*58b9f456SAndroid Build Coastguard Worker            }
1920*58b9f456SAndroid Build Coastguard Worker            else
1921*58b9f456SAndroid Build Coastguard Worker            {
1922*58b9f456SAndroid Build Coastguard Worker                if (__nd->__left_ != nullptr)
1923*58b9f456SAndroid Build Coastguard Worker                    __nd = static_cast<__node_pointer>(__nd->__left_);
1924*58b9f456SAndroid Build Coastguard Worker                else
1925*58b9f456SAndroid Build Coastguard Worker                {
1926*58b9f456SAndroid Build Coastguard Worker                    __parent = static_cast<__parent_pointer>(__nd);
1927*58b9f456SAndroid Build Coastguard Worker                    return __parent->__left_;
1928*58b9f456SAndroid Build Coastguard Worker                }
1929*58b9f456SAndroid Build Coastguard Worker            }
1930*58b9f456SAndroid Build Coastguard Worker        }
1931*58b9f456SAndroid Build Coastguard Worker    }
1932*58b9f456SAndroid Build Coastguard Worker    __parent = static_cast<__parent_pointer>(__end_node());
1933*58b9f456SAndroid Build Coastguard Worker    return __parent->__left_;
1934*58b9f456SAndroid Build Coastguard Worker}
1935*58b9f456SAndroid Build Coastguard Worker
1936*58b9f456SAndroid Build Coastguard Worker// Find upper_bound place to insert
1937*58b9f456SAndroid Build Coastguard Worker// Set __parent to parent of null leaf
1938*58b9f456SAndroid Build Coastguard Worker// Return reference to null leaf
1939*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1940*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1941*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
1942*58b9f456SAndroid Build Coastguard Worker                                                    const key_type& __v)
1943*58b9f456SAndroid Build Coastguard Worker{
1944*58b9f456SAndroid Build Coastguard Worker    __node_pointer __nd = __root();
1945*58b9f456SAndroid Build Coastguard Worker    if (__nd != nullptr)
1946*58b9f456SAndroid Build Coastguard Worker    {
1947*58b9f456SAndroid Build Coastguard Worker        while (true)
1948*58b9f456SAndroid Build Coastguard Worker        {
1949*58b9f456SAndroid Build Coastguard Worker            if (value_comp()(__v, __nd->__value_))
1950*58b9f456SAndroid Build Coastguard Worker            {
1951*58b9f456SAndroid Build Coastguard Worker                if (__nd->__left_ != nullptr)
1952*58b9f456SAndroid Build Coastguard Worker                    __nd = static_cast<__node_pointer>(__nd->__left_);
1953*58b9f456SAndroid Build Coastguard Worker                else
1954*58b9f456SAndroid Build Coastguard Worker                {
1955*58b9f456SAndroid Build Coastguard Worker                    __parent = static_cast<__parent_pointer>(__nd);
1956*58b9f456SAndroid Build Coastguard Worker                    return __parent->__left_;
1957*58b9f456SAndroid Build Coastguard Worker                }
1958*58b9f456SAndroid Build Coastguard Worker            }
1959*58b9f456SAndroid Build Coastguard Worker            else
1960*58b9f456SAndroid Build Coastguard Worker            {
1961*58b9f456SAndroid Build Coastguard Worker                if (__nd->__right_ != nullptr)
1962*58b9f456SAndroid Build Coastguard Worker                    __nd = static_cast<__node_pointer>(__nd->__right_);
1963*58b9f456SAndroid Build Coastguard Worker                else
1964*58b9f456SAndroid Build Coastguard Worker                {
1965*58b9f456SAndroid Build Coastguard Worker                    __parent = static_cast<__parent_pointer>(__nd);
1966*58b9f456SAndroid Build Coastguard Worker                    return __nd->__right_;
1967*58b9f456SAndroid Build Coastguard Worker                }
1968*58b9f456SAndroid Build Coastguard Worker            }
1969*58b9f456SAndroid Build Coastguard Worker        }
1970*58b9f456SAndroid Build Coastguard Worker    }
1971*58b9f456SAndroid Build Coastguard Worker    __parent = static_cast<__parent_pointer>(__end_node());
1972*58b9f456SAndroid Build Coastguard Worker    return __parent->__left_;
1973*58b9f456SAndroid Build Coastguard Worker}
1974*58b9f456SAndroid Build Coastguard Worker
1975*58b9f456SAndroid Build Coastguard Worker// Find leaf place to insert closest to __hint
1976*58b9f456SAndroid Build Coastguard Worker// First check prior to __hint.
1977*58b9f456SAndroid Build Coastguard Worker// Next check after __hint.
1978*58b9f456SAndroid Build Coastguard Worker// Next do O(log N) search.
1979*58b9f456SAndroid Build Coastguard Worker// Set __parent to parent of null leaf
1980*58b9f456SAndroid Build Coastguard Worker// Return reference to null leaf
1981*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
1982*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1983*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
1984*58b9f456SAndroid Build Coastguard Worker                                               __parent_pointer& __parent,
1985*58b9f456SAndroid Build Coastguard Worker                                               const key_type& __v)
1986*58b9f456SAndroid Build Coastguard Worker{
1987*58b9f456SAndroid Build Coastguard Worker    if (__hint == end() || !value_comp()(*__hint, __v))  // check before
1988*58b9f456SAndroid Build Coastguard Worker    {
1989*58b9f456SAndroid Build Coastguard Worker        // __v <= *__hint
1990*58b9f456SAndroid Build Coastguard Worker        const_iterator __prior = __hint;
1991*58b9f456SAndroid Build Coastguard Worker        if (__prior == begin() || !value_comp()(__v, *--__prior))
1992*58b9f456SAndroid Build Coastguard Worker        {
1993*58b9f456SAndroid Build Coastguard Worker            // *prev(__hint) <= __v <= *__hint
1994*58b9f456SAndroid Build Coastguard Worker            if (__hint.__ptr_->__left_ == nullptr)
1995*58b9f456SAndroid Build Coastguard Worker            {
1996*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__hint.__ptr_);
1997*58b9f456SAndroid Build Coastguard Worker                return __parent->__left_;
1998*58b9f456SAndroid Build Coastguard Worker            }
1999*58b9f456SAndroid Build Coastguard Worker            else
2000*58b9f456SAndroid Build Coastguard Worker            {
2001*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2002*58b9f456SAndroid Build Coastguard Worker                return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
2003*58b9f456SAndroid Build Coastguard Worker            }
2004*58b9f456SAndroid Build Coastguard Worker        }
2005*58b9f456SAndroid Build Coastguard Worker        // __v < *prev(__hint)
2006*58b9f456SAndroid Build Coastguard Worker        return __find_leaf_high(__parent, __v);
2007*58b9f456SAndroid Build Coastguard Worker    }
2008*58b9f456SAndroid Build Coastguard Worker    // else __v > *__hint
2009*58b9f456SAndroid Build Coastguard Worker    return __find_leaf_low(__parent, __v);
2010*58b9f456SAndroid Build Coastguard Worker}
2011*58b9f456SAndroid Build Coastguard Worker
2012*58b9f456SAndroid Build Coastguard Worker// Find place to insert if __v doesn't exist
2013*58b9f456SAndroid Build Coastguard Worker// Set __parent to parent of null leaf
2014*58b9f456SAndroid Build Coastguard Worker// Return reference to null leaf
2015*58b9f456SAndroid Build Coastguard Worker// If __v exists, set parent to node of __v and return reference to node of __v
2016*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2017*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2018*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
2019*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
2020*58b9f456SAndroid Build Coastguard Worker                                                const _Key& __v)
2021*58b9f456SAndroid Build Coastguard Worker{
2022*58b9f456SAndroid Build Coastguard Worker    __node_pointer __nd = __root();
2023*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer* __nd_ptr = __root_ptr();
2024*58b9f456SAndroid Build Coastguard Worker    if (__nd != nullptr)
2025*58b9f456SAndroid Build Coastguard Worker    {
2026*58b9f456SAndroid Build Coastguard Worker        while (true)
2027*58b9f456SAndroid Build Coastguard Worker        {
2028*58b9f456SAndroid Build Coastguard Worker            if (value_comp()(__v, __nd->__value_))
2029*58b9f456SAndroid Build Coastguard Worker            {
2030*58b9f456SAndroid Build Coastguard Worker                if (__nd->__left_ != nullptr) {
2031*58b9f456SAndroid Build Coastguard Worker                    __nd_ptr = _VSTD::addressof(__nd->__left_);
2032*58b9f456SAndroid Build Coastguard Worker                    __nd = static_cast<__node_pointer>(__nd->__left_);
2033*58b9f456SAndroid Build Coastguard Worker                } else {
2034*58b9f456SAndroid Build Coastguard Worker                    __parent = static_cast<__parent_pointer>(__nd);
2035*58b9f456SAndroid Build Coastguard Worker                    return __parent->__left_;
2036*58b9f456SAndroid Build Coastguard Worker                }
2037*58b9f456SAndroid Build Coastguard Worker            }
2038*58b9f456SAndroid Build Coastguard Worker            else if (value_comp()(__nd->__value_, __v))
2039*58b9f456SAndroid Build Coastguard Worker            {
2040*58b9f456SAndroid Build Coastguard Worker                if (__nd->__right_ != nullptr) {
2041*58b9f456SAndroid Build Coastguard Worker                    __nd_ptr = _VSTD::addressof(__nd->__right_);
2042*58b9f456SAndroid Build Coastguard Worker                    __nd = static_cast<__node_pointer>(__nd->__right_);
2043*58b9f456SAndroid Build Coastguard Worker                } else {
2044*58b9f456SAndroid Build Coastguard Worker                    __parent = static_cast<__parent_pointer>(__nd);
2045*58b9f456SAndroid Build Coastguard Worker                    return __nd->__right_;
2046*58b9f456SAndroid Build Coastguard Worker                }
2047*58b9f456SAndroid Build Coastguard Worker            }
2048*58b9f456SAndroid Build Coastguard Worker            else
2049*58b9f456SAndroid Build Coastguard Worker            {
2050*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__nd);
2051*58b9f456SAndroid Build Coastguard Worker                return *__nd_ptr;
2052*58b9f456SAndroid Build Coastguard Worker            }
2053*58b9f456SAndroid Build Coastguard Worker        }
2054*58b9f456SAndroid Build Coastguard Worker    }
2055*58b9f456SAndroid Build Coastguard Worker    __parent = static_cast<__parent_pointer>(__end_node());
2056*58b9f456SAndroid Build Coastguard Worker    return __parent->__left_;
2057*58b9f456SAndroid Build Coastguard Worker}
2058*58b9f456SAndroid Build Coastguard Worker
2059*58b9f456SAndroid Build Coastguard Worker// Find place to insert if __v doesn't exist
2060*58b9f456SAndroid Build Coastguard Worker// First check prior to __hint.
2061*58b9f456SAndroid Build Coastguard Worker// Next check after __hint.
2062*58b9f456SAndroid Build Coastguard Worker// Next do O(log N) search.
2063*58b9f456SAndroid Build Coastguard Worker// Set __parent to parent of null leaf
2064*58b9f456SAndroid Build Coastguard Worker// Return reference to null leaf
2065*58b9f456SAndroid Build Coastguard Worker// If __v exists, set parent to node of __v and return reference to node of __v
2066*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2067*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2068*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
2069*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
2070*58b9f456SAndroid Build Coastguard Worker                                                __parent_pointer& __parent,
2071*58b9f456SAndroid Build Coastguard Worker                                                __node_base_pointer& __dummy,
2072*58b9f456SAndroid Build Coastguard Worker                                                const _Key& __v)
2073*58b9f456SAndroid Build Coastguard Worker{
2074*58b9f456SAndroid Build Coastguard Worker    if (__hint == end() || value_comp()(__v, *__hint))  // check before
2075*58b9f456SAndroid Build Coastguard Worker    {
2076*58b9f456SAndroid Build Coastguard Worker        // __v < *__hint
2077*58b9f456SAndroid Build Coastguard Worker        const_iterator __prior = __hint;
2078*58b9f456SAndroid Build Coastguard Worker        if (__prior == begin() || value_comp()(*--__prior, __v))
2079*58b9f456SAndroid Build Coastguard Worker        {
2080*58b9f456SAndroid Build Coastguard Worker            // *prev(__hint) < __v < *__hint
2081*58b9f456SAndroid Build Coastguard Worker            if (__hint.__ptr_->__left_ == nullptr)
2082*58b9f456SAndroid Build Coastguard Worker            {
2083*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2084*58b9f456SAndroid Build Coastguard Worker                return __parent->__left_;
2085*58b9f456SAndroid Build Coastguard Worker            }
2086*58b9f456SAndroid Build Coastguard Worker            else
2087*58b9f456SAndroid Build Coastguard Worker            {
2088*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2089*58b9f456SAndroid Build Coastguard Worker                return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
2090*58b9f456SAndroid Build Coastguard Worker            }
2091*58b9f456SAndroid Build Coastguard Worker        }
2092*58b9f456SAndroid Build Coastguard Worker        // __v <= *prev(__hint)
2093*58b9f456SAndroid Build Coastguard Worker        return __find_equal(__parent, __v);
2094*58b9f456SAndroid Build Coastguard Worker    }
2095*58b9f456SAndroid Build Coastguard Worker    else if (value_comp()(*__hint, __v))  // check after
2096*58b9f456SAndroid Build Coastguard Worker    {
2097*58b9f456SAndroid Build Coastguard Worker        // *__hint < __v
2098*58b9f456SAndroid Build Coastguard Worker        const_iterator __next = _VSTD::next(__hint);
2099*58b9f456SAndroid Build Coastguard Worker        if (__next == end() || value_comp()(__v, *__next))
2100*58b9f456SAndroid Build Coastguard Worker        {
2101*58b9f456SAndroid Build Coastguard Worker            // *__hint < __v < *_VSTD::next(__hint)
2102*58b9f456SAndroid Build Coastguard Worker            if (__hint.__get_np()->__right_ == nullptr)
2103*58b9f456SAndroid Build Coastguard Worker            {
2104*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2105*58b9f456SAndroid Build Coastguard Worker                return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
2106*58b9f456SAndroid Build Coastguard Worker            }
2107*58b9f456SAndroid Build Coastguard Worker            else
2108*58b9f456SAndroid Build Coastguard Worker            {
2109*58b9f456SAndroid Build Coastguard Worker                __parent = static_cast<__parent_pointer>(__next.__ptr_);
2110*58b9f456SAndroid Build Coastguard Worker                return __parent->__left_;
2111*58b9f456SAndroid Build Coastguard Worker            }
2112*58b9f456SAndroid Build Coastguard Worker        }
2113*58b9f456SAndroid Build Coastguard Worker        // *next(__hint) <= __v
2114*58b9f456SAndroid Build Coastguard Worker        return __find_equal(__parent, __v);
2115*58b9f456SAndroid Build Coastguard Worker    }
2116*58b9f456SAndroid Build Coastguard Worker    // else __v == *__hint
2117*58b9f456SAndroid Build Coastguard Worker    __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2118*58b9f456SAndroid Build Coastguard Worker    __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2119*58b9f456SAndroid Build Coastguard Worker    return __dummy;
2120*58b9f456SAndroid Build Coastguard Worker}
2121*58b9f456SAndroid Build Coastguard Worker
2122*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2123*58b9f456SAndroid Build Coastguard Workervoid __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
2124*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent, __node_base_pointer& __child,
2125*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer __new_node) _NOEXCEPT
2126*58b9f456SAndroid Build Coastguard Worker{
2127*58b9f456SAndroid Build Coastguard Worker    __new_node->__left_   = nullptr;
2128*58b9f456SAndroid Build Coastguard Worker    __new_node->__right_  = nullptr;
2129*58b9f456SAndroid Build Coastguard Worker    __new_node->__parent_ = __parent;
2130*58b9f456SAndroid Build Coastguard Worker    // __new_node->__is_black_ is initialized in __tree_balance_after_insert
2131*58b9f456SAndroid Build Coastguard Worker    __child = __new_node;
2132*58b9f456SAndroid Build Coastguard Worker    if (__begin_node()->__left_ != nullptr)
2133*58b9f456SAndroid Build Coastguard Worker        __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
2134*58b9f456SAndroid Build Coastguard Worker    __tree_balance_after_insert(__end_node()->__left_, __child);
2135*58b9f456SAndroid Build Coastguard Worker    ++size();
2136*58b9f456SAndroid Build Coastguard Worker}
2137*58b9f456SAndroid Build Coastguard Worker
2138*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
2139*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2140*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class... _Args>
2141*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2142*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2143*58b9f456SAndroid Build Coastguard Worker#else
2144*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2145*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class _Args>
2146*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2147*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2148*58b9f456SAndroid Build Coastguard Worker#endif
2149*58b9f456SAndroid Build Coastguard Worker{
2150*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2151*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__parent, __k);
2152*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2153*58b9f456SAndroid Build Coastguard Worker    bool __inserted = false;
2154*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2155*58b9f456SAndroid Build Coastguard Worker    {
2156*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
2157*58b9f456SAndroid Build Coastguard Worker        __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2158*58b9f456SAndroid Build Coastguard Worker#else
2159*58b9f456SAndroid Build Coastguard Worker        __node_holder __h = __construct_node(__args);
2160*58b9f456SAndroid Build Coastguard Worker#endif
2161*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2162*58b9f456SAndroid Build Coastguard Worker        __r = __h.release();
2163*58b9f456SAndroid Build Coastguard Worker        __inserted = true;
2164*58b9f456SAndroid Build Coastguard Worker    }
2165*58b9f456SAndroid Build Coastguard Worker    return pair<iterator, bool>(iterator(__r), __inserted);
2166*58b9f456SAndroid Build Coastguard Worker}
2167*58b9f456SAndroid Build Coastguard Worker
2168*58b9f456SAndroid Build Coastguard Worker
2169*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
2170*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2171*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class... _Args>
2172*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2173*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2174*58b9f456SAndroid Build Coastguard Worker    const_iterator __p, _Key const& __k, _Args&&... __args)
2175*58b9f456SAndroid Build Coastguard Worker#else
2176*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2177*58b9f456SAndroid Build Coastguard Workertemplate <class _Key, class _Args>
2178*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2179*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2180*58b9f456SAndroid Build Coastguard Worker    const_iterator __p, _Key const& __k, _Args& __args)
2181*58b9f456SAndroid Build Coastguard Worker#endif
2182*58b9f456SAndroid Build Coastguard Worker{
2183*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2184*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer __dummy;
2185*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
2186*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2187*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2188*58b9f456SAndroid Build Coastguard Worker    {
2189*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
2190*58b9f456SAndroid Build Coastguard Worker        __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2191*58b9f456SAndroid Build Coastguard Worker#else
2192*58b9f456SAndroid Build Coastguard Worker        __node_holder __h = __construct_node(__args);
2193*58b9f456SAndroid Build Coastguard Worker#endif
2194*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2195*58b9f456SAndroid Build Coastguard Worker        __r = __h.release();
2196*58b9f456SAndroid Build Coastguard Worker    }
2197*58b9f456SAndroid Build Coastguard Worker    return iterator(__r);
2198*58b9f456SAndroid Build Coastguard Worker}
2199*58b9f456SAndroid Build Coastguard Worker
2200*58b9f456SAndroid Build Coastguard Worker
2201*58b9f456SAndroid Build Coastguard Worker#ifndef _LIBCPP_CXX03_LANG
2202*58b9f456SAndroid Build Coastguard Worker
2203*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2204*58b9f456SAndroid Build Coastguard Workertemplate <class ..._Args>
2205*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_holder
2206*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2207*58b9f456SAndroid Build Coastguard Worker{
2208*58b9f456SAndroid Build Coastguard Worker    static_assert(!__is_tree_value_type<_Args...>::value,
2209*58b9f456SAndroid Build Coastguard Worker                  "Cannot construct from __value_type");
2210*58b9f456SAndroid Build Coastguard Worker    __node_allocator& __na = __node_alloc();
2211*58b9f456SAndroid Build Coastguard Worker    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2212*58b9f456SAndroid Build Coastguard Worker    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
2213*58b9f456SAndroid Build Coastguard Worker    __h.get_deleter().__value_constructed = true;
2214*58b9f456SAndroid Build Coastguard Worker    return __h;
2215*58b9f456SAndroid Build Coastguard Worker}
2216*58b9f456SAndroid Build Coastguard Worker
2217*58b9f456SAndroid Build Coastguard Worker
2218*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2219*58b9f456SAndroid Build Coastguard Workertemplate <class... _Args>
2220*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2221*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
2222*58b9f456SAndroid Build Coastguard Worker{
2223*58b9f456SAndroid Build Coastguard Worker    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2224*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2225*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2226*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2227*58b9f456SAndroid Build Coastguard Worker    bool __inserted = false;
2228*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2229*58b9f456SAndroid Build Coastguard Worker    {
2230*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2231*58b9f456SAndroid Build Coastguard Worker        __r = __h.release();
2232*58b9f456SAndroid Build Coastguard Worker        __inserted = true;
2233*58b9f456SAndroid Build Coastguard Worker    }
2234*58b9f456SAndroid Build Coastguard Worker    return pair<iterator, bool>(iterator(__r), __inserted);
2235*58b9f456SAndroid Build Coastguard Worker}
2236*58b9f456SAndroid Build Coastguard Worker
2237*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2238*58b9f456SAndroid Build Coastguard Workertemplate <class... _Args>
2239*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2240*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
2241*58b9f456SAndroid Build Coastguard Worker{
2242*58b9f456SAndroid Build Coastguard Worker    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2243*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2244*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer __dummy;
2245*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
2246*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2247*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2248*58b9f456SAndroid Build Coastguard Worker    {
2249*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2250*58b9f456SAndroid Build Coastguard Worker        __r = __h.release();
2251*58b9f456SAndroid Build Coastguard Worker    }
2252*58b9f456SAndroid Build Coastguard Worker    return iterator(__r);
2253*58b9f456SAndroid Build Coastguard Worker}
2254*58b9f456SAndroid Build Coastguard Worker
2255*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2256*58b9f456SAndroid Build Coastguard Workertemplate <class... _Args>
2257*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2258*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2259*58b9f456SAndroid Build Coastguard Worker{
2260*58b9f456SAndroid Build Coastguard Worker    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2261*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2262*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
2263*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2264*58b9f456SAndroid Build Coastguard Worker    return iterator(static_cast<__node_pointer>(__h.release()));
2265*58b9f456SAndroid Build Coastguard Worker}
2266*58b9f456SAndroid Build Coastguard Worker
2267*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2268*58b9f456SAndroid Build Coastguard Workertemplate <class... _Args>
2269*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2270*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2271*58b9f456SAndroid Build Coastguard Worker                                                        _Args&&... __args)
2272*58b9f456SAndroid Build Coastguard Worker{
2273*58b9f456SAndroid Build Coastguard Worker    __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2274*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2275*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
2276*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2277*58b9f456SAndroid Build Coastguard Worker    return iterator(static_cast<__node_pointer>(__h.release()));
2278*58b9f456SAndroid Build Coastguard Worker}
2279*58b9f456SAndroid Build Coastguard Worker
2280*58b9f456SAndroid Build Coastguard Worker
2281*58b9f456SAndroid Build Coastguard Worker#else  // _LIBCPP_CXX03_LANG
2282*58b9f456SAndroid Build Coastguard Worker
2283*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2284*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_holder
2285*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
2286*58b9f456SAndroid Build Coastguard Worker{
2287*58b9f456SAndroid Build Coastguard Worker    __node_allocator& __na = __node_alloc();
2288*58b9f456SAndroid Build Coastguard Worker    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2289*58b9f456SAndroid Build Coastguard Worker    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
2290*58b9f456SAndroid Build Coastguard Worker    __h.get_deleter().__value_constructed = true;
2291*58b9f456SAndroid Build Coastguard Worker    return _LIBCPP_EXPLICIT_MOVE(__h);  // explicitly moved for C++03
2292*58b9f456SAndroid Build Coastguard Worker}
2293*58b9f456SAndroid Build Coastguard Worker
2294*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_CXX03_LANG
2295*58b9f456SAndroid Build Coastguard Worker
2296*58b9f456SAndroid Build Coastguard Worker#ifdef _LIBCPP_CXX03_LANG
2297*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2298*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2299*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
2300*58b9f456SAndroid Build Coastguard Worker{
2301*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2302*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
2303*58b9f456SAndroid Build Coastguard Worker    __node_holder __h = __construct_node(__v);
2304*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2305*58b9f456SAndroid Build Coastguard Worker    return iterator(__h.release());
2306*58b9f456SAndroid Build Coastguard Worker}
2307*58b9f456SAndroid Build Coastguard Worker
2308*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2309*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2310*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
2311*58b9f456SAndroid Build Coastguard Worker{
2312*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2313*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
2314*58b9f456SAndroid Build Coastguard Worker    __node_holder __h = __construct_node(__v);
2315*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2316*58b9f456SAndroid Build Coastguard Worker    return iterator(__h.release());
2317*58b9f456SAndroid Build Coastguard Worker}
2318*58b9f456SAndroid Build Coastguard Worker#endif
2319*58b9f456SAndroid Build Coastguard Worker
2320*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2321*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2322*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2323*58b9f456SAndroid Build Coastguard Worker{
2324*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2325*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2326*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2327*58b9f456SAndroid Build Coastguard Worker    bool __inserted = false;
2328*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2329*58b9f456SAndroid Build Coastguard Worker    {
2330*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2331*58b9f456SAndroid Build Coastguard Worker        __r = __nd;
2332*58b9f456SAndroid Build Coastguard Worker        __inserted = true;
2333*58b9f456SAndroid Build Coastguard Worker    }
2334*58b9f456SAndroid Build Coastguard Worker    return pair<iterator, bool>(iterator(__r), __inserted);
2335*58b9f456SAndroid Build Coastguard Worker}
2336*58b9f456SAndroid Build Coastguard Worker
2337*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2338*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2339*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2340*58b9f456SAndroid Build Coastguard Worker                                                        __node_pointer __nd)
2341*58b9f456SAndroid Build Coastguard Worker{
2342*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2343*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer __dummy;
2344*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2345*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2346*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2347*58b9f456SAndroid Build Coastguard Worker    {
2348*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2349*58b9f456SAndroid Build Coastguard Worker        __r = __nd;
2350*58b9f456SAndroid Build Coastguard Worker    }
2351*58b9f456SAndroid Build Coastguard Worker    return iterator(__r);
2352*58b9f456SAndroid Build Coastguard Worker}
2353*58b9f456SAndroid Build Coastguard Worker
2354*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2355*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2356*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2357*58b9f456SAndroid Build Coastguard Worker{
2358*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2359*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
2360*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2361*58b9f456SAndroid Build Coastguard Worker    return iterator(__nd);
2362*58b9f456SAndroid Build Coastguard Worker}
2363*58b9f456SAndroid Build Coastguard Worker
2364*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2365*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2366*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2367*58b9f456SAndroid Build Coastguard Worker                                                       __node_pointer __nd)
2368*58b9f456SAndroid Build Coastguard Worker{
2369*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2370*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
2371*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2372*58b9f456SAndroid Build Coastguard Worker    return iterator(__nd);
2373*58b9f456SAndroid Build Coastguard Worker}
2374*58b9f456SAndroid Build Coastguard Worker
2375*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2376*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2377*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT
2378*58b9f456SAndroid Build Coastguard Worker{
2379*58b9f456SAndroid Build Coastguard Worker    iterator __r(__ptr);
2380*58b9f456SAndroid Build Coastguard Worker    ++__r;
2381*58b9f456SAndroid Build Coastguard Worker    if (__begin_node() == __ptr)
2382*58b9f456SAndroid Build Coastguard Worker        __begin_node() = __r.__ptr_;
2383*58b9f456SAndroid Build Coastguard Worker    --size();
2384*58b9f456SAndroid Build Coastguard Worker    __tree_remove(__end_node()->__left_,
2385*58b9f456SAndroid Build Coastguard Worker                  static_cast<__node_base_pointer>(__ptr));
2386*58b9f456SAndroid Build Coastguard Worker    return __r;
2387*58b9f456SAndroid Build Coastguard Worker}
2388*58b9f456SAndroid Build Coastguard Worker
2389*58b9f456SAndroid Build Coastguard Worker#if _LIBCPP_STD_VER > 14
2390*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2391*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeHandle, class _InsertReturnType>
2392*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2393*58b9f456SAndroid Build Coastguard Worker_InsertReturnType
2394*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2395*58b9f456SAndroid Build Coastguard Worker    _NodeHandle&& __nh)
2396*58b9f456SAndroid Build Coastguard Worker{
2397*58b9f456SAndroid Build Coastguard Worker    if (__nh.empty())
2398*58b9f456SAndroid Build Coastguard Worker        return _InsertReturnType{end(), false, _NodeHandle()};
2399*58b9f456SAndroid Build Coastguard Worker
2400*58b9f456SAndroid Build Coastguard Worker    __node_pointer __ptr = __nh.__ptr_;
2401*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2402*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__parent,
2403*58b9f456SAndroid Build Coastguard Worker                                                __ptr->__value_);
2404*58b9f456SAndroid Build Coastguard Worker    if (__child != nullptr)
2405*58b9f456SAndroid Build Coastguard Worker        return _InsertReturnType{
2406*58b9f456SAndroid Build Coastguard Worker            iterator(static_cast<__node_pointer>(__child)),
2407*58b9f456SAndroid Build Coastguard Worker            false, _VSTD::move(__nh)};
2408*58b9f456SAndroid Build Coastguard Worker
2409*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child,
2410*58b9f456SAndroid Build Coastguard Worker                     static_cast<__node_base_pointer>(__ptr));
2411*58b9f456SAndroid Build Coastguard Worker    __nh.__release();
2412*58b9f456SAndroid Build Coastguard Worker    return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
2413*58b9f456SAndroid Build Coastguard Worker}
2414*58b9f456SAndroid Build Coastguard Worker
2415*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2416*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeHandle>
2417*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2418*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2419*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2420*58b9f456SAndroid Build Coastguard Worker    const_iterator __hint, _NodeHandle&& __nh)
2421*58b9f456SAndroid Build Coastguard Worker{
2422*58b9f456SAndroid Build Coastguard Worker    if (__nh.empty())
2423*58b9f456SAndroid Build Coastguard Worker        return end();
2424*58b9f456SAndroid Build Coastguard Worker
2425*58b9f456SAndroid Build Coastguard Worker    __node_pointer __ptr = __nh.__ptr_;
2426*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2427*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer __dummy;
2428*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy,
2429*58b9f456SAndroid Build Coastguard Worker                                                __ptr->__value_);
2430*58b9f456SAndroid Build Coastguard Worker    __node_pointer __r = static_cast<__node_pointer>(__child);
2431*58b9f456SAndroid Build Coastguard Worker    if (__child == nullptr)
2432*58b9f456SAndroid Build Coastguard Worker    {
2433*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child,
2434*58b9f456SAndroid Build Coastguard Worker                         static_cast<__node_base_pointer>(__ptr));
2435*58b9f456SAndroid Build Coastguard Worker        __r = __ptr;
2436*58b9f456SAndroid Build Coastguard Worker        __nh.__release();
2437*58b9f456SAndroid Build Coastguard Worker    }
2438*58b9f456SAndroid Build Coastguard Worker    return iterator(__r);
2439*58b9f456SAndroid Build Coastguard Worker}
2440*58b9f456SAndroid Build Coastguard Worker
2441*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2442*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeHandle>
2443*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2444*58b9f456SAndroid Build Coastguard Worker_NodeHandle
2445*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key)
2446*58b9f456SAndroid Build Coastguard Worker{
2447*58b9f456SAndroid Build Coastguard Worker    iterator __it = find(__key);
2448*58b9f456SAndroid Build Coastguard Worker    if (__it == end())
2449*58b9f456SAndroid Build Coastguard Worker        return _NodeHandle();
2450*58b9f456SAndroid Build Coastguard Worker    return __node_handle_extract<_NodeHandle>(__it);
2451*58b9f456SAndroid Build Coastguard Worker}
2452*58b9f456SAndroid Build Coastguard Worker
2453*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2454*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeHandle>
2455*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2456*58b9f456SAndroid Build Coastguard Worker_NodeHandle
2457*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p)
2458*58b9f456SAndroid Build Coastguard Worker{
2459*58b9f456SAndroid Build Coastguard Worker    __node_pointer __np = __p.__get_np();
2460*58b9f456SAndroid Build Coastguard Worker    __remove_node_pointer(__np);
2461*58b9f456SAndroid Build Coastguard Worker    return _NodeHandle(__np, __alloc());
2462*58b9f456SAndroid Build Coastguard Worker}
2463*58b9f456SAndroid Build Coastguard Worker
2464*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2465*58b9f456SAndroid Build Coastguard Workertemplate <class _Tree>
2466*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2467*58b9f456SAndroid Build Coastguard Workervoid
2468*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(_Tree& __source)
2469*58b9f456SAndroid Build Coastguard Worker{
2470*58b9f456SAndroid Build Coastguard Worker    static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2471*58b9f456SAndroid Build Coastguard Worker
2472*58b9f456SAndroid Build Coastguard Worker    for (typename _Tree::iterator __i = __source.begin();
2473*58b9f456SAndroid Build Coastguard Worker         __i != __source.end();)
2474*58b9f456SAndroid Build Coastguard Worker    {
2475*58b9f456SAndroid Build Coastguard Worker        __node_pointer __src_ptr = __i.__get_np();
2476*58b9f456SAndroid Build Coastguard Worker        __parent_pointer __parent;
2477*58b9f456SAndroid Build Coastguard Worker        __node_base_pointer& __child =
2478*58b9f456SAndroid Build Coastguard Worker            __find_equal(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
2479*58b9f456SAndroid Build Coastguard Worker        ++__i;
2480*58b9f456SAndroid Build Coastguard Worker        if (__child != nullptr)
2481*58b9f456SAndroid Build Coastguard Worker            continue;
2482*58b9f456SAndroid Build Coastguard Worker        __source.__remove_node_pointer(__src_ptr);
2483*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child,
2484*58b9f456SAndroid Build Coastguard Worker                         static_cast<__node_base_pointer>(__src_ptr));
2485*58b9f456SAndroid Build Coastguard Worker    }
2486*58b9f456SAndroid Build Coastguard Worker}
2487*58b9f456SAndroid Build Coastguard Worker
2488*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2489*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeHandle>
2490*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2491*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2492*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh)
2493*58b9f456SAndroid Build Coastguard Worker{
2494*58b9f456SAndroid Build Coastguard Worker    if (__nh.empty())
2495*58b9f456SAndroid Build Coastguard Worker        return end();
2496*58b9f456SAndroid Build Coastguard Worker    __node_pointer __ptr = __nh.__ptr_;
2497*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2498*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf_high(
2499*58b9f456SAndroid Build Coastguard Worker        __parent, _NodeTypes::__get_key(__ptr->__value_));
2500*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2501*58b9f456SAndroid Build Coastguard Worker    __nh.__release();
2502*58b9f456SAndroid Build Coastguard Worker    return iterator(__ptr);
2503*58b9f456SAndroid Build Coastguard Worker}
2504*58b9f456SAndroid Build Coastguard Worker
2505*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2506*58b9f456SAndroid Build Coastguard Workertemplate <class _NodeHandle>
2507*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2508*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2509*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(
2510*58b9f456SAndroid Build Coastguard Worker    const_iterator __hint, _NodeHandle&& __nh)
2511*58b9f456SAndroid Build Coastguard Worker{
2512*58b9f456SAndroid Build Coastguard Worker    if (__nh.empty())
2513*58b9f456SAndroid Build Coastguard Worker        return end();
2514*58b9f456SAndroid Build Coastguard Worker
2515*58b9f456SAndroid Build Coastguard Worker    __node_pointer __ptr = __nh.__ptr_;
2516*58b9f456SAndroid Build Coastguard Worker    __parent_pointer __parent;
2517*58b9f456SAndroid Build Coastguard Worker    __node_base_pointer& __child = __find_leaf(__hint, __parent,
2518*58b9f456SAndroid Build Coastguard Worker                                               _NodeTypes::__get_key(__ptr->__value_));
2519*58b9f456SAndroid Build Coastguard Worker    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2520*58b9f456SAndroid Build Coastguard Worker    __nh.__release();
2521*58b9f456SAndroid Build Coastguard Worker    return iterator(__ptr);
2522*58b9f456SAndroid Build Coastguard Worker}
2523*58b9f456SAndroid Build Coastguard Worker
2524*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2525*58b9f456SAndroid Build Coastguard Workertemplate <class _Tree>
2526*58b9f456SAndroid Build Coastguard Worker_LIBCPP_INLINE_VISIBILITY
2527*58b9f456SAndroid Build Coastguard Workervoid
2528*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(_Tree& __source)
2529*58b9f456SAndroid Build Coastguard Worker{
2530*58b9f456SAndroid Build Coastguard Worker    static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2531*58b9f456SAndroid Build Coastguard Worker
2532*58b9f456SAndroid Build Coastguard Worker    for (typename _Tree::iterator __i = __source.begin();
2533*58b9f456SAndroid Build Coastguard Worker         __i != __source.end();)
2534*58b9f456SAndroid Build Coastguard Worker    {
2535*58b9f456SAndroid Build Coastguard Worker        __node_pointer __src_ptr = __i.__get_np();
2536*58b9f456SAndroid Build Coastguard Worker        __parent_pointer __parent;
2537*58b9f456SAndroid Build Coastguard Worker        __node_base_pointer& __child = __find_leaf_high(
2538*58b9f456SAndroid Build Coastguard Worker            __parent, _NodeTypes::__get_key(__src_ptr->__value_));
2539*58b9f456SAndroid Build Coastguard Worker        ++__i;
2540*58b9f456SAndroid Build Coastguard Worker        __source.__remove_node_pointer(__src_ptr);
2541*58b9f456SAndroid Build Coastguard Worker        __insert_node_at(__parent, __child,
2542*58b9f456SAndroid Build Coastguard Worker                         static_cast<__node_base_pointer>(__src_ptr));
2543*58b9f456SAndroid Build Coastguard Worker    }
2544*58b9f456SAndroid Build Coastguard Worker}
2545*58b9f456SAndroid Build Coastguard Worker
2546*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP_STD_VER > 14
2547*58b9f456SAndroid Build Coastguard Worker
2548*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2549*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2550*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2551*58b9f456SAndroid Build Coastguard Worker{
2552*58b9f456SAndroid Build Coastguard Worker    __node_pointer __np = __p.__get_np();
2553*58b9f456SAndroid Build Coastguard Worker    iterator __r = __remove_node_pointer(__np);
2554*58b9f456SAndroid Build Coastguard Worker    __node_allocator& __na = __node_alloc();
2555*58b9f456SAndroid Build Coastguard Worker    __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2556*58b9f456SAndroid Build Coastguard Worker        const_cast<__node_value_type&>(*__p)));
2557*58b9f456SAndroid Build Coastguard Worker    __node_traits::deallocate(__na, __np, 1);
2558*58b9f456SAndroid Build Coastguard Worker    return __r;
2559*58b9f456SAndroid Build Coastguard Worker}
2560*58b9f456SAndroid Build Coastguard Worker
2561*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2562*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2563*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2564*58b9f456SAndroid Build Coastguard Worker{
2565*58b9f456SAndroid Build Coastguard Worker    while (__f != __l)
2566*58b9f456SAndroid Build Coastguard Worker        __f = erase(__f);
2567*58b9f456SAndroid Build Coastguard Worker    return iterator(__l.__ptr_);
2568*58b9f456SAndroid Build Coastguard Worker}
2569*58b9f456SAndroid Build Coastguard Worker
2570*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2571*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2572*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::size_type
2573*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2574*58b9f456SAndroid Build Coastguard Worker{
2575*58b9f456SAndroid Build Coastguard Worker    iterator __i = find(__k);
2576*58b9f456SAndroid Build Coastguard Worker    if (__i == end())
2577*58b9f456SAndroid Build Coastguard Worker        return 0;
2578*58b9f456SAndroid Build Coastguard Worker    erase(__i);
2579*58b9f456SAndroid Build Coastguard Worker    return 1;
2580*58b9f456SAndroid Build Coastguard Worker}
2581*58b9f456SAndroid Build Coastguard Worker
2582*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2583*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2584*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::size_type
2585*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2586*58b9f456SAndroid Build Coastguard Worker{
2587*58b9f456SAndroid Build Coastguard Worker    pair<iterator, iterator> __p = __equal_range_multi(__k);
2588*58b9f456SAndroid Build Coastguard Worker    size_type __r = 0;
2589*58b9f456SAndroid Build Coastguard Worker    for (; __p.first != __p.second; ++__r)
2590*58b9f456SAndroid Build Coastguard Worker        __p.first = erase(__p.first);
2591*58b9f456SAndroid Build Coastguard Worker    return __r;
2592*58b9f456SAndroid Build Coastguard Worker}
2593*58b9f456SAndroid Build Coastguard Worker
2594*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2595*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2596*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2597*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2598*58b9f456SAndroid Build Coastguard Worker{
2599*58b9f456SAndroid Build Coastguard Worker    iterator __p = __lower_bound(__v, __root(), __end_node());
2600*58b9f456SAndroid Build Coastguard Worker    if (__p != end() && !value_comp()(__v, *__p))
2601*58b9f456SAndroid Build Coastguard Worker        return __p;
2602*58b9f456SAndroid Build Coastguard Worker    return end();
2603*58b9f456SAndroid Build Coastguard Worker}
2604*58b9f456SAndroid Build Coastguard Worker
2605*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2606*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2607*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::const_iterator
2608*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2609*58b9f456SAndroid Build Coastguard Worker{
2610*58b9f456SAndroid Build Coastguard Worker    const_iterator __p = __lower_bound(__v, __root(), __end_node());
2611*58b9f456SAndroid Build Coastguard Worker    if (__p != end() && !value_comp()(__v, *__p))
2612*58b9f456SAndroid Build Coastguard Worker        return __p;
2613*58b9f456SAndroid Build Coastguard Worker    return end();
2614*58b9f456SAndroid Build Coastguard Worker}
2615*58b9f456SAndroid Build Coastguard Worker
2616*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2617*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2618*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::size_type
2619*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2620*58b9f456SAndroid Build Coastguard Worker{
2621*58b9f456SAndroid Build Coastguard Worker    __node_pointer __rt = __root();
2622*58b9f456SAndroid Build Coastguard Worker    while (__rt != nullptr)
2623*58b9f456SAndroid Build Coastguard Worker    {
2624*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__k, __rt->__value_))
2625*58b9f456SAndroid Build Coastguard Worker        {
2626*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__left_);
2627*58b9f456SAndroid Build Coastguard Worker        }
2628*58b9f456SAndroid Build Coastguard Worker        else if (value_comp()(__rt->__value_, __k))
2629*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__right_);
2630*58b9f456SAndroid Build Coastguard Worker        else
2631*58b9f456SAndroid Build Coastguard Worker            return 1;
2632*58b9f456SAndroid Build Coastguard Worker    }
2633*58b9f456SAndroid Build Coastguard Worker    return 0;
2634*58b9f456SAndroid Build Coastguard Worker}
2635*58b9f456SAndroid Build Coastguard Worker
2636*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2637*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2638*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::size_type
2639*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2640*58b9f456SAndroid Build Coastguard Worker{
2641*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __result = __end_node();
2642*58b9f456SAndroid Build Coastguard Worker    __node_pointer __rt = __root();
2643*58b9f456SAndroid Build Coastguard Worker    while (__rt != nullptr)
2644*58b9f456SAndroid Build Coastguard Worker    {
2645*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__k, __rt->__value_))
2646*58b9f456SAndroid Build Coastguard Worker        {
2647*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__rt);
2648*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__left_);
2649*58b9f456SAndroid Build Coastguard Worker        }
2650*58b9f456SAndroid Build Coastguard Worker        else if (value_comp()(__rt->__value_, __k))
2651*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__right_);
2652*58b9f456SAndroid Build Coastguard Worker        else
2653*58b9f456SAndroid Build Coastguard Worker            return _VSTD::distance(
2654*58b9f456SAndroid Build Coastguard Worker                __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2655*58b9f456SAndroid Build Coastguard Worker                __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
2656*58b9f456SAndroid Build Coastguard Worker            );
2657*58b9f456SAndroid Build Coastguard Worker    }
2658*58b9f456SAndroid Build Coastguard Worker    return 0;
2659*58b9f456SAndroid Build Coastguard Worker}
2660*58b9f456SAndroid Build Coastguard Worker
2661*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2662*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2663*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2664*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2665*58b9f456SAndroid Build Coastguard Worker                                                 __node_pointer __root,
2666*58b9f456SAndroid Build Coastguard Worker                                                 __iter_pointer __result)
2667*58b9f456SAndroid Build Coastguard Worker{
2668*58b9f456SAndroid Build Coastguard Worker    while (__root != nullptr)
2669*58b9f456SAndroid Build Coastguard Worker    {
2670*58b9f456SAndroid Build Coastguard Worker        if (!value_comp()(__root->__value_, __v))
2671*58b9f456SAndroid Build Coastguard Worker        {
2672*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__root);
2673*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__left_);
2674*58b9f456SAndroid Build Coastguard Worker        }
2675*58b9f456SAndroid Build Coastguard Worker        else
2676*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__right_);
2677*58b9f456SAndroid Build Coastguard Worker    }
2678*58b9f456SAndroid Build Coastguard Worker    return iterator(__result);
2679*58b9f456SAndroid Build Coastguard Worker}
2680*58b9f456SAndroid Build Coastguard Worker
2681*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2682*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2683*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::const_iterator
2684*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2685*58b9f456SAndroid Build Coastguard Worker                                                 __node_pointer __root,
2686*58b9f456SAndroid Build Coastguard Worker                                                 __iter_pointer __result) const
2687*58b9f456SAndroid Build Coastguard Worker{
2688*58b9f456SAndroid Build Coastguard Worker    while (__root != nullptr)
2689*58b9f456SAndroid Build Coastguard Worker    {
2690*58b9f456SAndroid Build Coastguard Worker        if (!value_comp()(__root->__value_, __v))
2691*58b9f456SAndroid Build Coastguard Worker        {
2692*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__root);
2693*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__left_);
2694*58b9f456SAndroid Build Coastguard Worker        }
2695*58b9f456SAndroid Build Coastguard Worker        else
2696*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__right_);
2697*58b9f456SAndroid Build Coastguard Worker    }
2698*58b9f456SAndroid Build Coastguard Worker    return const_iterator(__result);
2699*58b9f456SAndroid Build Coastguard Worker}
2700*58b9f456SAndroid Build Coastguard Worker
2701*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2702*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2703*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::iterator
2704*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2705*58b9f456SAndroid Build Coastguard Worker                                                 __node_pointer __root,
2706*58b9f456SAndroid Build Coastguard Worker                                                 __iter_pointer __result)
2707*58b9f456SAndroid Build Coastguard Worker{
2708*58b9f456SAndroid Build Coastguard Worker    while (__root != nullptr)
2709*58b9f456SAndroid Build Coastguard Worker    {
2710*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__v, __root->__value_))
2711*58b9f456SAndroid Build Coastguard Worker        {
2712*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__root);
2713*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__left_);
2714*58b9f456SAndroid Build Coastguard Worker        }
2715*58b9f456SAndroid Build Coastguard Worker        else
2716*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__right_);
2717*58b9f456SAndroid Build Coastguard Worker    }
2718*58b9f456SAndroid Build Coastguard Worker    return iterator(__result);
2719*58b9f456SAndroid Build Coastguard Worker}
2720*58b9f456SAndroid Build Coastguard Worker
2721*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2722*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2723*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::const_iterator
2724*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2725*58b9f456SAndroid Build Coastguard Worker                                                 __node_pointer __root,
2726*58b9f456SAndroid Build Coastguard Worker                                                 __iter_pointer __result) const
2727*58b9f456SAndroid Build Coastguard Worker{
2728*58b9f456SAndroid Build Coastguard Worker    while (__root != nullptr)
2729*58b9f456SAndroid Build Coastguard Worker    {
2730*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__v, __root->__value_))
2731*58b9f456SAndroid Build Coastguard Worker        {
2732*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__root);
2733*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__left_);
2734*58b9f456SAndroid Build Coastguard Worker        }
2735*58b9f456SAndroid Build Coastguard Worker        else
2736*58b9f456SAndroid Build Coastguard Worker            __root = static_cast<__node_pointer>(__root->__right_);
2737*58b9f456SAndroid Build Coastguard Worker    }
2738*58b9f456SAndroid Build Coastguard Worker    return const_iterator(__result);
2739*58b9f456SAndroid Build Coastguard Worker}
2740*58b9f456SAndroid Build Coastguard Worker
2741*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2742*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2743*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2744*58b9f456SAndroid Build Coastguard Worker     typename __tree<_Tp, _Compare, _Allocator>::iterator>
2745*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2746*58b9f456SAndroid Build Coastguard Worker{
2747*58b9f456SAndroid Build Coastguard Worker    typedef pair<iterator, iterator> _Pp;
2748*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __result = __end_node();
2749*58b9f456SAndroid Build Coastguard Worker    __node_pointer __rt = __root();
2750*58b9f456SAndroid Build Coastguard Worker    while (__rt != nullptr)
2751*58b9f456SAndroid Build Coastguard Worker    {
2752*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__k, __rt->__value_))
2753*58b9f456SAndroid Build Coastguard Worker        {
2754*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__rt);
2755*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__left_);
2756*58b9f456SAndroid Build Coastguard Worker        }
2757*58b9f456SAndroid Build Coastguard Worker        else if (value_comp()(__rt->__value_, __k))
2758*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__right_);
2759*58b9f456SAndroid Build Coastguard Worker        else
2760*58b9f456SAndroid Build Coastguard Worker            return _Pp(iterator(__rt),
2761*58b9f456SAndroid Build Coastguard Worker                      iterator(
2762*58b9f456SAndroid Build Coastguard Worker                          __rt->__right_ != nullptr ?
2763*58b9f456SAndroid Build Coastguard Worker                              static_cast<__iter_pointer>(__tree_min(__rt->__right_))
2764*58b9f456SAndroid Build Coastguard Worker                            : __result));
2765*58b9f456SAndroid Build Coastguard Worker    }
2766*58b9f456SAndroid Build Coastguard Worker    return _Pp(iterator(__result), iterator(__result));
2767*58b9f456SAndroid Build Coastguard Worker}
2768*58b9f456SAndroid Build Coastguard Worker
2769*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2770*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2771*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2772*58b9f456SAndroid Build Coastguard Worker     typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2773*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2774*58b9f456SAndroid Build Coastguard Worker{
2775*58b9f456SAndroid Build Coastguard Worker    typedef pair<const_iterator, const_iterator> _Pp;
2776*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __result = __end_node();
2777*58b9f456SAndroid Build Coastguard Worker    __node_pointer __rt = __root();
2778*58b9f456SAndroid Build Coastguard Worker    while (__rt != nullptr)
2779*58b9f456SAndroid Build Coastguard Worker    {
2780*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__k, __rt->__value_))
2781*58b9f456SAndroid Build Coastguard Worker        {
2782*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__rt);
2783*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__left_);
2784*58b9f456SAndroid Build Coastguard Worker        }
2785*58b9f456SAndroid Build Coastguard Worker        else if (value_comp()(__rt->__value_, __k))
2786*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__right_);
2787*58b9f456SAndroid Build Coastguard Worker        else
2788*58b9f456SAndroid Build Coastguard Worker            return _Pp(const_iterator(__rt),
2789*58b9f456SAndroid Build Coastguard Worker                      const_iterator(
2790*58b9f456SAndroid Build Coastguard Worker                          __rt->__right_ != nullptr ?
2791*58b9f456SAndroid Build Coastguard Worker                              static_cast<__iter_pointer>(__tree_min(__rt->__right_))
2792*58b9f456SAndroid Build Coastguard Worker                            : __result));
2793*58b9f456SAndroid Build Coastguard Worker    }
2794*58b9f456SAndroid Build Coastguard Worker    return _Pp(const_iterator(__result), const_iterator(__result));
2795*58b9f456SAndroid Build Coastguard Worker}
2796*58b9f456SAndroid Build Coastguard Worker
2797*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2798*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2799*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2800*58b9f456SAndroid Build Coastguard Worker     typename __tree<_Tp, _Compare, _Allocator>::iterator>
2801*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2802*58b9f456SAndroid Build Coastguard Worker{
2803*58b9f456SAndroid Build Coastguard Worker    typedef pair<iterator, iterator> _Pp;
2804*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __result = __end_node();
2805*58b9f456SAndroid Build Coastguard Worker    __node_pointer __rt = __root();
2806*58b9f456SAndroid Build Coastguard Worker    while (__rt != nullptr)
2807*58b9f456SAndroid Build Coastguard Worker    {
2808*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__k, __rt->__value_))
2809*58b9f456SAndroid Build Coastguard Worker        {
2810*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__rt);
2811*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__left_);
2812*58b9f456SAndroid Build Coastguard Worker        }
2813*58b9f456SAndroid Build Coastguard Worker        else if (value_comp()(__rt->__value_, __k))
2814*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__right_);
2815*58b9f456SAndroid Build Coastguard Worker        else
2816*58b9f456SAndroid Build Coastguard Worker            return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2817*58b9f456SAndroid Build Coastguard Worker                      __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2818*58b9f456SAndroid Build Coastguard Worker    }
2819*58b9f456SAndroid Build Coastguard Worker    return _Pp(iterator(__result), iterator(__result));
2820*58b9f456SAndroid Build Coastguard Worker}
2821*58b9f456SAndroid Build Coastguard Worker
2822*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2823*58b9f456SAndroid Build Coastguard Workertemplate <class _Key>
2824*58b9f456SAndroid Build Coastguard Workerpair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2825*58b9f456SAndroid Build Coastguard Worker     typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2826*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2827*58b9f456SAndroid Build Coastguard Worker{
2828*58b9f456SAndroid Build Coastguard Worker    typedef pair<const_iterator, const_iterator> _Pp;
2829*58b9f456SAndroid Build Coastguard Worker    __iter_pointer __result = __end_node();
2830*58b9f456SAndroid Build Coastguard Worker    __node_pointer __rt = __root();
2831*58b9f456SAndroid Build Coastguard Worker    while (__rt != nullptr)
2832*58b9f456SAndroid Build Coastguard Worker    {
2833*58b9f456SAndroid Build Coastguard Worker        if (value_comp()(__k, __rt->__value_))
2834*58b9f456SAndroid Build Coastguard Worker        {
2835*58b9f456SAndroid Build Coastguard Worker            __result = static_cast<__iter_pointer>(__rt);
2836*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__left_);
2837*58b9f456SAndroid Build Coastguard Worker        }
2838*58b9f456SAndroid Build Coastguard Worker        else if (value_comp()(__rt->__value_, __k))
2839*58b9f456SAndroid Build Coastguard Worker            __rt = static_cast<__node_pointer>(__rt->__right_);
2840*58b9f456SAndroid Build Coastguard Worker        else
2841*58b9f456SAndroid Build Coastguard Worker            return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2842*58b9f456SAndroid Build Coastguard Worker                      __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2843*58b9f456SAndroid Build Coastguard Worker    }
2844*58b9f456SAndroid Build Coastguard Worker    return _Pp(const_iterator(__result), const_iterator(__result));
2845*58b9f456SAndroid Build Coastguard Worker}
2846*58b9f456SAndroid Build Coastguard Worker
2847*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2848*58b9f456SAndroid Build Coastguard Workertypename __tree<_Tp, _Compare, _Allocator>::__node_holder
2849*58b9f456SAndroid Build Coastguard Worker__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
2850*58b9f456SAndroid Build Coastguard Worker{
2851*58b9f456SAndroid Build Coastguard Worker    __node_pointer __np = __p.__get_np();
2852*58b9f456SAndroid Build Coastguard Worker    if (__begin_node() == __p.__ptr_)
2853*58b9f456SAndroid Build Coastguard Worker    {
2854*58b9f456SAndroid Build Coastguard Worker        if (__np->__right_ != nullptr)
2855*58b9f456SAndroid Build Coastguard Worker            __begin_node() = static_cast<__iter_pointer>(__np->__right_);
2856*58b9f456SAndroid Build Coastguard Worker        else
2857*58b9f456SAndroid Build Coastguard Worker            __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
2858*58b9f456SAndroid Build Coastguard Worker    }
2859*58b9f456SAndroid Build Coastguard Worker    --size();
2860*58b9f456SAndroid Build Coastguard Worker    __tree_remove(__end_node()->__left_,
2861*58b9f456SAndroid Build Coastguard Worker                  static_cast<__node_base_pointer>(__np));
2862*58b9f456SAndroid Build Coastguard Worker    return __node_holder(__np, _Dp(__node_alloc(), true));
2863*58b9f456SAndroid Build Coastguard Worker}
2864*58b9f456SAndroid Build Coastguard Worker
2865*58b9f456SAndroid Build Coastguard Workertemplate <class _Tp, class _Compare, class _Allocator>
2866*58b9f456SAndroid Build Coastguard Workerinline _LIBCPP_INLINE_VISIBILITY
2867*58b9f456SAndroid Build Coastguard Workervoid
2868*58b9f456SAndroid Build Coastguard Workerswap(__tree<_Tp, _Compare, _Allocator>& __x,
2869*58b9f456SAndroid Build Coastguard Worker     __tree<_Tp, _Compare, _Allocator>& __y)
2870*58b9f456SAndroid Build Coastguard Worker    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2871*58b9f456SAndroid Build Coastguard Worker{
2872*58b9f456SAndroid Build Coastguard Worker    __x.swap(__y);
2873*58b9f456SAndroid Build Coastguard Worker}
2874*58b9f456SAndroid Build Coastguard Worker
2875*58b9f456SAndroid Build Coastguard Worker_LIBCPP_END_NAMESPACE_STD
2876*58b9f456SAndroid Build Coastguard Worker
2877*58b9f456SAndroid Build Coastguard Worker_LIBCPP_POP_MACROS
2878*58b9f456SAndroid Build Coastguard Worker
2879*58b9f456SAndroid Build Coastguard Worker#endif  // _LIBCPP___TREE
2880