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