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