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
25 #include <__undef_macros>
28 _LIBCPP_BEGIN_NAMESPACE_STD
30 template <class _Tp, class _Compare, class _Allocator> class __tree;
31 template <class _Tp, class _NodePtr, class _DiffType>
32 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
33 template <class _Tp, class _ConstNodePtr, class _DiffType>
34 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
36 template <class _Pointer> class __tree_end_node;
37 template <class _VoidPtr> class __tree_node_base;
38 template <class _Tp, class _VoidPtr> class __tree_node;
40 template <class _Key, class _Value>
43 template <class _Key, class _CP, class _Compare,
44 bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
45 class __map_value_compare;
47 template <class _Allocator> class __map_node_destructor;
48 template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
49 template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
55 The algorithms taking _NodePtr are red black tree algorithms. Those
56 algorithms taking a parameter named __root should assume that __root
57 points to a proper red black tree (unless otherwise specified).
59 Each algorithm herein assumes that __root->__parent_ points to a non-null
60 structure which has a member __left_ which points back to __root. No other
61 member is read or written to at __root->__parent_.
63 __root->__parent_ will be referred to below (in comments only) as end_node.
64 end_node->__left_ is an externably accessible lvalue for __root, and can be
65 changed by node insertion and removal (without explicit reference to end_node).
67 All nodes (with the exception of end_node), even the node referred to as
68 __root, have a non-null __parent_ field.
72 // Returns: true if __x is a left child of its parent, else false
73 // Precondition: __x != nullptr.
74 template <class _NodePtr>
75 inline _LIBCPP_INLINE_VISIBILITY
77 __tree_is_left_child(_NodePtr __x) _NOEXCEPT
79 return __x == __x->__parent_->__left_;
82 // Determines if the subtree rooted at __x is a proper red black subtree. If
83 // __x is a proper subtree, returns the black height (null counts as 1). If
84 // __x is an improper subtree, returns 0.
85 template <class _NodePtr>
87 __tree_sub_invariant(_NodePtr __x)
91 // parent consistency checked by caller
92 // check __x->__left_ consistency
93 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
95 // check __x->__right_ consistency
96 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
98 // check __x->__left_ != __x->__right_ unless both are nullptr
99 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
101 // If this is red, neither child can be red
102 if (!__x->__is_black_)
104 if (__x->__left_ && !__x->__left_->__is_black_)
106 if (__x->__right_ && !__x->__right_->__is_black_)
109 unsigned __h = __tree_sub_invariant(__x->__left_);
111 return 0; // invalid left subtree
112 if (__h != __tree_sub_invariant(__x->__right_))
113 return 0; // invalid or different height right subtree
114 return __h + __x->__is_black_; // return black height of this node
117 // Determines if the red black tree rooted at __root is a proper red black tree.
118 // __root == nullptr is a proper tree. Returns true is __root is a proper
119 // red black tree, else returns false.
120 template <class _NodePtr>
122 __tree_invariant(_NodePtr __root)
124 if (__root == nullptr)
126 // check __x->__parent_ consistency
127 if (__root->__parent_ == nullptr)
129 if (!__tree_is_left_child(__root))
131 // root must be black
132 if (!__root->__is_black_)
134 // do normal node checks
135 return __tree_sub_invariant(__root) != 0;
138 // Returns: pointer to the left-most node under __x.
139 // Precondition: __x != nullptr.
140 template <class _NodePtr>
141 inline _LIBCPP_INLINE_VISIBILITY
143 __tree_min(_NodePtr __x) _NOEXCEPT
145 while (__x->__left_ != nullptr)
150 // Returns: pointer to the right-most node under __x.
151 // Precondition: __x != nullptr.
152 template <class _NodePtr>
153 inline _LIBCPP_INLINE_VISIBILITY
155 __tree_max(_NodePtr __x) _NOEXCEPT
157 while (__x->__right_ != nullptr)
162 // Returns: pointer to the next in-order node after __x.
163 // Precondition: __x != nullptr.
164 template <class _NodePtr>
166 __tree_next(_NodePtr __x) _NOEXCEPT
168 if (__x->__right_ != nullptr)
169 return __tree_min(__x->__right_);
170 while (!__tree_is_left_child(__x))
171 __x = __x->__parent_unsafe();
172 return __x->__parent_unsafe();
175 template <class _EndNodePtr, class _NodePtr>
176 inline _LIBCPP_INLINE_VISIBILITY
178 __tree_next_iter(_NodePtr __x) _NOEXCEPT
180 if (__x->__right_ != nullptr)
181 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
182 while (!__tree_is_left_child(__x))
183 __x = __x->__parent_unsafe();
184 return static_cast<_EndNodePtr>(__x->__parent_);
187 // Returns: pointer to the previous in-order node before __x.
188 // Precondition: __x != nullptr.
189 // Note: __x may be the end node.
190 template <class _NodePtr, class _EndNodePtr>
191 inline _LIBCPP_INLINE_VISIBILITY
193 __tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
195 if (__x->__left_ != nullptr)
196 return __tree_max(__x->__left_);
197 _NodePtr __xx = static_cast<_NodePtr>(__x);
198 while (__tree_is_left_child(__xx))
199 __xx = __xx->__parent_unsafe();
200 return __xx->__parent_unsafe();
203 // Returns: pointer to a node which has no children
204 // Precondition: __x != nullptr.
205 template <class _NodePtr>
207 __tree_leaf(_NodePtr __x) _NOEXCEPT
211 if (__x->__left_ != nullptr)
216 if (__x->__right_ != nullptr)
226 // Effects: Makes __x->__right_ the subtree root with __x as its left child
227 // while preserving in-order order.
228 // Precondition: __x->__right_ != nullptr
229 template <class _NodePtr>
231 __tree_left_rotate(_NodePtr __x) _NOEXCEPT
233 _NodePtr __y = __x->__right_;
234 __x->__right_ = __y->__left_;
235 if (__x->__right_ != nullptr)
236 __x->__right_->__set_parent(__x);
237 __y->__parent_ = __x->__parent_;
238 if (__tree_is_left_child(__x))
239 __x->__parent_->__left_ = __y;
241 __x->__parent_unsafe()->__right_ = __y;
243 __x->__set_parent(__y);
246 // Effects: Makes __x->__left_ the subtree root with __x as its right child
247 // while preserving in-order order.
248 // Precondition: __x->__left_ != nullptr
249 template <class _NodePtr>
251 __tree_right_rotate(_NodePtr __x) _NOEXCEPT
253 _NodePtr __y = __x->__left_;
254 __x->__left_ = __y->__right_;
255 if (__x->__left_ != nullptr)
256 __x->__left_->__set_parent(__x);
257 __y->__parent_ = __x->__parent_;
258 if (__tree_is_left_child(__x))
259 __x->__parent_->__left_ = __y;
261 __x->__parent_unsafe()->__right_ = __y;
263 __x->__set_parent(__y);
266 // Effects: Rebalances __root after attaching __x to a leaf.
267 // Precondition: __root != nulptr && __x != nullptr.
268 // __x has no children.
269 // __x == __root or == a direct or indirect child of __root.
270 // If __x were to be unlinked from __root (setting __root to
271 // nullptr if __root == __x), __tree_invariant(__root) == true.
272 // Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
273 // may be different than the value passed in as __root.
274 template <class _NodePtr>
276 __tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
278 __x->__is_black_ = __x == __root;
279 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
281 // __x->__parent_ != __root because __x->__parent_->__is_black == false
282 if (__tree_is_left_child(__x->__parent_unsafe()))
284 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
285 if (__y != nullptr && !__y->__is_black_)
287 __x = __x->__parent_unsafe();
288 __x->__is_black_ = true;
289 __x = __x->__parent_unsafe();
290 __x->__is_black_ = __x == __root;
291 __y->__is_black_ = true;
295 if (!__tree_is_left_child(__x))
297 __x = __x->__parent_unsafe();
298 __tree_left_rotate(__x);
300 __x = __x->__parent_unsafe();
301 __x->__is_black_ = true;
302 __x = __x->__parent_unsafe();
303 __x->__is_black_ = false;
304 __tree_right_rotate(__x);
310 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
311 if (__y != nullptr && !__y->__is_black_)
313 __x = __x->__parent_unsafe();
314 __x->__is_black_ = true;
315 __x = __x->__parent_unsafe();
316 __x->__is_black_ = __x == __root;
317 __y->__is_black_ = true;
321 if (__tree_is_left_child(__x))
323 __x = __x->__parent_unsafe();
324 __tree_right_rotate(__x);
326 __x = __x->__parent_unsafe();
327 __x->__is_black_ = true;
328 __x = __x->__parent_unsafe();
329 __x->__is_black_ = false;
330 __tree_left_rotate(__x);
337 // Precondition: __root != nullptr && __z != nullptr.
338 // __tree_invariant(__root) == true.
339 // __z == __root or == a direct or indirect child of __root.
340 // Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
341 // Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
342 // nor any of its children refer to __z. end_node->__left_
343 // may be different than the value passed in as __root.
344 template <class _NodePtr>
346 __tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
348 // __z will be removed from the tree. Client still needs to destruct/deallocate it
349 // __y is either __z, or if __z has two children, __tree_next(__z).
350 // __y will have at most one child.
351 // __y will be the initial hole in the tree (make the hole at a leaf)
352 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
353 __z : __tree_next(__z);
354 // __x is __y's possibly null single child
355 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
356 // __w is __x's possibly null uncle (will become __x's sibling)
357 _NodePtr __w = nullptr;
358 // link __x to __y's parent, and find __w
360 __x->__parent_ = __y->__parent_;
361 if (__tree_is_left_child(__y))
363 __y->__parent_->__left_ = __x;
365 __w = __y->__parent_unsafe()->__right_;
367 __root = __x; // __w == nullptr
371 __y->__parent_unsafe()->__right_ = __x;
372 // __y can't be root if it is a right child
373 __w = __y->__parent_->__left_;
375 bool __removed_black = __y->__is_black_;
376 // If we didn't remove __z, do so now by splicing in __y for __z,
377 // but copy __z's color. This does not impact __x or __w.
380 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
381 __y->__parent_ = __z->__parent_;
382 if (__tree_is_left_child(__z))
383 __y->__parent_->__left_ = __y;
385 __y->__parent_unsafe()->__right_ = __y;
386 __y->__left_ = __z->__left_;
387 __y->__left_->__set_parent(__y);
388 __y->__right_ = __z->__right_;
389 if (__y->__right_ != nullptr)
390 __y->__right_->__set_parent(__y);
391 __y->__is_black_ = __z->__is_black_;
395 // There is no need to rebalance if we removed a red, or if we removed
397 if (__removed_black && __root != nullptr)
400 // __x has an implicit black color (transferred from the removed __y)
401 // associated with it, no matter what its color is.
402 // If __x is __root (in which case it can't be null), it is supposed
403 // to be black anyway, and if it is doubly black, then the double
404 // can just be ignored.
405 // If __x is red (in which case it can't be null), then it can absorb
406 // the implicit black just by setting its color to black.
407 // Since __y was black and only had one child (which __x points to), __x
408 // is either red with no children, else null, otherwise __y would have
409 // different black heights under left and right pointers.
410 // if (__x == __root || __x != nullptr && !__x->__is_black_)
412 __x->__is_black_ = true;
415 // Else __x isn't root, and is "doubly black", even though it may
416 // be null. __w can not be null here, else the parent would
417 // see a black height >= 2 on the __x side and a black height
418 // of 1 on the __w side (__w must be a non-null black or a red
419 // with a non-null black child).
422 if (!__tree_is_left_child(__w)) // if x is left child
424 if (!__w->__is_black_)
426 __w->__is_black_ = true;
427 __w->__parent_unsafe()->__is_black_ = false;
428 __tree_left_rotate(__w->__parent_unsafe());
429 // __x is still valid
430 // reset __root only if necessary
431 if (__root == __w->__left_)
433 // reset sibling, and it still can't be null
434 __w = __w->__left_->__right_;
436 // __w->__is_black_ is now true, __w may have null children
437 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
438 (__w->__right_ == nullptr || __w->__right_->__is_black_))
440 __w->__is_black_ = false;
441 __x = __w->__parent_unsafe();
442 // __x can no longer be null
443 if (__x == __root || !__x->__is_black_)
445 __x->__is_black_ = true;
448 // reset sibling, and it still can't be null
449 __w = __tree_is_left_child(__x) ?
450 __x->__parent_unsafe()->__right_ :
451 __x->__parent_->__left_;
454 else // __w has a red child
456 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
458 // __w left child is non-null and red
459 __w->__left_->__is_black_ = true;
460 __w->__is_black_ = false;
461 __tree_right_rotate(__w);
462 // __w is known not to be root, so root hasn't changed
463 // reset sibling, and it still can't be null
464 __w = __w->__parent_unsafe();
466 // __w has a right red child, left child may be null
467 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
468 __w->__parent_unsafe()->__is_black_ = true;
469 __w->__right_->__is_black_ = true;
470 __tree_left_rotate(__w->__parent_unsafe());
476 if (!__w->__is_black_)
478 __w->__is_black_ = true;
479 __w->__parent_unsafe()->__is_black_ = false;
480 __tree_right_rotate(__w->__parent_unsafe());
481 // __x is still valid
482 // reset __root only if necessary
483 if (__root == __w->__right_)
485 // reset sibling, and it still can't be null
486 __w = __w->__right_->__left_;
488 // __w->__is_black_ is now true, __w may have null children
489 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
490 (__w->__right_ == nullptr || __w->__right_->__is_black_))
492 __w->__is_black_ = false;
493 __x = __w->__parent_unsafe();
494 // __x can no longer be null
495 if (!__x->__is_black_ || __x == __root)
497 __x->__is_black_ = true;
500 // reset sibling, and it still can't be null
501 __w = __tree_is_left_child(__x) ?
502 __x->__parent_unsafe()->__right_ :
503 __x->__parent_->__left_;
506 else // __w has a red child
508 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
510 // __w right child is non-null and red
511 __w->__right_->__is_black_ = true;
512 __w->__is_black_ = false;
513 __tree_left_rotate(__w);
514 // __w is known not to be root, so root hasn't changed
515 // reset sibling, and it still can't be null
516 __w = __w->__parent_unsafe();
518 // __w has a left red child, right child may be null
519 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
520 __w->__parent_unsafe()->__is_black_ = true;
521 __w->__left_->__is_black_ = true;
522 __tree_right_rotate(__w->__parent_unsafe());
534 #ifndef _LIBCPP_CXX03_LANG
536 struct __is_tree_value_type_imp : false_type {};
538 template <class _Key, class _Value>
539 struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
541 template <class ..._Args>
542 struct __is_tree_value_type : false_type {};
544 template <class _One>
545 struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
549 struct __tree_key_value_types {
550 typedef _Tp key_type;
551 typedef _Tp __node_value_type;
552 typedef _Tp __container_value_type;
553 static const bool __is_map = false;
555 _LIBCPP_INLINE_VISIBILITY
556 static key_type const& __get_key(_Tp const& __v) {
559 _LIBCPP_INLINE_VISIBILITY
560 static __container_value_type const& __get_value(__node_value_type const& __v) {
563 _LIBCPP_INLINE_VISIBILITY
564 static __container_value_type* __get_ptr(__node_value_type& __n) {
565 return _VSTD::addressof(__n);
567 #ifndef _LIBCPP_CXX03_LANG
568 _LIBCPP_INLINE_VISIBILITY
569 static __container_value_type&& __move(__node_value_type& __v) {
570 return _VSTD::move(__v);
575 template <class _Key, class _Tp>
576 struct __tree_key_value_types<__value_type<_Key, _Tp> > {
577 typedef _Key key_type;
578 typedef _Tp mapped_type;
579 typedef __value_type<_Key, _Tp> __node_value_type;
580 typedef pair<const _Key, _Tp> __container_value_type;
581 typedef __container_value_type __map_value_type;
582 static const bool __is_map = true;
584 _LIBCPP_INLINE_VISIBILITY
585 static key_type const&
586 __get_key(__node_value_type const& __t) {
587 return __t.__get_value().first;
591 _LIBCPP_INLINE_VISIBILITY
592 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
593 key_type const&>::type
594 __get_key(_Up& __t) {
598 _LIBCPP_INLINE_VISIBILITY
599 static __container_value_type const&
600 __get_value(__node_value_type const& __t) {
601 return __t.__get_value();
605 _LIBCPP_INLINE_VISIBILITY
606 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
607 __container_value_type const&>::type
608 __get_value(_Up& __t) {
612 _LIBCPP_INLINE_VISIBILITY
613 static __container_value_type* __get_ptr(__node_value_type& __n) {
614 return _VSTD::addressof(__n.__get_value());
617 #ifndef _LIBCPP_CXX03_LANG
618 _LIBCPP_INLINE_VISIBILITY
619 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
625 template <class _VoidPtr>
626 struct __tree_node_base_types {
627 typedef _VoidPtr __void_pointer;
629 typedef __tree_node_base<__void_pointer> __node_base_type;
630 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
633 typedef __tree_end_node<__node_base_pointer> __end_node_type;
634 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
636 #if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
637 typedef __end_node_pointer __parent_pointer;
639 typedef typename conditional<
640 is_pointer<__end_node_pointer>::value,
642 __node_base_pointer>::type __parent_pointer;
646 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
647 "_VoidPtr does not point to unqualified void type");
650 template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
651 bool = _KVTypes::__is_map>
652 struct __tree_map_pointer_types {};
654 template <class _Tp, class _AllocPtr, class _KVTypes>
655 struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
656 typedef typename _KVTypes::__map_value_type _Mv;
657 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
658 __map_value_type_pointer;
659 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
660 __const_map_value_type_pointer;
663 template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
664 struct __tree_node_types;
666 template <class _NodePtr, class _Tp, class _VoidPtr>
667 struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
668 : public __tree_node_base_types<_VoidPtr>,
669 __tree_key_value_types<_Tp>,
670 __tree_map_pointer_types<_Tp, _VoidPtr>
672 typedef __tree_node_base_types<_VoidPtr> __base;
673 typedef __tree_key_value_types<_Tp> __key_base;
674 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
677 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
678 typedef _NodePtr __node_pointer;
680 typedef _Tp __node_value_type;
681 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
682 __node_value_type_pointer;
683 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
684 __const_node_value_type_pointer;
685 #if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
686 typedef typename __base::__end_node_pointer __iter_pointer;
688 typedef typename conditional<
689 is_pointer<__node_pointer>::value,
690 typename __base::__end_node_pointer,
691 __node_pointer>::type __iter_pointer;
694 static_assert(!is_const<__node_type>::value,
695 "_NodePtr should never be a pointer to const");
696 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
697 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
700 template <class _ValueTp, class _VoidPtr>
701 struct __make_tree_node_types {
702 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
704 typedef __tree_node_types<_NodePtr> type;
709 template <class _Pointer>
710 class __tree_end_node
713 typedef _Pointer pointer;
716 _LIBCPP_INLINE_VISIBILITY
717 __tree_end_node() _NOEXCEPT : __left_() {}
720 template <class _VoidPtr>
721 class __tree_node_base
722 : public __tree_node_base_types<_VoidPtr>::__end_node_type
724 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
727 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
728 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
731 __parent_pointer __parent_;
734 _LIBCPP_INLINE_VISIBILITY
735 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
737 _LIBCPP_INLINE_VISIBILITY
738 void __set_parent(pointer __p) {
739 __parent_ = static_cast<__parent_pointer>(__p);
743 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
744 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
745 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
748 template <class _Tp, class _VoidPtr>
750 : public __tree_node_base<_VoidPtr>
753 typedef _Tp __node_value_type;
755 __node_value_type __value_;
758 ~__tree_node() _LIBCPP_EQUAL_DELETE;
759 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
760 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
764 template <class _Allocator>
765 class __tree_node_destructor
767 typedef _Allocator allocator_type;
768 typedef allocator_traits<allocator_type> __alloc_traits;
771 typedef typename __alloc_traits::pointer pointer;
773 typedef __tree_node_types<pointer> _NodeTypes;
774 allocator_type& __na_;
776 __tree_node_destructor& operator=(const __tree_node_destructor&);
779 bool __value_constructed;
781 _LIBCPP_INLINE_VISIBILITY
782 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
784 __value_constructed(__val)
787 _LIBCPP_INLINE_VISIBILITY
788 void operator()(pointer __p) _NOEXCEPT
790 if (__value_constructed)
791 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
793 __alloc_traits::deallocate(__na_, __p, 1);
796 template <class> friend class __map_node_destructor;
800 template <class _Tp, class _NodePtr, class _DiffType>
801 class _LIBCPP_TEMPLATE_VIS __tree_iterator
803 typedef __tree_node_types<_NodePtr> _NodeTypes;
804 typedef _NodePtr __node_pointer;
805 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
806 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
807 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
808 typedef pointer_traits<__node_pointer> __pointer_traits;
810 __iter_pointer __ptr_;
813 typedef bidirectional_iterator_tag iterator_category;
814 typedef _Tp value_type;
815 typedef _DiffType difference_type;
816 typedef value_type& reference;
817 typedef typename _NodeTypes::__node_value_type_pointer pointer;
819 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
820 #if _LIBCPP_STD_VER > 11
825 _LIBCPP_INLINE_VISIBILITY reference operator*() const
826 {return __get_np()->__value_;}
827 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
828 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
830 _LIBCPP_INLINE_VISIBILITY
831 __tree_iterator& operator++() {
832 __ptr_ = static_cast<__iter_pointer>(
833 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
836 _LIBCPP_INLINE_VISIBILITY
837 __tree_iterator operator++(int)
838 {__tree_iterator __t(*this); ++(*this); return __t;}
840 _LIBCPP_INLINE_VISIBILITY
841 __tree_iterator& operator--() {
842 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
843 static_cast<__end_node_pointer>(__ptr_)));
846 _LIBCPP_INLINE_VISIBILITY
847 __tree_iterator operator--(int)
848 {__tree_iterator __t(*this); --(*this); return __t;}
850 friend _LIBCPP_INLINE_VISIBILITY
851 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
852 {return __x.__ptr_ == __y.__ptr_;}
853 friend _LIBCPP_INLINE_VISIBILITY
854 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
855 {return !(__x == __y);}
858 _LIBCPP_INLINE_VISIBILITY
859 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
860 _LIBCPP_INLINE_VISIBILITY
861 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
862 _LIBCPP_INLINE_VISIBILITY
863 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
864 template <class, class, class> friend class __tree;
865 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
866 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
867 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
868 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
869 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
870 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
873 template <class _Tp, class _NodePtr, class _DiffType>
874 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
876 typedef __tree_node_types<_NodePtr> _NodeTypes;
877 typedef typename _NodeTypes::__node_pointer __node_pointer;
878 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
879 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
880 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
881 typedef pointer_traits<__node_pointer> __pointer_traits;
883 __iter_pointer __ptr_;
886 typedef bidirectional_iterator_tag iterator_category;
887 typedef _Tp value_type;
888 typedef _DiffType difference_type;
889 typedef const value_type& reference;
890 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
892 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
893 #if _LIBCPP_STD_VER > 11
899 typedef __tree_iterator<value_type, __node_pointer, difference_type>
900 __non_const_iterator;
902 _LIBCPP_INLINE_VISIBILITY
903 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
904 : __ptr_(__p.__ptr_) {}
906 _LIBCPP_INLINE_VISIBILITY reference operator*() const
907 {return __get_np()->__value_;}
908 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
909 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
911 _LIBCPP_INLINE_VISIBILITY
912 __tree_const_iterator& operator++() {
913 __ptr_ = static_cast<__iter_pointer>(
914 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
918 _LIBCPP_INLINE_VISIBILITY
919 __tree_const_iterator operator++(int)
920 {__tree_const_iterator __t(*this); ++(*this); return __t;}
922 _LIBCPP_INLINE_VISIBILITY
923 __tree_const_iterator& operator--() {
924 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
925 static_cast<__end_node_pointer>(__ptr_)));
929 _LIBCPP_INLINE_VISIBILITY
930 __tree_const_iterator operator--(int)
931 {__tree_const_iterator __t(*this); --(*this); return __t;}
933 friend _LIBCPP_INLINE_VISIBILITY
934 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
935 {return __x.__ptr_ == __y.__ptr_;}
936 friend _LIBCPP_INLINE_VISIBILITY
937 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
938 {return !(__x == __y);}
941 _LIBCPP_INLINE_VISIBILITY
942 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
944 _LIBCPP_INLINE_VISIBILITY
945 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
947 _LIBCPP_INLINE_VISIBILITY
948 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
950 template <class, class, class> friend class __tree;
951 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
952 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
953 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
954 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
955 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
959 #ifndef _LIBCPP_CXX03_LANG
960 template <class _Tp, class _Compare, class _Allocator>
961 struct __diagnose_tree_helper {
962 static constexpr bool __trigger_diagnostics()
963 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
964 "the specified comparator type does not provide a const call operator")
968 template <class _Key, class _Value, class _KeyComp, class _Alloc>
969 struct __diagnose_tree_helper<
970 __value_type<_Key, _Value>,
971 __map_value_compare<_Key, __value_type<_Key, _Value>, _KeyComp>,
973 > : __diagnose_tree_helper<_Key, _KeyComp, _Alloc>
976 #endif // !_LIBCPP_CXX03_LANG
978 template <class _Tp, class _Compare, class _Allocator>
982 typedef _Tp value_type;
983 typedef _Compare value_compare;
984 typedef _Allocator allocator_type;
987 typedef allocator_traits<allocator_type> __alloc_traits;
988 typedef typename __make_tree_node_types<value_type,
989 typename __alloc_traits::void_pointer>::type
991 typedef typename _NodeTypes::key_type key_type;
993 typedef typename _NodeTypes::__node_value_type __node_value_type;
994 typedef typename _NodeTypes::__container_value_type __container_value_type;
996 typedef typename __alloc_traits::pointer pointer;
997 typedef typename __alloc_traits::const_pointer const_pointer;
998 typedef typename __alloc_traits::size_type size_type;
999 typedef typename __alloc_traits::difference_type difference_type;
1002 typedef typename _NodeTypes::__void_pointer __void_pointer;
1004 typedef typename _NodeTypes::__node_type __node;
1005 typedef typename _NodeTypes::__node_pointer __node_pointer;
1007 typedef typename _NodeTypes::__node_base_type __node_base;
1008 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
1010 typedef typename _NodeTypes::__end_node_type __end_node_t;
1011 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
1013 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
1014 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
1016 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
1017 typedef allocator_traits<__node_allocator> __node_traits;
1020 // check for sane allocator pointer rebinding semantics. Rebinding the
1021 // allocator for a new pointer type should be exactly the same as rebinding
1022 // the pointer using 'pointer_traits'.
1023 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1024 "Allocator does not rebind pointers in a sane manner.");
1025 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1026 __node_base_allocator;
1027 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1028 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1029 "Allocator does not rebind pointers in a sane manner.");
1032 __iter_pointer __begin_node_;
1033 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1034 __compressed_pair<size_type, value_compare> __pair3_;
1037 _LIBCPP_INLINE_VISIBILITY
1038 __iter_pointer __end_node() _NOEXCEPT
1040 return static_cast<__iter_pointer>(
1041 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1044 _LIBCPP_INLINE_VISIBILITY
1045 __iter_pointer __end_node() const _NOEXCEPT
1047 return static_cast<__iter_pointer>(
1048 pointer_traits<__end_node_ptr>::pointer_to(
1049 const_cast<__end_node_t&>(__pair1_.first())
1053 _LIBCPP_INLINE_VISIBILITY
1054 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
1056 _LIBCPP_INLINE_VISIBILITY
1057 const __node_allocator& __node_alloc() const _NOEXCEPT
1058 {return __pair1_.second();}
1059 _LIBCPP_INLINE_VISIBILITY
1060 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
1061 _LIBCPP_INLINE_VISIBILITY
1062 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
1064 _LIBCPP_INLINE_VISIBILITY
1065 allocator_type __alloc() const _NOEXCEPT
1066 {return allocator_type(__node_alloc());}
1068 _LIBCPP_INLINE_VISIBILITY
1069 size_type& size() _NOEXCEPT {return __pair3_.first();}
1071 _LIBCPP_INLINE_VISIBILITY
1072 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
1073 _LIBCPP_INLINE_VISIBILITY
1074 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
1075 _LIBCPP_INLINE_VISIBILITY
1076 const value_compare& value_comp() const _NOEXCEPT
1077 {return __pair3_.second();}
1080 _LIBCPP_INLINE_VISIBILITY
1081 __node_pointer __root() const _NOEXCEPT
1082 {return static_cast<__node_pointer>(__end_node()->__left_);}
1084 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1085 return _VSTD::addressof(__end_node()->__left_);
1088 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
1089 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
1091 explicit __tree(const value_compare& __comp)
1093 is_nothrow_default_constructible<__node_allocator>::value &&
1094 is_nothrow_copy_constructible<value_compare>::value);
1095 explicit __tree(const allocator_type& __a);
1096 __tree(const value_compare& __comp, const allocator_type& __a);
1097 __tree(const __tree& __t);
1098 __tree& operator=(const __tree& __t);
1099 template <class _InputIterator>
1100 void __assign_unique(_InputIterator __first, _InputIterator __last);
1101 template <class _InputIterator>
1102 void __assign_multi(_InputIterator __first, _InputIterator __last);
1103 #ifndef _LIBCPP_CXX03_LANG
1104 __tree(__tree&& __t)
1106 is_nothrow_move_constructible<__node_allocator>::value &&
1107 is_nothrow_move_constructible<value_compare>::value);
1108 __tree(__tree&& __t, const allocator_type& __a);
1109 __tree& operator=(__tree&& __t)
1111 __node_traits::propagate_on_container_move_assignment::value &&
1112 is_nothrow_move_assignable<value_compare>::value &&
1113 is_nothrow_move_assignable<__node_allocator>::value);
1114 #endif // _LIBCPP_CXX03_LANG
1118 _LIBCPP_INLINE_VISIBILITY
1119 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
1120 _LIBCPP_INLINE_VISIBILITY
1121 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
1122 _LIBCPP_INLINE_VISIBILITY
1123 iterator end() _NOEXCEPT {return iterator(__end_node());}
1124 _LIBCPP_INLINE_VISIBILITY
1125 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
1127 _LIBCPP_INLINE_VISIBILITY
1128 size_type max_size() const _NOEXCEPT
1129 {return std::min<size_type>(
1130 __node_traits::max_size(__node_alloc()),
1131 numeric_limits<difference_type >::max());}
1133 void clear() _NOEXCEPT;
1135 void swap(__tree& __t)
1136 #if _LIBCPP_STD_VER <= 11
1138 __is_nothrow_swappable<value_compare>::value
1139 && (!__node_traits::propagate_on_container_swap::value ||
1140 __is_nothrow_swappable<__node_allocator>::value)
1143 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1146 #ifndef _LIBCPP_CXX03_LANG
1147 template <class _Key, class ..._Args>
1148 pair<iterator, bool>
1149 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1150 template <class _Key, class ..._Args>
1152 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
1154 template <class... _Args>
1155 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1157 template <class... _Args>
1158 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
1160 template <class... _Args>
1161 iterator __emplace_multi(_Args&&... __args);
1163 template <class... _Args>
1164 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1166 template <class _Pp>
1167 _LIBCPP_INLINE_VISIBILITY
1168 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1169 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1170 __can_extract_key<_Pp, key_type>());
1173 template <class _First, class _Second>
1174 _LIBCPP_INLINE_VISIBILITY
1176 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1177 pair<iterator, bool>
1178 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1179 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1180 _VSTD::forward<_Second>(__s));
1183 template <class... _Args>
1184 _LIBCPP_INLINE_VISIBILITY
1185 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1186 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1189 template <class _Pp>
1190 _LIBCPP_INLINE_VISIBILITY
1191 pair<iterator, bool>
1192 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1193 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1196 template <class _Pp>
1197 _LIBCPP_INLINE_VISIBILITY
1198 pair<iterator, bool>
1199 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1200 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1203 template <class _Pp>
1204 _LIBCPP_INLINE_VISIBILITY
1205 pair<iterator, bool>
1206 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1207 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1210 template <class _Pp>
1211 _LIBCPP_INLINE_VISIBILITY
1212 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1213 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1214 __can_extract_key<_Pp, key_type>());
1217 template <class _First, class _Second>
1218 _LIBCPP_INLINE_VISIBILITY
1220 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1222 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1223 return __emplace_hint_unique_key_args(__p, __f,
1224 _VSTD::forward<_First>(__f),
1225 _VSTD::forward<_Second>(__s));
1228 template <class... _Args>
1229 _LIBCPP_INLINE_VISIBILITY
1230 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1231 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1234 template <class _Pp>
1235 _LIBCPP_INLINE_VISIBILITY
1237 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1238 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1241 template <class _Pp>
1242 _LIBCPP_INLINE_VISIBILITY
1244 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1245 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1248 template <class _Pp>
1249 _LIBCPP_INLINE_VISIBILITY
1251 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1252 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1256 template <class _Key, class _Args>
1257 _LIBCPP_INLINE_VISIBILITY
1258 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1259 template <class _Key, class _Args>
1260 _LIBCPP_INLINE_VISIBILITY
1261 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
1264 _LIBCPP_INLINE_VISIBILITY
1265 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1266 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1269 _LIBCPP_INLINE_VISIBILITY
1270 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1271 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1274 #ifdef _LIBCPP_CXX03_LANG
1275 _LIBCPP_INLINE_VISIBILITY
1276 iterator __insert_multi(const __container_value_type& __v);
1277 _LIBCPP_INLINE_VISIBILITY
1278 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1280 _LIBCPP_INLINE_VISIBILITY
1281 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1282 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1285 _LIBCPP_INLINE_VISIBILITY
1286 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1287 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1290 template <class _Vp, class = typename enable_if<
1291 !is_same<typename __unconstref<_Vp>::type,
1292 __container_value_type
1295 _LIBCPP_INLINE_VISIBILITY
1296 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1297 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1300 template <class _Vp, class = typename enable_if<
1301 !is_same<typename __unconstref<_Vp>::type,
1302 __container_value_type
1305 _LIBCPP_INLINE_VISIBILITY
1306 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1307 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1310 _LIBCPP_INLINE_VISIBILITY
1311 iterator __insert_multi(__container_value_type&& __v) {
1312 return __emplace_multi(_VSTD::move(__v));
1315 _LIBCPP_INLINE_VISIBILITY
1316 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1317 return __emplace_hint_multi(__p, _VSTD::move(__v));
1320 template <class _Vp>
1321 _LIBCPP_INLINE_VISIBILITY
1322 iterator __insert_multi(_Vp&& __v) {
1323 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1326 template <class _Vp>
1327 _LIBCPP_INLINE_VISIBILITY
1328 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1329 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1332 #endif // !_LIBCPP_CXX03_LANG
1334 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1335 iterator __node_insert_unique(const_iterator __p,
1336 __node_pointer __nd);
1338 iterator __node_insert_multi(__node_pointer __nd);
1339 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1341 iterator erase(const_iterator __p);
1342 iterator erase(const_iterator __f, const_iterator __l);
1343 template <class _Key>
1344 size_type __erase_unique(const _Key& __k);
1345 template <class _Key>
1346 size_type __erase_multi(const _Key& __k);
1348 void __insert_node_at(__parent_pointer __parent,
1349 __node_base_pointer& __child,
1350 __node_base_pointer __new_node);
1352 template <class _Key>
1353 iterator find(const _Key& __v);
1354 template <class _Key>
1355 const_iterator find(const _Key& __v) const;
1357 template <class _Key>
1358 size_type __count_unique(const _Key& __k) const;
1359 template <class _Key>
1360 size_type __count_multi(const _Key& __k) const;
1362 template <class _Key>
1363 _LIBCPP_INLINE_VISIBILITY
1364 iterator lower_bound(const _Key& __v)
1365 {return __lower_bound(__v, __root(), __end_node());}
1366 template <class _Key>
1367 iterator __lower_bound(const _Key& __v,
1368 __node_pointer __root,
1369 __iter_pointer __result);
1370 template <class _Key>
1371 _LIBCPP_INLINE_VISIBILITY
1372 const_iterator lower_bound(const _Key& __v) const
1373 {return __lower_bound(__v, __root(), __end_node());}
1374 template <class _Key>
1375 const_iterator __lower_bound(const _Key& __v,
1376 __node_pointer __root,
1377 __iter_pointer __result) const;
1378 template <class _Key>
1379 _LIBCPP_INLINE_VISIBILITY
1380 iterator upper_bound(const _Key& __v)
1381 {return __upper_bound(__v, __root(), __end_node());}
1382 template <class _Key>
1383 iterator __upper_bound(const _Key& __v,
1384 __node_pointer __root,
1385 __iter_pointer __result);
1386 template <class _Key>
1387 _LIBCPP_INLINE_VISIBILITY
1388 const_iterator upper_bound(const _Key& __v) const
1389 {return __upper_bound(__v, __root(), __end_node());}
1390 template <class _Key>
1391 const_iterator __upper_bound(const _Key& __v,
1392 __node_pointer __root,
1393 __iter_pointer __result) const;
1394 template <class _Key>
1395 pair<iterator, iterator>
1396 __equal_range_unique(const _Key& __k);
1397 template <class _Key>
1398 pair<const_iterator, const_iterator>
1399 __equal_range_unique(const _Key& __k) const;
1401 template <class _Key>
1402 pair<iterator, iterator>
1403 __equal_range_multi(const _Key& __k);
1404 template <class _Key>
1405 pair<const_iterator, const_iterator>
1406 __equal_range_multi(const _Key& __k) const;
1408 typedef __tree_node_destructor<__node_allocator> _Dp;
1409 typedef unique_ptr<__node, _Dp> __node_holder;
1411 __node_holder remove(const_iterator __p) _NOEXCEPT;
1413 __node_base_pointer&
1414 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1415 __node_base_pointer&
1416 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1417 __node_base_pointer&
1418 __find_leaf(const_iterator __hint,
1419 __parent_pointer& __parent, const key_type& __v);
1420 // FIXME: Make this function const qualified. Unfortunetly doing so
1421 // breaks existing code which uses non-const callable comparators.
1422 template <class _Key>
1423 __node_base_pointer&
1424 __find_equal(__parent_pointer& __parent, const _Key& __v);
1425 template <class _Key>
1426 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1427 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1428 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1430 template <class _Key>
1431 __node_base_pointer&
1432 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1433 __node_base_pointer& __dummy,
1436 #ifndef _LIBCPP_CXX03_LANG
1437 template <class ..._Args>
1438 __node_holder __construct_node(_Args&& ...__args);
1440 __node_holder __construct_node(const __container_value_type& __v);
1443 void destroy(__node_pointer __nd) _NOEXCEPT;
1445 _LIBCPP_INLINE_VISIBILITY
1446 void __copy_assign_alloc(const __tree& __t)
1447 {__copy_assign_alloc(__t, integral_constant<bool,
1448 __node_traits::propagate_on_container_copy_assignment::value>());}
1450 _LIBCPP_INLINE_VISIBILITY
1451 void __copy_assign_alloc(const __tree& __t, true_type)
1453 if (__node_alloc() != __t.__node_alloc())
1455 __node_alloc() = __t.__node_alloc();
1457 _LIBCPP_INLINE_VISIBILITY
1458 void __copy_assign_alloc(const __tree&, false_type) {}
1460 void __move_assign(__tree& __t, false_type);
1461 void __move_assign(__tree& __t, true_type)
1462 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1463 is_nothrow_move_assignable<__node_allocator>::value);
1465 _LIBCPP_INLINE_VISIBILITY
1466 void __move_assign_alloc(__tree& __t)
1468 !__node_traits::propagate_on_container_move_assignment::value ||
1469 is_nothrow_move_assignable<__node_allocator>::value)
1470 {__move_assign_alloc(__t, integral_constant<bool,
1471 __node_traits::propagate_on_container_move_assignment::value>());}
1473 _LIBCPP_INLINE_VISIBILITY
1474 void __move_assign_alloc(__tree& __t, true_type)
1475 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
1476 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
1477 _LIBCPP_INLINE_VISIBILITY
1478 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
1480 __node_pointer __detach();
1481 static __node_pointer __detach(__node_pointer);
1483 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1484 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
1487 template <class _Tp, class _Compare, class _Allocator>
1488 __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
1490 is_nothrow_default_constructible<__node_allocator>::value &&
1491 is_nothrow_copy_constructible<value_compare>::value)
1492 : __pair3_(0, __comp)
1494 __begin_node() = __end_node();
1497 template <class _Tp, class _Compare, class _Allocator>
1498 __tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1499 : __begin_node_(__iter_pointer()),
1500 __pair1_(__second_tag(), __node_allocator(__a)),
1503 __begin_node() = __end_node();
1506 template <class _Tp, class _Compare, class _Allocator>
1507 __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1508 const allocator_type& __a)
1509 : __begin_node_(__iter_pointer()),
1510 __pair1_(__second_tag(), __node_allocator(__a)),
1513 __begin_node() = __end_node();
1516 // Precondition: size() != 0
1517 template <class _Tp, class _Compare, class _Allocator>
1518 typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1519 __tree<_Tp, _Compare, _Allocator>::__detach()
1521 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
1522 __begin_node() = __end_node();
1523 __end_node()->__left_->__parent_ = nullptr;
1524 __end_node()->__left_ = nullptr;
1526 // __cache->__left_ == nullptr
1527 if (__cache->__right_ != nullptr)
1528 __cache = static_cast<__node_pointer>(__cache->__right_);
1529 // __cache->__left_ == nullptr
1530 // __cache->__right_ == nullptr
1534 // Precondition: __cache != nullptr
1535 // __cache->left_ == nullptr
1536 // __cache->right_ == nullptr
1537 // This is no longer a red-black tree
1538 template <class _Tp, class _Compare, class _Allocator>
1539 typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1540 __tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1542 if (__cache->__parent_ == nullptr)
1544 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
1546 __cache->__parent_->__left_ = nullptr;
1547 __cache = static_cast<__node_pointer>(__cache->__parent_);
1548 if (__cache->__right_ == nullptr)
1550 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1552 // __cache is right child
1553 __cache->__parent_unsafe()->__right_ = nullptr;
1554 __cache = static_cast<__node_pointer>(__cache->__parent_);
1555 if (__cache->__left_ == nullptr)
1557 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1560 template <class _Tp, class _Compare, class _Allocator>
1561 __tree<_Tp, _Compare, _Allocator>&
1562 __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1566 value_comp() = __t.value_comp();
1567 __copy_assign_alloc(__t);
1568 __assign_multi(__t.begin(), __t.end());
1573 template <class _Tp, class _Compare, class _Allocator>
1574 template <class _InputIterator>
1576 __tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1578 typedef iterator_traits<_InputIterator> _ITraits;
1579 typedef typename _ITraits::value_type _ItValueType;
1580 static_assert((is_same<_ItValueType, __container_value_type>::value),
1581 "__assign_unique may only be called with the containers value type");
1585 __node_pointer __cache = __detach();
1586 #ifndef _LIBCPP_NO_EXCEPTIONS
1589 #endif // _LIBCPP_NO_EXCEPTIONS
1590 for (; __cache != nullptr && __first != __last; ++__first)
1592 __cache->__value_ = *__first;
1593 __node_pointer __next = __detach(__cache);
1594 __node_insert_unique(__cache);
1597 #ifndef _LIBCPP_NO_EXCEPTIONS
1601 while (__cache->__parent_ != nullptr)
1602 __cache = static_cast<__node_pointer>(__cache->__parent_);
1606 #endif // _LIBCPP_NO_EXCEPTIONS
1607 if (__cache != nullptr)
1609 while (__cache->__parent_ != nullptr)
1610 __cache = static_cast<__node_pointer>(__cache->__parent_);
1614 for (; __first != __last; ++__first)
1615 __insert_unique(*__first);
1618 template <class _Tp, class _Compare, class _Allocator>
1619 template <class _InputIterator>
1621 __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1623 typedef iterator_traits<_InputIterator> _ITraits;
1624 typedef typename _ITraits::value_type _ItValueType;
1625 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1626 is_same<_ItValueType, __node_value_type>::value),
1627 "__assign_multi may only be called with the containers value type"
1628 " or the nodes value type");
1631 __node_pointer __cache = __detach();
1632 #ifndef _LIBCPP_NO_EXCEPTIONS
1635 #endif // _LIBCPP_NO_EXCEPTIONS
1636 for (; __cache != nullptr && __first != __last; ++__first)
1638 __cache->__value_ = *__first;
1639 __node_pointer __next = __detach(__cache);
1640 __node_insert_multi(__cache);
1643 #ifndef _LIBCPP_NO_EXCEPTIONS
1647 while (__cache->__parent_ != nullptr)
1648 __cache = static_cast<__node_pointer>(__cache->__parent_);
1652 #endif // _LIBCPP_NO_EXCEPTIONS
1653 if (__cache != nullptr)
1655 while (__cache->__parent_ != nullptr)
1656 __cache = static_cast<__node_pointer>(__cache->__parent_);
1660 for (; __first != __last; ++__first)
1661 __insert_multi(_NodeTypes::__get_value(*__first));
1664 template <class _Tp, class _Compare, class _Allocator>
1665 __tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1666 : __begin_node_(__iter_pointer()),
1667 __pair1_(__second_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1668 __pair3_(0, __t.value_comp())
1670 __begin_node() = __end_node();
1673 #ifndef _LIBCPP_CXX03_LANG
1675 template <class _Tp, class _Compare, class _Allocator>
1676 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
1678 is_nothrow_move_constructible<__node_allocator>::value &&
1679 is_nothrow_move_constructible<value_compare>::value)
1680 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1681 __pair1_(_VSTD::move(__t.__pair1_)),
1682 __pair3_(_VSTD::move(__t.__pair3_))
1685 __begin_node() = __end_node();
1688 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1689 __t.__begin_node() = __t.__end_node();
1690 __t.__end_node()->__left_ = nullptr;
1695 template <class _Tp, class _Compare, class _Allocator>
1696 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1697 : __pair1_(__second_tag(), __node_allocator(__a)),
1698 __pair3_(0, _VSTD::move(__t.value_comp()))
1700 if (__a == __t.__alloc())
1702 if (__t.size() == 0)
1703 __begin_node() = __end_node();
1706 __begin_node() = __t.__begin_node();
1707 __end_node()->__left_ = __t.__end_node()->__left_;
1708 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1709 size() = __t.size();
1710 __t.__begin_node() = __t.__end_node();
1711 __t.__end_node()->__left_ = nullptr;
1717 __begin_node() = __end_node();
1721 template <class _Tp, class _Compare, class _Allocator>
1723 __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1724 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1725 is_nothrow_move_assignable<__node_allocator>::value)
1727 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1728 __begin_node_ = __t.__begin_node_;
1729 __pair1_.first() = __t.__pair1_.first();
1730 __move_assign_alloc(__t);
1731 __pair3_ = _VSTD::move(__t.__pair3_);
1733 __begin_node() = __end_node();
1736 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1737 __t.__begin_node() = __t.__end_node();
1738 __t.__end_node()->__left_ = nullptr;
1743 template <class _Tp, class _Compare, class _Allocator>
1745 __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1747 if (__node_alloc() == __t.__node_alloc())
1748 __move_assign(__t, true_type());
1751 value_comp() = _VSTD::move(__t.value_comp());
1752 const_iterator __e = end();
1755 __node_pointer __cache = __detach();
1756 #ifndef _LIBCPP_NO_EXCEPTIONS
1759 #endif // _LIBCPP_NO_EXCEPTIONS
1760 while (__cache != nullptr && __t.size() != 0)
1762 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
1763 __node_pointer __next = __detach(__cache);
1764 __node_insert_multi(__cache);
1767 #ifndef _LIBCPP_NO_EXCEPTIONS
1771 while (__cache->__parent_ != nullptr)
1772 __cache = static_cast<__node_pointer>(__cache->__parent_);
1776 #endif // _LIBCPP_NO_EXCEPTIONS
1777 if (__cache != nullptr)
1779 while (__cache->__parent_ != nullptr)
1780 __cache = static_cast<__node_pointer>(__cache->__parent_);
1784 while (__t.size() != 0)
1785 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
1789 template <class _Tp, class _Compare, class _Allocator>
1790 __tree<_Tp, _Compare, _Allocator>&
1791 __tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
1793 __node_traits::propagate_on_container_move_assignment::value &&
1794 is_nothrow_move_assignable<value_compare>::value &&
1795 is_nothrow_move_assignable<__node_allocator>::value)
1798 __move_assign(__t, integral_constant<bool,
1799 __node_traits::propagate_on_container_move_assignment::value>());
1803 #endif // _LIBCPP_CXX03_LANG
1805 template <class _Tp, class _Compare, class _Allocator>
1806 __tree<_Tp, _Compare, _Allocator>::~__tree()
1808 static_assert((is_copy_constructible<value_compare>::value),
1809 "Comparator must be copy-constructible.");
1810 #ifndef _LIBCPP_CXX03_LANG
1811 static_assert((__diagnose_tree_helper<_Tp, _Compare, _Allocator>::
1812 __trigger_diagnostics()), "");
1817 template <class _Tp, class _Compare, class _Allocator>
1819 __tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
1821 if (__nd != nullptr)
1823 destroy(static_cast<__node_pointer>(__nd->__left_));
1824 destroy(static_cast<__node_pointer>(__nd->__right_));
1825 __node_allocator& __na = __node_alloc();
1826 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
1827 __node_traits::deallocate(__na, __nd, 1);
1831 template <class _Tp, class _Compare, class _Allocator>
1833 __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1834 #if _LIBCPP_STD_VER <= 11
1836 __is_nothrow_swappable<value_compare>::value
1837 && (!__node_traits::propagate_on_container_swap::value ||
1838 __is_nothrow_swappable<__node_allocator>::value)
1841 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1845 swap(__begin_node_, __t.__begin_node_);
1846 swap(__pair1_.first(), __t.__pair1_.first());
1847 __swap_allocator(__node_alloc(), __t.__node_alloc());
1848 __pair3_.swap(__t.__pair3_);
1850 __begin_node() = __end_node();
1852 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1853 if (__t.size() == 0)
1854 __t.__begin_node() = __t.__end_node();
1856 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
1859 template <class _Tp, class _Compare, class _Allocator>
1861 __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
1865 __begin_node() = __end_node();
1866 __end_node()->__left_ = nullptr;
1869 // Find lower_bound place to insert
1870 // Set __parent to parent of null leaf
1871 // Return reference to null leaf
1872 template <class _Tp, class _Compare, class _Allocator>
1873 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1874 __tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
1875 const key_type& __v)
1877 __node_pointer __nd = __root();
1878 if (__nd != nullptr)
1882 if (value_comp()(__nd->__value_, __v))
1884 if (__nd->__right_ != nullptr)
1885 __nd = static_cast<__node_pointer>(__nd->__right_);
1888 __parent = static_cast<__parent_pointer>(__nd);
1889 return __nd->__right_;
1894 if (__nd->__left_ != nullptr)
1895 __nd = static_cast<__node_pointer>(__nd->__left_);
1898 __parent = static_cast<__parent_pointer>(__nd);
1899 return __parent->__left_;
1904 __parent = static_cast<__parent_pointer>(__end_node());
1905 return __parent->__left_;
1908 // Find upper_bound place to insert
1909 // Set __parent to parent of null leaf
1910 // Return reference to null leaf
1911 template <class _Tp, class _Compare, class _Allocator>
1912 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1913 __tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
1914 const key_type& __v)
1916 __node_pointer __nd = __root();
1917 if (__nd != nullptr)
1921 if (value_comp()(__v, __nd->__value_))
1923 if (__nd->__left_ != nullptr)
1924 __nd = static_cast<__node_pointer>(__nd->__left_);
1927 __parent = static_cast<__parent_pointer>(__nd);
1928 return __parent->__left_;
1933 if (__nd->__right_ != nullptr)
1934 __nd = static_cast<__node_pointer>(__nd->__right_);
1937 __parent = static_cast<__parent_pointer>(__nd);
1938 return __nd->__right_;
1943 __parent = static_cast<__parent_pointer>(__end_node());
1944 return __parent->__left_;
1947 // Find leaf place to insert closest to __hint
1948 // First check prior to __hint.
1949 // Next check after __hint.
1950 // Next do O(log N) search.
1951 // Set __parent to parent of null leaf
1952 // Return reference to null leaf
1953 template <class _Tp, class _Compare, class _Allocator>
1954 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1955 __tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
1956 __parent_pointer& __parent,
1957 const key_type& __v)
1959 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1962 const_iterator __prior = __hint;
1963 if (__prior == begin() || !value_comp()(__v, *--__prior))
1965 // *prev(__hint) <= __v <= *__hint
1966 if (__hint.__ptr_->__left_ == nullptr)
1968 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
1969 return __parent->__left_;
1973 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1974 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
1977 // __v < *prev(__hint)
1978 return __find_leaf_high(__parent, __v);
1980 // else __v > *__hint
1981 return __find_leaf_low(__parent, __v);
1984 // Find place to insert if __v doesn't exist
1985 // Set __parent to parent of null leaf
1986 // Return reference to null leaf
1987 // If __v exists, set parent to node of __v and return reference to node of __v
1988 template <class _Tp, class _Compare, class _Allocator>
1989 template <class _Key>
1990 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1991 __tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
1994 __node_pointer __nd = __root();
1995 __node_base_pointer* __nd_ptr = __root_ptr();
1996 if (__nd != nullptr)
2000 if (value_comp()(__v, __nd->__value_))
2002 if (__nd->__left_ != nullptr) {
2003 __nd_ptr = _VSTD::addressof(__nd->__left_);
2004 __nd = static_cast<__node_pointer>(__nd->__left_);
2006 __parent = static_cast<__parent_pointer>(__nd);
2007 return __parent->__left_;
2010 else if (value_comp()(__nd->__value_, __v))
2012 if (__nd->__right_ != nullptr) {
2013 __nd_ptr = _VSTD::addressof(__nd->__right_);
2014 __nd = static_cast<__node_pointer>(__nd->__right_);
2016 __parent = static_cast<__parent_pointer>(__nd);
2017 return __nd->__right_;
2022 __parent = static_cast<__parent_pointer>(__nd);
2027 __parent = static_cast<__parent_pointer>(__end_node());
2028 return __parent->__left_;
2031 // Find place to insert if __v doesn't exist
2032 // First check prior to __hint.
2033 // Next check after __hint.
2034 // Next do O(log N) search.
2035 // Set __parent to parent of null leaf
2036 // Return reference to null leaf
2037 // If __v exists, set parent to node of __v and return reference to node of __v
2038 template <class _Tp, class _Compare, class _Allocator>
2039 template <class _Key>
2040 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
2041 __tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
2042 __parent_pointer& __parent,
2043 __node_base_pointer& __dummy,
2046 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2049 const_iterator __prior = __hint;
2050 if (__prior == begin() || value_comp()(*--__prior, __v))
2052 // *prev(__hint) < __v < *__hint
2053 if (__hint.__ptr_->__left_ == nullptr)
2055 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2056 return __parent->__left_;
2060 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2061 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
2064 // __v <= *prev(__hint)
2065 return __find_equal(__parent, __v);
2067 else if (value_comp()(*__hint, __v)) // check after
2070 const_iterator __next = _VSTD::next(__hint);
2071 if (__next == end() || value_comp()(__v, *__next))
2073 // *__hint < __v < *_VSTD::next(__hint)
2074 if (__hint.__get_np()->__right_ == nullptr)
2076 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2077 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
2081 __parent = static_cast<__parent_pointer>(__next.__ptr_);
2082 return __parent->__left_;
2085 // *next(__hint) <= __v
2086 return __find_equal(__parent, __v);
2088 // else __v == *__hint
2089 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2090 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2094 template <class _Tp, class _Compare, class _Allocator>
2096 __tree<_Tp, _Compare, _Allocator>::__insert_node_at(__parent_pointer __parent,
2097 __node_base_pointer& __child,
2098 __node_base_pointer __new_node)
2100 __new_node->__left_ = nullptr;
2101 __new_node->__right_ = nullptr;
2102 __new_node->__parent_ = __parent;
2103 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
2104 __child = __new_node;
2105 if (__begin_node()->__left_ != nullptr)
2106 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
2107 __tree_balance_after_insert(__end_node()->__left_, __child);
2111 #ifndef _LIBCPP_CXX03_LANG
2112 template <class _Tp, class _Compare, class _Allocator>
2113 template <class _Key, class... _Args>
2114 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2115 __tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2117 template <class _Tp, class _Compare, class _Allocator>
2118 template <class _Key, class _Args>
2119 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2120 __tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2123 __parent_pointer __parent;
2124 __node_base_pointer& __child = __find_equal(__parent, __k);
2125 __node_pointer __r = static_cast<__node_pointer>(__child);
2126 bool __inserted = false;
2127 if (__child == nullptr)
2129 #ifndef _LIBCPP_CXX03_LANG
2130 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2132 __node_holder __h = __construct_node(__args);
2134 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2135 __r = __h.release();
2138 return pair<iterator, bool>(iterator(__r), __inserted);
2142 #ifndef _LIBCPP_CXX03_LANG
2143 template <class _Tp, class _Compare, class _Allocator>
2144 template <class _Key, class... _Args>
2145 typename __tree<_Tp, _Compare, _Allocator>::iterator
2146 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2147 const_iterator __p, _Key const& __k, _Args&&... __args)
2149 template <class _Tp, class _Compare, class _Allocator>
2150 template <class _Key, class _Args>
2151 typename __tree<_Tp, _Compare, _Allocator>::iterator
2152 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2153 const_iterator __p, _Key const& __k, _Args& __args)
2156 __parent_pointer __parent;
2157 __node_base_pointer __dummy;
2158 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
2159 __node_pointer __r = static_cast<__node_pointer>(__child);
2160 if (__child == nullptr)
2162 #ifndef _LIBCPP_CXX03_LANG
2163 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2165 __node_holder __h = __construct_node(__args);
2167 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2168 __r = __h.release();
2170 return iterator(__r);
2174 #ifndef _LIBCPP_CXX03_LANG
2176 template <class _Tp, class _Compare, class _Allocator>
2177 template <class ..._Args>
2178 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2179 __tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2181 static_assert(!__is_tree_value_type<_Args...>::value,
2182 "Cannot construct from __value_type");
2183 __node_allocator& __na = __node_alloc();
2184 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2185 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
2186 __h.get_deleter().__value_constructed = true;
2191 template <class _Tp, class _Compare, class _Allocator>
2192 template <class... _Args>
2193 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2194 __tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
2196 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2197 __parent_pointer __parent;
2198 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2199 __node_pointer __r = static_cast<__node_pointer>(__child);
2200 bool __inserted = false;
2201 if (__child == nullptr)
2203 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2204 __r = __h.release();
2207 return pair<iterator, bool>(iterator(__r), __inserted);
2210 template <class _Tp, class _Compare, class _Allocator>
2211 template <class... _Args>
2212 typename __tree<_Tp, _Compare, _Allocator>::iterator
2213 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
2215 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2216 __parent_pointer __parent;
2217 __node_base_pointer __dummy;
2218 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
2219 __node_pointer __r = static_cast<__node_pointer>(__child);
2220 if (__child == nullptr)
2222 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2223 __r = __h.release();
2225 return iterator(__r);
2228 template <class _Tp, class _Compare, class _Allocator>
2229 template <class... _Args>
2230 typename __tree<_Tp, _Compare, _Allocator>::iterator
2231 __tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2233 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2234 __parent_pointer __parent;
2235 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
2236 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2237 return iterator(static_cast<__node_pointer>(__h.release()));
2240 template <class _Tp, class _Compare, class _Allocator>
2241 template <class... _Args>
2242 typename __tree<_Tp, _Compare, _Allocator>::iterator
2243 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2246 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2247 __parent_pointer __parent;
2248 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
2249 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2250 return iterator(static_cast<__node_pointer>(__h.release()));
2254 #else // _LIBCPP_CXX03_LANG
2256 template <class _Tp, class _Compare, class _Allocator>
2257 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2258 __tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
2260 __node_allocator& __na = __node_alloc();
2261 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
2262 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
2263 __h.get_deleter().__value_constructed = true;
2264 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
2267 #endif // _LIBCPP_CXX03_LANG
2269 #ifdef _LIBCPP_CXX03_LANG
2270 template <class _Tp, class _Compare, class _Allocator>
2271 typename __tree<_Tp, _Compare, _Allocator>::iterator
2272 __tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
2274 __parent_pointer __parent;
2275 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
2276 __node_holder __h = __construct_node(__v);
2277 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2278 return iterator(__h.release());
2281 template <class _Tp, class _Compare, class _Allocator>
2282 typename __tree<_Tp, _Compare, _Allocator>::iterator
2283 __tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
2285 __parent_pointer __parent;
2286 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
2287 __node_holder __h = __construct_node(__v);
2288 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2289 return iterator(__h.release());
2293 template <class _Tp, class _Compare, class _Allocator>
2294 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2295 __tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2297 __parent_pointer __parent;
2298 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2299 __node_pointer __r = static_cast<__node_pointer>(__child);
2300 bool __inserted = false;
2301 if (__child == nullptr)
2303 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2307 return pair<iterator, bool>(iterator(__r), __inserted);
2310 template <class _Tp, class _Compare, class _Allocator>
2311 typename __tree<_Tp, _Compare, _Allocator>::iterator
2312 __tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2313 __node_pointer __nd)
2315 __parent_pointer __parent;
2316 __node_base_pointer __dummy;
2317 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2318 __node_pointer __r = static_cast<__node_pointer>(__child);
2319 if (__child == nullptr)
2321 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2324 return iterator(__r);
2327 template <class _Tp, class _Compare, class _Allocator>
2328 typename __tree<_Tp, _Compare, _Allocator>::iterator
2329 __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2331 __parent_pointer __parent;
2332 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
2333 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2334 return iterator(__nd);
2337 template <class _Tp, class _Compare, class _Allocator>
2338 typename __tree<_Tp, _Compare, _Allocator>::iterator
2339 __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2340 __node_pointer __nd)
2342 __parent_pointer __parent;
2343 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
2344 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
2345 return iterator(__nd);
2348 template <class _Tp, class _Compare, class _Allocator>
2349 typename __tree<_Tp, _Compare, _Allocator>::iterator
2350 __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2352 __node_pointer __np = __p.__get_np();
2353 iterator __r(__p.__ptr_);
2355 if (__begin_node() == __p.__ptr_)
2356 __begin_node() = __r.__ptr_;
2358 __node_allocator& __na = __node_alloc();
2359 __tree_remove(__end_node()->__left_,
2360 static_cast<__node_base_pointer>(__np));
2361 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2362 const_cast<__node_value_type&>(*__p)));
2363 __node_traits::deallocate(__na, __np, 1);
2367 template <class _Tp, class _Compare, class _Allocator>
2368 typename __tree<_Tp, _Compare, _Allocator>::iterator
2369 __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2373 return iterator(__l.__ptr_);
2376 template <class _Tp, class _Compare, class _Allocator>
2377 template <class _Key>
2378 typename __tree<_Tp, _Compare, _Allocator>::size_type
2379 __tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2381 iterator __i = find(__k);
2388 template <class _Tp, class _Compare, class _Allocator>
2389 template <class _Key>
2390 typename __tree<_Tp, _Compare, _Allocator>::size_type
2391 __tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2393 pair<iterator, iterator> __p = __equal_range_multi(__k);
2395 for (; __p.first != __p.second; ++__r)
2396 __p.first = erase(__p.first);
2400 template <class _Tp, class _Compare, class _Allocator>
2401 template <class _Key>
2402 typename __tree<_Tp, _Compare, _Allocator>::iterator
2403 __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2405 iterator __p = __lower_bound(__v, __root(), __end_node());
2406 if (__p != end() && !value_comp()(__v, *__p))
2411 template <class _Tp, class _Compare, class _Allocator>
2412 template <class _Key>
2413 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2414 __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2416 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2417 if (__p != end() && !value_comp()(__v, *__p))
2422 template <class _Tp, class _Compare, class _Allocator>
2423 template <class _Key>
2424 typename __tree<_Tp, _Compare, _Allocator>::size_type
2425 __tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2427 __node_pointer __rt = __root();
2428 while (__rt != nullptr)
2430 if (value_comp()(__k, __rt->__value_))
2432 __rt = static_cast<__node_pointer>(__rt->__left_);
2434 else if (value_comp()(__rt->__value_, __k))
2435 __rt = static_cast<__node_pointer>(__rt->__right_);
2442 template <class _Tp, class _Compare, class _Allocator>
2443 template <class _Key>
2444 typename __tree<_Tp, _Compare, _Allocator>::size_type
2445 __tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2447 __iter_pointer __result = __end_node();
2448 __node_pointer __rt = __root();
2449 while (__rt != nullptr)
2451 if (value_comp()(__k, __rt->__value_))
2453 __result = static_cast<__iter_pointer>(__rt);
2454 __rt = static_cast<__node_pointer>(__rt->__left_);
2456 else if (value_comp()(__rt->__value_, __k))
2457 __rt = static_cast<__node_pointer>(__rt->__right_);
2459 return _VSTD::distance(
2460 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2461 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
2467 template <class _Tp, class _Compare, class _Allocator>
2468 template <class _Key>
2469 typename __tree<_Tp, _Compare, _Allocator>::iterator
2470 __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2471 __node_pointer __root,
2472 __iter_pointer __result)
2474 while (__root != nullptr)
2476 if (!value_comp()(__root->__value_, __v))
2478 __result = static_cast<__iter_pointer>(__root);
2479 __root = static_cast<__node_pointer>(__root->__left_);
2482 __root = static_cast<__node_pointer>(__root->__right_);
2484 return iterator(__result);
2487 template <class _Tp, class _Compare, class _Allocator>
2488 template <class _Key>
2489 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2490 __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2491 __node_pointer __root,
2492 __iter_pointer __result) const
2494 while (__root != nullptr)
2496 if (!value_comp()(__root->__value_, __v))
2498 __result = static_cast<__iter_pointer>(__root);
2499 __root = static_cast<__node_pointer>(__root->__left_);
2502 __root = static_cast<__node_pointer>(__root->__right_);
2504 return const_iterator(__result);
2507 template <class _Tp, class _Compare, class _Allocator>
2508 template <class _Key>
2509 typename __tree<_Tp, _Compare, _Allocator>::iterator
2510 __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2511 __node_pointer __root,
2512 __iter_pointer __result)
2514 while (__root != nullptr)
2516 if (value_comp()(__v, __root->__value_))
2518 __result = static_cast<__iter_pointer>(__root);
2519 __root = static_cast<__node_pointer>(__root->__left_);
2522 __root = static_cast<__node_pointer>(__root->__right_);
2524 return iterator(__result);
2527 template <class _Tp, class _Compare, class _Allocator>
2528 template <class _Key>
2529 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2530 __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2531 __node_pointer __root,
2532 __iter_pointer __result) const
2534 while (__root != nullptr)
2536 if (value_comp()(__v, __root->__value_))
2538 __result = static_cast<__iter_pointer>(__root);
2539 __root = static_cast<__node_pointer>(__root->__left_);
2542 __root = static_cast<__node_pointer>(__root->__right_);
2544 return const_iterator(__result);
2547 template <class _Tp, class _Compare, class _Allocator>
2548 template <class _Key>
2549 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2550 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2551 __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2553 typedef pair<iterator, iterator> _Pp;
2554 __iter_pointer __result = __end_node();
2555 __node_pointer __rt = __root();
2556 while (__rt != nullptr)
2558 if (value_comp()(__k, __rt->__value_))
2560 __result = static_cast<__iter_pointer>(__rt);
2561 __rt = static_cast<__node_pointer>(__rt->__left_);
2563 else if (value_comp()(__rt->__value_, __k))
2564 __rt = static_cast<__node_pointer>(__rt->__right_);
2566 return _Pp(iterator(__rt),
2568 __rt->__right_ != nullptr ?
2569 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
2572 return _Pp(iterator(__result), iterator(__result));
2575 template <class _Tp, class _Compare, class _Allocator>
2576 template <class _Key>
2577 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2578 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2579 __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2581 typedef pair<const_iterator, const_iterator> _Pp;
2582 __iter_pointer __result = __end_node();
2583 __node_pointer __rt = __root();
2584 while (__rt != nullptr)
2586 if (value_comp()(__k, __rt->__value_))
2588 __result = static_cast<__iter_pointer>(__rt);
2589 __rt = static_cast<__node_pointer>(__rt->__left_);
2591 else if (value_comp()(__rt->__value_, __k))
2592 __rt = static_cast<__node_pointer>(__rt->__right_);
2594 return _Pp(const_iterator(__rt),
2596 __rt->__right_ != nullptr ?
2597 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
2600 return _Pp(const_iterator(__result), const_iterator(__result));
2603 template <class _Tp, class _Compare, class _Allocator>
2604 template <class _Key>
2605 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2606 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2607 __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2609 typedef pair<iterator, iterator> _Pp;
2610 __iter_pointer __result = __end_node();
2611 __node_pointer __rt = __root();
2612 while (__rt != nullptr)
2614 if (value_comp()(__k, __rt->__value_))
2616 __result = static_cast<__iter_pointer>(__rt);
2617 __rt = static_cast<__node_pointer>(__rt->__left_);
2619 else if (value_comp()(__rt->__value_, __k))
2620 __rt = static_cast<__node_pointer>(__rt->__right_);
2622 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2623 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2625 return _Pp(iterator(__result), iterator(__result));
2628 template <class _Tp, class _Compare, class _Allocator>
2629 template <class _Key>
2630 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2631 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2632 __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2634 typedef pair<const_iterator, const_iterator> _Pp;
2635 __iter_pointer __result = __end_node();
2636 __node_pointer __rt = __root();
2637 while (__rt != nullptr)
2639 if (value_comp()(__k, __rt->__value_))
2641 __result = static_cast<__iter_pointer>(__rt);
2642 __rt = static_cast<__node_pointer>(__rt->__left_);
2644 else if (value_comp()(__rt->__value_, __k))
2645 __rt = static_cast<__node_pointer>(__rt->__right_);
2647 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2648 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2650 return _Pp(const_iterator(__result), const_iterator(__result));
2653 template <class _Tp, class _Compare, class _Allocator>
2654 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2655 __tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
2657 __node_pointer __np = __p.__get_np();
2658 if (__begin_node() == __p.__ptr_)
2660 if (__np->__right_ != nullptr)
2661 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
2663 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
2666 __tree_remove(__end_node()->__left_,
2667 static_cast<__node_base_pointer>(__np));
2668 return __node_holder(__np, _Dp(__node_alloc(), true));
2671 template <class _Tp, class _Compare, class _Allocator>
2672 inline _LIBCPP_INLINE_VISIBILITY
2674 swap(__tree<_Tp, _Compare, _Allocator>& __x,
2675 __tree<_Tp, _Compare, _Allocator>& __y)
2676 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2681 _LIBCPP_END_NAMESPACE_STD
2685 #endif // _LIBCPP___TREE