1*700637cbSDimitry Andric// -*- C++ -*- 2*700637cbSDimitry Andric//===----------------------------------------------------------------------===// 3*700637cbSDimitry Andric// 4*700637cbSDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5*700637cbSDimitry Andric// See https://llvm.org/LICENSE.txt for license information. 6*700637cbSDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7*700637cbSDimitry Andric// 8*700637cbSDimitry Andric//===----------------------------------------------------------------------===// 9*700637cbSDimitry Andric 10*700637cbSDimitry Andric#ifndef _LIBCPP___CXX03___TREE 11*700637cbSDimitry Andric#define _LIBCPP___CXX03___TREE 12*700637cbSDimitry Andric 13*700637cbSDimitry Andric#include <__cxx03/__algorithm/min.h> 14*700637cbSDimitry Andric#include <__cxx03/__assert> 15*700637cbSDimitry Andric#include <__cxx03/__config> 16*700637cbSDimitry Andric#include <__cxx03/__iterator/distance.h> 17*700637cbSDimitry Andric#include <__cxx03/__iterator/iterator_traits.h> 18*700637cbSDimitry Andric#include <__cxx03/__iterator/next.h> 19*700637cbSDimitry Andric#include <__cxx03/__memory/addressof.h> 20*700637cbSDimitry Andric#include <__cxx03/__memory/allocator_traits.h> 21*700637cbSDimitry Andric#include <__cxx03/__memory/compressed_pair.h> 22*700637cbSDimitry Andric#include <__cxx03/__memory/pointer_traits.h> 23*700637cbSDimitry Andric#include <__cxx03/__memory/swap_allocator.h> 24*700637cbSDimitry Andric#include <__cxx03/__memory/unique_ptr.h> 25*700637cbSDimitry Andric#include <__cxx03/__type_traits/can_extract_key.h> 26*700637cbSDimitry Andric#include <__cxx03/__type_traits/conditional.h> 27*700637cbSDimitry Andric#include <__cxx03/__type_traits/invoke.h> 28*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_const.h> 29*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_constructible.h> 30*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_nothrow_assignable.h> 31*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_nothrow_constructible.h> 32*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_pointer.h> 33*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_same.h> 34*700637cbSDimitry Andric#include <__cxx03/__type_traits/is_swappable.h> 35*700637cbSDimitry Andric#include <__cxx03/__type_traits/remove_const_ref.h> 36*700637cbSDimitry Andric#include <__cxx03/__type_traits/remove_cvref.h> 37*700637cbSDimitry Andric#include <__cxx03/__utility/forward.h> 38*700637cbSDimitry Andric#include <__cxx03/__utility/move.h> 39*700637cbSDimitry Andric#include <__cxx03/__utility/pair.h> 40*700637cbSDimitry Andric#include <__cxx03/__utility/swap.h> 41*700637cbSDimitry Andric#include <__cxx03/limits> 42*700637cbSDimitry Andric 43*700637cbSDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 44*700637cbSDimitry Andric# pragma GCC system_header 45*700637cbSDimitry Andric#endif 46*700637cbSDimitry Andric 47*700637cbSDimitry Andric_LIBCPP_PUSH_MACROS 48*700637cbSDimitry Andric#include <__cxx03/__undef_macros> 49*700637cbSDimitry Andric 50*700637cbSDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD 51*700637cbSDimitry Andric 52*700637cbSDimitry Andrictemplate <class, class, class, class> 53*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS map; 54*700637cbSDimitry Andrictemplate <class, class, class, class> 55*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS multimap; 56*700637cbSDimitry Andrictemplate <class, class, class> 57*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS set; 58*700637cbSDimitry Andrictemplate <class, class, class> 59*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS multiset; 60*700637cbSDimitry Andric 61*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 62*700637cbSDimitry Andricclass __tree; 63*700637cbSDimitry Andrictemplate <class _Tp, class _NodePtr, class _DiffType> 64*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __tree_iterator; 65*700637cbSDimitry Andrictemplate <class _Tp, class _ConstNodePtr, class _DiffType> 66*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __tree_const_iterator; 67*700637cbSDimitry Andric 68*700637cbSDimitry Andrictemplate <class _Pointer> 69*700637cbSDimitry Andricclass __tree_end_node; 70*700637cbSDimitry Andrictemplate <class _VoidPtr> 71*700637cbSDimitry Andricclass __tree_node_base; 72*700637cbSDimitry Andrictemplate <class _Tp, class _VoidPtr> 73*700637cbSDimitry Andricclass __tree_node; 74*700637cbSDimitry Andric 75*700637cbSDimitry Andrictemplate <class _Key, class _Value> 76*700637cbSDimitry Andricstruct __value_type; 77*700637cbSDimitry Andric 78*700637cbSDimitry Andrictemplate <class _Allocator> 79*700637cbSDimitry Andricclass __map_node_destructor; 80*700637cbSDimitry Andrictemplate <class _TreeIterator> 81*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __map_iterator; 82*700637cbSDimitry Andrictemplate <class _TreeIterator> 83*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __map_const_iterator; 84*700637cbSDimitry Andric 85*700637cbSDimitry Andric/* 86*700637cbSDimitry Andric 87*700637cbSDimitry Andric_NodePtr algorithms 88*700637cbSDimitry Andric 89*700637cbSDimitry AndricThe algorithms taking _NodePtr are red black tree algorithms. Those 90*700637cbSDimitry Andricalgorithms taking a parameter named __root should assume that __root 91*700637cbSDimitry Andricpoints to a proper red black tree (unless otherwise specified). 92*700637cbSDimitry Andric 93*700637cbSDimitry AndricEach algorithm herein assumes that __root->__parent_ points to a non-null 94*700637cbSDimitry Andricstructure which has a member __left_ which points back to __root. No other 95*700637cbSDimitry Andricmember is read or written to at __root->__parent_. 96*700637cbSDimitry Andric 97*700637cbSDimitry Andric__root->__parent_ will be referred to below (in comments only) as end_node. 98*700637cbSDimitry Andricend_node->__left_ is an externably accessible lvalue for __root, and can be 99*700637cbSDimitry Andricchanged by node insertion and removal (without explicit reference to end_node). 100*700637cbSDimitry Andric 101*700637cbSDimitry AndricAll nodes (with the exception of end_node), even the node referred to as 102*700637cbSDimitry Andric__root, have a non-null __parent_ field. 103*700637cbSDimitry Andric 104*700637cbSDimitry Andric*/ 105*700637cbSDimitry Andric 106*700637cbSDimitry Andric// Returns: true if __x is a left child of its parent, else false 107*700637cbSDimitry Andric// Precondition: __x != nullptr. 108*700637cbSDimitry Andrictemplate <class _NodePtr> 109*700637cbSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI bool __tree_is_left_child(_NodePtr __x) _NOEXCEPT { 110*700637cbSDimitry Andric return __x == __x->__parent_->__left_; 111*700637cbSDimitry Andric} 112*700637cbSDimitry Andric 113*700637cbSDimitry Andric// Determines if the subtree rooted at __x is a proper red black subtree. If 114*700637cbSDimitry Andric// __x is a proper subtree, returns the black height (null counts as 1). If 115*700637cbSDimitry Andric// __x is an improper subtree, returns 0. 116*700637cbSDimitry Andrictemplate <class _NodePtr> 117*700637cbSDimitry Andricunsigned __tree_sub_invariant(_NodePtr __x) { 118*700637cbSDimitry Andric if (__x == nullptr) 119*700637cbSDimitry Andric return 1; 120*700637cbSDimitry Andric // parent consistency checked by caller 121*700637cbSDimitry Andric // check __x->__left_ consistency 122*700637cbSDimitry Andric if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x) 123*700637cbSDimitry Andric return 0; 124*700637cbSDimitry Andric // check __x->__right_ consistency 125*700637cbSDimitry Andric if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x) 126*700637cbSDimitry Andric return 0; 127*700637cbSDimitry Andric // check __x->__left_ != __x->__right_ unless both are nullptr 128*700637cbSDimitry Andric if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr) 129*700637cbSDimitry Andric return 0; 130*700637cbSDimitry Andric // If this is red, neither child can be red 131*700637cbSDimitry Andric if (!__x->__is_black_) { 132*700637cbSDimitry Andric if (__x->__left_ && !__x->__left_->__is_black_) 133*700637cbSDimitry Andric return 0; 134*700637cbSDimitry Andric if (__x->__right_ && !__x->__right_->__is_black_) 135*700637cbSDimitry Andric return 0; 136*700637cbSDimitry Andric } 137*700637cbSDimitry Andric unsigned __h = std::__tree_sub_invariant(__x->__left_); 138*700637cbSDimitry Andric if (__h == 0) 139*700637cbSDimitry Andric return 0; // invalid left subtree 140*700637cbSDimitry Andric if (__h != std::__tree_sub_invariant(__x->__right_)) 141*700637cbSDimitry Andric return 0; // invalid or different height right subtree 142*700637cbSDimitry Andric return __h + __x->__is_black_; // return black height of this node 143*700637cbSDimitry Andric} 144*700637cbSDimitry Andric 145*700637cbSDimitry Andric// Determines if the red black tree rooted at __root is a proper red black tree. 146*700637cbSDimitry Andric// __root == nullptr is a proper tree. Returns true is __root is a proper 147*700637cbSDimitry Andric// red black tree, else returns false. 148*700637cbSDimitry Andrictemplate <class _NodePtr> 149*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI bool __tree_invariant(_NodePtr __root) { 150*700637cbSDimitry Andric if (__root == nullptr) 151*700637cbSDimitry Andric return true; 152*700637cbSDimitry Andric // check __x->__parent_ consistency 153*700637cbSDimitry Andric if (__root->__parent_ == nullptr) 154*700637cbSDimitry Andric return false; 155*700637cbSDimitry Andric if (!std::__tree_is_left_child(__root)) 156*700637cbSDimitry Andric return false; 157*700637cbSDimitry Andric // root must be black 158*700637cbSDimitry Andric if (!__root->__is_black_) 159*700637cbSDimitry Andric return false; 160*700637cbSDimitry Andric // do normal node checks 161*700637cbSDimitry Andric return std::__tree_sub_invariant(__root) != 0; 162*700637cbSDimitry Andric} 163*700637cbSDimitry Andric 164*700637cbSDimitry Andric// Returns: pointer to the left-most node under __x. 165*700637cbSDimitry Andrictemplate <class _NodePtr> 166*700637cbSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_min(_NodePtr __x) _NOEXCEPT { 167*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null"); 168*700637cbSDimitry Andric while (__x->__left_ != nullptr) 169*700637cbSDimitry Andric __x = __x->__left_; 170*700637cbSDimitry Andric return __x; 171*700637cbSDimitry Andric} 172*700637cbSDimitry Andric 173*700637cbSDimitry Andric// Returns: pointer to the right-most node under __x. 174*700637cbSDimitry Andrictemplate <class _NodePtr> 175*700637cbSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_max(_NodePtr __x) _NOEXCEPT { 176*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null"); 177*700637cbSDimitry Andric while (__x->__right_ != nullptr) 178*700637cbSDimitry Andric __x = __x->__right_; 179*700637cbSDimitry Andric return __x; 180*700637cbSDimitry Andric} 181*700637cbSDimitry Andric 182*700637cbSDimitry Andric// Returns: pointer to the next in-order node after __x. 183*700637cbSDimitry Andrictemplate <class _NodePtr> 184*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI _NodePtr __tree_next(_NodePtr __x) _NOEXCEPT { 185*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null"); 186*700637cbSDimitry Andric if (__x->__right_ != nullptr) 187*700637cbSDimitry Andric return std::__tree_min(__x->__right_); 188*700637cbSDimitry Andric while (!std::__tree_is_left_child(__x)) 189*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 190*700637cbSDimitry Andric return __x->__parent_unsafe(); 191*700637cbSDimitry Andric} 192*700637cbSDimitry Andric 193*700637cbSDimitry Andrictemplate <class _EndNodePtr, class _NodePtr> 194*700637cbSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _EndNodePtr __tree_next_iter(_NodePtr __x) _NOEXCEPT { 195*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null"); 196*700637cbSDimitry Andric if (__x->__right_ != nullptr) 197*700637cbSDimitry Andric return static_cast<_EndNodePtr>(std::__tree_min(__x->__right_)); 198*700637cbSDimitry Andric while (!std::__tree_is_left_child(__x)) 199*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 200*700637cbSDimitry Andric return static_cast<_EndNodePtr>(__x->__parent_); 201*700637cbSDimitry Andric} 202*700637cbSDimitry Andric 203*700637cbSDimitry Andric// Returns: pointer to the previous in-order node before __x. 204*700637cbSDimitry Andric// Note: __x may be the end node. 205*700637cbSDimitry Andrictemplate <class _NodePtr, class _EndNodePtr> 206*700637cbSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_prev_iter(_EndNodePtr __x) _NOEXCEPT { 207*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null"); 208*700637cbSDimitry Andric if (__x->__left_ != nullptr) 209*700637cbSDimitry Andric return std::__tree_max(__x->__left_); 210*700637cbSDimitry Andric _NodePtr __xx = static_cast<_NodePtr>(__x); 211*700637cbSDimitry Andric while (std::__tree_is_left_child(__xx)) 212*700637cbSDimitry Andric __xx = __xx->__parent_unsafe(); 213*700637cbSDimitry Andric return __xx->__parent_unsafe(); 214*700637cbSDimitry Andric} 215*700637cbSDimitry Andric 216*700637cbSDimitry Andric// Returns: pointer to a node which has no children 217*700637cbSDimitry Andrictemplate <class _NodePtr> 218*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI _NodePtr __tree_leaf(_NodePtr __x) _NOEXCEPT { 219*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null"); 220*700637cbSDimitry Andric while (true) { 221*700637cbSDimitry Andric if (__x->__left_ != nullptr) { 222*700637cbSDimitry Andric __x = __x->__left_; 223*700637cbSDimitry Andric continue; 224*700637cbSDimitry Andric } 225*700637cbSDimitry Andric if (__x->__right_ != nullptr) { 226*700637cbSDimitry Andric __x = __x->__right_; 227*700637cbSDimitry Andric continue; 228*700637cbSDimitry Andric } 229*700637cbSDimitry Andric break; 230*700637cbSDimitry Andric } 231*700637cbSDimitry Andric return __x; 232*700637cbSDimitry Andric} 233*700637cbSDimitry Andric 234*700637cbSDimitry Andric// Effects: Makes __x->__right_ the subtree root with __x as its left child 235*700637cbSDimitry Andric// while preserving in-order order. 236*700637cbSDimitry Andrictemplate <class _NodePtr> 237*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI void __tree_left_rotate(_NodePtr __x) _NOEXCEPT { 238*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null"); 239*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x->__right_ != nullptr, "node should have a right child"); 240*700637cbSDimitry Andric _NodePtr __y = __x->__right_; 241*700637cbSDimitry Andric __x->__right_ = __y->__left_; 242*700637cbSDimitry Andric if (__x->__right_ != nullptr) 243*700637cbSDimitry Andric __x->__right_->__set_parent(__x); 244*700637cbSDimitry Andric __y->__parent_ = __x->__parent_; 245*700637cbSDimitry Andric if (std::__tree_is_left_child(__x)) 246*700637cbSDimitry Andric __x->__parent_->__left_ = __y; 247*700637cbSDimitry Andric else 248*700637cbSDimitry Andric __x->__parent_unsafe()->__right_ = __y; 249*700637cbSDimitry Andric __y->__left_ = __x; 250*700637cbSDimitry Andric __x->__set_parent(__y); 251*700637cbSDimitry Andric} 252*700637cbSDimitry Andric 253*700637cbSDimitry Andric// Effects: Makes __x->__left_ the subtree root with __x as its right child 254*700637cbSDimitry Andric// while preserving in-order order. 255*700637cbSDimitry Andrictemplate <class _NodePtr> 256*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI void __tree_right_rotate(_NodePtr __x) _NOEXCEPT { 257*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null"); 258*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x->__left_ != nullptr, "node should have a left child"); 259*700637cbSDimitry Andric _NodePtr __y = __x->__left_; 260*700637cbSDimitry Andric __x->__left_ = __y->__right_; 261*700637cbSDimitry Andric if (__x->__left_ != nullptr) 262*700637cbSDimitry Andric __x->__left_->__set_parent(__x); 263*700637cbSDimitry Andric __y->__parent_ = __x->__parent_; 264*700637cbSDimitry Andric if (std::__tree_is_left_child(__x)) 265*700637cbSDimitry Andric __x->__parent_->__left_ = __y; 266*700637cbSDimitry Andric else 267*700637cbSDimitry Andric __x->__parent_unsafe()->__right_ = __y; 268*700637cbSDimitry Andric __y->__right_ = __x; 269*700637cbSDimitry Andric __x->__set_parent(__y); 270*700637cbSDimitry Andric} 271*700637cbSDimitry Andric 272*700637cbSDimitry Andric// Effects: Rebalances __root after attaching __x to a leaf. 273*700637cbSDimitry Andric// Precondition: __x has no children. 274*700637cbSDimitry Andric// __x == __root or == a direct or indirect child of __root. 275*700637cbSDimitry Andric// If __x were to be unlinked from __root (setting __root to 276*700637cbSDimitry Andric// nullptr if __root == __x), __tree_invariant(__root) == true. 277*700637cbSDimitry Andric// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_ 278*700637cbSDimitry Andric// may be different than the value passed in as __root. 279*700637cbSDimitry Andrictemplate <class _NodePtr> 280*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI void __tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT { 281*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root of the tree shouldn't be null"); 282*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Can't attach null node to a leaf"); 283*700637cbSDimitry Andric __x->__is_black_ = __x == __root; 284*700637cbSDimitry Andric while (__x != __root && !__x->__parent_unsafe()->__is_black_) { 285*700637cbSDimitry Andric // __x->__parent_ != __root because __x->__parent_->__is_black == false 286*700637cbSDimitry Andric if (std::__tree_is_left_child(__x->__parent_unsafe())) { 287*700637cbSDimitry Andric _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_; 288*700637cbSDimitry Andric if (__y != nullptr && !__y->__is_black_) { 289*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 290*700637cbSDimitry Andric __x->__is_black_ = true; 291*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 292*700637cbSDimitry Andric __x->__is_black_ = __x == __root; 293*700637cbSDimitry Andric __y->__is_black_ = true; 294*700637cbSDimitry Andric } else { 295*700637cbSDimitry Andric if (!std::__tree_is_left_child(__x)) { 296*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 297*700637cbSDimitry Andric std::__tree_left_rotate(__x); 298*700637cbSDimitry Andric } 299*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 300*700637cbSDimitry Andric __x->__is_black_ = true; 301*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 302*700637cbSDimitry Andric __x->__is_black_ = false; 303*700637cbSDimitry Andric std::__tree_right_rotate(__x); 304*700637cbSDimitry Andric break; 305*700637cbSDimitry Andric } 306*700637cbSDimitry Andric } else { 307*700637cbSDimitry Andric _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_; 308*700637cbSDimitry Andric if (__y != nullptr && !__y->__is_black_) { 309*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 310*700637cbSDimitry Andric __x->__is_black_ = true; 311*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 312*700637cbSDimitry Andric __x->__is_black_ = __x == __root; 313*700637cbSDimitry Andric __y->__is_black_ = true; 314*700637cbSDimitry Andric } else { 315*700637cbSDimitry Andric if (std::__tree_is_left_child(__x)) { 316*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 317*700637cbSDimitry Andric std::__tree_right_rotate(__x); 318*700637cbSDimitry Andric } 319*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 320*700637cbSDimitry Andric __x->__is_black_ = true; 321*700637cbSDimitry Andric __x = __x->__parent_unsafe(); 322*700637cbSDimitry Andric __x->__is_black_ = false; 323*700637cbSDimitry Andric std::__tree_left_rotate(__x); 324*700637cbSDimitry Andric break; 325*700637cbSDimitry Andric } 326*700637cbSDimitry Andric } 327*700637cbSDimitry Andric } 328*700637cbSDimitry Andric} 329*700637cbSDimitry Andric 330*700637cbSDimitry Andric// Precondition: __z == __root or == a direct or indirect child of __root. 331*700637cbSDimitry Andric// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed. 332*700637cbSDimitry Andric// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_ 333*700637cbSDimitry Andric// nor any of its children refer to __z. end_node->__left_ 334*700637cbSDimitry Andric// may be different than the value passed in as __root. 335*700637cbSDimitry Andrictemplate <class _NodePtr> 336*700637cbSDimitry Andric_LIBCPP_HIDE_FROM_ABI void __tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT { 337*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root node should not be null"); 338*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(__z != nullptr, "The node to remove should not be null"); 339*700637cbSDimitry Andric _LIBCPP_ASSERT_INTERNAL(std::__tree_invariant(__root), "The tree invariants should hold"); 340*700637cbSDimitry Andric // __z will be removed from the tree. Client still needs to destruct/deallocate it 341*700637cbSDimitry Andric // __y is either __z, or if __z has two children, __tree_next(__z). 342*700637cbSDimitry Andric // __y will have at most one child. 343*700637cbSDimitry Andric // __y will be the initial hole in the tree (make the hole at a leaf) 344*700637cbSDimitry Andric _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ? __z : std::__tree_next(__z); 345*700637cbSDimitry Andric // __x is __y's possibly null single child 346*700637cbSDimitry Andric _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_; 347*700637cbSDimitry Andric // __w is __x's possibly null uncle (will become __x's sibling) 348*700637cbSDimitry Andric _NodePtr __w = nullptr; 349*700637cbSDimitry Andric // link __x to __y's parent, and find __w 350*700637cbSDimitry Andric if (__x != nullptr) 351*700637cbSDimitry Andric __x->__parent_ = __y->__parent_; 352*700637cbSDimitry Andric if (std::__tree_is_left_child(__y)) { 353*700637cbSDimitry Andric __y->__parent_->__left_ = __x; 354*700637cbSDimitry Andric if (__y != __root) 355*700637cbSDimitry Andric __w = __y->__parent_unsafe()->__right_; 356*700637cbSDimitry Andric else 357*700637cbSDimitry Andric __root = __x; // __w == nullptr 358*700637cbSDimitry Andric } else { 359*700637cbSDimitry Andric __y->__parent_unsafe()->__right_ = __x; 360*700637cbSDimitry Andric // __y can't be root if it is a right child 361*700637cbSDimitry Andric __w = __y->__parent_->__left_; 362*700637cbSDimitry Andric } 363*700637cbSDimitry Andric bool __removed_black = __y->__is_black_; 364*700637cbSDimitry Andric // If we didn't remove __z, do so now by splicing in __y for __z, 365*700637cbSDimitry Andric // but copy __z's color. This does not impact __x or __w. 366*700637cbSDimitry Andric if (__y != __z) { 367*700637cbSDimitry Andric // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr 368*700637cbSDimitry Andric __y->__parent_ = __z->__parent_; 369*700637cbSDimitry Andric if (std::__tree_is_left_child(__z)) 370*700637cbSDimitry Andric __y->__parent_->__left_ = __y; 371*700637cbSDimitry Andric else 372*700637cbSDimitry Andric __y->__parent_unsafe()->__right_ = __y; 373*700637cbSDimitry Andric __y->__left_ = __z->__left_; 374*700637cbSDimitry Andric __y->__left_->__set_parent(__y); 375*700637cbSDimitry Andric __y->__right_ = __z->__right_; 376*700637cbSDimitry Andric if (__y->__right_ != nullptr) 377*700637cbSDimitry Andric __y->__right_->__set_parent(__y); 378*700637cbSDimitry Andric __y->__is_black_ = __z->__is_black_; 379*700637cbSDimitry Andric if (__root == __z) 380*700637cbSDimitry Andric __root = __y; 381*700637cbSDimitry Andric } 382*700637cbSDimitry Andric // There is no need to rebalance if we removed a red, or if we removed 383*700637cbSDimitry Andric // the last node. 384*700637cbSDimitry Andric if (__removed_black && __root != nullptr) { 385*700637cbSDimitry Andric // Rebalance: 386*700637cbSDimitry Andric // __x has an implicit black color (transferred from the removed __y) 387*700637cbSDimitry Andric // associated with it, no matter what its color is. 388*700637cbSDimitry Andric // If __x is __root (in which case it can't be null), it is supposed 389*700637cbSDimitry Andric // to be black anyway, and if it is doubly black, then the double 390*700637cbSDimitry Andric // can just be ignored. 391*700637cbSDimitry Andric // If __x is red (in which case it can't be null), then it can absorb 392*700637cbSDimitry Andric // the implicit black just by setting its color to black. 393*700637cbSDimitry Andric // Since __y was black and only had one child (which __x points to), __x 394*700637cbSDimitry Andric // is either red with no children, else null, otherwise __y would have 395*700637cbSDimitry Andric // different black heights under left and right pointers. 396*700637cbSDimitry Andric // if (__x == __root || __x != nullptr && !__x->__is_black_) 397*700637cbSDimitry Andric if (__x != nullptr) 398*700637cbSDimitry Andric __x->__is_black_ = true; 399*700637cbSDimitry Andric else { 400*700637cbSDimitry Andric // Else __x isn't root, and is "doubly black", even though it may 401*700637cbSDimitry Andric // be null. __w can not be null here, else the parent would 402*700637cbSDimitry Andric // see a black height >= 2 on the __x side and a black height 403*700637cbSDimitry Andric // of 1 on the __w side (__w must be a non-null black or a red 404*700637cbSDimitry Andric // with a non-null black child). 405*700637cbSDimitry Andric while (true) { 406*700637cbSDimitry Andric if (!std::__tree_is_left_child(__w)) // if x is left child 407*700637cbSDimitry Andric { 408*700637cbSDimitry Andric if (!__w->__is_black_) { 409*700637cbSDimitry Andric __w->__is_black_ = true; 410*700637cbSDimitry Andric __w->__parent_unsafe()->__is_black_ = false; 411*700637cbSDimitry Andric std::__tree_left_rotate(__w->__parent_unsafe()); 412*700637cbSDimitry Andric // __x is still valid 413*700637cbSDimitry Andric // reset __root only if necessary 414*700637cbSDimitry Andric if (__root == __w->__left_) 415*700637cbSDimitry Andric __root = __w; 416*700637cbSDimitry Andric // reset sibling, and it still can't be null 417*700637cbSDimitry Andric __w = __w->__left_->__right_; 418*700637cbSDimitry Andric } 419*700637cbSDimitry Andric // __w->__is_black_ is now true, __w may have null children 420*700637cbSDimitry Andric if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && 421*700637cbSDimitry Andric (__w->__right_ == nullptr || __w->__right_->__is_black_)) { 422*700637cbSDimitry Andric __w->__is_black_ = false; 423*700637cbSDimitry Andric __x = __w->__parent_unsafe(); 424*700637cbSDimitry Andric // __x can no longer be null 425*700637cbSDimitry Andric if (__x == __root || !__x->__is_black_) { 426*700637cbSDimitry Andric __x->__is_black_ = true; 427*700637cbSDimitry Andric break; 428*700637cbSDimitry Andric } 429*700637cbSDimitry Andric // reset sibling, and it still can't be null 430*700637cbSDimitry Andric __w = std::__tree_is_left_child(__x) ? __x->__parent_unsafe()->__right_ : __x->__parent_->__left_; 431*700637cbSDimitry Andric // continue; 432*700637cbSDimitry Andric } else // __w has a red child 433*700637cbSDimitry Andric { 434*700637cbSDimitry Andric if (__w->__right_ == nullptr || __w->__right_->__is_black_) { 435*700637cbSDimitry Andric // __w left child is non-null and red 436*700637cbSDimitry Andric __w->__left_->__is_black_ = true; 437*700637cbSDimitry Andric __w->__is_black_ = false; 438*700637cbSDimitry Andric std::__tree_right_rotate(__w); 439*700637cbSDimitry Andric // __w is known not to be root, so root hasn't changed 440*700637cbSDimitry Andric // reset sibling, and it still can't be null 441*700637cbSDimitry Andric __w = __w->__parent_unsafe(); 442*700637cbSDimitry Andric } 443*700637cbSDimitry Andric // __w has a right red child, left child may be null 444*700637cbSDimitry Andric __w->__is_black_ = __w->__parent_unsafe()->__is_black_; 445*700637cbSDimitry Andric __w->__parent_unsafe()->__is_black_ = true; 446*700637cbSDimitry Andric __w->__right_->__is_black_ = true; 447*700637cbSDimitry Andric std::__tree_left_rotate(__w->__parent_unsafe()); 448*700637cbSDimitry Andric break; 449*700637cbSDimitry Andric } 450*700637cbSDimitry Andric } else { 451*700637cbSDimitry Andric if (!__w->__is_black_) { 452*700637cbSDimitry Andric __w->__is_black_ = true; 453*700637cbSDimitry Andric __w->__parent_unsafe()->__is_black_ = false; 454*700637cbSDimitry Andric std::__tree_right_rotate(__w->__parent_unsafe()); 455*700637cbSDimitry Andric // __x is still valid 456*700637cbSDimitry Andric // reset __root only if necessary 457*700637cbSDimitry Andric if (__root == __w->__right_) 458*700637cbSDimitry Andric __root = __w; 459*700637cbSDimitry Andric // reset sibling, and it still can't be null 460*700637cbSDimitry Andric __w = __w->__right_->__left_; 461*700637cbSDimitry Andric } 462*700637cbSDimitry Andric // __w->__is_black_ is now true, __w may have null children 463*700637cbSDimitry Andric if ((__w->__left_ == nullptr || __w->__left_->__is_black_) && 464*700637cbSDimitry Andric (__w->__right_ == nullptr || __w->__right_->__is_black_)) { 465*700637cbSDimitry Andric __w->__is_black_ = false; 466*700637cbSDimitry Andric __x = __w->__parent_unsafe(); 467*700637cbSDimitry Andric // __x can no longer be null 468*700637cbSDimitry Andric if (!__x->__is_black_ || __x == __root) { 469*700637cbSDimitry Andric __x->__is_black_ = true; 470*700637cbSDimitry Andric break; 471*700637cbSDimitry Andric } 472*700637cbSDimitry Andric // reset sibling, and it still can't be null 473*700637cbSDimitry Andric __w = std::__tree_is_left_child(__x) ? __x->__parent_unsafe()->__right_ : __x->__parent_->__left_; 474*700637cbSDimitry Andric // continue; 475*700637cbSDimitry Andric } else // __w has a red child 476*700637cbSDimitry Andric { 477*700637cbSDimitry Andric if (__w->__left_ == nullptr || __w->__left_->__is_black_) { 478*700637cbSDimitry Andric // __w right child is non-null and red 479*700637cbSDimitry Andric __w->__right_->__is_black_ = true; 480*700637cbSDimitry Andric __w->__is_black_ = false; 481*700637cbSDimitry Andric std::__tree_left_rotate(__w); 482*700637cbSDimitry Andric // __w is known not to be root, so root hasn't changed 483*700637cbSDimitry Andric // reset sibling, and it still can't be null 484*700637cbSDimitry Andric __w = __w->__parent_unsafe(); 485*700637cbSDimitry Andric } 486*700637cbSDimitry Andric // __w has a left red child, right child may be null 487*700637cbSDimitry Andric __w->__is_black_ = __w->__parent_unsafe()->__is_black_; 488*700637cbSDimitry Andric __w->__parent_unsafe()->__is_black_ = true; 489*700637cbSDimitry Andric __w->__left_->__is_black_ = true; 490*700637cbSDimitry Andric std::__tree_right_rotate(__w->__parent_unsafe()); 491*700637cbSDimitry Andric break; 492*700637cbSDimitry Andric } 493*700637cbSDimitry Andric } 494*700637cbSDimitry Andric } 495*700637cbSDimitry Andric } 496*700637cbSDimitry Andric } 497*700637cbSDimitry Andric} 498*700637cbSDimitry Andric 499*700637cbSDimitry Andric// node traits 500*700637cbSDimitry Andric 501*700637cbSDimitry Andrictemplate <class _Tp> 502*700637cbSDimitry Andricstruct __is_tree_value_type_imp : false_type {}; 503*700637cbSDimitry Andric 504*700637cbSDimitry Andrictemplate <class _Key, class _Value> 505*700637cbSDimitry Andricstruct __is_tree_value_type_imp<__value_type<_Key, _Value> > : true_type {}; 506*700637cbSDimitry Andric 507*700637cbSDimitry Andrictemplate <class... _Args> 508*700637cbSDimitry Andricstruct __is_tree_value_type : false_type {}; 509*700637cbSDimitry Andric 510*700637cbSDimitry Andrictemplate <class _One> 511*700637cbSDimitry Andricstruct __is_tree_value_type<_One> : __is_tree_value_type_imp<__remove_cvref_t<_One> > {}; 512*700637cbSDimitry Andric 513*700637cbSDimitry Andrictemplate <class _Tp> 514*700637cbSDimitry Andricstruct __tree_key_value_types { 515*700637cbSDimitry Andric typedef _Tp key_type; 516*700637cbSDimitry Andric typedef _Tp __node_value_type; 517*700637cbSDimitry Andric typedef _Tp __container_value_type; 518*700637cbSDimitry Andric static const bool __is_map = false; 519*700637cbSDimitry Andric 520*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(_Tp const& __v) { return __v; } 521*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(__node_value_type const& __v) { return __v; } 522*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) { return std::addressof(__n); } 523*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __container_value_type&& __move(__node_value_type& __v) { return std::move(__v); } 524*700637cbSDimitry Andric}; 525*700637cbSDimitry Andric 526*700637cbSDimitry Andrictemplate <class _Key, class _Tp> 527*700637cbSDimitry Andricstruct __tree_key_value_types<__value_type<_Key, _Tp> > { 528*700637cbSDimitry Andric typedef _Key key_type; 529*700637cbSDimitry Andric typedef _Tp mapped_type; 530*700637cbSDimitry Andric typedef __value_type<_Key, _Tp> __node_value_type; 531*700637cbSDimitry Andric typedef pair<const _Key, _Tp> __container_value_type; 532*700637cbSDimitry Andric typedef __container_value_type __map_value_type; 533*700637cbSDimitry Andric static const bool __is_map = true; 534*700637cbSDimitry Andric 535*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(__node_value_type const& __t) { 536*700637cbSDimitry Andric return __t.__get_value().first; 537*700637cbSDimitry Andric } 538*700637cbSDimitry Andric 539*700637cbSDimitry Andric template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0> 540*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(_Up& __t) { 541*700637cbSDimitry Andric return __t.first; 542*700637cbSDimitry Andric } 543*700637cbSDimitry Andric 544*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(__node_value_type const& __t) { 545*700637cbSDimitry Andric return __t.__get_value(); 546*700637cbSDimitry Andric } 547*700637cbSDimitry Andric 548*700637cbSDimitry Andric template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0> 549*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(_Up& __t) { 550*700637cbSDimitry Andric return __t; 551*700637cbSDimitry Andric } 552*700637cbSDimitry Andric 553*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) { 554*700637cbSDimitry Andric return std::addressof(__n.__get_value()); 555*700637cbSDimitry Andric } 556*700637cbSDimitry Andric 557*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) { return __v.__move(); } 558*700637cbSDimitry Andric}; 559*700637cbSDimitry Andric 560*700637cbSDimitry Andrictemplate <class _VoidPtr> 561*700637cbSDimitry Andricstruct __tree_node_base_types { 562*700637cbSDimitry Andric typedef _VoidPtr __void_pointer; 563*700637cbSDimitry Andric 564*700637cbSDimitry Andric typedef __tree_node_base<__void_pointer> __node_base_type; 565*700637cbSDimitry Andric typedef __rebind_pointer_t<_VoidPtr, __node_base_type> __node_base_pointer; 566*700637cbSDimitry Andric 567*700637cbSDimitry Andric typedef __tree_end_node<__node_base_pointer> __end_node_type; 568*700637cbSDimitry Andric typedef __rebind_pointer_t<_VoidPtr, __end_node_type> __end_node_pointer; 569*700637cbSDimitry Andric#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB) 570*700637cbSDimitry Andric typedef __end_node_pointer __parent_pointer; 571*700637cbSDimitry Andric#else 572*700637cbSDimitry Andric typedef __conditional_t< is_pointer<__end_node_pointer>::value, __end_node_pointer, __node_base_pointer> 573*700637cbSDimitry Andric __parent_pointer; 574*700637cbSDimitry Andric#endif 575*700637cbSDimitry Andric 576*700637cbSDimitry Andricprivate: 577*700637cbSDimitry Andric static_assert(is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value, 578*700637cbSDimitry Andric "_VoidPtr does not point to unqualified void type"); 579*700637cbSDimitry Andric}; 580*700637cbSDimitry Andric 581*700637cbSDimitry Andrictemplate <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>, bool = _KVTypes::__is_map> 582*700637cbSDimitry Andricstruct __tree_map_pointer_types {}; 583*700637cbSDimitry Andric 584*700637cbSDimitry Andrictemplate <class _Tp, class _AllocPtr, class _KVTypes> 585*700637cbSDimitry Andricstruct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> { 586*700637cbSDimitry Andric typedef typename _KVTypes::__map_value_type _Mv; 587*700637cbSDimitry Andric typedef __rebind_pointer_t<_AllocPtr, _Mv> __map_value_type_pointer; 588*700637cbSDimitry Andric typedef __rebind_pointer_t<_AllocPtr, const _Mv> __const_map_value_type_pointer; 589*700637cbSDimitry Andric}; 590*700637cbSDimitry Andric 591*700637cbSDimitry Andrictemplate <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type> 592*700637cbSDimitry Andricstruct __tree_node_types; 593*700637cbSDimitry Andric 594*700637cbSDimitry Andrictemplate <class _NodePtr, class _Tp, class _VoidPtr> 595*700637cbSDimitry Andricstruct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> > 596*700637cbSDimitry Andric : public __tree_node_base_types<_VoidPtr>, __tree_key_value_types<_Tp>, __tree_map_pointer_types<_Tp, _VoidPtr> { 597*700637cbSDimitry Andric typedef __tree_node_base_types<_VoidPtr> __base; 598*700637cbSDimitry Andric typedef __tree_key_value_types<_Tp> __key_base; 599*700637cbSDimitry Andric typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base; 600*700637cbSDimitry Andric 601*700637cbSDimitry Andricpublic: 602*700637cbSDimitry Andric typedef typename pointer_traits<_NodePtr>::element_type __node_type; 603*700637cbSDimitry Andric typedef _NodePtr __node_pointer; 604*700637cbSDimitry Andric 605*700637cbSDimitry Andric typedef _Tp __node_value_type; 606*700637cbSDimitry Andric typedef __rebind_pointer_t<_VoidPtr, __node_value_type> __node_value_type_pointer; 607*700637cbSDimitry Andric typedef __rebind_pointer_t<_VoidPtr, const __node_value_type> __const_node_value_type_pointer; 608*700637cbSDimitry Andric#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB) 609*700637cbSDimitry Andric typedef typename __base::__end_node_pointer __iter_pointer; 610*700637cbSDimitry Andric#else 611*700637cbSDimitry Andric typedef __conditional_t< is_pointer<__node_pointer>::value, typename __base::__end_node_pointer, __node_pointer> 612*700637cbSDimitry Andric __iter_pointer; 613*700637cbSDimitry Andric#endif 614*700637cbSDimitry Andric 615*700637cbSDimitry Andricprivate: 616*700637cbSDimitry Andric static_assert(!is_const<__node_type>::value, "_NodePtr should never be a pointer to const"); 617*700637cbSDimitry Andric static_assert(is_same<__rebind_pointer_t<_VoidPtr, __node_type>, _NodePtr>::value, 618*700637cbSDimitry Andric "_VoidPtr does not rebind to _NodePtr."); 619*700637cbSDimitry Andric}; 620*700637cbSDimitry Andric 621*700637cbSDimitry Andrictemplate <class _ValueTp, class _VoidPtr> 622*700637cbSDimitry Andricstruct __make_tree_node_types { 623*700637cbSDimitry Andric typedef __rebind_pointer_t<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> > _NodePtr; 624*700637cbSDimitry Andric typedef __tree_node_types<_NodePtr> type; 625*700637cbSDimitry Andric}; 626*700637cbSDimitry Andric 627*700637cbSDimitry Andric// node 628*700637cbSDimitry Andric 629*700637cbSDimitry Andrictemplate <class _Pointer> 630*700637cbSDimitry Andricclass __tree_end_node { 631*700637cbSDimitry Andricpublic: 632*700637cbSDimitry Andric typedef _Pointer pointer; 633*700637cbSDimitry Andric pointer __left_; 634*700637cbSDimitry Andric 635*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_end_node() _NOEXCEPT : __left_() {} 636*700637cbSDimitry Andric}; 637*700637cbSDimitry Andric 638*700637cbSDimitry Andrictemplate <class _VoidPtr> 639*700637cbSDimitry Andricclass _LIBCPP_STANDALONE_DEBUG __tree_node_base : public __tree_node_base_types<_VoidPtr>::__end_node_type { 640*700637cbSDimitry Andric typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes; 641*700637cbSDimitry Andric 642*700637cbSDimitry Andricpublic: 643*700637cbSDimitry Andric typedef typename _NodeBaseTypes::__node_base_pointer pointer; 644*700637cbSDimitry Andric typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer; 645*700637cbSDimitry Andric 646*700637cbSDimitry Andric pointer __right_; 647*700637cbSDimitry Andric __parent_pointer __parent_; 648*700637cbSDimitry Andric bool __is_black_; 649*700637cbSDimitry Andric 650*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pointer __parent_unsafe() const { return static_cast<pointer>(__parent_); } 651*700637cbSDimitry Andric 652*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __set_parent(pointer __p) { __parent_ = static_cast<__parent_pointer>(__p); } 653*700637cbSDimitry Andric 654*700637cbSDimitry Andric ~__tree_node_base() = delete; 655*700637cbSDimitry Andric __tree_node_base(__tree_node_base const&) = delete; 656*700637cbSDimitry Andric __tree_node_base& operator=(__tree_node_base const&) = delete; 657*700637cbSDimitry Andric}; 658*700637cbSDimitry Andric 659*700637cbSDimitry Andrictemplate <class _Tp, class _VoidPtr> 660*700637cbSDimitry Andricclass _LIBCPP_STANDALONE_DEBUG __tree_node : public __tree_node_base<_VoidPtr> { 661*700637cbSDimitry Andricpublic: 662*700637cbSDimitry Andric typedef _Tp __node_value_type; 663*700637cbSDimitry Andric 664*700637cbSDimitry Andric __node_value_type __value_; 665*700637cbSDimitry Andric 666*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; } 667*700637cbSDimitry Andric 668*700637cbSDimitry Andric ~__tree_node() = delete; 669*700637cbSDimitry Andric __tree_node(__tree_node const&) = delete; 670*700637cbSDimitry Andric __tree_node& operator=(__tree_node const&) = delete; 671*700637cbSDimitry Andric}; 672*700637cbSDimitry Andric 673*700637cbSDimitry Andrictemplate <class _Allocator> 674*700637cbSDimitry Andricclass __tree_node_destructor { 675*700637cbSDimitry Andric typedef _Allocator allocator_type; 676*700637cbSDimitry Andric typedef allocator_traits<allocator_type> __alloc_traits; 677*700637cbSDimitry Andric 678*700637cbSDimitry Andricpublic: 679*700637cbSDimitry Andric typedef typename __alloc_traits::pointer pointer; 680*700637cbSDimitry Andric 681*700637cbSDimitry Andricprivate: 682*700637cbSDimitry Andric typedef __tree_node_types<pointer> _NodeTypes; 683*700637cbSDimitry Andric allocator_type& __na_; 684*700637cbSDimitry Andric 685*700637cbSDimitry Andricpublic: 686*700637cbSDimitry Andric bool __value_constructed; 687*700637cbSDimitry Andric 688*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_node_destructor(const __tree_node_destructor&) = default; 689*700637cbSDimitry Andric __tree_node_destructor& operator=(const __tree_node_destructor&) = delete; 690*700637cbSDimitry Andric 691*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT 692*700637cbSDimitry Andric : __na_(__na), 693*700637cbSDimitry Andric __value_constructed(__val) {} 694*700637cbSDimitry Andric 695*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT { 696*700637cbSDimitry Andric if (__value_constructed) 697*700637cbSDimitry Andric __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_)); 698*700637cbSDimitry Andric if (__p) 699*700637cbSDimitry Andric __alloc_traits::deallocate(__na_, __p, 1); 700*700637cbSDimitry Andric } 701*700637cbSDimitry Andric 702*700637cbSDimitry Andric template <class> 703*700637cbSDimitry Andric friend class __map_node_destructor; 704*700637cbSDimitry Andric}; 705*700637cbSDimitry Andric 706*700637cbSDimitry Andrictemplate <class _Tp, class _NodePtr, class _DiffType> 707*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __tree_iterator { 708*700637cbSDimitry Andric typedef __tree_node_types<_NodePtr> _NodeTypes; 709*700637cbSDimitry Andric typedef _NodePtr __node_pointer; 710*700637cbSDimitry Andric typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; 711*700637cbSDimitry Andric typedef typename _NodeTypes::__end_node_pointer __end_node_pointer; 712*700637cbSDimitry Andric typedef typename _NodeTypes::__iter_pointer __iter_pointer; 713*700637cbSDimitry Andric typedef pointer_traits<__node_pointer> __pointer_traits; 714*700637cbSDimitry Andric 715*700637cbSDimitry Andric __iter_pointer __ptr_; 716*700637cbSDimitry Andric 717*700637cbSDimitry Andricpublic: 718*700637cbSDimitry Andric typedef bidirectional_iterator_tag iterator_category; 719*700637cbSDimitry Andric typedef _Tp value_type; 720*700637cbSDimitry Andric typedef _DiffType difference_type; 721*700637cbSDimitry Andric typedef value_type& reference; 722*700637cbSDimitry Andric typedef typename _NodeTypes::__node_value_type_pointer pointer; 723*700637cbSDimitry Andric 724*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT {} 725*700637cbSDimitry Andric 726*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__value_; } 727*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__get_np()->__value_); } 728*700637cbSDimitry Andric 729*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator++() { 730*700637cbSDimitry Andric __ptr_ = static_cast<__iter_pointer>( 731*700637cbSDimitry Andric std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_))); 732*700637cbSDimitry Andric return *this; 733*700637cbSDimitry Andric } 734*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_iterator operator++(int) { 735*700637cbSDimitry Andric __tree_iterator __t(*this); 736*700637cbSDimitry Andric ++(*this); 737*700637cbSDimitry Andric return __t; 738*700637cbSDimitry Andric } 739*700637cbSDimitry Andric 740*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator--() { 741*700637cbSDimitry Andric __ptr_ = static_cast<__iter_pointer>( 742*700637cbSDimitry Andric std::__tree_prev_iter<__node_base_pointer>(static_cast<__end_node_pointer>(__ptr_))); 743*700637cbSDimitry Andric return *this; 744*700637cbSDimitry Andric } 745*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_iterator operator--(int) { 746*700637cbSDimitry Andric __tree_iterator __t(*this); 747*700637cbSDimitry Andric --(*this); 748*700637cbSDimitry Andric return __t; 749*700637cbSDimitry Andric } 750*700637cbSDimitry Andric 751*700637cbSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __tree_iterator& __x, const __tree_iterator& __y) { 752*700637cbSDimitry Andric return __x.__ptr_ == __y.__ptr_; 753*700637cbSDimitry Andric } 754*700637cbSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y) { 755*700637cbSDimitry Andric return !(__x == __y); 756*700637cbSDimitry Andric } 757*700637cbSDimitry Andric 758*700637cbSDimitry Andricprivate: 759*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {} 760*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {} 761*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); } 762*700637cbSDimitry Andric template <class, class, class> 763*700637cbSDimitry Andric friend class __tree; 764*700637cbSDimitry Andric template <class, class, class> 765*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator; 766*700637cbSDimitry Andric template <class> 767*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS __map_iterator; 768*700637cbSDimitry Andric template <class, class, class, class> 769*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS map; 770*700637cbSDimitry Andric template <class, class, class, class> 771*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS multimap; 772*700637cbSDimitry Andric template <class, class, class> 773*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS set; 774*700637cbSDimitry Andric template <class, class, class> 775*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS multiset; 776*700637cbSDimitry Andric}; 777*700637cbSDimitry Andric 778*700637cbSDimitry Andrictemplate <class _Tp, class _NodePtr, class _DiffType> 779*700637cbSDimitry Andricclass _LIBCPP_TEMPLATE_VIS __tree_const_iterator { 780*700637cbSDimitry Andric typedef __tree_node_types<_NodePtr> _NodeTypes; 781*700637cbSDimitry Andric typedef typename _NodeTypes::__node_pointer __node_pointer; 782*700637cbSDimitry Andric typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; 783*700637cbSDimitry Andric typedef typename _NodeTypes::__end_node_pointer __end_node_pointer; 784*700637cbSDimitry Andric typedef typename _NodeTypes::__iter_pointer __iter_pointer; 785*700637cbSDimitry Andric typedef pointer_traits<__node_pointer> __pointer_traits; 786*700637cbSDimitry Andric 787*700637cbSDimitry Andric __iter_pointer __ptr_; 788*700637cbSDimitry Andric 789*700637cbSDimitry Andricpublic: 790*700637cbSDimitry Andric typedef bidirectional_iterator_tag iterator_category; 791*700637cbSDimitry Andric typedef _Tp value_type; 792*700637cbSDimitry Andric typedef _DiffType difference_type; 793*700637cbSDimitry Andric typedef const value_type& reference; 794*700637cbSDimitry Andric typedef typename _NodeTypes::__const_node_value_type_pointer pointer; 795*700637cbSDimitry Andric 796*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT {} 797*700637cbSDimitry Andric 798*700637cbSDimitry Andricprivate: 799*700637cbSDimitry Andric typedef __tree_iterator<value_type, __node_pointer, difference_type> __non_const_iterator; 800*700637cbSDimitry Andric 801*700637cbSDimitry Andricpublic: 802*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT : __ptr_(__p.__ptr_) {} 803*700637cbSDimitry Andric 804*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__value_; } 805*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__get_np()->__value_); } 806*700637cbSDimitry Andric 807*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator++() { 808*700637cbSDimitry Andric __ptr_ = static_cast<__iter_pointer>( 809*700637cbSDimitry Andric std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_))); 810*700637cbSDimitry Andric return *this; 811*700637cbSDimitry Andric } 812*700637cbSDimitry Andric 813*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_const_iterator operator++(int) { 814*700637cbSDimitry Andric __tree_const_iterator __t(*this); 815*700637cbSDimitry Andric ++(*this); 816*700637cbSDimitry Andric return __t; 817*700637cbSDimitry Andric } 818*700637cbSDimitry Andric 819*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator--() { 820*700637cbSDimitry Andric __ptr_ = static_cast<__iter_pointer>( 821*700637cbSDimitry Andric std::__tree_prev_iter<__node_base_pointer>(static_cast<__end_node_pointer>(__ptr_))); 822*700637cbSDimitry Andric return *this; 823*700637cbSDimitry Andric } 824*700637cbSDimitry Andric 825*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree_const_iterator operator--(int) { 826*700637cbSDimitry Andric __tree_const_iterator __t(*this); 827*700637cbSDimitry Andric --(*this); 828*700637cbSDimitry Andric return __t; 829*700637cbSDimitry Andric } 830*700637cbSDimitry Andric 831*700637cbSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y) { 832*700637cbSDimitry Andric return __x.__ptr_ == __y.__ptr_; 833*700637cbSDimitry Andric } 834*700637cbSDimitry Andric friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y) { 835*700637cbSDimitry Andric return !(__x == __y); 836*700637cbSDimitry Andric } 837*700637cbSDimitry Andric 838*700637cbSDimitry Andricprivate: 839*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {} 840*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {} 841*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); } 842*700637cbSDimitry Andric 843*700637cbSDimitry Andric template <class, class, class> 844*700637cbSDimitry Andric friend class __tree; 845*700637cbSDimitry Andric template <class, class, class, class> 846*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS map; 847*700637cbSDimitry Andric template <class, class, class, class> 848*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS multimap; 849*700637cbSDimitry Andric template <class, class, class> 850*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS set; 851*700637cbSDimitry Andric template <class, class, class> 852*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS multiset; 853*700637cbSDimitry Andric template <class> 854*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator; 855*700637cbSDimitry Andric}; 856*700637cbSDimitry Andric 857*700637cbSDimitry Andrictemplate <class _Tp, class _Compare> 858*700637cbSDimitry Andricint __diagnose_non_const_comparator(); 859*700637cbSDimitry Andric 860*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 861*700637cbSDimitry Andricclass __tree { 862*700637cbSDimitry Andricpublic: 863*700637cbSDimitry Andric typedef _Tp value_type; 864*700637cbSDimitry Andric typedef _Compare value_compare; 865*700637cbSDimitry Andric typedef _Allocator allocator_type; 866*700637cbSDimitry Andric 867*700637cbSDimitry Andricprivate: 868*700637cbSDimitry Andric typedef allocator_traits<allocator_type> __alloc_traits; 869*700637cbSDimitry Andric typedef typename __make_tree_node_types<value_type, typename __alloc_traits::void_pointer>::type _NodeTypes; 870*700637cbSDimitry Andric typedef typename _NodeTypes::key_type key_type; 871*700637cbSDimitry Andric 872*700637cbSDimitry Andricpublic: 873*700637cbSDimitry Andric typedef typename _NodeTypes::__node_value_type __node_value_type; 874*700637cbSDimitry Andric typedef typename _NodeTypes::__container_value_type __container_value_type; 875*700637cbSDimitry Andric 876*700637cbSDimitry Andric typedef typename __alloc_traits::pointer pointer; 877*700637cbSDimitry Andric typedef typename __alloc_traits::const_pointer const_pointer; 878*700637cbSDimitry Andric typedef typename __alloc_traits::size_type size_type; 879*700637cbSDimitry Andric typedef typename __alloc_traits::difference_type difference_type; 880*700637cbSDimitry Andric 881*700637cbSDimitry Andricpublic: 882*700637cbSDimitry Andric typedef typename _NodeTypes::__void_pointer __void_pointer; 883*700637cbSDimitry Andric 884*700637cbSDimitry Andric typedef typename _NodeTypes::__node_type __node; 885*700637cbSDimitry Andric typedef typename _NodeTypes::__node_pointer __node_pointer; 886*700637cbSDimitry Andric 887*700637cbSDimitry Andric typedef typename _NodeTypes::__node_base_type __node_base; 888*700637cbSDimitry Andric typedef typename _NodeTypes::__node_base_pointer __node_base_pointer; 889*700637cbSDimitry Andric 890*700637cbSDimitry Andric typedef typename _NodeTypes::__end_node_type __end_node_t; 891*700637cbSDimitry Andric typedef typename _NodeTypes::__end_node_pointer __end_node_ptr; 892*700637cbSDimitry Andric 893*700637cbSDimitry Andric typedef typename _NodeTypes::__parent_pointer __parent_pointer; 894*700637cbSDimitry Andric typedef typename _NodeTypes::__iter_pointer __iter_pointer; 895*700637cbSDimitry Andric 896*700637cbSDimitry Andric typedef __rebind_alloc<__alloc_traits, __node> __node_allocator; 897*700637cbSDimitry Andric typedef allocator_traits<__node_allocator> __node_traits; 898*700637cbSDimitry Andric 899*700637cbSDimitry Andricprivate: 900*700637cbSDimitry Andric // check for sane allocator pointer rebinding semantics. Rebinding the 901*700637cbSDimitry Andric // allocator for a new pointer type should be exactly the same as rebinding 902*700637cbSDimitry Andric // the pointer using 'pointer_traits'. 903*700637cbSDimitry Andric static_assert(is_same<__node_pointer, typename __node_traits::pointer>::value, 904*700637cbSDimitry Andric "Allocator does not rebind pointers in a sane manner."); 905*700637cbSDimitry Andric typedef __rebind_alloc<__node_traits, __node_base> __node_base_allocator; 906*700637cbSDimitry Andric typedef allocator_traits<__node_base_allocator> __node_base_traits; 907*700637cbSDimitry Andric static_assert(is_same<__node_base_pointer, typename __node_base_traits::pointer>::value, 908*700637cbSDimitry Andric "Allocator does not rebind pointers in a sane manner."); 909*700637cbSDimitry Andric 910*700637cbSDimitry Andricprivate: 911*700637cbSDimitry Andric __iter_pointer __begin_node_; 912*700637cbSDimitry Andric __compressed_pair<__end_node_t, __node_allocator> __pair1_; 913*700637cbSDimitry Andric __compressed_pair<size_type, value_compare> __pair3_; 914*700637cbSDimitry Andric 915*700637cbSDimitry Andricpublic: 916*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __iter_pointer __end_node() _NOEXCEPT { 917*700637cbSDimitry Andric return static_cast<__iter_pointer>(pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())); 918*700637cbSDimitry Andric } 919*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __iter_pointer __end_node() const _NOEXCEPT { 920*700637cbSDimitry Andric return static_cast<__iter_pointer>( 921*700637cbSDimitry Andric pointer_traits<__end_node_ptr>::pointer_to(const_cast<__end_node_t&>(__pair1_.first()))); 922*700637cbSDimitry Andric } 923*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __pair1_.second(); } 924*700637cbSDimitry Andric 925*700637cbSDimitry Andricprivate: 926*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __pair1_.second(); } 927*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __iter_pointer& __begin_node() _NOEXCEPT { return __begin_node_; } 928*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const __iter_pointer& __begin_node() const _NOEXCEPT { return __begin_node_; } 929*700637cbSDimitry Andric 930*700637cbSDimitry Andricpublic: 931*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI allocator_type __alloc() const _NOEXCEPT { return allocator_type(__node_alloc()); } 932*700637cbSDimitry Andric 933*700637cbSDimitry Andricprivate: 934*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_type& size() _NOEXCEPT { return __pair3_.first(); } 935*700637cbSDimitry Andric 936*700637cbSDimitry Andricpublic: 937*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const size_type& size() const _NOEXCEPT { return __pair3_.first(); } 938*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI value_compare& value_comp() _NOEXCEPT { return __pair3_.second(); } 939*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const value_compare& value_comp() const _NOEXCEPT { return __pair3_.second(); } 940*700637cbSDimitry Andric 941*700637cbSDimitry Andricpublic: 942*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_pointer __root() const _NOEXCEPT { 943*700637cbSDimitry Andric return static_cast<__node_pointer>(__end_node()->__left_); 944*700637cbSDimitry Andric } 945*700637cbSDimitry Andric 946*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer* __root_ptr() const _NOEXCEPT { 947*700637cbSDimitry Andric return std::addressof(__end_node()->__left_); 948*700637cbSDimitry Andric } 949*700637cbSDimitry Andric 950*700637cbSDimitry Andric typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator; 951*700637cbSDimitry Andric typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator; 952*700637cbSDimitry Andric 953*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree(const value_compare& __comp); 954*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __tree(const allocator_type& __a); 955*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree(const value_compare& __comp, const allocator_type& __a); 956*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree(const __tree& __t); 957*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree& operator=(const __tree& __t); 958*700637cbSDimitry Andric template <class _ForwardIterator> 959*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __assign_unique(_ForwardIterator __first, _ForwardIterator __last); 960*700637cbSDimitry Andric template <class _InputIterator> 961*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __assign_multi(_InputIterator __first, _InputIterator __last); 962*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t); 963*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t, const allocator_type& __a); 964*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __tree& operator=(__tree&& __t); 965*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI ~__tree(); 966*700637cbSDimitry Andric 967*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__begin_node()); } 968*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__begin_node()); } 969*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_node()); } 970*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_node()); } 971*700637cbSDimitry Andric 972*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT { 973*700637cbSDimitry Andric return std::min<size_type>(__node_traits::max_size(__node_alloc()), numeric_limits<difference_type >::max()); 974*700637cbSDimitry Andric } 975*700637cbSDimitry Andric 976*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT; 977*700637cbSDimitry Andric 978*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void swap(__tree& __t); 979*700637cbSDimitry Andric 980*700637cbSDimitry Andric template <class _Key, class... _Args> 981*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args&&... __args); 982*700637cbSDimitry Andric template <class _Key, class... _Args> 983*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...); 984*700637cbSDimitry Andric 985*700637cbSDimitry Andric template <class... _Args> 986*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_impl(_Args&&... __args); 987*700637cbSDimitry Andric 988*700637cbSDimitry Andric template <class... _Args> 989*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args); 990*700637cbSDimitry Andric 991*700637cbSDimitry Andric template <class... _Args> 992*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __emplace_multi(_Args&&... __args); 993*700637cbSDimitry Andric 994*700637cbSDimitry Andric template <class... _Args> 995*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args); 996*700637cbSDimitry Andric 997*700637cbSDimitry Andric template <class _Pp> 998*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Pp&& __x) { 999*700637cbSDimitry Andric return __emplace_unique_extract_key(std::forward<_Pp>(__x), __can_extract_key<_Pp, key_type>()); 1000*700637cbSDimitry Andric } 1001*700637cbSDimitry Andric 1002*700637cbSDimitry Andric template <class _First, 1003*700637cbSDimitry Andric class _Second, 1004*700637cbSDimitry Andric __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0> 1005*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_First&& __f, _Second&& __s) { 1006*700637cbSDimitry Andric return __emplace_unique_key_args(__f, std::forward<_First>(__f), std::forward<_Second>(__s)); 1007*700637cbSDimitry Andric } 1008*700637cbSDimitry Andric 1009*700637cbSDimitry Andric template <class... _Args> 1010*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Args&&... __args) { 1011*700637cbSDimitry Andric return __emplace_unique_impl(std::forward<_Args>(__args)...); 1012*700637cbSDimitry Andric } 1013*700637cbSDimitry Andric 1014*700637cbSDimitry Andric template <class _Pp> 1015*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) { 1016*700637cbSDimitry Andric return __emplace_unique_impl(std::forward<_Pp>(__x)); 1017*700637cbSDimitry Andric } 1018*700637cbSDimitry Andric 1019*700637cbSDimitry Andric template <class _Pp> 1020*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) { 1021*700637cbSDimitry Andric return __emplace_unique_key_args(__x, std::forward<_Pp>(__x)); 1022*700637cbSDimitry Andric } 1023*700637cbSDimitry Andric 1024*700637cbSDimitry Andric template <class _Pp> 1025*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) { 1026*700637cbSDimitry Andric return __emplace_unique_key_args(__x.first, std::forward<_Pp>(__x)); 1027*700637cbSDimitry Andric } 1028*700637cbSDimitry Andric 1029*700637cbSDimitry Andric template <class _Pp> 1030*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) { 1031*700637cbSDimitry Andric return __emplace_hint_unique_extract_key(__p, std::forward<_Pp>(__x), __can_extract_key<_Pp, key_type>()); 1032*700637cbSDimitry Andric } 1033*700637cbSDimitry Andric 1034*700637cbSDimitry Andric template <class _First, 1035*700637cbSDimitry Andric class _Second, 1036*700637cbSDimitry Andric __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0> 1037*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) { 1038*700637cbSDimitry Andric return __emplace_hint_unique_key_args(__p, __f, std::forward<_First>(__f), std::forward<_Second>(__s)).first; 1039*700637cbSDimitry Andric } 1040*700637cbSDimitry Andric 1041*700637cbSDimitry Andric template <class... _Args> 1042*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) { 1043*700637cbSDimitry Andric return __emplace_hint_unique_impl(__p, std::forward<_Args>(__args)...); 1044*700637cbSDimitry Andric } 1045*700637cbSDimitry Andric 1046*700637cbSDimitry Andric template <class _Pp> 1047*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator 1048*700637cbSDimitry Andric __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) { 1049*700637cbSDimitry Andric return __emplace_hint_unique_impl(__p, std::forward<_Pp>(__x)); 1050*700637cbSDimitry Andric } 1051*700637cbSDimitry Andric 1052*700637cbSDimitry Andric template <class _Pp> 1053*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator 1054*700637cbSDimitry Andric __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) { 1055*700637cbSDimitry Andric return __emplace_hint_unique_key_args(__p, __x, std::forward<_Pp>(__x)).first; 1056*700637cbSDimitry Andric } 1057*700637cbSDimitry Andric 1058*700637cbSDimitry Andric template <class _Pp> 1059*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator 1060*700637cbSDimitry Andric __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) { 1061*700637cbSDimitry Andric return __emplace_hint_unique_key_args(__p, __x.first, std::forward<_Pp>(__x)).first; 1062*700637cbSDimitry Andric } 1063*700637cbSDimitry Andric 1064*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(const __container_value_type& __v) { 1065*700637cbSDimitry Andric return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v); 1066*700637cbSDimitry Andric } 1067*700637cbSDimitry Andric 1068*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_unique(const_iterator __p, const __container_value_type& __v) { 1069*700637cbSDimitry Andric return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v).first; 1070*700637cbSDimitry Andric } 1071*700637cbSDimitry Andric 1072*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(__container_value_type&& __v) { 1073*700637cbSDimitry Andric return __emplace_unique_key_args(_NodeTypes::__get_key(__v), std::move(__v)); 1074*700637cbSDimitry Andric } 1075*700637cbSDimitry Andric 1076*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_unique(const_iterator __p, __container_value_type&& __v) { 1077*700637cbSDimitry Andric return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), std::move(__v)).first; 1078*700637cbSDimitry Andric } 1079*700637cbSDimitry Andric 1080*700637cbSDimitry Andric template <class _Vp, __enable_if_t<!is_same<__remove_const_ref_t<_Vp>, __container_value_type>::value, int> = 0> 1081*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(_Vp&& __v) { 1082*700637cbSDimitry Andric return __emplace_unique(std::forward<_Vp>(__v)); 1083*700637cbSDimitry Andric } 1084*700637cbSDimitry Andric 1085*700637cbSDimitry Andric template <class _Vp, __enable_if_t<!is_same<__remove_const_ref_t<_Vp>, __container_value_type>::value, int> = 0> 1086*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_unique(const_iterator __p, _Vp&& __v) { 1087*700637cbSDimitry Andric return __emplace_hint_unique(__p, std::forward<_Vp>(__v)); 1088*700637cbSDimitry Andric } 1089*700637cbSDimitry Andric 1090*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(__container_value_type&& __v) { 1091*700637cbSDimitry Andric return __emplace_multi(std::move(__v)); 1092*700637cbSDimitry Andric } 1093*700637cbSDimitry Andric 1094*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(const_iterator __p, __container_value_type&& __v) { 1095*700637cbSDimitry Andric return __emplace_hint_multi(__p, std::move(__v)); 1096*700637cbSDimitry Andric } 1097*700637cbSDimitry Andric 1098*700637cbSDimitry Andric template <class _Vp> 1099*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(_Vp&& __v) { 1100*700637cbSDimitry Andric return __emplace_multi(std::forward<_Vp>(__v)); 1101*700637cbSDimitry Andric } 1102*700637cbSDimitry Andric 1103*700637cbSDimitry Andric template <class _Vp> 1104*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(const_iterator __p, _Vp&& __v) { 1105*700637cbSDimitry Andric return __emplace_hint_multi(__p, std::forward<_Vp>(__v)); 1106*700637cbSDimitry Andric } 1107*700637cbSDimitry Andric 1108*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> 1109*700637cbSDimitry Andric __node_assign_unique(const __container_value_type& __v, __node_pointer __dest); 1110*700637cbSDimitry Andric 1111*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(__node_pointer __nd); 1112*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(const_iterator __p, __node_pointer __nd); 1113*700637cbSDimitry Andric 1114*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __remove_node_pointer(__node_pointer) _NOEXCEPT; 1115*700637cbSDimitry Andric 1116*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p); 1117*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l); 1118*700637cbSDimitry Andric template <class _Key> 1119*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_type __erase_unique(const _Key& __k); 1120*700637cbSDimitry Andric template <class _Key> 1121*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_type __erase_multi(const _Key& __k); 1122*700637cbSDimitry Andric 1123*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void 1124*700637cbSDimitry Andric __insert_node_at(__parent_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT; 1125*700637cbSDimitry Andric 1126*700637cbSDimitry Andric template <class _Key> 1127*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __v); 1128*700637cbSDimitry Andric template <class _Key> 1129*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __v) const; 1130*700637cbSDimitry Andric 1131*700637cbSDimitry Andric template <class _Key> 1132*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const; 1133*700637cbSDimitry Andric template <class _Key> 1134*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const; 1135*700637cbSDimitry Andric 1136*700637cbSDimitry Andric template <class _Key> 1137*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Key& __v) { 1138*700637cbSDimitry Andric return __lower_bound(__v, __root(), __end_node()); 1139*700637cbSDimitry Andric } 1140*700637cbSDimitry Andric template <class _Key> 1141*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __lower_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result); 1142*700637cbSDimitry Andric template <class _Key> 1143*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Key& __v) const { 1144*700637cbSDimitry Andric return __lower_bound(__v, __root(), __end_node()); 1145*700637cbSDimitry Andric } 1146*700637cbSDimitry Andric template <class _Key> 1147*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator 1148*700637cbSDimitry Andric __lower_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) const; 1149*700637cbSDimitry Andric template <class _Key> 1150*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Key& __v) { 1151*700637cbSDimitry Andric return __upper_bound(__v, __root(), __end_node()); 1152*700637cbSDimitry Andric } 1153*700637cbSDimitry Andric template <class _Key> 1154*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI iterator __upper_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result); 1155*700637cbSDimitry Andric template <class _Key> 1156*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Key& __v) const { 1157*700637cbSDimitry Andric return __upper_bound(__v, __root(), __end_node()); 1158*700637cbSDimitry Andric } 1159*700637cbSDimitry Andric template <class _Key> 1160*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI const_iterator 1161*700637cbSDimitry Andric __upper_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) const; 1162*700637cbSDimitry Andric template <class _Key> 1163*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_unique(const _Key& __k); 1164*700637cbSDimitry Andric template <class _Key> 1165*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_unique(const _Key& __k) const; 1166*700637cbSDimitry Andric 1167*700637cbSDimitry Andric template <class _Key> 1168*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_multi(const _Key& __k); 1169*700637cbSDimitry Andric template <class _Key> 1170*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_multi(const _Key& __k) const; 1171*700637cbSDimitry Andric 1172*700637cbSDimitry Andric typedef __tree_node_destructor<__node_allocator> _Dp; 1173*700637cbSDimitry Andric typedef unique_ptr<__node, _Dp> __node_holder; 1174*700637cbSDimitry Andric 1175*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_holder remove(const_iterator __p) _NOEXCEPT; 1176*700637cbSDimitry Andric 1177*700637cbSDimitry Andricprivate: 1178*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_low(__parent_pointer& __parent, const key_type& __v); 1179*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_high(__parent_pointer& __parent, const key_type& __v); 1180*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer& 1181*700637cbSDimitry Andric __find_leaf(const_iterator __hint, __parent_pointer& __parent, const key_type& __v); 1182*700637cbSDimitry Andric // FIXME: Make this function const qualified. Unfortunately doing so 1183*700637cbSDimitry Andric // breaks existing code which uses non-const callable comparators. 1184*700637cbSDimitry Andric template <class _Key> 1185*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_equal(__parent_pointer& __parent, const _Key& __v); 1186*700637cbSDimitry Andric template <class _Key> 1187*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_equal(__parent_pointer& __parent, const _Key& __v) const { 1188*700637cbSDimitry Andric return const_cast<__tree*>(this)->__find_equal(__parent, __v); 1189*700637cbSDimitry Andric } 1190*700637cbSDimitry Andric template <class _Key> 1191*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_base_pointer& 1192*700637cbSDimitry Andric __find_equal(const_iterator __hint, __parent_pointer& __parent, __node_base_pointer& __dummy, const _Key& __v); 1193*700637cbSDimitry Andric 1194*700637cbSDimitry Andric template <class... _Args> 1195*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(_Args&&... __args); 1196*700637cbSDimitry Andric 1197*700637cbSDimitry Andric // TODO: Make this _LIBCPP_HIDE_FROM_ABI 1198*700637cbSDimitry Andric _LIBCPP_HIDDEN void destroy(__node_pointer __nd) _NOEXCEPT; 1199*700637cbSDimitry Andric 1200*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree& __t) { 1201*700637cbSDimitry Andric __copy_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_copy_assignment::value>()); 1202*700637cbSDimitry Andric } 1203*700637cbSDimitry Andric 1204*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree& __t, true_type) { 1205*700637cbSDimitry Andric if (__node_alloc() != __t.__node_alloc()) 1206*700637cbSDimitry Andric clear(); 1207*700637cbSDimitry Andric __node_alloc() = __t.__node_alloc(); 1208*700637cbSDimitry Andric } 1209*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree&, false_type) {} 1210*700637cbSDimitry Andric 1211*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, false_type); 1212*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, true_type); 1213*700637cbSDimitry Andric 1214*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree& __t) { 1215*700637cbSDimitry Andric __move_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>()); 1216*700637cbSDimitry Andric } 1217*700637cbSDimitry Andric 1218*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree& __t, true_type) { 1219*700637cbSDimitry Andric __node_alloc() = std::move(__t.__node_alloc()); 1220*700637cbSDimitry Andric } 1221*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {} 1222*700637cbSDimitry Andric 1223*700637cbSDimitry Andric struct _DetachedTreeCache { 1224*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit _DetachedTreeCache(__tree* __t) _NOEXCEPT 1225*700637cbSDimitry Andric : __t_(__t), 1226*700637cbSDimitry Andric __cache_root_(__detach_from_tree(__t)) { 1227*700637cbSDimitry Andric __advance(); 1228*700637cbSDimitry Andric } 1229*700637cbSDimitry Andric 1230*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI __node_pointer __get() const _NOEXCEPT { return __cache_elem_; } 1231*700637cbSDimitry Andric 1232*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI void __advance() _NOEXCEPT { 1233*700637cbSDimitry Andric __cache_elem_ = __cache_root_; 1234*700637cbSDimitry Andric if (__cache_root_) { 1235*700637cbSDimitry Andric __cache_root_ = __detach_next(__cache_root_); 1236*700637cbSDimitry Andric } 1237*700637cbSDimitry Andric } 1238*700637cbSDimitry Andric 1239*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI ~_DetachedTreeCache() { 1240*700637cbSDimitry Andric __t_->destroy(__cache_elem_); 1241*700637cbSDimitry Andric if (__cache_root_) { 1242*700637cbSDimitry Andric while (__cache_root_->__parent_ != nullptr) 1243*700637cbSDimitry Andric __cache_root_ = static_cast<__node_pointer>(__cache_root_->__parent_); 1244*700637cbSDimitry Andric __t_->destroy(__cache_root_); 1245*700637cbSDimitry Andric } 1246*700637cbSDimitry Andric } 1247*700637cbSDimitry Andric 1248*700637cbSDimitry Andric _DetachedTreeCache(_DetachedTreeCache const&) = delete; 1249*700637cbSDimitry Andric _DetachedTreeCache& operator=(_DetachedTreeCache const&) = delete; 1250*700637cbSDimitry Andric 1251*700637cbSDimitry Andric private: 1252*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __node_pointer __detach_from_tree(__tree* __t) _NOEXCEPT; 1253*700637cbSDimitry Andric _LIBCPP_HIDE_FROM_ABI static __node_pointer __detach_next(__node_pointer) _NOEXCEPT; 1254*700637cbSDimitry Andric 1255*700637cbSDimitry Andric __tree* __t_; 1256*700637cbSDimitry Andric __node_pointer __cache_root_; 1257*700637cbSDimitry Andric __node_pointer __cache_elem_; 1258*700637cbSDimitry Andric }; 1259*700637cbSDimitry Andric 1260*700637cbSDimitry Andric template <class, class, class, class> 1261*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS map; 1262*700637cbSDimitry Andric template <class, class, class, class> 1263*700637cbSDimitry Andric friend class _LIBCPP_TEMPLATE_VIS multimap; 1264*700637cbSDimitry Andric}; 1265*700637cbSDimitry Andric 1266*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1267*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp) : __pair3_(0, __comp) { 1268*700637cbSDimitry Andric __begin_node() = __end_node(); 1269*700637cbSDimitry Andric} 1270*700637cbSDimitry Andric 1271*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1272*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a) 1273*700637cbSDimitry Andric : __begin_node_(__iter_pointer()), 1274*700637cbSDimitry Andric __pair1_(__default_init_tag(), __node_allocator(__a)), 1275*700637cbSDimitry Andric __pair3_(0, __default_init_tag()) { 1276*700637cbSDimitry Andric __begin_node() = __end_node(); 1277*700637cbSDimitry Andric} 1278*700637cbSDimitry Andric 1279*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1280*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp, const allocator_type& __a) 1281*700637cbSDimitry Andric : __begin_node_(__iter_pointer()), __pair1_(__default_init_tag(), __node_allocator(__a)), __pair3_(0, __comp) { 1282*700637cbSDimitry Andric __begin_node() = __end_node(); 1283*700637cbSDimitry Andric} 1284*700637cbSDimitry Andric 1285*700637cbSDimitry Andric// Precondition: size() != 0 1286*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1287*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_pointer 1288*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_from_tree(__tree* __t) _NOEXCEPT { 1289*700637cbSDimitry Andric __node_pointer __cache = static_cast<__node_pointer>(__t->__begin_node()); 1290*700637cbSDimitry Andric __t->__begin_node() = __t->__end_node(); 1291*700637cbSDimitry Andric __t->__end_node()->__left_->__parent_ = nullptr; 1292*700637cbSDimitry Andric __t->__end_node()->__left_ = nullptr; 1293*700637cbSDimitry Andric __t->size() = 0; 1294*700637cbSDimitry Andric // __cache->__left_ == nullptr 1295*700637cbSDimitry Andric if (__cache->__right_ != nullptr) 1296*700637cbSDimitry Andric __cache = static_cast<__node_pointer>(__cache->__right_); 1297*700637cbSDimitry Andric // __cache->__left_ == nullptr 1298*700637cbSDimitry Andric // __cache->__right_ == nullptr 1299*700637cbSDimitry Andric return __cache; 1300*700637cbSDimitry Andric} 1301*700637cbSDimitry Andric 1302*700637cbSDimitry Andric// Precondition: __cache != nullptr 1303*700637cbSDimitry Andric// __cache->left_ == nullptr 1304*700637cbSDimitry Andric// __cache->right_ == nullptr 1305*700637cbSDimitry Andric// This is no longer a red-black tree 1306*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1307*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_pointer 1308*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_next(__node_pointer __cache) _NOEXCEPT { 1309*700637cbSDimitry Andric if (__cache->__parent_ == nullptr) 1310*700637cbSDimitry Andric return nullptr; 1311*700637cbSDimitry Andric if (std::__tree_is_left_child(static_cast<__node_base_pointer>(__cache))) { 1312*700637cbSDimitry Andric __cache->__parent_->__left_ = nullptr; 1313*700637cbSDimitry Andric __cache = static_cast<__node_pointer>(__cache->__parent_); 1314*700637cbSDimitry Andric if (__cache->__right_ == nullptr) 1315*700637cbSDimitry Andric return __cache; 1316*700637cbSDimitry Andric return static_cast<__node_pointer>(std::__tree_leaf(__cache->__right_)); 1317*700637cbSDimitry Andric } 1318*700637cbSDimitry Andric // __cache is right child 1319*700637cbSDimitry Andric __cache->__parent_unsafe()->__right_ = nullptr; 1320*700637cbSDimitry Andric __cache = static_cast<__node_pointer>(__cache->__parent_); 1321*700637cbSDimitry Andric if (__cache->__left_ == nullptr) 1322*700637cbSDimitry Andric return __cache; 1323*700637cbSDimitry Andric return static_cast<__node_pointer>(std::__tree_leaf(__cache->__left_)); 1324*700637cbSDimitry Andric} 1325*700637cbSDimitry Andric 1326*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1327*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) { 1328*700637cbSDimitry Andric if (this != std::addressof(__t)) { 1329*700637cbSDimitry Andric value_comp() = __t.value_comp(); 1330*700637cbSDimitry Andric __copy_assign_alloc(__t); 1331*700637cbSDimitry Andric __assign_multi(__t.begin(), __t.end()); 1332*700637cbSDimitry Andric } 1333*700637cbSDimitry Andric return *this; 1334*700637cbSDimitry Andric} 1335*700637cbSDimitry Andric 1336*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1337*700637cbSDimitry Andrictemplate <class _ForwardIterator> 1338*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::__assign_unique(_ForwardIterator __first, _ForwardIterator __last) { 1339*700637cbSDimitry Andric typedef iterator_traits<_ForwardIterator> _ITraits; 1340*700637cbSDimitry Andric typedef typename _ITraits::value_type _ItValueType; 1341*700637cbSDimitry Andric static_assert(is_same<_ItValueType, __container_value_type>::value, 1342*700637cbSDimitry Andric "__assign_unique may only be called with the containers value type"); 1343*700637cbSDimitry Andric static_assert( 1344*700637cbSDimitry Andric __has_forward_iterator_category<_ForwardIterator>::value, "__assign_unique requires a forward iterator"); 1345*700637cbSDimitry Andric if (size() != 0) { 1346*700637cbSDimitry Andric _DetachedTreeCache __cache(this); 1347*700637cbSDimitry Andric for (; __cache.__get() != nullptr && __first != __last; ++__first) { 1348*700637cbSDimitry Andric if (__node_assign_unique(*__first, __cache.__get()).second) 1349*700637cbSDimitry Andric __cache.__advance(); 1350*700637cbSDimitry Andric } 1351*700637cbSDimitry Andric } 1352*700637cbSDimitry Andric for (; __first != __last; ++__first) 1353*700637cbSDimitry Andric __insert_unique(*__first); 1354*700637cbSDimitry Andric} 1355*700637cbSDimitry Andric 1356*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1357*700637cbSDimitry Andrictemplate <class _InputIterator> 1358*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last) { 1359*700637cbSDimitry Andric typedef iterator_traits<_InputIterator> _ITraits; 1360*700637cbSDimitry Andric typedef typename _ITraits::value_type _ItValueType; 1361*700637cbSDimitry Andric static_assert( 1362*700637cbSDimitry Andric (is_same<_ItValueType, __container_value_type>::value || is_same<_ItValueType, __node_value_type>::value), 1363*700637cbSDimitry Andric "__assign_multi may only be called with the containers value type" 1364*700637cbSDimitry Andric " or the nodes value type"); 1365*700637cbSDimitry Andric if (size() != 0) { 1366*700637cbSDimitry Andric _DetachedTreeCache __cache(this); 1367*700637cbSDimitry Andric for (; __cache.__get() && __first != __last; ++__first) { 1368*700637cbSDimitry Andric __cache.__get()->__value_ = *__first; 1369*700637cbSDimitry Andric __node_insert_multi(__cache.__get()); 1370*700637cbSDimitry Andric __cache.__advance(); 1371*700637cbSDimitry Andric } 1372*700637cbSDimitry Andric } 1373*700637cbSDimitry Andric for (; __first != __last; ++__first) 1374*700637cbSDimitry Andric __insert_multi(_NodeTypes::__get_value(*__first)); 1375*700637cbSDimitry Andric} 1376*700637cbSDimitry Andric 1377*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1378*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t) 1379*700637cbSDimitry Andric : __begin_node_(__iter_pointer()), 1380*700637cbSDimitry Andric __pair1_(__default_init_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())), 1381*700637cbSDimitry Andric __pair3_(0, __t.value_comp()) { 1382*700637cbSDimitry Andric __begin_node() = __end_node(); 1383*700637cbSDimitry Andric} 1384*700637cbSDimitry Andric 1385*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1386*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t) 1387*700637cbSDimitry Andric : __begin_node_(std::move(__t.__begin_node_)), 1388*700637cbSDimitry Andric __pair1_(std::move(__t.__pair1_)), 1389*700637cbSDimitry Andric __pair3_(std::move(__t.__pair3_)) { 1390*700637cbSDimitry Andric if (size() == 0) 1391*700637cbSDimitry Andric __begin_node() = __end_node(); 1392*700637cbSDimitry Andric else { 1393*700637cbSDimitry Andric __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); 1394*700637cbSDimitry Andric __t.__begin_node() = __t.__end_node(); 1395*700637cbSDimitry Andric __t.__end_node()->__left_ = nullptr; 1396*700637cbSDimitry Andric __t.size() = 0; 1397*700637cbSDimitry Andric } 1398*700637cbSDimitry Andric} 1399*700637cbSDimitry Andric 1400*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1401*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a) 1402*700637cbSDimitry Andric : __pair1_(__default_init_tag(), __node_allocator(__a)), __pair3_(0, std::move(__t.value_comp())) { 1403*700637cbSDimitry Andric if (__a == __t.__alloc()) { 1404*700637cbSDimitry Andric if (__t.size() == 0) 1405*700637cbSDimitry Andric __begin_node() = __end_node(); 1406*700637cbSDimitry Andric else { 1407*700637cbSDimitry Andric __begin_node() = __t.__begin_node(); 1408*700637cbSDimitry Andric __end_node()->__left_ = __t.__end_node()->__left_; 1409*700637cbSDimitry Andric __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); 1410*700637cbSDimitry Andric size() = __t.size(); 1411*700637cbSDimitry Andric __t.__begin_node() = __t.__end_node(); 1412*700637cbSDimitry Andric __t.__end_node()->__left_ = nullptr; 1413*700637cbSDimitry Andric __t.size() = 0; 1414*700637cbSDimitry Andric } 1415*700637cbSDimitry Andric } else { 1416*700637cbSDimitry Andric __begin_node() = __end_node(); 1417*700637cbSDimitry Andric } 1418*700637cbSDimitry Andric} 1419*700637cbSDimitry Andric 1420*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1421*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type) { 1422*700637cbSDimitry Andric destroy(static_cast<__node_pointer>(__end_node()->__left_)); 1423*700637cbSDimitry Andric __begin_node_ = __t.__begin_node_; 1424*700637cbSDimitry Andric __pair1_.first() = __t.__pair1_.first(); 1425*700637cbSDimitry Andric __move_assign_alloc(__t); 1426*700637cbSDimitry Andric __pair3_ = std::move(__t.__pair3_); 1427*700637cbSDimitry Andric if (size() == 0) 1428*700637cbSDimitry Andric __begin_node() = __end_node(); 1429*700637cbSDimitry Andric else { 1430*700637cbSDimitry Andric __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); 1431*700637cbSDimitry Andric __t.__begin_node() = __t.__end_node(); 1432*700637cbSDimitry Andric __t.__end_node()->__left_ = nullptr; 1433*700637cbSDimitry Andric __t.size() = 0; 1434*700637cbSDimitry Andric } 1435*700637cbSDimitry Andric} 1436*700637cbSDimitry Andric 1437*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1438*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type) { 1439*700637cbSDimitry Andric if (__node_alloc() == __t.__node_alloc()) 1440*700637cbSDimitry Andric __move_assign(__t, true_type()); 1441*700637cbSDimitry Andric else { 1442*700637cbSDimitry Andric value_comp() = std::move(__t.value_comp()); 1443*700637cbSDimitry Andric const_iterator __e = end(); 1444*700637cbSDimitry Andric if (size() != 0) { 1445*700637cbSDimitry Andric _DetachedTreeCache __cache(this); 1446*700637cbSDimitry Andric while (__cache.__get() != nullptr && __t.size() != 0) { 1447*700637cbSDimitry Andric __cache.__get()->__value_ = std::move(__t.remove(__t.begin())->__value_); 1448*700637cbSDimitry Andric __node_insert_multi(__cache.__get()); 1449*700637cbSDimitry Andric __cache.__advance(); 1450*700637cbSDimitry Andric } 1451*700637cbSDimitry Andric } 1452*700637cbSDimitry Andric while (__t.size() != 0) 1453*700637cbSDimitry Andric __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_)); 1454*700637cbSDimitry Andric } 1455*700637cbSDimitry Andric} 1456*700637cbSDimitry Andric 1457*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1458*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t) { 1459*700637cbSDimitry Andric __move_assign(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>()); 1460*700637cbSDimitry Andric return *this; 1461*700637cbSDimitry Andric} 1462*700637cbSDimitry Andric 1463*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1464*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::~__tree() { 1465*700637cbSDimitry Andric static_assert(is_copy_constructible<value_compare>::value, "Comparator must be copy-constructible."); 1466*700637cbSDimitry Andric destroy(__root()); 1467*700637cbSDimitry Andric} 1468*700637cbSDimitry Andric 1469*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1470*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT { 1471*700637cbSDimitry Andric if (__nd != nullptr) { 1472*700637cbSDimitry Andric destroy(static_cast<__node_pointer>(__nd->__left_)); 1473*700637cbSDimitry Andric destroy(static_cast<__node_pointer>(__nd->__right_)); 1474*700637cbSDimitry Andric __node_allocator& __na = __node_alloc(); 1475*700637cbSDimitry Andric __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_)); 1476*700637cbSDimitry Andric __node_traits::deallocate(__na, __nd, 1); 1477*700637cbSDimitry Andric } 1478*700637cbSDimitry Andric} 1479*700637cbSDimitry Andric 1480*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1481*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t) { 1482*700637cbSDimitry Andric using std::swap; 1483*700637cbSDimitry Andric swap(__begin_node_, __t.__begin_node_); 1484*700637cbSDimitry Andric swap(__pair1_.first(), __t.__pair1_.first()); 1485*700637cbSDimitry Andric std::__swap_allocator(__node_alloc(), __t.__node_alloc()); 1486*700637cbSDimitry Andric __pair3_.swap(__t.__pair3_); 1487*700637cbSDimitry Andric if (size() == 0) 1488*700637cbSDimitry Andric __begin_node() = __end_node(); 1489*700637cbSDimitry Andric else 1490*700637cbSDimitry Andric __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node()); 1491*700637cbSDimitry Andric if (__t.size() == 0) 1492*700637cbSDimitry Andric __t.__begin_node() = __t.__end_node(); 1493*700637cbSDimitry Andric else 1494*700637cbSDimitry Andric __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node()); 1495*700637cbSDimitry Andric} 1496*700637cbSDimitry Andric 1497*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1498*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT { 1499*700637cbSDimitry Andric destroy(__root()); 1500*700637cbSDimitry Andric size() = 0; 1501*700637cbSDimitry Andric __begin_node() = __end_node(); 1502*700637cbSDimitry Andric __end_node()->__left_ = nullptr; 1503*700637cbSDimitry Andric} 1504*700637cbSDimitry Andric 1505*700637cbSDimitry Andric// Find lower_bound place to insert 1506*700637cbSDimitry Andric// Set __parent to parent of null leaf 1507*700637cbSDimitry Andric// Return reference to null leaf 1508*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1509*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& 1510*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent, const key_type& __v) { 1511*700637cbSDimitry Andric __node_pointer __nd = __root(); 1512*700637cbSDimitry Andric if (__nd != nullptr) { 1513*700637cbSDimitry Andric while (true) { 1514*700637cbSDimitry Andric if (value_comp()(__nd->__value_, __v)) { 1515*700637cbSDimitry Andric if (__nd->__right_ != nullptr) 1516*700637cbSDimitry Andric __nd = static_cast<__node_pointer>(__nd->__right_); 1517*700637cbSDimitry Andric else { 1518*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1519*700637cbSDimitry Andric return __nd->__right_; 1520*700637cbSDimitry Andric } 1521*700637cbSDimitry Andric } else { 1522*700637cbSDimitry Andric if (__nd->__left_ != nullptr) 1523*700637cbSDimitry Andric __nd = static_cast<__node_pointer>(__nd->__left_); 1524*700637cbSDimitry Andric else { 1525*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1526*700637cbSDimitry Andric return __parent->__left_; 1527*700637cbSDimitry Andric } 1528*700637cbSDimitry Andric } 1529*700637cbSDimitry Andric } 1530*700637cbSDimitry Andric } 1531*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__end_node()); 1532*700637cbSDimitry Andric return __parent->__left_; 1533*700637cbSDimitry Andric} 1534*700637cbSDimitry Andric 1535*700637cbSDimitry Andric// Find upper_bound place to insert 1536*700637cbSDimitry Andric// Set __parent to parent of null leaf 1537*700637cbSDimitry Andric// Return reference to null leaf 1538*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1539*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& 1540*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent, const key_type& __v) { 1541*700637cbSDimitry Andric __node_pointer __nd = __root(); 1542*700637cbSDimitry Andric if (__nd != nullptr) { 1543*700637cbSDimitry Andric while (true) { 1544*700637cbSDimitry Andric if (value_comp()(__v, __nd->__value_)) { 1545*700637cbSDimitry Andric if (__nd->__left_ != nullptr) 1546*700637cbSDimitry Andric __nd = static_cast<__node_pointer>(__nd->__left_); 1547*700637cbSDimitry Andric else { 1548*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1549*700637cbSDimitry Andric return __parent->__left_; 1550*700637cbSDimitry Andric } 1551*700637cbSDimitry Andric } else { 1552*700637cbSDimitry Andric if (__nd->__right_ != nullptr) 1553*700637cbSDimitry Andric __nd = static_cast<__node_pointer>(__nd->__right_); 1554*700637cbSDimitry Andric else { 1555*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1556*700637cbSDimitry Andric return __nd->__right_; 1557*700637cbSDimitry Andric } 1558*700637cbSDimitry Andric } 1559*700637cbSDimitry Andric } 1560*700637cbSDimitry Andric } 1561*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__end_node()); 1562*700637cbSDimitry Andric return __parent->__left_; 1563*700637cbSDimitry Andric} 1564*700637cbSDimitry Andric 1565*700637cbSDimitry Andric// Find leaf place to insert closest to __hint 1566*700637cbSDimitry Andric// First check prior to __hint. 1567*700637cbSDimitry Andric// Next check after __hint. 1568*700637cbSDimitry Andric// Next do O(log N) search. 1569*700637cbSDimitry Andric// Set __parent to parent of null leaf 1570*700637cbSDimitry Andric// Return reference to null leaf 1571*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1572*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& 1573*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint, __parent_pointer& __parent, const key_type& __v) { 1574*700637cbSDimitry Andric if (__hint == end() || !value_comp()(*__hint, __v)) // check before 1575*700637cbSDimitry Andric { 1576*700637cbSDimitry Andric // __v <= *__hint 1577*700637cbSDimitry Andric const_iterator __prior = __hint; 1578*700637cbSDimitry Andric if (__prior == begin() || !value_comp()(__v, *--__prior)) { 1579*700637cbSDimitry Andric // *prev(__hint) <= __v <= *__hint 1580*700637cbSDimitry Andric if (__hint.__ptr_->__left_ == nullptr) { 1581*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__hint.__ptr_); 1582*700637cbSDimitry Andric return __parent->__left_; 1583*700637cbSDimitry Andric } else { 1584*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__prior.__ptr_); 1585*700637cbSDimitry Andric return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_; 1586*700637cbSDimitry Andric } 1587*700637cbSDimitry Andric } 1588*700637cbSDimitry Andric // __v < *prev(__hint) 1589*700637cbSDimitry Andric return __find_leaf_high(__parent, __v); 1590*700637cbSDimitry Andric } 1591*700637cbSDimitry Andric // else __v > *__hint 1592*700637cbSDimitry Andric return __find_leaf_low(__parent, __v); 1593*700637cbSDimitry Andric} 1594*700637cbSDimitry Andric 1595*700637cbSDimitry Andric// Find place to insert if __v doesn't exist 1596*700637cbSDimitry Andric// Set __parent to parent of null leaf 1597*700637cbSDimitry Andric// Return reference to null leaf 1598*700637cbSDimitry Andric// If __v exists, set parent to node of __v and return reference to node of __v 1599*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1600*700637cbSDimitry Andrictemplate <class _Key> 1601*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& 1602*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent, const _Key& __v) { 1603*700637cbSDimitry Andric __node_pointer __nd = __root(); 1604*700637cbSDimitry Andric __node_base_pointer* __nd_ptr = __root_ptr(); 1605*700637cbSDimitry Andric if (__nd != nullptr) { 1606*700637cbSDimitry Andric while (true) { 1607*700637cbSDimitry Andric if (value_comp()(__v, __nd->__value_)) { 1608*700637cbSDimitry Andric if (__nd->__left_ != nullptr) { 1609*700637cbSDimitry Andric __nd_ptr = std::addressof(__nd->__left_); 1610*700637cbSDimitry Andric __nd = static_cast<__node_pointer>(__nd->__left_); 1611*700637cbSDimitry Andric } else { 1612*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1613*700637cbSDimitry Andric return __parent->__left_; 1614*700637cbSDimitry Andric } 1615*700637cbSDimitry Andric } else if (value_comp()(__nd->__value_, __v)) { 1616*700637cbSDimitry Andric if (__nd->__right_ != nullptr) { 1617*700637cbSDimitry Andric __nd_ptr = std::addressof(__nd->__right_); 1618*700637cbSDimitry Andric __nd = static_cast<__node_pointer>(__nd->__right_); 1619*700637cbSDimitry Andric } else { 1620*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1621*700637cbSDimitry Andric return __nd->__right_; 1622*700637cbSDimitry Andric } 1623*700637cbSDimitry Andric } else { 1624*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__nd); 1625*700637cbSDimitry Andric return *__nd_ptr; 1626*700637cbSDimitry Andric } 1627*700637cbSDimitry Andric } 1628*700637cbSDimitry Andric } 1629*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__end_node()); 1630*700637cbSDimitry Andric return __parent->__left_; 1631*700637cbSDimitry Andric} 1632*700637cbSDimitry Andric 1633*700637cbSDimitry Andric// Find place to insert if __v doesn't exist 1634*700637cbSDimitry Andric// First check prior to __hint. 1635*700637cbSDimitry Andric// Next check after __hint. 1636*700637cbSDimitry Andric// Next do O(log N) search. 1637*700637cbSDimitry Andric// Set __parent to parent of null leaf 1638*700637cbSDimitry Andric// Return reference to null leaf 1639*700637cbSDimitry Andric// If __v exists, set parent to node of __v and return reference to node of __v 1640*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1641*700637cbSDimitry Andrictemplate <class _Key> 1642*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& __tree<_Tp, _Compare, _Allocator>::__find_equal( 1643*700637cbSDimitry Andric const_iterator __hint, __parent_pointer& __parent, __node_base_pointer& __dummy, const _Key& __v) { 1644*700637cbSDimitry Andric if (__hint == end() || value_comp()(__v, *__hint)) // check before 1645*700637cbSDimitry Andric { 1646*700637cbSDimitry Andric // __v < *__hint 1647*700637cbSDimitry Andric const_iterator __prior = __hint; 1648*700637cbSDimitry Andric if (__prior == begin() || value_comp()(*--__prior, __v)) { 1649*700637cbSDimitry Andric // *prev(__hint) < __v < *__hint 1650*700637cbSDimitry Andric if (__hint.__ptr_->__left_ == nullptr) { 1651*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__hint.__ptr_); 1652*700637cbSDimitry Andric return __parent->__left_; 1653*700637cbSDimitry Andric } else { 1654*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__prior.__ptr_); 1655*700637cbSDimitry Andric return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_; 1656*700637cbSDimitry Andric } 1657*700637cbSDimitry Andric } 1658*700637cbSDimitry Andric // __v <= *prev(__hint) 1659*700637cbSDimitry Andric return __find_equal(__parent, __v); 1660*700637cbSDimitry Andric } else if (value_comp()(*__hint, __v)) // check after 1661*700637cbSDimitry Andric { 1662*700637cbSDimitry Andric // *__hint < __v 1663*700637cbSDimitry Andric const_iterator __next = std::next(__hint); 1664*700637cbSDimitry Andric if (__next == end() || value_comp()(__v, *__next)) { 1665*700637cbSDimitry Andric // *__hint < __v < *std::next(__hint) 1666*700637cbSDimitry Andric if (__hint.__get_np()->__right_ == nullptr) { 1667*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__hint.__ptr_); 1668*700637cbSDimitry Andric return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_; 1669*700637cbSDimitry Andric } else { 1670*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__next.__ptr_); 1671*700637cbSDimitry Andric return __parent->__left_; 1672*700637cbSDimitry Andric } 1673*700637cbSDimitry Andric } 1674*700637cbSDimitry Andric // *next(__hint) <= __v 1675*700637cbSDimitry Andric return __find_equal(__parent, __v); 1676*700637cbSDimitry Andric } 1677*700637cbSDimitry Andric // else __v == *__hint 1678*700637cbSDimitry Andric __parent = static_cast<__parent_pointer>(__hint.__ptr_); 1679*700637cbSDimitry Andric __dummy = static_cast<__node_base_pointer>(__hint.__ptr_); 1680*700637cbSDimitry Andric return __dummy; 1681*700637cbSDimitry Andric} 1682*700637cbSDimitry Andric 1683*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1684*700637cbSDimitry Andricvoid __tree<_Tp, _Compare, _Allocator>::__insert_node_at( 1685*700637cbSDimitry Andric __parent_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT { 1686*700637cbSDimitry Andric __new_node->__left_ = nullptr; 1687*700637cbSDimitry Andric __new_node->__right_ = nullptr; 1688*700637cbSDimitry Andric __new_node->__parent_ = __parent; 1689*700637cbSDimitry Andric // __new_node->__is_black_ is initialized in __tree_balance_after_insert 1690*700637cbSDimitry Andric __child = __new_node; 1691*700637cbSDimitry Andric if (__begin_node()->__left_ != nullptr) 1692*700637cbSDimitry Andric __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_); 1693*700637cbSDimitry Andric std::__tree_balance_after_insert(__end_node()->__left_, __child); 1694*700637cbSDimitry Andric ++size(); 1695*700637cbSDimitry Andric} 1696*700637cbSDimitry Andric 1697*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1698*700637cbSDimitry Andrictemplate <class _Key, class... _Args> 1699*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> 1700*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) { 1701*700637cbSDimitry Andric __parent_pointer __parent; 1702*700637cbSDimitry Andric __node_base_pointer& __child = __find_equal(__parent, __k); 1703*700637cbSDimitry Andric __node_pointer __r = static_cast<__node_pointer>(__child); 1704*700637cbSDimitry Andric bool __inserted = false; 1705*700637cbSDimitry Andric if (__child == nullptr) { 1706*700637cbSDimitry Andric __node_holder __h = __construct_node(std::forward<_Args>(__args)...); 1707*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); 1708*700637cbSDimitry Andric __r = __h.release(); 1709*700637cbSDimitry Andric __inserted = true; 1710*700637cbSDimitry Andric } 1711*700637cbSDimitry Andric return pair<iterator, bool>(iterator(__r), __inserted); 1712*700637cbSDimitry Andric} 1713*700637cbSDimitry Andric 1714*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1715*700637cbSDimitry Andrictemplate <class _Key, class... _Args> 1716*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> 1717*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args( 1718*700637cbSDimitry Andric const_iterator __p, _Key const& __k, _Args&&... __args) { 1719*700637cbSDimitry Andric __parent_pointer __parent; 1720*700637cbSDimitry Andric __node_base_pointer __dummy; 1721*700637cbSDimitry Andric __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k); 1722*700637cbSDimitry Andric __node_pointer __r = static_cast<__node_pointer>(__child); 1723*700637cbSDimitry Andric bool __inserted = false; 1724*700637cbSDimitry Andric if (__child == nullptr) { 1725*700637cbSDimitry Andric __node_holder __h = __construct_node(std::forward<_Args>(__args)...); 1726*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); 1727*700637cbSDimitry Andric __r = __h.release(); 1728*700637cbSDimitry Andric __inserted = true; 1729*700637cbSDimitry Andric } 1730*700637cbSDimitry Andric return pair<iterator, bool>(iterator(__r), __inserted); 1731*700637cbSDimitry Andric} 1732*700637cbSDimitry Andric 1733*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1734*700637cbSDimitry Andrictemplate <class... _Args> 1735*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_holder 1736*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&&... __args) { 1737*700637cbSDimitry Andric static_assert(!__is_tree_value_type<_Args...>::value, "Cannot construct from __value_type"); 1738*700637cbSDimitry Andric __node_allocator& __na = __node_alloc(); 1739*700637cbSDimitry Andric __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na)); 1740*700637cbSDimitry Andric __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), std::forward<_Args>(__args)...); 1741*700637cbSDimitry Andric __h.get_deleter().__value_constructed = true; 1742*700637cbSDimitry Andric return __h; 1743*700637cbSDimitry Andric} 1744*700637cbSDimitry Andric 1745*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1746*700637cbSDimitry Andrictemplate <class... _Args> 1747*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> 1748*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args) { 1749*700637cbSDimitry Andric __node_holder __h = __construct_node(std::forward<_Args>(__args)...); 1750*700637cbSDimitry Andric __parent_pointer __parent; 1751*700637cbSDimitry Andric __node_base_pointer& __child = __find_equal(__parent, __h->__value_); 1752*700637cbSDimitry Andric __node_pointer __r = static_cast<__node_pointer>(__child); 1753*700637cbSDimitry Andric bool __inserted = false; 1754*700637cbSDimitry Andric if (__child == nullptr) { 1755*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); 1756*700637cbSDimitry Andric __r = __h.release(); 1757*700637cbSDimitry Andric __inserted = true; 1758*700637cbSDimitry Andric } 1759*700637cbSDimitry Andric return pair<iterator, bool>(iterator(__r), __inserted); 1760*700637cbSDimitry Andric} 1761*700637cbSDimitry Andric 1762*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1763*700637cbSDimitry Andrictemplate <class... _Args> 1764*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1765*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args) { 1766*700637cbSDimitry Andric __node_holder __h = __construct_node(std::forward<_Args>(__args)...); 1767*700637cbSDimitry Andric __parent_pointer __parent; 1768*700637cbSDimitry Andric __node_base_pointer __dummy; 1769*700637cbSDimitry Andric __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_); 1770*700637cbSDimitry Andric __node_pointer __r = static_cast<__node_pointer>(__child); 1771*700637cbSDimitry Andric if (__child == nullptr) { 1772*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); 1773*700637cbSDimitry Andric __r = __h.release(); 1774*700637cbSDimitry Andric } 1775*700637cbSDimitry Andric return iterator(__r); 1776*700637cbSDimitry Andric} 1777*700637cbSDimitry Andric 1778*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1779*700637cbSDimitry Andrictemplate <class... _Args> 1780*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1781*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args) { 1782*700637cbSDimitry Andric __node_holder __h = __construct_node(std::forward<_Args>(__args)...); 1783*700637cbSDimitry Andric __parent_pointer __parent; 1784*700637cbSDimitry Andric __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_)); 1785*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); 1786*700637cbSDimitry Andric return iterator(static_cast<__node_pointer>(__h.release())); 1787*700637cbSDimitry Andric} 1788*700637cbSDimitry Andric 1789*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1790*700637cbSDimitry Andrictemplate <class... _Args> 1791*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1792*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, _Args&&... __args) { 1793*700637cbSDimitry Andric __node_holder __h = __construct_node(std::forward<_Args>(__args)...); 1794*700637cbSDimitry Andric __parent_pointer __parent; 1795*700637cbSDimitry Andric __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_)); 1796*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get())); 1797*700637cbSDimitry Andric return iterator(static_cast<__node_pointer>(__h.release())); 1798*700637cbSDimitry Andric} 1799*700637cbSDimitry Andric 1800*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1801*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool> 1802*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_assign_unique(const __container_value_type& __v, __node_pointer __nd) { 1803*700637cbSDimitry Andric __parent_pointer __parent; 1804*700637cbSDimitry Andric __node_base_pointer& __child = __find_equal(__parent, _NodeTypes::__get_key(__v)); 1805*700637cbSDimitry Andric __node_pointer __r = static_cast<__node_pointer>(__child); 1806*700637cbSDimitry Andric bool __inserted = false; 1807*700637cbSDimitry Andric if (__child == nullptr) { 1808*700637cbSDimitry Andric __nd->__value_ = __v; 1809*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); 1810*700637cbSDimitry Andric __r = __nd; 1811*700637cbSDimitry Andric __inserted = true; 1812*700637cbSDimitry Andric } 1813*700637cbSDimitry Andric return pair<iterator, bool>(iterator(__r), __inserted); 1814*700637cbSDimitry Andric} 1815*700637cbSDimitry Andric 1816*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1817*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1818*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd) { 1819*700637cbSDimitry Andric __parent_pointer __parent; 1820*700637cbSDimitry Andric __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_)); 1821*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); 1822*700637cbSDimitry Andric return iterator(__nd); 1823*700637cbSDimitry Andric} 1824*700637cbSDimitry Andric 1825*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1826*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1827*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p, __node_pointer __nd) { 1828*700637cbSDimitry Andric __parent_pointer __parent; 1829*700637cbSDimitry Andric __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_)); 1830*700637cbSDimitry Andric __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd)); 1831*700637cbSDimitry Andric return iterator(__nd); 1832*700637cbSDimitry Andric} 1833*700637cbSDimitry Andric 1834*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1835*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1836*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT { 1837*700637cbSDimitry Andric iterator __r(__ptr); 1838*700637cbSDimitry Andric ++__r; 1839*700637cbSDimitry Andric if (__begin_node() == __ptr) 1840*700637cbSDimitry Andric __begin_node() = __r.__ptr_; 1841*700637cbSDimitry Andric --size(); 1842*700637cbSDimitry Andric std::__tree_remove(__end_node()->__left_, static_cast<__node_base_pointer>(__ptr)); 1843*700637cbSDimitry Andric return __r; 1844*700637cbSDimitry Andric} 1845*700637cbSDimitry Andric 1846*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1847*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) { 1848*700637cbSDimitry Andric __node_pointer __np = __p.__get_np(); 1849*700637cbSDimitry Andric iterator __r = __remove_node_pointer(__np); 1850*700637cbSDimitry Andric __node_allocator& __na = __node_alloc(); 1851*700637cbSDimitry Andric __node_traits::destroy(__na, _NodeTypes::__get_ptr(const_cast<__node_value_type&>(*__p))); 1852*700637cbSDimitry Andric __node_traits::deallocate(__na, __np, 1); 1853*700637cbSDimitry Andric return __r; 1854*700637cbSDimitry Andric} 1855*700637cbSDimitry Andric 1856*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1857*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1858*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l) { 1859*700637cbSDimitry Andric while (__f != __l) 1860*700637cbSDimitry Andric __f = erase(__f); 1861*700637cbSDimitry Andric return iterator(__l.__ptr_); 1862*700637cbSDimitry Andric} 1863*700637cbSDimitry Andric 1864*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1865*700637cbSDimitry Andrictemplate <class _Key> 1866*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type 1867*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) { 1868*700637cbSDimitry Andric iterator __i = find(__k); 1869*700637cbSDimitry Andric if (__i == end()) 1870*700637cbSDimitry Andric return 0; 1871*700637cbSDimitry Andric erase(__i); 1872*700637cbSDimitry Andric return 1; 1873*700637cbSDimitry Andric} 1874*700637cbSDimitry Andric 1875*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1876*700637cbSDimitry Andrictemplate <class _Key> 1877*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type 1878*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) { 1879*700637cbSDimitry Andric pair<iterator, iterator> __p = __equal_range_multi(__k); 1880*700637cbSDimitry Andric size_type __r = 0; 1881*700637cbSDimitry Andric for (; __p.first != __p.second; ++__r) 1882*700637cbSDimitry Andric __p.first = erase(__p.first); 1883*700637cbSDimitry Andric return __r; 1884*700637cbSDimitry Andric} 1885*700637cbSDimitry Andric 1886*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1887*700637cbSDimitry Andrictemplate <class _Key> 1888*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) { 1889*700637cbSDimitry Andric iterator __p = __lower_bound(__v, __root(), __end_node()); 1890*700637cbSDimitry Andric if (__p != end() && !value_comp()(__v, *__p)) 1891*700637cbSDimitry Andric return __p; 1892*700637cbSDimitry Andric return end(); 1893*700637cbSDimitry Andric} 1894*700637cbSDimitry Andric 1895*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1896*700637cbSDimitry Andrictemplate <class _Key> 1897*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::const_iterator 1898*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const { 1899*700637cbSDimitry Andric const_iterator __p = __lower_bound(__v, __root(), __end_node()); 1900*700637cbSDimitry Andric if (__p != end() && !value_comp()(__v, *__p)) 1901*700637cbSDimitry Andric return __p; 1902*700637cbSDimitry Andric return end(); 1903*700637cbSDimitry Andric} 1904*700637cbSDimitry Andric 1905*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1906*700637cbSDimitry Andrictemplate <class _Key> 1907*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type 1908*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const { 1909*700637cbSDimitry Andric __node_pointer __rt = __root(); 1910*700637cbSDimitry Andric while (__rt != nullptr) { 1911*700637cbSDimitry Andric if (value_comp()(__k, __rt->__value_)) { 1912*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__left_); 1913*700637cbSDimitry Andric } else if (value_comp()(__rt->__value_, __k)) 1914*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__right_); 1915*700637cbSDimitry Andric else 1916*700637cbSDimitry Andric return 1; 1917*700637cbSDimitry Andric } 1918*700637cbSDimitry Andric return 0; 1919*700637cbSDimitry Andric} 1920*700637cbSDimitry Andric 1921*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1922*700637cbSDimitry Andrictemplate <class _Key> 1923*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::size_type 1924*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const { 1925*700637cbSDimitry Andric __iter_pointer __result = __end_node(); 1926*700637cbSDimitry Andric __node_pointer __rt = __root(); 1927*700637cbSDimitry Andric while (__rt != nullptr) { 1928*700637cbSDimitry Andric if (value_comp()(__k, __rt->__value_)) { 1929*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__rt); 1930*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__left_); 1931*700637cbSDimitry Andric } else if (value_comp()(__rt->__value_, __k)) 1932*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__right_); 1933*700637cbSDimitry Andric else 1934*700637cbSDimitry Andric return std::distance( 1935*700637cbSDimitry Andric __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)), 1936*700637cbSDimitry Andric __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)); 1937*700637cbSDimitry Andric } 1938*700637cbSDimitry Andric return 0; 1939*700637cbSDimitry Andric} 1940*700637cbSDimitry Andric 1941*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1942*700637cbSDimitry Andrictemplate <class _Key> 1943*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1944*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) { 1945*700637cbSDimitry Andric while (__root != nullptr) { 1946*700637cbSDimitry Andric if (!value_comp()(__root->__value_, __v)) { 1947*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__root); 1948*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__left_); 1949*700637cbSDimitry Andric } else 1950*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__right_); 1951*700637cbSDimitry Andric } 1952*700637cbSDimitry Andric return iterator(__result); 1953*700637cbSDimitry Andric} 1954*700637cbSDimitry Andric 1955*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1956*700637cbSDimitry Andrictemplate <class _Key> 1957*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::const_iterator __tree<_Tp, _Compare, _Allocator>::__lower_bound( 1958*700637cbSDimitry Andric const _Key& __v, __node_pointer __root, __iter_pointer __result) const { 1959*700637cbSDimitry Andric while (__root != nullptr) { 1960*700637cbSDimitry Andric if (!value_comp()(__root->__value_, __v)) { 1961*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__root); 1962*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__left_); 1963*700637cbSDimitry Andric } else 1964*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__right_); 1965*700637cbSDimitry Andric } 1966*700637cbSDimitry Andric return const_iterator(__result); 1967*700637cbSDimitry Andric} 1968*700637cbSDimitry Andric 1969*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1970*700637cbSDimitry Andrictemplate <class _Key> 1971*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::iterator 1972*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) { 1973*700637cbSDimitry Andric while (__root != nullptr) { 1974*700637cbSDimitry Andric if (value_comp()(__v, __root->__value_)) { 1975*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__root); 1976*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__left_); 1977*700637cbSDimitry Andric } else 1978*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__right_); 1979*700637cbSDimitry Andric } 1980*700637cbSDimitry Andric return iterator(__result); 1981*700637cbSDimitry Andric} 1982*700637cbSDimitry Andric 1983*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1984*700637cbSDimitry Andrictemplate <class _Key> 1985*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::const_iterator __tree<_Tp, _Compare, _Allocator>::__upper_bound( 1986*700637cbSDimitry Andric const _Key& __v, __node_pointer __root, __iter_pointer __result) const { 1987*700637cbSDimitry Andric while (__root != nullptr) { 1988*700637cbSDimitry Andric if (value_comp()(__v, __root->__value_)) { 1989*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__root); 1990*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__left_); 1991*700637cbSDimitry Andric } else 1992*700637cbSDimitry Andric __root = static_cast<__node_pointer>(__root->__right_); 1993*700637cbSDimitry Andric } 1994*700637cbSDimitry Andric return const_iterator(__result); 1995*700637cbSDimitry Andric} 1996*700637cbSDimitry Andric 1997*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 1998*700637cbSDimitry Andrictemplate <class _Key> 1999*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator> 2000*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) { 2001*700637cbSDimitry Andric typedef pair<iterator, iterator> _Pp; 2002*700637cbSDimitry Andric __iter_pointer __result = __end_node(); 2003*700637cbSDimitry Andric __node_pointer __rt = __root(); 2004*700637cbSDimitry Andric while (__rt != nullptr) { 2005*700637cbSDimitry Andric if (value_comp()(__k, __rt->__value_)) { 2006*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__rt); 2007*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__left_); 2008*700637cbSDimitry Andric } else if (value_comp()(__rt->__value_, __k)) 2009*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__right_); 2010*700637cbSDimitry Andric else 2011*700637cbSDimitry Andric return _Pp(iterator(__rt), 2012*700637cbSDimitry Andric iterator(__rt->__right_ != nullptr ? static_cast<__iter_pointer>(std::__tree_min(__rt->__right_)) 2013*700637cbSDimitry Andric : __result)); 2014*700637cbSDimitry Andric } 2015*700637cbSDimitry Andric return _Pp(iterator(__result), iterator(__result)); 2016*700637cbSDimitry Andric} 2017*700637cbSDimitry Andric 2018*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 2019*700637cbSDimitry Andrictemplate <class _Key> 2020*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, 2021*700637cbSDimitry Andric typename __tree<_Tp, _Compare, _Allocator>::const_iterator> 2022*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const { 2023*700637cbSDimitry Andric typedef pair<const_iterator, const_iterator> _Pp; 2024*700637cbSDimitry Andric __iter_pointer __result = __end_node(); 2025*700637cbSDimitry Andric __node_pointer __rt = __root(); 2026*700637cbSDimitry Andric while (__rt != nullptr) { 2027*700637cbSDimitry Andric if (value_comp()(__k, __rt->__value_)) { 2028*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__rt); 2029*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__left_); 2030*700637cbSDimitry Andric } else if (value_comp()(__rt->__value_, __k)) 2031*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__right_); 2032*700637cbSDimitry Andric else 2033*700637cbSDimitry Andric return _Pp( 2034*700637cbSDimitry Andric const_iterator(__rt), 2035*700637cbSDimitry Andric const_iterator( 2036*700637cbSDimitry Andric __rt->__right_ != nullptr ? static_cast<__iter_pointer>(std::__tree_min(__rt->__right_)) : __result)); 2037*700637cbSDimitry Andric } 2038*700637cbSDimitry Andric return _Pp(const_iterator(__result), const_iterator(__result)); 2039*700637cbSDimitry Andric} 2040*700637cbSDimitry Andric 2041*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 2042*700637cbSDimitry Andrictemplate <class _Key> 2043*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator> 2044*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) { 2045*700637cbSDimitry Andric typedef pair<iterator, iterator> _Pp; 2046*700637cbSDimitry Andric __iter_pointer __result = __end_node(); 2047*700637cbSDimitry Andric __node_pointer __rt = __root(); 2048*700637cbSDimitry Andric while (__rt != nullptr) { 2049*700637cbSDimitry Andric if (value_comp()(__k, __rt->__value_)) { 2050*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__rt); 2051*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__left_); 2052*700637cbSDimitry Andric } else if (value_comp()(__rt->__value_, __k)) 2053*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__right_); 2054*700637cbSDimitry Andric else 2055*700637cbSDimitry Andric return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)), 2056*700637cbSDimitry Andric __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)); 2057*700637cbSDimitry Andric } 2058*700637cbSDimitry Andric return _Pp(iterator(__result), iterator(__result)); 2059*700637cbSDimitry Andric} 2060*700637cbSDimitry Andric 2061*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 2062*700637cbSDimitry Andrictemplate <class _Key> 2063*700637cbSDimitry Andricpair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator, 2064*700637cbSDimitry Andric typename __tree<_Tp, _Compare, _Allocator>::const_iterator> 2065*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const { 2066*700637cbSDimitry Andric typedef pair<const_iterator, const_iterator> _Pp; 2067*700637cbSDimitry Andric __iter_pointer __result = __end_node(); 2068*700637cbSDimitry Andric __node_pointer __rt = __root(); 2069*700637cbSDimitry Andric while (__rt != nullptr) { 2070*700637cbSDimitry Andric if (value_comp()(__k, __rt->__value_)) { 2071*700637cbSDimitry Andric __result = static_cast<__iter_pointer>(__rt); 2072*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__left_); 2073*700637cbSDimitry Andric } else if (value_comp()(__rt->__value_, __k)) 2074*700637cbSDimitry Andric __rt = static_cast<__node_pointer>(__rt->__right_); 2075*700637cbSDimitry Andric else 2076*700637cbSDimitry Andric return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)), 2077*700637cbSDimitry Andric __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)); 2078*700637cbSDimitry Andric } 2079*700637cbSDimitry Andric return _Pp(const_iterator(__result), const_iterator(__result)); 2080*700637cbSDimitry Andric} 2081*700637cbSDimitry Andric 2082*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 2083*700637cbSDimitry Andrictypename __tree<_Tp, _Compare, _Allocator>::__node_holder 2084*700637cbSDimitry Andric__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT { 2085*700637cbSDimitry Andric __node_pointer __np = __p.__get_np(); 2086*700637cbSDimitry Andric if (__begin_node() == __p.__ptr_) { 2087*700637cbSDimitry Andric if (__np->__right_ != nullptr) 2088*700637cbSDimitry Andric __begin_node() = static_cast<__iter_pointer>(__np->__right_); 2089*700637cbSDimitry Andric else 2090*700637cbSDimitry Andric __begin_node() = static_cast<__iter_pointer>(__np->__parent_); 2091*700637cbSDimitry Andric } 2092*700637cbSDimitry Andric --size(); 2093*700637cbSDimitry Andric std::__tree_remove(__end_node()->__left_, static_cast<__node_base_pointer>(__np)); 2094*700637cbSDimitry Andric return __node_holder(__np, _Dp(__node_alloc(), true)); 2095*700637cbSDimitry Andric} 2096*700637cbSDimitry Andric 2097*700637cbSDimitry Andrictemplate <class _Tp, class _Compare, class _Allocator> 2098*700637cbSDimitry Andricinline _LIBCPP_HIDE_FROM_ABI void swap(__tree<_Tp, _Compare, _Allocator>& __x, __tree<_Tp, _Compare, _Allocator>& __y) { 2099*700637cbSDimitry Andric __x.swap(__y); 2100*700637cbSDimitry Andric} 2101*700637cbSDimitry Andric 2102*700637cbSDimitry Andric_LIBCPP_END_NAMESPACE_STD 2103*700637cbSDimitry Andric 2104*700637cbSDimitry Andric_LIBCPP_POP_MACROS 2105*700637cbSDimitry Andric 2106*700637cbSDimitry Andric#endif // _LIBCPP___CXX03___TREE 2107