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