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 static_cast<_EndNodePtr>(std::__tree_min(__x->__right_));
210 while (!std::__tree_is_left_child(__x))
211 __x = __x->__parent_unsafe();
212 return static_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 = static_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 static_cast<pointer>(__parent_); }
577
578 _LIBCPP_HIDE_FROM_ABI void __set_parent(pointer __p) { __parent_ = static_cast<__end_node_pointer>(__p); }
579
580 _LIBCPP_HIDE_FROM_ABI __tree_node_base() = default;
581 __tree_node_base(__tree_node_base const&) = delete;
582 __tree_node_base& operator=(__tree_node_base const&) = delete;
583};
584
585template <class _Tp, class _VoidPtr>
586class __tree_node : public __tree_node_base<_VoidPtr> {
587public:
588 using __node_value_type _LIBCPP_NODEBUG = __get_node_value_type_t<_Tp>;
589
590// We use a union to avoid initialization during member initialization, which allows us
591// to use the allocator from the container to construct the `__node_value_type` in the
592// memory provided by the union member
593#ifndef _LIBCPP_CXX03_LANG
594
595private:
596 union {
597 __node_value_type __value_;
598 };
599
600public:
601 _LIBCPP_HIDE_FROM_ABI __node_value_type& __get_value() { return __value_; }
602#else
603
604private:
605 _ALIGNAS_TYPE(__node_value_type) unsigned char __buffer_[sizeof(__node_value_type)];
606
607public:
608 _LIBCPP_HIDE_FROM_ABI __node_value_type& __get_value() { return *reinterpret_cast<__node_value_type*>(__buffer_); }
609#endif
610
611 template <class _Alloc, class... _Args>
612 _LIBCPP_HIDE_FROM_ABI explicit __tree_node(_Alloc& __na, _Args&&... __args) {
613 allocator_traits<_Alloc>::construct(__na, std::addressof(__get_value()), std::forward<_Args>(__args)...);
614 }
615 ~__tree_node() = delete;
616 __tree_node(__tree_node const&) = delete;
617 __tree_node& operator=(__tree_node const&) = delete;
618};
619
620template <class _Allocator>
621class __tree_node_destructor {
622 using allocator_type = _Allocator;
623 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
624
625public:
626 using pointer = typename __alloc_traits::pointer;
627
628private:
629 allocator_type& __na_;
630
631public:
632 bool __value_constructed;
633
634 _LIBCPP_HIDE_FROM_ABI __tree_node_destructor(const __tree_node_destructor&) = default;
635 __tree_node_destructor& operator=(const __tree_node_destructor&) = delete;
636
637 _LIBCPP_HIDE_FROM_ABI explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
638 : __na_(__na),
639 __value_constructed(__val) {}
640
641 _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) _NOEXCEPT {
642 if (__value_constructed)
643 __alloc_traits::destroy(__na_, std::addressof(__p->__get_value()));
644 if (__p)
645 __alloc_traits::deallocate(__na_, __p, 1);
646 }
647
648 template <class>
649 friend class __map_node_destructor;
650};
651
652#if _LIBCPP_STD_VER >= 17
653template <class _NodeType, class _Alloc>
654struct __generic_container_node_destructor;
655template <class _Tp, class _VoidPtr, class _Alloc>
656struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc> : __tree_node_destructor<_Alloc> {
657 using __tree_node_destructor<_Alloc>::__tree_node_destructor;
658};
659#endif
660
661// Do an in-order traversal of the tree until `__break` returns true. Takes the root node of the tree.
662template <class _Reference, class _Break, class _NodePtr, class _Func, class _Proj>
663#ifndef _LIBCPP_COMPILER_GCC // This function is recursive, so GCC complains about always_inline.
664_LIBCPP_HIDE_FROM_ABI
665#endif
666bool __tree_iterate_from_root(_Break __break, _NodePtr __root, _Func& __func, _Proj& __proj) {
667 if (__root->__left_) {
668 if (std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__left_), __func, __proj))
669 return true;
670 }
671 if (__break(__root))
672 return true;
673 std::__invoke(__func, std::__invoke(__proj, static_cast<_Reference>(__root->__get_value())));
674 if (__root->__right_)
675 return std::__tree_iterate_from_root<_Reference>(__break, static_cast<_NodePtr>(__root->__right_), __func, __proj);
676 return false;
677}
678
679// Do an in-order traversal of the tree from __first to __last.
680template <class _NodeIter, class _Func, class _Proj>
681_LIBCPP_HIDE_FROM_ABI void
682__tree_iterate_subrange(_NodeIter __first_it, _NodeIter __last_it, _Func& __func, _Proj& __proj) {
683 using _NodePtr = typename _NodeIter::__node_pointer;
684 using _Reference = typename _NodeIter::reference;
685
686 auto __first = __first_it.__ptr_;
687 auto __last = __last_it.__ptr_;
688
689 while (true) {
690 if (__first == __last)
691 return;
692 const auto __nfirst = static_cast<_NodePtr>(__first);
693 std::__invoke(__func, std::__invoke(__proj, static_cast<_Reference>(__nfirst->__get_value())));
694 if (__nfirst->__right_) {
695 if (std::__tree_iterate_from_root<_Reference>(
696 [&](_NodePtr __node) -> bool { return __node == __last; },
697 static_cast<_NodePtr>(__nfirst->__right_),
698 __func,
699 __proj))
700 return;
701 }
702 while (!std::__tree_is_left_child(static_cast<_NodePtr>(__first)))
703 __first = static_cast<_NodePtr>(__first)->__parent_;
704 __first = static_cast<_NodePtr>(__first)->__parent_;
705 }
706}
707
708template <class _Tp, class _NodePtr, class _DiffType>
709class __tree_iterator {
710 using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>;
711 // NOLINTNEXTLINE(libcpp-nodebug-on-aliases) lldb relies on this alias for pretty printing
712 using __node_pointer = _NodePtr;
713 using __node_base_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__node_base_pointer;
714 using __end_node_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__end_node_pointer;
715
716 __end_node_pointer __ptr_;
717
718public:
719 using iterator_category = bidirectional_iterator_tag;
720 using value_type = __get_node_value_type_t<_Tp>;
721 using difference_type = _DiffType;
722 using reference = value_type&;
723 using pointer = __rebind_pointer_t<_NodePtr, value_type>;
724
725 _LIBCPP_HIDE_FROM_ABI __tree_iterator() _NOEXCEPT : __ptr_(nullptr) {}
726
727 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__get_value(); }
728 _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
729 return pointer_traits<pointer>::pointer_to(__get_np()->__get_value());
730 }
731
732 _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator++() {
733 __ptr_ = std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_));
734 return *this;
735 }
736 _LIBCPP_HIDE_FROM_ABI __tree_iterator operator++(int) {
737 __tree_iterator __t(*this);
738 ++(*this);
739 return __t;
740 }
741
742 _LIBCPP_HIDE_FROM_ABI __tree_iterator& operator--() {
743 __ptr_ = static_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_));
744 return *this;
745 }
746 _LIBCPP_HIDE_FROM_ABI __tree_iterator operator--(int) {
747 __tree_iterator __t(*this);
748 --(*this);
749 return __t;
750 }
751
752 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __tree_iterator& __x, const __tree_iterator& __y) {
753 return __x.__ptr_ == __y.__ptr_;
754 }
755 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y) {
756 return !(__x == __y);
757 }
758
759private:
760 _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
761 _LIBCPP_HIDE_FROM_ABI explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
762 _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
763 template <class, class, class>
764 friend class __tree;
765 template <class, class, class>
766 friend class __tree_const_iterator;
767
768 template <class _NodeIter, class _Func, class _Proj>
769 friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&);
770};
771
772#ifndef _LIBCPP_CXX03_LANG
773// This also handles {multi,}set::iterator, since they're just aliases to __tree::iterator
774template <class _Tp, class _NodePtr, class _DiffType>
775struct __specialized_algorithm<
776 _Algorithm::__for_each,
777 __iterator_pair<__tree_iterator<_Tp, _NodePtr, _DiffType>, __tree_iterator<_Tp, _NodePtr, _DiffType>>> {
778 static const bool __has_algorithm = true;
779
780 using __iterator _LIBCPP_NODEBUG = __tree_iterator<_Tp, _NodePtr, _DiffType>;
781
782 template <class _Func, class _Proj>
783 _LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) {
784 std::__tree_iterate_subrange(__first, __last, __func, __proj);
785 }
786};
787#endif
788
789template <class _Tp, class _NodePtr, class _DiffType>
790class __tree_const_iterator {
791 using _NodeTypes _LIBCPP_NODEBUG = __tree_node_types<_NodePtr>;
792 // NOLINTNEXTLINE(libcpp-nodebug-on-aliases) lldb relies on this alias for pretty printing
793 using __node_pointer = _NodePtr;
794 using __node_base_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__node_base_pointer;
795 using __end_node_pointer _LIBCPP_NODEBUG = typename _NodeTypes::__end_node_pointer;
796
797 __end_node_pointer __ptr_;
798
799public:
800 using iterator_category = bidirectional_iterator_tag;
801 using value_type = __get_node_value_type_t<_Tp>;
802 using difference_type = _DiffType;
803 using reference = const value_type&;
804 using pointer = __rebind_pointer_t<_NodePtr, const value_type>;
805 using __non_const_iterator _LIBCPP_NODEBUG = __tree_iterator<_Tp, __node_pointer, difference_type>;
806
807 _LIBCPP_HIDE_FROM_ABI __tree_const_iterator() _NOEXCEPT : __ptr_(nullptr) {}
808
809 _LIBCPP_HIDE_FROM_ABI __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT : __ptr_(__p.__ptr_) {}
810
811 _LIBCPP_HIDE_FROM_ABI reference operator*() const { return __get_np()->__get_value(); }
812 _LIBCPP_HIDE_FROM_ABI pointer operator->() const {
813 return pointer_traits<pointer>::pointer_to(__get_np()->__get_value());
814 }
815
816 _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator++() {
817 __ptr_ = std::__tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_));
818 return *this;
819 }
820
821 _LIBCPP_HIDE_FROM_ABI __tree_const_iterator operator++(int) {
822 __tree_const_iterator __t(*this);
823 ++(*this);
824 return __t;
825 }
826
827 _LIBCPP_HIDE_FROM_ABI __tree_const_iterator& operator--() {
828 __ptr_ = static_cast<__end_node_pointer>(std::__tree_prev_iter<__node_base_pointer>(__ptr_));
829 return *this;
830 }
831
832 _LIBCPP_HIDE_FROM_ABI __tree_const_iterator operator--(int) {
833 __tree_const_iterator __t(*this);
834 --(*this);
835 return __t;
836 }
837
838 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y) {
839 return __x.__ptr_ == __y.__ptr_;
840 }
841 friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y) {
842 return !(__x == __y);
843 }
844
845private:
846 _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
847 _LIBCPP_HIDE_FROM_ABI explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
848 _LIBCPP_HIDE_FROM_ABI __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
849
850 template <class, class, class>
851 friend class __tree;
852
853 template <class _NodeIter, class _Func, class _Proj>
854 friend void __tree_iterate_subrange(_NodeIter, _NodeIter, _Func&, _Proj&);
855};
856
857#ifndef _LIBCPP_CXX03_LANG
858// This also handles {multi,}set::const_iterator, since they're just aliases to __tree::iterator
859template <class _Tp, class _NodePtr, class _DiffType>
860struct __specialized_algorithm<
861 _Algorithm::__for_each,
862 __iterator_pair<__tree_const_iterator<_Tp, _NodePtr, _DiffType>, __tree_const_iterator<_Tp, _NodePtr, _DiffType>>> {
863 static const bool __has_algorithm = true;
864
865 using __iterator _LIBCPP_NODEBUG = __tree_const_iterator<_Tp, _NodePtr, _DiffType>;
866
867 template <class _Func, class _Proj>
868 _LIBCPP_HIDE_FROM_ABI static void operator()(__iterator __first, __iterator __last, _Func& __func, _Proj& __proj) {
869 std::__tree_iterate_subrange(__first, __last, __func, __proj);
870 }
871};
872#endif
873
874template <class _Tp, class _Compare>
875#ifndef _LIBCPP_CXX03_LANG
876_LIBCPP_DIAGNOSE_WARNING(!__is_invocable_v<_Compare const&, _Tp const&, _Tp const&>,
877 "the specified comparator type does not provide a viable const call operator")
878#endif
879int __diagnose_non_const_comparator();
880
881template <class _Tp, class _Compare, class _Allocator>
882class __tree {
883public:
884 using value_type = __get_node_value_type_t<_Tp>;
885 using value_compare = _Compare;
886 using allocator_type = _Allocator;
887
888private:
889 using __alloc_traits _LIBCPP_NODEBUG = allocator_traits<allocator_type>;
890 using key_type = __get_tree_key_type_t<_Tp>;
891
892public:
893 using pointer = typename __alloc_traits::pointer;
894 using const_pointer = typename __alloc_traits::const_pointer;
895 using size_type = typename __alloc_traits::size_type;
896 using difference_type = typename __alloc_traits::difference_type;
897
898 using __void_pointer _LIBCPP_NODEBUG = typename __alloc_traits::void_pointer;
899
900 using __node _LIBCPP_NODEBUG = __tree_node<_Tp, __void_pointer>;
901 // NOLINTNEXTLINE(libcpp-nodebug-on-aliases) lldb relies on this alias for pretty printing
902 using __node_pointer = __rebind_pointer_t<__void_pointer, __node>;
903
904 using __node_base _LIBCPP_NODEBUG = __tree_node_base<__void_pointer>;
905 using __node_base_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<__void_pointer, __node_base>;
906
907 using __end_node_t _LIBCPP_NODEBUG = __tree_end_node<__node_base_pointer>;
908 using __end_node_pointer _LIBCPP_NODEBUG = __rebind_pointer_t<__void_pointer, __end_node_t>;
909
910 using __node_allocator _LIBCPP_NODEBUG = __rebind_alloc<__alloc_traits, __node>;
911 using __node_traits _LIBCPP_NODEBUG = allocator_traits<__node_allocator>;
912
913private:
914 // check for sane allocator pointer rebinding semantics. Rebinding the
915 // allocator for a new pointer type should be exactly the same as rebinding
916 // the pointer using 'pointer_traits'.
917 static_assert(is_same<__node_pointer, typename __node_traits::pointer>::value,
918 "Allocator does not rebind pointers in a sane manner.");
919 using __node_base_allocator _LIBCPP_NODEBUG = __rebind_alloc<__node_traits, __node_base>;
920 using __node_base_traits _LIBCPP_NODEBUG = allocator_traits<__node_base_allocator>;
921 static_assert(is_same<__node_base_pointer, typename __node_base_traits::pointer>::value,
922 "Allocator does not rebind pointers in a sane manner.");
923
924private:
925 __end_node_pointer __begin_node_;
926 _LIBCPP_COMPRESSED_PAIR(__end_node_t, __end_node_, __node_allocator, __node_alloc_);
927 _LIBCPP_COMPRESSED_PAIR(size_type, __size_, value_compare, __value_comp_);
928
929public:
930 _LIBCPP_HIDE_FROM_ABI __end_node_pointer __end_node() _NOEXCEPT {
931 return pointer_traits<__end_node_pointer>::pointer_to(__end_node_);
932 }
933 _LIBCPP_HIDE_FROM_ABI __end_node_pointer __end_node() const _NOEXCEPT {
934 return pointer_traits<__end_node_pointer>::pointer_to(const_cast<__end_node_t&>(__end_node_));
935 }
936 _LIBCPP_HIDE_FROM_ABI __node_allocator& __node_alloc() _NOEXCEPT { return __node_alloc_; }
937
938private:
939 _LIBCPP_HIDE_FROM_ABI const __node_allocator& __node_alloc() const _NOEXCEPT { return __node_alloc_; }
940
941public:
942 _LIBCPP_HIDE_FROM_ABI allocator_type __alloc() const _NOEXCEPT { return allocator_type(__node_alloc()); }
943
944 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size_; }
945 _LIBCPP_HIDE_FROM_ABI value_compare& value_comp() _NOEXCEPT { return __value_comp_; }
946 _LIBCPP_HIDE_FROM_ABI const value_compare& value_comp() const _NOEXCEPT { return __value_comp_; }
947
948public:
949 _LIBCPP_HIDE_FROM_ABI __node_pointer __root() const _NOEXCEPT {
950 return static_cast<__node_pointer>(__end_node()->__left_);
951 }
952
953 _LIBCPP_HIDE_FROM_ABI __node_base_pointer* __root_ptr() const _NOEXCEPT {
954 return std::addressof(__end_node()->__left_);
955 }
956
957 using iterator = __tree_iterator<_Tp, __node_pointer, difference_type>;
958 using const_iterator = __tree_const_iterator<_Tp, __node_pointer, difference_type>;
959
960 _LIBCPP_HIDE_FROM_ABI explicit __tree(const value_compare& __comp) _NOEXCEPT_(
961 is_nothrow_default_constructible<__node_allocator>::value&& is_nothrow_copy_constructible<value_compare>::value)
962 : __size_(0), __value_comp_(__comp) {
963 __begin_node_ = __end_node();
964 }
965
966 _LIBCPP_HIDE_FROM_ABI explicit __tree(const allocator_type& __a)
967 : __begin_node_(), __node_alloc_(__node_allocator(__a)), __size_(0) {
968 __begin_node_ = __end_node();
969 }
970
971 _LIBCPP_HIDE_FROM_ABI __tree(const value_compare& __comp, const allocator_type& __a)
972 : __begin_node_(), __node_alloc_(__node_allocator(__a)), __size_(0), __value_comp_(__comp) {
973 __begin_node_ = __end_node();
974 }
975
976 _LIBCPP_HIDE_FROM_ABI __tree(const __tree& __t);
977
978 _LIBCPP_HIDE_FROM_ABI __tree(const __tree& __other, const allocator_type& __alloc)
979 : __begin_node_(__end_node()), __node_alloc_(__alloc), __size_(0), __value_comp_(__other.value_comp()) {
980 if (__other.size() == 0)
981 return;
982
983 *__root_ptr() = static_cast<__node_base_pointer>(__copy_construct_tree(__other.__root()));
984 __root()->__parent_ = __end_node();
985 __begin_node_ = static_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_));
986 __size_ = __other.size();
987 }
988
989 _LIBCPP_HIDE_FROM_ABI __tree& operator=(const __tree& __t);
990 template <class _ForwardIterator>
991 _LIBCPP_HIDE_FROM_ABI void __assign_unique(_ForwardIterator __first, _ForwardIterator __last);
992 _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t) _NOEXCEPT_(
993 is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<value_compare>::value);
994 _LIBCPP_HIDE_FROM_ABI __tree(__tree&& __t, const allocator_type& __a);
995
996 _LIBCPP_HIDE_FROM_ABI __tree& operator=(__tree&& __t)
997 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
998 ((__node_traits::propagate_on_container_move_assignment::value &&
999 is_nothrow_move_assignable<__node_allocator>::value) ||
1000 allocator_traits<__node_allocator>::is_always_equal::value)) {
1001 __move_assign(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
1002 return *this;
1003 }
1004
1005 _LIBCPP_HIDE_FROM_ABI ~__tree() {
1006 static_assert(is_copy_constructible<value_compare>::value, "Comparator must be copy-constructible.");
1007 destroy(__root());
1008 }
1009
1010 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT { return iterator(__begin_node_); }
1011 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return const_iterator(__begin_node_); }
1012 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT { return iterator(__end_node()); }
1013 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return const_iterator(__end_node()); }
1014
1015 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
1016 return std::min<size_type>(__node_traits::max_size(__node_alloc()), numeric_limits<difference_type >::max());
1017 }
1018
1019 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT;
1020
1021 _LIBCPP_HIDE_FROM_ABI void swap(__tree& __t)
1022#if _LIBCPP_STD_VER <= 11
1023 _NOEXCEPT_(__is_nothrow_swappable_v<value_compare> &&
1024 (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>));
1025#else
1026 _NOEXCEPT_(__is_nothrow_swappable_v<value_compare>);
1027#endif
1028
1029 template <class... _Args>
1030 _LIBCPP_HIDE_FROM_ABI iterator __emplace_multi(_Args&&... __args);
1031
1032 template <class... _Args>
1033 _LIBCPP_HIDE_FROM_ABI iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1034
1035 template <class... _Args>
1036 _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1037 return std::__try_key_extraction<key_type>(
1038 [this](const key_type& __key, _Args&&... __args2) {
1039 auto [__parent, __child] = __find_equal(__key);
1040 __node_pointer __r = static_cast<__node_pointer>(__child);
1041 bool __inserted = false;
1042 if (__child == nullptr) {
1043 __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
1044 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1045 __r = __h.release();
1046 __inserted = true;
1047 }
1048 return pair<iterator, bool>(iterator(__r), __inserted);
1049 },
1050 [this](_Args&&... __args2) {
1051 __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
1052 auto [__parent, __child] = __find_equal(__h->__get_value());
1053 __node_pointer __r = static_cast<__node_pointer>(__child);
1054 bool __inserted = false;
1055 if (__child == nullptr) {
1056 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1057 __r = __h.release();
1058 __inserted = true;
1059 }
1060 return pair<iterator, bool>(iterator(__r), __inserted);
1061 },
1062 std::forward<_Args>(__args)...);
1063 }
1064
1065 template <class... _Args>
1066 _LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1067 return std::__try_key_extraction<key_type>(
1068 [this, __p](const key_type& __key, _Args&&... __args2) {
1069 __node_base_pointer __dummy;
1070 auto [__parent, __child] = __find_equal(__p, __dummy, __key);
1071 __node_pointer __r = static_cast<__node_pointer>(__child);
1072 bool __inserted = false;
1073 if (__child == nullptr) {
1074 __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
1075 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1076 __r = __h.release();
1077 __inserted = true;
1078 }
1079 return pair<iterator, bool>(iterator(__r), __inserted);
1080 },
1081 [this, __p](_Args&&... __args2) {
1082 __node_holder __h = __construct_node(std::forward<_Args>(__args2)...);
1083 __node_base_pointer __dummy;
1084 auto [__parent, __child] = __find_equal(__p, __dummy, __h->__get_value());
1085 __node_pointer __r = static_cast<__node_pointer>(__child);
1086 if (__child == nullptr) {
1087 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1088 __r = __h.release();
1089 }
1090 return pair<iterator, bool>(iterator(__r), __child == nullptr);
1091 },
1092 std::forward<_Args>(__args)...);
1093 }
1094
1095 template <class _InIter, class _Sent>
1096 _LIBCPP_HIDE_FROM_ABI void __insert_range_multi(_InIter __first, _Sent __last) {
1097 if (__first == __last)
1098 return;
1099
1100 if (__root() == nullptr) { // Make sure we always have a root node
1101 __insert_node_at(
1102 __end_node(), __end_node()->__left_, static_cast<__node_base_pointer>(__construct_node(*__first).release()));
1103 ++__first;
1104 }
1105
1106 auto __max_node = static_cast<__node_pointer>(std::__tree_max(static_cast<__node_base_pointer>(__root())));
1107
1108 for (; __first != __last; ++__first) {
1109 __node_holder __nd = __construct_node(*__first);
1110 // Always check the max node first. This optimizes for sorted ranges inserted at the end.
1111 if (!value_comp()(__nd->__get_value(), __max_node->__get_value())) { // __node >= __max_val
1112 __insert_node_at(static_cast<__end_node_pointer>(__max_node),
1113 __max_node->__right_,
1114 static_cast<__node_base_pointer>(__nd.get()));
1115 __max_node = __nd.release();
1116 } else {
1117 __end_node_pointer __parent;
1118 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__get_value());
1119 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd.release()));
1120 }
1121 }
1122 }
1123
1124 template <class _InIter, class _Sent>
1125 _LIBCPP_HIDE_FROM_ABI void __insert_range_unique(_InIter __first, _Sent __last) {
1126 if (__first == __last)
1127 return;
1128
1129 if (__root() == nullptr) {
1130 __insert_node_at(
1131 __end_node(), __end_node()->__left_, static_cast<__node_base_pointer>(__construct_node(*__first).release()));
1132 ++__first;
1133 }
1134
1135 auto __max_node = static_cast<__node_pointer>(std::__tree_max(static_cast<__node_base_pointer>(__root())));
1136
1137 using __reference = decltype(*__first);
1138
1139 for (; __first != __last; ++__first) {
1140 std::__try_key_extraction<key_type>(
1141 [this, &__max_node](const key_type& __key, __reference&& __val) {
1142 if (value_comp()(__max_node->__get_value(), __key)) { // __key > __max_node
1143 __node_holder __nd = __construct_node(std::forward<__reference>(__val));
1144 __insert_node_at(static_cast<__end_node_pointer>(__max_node),
1145 __max_node->__right_,
1146 static_cast<__node_base_pointer>(__nd.get()));
1147 __max_node = __nd.release();
1148 } else {
1149 auto [__parent, __child] = __find_equal(__key);
1150 if (__child == nullptr) {
1151 __node_holder __nd = __construct_node(std::forward<__reference>(__val));
1152 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd.release()));
1153 }
1154 }
1155 },
1156 [this, &__max_node](__reference&& __val) {
1157 __node_holder __nd = __construct_node(std::forward<__reference>(__val));
1158 if (value_comp()(__max_node->__get_value(), __nd->__get_value())) { // __node > __max_node
1159 __insert_node_at(static_cast<__end_node_pointer>(__max_node),
1160 __max_node->__right_,
1161 static_cast<__node_base_pointer>(__nd.get()));
1162 __max_node = __nd.release();
1163 } else {
1164 auto [__parent, __child] = __find_equal(__nd->__get_value());
1165 if (__child == nullptr) {
1166 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd.release()));
1167 }
1168 }
1169 },
1170 *__first);
1171 }
1172 }
1173
1174 _LIBCPP_HIDE_FROM_ABI iterator __remove_node_pointer(__node_pointer) _NOEXCEPT;
1175
1176#if _LIBCPP_STD_VER >= 17
1177 template <class _NodeHandle, class _InsertReturnType>
1178 _LIBCPP_HIDE_FROM_ABI _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1179 template <class _NodeHandle>
1180 _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
1181 template <class _Comp2>
1182 _LIBCPP_HIDE_FROM_ABI void __node_handle_merge_unique(__tree<_Tp, _Comp2, _Allocator>& __source);
1183
1184 template <class _NodeHandle>
1185 _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(_NodeHandle&&);
1186 template <class _NodeHandle>
1187 _LIBCPP_HIDE_FROM_ABI iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
1188 template <class _Comp2>
1189 _LIBCPP_HIDE_FROM_ABI void __node_handle_merge_multi(__tree<_Tp, _Comp2, _Allocator>& __source);
1190
1191 template <class _NodeHandle>
1192 _LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(key_type const&);
1193 template <class _NodeHandle>
1194 _LIBCPP_HIDE_FROM_ABI _NodeHandle __node_handle_extract(const_iterator);
1195#endif
1196
1197 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __p);
1198 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __f, const_iterator __l);
1199 template <class _Key>
1200 _LIBCPP_HIDE_FROM_ABI size_type __erase_unique(const _Key& __k);
1201 template <class _Key>
1202 _LIBCPP_HIDE_FROM_ABI size_type __erase_multi(const _Key& __k);
1203
1204 _LIBCPP_HIDE_FROM_ABI void
1205 __insert_node_at(__end_node_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT;
1206
1207 template <class _Key>
1208 _LIBCPP_HIDE_FROM_ABI iterator find(const _Key& __key) {
1209 auto [__, __match] = __find_equal(__key);
1210 if (__match == nullptr)
1211 return end();
1212 return iterator(static_cast<__node_pointer>(__match));
1213 }
1214
1215 template <class _Key>
1216 _LIBCPP_HIDE_FROM_ABI const_iterator find(const _Key& __key) const {
1217 auto [__, __match] = __find_equal(__key);
1218 if (__match == nullptr)
1219 return end();
1220 return const_iterator(static_cast<__node_pointer>(__match));
1221 }
1222
1223 template <class _Key>
1224 _LIBCPP_HIDE_FROM_ABI size_type __count_unique(const _Key& __k) const;
1225 template <class _Key>
1226 _LIBCPP_HIDE_FROM_ABI size_type __count_multi(const _Key& __k) const;
1227
1228 template <bool _LowerBound, class _Key>
1229 _LIBCPP_HIDE_FROM_ABI __end_node_pointer __lower_upper_bound_unique_impl(const _Key& __v) const {
1230 auto __rt = __root();
1231 auto __result = __end_node();
1232 auto __comp = __lazy_synth_three_way_comparator<_Compare, _Key, value_type>(value_comp());
1233 while (__rt != nullptr) {
1234 auto __comp_res = __comp(__v, __rt->__get_value());
1235
1236 if (__comp_res.__less()) {
1237 __result = static_cast<__end_node_pointer>(__rt);
1238 __rt = static_cast<__node_pointer>(__rt->__left_);
1239 } else if (__comp_res.__greater()) {
1240 __rt = static_cast<__node_pointer>(__rt->__right_);
1241 } else if _LIBCPP_CONSTEXPR (_LowerBound) {
1242 return static_cast<__end_node_pointer>(__rt);
1243 } else {
1244 return __rt->__right_ ? static_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) : __result;
1245 }
1246 }
1247 return __result;
1248 }
1249
1250 // Compatibility escape hatch for comparators that are not strict weak orderings. This
1251 // can be removed for the LLVM 23 release.
1252#if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND)
1253 template <class _Key>
1254 _LIBCPP_HIDE_FROM_ABI __end_node_pointer __lower_bound_unique_compat_impl(const _Key& __v) const {
1255 auto __rt = __root();
1256 auto __result = __end_node();
1257 while (__rt != nullptr) {
1258 if (!value_comp()(__rt->__get_value(), __v)) {
1259 __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt);
1260 __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_);
1261 } else {
1262 __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_);
1263 }
1264 }
1265 return __result;
1266 }
1267
1268 template <class _Key>
1269 _LIBCPP_HIDE_FROM_ABI __end_node_pointer __upper_bound_unique_compat_impl(const _Key& __v) const {
1270 auto __rt = __root();
1271 auto __result = __end_node();
1272 while (__rt != nullptr) {
1273 if (value_comp()(__v, __rt->__get_value())) {
1274 __result = std::__static_fancy_pointer_cast<__end_node_pointer>(__rt);
1275 __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__left_);
1276 } else {
1277 __rt = std::__static_fancy_pointer_cast<__node_pointer>(__rt->__right_);
1278 }
1279 }
1280 return __result;
1281 }
1282#endif // _LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND
1283
1284 template <class _Key>
1285 _LIBCPP_HIDE_FROM_ABI iterator __lower_bound_unique(const _Key& __v) {
1286#if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND)
1287 return iterator(__lower_bound_unique_compat_impl(__v));
1288#else
1289 return iterator(__lower_upper_bound_unique_impl<true>(__v));
1290#endif
1291 }
1292
1293 template <class _Key>
1294 _LIBCPP_HIDE_FROM_ABI const_iterator __lower_bound_unique(const _Key& __v) const {
1295#if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND)
1296 return const_iterator(__lower_bound_unique_compat_impl(__v));
1297#else
1298 return const_iterator(__lower_upper_bound_unique_impl<true>(__v));
1299#endif
1300 }
1301
1302 template <class _Key>
1303 _LIBCPP_HIDE_FROM_ABI iterator __upper_bound_unique(const _Key& __v) {
1304#if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND)
1305 return iterator(__upper_bound_unique_compat_impl(__v));
1306#else
1307 return iterator(__lower_upper_bound_unique_impl<false>(__v));
1308#endif
1309 }
1310
1311 template <class _Key>
1312 _LIBCPP_HIDE_FROM_ABI const_iterator __upper_bound_unique(const _Key& __v) const {
1313#if defined(_LIBCPP_ENABLE_LEGACY_TREE_LOWER_UPPER_BOUND)
1314 return iterator(__upper_bound_unique_compat_impl(__v));
1315#else
1316 return iterator(__lower_upper_bound_unique_impl<false>(__v));
1317#endif
1318 }
1319
1320private:
1321 template <class _Key>
1322 _LIBCPP_HIDE_FROM_ABI iterator
1323 __lower_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result);
1324
1325 template <class _Key>
1326 _LIBCPP_HIDE_FROM_ABI const_iterator
1327 __lower_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result) const;
1328
1329public:
1330 template <class _Key>
1331 _LIBCPP_HIDE_FROM_ABI iterator __lower_bound_multi(const _Key& __v) {
1332 return __lower_bound_multi(__v, __root(), __end_node());
1333 }
1334 template <class _Key>
1335 _LIBCPP_HIDE_FROM_ABI const_iterator __lower_bound_multi(const _Key& __v) const {
1336 return __lower_bound_multi(__v, __root(), __end_node());
1337 }
1338
1339 template <class _Key>
1340 _LIBCPP_HIDE_FROM_ABI iterator __upper_bound_multi(const _Key& __v) {
1341 return __upper_bound_multi(__v, __root(), __end_node());
1342 }
1343
1344 template <class _Key>
1345 _LIBCPP_HIDE_FROM_ABI const_iterator __upper_bound_multi(const _Key& __v) const {
1346 return __upper_bound_multi(__v, __root(), __end_node());
1347 }
1348
1349private:
1350 template <class _Key>
1351 _LIBCPP_HIDE_FROM_ABI iterator
1352 __upper_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result);
1353
1354 template <class _Key>
1355 _LIBCPP_HIDE_FROM_ABI const_iterator
1356 __upper_bound_multi(const _Key& __v, __node_pointer __root, __end_node_pointer __result) const;
1357
1358public:
1359 template <class _Key>
1360 _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_unique(const _Key& __k);
1361 template <class _Key>
1362 _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_unique(const _Key& __k) const;
1363
1364 template <class _Key>
1365 _LIBCPP_HIDE_FROM_ABI pair<iterator, iterator> __equal_range_multi(const _Key& __k);
1366 template <class _Key>
1367 _LIBCPP_HIDE_FROM_ABI pair<const_iterator, const_iterator> __equal_range_multi(const _Key& __k) const;
1368
1369 using _Dp _LIBCPP_NODEBUG = __tree_node_destructor<__node_allocator>;
1370 using __node_holder _LIBCPP_NODEBUG = unique_ptr<__node, _Dp>;
1371
1372 _LIBCPP_HIDE_FROM_ABI __node_holder remove(const_iterator __p) _NOEXCEPT;
1373
1374 // FIXME: Make this function const qualified. Unfortunately doing so
1375 // breaks existing code which uses non-const callable comparators.
1376 template <class _Key>
1377 _LIBCPP_HIDE_FROM_ABI pair<__end_node_pointer, __node_base_pointer&> __find_equal(const _Key& __v);
1378
1379 template <class _Key>
1380 _LIBCPP_HIDE_FROM_ABI pair<__end_node_pointer, __node_base_pointer&> __find_equal(const _Key& __v) const {
1381 return const_cast<__tree*>(this)->__find_equal(__v);
1382 }
1383
1384 template <class _Key>
1385 _LIBCPP_HIDE_FROM_ABI pair<__end_node_pointer, __node_base_pointer&>
1386 __find_equal(const_iterator __hint, __node_base_pointer& __dummy, const _Key& __v);
1387
1388 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree& __t) {
1389 __copy_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_copy_assignment::value>());
1390 }
1391
1392 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree& __t, true_type) {
1393 if (__node_alloc() != __t.__node_alloc())
1394 clear();
1395 __node_alloc() = __t.__node_alloc();
1396 }
1397 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const __tree&, false_type) {}
1398
1399private:
1400 _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_low(__end_node_pointer& __parent, const value_type& __v);
1401
1402 _LIBCPP_HIDE_FROM_ABI __node_base_pointer& __find_leaf_high(__end_node_pointer& __parent, const value_type& __v);
1403
1404 _LIBCPP_HIDE_FROM_ABI __node_base_pointer&
1405 __find_leaf(const_iterator __hint, __end_node_pointer& __parent, const value_type& __v);
1406
1407 template <class... _Args>
1408 _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(_Args&&... __args);
1409
1410 // TODO: Make this _LIBCPP_HIDE_FROM_ABI
1411 _LIBCPP_HIDDEN void destroy(__node_pointer __nd) _NOEXCEPT { (__tree_deleter(__node_alloc_))(__nd); }
1412
1413 _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, false_type);
1414 _LIBCPP_HIDE_FROM_ABI void __move_assign(__tree& __t, true_type) _NOEXCEPT_(
1415 is_nothrow_move_assignable<value_compare>::value&& is_nothrow_move_assignable<__node_allocator>::value);
1416
1417 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree& __t)
1418 _NOEXCEPT_(!__node_traits::propagate_on_container_move_assignment::value ||
1419 is_nothrow_move_assignable<__node_allocator>::value) {
1420 __move_assign_alloc(__t, integral_constant<bool, __node_traits::propagate_on_container_move_assignment::value>());
1421 }
1422
1423 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree& __t, true_type)
1424 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value) {
1425 __node_alloc() = std::move(__t.__node_alloc());
1426 }
1427 _LIBCPP_HIDE_FROM_ABI void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
1428
1429 template <class _From, class _ValueT = _Tp, __enable_if_t<__is_tree_value_type_v<_ValueT>, int> = 0>
1430 _LIBCPP_HIDE_FROM_ABI static void __assign_value(__get_node_value_type_t<value_type>& __lhs, _From&& __rhs) {
1431 using __key_type = __remove_const_t<typename value_type::first_type>;
1432
1433 // This is technically UB, since the object was constructed as `const`.
1434 // Clang doesn't optimize on this currently though.
1435 const_cast<__key_type&>(__lhs.first) = const_cast<__copy_cvref_t<_From, __key_type>&&>(__rhs.first);
1436 __lhs.second = std::forward<_From>(__rhs).second;
1437 }
1438
1439 template <class _To, class _From, class _ValueT = _Tp, __enable_if_t<!__is_tree_value_type_v<_ValueT>, int> = 0>
1440 _LIBCPP_HIDE_FROM_ABI static void __assign_value(_To& __lhs, _From&& __rhs) {
1441 __lhs = std::forward<_From>(__rhs);
1442 }
1443
1444 class __tree_deleter {
1445 __node_allocator& __alloc_;
1446
1447 public:
1448 using pointer = __node_pointer;
1449
1450 _LIBCPP_HIDE_FROM_ABI __tree_deleter(__node_allocator& __alloc) : __alloc_(__alloc) {}
1451
1452#ifdef _LIBCPP_COMPILER_CLANG_BASED // FIXME: GCC complains about not being able to always_inline a recursive function
1453 _LIBCPP_HIDE_FROM_ABI
1454#endif
1455 void
1456 operator()(__node_pointer __ptr) {
1457 if (!__ptr)
1458 return;
1459
1460 (*this)(static_cast<__node_pointer>(__ptr->__left_));
1461
1462 auto __right = __ptr->__right_;
1463
1464 __node_traits::destroy(__alloc_, std::addressof(__ptr->__get_value()));
1465 __node_traits::deallocate(__alloc_, __ptr, 1);
1466
1467 (*this)(static_cast<__node_pointer>(__right));
1468 }
1469 };
1470
1471 // This copy construction will always produce a correct red-black-tree assuming the incoming tree is correct, since we
1472 // copy the exact structure 1:1. Since this is for copy construction _only_ we know that we get a correct tree. If we
1473 // didn't get a correct tree, the invariants of __tree are broken and we have a much bigger problem than an improperly
1474 // balanced tree.
1475 template <class _NodeConstructor>
1476#ifdef _LIBCPP_COMPILER_CLANG_BASED // FIXME: GCC complains about not being able to always_inline a recursive function
1477 _LIBCPP_HIDE_FROM_ABI
1478#endif
1479 __node_pointer __construct_from_tree(__node_pointer __src, _NodeConstructor __construct) {
1480 if (!__src)
1481 return nullptr;
1482
1483 __node_holder __new_node = __construct(__src->__get_value());
1484
1485 unique_ptr<__node, __tree_deleter> __left(
1486 __construct_from_tree(static_cast<__node_pointer>(__src->__left_), __construct), __node_alloc_);
1487 __node_pointer __right = __construct_from_tree(static_cast<__node_pointer>(__src->__right_), __construct);
1488
1489 __node_pointer __new_node_ptr = __new_node.release();
1490
1491 __new_node_ptr->__is_black_ = __src->__is_black_;
1492 __new_node_ptr->__left_ = static_cast<__node_base_pointer>(__left.release());
1493 __new_node_ptr->__right_ = static_cast<__node_base_pointer>(__right);
1494 if (__new_node_ptr->__left_)
1495 __new_node_ptr->__left_->__parent_ = static_cast<__end_node_pointer>(__new_node_ptr);
1496 if (__new_node_ptr->__right_)
1497 __new_node_ptr->__right_->__parent_ = static_cast<__end_node_pointer>(__new_node_ptr);
1498 return __new_node_ptr;
1499 }
1500
1501 _LIBCPP_HIDE_FROM_ABI __node_pointer __copy_construct_tree(__node_pointer __src) {
1502 return __construct_from_tree(__src, [this](const value_type& __val) { return __construct_node(__val); });
1503 }
1504
1505 template <class _ValueT = _Tp, __enable_if_t<__is_tree_value_type_v<_ValueT>, int> = 0>
1506 _LIBCPP_HIDE_FROM_ABI __node_pointer __move_construct_tree(__node_pointer __src) {
1507 return __construct_from_tree(__src, [this](value_type& __val) {
1508 return __construct_node(const_cast<key_type&&>(__val.first), std::move(__val.second));
1509 });
1510 }
1511
1512 template <class _ValueT = _Tp, __enable_if_t<!__is_tree_value_type_v<_ValueT>, int> = 0>
1513 _LIBCPP_HIDE_FROM_ABI __node_pointer __move_construct_tree(__node_pointer __src) {
1514 return __construct_from_tree(__src, [this](value_type& __val) { return __construct_node(std::move(__val)); });
1515 }
1516
1517 template <class _Assignment, class _ConstructionAlg>
1518 // This copy assignment will always produce a correct red-black-tree assuming the incoming tree is correct, since our
1519 // own tree is a red-black-tree and the incoming tree is a red-black-tree. The invariants of a red-black-tree are
1520 // temporarily not met until all of the incoming red-black tree is copied.
1521#ifdef _LIBCPP_COMPILER_CLANG_BASED // FIXME: GCC complains about not being able to always_inline a recursive function
1522 _LIBCPP_HIDE_FROM_ABI
1523#endif
1524 __node_pointer __assign_from_tree(
1525 __node_pointer __dest, __node_pointer __src, _Assignment __assign, _ConstructionAlg __construct_subtree) {
1526 if (!__src) {
1527 destroy(__dest);
1528 return nullptr;
1529 }
1530
1531 __assign(__dest->__get_value(), __src->__get_value());
1532 __dest->__is_black_ = __src->__is_black_;
1533
1534 // If we already have a left node in the destination tree, reuse it and copy-assign recursively
1535 if (__dest->__left_) {
1536 __dest->__left_ = static_cast<__node_base_pointer>(__assign_from_tree(
1537 static_cast<__node_pointer>(__dest->__left_),
1538 static_cast<__node_pointer>(__src->__left_),
1539 __assign,
1540 __construct_subtree));
1541
1542 // Otherwise, we must create new nodes; copy-construct from here on
1543 } else if (__src->__left_) {
1544 auto __new_left = __construct_subtree(static_cast<__node_pointer>(__src->__left_));
1545 __dest->__left_ = static_cast<__node_base_pointer>(__new_left);
1546 __new_left->__parent_ = static_cast<__end_node_pointer>(__dest);
1547 }
1548
1549 // Identical to the left case above, just for the right nodes
1550 if (__dest->__right_) {
1551 __dest->__right_ = static_cast<__node_base_pointer>(__assign_from_tree(
1552 static_cast<__node_pointer>(__dest->__right_),
1553 static_cast<__node_pointer>(__src->__right_),
1554 __assign,
1555 __construct_subtree));
1556 } else if (__src->__right_) {
1557 auto __new_right = __construct_subtree(static_cast<__node_pointer>(__src->__right_));
1558 __dest->__right_ = static_cast<__node_base_pointer>(__new_right);
1559 __new_right->__parent_ = static_cast<__end_node_pointer>(__dest);
1560 }
1561
1562 return __dest;
1563 }
1564
1565 _LIBCPP_HIDE_FROM_ABI __node_pointer __copy_assign_tree(__node_pointer __dest, __node_pointer __src) {
1566 return __assign_from_tree(
1567 __dest,
1568 __src,
1569 [](value_type& __lhs, const value_type& __rhs) { __assign_value(__lhs, __rhs); },
1570 [this](__node_pointer __nd) { return __copy_construct_tree(__nd); });
1571 }
1572
1573 _LIBCPP_HIDE_FROM_ABI __node_pointer __move_assign_tree(__node_pointer __dest, __node_pointer __src) {
1574 return __assign_from_tree(
1575 __dest,
1576 __src,
1577 [](value_type& __lhs, value_type& __rhs) { __assign_value(__lhs, std::move(__rhs)); },
1578 [this](__node_pointer __nd) { return __move_construct_tree(__nd); });
1579 }
1580
1581 friend struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree> >;
1582};
1583
1584#if _LIBCPP_STD_VER >= 14
1585template <class _Tp, class _Compare, class _Allocator>
1586struct __specialized_algorithm<_Algorithm::__for_each, __single_range<__tree<_Tp, _Compare, _Allocator> > > {
1587 static const bool __has_algorithm = true;
1588
1589 using __node_pointer _LIBCPP_NODEBUG = typename __tree<_Tp, _Compare, _Allocator>::__node_pointer;
1590
1591 template <class _Tree, class _Func, class _Proj>
1592 _LIBCPP_HIDE_FROM_ABI static auto operator()(_Tree&& __range, _Func __func, _Proj __proj) {
1593 if (__range.size() != 0)
1594 std::__tree_iterate_from_root<__copy_cvref_t<_Tree, typename __remove_cvref_t<_Tree>::value_type>>(
1595 [](__node_pointer) { return false; }, __range.__root(), __func, __proj);
1596 return std::make_pair(__range.end(), std::move(__func));
1597 }
1598};
1599#endif
1600
1601template <class _Tp, class _Compare, class _Allocator>
1602__tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) {
1603 if (this == std::addressof(__t))
1604 return *this;
1605
1606 value_comp() = __t.value_comp();
1607 __copy_assign_alloc(__t);
1608
1609 if (__size_ != 0) {
1610 *__root_ptr() = static_cast<__node_base_pointer>(__copy_assign_tree(__root(), __t.__root()));
1611 } else {
1612 *__root_ptr() = static_cast<__node_base_pointer>(__copy_construct_tree(__t.__root()));
1613 if (__root())
1614 __root()->__parent_ = __end_node();
1615 }
1616 __begin_node_ =
1617 __end_node()->__left_ ? static_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)) : __end_node();
1618 __size_ = __t.size();
1619
1620 return *this;
1621}
1622
1623template <class _Tp, class _Compare, class _Allocator>
1624__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1625 : __begin_node_(__end_node()),
1626 __node_alloc_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1627 __size_(0),
1628 __value_comp_(__t.value_comp()) {
1629 if (__t.size() == 0)
1630 return;
1631
1632 *__root_ptr() = static_cast<__node_base_pointer>(__copy_construct_tree(__t.__root()));
1633 __root()->__parent_ = __end_node();
1634 __begin_node_ = static_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_));
1635 __size_ = __t.size();
1636}
1637
1638template <class _Tp, class _Compare, class _Allocator>
1639__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t) _NOEXCEPT_(
1640 is_nothrow_move_constructible<__node_allocator>::value&& is_nothrow_move_constructible<value_compare>::value)
1641 : __begin_node_(std::move(__t.__begin_node_)),
1642 __end_node_(std::move(__t.__end_node_)),
1643 __node_alloc_(std::move(__t.__node_alloc_)),
1644 __size_(__t.__size_),
1645 __value_comp_(std::move(__t.__value_comp_)) {
1646 if (__size_ == 0)
1647 __begin_node_ = __end_node();
1648 else {
1649 __end_node()->__left_->__parent_ = static_cast<__end_node_pointer>(__end_node());
1650 __t.__begin_node_ = __t.__end_node();
1651 __t.__end_node()->__left_ = nullptr;
1652 __t.__size_ = 0;
1653 }
1654}
1655
1656template <class _Tp, class _Compare, class _Allocator>
1657__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1658 : __begin_node_(__end_node()),
1659 __node_alloc_(__node_allocator(__a)),
1660 __size_(0),
1661 __value_comp_(std::move(__t.value_comp())) {
1662 if (__t.size() == 0)
1663 return;
1664 if (__a == __t.__alloc()) {
1665 __begin_node_ = __t.__begin_node_;
1666 __end_node()->__left_ = __t.__end_node()->__left_;
1667 __end_node()->__left_->__parent_ = static_cast<__end_node_pointer>(__end_node());
1668 __size_ = __t.__size_;
1669 __t.__begin_node_ = __t.__end_node();
1670 __t.__end_node()->__left_ = nullptr;
1671 __t.__size_ = 0;
1672 } else {
1673 *__root_ptr() = static_cast<__node_base_pointer>(__move_construct_tree(__t.__root()));
1674 __root()->__parent_ = __end_node();
1675 __begin_node_ = static_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_));
1676 __size_ = __t.size();
1677 __t.clear(); // Ensure that __t is in a valid state after moving out the keys
1678 }
1679}
1680
1681template <class _Tp, class _Compare, class _Allocator>
1682void __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1683 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value&& is_nothrow_move_assignable<__node_allocator>::value) {
1684 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1685 __begin_node_ = __t.__begin_node_;
1686 __end_node_ = __t.__end_node_;
1687 __move_assign_alloc(__t);
1688 __size_ = __t.__size_;
1689 __value_comp_ = std::move(__t.__value_comp_);
1690 if (__size_ == 0)
1691 __begin_node_ = __end_node();
1692 else {
1693 __end_node()->__left_->__parent_ = static_cast<__end_node_pointer>(__end_node());
1694 __t.__begin_node_ = __t.__end_node();
1695 __t.__end_node()->__left_ = nullptr;
1696 __t.__size_ = 0;
1697 }
1698}
1699
1700template <class _Tp, class _Compare, class _Allocator>
1701void __tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type) {
1702 if (__node_alloc() == __t.__node_alloc()) {
1703 __move_assign(__t, true_type());
1704 } else {
1705 value_comp() = std::move(__t.value_comp());
1706 if (__size_ != 0) {
1707 *__root_ptr() = static_cast<__node_base_pointer>(__move_assign_tree(__root(), __t.__root()));
1708 } else {
1709 *__root_ptr() = static_cast<__node_base_pointer>(__move_construct_tree(__t.__root()));
1710 if (__root())
1711 __root()->__parent_ = __end_node();
1712 }
1713 __begin_node_ =
1714 __end_node()->__left_ ? static_cast<__end_node_pointer>(std::__tree_min(__end_node()->__left_)) : __end_node();
1715 __size_ = __t.size();
1716 __t.clear(); // Ensure that __t is in a valid state after moving out the keys
1717 }
1718}
1719
1720template <class _Tp, class _Compare, class _Allocator>
1721void __tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1722#if _LIBCPP_STD_VER <= 11
1723 _NOEXCEPT_(__is_nothrow_swappable_v<value_compare> &&
1724 (!__node_traits::propagate_on_container_swap::value || __is_nothrow_swappable_v<__node_allocator>))
1725#else
1726 _NOEXCEPT_(__is_nothrow_swappable_v<value_compare>)
1727#endif
1728{
1729 using std::swap;
1730 swap(__begin_node_, __t.__begin_node_);
1731 swap(__end_node_, __t.__end_node_);
1732 std::__swap_allocator(__node_alloc(), __t.__node_alloc());
1733 swap(__size_, __t.__size_);
1734 swap(__value_comp_, __t.__value_comp_);
1735 if (__size_ == 0)
1736 __begin_node_ = __end_node();
1737 else
1738 __end_node()->__left_->__parent_ = __end_node();
1739 if (__t.__size_ == 0)
1740 __t.__begin_node_ = __t.__end_node();
1741 else
1742 __t.__end_node()->__left_->__parent_ = __t.__end_node();
1743}
1744
1745template <class _Tp, class _Compare, class _Allocator>
1746void __tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT {
1747 destroy(__root());
1748 __size_ = 0;
1749 __begin_node_ = __end_node();
1750 __end_node()->__left_ = nullptr;
1751}
1752
1753// Find lower_bound place to insert
1754// Set __parent to parent of null leaf
1755// Return reference to null leaf
1756template <class _Tp, class _Compare, class _Allocator>
1757typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1758__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__end_node_pointer& __parent, const value_type& __v) {
1759 __node_pointer __nd = __root();
1760 if (__nd != nullptr) {
1761 while (true) {
1762 if (value_comp()(__nd->__get_value(), __v)) {
1763 if (__nd->__right_ != nullptr)
1764 __nd = static_cast<__node_pointer>(__nd->__right_);
1765 else {
1766 __parent = static_cast<__end_node_pointer>(__nd);
1767 return __nd->__right_;
1768 }
1769 } else {
1770 if (__nd->__left_ != nullptr)
1771 __nd = static_cast<__node_pointer>(__nd->__left_);
1772 else {
1773 __parent = static_cast<__end_node_pointer>(__nd);
1774 return __parent->__left_;
1775 }
1776 }
1777 }
1778 }
1779 __parent = __end_node();
1780 return __parent->__left_;
1781}
1782
1783// Find upper_bound place to insert
1784// Set __parent to parent of null leaf
1785// Return reference to null leaf
1786template <class _Tp, class _Compare, class _Allocator>
1787typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1788__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__end_node_pointer& __parent, const value_type& __v) {
1789 __node_pointer __nd = __root();
1790 if (__nd != nullptr) {
1791 while (true) {
1792 if (value_comp()(__v, __nd->__get_value())) {
1793 if (__nd->__left_ != nullptr)
1794 __nd = static_cast<__node_pointer>(__nd->__left_);
1795 else {
1796 __parent = static_cast<__end_node_pointer>(__nd);
1797 return __parent->__left_;
1798 }
1799 } else {
1800 if (__nd->__right_ != nullptr)
1801 __nd = static_cast<__node_pointer>(__nd->__right_);
1802 else {
1803 __parent = static_cast<__end_node_pointer>(__nd);
1804 return __nd->__right_;
1805 }
1806 }
1807 }
1808 }
1809 __parent = __end_node();
1810 return __parent->__left_;
1811}
1812
1813// Find leaf place to insert closest to __hint
1814// First check prior to __hint.
1815// Next check after __hint.
1816// Next do O(log N) search.
1817// Set __parent to parent of null leaf
1818// Return reference to null leaf
1819template <class _Tp, class _Compare, class _Allocator>
1820typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer& __tree<_Tp, _Compare, _Allocator>::__find_leaf(
1821 const_iterator __hint, __end_node_pointer& __parent, const value_type& __v) {
1822 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1823 {
1824 // __v <= *__hint
1825 const_iterator __prior = __hint;
1826 if (__prior == begin() || !value_comp()(__v, *--__prior)) {
1827 // *prev(__hint) <= __v <= *__hint
1828 if (__hint.__ptr_->__left_ == nullptr) {
1829 __parent = static_cast<__end_node_pointer>(__hint.__ptr_);
1830 return __parent->__left_;
1831 } else {
1832 __parent = static_cast<__end_node_pointer>(__prior.__ptr_);
1833 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
1834 }
1835 }
1836 // __v < *prev(__hint)
1837 return __find_leaf_high(__parent, __v);
1838 }
1839 // else __v > *__hint
1840 return __find_leaf_low(__parent, __v);
1841}
1842
1843// Find __v
1844// If __v exists, return the parent of the node of __v and a reference to the pointer to the node of __v.
1845// If __v doesn't exist, return the parent of the null leaf and a reference to the pointer to the null leaf.
1846template <class _Tp, class _Compare, class _Allocator>
1847template <class _Key>
1848_LIBCPP_HIDE_FROM_ABI pair<typename __tree<_Tp, _Compare, _Allocator>::__end_node_pointer,
1849 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&>
1850__tree<_Tp, _Compare, _Allocator>::__find_equal(const _Key& __v) {
1851 using _Pair = pair<__end_node_pointer, __node_base_pointer&>;
1852
1853 __node_pointer __nd = __root();
1854
1855 if (__nd == nullptr) {
1856 auto __end = __end_node();
1857 return _Pair(__end, __end->__left_);
1858 }
1859
1860 __node_base_pointer* __node_ptr = __root_ptr();
1861 auto&& __transparent = std::__as_transparent<_Key>(value_comp());
1862 auto __comp =
1863 __lazy_synth_three_way_comparator<__make_transparent_t<_Key, _Compare>, _Key, value_type>(__transparent);
1864
1865 while (true) {
1866 auto __comp_res = __comp(__v, __nd->__get_value());
1867
1868 if (__comp_res.__less()) {
1869 if (__nd->__left_ == nullptr)
1870 return _Pair(static_cast<__end_node_pointer>(__nd), __nd->__left_);
1871
1872 __node_ptr = std::addressof(__nd->__left_);
1873 __nd = static_cast<__node_pointer>(__nd->__left_);
1874 } else if (__comp_res.__greater()) {
1875 if (__nd->__right_ == nullptr)
1876 return _Pair(static_cast<__end_node_pointer>(__nd), __nd->__right_);
1877
1878 __node_ptr = std::addressof(__nd->__right_);
1879 __nd = static_cast<__node_pointer>(__nd->__right_);
1880 } else {
1881 return _Pair(static_cast<__end_node_pointer>(__nd), *__node_ptr);
1882 }
1883 }
1884}
1885
1886// Find __v
1887// First check prior to __hint.
1888// Next check after __hint.
1889// Next do O(log N) search.
1890// If __v exists, return the parent of the node of __v and a reference to the pointer to the node of __v.
1891// If __v doesn't exist, return the parent of the null leaf and a reference to the pointer to the null leaf.
1892template <class _Tp, class _Compare, class _Allocator>
1893template <class _Key>
1894_LIBCPP_HIDE_FROM_ABI pair<typename __tree<_Tp, _Compare, _Allocator>::__end_node_pointer,
1895 typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&>
1896__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint, __node_base_pointer& __dummy, const _Key& __v) {
1897 using _Pair = pair<__end_node_pointer, __node_base_pointer&>;
1898
1899 if (__hint == end() || value_comp()(__v, *__hint)) { // check before
1900 // __v < *__hint
1901 const_iterator __prior = __hint;
1902 if (__prior == begin() || value_comp()(*--__prior, __v)) {
1903 // *prev(__hint) < __v < *__hint
1904 if (__hint.__ptr_->__left_ == nullptr)
1905 return _Pair(__hint.__ptr_, __hint.__ptr_->__left_);
1906 return _Pair(__prior.__ptr_, static_cast<__node_pointer>(__prior.__ptr_)->__right_);
1907 }
1908 // __v <= *prev(__hint)
1909 return __find_equal(__v);
1910 }
1911
1912 if (value_comp()(*__hint, __v)) { // check after
1913 // *__hint < __v
1914 const_iterator __next = std::next(__hint);
1915 if (__next == end() || value_comp()(__v, *__next)) {
1916 // *__hint < __v < *std::next(__hint)
1917 if (__hint.__get_np()->__right_ == nullptr)
1918 return _Pair(__hint.__ptr_, static_cast<__node_pointer>(__hint.__ptr_)->__right_);
1919 return _Pair(__next.__ptr_, __next.__ptr_->__left_);
1920 }
1921 // *next(__hint) <= __v
1922 return __find_equal(__v);
1923 }
1924
1925 // else __v == *__hint
1926 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
1927 return _Pair(__hint.__ptr_, __dummy);
1928}
1929
1930template <class _Tp, class _Compare, class _Allocator>
1931void __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
1932 __end_node_pointer __parent, __node_base_pointer& __child, __node_base_pointer __new_node) _NOEXCEPT {
1933 __new_node->__left_ = nullptr;
1934 __new_node->__right_ = nullptr;
1935 __new_node->__parent_ = __parent;
1936 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
1937 __child = __new_node;
1938 if (__begin_node_->__left_ != nullptr)
1939 __begin_node_ = static_cast<__end_node_pointer>(__begin_node_->__left_);
1940 std::__tree_balance_after_insert(__end_node()->__left_, __child);
1941 ++__size_;
1942}
1943
1944template <class _Tp, class _Compare, class _Allocator>
1945template <class... _Args>
1946typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1947__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&&... __args) {
1948 __node_allocator& __na = __node_alloc();
1949 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
1950 std::__construct_at(std::addressof(*__h), __na, std::forward<_Args>(__args)...);
1951 __h.get_deleter().__value_constructed = true;
1952 return __h;
1953}
1954
1955template <class _Tp, class _Compare, class _Allocator>
1956template <class... _Args>
1957typename __tree<_Tp, _Compare, _Allocator>::iterator
1958__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args) {
1959 __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1960 __end_node_pointer __parent;
1961 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__get_value());
1962 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1963 return iterator(static_cast<__node_pointer>(__h.release()));
1964}
1965
1966template <class _Tp, class _Compare, class _Allocator>
1967template <class... _Args>
1968typename __tree<_Tp, _Compare, _Allocator>::iterator
1969__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p, _Args&&... __args) {
1970 __node_holder __h = __construct_node(std::forward<_Args>(__args)...);
1971 __end_node_pointer __parent;
1972 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__get_value());
1973 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1974 return iterator(static_cast<__node_pointer>(__h.release()));
1975}
1976
1977template <class _Tp, class _Compare, class _Allocator>
1978typename __tree<_Tp, _Compare, _Allocator>::iterator
1979__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT {
1980 iterator __r(__ptr);
1981 ++__r;
1982 if (__begin_node_ == __ptr)
1983 __begin_node_ = __r.__ptr_;
1984 --__size_;
1985 std::__tree_remove(__end_node()->__left_, static_cast<__node_base_pointer>(__ptr));
1986 return __r;
1987}
1988
1989#if _LIBCPP_STD_VER >= 17
1990template <class _Tp, class _Compare, class _Allocator>
1991template <class _NodeHandle, class _InsertReturnType>
1992_LIBCPP_HIDE_FROM_ABI _InsertReturnType
1993__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(_NodeHandle&& __nh) {
1994 if (__nh.empty())
1995 return _InsertReturnType{end(), false, _NodeHandle()};
1996
1997 __node_pointer __ptr = __nh.__ptr_;
1998 auto [__parent, __child] = __find_equal(__ptr->__get_value());
1999 if (__child != nullptr)
2000 return _InsertReturnType{iterator(static_cast<__node_pointer>(__child)), false, std::move(__nh)};
2001
2002 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2003 __nh.__release_ptr();
2004 return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
2005}
2006
2007template <class _Tp, class _Compare, class _Allocator>
2008template <class _NodeHandle>
2009_LIBCPP_HIDE_FROM_ABI typename __tree<_Tp, _Compare, _Allocator>::iterator
2010__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(const_iterator __hint, _NodeHandle&& __nh) {
2011 if (__nh.empty())
2012 return end();
2013
2014 __node_pointer __ptr = __nh.__ptr_;
2015 __node_base_pointer __dummy;
2016 auto [__parent, __child] = __find_equal(__hint, __dummy, __ptr->__get_value());
2017 __node_pointer __r = static_cast<__node_pointer>(__child);
2018 if (__child == nullptr) {
2019 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2020 __r = __ptr;
2021 __nh.__release_ptr();
2022 }
2023 return iterator(__r);
2024}
2025
2026template <class _Tp, class _Compare, class _Allocator>
2027template <class _NodeHandle>
2028_LIBCPP_HIDE_FROM_ABI _NodeHandle __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key) {
2029 iterator __it = __lower_bound_multi(__key);
2030 if (__it == end() || __value_comp_(__key, *__it))
2031 return _NodeHandle();
2032 return __node_handle_extract<_NodeHandle>(__it);
2033}
2034
2035template <class _Tp, class _Compare, class _Allocator>
2036template <class _NodeHandle>
2037_LIBCPP_HIDE_FROM_ABI _NodeHandle __tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p) {
2038 __node_pointer __np = __p.__get_np();
2039 __remove_node_pointer(__np);
2040 return _NodeHandle(__np, __alloc());
2041}
2042
2043template <class _Tp, class _Compare, class _Allocator>
2044template <class _Comp2>
2045_LIBCPP_HIDE_FROM_ABI void
2046__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(__tree<_Tp, _Comp2, _Allocator>& __source) {
2047 for (iterator __i = __source.begin(); __i != __source.end();) {
2048 __node_pointer __src_ptr = __i.__get_np();
2049 auto [__parent, __child] = __find_equal(__src_ptr->__get_value());
2050 ++__i;
2051 if (__child != nullptr)
2052 continue;
2053 __source.__remove_node_pointer(__src_ptr);
2054 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__src_ptr));
2055 }
2056}
2057
2058template <class _Tp, class _Compare, class _Allocator>
2059template <class _NodeHandle>
2060_LIBCPP_HIDE_FROM_ABI typename __tree<_Tp, _Compare, _Allocator>::iterator
2061__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh) {
2062 if (__nh.empty())
2063 return end();
2064 __node_pointer __ptr = __nh.__ptr_;
2065 __end_node_pointer __parent;
2066 __node_base_pointer& __child = __find_leaf_high(__parent, __ptr->__get_value());
2067 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2068 __nh.__release_ptr();
2069 return iterator(__ptr);
2070}
2071
2072template <class _Tp, class _Compare, class _Allocator>
2073template <class _NodeHandle>
2074_LIBCPP_HIDE_FROM_ABI typename __tree<_Tp, _Compare, _Allocator>::iterator
2075__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh) {
2076 if (__nh.empty())
2077 return end();
2078
2079 __node_pointer __ptr = __nh.__ptr_;
2080 __end_node_pointer __parent;
2081 __node_base_pointer& __child = __find_leaf(__hint, __parent, __ptr->__get_value());
2082 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2083 __nh.__release_ptr();
2084 return iterator(__ptr);
2085}
2086
2087template <class _Tp, class _Compare, class _Allocator>
2088template <class _Comp2>
2089_LIBCPP_HIDE_FROM_ABI void
2090__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(__tree<_Tp, _Comp2, _Allocator>& __source) {
2091 for (iterator __i = __source.begin(); __i != __source.end();) {
2092 __node_pointer __src_ptr = __i.__get_np();
2093 __end_node_pointer __parent;
2094 __node_base_pointer& __child = __find_leaf_high(__parent, __src_ptr->__get_value());
2095 ++__i;
2096 __source.__remove_node_pointer(__src_ptr);
2097 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__src_ptr));
2098 }
2099}
2100
2101#endif // _LIBCPP_STD_VER >= 17
2102
2103template <class _Tp, class _Compare, class _Allocator>
2104typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) {
2105 __node_pointer __np = __p.__get_np();
2106 iterator __r = __remove_node_pointer(__np);
2107 __node_allocator& __na = __node_alloc();
2108 __node_traits::destroy(__na, std::addressof(const_cast<value_type&>(*__p)));
2109 __node_traits::deallocate(__na, __np, 1);
2110 return __r;
2111}
2112
2113template <class _Tp, class _Compare, class _Allocator>
2114typename __tree<_Tp, _Compare, _Allocator>::iterator
2115__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l) {
2116 while (__f != __l)
2117 __f = erase(__f);
2118 return iterator(__l.__ptr_);
2119}
2120
2121template <class _Tp, class _Compare, class _Allocator>
2122template <class _Key>
2123typename __tree<_Tp, _Compare, _Allocator>::size_type
2124__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) {
2125 iterator __i = find(__k);
2126 if (__i == end())
2127 return 0;
2128 erase(__i);
2129 return 1;
2130}
2131
2132template <class _Tp, class _Compare, class _Allocator>
2133template <class _Key>
2134typename __tree<_Tp, _Compare, _Allocator>::size_type
2135__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k) {
2136 pair<iterator, iterator> __p = __equal_range_multi(__k);
2137 size_type __r = 0;
2138 for (; __p.first != __p.second; ++__r)
2139 __p.first = erase(__p.first);
2140 return __r;
2141}
2142
2143template <class _Tp, class _Compare, class _Allocator>
2144template <class _Key>
2145typename __tree<_Tp, _Compare, _Allocator>::size_type
2146__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const {
2147 __node_pointer __rt = __root();
2148 auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp());
2149 while (__rt != nullptr) {
2150 auto __comp_res = __comp(__k, __rt->__get_value());
2151 if (__comp_res.__less()) {
2152 __rt = static_cast<__node_pointer>(__rt->__left_);
2153 } else if (__comp_res.__greater())
2154 __rt = static_cast<__node_pointer>(__rt->__right_);
2155 else
2156 return 1;
2157 }
2158 return 0;
2159}
2160
2161template <class _Tp, class _Compare, class _Allocator>
2162template <class _Key>
2163typename __tree<_Tp, _Compare, _Allocator>::size_type
2164__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const {
2165 __end_node_pointer __result = __end_node();
2166 __node_pointer __rt = __root();
2167 auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp());
2168 while (__rt != nullptr) {
2169 auto __comp_res = __comp(__k, __rt->__get_value());
2170 if (__comp_res.__less()) {
2171 __result = static_cast<__end_node_pointer>(__rt);
2172 __rt = static_cast<__node_pointer>(__rt->__left_);
2173 } else if (__comp_res.__greater())
2174 __rt = static_cast<__node_pointer>(__rt->__right_);
2175 else
2176 return std::distance(
2177 __lower_bound_multi(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__end_node_pointer>(__rt)),
2178 __upper_bound_multi(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2179 }
2180 return 0;
2181}
2182
2183template <class _Tp, class _Compare, class _Allocator>
2184template <class _Key>
2185typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::__lower_bound_multi(
2186 const _Key& __v, __node_pointer __root, __end_node_pointer __result) {
2187 while (__root != nullptr) {
2188 if (!value_comp()(__root->__get_value(), __v)) {
2189 __result = static_cast<__end_node_pointer>(__root);
2190 __root = static_cast<__node_pointer>(__root->__left_);
2191 } else
2192 __root = static_cast<__node_pointer>(__root->__right_);
2193 }
2194 return iterator(__result);
2195}
2196
2197template <class _Tp, class _Compare, class _Allocator>
2198template <class _Key>
2199typename __tree<_Tp, _Compare, _Allocator>::const_iterator __tree<_Tp, _Compare, _Allocator>::__lower_bound_multi(
2200 const _Key& __v, __node_pointer __root, __end_node_pointer __result) const {
2201 while (__root != nullptr) {
2202 if (!value_comp()(__root->__get_value(), __v)) {
2203 __result = static_cast<__end_node_pointer>(__root);
2204 __root = static_cast<__node_pointer>(__root->__left_);
2205 } else
2206 __root = static_cast<__node_pointer>(__root->__right_);
2207 }
2208 return const_iterator(__result);
2209}
2210
2211template <class _Tp, class _Compare, class _Allocator>
2212template <class _Key>
2213typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::__upper_bound_multi(
2214 const _Key& __v, __node_pointer __root, __end_node_pointer __result) {
2215 while (__root != nullptr) {
2216 if (value_comp()(__v, __root->__get_value())) {
2217 __result = static_cast<__end_node_pointer>(__root);
2218 __root = static_cast<__node_pointer>(__root->__left_);
2219 } else
2220 __root = static_cast<__node_pointer>(__root->__right_);
2221 }
2222 return iterator(__result);
2223}
2224
2225template <class _Tp, class _Compare, class _Allocator>
2226template <class _Key>
2227typename __tree<_Tp, _Compare, _Allocator>::const_iterator __tree<_Tp, _Compare, _Allocator>::__upper_bound_multi(
2228 const _Key& __v, __node_pointer __root, __end_node_pointer __result) const {
2229 while (__root != nullptr) {
2230 if (value_comp()(__v, __root->__get_value())) {
2231 __result = static_cast<__end_node_pointer>(__root);
2232 __root = static_cast<__node_pointer>(__root->__left_);
2233 } else
2234 __root = static_cast<__node_pointer>(__root->__right_);
2235 }
2236 return const_iterator(__result);
2237}
2238
2239template <class _Tp, class _Compare, class _Allocator>
2240template <class _Key>
2241pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator>
2242__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) {
2243 using _Pp = pair<iterator, iterator>;
2244 __end_node_pointer __result = __end_node();
2245 __node_pointer __rt = __root();
2246 auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp());
2247 while (__rt != nullptr) {
2248 auto __comp_res = __comp(__k, __rt->__get_value());
2249 if (__comp_res.__less()) {
2250 __result = static_cast<__end_node_pointer>(__rt);
2251 __rt = static_cast<__node_pointer>(__rt->__left_);
2252 } else if (__comp_res.__greater())
2253 __rt = static_cast<__node_pointer>(__rt->__right_);
2254 else
2255 return _Pp(iterator(__rt),
2256 iterator(__rt->__right_ != nullptr ? static_cast<__end_node_pointer>(std::__tree_min(__rt->__right_))
2257 : __result));
2258 }
2259 return _Pp(iterator(__result), iterator(__result));
2260}
2261
2262template <class _Tp, class _Compare, class _Allocator>
2263template <class _Key>
2264pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2265 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2266__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const {
2267 using _Pp = pair<const_iterator, const_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(
2280 const_iterator(__rt),
2281 const_iterator(
2282 __rt->__right_ != nullptr ? static_cast<__end_node_pointer>(std::__tree_min(__rt->__right_)) : __result));
2283 }
2284 return _Pp(const_iterator(__result), const_iterator(__result));
2285}
2286
2287template <class _Tp, class _Compare, class _Allocator>
2288template <class _Key>
2289pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, typename __tree<_Tp, _Compare, _Allocator>::iterator>
2290__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) {
2291 using _Pp = pair<iterator, 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 = static_cast<__end_node_pointer>(__rt);
2299 __rt = static_cast<__node_pointer>(__rt->__left_);
2300 } else if (__comp_res.__greater())
2301 __rt = static_cast<__node_pointer>(__rt->__right_);
2302 else
2303 return _Pp(
2304 __lower_bound_multi(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__end_node_pointer>(__rt)),
2305 __upper_bound_multi(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2306 }
2307 return _Pp(iterator(__result), iterator(__result));
2308}
2309
2310template <class _Tp, class _Compare, class _Allocator>
2311template <class _Key>
2312pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2313 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2314__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const {
2315 using _Pp = pair<const_iterator, const_iterator>;
2316 __end_node_pointer __result = __end_node();
2317 __node_pointer __rt = __root();
2318 auto __comp = __lazy_synth_three_way_comparator<value_compare, _Key, value_type>(value_comp());
2319 while (__rt != nullptr) {
2320 auto __comp_res = __comp(__k, __rt->__get_value());
2321 if (__comp_res.__less()) {
2322 __result = static_cast<__end_node_pointer>(__rt);
2323 __rt = static_cast<__node_pointer>(__rt->__left_);
2324 } else if (__comp_res.__greater())
2325 __rt = static_cast<__node_pointer>(__rt->__right_);
2326 else
2327 return _Pp(
2328 __lower_bound_multi(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__end_node_pointer>(__rt)),
2329 __upper_bound_multi(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2330 }
2331 return _Pp(const_iterator(__result), const_iterator(__result));
2332}
2333
2334template <class _Tp, class _Compare, class _Allocator>
2335typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2336__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT {
2337 __node_pointer __np = __p.__get_np();
2338 if (__begin_node_ == __p.__ptr_) {
2339 if (__np->__right_ != nullptr)
2340 __begin_node_ = static_cast<__end_node_pointer>(__np->__right_);
2341 else
2342 __begin_node_ = static_cast<__end_node_pointer>(__np->__parent_);
2343 }
2344 --__size_;
2345 std::__tree_remove(__end_node()->__left_, static_cast<__node_base_pointer>(__np));
2346 return __node_holder(__np, _Dp(__node_alloc(), true));
2347}
2348
2349template <class _Tp, class _Compare, class _Allocator>
2350inline _LIBCPP_HIDE_FROM_ABI void swap(__tree<_Tp, _Compare, _Allocator>& __x, __tree<_Tp, _Compare, _Allocator>& __y)
2351 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y))) {
2352 __x.swap(__y);
2353}
2354
2355_LIBCPP_END_NAMESPACE_STD
2356
2357_LIBCPP_POP_MACROS
2358
2359#endif // _LIBCPP___TREE