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