xref: /freebsd/contrib/llvm-project/libcxx/include/__tree (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP___TREE
110b57cec5SDimitry Andric#define _LIBCPP___TREE
120b57cec5SDimitry Andric
1381ad6265SDimitry Andric#include <__algorithm/min.h>
1481ad6265SDimitry Andric#include <__assert>
150b57cec5SDimitry Andric#include <__config>
16bdd1243dSDimitry Andric#include <__functional/invoke.h>
1781ad6265SDimitry Andric#include <__iterator/distance.h>
1881ad6265SDimitry Andric#include <__iterator/iterator_traits.h>
1981ad6265SDimitry Andric#include <__iterator/next.h>
2006c3fb27SDimitry Andric#include <__memory/addressof.h>
21bdd1243dSDimitry Andric#include <__memory/allocator_traits.h>
22bdd1243dSDimitry Andric#include <__memory/compressed_pair.h>
23bdd1243dSDimitry Andric#include <__memory/pointer_traits.h>
24972a253aSDimitry Andric#include <__memory/swap_allocator.h>
25bdd1243dSDimitry Andric#include <__memory/unique_ptr.h>
26bdd1243dSDimitry Andric#include <__type_traits/can_extract_key.h>
27bdd1243dSDimitry Andric#include <__type_traits/conditional.h>
28bdd1243dSDimitry Andric#include <__type_traits/is_const.h>
2906c3fb27SDimitry Andric#include <__type_traits/is_copy_constructible.h>
30bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_copy_constructible.h>
31bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_default_constructible.h>
32bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_move_assignable.h>
33bdd1243dSDimitry Andric#include <__type_traits/is_nothrow_move_constructible.h>
34bdd1243dSDimitry Andric#include <__type_traits/is_pointer.h>
35bdd1243dSDimitry Andric#include <__type_traits/is_same.h>
36bdd1243dSDimitry Andric#include <__type_traits/is_swappable.h>
37bdd1243dSDimitry Andric#include <__type_traits/remove_const_ref.h>
38bdd1243dSDimitry Andric#include <__type_traits/remove_cvref.h>
39fe6060f1SDimitry Andric#include <__utility/forward.h>
40bdd1243dSDimitry Andric#include <__utility/move.h>
41bdd1243dSDimitry Andric#include <__utility/pair.h>
4281ad6265SDimitry Andric#include <__utility/swap.h>
43fe6060f1SDimitry Andric#include <limits>
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
460b57cec5SDimitry Andric#  pragma GCC system_header
470b57cec5SDimitry Andric#endif
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
500b57cec5SDimitry Andric#include <__undef_macros>
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
540b57cec5SDimitry Andric
550b57cec5SDimitry Andrictemplate <class, class, class, class> class _LIBCPP_TEMPLATE_VIS map;
560b57cec5SDimitry Andrictemplate <class, class, class, class> class _LIBCPP_TEMPLATE_VIS multimap;
570b57cec5SDimitry Andrictemplate <class, class, class> class _LIBCPP_TEMPLATE_VIS set;
580b57cec5SDimitry Andrictemplate <class, class, class> class _LIBCPP_TEMPLATE_VIS multiset;
590b57cec5SDimitry Andric
600b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> class __tree;
610b57cec5SDimitry Andrictemplate <class _Tp, class _NodePtr, class _DiffType>
620b57cec5SDimitry Andric    class _LIBCPP_TEMPLATE_VIS __tree_iterator;
630b57cec5SDimitry Andrictemplate <class _Tp, class _ConstNodePtr, class _DiffType>
640b57cec5SDimitry Andric    class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
650b57cec5SDimitry Andric
660b57cec5SDimitry Andrictemplate <class _Pointer> class __tree_end_node;
670b57cec5SDimitry Andrictemplate <class _VoidPtr> class __tree_node_base;
680b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr> class __tree_node;
690b57cec5SDimitry Andric
700b57cec5SDimitry Andrictemplate <class _Key, class _Value>
710b57cec5SDimitry Andricstruct __value_type;
720b57cec5SDimitry Andric
730b57cec5SDimitry Andrictemplate <class _Allocator> class __map_node_destructor;
740b57cec5SDimitry Andrictemplate <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
750b57cec5SDimitry Andrictemplate <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
760b57cec5SDimitry Andric
770b57cec5SDimitry Andric/*
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric_NodePtr algorithms
800b57cec5SDimitry Andric
810b57cec5SDimitry AndricThe algorithms taking _NodePtr are red black tree algorithms.  Those
820b57cec5SDimitry Andricalgorithms taking a parameter named __root should assume that __root
830b57cec5SDimitry Andricpoints to a proper red black tree (unless otherwise specified).
840b57cec5SDimitry Andric
850b57cec5SDimitry AndricEach algorithm herein assumes that __root->__parent_ points to a non-null
860b57cec5SDimitry Andricstructure which has a member __left_ which points back to __root.  No other
870b57cec5SDimitry Andricmember is read or written to at __root->__parent_.
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric__root->__parent_ will be referred to below (in comments only) as end_node.
900b57cec5SDimitry Andricend_node->__left_ is an externably accessible lvalue for __root, and can be
910b57cec5SDimitry Andricchanged by node insertion and removal (without explicit reference to end_node).
920b57cec5SDimitry Andric
930b57cec5SDimitry AndricAll nodes (with the exception of end_node), even the node referred to as
940b57cec5SDimitry Andric__root, have a non-null __parent_ field.
950b57cec5SDimitry Andric
960b57cec5SDimitry Andric*/
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric// Returns:  true if __x is a left child of its parent, else false
990b57cec5SDimitry Andric// Precondition:  __x != nullptr.
1000b57cec5SDimitry Andrictemplate <class _NodePtr>
101*5f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
1020b57cec5SDimitry Andricbool
1030b57cec5SDimitry Andric__tree_is_left_child(_NodePtr __x) _NOEXCEPT
1040b57cec5SDimitry Andric{
1050b57cec5SDimitry Andric    return __x == __x->__parent_->__left_;
1060b57cec5SDimitry Andric}
1070b57cec5SDimitry Andric
1080b57cec5SDimitry Andric// Determines if the subtree rooted at __x is a proper red black subtree.  If
1090b57cec5SDimitry Andric//    __x is a proper subtree, returns the black height (null counts as 1).  If
1100b57cec5SDimitry Andric//    __x is an improper subtree, returns 0.
1110b57cec5SDimitry Andrictemplate <class _NodePtr>
1120b57cec5SDimitry Andricunsigned
1130b57cec5SDimitry Andric__tree_sub_invariant(_NodePtr __x)
1140b57cec5SDimitry Andric{
1150b57cec5SDimitry Andric    if (__x == nullptr)
1160b57cec5SDimitry Andric        return 1;
1170b57cec5SDimitry Andric    // parent consistency checked by caller
1180b57cec5SDimitry Andric    // check __x->__left_ consistency
1190b57cec5SDimitry Andric    if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
1200b57cec5SDimitry Andric        return 0;
1210b57cec5SDimitry Andric    // check __x->__right_ consistency
1220b57cec5SDimitry Andric    if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
1230b57cec5SDimitry Andric        return 0;
1240b57cec5SDimitry Andric    // check __x->__left_ != __x->__right_ unless both are nullptr
1250b57cec5SDimitry Andric    if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
1260b57cec5SDimitry Andric        return 0;
1270b57cec5SDimitry Andric    // If this is red, neither child can be red
1280b57cec5SDimitry Andric    if (!__x->__is_black_)
1290b57cec5SDimitry Andric    {
1300b57cec5SDimitry Andric        if (__x->__left_ && !__x->__left_->__is_black_)
1310b57cec5SDimitry Andric            return 0;
1320b57cec5SDimitry Andric        if (__x->__right_ && !__x->__right_->__is_black_)
1330b57cec5SDimitry Andric            return 0;
1340b57cec5SDimitry Andric    }
135*5f757f3fSDimitry Andric    unsigned __h = std::__tree_sub_invariant(__x->__left_);
1360b57cec5SDimitry Andric    if (__h == 0)
1370b57cec5SDimitry Andric        return 0;  // invalid left subtree
138*5f757f3fSDimitry Andric    if (__h != std::__tree_sub_invariant(__x->__right_))
1390b57cec5SDimitry Andric        return 0;  // invalid or different height right subtree
1400b57cec5SDimitry Andric    return __h + __x->__is_black_;  // return black height of this node
1410b57cec5SDimitry Andric}
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric// Determines if the red black tree rooted at __root is a proper red black tree.
1440b57cec5SDimitry Andric//    __root == nullptr is a proper tree.  Returns true is __root is a proper
1450b57cec5SDimitry Andric//    red black tree, else returns false.
1460b57cec5SDimitry Andrictemplate <class _NodePtr>
147bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI bool
1480b57cec5SDimitry Andric__tree_invariant(_NodePtr __root)
1490b57cec5SDimitry Andric{
1500b57cec5SDimitry Andric    if (__root == nullptr)
1510b57cec5SDimitry Andric        return true;
1520b57cec5SDimitry Andric    // check __x->__parent_ consistency
1530b57cec5SDimitry Andric    if (__root->__parent_ == nullptr)
1540b57cec5SDimitry Andric        return false;
155*5f757f3fSDimitry Andric    if (!std::__tree_is_left_child(__root))
1560b57cec5SDimitry Andric        return false;
1570b57cec5SDimitry Andric    // root must be black
1580b57cec5SDimitry Andric    if (!__root->__is_black_)
1590b57cec5SDimitry Andric        return false;
1600b57cec5SDimitry Andric    // do normal node checks
161*5f757f3fSDimitry Andric    return std::__tree_sub_invariant(__root) != 0;
1620b57cec5SDimitry Andric}
1630b57cec5SDimitry Andric
1640b57cec5SDimitry Andric// Returns:  pointer to the left-most node under __x.
1650b57cec5SDimitry Andrictemplate <class _NodePtr>
166*5f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
1670b57cec5SDimitry Andric_NodePtr
1680b57cec5SDimitry Andric__tree_min(_NodePtr __x) _NOEXCEPT
1690b57cec5SDimitry Andric{
17006c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null");
1710b57cec5SDimitry Andric    while (__x->__left_ != nullptr)
1720b57cec5SDimitry Andric        __x = __x->__left_;
1730b57cec5SDimitry Andric    return __x;
1740b57cec5SDimitry Andric}
1750b57cec5SDimitry Andric
1760b57cec5SDimitry Andric// Returns:  pointer to the right-most node under __x.
1770b57cec5SDimitry Andrictemplate <class _NodePtr>
178*5f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
1790b57cec5SDimitry Andric_NodePtr
1800b57cec5SDimitry Andric__tree_max(_NodePtr __x) _NOEXCEPT
1810b57cec5SDimitry Andric{
18206c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null");
1830b57cec5SDimitry Andric    while (__x->__right_ != nullptr)
1840b57cec5SDimitry Andric        __x = __x->__right_;
1850b57cec5SDimitry Andric    return __x;
1860b57cec5SDimitry Andric}
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric// Returns:  pointer to the next in-order node after __x.
1890b57cec5SDimitry Andrictemplate <class _NodePtr>
190bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI _NodePtr
1910b57cec5SDimitry Andric__tree_next(_NodePtr __x) _NOEXCEPT
1920b57cec5SDimitry Andric{
19306c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
1940b57cec5SDimitry Andric    if (__x->__right_ != nullptr)
195*5f757f3fSDimitry Andric        return std::__tree_min(__x->__right_);
196*5f757f3fSDimitry Andric    while (!std::__tree_is_left_child(__x))
1970b57cec5SDimitry Andric        __x = __x->__parent_unsafe();
1980b57cec5SDimitry Andric    return __x->__parent_unsafe();
1990b57cec5SDimitry Andric}
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andrictemplate <class _EndNodePtr, class _NodePtr>
202*5f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
2030b57cec5SDimitry Andric_EndNodePtr
2040b57cec5SDimitry Andric__tree_next_iter(_NodePtr __x) _NOEXCEPT
2050b57cec5SDimitry Andric{
20606c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
2070b57cec5SDimitry Andric    if (__x->__right_ != nullptr)
208*5f757f3fSDimitry Andric        return static_cast<_EndNodePtr>(std::__tree_min(__x->__right_));
209*5f757f3fSDimitry Andric    while (!std::__tree_is_left_child(__x))
2100b57cec5SDimitry Andric        __x = __x->__parent_unsafe();
2110b57cec5SDimitry Andric    return static_cast<_EndNodePtr>(__x->__parent_);
2120b57cec5SDimitry Andric}
2130b57cec5SDimitry Andric
2140b57cec5SDimitry Andric// Returns:  pointer to the previous in-order node before __x.
2150b57cec5SDimitry Andric// Note: __x may be the end node.
2160b57cec5SDimitry Andrictemplate <class _NodePtr, class _EndNodePtr>
217*5f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
2180b57cec5SDimitry Andric_NodePtr
2190b57cec5SDimitry Andric__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
2200b57cec5SDimitry Andric{
22106c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
2220b57cec5SDimitry Andric    if (__x->__left_ != nullptr)
223*5f757f3fSDimitry Andric        return std::__tree_max(__x->__left_);
2240b57cec5SDimitry Andric    _NodePtr __xx = static_cast<_NodePtr>(__x);
225*5f757f3fSDimitry Andric    while (std::__tree_is_left_child(__xx))
2260b57cec5SDimitry Andric        __xx = __xx->__parent_unsafe();
2270b57cec5SDimitry Andric    return __xx->__parent_unsafe();
2280b57cec5SDimitry Andric}
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric// Returns:  pointer to a node which has no children
2310b57cec5SDimitry Andrictemplate <class _NodePtr>
232bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI _NodePtr
2330b57cec5SDimitry Andric__tree_leaf(_NodePtr __x) _NOEXCEPT
2340b57cec5SDimitry Andric{
23506c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
2360b57cec5SDimitry Andric    while (true)
2370b57cec5SDimitry Andric    {
2380b57cec5SDimitry Andric        if (__x->__left_ != nullptr)
2390b57cec5SDimitry Andric        {
2400b57cec5SDimitry Andric            __x = __x->__left_;
2410b57cec5SDimitry Andric            continue;
2420b57cec5SDimitry Andric        }
2430b57cec5SDimitry Andric        if (__x->__right_ != nullptr)
2440b57cec5SDimitry Andric        {
2450b57cec5SDimitry Andric            __x = __x->__right_;
2460b57cec5SDimitry Andric            continue;
2470b57cec5SDimitry Andric        }
2480b57cec5SDimitry Andric        break;
2490b57cec5SDimitry Andric    }
2500b57cec5SDimitry Andric    return __x;
2510b57cec5SDimitry Andric}
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric// Effects:  Makes __x->__right_ the subtree root with __x as its left child
2540b57cec5SDimitry Andric//           while preserving in-order order.
2550b57cec5SDimitry Andrictemplate <class _NodePtr>
256bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI void
2570b57cec5SDimitry Andric__tree_left_rotate(_NodePtr __x) _NOEXCEPT
2580b57cec5SDimitry Andric{
25906c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
26006c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x->__right_ != nullptr, "node should have a right child");
2610b57cec5SDimitry Andric    _NodePtr __y = __x->__right_;
2620b57cec5SDimitry Andric    __x->__right_ = __y->__left_;
2630b57cec5SDimitry Andric    if (__x->__right_ != nullptr)
2640b57cec5SDimitry Andric        __x->__right_->__set_parent(__x);
2650b57cec5SDimitry Andric    __y->__parent_ = __x->__parent_;
266*5f757f3fSDimitry Andric    if (std::__tree_is_left_child(__x))
2670b57cec5SDimitry Andric        __x->__parent_->__left_ = __y;
2680b57cec5SDimitry Andric    else
2690b57cec5SDimitry Andric        __x->__parent_unsafe()->__right_ = __y;
2700b57cec5SDimitry Andric    __y->__left_ = __x;
2710b57cec5SDimitry Andric    __x->__set_parent(__y);
2720b57cec5SDimitry Andric}
2730b57cec5SDimitry Andric
2740b57cec5SDimitry Andric// Effects:  Makes __x->__left_ the subtree root with __x as its right child
2750b57cec5SDimitry Andric//           while preserving in-order order.
2760b57cec5SDimitry Andrictemplate <class _NodePtr>
277bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI void
2780b57cec5SDimitry Andric__tree_right_rotate(_NodePtr __x) _NOEXCEPT
2790b57cec5SDimitry Andric{
28006c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
28106c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x->__left_ != nullptr, "node should have a left child");
2820b57cec5SDimitry Andric    _NodePtr __y = __x->__left_;
2830b57cec5SDimitry Andric    __x->__left_ = __y->__right_;
2840b57cec5SDimitry Andric    if (__x->__left_ != nullptr)
2850b57cec5SDimitry Andric        __x->__left_->__set_parent(__x);
2860b57cec5SDimitry Andric    __y->__parent_ = __x->__parent_;
287*5f757f3fSDimitry Andric    if (std::__tree_is_left_child(__x))
2880b57cec5SDimitry Andric        __x->__parent_->__left_ = __y;
2890b57cec5SDimitry Andric    else
2900b57cec5SDimitry Andric        __x->__parent_unsafe()->__right_ = __y;
2910b57cec5SDimitry Andric    __y->__right_ = __x;
2920b57cec5SDimitry Andric    __x->__set_parent(__y);
2930b57cec5SDimitry Andric}
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric// Effects:  Rebalances __root after attaching __x to a leaf.
29681ad6265SDimitry Andric// Precondition:  __x has no children.
2970b57cec5SDimitry Andric//                __x == __root or == a direct or indirect child of __root.
2980b57cec5SDimitry Andric//                If __x were to be unlinked from __root (setting __root to
2990b57cec5SDimitry Andric//                  nullptr if __root == __x), __tree_invariant(__root) == true.
3000b57cec5SDimitry Andric// Postcondition: __tree_invariant(end_node->__left_) == true.  end_node->__left_
3010b57cec5SDimitry Andric//                may be different than the value passed in as __root.
3020b57cec5SDimitry Andrictemplate <class _NodePtr>
303bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI void
3040b57cec5SDimitry Andric__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
3050b57cec5SDimitry Andric{
30606c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root of the tree shouldn't be null");
30706c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Can't attach null node to a leaf");
3080b57cec5SDimitry Andric    __x->__is_black_ = __x == __root;
3090b57cec5SDimitry Andric    while (__x != __root && !__x->__parent_unsafe()->__is_black_)
3100b57cec5SDimitry Andric    {
3110b57cec5SDimitry Andric        // __x->__parent_ != __root because __x->__parent_->__is_black == false
312*5f757f3fSDimitry Andric        if (std::__tree_is_left_child(__x->__parent_unsafe()))
3130b57cec5SDimitry Andric        {
3140b57cec5SDimitry Andric            _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
3150b57cec5SDimitry Andric            if (__y != nullptr && !__y->__is_black_)
3160b57cec5SDimitry Andric            {
3170b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3180b57cec5SDimitry Andric                __x->__is_black_ = true;
3190b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3200b57cec5SDimitry Andric                __x->__is_black_ = __x == __root;
3210b57cec5SDimitry Andric                __y->__is_black_ = true;
3220b57cec5SDimitry Andric            }
3230b57cec5SDimitry Andric            else
3240b57cec5SDimitry Andric            {
325*5f757f3fSDimitry Andric                if (!std::__tree_is_left_child(__x))
3260b57cec5SDimitry Andric                {
3270b57cec5SDimitry Andric                    __x = __x->__parent_unsafe();
328*5f757f3fSDimitry Andric                    std::__tree_left_rotate(__x);
3290b57cec5SDimitry Andric                }
3300b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3310b57cec5SDimitry Andric                __x->__is_black_ = true;
3320b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3330b57cec5SDimitry Andric                __x->__is_black_ = false;
334*5f757f3fSDimitry Andric                std::__tree_right_rotate(__x);
3350b57cec5SDimitry Andric                break;
3360b57cec5SDimitry Andric            }
3370b57cec5SDimitry Andric        }
3380b57cec5SDimitry Andric        else
3390b57cec5SDimitry Andric        {
3400b57cec5SDimitry Andric            _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
3410b57cec5SDimitry Andric            if (__y != nullptr && !__y->__is_black_)
3420b57cec5SDimitry Andric            {
3430b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3440b57cec5SDimitry Andric                __x->__is_black_ = true;
3450b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3460b57cec5SDimitry Andric                __x->__is_black_ = __x == __root;
3470b57cec5SDimitry Andric                __y->__is_black_ = true;
3480b57cec5SDimitry Andric            }
3490b57cec5SDimitry Andric            else
3500b57cec5SDimitry Andric            {
351*5f757f3fSDimitry Andric                if (std::__tree_is_left_child(__x))
3520b57cec5SDimitry Andric                {
3530b57cec5SDimitry Andric                    __x = __x->__parent_unsafe();
354*5f757f3fSDimitry Andric                    std::__tree_right_rotate(__x);
3550b57cec5SDimitry Andric                }
3560b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3570b57cec5SDimitry Andric                __x->__is_black_ = true;
3580b57cec5SDimitry Andric                __x = __x->__parent_unsafe();
3590b57cec5SDimitry Andric                __x->__is_black_ = false;
360*5f757f3fSDimitry Andric                std::__tree_left_rotate(__x);
3610b57cec5SDimitry Andric                break;
3620b57cec5SDimitry Andric            }
3630b57cec5SDimitry Andric        }
3640b57cec5SDimitry Andric    }
3650b57cec5SDimitry Andric}
3660b57cec5SDimitry Andric
36781ad6265SDimitry Andric// Precondition:  __z == __root or == a direct or indirect child of __root.
3680b57cec5SDimitry Andric// Effects:  unlinks __z from the tree rooted at __root, rebalancing as needed.
3690b57cec5SDimitry Andric// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
3700b57cec5SDimitry Andric//                nor any of its children refer to __z.  end_node->__left_
3710b57cec5SDimitry Andric//                may be different than the value passed in as __root.
3720b57cec5SDimitry Andrictemplate <class _NodePtr>
373bdd1243dSDimitry Andric_LIBCPP_HIDE_FROM_ABI void
3740b57cec5SDimitry Andric__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
3750b57cec5SDimitry Andric{
37606c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root node should not be null");
37706c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(__z != nullptr, "The node to remove should not be null");
37806c3fb27SDimitry Andric    _LIBCPP_ASSERT_INTERNAL(std::__tree_invariant(__root), "The tree invariants should hold");
3790b57cec5SDimitry Andric    // __z will be removed from the tree.  Client still needs to destruct/deallocate it
3800b57cec5SDimitry Andric    // __y is either __z, or if __z has two children, __tree_next(__z).
3810b57cec5SDimitry Andric    // __y will have at most one child.
3820b57cec5SDimitry Andric    // __y will be the initial hole in the tree (make the hole at a leaf)
3830b57cec5SDimitry Andric    _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
384*5f757f3fSDimitry Andric                    __z : std::__tree_next(__z);
3850b57cec5SDimitry Andric    // __x is __y's possibly null single child
3860b57cec5SDimitry Andric    _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
3870b57cec5SDimitry Andric    // __w is __x's possibly null uncle (will become __x's sibling)
3880b57cec5SDimitry Andric    _NodePtr __w = nullptr;
3890b57cec5SDimitry Andric    // link __x to __y's parent, and find __w
3900b57cec5SDimitry Andric    if (__x != nullptr)
3910b57cec5SDimitry Andric        __x->__parent_ = __y->__parent_;
392*5f757f3fSDimitry Andric    if (std::__tree_is_left_child(__y))
3930b57cec5SDimitry Andric    {
3940b57cec5SDimitry Andric        __y->__parent_->__left_ = __x;
3950b57cec5SDimitry Andric        if (__y != __root)
3960b57cec5SDimitry Andric            __w = __y->__parent_unsafe()->__right_;
3970b57cec5SDimitry Andric        else
3980b57cec5SDimitry Andric            __root = __x;  // __w == nullptr
3990b57cec5SDimitry Andric    }
4000b57cec5SDimitry Andric    else
4010b57cec5SDimitry Andric    {
4020b57cec5SDimitry Andric        __y->__parent_unsafe()->__right_ = __x;
4030b57cec5SDimitry Andric        // __y can't be root if it is a right child
4040b57cec5SDimitry Andric        __w = __y->__parent_->__left_;
4050b57cec5SDimitry Andric    }
4060b57cec5SDimitry Andric    bool __removed_black = __y->__is_black_;
4070b57cec5SDimitry Andric    // If we didn't remove __z, do so now by splicing in __y for __z,
4080b57cec5SDimitry Andric    //    but copy __z's color.  This does not impact __x or __w.
4090b57cec5SDimitry Andric    if (__y != __z)
4100b57cec5SDimitry Andric    {
4110b57cec5SDimitry Andric        // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
4120b57cec5SDimitry Andric        __y->__parent_ = __z->__parent_;
413*5f757f3fSDimitry Andric        if (std::__tree_is_left_child(__z))
4140b57cec5SDimitry Andric            __y->__parent_->__left_ = __y;
4150b57cec5SDimitry Andric        else
4160b57cec5SDimitry Andric            __y->__parent_unsafe()->__right_ = __y;
4170b57cec5SDimitry Andric        __y->__left_ = __z->__left_;
4180b57cec5SDimitry Andric        __y->__left_->__set_parent(__y);
4190b57cec5SDimitry Andric        __y->__right_ = __z->__right_;
4200b57cec5SDimitry Andric        if (__y->__right_ != nullptr)
4210b57cec5SDimitry Andric            __y->__right_->__set_parent(__y);
4220b57cec5SDimitry Andric        __y->__is_black_ = __z->__is_black_;
4230b57cec5SDimitry Andric        if (__root == __z)
4240b57cec5SDimitry Andric            __root = __y;
4250b57cec5SDimitry Andric    }
4260b57cec5SDimitry Andric    // There is no need to rebalance if we removed a red, or if we removed
4270b57cec5SDimitry Andric    //     the last node.
4280b57cec5SDimitry Andric    if (__removed_black && __root != nullptr)
4290b57cec5SDimitry Andric    {
4300b57cec5SDimitry Andric        // Rebalance:
4310b57cec5SDimitry Andric        // __x has an implicit black color (transferred from the removed __y)
4320b57cec5SDimitry Andric        //    associated with it, no matter what its color is.
4330b57cec5SDimitry Andric        // If __x is __root (in which case it can't be null), it is supposed
4340b57cec5SDimitry Andric        //    to be black anyway, and if it is doubly black, then the double
4350b57cec5SDimitry Andric        //    can just be ignored.
4360b57cec5SDimitry Andric        // If __x is red (in which case it can't be null), then it can absorb
4370b57cec5SDimitry Andric        //    the implicit black just by setting its color to black.
4380b57cec5SDimitry Andric        // Since __y was black and only had one child (which __x points to), __x
4390b57cec5SDimitry Andric        //   is either red with no children, else null, otherwise __y would have
4400b57cec5SDimitry Andric        //   different black heights under left and right pointers.
4410b57cec5SDimitry Andric        // if (__x == __root || __x != nullptr && !__x->__is_black_)
4420b57cec5SDimitry Andric        if (__x != nullptr)
4430b57cec5SDimitry Andric            __x->__is_black_ = true;
4440b57cec5SDimitry Andric        else
4450b57cec5SDimitry Andric        {
4460b57cec5SDimitry Andric            //  Else __x isn't root, and is "doubly black", even though it may
4470b57cec5SDimitry Andric            //     be null.  __w can not be null here, else the parent would
4480b57cec5SDimitry Andric            //     see a black height >= 2 on the __x side and a black height
4490b57cec5SDimitry Andric            //     of 1 on the __w side (__w must be a non-null black or a red
4500b57cec5SDimitry Andric            //     with a non-null black child).
4510b57cec5SDimitry Andric            while (true)
4520b57cec5SDimitry Andric            {
453*5f757f3fSDimitry Andric                if (!std::__tree_is_left_child(__w))  // if x is left child
4540b57cec5SDimitry Andric                {
4550b57cec5SDimitry Andric                    if (!__w->__is_black_)
4560b57cec5SDimitry Andric                    {
4570b57cec5SDimitry Andric                        __w->__is_black_ = true;
4580b57cec5SDimitry Andric                        __w->__parent_unsafe()->__is_black_ = false;
459*5f757f3fSDimitry Andric                        std::__tree_left_rotate(__w->__parent_unsafe());
4600b57cec5SDimitry Andric                        // __x is still valid
4610b57cec5SDimitry Andric                        // reset __root only if necessary
4620b57cec5SDimitry Andric                        if (__root == __w->__left_)
4630b57cec5SDimitry Andric                            __root = __w;
4640b57cec5SDimitry Andric                        // reset sibling, and it still can't be null
4650b57cec5SDimitry Andric                        __w = __w->__left_->__right_;
4660b57cec5SDimitry Andric                    }
4670b57cec5SDimitry Andric                    // __w->__is_black_ is now true, __w may have null children
4680b57cec5SDimitry Andric                    if ((__w->__left_  == nullptr || __w->__left_->__is_black_) &&
4690b57cec5SDimitry Andric                        (__w->__right_ == nullptr || __w->__right_->__is_black_))
4700b57cec5SDimitry Andric                    {
4710b57cec5SDimitry Andric                        __w->__is_black_ = false;
4720b57cec5SDimitry Andric                        __x = __w->__parent_unsafe();
4730b57cec5SDimitry Andric                        // __x can no longer be null
4740b57cec5SDimitry Andric                        if (__x == __root || !__x->__is_black_)
4750b57cec5SDimitry Andric                        {
4760b57cec5SDimitry Andric                            __x->__is_black_ = true;
4770b57cec5SDimitry Andric                            break;
4780b57cec5SDimitry Andric                        }
4790b57cec5SDimitry Andric                        // reset sibling, and it still can't be null
480*5f757f3fSDimitry Andric                        __w = std::__tree_is_left_child(__x) ?
4810b57cec5SDimitry Andric                                    __x->__parent_unsafe()->__right_ :
4820b57cec5SDimitry Andric                                    __x->__parent_->__left_;
4830b57cec5SDimitry Andric                        // continue;
4840b57cec5SDimitry Andric                    }
4850b57cec5SDimitry Andric                    else  // __w has a red child
4860b57cec5SDimitry Andric                    {
4870b57cec5SDimitry Andric                        if (__w->__right_ == nullptr || __w->__right_->__is_black_)
4880b57cec5SDimitry Andric                        {
4890b57cec5SDimitry Andric                            // __w left child is non-null and red
4900b57cec5SDimitry Andric                            __w->__left_->__is_black_ = true;
4910b57cec5SDimitry Andric                            __w->__is_black_ = false;
492*5f757f3fSDimitry Andric                            std::__tree_right_rotate(__w);
4930b57cec5SDimitry Andric                            // __w is known not to be root, so root hasn't changed
4940b57cec5SDimitry Andric                            // reset sibling, and it still can't be null
4950b57cec5SDimitry Andric                            __w = __w->__parent_unsafe();
4960b57cec5SDimitry Andric                        }
4970b57cec5SDimitry Andric                        // __w has a right red child, left child may be null
4980b57cec5SDimitry Andric                        __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
4990b57cec5SDimitry Andric                        __w->__parent_unsafe()->__is_black_ = true;
5000b57cec5SDimitry Andric                        __w->__right_->__is_black_ = true;
501*5f757f3fSDimitry Andric                        std::__tree_left_rotate(__w->__parent_unsafe());
5020b57cec5SDimitry Andric                        break;
5030b57cec5SDimitry Andric                    }
5040b57cec5SDimitry Andric                }
5050b57cec5SDimitry Andric                else
5060b57cec5SDimitry Andric                {
5070b57cec5SDimitry Andric                    if (!__w->__is_black_)
5080b57cec5SDimitry Andric                    {
5090b57cec5SDimitry Andric                        __w->__is_black_ = true;
5100b57cec5SDimitry Andric                        __w->__parent_unsafe()->__is_black_ = false;
511*5f757f3fSDimitry Andric                        std::__tree_right_rotate(__w->__parent_unsafe());
5120b57cec5SDimitry Andric                        // __x is still valid
5130b57cec5SDimitry Andric                        // reset __root only if necessary
5140b57cec5SDimitry Andric                        if (__root == __w->__right_)
5150b57cec5SDimitry Andric                            __root = __w;
5160b57cec5SDimitry Andric                        // reset sibling, and it still can't be null
5170b57cec5SDimitry Andric                        __w = __w->__right_->__left_;
5180b57cec5SDimitry Andric                    }
5190b57cec5SDimitry Andric                    // __w->__is_black_ is now true, __w may have null children
5200b57cec5SDimitry Andric                    if ((__w->__left_  == nullptr || __w->__left_->__is_black_) &&
5210b57cec5SDimitry Andric                        (__w->__right_ == nullptr || __w->__right_->__is_black_))
5220b57cec5SDimitry Andric                    {
5230b57cec5SDimitry Andric                        __w->__is_black_ = false;
5240b57cec5SDimitry Andric                        __x = __w->__parent_unsafe();
5250b57cec5SDimitry Andric                        // __x can no longer be null
5260b57cec5SDimitry Andric                        if (!__x->__is_black_ || __x == __root)
5270b57cec5SDimitry Andric                        {
5280b57cec5SDimitry Andric                            __x->__is_black_ = true;
5290b57cec5SDimitry Andric                            break;
5300b57cec5SDimitry Andric                        }
5310b57cec5SDimitry Andric                        // reset sibling, and it still can't be null
532*5f757f3fSDimitry Andric                        __w = std::__tree_is_left_child(__x) ?
5330b57cec5SDimitry Andric                                    __x->__parent_unsafe()->__right_ :
5340b57cec5SDimitry Andric                                    __x->__parent_->__left_;
5350b57cec5SDimitry Andric                        // continue;
5360b57cec5SDimitry Andric                    }
5370b57cec5SDimitry Andric                    else  // __w has a red child
5380b57cec5SDimitry Andric                    {
5390b57cec5SDimitry Andric                        if (__w->__left_ == nullptr || __w->__left_->__is_black_)
5400b57cec5SDimitry Andric                        {
5410b57cec5SDimitry Andric                            // __w right child is non-null and red
5420b57cec5SDimitry Andric                            __w->__right_->__is_black_ = true;
5430b57cec5SDimitry Andric                            __w->__is_black_ = false;
544*5f757f3fSDimitry Andric                            std::__tree_left_rotate(__w);
5450b57cec5SDimitry Andric                            // __w is known not to be root, so root hasn't changed
5460b57cec5SDimitry Andric                            // reset sibling, and it still can't be null
5470b57cec5SDimitry Andric                            __w = __w->__parent_unsafe();
5480b57cec5SDimitry Andric                        }
5490b57cec5SDimitry Andric                        // __w has a left red child, right child may be null
5500b57cec5SDimitry Andric                        __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
5510b57cec5SDimitry Andric                        __w->__parent_unsafe()->__is_black_ = true;
5520b57cec5SDimitry Andric                        __w->__left_->__is_black_ = true;
553*5f757f3fSDimitry Andric                        std::__tree_right_rotate(__w->__parent_unsafe());
5540b57cec5SDimitry Andric                        break;
5550b57cec5SDimitry Andric                    }
5560b57cec5SDimitry Andric                }
5570b57cec5SDimitry Andric            }
5580b57cec5SDimitry Andric        }
5590b57cec5SDimitry Andric    }
5600b57cec5SDimitry Andric}
5610b57cec5SDimitry Andric
5620b57cec5SDimitry Andric// node traits
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric
5650b57cec5SDimitry Andrictemplate <class _Tp>
5660b57cec5SDimitry Andricstruct __is_tree_value_type_imp : false_type {};
5670b57cec5SDimitry Andric
5680b57cec5SDimitry Andrictemplate <class _Key, class _Value>
5690b57cec5SDimitry Andricstruct __is_tree_value_type_imp<__value_type<_Key, _Value> > : true_type {};
5700b57cec5SDimitry Andric
5710b57cec5SDimitry Andrictemplate <class ..._Args>
5720b57cec5SDimitry Andricstruct __is_tree_value_type : false_type {};
5730b57cec5SDimitry Andric
5740b57cec5SDimitry Andrictemplate <class _One>
575bdd1243dSDimitry Andricstruct __is_tree_value_type<_One> : __is_tree_value_type_imp<__remove_cvref_t<_One> > {};
5760b57cec5SDimitry Andric
5770b57cec5SDimitry Andrictemplate <class _Tp>
5780b57cec5SDimitry Andricstruct __tree_key_value_types {
5790b57cec5SDimitry Andric  typedef _Tp key_type;
5800b57cec5SDimitry Andric  typedef _Tp __node_value_type;
5810b57cec5SDimitry Andric  typedef _Tp __container_value_type;
5820b57cec5SDimitry Andric  static const bool __is_map = false;
5830b57cec5SDimitry Andric
584*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5850b57cec5SDimitry Andric  static key_type const& __get_key(_Tp const& __v) {
5860b57cec5SDimitry Andric    return __v;
5870b57cec5SDimitry Andric  }
588*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5890b57cec5SDimitry Andric  static __container_value_type const& __get_value(__node_value_type const& __v) {
5900b57cec5SDimitry Andric    return __v;
5910b57cec5SDimitry Andric  }
592*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5930b57cec5SDimitry Andric  static __container_value_type* __get_ptr(__node_value_type& __n) {
594*5f757f3fSDimitry Andric    return std::addressof(__n);
5950b57cec5SDimitry Andric  }
596*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
5970b57cec5SDimitry Andric  static __container_value_type&& __move(__node_value_type& __v) {
598*5f757f3fSDimitry Andric    return std::move(__v);
5990b57cec5SDimitry Andric  }
6000b57cec5SDimitry Andric};
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andrictemplate <class _Key, class _Tp>
6030b57cec5SDimitry Andricstruct __tree_key_value_types<__value_type<_Key, _Tp> > {
6040b57cec5SDimitry Andric  typedef _Key                                         key_type;
6050b57cec5SDimitry Andric  typedef _Tp                                          mapped_type;
6060b57cec5SDimitry Andric  typedef __value_type<_Key, _Tp>                      __node_value_type;
6070b57cec5SDimitry Andric  typedef pair<const _Key, _Tp>                        __container_value_type;
6080b57cec5SDimitry Andric  typedef __container_value_type                       __map_value_type;
6090b57cec5SDimitry Andric  static const bool __is_map = true;
6100b57cec5SDimitry Andric
611*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6120b57cec5SDimitry Andric  static key_type const&
6130b57cec5SDimitry Andric  __get_key(__node_value_type const& __t) {
6140b57cec5SDimitry Andric    return __t.__get_value().first;
6150b57cec5SDimitry Andric  }
6160b57cec5SDimitry Andric
617*5f757f3fSDimitry Andric  template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0>
618*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
619*5f757f3fSDimitry Andric  static key_type const&
6200b57cec5SDimitry Andric  __get_key(_Up& __t) {
6210b57cec5SDimitry Andric    return __t.first;
6220b57cec5SDimitry Andric  }
6230b57cec5SDimitry Andric
624*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6250b57cec5SDimitry Andric  static __container_value_type const&
6260b57cec5SDimitry Andric  __get_value(__node_value_type const& __t) {
6270b57cec5SDimitry Andric    return __t.__get_value();
6280b57cec5SDimitry Andric  }
6290b57cec5SDimitry Andric
6300b57cec5SDimitry Andric  template <class _Up>
631*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
632753f127fSDimitry Andric  static __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, __container_value_type const&>
6330b57cec5SDimitry Andric  __get_value(_Up& __t) {
6340b57cec5SDimitry Andric    return __t;
6350b57cec5SDimitry Andric  }
6360b57cec5SDimitry Andric
637*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6380b57cec5SDimitry Andric  static __container_value_type* __get_ptr(__node_value_type& __n) {
639*5f757f3fSDimitry Andric    return std::addressof(__n.__get_value());
6400b57cec5SDimitry Andric  }
6410b57cec5SDimitry Andric
642*5f757f3fSDimitry Andric  _LIBCPP_HIDE_FROM_ABI
6430b57cec5SDimitry Andric  static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
6440b57cec5SDimitry Andric    return __v.__move();
6450b57cec5SDimitry Andric  }
6460b57cec5SDimitry Andric};
6470b57cec5SDimitry Andric
6480b57cec5SDimitry Andrictemplate <class _VoidPtr>
6490b57cec5SDimitry Andricstruct __tree_node_base_types {
6500b57cec5SDimitry Andric  typedef _VoidPtr                                               __void_pointer;
6510b57cec5SDimitry Andric
6520b57cec5SDimitry Andric  typedef __tree_node_base<__void_pointer>                      __node_base_type;
653bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, __node_base_type>
6540b57cec5SDimitry Andric                                                             __node_base_pointer;
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andric  typedef __tree_end_node<__node_base_pointer>                  __end_node_type;
657bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, __end_node_type>
6580b57cec5SDimitry Andric                                                             __end_node_pointer;
6590b57cec5SDimitry Andric#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
6600b57cec5SDimitry Andric  typedef __end_node_pointer __parent_pointer;
6610b57cec5SDimitry Andric#else
662bdd1243dSDimitry Andric  typedef __conditional_t< is_pointer<__end_node_pointer>::value, __end_node_pointer, __node_base_pointer>
663bdd1243dSDimitry Andric      __parent_pointer;
6640b57cec5SDimitry Andric#endif
6650b57cec5SDimitry Andric
6660b57cec5SDimitry Andricprivate:
6670b57cec5SDimitry Andric  static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
6680b57cec5SDimitry Andric                  "_VoidPtr does not point to unqualified void type");
6690b57cec5SDimitry Andric};
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andrictemplate <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
6720b57cec5SDimitry Andric         bool = _KVTypes::__is_map>
6730b57cec5SDimitry Andricstruct __tree_map_pointer_types {};
6740b57cec5SDimitry Andric
6750b57cec5SDimitry Andrictemplate <class _Tp, class _AllocPtr, class _KVTypes>
6760b57cec5SDimitry Andricstruct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
6770b57cec5SDimitry Andric  typedef typename _KVTypes::__map_value_type   _Mv;
678bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_AllocPtr, _Mv>
6790b57cec5SDimitry Andric                                                       __map_value_type_pointer;
680bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_AllocPtr, const _Mv>
6810b57cec5SDimitry Andric                                                 __const_map_value_type_pointer;
6820b57cec5SDimitry Andric};
6830b57cec5SDimitry Andric
6840b57cec5SDimitry Andrictemplate <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
6850b57cec5SDimitry Andricstruct __tree_node_types;
6860b57cec5SDimitry Andric
6870b57cec5SDimitry Andrictemplate <class _NodePtr, class _Tp, class _VoidPtr>
6880b57cec5SDimitry Andricstruct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
6890b57cec5SDimitry Andric    : public __tree_node_base_types<_VoidPtr>,
6900b57cec5SDimitry Andric             __tree_key_value_types<_Tp>,
6910b57cec5SDimitry Andric             __tree_map_pointer_types<_Tp, _VoidPtr>
6920b57cec5SDimitry Andric{
6930b57cec5SDimitry Andric  typedef __tree_node_base_types<_VoidPtr> __base;
6940b57cec5SDimitry Andric  typedef __tree_key_value_types<_Tp>      __key_base;
6950b57cec5SDimitry Andric  typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
6960b57cec5SDimitry Andricpublic:
6970b57cec5SDimitry Andric
6980b57cec5SDimitry Andric  typedef typename pointer_traits<_NodePtr>::element_type       __node_type;
6990b57cec5SDimitry Andric  typedef _NodePtr                                              __node_pointer;
7000b57cec5SDimitry Andric
7010b57cec5SDimitry Andric  typedef _Tp                                                 __node_value_type;
702bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, __node_value_type>
7030b57cec5SDimitry Andric                                                      __node_value_type_pointer;
704bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, const __node_value_type>
7050b57cec5SDimitry Andric                                                __const_node_value_type_pointer;
7060b57cec5SDimitry Andric#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
7070b57cec5SDimitry Andric  typedef typename __base::__end_node_pointer __iter_pointer;
7080b57cec5SDimitry Andric#else
709bdd1243dSDimitry Andric  typedef __conditional_t< is_pointer<__node_pointer>::value, typename __base::__end_node_pointer, __node_pointer>
710bdd1243dSDimitry Andric      __iter_pointer;
7110b57cec5SDimitry Andric#endif
7120b57cec5SDimitry Andricprivate:
7130b57cec5SDimitry Andric    static_assert(!is_const<__node_type>::value,
7140b57cec5SDimitry Andric                "_NodePtr should never be a pointer to const");
715bdd1243dSDimitry Andric    static_assert((is_same<__rebind_pointer_t<_VoidPtr, __node_type>,
7160b57cec5SDimitry Andric                          _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
7170b57cec5SDimitry Andric};
7180b57cec5SDimitry Andric
7190b57cec5SDimitry Andrictemplate <class _ValueTp, class _VoidPtr>
7200b57cec5SDimitry Andricstruct __make_tree_node_types {
721bdd1243dSDimitry Andric  typedef __rebind_pointer_t<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >
7220b57cec5SDimitry Andric                                                                        _NodePtr;
7230b57cec5SDimitry Andric  typedef __tree_node_types<_NodePtr> type;
7240b57cec5SDimitry Andric};
7250b57cec5SDimitry Andric
7260b57cec5SDimitry Andric// node
7270b57cec5SDimitry Andric
7280b57cec5SDimitry Andrictemplate <class _Pointer>
7290b57cec5SDimitry Andricclass __tree_end_node
7300b57cec5SDimitry Andric{
7310b57cec5SDimitry Andricpublic:
7320b57cec5SDimitry Andric    typedef _Pointer pointer;
7330b57cec5SDimitry Andric    pointer __left_;
7340b57cec5SDimitry Andric
735*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
7360b57cec5SDimitry Andric    __tree_end_node() _NOEXCEPT : __left_() {}
7370b57cec5SDimitry Andric};
7380b57cec5SDimitry Andric
7390b57cec5SDimitry Andrictemplate <class _VoidPtr>
740fe6060f1SDimitry Andricclass _LIBCPP_STANDALONE_DEBUG __tree_node_base
7410b57cec5SDimitry Andric    : public __tree_node_base_types<_VoidPtr>::__end_node_type
7420b57cec5SDimitry Andric{
7430b57cec5SDimitry Andric    typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
7440b57cec5SDimitry Andric
7450b57cec5SDimitry Andricpublic:
7460b57cec5SDimitry Andric    typedef typename _NodeBaseTypes::__node_base_pointer pointer;
7470b57cec5SDimitry Andric    typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
7480b57cec5SDimitry Andric
7490b57cec5SDimitry Andric    pointer          __right_;
7500b57cec5SDimitry Andric    __parent_pointer __parent_;
7510b57cec5SDimitry Andric    bool __is_black_;
7520b57cec5SDimitry Andric
753*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
7540b57cec5SDimitry Andric    pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
7550b57cec5SDimitry Andric
756*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
7570b57cec5SDimitry Andric    void __set_parent(pointer __p) {
7580b57cec5SDimitry Andric        __parent_ = static_cast<__parent_pointer>(__p);
7590b57cec5SDimitry Andric    }
7600b57cec5SDimitry Andric
7610b57cec5SDimitry Andricprivate:
762349cc55cSDimitry Andric  ~__tree_node_base() = delete;
763349cc55cSDimitry Andric  __tree_node_base(__tree_node_base const&) = delete;
764349cc55cSDimitry Andric  __tree_node_base& operator=(__tree_node_base const&) = delete;
7650b57cec5SDimitry Andric};
7660b57cec5SDimitry Andric
7670b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr>
768fe6060f1SDimitry Andricclass _LIBCPP_STANDALONE_DEBUG __tree_node
7690b57cec5SDimitry Andric    : public __tree_node_base<_VoidPtr>
7700b57cec5SDimitry Andric{
7710b57cec5SDimitry Andricpublic:
7720b57cec5SDimitry Andric    typedef _Tp __node_value_type;
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric    __node_value_type __value_;
7750b57cec5SDimitry Andric
776*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
777*5f757f3fSDimitry Andric
7780b57cec5SDimitry Andricprivate:
779349cc55cSDimitry Andric  ~__tree_node() = delete;
780349cc55cSDimitry Andric  __tree_node(__tree_node const&) = delete;
781349cc55cSDimitry Andric  __tree_node& operator=(__tree_node const&) = delete;
7820b57cec5SDimitry Andric};
7830b57cec5SDimitry Andric
7840b57cec5SDimitry Andric
7850b57cec5SDimitry Andrictemplate <class _Allocator>
7860b57cec5SDimitry Andricclass __tree_node_destructor
7870b57cec5SDimitry Andric{
7880b57cec5SDimitry Andric    typedef _Allocator                                      allocator_type;
7890b57cec5SDimitry Andric    typedef allocator_traits<allocator_type>                __alloc_traits;
7900b57cec5SDimitry Andric
7910b57cec5SDimitry Andricpublic:
7920b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer                pointer;
7930b57cec5SDimitry Andricprivate:
7940b57cec5SDimitry Andric    typedef __tree_node_types<pointer> _NodeTypes;
7950b57cec5SDimitry Andric    allocator_type& __na_;
7960b57cec5SDimitry Andric
7970b57cec5SDimitry Andric
7980b57cec5SDimitry Andricpublic:
7990b57cec5SDimitry Andric    bool __value_constructed;
8000b57cec5SDimitry Andric
801cd0c3137SDimitry Andric
80206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree_node_destructor(const __tree_node_destructor &) = default;
803cd0c3137SDimitry Andric    __tree_node_destructor& operator=(const __tree_node_destructor&) = delete;
804cd0c3137SDimitry Andric
805*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8060b57cec5SDimitry Andric    explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
8070b57cec5SDimitry Andric        : __na_(__na),
8080b57cec5SDimitry Andric          __value_constructed(__val)
8090b57cec5SDimitry Andric        {}
8100b57cec5SDimitry Andric
811*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8120b57cec5SDimitry Andric    void operator()(pointer __p) _NOEXCEPT
8130b57cec5SDimitry Andric    {
8140b57cec5SDimitry Andric        if (__value_constructed)
8150b57cec5SDimitry Andric            __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
8160b57cec5SDimitry Andric        if (__p)
8170b57cec5SDimitry Andric            __alloc_traits::deallocate(__na_, __p, 1);
8180b57cec5SDimitry Andric    }
8190b57cec5SDimitry Andric
8200b57cec5SDimitry Andric    template <class> friend class __map_node_destructor;
8210b57cec5SDimitry Andric};
8220b57cec5SDimitry Andric
82306c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
8240b57cec5SDimitry Andrictemplate <class _NodeType, class _Alloc>
8250b57cec5SDimitry Andricstruct __generic_container_node_destructor;
8260b57cec5SDimitry Andrictemplate <class _Tp, class _VoidPtr, class _Alloc>
8270b57cec5SDimitry Andricstruct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc>
8280b57cec5SDimitry Andric    : __tree_node_destructor<_Alloc>
8290b57cec5SDimitry Andric{
8300b57cec5SDimitry Andric    using __tree_node_destructor<_Alloc>::__tree_node_destructor;
8310b57cec5SDimitry Andric};
8320b57cec5SDimitry Andric#endif
8330b57cec5SDimitry Andric
8340b57cec5SDimitry Andrictemplate <class _Tp, class _NodePtr, class _DiffType>
8350b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __tree_iterator
8360b57cec5SDimitry Andric{
8370b57cec5SDimitry Andric    typedef __tree_node_types<_NodePtr>                     _NodeTypes;
8380b57cec5SDimitry Andric    typedef _NodePtr                                        __node_pointer;
8390b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_base_pointer        __node_base_pointer;
8400b57cec5SDimitry Andric    typedef typename _NodeTypes::__end_node_pointer         __end_node_pointer;
8410b57cec5SDimitry Andric    typedef typename _NodeTypes::__iter_pointer             __iter_pointer;
8420b57cec5SDimitry Andric    typedef pointer_traits<__node_pointer> __pointer_traits;
8430b57cec5SDimitry Andric
8440b57cec5SDimitry Andric    __iter_pointer __ptr_;
8450b57cec5SDimitry Andric
8460b57cec5SDimitry Andricpublic:
8470b57cec5SDimitry Andric    typedef bidirectional_iterator_tag                     iterator_category;
8480b57cec5SDimitry Andric    typedef _Tp                                            value_type;
8490b57cec5SDimitry Andric    typedef _DiffType                                      difference_type;
8500b57cec5SDimitry Andric    typedef value_type&                                    reference;
8510b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_value_type_pointer pointer;
8520b57cec5SDimitry Andric
853*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT
85406c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
8550b57cec5SDimitry Andric    : __ptr_(nullptr)
8560b57cec5SDimitry Andric#endif
8570b57cec5SDimitry Andric    {}
8580b57cec5SDimitry Andric
859*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI reference operator*() const
8600b57cec5SDimitry Andric        {return __get_np()->__value_;}
861*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI pointer operator->() const
8620b57cec5SDimitry Andric        {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
8630b57cec5SDimitry Andric
864*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8650b57cec5SDimitry Andric    __tree_iterator& operator++() {
8660b57cec5SDimitry Andric      __ptr_ = static_cast<__iter_pointer>(
867*5f757f3fSDimitry Andric          std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
8680b57cec5SDimitry Andric      return *this;
8690b57cec5SDimitry Andric    }
870*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8710b57cec5SDimitry Andric    __tree_iterator operator++(int)
8720b57cec5SDimitry Andric        {__tree_iterator __t(*this); ++(*this); return __t;}
8730b57cec5SDimitry Andric
874*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8750b57cec5SDimitry Andric    __tree_iterator& operator--() {
876*5f757f3fSDimitry Andric      __ptr_ = static_cast<__iter_pointer>(std::__tree_prev_iter<__node_base_pointer>(
8770b57cec5SDimitry Andric          static_cast<__end_node_pointer>(__ptr_)));
8780b57cec5SDimitry Andric      return *this;
8790b57cec5SDimitry Andric    }
880*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8810b57cec5SDimitry Andric    __tree_iterator operator--(int)
8820b57cec5SDimitry Andric        {__tree_iterator __t(*this); --(*this); return __t;}
8830b57cec5SDimitry Andric
884*5f757f3fSDimitry Andric    friend _LIBCPP_HIDE_FROM_ABI
8850b57cec5SDimitry Andric        bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
8860b57cec5SDimitry Andric        {return __x.__ptr_ == __y.__ptr_;}
887*5f757f3fSDimitry Andric    friend _LIBCPP_HIDE_FROM_ABI
8880b57cec5SDimitry Andric        bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
8890b57cec5SDimitry Andric        {return !(__x == __y);}
8900b57cec5SDimitry Andric
8910b57cec5SDimitry Andricprivate:
892*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8930b57cec5SDimitry Andric    explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
894*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8950b57cec5SDimitry Andric    explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
896*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
8970b57cec5SDimitry Andric    __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
8980b57cec5SDimitry Andric    template <class, class, class> friend class __tree;
8990b57cec5SDimitry Andric    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
9000b57cec5SDimitry Andric    template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
9010b57cec5SDimitry Andric    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
9020b57cec5SDimitry Andric    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
9030b57cec5SDimitry Andric    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
9040b57cec5SDimitry Andric    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
9050b57cec5SDimitry Andric};
9060b57cec5SDimitry Andric
9070b57cec5SDimitry Andrictemplate <class _Tp, class _NodePtr, class _DiffType>
9080b57cec5SDimitry Andricclass _LIBCPP_TEMPLATE_VIS __tree_const_iterator
9090b57cec5SDimitry Andric{
9100b57cec5SDimitry Andric    typedef __tree_node_types<_NodePtr>                     _NodeTypes;
9110b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_pointer             __node_pointer;
9120b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_base_pointer        __node_base_pointer;
9130b57cec5SDimitry Andric    typedef typename _NodeTypes::__end_node_pointer         __end_node_pointer;
9140b57cec5SDimitry Andric    typedef typename _NodeTypes::__iter_pointer             __iter_pointer;
9150b57cec5SDimitry Andric    typedef pointer_traits<__node_pointer> __pointer_traits;
9160b57cec5SDimitry Andric
9170b57cec5SDimitry Andric    __iter_pointer __ptr_;
9180b57cec5SDimitry Andric
9190b57cec5SDimitry Andricpublic:
9200b57cec5SDimitry Andric    typedef bidirectional_iterator_tag                           iterator_category;
9210b57cec5SDimitry Andric    typedef _Tp                                                  value_type;
9220b57cec5SDimitry Andric    typedef _DiffType                                            difference_type;
9230b57cec5SDimitry Andric    typedef const value_type&                                    reference;
9240b57cec5SDimitry Andric    typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
9250b57cec5SDimitry Andric
926*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT
92706c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 14
9280b57cec5SDimitry Andric    : __ptr_(nullptr)
9290b57cec5SDimitry Andric#endif
9300b57cec5SDimitry Andric    {}
9310b57cec5SDimitry Andric
9320b57cec5SDimitry Andricprivate:
9330b57cec5SDimitry Andric    typedef __tree_iterator<value_type, __node_pointer, difference_type>
9340b57cec5SDimitry Andric                                                           __non_const_iterator;
9350b57cec5SDimitry Andricpublic:
936*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9370b57cec5SDimitry Andric    __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
9380b57cec5SDimitry Andric        : __ptr_(__p.__ptr_) {}
9390b57cec5SDimitry Andric
940*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI reference operator*() const
9410b57cec5SDimitry Andric        {return __get_np()->__value_;}
942*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI pointer operator->() const
9430b57cec5SDimitry Andric        {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
9440b57cec5SDimitry Andric
945*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9460b57cec5SDimitry Andric    __tree_const_iterator& operator++() {
9470b57cec5SDimitry Andric      __ptr_ = static_cast<__iter_pointer>(
948*5f757f3fSDimitry Andric          std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
9490b57cec5SDimitry Andric      return *this;
9500b57cec5SDimitry Andric    }
9510b57cec5SDimitry Andric
952*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9530b57cec5SDimitry Andric    __tree_const_iterator operator++(int)
9540b57cec5SDimitry Andric        {__tree_const_iterator __t(*this); ++(*this); return __t;}
9550b57cec5SDimitry Andric
956*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9570b57cec5SDimitry Andric    __tree_const_iterator& operator--() {
958*5f757f3fSDimitry Andric      __ptr_ = static_cast<__iter_pointer>(std::__tree_prev_iter<__node_base_pointer>(
9590b57cec5SDimitry Andric          static_cast<__end_node_pointer>(__ptr_)));
9600b57cec5SDimitry Andric      return *this;
9610b57cec5SDimitry Andric    }
9620b57cec5SDimitry Andric
963*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9640b57cec5SDimitry Andric    __tree_const_iterator operator--(int)
9650b57cec5SDimitry Andric        {__tree_const_iterator __t(*this); --(*this); return __t;}
9660b57cec5SDimitry Andric
967*5f757f3fSDimitry Andric    friend _LIBCPP_HIDE_FROM_ABI
9680b57cec5SDimitry Andric        bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
9690b57cec5SDimitry Andric        {return __x.__ptr_ == __y.__ptr_;}
970*5f757f3fSDimitry Andric    friend _LIBCPP_HIDE_FROM_ABI
9710b57cec5SDimitry Andric        bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
9720b57cec5SDimitry Andric        {return !(__x == __y);}
9730b57cec5SDimitry Andric
9740b57cec5SDimitry Andricprivate:
975*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9760b57cec5SDimitry Andric    explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
9770b57cec5SDimitry Andric        : __ptr_(__p) {}
978*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9790b57cec5SDimitry Andric    explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
9800b57cec5SDimitry Andric        : __ptr_(__p) {}
981*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
9820b57cec5SDimitry Andric    __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
9830b57cec5SDimitry Andric
9840b57cec5SDimitry Andric    template <class, class, class> friend class __tree;
9850b57cec5SDimitry Andric    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
9860b57cec5SDimitry Andric    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
9870b57cec5SDimitry Andric    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
9880b57cec5SDimitry Andric    template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
9890b57cec5SDimitry Andric    template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
9900b57cec5SDimitry Andric
9910b57cec5SDimitry Andric};
9920b57cec5SDimitry Andric
9930b57cec5SDimitry Andrictemplate<class _Tp, class _Compare>
9940b57cec5SDimitry Andric#ifndef _LIBCPP_CXX03_LANG
995e8d8bef9SDimitry Andric    _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
9960b57cec5SDimitry Andric        "the specified comparator type does not provide a viable const call operator")
9970b57cec5SDimitry Andric#endif
9980b57cec5SDimitry Andricint __diagnose_non_const_comparator();
9990b57cec5SDimitry Andric
10000b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
10010b57cec5SDimitry Andricclass __tree
10020b57cec5SDimitry Andric{
10030b57cec5SDimitry Andricpublic:
10040b57cec5SDimitry Andric    typedef _Tp                                      value_type;
10050b57cec5SDimitry Andric    typedef _Compare                                 value_compare;
10060b57cec5SDimitry Andric    typedef _Allocator                               allocator_type;
10070b57cec5SDimitry Andric
10080b57cec5SDimitry Andricprivate:
10090b57cec5SDimitry Andric    typedef allocator_traits<allocator_type>         __alloc_traits;
10100b57cec5SDimitry Andric    typedef typename __make_tree_node_types<value_type,
10110b57cec5SDimitry Andric        typename __alloc_traits::void_pointer>::type
10120b57cec5SDimitry Andric                                                    _NodeTypes;
10130b57cec5SDimitry Andric    typedef typename _NodeTypes::key_type           key_type;
10140b57cec5SDimitry Andricpublic:
10150b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_value_type      __node_value_type;
10160b57cec5SDimitry Andric    typedef typename _NodeTypes::__container_value_type __container_value_type;
10170b57cec5SDimitry Andric
10180b57cec5SDimitry Andric    typedef typename __alloc_traits::pointer         pointer;
10190b57cec5SDimitry Andric    typedef typename __alloc_traits::const_pointer   const_pointer;
10200b57cec5SDimitry Andric    typedef typename __alloc_traits::size_type       size_type;
10210b57cec5SDimitry Andric    typedef typename __alloc_traits::difference_type difference_type;
10220b57cec5SDimitry Andric
10230b57cec5SDimitry Andricpublic:
10240b57cec5SDimitry Andric    typedef typename _NodeTypes::__void_pointer        __void_pointer;
10250b57cec5SDimitry Andric
10260b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_type           __node;
10270b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_pointer        __node_pointer;
10280b57cec5SDimitry Andric
10290b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_base_type      __node_base;
10300b57cec5SDimitry Andric    typedef typename _NodeTypes::__node_base_pointer   __node_base_pointer;
10310b57cec5SDimitry Andric
10320b57cec5SDimitry Andric    typedef typename _NodeTypes::__end_node_type       __end_node_t;
10330b57cec5SDimitry Andric    typedef typename _NodeTypes::__end_node_pointer    __end_node_ptr;
10340b57cec5SDimitry Andric
10350b57cec5SDimitry Andric    typedef typename _NodeTypes::__parent_pointer      __parent_pointer;
10360b57cec5SDimitry Andric    typedef typename _NodeTypes::__iter_pointer        __iter_pointer;
10370b57cec5SDimitry Andric
1038bdd1243dSDimitry Andric    typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
10390b57cec5SDimitry Andric    typedef allocator_traits<__node_allocator>         __node_traits;
10400b57cec5SDimitry Andric
10410b57cec5SDimitry Andricprivate:
10420b57cec5SDimitry Andric    // check for sane allocator pointer rebinding semantics. Rebinding the
10430b57cec5SDimitry Andric    // allocator for a new pointer type should be exactly the same as rebinding
10440b57cec5SDimitry Andric    // the pointer using 'pointer_traits'.
10450b57cec5SDimitry Andric    static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
10460b57cec5SDimitry Andric                  "Allocator does not rebind pointers in a sane manner.");
1047bdd1243dSDimitry Andric    typedef __rebind_alloc<__node_traits, __node_base> __node_base_allocator;
10480b57cec5SDimitry Andric    typedef allocator_traits<__node_base_allocator> __node_base_traits;
10490b57cec5SDimitry Andric    static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
10500b57cec5SDimitry Andric                 "Allocator does not rebind pointers in a sane manner.");
10510b57cec5SDimitry Andric
10520b57cec5SDimitry Andricprivate:
10530b57cec5SDimitry Andric    __iter_pointer                                     __begin_node_;
10540b57cec5SDimitry Andric    __compressed_pair<__end_node_t, __node_allocator>  __pair1_;
10550b57cec5SDimitry Andric    __compressed_pair<size_type, value_compare>        __pair3_;
10560b57cec5SDimitry Andric
10570b57cec5SDimitry Andricpublic:
1058*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10590b57cec5SDimitry Andric    __iter_pointer __end_node() _NOEXCEPT
10600b57cec5SDimitry Andric    {
10610b57cec5SDimitry Andric        return static_cast<__iter_pointer>(
10620b57cec5SDimitry Andric                pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
10630b57cec5SDimitry Andric        );
10640b57cec5SDimitry Andric    }
1065*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10660b57cec5SDimitry Andric    __iter_pointer __end_node() const _NOEXCEPT
10670b57cec5SDimitry Andric    {
10680b57cec5SDimitry Andric        return static_cast<__iter_pointer>(
10690b57cec5SDimitry Andric            pointer_traits<__end_node_ptr>::pointer_to(
10700b57cec5SDimitry Andric                const_cast<__end_node_t&>(__pair1_.first())
10710b57cec5SDimitry Andric            )
10720b57cec5SDimitry Andric        );
10730b57cec5SDimitry Andric    }
1074*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10750b57cec5SDimitry Andric          __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
10760b57cec5SDimitry Andricprivate:
1077*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10780b57cec5SDimitry Andric    const __node_allocator& __node_alloc() const _NOEXCEPT
10790b57cec5SDimitry Andric        {return __pair1_.second();}
1080*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10810b57cec5SDimitry Andric          __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
1082*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10830b57cec5SDimitry Andric    const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
10840b57cec5SDimitry Andricpublic:
1085*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10860b57cec5SDimitry Andric    allocator_type __alloc() const _NOEXCEPT
10870b57cec5SDimitry Andric        {return allocator_type(__node_alloc());}
10880b57cec5SDimitry Andricprivate:
1089*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10900b57cec5SDimitry Andric          size_type& size() _NOEXCEPT {return __pair3_.first();}
10910b57cec5SDimitry Andricpublic:
1092*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10930b57cec5SDimitry Andric    const size_type& size() const _NOEXCEPT {return __pair3_.first();}
1094*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10950b57cec5SDimitry Andric          value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
1096*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
10970b57cec5SDimitry Andric    const value_compare& value_comp() const _NOEXCEPT
10980b57cec5SDimitry Andric        {return __pair3_.second();}
10990b57cec5SDimitry Andricpublic:
11000b57cec5SDimitry Andric
1101*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11020b57cec5SDimitry Andric    __node_pointer __root() const _NOEXCEPT
11030b57cec5SDimitry Andric        {return static_cast<__node_pointer>(__end_node()->__left_);}
11040b57cec5SDimitry Andric
110506c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer* __root_ptr() const _NOEXCEPT {
1106*5f757f3fSDimitry Andric        return std::addressof(__end_node()->__left_);
11070b57cec5SDimitry Andric    }
11080b57cec5SDimitry Andric
11090b57cec5SDimitry Andric    typedef __tree_iterator<value_type, __node_pointer, difference_type>             iterator;
11100b57cec5SDimitry Andric    typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
11110b57cec5SDimitry Andric
111206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI explicit __tree(const value_compare& __comp)
11130b57cec5SDimitry Andric        _NOEXCEPT_(
11140b57cec5SDimitry Andric            is_nothrow_default_constructible<__node_allocator>::value &&
11150b57cec5SDimitry Andric            is_nothrow_copy_constructible<value_compare>::value);
111606c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI explicit __tree(const allocator_type& __a);
111706c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree(const value_compare& __comp, const allocator_type& __a);
111806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree(const __tree& __t);
111906c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree& operator=(const __tree& __t);
11200b57cec5SDimitry Andric    template <class _ForwardIterator>
112106c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void __assign_unique(_ForwardIterator __first, _ForwardIterator __last);
11220b57cec5SDimitry Andric    template <class _InputIterator>
112306c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void __assign_multi(_InputIterator __first, _InputIterator __last);
112406c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t)
11250b57cec5SDimitry Andric        _NOEXCEPT_(
11260b57cec5SDimitry Andric            is_nothrow_move_constructible<__node_allocator>::value &&
11270b57cec5SDimitry Andric            is_nothrow_move_constructible<value_compare>::value);
112806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t, const allocator_type& __a);
112906c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __tree& operator=(__tree&& __t)
11300b57cec5SDimitry Andric        _NOEXCEPT_(
11310b57cec5SDimitry Andric            __node_traits::propagate_on_container_move_assignment::value &&
11320b57cec5SDimitry Andric            is_nothrow_move_assignable<value_compare>::value &&
11330b57cec5SDimitry Andric            is_nothrow_move_assignable<__node_allocator>::value);
113406c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI ~__tree();
11350b57cec5SDimitry Andric
1136*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11370b57cec5SDimitry Andric          iterator begin()  _NOEXCEPT {return       iterator(__begin_node());}
1138*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11390b57cec5SDimitry Andric    const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
1140*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11410b57cec5SDimitry Andric          iterator end() _NOEXCEPT {return       iterator(__end_node());}
1142*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11430b57cec5SDimitry Andric    const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
11440b57cec5SDimitry Andric
1145*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11460b57cec5SDimitry Andric    size_type max_size() const _NOEXCEPT
1147*5f757f3fSDimitry Andric        {return std::min<size_type>(
11480b57cec5SDimitry Andric                __node_traits::max_size(__node_alloc()),
11490b57cec5SDimitry Andric                numeric_limits<difference_type >::max());}
11500b57cec5SDimitry Andric
115106c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
11520b57cec5SDimitry Andric
115306c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void swap(__tree& __t)
11540b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 11
11550b57cec5SDimitry Andric        _NOEXCEPT_(
11560b57cec5SDimitry Andric            __is_nothrow_swappable<value_compare>::value
11570b57cec5SDimitry Andric            && (!__node_traits::propagate_on_container_swap::value ||
11580b57cec5SDimitry Andric                 __is_nothrow_swappable<__node_allocator>::value)
11590b57cec5SDimitry Andric            );
11600b57cec5SDimitry Andric#else
11610b57cec5SDimitry Andric        _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
11620b57cec5SDimitry Andric#endif
11630b57cec5SDimitry Andric
11640b57cec5SDimitry Andric    template <class _Key, class ..._Args>
116506c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<iterator, bool>
11660b57cec5SDimitry Andric    __emplace_unique_key_args(_Key const&, _Args&&... __args);
11670b57cec5SDimitry Andric    template <class _Key, class ..._Args>
116806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<iterator, bool>
11690b57cec5SDimitry Andric    __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
11700b57cec5SDimitry Andric
11710b57cec5SDimitry Andric    template <class... _Args>
117206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
11730b57cec5SDimitry Andric
11740b57cec5SDimitry Andric    template <class... _Args>
117506c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
11760b57cec5SDimitry Andric
11770b57cec5SDimitry Andric    template <class... _Args>
117806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator __emplace_multi(_Args&&... __args);
11790b57cec5SDimitry Andric
11800b57cec5SDimitry Andric    template <class... _Args>
118106c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
11820b57cec5SDimitry Andric
11830b57cec5SDimitry Andric    template <class _Pp>
1184*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
11850b57cec5SDimitry Andric    pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1186*5f757f3fSDimitry Andric        return __emplace_unique_extract_key(std::forward<_Pp>(__x),
11870b57cec5SDimitry Andric                                            __can_extract_key<_Pp, key_type>());
11880b57cec5SDimitry Andric    }
11890b57cec5SDimitry Andric
1190*5f757f3fSDimitry Andric    template <class _First, class _Second,
1191*5f757f3fSDimitry Andric              __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0>
1192*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
1193*5f757f3fSDimitry Andric    pair<iterator, bool>
1194753f127fSDimitry Andric    __emplace_unique(_First&& __f, _Second&& __s) {
1195*5f757f3fSDimitry Andric        return __emplace_unique_key_args(__f, std::forward<_First>(__f),
1196*5f757f3fSDimitry Andric                                              std::forward<_Second>(__s));
11970b57cec5SDimitry Andric    }
11980b57cec5SDimitry Andric
11990b57cec5SDimitry Andric    template <class... _Args>
1200*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12010b57cec5SDimitry Andric    pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1202*5f757f3fSDimitry Andric        return __emplace_unique_impl(std::forward<_Args>(__args)...);
12030b57cec5SDimitry Andric    }
12040b57cec5SDimitry Andric
12050b57cec5SDimitry Andric    template <class _Pp>
1206*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12070b57cec5SDimitry Andric    pair<iterator, bool>
12080b57cec5SDimitry Andric    __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1209*5f757f3fSDimitry Andric      return __emplace_unique_impl(std::forward<_Pp>(__x));
12100b57cec5SDimitry Andric    }
12110b57cec5SDimitry Andric
12120b57cec5SDimitry Andric    template <class _Pp>
1213*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12140b57cec5SDimitry Andric    pair<iterator, bool>
12150b57cec5SDimitry Andric    __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1216*5f757f3fSDimitry Andric      return __emplace_unique_key_args(__x, std::forward<_Pp>(__x));
12170b57cec5SDimitry Andric    }
12180b57cec5SDimitry Andric
12190b57cec5SDimitry Andric    template <class _Pp>
1220*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12210b57cec5SDimitry Andric    pair<iterator, bool>
12220b57cec5SDimitry Andric    __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1223*5f757f3fSDimitry Andric      return __emplace_unique_key_args(__x.first, std::forward<_Pp>(__x));
12240b57cec5SDimitry Andric    }
12250b57cec5SDimitry Andric
12260b57cec5SDimitry Andric    template <class _Pp>
1227*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12280b57cec5SDimitry Andric    iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1229*5f757f3fSDimitry Andric        return __emplace_hint_unique_extract_key(__p, std::forward<_Pp>(__x),
12300b57cec5SDimitry Andric                                            __can_extract_key<_Pp, key_type>());
12310b57cec5SDimitry Andric    }
12320b57cec5SDimitry Andric
1233*5f757f3fSDimitry Andric    template <class _First, class _Second,
1234*5f757f3fSDimitry Andric              __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0>
1235*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
1236*5f757f3fSDimitry Andric    iterator
1237753f127fSDimitry Andric    __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
12380b57cec5SDimitry Andric        return __emplace_hint_unique_key_args(__p, __f,
1239*5f757f3fSDimitry Andric                                              std::forward<_First>(__f),
1240*5f757f3fSDimitry Andric                                              std::forward<_Second>(__s)).first;
12410b57cec5SDimitry Andric    }
12420b57cec5SDimitry Andric
12430b57cec5SDimitry Andric    template <class... _Args>
1244*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12450b57cec5SDimitry Andric    iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1246*5f757f3fSDimitry Andric        return __emplace_hint_unique_impl(__p, std::forward<_Args>(__args)...);
12470b57cec5SDimitry Andric    }
12480b57cec5SDimitry Andric
12490b57cec5SDimitry Andric    template <class _Pp>
1250*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12510b57cec5SDimitry Andric    iterator
12520b57cec5SDimitry Andric    __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1253*5f757f3fSDimitry Andric      return __emplace_hint_unique_impl(__p, std::forward<_Pp>(__x));
12540b57cec5SDimitry Andric    }
12550b57cec5SDimitry Andric
12560b57cec5SDimitry Andric    template <class _Pp>
1257*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12580b57cec5SDimitry Andric    iterator
12590b57cec5SDimitry Andric    __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1260*5f757f3fSDimitry Andric      return __emplace_hint_unique_key_args(__p, __x, std::forward<_Pp>(__x)).first;
12610b57cec5SDimitry Andric    }
12620b57cec5SDimitry Andric
12630b57cec5SDimitry Andric    template <class _Pp>
1264*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12650b57cec5SDimitry Andric    iterator
12660b57cec5SDimitry Andric    __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1267*5f757f3fSDimitry Andric      return __emplace_hint_unique_key_args(__p, __x.first, std::forward<_Pp>(__x)).first;
12680b57cec5SDimitry Andric    }
12690b57cec5SDimitry Andric
1270*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12710b57cec5SDimitry Andric    pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
12720b57cec5SDimitry Andric        return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
12730b57cec5SDimitry Andric    }
12740b57cec5SDimitry Andric
1275*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12760b57cec5SDimitry Andric    iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1277e8d8bef9SDimitry Andric        return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v).first;
12780b57cec5SDimitry Andric    }
12790b57cec5SDimitry Andric
1280*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12810b57cec5SDimitry Andric    pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1282*5f757f3fSDimitry Andric        return __emplace_unique_key_args(_NodeTypes::__get_key(__v), std::move(__v));
12830b57cec5SDimitry Andric    }
12840b57cec5SDimitry Andric
1285*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12860b57cec5SDimitry Andric    iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1287*5f757f3fSDimitry Andric        return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), std::move(__v)).first;
12880b57cec5SDimitry Andric    }
12890b57cec5SDimitry Andric
1290753f127fSDimitry Andric    template <class _Vp,
1291bdd1243dSDimitry Andric              class = __enable_if_t<!is_same<__remove_const_ref_t<_Vp>, __container_value_type>::value> >
1292*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
12930b57cec5SDimitry Andric    pair<iterator, bool> __insert_unique(_Vp&& __v) {
1294*5f757f3fSDimitry Andric        return __emplace_unique(std::forward<_Vp>(__v));
12950b57cec5SDimitry Andric    }
12960b57cec5SDimitry Andric
1297753f127fSDimitry Andric    template <class _Vp,
1298bdd1243dSDimitry Andric              class = __enable_if_t<!is_same<__remove_const_ref_t<_Vp>, __container_value_type>::value> >
1299*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13000b57cec5SDimitry Andric    iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1301*5f757f3fSDimitry Andric        return __emplace_hint_unique(__p, std::forward<_Vp>(__v));
13020b57cec5SDimitry Andric    }
13030b57cec5SDimitry Andric
1304*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13050b57cec5SDimitry Andric    iterator __insert_multi(__container_value_type&& __v) {
1306*5f757f3fSDimitry Andric        return __emplace_multi(std::move(__v));
13070b57cec5SDimitry Andric    }
13080b57cec5SDimitry Andric
1309*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13100b57cec5SDimitry Andric    iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1311*5f757f3fSDimitry Andric        return __emplace_hint_multi(__p, std::move(__v));
13120b57cec5SDimitry Andric    }
13130b57cec5SDimitry Andric
13140b57cec5SDimitry Andric    template <class _Vp>
1315*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13160b57cec5SDimitry Andric    iterator __insert_multi(_Vp&& __v) {
1317*5f757f3fSDimitry Andric        return __emplace_multi(std::forward<_Vp>(__v));
13180b57cec5SDimitry Andric    }
13190b57cec5SDimitry Andric
13200b57cec5SDimitry Andric    template <class _Vp>
1321*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13220b57cec5SDimitry Andric    iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1323*5f757f3fSDimitry Andric        return __emplace_hint_multi(__p, std::forward<_Vp>(__v));
13240b57cec5SDimitry Andric    }
13250b57cec5SDimitry Andric
1326*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13270b57cec5SDimitry Andric    pair<iterator, bool> __node_assign_unique(const __container_value_type& __v, __node_pointer __dest);
13280b57cec5SDimitry Andric
1329*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13300b57cec5SDimitry Andric    iterator __node_insert_multi(__node_pointer __nd);
1331*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13320b57cec5SDimitry Andric    iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
13330b57cec5SDimitry Andric
13340b57cec5SDimitry Andric
1335*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator
13360b57cec5SDimitry Andric    __remove_node_pointer(__node_pointer) _NOEXCEPT;
13370b57cec5SDimitry Andric
133806c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
13390b57cec5SDimitry Andric    template <class _NodeHandle, class _InsertReturnType>
1340*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13410b57cec5SDimitry Andric    _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
13420b57cec5SDimitry Andric    template <class _NodeHandle>
1343*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13440b57cec5SDimitry Andric    iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
13450b57cec5SDimitry Andric    template <class _Tree>
1346*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13470b57cec5SDimitry Andric    void __node_handle_merge_unique(_Tree& __source);
13480b57cec5SDimitry Andric
13490b57cec5SDimitry Andric    template <class _NodeHandle>
1350*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13510b57cec5SDimitry Andric    iterator __node_handle_insert_multi(_NodeHandle&&);
13520b57cec5SDimitry Andric    template <class _NodeHandle>
1353*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13540b57cec5SDimitry Andric    iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
13550b57cec5SDimitry Andric    template <class _Tree>
1356*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13570b57cec5SDimitry Andric    void __node_handle_merge_multi(_Tree& __source);
13580b57cec5SDimitry Andric
13590b57cec5SDimitry Andric
13600b57cec5SDimitry Andric    template <class _NodeHandle>
1361*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13620b57cec5SDimitry Andric    _NodeHandle __node_handle_extract(key_type const&);
13630b57cec5SDimitry Andric    template <class _NodeHandle>
1364*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
13650b57cec5SDimitry Andric    _NodeHandle __node_handle_extract(const_iterator);
13660b57cec5SDimitry Andric#endif
13670b57cec5SDimitry Andric
136806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
136906c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
13700b57cec5SDimitry Andric    template <class _Key>
137106c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI size_type __erase_unique(const _Key& __k);
13720b57cec5SDimitry Andric    template <class _Key>
137306c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI size_type __erase_multi(const _Key& __k);
13740b57cec5SDimitry Andric
137506c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void __insert_node_at(__parent_pointer     __parent,
13760b57cec5SDimitry Andric                          __node_base_pointer& __child,
13770b57cec5SDimitry Andric                          __node_base_pointer __new_node) _NOEXCEPT;
13780b57cec5SDimitry Andric
13790b57cec5SDimitry Andric    template <class _Key>
138006c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __v);
13810b57cec5SDimitry Andric    template <class _Key>
138206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __v) const;
13830b57cec5SDimitry Andric
13840b57cec5SDimitry Andric    template <class _Key>
138506c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const;
13860b57cec5SDimitry Andric    template <class _Key>
138706c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const;
13880b57cec5SDimitry Andric
13890b57cec5SDimitry Andric    template <class _Key>
1390*5f757f3fSDimitry Andric        _LIBCPP_HIDE_FROM_ABI
13910b57cec5SDimitry Andric        iterator lower_bound(const _Key& __v)
13920b57cec5SDimitry Andric            {return __lower_bound(__v, __root(), __end_node());}
13930b57cec5SDimitry Andric    template <class _Key>
139406c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator __lower_bound(const _Key& __v,
13950b57cec5SDimitry Andric                               __node_pointer __root,
13960b57cec5SDimitry Andric                               __iter_pointer __result);
13970b57cec5SDimitry Andric    template <class _Key>
1398*5f757f3fSDimitry Andric        _LIBCPP_HIDE_FROM_ABI
13990b57cec5SDimitry Andric        const_iterator lower_bound(const _Key& __v) const
14000b57cec5SDimitry Andric            {return __lower_bound(__v, __root(), __end_node());}
14010b57cec5SDimitry Andric    template <class _Key>
140206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI const_iterator __lower_bound(const _Key& __v,
14030b57cec5SDimitry Andric                                     __node_pointer __root,
14040b57cec5SDimitry Andric                                     __iter_pointer __result) const;
14050b57cec5SDimitry Andric    template <class _Key>
1406*5f757f3fSDimitry Andric        _LIBCPP_HIDE_FROM_ABI
14070b57cec5SDimitry Andric        iterator upper_bound(const _Key& __v)
14080b57cec5SDimitry Andric            {return __upper_bound(__v, __root(), __end_node());}
14090b57cec5SDimitry Andric    template <class _Key>
141006c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI iterator __upper_bound(const _Key& __v,
14110b57cec5SDimitry Andric                               __node_pointer __root,
14120b57cec5SDimitry Andric                               __iter_pointer __result);
14130b57cec5SDimitry Andric    template <class _Key>
1414*5f757f3fSDimitry Andric        _LIBCPP_HIDE_FROM_ABI
14150b57cec5SDimitry Andric        const_iterator upper_bound(const _Key& __v) const
14160b57cec5SDimitry Andric            {return __upper_bound(__v, __root(), __end_node());}
14170b57cec5SDimitry Andric    template <class _Key>
141806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI const_iterator __upper_bound(const _Key& __v,
14190b57cec5SDimitry Andric                                     __node_pointer __root,
14200b57cec5SDimitry Andric                                     __iter_pointer __result) const;
14210b57cec5SDimitry Andric    template <class _Key>
142206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator>
14230b57cec5SDimitry Andric        __equal_range_unique(const _Key& __k);
14240b57cec5SDimitry Andric    template <class _Key>
142506c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator>
14260b57cec5SDimitry Andric        __equal_range_unique(const _Key& __k) const;
14270b57cec5SDimitry Andric
14280b57cec5SDimitry Andric    template <class _Key>
142906c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator>
14300b57cec5SDimitry Andric        __equal_range_multi(const _Key& __k);
14310b57cec5SDimitry Andric    template <class _Key>
143206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator>
14330b57cec5SDimitry Andric        __equal_range_multi(const _Key& __k) const;
14340b57cec5SDimitry Andric
14350b57cec5SDimitry Andric    typedef __tree_node_destructor<__node_allocator> _Dp;
14360b57cec5SDimitry Andric    typedef unique_ptr<__node, _Dp> __node_holder;
14370b57cec5SDimitry Andric
143806c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_holder remove(const_iterator __p) _NOEXCEPT;
14390b57cec5SDimitry Andricprivate:
144006c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
144106c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
144206c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer&
144306c3fb27SDimitry Andric    __find_leaf(const_iterator __hint, __parent_pointer& __parent, const key_type& __v);
1444e8d8bef9SDimitry Andric    // FIXME: Make this function const qualified. Unfortunately doing so
14450b57cec5SDimitry Andric    // breaks existing code which uses non-const callable comparators.
14460b57cec5SDimitry Andric    template <class _Key>
144706c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_equal(__parent_pointer& __parent, const _Key& __v);
14480b57cec5SDimitry Andric    template <class _Key>
1449*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer&
14500b57cec5SDimitry Andric    __find_equal(__parent_pointer& __parent, const _Key& __v) const {
14510b57cec5SDimitry Andric      return const_cast<__tree*>(this)->__find_equal(__parent, __v);
14520b57cec5SDimitry Andric    }
14530b57cec5SDimitry Andric    template <class _Key>
145406c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_base_pointer&
14550b57cec5SDimitry Andric        __find_equal(const_iterator __hint, __parent_pointer& __parent,
14560b57cec5SDimitry Andric                     __node_base_pointer& __dummy,
14570b57cec5SDimitry Andric                     const _Key& __v);
14580b57cec5SDimitry Andric
14590b57cec5SDimitry Andric    template <class ..._Args>
146006c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(_Args&& ...__args);
14610b57cec5SDimitry Andric
146206c3fb27SDimitry Andric    // TODO: Make this _LIBCPP_HIDE_FROM_ABI
146306c3fb27SDimitry Andric    _LIBCPP_HIDDEN void destroy(__node_pointer __nd) _NOEXCEPT;
14640b57cec5SDimitry Andric
1465*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
14660b57cec5SDimitry Andric    void __copy_assign_alloc(const __tree& __t)
14670b57cec5SDimitry Andric        {__copy_assign_alloc(__t, integral_constant<bool,
14680b57cec5SDimitry Andric             __node_traits::propagate_on_container_copy_assignment::value>());}
14690b57cec5SDimitry Andric
1470*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
14710b57cec5SDimitry Andric    void __copy_assign_alloc(const __tree& __t, true_type)
14720b57cec5SDimitry Andric        {
14730b57cec5SDimitry Andric        if (__node_alloc() != __t.__node_alloc())
14740b57cec5SDimitry Andric            clear();
14750b57cec5SDimitry Andric        __node_alloc() = __t.__node_alloc();
14760b57cec5SDimitry Andric        }
1477*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
14780b57cec5SDimitry Andric    void __copy_assign_alloc(const __tree&, false_type) {}
14790b57cec5SDimitry Andric
148006c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, false_type);
148106c3fb27SDimitry Andric    _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, true_type)
14820b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
14830b57cec5SDimitry Andric                   is_nothrow_move_assignable<__node_allocator>::value);
14840b57cec5SDimitry Andric
1485*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
14860b57cec5SDimitry Andric    void __move_assign_alloc(__tree& __t)
14870b57cec5SDimitry Andric        _NOEXCEPT_(
14880b57cec5SDimitry Andric            !__node_traits::propagate_on_container_move_assignment::value ||
14890b57cec5SDimitry Andric            is_nothrow_move_assignable<__node_allocator>::value)
14900b57cec5SDimitry Andric        {__move_assign_alloc(__t, integral_constant<bool,
14910b57cec5SDimitry Andric             __node_traits::propagate_on_container_move_assignment::value>());}
14920b57cec5SDimitry Andric
1493*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
14940b57cec5SDimitry Andric    void __move_assign_alloc(__tree& __t, true_type)
14950b57cec5SDimitry Andric        _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1496*5f757f3fSDimitry Andric        {__node_alloc() = std::move(__t.__node_alloc());}
1497*5f757f3fSDimitry Andric    _LIBCPP_HIDE_FROM_ABI
14980b57cec5SDimitry Andric    void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
14990b57cec5SDimitry Andric
15000b57cec5SDimitry Andric    struct _DetachedTreeCache {
1501*5f757f3fSDimitry Andric      _LIBCPP_HIDE_FROM_ABI
15020b57cec5SDimitry Andric      explicit _DetachedTreeCache(__tree *__t) _NOEXCEPT : __t_(__t),
15030b57cec5SDimitry Andric        __cache_root_(__detach_from_tree(__t)) {
15040b57cec5SDimitry Andric          __advance();
15050b57cec5SDimitry Andric        }
15060b57cec5SDimitry Andric
1507*5f757f3fSDimitry Andric      _LIBCPP_HIDE_FROM_ABI
15080b57cec5SDimitry Andric      __node_pointer __get() const _NOEXCEPT {
15090b57cec5SDimitry Andric        return __cache_elem_;
15100b57cec5SDimitry Andric      }
15110b57cec5SDimitry Andric
1512*5f757f3fSDimitry Andric      _LIBCPP_HIDE_FROM_ABI
15130b57cec5SDimitry Andric      void __advance() _NOEXCEPT {
15140b57cec5SDimitry Andric        __cache_elem_ = __cache_root_;
15150b57cec5SDimitry Andric        if (__cache_root_) {
15160b57cec5SDimitry Andric          __cache_root_ = __detach_next(__cache_root_);
15170b57cec5SDimitry Andric        }
15180b57cec5SDimitry Andric      }
15190b57cec5SDimitry Andric
1520*5f757f3fSDimitry Andric      _LIBCPP_HIDE_FROM_ABI
15210b57cec5SDimitry Andric      ~_DetachedTreeCache() {
15220b57cec5SDimitry Andric        __t_->destroy(__cache_elem_);
15230b57cec5SDimitry Andric        if (__cache_root_) {
15240b57cec5SDimitry Andric          while (__cache_root_->__parent_ != nullptr)
15250b57cec5SDimitry Andric            __cache_root_ = static_cast<__node_pointer>(__cache_root_->__parent_);
15260b57cec5SDimitry Andric          __t_->destroy(__cache_root_);
15270b57cec5SDimitry Andric        }
15280b57cec5SDimitry Andric      }
15290b57cec5SDimitry Andric
15300b57cec5SDimitry Andric       _DetachedTreeCache(_DetachedTreeCache const&) = delete;
15310b57cec5SDimitry Andric       _DetachedTreeCache& operator=(_DetachedTreeCache const&) = delete;
15320b57cec5SDimitry Andric
15330b57cec5SDimitry Andric    private:
1534*5f757f3fSDimitry Andric      _LIBCPP_HIDE_FROM_ABI
15350b57cec5SDimitry Andric      static __node_pointer __detach_from_tree(__tree *__t) _NOEXCEPT;
1536*5f757f3fSDimitry Andric      _LIBCPP_HIDE_FROM_ABI
15370b57cec5SDimitry Andric      static __node_pointer __detach_next(__node_pointer) _NOEXCEPT;
15380b57cec5SDimitry Andric
15390b57cec5SDimitry Andric      __tree *__t_;
15400b57cec5SDimitry Andric      __node_pointer __cache_root_;
15410b57cec5SDimitry Andric      __node_pointer __cache_elem_;
15420b57cec5SDimitry Andric    };
15430b57cec5SDimitry Andric
15440b57cec5SDimitry Andric
15450b57cec5SDimitry Andric    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
15460b57cec5SDimitry Andric    template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
15470b57cec5SDimitry Andric};
15480b57cec5SDimitry Andric
15490b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
15500b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
15510b57cec5SDimitry Andric        _NOEXCEPT_(
15520b57cec5SDimitry Andric            is_nothrow_default_constructible<__node_allocator>::value &&
15530b57cec5SDimitry Andric            is_nothrow_copy_constructible<value_compare>::value)
15540b57cec5SDimitry Andric    : __pair3_(0, __comp)
15550b57cec5SDimitry Andric{
15560b57cec5SDimitry Andric    __begin_node() = __end_node();
15570b57cec5SDimitry Andric}
15580b57cec5SDimitry Andric
15590b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
15600b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
15610b57cec5SDimitry Andric    : __begin_node_(__iter_pointer()),
1562480093f4SDimitry Andric      __pair1_(__default_init_tag(), __node_allocator(__a)),
1563480093f4SDimitry Andric      __pair3_(0, __default_init_tag())
15640b57cec5SDimitry Andric{
15650b57cec5SDimitry Andric    __begin_node() = __end_node();
15660b57cec5SDimitry Andric}
15670b57cec5SDimitry Andric
15680b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
15690b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
15700b57cec5SDimitry Andric                                           const allocator_type& __a)
15710b57cec5SDimitry Andric    : __begin_node_(__iter_pointer()),
1572480093f4SDimitry Andric      __pair1_(__default_init_tag(), __node_allocator(__a)),
15730b57cec5SDimitry Andric      __pair3_(0, __comp)
15740b57cec5SDimitry Andric{
15750b57cec5SDimitry Andric    __begin_node() = __end_node();
15760b57cec5SDimitry Andric}
15770b57cec5SDimitry Andric
15780b57cec5SDimitry Andric// Precondition:  size() != 0
15790b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
15800b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_pointer
15810b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_from_tree(__tree *__t) _NOEXCEPT
15820b57cec5SDimitry Andric{
15830b57cec5SDimitry Andric    __node_pointer __cache = static_cast<__node_pointer>(__t->__begin_node());
15840b57cec5SDimitry Andric    __t->__begin_node() = __t->__end_node();
15850b57cec5SDimitry Andric    __t->__end_node()->__left_->__parent_ = nullptr;
15860b57cec5SDimitry Andric    __t->__end_node()->__left_ = nullptr;
15870b57cec5SDimitry Andric    __t->size() = 0;
15880b57cec5SDimitry Andric    // __cache->__left_ == nullptr
15890b57cec5SDimitry Andric    if (__cache->__right_ != nullptr)
15900b57cec5SDimitry Andric        __cache = static_cast<__node_pointer>(__cache->__right_);
15910b57cec5SDimitry Andric    // __cache->__left_ == nullptr
15920b57cec5SDimitry Andric    // __cache->__right_ == nullptr
15930b57cec5SDimitry Andric    return __cache;
15940b57cec5SDimitry Andric}
15950b57cec5SDimitry Andric
15960b57cec5SDimitry Andric// Precondition:  __cache != nullptr
15970b57cec5SDimitry Andric//    __cache->left_ == nullptr
15980b57cec5SDimitry Andric//    __cache->right_ == nullptr
15990b57cec5SDimitry Andric//    This is no longer a red-black tree
16000b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
16010b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_pointer
16020b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_next(__node_pointer __cache) _NOEXCEPT
16030b57cec5SDimitry Andric{
16040b57cec5SDimitry Andric    if (__cache->__parent_ == nullptr)
16050b57cec5SDimitry Andric        return nullptr;
1606*5f757f3fSDimitry Andric    if (std::__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
16070b57cec5SDimitry Andric    {
16080b57cec5SDimitry Andric        __cache->__parent_->__left_ = nullptr;
16090b57cec5SDimitry Andric        __cache = static_cast<__node_pointer>(__cache->__parent_);
16100b57cec5SDimitry Andric        if (__cache->__right_ == nullptr)
16110b57cec5SDimitry Andric            return __cache;
1612*5f757f3fSDimitry Andric        return static_cast<__node_pointer>(std::__tree_leaf(__cache->__right_));
16130b57cec5SDimitry Andric    }
16140b57cec5SDimitry Andric    // __cache is right child
16150b57cec5SDimitry Andric    __cache->__parent_unsafe()->__right_ = nullptr;
16160b57cec5SDimitry Andric    __cache = static_cast<__node_pointer>(__cache->__parent_);
16170b57cec5SDimitry Andric    if (__cache->__left_ == nullptr)
16180b57cec5SDimitry Andric        return __cache;
1619*5f757f3fSDimitry Andric    return static_cast<__node_pointer>(std::__tree_leaf(__cache->__left_));
16200b57cec5SDimitry Andric}
16210b57cec5SDimitry Andric
16220b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
16230b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>&
16240b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
16250b57cec5SDimitry Andric{
1626*5f757f3fSDimitry Andric    if (this != std::addressof(__t))
16270b57cec5SDimitry Andric    {
16280b57cec5SDimitry Andric        value_comp() = __t.value_comp();
16290b57cec5SDimitry Andric        __copy_assign_alloc(__t);
16300b57cec5SDimitry Andric        __assign_multi(__t.begin(), __t.end());
16310b57cec5SDimitry Andric    }
16320b57cec5SDimitry Andric    return *this;
16330b57cec5SDimitry Andric}
16340b57cec5SDimitry Andric
16350b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
16360b57cec5SDimitry Andrictemplate <class _ForwardIterator>
16370b57cec5SDimitry Andricvoid
16380b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__assign_unique(_ForwardIterator __first, _ForwardIterator __last)
16390b57cec5SDimitry Andric{
16400b57cec5SDimitry Andric    typedef iterator_traits<_ForwardIterator> _ITraits;
16410b57cec5SDimitry Andric    typedef typename _ITraits::value_type _ItValueType;
16420b57cec5SDimitry Andric    static_assert((is_same<_ItValueType, __container_value_type>::value),
16430b57cec5SDimitry Andric                  "__assign_unique may only be called with the containers value type");
164406c3fb27SDimitry Andric    static_assert(__has_forward_iterator_category<_ForwardIterator>::value,
16450b57cec5SDimitry Andric                  "__assign_unique requires a forward iterator");
16460b57cec5SDimitry Andric    if (size() != 0)
16470b57cec5SDimitry Andric    {
16480b57cec5SDimitry Andric        _DetachedTreeCache __cache(this);
16490b57cec5SDimitry Andric          for (; __cache.__get() != nullptr && __first != __last; ++__first) {
16500b57cec5SDimitry Andric              if (__node_assign_unique(*__first, __cache.__get()).second)
16510b57cec5SDimitry Andric                  __cache.__advance();
16520b57cec5SDimitry Andric            }
16530b57cec5SDimitry Andric    }
16540b57cec5SDimitry Andric    for (; __first != __last; ++__first)
16550b57cec5SDimitry Andric        __insert_unique(*__first);
16560b57cec5SDimitry Andric}
16570b57cec5SDimitry Andric
16580b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
16590b57cec5SDimitry Andrictemplate <class _InputIterator>
16600b57cec5SDimitry Andricvoid
16610b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
16620b57cec5SDimitry Andric{
16630b57cec5SDimitry Andric    typedef iterator_traits<_InputIterator> _ITraits;
16640b57cec5SDimitry Andric    typedef typename _ITraits::value_type _ItValueType;
16650b57cec5SDimitry Andric    static_assert((is_same<_ItValueType, __container_value_type>::value ||
16660b57cec5SDimitry Andric                  is_same<_ItValueType, __node_value_type>::value),
16670b57cec5SDimitry Andric                  "__assign_multi may only be called with the containers value type"
16680b57cec5SDimitry Andric                  " or the nodes value type");
16690b57cec5SDimitry Andric    if (size() != 0)
16700b57cec5SDimitry Andric    {
16710b57cec5SDimitry Andric        _DetachedTreeCache __cache(this);
16720b57cec5SDimitry Andric        for (; __cache.__get() && __first != __last; ++__first) {
16730b57cec5SDimitry Andric            __cache.__get()->__value_ = *__first;
16740b57cec5SDimitry Andric            __node_insert_multi(__cache.__get());
16750b57cec5SDimitry Andric            __cache.__advance();
16760b57cec5SDimitry Andric        }
16770b57cec5SDimitry Andric    }
16780b57cec5SDimitry Andric    for (; __first != __last; ++__first)
16790b57cec5SDimitry Andric        __insert_multi(_NodeTypes::__get_value(*__first));
16800b57cec5SDimitry Andric}
16810b57cec5SDimitry Andric
16820b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
16830b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
16840b57cec5SDimitry Andric    : __begin_node_(__iter_pointer()),
1685480093f4SDimitry Andric      __pair1_(__default_init_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
16860b57cec5SDimitry Andric      __pair3_(0, __t.value_comp())
16870b57cec5SDimitry Andric{
16880b57cec5SDimitry Andric    __begin_node() = __end_node();
16890b57cec5SDimitry Andric}
16900b57cec5SDimitry Andric
16910b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
16920b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
16930b57cec5SDimitry Andric    _NOEXCEPT_(
16940b57cec5SDimitry Andric        is_nothrow_move_constructible<__node_allocator>::value &&
16950b57cec5SDimitry Andric        is_nothrow_move_constructible<value_compare>::value)
1696*5f757f3fSDimitry Andric    : __begin_node_(std::move(__t.__begin_node_)),
1697*5f757f3fSDimitry Andric      __pair1_(std::move(__t.__pair1_)),
1698*5f757f3fSDimitry Andric      __pair3_(std::move(__t.__pair3_))
16990b57cec5SDimitry Andric{
17000b57cec5SDimitry Andric    if (size() == 0)
17010b57cec5SDimitry Andric        __begin_node() = __end_node();
17020b57cec5SDimitry Andric    else
17030b57cec5SDimitry Andric    {
17040b57cec5SDimitry Andric        __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
17050b57cec5SDimitry Andric        __t.__begin_node() = __t.__end_node();
17060b57cec5SDimitry Andric        __t.__end_node()->__left_ = nullptr;
17070b57cec5SDimitry Andric        __t.size() = 0;
17080b57cec5SDimitry Andric    }
17090b57cec5SDimitry Andric}
17100b57cec5SDimitry Andric
17110b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
17120b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1713480093f4SDimitry Andric    : __pair1_(__default_init_tag(), __node_allocator(__a)),
1714*5f757f3fSDimitry Andric      __pair3_(0, std::move(__t.value_comp()))
17150b57cec5SDimitry Andric{
17160b57cec5SDimitry Andric    if (__a == __t.__alloc())
17170b57cec5SDimitry Andric    {
17180b57cec5SDimitry Andric        if (__t.size() == 0)
17190b57cec5SDimitry Andric            __begin_node() = __end_node();
17200b57cec5SDimitry Andric        else
17210b57cec5SDimitry Andric        {
17220b57cec5SDimitry Andric            __begin_node() = __t.__begin_node();
17230b57cec5SDimitry Andric            __end_node()->__left_ = __t.__end_node()->__left_;
17240b57cec5SDimitry Andric            __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
17250b57cec5SDimitry Andric            size() = __t.size();
17260b57cec5SDimitry Andric            __t.__begin_node() = __t.__end_node();
17270b57cec5SDimitry Andric            __t.__end_node()->__left_ = nullptr;
17280b57cec5SDimitry Andric            __t.size() = 0;
17290b57cec5SDimitry Andric        }
17300b57cec5SDimitry Andric    }
17310b57cec5SDimitry Andric    else
17320b57cec5SDimitry Andric    {
17330b57cec5SDimitry Andric        __begin_node() = __end_node();
17340b57cec5SDimitry Andric    }
17350b57cec5SDimitry Andric}
17360b57cec5SDimitry Andric
17370b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
17380b57cec5SDimitry Andricvoid
17390b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
17400b57cec5SDimitry Andric    _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
17410b57cec5SDimitry Andric               is_nothrow_move_assignable<__node_allocator>::value)
17420b57cec5SDimitry Andric{
17430b57cec5SDimitry Andric    destroy(static_cast<__node_pointer>(__end_node()->__left_));
17440b57cec5SDimitry Andric    __begin_node_ = __t.__begin_node_;
17450b57cec5SDimitry Andric    __pair1_.first() = __t.__pair1_.first();
17460b57cec5SDimitry Andric    __move_assign_alloc(__t);
1747*5f757f3fSDimitry Andric    __pair3_ = std::move(__t.__pair3_);
17480b57cec5SDimitry Andric    if (size() == 0)
17490b57cec5SDimitry Andric        __begin_node() = __end_node();
17500b57cec5SDimitry Andric    else
17510b57cec5SDimitry Andric    {
17520b57cec5SDimitry Andric        __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
17530b57cec5SDimitry Andric        __t.__begin_node() = __t.__end_node();
17540b57cec5SDimitry Andric        __t.__end_node()->__left_ = nullptr;
17550b57cec5SDimitry Andric        __t.size() = 0;
17560b57cec5SDimitry Andric    }
17570b57cec5SDimitry Andric}
17580b57cec5SDimitry Andric
17590b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
17600b57cec5SDimitry Andricvoid
17610b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
17620b57cec5SDimitry Andric{
17630b57cec5SDimitry Andric    if (__node_alloc() == __t.__node_alloc())
17640b57cec5SDimitry Andric        __move_assign(__t, true_type());
17650b57cec5SDimitry Andric    else
17660b57cec5SDimitry Andric    {
1767*5f757f3fSDimitry Andric        value_comp() = std::move(__t.value_comp());
17680b57cec5SDimitry Andric        const_iterator __e = end();
17690b57cec5SDimitry Andric        if (size() != 0)
17700b57cec5SDimitry Andric        {
17710b57cec5SDimitry Andric            _DetachedTreeCache __cache(this);
17720b57cec5SDimitry Andric            while (__cache.__get() != nullptr && __t.size() != 0) {
1773*5f757f3fSDimitry Andric              __cache.__get()->__value_ = std::move(__t.remove(__t.begin())->__value_);
17740b57cec5SDimitry Andric              __node_insert_multi(__cache.__get());
17750b57cec5SDimitry Andric              __cache.__advance();
17760b57cec5SDimitry Andric            }
17770b57cec5SDimitry Andric        }
17780b57cec5SDimitry Andric        while (__t.size() != 0)
17790b57cec5SDimitry Andric            __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
17800b57cec5SDimitry Andric    }
17810b57cec5SDimitry Andric}
17820b57cec5SDimitry Andric
17830b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
17840b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>&
17850b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
17860b57cec5SDimitry Andric    _NOEXCEPT_(
17870b57cec5SDimitry Andric        __node_traits::propagate_on_container_move_assignment::value &&
17880b57cec5SDimitry Andric        is_nothrow_move_assignable<value_compare>::value &&
17890b57cec5SDimitry Andric        is_nothrow_move_assignable<__node_allocator>::value)
17900b57cec5SDimitry Andric
17910b57cec5SDimitry Andric{
17920b57cec5SDimitry Andric    __move_assign(__t, integral_constant<bool,
17930b57cec5SDimitry Andric                  __node_traits::propagate_on_container_move_assignment::value>());
17940b57cec5SDimitry Andric    return *this;
17950b57cec5SDimitry Andric}
17960b57cec5SDimitry Andric
17970b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
17980b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::~__tree()
17990b57cec5SDimitry Andric{
18000b57cec5SDimitry Andric    static_assert((is_copy_constructible<value_compare>::value),
18010b57cec5SDimitry Andric                 "Comparator must be copy-constructible.");
18020b57cec5SDimitry Andric  destroy(__root());
18030b57cec5SDimitry Andric}
18040b57cec5SDimitry Andric
18050b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
18060b57cec5SDimitry Andricvoid
18070b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
18080b57cec5SDimitry Andric{
18090b57cec5SDimitry Andric    if (__nd != nullptr)
18100b57cec5SDimitry Andric    {
18110b57cec5SDimitry Andric        destroy(static_cast<__node_pointer>(__nd->__left_));
18120b57cec5SDimitry Andric        destroy(static_cast<__node_pointer>(__nd->__right_));
18130b57cec5SDimitry Andric        __node_allocator& __na = __node_alloc();
18140b57cec5SDimitry Andric        __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
18150b57cec5SDimitry Andric        __node_traits::deallocate(__na, __nd, 1);
18160b57cec5SDimitry Andric    }
18170b57cec5SDimitry Andric}
18180b57cec5SDimitry Andric
18190b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
18200b57cec5SDimitry Andricvoid
18210b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
18220b57cec5SDimitry Andric#if _LIBCPP_STD_VER <= 11
18230b57cec5SDimitry Andric        _NOEXCEPT_(
18240b57cec5SDimitry Andric            __is_nothrow_swappable<value_compare>::value
18250b57cec5SDimitry Andric            && (!__node_traits::propagate_on_container_swap::value ||
18260b57cec5SDimitry Andric                 __is_nothrow_swappable<__node_allocator>::value)
18270b57cec5SDimitry Andric            )
18280b57cec5SDimitry Andric#else
18290b57cec5SDimitry Andric        _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
18300b57cec5SDimitry Andric#endif
18310b57cec5SDimitry Andric{
1832*5f757f3fSDimitry Andric    using std::swap;
18330b57cec5SDimitry Andric    swap(__begin_node_, __t.__begin_node_);
18340b57cec5SDimitry Andric    swap(__pair1_.first(), __t.__pair1_.first());
1835*5f757f3fSDimitry Andric    std::__swap_allocator(__node_alloc(), __t.__node_alloc());
18360b57cec5SDimitry Andric    __pair3_.swap(__t.__pair3_);
18370b57cec5SDimitry Andric    if (size() == 0)
18380b57cec5SDimitry Andric        __begin_node() = __end_node();
18390b57cec5SDimitry Andric    else
18400b57cec5SDimitry Andric        __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
18410b57cec5SDimitry Andric    if (__t.size() == 0)
18420b57cec5SDimitry Andric        __t.__begin_node() = __t.__end_node();
18430b57cec5SDimitry Andric    else
18440b57cec5SDimitry Andric        __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
18450b57cec5SDimitry Andric}
18460b57cec5SDimitry Andric
18470b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
18480b57cec5SDimitry Andricvoid
18490b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
18500b57cec5SDimitry Andric{
18510b57cec5SDimitry Andric    destroy(__root());
18520b57cec5SDimitry Andric    size() = 0;
18530b57cec5SDimitry Andric    __begin_node() = __end_node();
18540b57cec5SDimitry Andric    __end_node()->__left_ = nullptr;
18550b57cec5SDimitry Andric}
18560b57cec5SDimitry Andric
18570b57cec5SDimitry Andric// Find lower_bound place to insert
18580b57cec5SDimitry Andric// Set __parent to parent of null leaf
18590b57cec5SDimitry Andric// Return reference to null leaf
18600b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
18610b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
18620b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
18630b57cec5SDimitry Andric                                                   const key_type& __v)
18640b57cec5SDimitry Andric{
18650b57cec5SDimitry Andric    __node_pointer __nd = __root();
18660b57cec5SDimitry Andric    if (__nd != nullptr)
18670b57cec5SDimitry Andric    {
18680b57cec5SDimitry Andric        while (true)
18690b57cec5SDimitry Andric        {
18700b57cec5SDimitry Andric            if (value_comp()(__nd->__value_, __v))
18710b57cec5SDimitry Andric            {
18720b57cec5SDimitry Andric                if (__nd->__right_ != nullptr)
18730b57cec5SDimitry Andric                    __nd = static_cast<__node_pointer>(__nd->__right_);
18740b57cec5SDimitry Andric                else
18750b57cec5SDimitry Andric                {
18760b57cec5SDimitry Andric                    __parent = static_cast<__parent_pointer>(__nd);
18770b57cec5SDimitry Andric                    return __nd->__right_;
18780b57cec5SDimitry Andric                }
18790b57cec5SDimitry Andric            }
18800b57cec5SDimitry Andric            else
18810b57cec5SDimitry Andric            {
18820b57cec5SDimitry Andric                if (__nd->__left_ != nullptr)
18830b57cec5SDimitry Andric                    __nd = static_cast<__node_pointer>(__nd->__left_);
18840b57cec5SDimitry Andric                else
18850b57cec5SDimitry Andric                {
18860b57cec5SDimitry Andric                    __parent = static_cast<__parent_pointer>(__nd);
18870b57cec5SDimitry Andric                    return __parent->__left_;
18880b57cec5SDimitry Andric                }
18890b57cec5SDimitry Andric            }
18900b57cec5SDimitry Andric        }
18910b57cec5SDimitry Andric    }
18920b57cec5SDimitry Andric    __parent = static_cast<__parent_pointer>(__end_node());
18930b57cec5SDimitry Andric    return __parent->__left_;
18940b57cec5SDimitry Andric}
18950b57cec5SDimitry Andric
18960b57cec5SDimitry Andric// Find upper_bound place to insert
18970b57cec5SDimitry Andric// Set __parent to parent of null leaf
18980b57cec5SDimitry Andric// Return reference to null leaf
18990b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
19000b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
19010b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
19020b57cec5SDimitry Andric                                                    const key_type& __v)
19030b57cec5SDimitry Andric{
19040b57cec5SDimitry Andric    __node_pointer __nd = __root();
19050b57cec5SDimitry Andric    if (__nd != nullptr)
19060b57cec5SDimitry Andric    {
19070b57cec5SDimitry Andric        while (true)
19080b57cec5SDimitry Andric        {
19090b57cec5SDimitry Andric            if (value_comp()(__v, __nd->__value_))
19100b57cec5SDimitry Andric            {
19110b57cec5SDimitry Andric                if (__nd->__left_ != nullptr)
19120b57cec5SDimitry Andric                    __nd = static_cast<__node_pointer>(__nd->__left_);
19130b57cec5SDimitry Andric                else
19140b57cec5SDimitry Andric                {
19150b57cec5SDimitry Andric                    __parent = static_cast<__parent_pointer>(__nd);
19160b57cec5SDimitry Andric                    return __parent->__left_;
19170b57cec5SDimitry Andric                }
19180b57cec5SDimitry Andric            }
19190b57cec5SDimitry Andric            else
19200b57cec5SDimitry Andric            {
19210b57cec5SDimitry Andric                if (__nd->__right_ != nullptr)
19220b57cec5SDimitry Andric                    __nd = static_cast<__node_pointer>(__nd->__right_);
19230b57cec5SDimitry Andric                else
19240b57cec5SDimitry Andric                {
19250b57cec5SDimitry Andric                    __parent = static_cast<__parent_pointer>(__nd);
19260b57cec5SDimitry Andric                    return __nd->__right_;
19270b57cec5SDimitry Andric                }
19280b57cec5SDimitry Andric            }
19290b57cec5SDimitry Andric        }
19300b57cec5SDimitry Andric    }
19310b57cec5SDimitry Andric    __parent = static_cast<__parent_pointer>(__end_node());
19320b57cec5SDimitry Andric    return __parent->__left_;
19330b57cec5SDimitry Andric}
19340b57cec5SDimitry Andric
19350b57cec5SDimitry Andric// Find leaf place to insert closest to __hint
19360b57cec5SDimitry Andric// First check prior to __hint.
19370b57cec5SDimitry Andric// Next check after __hint.
19380b57cec5SDimitry Andric// Next do O(log N) search.
19390b57cec5SDimitry Andric// Set __parent to parent of null leaf
19400b57cec5SDimitry Andric// Return reference to null leaf
19410b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
19420b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
19430b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
19440b57cec5SDimitry Andric                                               __parent_pointer& __parent,
19450b57cec5SDimitry Andric                                               const key_type& __v)
19460b57cec5SDimitry Andric{
19470b57cec5SDimitry Andric    if (__hint == end() || !value_comp()(*__hint, __v))  // check before
19480b57cec5SDimitry Andric    {
19490b57cec5SDimitry Andric        // __v <= *__hint
19500b57cec5SDimitry Andric        const_iterator __prior = __hint;
19510b57cec5SDimitry Andric        if (__prior == begin() || !value_comp()(__v, *--__prior))
19520b57cec5SDimitry Andric        {
19530b57cec5SDimitry Andric            // *prev(__hint) <= __v <= *__hint
19540b57cec5SDimitry Andric            if (__hint.__ptr_->__left_ == nullptr)
19550b57cec5SDimitry Andric            {
19560b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__hint.__ptr_);
19570b57cec5SDimitry Andric                return __parent->__left_;
19580b57cec5SDimitry Andric            }
19590b57cec5SDimitry Andric            else
19600b57cec5SDimitry Andric            {
19610b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__prior.__ptr_);
19620b57cec5SDimitry Andric                return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
19630b57cec5SDimitry Andric            }
19640b57cec5SDimitry Andric        }
19650b57cec5SDimitry Andric        // __v < *prev(__hint)
19660b57cec5SDimitry Andric        return __find_leaf_high(__parent, __v);
19670b57cec5SDimitry Andric    }
19680b57cec5SDimitry Andric    // else __v > *__hint
19690b57cec5SDimitry Andric    return __find_leaf_low(__parent, __v);
19700b57cec5SDimitry Andric}
19710b57cec5SDimitry Andric
19720b57cec5SDimitry Andric// Find place to insert if __v doesn't exist
19730b57cec5SDimitry Andric// Set __parent to parent of null leaf
19740b57cec5SDimitry Andric// Return reference to null leaf
19750b57cec5SDimitry Andric// If __v exists, set parent to node of __v and return reference to node of __v
19760b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
19770b57cec5SDimitry Andrictemplate <class _Key>
19780b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
19790b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
19800b57cec5SDimitry Andric                                                const _Key& __v)
19810b57cec5SDimitry Andric{
19820b57cec5SDimitry Andric    __node_pointer __nd = __root();
19830b57cec5SDimitry Andric    __node_base_pointer* __nd_ptr = __root_ptr();
19840b57cec5SDimitry Andric    if (__nd != nullptr)
19850b57cec5SDimitry Andric    {
19860b57cec5SDimitry Andric        while (true)
19870b57cec5SDimitry Andric        {
19880b57cec5SDimitry Andric            if (value_comp()(__v, __nd->__value_))
19890b57cec5SDimitry Andric            {
19900b57cec5SDimitry Andric                if (__nd->__left_ != nullptr) {
1991*5f757f3fSDimitry Andric                    __nd_ptr = std::addressof(__nd->__left_);
19920b57cec5SDimitry Andric                    __nd = static_cast<__node_pointer>(__nd->__left_);
19930b57cec5SDimitry Andric                } else {
19940b57cec5SDimitry Andric                    __parent = static_cast<__parent_pointer>(__nd);
19950b57cec5SDimitry Andric                    return __parent->__left_;
19960b57cec5SDimitry Andric                }
19970b57cec5SDimitry Andric            }
19980b57cec5SDimitry Andric            else if (value_comp()(__nd->__value_, __v))
19990b57cec5SDimitry Andric            {
20000b57cec5SDimitry Andric                if (__nd->__right_ != nullptr) {
2001*5f757f3fSDimitry Andric                    __nd_ptr = std::addressof(__nd->__right_);
20020b57cec5SDimitry Andric                    __nd = static_cast<__node_pointer>(__nd->__right_);
20030b57cec5SDimitry Andric                } else {
20040b57cec5SDimitry Andric                    __parent = static_cast<__parent_pointer>(__nd);
20050b57cec5SDimitry Andric                    return __nd->__right_;
20060b57cec5SDimitry Andric                }
20070b57cec5SDimitry Andric            }
20080b57cec5SDimitry Andric            else
20090b57cec5SDimitry Andric            {
20100b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__nd);
20110b57cec5SDimitry Andric                return *__nd_ptr;
20120b57cec5SDimitry Andric            }
20130b57cec5SDimitry Andric        }
20140b57cec5SDimitry Andric    }
20150b57cec5SDimitry Andric    __parent = static_cast<__parent_pointer>(__end_node());
20160b57cec5SDimitry Andric    return __parent->__left_;
20170b57cec5SDimitry Andric}
20180b57cec5SDimitry Andric
20190b57cec5SDimitry Andric// Find place to insert if __v doesn't exist
20200b57cec5SDimitry Andric// First check prior to __hint.
20210b57cec5SDimitry Andric// Next check after __hint.
20220b57cec5SDimitry Andric// Next do O(log N) search.
20230b57cec5SDimitry Andric// Set __parent to parent of null leaf
20240b57cec5SDimitry Andric// Return reference to null leaf
20250b57cec5SDimitry Andric// If __v exists, set parent to node of __v and return reference to node of __v
20260b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
20270b57cec5SDimitry Andrictemplate <class _Key>
20280b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
20290b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
20300b57cec5SDimitry Andric                                                __parent_pointer& __parent,
20310b57cec5SDimitry Andric                                                __node_base_pointer& __dummy,
20320b57cec5SDimitry Andric                                                const _Key& __v)
20330b57cec5SDimitry Andric{
20340b57cec5SDimitry Andric    if (__hint == end() || value_comp()(__v, *__hint))  // check before
20350b57cec5SDimitry Andric    {
20360b57cec5SDimitry Andric        // __v < *__hint
20370b57cec5SDimitry Andric        const_iterator __prior = __hint;
20380b57cec5SDimitry Andric        if (__prior == begin() || value_comp()(*--__prior, __v))
20390b57cec5SDimitry Andric        {
20400b57cec5SDimitry Andric            // *prev(__hint) < __v < *__hint
20410b57cec5SDimitry Andric            if (__hint.__ptr_->__left_ == nullptr)
20420b57cec5SDimitry Andric            {
20430b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__hint.__ptr_);
20440b57cec5SDimitry Andric                return __parent->__left_;
20450b57cec5SDimitry Andric            }
20460b57cec5SDimitry Andric            else
20470b57cec5SDimitry Andric            {
20480b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__prior.__ptr_);
20490b57cec5SDimitry Andric                return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
20500b57cec5SDimitry Andric            }
20510b57cec5SDimitry Andric        }
20520b57cec5SDimitry Andric        // __v <= *prev(__hint)
20530b57cec5SDimitry Andric        return __find_equal(__parent, __v);
20540b57cec5SDimitry Andric    }
20550b57cec5SDimitry Andric    else if (value_comp()(*__hint, __v))  // check after
20560b57cec5SDimitry Andric    {
20570b57cec5SDimitry Andric        // *__hint < __v
2058*5f757f3fSDimitry Andric        const_iterator __next = std::next(__hint);
20590b57cec5SDimitry Andric        if (__next == end() || value_comp()(__v, *__next))
20600b57cec5SDimitry Andric        {
2061*5f757f3fSDimitry Andric            // *__hint < __v < *std::next(__hint)
20620b57cec5SDimitry Andric            if (__hint.__get_np()->__right_ == nullptr)
20630b57cec5SDimitry Andric            {
20640b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__hint.__ptr_);
20650b57cec5SDimitry Andric                return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
20660b57cec5SDimitry Andric            }
20670b57cec5SDimitry Andric            else
20680b57cec5SDimitry Andric            {
20690b57cec5SDimitry Andric                __parent = static_cast<__parent_pointer>(__next.__ptr_);
20700b57cec5SDimitry Andric                return __parent->__left_;
20710b57cec5SDimitry Andric            }
20720b57cec5SDimitry Andric        }
20730b57cec5SDimitry Andric        // *next(__hint) <= __v
20740b57cec5SDimitry Andric        return __find_equal(__parent, __v);
20750b57cec5SDimitry Andric    }
20760b57cec5SDimitry Andric    // else __v == *__hint
20770b57cec5SDimitry Andric    __parent = static_cast<__parent_pointer>(__hint.__ptr_);
20780b57cec5SDimitry Andric    __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
20790b57cec5SDimitry Andric    return __dummy;
20800b57cec5SDimitry Andric}
20810b57cec5SDimitry Andric
20820b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
20830b57cec5SDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
20840b57cec5SDimitry Andric    __parent_pointer __parent, __node_base_pointer& __child,
20850b57cec5SDimitry Andric    __node_base_pointer __new_node) _NOEXCEPT
20860b57cec5SDimitry Andric{
20870b57cec5SDimitry Andric    __new_node->__left_   = nullptr;
20880b57cec5SDimitry Andric    __new_node->__right_  = nullptr;
20890b57cec5SDimitry Andric    __new_node->__parent_ = __parent;
20900b57cec5SDimitry Andric    // __new_node->__is_black_ is initialized in __tree_balance_after_insert
20910b57cec5SDimitry Andric    __child = __new_node;
20920b57cec5SDimitry Andric    if (__begin_node()->__left_ != nullptr)
20930b57cec5SDimitry Andric        __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
2094*5f757f3fSDimitry Andric    std::__tree_balance_after_insert(__end_node()->__left_, __child);
20950b57cec5SDimitry Andric    ++size();
20960b57cec5SDimitry Andric}
20970b57cec5SDimitry Andric
20980b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
20990b57cec5SDimitry Andrictemplate <class _Key, class... _Args>
21000b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
21010b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
21020b57cec5SDimitry Andric{
21030b57cec5SDimitry Andric    __parent_pointer __parent;
21040b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__parent, __k);
21050b57cec5SDimitry Andric    __node_pointer __r = static_cast<__node_pointer>(__child);
21060b57cec5SDimitry Andric    bool __inserted = false;
21070b57cec5SDimitry Andric    if (__child == nullptr)
21080b57cec5SDimitry Andric    {
2109*5f757f3fSDimitry Andric        __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
21100b57cec5SDimitry Andric        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
21110b57cec5SDimitry Andric        __r = __h.release();
21120b57cec5SDimitry Andric        __inserted = true;
21130b57cec5SDimitry Andric    }
21140b57cec5SDimitry Andric    return pair<iterator, bool>(iterator(__r), __inserted);
21150b57cec5SDimitry Andric}
21160b57cec5SDimitry Andric
21170b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
21180b57cec5SDimitry Andrictemplate <class _Key, class... _Args>
2119e8d8bef9SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
21200b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
21210b57cec5SDimitry Andric    const_iterator __p, _Key const& __k, _Args&&... __args)
21220b57cec5SDimitry Andric{
21230b57cec5SDimitry Andric    __parent_pointer __parent;
21240b57cec5SDimitry Andric    __node_base_pointer __dummy;
21250b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
21260b57cec5SDimitry Andric    __node_pointer __r = static_cast<__node_pointer>(__child);
2127e8d8bef9SDimitry Andric    bool __inserted = false;
21280b57cec5SDimitry Andric    if (__child == nullptr)
21290b57cec5SDimitry Andric    {
2130*5f757f3fSDimitry Andric        __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
21310b57cec5SDimitry Andric        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
21320b57cec5SDimitry Andric        __r = __h.release();
2133e8d8bef9SDimitry Andric        __inserted = true;
21340b57cec5SDimitry Andric    }
2135e8d8bef9SDimitry Andric    return pair<iterator, bool>(iterator(__r), __inserted);
21360b57cec5SDimitry Andric}
21370b57cec5SDimitry Andric
21380b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
21390b57cec5SDimitry Andrictemplate <class ..._Args>
21400b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_holder
21410b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
21420b57cec5SDimitry Andric{
21430b57cec5SDimitry Andric    static_assert(!__is_tree_value_type<_Args...>::value,
21440b57cec5SDimitry Andric                  "Cannot construct from __value_type");
21450b57cec5SDimitry Andric    __node_allocator& __na = __node_alloc();
21460b57cec5SDimitry Andric    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2147*5f757f3fSDimitry Andric    __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), std::forward<_Args>(__args)...);
21480b57cec5SDimitry Andric    __h.get_deleter().__value_constructed = true;
21490b57cec5SDimitry Andric    return __h;
21500b57cec5SDimitry Andric}
21510b57cec5SDimitry Andric
21520b57cec5SDimitry Andric
21530b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
21540b57cec5SDimitry Andrictemplate <class... _Args>
21550b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
21560b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
21570b57cec5SDimitry Andric{
2158*5f757f3fSDimitry Andric    __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
21590b57cec5SDimitry Andric    __parent_pointer __parent;
21600b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
21610b57cec5SDimitry Andric    __node_pointer __r = static_cast<__node_pointer>(__child);
21620b57cec5SDimitry Andric    bool __inserted = false;
21630b57cec5SDimitry Andric    if (__child == nullptr)
21640b57cec5SDimitry Andric    {
21650b57cec5SDimitry Andric        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
21660b57cec5SDimitry Andric        __r = __h.release();
21670b57cec5SDimitry Andric        __inserted = true;
21680b57cec5SDimitry Andric    }
21690b57cec5SDimitry Andric    return pair<iterator, bool>(iterator(__r), __inserted);
21700b57cec5SDimitry Andric}
21710b57cec5SDimitry Andric
21720b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
21730b57cec5SDimitry Andrictemplate <class... _Args>
21740b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
21750b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
21760b57cec5SDimitry Andric{
2177*5f757f3fSDimitry Andric    __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
21780b57cec5SDimitry Andric    __parent_pointer __parent;
21790b57cec5SDimitry Andric    __node_base_pointer __dummy;
21800b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
21810b57cec5SDimitry Andric    __node_pointer __r = static_cast<__node_pointer>(__child);
21820b57cec5SDimitry Andric    if (__child == nullptr)
21830b57cec5SDimitry Andric    {
21840b57cec5SDimitry Andric        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
21850b57cec5SDimitry Andric        __r = __h.release();
21860b57cec5SDimitry Andric    }
21870b57cec5SDimitry Andric    return iterator(__r);
21880b57cec5SDimitry Andric}
21890b57cec5SDimitry Andric
21900b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
21910b57cec5SDimitry Andrictemplate <class... _Args>
21920b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
21930b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
21940b57cec5SDimitry Andric{
2195*5f757f3fSDimitry Andric    __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
21960b57cec5SDimitry Andric    __parent_pointer __parent;
21970b57cec5SDimitry Andric    __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
21980b57cec5SDimitry Andric    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
21990b57cec5SDimitry Andric    return iterator(static_cast<__node_pointer>(__h.release()));
22000b57cec5SDimitry Andric}
22010b57cec5SDimitry Andric
22020b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22030b57cec5SDimitry Andrictemplate <class... _Args>
22040b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
22050b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
22060b57cec5SDimitry Andric                                                        _Args&&... __args)
22070b57cec5SDimitry Andric{
2208*5f757f3fSDimitry Andric    __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
22090b57cec5SDimitry Andric    __parent_pointer __parent;
22100b57cec5SDimitry Andric    __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
22110b57cec5SDimitry Andric    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
22120b57cec5SDimitry Andric    return iterator(static_cast<__node_pointer>(__h.release()));
22130b57cec5SDimitry Andric}
22140b57cec5SDimitry Andric
22150b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22160b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
22170b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_assign_unique(const __container_value_type& __v, __node_pointer __nd)
22180b57cec5SDimitry Andric{
22190b57cec5SDimitry Andric    __parent_pointer __parent;
22200b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__parent, _NodeTypes::__get_key(__v));
22210b57cec5SDimitry Andric    __node_pointer __r = static_cast<__node_pointer>(__child);
22220b57cec5SDimitry Andric    bool __inserted = false;
22230b57cec5SDimitry Andric    if (__child == nullptr)
22240b57cec5SDimitry Andric    {
22250b57cec5SDimitry Andric        __nd->__value_ = __v;
22260b57cec5SDimitry Andric        __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
22270b57cec5SDimitry Andric        __r = __nd;
22280b57cec5SDimitry Andric        __inserted = true;
22290b57cec5SDimitry Andric    }
22300b57cec5SDimitry Andric    return pair<iterator, bool>(iterator(__r), __inserted);
22310b57cec5SDimitry Andric}
22320b57cec5SDimitry Andric
22330b57cec5SDimitry Andric
22340b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22350b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
22360b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
22370b57cec5SDimitry Andric{
22380b57cec5SDimitry Andric    __parent_pointer __parent;
22390b57cec5SDimitry Andric    __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
22400b57cec5SDimitry Andric    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
22410b57cec5SDimitry Andric    return iterator(__nd);
22420b57cec5SDimitry Andric}
22430b57cec5SDimitry Andric
22440b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22450b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
22460b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
22470b57cec5SDimitry Andric                                                       __node_pointer __nd)
22480b57cec5SDimitry Andric{
22490b57cec5SDimitry Andric    __parent_pointer __parent;
22500b57cec5SDimitry Andric    __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
22510b57cec5SDimitry Andric    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
22520b57cec5SDimitry Andric    return iterator(__nd);
22530b57cec5SDimitry Andric}
22540b57cec5SDimitry Andric
22550b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22560b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
22570b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT
22580b57cec5SDimitry Andric{
22590b57cec5SDimitry Andric    iterator __r(__ptr);
22600b57cec5SDimitry Andric    ++__r;
22610b57cec5SDimitry Andric    if (__begin_node() == __ptr)
22620b57cec5SDimitry Andric        __begin_node() = __r.__ptr_;
22630b57cec5SDimitry Andric    --size();
2264*5f757f3fSDimitry Andric    std::__tree_remove(__end_node()->__left_,
22650b57cec5SDimitry Andric                         static_cast<__node_base_pointer>(__ptr));
22660b57cec5SDimitry Andric    return __r;
22670b57cec5SDimitry Andric}
22680b57cec5SDimitry Andric
226906c3fb27SDimitry Andric#if _LIBCPP_STD_VER >= 17
22700b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22710b57cec5SDimitry Andrictemplate <class _NodeHandle, class _InsertReturnType>
2272*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
22730b57cec5SDimitry Andric_InsertReturnType
22740b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
22750b57cec5SDimitry Andric    _NodeHandle&& __nh)
22760b57cec5SDimitry Andric{
22770b57cec5SDimitry Andric    if (__nh.empty())
22780b57cec5SDimitry Andric        return _InsertReturnType{end(), false, _NodeHandle()};
22790b57cec5SDimitry Andric
22800b57cec5SDimitry Andric    __node_pointer __ptr = __nh.__ptr_;
22810b57cec5SDimitry Andric    __parent_pointer __parent;
22820b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__parent,
22830b57cec5SDimitry Andric                                                __ptr->__value_);
22840b57cec5SDimitry Andric    if (__child != nullptr)
22850b57cec5SDimitry Andric        return _InsertReturnType{
22860b57cec5SDimitry Andric            iterator(static_cast<__node_pointer>(__child)),
2287*5f757f3fSDimitry Andric            false, std::move(__nh)};
22880b57cec5SDimitry Andric
22890b57cec5SDimitry Andric    __insert_node_at(__parent, __child,
22900b57cec5SDimitry Andric                     static_cast<__node_base_pointer>(__ptr));
22910b57cec5SDimitry Andric    __nh.__release_ptr();
22920b57cec5SDimitry Andric    return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
22930b57cec5SDimitry Andric}
22940b57cec5SDimitry Andric
22950b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
22960b57cec5SDimitry Andrictemplate <class _NodeHandle>
2297*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
22980b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
22990b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
23000b57cec5SDimitry Andric    const_iterator __hint, _NodeHandle&& __nh)
23010b57cec5SDimitry Andric{
23020b57cec5SDimitry Andric    if (__nh.empty())
23030b57cec5SDimitry Andric        return end();
23040b57cec5SDimitry Andric
23050b57cec5SDimitry Andric    __node_pointer __ptr = __nh.__ptr_;
23060b57cec5SDimitry Andric    __parent_pointer __parent;
23070b57cec5SDimitry Andric    __node_base_pointer __dummy;
23080b57cec5SDimitry Andric    __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy,
23090b57cec5SDimitry Andric                                                __ptr->__value_);
23100b57cec5SDimitry Andric    __node_pointer __r = static_cast<__node_pointer>(__child);
23110b57cec5SDimitry Andric    if (__child == nullptr)
23120b57cec5SDimitry Andric    {
23130b57cec5SDimitry Andric        __insert_node_at(__parent, __child,
23140b57cec5SDimitry Andric                         static_cast<__node_base_pointer>(__ptr));
23150b57cec5SDimitry Andric        __r = __ptr;
23160b57cec5SDimitry Andric        __nh.__release_ptr();
23170b57cec5SDimitry Andric    }
23180b57cec5SDimitry Andric    return iterator(__r);
23190b57cec5SDimitry Andric}
23200b57cec5SDimitry Andric
23210b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
23220b57cec5SDimitry Andrictemplate <class _NodeHandle>
2323*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
23240b57cec5SDimitry Andric_NodeHandle
23250b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key)
23260b57cec5SDimitry Andric{
23270b57cec5SDimitry Andric    iterator __it = find(__key);
23280b57cec5SDimitry Andric    if (__it == end())
23290b57cec5SDimitry Andric        return _NodeHandle();
23300b57cec5SDimitry Andric    return __node_handle_extract<_NodeHandle>(__it);
23310b57cec5SDimitry Andric}
23320b57cec5SDimitry Andric
23330b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
23340b57cec5SDimitry Andrictemplate <class _NodeHandle>
2335*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
23360b57cec5SDimitry Andric_NodeHandle
23370b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p)
23380b57cec5SDimitry Andric{
23390b57cec5SDimitry Andric    __node_pointer __np = __p.__get_np();
23400b57cec5SDimitry Andric    __remove_node_pointer(__np);
23410b57cec5SDimitry Andric    return _NodeHandle(__np, __alloc());
23420b57cec5SDimitry Andric}
23430b57cec5SDimitry Andric
23440b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
23450b57cec5SDimitry Andrictemplate <class _Tree>
2346*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
23470b57cec5SDimitry Andricvoid
23480b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(_Tree& __source)
23490b57cec5SDimitry Andric{
23500b57cec5SDimitry Andric    static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
23510b57cec5SDimitry Andric
23520b57cec5SDimitry Andric    for (typename _Tree::iterator __i = __source.begin();
23530b57cec5SDimitry Andric         __i != __source.end();)
23540b57cec5SDimitry Andric    {
23550b57cec5SDimitry Andric        __node_pointer __src_ptr = __i.__get_np();
23560b57cec5SDimitry Andric        __parent_pointer __parent;
23570b57cec5SDimitry Andric        __node_base_pointer& __child =
23580b57cec5SDimitry Andric            __find_equal(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
23590b57cec5SDimitry Andric        ++__i;
23600b57cec5SDimitry Andric        if (__child != nullptr)
23610b57cec5SDimitry Andric            continue;
23620b57cec5SDimitry Andric        __source.__remove_node_pointer(__src_ptr);
23630b57cec5SDimitry Andric        __insert_node_at(__parent, __child,
23640b57cec5SDimitry Andric                         static_cast<__node_base_pointer>(__src_ptr));
23650b57cec5SDimitry Andric    }
23660b57cec5SDimitry Andric}
23670b57cec5SDimitry Andric
23680b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
23690b57cec5SDimitry Andrictemplate <class _NodeHandle>
2370*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
23710b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
23720b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh)
23730b57cec5SDimitry Andric{
23740b57cec5SDimitry Andric    if (__nh.empty())
23750b57cec5SDimitry Andric        return end();
23760b57cec5SDimitry Andric    __node_pointer __ptr = __nh.__ptr_;
23770b57cec5SDimitry Andric    __parent_pointer __parent;
23780b57cec5SDimitry Andric    __node_base_pointer& __child = __find_leaf_high(
23790b57cec5SDimitry Andric        __parent, _NodeTypes::__get_key(__ptr->__value_));
23800b57cec5SDimitry Andric    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
23810b57cec5SDimitry Andric    __nh.__release_ptr();
23820b57cec5SDimitry Andric    return iterator(__ptr);
23830b57cec5SDimitry Andric}
23840b57cec5SDimitry Andric
23850b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
23860b57cec5SDimitry Andrictemplate <class _NodeHandle>
2387*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
23880b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
23890b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(
23900b57cec5SDimitry Andric    const_iterator __hint, _NodeHandle&& __nh)
23910b57cec5SDimitry Andric{
23920b57cec5SDimitry Andric    if (__nh.empty())
23930b57cec5SDimitry Andric        return end();
23940b57cec5SDimitry Andric
23950b57cec5SDimitry Andric    __node_pointer __ptr = __nh.__ptr_;
23960b57cec5SDimitry Andric    __parent_pointer __parent;
23970b57cec5SDimitry Andric    __node_base_pointer& __child = __find_leaf(__hint, __parent,
23980b57cec5SDimitry Andric                                               _NodeTypes::__get_key(__ptr->__value_));
23990b57cec5SDimitry Andric    __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
24000b57cec5SDimitry Andric    __nh.__release_ptr();
24010b57cec5SDimitry Andric    return iterator(__ptr);
24020b57cec5SDimitry Andric}
24030b57cec5SDimitry Andric
24040b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24050b57cec5SDimitry Andrictemplate <class _Tree>
2406*5f757f3fSDimitry Andric_LIBCPP_HIDE_FROM_ABI
24070b57cec5SDimitry Andricvoid
24080b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(_Tree& __source)
24090b57cec5SDimitry Andric{
24100b57cec5SDimitry Andric    static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
24110b57cec5SDimitry Andric
24120b57cec5SDimitry Andric    for (typename _Tree::iterator __i = __source.begin();
24130b57cec5SDimitry Andric         __i != __source.end();)
24140b57cec5SDimitry Andric    {
24150b57cec5SDimitry Andric        __node_pointer __src_ptr = __i.__get_np();
24160b57cec5SDimitry Andric        __parent_pointer __parent;
24170b57cec5SDimitry Andric        __node_base_pointer& __child = __find_leaf_high(
24180b57cec5SDimitry Andric            __parent, _NodeTypes::__get_key(__src_ptr->__value_));
24190b57cec5SDimitry Andric        ++__i;
24200b57cec5SDimitry Andric        __source.__remove_node_pointer(__src_ptr);
24210b57cec5SDimitry Andric        __insert_node_at(__parent, __child,
24220b57cec5SDimitry Andric                         static_cast<__node_base_pointer>(__src_ptr));
24230b57cec5SDimitry Andric    }
24240b57cec5SDimitry Andric}
24250b57cec5SDimitry Andric
242606c3fb27SDimitry Andric#endif // _LIBCPP_STD_VER >= 17
24270b57cec5SDimitry Andric
24280b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24290b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
24300b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
24310b57cec5SDimitry Andric{
24320b57cec5SDimitry Andric    __node_pointer __np = __p.__get_np();
24330b57cec5SDimitry Andric    iterator __r = __remove_node_pointer(__np);
24340b57cec5SDimitry Andric    __node_allocator& __na = __node_alloc();
24350b57cec5SDimitry Andric    __node_traits::destroy(__na, _NodeTypes::__get_ptr(
24360b57cec5SDimitry Andric        const_cast<__node_value_type&>(*__p)));
24370b57cec5SDimitry Andric    __node_traits::deallocate(__na, __np, 1);
24380b57cec5SDimitry Andric    return __r;
24390b57cec5SDimitry Andric}
24400b57cec5SDimitry Andric
24410b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24420b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
24430b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
24440b57cec5SDimitry Andric{
24450b57cec5SDimitry Andric    while (__f != __l)
24460b57cec5SDimitry Andric        __f = erase(__f);
24470b57cec5SDimitry Andric    return iterator(__l.__ptr_);
24480b57cec5SDimitry Andric}
24490b57cec5SDimitry Andric
24500b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24510b57cec5SDimitry Andrictemplate <class _Key>
24520b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type
24530b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
24540b57cec5SDimitry Andric{
24550b57cec5SDimitry Andric    iterator __i = find(__k);
24560b57cec5SDimitry Andric    if (__i == end())
24570b57cec5SDimitry Andric        return 0;
24580b57cec5SDimitry Andric    erase(__i);
24590b57cec5SDimitry Andric    return 1;
24600b57cec5SDimitry Andric}
24610b57cec5SDimitry Andric
24620b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24630b57cec5SDimitry Andrictemplate <class _Key>
24640b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type
24650b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
24660b57cec5SDimitry Andric{
24670b57cec5SDimitry Andric    pair<iterator, iterator> __p = __equal_range_multi(__k);
24680b57cec5SDimitry Andric    size_type __r = 0;
24690b57cec5SDimitry Andric    for (; __p.first != __p.second; ++__r)
24700b57cec5SDimitry Andric        __p.first = erase(__p.first);
24710b57cec5SDimitry Andric    return __r;
24720b57cec5SDimitry Andric}
24730b57cec5SDimitry Andric
24740b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24750b57cec5SDimitry Andrictemplate <class _Key>
24760b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
24770b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
24780b57cec5SDimitry Andric{
24790b57cec5SDimitry Andric    iterator __p = __lower_bound(__v, __root(), __end_node());
24800b57cec5SDimitry Andric    if (__p != end() && !value_comp()(__v, *__p))
24810b57cec5SDimitry Andric        return __p;
24820b57cec5SDimitry Andric    return end();
24830b57cec5SDimitry Andric}
24840b57cec5SDimitry Andric
24850b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24860b57cec5SDimitry Andrictemplate <class _Key>
24870b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::const_iterator
24880b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
24890b57cec5SDimitry Andric{
24900b57cec5SDimitry Andric    const_iterator __p = __lower_bound(__v, __root(), __end_node());
24910b57cec5SDimitry Andric    if (__p != end() && !value_comp()(__v, *__p))
24920b57cec5SDimitry Andric        return __p;
24930b57cec5SDimitry Andric    return end();
24940b57cec5SDimitry Andric}
24950b57cec5SDimitry Andric
24960b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
24970b57cec5SDimitry Andrictemplate <class _Key>
24980b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type
24990b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
25000b57cec5SDimitry Andric{
25010b57cec5SDimitry Andric    __node_pointer __rt = __root();
25020b57cec5SDimitry Andric    while (__rt != nullptr)
25030b57cec5SDimitry Andric    {
25040b57cec5SDimitry Andric        if (value_comp()(__k, __rt->__value_))
25050b57cec5SDimitry Andric        {
25060b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__left_);
25070b57cec5SDimitry Andric        }
25080b57cec5SDimitry Andric        else if (value_comp()(__rt->__value_, __k))
25090b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__right_);
25100b57cec5SDimitry Andric        else
25110b57cec5SDimitry Andric            return 1;
25120b57cec5SDimitry Andric    }
25130b57cec5SDimitry Andric    return 0;
25140b57cec5SDimitry Andric}
25150b57cec5SDimitry Andric
25160b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
25170b57cec5SDimitry Andrictemplate <class _Key>
25180b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type
25190b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
25200b57cec5SDimitry Andric{
25210b57cec5SDimitry Andric    __iter_pointer __result = __end_node();
25220b57cec5SDimitry Andric    __node_pointer __rt = __root();
25230b57cec5SDimitry Andric    while (__rt != nullptr)
25240b57cec5SDimitry Andric    {
25250b57cec5SDimitry Andric        if (value_comp()(__k, __rt->__value_))
25260b57cec5SDimitry Andric        {
25270b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__rt);
25280b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__left_);
25290b57cec5SDimitry Andric        }
25300b57cec5SDimitry Andric        else if (value_comp()(__rt->__value_, __k))
25310b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__right_);
25320b57cec5SDimitry Andric        else
2533*5f757f3fSDimitry Andric            return std::distance(
25340b57cec5SDimitry Andric                __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
25350b57cec5SDimitry Andric                __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
25360b57cec5SDimitry Andric            );
25370b57cec5SDimitry Andric    }
25380b57cec5SDimitry Andric    return 0;
25390b57cec5SDimitry Andric}
25400b57cec5SDimitry Andric
25410b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
25420b57cec5SDimitry Andrictemplate <class _Key>
25430b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
25440b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
25450b57cec5SDimitry Andric                                                 __node_pointer __root,
25460b57cec5SDimitry Andric                                                 __iter_pointer __result)
25470b57cec5SDimitry Andric{
25480b57cec5SDimitry Andric    while (__root != nullptr)
25490b57cec5SDimitry Andric    {
25500b57cec5SDimitry Andric        if (!value_comp()(__root->__value_, __v))
25510b57cec5SDimitry Andric        {
25520b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__root);
25530b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__left_);
25540b57cec5SDimitry Andric        }
25550b57cec5SDimitry Andric        else
25560b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__right_);
25570b57cec5SDimitry Andric    }
25580b57cec5SDimitry Andric    return iterator(__result);
25590b57cec5SDimitry Andric}
25600b57cec5SDimitry Andric
25610b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
25620b57cec5SDimitry Andrictemplate <class _Key>
25630b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::const_iterator
25640b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
25650b57cec5SDimitry Andric                                                 __node_pointer __root,
25660b57cec5SDimitry Andric                                                 __iter_pointer __result) const
25670b57cec5SDimitry Andric{
25680b57cec5SDimitry Andric    while (__root != nullptr)
25690b57cec5SDimitry Andric    {
25700b57cec5SDimitry Andric        if (!value_comp()(__root->__value_, __v))
25710b57cec5SDimitry Andric        {
25720b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__root);
25730b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__left_);
25740b57cec5SDimitry Andric        }
25750b57cec5SDimitry Andric        else
25760b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__right_);
25770b57cec5SDimitry Andric    }
25780b57cec5SDimitry Andric    return const_iterator(__result);
25790b57cec5SDimitry Andric}
25800b57cec5SDimitry Andric
25810b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
25820b57cec5SDimitry Andrictemplate <class _Key>
25830b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator
25840b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
25850b57cec5SDimitry Andric                                                 __node_pointer __root,
25860b57cec5SDimitry Andric                                                 __iter_pointer __result)
25870b57cec5SDimitry Andric{
25880b57cec5SDimitry Andric    while (__root != nullptr)
25890b57cec5SDimitry Andric    {
25900b57cec5SDimitry Andric        if (value_comp()(__v, __root->__value_))
25910b57cec5SDimitry Andric        {
25920b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__root);
25930b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__left_);
25940b57cec5SDimitry Andric        }
25950b57cec5SDimitry Andric        else
25960b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__right_);
25970b57cec5SDimitry Andric    }
25980b57cec5SDimitry Andric    return iterator(__result);
25990b57cec5SDimitry Andric}
26000b57cec5SDimitry Andric
26010b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
26020b57cec5SDimitry Andrictemplate <class _Key>
26030b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::const_iterator
26040b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
26050b57cec5SDimitry Andric                                                 __node_pointer __root,
26060b57cec5SDimitry Andric                                                 __iter_pointer __result) const
26070b57cec5SDimitry Andric{
26080b57cec5SDimitry Andric    while (__root != nullptr)
26090b57cec5SDimitry Andric    {
26100b57cec5SDimitry Andric        if (value_comp()(__v, __root->__value_))
26110b57cec5SDimitry Andric        {
26120b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__root);
26130b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__left_);
26140b57cec5SDimitry Andric        }
26150b57cec5SDimitry Andric        else
26160b57cec5SDimitry Andric            __root = static_cast<__node_pointer>(__root->__right_);
26170b57cec5SDimitry Andric    }
26180b57cec5SDimitry Andric    return const_iterator(__result);
26190b57cec5SDimitry Andric}
26200b57cec5SDimitry Andric
26210b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
26220b57cec5SDimitry Andrictemplate <class _Key>
26230b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
26240b57cec5SDimitry Andric     typename __tree<_Tp, _Compare, _Allocator>::iterator>
26250b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
26260b57cec5SDimitry Andric{
26270b57cec5SDimitry Andric    typedef pair<iterator, iterator> _Pp;
26280b57cec5SDimitry Andric    __iter_pointer __result = __end_node();
26290b57cec5SDimitry Andric    __node_pointer __rt = __root();
26300b57cec5SDimitry Andric    while (__rt != nullptr)
26310b57cec5SDimitry Andric    {
26320b57cec5SDimitry Andric        if (value_comp()(__k, __rt->__value_))
26330b57cec5SDimitry Andric        {
26340b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__rt);
26350b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__left_);
26360b57cec5SDimitry Andric        }
26370b57cec5SDimitry Andric        else if (value_comp()(__rt->__value_, __k))
26380b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__right_);
26390b57cec5SDimitry Andric        else
26400b57cec5SDimitry Andric            return _Pp(iterator(__rt),
26410b57cec5SDimitry Andric                      iterator(
26420b57cec5SDimitry Andric                          __rt->__right_ != nullptr ?
2643*5f757f3fSDimitry Andric                              static_cast<__iter_pointer>(std::__tree_min(__rt->__right_))
26440b57cec5SDimitry Andric                            : __result));
26450b57cec5SDimitry Andric    }
26460b57cec5SDimitry Andric    return _Pp(iterator(__result), iterator(__result));
26470b57cec5SDimitry Andric}
26480b57cec5SDimitry Andric
26490b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
26500b57cec5SDimitry Andrictemplate <class _Key>
26510b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
26520b57cec5SDimitry Andric     typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
26530b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
26540b57cec5SDimitry Andric{
26550b57cec5SDimitry Andric    typedef pair<const_iterator, const_iterator> _Pp;
26560b57cec5SDimitry Andric    __iter_pointer __result = __end_node();
26570b57cec5SDimitry Andric    __node_pointer __rt = __root();
26580b57cec5SDimitry Andric    while (__rt != nullptr)
26590b57cec5SDimitry Andric    {
26600b57cec5SDimitry Andric        if (value_comp()(__k, __rt->__value_))
26610b57cec5SDimitry Andric        {
26620b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__rt);
26630b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__left_);
26640b57cec5SDimitry Andric        }
26650b57cec5SDimitry Andric        else if (value_comp()(__rt->__value_, __k))
26660b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__right_);
26670b57cec5SDimitry Andric        else
26680b57cec5SDimitry Andric            return _Pp(const_iterator(__rt),
26690b57cec5SDimitry Andric                      const_iterator(
26700b57cec5SDimitry Andric                          __rt->__right_ != nullptr ?
2671*5f757f3fSDimitry Andric                              static_cast<__iter_pointer>(std::__tree_min(__rt->__right_))
26720b57cec5SDimitry Andric                            : __result));
26730b57cec5SDimitry Andric    }
26740b57cec5SDimitry Andric    return _Pp(const_iterator(__result), const_iterator(__result));
26750b57cec5SDimitry Andric}
26760b57cec5SDimitry Andric
26770b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
26780b57cec5SDimitry Andrictemplate <class _Key>
26790b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
26800b57cec5SDimitry Andric     typename __tree<_Tp, _Compare, _Allocator>::iterator>
26810b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
26820b57cec5SDimitry Andric{
26830b57cec5SDimitry Andric    typedef pair<iterator, iterator> _Pp;
26840b57cec5SDimitry Andric    __iter_pointer __result = __end_node();
26850b57cec5SDimitry Andric    __node_pointer __rt = __root();
26860b57cec5SDimitry Andric    while (__rt != nullptr)
26870b57cec5SDimitry Andric    {
26880b57cec5SDimitry Andric        if (value_comp()(__k, __rt->__value_))
26890b57cec5SDimitry Andric        {
26900b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__rt);
26910b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__left_);
26920b57cec5SDimitry Andric        }
26930b57cec5SDimitry Andric        else if (value_comp()(__rt->__value_, __k))
26940b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__right_);
26950b57cec5SDimitry Andric        else
26960b57cec5SDimitry Andric            return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
26970b57cec5SDimitry Andric                      __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
26980b57cec5SDimitry Andric    }
26990b57cec5SDimitry Andric    return _Pp(iterator(__result), iterator(__result));
27000b57cec5SDimitry Andric}
27010b57cec5SDimitry Andric
27020b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
27030b57cec5SDimitry Andrictemplate <class _Key>
27040b57cec5SDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
27050b57cec5SDimitry Andric     typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
27060b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
27070b57cec5SDimitry Andric{
27080b57cec5SDimitry Andric    typedef pair<const_iterator, const_iterator> _Pp;
27090b57cec5SDimitry Andric    __iter_pointer __result = __end_node();
27100b57cec5SDimitry Andric    __node_pointer __rt = __root();
27110b57cec5SDimitry Andric    while (__rt != nullptr)
27120b57cec5SDimitry Andric    {
27130b57cec5SDimitry Andric        if (value_comp()(__k, __rt->__value_))
27140b57cec5SDimitry Andric        {
27150b57cec5SDimitry Andric            __result = static_cast<__iter_pointer>(__rt);
27160b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__left_);
27170b57cec5SDimitry Andric        }
27180b57cec5SDimitry Andric        else if (value_comp()(__rt->__value_, __k))
27190b57cec5SDimitry Andric            __rt = static_cast<__node_pointer>(__rt->__right_);
27200b57cec5SDimitry Andric        else
27210b57cec5SDimitry Andric            return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
27220b57cec5SDimitry Andric                      __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
27230b57cec5SDimitry Andric    }
27240b57cec5SDimitry Andric    return _Pp(const_iterator(__result), const_iterator(__result));
27250b57cec5SDimitry Andric}
27260b57cec5SDimitry Andric
27270b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
27280b57cec5SDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_holder
27290b57cec5SDimitry Andric__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
27300b57cec5SDimitry Andric{
27310b57cec5SDimitry Andric    __node_pointer __np = __p.__get_np();
27320b57cec5SDimitry Andric    if (__begin_node() == __p.__ptr_)
27330b57cec5SDimitry Andric    {
27340b57cec5SDimitry Andric        if (__np->__right_ != nullptr)
27350b57cec5SDimitry Andric            __begin_node() = static_cast<__iter_pointer>(__np->__right_);
27360b57cec5SDimitry Andric        else
27370b57cec5SDimitry Andric            __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
27380b57cec5SDimitry Andric    }
27390b57cec5SDimitry Andric    --size();
2740*5f757f3fSDimitry Andric    std::__tree_remove(__end_node()->__left_,
27410b57cec5SDimitry Andric                         static_cast<__node_base_pointer>(__np));
27420b57cec5SDimitry Andric    return __node_holder(__np, _Dp(__node_alloc(), true));
27430b57cec5SDimitry Andric}
27440b57cec5SDimitry Andric
27450b57cec5SDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator>
2746*5f757f3fSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI
27470b57cec5SDimitry Andricvoid
27480b57cec5SDimitry Andricswap(__tree<_Tp, _Compare, _Allocator>& __x,
27490b57cec5SDimitry Andric     __tree<_Tp, _Compare, _Allocator>& __y)
27500b57cec5SDimitry Andric    _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
27510b57cec5SDimitry Andric{
27520b57cec5SDimitry Andric    __x.swap(__y);
27530b57cec5SDimitry Andric}
27540b57cec5SDimitry Andric
27550b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
27560b57cec5SDimitry Andric
27570b57cec5SDimitry Andric_LIBCPP_POP_MACROS
27580b57cec5SDimitry Andric
27590b57cec5SDimitry Andric#endif // _LIBCPP___TREE
2760