[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / libcxx / include / __tree
blobf6ef21cdaa5b9851c73daccd541c30e8dc80c308
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___TREE
11 #define _LIBCPP___TREE
13 #include <__algorithm/min.h>
14 #include <__assert>
15 #include <__config>
16 #include <__iterator/distance.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__iterator/next.h>
19 #include <__memory/addressof.h>
20 #include <__memory/allocator_traits.h>
21 #include <__memory/compressed_pair.h>
22 #include <__memory/pointer_traits.h>
23 #include <__memory/swap_allocator.h>
24 #include <__memory/unique_ptr.h>
25 #include <__type_traits/can_extract_key.h>
26 #include <__type_traits/conditional.h>
27 #include <__type_traits/enable_if.h>
28 #include <__type_traits/invoke.h>
29 #include <__type_traits/is_const.h>
30 #include <__type_traits/is_constructible.h>
31 #include <__type_traits/is_nothrow_assignable.h>
32 #include <__type_traits/is_nothrow_constructible.h>
33 #include <__type_traits/is_pointer.h>
34 #include <__type_traits/is_same.h>
35 #include <__type_traits/is_swappable.h>
36 #include <__type_traits/remove_const_ref.h>
37 #include <__type_traits/remove_cvref.h>
38 #include <__utility/forward.h>
39 #include <__utility/move.h>
40 #include <__utility/pair.h>
41 #include <__utility/swap.h>
42 #include <limits>
44 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
45 #  pragma GCC system_header
46 #endif
48 _LIBCPP_PUSH_MACROS
49 #include <__undef_macros>
51 _LIBCPP_BEGIN_NAMESPACE_STD
53 template <class, class, class, class>
54 class _LIBCPP_TEMPLATE_VIS map;
55 template <class, class, class, class>
56 class _LIBCPP_TEMPLATE_VIS multimap;
57 template <class, class, class>
58 class _LIBCPP_TEMPLATE_VIS set;
59 template <class, class, class>
60 class _LIBCPP_TEMPLATE_VIS multiset;
62 template <class _Tp, class _Compare, class _Allocator>
63 class __tree;
64 template <class _Tp, class _NodePtr, class _DiffType>
65 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
66 template <class _Tp, class _ConstNodePtr, class _DiffType>
67 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
69 template <class _Pointer>
70 class __tree_end_node;
71 template <class _VoidPtr>
72 class __tree_node_base;
73 template <class _Tp, class _VoidPtr>
74 class __tree_node;
76 template <class _Key, class _Value>
77 struct __value_type;
79 template <class _Allocator>
80 class __map_node_destructor;
81 template <class _TreeIterator>
82 class _LIBCPP_TEMPLATE_VIS __map_iterator;
83 template <class _TreeIterator>
84 class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
88 _NodePtr algorithms
90 The algorithms taking _NodePtr are red black tree algorithms.  Those
91 algorithms taking a parameter named __root should assume that __root
92 points to a proper red black tree (unless otherwise specified).
94 Each algorithm herein assumes that __root->__parent_ points to a non-null
95 structure which has a member __left_ which points back to __root.  No other
96 member is read or written to at __root->__parent_.
98 __root->__parent_ will be referred to below (in comments only) as end_node.
99 end_node->__left_ is an externably accessible lvalue for __root, and can be
100 changed by node insertion and removal (without explicit reference to end_node).
102 All nodes (with the exception of end_node), even the node referred to as
103 __root, have a non-null __parent_ field.
107 // Returns:  true if __x is a left child of its parent, else false
108 // Precondition:  __x != nullptr.
109 template <class _NodePtr>
110 inline _LIBCPP_HIDE_FROM_ABI bool __tree_is_left_child(_NodePtr __x) _NOEXCEPT {
111   return __x == __x->__parent_->__left_;
114 // Determines if the subtree rooted at __x is a proper red black subtree.  If
115 //    __x is a proper subtree, returns the black height (null counts as 1).  If
116 //    __x is an improper subtree, returns 0.
117 template <class _NodePtr>
118 unsigned __tree_sub_invariant(_NodePtr __x) {
119   if (__x == nullptr)
120     return 1;
121   // parent consistency checked by caller
122   // check __x->__left_ consistency
123   if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
124     return 0;
125   // check __x->__right_ consistency
126   if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
127     return 0;
128   // check __x->__left_ != __x->__right_ unless both are nullptr
129   if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
130     return 0;
131   // If this is red, neither child can be red
132   if (!__x->__is_black_) {
133     if (__x->__left_ && !__x->__left_->__is_black_)
134       return 0;
135     if (__x->__right_ && !__x->__right_->__is_black_)
136       return 0;
137   }
138   unsigned __h = std::__tree_sub_invariant(__x->__left_);
139   if (__h == 0)
140     return 0; // invalid left subtree
141   if (__h != std::__tree_sub_invariant(__x->__right_))
142     return 0;                    // invalid or different height right subtree
143   return __h + __x->__is_black_; // return black height of this node
146 // Determines if the red black tree rooted at __root is a proper red black tree.
147 //    __root == nullptr is a proper tree.  Returns true is __root is a proper
148 //    red black tree, else returns false.
149 template <class _NodePtr>
150 _LIBCPP_HIDE_FROM_ABI bool __tree_invariant(_NodePtr __root) {
151   if (__root == nullptr)
152     return true;
153   // check __x->__parent_ consistency
154   if (__root->__parent_ == nullptr)
155     return false;
156   if (!std::__tree_is_left_child(__root))
157     return false;
158   // root must be black
159   if (!__root->__is_black_)
160     return false;
161   // do normal node checks
162   return std::__tree_sub_invariant(__root) != 0;
165 // Returns:  pointer to the left-most node under __x.
166 template <class _NodePtr>
167 inline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_min(_NodePtr __x) _NOEXCEPT {
168   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null");
169   while (__x->__left_ != nullptr)
170     __x = __x->__left_;
171   return __x;
174 // Returns:  pointer to the right-most node under __x.
175 template <class _NodePtr>
176 inline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_max(_NodePtr __x) _NOEXCEPT {
177   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Root node shouldn't be null");
178   while (__x->__right_ != nullptr)
179     __x = __x->__right_;
180   return __x;
183 // Returns:  pointer to the next in-order node after __x.
184 template <class _NodePtr>
185 _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_next(_NodePtr __x) _NOEXCEPT {
186   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
187   if (__x->__right_ != nullptr)
188     return std::__tree_min(__x->__right_);
189   while (!std::__tree_is_left_child(__x))
190     __x = __x->__parent_unsafe();
191   return __x->__parent_unsafe();
194 template <class _EndNodePtr, class _NodePtr>
195 inline _LIBCPP_HIDE_FROM_ABI _EndNodePtr __tree_next_iter(_NodePtr __x) _NOEXCEPT {
196   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
197   if (__x->__right_ != nullptr)
198     return static_cast<_EndNodePtr>(std::__tree_min(__x->__right_));
199   while (!std::__tree_is_left_child(__x))
200     __x = __x->__parent_unsafe();
201   return static_cast<_EndNodePtr>(__x->__parent_);
204 // Returns:  pointer to the previous in-order node before __x.
205 // Note: __x may be the end node.
206 template <class _NodePtr, class _EndNodePtr>
207 inline _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_prev_iter(_EndNodePtr __x) _NOEXCEPT {
208   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
209   if (__x->__left_ != nullptr)
210     return std::__tree_max(__x->__left_);
211   _NodePtr __xx = static_cast<_NodePtr>(__x);
212   while (std::__tree_is_left_child(__xx))
213     __xx = __xx->__parent_unsafe();
214   return __xx->__parent_unsafe();
217 // Returns:  pointer to a node which has no children
218 template <class _NodePtr>
219 _LIBCPP_HIDE_FROM_ABI _NodePtr __tree_leaf(_NodePtr __x) _NOEXCEPT {
220   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
221   while (true) {
222     if (__x->__left_ != nullptr) {
223       __x = __x->__left_;
224       continue;
225     }
226     if (__x->__right_ != nullptr) {
227       __x = __x->__right_;
228       continue;
229     }
230     break;
231   }
232   return __x;
235 // Effects:  Makes __x->__right_ the subtree root with __x as its left child
236 //           while preserving in-order order.
237 template <class _NodePtr>
238 _LIBCPP_HIDE_FROM_ABI void __tree_left_rotate(_NodePtr __x) _NOEXCEPT {
239   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
240   _LIBCPP_ASSERT_INTERNAL(__x->__right_ != nullptr, "node should have a right child");
241   _NodePtr __y  = __x->__right_;
242   __x->__right_ = __y->__left_;
243   if (__x->__right_ != nullptr)
244     __x->__right_->__set_parent(__x);
245   __y->__parent_ = __x->__parent_;
246   if (std::__tree_is_left_child(__x))
247     __x->__parent_->__left_ = __y;
248   else
249     __x->__parent_unsafe()->__right_ = __y;
250   __y->__left_ = __x;
251   __x->__set_parent(__y);
254 // Effects:  Makes __x->__left_ the subtree root with __x as its right child
255 //           while preserving in-order order.
256 template <class _NodePtr>
257 _LIBCPP_HIDE_FROM_ABI void __tree_right_rotate(_NodePtr __x) _NOEXCEPT {
258   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "node shouldn't be null");
259   _LIBCPP_ASSERT_INTERNAL(__x->__left_ != nullptr, "node should have a left child");
260   _NodePtr __y = __x->__left_;
261   __x->__left_ = __y->__right_;
262   if (__x->__left_ != nullptr)
263     __x->__left_->__set_parent(__x);
264   __y->__parent_ = __x->__parent_;
265   if (std::__tree_is_left_child(__x))
266     __x->__parent_->__left_ = __y;
267   else
268     __x->__parent_unsafe()->__right_ = __y;
269   __y->__right_ = __x;
270   __x->__set_parent(__y);
273 // Effects:  Rebalances __root after attaching __x to a leaf.
274 // Precondition:  __x has no children.
275 //                __x == __root or == a direct or indirect child of __root.
276 //                If __x were to be unlinked from __root (setting __root to
277 //                  nullptr if __root == __x), __tree_invariant(__root) == true.
278 // Postcondition: __tree_invariant(end_node->__left_) == true.  end_node->__left_
279 //                may be different than the value passed in as __root.
280 template <class _NodePtr>
281 _LIBCPP_HIDE_FROM_ABI void __tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT {
282   _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root of the tree shouldn't be null");
283   _LIBCPP_ASSERT_INTERNAL(__x != nullptr, "Can't attach null node to a leaf");
284   __x->__is_black_ = __x == __root;
285   while (__x != __root && !__x->__parent_unsafe()->__is_black_) {
286     // __x->__parent_ != __root because __x->__parent_->__is_black == false
287     if (std::__tree_is_left_child(__x->__parent_unsafe())) {
288       _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
289       if (__y != nullptr && !__y->__is_black_) {
290         __x              = __x->__parent_unsafe();
291         __x->__is_black_ = true;
292         __x              = __x->__parent_unsafe();
293         __x->__is_black_ = __x == __root;
294         __y->__is_black_ = true;
295       } else {
296         if (!std::__tree_is_left_child(__x)) {
297           __x = __x->__parent_unsafe();
298           std::__tree_left_rotate(__x);
299         }
300         __x              = __x->__parent_unsafe();
301         __x->__is_black_ = true;
302         __x              = __x->__parent_unsafe();
303         __x->__is_black_ = false;
304         std::__tree_right_rotate(__x);
305         break;
306       }
307     } else {
308       _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
309       if (__y != nullptr && !__y->__is_black_) {
310         __x              = __x->__parent_unsafe();
311         __x->__is_black_ = true;
312         __x              = __x->__parent_unsafe();
313         __x->__is_black_ = __x == __root;
314         __y->__is_black_ = true;
315       } else {
316         if (std::__tree_is_left_child(__x)) {
317           __x = __x->__parent_unsafe();
318           std::__tree_right_rotate(__x);
319         }
320         __x              = __x->__parent_unsafe();
321         __x->__is_black_ = true;
322         __x              = __x->__parent_unsafe();
323         __x->__is_black_ = false;
324         std::__tree_left_rotate(__x);
325         break;
326       }
327     }
328   }
331 // Precondition:  __z == __root or == a direct or indirect child of __root.
332 // Effects:  unlinks __z from the tree rooted at __root, rebalancing as needed.
333 // Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
334 //                nor any of its children refer to __z.  end_node->__left_
335 //                may be different than the value passed in as __root.
336 template <class _NodePtr>
337 _LIBCPP_HIDE_FROM_ABI void __tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT {
338   _LIBCPP_ASSERT_INTERNAL(__root != nullptr, "Root node should not be null");
339   _LIBCPP_ASSERT_INTERNAL(__z != nullptr, "The node to remove should not be null");
340   _LIBCPP_ASSERT_INTERNAL(std::__tree_invariant(__root), "The tree invariants should hold");
341   // __z will be removed from the tree.  Client still needs to destruct/deallocate it
342   // __y is either __z, or if __z has two children, __tree_next(__z).
343   // __y will have at most one child.
344   // __y will be the initial hole in the tree (make the hole at a leaf)
345   _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ? __z : std::__tree_next(__z);
346   // __x is __y's possibly null single child
347   _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
348   // __w is __x's possibly null uncle (will become __x's sibling)
349   _NodePtr __w = nullptr;
350   // link __x to __y's parent, and find __w
351   if (__x != nullptr)
352     __x->__parent_ = __y->__parent_;
353   if (std::__tree_is_left_child(__y)) {
354     __y->__parent_->__left_ = __x;
355     if (__y != __root)
356       __w = __y->__parent_unsafe()->__right_;
357     else
358       __root = __x; // __w == nullptr
359   } else {
360     __y->__parent_unsafe()->__right_ = __x;
361     // __y can't be root if it is a right child
362     __w = __y->__parent_->__left_;
363   }
364   bool __removed_black = __y->__is_black_;
365   // If we didn't remove __z, do so now by splicing in __y for __z,
366   //    but copy __z's color.  This does not impact __x or __w.
367   if (__y != __z) {
368     // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
369     __y->__parent_ = __z->__parent_;
370     if (std::__tree_is_left_child(__z))
371       __y->__parent_->__left_ = __y;
372     else
373       __y->__parent_unsafe()->__right_ = __y;
374     __y->__left_ = __z->__left_;
375     __y->__left_->__set_parent(__y);
376     __y->__right_ = __z->__right_;
377     if (__y->__right_ != nullptr)
378       __y->__right_->__set_parent(__y);
379     __y->__is_black_ = __z->__is_black_;
380     if (__root == __z)
381       __root = __y;
382   }
383   // There is no need to rebalance if we removed a red, or if we removed
384   //     the last node.
385   if (__removed_black && __root != nullptr) {
386     // Rebalance:
387     // __x has an implicit black color (transferred from the removed __y)
388     //    associated with it, no matter what its color is.
389     // If __x is __root (in which case it can't be null), it is supposed
390     //    to be black anyway, and if it is doubly black, then the double
391     //    can just be ignored.
392     // If __x is red (in which case it can't be null), then it can absorb
393     //    the implicit black just by setting its color to black.
394     // Since __y was black and only had one child (which __x points to), __x
395     //   is either red with no children, else null, otherwise __y would have
396     //   different black heights under left and right pointers.
397     // if (__x == __root || __x != nullptr && !__x->__is_black_)
398     if (__x != nullptr)
399       __x->__is_black_ = true;
400     else {
401       //  Else __x isn't root, and is "doubly black", even though it may
402       //     be null.  __w can not be null here, else the parent would
403       //     see a black height >= 2 on the __x side and a black height
404       //     of 1 on the __w side (__w must be a non-null black or a red
405       //     with a non-null black child).
406       while (true) {
407         if (!std::__tree_is_left_child(__w)) // if x is left child
408         {
409           if (!__w->__is_black_) {
410             __w->__is_black_                    = true;
411             __w->__parent_unsafe()->__is_black_ = false;
412             std::__tree_left_rotate(__w->__parent_unsafe());
413             // __x is still valid
414             // reset __root only if necessary
415             if (__root == __w->__left_)
416               __root = __w;
417             // reset sibling, and it still can't be null
418             __w = __w->__left_->__right_;
419           }
420           // __w->__is_black_ is now true, __w may have null children
421           if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
422               (__w->__right_ == nullptr || __w->__right_->__is_black_)) {
423             __w->__is_black_ = false;
424             __x              = __w->__parent_unsafe();
425             // __x can no longer be null
426             if (__x == __root || !__x->__is_black_) {
427               __x->__is_black_ = true;
428               break;
429             }
430             // reset sibling, and it still can't be null
431             __w = std::__tree_is_left_child(__x) ? __x->__parent_unsafe()->__right_ : __x->__parent_->__left_;
432             // continue;
433           } else // __w has a red child
434           {
435             if (__w->__right_ == nullptr || __w->__right_->__is_black_) {
436               // __w left child is non-null and red
437               __w->__left_->__is_black_ = true;
438               __w->__is_black_          = false;
439               std::__tree_right_rotate(__w);
440               // __w is known not to be root, so root hasn't changed
441               // reset sibling, and it still can't be null
442               __w = __w->__parent_unsafe();
443             }
444             // __w has a right red child, left child may be null
445             __w->__is_black_                    = __w->__parent_unsafe()->__is_black_;
446             __w->__parent_unsafe()->__is_black_ = true;
447             __w->__right_->__is_black_          = true;
448             std::__tree_left_rotate(__w->__parent_unsafe());
449             break;
450           }
451         } else {
452           if (!__w->__is_black_) {
453             __w->__is_black_                    = true;
454             __w->__parent_unsafe()->__is_black_ = false;
455             std::__tree_right_rotate(__w->__parent_unsafe());
456             // __x is still valid
457             // reset __root only if necessary
458             if (__root == __w->__right_)
459               __root = __w;
460             // reset sibling, and it still can't be null
461             __w = __w->__right_->__left_;
462           }
463           // __w->__is_black_ is now true, __w may have null children
464           if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
465               (__w->__right_ == nullptr || __w->__right_->__is_black_)) {
466             __w->__is_black_ = false;
467             __x              = __w->__parent_unsafe();
468             // __x can no longer be null
469             if (!__x->__is_black_ || __x == __root) {
470               __x->__is_black_ = true;
471               break;
472             }
473             // reset sibling, and it still can't be null
474             __w = std::__tree_is_left_child(__x) ? __x->__parent_unsafe()->__right_ : __x->__parent_->__left_;
475             // continue;
476           } else // __w has a red child
477           {
478             if (__w->__left_ == nullptr || __w->__left_->__is_black_) {
479               // __w right child is non-null and red
480               __w->__right_->__is_black_ = true;
481               __w->__is_black_           = false;
482               std::__tree_left_rotate(__w);
483               // __w is known not to be root, so root hasn't changed
484               // reset sibling, and it still can't be null
485               __w = __w->__parent_unsafe();
486             }
487             // __w has a left red child, right child may be null
488             __w->__is_black_                    = __w->__parent_unsafe()->__is_black_;
489             __w->__parent_unsafe()->__is_black_ = true;
490             __w->__left_->__is_black_           = true;
491             std::__tree_right_rotate(__w->__parent_unsafe());
492             break;
493           }
494         }
495       }
496     }
497   }
500 // node traits
502 template <class _Tp>
503 struct __is_tree_value_type_imp : false_type {};
505 template <class _Key, class _Value>
506 struct __is_tree_value_type_imp<__value_type<_Key, _Value> > : true_type {};
508 template <class... _Args>
509 struct __is_tree_value_type : false_type {};
511 template <class _One>
512 struct __is_tree_value_type<_One> : __is_tree_value_type_imp<__remove_cvref_t<_One> > {};
514 template <class _Tp>
515 struct __tree_key_value_types {
516   typedef _Tp key_type;
517   typedef _Tp __node_value_type;
518   typedef _Tp __container_value_type;
519   static const bool __is_map = false;
521   _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(_Tp const& __v) { return __v; }
522   _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(__node_value_type const& __v) { return __v; }
523   _LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) { return std::addressof(__n); }
524   _LIBCPP_HIDE_FROM_ABI static __container_value_type&& __move(__node_value_type& __v) { return std::move(__v); }
527 template <class _Key, class _Tp>
528 struct __tree_key_value_types<__value_type<_Key, _Tp> > {
529   typedef _Key key_type;
530   typedef _Tp mapped_type;
531   typedef __value_type<_Key, _Tp> __node_value_type;
532   typedef pair<const _Key, _Tp> __container_value_type;
533   typedef __container_value_type __map_value_type;
534   static const bool __is_map = true;
536   _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(__node_value_type const& __t) {
537     return __t.__get_value().first;
538   }
540   template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0>
541   _LIBCPP_HIDE_FROM_ABI static key_type const& __get_key(_Up& __t) {
542     return __t.first;
543   }
545   _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(__node_value_type const& __t) {
546     return __t.__get_value();
547   }
549   template <class _Up, __enable_if_t<__is_same_uncvref<_Up, __container_value_type>::value, int> = 0>
550   _LIBCPP_HIDE_FROM_ABI static __container_value_type const& __get_value(_Up& __t) {
551     return __t;
552   }
554   _LIBCPP_HIDE_FROM_ABI static __container_value_type* __get_ptr(__node_value_type& __n) {
555     return std::addressof(__n.__get_value());
556   }
558   _LIBCPP_HIDE_FROM_ABI static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) { return __v.__move(); }
561 template <class _VoidPtr>
562 struct __tree_node_base_types {
563   typedef _VoidPtr __void_pointer;
565   typedef __tree_node_base<__void_pointer> __node_base_type;
566   typedef __rebind_pointer_t<_VoidPtr, __node_base_type> __node_base_pointer;
568   typedef __tree_end_node<__node_base_pointer> __end_node_type;
569   typedef __rebind_pointer_t<_VoidPtr, __end_node_type> __end_node_pointer;
570   typedef __end_node_pointer __parent_pointer;
572 // TODO(LLVM 22): Remove this check
573 #ifndef _LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB
574   static_assert(sizeof(__node_base_pointer) == sizeof(__end_node_pointer) && _LIBCPP_ALIGNOF(__node_base_pointer) ==
575                     _LIBCPP_ALIGNOF(__end_node_pointer),
576                 "It looks like you are using std::__tree (an implementation detail for (multi)map/set) with a fancy "
577                 "pointer type that thas a different representation depending on whether it points to a __tree base "
578                 "pointer or a __tree node pointer (both of which are implementation details of the standard library). "
579                 "This means that your ABI is being broken between LLVM 19 and LLVM 20. If you don't care about your "
580                 "ABI being broken, define the _LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB macro to silence this "
581                 "diagnostic.");
582 #endif
584 private:
585   static_assert(is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value,
586                 "_VoidPtr does not point to unqualified void type");
589 template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>, bool = _KVTypes::__is_map>
590 struct __tree_map_pointer_types {};
592 template <class _Tp, class _AllocPtr, class _KVTypes>
593 struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
594   typedef typename _KVTypes::__map_value_type _Mv;
595   typedef __rebind_pointer_t<_AllocPtr, _Mv> __map_value_type_pointer;
596   typedef __rebind_pointer_t<_AllocPtr, const _Mv> __const_map_value_type_pointer;
599 template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
600 struct __tree_node_types;
602 template <class _NodePtr, class _Tp, class _VoidPtr>
603 struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
604     : public __tree_node_base_types<_VoidPtr>, __tree_key_value_types<_Tp>, __tree_map_pointer_types<_Tp, _VoidPtr> {
605   typedef __tree_node_base_types<_VoidPtr> __base;
606   typedef __tree_key_value_types<_Tp> __key_base;
607   typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
609 public:
610   typedef typename pointer_traits<_NodePtr>::element_type __node_type;
611   typedef _NodePtr __node_pointer;
613   typedef _Tp __node_value_type;
614   typedef __rebind_pointer_t<_VoidPtr, __node_value_type> __node_value_type_pointer;
615   typedef __rebind_pointer_t<_VoidPtr, const __node_value_type> __const_node_value_type_pointer;
616   typedef typename __base::__end_node_pointer __iter_pointer;
618 private:
619   static_assert(!is_const<__node_type>::value, "_NodePtr should never be a pointer to const");
620   static_assert(is_same<__rebind_pointer_t<_VoidPtr, __node_type>, _NodePtr>::value,
621                 "_VoidPtr does not rebind to _NodePtr.");
624 template <class _ValueTp, class _VoidPtr>
625 struct __make_tree_node_types {
626   typedef __rebind_pointer_t<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> > _NodePtr;
627   typedef __tree_node_types<_NodePtr> type;
630 // node
632 template <class _Pointer>
633 class __tree_end_node {
634 public:
635   typedef _Pointer pointer;
636   pointer __left_;
638   _LIBCPP_HIDE_FROM_ABI __tree_end_node() _NOEXCEPT : __left_() {}
641 template <class _VoidPtr>
642 class _LIBCPP_STANDALONE_DEBUG __tree_node_base : public __tree_node_base_types<_VoidPtr>::__end_node_type {
643   typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
645 public:
646   typedef typename _NodeBaseTypes::__node_base_pointer pointer;
647   typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
649   pointer __right_;
650   __parent_pointer __parent_;
651   bool __is_black_;
653   _LIBCPP_HIDE_FROM_ABI pointer __parent_unsafe() const { return static_cast<pointer>(__parent_); }
655   _LIBCPP_HIDE_FROM_ABI void __set_parent(pointer __p) { __parent_ = static_cast<__parent_pointer>(__p); }
657   ~__tree_node_base()                                  = delete;
658   __tree_node_base(__tree_node_base const&)            = delete;
659   __tree_node_base& operator=(__tree_node_base const&) = delete;
662 template <class _Tp, class _VoidPtr>
663 class _LIBCPP_STANDALONE_DEBUG __tree_node : public __tree_node_base<_VoidPtr> {
664 public:
665   typedef _Tp __node_value_type;
667   __node_value_type __value_;
669   _LIBCPP_HIDE_FROM_ABI _Tp& __get_value() { return __value_; }
671   ~__tree_node()                             = delete;
672   __tree_node(__tree_node const&)            = delete;
673   __tree_node& operator=(__tree_node const&) = delete;
676 template <class _Allocator>
677 class __tree_node_destructor {
678   typedef _Allocator allocator_type;
679   typedef allocator_traits<allocator_type> __alloc_traits;
681 public:
682   typedef typename __alloc_traits::pointer pointer;
684 private:
685   typedef __tree_node_types<pointer> _NodeTypes;
686   allocator_type& __na_;
688 public:
689   bool __value_constructed;
691   _LIBCPP_HIDE_FROM_ABI __tree_node_destructor(const __tree_node_destructor&) = default;
692   __tree_node_destructor& operator=(const __tree_node_destructor&)            = delete;
694   _LIBCPP_HIDE_FROM_ABI explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
695       : __na_(__na),
696         __value_constructed(__val) {}
698   _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT {
699     if (__value_constructed)
700       __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
701     if (__p)
702       __alloc_traits::deallocate(__na_, __p, 1);
703   }
705   template <class>
706   friend class __map_node_destructor;
709 #if _LIBCPP_STD_VER >= 17
710 template <class _NodeType, class _Alloc>
711 struct __generic_container_node_destructor;
712 template <class _Tp, class _VoidPtr, class _Alloc>
713 struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc> : __tree_node_destructor<_Alloc> {
714   using __tree_node_destructor<_Alloc>::__tree_node_destructor;
716 #endif
718 template <class _Tp, class _NodePtr, class _DiffType>
719 class _LIBCPP_TEMPLATE_VIS __tree_iterator {
720   typedef __tree_node_types<_NodePtr> _NodeTypes;
721   typedef _NodePtr __node_pointer;
722   typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
723   typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
724   typedef typename _NodeTypes::__iter_pointer __iter_pointer;
725   typedef pointer_traits<__node_pointer> __pointer_traits;
727   __iter_pointer __ptr_;
729 public:
730   typedef bidirectional_iterator_tag iterator_category;
731   typedef _Tp value_type;
732   typedef _DiffType difference_type;
733   typedef value_type& reference;
734   typedef typename _NodeTypes::__node_value_type_pointer pointer;
736   _LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT
737 #if _LIBCPP_STD_VER >= 14
738       : __ptr_(nullptr)
739 #endif
740   {
741   }
743   _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__value_; }
744   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__get_np()->__value_); }
746   _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator++() {
747     __ptr_ = static_cast<__iter_pointer>(
748         std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
749     return *this;
750   }
751   _LIBCPP_HIDE_FROM_ABI __tree_iterator operator++(int) {
752     __tree_iterator __t(*this);
753     ++(*this);
754     return __t;
755   }
757   _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator--() {
758     __ptr_ = static_cast<__iter_pointer>(
759         std::__tree_prev_iter<__node_base_pointer>(static_cast<__end_node_pointer>(__ptr_)));
760     return *this;
761   }
762   _LIBCPP_HIDE_FROM_ABI __tree_iterator operator--(int) {
763     __tree_iterator __t(*this);
764     --(*this);
765     return __t;
766   }
768   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __tree_iterator& __x, const __tree_iterator& __y) {
769     return __x.__ptr_ == __y.__ptr_;
770   }
771   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y) {
772     return !(__x == __y);
773   }
775 private:
776   _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
777   _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
778   _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
779   template <class, class, class>
780   friend class __tree;
781   template <class, class, class>
782   friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
783   template <class>
784   friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
785   template <class, class, class, class>
786   friend class _LIBCPP_TEMPLATE_VIS map;
787   template <class, class, class, class>
788   friend class _LIBCPP_TEMPLATE_VIS multimap;
789   template <class, class, class>
790   friend class _LIBCPP_TEMPLATE_VIS set;
791   template <class, class, class>
792   friend class _LIBCPP_TEMPLATE_VIS multiset;
795 template <class _Tp, class _NodePtr, class _DiffType>
796 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator {
797   typedef __tree_node_types<_NodePtr> _NodeTypes;
798   typedef typename _NodeTypes::__node_pointer __node_pointer;
799   typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
800   typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
801   typedef typename _NodeTypes::__iter_pointer __iter_pointer;
802   typedef pointer_traits<__node_pointer> __pointer_traits;
804   __iter_pointer __ptr_;
806 public:
807   typedef bidirectional_iterator_tag iterator_category;
808   typedef _Tp value_type;
809   typedef _DiffType difference_type;
810   typedef const value_type& reference;
811   typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
813   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT
814 #if _LIBCPP_STD_VER >= 14
815       : __ptr_(nullptr)
816 #endif
817   {
818   }
820 private:
821   typedef __tree_iterator<value_type, __node_pointer, difference_type> __non_const_iterator;
823 public:
824   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
826   _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__value_; }
827   _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return pointer_traits<pointer>::pointer_to(__get_np()->__value_); }
829   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator++() {
830     __ptr_ = static_cast<__iter_pointer>(
831         std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
832     return *this;
833   }
835   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator operator++(int) {
836     __tree_const_iterator __t(*this);
837     ++(*this);
838     return __t;
839   }
841   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator--() {
842     __ptr_ = static_cast<__iter_pointer>(
843         std::__tree_prev_iter<__node_base_pointer>(static_cast<__end_node_pointer>(__ptr_)));
844     return *this;
845   }
847   _LIBCPP_HIDE_FROM_ABI __tree_const_iterator operator--(int) {
848     __tree_const_iterator __t(*this);
849     --(*this);
850     return __t;
851   }
853   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y) {
854     return __x.__ptr_ == __y.__ptr_;
855   }
856   friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y) {
857     return !(__x == __y);
858   }
860 private:
861   _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
862   _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
863   _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
865   template <class, class, class>
866   friend class __tree;
867   template <class, class, class, class>
868   friend class _LIBCPP_TEMPLATE_VIS map;
869   template <class, class, class, class>
870   friend class _LIBCPP_TEMPLATE_VIS multimap;
871   template <class, class, class>
872   friend class _LIBCPP_TEMPLATE_VIS set;
873   template <class, class, class>
874   friend class _LIBCPP_TEMPLATE_VIS multiset;
875   template <class>
876   friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
879 template <class _Tp, class _Compare>
880 #ifndef _LIBCPP_CXX03_LANG
881 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
882                          "the specified comparator type does not provide a viable const call operator")
883 #endif
884 int __diagnose_non_const_comparator();
886 template <class _Tp, class _Compare, class _Allocator>
887 class __tree {
888 public:
889   typedef _Tp value_type;
890   typedef _Compare value_compare;
891   typedef _Allocator allocator_type;
893 private:
894   typedef allocator_traits<allocator_type> __alloc_traits;
895   typedef typename __make_tree_node_types<value_type, typename __alloc_traits::void_pointer>::type _NodeTypes;
896   typedef typename _NodeTypes::key_type key_type;
898 public:
899   typedef typename _NodeTypes::__node_value_type __node_value_type;
900   typedef typename _NodeTypes::__container_value_type __container_value_type;
902   typedef typename __alloc_traits::pointer pointer;
903   typedef typename __alloc_traits::const_pointer const_pointer;
904   typedef typename __alloc_traits::size_type size_type;
905   typedef typename __alloc_traits::difference_type difference_type;
907 public:
908   typedef typename _NodeTypes::__void_pointer __void_pointer;
910   typedef typename _NodeTypes::__node_type __node;
911   typedef typename _NodeTypes::__node_pointer __node_pointer;
913   typedef typename _NodeTypes::__node_base_type __node_base;
914   typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
916   typedef typename _NodeTypes::__end_node_type __end_node_t;
917   typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
919   typedef typename _NodeTypes::__parent_pointer __parent_pointer;
920   typedef typename _NodeTypes::__iter_pointer __iter_pointer;
922   typedef __rebind_alloc<__alloc_traits, __node> __node_allocator;
923   typedef allocator_traits<__node_allocator> __node_traits;
925 private:
926   // check for sane allocator pointer rebinding semantics. Rebinding the
927   // allocator for a new pointer type should be exactly the same as rebinding
928   // the pointer using 'pointer_traits'.
929   static_assert(is_same<__node_pointer, typename __node_traits::pointer>::value,
930                 "Allocator does not rebind pointers in a sane manner.");
931   typedef __rebind_alloc<__node_traits, __node_base> __node_base_allocator;
932   typedef allocator_traits<__node_base_allocator> __node_base_traits;
933   static_assert(is_same<__node_base_pointer, typename __node_base_traits::pointer>::value,
934                 "Allocator does not rebind pointers in a sane manner.");
936 private:
937   __iter_pointer __begin_node_;
938   _LIBCPP_COMPRESSED_PAIR(__end_node_t, __end_node_, __node_allocator, __node_alloc_);
939   _LIBCPP_COMPRESSED_PAIR(size_type, __size_, value_compare, __value_comp_);
941 public:
942   _LIBCPP_HIDE_FROM_ABI __iter_pointer __end_node() _NOEXCEPT {
943     return static_cast<__iter_pointer>(pointer_traits<__end_node_ptr>::pointer_to(__end_node_));
944   }
945   _LIBCPP_HIDE_FROM_ABI __iter_pointer __end_node() const _NOEXCEPT {
946     return static_cast<__iter_pointer>(
947         pointer_traits<__end_node_ptr>::pointer_to(const_cast<__end_node_t&>(__end_node_)));
948   }
949   _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __node_alloc_; }
951 private:
952   _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __node_alloc_; }
953   _LIBCPP_HIDE_FROM_ABI __iter_pointer& __begin_node() _NOEXCEPT { return __begin_node_; }
954   _LIBCPP_HIDE_FROM_ABI const __iter_pointer& __begin_node() const _NOEXCEPT { return __begin_node_; }
956 public:
957   _LIBCPP_HIDE_FROM_ABI allocator_type __alloc() const _NOEXCEPT { return allocator_type(__node_alloc()); }
959 private:
960   _LIBCPP_HIDE_FROM_ABI size_type& size() _NOEXCEPT { return __size_; }
962 public:
963   _LIBCPP_HIDE_FROM_ABI const size_type& size() const _NOEXCEPT { return __size_; }
964   _LIBCPP_HIDE_FROM_ABI value_compare& value_comp() _NOEXCEPT { return __value_comp_; }
965   _LIBCPP_HIDE_FROM_ABI const value_compare& value_comp() const _NOEXCEPT { return __value_comp_; }
967 public:
968   _LIBCPP_HIDE_FROM_ABI __node_pointer __root() const _NOEXCEPT {
969     return static_cast<__node_pointer>(__end_node()->__left_);
970   }
972   _LIBCPP_HIDE_FROM_ABI __node_base_pointer* __root_ptr() const _NOEXCEPT {
973     return std::addressof(__end_node()->__left_);
974   }
976   typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
977   typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
979   _LIBCPP_HIDE_FROM_ABI explicit __tree(const value_compare& __comp) _NOEXCEPT_(
980       is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_copy_constructible<value_compare>::value);
981   _LIBCPP_HIDE_FROM_ABI explicit __tree(const allocator_type& __a);
982   _LIBCPP_HIDE_FROM_ABI __tree(const value_compare& __comp, const allocator_type& __a);
983   _LIBCPP_HIDE_FROM_ABI __tree(const __tree& __t);
984   _LIBCPP_HIDE_FROM_ABI __tree& operator=(const __tree& __t);
985   template <class _ForwardIterator>
986   _LIBCPP_HIDE_FROM_ABI void __assign_unique(_ForwardIterator __first, _ForwardIterator __last);
987   template <class _InputIterator>
988   _LIBCPP_HIDE_FROM_ABI void __assign_multi(_InputIterator __first, _InputIterator __last);
989   _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t) _NOEXCEPT_(
990       is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<value_compare>::value);
991   _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t, const allocator_type& __a);
992   _LIBCPP_HIDE_FROM_ABI __tree& operator=(__tree&& __t) _NOEXCEPT_(
993       __node_traits::propagate_on_container_move_assignment::value&& is_nothrow_move_assignable<value_compare>::value&&
994           is_nothrow_move_assignable<__node_allocator>::value);
995   _LIBCPP_HIDE_FROM_ABI ~__tree();
997   _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__begin_node()); }
998   _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__begin_node()); }
999   _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_node()); }
1000   _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_node()); }
1002   _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
1003     return std::min<size_type>(__node_traits::max_size(__node_alloc()), numeric_limits<difference_type >::max());
1004   }
1006   _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
1008   _LIBCPP_HIDE_FROM_ABI void swap(__tree& __t)
1009 #if _LIBCPP_STD_VER <= 11
1010       _NOEXCEPT_(__is_nothrow_swappable_v<value_compare> &&
1011                  (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>));
1012 #else
1013       _NOEXCEPT_(__is_nothrow_swappable_v<value_compare>);
1014 #endif
1016   template <class _Key, class... _Args>
1017   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args&&... __args);
1018   template <class _Key, class... _Args>
1019   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
1021   template <class... _Args>
1022   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1024   template <class... _Args>
1025   _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
1027   template <class... _Args>
1028   _LIBCPP_HIDE_FROM_ABI iterator __emplace_multi(_Args&&... __args);
1030   template <class... _Args>
1031   _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1033   template <class _Pp>
1034   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1035     return __emplace_unique_extract_key(std::forward<_Pp>(__x), __can_extract_key<_Pp, key_type>());
1036   }
1038   template <class _First,
1039             class _Second,
1040             __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0>
1041   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_First&& __f, _Second&& __s) {
1042     return __emplace_unique_key_args(__f, std::forward<_First>(__f), std::forward<_Second>(__s));
1043   }
1045   template <class... _Args>
1046   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1047     return __emplace_unique_impl(std::forward<_Args>(__args)...);
1048   }
1050   template <class _Pp>
1051   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1052     return __emplace_unique_impl(std::forward<_Pp>(__x));
1053   }
1055   template <class _Pp>
1056   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1057     return __emplace_unique_key_args(__x, std::forward<_Pp>(__x));
1058   }
1060   template <class _Pp>
1061   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1062     return __emplace_unique_key_args(__x.first, std::forward<_Pp>(__x));
1063   }
1065   template <class _Pp>
1066   _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1067     return __emplace_hint_unique_extract_key(__p, std::forward<_Pp>(__x), __can_extract_key<_Pp, key_type>());
1068   }
1070   template <class _First,
1071             class _Second,
1072             __enable_if_t<__can_extract_map_key<_First, key_type, __container_value_type>::value, int> = 0>
1073   _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1074     return __emplace_hint_unique_key_args(__p, __f, std::forward<_First>(__f), std::forward<_Second>(__s)).first;
1075   }
1077   template <class... _Args>
1078   _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1079     return __emplace_hint_unique_impl(__p, std::forward<_Args>(__args)...);
1080   }
1082   template <class _Pp>
1083   _LIBCPP_HIDE_FROM_ABI iterator
1084   __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1085     return __emplace_hint_unique_impl(__p, std::forward<_Pp>(__x));
1086   }
1088   template <class _Pp>
1089   _LIBCPP_HIDE_FROM_ABI iterator
1090   __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1091     return __emplace_hint_unique_key_args(__p, __x, std::forward<_Pp>(__x)).first;
1092   }
1094   template <class _Pp>
1095   _LIBCPP_HIDE_FROM_ABI iterator
1096   __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1097     return __emplace_hint_unique_key_args(__p, __x.first, std::forward<_Pp>(__x)).first;
1098   }
1100   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1101     return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1102   }
1104   _LIBCPP_HIDE_FROM_ABI iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1105     return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v).first;
1106   }
1108   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1109     return __emplace_unique_key_args(_NodeTypes::__get_key(__v), std::move(__v));
1110   }
1112   _LIBCPP_HIDE_FROM_ABI iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1113     return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), std::move(__v)).first;
1114   }
1116   template <class _Vp, __enable_if_t<!is_same<__remove_const_ref_t<_Vp>, __container_value_type>::value, int> = 0>
1117   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __insert_unique(_Vp&& __v) {
1118     return __emplace_unique(std::forward<_Vp>(__v));
1119   }
1121   template <class _Vp, __enable_if_t<!is_same<__remove_const_ref_t<_Vp>, __container_value_type>::value, int> = 0>
1122   _LIBCPP_HIDE_FROM_ABI iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1123     return __emplace_hint_unique(__p, std::forward<_Vp>(__v));
1124   }
1126   _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(__container_value_type&& __v) {
1127     return __emplace_multi(std::move(__v));
1128   }
1130   _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1131     return __emplace_hint_multi(__p, std::move(__v));
1132   }
1134   template <class _Vp>
1135   _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(_Vp&& __v) {
1136     return __emplace_multi(std::forward<_Vp>(__v));
1137   }
1139   template <class _Vp>
1140   _LIBCPP_HIDE_FROM_ABI iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1141     return __emplace_hint_multi(__p, std::forward<_Vp>(__v));
1142   }
1144   _LIBCPP_HIDE_FROM_ABI pair<iterator, bool>
1145   __node_assign_unique(const __container_value_type& __v, __node_pointer __dest);
1147   _LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(__node_pointer __nd);
1148   _LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1150   _LIBCPP_HIDE_FROM_ABI iterator __remove_node_pointer(__node_pointer) _NOEXCEPT;
1152 #if _LIBCPP_STD_VER >= 17
1153   template <class _NodeHandle, class _InsertReturnType>
1154   _LIBCPP_HIDE_FROM_ABI _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1155   template <class _NodeHandle>
1156   _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
1157   template <class _Tree>
1158   _LIBCPP_HIDE_FROM_ABI void __node_handle_merge_unique(_Tree& __source);
1160   template <class _NodeHandle>
1161   _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(_NodeHandle&&);
1162   template <class _NodeHandle>
1163   _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
1164   template <class _Tree>
1165   _LIBCPP_HIDE_FROM_ABI void __node_handle_merge_multi(_Tree& __source);
1167   template <class _NodeHandle>
1168   _LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(key_type const&);
1169   template <class _NodeHandle>
1170   _LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(const_iterator);
1171 #endif
1173   _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
1174   _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
1175   template <class _Key>
1176   _LIBCPP_HIDE_FROM_ABI size_type __erase_unique(const _Key& __k);
1177   template <class _Key>
1178   _LIBCPP_HIDE_FROM_ABI size_type __erase_multi(const _Key& __k);
1180   _LIBCPP_HIDE_FROM_ABI void
1181   __insert_node_at(__parent_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT;
1183   template <class _Key>
1184   _LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __v);
1185   template <class _Key>
1186   _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __v) const;
1188   template <class _Key>
1189   _LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const;
1190   template <class _Key>
1191   _LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const;
1193   template <class _Key>
1194   _LIBCPP_HIDE_FROM_ABI iterator lower_bound(const _Key& __v) {
1195     return __lower_bound(__v, __root(), __end_node());
1196   }
1197   template <class _Key>
1198   _LIBCPP_HIDE_FROM_ABI iterator __lower_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result);
1199   template <class _Key>
1200   _LIBCPP_HIDE_FROM_ABI const_iterator lower_bound(const _Key& __v) const {
1201     return __lower_bound(__v, __root(), __end_node());
1202   }
1203   template <class _Key>
1204   _LIBCPP_HIDE_FROM_ABI const_iterator
1205   __lower_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) const;
1206   template <class _Key>
1207   _LIBCPP_HIDE_FROM_ABI iterator upper_bound(const _Key& __v) {
1208     return __upper_bound(__v, __root(), __end_node());
1209   }
1210   template <class _Key>
1211   _LIBCPP_HIDE_FROM_ABI iterator __upper_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result);
1212   template <class _Key>
1213   _LIBCPP_HIDE_FROM_ABI const_iterator upper_bound(const _Key& __v) const {
1214     return __upper_bound(__v, __root(), __end_node());
1215   }
1216   template <class _Key>
1217   _LIBCPP_HIDE_FROM_ABI const_iterator
1218   __upper_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) const;
1219   template <class _Key>
1220   _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_unique(const _Key& __k);
1221   template <class _Key>
1222   _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_unique(const _Key& __k) const;
1224   template <class _Key>
1225   _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_multi(const _Key& __k);
1226   template <class _Key>
1227   _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_multi(const _Key& __k) const;
1229   typedef __tree_node_destructor<__node_allocator> _Dp;
1230   typedef unique_ptr<__node, _Dp> __node_holder;
1232   _LIBCPP_HIDE_FROM_ABI __node_holder remove(const_iterator __p) _NOEXCEPT;
1234 private:
1235   _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1236   _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1237   _LIBCPP_HIDE_FROM_ABI __node_base_pointer&
1238   __find_leaf(const_iterator __hint, __parent_pointer& __parent, const key_type& __v);
1239   // FIXME: Make this function const qualified. Unfortunately doing so
1240   // breaks existing code which uses non-const callable comparators.
1241   template <class _Key>
1242   _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_equal(__parent_pointer& __parent, const _Key& __v);
1243   template <class _Key>
1244   _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1245     return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1246   }
1247   template <class _Key>
1248   _LIBCPP_HIDE_FROM_ABI __node_base_pointer&
1249   __find_equal(const_iterator __hint, __parent_pointer& __parent, __node_base_pointer& __dummy, const _Key& __v);
1251   template <class... _Args>
1252   _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(_Args&&... __args);
1254   // TODO: Make this _LIBCPP_HIDE_FROM_ABI
1255   _LIBCPP_HIDDEN void destroy(__node_pointer __nd) _NOEXCEPT;
1257   _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree& __t) {
1258     __copy_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_copy_assignment::value>());
1259   }
1261   _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree& __t, true_type) {
1262     if (__node_alloc() != __t.__node_alloc())
1263       clear();
1264     __node_alloc() = __t.__node_alloc();
1265   }
1266   _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree&, false_type) {}
1268   _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, false_type);
1269   _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, true_type) _NOEXCEPT_(
1270       is_nothrow_move_assignable<value_compare>::value&& is_nothrow_move_assignable<__node_allocator>::value);
1272   _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree& __t)
1273       _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
1274                  is_nothrow_move_assignable<__node_allocator>::value) {
1275     __move_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
1276   }
1278   _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree& __t, true_type)
1279       _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
1280     __node_alloc() = std::move(__t.__node_alloc());
1281   }
1282   _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
1284   struct _DetachedTreeCache {
1285     _LIBCPP_HIDE_FROM_ABI explicit _DetachedTreeCache(__tree* __t) _NOEXCEPT
1286         : __t_(__t),
1287           __cache_root_(__detach_from_tree(__t)) {
1288       __advance();
1289     }
1291     _LIBCPP_HIDE_FROM_ABI __node_pointer __get() const _NOEXCEPT { return __cache_elem_; }
1293     _LIBCPP_HIDE_FROM_ABI void __advance() _NOEXCEPT {
1294       __cache_elem_ = __cache_root_;
1295       if (__cache_root_) {
1296         __cache_root_ = __detach_next(__cache_root_);
1297       }
1298     }
1300     _LIBCPP_HIDE_FROM_ABI ~_DetachedTreeCache() {
1301       __t_->destroy(__cache_elem_);
1302       if (__cache_root_) {
1303         while (__cache_root_->__parent_ != nullptr)
1304           __cache_root_ = static_cast<__node_pointer>(__cache_root_->__parent_);
1305         __t_->destroy(__cache_root_);
1306       }
1307     }
1309     _DetachedTreeCache(_DetachedTreeCache const&)            = delete;
1310     _DetachedTreeCache& operator=(_DetachedTreeCache const&) = delete;
1312   private:
1313     _LIBCPP_HIDE_FROM_ABI static __node_pointer __detach_from_tree(__tree* __t) _NOEXCEPT;
1314     _LIBCPP_HIDE_FROM_ABI static __node_pointer __detach_next(__node_pointer) _NOEXCEPT;
1316     __tree* __t_;
1317     __node_pointer __cache_root_;
1318     __node_pointer __cache_elem_;
1319   };
1321   template <class, class, class, class>
1322   friend class _LIBCPP_TEMPLATE_VIS map;
1323   template <class, class, class, class>
1324   friend class _LIBCPP_TEMPLATE_VIS multimap;
1327 template <class _Tp, class _Compare, class _Allocator>
1328 __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp) _NOEXCEPT_(
1329     is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_copy_constructible<value_compare>::value)
1330     : __size_(0), __value_comp_(__comp) {
1331   __begin_node() = __end_node();
1334 template <class _Tp, class _Compare, class _Allocator>
1335 __tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1336     : __begin_node_(__iter_pointer()), __node_alloc_(__node_allocator(__a)), __size_(0) {
1337   __begin_node() = __end_node();
1340 template <class _Tp, class _Compare, class _Allocator>
1341 __tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp, const allocator_type& __a)
1342     : __begin_node_(__iter_pointer()), __node_alloc_(__node_allocator(__a)), __size_(0), __value_comp_(__comp) {
1343   __begin_node() = __end_node();
1346 // Precondition:  size() != 0
1347 template <class _Tp, class _Compare, class _Allocator>
1348 typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1349 __tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_from_tree(__tree* __t) _NOEXCEPT {
1350   __node_pointer __cache                = static_cast<__node_pointer>(__t->__begin_node());
1351   __t->__begin_node()                   = __t->__end_node();
1352   __t->__end_node()->__left_->__parent_ = nullptr;
1353   __t->__end_node()->__left_            = nullptr;
1354   __t->size()                           = 0;
1355   // __cache->__left_ == nullptr
1356   if (__cache->__right_ != nullptr)
1357     __cache = static_cast<__node_pointer>(__cache->__right_);
1358   // __cache->__left_ == nullptr
1359   // __cache->__right_ == nullptr
1360   return __cache;
1363 // Precondition:  __cache != nullptr
1364 //    __cache->left_ == nullptr
1365 //    __cache->right_ == nullptr
1366 //    This is no longer a red-black tree
1367 template <class _Tp, class _Compare, class _Allocator>
1368 typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1369 __tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_next(__node_pointer __cache) _NOEXCEPT {
1370   if (__cache->__parent_ == nullptr)
1371     return nullptr;
1372   if (std::__tree_is_left_child(static_cast<__node_base_pointer>(__cache))) {
1373     __cache->__parent_->__left_ = nullptr;
1374     __cache                     = static_cast<__node_pointer>(__cache->__parent_);
1375     if (__cache->__right_ == nullptr)
1376       return __cache;
1377     return static_cast<__node_pointer>(std::__tree_leaf(__cache->__right_));
1378   }
1379   // __cache is right child
1380   __cache->__parent_unsafe()->__right_ = nullptr;
1381   __cache                              = static_cast<__node_pointer>(__cache->__parent_);
1382   if (__cache->__left_ == nullptr)
1383     return __cache;
1384   return static_cast<__node_pointer>(std::__tree_leaf(__cache->__left_));
1387 template <class _Tp, class _Compare, class _Allocator>
1388 __tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) {
1389   if (this != std::addressof(__t)) {
1390     value_comp() = __t.value_comp();
1391     __copy_assign_alloc(__t);
1392     __assign_multi(__t.begin(), __t.end());
1393   }
1394   return *this;
1397 template <class _Tp, class _Compare, class _Allocator>
1398 template <class _ForwardIterator>
1399 void __tree<_Tp, _Compare, _Allocator>::__assign_unique(_ForwardIterator __first, _ForwardIterator __last) {
1400   typedef iterator_traits<_ForwardIterator> _ITraits;
1401   typedef typename _ITraits::value_type _ItValueType;
1402   static_assert(is_same<_ItValueType, __container_value_type>::value,
1403                 "__assign_unique may only be called with the containers value type");
1404   static_assert(
1405       __has_forward_iterator_category<_ForwardIterator>::value, "__assign_unique requires a forward iterator");
1406   if (size() != 0) {
1407     _DetachedTreeCache __cache(this);
1408     for (; __cache.__get() != nullptr && __first != __last; ++__first) {
1409       if (__node_assign_unique(*__first, __cache.__get()).second)
1410         __cache.__advance();
1411     }
1412   }
1413   for (; __first != __last; ++__first)
1414     __insert_unique(*__first);
1417 template <class _Tp, class _Compare, class _Allocator>
1418 template <class _InputIterator>
1419 void __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last) {
1420   typedef iterator_traits<_InputIterator> _ITraits;
1421   typedef typename _ITraits::value_type _ItValueType;
1422   static_assert(
1423       (is_same<_ItValueType, __container_value_type>::value || is_same<_ItValueType, __node_value_type>::value),
1424       "__assign_multi may only be called with the containers value type"
1425       " or the nodes value type");
1426   if (size() != 0) {
1427     _DetachedTreeCache __cache(this);
1428     for (; __cache.__get() && __first != __last; ++__first) {
1429       __cache.__get()->__value_ = *__first;
1430       __node_insert_multi(__cache.__get());
1431       __cache.__advance();
1432     }
1433   }
1434   for (; __first != __last; ++__first)
1435     __insert_multi(_NodeTypes::__get_value(*__first));
1438 template <class _Tp, class _Compare, class _Allocator>
1439 __tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1440     : __begin_node_(__iter_pointer()),
1441       __node_alloc_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1442       __size_(0),
1443       __value_comp_(__t.value_comp()) {
1444   __begin_node() = __end_node();
1447 template <class _Tp, class _Compare, class _Allocator>
1448 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t) _NOEXCEPT_(
1449     is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<value_compare>::value)
1450     : __begin_node_(std::move(__t.__begin_node_)),
1451       __end_node_(std::move(__t.__end_node_)),
1452       __node_alloc_(std::move(__t.__node_alloc_)),
1453       __size_(__t.__size_),
1454       __value_comp_(std::move(__t.__value_comp_)) {
1455   if (size() == 0)
1456     __begin_node() = __end_node();
1457   else {
1458     __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1459     __t.__begin_node()               = __t.__end_node();
1460     __t.__end_node()->__left_        = nullptr;
1461     __t.size()                       = 0;
1462   }
1465 template <class _Tp, class _Compare, class _Allocator>
1466 __tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1467     : __node_alloc_(__node_allocator(__a)), __size_(0), __value_comp_(std::move(__t.value_comp())) {
1468   if (__a == __t.__alloc()) {
1469     if (__t.size() == 0)
1470       __begin_node() = __end_node();
1471     else {
1472       __begin_node()                   = __t.__begin_node();
1473       __end_node()->__left_            = __t.__end_node()->__left_;
1474       __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1475       size()                           = __t.size();
1476       __t.__begin_node()               = __t.__end_node();
1477       __t.__end_node()->__left_        = nullptr;
1478       __t.size()                       = 0;
1479     }
1480   } else {
1481     __begin_node() = __end_node();
1482   }
1485 template <class _Tp, class _Compare, class _Allocator>
1486 void __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1487     _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value&& is_nothrow_move_assignable<__node_allocator>::value) {
1488   destroy(static_cast<__node_pointer>(__end_node()->__left_));
1489   __begin_node_ = __t.__begin_node_;
1490   __end_node_   = __t.__end_node_;
1491   __move_assign_alloc(__t);
1492   __size_       = __t.__size_;
1493   __value_comp_ = std::move(__t.__value_comp_);
1494   if (size() == 0)
1495     __begin_node() = __end_node();
1496   else {
1497     __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1498     __t.__begin_node()               = __t.__end_node();
1499     __t.__end_node()->__left_        = nullptr;
1500     __t.size()                       = 0;
1501   }
1504 template <class _Tp, class _Compare, class _Allocator>
1505 void __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type) {
1506   if (__node_alloc() == __t.__node_alloc())
1507     __move_assign(__t, true_type());
1508   else {
1509     value_comp()       = std::move(__t.value_comp());
1510     const_iterator __e = end();
1511     if (size() != 0) {
1512       _DetachedTreeCache __cache(this);
1513       while (__cache.__get() != nullptr && __t.size() != 0) {
1514         __cache.__get()->__value_ = std::move(__t.remove(__t.begin())->__value_);
1515         __node_insert_multi(__cache.__get());
1516         __cache.__advance();
1517       }
1518     }
1519     while (__t.size() != 0)
1520       __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
1521   }
1524 template <class _Tp, class _Compare, class _Allocator>
1525 __tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t) _NOEXCEPT_(
1526     __node_traits::propagate_on_container_move_assignment::value&& is_nothrow_move_assignable<value_compare>::value&&
1527         is_nothrow_move_assignable<__node_allocator>::value)
1530   __move_assign(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
1531   return *this;
1534 template <class _Tp, class _Compare, class _Allocator>
1535 __tree<_Tp, _Compare, _Allocator>::~__tree() {
1536   static_assert(is_copy_constructible<value_compare>::value, "Comparator must be copy-constructible.");
1537   destroy(__root());
1540 template <class _Tp, class _Compare, class _Allocator>
1541 void __tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT {
1542   if (__nd != nullptr) {
1543     destroy(static_cast<__node_pointer>(__nd->__left_));
1544     destroy(static_cast<__node_pointer>(__nd->__right_));
1545     __node_allocator& __na = __node_alloc();
1546     __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
1547     __node_traits::deallocate(__na, __nd, 1);
1548   }
1551 template <class _Tp, class _Compare, class _Allocator>
1552 void __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1553 #if _LIBCPP_STD_VER <= 11
1554     _NOEXCEPT_(__is_nothrow_swappable_v<value_compare> &&
1555                (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>))
1556 #else
1557     _NOEXCEPT_(__is_nothrow_swappable_v<value_compare>)
1558 #endif
1560   using std::swap;
1561   swap(__begin_node_, __t.__begin_node_);
1562   swap(__end_node_, __t.__end_node_);
1563   std::__swap_allocator(__node_alloc(), __t.__node_alloc());
1564   swap(__size_, __t.__size_);
1565   swap(__value_comp_, __t.__value_comp_);
1566   if (size() == 0)
1567     __begin_node() = __end_node();
1568   else
1569     __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
1570   if (__t.size() == 0)
1571     __t.__begin_node() = __t.__end_node();
1572   else
1573     __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
1576 template <class _Tp, class _Compare, class _Allocator>
1577 void __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT {
1578   destroy(__root());
1579   size()                = 0;
1580   __begin_node()        = __end_node();
1581   __end_node()->__left_ = nullptr;
1584 // Find lower_bound place to insert
1585 // Set __parent to parent of null leaf
1586 // Return reference to null leaf
1587 template <class _Tp, class _Compare, class _Allocator>
1588 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1589 __tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent, const key_type& __v) {
1590   __node_pointer __nd = __root();
1591   if (__nd != nullptr) {
1592     while (true) {
1593       if (value_comp()(__nd->__value_, __v)) {
1594         if (__nd->__right_ != nullptr)
1595           __nd = static_cast<__node_pointer>(__nd->__right_);
1596         else {
1597           __parent = static_cast<__parent_pointer>(__nd);
1598           return __nd->__right_;
1599         }
1600       } else {
1601         if (__nd->__left_ != nullptr)
1602           __nd = static_cast<__node_pointer>(__nd->__left_);
1603         else {
1604           __parent = static_cast<__parent_pointer>(__nd);
1605           return __parent->__left_;
1606         }
1607       }
1608     }
1609   }
1610   __parent = static_cast<__parent_pointer>(__end_node());
1611   return __parent->__left_;
1614 // Find upper_bound place to insert
1615 // Set __parent to parent of null leaf
1616 // Return reference to null leaf
1617 template <class _Tp, class _Compare, class _Allocator>
1618 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1619 __tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent, const key_type& __v) {
1620   __node_pointer __nd = __root();
1621   if (__nd != nullptr) {
1622     while (true) {
1623       if (value_comp()(__v, __nd->__value_)) {
1624         if (__nd->__left_ != nullptr)
1625           __nd = static_cast<__node_pointer>(__nd->__left_);
1626         else {
1627           __parent = static_cast<__parent_pointer>(__nd);
1628           return __parent->__left_;
1629         }
1630       } else {
1631         if (__nd->__right_ != nullptr)
1632           __nd = static_cast<__node_pointer>(__nd->__right_);
1633         else {
1634           __parent = static_cast<__parent_pointer>(__nd);
1635           return __nd->__right_;
1636         }
1637       }
1638     }
1639   }
1640   __parent = static_cast<__parent_pointer>(__end_node());
1641   return __parent->__left_;
1644 // Find leaf place to insert closest to __hint
1645 // First check prior to __hint.
1646 // Next check after __hint.
1647 // Next do O(log N) search.
1648 // Set __parent to parent of null leaf
1649 // Return reference to null leaf
1650 template <class _Tp, class _Compare, class _Allocator>
1651 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1652 __tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint, __parent_pointer& __parent, const key_type& __v) {
1653   if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1654   {
1655     // __v <= *__hint
1656     const_iterator __prior = __hint;
1657     if (__prior == begin() || !value_comp()(__v, *--__prior)) {
1658       // *prev(__hint) <= __v <= *__hint
1659       if (__hint.__ptr_->__left_ == nullptr) {
1660         __parent = static_cast<__parent_pointer>(__hint.__ptr_);
1661         return __parent->__left_;
1662       } else {
1663         __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1664         return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
1665       }
1666     }
1667     // __v < *prev(__hint)
1668     return __find_leaf_high(__parent, __v);
1669   }
1670   // else __v > *__hint
1671   return __find_leaf_low(__parent, __v);
1674 // Find place to insert if __v doesn't exist
1675 // Set __parent to parent of null leaf
1676 // Return reference to null leaf
1677 // If __v exists, set parent to node of __v and return reference to node of __v
1678 template <class _Tp, class _Compare, class _Allocator>
1679 template <class _Key>
1680 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1681 __tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent, const _Key& __v) {
1682   __node_pointer __nd           = __root();
1683   __node_base_pointer* __nd_ptr = __root_ptr();
1684   if (__nd != nullptr) {
1685     while (true) {
1686       if (value_comp()(__v, __nd->__value_)) {
1687         if (__nd->__left_ != nullptr) {
1688           __nd_ptr = std::addressof(__nd->__left_);
1689           __nd     = static_cast<__node_pointer>(__nd->__left_);
1690         } else {
1691           __parent = static_cast<__parent_pointer>(__nd);
1692           return __parent->__left_;
1693         }
1694       } else if (value_comp()(__nd->__value_, __v)) {
1695         if (__nd->__right_ != nullptr) {
1696           __nd_ptr = std::addressof(__nd->__right_);
1697           __nd     = static_cast<__node_pointer>(__nd->__right_);
1698         } else {
1699           __parent = static_cast<__parent_pointer>(__nd);
1700           return __nd->__right_;
1701         }
1702       } else {
1703         __parent = static_cast<__parent_pointer>(__nd);
1704         return *__nd_ptr;
1705       }
1706     }
1707   }
1708   __parent = static_cast<__parent_pointer>(__end_node());
1709   return __parent->__left_;
1712 // Find place to insert if __v doesn't exist
1713 // First check prior to __hint.
1714 // Next check after __hint.
1715 // Next do O(log N) search.
1716 // Set __parent to parent of null leaf
1717 // Return reference to null leaf
1718 // If __v exists, set parent to node of __v and return reference to node of __v
1719 template <class _Tp, class _Compare, class _Allocator>
1720 template <class _Key>
1721 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& __tree<_Tp, _Compare, _Allocator>::__find_equal(
1722     const_iterator __hint, __parent_pointer& __parent, __node_base_pointer& __dummy, const _Key& __v) {
1723   if (__hint == end() || value_comp()(__v, *__hint)) // check before
1724   {
1725     // __v < *__hint
1726     const_iterator __prior = __hint;
1727     if (__prior == begin() || value_comp()(*--__prior, __v)) {
1728       // *prev(__hint) < __v < *__hint
1729       if (__hint.__ptr_->__left_ == nullptr) {
1730         __parent = static_cast<__parent_pointer>(__hint.__ptr_);
1731         return __parent->__left_;
1732       } else {
1733         __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1734         return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
1735       }
1736     }
1737     // __v <= *prev(__hint)
1738     return __find_equal(__parent, __v);
1739   } else if (value_comp()(*__hint, __v)) // check after
1740   {
1741     // *__hint < __v
1742     const_iterator __next = std::next(__hint);
1743     if (__next == end() || value_comp()(__v, *__next)) {
1744       // *__hint < __v < *std::next(__hint)
1745       if (__hint.__get_np()->__right_ == nullptr) {
1746         __parent = static_cast<__parent_pointer>(__hint.__ptr_);
1747         return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
1748       } else {
1749         __parent = static_cast<__parent_pointer>(__next.__ptr_);
1750         return __parent->__left_;
1751       }
1752     }
1753     // *next(__hint) <= __v
1754     return __find_equal(__parent, __v);
1755   }
1756   // else __v == *__hint
1757   __parent = static_cast<__parent_pointer>(__hint.__ptr_);
1758   __dummy  = static_cast<__node_base_pointer>(__hint.__ptr_);
1759   return __dummy;
1762 template <class _Tp, class _Compare, class _Allocator>
1763 void __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
1764     __parent_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT {
1765   __new_node->__left_   = nullptr;
1766   __new_node->__right_  = nullptr;
1767   __new_node->__parent_ = __parent;
1768   // __new_node->__is_black_ is initialized in __tree_balance_after_insert
1769   __child = __new_node;
1770   if (__begin_node()->__left_ != nullptr)
1771     __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
1772   std::__tree_balance_after_insert(__end_node()->__left_, __child);
1773   ++size();
1776 template <class _Tp, class _Compare, class _Allocator>
1777 template <class _Key, class... _Args>
1778 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1779 __tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args) {
1780   __parent_pointer __parent;
1781   __node_base_pointer& __child = __find_equal(__parent, __k);
1782   __node_pointer __r           = static_cast<__node_pointer>(__child);
1783   bool __inserted              = false;
1784   if (__child == nullptr) {
1785     __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1786     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1787     __r        = __h.release();
1788     __inserted = true;
1789   }
1790   return pair<iterator, bool>(iterator(__r), __inserted);
1793 template <class _Tp, class _Compare, class _Allocator>
1794 template <class _Key, class... _Args>
1795 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1796 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
1797     const_iterator __p, _Key const& __k, _Args&&... __args) {
1798   __parent_pointer __parent;
1799   __node_base_pointer __dummy;
1800   __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
1801   __node_pointer __r           = static_cast<__node_pointer>(__child);
1802   bool __inserted              = false;
1803   if (__child == nullptr) {
1804     __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1805     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1806     __r        = __h.release();
1807     __inserted = true;
1808   }
1809   return pair<iterator, bool>(iterator(__r), __inserted);
1812 template <class _Tp, class _Compare, class _Allocator>
1813 template <class... _Args>
1814 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1815 __tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&&... __args) {
1816   static_assert(!__is_tree_value_type<_Args...>::value, "Cannot construct from __value_type");
1817   __node_allocator& __na = __node_alloc();
1818   __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1819   __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), std::forward<_Args>(__args)...);
1820   __h.get_deleter().__value_constructed = true;
1821   return __h;
1824 template <class _Tp, class _Compare, class _Allocator>
1825 template <class... _Args>
1826 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1827 __tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args) {
1828   __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1829   __parent_pointer __parent;
1830   __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1831   __node_pointer __r           = static_cast<__node_pointer>(__child);
1832   bool __inserted              = false;
1833   if (__child == nullptr) {
1834     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1835     __r        = __h.release();
1836     __inserted = true;
1837   }
1838   return pair<iterator, bool>(iterator(__r), __inserted);
1841 template <class _Tp, class _Compare, class _Allocator>
1842 template <class... _Args>
1843 typename __tree<_Tp, _Compare, _Allocator>::iterator
1844 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args) {
1845   __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1846   __parent_pointer __parent;
1847   __node_base_pointer __dummy;
1848   __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
1849   __node_pointer __r           = static_cast<__node_pointer>(__child);
1850   if (__child == nullptr) {
1851     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1852     __r = __h.release();
1853   }
1854   return iterator(__r);
1857 template <class _Tp, class _Compare, class _Allocator>
1858 template <class... _Args>
1859 typename __tree<_Tp, _Compare, _Allocator>::iterator
1860 __tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args) {
1861   __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1862   __parent_pointer __parent;
1863   __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
1864   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1865   return iterator(static_cast<__node_pointer>(__h.release()));
1868 template <class _Tp, class _Compare, class _Allocator>
1869 template <class... _Args>
1870 typename __tree<_Tp, _Compare, _Allocator>::iterator
1871 __tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, _Args&&... __args) {
1872   __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1873   __parent_pointer __parent;
1874   __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
1875   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1876   return iterator(static_cast<__node_pointer>(__h.release()));
1879 template <class _Tp, class _Compare, class _Allocator>
1880 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1881 __tree<_Tp, _Compare, _Allocator>::__node_assign_unique(const __container_value_type& __v, __node_pointer __nd) {
1882   __parent_pointer __parent;
1883   __node_base_pointer& __child = __find_equal(__parent, _NodeTypes::__get_key(__v));
1884   __node_pointer __r           = static_cast<__node_pointer>(__child);
1885   bool __inserted              = false;
1886   if (__child == nullptr) {
1887     __nd->__value_ = __v;
1888     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1889     __r        = __nd;
1890     __inserted = true;
1891   }
1892   return pair<iterator, bool>(iterator(__r), __inserted);
1895 template <class _Tp, class _Compare, class _Allocator>
1896 typename __tree<_Tp, _Compare, _Allocator>::iterator
1897 __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd) {
1898   __parent_pointer __parent;
1899   __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
1900   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1901   return iterator(__nd);
1904 template <class _Tp, class _Compare, class _Allocator>
1905 typename __tree<_Tp, _Compare, _Allocator>::iterator
1906 __tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p, __node_pointer __nd) {
1907   __parent_pointer __parent;
1908   __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
1909   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
1910   return iterator(__nd);
1913 template <class _Tp, class _Compare, class _Allocator>
1914 typename __tree<_Tp, _Compare, _Allocator>::iterator
1915 __tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT {
1916   iterator __r(__ptr);
1917   ++__r;
1918   if (__begin_node() == __ptr)
1919     __begin_node() = __r.__ptr_;
1920   --size();
1921   std::__tree_remove(__end_node()->__left_, static_cast<__node_base_pointer>(__ptr));
1922   return __r;
1925 #if _LIBCPP_STD_VER >= 17
1926 template <class _Tp, class _Compare, class _Allocator>
1927 template <class _NodeHandle, class _InsertReturnType>
1928 _LIBCPP_HIDE_FROM_ABI _InsertReturnType
1929 __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(_NodeHandle&& __nh) {
1930   if (__nh.empty())
1931     return _InsertReturnType{end(), false, _NodeHandle()};
1933   __node_pointer __ptr = __nh.__ptr_;
1934   __parent_pointer __parent;
1935   __node_base_pointer& __child = __find_equal(__parent, __ptr->__value_);
1936   if (__child != nullptr)
1937     return _InsertReturnType{iterator(static_cast<__node_pointer>(__child)), false, std::move(__nh)};
1939   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
1940   __nh.__release_ptr();
1941   return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
1944 template <class _Tp, class _Compare, class _Allocator>
1945 template <class _NodeHandle>
1946 _LIBCPP_HIDE_FROM_ABI typename __tree<_Tp, _Compare, _Allocator>::iterator
1947 __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(const_iterator __hint, _NodeHandle&& __nh) {
1948   if (__nh.empty())
1949     return end();
1951   __node_pointer __ptr = __nh.__ptr_;
1952   __parent_pointer __parent;
1953   __node_base_pointer __dummy;
1954   __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy, __ptr->__value_);
1955   __node_pointer __r           = static_cast<__node_pointer>(__child);
1956   if (__child == nullptr) {
1957     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
1958     __r = __ptr;
1959     __nh.__release_ptr();
1960   }
1961   return iterator(__r);
1964 template <class _Tp, class _Compare, class _Allocator>
1965 template <class _NodeHandle>
1966 _LIBCPP_HIDE_FROM_ABI _NodeHandle __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key) {
1967   iterator __it = find(__key);
1968   if (__it == end())
1969     return _NodeHandle();
1970   return __node_handle_extract<_NodeHandle>(__it);
1973 template <class _Tp, class _Compare, class _Allocator>
1974 template <class _NodeHandle>
1975 _LIBCPP_HIDE_FROM_ABI _NodeHandle __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p) {
1976   __node_pointer __np = __p.__get_np();
1977   __remove_node_pointer(__np);
1978   return _NodeHandle(__np, __alloc());
1981 template <class _Tp, class _Compare, class _Allocator>
1982 template <class _Tree>
1983 _LIBCPP_HIDE_FROM_ABI void __tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(_Tree& __source) {
1984   static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
1986   for (typename _Tree::iterator __i = __source.begin(); __i != __source.end();) {
1987     __node_pointer __src_ptr = __i.__get_np();
1988     __parent_pointer __parent;
1989     __node_base_pointer& __child = __find_equal(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
1990     ++__i;
1991     if (__child != nullptr)
1992       continue;
1993     __source.__remove_node_pointer(__src_ptr);
1994     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__src_ptr));
1995   }
1998 template <class _Tp, class _Compare, class _Allocator>
1999 template <class _NodeHandle>
2000 _LIBCPP_HIDE_FROM_ABI typename __tree<_Tp, _Compare, _Allocator>::iterator
2001 __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh) {
2002   if (__nh.empty())
2003     return end();
2004   __node_pointer __ptr = __nh.__ptr_;
2005   __parent_pointer __parent;
2006   __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__ptr->__value_));
2007   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2008   __nh.__release_ptr();
2009   return iterator(__ptr);
2012 template <class _Tp, class _Compare, class _Allocator>
2013 template <class _NodeHandle>
2014 _LIBCPP_HIDE_FROM_ABI typename __tree<_Tp, _Compare, _Allocator>::iterator
2015 __tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh) {
2016   if (__nh.empty())
2017     return end();
2019   __node_pointer __ptr = __nh.__ptr_;
2020   __parent_pointer __parent;
2021   __node_base_pointer& __child = __find_leaf(__hint, __parent, _NodeTypes::__get_key(__ptr->__value_));
2022   __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2023   __nh.__release_ptr();
2024   return iterator(__ptr);
2027 template <class _Tp, class _Compare, class _Allocator>
2028 template <class _Tree>
2029 _LIBCPP_HIDE_FROM_ABI void __tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(_Tree& __source) {
2030   static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2032   for (typename _Tree::iterator __i = __source.begin(); __i != __source.end();) {
2033     __node_pointer __src_ptr = __i.__get_np();
2034     __parent_pointer __parent;
2035     __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
2036     ++__i;
2037     __source.__remove_node_pointer(__src_ptr);
2038     __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__src_ptr));
2039   }
2042 #endif // _LIBCPP_STD_VER >= 17
2044 template <class _Tp, class _Compare, class _Allocator>
2045 typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) {
2046   __node_pointer __np    = __p.__get_np();
2047   iterator __r           = __remove_node_pointer(__np);
2048   __node_allocator& __na = __node_alloc();
2049   __node_traits::destroy(__na, _NodeTypes::__get_ptr(const_cast<__node_value_type&>(*__p)));
2050   __node_traits::deallocate(__na, __np, 1);
2051   return __r;
2054 template <class _Tp, class _Compare, class _Allocator>
2055 typename __tree<_Tp, _Compare, _Allocator>::iterator
2056 __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l) {
2057   while (__f != __l)
2058     __f = erase(__f);
2059   return iterator(__l.__ptr_);
2062 template <class _Tp, class _Compare, class _Allocator>
2063 template <class _Key>
2064 typename __tree<_Tp, _Compare, _Allocator>::size_type
2065 __tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) {
2066   iterator __i = find(__k);
2067   if (__i == end())
2068     return 0;
2069   erase(__i);
2070   return 1;
2073 template <class _Tp, class _Compare, class _Allocator>
2074 template <class _Key>
2075 typename __tree<_Tp, _Compare, _Allocator>::size_type
2076 __tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) {
2077   pair<iterator, iterator> __p = __equal_range_multi(__k);
2078   size_type __r                = 0;
2079   for (; __p.first != __p.second; ++__r)
2080     __p.first = erase(__p.first);
2081   return __r;
2084 template <class _Tp, class _Compare, class _Allocator>
2085 template <class _Key>
2086 typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) {
2087   iterator __p = __lower_bound(__v, __root(), __end_node());
2088   if (__p != end() && !value_comp()(__v, *__p))
2089     return __p;
2090   return end();
2093 template <class _Tp, class _Compare, class _Allocator>
2094 template <class _Key>
2095 typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2096 __tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const {
2097   const_iterator __p = __lower_bound(__v, __root(), __end_node());
2098   if (__p != end() && !value_comp()(__v, *__p))
2099     return __p;
2100   return end();
2103 template <class _Tp, class _Compare, class _Allocator>
2104 template <class _Key>
2105 typename __tree<_Tp, _Compare, _Allocator>::size_type
2106 __tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const {
2107   __node_pointer __rt = __root();
2108   while (__rt != nullptr) {
2109     if (value_comp()(__k, __rt->__value_)) {
2110       __rt = static_cast<__node_pointer>(__rt->__left_);
2111     } else if (value_comp()(__rt->__value_, __k))
2112       __rt = static_cast<__node_pointer>(__rt->__right_);
2113     else
2114       return 1;
2115   }
2116   return 0;
2119 template <class _Tp, class _Compare, class _Allocator>
2120 template <class _Key>
2121 typename __tree<_Tp, _Compare, _Allocator>::size_type
2122 __tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const {
2123   __iter_pointer __result = __end_node();
2124   __node_pointer __rt     = __root();
2125   while (__rt != nullptr) {
2126     if (value_comp()(__k, __rt->__value_)) {
2127       __result = static_cast<__iter_pointer>(__rt);
2128       __rt     = static_cast<__node_pointer>(__rt->__left_);
2129     } else if (value_comp()(__rt->__value_, __k))
2130       __rt = static_cast<__node_pointer>(__rt->__right_);
2131     else
2132       return std::distance(
2133           __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2134           __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2135   }
2136   return 0;
2139 template <class _Tp, class _Compare, class _Allocator>
2140 template <class _Key>
2141 typename __tree<_Tp, _Compare, _Allocator>::iterator
2142 __tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) {
2143   while (__root != nullptr) {
2144     if (!value_comp()(__root->__value_, __v)) {
2145       __result = static_cast<__iter_pointer>(__root);
2146       __root   = static_cast<__node_pointer>(__root->__left_);
2147     } else
2148       __root = static_cast<__node_pointer>(__root->__right_);
2149   }
2150   return iterator(__result);
2153 template <class _Tp, class _Compare, class _Allocator>
2154 template <class _Key>
2155 typename __tree<_Tp, _Compare, _Allocator>::const_iterator __tree<_Tp, _Compare, _Allocator>::__lower_bound(
2156     const _Key& __v, __node_pointer __root, __iter_pointer __result) const {
2157   while (__root != nullptr) {
2158     if (!value_comp()(__root->__value_, __v)) {
2159       __result = static_cast<__iter_pointer>(__root);
2160       __root   = static_cast<__node_pointer>(__root->__left_);
2161     } else
2162       __root = static_cast<__node_pointer>(__root->__right_);
2163   }
2164   return const_iterator(__result);
2167 template <class _Tp, class _Compare, class _Allocator>
2168 template <class _Key>
2169 typename __tree<_Tp, _Compare, _Allocator>::iterator
2170 __tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v, __node_pointer __root, __iter_pointer __result) {
2171   while (__root != nullptr) {
2172     if (value_comp()(__v, __root->__value_)) {
2173       __result = static_cast<__iter_pointer>(__root);
2174       __root   = static_cast<__node_pointer>(__root->__left_);
2175     } else
2176       __root = static_cast<__node_pointer>(__root->__right_);
2177   }
2178   return iterator(__result);
2181 template <class _Tp, class _Compare, class _Allocator>
2182 template <class _Key>
2183 typename __tree<_Tp, _Compare, _Allocator>::const_iterator __tree<_Tp, _Compare, _Allocator>::__upper_bound(
2184     const _Key& __v, __node_pointer __root, __iter_pointer __result) const {
2185   while (__root != nullptr) {
2186     if (value_comp()(__v, __root->__value_)) {
2187       __result = static_cast<__iter_pointer>(__root);
2188       __root   = static_cast<__node_pointer>(__root->__left_);
2189     } else
2190       __root = static_cast<__node_pointer>(__root->__right_);
2191   }
2192   return const_iterator(__result);
2195 template <class _Tp, class _Compare, class _Allocator>
2196 template <class _Key>
2197 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator>
2198 __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) {
2199   typedef pair<iterator, iterator> _Pp;
2200   __iter_pointer __result = __end_node();
2201   __node_pointer __rt     = __root();
2202   while (__rt != nullptr) {
2203     if (value_comp()(__k, __rt->__value_)) {
2204       __result = static_cast<__iter_pointer>(__rt);
2205       __rt     = static_cast<__node_pointer>(__rt->__left_);
2206     } else if (value_comp()(__rt->__value_, __k))
2207       __rt = static_cast<__node_pointer>(__rt->__right_);
2208     else
2209       return _Pp(iterator(__rt),
2210                  iterator(__rt->__right_ != nullptr ? static_cast<__iter_pointer>(std::__tree_min(__rt->__right_))
2211                                                     : __result));
2212   }
2213   return _Pp(iterator(__result), iterator(__result));
2216 template <class _Tp, class _Compare, class _Allocator>
2217 template <class _Key>
2218 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2219      typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2220 __tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const {
2221   typedef pair<const_iterator, const_iterator> _Pp;
2222   __iter_pointer __result = __end_node();
2223   __node_pointer __rt     = __root();
2224   while (__rt != nullptr) {
2225     if (value_comp()(__k, __rt->__value_)) {
2226       __result = static_cast<__iter_pointer>(__rt);
2227       __rt     = static_cast<__node_pointer>(__rt->__left_);
2228     } else if (value_comp()(__rt->__value_, __k))
2229       __rt = static_cast<__node_pointer>(__rt->__right_);
2230     else
2231       return _Pp(
2232           const_iterator(__rt),
2233           const_iterator(
2234               __rt->__right_ != nullptr ? static_cast<__iter_pointer>(std::__tree_min(__rt->__right_)) : __result));
2235   }
2236   return _Pp(const_iterator(__result), const_iterator(__result));
2239 template <class _Tp, class _Compare, class _Allocator>
2240 template <class _Key>
2241 pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator>
2242 __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) {
2243   typedef pair<iterator, iterator> _Pp;
2244   __iter_pointer __result = __end_node();
2245   __node_pointer __rt     = __root();
2246   while (__rt != nullptr) {
2247     if (value_comp()(__k, __rt->__value_)) {
2248       __result = static_cast<__iter_pointer>(__rt);
2249       __rt     = static_cast<__node_pointer>(__rt->__left_);
2250     } else if (value_comp()(__rt->__value_, __k))
2251       __rt = static_cast<__node_pointer>(__rt->__right_);
2252     else
2253       return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2254                  __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2255   }
2256   return _Pp(iterator(__result), iterator(__result));
2259 template <class _Tp, class _Compare, class _Allocator>
2260 template <class _Key>
2261 pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2262      typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2263 __tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const {
2264   typedef pair<const_iterator, const_iterator> _Pp;
2265   __iter_pointer __result = __end_node();
2266   __node_pointer __rt     = __root();
2267   while (__rt != nullptr) {
2268     if (value_comp()(__k, __rt->__value_)) {
2269       __result = static_cast<__iter_pointer>(__rt);
2270       __rt     = static_cast<__node_pointer>(__rt->__left_);
2271     } else if (value_comp()(__rt->__value_, __k))
2272       __rt = static_cast<__node_pointer>(__rt->__right_);
2273     else
2274       return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
2275                  __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2276   }
2277   return _Pp(const_iterator(__result), const_iterator(__result));
2280 template <class _Tp, class _Compare, class _Allocator>
2281 typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2282 __tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT {
2283   __node_pointer __np = __p.__get_np();
2284   if (__begin_node() == __p.__ptr_) {
2285     if (__np->__right_ != nullptr)
2286       __begin_node() = static_cast<__iter_pointer>(__np->__right_);
2287     else
2288       __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
2289   }
2290   --size();
2291   std::__tree_remove(__end_node()->__left_, static_cast<__node_base_pointer>(__np));
2292   return __node_holder(__np, _Dp(__node_alloc(), true));
2295 template <class _Tp, class _Compare, class _Allocator>
2296 inline _LIBCPP_HIDE_FROM_ABI void swap(__tree<_Tp, _Compare, _Allocator>& __x, __tree<_Tp, _Compare, _Allocator>& __y)
2297     _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2298   __x.swap(__y);
2301 _LIBCPP_END_NAMESPACE_STD
2303 _LIBCPP_POP_MACROS
2305 #endif // _LIBCPP___TREE