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