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