blob: 26c4c4121c5f3c34a0fc2e5234b8e22fd7466428 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___TREE
11#define _LIBCPP___TREE
12
13#include <__config>
14#include <iterator>
15#include <memory>
16#include <stdexcept>
17#include <algorithm>
18
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000019#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000020#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000021#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000022
Eric Fiselierf4433a32017-05-31 22:07:49 +000023_LIBCPP_PUSH_MACROS
24#include <__undef_macros>
25
26
Howard Hinnantc51e1022010-05-11 19:42:16 +000027_LIBCPP_BEGIN_NAMESPACE_STD
28
Marshall Clowa2fd8b82019-01-23 23:06:18 +000029#if defined(__GNUC__) && !defined(__clang__) // gcc.gnu.org/PR37804
30template <class, class, class, class> class _LIBCPP_TEMPLATE_VIS map;
31template <class, class, class, class> class _LIBCPP_TEMPLATE_VIS multimap;
32template <class, class, class> class _LIBCPP_TEMPLATE_VIS set;
33template <class, class, class> class _LIBCPP_TEMPLATE_VIS multiset;
34#endif
35
Howard Hinnant944510a2011-06-14 19:58:17 +000036template <class _Tp, class _Compare, class _Allocator> class __tree;
37template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000038 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
Howard Hinnant944510a2011-06-14 19:58:17 +000039template <class _Tp, class _ConstNodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000040 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +000041
Eric Fiseliera00b4842016-02-20 05:28:30 +000042template <class _Pointer> class __tree_end_node;
43template <class _VoidPtr> class __tree_node_base;
44template <class _Tp, class _VoidPtr> class __tree_node;
45
Eric Fiseliera00b4842016-02-20 05:28:30 +000046template <class _Key, class _Value>
47struct __value_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +000048
49template <class _Allocator> class __map_node_destructor;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000050template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
51template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiseliera00b4842016-02-20 05:28:30 +000052
Howard Hinnantc51e1022010-05-11 19:42:16 +000053/*
54
55_NodePtr algorithms
56
57The algorithms taking _NodePtr are red black tree algorithms. Those
58algorithms taking a parameter named __root should assume that __root
59points to a proper red black tree (unless otherwise specified).
60
61Each algorithm herein assumes that __root->__parent_ points to a non-null
62structure which has a member __left_ which points back to __root. No other
63member is read or written to at __root->__parent_.
64
65__root->__parent_ will be referred to below (in comments only) as end_node.
66end_node->__left_ is an externably accessible lvalue for __root, and can be
67changed by node insertion and removal (without explicit reference to end_node).
68
69All nodes (with the exception of end_node), even the node referred to as
70__root, have a non-null __parent_ field.
71
72*/
73
74// Returns: true if __x is a left child of its parent, else false
75// Precondition: __x != nullptr.
76template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +000077inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000078bool
Howard Hinnant1113b5e2011-06-04 17:10:24 +000079__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000080{
81 return __x == __x->__parent_->__left_;
82}
83
Joerg Sonnenbergerd74b3192017-08-18 12:57:36 +000084// Determines if the subtree rooted at __x is a proper red black subtree. If
Howard Hinnantc51e1022010-05-11 19:42:16 +000085// __x is a proper subtree, returns the black height (null counts as 1). If
86// __x is an improper subtree, returns 0.
87template <class _NodePtr>
88unsigned
89__tree_sub_invariant(_NodePtr __x)
90{
91 if (__x == nullptr)
92 return 1;
93 // parent consistency checked by caller
94 // check __x->__left_ consistency
95 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
96 return 0;
97 // check __x->__right_ consistency
98 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
99 return 0;
100 // check __x->__left_ != __x->__right_ unless both are nullptr
101 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
102 return 0;
103 // If this is red, neither child can be red
104 if (!__x->__is_black_)
105 {
106 if (__x->__left_ && !__x->__left_->__is_black_)
107 return 0;
108 if (__x->__right_ && !__x->__right_->__is_black_)
109 return 0;
110 }
111 unsigned __h = __tree_sub_invariant(__x->__left_);
112 if (__h == 0)
113 return 0; // invalid left subtree
114 if (__h != __tree_sub_invariant(__x->__right_))
115 return 0; // invalid or different height right subtree
116 return __h + __x->__is_black_; // return black height of this node
117}
118
Joerg Sonnenbergerd74b3192017-08-18 12:57:36 +0000119// Determines if the red black tree rooted at __root is a proper red black tree.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120// __root == nullptr is a proper tree. Returns true is __root is a proper
121// red black tree, else returns false.
122template <class _NodePtr>
123bool
124__tree_invariant(_NodePtr __root)
125{
126 if (__root == nullptr)
127 return true;
128 // check __x->__parent_ consistency
129 if (__root->__parent_ == nullptr)
130 return false;
131 if (!__tree_is_left_child(__root))
132 return false;
133 // root must be black
134 if (!__root->__is_black_)
135 return false;
136 // do normal node checks
137 return __tree_sub_invariant(__root) != 0;
138}
139
140// Returns: pointer to the left-most node under __x.
141// Precondition: __x != nullptr.
142template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000143inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000145__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146{
147 while (__x->__left_ != nullptr)
148 __x = __x->__left_;
149 return __x;
150}
151
152// Returns: pointer to the right-most node under __x.
153// Precondition: __x != nullptr.
154template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000155inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000157__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158{
159 while (__x->__right_ != nullptr)
160 __x = __x->__right_;
161 return __x;
162}
163
164// Returns: pointer to the next in-order node after __x.
165// Precondition: __x != nullptr.
166template <class _NodePtr>
167_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000168__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169{
170 if (__x->__right_ != nullptr)
171 return __tree_min(__x->__right_);
172 while (!__tree_is_left_child(__x))
Eric Fiselier70f5f872016-07-19 17:56:20 +0000173 __x = __x->__parent_unsafe();
174 return __x->__parent_unsafe();
175}
176
177template <class _EndNodePtr, class _NodePtr>
178inline _LIBCPP_INLINE_VISIBILITY
179_EndNodePtr
180__tree_next_iter(_NodePtr __x) _NOEXCEPT
181{
182 if (__x->__right_ != nullptr)
183 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
184 while (!__tree_is_left_child(__x))
185 __x = __x->__parent_unsafe();
186 return static_cast<_EndNodePtr>(__x->__parent_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187}
188
189// Returns: pointer to the previous in-order node before __x.
190// Precondition: __x != nullptr.
Eric Fiselier70f5f872016-07-19 17:56:20 +0000191// Note: __x may be the end node.
192template <class _NodePtr, class _EndNodePtr>
193inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194_NodePtr
Eric Fiselier70f5f872016-07-19 17:56:20 +0000195__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196{
197 if (__x->__left_ != nullptr)
198 return __tree_max(__x->__left_);
Eric Fiselier70f5f872016-07-19 17:56:20 +0000199 _NodePtr __xx = static_cast<_NodePtr>(__x);
200 while (__tree_is_left_child(__xx))
201 __xx = __xx->__parent_unsafe();
202 return __xx->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000203}
204
205// Returns: pointer to a node which has no children
206// Precondition: __x != nullptr.
207template <class _NodePtr>
208_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000209__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210{
211 while (true)
212 {
213 if (__x->__left_ != nullptr)
214 {
215 __x = __x->__left_;
216 continue;
217 }
218 if (__x->__right_ != nullptr)
219 {
220 __x = __x->__right_;
221 continue;
222 }
223 break;
224 }
225 return __x;
226}
227
228// Effects: Makes __x->__right_ the subtree root with __x as its left child
229// while preserving in-order order.
230// Precondition: __x->__right_ != nullptr
231template <class _NodePtr>
232void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000233__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234{
235 _NodePtr __y = __x->__right_;
236 __x->__right_ = __y->__left_;
237 if (__x->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000238 __x->__right_->__set_parent(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 __y->__parent_ = __x->__parent_;
240 if (__tree_is_left_child(__x))
241 __x->__parent_->__left_ = __y;
242 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000243 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 __y->__left_ = __x;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000245 __x->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246}
247
248// Effects: Makes __x->__left_ the subtree root with __x as its right child
249// while preserving in-order order.
250// Precondition: __x->__left_ != nullptr
251template <class _NodePtr>
252void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000253__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254{
255 _NodePtr __y = __x->__left_;
256 __x->__left_ = __y->__right_;
257 if (__x->__left_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000258 __x->__left_->__set_parent(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 __y->__parent_ = __x->__parent_;
260 if (__tree_is_left_child(__x))
261 __x->__parent_->__left_ = __y;
262 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000263 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264 __y->__right_ = __x;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000265 __x->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000266}
267
268// Effects: Rebalances __root after attaching __x to a leaf.
269// Precondition: __root != nulptr && __x != nullptr.
270// __x has no children.
271// __x == __root or == a direct or indirect child of __root.
272// If __x were to be unlinked from __root (setting __root to
273// nullptr if __root == __x), __tree_invariant(__root) == true.
274// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
275// may be different than the value passed in as __root.
276template <class _NodePtr>
277void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000278__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279{
280 __x->__is_black_ = __x == __root;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000281 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282 {
283 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier70f5f872016-07-19 17:56:20 +0000284 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000286 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287 if (__y != nullptr && !__y->__is_black_)
288 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000289 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000291 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000292 __x->__is_black_ = __x == __root;
293 __y->__is_black_ = true;
294 }
295 else
296 {
297 if (!__tree_is_left_child(__x))
298 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000299 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __tree_left_rotate(__x);
301 }
Eric Fiselier70f5f872016-07-19 17:56:20 +0000302 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000304 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305 __x->__is_black_ = false;
306 __tree_right_rotate(__x);
307 break;
308 }
309 }
310 else
311 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000312 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 if (__y != nullptr && !__y->__is_black_)
314 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000315 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000317 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318 __x->__is_black_ = __x == __root;
319 __y->__is_black_ = true;
320 }
321 else
322 {
323 if (__tree_is_left_child(__x))
324 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000325 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 __tree_right_rotate(__x);
327 }
Eric Fiselier70f5f872016-07-19 17:56:20 +0000328 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000330 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000331 __x->__is_black_ = false;
332 __tree_left_rotate(__x);
333 break;
334 }
335 }
336 }
337}
338
339// Precondition: __root != nullptr && __z != nullptr.
340// __tree_invariant(__root) == true.
341// __z == __root or == a direct or indirect child of __root.
342// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
343// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
344// nor any of its children refer to __z. end_node->__left_
345// may be different than the value passed in as __root.
346template <class _NodePtr>
347void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000348__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000349{
350 // __z will be removed from the tree. Client still needs to destruct/deallocate it
351 // __y is either __z, or if __z has two children, __tree_next(__z).
352 // __y will have at most one child.
353 // __y will be the initial hole in the tree (make the hole at a leaf)
354 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
355 __z : __tree_next(__z);
356 // __x is __y's possibly null single child
357 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
358 // __w is __x's possibly null uncle (will become __x's sibling)
359 _NodePtr __w = nullptr;
360 // link __x to __y's parent, and find __w
361 if (__x != nullptr)
362 __x->__parent_ = __y->__parent_;
363 if (__tree_is_left_child(__y))
364 {
365 __y->__parent_->__left_ = __x;
366 if (__y != __root)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000367 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368 else
369 __root = __x; // __w == nullptr
370 }
371 else
372 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000373 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 // __y can't be root if it is a right child
375 __w = __y->__parent_->__left_;
376 }
377 bool __removed_black = __y->__is_black_;
378 // If we didn't remove __z, do so now by splicing in __y for __z,
379 // but copy __z's color. This does not impact __x or __w.
380 if (__y != __z)
381 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000382 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 __y->__parent_ = __z->__parent_;
384 if (__tree_is_left_child(__z))
385 __y->__parent_->__left_ = __y;
386 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000387 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 __y->__left_ = __z->__left_;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000389 __y->__left_->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000390 __y->__right_ = __z->__right_;
391 if (__y->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000392 __y->__right_->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393 __y->__is_black_ = __z->__is_black_;
394 if (__root == __z)
395 __root = __y;
396 }
397 // There is no need to rebalance if we removed a red, or if we removed
398 // the last node.
399 if (__removed_black && __root != nullptr)
400 {
401 // Rebalance:
402 // __x has an implicit black color (transferred from the removed __y)
403 // associated with it, no matter what its color is.
404 // If __x is __root (in which case it can't be null), it is supposed
405 // to be black anyway, and if it is doubly black, then the double
406 // can just be ignored.
407 // If __x is red (in which case it can't be null), then it can absorb
408 // the implicit black just by setting its color to black.
409 // Since __y was black and only had one child (which __x points to), __x
410 // is either red with no children, else null, otherwise __y would have
411 // different black heights under left and right pointers.
412 // if (__x == __root || __x != nullptr && !__x->__is_black_)
413 if (__x != nullptr)
414 __x->__is_black_ = true;
415 else
416 {
417 // Else __x isn't root, and is "doubly black", even though it may
418 // be null. __w can not be null here, else the parent would
419 // see a black height >= 2 on the __x side and a black height
420 // of 1 on the __w side (__w must be a non-null black or a red
421 // with a non-null black child).
422 while (true)
423 {
424 if (!__tree_is_left_child(__w)) // if x is left child
425 {
426 if (!__w->__is_black_)
427 {
428 __w->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000429 __w->__parent_unsafe()->__is_black_ = false;
430 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431 // __x is still valid
432 // reset __root only if necessary
433 if (__root == __w->__left_)
434 __root = __w;
435 // reset sibling, and it still can't be null
436 __w = __w->__left_->__right_;
437 }
438 // __w->__is_black_ is now true, __w may have null children
439 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
440 (__w->__right_ == nullptr || __w->__right_->__is_black_))
441 {
442 __w->__is_black_ = false;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000443 __x = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444 // __x can no longer be null
445 if (__x == __root || !__x->__is_black_)
446 {
447 __x->__is_black_ = true;
448 break;
449 }
450 // reset sibling, and it still can't be null
451 __w = __tree_is_left_child(__x) ?
Eric Fiselier70f5f872016-07-19 17:56:20 +0000452 __x->__parent_unsafe()->__right_ :
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000453 __x->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000454 // continue;
455 }
456 else // __w has a red child
457 {
458 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
459 {
460 // __w left child is non-null and red
461 __w->__left_->__is_black_ = true;
462 __w->__is_black_ = false;
463 __tree_right_rotate(__w);
464 // __w is known not to be root, so root hasn't changed
465 // reset sibling, and it still can't be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000466 __w = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467 }
468 // __w has a right red child, left child may be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000469 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
470 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 __w->__right_->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000472 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473 break;
474 }
475 }
476 else
477 {
478 if (!__w->__is_black_)
479 {
480 __w->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000481 __w->__parent_unsafe()->__is_black_ = false;
482 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000483 // __x is still valid
484 // reset __root only if necessary
485 if (__root == __w->__right_)
486 __root = __w;
487 // reset sibling, and it still can't be null
488 __w = __w->__right_->__left_;
489 }
490 // __w->__is_black_ is now true, __w may have null children
491 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
492 (__w->__right_ == nullptr || __w->__right_->__is_black_))
493 {
494 __w->__is_black_ = false;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000495 __x = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000496 // __x can no longer be null
497 if (!__x->__is_black_ || __x == __root)
498 {
499 __x->__is_black_ = true;
500 break;
501 }
502 // reset sibling, and it still can't be null
503 __w = __tree_is_left_child(__x) ?
Eric Fiselier70f5f872016-07-19 17:56:20 +0000504 __x->__parent_unsafe()->__right_ :
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000505 __x->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506 // continue;
507 }
508 else // __w has a red child
509 {
510 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
511 {
512 // __w right child is non-null and red
513 __w->__right_->__is_black_ = true;
514 __w->__is_black_ = false;
515 __tree_left_rotate(__w);
516 // __w is known not to be root, so root hasn't changed
517 // reset sibling, and it still can't be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000518 __w = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000519 }
520 // __w has a left red child, right child may be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000521 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
522 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523 __w->__left_->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000524 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000525 break;
526 }
527 }
528 }
529 }
530 }
531}
532
Eric Fiseliera00b4842016-02-20 05:28:30 +0000533// node traits
534
Eric Fiselierd06276b2016-03-31 02:15:15 +0000535
Eric Fiselierd06276b2016-03-31 02:15:15 +0000536template <class _Tp>
537struct __is_tree_value_type_imp : false_type {};
538
539template <class _Key, class _Value>
Louis Dionne08e1a782020-10-09 12:33:49 -0400540struct __is_tree_value_type_imp<__value_type<_Key, _Value> > : true_type {};
Eric Fiselierd06276b2016-03-31 02:15:15 +0000541
542template <class ..._Args>
543struct __is_tree_value_type : false_type {};
544
545template <class _One>
546struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
Eric Fiselierd06276b2016-03-31 02:15:15 +0000547
Eric Fiseliera00b4842016-02-20 05:28:30 +0000548template <class _Tp>
549struct __tree_key_value_types {
550 typedef _Tp key_type;
551 typedef _Tp __node_value_type;
552 typedef _Tp __container_value_type;
553 static const bool __is_map = false;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000554
555 _LIBCPP_INLINE_VISIBILITY
556 static key_type const& __get_key(_Tp const& __v) {
557 return __v;
558 }
559 _LIBCPP_INLINE_VISIBILITY
560 static __container_value_type const& __get_value(__node_value_type const& __v) {
561 return __v;
562 }
563 _LIBCPP_INLINE_VISIBILITY
564 static __container_value_type* __get_ptr(__node_value_type& __n) {
565 return _VSTD::addressof(__n);
566 }
Eric Fiselierd06276b2016-03-31 02:15:15 +0000567 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000568 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierd06276b2016-03-31 02:15:15 +0000569 return _VSTD::move(__v);
570 }
Eric Fiseliera00b4842016-02-20 05:28:30 +0000571};
572
573template <class _Key, class _Tp>
574struct __tree_key_value_types<__value_type<_Key, _Tp> > {
575 typedef _Key key_type;
576 typedef _Tp mapped_type;
577 typedef __value_type<_Key, _Tp> __node_value_type;
578 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000579 typedef __container_value_type __map_value_type;
580 static const bool __is_map = true;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000581
582 _LIBCPP_INLINE_VISIBILITY
583 static key_type const&
584 __get_key(__node_value_type const& __t) {
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000585 return __t.__get_value().first;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000586 }
587
588 template <class _Up>
589 _LIBCPP_INLINE_VISIBILITY
590 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
591 key_type const&>::type
592 __get_key(_Up& __t) {
593 return __t.first;
594 }
595
596 _LIBCPP_INLINE_VISIBILITY
597 static __container_value_type const&
598 __get_value(__node_value_type const& __t) {
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000599 return __t.__get_value();
Eric Fiselierd06276b2016-03-31 02:15:15 +0000600 }
601
602 template <class _Up>
603 _LIBCPP_INLINE_VISIBILITY
604 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
605 __container_value_type const&>::type
606 __get_value(_Up& __t) {
607 return __t;
608 }
609
610 _LIBCPP_INLINE_VISIBILITY
611 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000612 return _VSTD::addressof(__n.__get_value());
Eric Fiselierd06276b2016-03-31 02:15:15 +0000613 }
614
Eric Fiselierd06276b2016-03-31 02:15:15 +0000615 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000616 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
617 return __v.__move();
Eric Fiselierd06276b2016-03-31 02:15:15 +0000618 }
Eric Fiseliera00b4842016-02-20 05:28:30 +0000619};
620
621template <class _VoidPtr>
622struct __tree_node_base_types {
623 typedef _VoidPtr __void_pointer;
624
625 typedef __tree_node_base<__void_pointer> __node_base_type;
626 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
627 __node_base_pointer;
628
629 typedef __tree_end_node<__node_base_pointer> __end_node_type;
630 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
631 __end_node_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000632#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
633 typedef __end_node_pointer __parent_pointer;
634#else
635 typedef typename conditional<
636 is_pointer<__end_node_pointer>::value,
637 __end_node_pointer,
638 __node_base_pointer>::type __parent_pointer;
639#endif
640
Eric Fiseliera00b4842016-02-20 05:28:30 +0000641private:
642 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
643 "_VoidPtr does not point to unqualified void type");
644};
645
646template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
647 bool = _KVTypes::__is_map>
648struct __tree_map_pointer_types {};
649
650template <class _Tp, class _AllocPtr, class _KVTypes>
651struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
652 typedef typename _KVTypes::__map_value_type _Mv;
653 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
654 __map_value_type_pointer;
655 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
656 __const_map_value_type_pointer;
657};
658
659template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
660struct __tree_node_types;
661
662template <class _NodePtr, class _Tp, class _VoidPtr>
663struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
664 : public __tree_node_base_types<_VoidPtr>,
665 __tree_key_value_types<_Tp>,
666 __tree_map_pointer_types<_Tp, _VoidPtr>
667{
668 typedef __tree_node_base_types<_VoidPtr> __base;
669 typedef __tree_key_value_types<_Tp> __key_base;
670 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
671public:
672
673 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
674 typedef _NodePtr __node_pointer;
675
676 typedef _Tp __node_value_type;
677 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
678 __node_value_type_pointer;
679 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
680 __const_node_value_type_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000681#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
682 typedef typename __base::__end_node_pointer __iter_pointer;
683#else
684 typedef typename conditional<
685 is_pointer<__node_pointer>::value,
686 typename __base::__end_node_pointer,
687 __node_pointer>::type __iter_pointer;
688#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000689private:
690 static_assert(!is_const<__node_type>::value,
691 "_NodePtr should never be a pointer to const");
692 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
693 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
694};
695
696template <class _ValueTp, class _VoidPtr>
697struct __make_tree_node_types {
698 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
699 _NodePtr;
700 typedef __tree_node_types<_NodePtr> type;
701};
702
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703// node
704
705template <class _Pointer>
706class __tree_end_node
707{
708public:
709 typedef _Pointer pointer;
710 pointer __left_;
711
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000713 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714};
715
716template <class _VoidPtr>
717class __tree_node_base
Eric Fiseliera00b4842016-02-20 05:28:30 +0000718 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000720 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
721
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000723 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000724 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725
Eric Fiselier70f5f872016-07-19 17:56:20 +0000726 pointer __right_;
727 __parent_pointer __parent_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728 bool __is_black_;
729
Eric Fiselier70f5f872016-07-19 17:56:20 +0000730 _LIBCPP_INLINE_VISIBILITY
731 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
732
733 _LIBCPP_INLINE_VISIBILITY
734 void __set_parent(pointer __p) {
735 __parent_ = static_cast<__parent_pointer>(__p);
736 }
737
Eric Fiselierd06276b2016-03-31 02:15:15 +0000738private:
739 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
740 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
741 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000742};
743
744template <class _Tp, class _VoidPtr>
745class __tree_node
746 : public __tree_node_base<_VoidPtr>
747{
748public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000749 typedef _Tp __node_value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000750
Eric Fiseliera00b4842016-02-20 05:28:30 +0000751 __node_value_type __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000752
Eric Fiselierd06276b2016-03-31 02:15:15 +0000753private:
754 ~__tree_node() _LIBCPP_EQUAL_DELETE;
755 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
756 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757};
758
Eric Fiselierd06276b2016-03-31 02:15:15 +0000759
760template <class _Allocator>
761class __tree_node_destructor
762{
763 typedef _Allocator allocator_type;
764 typedef allocator_traits<allocator_type> __alloc_traits;
765
766public:
767 typedef typename __alloc_traits::pointer pointer;
768private:
769 typedef __tree_node_types<pointer> _NodeTypes;
770 allocator_type& __na_;
771
Eric Fiselierd06276b2016-03-31 02:15:15 +0000772
773public:
774 bool __value_constructed;
775
Eric Fiselierb9f37ca2019-12-12 20:48:11 -0500776
777 __tree_node_destructor(const __tree_node_destructor &) = default;
778 __tree_node_destructor& operator=(const __tree_node_destructor&) = delete;
779
Eric Fiselierd06276b2016-03-31 02:15:15 +0000780 _LIBCPP_INLINE_VISIBILITY
781 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
782 : __na_(__na),
783 __value_constructed(__val)
784 {}
785
786 _LIBCPP_INLINE_VISIBILITY
787 void operator()(pointer __p) _NOEXCEPT
788 {
789 if (__value_constructed)
790 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
791 if (__p)
792 __alloc_traits::deallocate(__na_, __p, 1);
793 }
794
795 template <class> friend class __map_node_destructor;
796};
797
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +0000798#if _LIBCPP_STD_VER > 14
799template <class _NodeType, class _Alloc>
800struct __generic_container_node_destructor;
801template <class _Tp, class _VoidPtr, class _Alloc>
802struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc>
803 : __tree_node_destructor<_Alloc>
804{
805 using __tree_node_destructor<_Alloc>::__tree_node_destructor;
806};
807#endif
Eric Fiselierd06276b2016-03-31 02:15:15 +0000808
Howard Hinnantc51e1022010-05-11 19:42:16 +0000809template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000810class _LIBCPP_TEMPLATE_VIS __tree_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000812 typedef __tree_node_types<_NodePtr> _NodeTypes;
813 typedef _NodePtr __node_pointer;
814 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000815 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
816 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000817 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000818
Eric Fiselier70f5f872016-07-19 17:56:20 +0000819 __iter_pointer __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000822 typedef bidirectional_iterator_tag iterator_category;
823 typedef _Tp value_type;
824 typedef _DiffType difference_type;
825 typedef value_type& reference;
826 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827
Marshall Clow8fc07302013-08-08 21:52:50 +0000828 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
829#if _LIBCPP_STD_VER > 11
830 : __ptr_(nullptr)
831#endif
832 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000833
Eric Fiselier70f5f872016-07-19 17:56:20 +0000834 _LIBCPP_INLINE_VISIBILITY reference operator*() const
835 {return __get_np()->__value_;}
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000836 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier70f5f872016-07-19 17:56:20 +0000837 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000839 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000840 __tree_iterator& operator++() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000841 __ptr_ = static_cast<__iter_pointer>(
842 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000843 return *this;
844 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846 __tree_iterator operator++(int)
847 {__tree_iterator __t(*this); ++(*this); return __t;}
848
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000849 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000850 __tree_iterator& operator--() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000851 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
852 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000853 return *this;
854 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000856 __tree_iterator operator--(int)
857 {__tree_iterator __t(*this); --(*this); return __t;}
858
Louis Dionne44bcff92018-08-03 22:36:53 +0000859 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000860 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000861 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000862 friend _LIBCPP_INLINE_VISIBILITY
863 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000864 {return !(__x == __y);}
865
866private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000868 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier70f5f872016-07-19 17:56:20 +0000869 _LIBCPP_INLINE_VISIBILITY
870 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
871 _LIBCPP_INLINE_VISIBILITY
872 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873 template <class, class, class> friend class __tree;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000874 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
875 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
876 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
877 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
878 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
879 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880};
881
Eric Fiseliera00b4842016-02-20 05:28:30 +0000882template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000883class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000885 typedef __tree_node_types<_NodePtr> _NodeTypes;
886 typedef typename _NodeTypes::__node_pointer __node_pointer;
887 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000888 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
889 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000890 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000891
Eric Fiselier70f5f872016-07-19 17:56:20 +0000892 __iter_pointer __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000895 typedef bidirectional_iterator_tag iterator_category;
896 typedef _Tp value_type;
897 typedef _DiffType difference_type;
898 typedef const value_type& reference;
899 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900
Marshall Clow8fc07302013-08-08 21:52:50 +0000901 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
902#if _LIBCPP_STD_VER > 11
903 : __ptr_(nullptr)
904#endif
905 {}
906
Howard Hinnantc51e1022010-05-11 19:42:16 +0000907private:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000908 typedef __tree_iterator<value_type, __node_pointer, difference_type>
909 __non_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000912 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
913 : __ptr_(__p.__ptr_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000914
Eric Fiselier70f5f872016-07-19 17:56:20 +0000915 _LIBCPP_INLINE_VISIBILITY reference operator*() const
916 {return __get_np()->__value_;}
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000917 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier70f5f872016-07-19 17:56:20 +0000918 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000919
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000920 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000921 __tree_const_iterator& operator++() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000922 __ptr_ = static_cast<__iter_pointer>(
923 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000924 return *this;
925 }
926
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000927 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 __tree_const_iterator operator++(int)
929 {__tree_const_iterator __t(*this); ++(*this); return __t;}
930
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000931 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000932 __tree_const_iterator& operator--() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000933 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
934 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000935 return *this;
936 }
937
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000938 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000939 __tree_const_iterator operator--(int)
940 {__tree_const_iterator __t(*this); --(*this); return __t;}
941
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000942 friend _LIBCPP_INLINE_VISIBILITY
943 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000945 friend _LIBCPP_INLINE_VISIBILITY
946 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 {return !(__x == __y);}
948
949private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000950 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000951 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
952 : __ptr_(__p) {}
Eric Fiselier70f5f872016-07-19 17:56:20 +0000953 _LIBCPP_INLINE_VISIBILITY
954 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
955 : __ptr_(__p) {}
956 _LIBCPP_INLINE_VISIBILITY
957 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
958
Howard Hinnantc51e1022010-05-11 19:42:16 +0000959 template <class, class, class> friend class __tree;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000960 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
961 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
962 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
963 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
964 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000965
Howard Hinnantc51e1022010-05-11 19:42:16 +0000966};
967
Louis Dionne878a3a82018-12-06 21:46:17 +0000968template<class _Tp, class _Compare>
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000969#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500970 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
Louis Dionne69c42c02019-04-11 16:14:56 +0000971 "the specified comparator type does not provide a viable const call operator")
Louis Dionne878a3a82018-12-06 21:46:17 +0000972#endif
973int __diagnose_non_const_comparator();
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000974
Howard Hinnantc51e1022010-05-11 19:42:16 +0000975template <class _Tp, class _Compare, class _Allocator>
976class __tree
977{
978public:
979 typedef _Tp value_type;
980 typedef _Compare value_compare;
981 typedef _Allocator allocator_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000982
983private:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000984 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000985 typedef typename __make_tree_node_types<value_type,
986 typename __alloc_traits::void_pointer>::type
987 _NodeTypes;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000988 typedef typename _NodeTypes::key_type key_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000989public:
990 typedef typename _NodeTypes::__node_value_type __node_value_type;
991 typedef typename _NodeTypes::__container_value_type __container_value_type;
992
Howard Hinnantc51e1022010-05-11 19:42:16 +0000993 typedef typename __alloc_traits::pointer pointer;
994 typedef typename __alloc_traits::const_pointer const_pointer;
995 typedef typename __alloc_traits::size_type size_type;
996 typedef typename __alloc_traits::difference_type difference_type;
997
Eric Fiseliera00b4842016-02-20 05:28:30 +0000998public:
999 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001000
Eric Fiseliera00b4842016-02-20 05:28:30 +00001001 typedef typename _NodeTypes::__node_type __node;
1002 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001003
1004 typedef typename _NodeTypes::__node_base_type __node_base;
1005 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001006
1007 typedef typename _NodeTypes::__end_node_type __end_node_t;
1008 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001009
Eric Fiselier70f5f872016-07-19 17:56:20 +00001010 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
1011 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
1012
Marshall Clow940e01c2015-04-07 05:21:38 +00001013 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001014 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001015
Eric Fiseliera00b4842016-02-20 05:28:30 +00001016private:
1017 // check for sane allocator pointer rebinding semantics. Rebinding the
1018 // allocator for a new pointer type should be exactly the same as rebinding
1019 // the pointer using 'pointer_traits'.
1020 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1021 "Allocator does not rebind pointers in a sane manner.");
1022 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1023 __node_base_allocator;
1024 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1025 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1026 "Allocator does not rebind pointers in a sane manner.");
1027
1028private:
Eric Fiselier70f5f872016-07-19 17:56:20 +00001029 __iter_pointer __begin_node_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1031 __compressed_pair<size_type, value_compare> __pair3_;
1032
1033public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001034 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001035 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001036 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001037 return static_cast<__iter_pointer>(
Eric Fiseliera92b0732016-02-20 07:12:17 +00001038 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1039 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001041 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001042 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001044 return static_cast<__iter_pointer>(
Eric Fiseliera92b0732016-02-20 07:12:17 +00001045 pointer_traits<__end_node_ptr>::pointer_to(
1046 const_cast<__end_node_t&>(__pair1_.first())
1047 )
1048 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001051 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001052private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001054 const __node_allocator& __node_alloc() const _NOEXCEPT
1055 {return __pair1_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001056 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001057 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001058 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001059 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001062 allocator_type __alloc() const _NOEXCEPT
1063 {return allocator_type(__node_alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001064private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001066 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001067public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001069 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001070 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001071 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001073 const value_compare& value_comp() const _NOEXCEPT
1074 {return __pair3_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075public:
Eric Fiseliera92b0732016-02-20 07:12:17 +00001076
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001077 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera92b0732016-02-20 07:12:17 +00001078 __node_pointer __root() const _NOEXCEPT
1079 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080
Eric Fiselier70f5f872016-07-19 17:56:20 +00001081 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1082 return _VSTD::addressof(__end_node()->__left_);
1083 }
1084
Howard Hinnantc51e1022010-05-11 19:42:16 +00001085 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001086 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001087
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001088 explicit __tree(const value_compare& __comp)
1089 _NOEXCEPT_(
1090 is_nothrow_default_constructible<__node_allocator>::value &&
1091 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001092 explicit __tree(const allocator_type& __a);
1093 __tree(const value_compare& __comp, const allocator_type& __a);
1094 __tree(const __tree& __t);
1095 __tree& operator=(const __tree& __t);
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001096 template <class _ForwardIterator>
1097 void __assign_unique(_ForwardIterator __first, _ForwardIterator __last);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 template <class _InputIterator>
1099 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001100 __tree(__tree&& __t)
1101 _NOEXCEPT_(
1102 is_nothrow_move_constructible<__node_allocator>::value &&
1103 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001105 __tree& operator=(__tree&& __t)
1106 _NOEXCEPT_(
1107 __node_traits::propagate_on_container_move_assignment::value &&
1108 is_nothrow_move_assignable<value_compare>::value &&
1109 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110 ~__tree();
1111
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001113 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001115 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001117 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001119 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001120
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001122 size_type max_size() const _NOEXCEPT
Arthur O'Dwyer07b22492020-11-27 11:02:06 -05001123 {return _VSTD::min<size_type>(
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001124 __node_traits::max_size(__node_alloc()),
1125 numeric_limits<difference_type >::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001126
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001127 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001128
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001129 void swap(__tree& __t)
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001130#if _LIBCPP_STD_VER <= 11
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001131 _NOEXCEPT_(
Marshall Clow8982dcd2015-07-13 20:04:56 +00001132 __is_nothrow_swappable<value_compare>::value
Marshall Clow8982dcd2015-07-13 20:04:56 +00001133 && (!__node_traits::propagate_on_container_swap::value ||
1134 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001135 );
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001136#else
1137 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1138#endif
Eric Fiselierd06276b2016-03-31 02:15:15 +00001139
Eric Fiselierd06276b2016-03-31 02:15:15 +00001140 template <class _Key, class ..._Args>
1141 pair<iterator, bool>
1142 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1143 template <class _Key, class ..._Args>
Mark de Wever1ba476f2020-09-19 15:39:09 +02001144 pair<iterator, bool>
Eric Fiselierd06276b2016-03-31 02:15:15 +00001145 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001146
1147 template <class... _Args>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001148 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierd06276b2016-03-31 02:15:15 +00001149
Howard Hinnantc51e1022010-05-11 19:42:16 +00001150 template <class... _Args>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001151 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152
Eric Fiselierd06276b2016-03-31 02:15:15 +00001153 template <class... _Args>
1154 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001155
Eric Fiselierd06276b2016-03-31 02:15:15 +00001156 template <class... _Args>
1157 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001158
1159 template <class _Pp>
1160 _LIBCPP_INLINE_VISIBILITY
1161 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1162 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1163 __can_extract_key<_Pp, key_type>());
1164 }
1165
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001166 template <class _First, class _Second>
1167 _LIBCPP_INLINE_VISIBILITY
1168 typename enable_if<
1169 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1170 pair<iterator, bool>
1171 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1172 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1173 _VSTD::forward<_Second>(__s));
1174 }
1175
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001176 template <class... _Args>
1177 _LIBCPP_INLINE_VISIBILITY
1178 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1179 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1180 }
1181
1182 template <class _Pp>
1183 _LIBCPP_INLINE_VISIBILITY
1184 pair<iterator, bool>
1185 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1186 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1187 }
1188
1189 template <class _Pp>
1190 _LIBCPP_INLINE_VISIBILITY
1191 pair<iterator, bool>
1192 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1193 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1194 }
1195
1196 template <class _Pp>
1197 _LIBCPP_INLINE_VISIBILITY
1198 pair<iterator, bool>
1199 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1200 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1201 }
1202
1203 template <class _Pp>
1204 _LIBCPP_INLINE_VISIBILITY
1205 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1206 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1207 __can_extract_key<_Pp, key_type>());
1208 }
1209
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001210 template <class _First, class _Second>
1211 _LIBCPP_INLINE_VISIBILITY
1212 typename enable_if<
1213 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1214 iterator
1215 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1216 return __emplace_hint_unique_key_args(__p, __f,
1217 _VSTD::forward<_First>(__f),
Mark de Wever1ba476f2020-09-19 15:39:09 +02001218 _VSTD::forward<_Second>(__s)).first;
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001219 }
1220
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001221 template <class... _Args>
1222 _LIBCPP_INLINE_VISIBILITY
1223 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1224 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1225 }
1226
1227 template <class _Pp>
1228 _LIBCPP_INLINE_VISIBILITY
1229 iterator
1230 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1231 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1232 }
1233
1234 template <class _Pp>
1235 _LIBCPP_INLINE_VISIBILITY
1236 iterator
1237 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
Mark de Wever1ba476f2020-09-19 15:39:09 +02001238 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x)).first;
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001239 }
1240
1241 template <class _Pp>
1242 _LIBCPP_INLINE_VISIBILITY
1243 iterator
1244 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
Mark de Wever1ba476f2020-09-19 15:39:09 +02001245 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x)).first;
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001246 }
1247
Eric Fiselierd06276b2016-03-31 02:15:15 +00001248 _LIBCPP_INLINE_VISIBILITY
1249 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1250 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1251 }
1252
1253 _LIBCPP_INLINE_VISIBILITY
1254 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
Mark de Wever1ba476f2020-09-19 15:39:09 +02001255 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v).first;
Eric Fiselierd06276b2016-03-31 02:15:15 +00001256 }
1257
Eric Fiselierd06276b2016-03-31 02:15:15 +00001258 _LIBCPP_INLINE_VISIBILITY
1259 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1260 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1261 }
1262
1263 _LIBCPP_INLINE_VISIBILITY
1264 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
Mark de Wever1ba476f2020-09-19 15:39:09 +02001265 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v)).first;
Eric Fiselierd06276b2016-03-31 02:15:15 +00001266 }
1267
1268 template <class _Vp, class = typename enable_if<
1269 !is_same<typename __unconstref<_Vp>::type,
1270 __container_value_type
1271 >::value
1272 >::type>
1273 _LIBCPP_INLINE_VISIBILITY
1274 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1275 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1276 }
1277
1278 template <class _Vp, class = typename enable_if<
1279 !is_same<typename __unconstref<_Vp>::type,
1280 __container_value_type
1281 >::value
1282 >::type>
1283 _LIBCPP_INLINE_VISIBILITY
1284 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1285 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1286 }
1287
1288 _LIBCPP_INLINE_VISIBILITY
1289 iterator __insert_multi(__container_value_type&& __v) {
1290 return __emplace_multi(_VSTD::move(__v));
1291 }
1292
1293 _LIBCPP_INLINE_VISIBILITY
1294 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1295 return __emplace_hint_multi(__p, _VSTD::move(__v));
1296 }
1297
1298 template <class _Vp>
1299 _LIBCPP_INLINE_VISIBILITY
1300 iterator __insert_multi(_Vp&& __v) {
1301 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1302 }
1303
1304 template <class _Vp>
1305 _LIBCPP_INLINE_VISIBILITY
1306 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1307 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1308 }
1309
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001310 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001311 pair<iterator, bool> __node_assign_unique(const __container_value_type& __v, __node_pointer __dest);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001312
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001313 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001314 iterator __node_insert_multi(__node_pointer __nd);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001316 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1317
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001318
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001319 _LIBCPP_INLINE_VISIBILITY iterator
1320 __remove_node_pointer(__node_pointer) _NOEXCEPT;
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001321
1322#if _LIBCPP_STD_VER > 14
1323 template <class _NodeHandle, class _InsertReturnType>
1324 _LIBCPP_INLINE_VISIBILITY
1325 _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1326 template <class _NodeHandle>
1327 _LIBCPP_INLINE_VISIBILITY
1328 iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001329 template <class _Tree>
1330 _LIBCPP_INLINE_VISIBILITY
1331 void __node_handle_merge_unique(_Tree& __source);
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001332
1333 template <class _NodeHandle>
1334 _LIBCPP_INLINE_VISIBILITY
1335 iterator __node_handle_insert_multi(_NodeHandle&&);
1336 template <class _NodeHandle>
1337 _LIBCPP_INLINE_VISIBILITY
1338 iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001339 template <class _Tree>
1340 _LIBCPP_INLINE_VISIBILITY
1341 void __node_handle_merge_multi(_Tree& __source);
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001342
1343
1344 template <class _NodeHandle>
1345 _LIBCPP_INLINE_VISIBILITY
1346 _NodeHandle __node_handle_extract(key_type const&);
1347 template <class _NodeHandle>
1348 _LIBCPP_INLINE_VISIBILITY
1349 _NodeHandle __node_handle_extract(const_iterator);
1350#endif
1351
Howard Hinnantc51e1022010-05-11 19:42:16 +00001352 iterator erase(const_iterator __p);
1353 iterator erase(const_iterator __f, const_iterator __l);
1354 template <class _Key>
1355 size_type __erase_unique(const _Key& __k);
1356 template <class _Key>
1357 size_type __erase_multi(const _Key& __k);
1358
Eric Fiselier70f5f872016-07-19 17:56:20 +00001359 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001360 __node_base_pointer& __child,
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001361 __node_base_pointer __new_node) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001362
1363 template <class _Key>
1364 iterator find(const _Key& __v);
1365 template <class _Key>
1366 const_iterator find(const _Key& __v) const;
1367
1368 template <class _Key>
1369 size_type __count_unique(const _Key& __k) const;
1370 template <class _Key>
1371 size_type __count_multi(const _Key& __k) const;
1372
1373 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001374 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001375 iterator lower_bound(const _Key& __v)
1376 {return __lower_bound(__v, __root(), __end_node());}
1377 template <class _Key>
1378 iterator __lower_bound(const _Key& __v,
1379 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001380 __iter_pointer __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001381 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001383 const_iterator lower_bound(const _Key& __v) const
1384 {return __lower_bound(__v, __root(), __end_node());}
1385 template <class _Key>
1386 const_iterator __lower_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00001387 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001388 __iter_pointer __result) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001390 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001391 iterator upper_bound(const _Key& __v)
1392 {return __upper_bound(__v, __root(), __end_node());}
1393 template <class _Key>
1394 iterator __upper_bound(const _Key& __v,
1395 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001396 __iter_pointer __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001398 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399 const_iterator upper_bound(const _Key& __v) const
1400 {return __upper_bound(__v, __root(), __end_node());}
1401 template <class _Key>
1402 const_iterator __upper_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00001403 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001404 __iter_pointer __result) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405 template <class _Key>
1406 pair<iterator, iterator>
1407 __equal_range_unique(const _Key& __k);
1408 template <class _Key>
1409 pair<const_iterator, const_iterator>
1410 __equal_range_unique(const _Key& __k) const;
1411
1412 template <class _Key>
1413 pair<iterator, iterator>
1414 __equal_range_multi(const _Key& __k);
1415 template <class _Key>
1416 pair<const_iterator, const_iterator>
1417 __equal_range_multi(const _Key& __k) const;
1418
Howard Hinnantc834c512011-11-29 18:15:50 +00001419 typedef __tree_node_destructor<__node_allocator> _Dp;
1420 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421
Howard Hinnant1113b5e2011-06-04 17:10:24 +00001422 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001423private:
Eric Fiselier70f5f872016-07-19 17:56:20 +00001424 __node_base_pointer&
1425 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1426 __node_base_pointer&
1427 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1428 __node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001429 __find_leaf(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001430 __parent_pointer& __parent, const key_type& __v);
Eric Fiseliercf8c0212017-01-05 06:06:18 +00001431 // FIXME: Make this function const qualified. Unfortunetly doing so
1432 // breaks existing code which uses non-const callable comparators.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433 template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001434 __node_base_pointer&
1435 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 template <class _Key>
Eric Fiseliercf8c0212017-01-05 06:06:18 +00001437 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1438 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1439 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1440 }
1441 template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001442 __node_base_pointer&
1443 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1444 __node_base_pointer& __dummy,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445 const _Key& __v);
1446
Howard Hinnantc51e1022010-05-11 19:42:16 +00001447 template <class ..._Args>
Eric Fiselierd06276b2016-03-31 02:15:15 +00001448 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001449
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001450 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001453 void __copy_assign_alloc(const __tree& __t)
1454 {__copy_assign_alloc(__t, integral_constant<bool,
1455 __node_traits::propagate_on_container_copy_assignment::value>());}
1456
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 void __copy_assign_alloc(const __tree& __t, true_type)
Marshall Clowac8a40b2016-08-17 23:24:02 +00001459 {
1460 if (__node_alloc() != __t.__node_alloc())
Louis Dionne44bcff92018-08-03 22:36:53 +00001461 clear();
Marshall Clowac8a40b2016-08-17 23:24:02 +00001462 __node_alloc() = __t.__node_alloc();
1463 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001464 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001465 void __copy_assign_alloc(const __tree&, false_type) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466
1467 void __move_assign(__tree& __t, false_type);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001468 void __move_assign(__tree& __t, true_type)
1469 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1470 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001471
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473 void __move_assign_alloc(__tree& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001474 _NOEXCEPT_(
1475 !__node_traits::propagate_on_container_move_assignment::value ||
1476 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 {__move_assign_alloc(__t, integral_constant<bool,
1478 __node_traits::propagate_on_container_move_assignment::value>());}
1479
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001481 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001482 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001483 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001484 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001485 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001486
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001487 struct _DetachedTreeCache {
1488 _LIBCPP_INLINE_VISIBILITY
1489 explicit _DetachedTreeCache(__tree *__t) _NOEXCEPT : __t_(__t),
1490 __cache_root_(__detach_from_tree(__t)) {
1491 __advance();
1492 }
1493
1494 _LIBCPP_INLINE_VISIBILITY
1495 __node_pointer __get() const _NOEXCEPT {
1496 return __cache_elem_;
1497 }
1498
1499 _LIBCPP_INLINE_VISIBILITY
1500 void __advance() _NOEXCEPT {
1501 __cache_elem_ = __cache_root_;
1502 if (__cache_root_) {
1503 __cache_root_ = __detach_next(__cache_root_);
1504 }
1505 }
1506
1507 _LIBCPP_INLINE_VISIBILITY
1508 ~_DetachedTreeCache() {
1509 __t_->destroy(__cache_elem_);
1510 if (__cache_root_) {
1511 while (__cache_root_->__parent_ != nullptr)
1512 __cache_root_ = static_cast<__node_pointer>(__cache_root_->__parent_);
1513 __t_->destroy(__cache_root_);
1514 }
1515 }
1516
1517 _DetachedTreeCache(_DetachedTreeCache const&) = delete;
1518 _DetachedTreeCache& operator=(_DetachedTreeCache const&) = delete;
1519
1520 private:
1521 _LIBCPP_INLINE_VISIBILITY
1522 static __node_pointer __detach_from_tree(__tree *__t) _NOEXCEPT;
1523 _LIBCPP_INLINE_VISIBILITY
1524 static __node_pointer __detach_next(__node_pointer) _NOEXCEPT;
1525
1526 __tree *__t_;
1527 __node_pointer __cache_root_;
1528 __node_pointer __cache_elem_;
1529 };
1530
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001531
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001532 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1533 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001534};
1535
1536template <class _Tp, class _Compare, class _Allocator>
1537__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001538 _NOEXCEPT_(
1539 is_nothrow_default_constructible<__node_allocator>::value &&
1540 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001541 : __pair3_(0, __comp)
1542{
1543 __begin_node() = __end_node();
1544}
1545
1546template <class _Tp, class _Compare, class _Allocator>
1547__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001548 : __begin_node_(__iter_pointer()),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001549 __pair1_(__default_init_tag(), __node_allocator(__a)),
1550 __pair3_(0, __default_init_tag())
Howard Hinnantc51e1022010-05-11 19:42:16 +00001551{
1552 __begin_node() = __end_node();
1553}
1554
1555template <class _Tp, class _Compare, class _Allocator>
1556__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1557 const allocator_type& __a)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001558 : __begin_node_(__iter_pointer()),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001559 __pair1_(__default_init_tag(), __node_allocator(__a)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001560 __pair3_(0, __comp)
1561{
1562 __begin_node() = __end_node();
1563}
1564
1565// Precondition: size() != 0
1566template <class _Tp, class _Compare, class _Allocator>
1567typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001568__tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_from_tree(__tree *__t) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001569{
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001570 __node_pointer __cache = static_cast<__node_pointer>(__t->__begin_node());
1571 __t->__begin_node() = __t->__end_node();
1572 __t->__end_node()->__left_->__parent_ = nullptr;
1573 __t->__end_node()->__left_ = nullptr;
1574 __t->size() = 0;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001575 // __cache->__left_ == nullptr
1576 if (__cache->__right_ != nullptr)
1577 __cache = static_cast<__node_pointer>(__cache->__right_);
1578 // __cache->__left_ == nullptr
1579 // __cache->__right_ == nullptr
1580 return __cache;
1581}
1582
1583// Precondition: __cache != nullptr
1584// __cache->left_ == nullptr
1585// __cache->right_ == nullptr
1586// This is no longer a red-black tree
1587template <class _Tp, class _Compare, class _Allocator>
1588typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001589__tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_next(__node_pointer __cache) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001590{
1591 if (__cache->__parent_ == nullptr)
1592 return nullptr;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001593 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001594 {
1595 __cache->__parent_->__left_ = nullptr;
1596 __cache = static_cast<__node_pointer>(__cache->__parent_);
1597 if (__cache->__right_ == nullptr)
1598 return __cache;
1599 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1600 }
1601 // __cache is right child
Eric Fiselier70f5f872016-07-19 17:56:20 +00001602 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001603 __cache = static_cast<__node_pointer>(__cache->__parent_);
1604 if (__cache->__left_ == nullptr)
1605 return __cache;
1606 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1607}
1608
1609template <class _Tp, class _Compare, class _Allocator>
1610__tree<_Tp, _Compare, _Allocator>&
1611__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1612{
1613 if (this != &__t)
1614 {
1615 value_comp() = __t.value_comp();
1616 __copy_assign_alloc(__t);
1617 __assign_multi(__t.begin(), __t.end());
1618 }
1619 return *this;
1620}
1621
1622template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001623template <class _ForwardIterator>
Howard Hinnantc51e1022010-05-11 19:42:16 +00001624void
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001625__tree<_Tp, _Compare, _Allocator>::__assign_unique(_ForwardIterator __first, _ForwardIterator __last)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001626{
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001627 typedef iterator_traits<_ForwardIterator> _ITraits;
Eric Fiselierd06276b2016-03-31 02:15:15 +00001628 typedef typename _ITraits::value_type _ItValueType;
1629 static_assert((is_same<_ItValueType, __container_value_type>::value),
1630 "__assign_unique may only be called with the containers value type");
Eric Fiseliercd5a6772019-11-18 01:46:58 -05001631 static_assert(__is_cpp17_forward_iterator<_ForwardIterator>::value,
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001632 "__assign_unique requires a forward iterator");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 if (size() != 0)
1634 {
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001635 _DetachedTreeCache __cache(this);
1636 for (; __cache.__get() != nullptr && __first != __last; ++__first) {
1637 if (__node_assign_unique(*__first, __cache.__get()).second)
1638 __cache.__advance();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001639 }
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640 }
1641 for (; __first != __last; ++__first)
1642 __insert_unique(*__first);
1643}
1644
1645template <class _Tp, class _Compare, class _Allocator>
1646template <class _InputIterator>
1647void
1648__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1649{
Eric Fiselierd06276b2016-03-31 02:15:15 +00001650 typedef iterator_traits<_InputIterator> _ITraits;
1651 typedef typename _ITraits::value_type _ItValueType;
1652 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1653 is_same<_ItValueType, __node_value_type>::value),
1654 "__assign_multi may only be called with the containers value type"
1655 " or the nodes value type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001656 if (size() != 0)
1657 {
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001658 _DetachedTreeCache __cache(this);
1659 for (; __cache.__get() && __first != __last; ++__first) {
1660 __cache.__get()->__value_ = *__first;
1661 __node_insert_multi(__cache.__get());
1662 __cache.__advance();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001663 }
1664 }
1665 for (; __first != __last; ++__first)
Eric Fiselierd06276b2016-03-31 02:15:15 +00001666 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001667}
1668
1669template <class _Tp, class _Compare, class _Allocator>
1670__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001671 : __begin_node_(__iter_pointer()),
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001672 __pair1_(__default_init_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001673 __pair3_(0, __t.value_comp())
1674{
1675 __begin_node() = __end_node();
1676}
1677
Howard Hinnantc51e1022010-05-11 19:42:16 +00001678template <class _Tp, class _Compare, class _Allocator>
1679__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001680 _NOEXCEPT_(
1681 is_nothrow_move_constructible<__node_allocator>::value &&
1682 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001683 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1684 __pair1_(_VSTD::move(__t.__pair1_)),
1685 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001686{
1687 if (size() == 0)
1688 __begin_node() = __end_node();
1689 else
1690 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001691 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 __t.__begin_node() = __t.__end_node();
1693 __t.__end_node()->__left_ = nullptr;
1694 __t.size() = 0;
1695 }
1696}
1697
1698template <class _Tp, class _Compare, class _Allocator>
1699__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
Eric Fiselier33ebfb62019-12-16 18:23:39 -05001700 : __pair1_(__default_init_tag(), __node_allocator(__a)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001701 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001702{
1703 if (__a == __t.__alloc())
1704 {
1705 if (__t.size() == 0)
1706 __begin_node() = __end_node();
1707 else
1708 {
1709 __begin_node() = __t.__begin_node();
1710 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier70f5f872016-07-19 17:56:20 +00001711 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001712 size() = __t.size();
1713 __t.__begin_node() = __t.__end_node();
1714 __t.__end_node()->__left_ = nullptr;
1715 __t.size() = 0;
1716 }
1717 }
1718 else
1719 {
1720 __begin_node() = __end_node();
1721 }
1722}
1723
1724template <class _Tp, class _Compare, class _Allocator>
1725void
1726__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001727 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1728 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729{
1730 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1731 __begin_node_ = __t.__begin_node_;
1732 __pair1_.first() = __t.__pair1_.first();
1733 __move_assign_alloc(__t);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001734 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001735 if (size() == 0)
1736 __begin_node() = __end_node();
1737 else
1738 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001739 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001740 __t.__begin_node() = __t.__end_node();
1741 __t.__end_node()->__left_ = nullptr;
1742 __t.size() = 0;
1743 }
1744}
1745
1746template <class _Tp, class _Compare, class _Allocator>
1747void
1748__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1749{
1750 if (__node_alloc() == __t.__node_alloc())
1751 __move_assign(__t, true_type());
1752 else
1753 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001754 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001755 const_iterator __e = end();
1756 if (size() != 0)
1757 {
Eric Fiselier9f3cb342019-07-11 23:13:38 +00001758 _DetachedTreeCache __cache(this);
1759 while (__cache.__get() != nullptr && __t.size() != 0) {
1760 __cache.__get()->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
1761 __node_insert_multi(__cache.__get());
1762 __cache.__advance();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763 }
1764 }
1765 while (__t.size() != 0)
Eric Fiselierd06276b2016-03-31 02:15:15 +00001766 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001767 }
1768}
1769
1770template <class _Tp, class _Compare, class _Allocator>
1771__tree<_Tp, _Compare, _Allocator>&
1772__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001773 _NOEXCEPT_(
1774 __node_traits::propagate_on_container_move_assignment::value &&
1775 is_nothrow_move_assignable<value_compare>::value &&
1776 is_nothrow_move_assignable<__node_allocator>::value)
Louis Dionne44bcff92018-08-03 22:36:53 +00001777
Howard Hinnantc51e1022010-05-11 19:42:16 +00001778{
1779 __move_assign(__t, integral_constant<bool,
1780 __node_traits::propagate_on_container_move_assignment::value>());
1781 return *this;
1782}
1783
Howard Hinnantc51e1022010-05-11 19:42:16 +00001784template <class _Tp, class _Compare, class _Allocator>
1785__tree<_Tp, _Compare, _Allocator>::~__tree()
1786{
Marshall Clow140d9432016-06-30 22:05:45 +00001787 static_assert((is_copy_constructible<value_compare>::value),
1788 "Comparator must be copy-constructible.");
Eric Fiseliera7a14ed2017-01-13 22:02:08 +00001789 destroy(__root());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001790}
1791
1792template <class _Tp, class _Compare, class _Allocator>
1793void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001794__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001795{
1796 if (__nd != nullptr)
1797 {
1798 destroy(static_cast<__node_pointer>(__nd->__left_));
1799 destroy(static_cast<__node_pointer>(__nd->__right_));
1800 __node_allocator& __na = __node_alloc();
Eric Fiselierd06276b2016-03-31 02:15:15 +00001801 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001802 __node_traits::deallocate(__na, __nd, 1);
1803 }
1804}
1805
1806template <class _Tp, class _Compare, class _Allocator>
1807void
1808__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001809#if _LIBCPP_STD_VER <= 11
Marshall Clow8982dcd2015-07-13 20:04:56 +00001810 _NOEXCEPT_(
1811 __is_nothrow_swappable<value_compare>::value
Marshall Clow8982dcd2015-07-13 20:04:56 +00001812 && (!__node_traits::propagate_on_container_swap::value ||
1813 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001814 )
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001815#else
1816 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1817#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001818{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001819 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001820 swap(__begin_node_, __t.__begin_node_);
1821 swap(__pair1_.first(), __t.__pair1_.first());
Arthur O'Dwyer4e0de312020-11-18 18:54:38 -05001822 _VSTD::__swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001823 __pair3_.swap(__t.__pair3_);
1824 if (size() == 0)
1825 __begin_node() = __end_node();
1826 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00001827 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001828 if (__t.size() == 0)
1829 __t.__begin_node() = __t.__end_node();
1830 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00001831 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832}
1833
1834template <class _Tp, class _Compare, class _Allocator>
1835void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001836__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001837{
1838 destroy(__root());
1839 size() = 0;
1840 __begin_node() = __end_node();
1841 __end_node()->__left_ = nullptr;
1842}
1843
1844// Find lower_bound place to insert
1845// Set __parent to parent of null leaf
1846// Return reference to null leaf
1847template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001848typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1849__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001850 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001851{
1852 __node_pointer __nd = __root();
1853 if (__nd != nullptr)
1854 {
1855 while (true)
1856 {
1857 if (value_comp()(__nd->__value_, __v))
1858 {
1859 if (__nd->__right_ != nullptr)
1860 __nd = static_cast<__node_pointer>(__nd->__right_);
1861 else
1862 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001863 __parent = static_cast<__parent_pointer>(__nd);
1864 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 }
1866 }
1867 else
1868 {
1869 if (__nd->__left_ != nullptr)
1870 __nd = static_cast<__node_pointer>(__nd->__left_);
1871 else
1872 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001873 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001874 return __parent->__left_;
1875 }
1876 }
1877 }
1878 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001879 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001880 return __parent->__left_;
1881}
1882
1883// Find upper_bound place to insert
1884// Set __parent to parent of null leaf
1885// Return reference to null leaf
1886template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001887typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1888__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001889 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001890{
1891 __node_pointer __nd = __root();
1892 if (__nd != nullptr)
1893 {
1894 while (true)
1895 {
1896 if (value_comp()(__v, __nd->__value_))
1897 {
1898 if (__nd->__left_ != nullptr)
1899 __nd = static_cast<__node_pointer>(__nd->__left_);
1900 else
1901 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001902 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001903 return __parent->__left_;
1904 }
1905 }
1906 else
1907 {
1908 if (__nd->__right_ != nullptr)
1909 __nd = static_cast<__node_pointer>(__nd->__right_);
1910 else
1911 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001912 __parent = static_cast<__parent_pointer>(__nd);
1913 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001914 }
1915 }
1916 }
1917 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001918 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001919 return __parent->__left_;
1920}
1921
1922// Find leaf place to insert closest to __hint
1923// First check prior to __hint.
1924// Next check after __hint.
1925// Next do O(log N) search.
1926// Set __parent to parent of null leaf
1927// Return reference to null leaf
1928template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001929typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001930__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001931 __parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001932 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001933{
1934 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1935 {
1936 // __v <= *__hint
1937 const_iterator __prior = __hint;
1938 if (__prior == begin() || !value_comp()(__v, *--__prior))
1939 {
1940 // *prev(__hint) <= __v <= *__hint
1941 if (__hint.__ptr_->__left_ == nullptr)
1942 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001943 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001944 return __parent->__left_;
1945 }
1946 else
1947 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001948 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1949 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001950 }
1951 }
1952 // __v < *prev(__hint)
1953 return __find_leaf_high(__parent, __v);
1954 }
1955 // else __v > *__hint
1956 return __find_leaf_low(__parent, __v);
1957}
1958
1959// Find place to insert if __v doesn't exist
1960// Set __parent to parent of null leaf
1961// Return reference to null leaf
1962// If __v exists, set parent to node of __v and return reference to node of __v
1963template <class _Tp, class _Compare, class _Allocator>
1964template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001965typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1966__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 const _Key& __v)
1968{
1969 __node_pointer __nd = __root();
Eric Fiselier70f5f872016-07-19 17:56:20 +00001970 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001971 if (__nd != nullptr)
1972 {
1973 while (true)
1974 {
1975 if (value_comp()(__v, __nd->__value_))
1976 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001977 if (__nd->__left_ != nullptr) {
1978 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001979 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier70f5f872016-07-19 17:56:20 +00001980 } else {
1981 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001982 return __parent->__left_;
1983 }
1984 }
1985 else if (value_comp()(__nd->__value_, __v))
1986 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001987 if (__nd->__right_ != nullptr) {
1988 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001989 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier70f5f872016-07-19 17:56:20 +00001990 } else {
1991 __parent = static_cast<__parent_pointer>(__nd);
1992 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001993 }
1994 }
1995 else
1996 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001997 __parent = static_cast<__parent_pointer>(__nd);
1998 return *__nd_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001999 }
2000 }
2001 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00002002 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003 return __parent->__left_;
2004}
2005
2006// Find place to insert if __v doesn't exist
2007// First check prior to __hint.
2008// Next check after __hint.
2009// Next do O(log N) search.
2010// Set __parent to parent of null leaf
2011// Return reference to null leaf
2012// If __v exists, set parent to node of __v and return reference to node of __v
2013template <class _Tp, class _Compare, class _Allocator>
2014template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00002015typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002017 __parent_pointer& __parent,
2018 __node_base_pointer& __dummy,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002019 const _Key& __v)
2020{
2021 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2022 {
2023 // __v < *__hint
2024 const_iterator __prior = __hint;
2025 if (__prior == begin() || value_comp()(*--__prior, __v))
2026 {
2027 // *prev(__hint) < __v < *__hint
2028 if (__hint.__ptr_->__left_ == nullptr)
2029 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002030 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031 return __parent->__left_;
2032 }
2033 else
2034 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002035 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2036 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002037 }
2038 }
2039 // __v <= *prev(__hint)
2040 return __find_equal(__parent, __v);
2041 }
2042 else if (value_comp()(*__hint, __v)) // check after
2043 {
2044 // *__hint < __v
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002045 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002046 if (__next == end() || value_comp()(__v, *__next))
2047 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002048 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002049 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002050 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002051 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2052 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002053 }
2054 else
2055 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002056 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057 return __parent->__left_;
2058 }
2059 }
2060 // *next(__hint) <= __v
2061 return __find_equal(__parent, __v);
2062 }
2063 // else __v == *__hint
Eric Fiselier70f5f872016-07-19 17:56:20 +00002064 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2065 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2066 return __dummy;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067}
2068
2069template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002070void __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
2071 __parent_pointer __parent, __node_base_pointer& __child,
2072 __node_base_pointer __new_node) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002073{
2074 __new_node->__left_ = nullptr;
2075 __new_node->__right_ = nullptr;
2076 __new_node->__parent_ = __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002077 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantc51e1022010-05-11 19:42:16 +00002078 __child = __new_node;
2079 if (__begin_node()->__left_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002080 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002081 __tree_balance_after_insert(__end_node()->__left_, __child);
2082 ++size();
2083}
2084
Eric Fiselierd06276b2016-03-31 02:15:15 +00002085template <class _Tp, class _Compare, class _Allocator>
2086template <class _Key, class... _Args>
2087pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2088__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierd06276b2016-03-31 02:15:15 +00002089{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002090 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002091 __node_base_pointer& __child = __find_equal(__parent, __k);
2092 __node_pointer __r = static_cast<__node_pointer>(__child);
2093 bool __inserted = false;
2094 if (__child == nullptr)
2095 {
Eric Fiselierd06276b2016-03-31 02:15:15 +00002096 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselierd06276b2016-03-31 02:15:15 +00002097 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2098 __r = __h.release();
2099 __inserted = true;
2100 }
2101 return pair<iterator, bool>(iterator(__r), __inserted);
2102}
2103
Eric Fiselierd06276b2016-03-31 02:15:15 +00002104template <class _Tp, class _Compare, class _Allocator>
2105template <class _Key, class... _Args>
Mark de Wever1ba476f2020-09-19 15:39:09 +02002106pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselierd06276b2016-03-31 02:15:15 +00002107__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2108 const_iterator __p, _Key const& __k, _Args&&... __args)
Eric Fiselierd06276b2016-03-31 02:15:15 +00002109{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002110 __parent_pointer __parent;
2111 __node_base_pointer __dummy;
2112 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierd06276b2016-03-31 02:15:15 +00002113 __node_pointer __r = static_cast<__node_pointer>(__child);
Mark de Wever1ba476f2020-09-19 15:39:09 +02002114 bool __inserted = false;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002115 if (__child == nullptr)
2116 {
Eric Fiselierd06276b2016-03-31 02:15:15 +00002117 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselierd06276b2016-03-31 02:15:15 +00002118 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2119 __r = __h.release();
Mark de Wever1ba476f2020-09-19 15:39:09 +02002120 __inserted = true;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002121 }
Mark de Wever1ba476f2020-09-19 15:39:09 +02002122 return pair<iterator, bool>(iterator(__r), __inserted);
Eric Fiselierd06276b2016-03-31 02:15:15 +00002123}
2124
Howard Hinnantc51e1022010-05-11 19:42:16 +00002125template <class _Tp, class _Compare, class _Allocator>
2126template <class ..._Args>
2127typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2128__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2129{
Eric Fiselierd06276b2016-03-31 02:15:15 +00002130 static_assert(!__is_tree_value_type<_Args...>::value,
2131 "Cannot construct from __value_type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002132 __node_allocator& __na = __node_alloc();
Howard Hinnantc834c512011-11-29 18:15:50 +00002133 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierd06276b2016-03-31 02:15:15 +00002134 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135 __h.get_deleter().__value_constructed = true;
2136 return __h;
2137}
2138
Eric Fiselierd06276b2016-03-31 02:15:15 +00002139
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140template <class _Tp, class _Compare, class _Allocator>
2141template <class... _Args>
2142pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00002143__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002144{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002145 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002146 __parent_pointer __parent;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002147 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2148 __node_pointer __r = static_cast<__node_pointer>(__child);
2149 bool __inserted = false;
2150 if (__child == nullptr)
2151 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002152 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002153 __r = __h.release();
2154 __inserted = true;
2155 }
2156 return pair<iterator, bool>(iterator(__r), __inserted);
2157}
2158
2159template <class _Tp, class _Compare, class _Allocator>
2160template <class... _Args>
2161typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier6cce51e2016-04-15 23:27:27 +00002162__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002163{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002164 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002165 __parent_pointer __parent;
2166 __node_base_pointer __dummy;
2167 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002168 __node_pointer __r = static_cast<__node_pointer>(__child);
2169 if (__child == nullptr)
2170 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002171 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002172 __r = __h.release();
2173 }
2174 return iterator(__r);
2175}
2176
2177template <class _Tp, class _Compare, class _Allocator>
2178template <class... _Args>
2179typename __tree<_Tp, _Compare, _Allocator>::iterator
2180__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2181{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002182 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002183 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002184 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002185 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002186 return iterator(static_cast<__node_pointer>(__h.release()));
2187}
2188
2189template <class _Tp, class _Compare, class _Allocator>
2190template <class... _Args>
2191typename __tree<_Tp, _Compare, _Allocator>::iterator
2192__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2193 _Args&&... __args)
2194{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002195 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002196 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002197 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002198 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002199 return iterator(static_cast<__node_pointer>(__h.release()));
2200}
2201
Howard Hinnantc51e1022010-05-11 19:42:16 +00002202template <class _Tp, class _Compare, class _Allocator>
2203pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier9f3cb342019-07-11 23:13:38 +00002204__tree<_Tp, _Compare, _Allocator>::__node_assign_unique(const __container_value_type& __v, __node_pointer __nd)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002205{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002206 __parent_pointer __parent;
Eric Fiselier9f3cb342019-07-11 23:13:38 +00002207 __node_base_pointer& __child = __find_equal(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002208 __node_pointer __r = static_cast<__node_pointer>(__child);
2209 bool __inserted = false;
2210 if (__child == nullptr)
2211 {
Eric Fiselier9f3cb342019-07-11 23:13:38 +00002212 __nd->__value_ = __v;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002213 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002214 __r = __nd;
2215 __inserted = true;
2216 }
2217 return pair<iterator, bool>(iterator(__r), __inserted);
2218}
2219
Howard Hinnantc51e1022010-05-11 19:42:16 +00002220
2221template <class _Tp, class _Compare, class _Allocator>
2222typename __tree<_Tp, _Compare, _Allocator>::iterator
2223__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2224{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002225 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002226 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002227 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002228 return iterator(__nd);
2229}
2230
2231template <class _Tp, class _Compare, class _Allocator>
2232typename __tree<_Tp, _Compare, _Allocator>::iterator
2233__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2234 __node_pointer __nd)
2235{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002236 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002237 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002238 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002239 return iterator(__nd);
2240}
2241
2242template <class _Tp, class _Compare, class _Allocator>
2243typename __tree<_Tp, _Compare, _Allocator>::iterator
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002244__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002245{
2246 iterator __r(__ptr);
2247 ++__r;
2248 if (__begin_node() == __ptr)
2249 __begin_node() = __r.__ptr_;
2250 --size();
2251 __tree_remove(__end_node()->__left_,
2252 static_cast<__node_base_pointer>(__ptr));
2253 return __r;
2254}
2255
2256#if _LIBCPP_STD_VER > 14
2257template <class _Tp, class _Compare, class _Allocator>
2258template <class _NodeHandle, class _InsertReturnType>
2259_LIBCPP_INLINE_VISIBILITY
2260_InsertReturnType
2261__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2262 _NodeHandle&& __nh)
2263{
2264 if (__nh.empty())
2265 return _InsertReturnType{end(), false, _NodeHandle()};
2266
2267 __node_pointer __ptr = __nh.__ptr_;
2268 __parent_pointer __parent;
2269 __node_base_pointer& __child = __find_equal(__parent,
2270 __ptr->__value_);
2271 if (__child != nullptr)
2272 return _InsertReturnType{
2273 iterator(static_cast<__node_pointer>(__child)),
2274 false, _VSTD::move(__nh)};
2275
2276 __insert_node_at(__parent, __child,
2277 static_cast<__node_base_pointer>(__ptr));
Eric Fiselierb59eaa72019-04-24 09:43:44 +00002278 __nh.__release_ptr();
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002279 return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
2280}
2281
2282template <class _Tp, class _Compare, class _Allocator>
2283template <class _NodeHandle>
2284_LIBCPP_INLINE_VISIBILITY
2285typename __tree<_Tp, _Compare, _Allocator>::iterator
2286__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2287 const_iterator __hint, _NodeHandle&& __nh)
2288{
2289 if (__nh.empty())
2290 return end();
2291
2292 __node_pointer __ptr = __nh.__ptr_;
2293 __parent_pointer __parent;
2294 __node_base_pointer __dummy;
2295 __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy,
2296 __ptr->__value_);
2297 __node_pointer __r = static_cast<__node_pointer>(__child);
2298 if (__child == nullptr)
2299 {
2300 __insert_node_at(__parent, __child,
2301 static_cast<__node_base_pointer>(__ptr));
2302 __r = __ptr;
Eric Fiselierb59eaa72019-04-24 09:43:44 +00002303 __nh.__release_ptr();
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002304 }
2305 return iterator(__r);
2306}
2307
2308template <class _Tp, class _Compare, class _Allocator>
2309template <class _NodeHandle>
2310_LIBCPP_INLINE_VISIBILITY
2311_NodeHandle
2312__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key)
2313{
2314 iterator __it = find(__key);
2315 if (__it == end())
2316 return _NodeHandle();
2317 return __node_handle_extract<_NodeHandle>(__it);
2318}
2319
2320template <class _Tp, class _Compare, class _Allocator>
2321template <class _NodeHandle>
2322_LIBCPP_INLINE_VISIBILITY
2323_NodeHandle
2324__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p)
2325{
2326 __node_pointer __np = __p.__get_np();
2327 __remove_node_pointer(__np);
2328 return _NodeHandle(__np, __alloc());
2329}
2330
2331template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002332template <class _Tree>
2333_LIBCPP_INLINE_VISIBILITY
2334void
2335__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(_Tree& __source)
2336{
2337 static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2338
2339 for (typename _Tree::iterator __i = __source.begin();
2340 __i != __source.end();)
2341 {
2342 __node_pointer __src_ptr = __i.__get_np();
2343 __parent_pointer __parent;
2344 __node_base_pointer& __child =
2345 __find_equal(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
2346 ++__i;
2347 if (__child != nullptr)
2348 continue;
2349 __source.__remove_node_pointer(__src_ptr);
2350 __insert_node_at(__parent, __child,
2351 static_cast<__node_base_pointer>(__src_ptr));
2352 }
2353}
2354
2355template <class _Tp, class _Compare, class _Allocator>
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002356template <class _NodeHandle>
2357_LIBCPP_INLINE_VISIBILITY
2358typename __tree<_Tp, _Compare, _Allocator>::iterator
2359__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh)
2360{
2361 if (__nh.empty())
2362 return end();
2363 __node_pointer __ptr = __nh.__ptr_;
2364 __parent_pointer __parent;
2365 __node_base_pointer& __child = __find_leaf_high(
2366 __parent, _NodeTypes::__get_key(__ptr->__value_));
2367 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
Eric Fiselierb59eaa72019-04-24 09:43:44 +00002368 __nh.__release_ptr();
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002369 return iterator(__ptr);
2370}
2371
2372template <class _Tp, class _Compare, class _Allocator>
2373template <class _NodeHandle>
2374_LIBCPP_INLINE_VISIBILITY
2375typename __tree<_Tp, _Compare, _Allocator>::iterator
2376__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(
2377 const_iterator __hint, _NodeHandle&& __nh)
2378{
2379 if (__nh.empty())
2380 return end();
2381
2382 __node_pointer __ptr = __nh.__ptr_;
2383 __parent_pointer __parent;
2384 __node_base_pointer& __child = __find_leaf(__hint, __parent,
2385 _NodeTypes::__get_key(__ptr->__value_));
2386 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
Eric Fiselierb59eaa72019-04-24 09:43:44 +00002387 __nh.__release_ptr();
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002388 return iterator(__ptr);
2389}
2390
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002391template <class _Tp, class _Compare, class _Allocator>
2392template <class _Tree>
2393_LIBCPP_INLINE_VISIBILITY
2394void
2395__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(_Tree& __source)
2396{
2397 static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2398
2399 for (typename _Tree::iterator __i = __source.begin();
2400 __i != __source.end();)
2401 {
2402 __node_pointer __src_ptr = __i.__get_np();
2403 __parent_pointer __parent;
2404 __node_base_pointer& __child = __find_leaf_high(
2405 __parent, _NodeTypes::__get_key(__src_ptr->__value_));
2406 ++__i;
2407 __source.__remove_node_pointer(__src_ptr);
2408 __insert_node_at(__parent, __child,
2409 static_cast<__node_base_pointer>(__src_ptr));
2410 }
2411}
2412
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002413#endif // _LIBCPP_STD_VER > 14
2414
2415template <class _Tp, class _Compare, class _Allocator>
2416typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002417__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2418{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002419 __node_pointer __np = __p.__get_np();
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002420 iterator __r = __remove_node_pointer(__np);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002421 __node_allocator& __na = __node_alloc();
Eric Fiselierd06276b2016-03-31 02:15:15 +00002422 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2423 const_cast<__node_value_type&>(*__p)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002424 __node_traits::deallocate(__na, __np, 1);
2425 return __r;
2426}
2427
2428template <class _Tp, class _Compare, class _Allocator>
2429typename __tree<_Tp, _Compare, _Allocator>::iterator
2430__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2431{
2432 while (__f != __l)
2433 __f = erase(__f);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002434 return iterator(__l.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002435}
2436
2437template <class _Tp, class _Compare, class _Allocator>
2438template <class _Key>
2439typename __tree<_Tp, _Compare, _Allocator>::size_type
2440__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2441{
2442 iterator __i = find(__k);
2443 if (__i == end())
2444 return 0;
2445 erase(__i);
2446 return 1;
2447}
2448
2449template <class _Tp, class _Compare, class _Allocator>
2450template <class _Key>
2451typename __tree<_Tp, _Compare, _Allocator>::size_type
2452__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2453{
2454 pair<iterator, iterator> __p = __equal_range_multi(__k);
2455 size_type __r = 0;
2456 for (; __p.first != __p.second; ++__r)
2457 __p.first = erase(__p.first);
2458 return __r;
2459}
2460
2461template <class _Tp, class _Compare, class _Allocator>
2462template <class _Key>
2463typename __tree<_Tp, _Compare, _Allocator>::iterator
2464__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2465{
2466 iterator __p = __lower_bound(__v, __root(), __end_node());
2467 if (__p != end() && !value_comp()(__v, *__p))
2468 return __p;
2469 return end();
2470}
2471
2472template <class _Tp, class _Compare, class _Allocator>
2473template <class _Key>
2474typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2475__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2476{
2477 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2478 if (__p != end() && !value_comp()(__v, *__p))
2479 return __p;
2480 return end();
2481}
2482
2483template <class _Tp, class _Compare, class _Allocator>
2484template <class _Key>
2485typename __tree<_Tp, _Compare, _Allocator>::size_type
2486__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2487{
Eric Fiseliera92b0732016-02-20 07:12:17 +00002488 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002489 while (__rt != nullptr)
2490 {
2491 if (value_comp()(__k, __rt->__value_))
2492 {
Eric Fiseliera92b0732016-02-20 07:12:17 +00002493 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002494 }
2495 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002496 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002497 else
2498 return 1;
2499 }
2500 return 0;
2501}
2502
2503template <class _Tp, class _Compare, class _Allocator>
2504template <class _Key>
2505typename __tree<_Tp, _Compare, _Allocator>::size_type
2506__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2507{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002508 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002509 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002510 while (__rt != nullptr)
2511 {
2512 if (value_comp()(__k, __rt->__value_))
2513 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002514 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002515 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002516 }
2517 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002518 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002519 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002520 return _VSTD::distance(
Eric Fiselier70f5f872016-07-19 17:56:20 +00002521 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiseliera92b0732016-02-20 07:12:17 +00002522 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002523 );
2524 }
2525 return 0;
2526}
2527
2528template <class _Tp, class _Compare, class _Allocator>
2529template <class _Key>
2530typename __tree<_Tp, _Compare, _Allocator>::iterator
2531__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2532 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002533 __iter_pointer __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002534{
2535 while (__root != nullptr)
2536 {
2537 if (!value_comp()(__root->__value_, __v))
2538 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002539 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002540 __root = static_cast<__node_pointer>(__root->__left_);
2541 }
2542 else
2543 __root = static_cast<__node_pointer>(__root->__right_);
2544 }
2545 return iterator(__result);
2546}
2547
2548template <class _Tp, class _Compare, class _Allocator>
2549template <class _Key>
2550typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2551__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00002552 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002553 __iter_pointer __result) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002554{
2555 while (__root != nullptr)
2556 {
2557 if (!value_comp()(__root->__value_, __v))
2558 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002559 __result = static_cast<__iter_pointer>(__root);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002560 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561 }
2562 else
Eric Fiseliera92b0732016-02-20 07:12:17 +00002563 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002564 }
2565 return const_iterator(__result);
2566}
2567
2568template <class _Tp, class _Compare, class _Allocator>
2569template <class _Key>
2570typename __tree<_Tp, _Compare, _Allocator>::iterator
2571__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2572 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002573 __iter_pointer __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574{
2575 while (__root != nullptr)
2576 {
2577 if (value_comp()(__v, __root->__value_))
2578 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002579 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002580 __root = static_cast<__node_pointer>(__root->__left_);
2581 }
2582 else
2583 __root = static_cast<__node_pointer>(__root->__right_);
2584 }
2585 return iterator(__result);
2586}
2587
2588template <class _Tp, class _Compare, class _Allocator>
2589template <class _Key>
2590typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2591__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00002592 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002593 __iter_pointer __result) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002594{
2595 while (__root != nullptr)
2596 {
2597 if (value_comp()(__v, __root->__value_))
2598 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002599 __result = static_cast<__iter_pointer>(__root);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002600 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002601 }
2602 else
Eric Fiseliera92b0732016-02-20 07:12:17 +00002603 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002604 }
2605 return const_iterator(__result);
2606}
2607
2608template <class _Tp, class _Compare, class _Allocator>
2609template <class _Key>
2610pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2611 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2612__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2613{
Howard Hinnantc834c512011-11-29 18:15:50 +00002614 typedef pair<iterator, iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002615 __iter_pointer __result = __end_node();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616 __node_pointer __rt = __root();
2617 while (__rt != nullptr)
2618 {
2619 if (value_comp()(__k, __rt->__value_))
2620 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002621 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 __rt = static_cast<__node_pointer>(__rt->__left_);
2623 }
2624 else if (value_comp()(__rt->__value_, __k))
2625 __rt = static_cast<__node_pointer>(__rt->__right_);
2626 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002627 return _Pp(iterator(__rt),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002628 iterator(
2629 __rt->__right_ != nullptr ?
Eric Fiselier70f5f872016-07-19 17:56:20 +00002630 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002631 : __result));
2632 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002633 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002634}
2635
2636template <class _Tp, class _Compare, class _Allocator>
2637template <class _Key>
2638pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2639 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2640__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2641{
Howard Hinnantc834c512011-11-29 18:15:50 +00002642 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002643 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002644 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002645 while (__rt != nullptr)
2646 {
2647 if (value_comp()(__k, __rt->__value_))
2648 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002649 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002650 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002651 }
2652 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002653 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002654 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002655 return _Pp(const_iterator(__rt),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002656 const_iterator(
2657 __rt->__right_ != nullptr ?
Eric Fiselier70f5f872016-07-19 17:56:20 +00002658 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002659 : __result));
2660 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002661 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002662}
2663
2664template <class _Tp, class _Compare, class _Allocator>
2665template <class _Key>
2666pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2667 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2668__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2669{
Howard Hinnantc834c512011-11-29 18:15:50 +00002670 typedef pair<iterator, iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002671 __iter_pointer __result = __end_node();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 __node_pointer __rt = __root();
2673 while (__rt != nullptr)
2674 {
2675 if (value_comp()(__k, __rt->__value_))
2676 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002677 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002678 __rt = static_cast<__node_pointer>(__rt->__left_);
2679 }
2680 else if (value_comp()(__rt->__value_, __k))
2681 __rt = static_cast<__node_pointer>(__rt->__right_);
2682 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002683 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002684 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2685 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002686 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687}
2688
2689template <class _Tp, class _Compare, class _Allocator>
2690template <class _Key>
2691pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2692 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2693__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2694{
Howard Hinnantc834c512011-11-29 18:15:50 +00002695 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002696 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002697 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002698 while (__rt != nullptr)
2699 {
2700 if (value_comp()(__k, __rt->__value_))
2701 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002702 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002703 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002704 }
2705 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002706 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002708 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiseliera92b0732016-02-20 07:12:17 +00002709 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002710 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002711 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002712}
2713
2714template <class _Tp, class _Compare, class _Allocator>
2715typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant1113b5e2011-06-04 17:10:24 +00002716__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002718 __node_pointer __np = __p.__get_np();
2719 if (__begin_node() == __p.__ptr_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002720 {
2721 if (__np->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002722 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002723 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002724 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002725 }
2726 --size();
2727 __tree_remove(__end_node()->__left_,
2728 static_cast<__node_base_pointer>(__np));
Marshall Clow95af65e2015-01-28 19:54:25 +00002729 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002730}
2731
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00002732template <class _Tp, class _Compare, class _Allocator>
2733inline _LIBCPP_INLINE_VISIBILITY
2734void
2735swap(__tree<_Tp, _Compare, _Allocator>& __x,
2736 __tree<_Tp, _Compare, _Allocator>& __y)
2737 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2738{
2739 __x.swap(__y);
2740}
2741
Howard Hinnantc51e1022010-05-11 19:42:16 +00002742_LIBCPP_END_NAMESPACE_STD
2743
Eric Fiselierf4433a32017-05-31 22:07:49 +00002744_LIBCPP_POP_MACROS
2745
Howard Hinnantc51e1022010-05-11 19:42:16 +00002746#endif // _LIBCPP___TREE