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