2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP___TREE
12 #define _LIBCPP___TREE
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #pragma GCC system_header
24 _LIBCPP_BEGIN_NAMESPACE_STD
26 template <class _Tp, class _Compare, class _Allocator> class __tree;
27 template <class _Tp, class _NodePtr, class _DiffType>
28 class _LIBCPP_TYPE_VIS __tree_iterator;
29 template <class _Tp, class _ConstNodePtr, class _DiffType>
30 class _LIBCPP_TYPE_VIS __tree_const_iterator;
31 template <class _Key, class _Tp, class _Compare, class _Allocator>
32 class _LIBCPP_TYPE_VIS map;
33 template <class _Key, class _Tp, class _Compare, class _Allocator>
34 class _LIBCPP_TYPE_VIS multimap;
35 template <class _Key, class _Compare, class _Allocator>
36 class _LIBCPP_TYPE_VIS set;
37 template <class _Key, class _Compare, class _Allocator>
38 class _LIBCPP_TYPE_VIS multiset;
44 The algorithms taking _NodePtr are red black tree algorithms. Those
45 algorithms taking a parameter named __root should assume that __root
46 points to a proper red black tree (unless otherwise specified).
48 Each algorithm herein assumes that __root->__parent_ points to a non-null
49 structure which has a member __left_ which points back to __root. No other
50 member is read or written to at __root->__parent_.
52 __root->__parent_ will be referred to below (in comments only) as end_node.
53 end_node->__left_ is an externably accessible lvalue for __root, and can be
54 changed by node insertion and removal (without explicit reference to end_node).
56 All nodes (with the exception of end_node), even the node referred to as
57 __root, have a non-null __parent_ field.
61 // Returns: true if __x is a left child of its parent, else false
62 // Precondition: __x != nullptr.
63 template <class _NodePtr>
64 inline _LIBCPP_INLINE_VISIBILITY
66 __tree_is_left_child(_NodePtr __x) _NOEXCEPT
68 return __x == __x->__parent_->__left_;
71 // Determintes if the subtree rooted at __x is a proper red black subtree. If
72 // __x is a proper subtree, returns the black height (null counts as 1). If
73 // __x is an improper subtree, returns 0.
74 template <class _NodePtr>
76 __tree_sub_invariant(_NodePtr __x)
80 // parent consistency checked by caller
81 // check __x->__left_ consistency
82 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
84 // check __x->__right_ consistency
85 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
87 // check __x->__left_ != __x->__right_ unless both are nullptr
88 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
90 // If this is red, neither child can be red
91 if (!__x->__is_black_)
93 if (__x->__left_ && !__x->__left_->__is_black_)
95 if (__x->__right_ && !__x->__right_->__is_black_)
98 unsigned __h = __tree_sub_invariant(__x->__left_);
100 return 0; // invalid left subtree
101 if (__h != __tree_sub_invariant(__x->__right_))
102 return 0; // invalid or different height right subtree
103 return __h + __x->__is_black_; // return black height of this node
106 // Determintes if the red black tree rooted at __root is a proper red black tree.
107 // __root == nullptr is a proper tree. Returns true is __root is a proper
108 // red black tree, else returns false.
109 template <class _NodePtr>
111 __tree_invariant(_NodePtr __root)
113 if (__root == nullptr)
115 // check __x->__parent_ consistency
116 if (__root->__parent_ == nullptr)
118 if (!__tree_is_left_child(__root))
120 // root must be black
121 if (!__root->__is_black_)
123 // do normal node checks
124 return __tree_sub_invariant(__root) != 0;
127 // Returns: pointer to the left-most node under __x.
128 // Precondition: __x != nullptr.
129 template <class _NodePtr>
130 inline _LIBCPP_INLINE_VISIBILITY
132 __tree_min(_NodePtr __x) _NOEXCEPT
134 while (__x->__left_ != nullptr)
139 // Returns: pointer to the right-most node under __x.
140 // Precondition: __x != nullptr.
141 template <class _NodePtr>
142 inline _LIBCPP_INLINE_VISIBILITY
144 __tree_max(_NodePtr __x) _NOEXCEPT
146 while (__x->__right_ != nullptr)
151 // Returns: pointer to the next in-order node after __x.
152 // Precondition: __x != nullptr.
153 template <class _NodePtr>
155 __tree_next(_NodePtr __x) _NOEXCEPT
157 if (__x->__right_ != nullptr)
158 return __tree_min(__x->__right_);
159 while (!__tree_is_left_child(__x))
160 __x = __x->__parent_;
161 return __x->__parent_;
164 // Returns: pointer to the previous in-order node before __x.
165 // Precondition: __x != nullptr.
166 template <class _NodePtr>
168 __tree_prev(_NodePtr __x) _NOEXCEPT
170 if (__x->__left_ != nullptr)
171 return __tree_max(__x->__left_);
172 while (__tree_is_left_child(__x))
173 __x = __x->__parent_;
174 return __x->__parent_;
177 // Returns: pointer to a node which has no children
178 // Precondition: __x != nullptr.
179 template <class _NodePtr>
181 __tree_leaf(_NodePtr __x) _NOEXCEPT
185 if (__x->__left_ != nullptr)
190 if (__x->__right_ != nullptr)
200 // Effects: Makes __x->__right_ the subtree root with __x as its left child
201 // while preserving in-order order.
202 // Precondition: __x->__right_ != nullptr
203 template <class _NodePtr>
205 __tree_left_rotate(_NodePtr __x) _NOEXCEPT
207 _NodePtr __y = __x->__right_;
208 __x->__right_ = __y->__left_;
209 if (__x->__right_ != nullptr)
210 __x->__right_->__parent_ = __x;
211 __y->__parent_ = __x->__parent_;
212 if (__tree_is_left_child(__x))
213 __x->__parent_->__left_ = __y;
215 __x->__parent_->__right_ = __y;
217 __x->__parent_ = __y;
220 // Effects: Makes __x->__left_ the subtree root with __x as its right child
221 // while preserving in-order order.
222 // Precondition: __x->__left_ != nullptr
223 template <class _NodePtr>
225 __tree_right_rotate(_NodePtr __x) _NOEXCEPT
227 _NodePtr __y = __x->__left_;
228 __x->__left_ = __y->__right_;
229 if (__x->__left_ != nullptr)
230 __x->__left_->__parent_ = __x;
231 __y->__parent_ = __x->__parent_;
232 if (__tree_is_left_child(__x))
233 __x->__parent_->__left_ = __y;
235 __x->__parent_->__right_ = __y;
237 __x->__parent_ = __y;
240 // Effects: Rebalances __root after attaching __x to a leaf.
241 // Precondition: __root != nulptr && __x != nullptr.
242 // __x has no children.
243 // __x == __root or == a direct or indirect child of __root.
244 // If __x were to be unlinked from __root (setting __root to
245 // nullptr if __root == __x), __tree_invariant(__root) == true.
246 // Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
247 // may be different than the value passed in as __root.
248 template <class _NodePtr>
250 __tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
252 __x->__is_black_ = __x == __root;
253 while (__x != __root && !__x->__parent_->__is_black_)
255 // __x->__parent_ != __root because __x->__parent_->__is_black == false
256 if (__tree_is_left_child(__x->__parent_))
258 _NodePtr __y = __x->__parent_->__parent_->__right_;
259 if (__y != nullptr && !__y->__is_black_)
261 __x = __x->__parent_;
262 __x->__is_black_ = true;
263 __x = __x->__parent_;
264 __x->__is_black_ = __x == __root;
265 __y->__is_black_ = true;
269 if (!__tree_is_left_child(__x))
271 __x = __x->__parent_;
272 __tree_left_rotate(__x);
274 __x = __x->__parent_;
275 __x->__is_black_ = true;
276 __x = __x->__parent_;
277 __x->__is_black_ = false;
278 __tree_right_rotate(__x);
284 _NodePtr __y = __x->__parent_->__parent_->__left_;
285 if (__y != nullptr && !__y->__is_black_)
287 __x = __x->__parent_;
288 __x->__is_black_ = true;
289 __x = __x->__parent_;
290 __x->__is_black_ = __x == __root;
291 __y->__is_black_ = true;
295 if (__tree_is_left_child(__x))
297 __x = __x->__parent_;
298 __tree_right_rotate(__x);
300 __x = __x->__parent_;
301 __x->__is_black_ = true;
302 __x = __x->__parent_;
303 __x->__is_black_ = false;
304 __tree_left_rotate(__x);
311 // Precondition: __root != nullptr && __z != nullptr.
312 // __tree_invariant(__root) == true.
313 // __z == __root or == a direct or indirect child of __root.
314 // Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
315 // Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
316 // nor any of its children refer to __z. end_node->__left_
317 // may be different than the value passed in as __root.
318 template <class _NodePtr>
320 __tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
322 // __z will be removed from the tree. Client still needs to destruct/deallocate it
323 // __y is either __z, or if __z has two children, __tree_next(__z).
324 // __y will have at most one child.
325 // __y will be the initial hole in the tree (make the hole at a leaf)
326 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
327 __z : __tree_next(__z);
328 // __x is __y's possibly null single child
329 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
330 // __w is __x's possibly null uncle (will become __x's sibling)
331 _NodePtr __w = nullptr;
332 // link __x to __y's parent, and find __w
334 __x->__parent_ = __y->__parent_;
335 if (__tree_is_left_child(__y))
337 __y->__parent_->__left_ = __x;
339 __w = __y->__parent_->__right_;
341 __root = __x; // __w == nullptr
345 __y->__parent_->__right_ = __x;
346 // __y can't be root if it is a right child
347 __w = __y->__parent_->__left_;
349 bool __removed_black = __y->__is_black_;
350 // If we didn't remove __z, do so now by splicing in __y for __z,
351 // but copy __z's color. This does not impact __x or __w.
354 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
355 __y->__parent_ = __z->__parent_;
356 if (__tree_is_left_child(__z))
357 __y->__parent_->__left_ = __y;
359 __y->__parent_->__right_ = __y;
360 __y->__left_ = __z->__left_;
361 __y->__left_->__parent_ = __y;
362 __y->__right_ = __z->__right_;
363 if (__y->__right_ != nullptr)
364 __y->__right_->__parent_ = __y;
365 __y->__is_black_ = __z->__is_black_;
369 // There is no need to rebalance if we removed a red, or if we removed
371 if (__removed_black && __root != nullptr)
374 // __x has an implicit black color (transferred from the removed __y)
375 // associated with it, no matter what its color is.
376 // If __x is __root (in which case it can't be null), it is supposed
377 // to be black anyway, and if it is doubly black, then the double
378 // can just be ignored.
379 // If __x is red (in which case it can't be null), then it can absorb
380 // the implicit black just by setting its color to black.
381 // Since __y was black and only had one child (which __x points to), __x
382 // is either red with no children, else null, otherwise __y would have
383 // different black heights under left and right pointers.
384 // if (__x == __root || __x != nullptr && !__x->__is_black_)
386 __x->__is_black_ = true;
389 // Else __x isn't root, and is "doubly black", even though it may
390 // be null. __w can not be null here, else the parent would
391 // see a black height >= 2 on the __x side and a black height
392 // of 1 on the __w side (__w must be a non-null black or a red
393 // with a non-null black child).
396 if (!__tree_is_left_child(__w)) // if x is left child
398 if (!__w->__is_black_)
400 __w->__is_black_ = true;
401 __w->__parent_->__is_black_ = false;
402 __tree_left_rotate(__w->__parent_);
403 // __x is still valid
404 // reset __root only if necessary
405 if (__root == __w->__left_)
407 // reset sibling, and it still can't be null
408 __w = __w->__left_->__right_;
410 // __w->__is_black_ is now true, __w may have null children
411 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
412 (__w->__right_ == nullptr || __w->__right_->__is_black_))
414 __w->__is_black_ = false;
415 __x = __w->__parent_;
416 // __x can no longer be null
417 if (__x == __root || !__x->__is_black_)
419 __x->__is_black_ = true;
422 // reset sibling, and it still can't be null
423 __w = __tree_is_left_child(__x) ?
424 __x->__parent_->__right_ :
425 __x->__parent_->__left_;
428 else // __w has a red child
430 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
432 // __w left child is non-null and red
433 __w->__left_->__is_black_ = true;
434 __w->__is_black_ = false;
435 __tree_right_rotate(__w);
436 // __w is known not to be root, so root hasn't changed
437 // reset sibling, and it still can't be null
438 __w = __w->__parent_;
440 // __w has a right red child, left child may be null
441 __w->__is_black_ = __w->__parent_->__is_black_;
442 __w->__parent_->__is_black_ = true;
443 __w->__right_->__is_black_ = true;
444 __tree_left_rotate(__w->__parent_);
450 if (!__w->__is_black_)
452 __w->__is_black_ = true;
453 __w->__parent_->__is_black_ = false;
454 __tree_right_rotate(__w->__parent_);
455 // __x is still valid
456 // reset __root only if necessary
457 if (__root == __w->__right_)
459 // reset sibling, and it still can't be null
460 __w = __w->__right_->__left_;
462 // __w->__is_black_ is now true, __w may have null children
463 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
464 (__w->__right_ == nullptr || __w->__right_->__is_black_))
466 __w->__is_black_ = false;
467 __x = __w->__parent_;
468 // __x can no longer be null
469 if (!__x->__is_black_ || __x == __root)
471 __x->__is_black_ = true;
474 // reset sibling, and it still can't be null
475 __w = __tree_is_left_child(__x) ?
476 __x->__parent_->__right_ :
477 __x->__parent_->__left_;
480 else // __w has a red child
482 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
484 // __w right child is non-null and red
485 __w->__right_->__is_black_ = true;
486 __w->__is_black_ = false;
487 __tree_left_rotate(__w);
488 // __w is known not to be root, so root hasn't changed
489 // reset sibling, and it still can't be null
490 __w = __w->__parent_;
492 // __w has a left red child, right child may be null
493 __w->__is_black_ = __w->__parent_->__is_black_;
494 __w->__parent_->__is_black_ = true;
495 __w->__left_->__is_black_ = true;
496 __tree_right_rotate(__w->__parent_);
505 template <class _Allocator> class __map_node_destructor;
507 template <class _Allocator>
508 class __tree_node_destructor
510 typedef _Allocator allocator_type;
511 typedef allocator_traits<allocator_type> __alloc_traits;
512 typedef typename __alloc_traits::value_type::value_type value_type;
514 typedef typename __alloc_traits::pointer pointer;
517 allocator_type& __na_;
519 __tree_node_destructor& operator=(const __tree_node_destructor&);
522 bool __value_constructed;
524 _LIBCPP_INLINE_VISIBILITY
525 explicit __tree_node_destructor(allocator_type& __na) _NOEXCEPT
527 __value_constructed(false)
530 _LIBCPP_INLINE_VISIBILITY
531 void operator()(pointer __p) _NOEXCEPT
533 if (__value_constructed)
534 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
536 __alloc_traits::deallocate(__na_, __p, 1);
539 template <class> friend class __map_node_destructor;
544 template <class _Pointer>
545 class __tree_end_node
548 typedef _Pointer pointer;
551 _LIBCPP_INLINE_VISIBILITY
552 __tree_end_node() _NOEXCEPT : __left_() {}
555 template <class _VoidPtr>
556 class __tree_node_base
557 : public __tree_end_node
559 typename pointer_traits<_VoidPtr>::template
560 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
561 rebind<__tree_node_base<_VoidPtr> >
563 rebind<__tree_node_base<_VoidPtr> >::other
567 __tree_node_base(const __tree_node_base&);
568 __tree_node_base& operator=(const __tree_node_base&);
570 typedef typename pointer_traits<_VoidPtr>::template
571 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
572 rebind<__tree_node_base>
574 rebind<__tree_node_base>::other
577 typedef typename pointer_traits<_VoidPtr>::template
578 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
579 rebind<const __tree_node_base>
581 rebind<const __tree_node_base>::other
584 typedef __tree_end_node<pointer> base;
590 _LIBCPP_INLINE_VISIBILITY
591 __tree_node_base() _NOEXCEPT
592 : __right_(), __parent_(), __is_black_(false) {}
595 template <class _Tp, class _VoidPtr>
597 : public __tree_node_base<_VoidPtr>
600 typedef __tree_node_base<_VoidPtr> base;
601 typedef _Tp value_type;
605 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
606 template <class ..._Args>
607 _LIBCPP_INLINE_VISIBILITY
608 explicit __tree_node(_Args&& ...__args)
609 : __value_(_VSTD::forward<_Args>(__args)...) {}
610 #else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
611 _LIBCPP_INLINE_VISIBILITY
612 explicit __tree_node(const value_type& __v)
614 #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
617 template <class _TreeIterator> class _LIBCPP_TYPE_VIS __map_iterator;
618 template <class _TreeIterator> class _LIBCPP_TYPE_VIS __map_const_iterator;
620 template <class _Tp, class _NodePtr, class _DiffType>
621 class _LIBCPP_TYPE_VIS __tree_iterator
623 typedef _NodePtr __node_pointer;
624 typedef typename pointer_traits<__node_pointer>::element_type __node;
625 typedef typename __node::base __node_base;
626 typedef typename __node_base::pointer __node_base_pointer;
628 __node_pointer __ptr_;
630 typedef pointer_traits<__node_pointer> __pointer_traits;
632 typedef bidirectional_iterator_tag iterator_category;
633 typedef _Tp value_type;
634 typedef _DiffType difference_type;
635 typedef value_type& reference;
636 typedef typename pointer_traits<__node_pointer>::template
637 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
640 rebind<value_type>::other
644 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT {}
646 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
647 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
648 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
650 _LIBCPP_INLINE_VISIBILITY
651 __tree_iterator& operator++()
652 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
654 _LIBCPP_INLINE_VISIBILITY
655 __tree_iterator operator++(int)
656 {__tree_iterator __t(*this); ++(*this); return __t;}
658 _LIBCPP_INLINE_VISIBILITY
659 __tree_iterator& operator--()
660 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
662 _LIBCPP_INLINE_VISIBILITY
663 __tree_iterator operator--(int)
664 {__tree_iterator __t(*this); --(*this); return __t;}
666 friend _LIBCPP_INLINE_VISIBILITY
667 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
668 {return __x.__ptr_ == __y.__ptr_;}
669 friend _LIBCPP_INLINE_VISIBILITY
670 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
671 {return !(__x == __y);}
674 _LIBCPP_INLINE_VISIBILITY
675 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
676 template <class, class, class> friend class __tree;
677 template <class, class, class> friend class _LIBCPP_TYPE_VIS __tree_const_iterator;
678 template <class> friend class _LIBCPP_TYPE_VIS __map_iterator;
679 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS map;
680 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS multimap;
681 template <class, class, class> friend class _LIBCPP_TYPE_VIS set;
682 template <class, class, class> friend class _LIBCPP_TYPE_VIS multiset;
685 template <class _Tp, class _ConstNodePtr, class _DiffType>
686 class _LIBCPP_TYPE_VIS __tree_const_iterator
688 typedef _ConstNodePtr __node_pointer;
689 typedef typename pointer_traits<__node_pointer>::element_type __node;
690 typedef typename __node::base __node_base;
691 typedef typename pointer_traits<__node_pointer>::template
692 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
695 rebind<__node_base>::other
699 __node_pointer __ptr_;
701 typedef pointer_traits<__node_pointer> __pointer_traits;
703 typedef bidirectional_iterator_tag iterator_category;
704 typedef _Tp value_type;
705 typedef _DiffType difference_type;
706 typedef const value_type& reference;
707 typedef typename pointer_traits<__node_pointer>::template
708 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
709 rebind<const value_type>
711 rebind<const value_type>::other
715 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() {}
717 typedef typename remove_const<__node>::type __non_const_node;
718 typedef typename pointer_traits<__node_pointer>::template
719 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
720 rebind<__non_const_node>
722 rebind<__non_const_node>::other
724 __non_const_node_pointer;
725 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
726 __non_const_iterator;
728 _LIBCPP_INLINE_VISIBILITY
729 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
730 : __ptr_(__p.__ptr_) {}
732 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
733 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
734 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
736 _LIBCPP_INLINE_VISIBILITY
737 __tree_const_iterator& operator++()
738 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
740 _LIBCPP_INLINE_VISIBILITY
741 __tree_const_iterator operator++(int)
742 {__tree_const_iterator __t(*this); ++(*this); return __t;}
744 _LIBCPP_INLINE_VISIBILITY
745 __tree_const_iterator& operator--()
746 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
748 _LIBCPP_INLINE_VISIBILITY
749 __tree_const_iterator operator--(int)
750 {__tree_const_iterator __t(*this); --(*this); return __t;}
752 friend _LIBCPP_INLINE_VISIBILITY
753 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
754 {return __x.__ptr_ == __y.__ptr_;}
755 friend _LIBCPP_INLINE_VISIBILITY
756 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
757 {return !(__x == __y);}
760 _LIBCPP_INLINE_VISIBILITY
761 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
763 template <class, class, class> friend class __tree;
764 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS map;
765 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS multimap;
766 template <class, class, class> friend class _LIBCPP_TYPE_VIS set;
767 template <class, class, class> friend class _LIBCPP_TYPE_VIS multiset;
768 template <class> friend class _LIBCPP_TYPE_VIS __map_const_iterator;
771 template <class _Tp, class _Compare, class _Allocator>
775 typedef _Tp value_type;
776 typedef _Compare value_compare;
777 typedef _Allocator allocator_type;
778 typedef allocator_traits<allocator_type> __alloc_traits;
779 typedef typename __alloc_traits::pointer pointer;
780 typedef typename __alloc_traits::const_pointer const_pointer;
781 typedef typename __alloc_traits::size_type size_type;
782 typedef typename __alloc_traits::difference_type difference_type;
784 typedef typename __alloc_traits::void_pointer __void_pointer;
786 typedef __tree_node<value_type, __void_pointer> __node;
787 typedef __tree_node_base<__void_pointer> __node_base;
788 typedef typename __alloc_traits::template
789 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
792 rebind_alloc<__node>::other
795 typedef allocator_traits<__node_allocator> __node_traits;
796 typedef typename __node_traits::pointer __node_pointer;
797 typedef typename __node_traits::pointer __node_const_pointer;
798 typedef typename __node_base::pointer __node_base_pointer;
799 typedef typename __node_base::pointer __node_base_const_pointer;
801 typedef typename __node_base::base __end_node_t;
802 typedef typename pointer_traits<__node_pointer>::template
803 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
806 rebind<__end_node_t>::other
809 typedef typename pointer_traits<__node_pointer>::template
810 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
813 rebind<__end_node_t>::other
815 __end_node_const_ptr;
817 __node_pointer __begin_node_;
818 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
819 __compressed_pair<size_type, value_compare> __pair3_;
822 _LIBCPP_INLINE_VISIBILITY
823 __node_pointer __end_node() _NOEXCEPT
825 return static_cast<__node_pointer>
827 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
830 _LIBCPP_INLINE_VISIBILITY
831 __node_const_pointer __end_node() const _NOEXCEPT
833 return static_cast<__node_const_pointer>
835 pointer_traits<__end_node_const_ptr>::pointer_to(const_cast<__end_node_t&>(__pair1_.first()))
838 _LIBCPP_INLINE_VISIBILITY
839 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
841 _LIBCPP_INLINE_VISIBILITY
842 const __node_allocator& __node_alloc() const _NOEXCEPT
843 {return __pair1_.second();}
844 _LIBCPP_INLINE_VISIBILITY
845 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
846 _LIBCPP_INLINE_VISIBILITY
847 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
849 _LIBCPP_INLINE_VISIBILITY
850 allocator_type __alloc() const _NOEXCEPT
851 {return allocator_type(__node_alloc());}
853 _LIBCPP_INLINE_VISIBILITY
854 size_type& size() _NOEXCEPT {return __pair3_.first();}
856 _LIBCPP_INLINE_VISIBILITY
857 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
858 _LIBCPP_INLINE_VISIBILITY
859 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
860 _LIBCPP_INLINE_VISIBILITY
861 const value_compare& value_comp() const _NOEXCEPT
862 {return __pair3_.second();}
864 _LIBCPP_INLINE_VISIBILITY
865 __node_pointer __root() _NOEXCEPT
866 {return static_cast<__node_pointer> (__end_node()->__left_);}
867 _LIBCPP_INLINE_VISIBILITY
868 __node_const_pointer __root() const _NOEXCEPT
869 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
871 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
872 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
874 explicit __tree(const value_compare& __comp)
876 is_nothrow_default_constructible<__node_allocator>::value &&
877 is_nothrow_copy_constructible<value_compare>::value);
878 explicit __tree(const allocator_type& __a);
879 __tree(const value_compare& __comp, const allocator_type& __a);
880 __tree(const __tree& __t);
881 __tree& operator=(const __tree& __t);
882 template <class _InputIterator>
883 void __assign_unique(_InputIterator __first, _InputIterator __last);
884 template <class _InputIterator>
885 void __assign_multi(_InputIterator __first, _InputIterator __last);
886 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
889 is_nothrow_move_constructible<__node_allocator>::value &&
890 is_nothrow_move_constructible<value_compare>::value);
891 __tree(__tree&& __t, const allocator_type& __a);
892 __tree& operator=(__tree&& __t)
894 __node_traits::propagate_on_container_move_assignment::value &&
895 is_nothrow_move_assignable<value_compare>::value &&
896 is_nothrow_move_assignable<__node_allocator>::value);
897 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
901 _LIBCPP_INLINE_VISIBILITY
902 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
903 _LIBCPP_INLINE_VISIBILITY
904 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
905 _LIBCPP_INLINE_VISIBILITY
906 iterator end() _NOEXCEPT {return iterator(__end_node());}
907 _LIBCPP_INLINE_VISIBILITY
908 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
910 _LIBCPP_INLINE_VISIBILITY
911 size_type max_size() const _NOEXCEPT
912 {return __node_traits::max_size(__node_alloc());}
914 void clear() _NOEXCEPT;
916 void swap(__tree& __t)
918 __is_nothrow_swappable<value_compare>::value &&
919 (!__node_traits::propagate_on_container_swap::value ||
920 __is_nothrow_swappable<__node_allocator>::value));
922 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
923 #ifndef _LIBCPP_HAS_NO_VARIADICS
924 template <class... _Args>
926 __emplace_unique(_Args&&... __args);
927 template <class... _Args>
929 __emplace_multi(_Args&&... __args);
931 template <class... _Args>
933 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
934 template <class... _Args>
936 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
937 #endif // _LIBCPP_HAS_NO_VARIADICS
940 pair<iterator, bool> __insert_unique(_Vp&& __v);
942 iterator __insert_unique(const_iterator __p, _Vp&& __v);
944 iterator __insert_multi(_Vp&& __v);
946 iterator __insert_multi(const_iterator __p, _Vp&& __v);
947 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
949 pair<iterator, bool> __insert_unique(const value_type& __v);
950 iterator __insert_unique(const_iterator __p, const value_type& __v);
951 iterator __insert_multi(const value_type& __v);
952 iterator __insert_multi(const_iterator __p, const value_type& __v);
954 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
955 iterator __node_insert_unique(const_iterator __p,
956 __node_pointer __nd);
958 iterator __node_insert_multi(__node_pointer __nd);
959 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
961 iterator erase(const_iterator __p);
962 iterator erase(const_iterator __f, const_iterator __l);
963 template <class _Key>
964 size_type __erase_unique(const _Key& __k);
965 template <class _Key>
966 size_type __erase_multi(const _Key& __k);
968 void __insert_node_at(__node_base_pointer __parent,
969 __node_base_pointer& __child,
970 __node_base_pointer __new_node);
972 template <class _Key>
973 iterator find(const _Key& __v);
974 template <class _Key>
975 const_iterator find(const _Key& __v) const;
977 template <class _Key>
978 size_type __count_unique(const _Key& __k) const;
979 template <class _Key>
980 size_type __count_multi(const _Key& __k) const;
982 template <class _Key>
983 _LIBCPP_INLINE_VISIBILITY
984 iterator lower_bound(const _Key& __v)
985 {return __lower_bound(__v, __root(), __end_node());}
986 template <class _Key>
987 iterator __lower_bound(const _Key& __v,
988 __node_pointer __root,
989 __node_pointer __result);
990 template <class _Key>
991 _LIBCPP_INLINE_VISIBILITY
992 const_iterator lower_bound(const _Key& __v) const
993 {return __lower_bound(__v, __root(), __end_node());}
994 template <class _Key>
995 const_iterator __lower_bound(const _Key& __v,
996 __node_const_pointer __root,
997 __node_const_pointer __result) const;
998 template <class _Key>
999 _LIBCPP_INLINE_VISIBILITY
1000 iterator upper_bound(const _Key& __v)
1001 {return __upper_bound(__v, __root(), __end_node());}
1002 template <class _Key>
1003 iterator __upper_bound(const _Key& __v,
1004 __node_pointer __root,
1005 __node_pointer __result);
1006 template <class _Key>
1007 _LIBCPP_INLINE_VISIBILITY
1008 const_iterator upper_bound(const _Key& __v) const
1009 {return __upper_bound(__v, __root(), __end_node());}
1010 template <class _Key>
1011 const_iterator __upper_bound(const _Key& __v,
1012 __node_const_pointer __root,
1013 __node_const_pointer __result) const;
1014 template <class _Key>
1015 pair<iterator, iterator>
1016 __equal_range_unique(const _Key& __k);
1017 template <class _Key>
1018 pair<const_iterator, const_iterator>
1019 __equal_range_unique(const _Key& __k) const;
1021 template <class _Key>
1022 pair<iterator, iterator>
1023 __equal_range_multi(const _Key& __k);
1024 template <class _Key>
1025 pair<const_iterator, const_iterator>
1026 __equal_range_multi(const _Key& __k) const;
1028 typedef __tree_node_destructor<__node_allocator> _Dp;
1029 typedef unique_ptr<__node, _Dp> __node_holder;
1031 __node_holder remove(const_iterator __p) _NOEXCEPT;
1033 typename __node_base::pointer&
1034 __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v);
1035 typename __node_base::pointer&
1036 __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v);
1037 typename __node_base::pointer&
1038 __find_leaf(const_iterator __hint,
1039 typename __node_base::pointer& __parent, const value_type& __v);
1040 template <class _Key>
1041 typename __node_base::pointer&
1042 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
1043 template <class _Key>
1044 typename __node_base::pointer&
1045 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
1048 #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1049 template <class ..._Args>
1050 __node_holder __construct_node(_Args&& ...__args);
1051 #else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
1052 __node_holder __construct_node(const value_type& __v);
1055 void destroy(__node_pointer __nd) _NOEXCEPT;
1057 _LIBCPP_INLINE_VISIBILITY
1058 void __copy_assign_alloc(const __tree& __t)
1059 {__copy_assign_alloc(__t, integral_constant<bool,
1060 __node_traits::propagate_on_container_copy_assignment::value>());}
1062 _LIBCPP_INLINE_VISIBILITY
1063 void __copy_assign_alloc(const __tree& __t, true_type)
1064 {__node_alloc() = __t.__node_alloc();}
1065 _LIBCPP_INLINE_VISIBILITY
1066 void __copy_assign_alloc(const __tree& __t, false_type) {}
1068 void __move_assign(__tree& __t, false_type);
1069 void __move_assign(__tree& __t, true_type)
1070 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1071 is_nothrow_move_assignable<__node_allocator>::value);
1073 _LIBCPP_INLINE_VISIBILITY
1074 void __move_assign_alloc(__tree& __t)
1076 !__node_traits::propagate_on_container_move_assignment::value ||
1077 is_nothrow_move_assignable<__node_allocator>::value)
1078 {__move_assign_alloc(__t, integral_constant<bool,
1079 __node_traits::propagate_on_container_move_assignment::value>());}
1081 _LIBCPP_INLINE_VISIBILITY
1082 void __move_assign_alloc(__tree& __t, true_type)
1083 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1084 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
1085 _LIBCPP_INLINE_VISIBILITY
1086 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
1088 _LIBCPP_INLINE_VISIBILITY
1089 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
1091 !__node_traits::propagate_on_container_swap::value ||
1092 __is_nothrow_swappable<__node_allocator>::value)
1093 {__swap_alloc(__x, __y, integral_constant<bool,
1094 __node_traits::propagate_on_container_swap::value>());}
1095 _LIBCPP_INLINE_VISIBILITY
1096 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
1097 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
1102 _LIBCPP_INLINE_VISIBILITY
1103 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
1107 __node_pointer __detach();
1108 static __node_pointer __detach(__node_pointer);
1110 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS map;
1111 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS multimap;
1114 template <class _Tp, class _Compare, class _Allocator>
1115 __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
1117 is_nothrow_default_constructible<__node_allocator>::value &&
1118 is_nothrow_copy_constructible<value_compare>::value)
1119 : __pair3_(0, __comp)
1121 __begin_node() = __end_node();
1124 template <class _Tp, class _Compare, class _Allocator>
1125 __tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1126 : __pair1_(__node_allocator(__a)),
1127 __begin_node_(__node_pointer()),
1130 __begin_node() = __end_node();
1133 template <class _Tp, class _Compare, class _Allocator>
1134 __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1135 const allocator_type& __a)
1136 : __pair1_(__node_allocator(__a)),
1137 __begin_node_(__node_pointer()),
1140 __begin_node() = __end_node();
1143 // Precondition: size() != 0
1144 template <class _Tp, class _Compare, class _Allocator>
1145 typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1146 __tree<_Tp, _Compare, _Allocator>::__detach()
1148 __node_pointer __cache = __begin_node();
1149 __begin_node() = __end_node();
1150 __end_node()->__left_->__parent_ = nullptr;
1151 __end_node()->__left_ = nullptr;
1153 // __cache->__left_ == nullptr
1154 if (__cache->__right_ != nullptr)
1155 __cache = static_cast<__node_pointer>(__cache->__right_);
1156 // __cache->__left_ == nullptr
1157 // __cache->__right_ == nullptr
1161 // Precondition: __cache != nullptr
1162 // __cache->left_ == nullptr
1163 // __cache->right_ == nullptr
1164 // This is no longer a red-black tree
1165 template <class _Tp, class _Compare, class _Allocator>
1166 typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1167 __tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1169 if (__cache->__parent_ == nullptr)
1171 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
1173 __cache->__parent_->__left_ = nullptr;
1174 __cache = static_cast<__node_pointer>(__cache->__parent_);
1175 if (__cache->__right_ == nullptr)
1177 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1179 // __cache is right child
1180 __cache->__parent_->__right_ = nullptr;
1181 __cache = static_cast<__node_pointer>(__cache->__parent_);
1182 if (__cache->__left_ == nullptr)
1184 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1187 template <class _Tp, class _Compare, class _Allocator>
1188 __tree<_Tp, _Compare, _Allocator>&
1189 __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1193 value_comp() = __t.value_comp();
1194 __copy_assign_alloc(__t);
1195 __assign_multi(__t.begin(), __t.end());
1200 template <class _Tp, class _Compare, class _Allocator>
1201 template <class _InputIterator>
1203 __tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1207 __node_pointer __cache = __detach();
1208 #ifndef _LIBCPP_NO_EXCEPTIONS
1211 #endif // _LIBCPP_NO_EXCEPTIONS
1212 for (; __cache != nullptr && __first != __last; ++__first)
1214 __cache->__value_ = *__first;
1215 __node_pointer __next = __detach(__cache);
1216 __node_insert_unique(__cache);
1219 #ifndef _LIBCPP_NO_EXCEPTIONS
1223 while (__cache->__parent_ != nullptr)
1224 __cache = static_cast<__node_pointer>(__cache->__parent_);
1228 #endif // _LIBCPP_NO_EXCEPTIONS
1229 if (__cache != nullptr)
1231 while (__cache->__parent_ != nullptr)
1232 __cache = static_cast<__node_pointer>(__cache->__parent_);
1236 for (; __first != __last; ++__first)
1237 __insert_unique(*__first);
1240 template <class _Tp, class _Compare, class _Allocator>
1241 template <class _InputIterator>
1243 __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1247 __node_pointer __cache = __detach();
1248 #ifndef _LIBCPP_NO_EXCEPTIONS
1251 #endif // _LIBCPP_NO_EXCEPTIONS
1252 for (; __cache != nullptr && __first != __last; ++__first)
1254 __cache->__value_ = *__first;
1255 __node_pointer __next = __detach(__cache);
1256 __node_insert_multi(__cache);
1259 #ifndef _LIBCPP_NO_EXCEPTIONS
1263 while (__cache->__parent_ != nullptr)
1264 __cache = static_cast<__node_pointer>(__cache->__parent_);
1268 #endif // _LIBCPP_NO_EXCEPTIONS
1269 if (__cache != nullptr)
1271 while (__cache->__parent_ != nullptr)
1272 __cache = static_cast<__node_pointer>(__cache->__parent_);
1276 for (; __first != __last; ++__first)
1277 __insert_multi(*__first);
1280 template <class _Tp, class _Compare, class _Allocator>
1281 __tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1282 : __begin_node_(__node_pointer()),
1283 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1284 __pair3_(0, __t.value_comp())
1286 __begin_node() = __end_node();
1289 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1291 template <class _Tp, class _Compare, class _Allocator>
1292 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
1294 is_nothrow_move_constructible<__node_allocator>::value &&
1295 is_nothrow_move_constructible<value_compare>::value)
1296 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1297 __pair1_(_VSTD::move(__t.__pair1_)),
1298 __pair3_(_VSTD::move(__t.__pair3_))
1301 __begin_node() = __end_node();
1304 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
1305 __t.__begin_node() = __t.__end_node();
1306 __t.__end_node()->__left_ = nullptr;
1311 template <class _Tp, class _Compare, class _Allocator>
1312 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1313 : __pair1_(__node_allocator(__a)),
1314 __pair3_(0, _VSTD::move(__t.value_comp()))
1316 if (__a == __t.__alloc())
1318 if (__t.size() == 0)
1319 __begin_node() = __end_node();
1322 __begin_node() = __t.__begin_node();
1323 __end_node()->__left_ = __t.__end_node()->__left_;
1324 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
1325 size() = __t.size();
1326 __t.__begin_node() = __t.__end_node();
1327 __t.__end_node()->__left_ = nullptr;
1333 __begin_node() = __end_node();
1337 template <class _Tp, class _Compare, class _Allocator>
1339 __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1340 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1341 is_nothrow_move_assignable<__node_allocator>::value)
1343 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1344 __begin_node_ = __t.__begin_node_;
1345 __pair1_.first() = __t.__pair1_.first();
1346 __move_assign_alloc(__t);
1347 __pair3_ = _VSTD::move(__t.__pair3_);
1349 __begin_node() = __end_node();
1352 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
1353 __t.__begin_node() = __t.__end_node();
1354 __t.__end_node()->__left_ = nullptr;
1359 template <class _Tp, class _Compare, class _Allocator>
1361 __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1363 if (__node_alloc() == __t.__node_alloc())
1364 __move_assign(__t, true_type());
1367 value_comp() = _VSTD::move(__t.value_comp());
1368 const_iterator __e = end();
1371 __node_pointer __cache = __detach();
1372 #ifndef _LIBCPP_NO_EXCEPTIONS
1375 #endif // _LIBCPP_NO_EXCEPTIONS
1376 while (__cache != nullptr && __t.size() != 0)
1378 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
1379 __node_pointer __next = __detach(__cache);
1380 __node_insert_multi(__cache);
1383 #ifndef _LIBCPP_NO_EXCEPTIONS
1387 while (__cache->__parent_ != nullptr)
1388 __cache = static_cast<__node_pointer>(__cache->__parent_);
1392 #endif // _LIBCPP_NO_EXCEPTIONS
1393 if (__cache != nullptr)
1395 while (__cache->__parent_ != nullptr)
1396 __cache = static_cast<__node_pointer>(__cache->__parent_);
1400 while (__t.size() != 0)
1401 __insert_multi(__e, _VSTD::move(__t.remove(__t.begin())->__value_));
1405 template <class _Tp, class _Compare, class _Allocator>
1406 __tree<_Tp, _Compare, _Allocator>&
1407 __tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
1409 __node_traits::propagate_on_container_move_assignment::value &&
1410 is_nothrow_move_assignable<value_compare>::value &&
1411 is_nothrow_move_assignable<__node_allocator>::value)
1414 __move_assign(__t, integral_constant<bool,
1415 __node_traits::propagate_on_container_move_assignment::value>());
1419 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1421 template <class _Tp, class _Compare, class _Allocator>
1422 __tree<_Tp, _Compare, _Allocator>::~__tree()
1427 template <class _Tp, class _Compare, class _Allocator>
1429 __tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
1431 if (__nd != nullptr)
1433 destroy(static_cast<__node_pointer>(__nd->__left_));
1434 destroy(static_cast<__node_pointer>(__nd->__right_));
1435 __node_allocator& __na = __node_alloc();
1436 __node_traits::destroy(__na, _VSTD::addressof(__nd->__value_));
1437 __node_traits::deallocate(__na, __nd, 1);
1441 template <class _Tp, class _Compare, class _Allocator>
1443 __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1445 __is_nothrow_swappable<value_compare>::value &&
1446 (!__node_traits::propagate_on_container_swap::value ||
1447 __is_nothrow_swappable<__node_allocator>::value))
1450 swap(__begin_node_, __t.__begin_node_);
1451 swap(__pair1_.first(), __t.__pair1_.first());
1452 __swap_alloc(__node_alloc(), __t.__node_alloc());
1453 __pair3_.swap(__t.__pair3_);
1455 __begin_node() = __end_node();
1457 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
1458 if (__t.size() == 0)
1459 __t.__begin_node() = __t.__end_node();
1461 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
1464 template <class _Tp, class _Compare, class _Allocator>
1466 __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
1470 __begin_node() = __end_node();
1471 __end_node()->__left_ = nullptr;
1474 // Find lower_bound place to insert
1475 // Set __parent to parent of null leaf
1476 // Return reference to null leaf
1477 template <class _Tp, class _Compare, class _Allocator>
1478 typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1479 __tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
1480 const value_type& __v)
1482 __node_pointer __nd = __root();
1483 if (__nd != nullptr)
1487 if (value_comp()(__nd->__value_, __v))
1489 if (__nd->__right_ != nullptr)
1490 __nd = static_cast<__node_pointer>(__nd->__right_);
1493 __parent = static_cast<__node_base_pointer>(__nd);
1494 return __parent->__right_;
1499 if (__nd->__left_ != nullptr)
1500 __nd = static_cast<__node_pointer>(__nd->__left_);
1503 __parent = static_cast<__node_base_pointer>(__nd);
1504 return __parent->__left_;
1509 __parent = static_cast<__node_base_pointer>(__end_node());
1510 return __parent->__left_;
1513 // Find upper_bound place to insert
1514 // Set __parent to parent of null leaf
1515 // Return reference to null leaf
1516 template <class _Tp, class _Compare, class _Allocator>
1517 typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1518 __tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
1519 const value_type& __v)
1521 __node_pointer __nd = __root();
1522 if (__nd != nullptr)
1526 if (value_comp()(__v, __nd->__value_))
1528 if (__nd->__left_ != nullptr)
1529 __nd = static_cast<__node_pointer>(__nd->__left_);
1532 __parent = static_cast<__node_base_pointer>(__nd);
1533 return __parent->__left_;
1538 if (__nd->__right_ != nullptr)
1539 __nd = static_cast<__node_pointer>(__nd->__right_);
1542 __parent = static_cast<__node_base_pointer>(__nd);
1543 return __parent->__right_;
1548 __parent = static_cast<__node_base_pointer>(__end_node());
1549 return __parent->__left_;
1552 // Find leaf place to insert closest to __hint
1553 // First check prior to __hint.
1554 // Next check after __hint.
1555 // Next do O(log N) search.
1556 // Set __parent to parent of null leaf
1557 // Return reference to null leaf
1558 template <class _Tp, class _Compare, class _Allocator>
1559 typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1560 __tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
1561 typename __node_base::pointer& __parent,
1562 const value_type& __v)
1564 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1567 const_iterator __prior = __hint;
1568 if (__prior == begin() || !value_comp()(__v, *--__prior))
1570 // *prev(__hint) <= __v <= *__hint
1571 if (__hint.__ptr_->__left_ == nullptr)
1573 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
1574 return __parent->__left_;
1578 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
1579 return __parent->__right_;
1582 // __v < *prev(__hint)
1583 return __find_leaf_high(__parent, __v);
1585 // else __v > *__hint
1586 return __find_leaf_low(__parent, __v);
1589 // Find place to insert if __v doesn't exist
1590 // Set __parent to parent of null leaf
1591 // Return reference to null leaf
1592 // If __v exists, set parent to node of __v and return reference to node of __v
1593 template <class _Tp, class _Compare, class _Allocator>
1594 template <class _Key>
1595 typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1596 __tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
1599 __node_pointer __nd = __root();
1600 if (__nd != nullptr)
1604 if (value_comp()(__v, __nd->__value_))
1606 if (__nd->__left_ != nullptr)
1607 __nd = static_cast<__node_pointer>(__nd->__left_);
1610 __parent = static_cast<__node_base_pointer>(__nd);
1611 return __parent->__left_;
1614 else if (value_comp()(__nd->__value_, __v))
1616 if (__nd->__right_ != nullptr)
1617 __nd = static_cast<__node_pointer>(__nd->__right_);
1620 __parent = static_cast<__node_base_pointer>(__nd);
1621 return __parent->__right_;
1626 __parent = static_cast<__node_base_pointer>(__nd);
1631 __parent = static_cast<__node_base_pointer>(__end_node());
1632 return __parent->__left_;
1635 // Find place to insert if __v doesn't exist
1636 // First check prior to __hint.
1637 // Next check after __hint.
1638 // Next do O(log N) search.
1639 // Set __parent to parent of null leaf
1640 // Return reference to null leaf
1641 // If __v exists, set parent to node of __v and return reference to node of __v
1642 template <class _Tp, class _Compare, class _Allocator>
1643 template <class _Key>
1644 typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1645 __tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
1646 typename __node_base::pointer& __parent,
1649 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1652 const_iterator __prior = __hint;
1653 if (__prior == begin() || value_comp()(*--__prior, __v))
1655 // *prev(__hint) < __v < *__hint
1656 if (__hint.__ptr_->__left_ == nullptr)
1658 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
1659 return __parent->__left_;
1663 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
1664 return __parent->__right_;
1667 // __v <= *prev(__hint)
1668 return __find_equal(__parent, __v);
1670 else if (value_comp()(*__hint, __v)) // check after
1673 const_iterator __next = _VSTD::next(__hint);
1674 if (__next == end() || value_comp()(__v, *__next))
1676 // *__hint < __v < *_VSTD::next(__hint)
1677 if (__hint.__ptr_->__right_ == nullptr)
1679 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
1680 return __parent->__right_;
1684 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
1685 return __parent->__left_;
1688 // *next(__hint) <= __v
1689 return __find_equal(__parent, __v);
1691 // else __v == *__hint
1692 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
1696 template <class _Tp, class _Compare, class _Allocator>
1698 __tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1699 __node_base_pointer& __child,
1700 __node_base_pointer __new_node)
1702 __new_node->__left_ = nullptr;
1703 __new_node->__right_ = nullptr;
1704 __new_node->__parent_ = __parent;
1705 __child = __new_node;
1706 if (__begin_node()->__left_ != nullptr)
1707 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1708 __tree_balance_after_insert(__end_node()->__left_, __child);
1712 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1713 #ifndef _LIBCPP_HAS_NO_VARIADICS
1715 template <class _Tp, class _Compare, class _Allocator>
1716 template <class ..._Args>
1717 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1718 __tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1720 __node_allocator& __na = __node_alloc();
1721 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1722 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
1723 __h.get_deleter().__value_constructed = true;
1727 template <class _Tp, class _Compare, class _Allocator>
1728 template <class... _Args>
1729 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1730 __tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1732 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1733 __node_base_pointer __parent;
1734 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1735 __node_pointer __r = static_cast<__node_pointer>(__child);
1736 bool __inserted = false;
1737 if (__child == nullptr)
1739 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1740 __r = __h.release();
1743 return pair<iterator, bool>(iterator(__r), __inserted);
1746 template <class _Tp, class _Compare, class _Allocator>
1747 template <class... _Args>
1748 typename __tree<_Tp, _Compare, _Allocator>::iterator
1749 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1751 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1752 __node_base_pointer __parent;
1753 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1754 __node_pointer __r = static_cast<__node_pointer>(__child);
1755 if (__child == nullptr)
1757 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1758 __r = __h.release();
1760 return iterator(__r);
1763 template <class _Tp, class _Compare, class _Allocator>
1764 template <class... _Args>
1765 typename __tree<_Tp, _Compare, _Allocator>::iterator
1766 __tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1768 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1769 __node_base_pointer __parent;
1770 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1771 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1772 return iterator(static_cast<__node_pointer>(__h.release()));
1775 template <class _Tp, class _Compare, class _Allocator>
1776 template <class... _Args>
1777 typename __tree<_Tp, _Compare, _Allocator>::iterator
1778 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1781 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1782 __node_base_pointer __parent;
1783 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1784 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1785 return iterator(static_cast<__node_pointer>(__h.release()));
1788 #endif // _LIBCPP_HAS_NO_VARIADICS
1790 template <class _Tp, class _Compare, class _Allocator>
1791 template <class _Vp>
1792 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1793 __tree<_Tp, _Compare, _Allocator>::__insert_unique(_Vp&& __v)
1795 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
1796 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1802 template <class _Tp, class _Compare, class _Allocator>
1803 template <class _Vp>
1804 typename __tree<_Tp, _Compare, _Allocator>::iterator
1805 __tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _Vp&& __v)
1807 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
1808 iterator __r = __node_insert_unique(__p, __h.get());
1809 if (__r.__ptr_ == __h.get())
1814 template <class _Tp, class _Compare, class _Allocator>
1815 template <class _Vp>
1816 typename __tree<_Tp, _Compare, _Allocator>::iterator
1817 __tree<_Tp, _Compare, _Allocator>::__insert_multi(_Vp&& __v)
1819 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
1820 __node_base_pointer __parent;
1821 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1822 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1823 return iterator(__h.release());
1826 template <class _Tp, class _Compare, class _Allocator>
1827 template <class _Vp>
1828 typename __tree<_Tp, _Compare, _Allocator>::iterator
1829 __tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _Vp&& __v)
1831 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
1832 __node_base_pointer __parent;
1833 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1834 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1835 return iterator(__h.release());
1838 #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1840 template <class _Tp, class _Compare, class _Allocator>
1841 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1842 __tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1844 __node_allocator& __na = __node_alloc();
1845 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1846 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
1847 __h.get_deleter().__value_constructed = true;
1848 return _VSTD::move(__h);
1851 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1853 template <class _Tp, class _Compare, class _Allocator>
1854 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1855 __tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1857 __node_base_pointer __parent;
1858 __node_base_pointer& __child = __find_equal(__parent, __v);
1859 __node_pointer __r = static_cast<__node_pointer>(__child);
1860 bool __inserted = false;
1861 if (__child == nullptr)
1863 __node_holder __h = __construct_node(__v);
1864 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1865 __r = __h.release();
1868 return pair<iterator, bool>(iterator(__r), __inserted);
1871 template <class _Tp, class _Compare, class _Allocator>
1872 typename __tree<_Tp, _Compare, _Allocator>::iterator
1873 __tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1875 __node_base_pointer __parent;
1876 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1877 __node_pointer __r = static_cast<__node_pointer>(__child);
1878 if (__child == nullptr)
1880 __node_holder __h = __construct_node(__v);
1881 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1882 __r = __h.release();
1884 return iterator(__r);
1887 template <class _Tp, class _Compare, class _Allocator>
1888 typename __tree<_Tp, _Compare, _Allocator>::iterator
1889 __tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1891 __node_base_pointer __parent;
1892 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1893 __node_holder __h = __construct_node(__v);
1894 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1895 return iterator(__h.release());
1898 template <class _Tp, class _Compare, class _Allocator>
1899 typename __tree<_Tp, _Compare, _Allocator>::iterator
1900 __tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1902 __node_base_pointer __parent;
1903 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1904 __node_holder __h = __construct_node(__v);
1905 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1906 return iterator(__h.release());
1909 template <class _Tp, class _Compare, class _Allocator>
1910 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1911 __tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1913 __node_base_pointer __parent;
1914 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1915 __node_pointer __r = static_cast<__node_pointer>(__child);
1916 bool __inserted = false;
1917 if (__child == nullptr)
1919 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1923 return pair<iterator, bool>(iterator(__r), __inserted);
1926 template <class _Tp, class _Compare, class _Allocator>
1927 typename __tree<_Tp, _Compare, _Allocator>::iterator
1928 __tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1929 __node_pointer __nd)
1931 __node_base_pointer __parent;
1932 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1933 __node_pointer __r = static_cast<__node_pointer>(__child);
1934 if (__child == nullptr)
1936 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1939 return iterator(__r);
1942 template <class _Tp, class _Compare, class _Allocator>
1943 typename __tree<_Tp, _Compare, _Allocator>::iterator
1944 __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1946 __node_base_pointer __parent;
1947 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
1948 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1949 return iterator(__nd);
1952 template <class _Tp, class _Compare, class _Allocator>
1953 typename __tree<_Tp, _Compare, _Allocator>::iterator
1954 __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1955 __node_pointer __nd)
1957 __node_base_pointer __parent;
1958 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
1959 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1960 return iterator(__nd);
1963 template <class _Tp, class _Compare, class _Allocator>
1964 typename __tree<_Tp, _Compare, _Allocator>::iterator
1965 __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1967 __node_pointer __np = __p.__ptr_;
1970 if (__begin_node() == __np)
1971 __begin_node() = __r.__ptr_;
1973 __node_allocator& __na = __node_alloc();
1974 __node_traits::destroy(__na, const_cast<value_type*>(_VSTD::addressof(*__p)));
1975 __tree_remove(__end_node()->__left_,
1976 static_cast<__node_base_pointer>(__np));
1977 __node_traits::deallocate(__na, __np, 1);
1981 template <class _Tp, class _Compare, class _Allocator>
1982 typename __tree<_Tp, _Compare, _Allocator>::iterator
1983 __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1987 return iterator(__l.__ptr_);
1990 template <class _Tp, class _Compare, class _Allocator>
1991 template <class _Key>
1992 typename __tree<_Tp, _Compare, _Allocator>::size_type
1993 __tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1995 iterator __i = find(__k);
2002 template <class _Tp, class _Compare, class _Allocator>
2003 template <class _Key>
2004 typename __tree<_Tp, _Compare, _Allocator>::size_type
2005 __tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2007 pair<iterator, iterator> __p = __equal_range_multi(__k);
2009 for (; __p.first != __p.second; ++__r)
2010 __p.first = erase(__p.first);
2014 template <class _Tp, class _Compare, class _Allocator>
2015 template <class _Key>
2016 typename __tree<_Tp, _Compare, _Allocator>::iterator
2017 __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2019 iterator __p = __lower_bound(__v, __root(), __end_node());
2020 if (__p != end() && !value_comp()(__v, *__p))
2025 template <class _Tp, class _Compare, class _Allocator>
2026 template <class _Key>
2027 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2028 __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2030 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2031 if (__p != end() && !value_comp()(__v, *__p))
2036 template <class _Tp, class _Compare, class _Allocator>
2037 template <class _Key>
2038 typename __tree<_Tp, _Compare, _Allocator>::size_type
2039 __tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2041 __node_const_pointer __result = __end_node();
2042 __node_const_pointer __rt = __root();
2043 while (__rt != nullptr)
2045 if (value_comp()(__k, __rt->__value_))
2048 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2050 else if (value_comp()(__rt->__value_, __k))
2051 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2058 template <class _Tp, class _Compare, class _Allocator>
2059 template <class _Key>
2060 typename __tree<_Tp, _Compare, _Allocator>::size_type
2061 __tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2063 typedef pair<const_iterator, const_iterator> _Pp;
2064 __node_const_pointer __result = __end_node();
2065 __node_const_pointer __rt = __root();
2066 while (__rt != nullptr)
2068 if (value_comp()(__k, __rt->__value_))
2071 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2073 else if (value_comp()(__rt->__value_, __k))
2074 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2076 return _VSTD::distance(
2077 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2078 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
2084 template <class _Tp, class _Compare, class _Allocator>
2085 template <class _Key>
2086 typename __tree<_Tp, _Compare, _Allocator>::iterator
2087 __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2088 __node_pointer __root,
2089 __node_pointer __result)
2091 while (__root != nullptr)
2093 if (!value_comp()(__root->__value_, __v))
2096 __root = static_cast<__node_pointer>(__root->__left_);
2099 __root = static_cast<__node_pointer>(__root->__right_);
2101 return iterator(__result);
2104 template <class _Tp, class _Compare, class _Allocator>
2105 template <class _Key>
2106 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2107 __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2108 __node_const_pointer __root,
2109 __node_const_pointer __result) const
2111 while (__root != nullptr)
2113 if (!value_comp()(__root->__value_, __v))
2116 __root = static_cast<__node_const_pointer>(__root->__left_);
2119 __root = static_cast<__node_const_pointer>(__root->__right_);
2121 return const_iterator(__result);
2124 template <class _Tp, class _Compare, class _Allocator>
2125 template <class _Key>
2126 typename __tree<_Tp, _Compare, _Allocator>::iterator
2127 __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2128 __node_pointer __root,
2129 __node_pointer __result)
2131 while (__root != nullptr)
2133 if (value_comp()(__v, __root->__value_))
2136 __root = static_cast<__node_pointer>(__root->__left_);
2139 __root = static_cast<__node_pointer>(__root->__right_);
2141 return iterator(__result);
2144 template <class _Tp, class _Compare, class _Allocator>
2145 template <class _Key>
2146 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2147 __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2148 __node_const_pointer __root,
2149 __node_const_pointer __result) const
2151 while (__root != nullptr)
2153 if (value_comp()(__v, __root->__value_))
2156 __root = static_cast<__node_const_pointer>(__root->__left_);
2159 __root = static_cast<__node_const_pointer>(__root->__right_);
2161 return const_iterator(__result);
2164 template <class _Tp, class _Compare, class _Allocator>
2165 template <class _Key>
2166 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2167 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2168 __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2170 typedef pair<iterator, iterator> _Pp;
2171 __node_pointer __result = __end_node();
2172 __node_pointer __rt = __root();
2173 while (__rt != nullptr)
2175 if (value_comp()(__k, __rt->__value_))
2178 __rt = static_cast<__node_pointer>(__rt->__left_);
2180 else if (value_comp()(__rt->__value_, __k))
2181 __rt = static_cast<__node_pointer>(__rt->__right_);
2183 return _Pp(iterator(__rt),
2185 __rt->__right_ != nullptr ?
2186 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2189 return _Pp(iterator(__result), iterator(__result));
2192 template <class _Tp, class _Compare, class _Allocator>
2193 template <class _Key>
2194 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2195 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2196 __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2198 typedef pair<const_iterator, const_iterator> _Pp;
2199 __node_const_pointer __result = __end_node();
2200 __node_const_pointer __rt = __root();
2201 while (__rt != nullptr)
2203 if (value_comp()(__k, __rt->__value_))
2206 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2208 else if (value_comp()(__rt->__value_, __k))
2209 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2211 return _Pp(const_iterator(__rt),
2213 __rt->__right_ != nullptr ?
2214 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2217 return _Pp(const_iterator(__result), const_iterator(__result));
2220 template <class _Tp, class _Compare, class _Allocator>
2221 template <class _Key>
2222 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2223 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2224 __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2226 typedef pair<iterator, iterator> _Pp;
2227 __node_pointer __result = __end_node();
2228 __node_pointer __rt = __root();
2229 while (__rt != nullptr)
2231 if (value_comp()(__k, __rt->__value_))
2234 __rt = static_cast<__node_pointer>(__rt->__left_);
2236 else if (value_comp()(__rt->__value_, __k))
2237 __rt = static_cast<__node_pointer>(__rt->__right_);
2239 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2240 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2242 return _Pp(iterator(__result), iterator(__result));
2245 template <class _Tp, class _Compare, class _Allocator>
2246 template <class _Key>
2247 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2248 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2249 __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2251 typedef pair<const_iterator, const_iterator> _Pp;
2252 __node_const_pointer __result = __end_node();
2253 __node_const_pointer __rt = __root();
2254 while (__rt != nullptr)
2256 if (value_comp()(__k, __rt->__value_))
2259 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2261 else if (value_comp()(__rt->__value_, __k))
2262 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2264 return _Pp(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2265 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2267 return _Pp(const_iterator(__result), const_iterator(__result));
2270 template <class _Tp, class _Compare, class _Allocator>
2271 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2272 __tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
2274 __node_pointer __np = __p.__ptr_;
2275 if (__begin_node() == __np)
2277 if (__np->__right_ != nullptr)
2278 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2280 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2283 __tree_remove(__end_node()->__left_,
2284 static_cast<__node_base_pointer>(__np));
2285 return __node_holder(__np, _Dp(__node_alloc()));
2288 template <class _Tp, class _Compare, class _Allocator>
2289 inline _LIBCPP_INLINE_VISIBILITY
2291 swap(__tree<_Tp, _Compare, _Allocator>& __x,
2292 __tree<_Tp, _Compare, _Allocator>& __y)
2293 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2298 _LIBCPP_END_NAMESPACE_STD
2300 #endif // _LIBCPP___TREE