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