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