blob: aa7370bfb043a32bf386b9e3a943845e6bdbd5c2 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000022#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000023
Eric Fiselierf4433a32017-05-31 22:07:49 +000024_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27
Howard Hinnantc51e1022010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
Howard Hinnant944510a2011-06-14 19:58:17 +000030template <class _Tp, class _Compare, class _Allocator> class __tree;
31template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000032 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
Howard Hinnant944510a2011-06-14 19:58:17 +000033template <class _Tp, class _ConstNodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000034 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +000035
Eric Fiseliera00b4842016-02-20 05:28:30 +000036template <class _Pointer> class __tree_end_node;
37template <class _VoidPtr> class __tree_node_base;
38template <class _Tp, class _VoidPtr> class __tree_node;
39
Eric Fiseliera00b4842016-02-20 05:28:30 +000040template <class _Key, class _Value>
41struct __value_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +000042
Eric Fiseliera7a14ed2017-01-13 22:02:08 +000043template <class _Key, class _CP, class _Compare,
44 bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
45class __map_value_compare;
46
Eric Fiseliera00b4842016-02-20 05:28:30 +000047template <class _Allocator> class __map_node_destructor;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000048template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
49template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiseliera00b4842016-02-20 05:28:30 +000050
Howard Hinnantc51e1022010-05-11 19:42:16 +000051/*
52
53_NodePtr algorithms
54
55The algorithms taking _NodePtr are red black tree algorithms. Those
56algorithms taking a parameter named __root should assume that __root
57points to a proper red black tree (unless otherwise specified).
58
59Each algorithm herein assumes that __root->__parent_ points to a non-null
60structure which has a member __left_ which points back to __root. No other
61member is read or written to at __root->__parent_.
62
63__root->__parent_ will be referred to below (in comments only) as end_node.
64end_node->__left_ is an externably accessible lvalue for __root, and can be
65changed by node insertion and removal (without explicit reference to end_node).
66
67All nodes (with the exception of end_node), even the node referred to as
68__root, have a non-null __parent_ field.
69
70*/
71
72// Returns: true if __x is a left child of its parent, else false
73// Precondition: __x != nullptr.
74template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +000075inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000076bool
Howard Hinnant1113b5e2011-06-04 17:10:24 +000077__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000078{
79 return __x == __x->__parent_->__left_;
80}
81
Joerg Sonnenbergerd74b3192017-08-18 12:57:36 +000082// Determines if the subtree rooted at __x is a proper red black subtree. If
Howard Hinnantc51e1022010-05-11 19:42:16 +000083// __x is a proper subtree, returns the black height (null counts as 1). If
84// __x is an improper subtree, returns 0.
85template <class _NodePtr>
86unsigned
87__tree_sub_invariant(_NodePtr __x)
88{
89 if (__x == nullptr)
90 return 1;
91 // parent consistency checked by caller
92 // check __x->__left_ consistency
93 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
94 return 0;
95 // check __x->__right_ consistency
96 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
97 return 0;
98 // check __x->__left_ != __x->__right_ unless both are nullptr
99 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
100 return 0;
101 // If this is red, neither child can be red
102 if (!__x->__is_black_)
103 {
104 if (__x->__left_ && !__x->__left_->__is_black_)
105 return 0;
106 if (__x->__right_ && !__x->__right_->__is_black_)
107 return 0;
108 }
109 unsigned __h = __tree_sub_invariant(__x->__left_);
110 if (__h == 0)
111 return 0; // invalid left subtree
112 if (__h != __tree_sub_invariant(__x->__right_))
113 return 0; // invalid or different height right subtree
114 return __h + __x->__is_black_; // return black height of this node
115}
116
Joerg Sonnenbergerd74b3192017-08-18 12:57:36 +0000117// Determines if the red black tree rooted at __root is a proper red black tree.
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118// __root == nullptr is a proper tree. Returns true is __root is a proper
119// red black tree, else returns false.
120template <class _NodePtr>
121bool
122__tree_invariant(_NodePtr __root)
123{
124 if (__root == nullptr)
125 return true;
126 // check __x->__parent_ consistency
127 if (__root->__parent_ == nullptr)
128 return false;
129 if (!__tree_is_left_child(__root))
130 return false;
131 // root must be black
132 if (!__root->__is_black_)
133 return false;
134 // do normal node checks
135 return __tree_sub_invariant(__root) != 0;
136}
137
138// Returns: pointer to the left-most node under __x.
139// Precondition: __x != nullptr.
140template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000143__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144{
145 while (__x->__left_ != nullptr)
146 __x = __x->__left_;
147 return __x;
148}
149
150// Returns: pointer to the right-most node under __x.
151// Precondition: __x != nullptr.
152template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000155__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156{
157 while (__x->__right_ != nullptr)
158 __x = __x->__right_;
159 return __x;
160}
161
162// Returns: pointer to the next in-order node after __x.
163// Precondition: __x != nullptr.
164template <class _NodePtr>
165_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000166__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167{
168 if (__x->__right_ != nullptr)
169 return __tree_min(__x->__right_);
170 while (!__tree_is_left_child(__x))
Eric Fiselier70f5f872016-07-19 17:56:20 +0000171 __x = __x->__parent_unsafe();
172 return __x->__parent_unsafe();
173}
174
175template <class _EndNodePtr, class _NodePtr>
176inline _LIBCPP_INLINE_VISIBILITY
177_EndNodePtr
178__tree_next_iter(_NodePtr __x) _NOEXCEPT
179{
180 if (__x->__right_ != nullptr)
181 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
182 while (!__tree_is_left_child(__x))
183 __x = __x->__parent_unsafe();
184 return static_cast<_EndNodePtr>(__x->__parent_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185}
186
187// Returns: pointer to the previous in-order node before __x.
188// Precondition: __x != nullptr.
Eric Fiselier70f5f872016-07-19 17:56:20 +0000189// Note: __x may be the end node.
190template <class _NodePtr, class _EndNodePtr>
191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192_NodePtr
Eric Fiselier70f5f872016-07-19 17:56:20 +0000193__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194{
195 if (__x->__left_ != nullptr)
196 return __tree_max(__x->__left_);
Eric Fiselier70f5f872016-07-19 17:56:20 +0000197 _NodePtr __xx = static_cast<_NodePtr>(__x);
198 while (__tree_is_left_child(__xx))
199 __xx = __xx->__parent_unsafe();
200 return __xx->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000201}
202
203// Returns: pointer to a node which has no children
204// Precondition: __x != nullptr.
205template <class _NodePtr>
206_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000207__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208{
209 while (true)
210 {
211 if (__x->__left_ != nullptr)
212 {
213 __x = __x->__left_;
214 continue;
215 }
216 if (__x->__right_ != nullptr)
217 {
218 __x = __x->__right_;
219 continue;
220 }
221 break;
222 }
223 return __x;
224}
225
226// Effects: Makes __x->__right_ the subtree root with __x as its left child
227// while preserving in-order order.
228// Precondition: __x->__right_ != nullptr
229template <class _NodePtr>
230void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000231__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232{
233 _NodePtr __y = __x->__right_;
234 __x->__right_ = __y->__left_;
235 if (__x->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000236 __x->__right_->__set_parent(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 __y->__parent_ = __x->__parent_;
238 if (__tree_is_left_child(__x))
239 __x->__parent_->__left_ = __y;
240 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000241 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 __y->__left_ = __x;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000243 __x->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244}
245
246// Effects: Makes __x->__left_ the subtree root with __x as its right child
247// while preserving in-order order.
248// Precondition: __x->__left_ != nullptr
249template <class _NodePtr>
250void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000251__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252{
253 _NodePtr __y = __x->__left_;
254 __x->__left_ = __y->__right_;
255 if (__x->__left_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000256 __x->__left_->__set_parent(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000257 __y->__parent_ = __x->__parent_;
258 if (__tree_is_left_child(__x))
259 __x->__parent_->__left_ = __y;
260 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000261 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262 __y->__right_ = __x;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000263 __x->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264}
265
266// Effects: Rebalances __root after attaching __x to a leaf.
267// Precondition: __root != nulptr && __x != nullptr.
268// __x has no children.
269// __x == __root or == a direct or indirect child of __root.
270// If __x were to be unlinked from __root (setting __root to
271// nullptr if __root == __x), __tree_invariant(__root) == true.
272// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
273// may be different than the value passed in as __root.
274template <class _NodePtr>
275void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000276__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277{
278 __x->__is_black_ = __x == __root;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000279 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280 {
281 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier70f5f872016-07-19 17:56:20 +0000282 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000284 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 if (__y != nullptr && !__y->__is_black_)
286 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000287 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000288 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000289 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290 __x->__is_black_ = __x == __root;
291 __y->__is_black_ = true;
292 }
293 else
294 {
295 if (!__tree_is_left_child(__x))
296 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000297 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 __tree_left_rotate(__x);
299 }
Eric Fiselier70f5f872016-07-19 17:56:20 +0000300 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000301 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000302 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303 __x->__is_black_ = false;
304 __tree_right_rotate(__x);
305 break;
306 }
307 }
308 else
309 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000310 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311 if (__y != nullptr && !__y->__is_black_)
312 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000313 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000315 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316 __x->__is_black_ = __x == __root;
317 __y->__is_black_ = true;
318 }
319 else
320 {
321 if (__tree_is_left_child(__x))
322 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000323 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 __tree_right_rotate(__x);
325 }
Eric Fiselier70f5f872016-07-19 17:56:20 +0000326 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000328 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000329 __x->__is_black_ = false;
330 __tree_left_rotate(__x);
331 break;
332 }
333 }
334 }
335}
336
337// Precondition: __root != nullptr && __z != nullptr.
338// __tree_invariant(__root) == true.
339// __z == __root or == a direct or indirect child of __root.
340// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
341// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
342// nor any of its children refer to __z. end_node->__left_
343// may be different than the value passed in as __root.
344template <class _NodePtr>
345void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000346__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000347{
348 // __z will be removed from the tree. Client still needs to destruct/deallocate it
349 // __y is either __z, or if __z has two children, __tree_next(__z).
350 // __y will have at most one child.
351 // __y will be the initial hole in the tree (make the hole at a leaf)
352 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
353 __z : __tree_next(__z);
354 // __x is __y's possibly null single child
355 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
356 // __w is __x's possibly null uncle (will become __x's sibling)
357 _NodePtr __w = nullptr;
358 // link __x to __y's parent, and find __w
359 if (__x != nullptr)
360 __x->__parent_ = __y->__parent_;
361 if (__tree_is_left_child(__y))
362 {
363 __y->__parent_->__left_ = __x;
364 if (__y != __root)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000365 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366 else
367 __root = __x; // __w == nullptr
368 }
369 else
370 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000371 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000372 // __y can't be root if it is a right child
373 __w = __y->__parent_->__left_;
374 }
375 bool __removed_black = __y->__is_black_;
376 // If we didn't remove __z, do so now by splicing in __y for __z,
377 // but copy __z's color. This does not impact __x or __w.
378 if (__y != __z)
379 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000380 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381 __y->__parent_ = __z->__parent_;
382 if (__tree_is_left_child(__z))
383 __y->__parent_->__left_ = __y;
384 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000385 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386 __y->__left_ = __z->__left_;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000387 __y->__left_->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 __y->__right_ = __z->__right_;
389 if (__y->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000390 __y->__right_->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 __y->__is_black_ = __z->__is_black_;
392 if (__root == __z)
393 __root = __y;
394 }
395 // There is no need to rebalance if we removed a red, or if we removed
396 // the last node.
397 if (__removed_black && __root != nullptr)
398 {
399 // Rebalance:
400 // __x has an implicit black color (transferred from the removed __y)
401 // associated with it, no matter what its color is.
402 // If __x is __root (in which case it can't be null), it is supposed
403 // to be black anyway, and if it is doubly black, then the double
404 // can just be ignored.
405 // If __x is red (in which case it can't be null), then it can absorb
406 // the implicit black just by setting its color to black.
407 // Since __y was black and only had one child (which __x points to), __x
408 // is either red with no children, else null, otherwise __y would have
409 // different black heights under left and right pointers.
410 // if (__x == __root || __x != nullptr && !__x->__is_black_)
411 if (__x != nullptr)
412 __x->__is_black_ = true;
413 else
414 {
415 // Else __x isn't root, and is "doubly black", even though it may
416 // be null. __w can not be null here, else the parent would
417 // see a black height >= 2 on the __x side and a black height
418 // of 1 on the __w side (__w must be a non-null black or a red
419 // with a non-null black child).
420 while (true)
421 {
422 if (!__tree_is_left_child(__w)) // if x is left child
423 {
424 if (!__w->__is_black_)
425 {
426 __w->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000427 __w->__parent_unsafe()->__is_black_ = false;
428 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429 // __x is still valid
430 // reset __root only if necessary
431 if (__root == __w->__left_)
432 __root = __w;
433 // reset sibling, and it still can't be null
434 __w = __w->__left_->__right_;
435 }
436 // __w->__is_black_ is now true, __w may have null children
437 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
438 (__w->__right_ == nullptr || __w->__right_->__is_black_))
439 {
440 __w->__is_black_ = false;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000441 __x = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442 // __x can no longer be null
443 if (__x == __root || !__x->__is_black_)
444 {
445 __x->__is_black_ = true;
446 break;
447 }
448 // reset sibling, and it still can't be null
449 __w = __tree_is_left_child(__x) ?
Eric Fiselier70f5f872016-07-19 17:56:20 +0000450 __x->__parent_unsafe()->__right_ :
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000451 __x->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000452 // continue;
453 }
454 else // __w has a red child
455 {
456 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
457 {
458 // __w left child is non-null and red
459 __w->__left_->__is_black_ = true;
460 __w->__is_black_ = false;
461 __tree_right_rotate(__w);
462 // __w is known not to be root, so root hasn't changed
463 // reset sibling, and it still can't be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000464 __w = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465 }
466 // __w has a right red child, left child may be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000467 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
468 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000469 __w->__right_->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000470 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 break;
472 }
473 }
474 else
475 {
476 if (!__w->__is_black_)
477 {
478 __w->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000479 __w->__parent_unsafe()->__is_black_ = false;
480 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481 // __x is still valid
482 // reset __root only if necessary
483 if (__root == __w->__right_)
484 __root = __w;
485 // reset sibling, and it still can't be null
486 __w = __w->__right_->__left_;
487 }
488 // __w->__is_black_ is now true, __w may have null children
489 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
490 (__w->__right_ == nullptr || __w->__right_->__is_black_))
491 {
492 __w->__is_black_ = false;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000493 __x = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494 // __x can no longer be null
495 if (!__x->__is_black_ || __x == __root)
496 {
497 __x->__is_black_ = true;
498 break;
499 }
500 // reset sibling, and it still can't be null
501 __w = __tree_is_left_child(__x) ?
Eric Fiselier70f5f872016-07-19 17:56:20 +0000502 __x->__parent_unsafe()->__right_ :
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000503 __x->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 // continue;
505 }
506 else // __w has a red child
507 {
508 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
509 {
510 // __w right child is non-null and red
511 __w->__right_->__is_black_ = true;
512 __w->__is_black_ = false;
513 __tree_left_rotate(__w);
514 // __w is known not to be root, so root hasn't changed
515 // reset sibling, and it still can't be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000516 __w = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517 }
518 // __w has a left red child, right child may be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000519 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
520 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000521 __w->__left_->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000522 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523 break;
524 }
525 }
526 }
527 }
528 }
529}
530
Eric Fiseliera00b4842016-02-20 05:28:30 +0000531// node traits
532
Eric Fiselierd06276b2016-03-31 02:15:15 +0000533
534#ifndef _LIBCPP_CXX03_LANG
535template <class _Tp>
536struct __is_tree_value_type_imp : false_type {};
537
538template <class _Key, class _Value>
539struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
540
541template <class ..._Args>
542struct __is_tree_value_type : false_type {};
543
544template <class _One>
545struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
546#endif
547
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#ifndef _LIBCPP_CXX03_LANG
568 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000569 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierd06276b2016-03-31 02:15:15 +0000570 return _VSTD::move(__v);
571 }
572#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000573};
574
575template <class _Key, class _Tp>
576struct __tree_key_value_types<__value_type<_Key, _Tp> > {
577 typedef _Key key_type;
578 typedef _Tp mapped_type;
579 typedef __value_type<_Key, _Tp> __node_value_type;
580 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000581 typedef __container_value_type __map_value_type;
582 static const bool __is_map = true;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000583
584 _LIBCPP_INLINE_VISIBILITY
585 static key_type const&
586 __get_key(__node_value_type const& __t) {
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000587 return __t.__get_value().first;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000588 }
589
590 template <class _Up>
591 _LIBCPP_INLINE_VISIBILITY
592 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
593 key_type const&>::type
594 __get_key(_Up& __t) {
595 return __t.first;
596 }
597
598 _LIBCPP_INLINE_VISIBILITY
599 static __container_value_type const&
600 __get_value(__node_value_type const& __t) {
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000601 return __t.__get_value();
Eric Fiselierd06276b2016-03-31 02:15:15 +0000602 }
603
604 template <class _Up>
605 _LIBCPP_INLINE_VISIBILITY
606 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
607 __container_value_type const&>::type
608 __get_value(_Up& __t) {
609 return __t;
610 }
611
612 _LIBCPP_INLINE_VISIBILITY
613 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000614 return _VSTD::addressof(__n.__get_value());
Eric Fiselierd06276b2016-03-31 02:15:15 +0000615 }
616
617#ifndef _LIBCPP_CXX03_LANG
618 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtond3fe2992018-06-04 20:38:23 +0000619 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
620 return __v.__move();
Eric Fiselierd06276b2016-03-31 02:15:15 +0000621 }
622#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000623};
624
625template <class _VoidPtr>
626struct __tree_node_base_types {
627 typedef _VoidPtr __void_pointer;
628
629 typedef __tree_node_base<__void_pointer> __node_base_type;
630 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
631 __node_base_pointer;
632
633 typedef __tree_end_node<__node_base_pointer> __end_node_type;
634 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
635 __end_node_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000636#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
637 typedef __end_node_pointer __parent_pointer;
638#else
639 typedef typename conditional<
640 is_pointer<__end_node_pointer>::value,
641 __end_node_pointer,
642 __node_base_pointer>::type __parent_pointer;
643#endif
644
Eric Fiseliera00b4842016-02-20 05:28:30 +0000645private:
646 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
647 "_VoidPtr does not point to unqualified void type");
648};
649
650template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
651 bool = _KVTypes::__is_map>
652struct __tree_map_pointer_types {};
653
654template <class _Tp, class _AllocPtr, class _KVTypes>
655struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
656 typedef typename _KVTypes::__map_value_type _Mv;
657 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
658 __map_value_type_pointer;
659 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
660 __const_map_value_type_pointer;
661};
662
663template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
664struct __tree_node_types;
665
666template <class _NodePtr, class _Tp, class _VoidPtr>
667struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
668 : public __tree_node_base_types<_VoidPtr>,
669 __tree_key_value_types<_Tp>,
670 __tree_map_pointer_types<_Tp, _VoidPtr>
671{
672 typedef __tree_node_base_types<_VoidPtr> __base;
673 typedef __tree_key_value_types<_Tp> __key_base;
674 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
675public:
676
677 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
678 typedef _NodePtr __node_pointer;
679
680 typedef _Tp __node_value_type;
681 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
682 __node_value_type_pointer;
683 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
684 __const_node_value_type_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000685#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
686 typedef typename __base::__end_node_pointer __iter_pointer;
687#else
688 typedef typename conditional<
689 is_pointer<__node_pointer>::value,
690 typename __base::__end_node_pointer,
691 __node_pointer>::type __iter_pointer;
692#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000693private:
694 static_assert(!is_const<__node_type>::value,
695 "_NodePtr should never be a pointer to const");
696 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
697 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
698};
699
700template <class _ValueTp, class _VoidPtr>
701struct __make_tree_node_types {
702 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
703 _NodePtr;
704 typedef __tree_node_types<_NodePtr> type;
705};
706
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707// node
708
709template <class _Pointer>
710class __tree_end_node
711{
712public:
713 typedef _Pointer pointer;
714 pointer __left_;
715
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000717 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000718};
719
720template <class _VoidPtr>
721class __tree_node_base
Eric Fiseliera00b4842016-02-20 05:28:30 +0000722 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantc51e1022010-05-11 19:42:16 +0000723{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000724 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
725
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000727 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000728 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000729
Eric Fiselier70f5f872016-07-19 17:56:20 +0000730 pointer __right_;
731 __parent_pointer __parent_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 bool __is_black_;
733
Eric Fiselier70f5f872016-07-19 17:56:20 +0000734 _LIBCPP_INLINE_VISIBILITY
735 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
736
737 _LIBCPP_INLINE_VISIBILITY
738 void __set_parent(pointer __p) {
739 __parent_ = static_cast<__parent_pointer>(__p);
740 }
741
Eric Fiselierd06276b2016-03-31 02:15:15 +0000742private:
743 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
744 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
745 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000746};
747
748template <class _Tp, class _VoidPtr>
749class __tree_node
750 : public __tree_node_base<_VoidPtr>
751{
752public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000753 typedef _Tp __node_value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000754
Eric Fiseliera00b4842016-02-20 05:28:30 +0000755 __node_value_type __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756
Eric Fiselierd06276b2016-03-31 02:15:15 +0000757private:
758 ~__tree_node() _LIBCPP_EQUAL_DELETE;
759 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
760 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000761};
762
Eric Fiselierd06276b2016-03-31 02:15:15 +0000763
764template <class _Allocator>
765class __tree_node_destructor
766{
767 typedef _Allocator allocator_type;
768 typedef allocator_traits<allocator_type> __alloc_traits;
769
770public:
771 typedef typename __alloc_traits::pointer pointer;
772private:
773 typedef __tree_node_types<pointer> _NodeTypes;
774 allocator_type& __na_;
775
776 __tree_node_destructor& operator=(const __tree_node_destructor&);
777
778public:
779 bool __value_constructed;
780
781 _LIBCPP_INLINE_VISIBILITY
782 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
783 : __na_(__na),
784 __value_constructed(__val)
785 {}
786
787 _LIBCPP_INLINE_VISIBILITY
788 void operator()(pointer __p) _NOEXCEPT
789 {
790 if (__value_constructed)
791 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
792 if (__p)
793 __alloc_traits::deallocate(__na_, __p, 1);
794 }
795
796 template <class> friend class __map_node_destructor;
797};
798
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +0000799#if _LIBCPP_STD_VER > 14
800template <class _NodeType, class _Alloc>
801struct __generic_container_node_destructor;
802template <class _Tp, class _VoidPtr, class _Alloc>
803struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc>
804 : __tree_node_destructor<_Alloc>
805{
806 using __tree_node_destructor<_Alloc>::__tree_node_destructor;
807};
808#endif
Eric Fiselierd06276b2016-03-31 02:15:15 +0000809
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000811class _LIBCPP_TEMPLATE_VIS __tree_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000813 typedef __tree_node_types<_NodePtr> _NodeTypes;
814 typedef _NodePtr __node_pointer;
815 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000816 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
817 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000818 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000819
Eric Fiselier70f5f872016-07-19 17:56:20 +0000820 __iter_pointer __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821
Howard Hinnantc51e1022010-05-11 19:42:16 +0000822public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000823 typedef bidirectional_iterator_tag iterator_category;
824 typedef _Tp value_type;
825 typedef _DiffType difference_type;
826 typedef value_type& reference;
827 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828
Marshall Clow8fc07302013-08-08 21:52:50 +0000829 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
830#if _LIBCPP_STD_VER > 11
831 : __ptr_(nullptr)
832#endif
833 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000834
Eric Fiselier70f5f872016-07-19 17:56:20 +0000835 _LIBCPP_INLINE_VISIBILITY reference operator*() const
836 {return __get_np()->__value_;}
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000837 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier70f5f872016-07-19 17:56:20 +0000838 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000839
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000840 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000841 __tree_iterator& operator++() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000842 __ptr_ = static_cast<__iter_pointer>(
843 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000844 return *this;
845 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000847 __tree_iterator operator++(int)
848 {__tree_iterator __t(*this); ++(*this); return __t;}
849
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000850 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000851 __tree_iterator& operator--() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000852 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
853 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000854 return *this;
855 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857 __tree_iterator operator--(int)
858 {__tree_iterator __t(*this); --(*this); return __t;}
859
Louis Dionne44bcff92018-08-03 22:36:53 +0000860 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000861 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000863 friend _LIBCPP_INLINE_VISIBILITY
864 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000865 {return !(__x == __y);}
866
867private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000869 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier70f5f872016-07-19 17:56:20 +0000870 _LIBCPP_INLINE_VISIBILITY
871 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
872 _LIBCPP_INLINE_VISIBILITY
873 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874 template <class, class, class> friend class __tree;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000875 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
876 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
877 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
878 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
879 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
880 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881};
882
Eric Fiseliera00b4842016-02-20 05:28:30 +0000883template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000884class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000885{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000886 typedef __tree_node_types<_NodePtr> _NodeTypes;
887 typedef typename _NodeTypes::__node_pointer __node_pointer;
888 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000889 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
890 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000891 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892
Eric Fiselier70f5f872016-07-19 17:56:20 +0000893 __iter_pointer __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894
Howard Hinnantc51e1022010-05-11 19:42:16 +0000895public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000896 typedef bidirectional_iterator_tag iterator_category;
897 typedef _Tp value_type;
898 typedef _DiffType difference_type;
899 typedef const value_type& reference;
900 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901
Marshall Clow8fc07302013-08-08 21:52:50 +0000902 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
903#if _LIBCPP_STD_VER > 11
904 : __ptr_(nullptr)
905#endif
906 {}
907
Howard Hinnantc51e1022010-05-11 19:42:16 +0000908private:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000909 typedef __tree_iterator<value_type, __node_pointer, difference_type>
910 __non_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000911public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000913 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
914 : __ptr_(__p.__ptr_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000915
Eric Fiselier70f5f872016-07-19 17:56:20 +0000916 _LIBCPP_INLINE_VISIBILITY reference operator*() const
917 {return __get_np()->__value_;}
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000918 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier70f5f872016-07-19 17:56:20 +0000919 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000920
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000921 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000922 __tree_const_iterator& operator++() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000923 __ptr_ = static_cast<__iter_pointer>(
924 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000925 return *this;
926 }
927
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 __tree_const_iterator operator++(int)
930 {__tree_const_iterator __t(*this); ++(*this); return __t;}
931
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000932 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000933 __tree_const_iterator& operator--() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000934 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
935 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000936 return *this;
937 }
938
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000940 __tree_const_iterator operator--(int)
941 {__tree_const_iterator __t(*this); --(*this); return __t;}
942
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000943 friend _LIBCPP_INLINE_VISIBILITY
944 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000946 friend _LIBCPP_INLINE_VISIBILITY
947 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948 {return !(__x == __y);}
949
950private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000952 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
953 : __ptr_(__p) {}
Eric Fiselier70f5f872016-07-19 17:56:20 +0000954 _LIBCPP_INLINE_VISIBILITY
955 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
956 : __ptr_(__p) {}
957 _LIBCPP_INLINE_VISIBILITY
958 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
959
Howard Hinnantc51e1022010-05-11 19:42:16 +0000960 template <class, class, class> friend class __tree;
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000961 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
962 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
963 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
964 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
965 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000966
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967};
968
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000969#ifndef _LIBCPP_CXX03_LANG
970template <class _Tp, class _Compare, class _Allocator>
971struct __diagnose_tree_helper {
972 static constexpr bool __trigger_diagnostics()
Eric Fiselier71c26752017-01-13 22:42:53 +0000973 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000974 "the specified comparator type does not provide a const call operator")
975 { return true; }
976};
977
978template <class _Key, class _Value, class _KeyComp, class _Alloc>
979struct __diagnose_tree_helper<
980 __value_type<_Key, _Value>,
981 __map_value_compare<_Key, __value_type<_Key, _Value>, _KeyComp>,
982 _Alloc
Eric Fiselier71c26752017-01-13 22:42:53 +0000983> : __diagnose_tree_helper<_Key, _KeyComp, _Alloc>
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000984{
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000985};
Eric Fiselier71c26752017-01-13 22:42:53 +0000986#endif // !_LIBCPP_CXX03_LANG
Eric Fiseliera7a14ed2017-01-13 22:02:08 +0000987
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988template <class _Tp, class _Compare, class _Allocator>
989class __tree
990{
991public:
992 typedef _Tp value_type;
993 typedef _Compare value_compare;
994 typedef _Allocator allocator_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000995
996private:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000998 typedef typename __make_tree_node_types<value_type,
999 typename __alloc_traits::void_pointer>::type
1000 _NodeTypes;
Eric Fiselierd06276b2016-03-31 02:15:15 +00001001 typedef typename _NodeTypes::key_type key_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001002public:
1003 typedef typename _NodeTypes::__node_value_type __node_value_type;
1004 typedef typename _NodeTypes::__container_value_type __container_value_type;
1005
Howard Hinnantc51e1022010-05-11 19:42:16 +00001006 typedef typename __alloc_traits::pointer pointer;
1007 typedef typename __alloc_traits::const_pointer const_pointer;
1008 typedef typename __alloc_traits::size_type size_type;
1009 typedef typename __alloc_traits::difference_type difference_type;
1010
Eric Fiseliera00b4842016-02-20 05:28:30 +00001011public:
1012 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001013
Eric Fiseliera00b4842016-02-20 05:28:30 +00001014 typedef typename _NodeTypes::__node_type __node;
1015 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001016
1017 typedef typename _NodeTypes::__node_base_type __node_base;
1018 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001019
1020 typedef typename _NodeTypes::__end_node_type __end_node_t;
1021 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001022
Eric Fiselier70f5f872016-07-19 17:56:20 +00001023 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
1024 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
1025
Marshall Clow940e01c2015-04-07 05:21:38 +00001026 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiseliera00b4842016-02-20 05:28:30 +00001027 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028
Eric Fiseliera00b4842016-02-20 05:28:30 +00001029private:
1030 // check for sane allocator pointer rebinding semantics. Rebinding the
1031 // allocator for a new pointer type should be exactly the same as rebinding
1032 // the pointer using 'pointer_traits'.
1033 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1034 "Allocator does not rebind pointers in a sane manner.");
1035 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1036 __node_base_allocator;
1037 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1038 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1039 "Allocator does not rebind pointers in a sane manner.");
1040
1041private:
Eric Fiselier70f5f872016-07-19 17:56:20 +00001042 __iter_pointer __begin_node_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1044 __compressed_pair<size_type, value_compare> __pair3_;
1045
1046public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001047 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001048 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001050 return static_cast<__iter_pointer>(
Eric Fiseliera92b0732016-02-20 07:12:17 +00001051 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1052 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001054 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001055 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001057 return static_cast<__iter_pointer>(
Eric Fiseliera92b0732016-02-20 07:12:17 +00001058 pointer_traits<__end_node_ptr>::pointer_to(
1059 const_cast<__end_node_t&>(__pair1_.first())
1060 )
1061 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001064 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001067 const __node_allocator& __node_alloc() const _NOEXCEPT
1068 {return __pair1_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001069 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001070 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001071 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001072 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001075 allocator_type __alloc() const _NOEXCEPT
1076 {return allocator_type(__node_alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001079 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001080public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001082 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001084 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001086 const value_compare& value_comp() const _NOEXCEPT
1087 {return __pair3_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088public:
Eric Fiseliera92b0732016-02-20 07:12:17 +00001089
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001090 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera92b0732016-02-20 07:12:17 +00001091 __node_pointer __root() const _NOEXCEPT
1092 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093
Eric Fiselier70f5f872016-07-19 17:56:20 +00001094 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1095 return _VSTD::addressof(__end_node()->__left_);
1096 }
1097
Howard Hinnantc51e1022010-05-11 19:42:16 +00001098 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001099 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001100
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001101 explicit __tree(const value_compare& __comp)
1102 _NOEXCEPT_(
1103 is_nothrow_default_constructible<__node_allocator>::value &&
1104 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105 explicit __tree(const allocator_type& __a);
1106 __tree(const value_compare& __comp, const allocator_type& __a);
1107 __tree(const __tree& __t);
1108 __tree& operator=(const __tree& __t);
1109 template <class _InputIterator>
1110 void __assign_unique(_InputIterator __first, _InputIterator __last);
1111 template <class _InputIterator>
1112 void __assign_multi(_InputIterator __first, _InputIterator __last);
Eric Fiselierb63508a2017-04-19 01:23:04 +00001113#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001114 __tree(__tree&& __t)
1115 _NOEXCEPT_(
1116 is_nothrow_move_constructible<__node_allocator>::value &&
1117 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001118 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001119 __tree& operator=(__tree&& __t)
1120 _NOEXCEPT_(
1121 __node_traits::propagate_on_container_move_assignment::value &&
1122 is_nothrow_move_assignable<value_compare>::value &&
1123 is_nothrow_move_assignable<__node_allocator>::value);
Eric Fiselierb63508a2017-04-19 01:23:04 +00001124#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001125
1126 ~__tree();
1127
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001129 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001131 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001133 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001135 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001138 size_type max_size() const _NOEXCEPT
Eric Fiselierb5d9f442016-11-23 01:18:56 +00001139 {return std::min<size_type>(
1140 __node_traits::max_size(__node_alloc()),
1141 numeric_limits<difference_type >::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001142
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001143 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001144
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001145 void swap(__tree& __t)
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001146#if _LIBCPP_STD_VER <= 11
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001147 _NOEXCEPT_(
Marshall Clow8982dcd2015-07-13 20:04:56 +00001148 __is_nothrow_swappable<value_compare>::value
Marshall Clow8982dcd2015-07-13 20:04:56 +00001149 && (!__node_traits::propagate_on_container_swap::value ||
1150 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001151 );
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001152#else
1153 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1154#endif
Eric Fiselierd06276b2016-03-31 02:15:15 +00001155
1156#ifndef _LIBCPP_CXX03_LANG
1157 template <class _Key, class ..._Args>
1158 pair<iterator, bool>
1159 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1160 template <class _Key, class ..._Args>
1161 iterator
1162 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001163
1164 template <class... _Args>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001165 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierd06276b2016-03-31 02:15:15 +00001166
Howard Hinnantc51e1022010-05-11 19:42:16 +00001167 template <class... _Args>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001168 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001169
Eric Fiselierd06276b2016-03-31 02:15:15 +00001170 template <class... _Args>
1171 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001172
Eric Fiselierd06276b2016-03-31 02:15:15 +00001173 template <class... _Args>
1174 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001175
1176 template <class _Pp>
1177 _LIBCPP_INLINE_VISIBILITY
1178 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1179 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1180 __can_extract_key<_Pp, key_type>());
1181 }
1182
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001183 template <class _First, class _Second>
1184 _LIBCPP_INLINE_VISIBILITY
1185 typename enable_if<
1186 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1187 pair<iterator, bool>
1188 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1189 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1190 _VSTD::forward<_Second>(__s));
1191 }
1192
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001193 template <class... _Args>
1194 _LIBCPP_INLINE_VISIBILITY
1195 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1196 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1197 }
1198
1199 template <class _Pp>
1200 _LIBCPP_INLINE_VISIBILITY
1201 pair<iterator, bool>
1202 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1203 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1204 }
1205
1206 template <class _Pp>
1207 _LIBCPP_INLINE_VISIBILITY
1208 pair<iterator, bool>
1209 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1210 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1211 }
1212
1213 template <class _Pp>
1214 _LIBCPP_INLINE_VISIBILITY
1215 pair<iterator, bool>
1216 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1217 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1218 }
1219
1220 template <class _Pp>
1221 _LIBCPP_INLINE_VISIBILITY
1222 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1223 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1224 __can_extract_key<_Pp, key_type>());
1225 }
1226
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001227 template <class _First, class _Second>
1228 _LIBCPP_INLINE_VISIBILITY
1229 typename enable_if<
1230 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1231 iterator
1232 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1233 return __emplace_hint_unique_key_args(__p, __f,
1234 _VSTD::forward<_First>(__f),
1235 _VSTD::forward<_Second>(__s));
1236 }
1237
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001238 template <class... _Args>
1239 _LIBCPP_INLINE_VISIBILITY
1240 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1241 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1242 }
1243
1244 template <class _Pp>
1245 _LIBCPP_INLINE_VISIBILITY
1246 iterator
1247 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1248 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1249 }
1250
1251 template <class _Pp>
1252 _LIBCPP_INLINE_VISIBILITY
1253 iterator
1254 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1255 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1256 }
1257
1258 template <class _Pp>
1259 _LIBCPP_INLINE_VISIBILITY
1260 iterator
1261 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1262 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1263 }
1264
Eric Fiselierd06276b2016-03-31 02:15:15 +00001265#else
1266 template <class _Key, class _Args>
1267 _LIBCPP_INLINE_VISIBILITY
1268 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1269 template <class _Key, class _Args>
1270 _LIBCPP_INLINE_VISIBILITY
1271 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clowd430d022016-01-05 19:32:41 +00001272#endif
1273
Eric Fiselierd06276b2016-03-31 02:15:15 +00001274 _LIBCPP_INLINE_VISIBILITY
1275 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1276 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1277 }
1278
1279 _LIBCPP_INLINE_VISIBILITY
1280 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1281 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1282 }
1283
1284#ifdef _LIBCPP_CXX03_LANG
1285 _LIBCPP_INLINE_VISIBILITY
1286 iterator __insert_multi(const __container_value_type& __v);
1287 _LIBCPP_INLINE_VISIBILITY
1288 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1289#else
1290 _LIBCPP_INLINE_VISIBILITY
1291 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1292 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1293 }
1294
1295 _LIBCPP_INLINE_VISIBILITY
1296 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1297 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1298 }
1299
1300 template <class _Vp, class = typename enable_if<
1301 !is_same<typename __unconstref<_Vp>::type,
1302 __container_value_type
1303 >::value
1304 >::type>
1305 _LIBCPP_INLINE_VISIBILITY
1306 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1307 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1308 }
1309
1310 template <class _Vp, class = typename enable_if<
1311 !is_same<typename __unconstref<_Vp>::type,
1312 __container_value_type
1313 >::value
1314 >::type>
1315 _LIBCPP_INLINE_VISIBILITY
1316 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1317 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1318 }
1319
1320 _LIBCPP_INLINE_VISIBILITY
1321 iterator __insert_multi(__container_value_type&& __v) {
1322 return __emplace_multi(_VSTD::move(__v));
1323 }
1324
1325 _LIBCPP_INLINE_VISIBILITY
1326 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1327 return __emplace_hint_multi(__p, _VSTD::move(__v));
1328 }
1329
1330 template <class _Vp>
1331 _LIBCPP_INLINE_VISIBILITY
1332 iterator __insert_multi(_Vp&& __v) {
1333 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1334 }
1335
1336 template <class _Vp>
1337 _LIBCPP_INLINE_VISIBILITY
1338 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1339 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1340 }
1341
1342#endif // !_LIBCPP_CXX03_LANG
1343
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001345 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001346 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 iterator __node_insert_unique(const_iterator __p,
1348 __node_pointer __nd);
1349
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001350 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001351 iterator __node_insert_multi(__node_pointer __nd);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001353 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1354
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001355
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001356 _LIBCPP_INLINE_VISIBILITY iterator
1357 __remove_node_pointer(__node_pointer) _NOEXCEPT;
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001358
1359#if _LIBCPP_STD_VER > 14
1360 template <class _NodeHandle, class _InsertReturnType>
1361 _LIBCPP_INLINE_VISIBILITY
1362 _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1363 template <class _NodeHandle>
1364 _LIBCPP_INLINE_VISIBILITY
1365 iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001366 template <class _Tree>
1367 _LIBCPP_INLINE_VISIBILITY
1368 void __node_handle_merge_unique(_Tree& __source);
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001369
1370 template <class _NodeHandle>
1371 _LIBCPP_INLINE_VISIBILITY
1372 iterator __node_handle_insert_multi(_NodeHandle&&);
1373 template <class _NodeHandle>
1374 _LIBCPP_INLINE_VISIBILITY
1375 iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001376 template <class _Tree>
1377 _LIBCPP_INLINE_VISIBILITY
1378 void __node_handle_merge_multi(_Tree& __source);
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00001379
1380
1381 template <class _NodeHandle>
1382 _LIBCPP_INLINE_VISIBILITY
1383 _NodeHandle __node_handle_extract(key_type const&);
1384 template <class _NodeHandle>
1385 _LIBCPP_INLINE_VISIBILITY
1386 _NodeHandle __node_handle_extract(const_iterator);
1387#endif
1388
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389 iterator erase(const_iterator __p);
1390 iterator erase(const_iterator __f, const_iterator __l);
1391 template <class _Key>
1392 size_type __erase_unique(const _Key& __k);
1393 template <class _Key>
1394 size_type __erase_multi(const _Key& __k);
1395
Eric Fiselier70f5f872016-07-19 17:56:20 +00001396 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 __node_base_pointer& __child,
Erik Pilkington82a65ad2018-10-31 17:31:35 +00001398 __node_base_pointer __new_node) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001399
1400 template <class _Key>
1401 iterator find(const _Key& __v);
1402 template <class _Key>
1403 const_iterator find(const _Key& __v) const;
1404
1405 template <class _Key>
1406 size_type __count_unique(const _Key& __k) const;
1407 template <class _Key>
1408 size_type __count_multi(const _Key& __k) const;
1409
1410 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001412 iterator lower_bound(const _Key& __v)
1413 {return __lower_bound(__v, __root(), __end_node());}
1414 template <class _Key>
1415 iterator __lower_bound(const _Key& __v,
1416 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001417 __iter_pointer __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001418 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001420 const_iterator lower_bound(const _Key& __v) const
1421 {return __lower_bound(__v, __root(), __end_node());}
1422 template <class _Key>
1423 const_iterator __lower_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00001424 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001425 __iter_pointer __result) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001426 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001427 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001428 iterator upper_bound(const _Key& __v)
1429 {return __upper_bound(__v, __root(), __end_node());}
1430 template <class _Key>
1431 iterator __upper_bound(const _Key& __v,
1432 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001433 __iter_pointer __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001434 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 const_iterator upper_bound(const _Key& __v) const
1437 {return __upper_bound(__v, __root(), __end_node());}
1438 template <class _Key>
1439 const_iterator __upper_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00001440 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001441 __iter_pointer __result) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001442 template <class _Key>
1443 pair<iterator, iterator>
1444 __equal_range_unique(const _Key& __k);
1445 template <class _Key>
1446 pair<const_iterator, const_iterator>
1447 __equal_range_unique(const _Key& __k) const;
1448
1449 template <class _Key>
1450 pair<iterator, iterator>
1451 __equal_range_multi(const _Key& __k);
1452 template <class _Key>
1453 pair<const_iterator, const_iterator>
1454 __equal_range_multi(const _Key& __k) const;
1455
Howard Hinnantc834c512011-11-29 18:15:50 +00001456 typedef __tree_node_destructor<__node_allocator> _Dp;
1457 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458
Howard Hinnant1113b5e2011-06-04 17:10:24 +00001459 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001460private:
Eric Fiselier70f5f872016-07-19 17:56:20 +00001461 __node_base_pointer&
1462 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1463 __node_base_pointer&
1464 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1465 __node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001466 __find_leaf(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001467 __parent_pointer& __parent, const key_type& __v);
Eric Fiseliercf8c0212017-01-05 06:06:18 +00001468 // FIXME: Make this function const qualified. Unfortunetly doing so
1469 // breaks existing code which uses non-const callable comparators.
Howard Hinnantc51e1022010-05-11 19:42:16 +00001470 template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001471 __node_base_pointer&
1472 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001473 template <class _Key>
Eric Fiseliercf8c0212017-01-05 06:06:18 +00001474 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1475 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1476 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1477 }
1478 template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001479 __node_base_pointer&
1480 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1481 __node_base_pointer& __dummy,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001482 const _Key& __v);
1483
Eric Fiselierd06276b2016-03-31 02:15:15 +00001484#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001485 template <class ..._Args>
Eric Fiselierd06276b2016-03-31 02:15:15 +00001486 __node_holder __construct_node(_Args&& ...__args);
1487#else
1488 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001489#endif
1490
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001491 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001492
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001494 void __copy_assign_alloc(const __tree& __t)
1495 {__copy_assign_alloc(__t, integral_constant<bool,
1496 __node_traits::propagate_on_container_copy_assignment::value>());}
1497
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001498 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001499 void __copy_assign_alloc(const __tree& __t, true_type)
Marshall Clowac8a40b2016-08-17 23:24:02 +00001500 {
1501 if (__node_alloc() != __t.__node_alloc())
Louis Dionne44bcff92018-08-03 22:36:53 +00001502 clear();
Marshall Clowac8a40b2016-08-17 23:24:02 +00001503 __node_alloc() = __t.__node_alloc();
1504 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001505 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001506 void __copy_assign_alloc(const __tree&, false_type) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001507
1508 void __move_assign(__tree& __t, false_type);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001509 void __move_assign(__tree& __t, true_type)
1510 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1511 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001512
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001513 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001514 void __move_assign_alloc(__tree& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001515 _NOEXCEPT_(
1516 !__node_traits::propagate_on_container_move_assignment::value ||
1517 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001518 {__move_assign_alloc(__t, integral_constant<bool,
1519 __node_traits::propagate_on_container_move_assignment::value>());}
1520
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001521 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001522 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001523 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001524 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001525 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier6003c772016-12-23 23:37:52 +00001526 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001527
Howard Hinnantc51e1022010-05-11 19:42:16 +00001528 __node_pointer __detach();
1529 static __node_pointer __detach(__node_pointer);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001530
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +00001531 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1532 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001533};
1534
1535template <class _Tp, class _Compare, class _Allocator>
1536__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001537 _NOEXCEPT_(
1538 is_nothrow_default_constructible<__node_allocator>::value &&
1539 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001540 : __pair3_(0, __comp)
1541{
1542 __begin_node() = __end_node();
1543}
1544
1545template <class _Tp, class _Compare, class _Allocator>
1546__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001547 : __begin_node_(__iter_pointer()),
Eric Fiselier68b5f0b2017-04-13 00:34:24 +00001548 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 __pair3_(0)
1550{
1551 __begin_node() = __end_node();
1552}
1553
1554template <class _Tp, class _Compare, class _Allocator>
1555__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1556 const allocator_type& __a)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001557 : __begin_node_(__iter_pointer()),
Eric Fiselier68b5f0b2017-04-13 00:34:24 +00001558 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001559 __pair3_(0, __comp)
1560{
1561 __begin_node() = __end_node();
1562}
1563
1564// Precondition: size() != 0
1565template <class _Tp, class _Compare, class _Allocator>
1566typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1567__tree<_Tp, _Compare, _Allocator>::__detach()
1568{
Eric Fiselier70f5f872016-07-19 17:56:20 +00001569 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001570 __begin_node() = __end_node();
1571 __end_node()->__left_->__parent_ = nullptr;
1572 __end_node()->__left_ = nullptr;
1573 size() = 0;
1574 // __cache->__left_ == nullptr
1575 if (__cache->__right_ != nullptr)
1576 __cache = static_cast<__node_pointer>(__cache->__right_);
1577 // __cache->__left_ == nullptr
1578 // __cache->__right_ == nullptr
1579 return __cache;
1580}
1581
1582// Precondition: __cache != nullptr
1583// __cache->left_ == nullptr
1584// __cache->right_ == nullptr
1585// This is no longer a red-black tree
1586template <class _Tp, class _Compare, class _Allocator>
1587typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1588__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1589{
1590 if (__cache->__parent_ == nullptr)
1591 return nullptr;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001592 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001593 {
1594 __cache->__parent_->__left_ = nullptr;
1595 __cache = static_cast<__node_pointer>(__cache->__parent_);
1596 if (__cache->__right_ == nullptr)
1597 return __cache;
1598 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1599 }
1600 // __cache is right child
Eric Fiselier70f5f872016-07-19 17:56:20 +00001601 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 __cache = static_cast<__node_pointer>(__cache->__parent_);
1603 if (__cache->__left_ == nullptr)
1604 return __cache;
1605 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1606}
1607
1608template <class _Tp, class _Compare, class _Allocator>
1609__tree<_Tp, _Compare, _Allocator>&
1610__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1611{
1612 if (this != &__t)
1613 {
1614 value_comp() = __t.value_comp();
1615 __copy_assign_alloc(__t);
1616 __assign_multi(__t.begin(), __t.end());
1617 }
1618 return *this;
1619}
1620
1621template <class _Tp, class _Compare, class _Allocator>
1622template <class _InputIterator>
1623void
1624__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1625{
Eric Fiselierd06276b2016-03-31 02:15:15 +00001626 typedef iterator_traits<_InputIterator> _ITraits;
1627 typedef typename _ITraits::value_type _ItValueType;
1628 static_assert((is_same<_ItValueType, __container_value_type>::value),
1629 "__assign_unique may only be called with the containers value type");
1630
Howard Hinnantc51e1022010-05-11 19:42:16 +00001631 if (size() != 0)
1632 {
1633 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001634#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001635 try
1636 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001637#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001638 for (; __cache != nullptr && __first != __last; ++__first)
1639 {
1640 __cache->__value_ = *__first;
1641 __node_pointer __next = __detach(__cache);
1642 __node_insert_unique(__cache);
1643 __cache = __next;
1644 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001645#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001646 }
1647 catch (...)
1648 {
1649 while (__cache->__parent_ != nullptr)
1650 __cache = static_cast<__node_pointer>(__cache->__parent_);
1651 destroy(__cache);
1652 throw;
1653 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001654#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655 if (__cache != nullptr)
1656 {
1657 while (__cache->__parent_ != nullptr)
1658 __cache = static_cast<__node_pointer>(__cache->__parent_);
1659 destroy(__cache);
1660 }
1661 }
1662 for (; __first != __last; ++__first)
1663 __insert_unique(*__first);
1664}
1665
1666template <class _Tp, class _Compare, class _Allocator>
1667template <class _InputIterator>
1668void
1669__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1670{
Eric Fiselierd06276b2016-03-31 02:15:15 +00001671 typedef iterator_traits<_InputIterator> _ITraits;
1672 typedef typename _ITraits::value_type _ItValueType;
1673 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1674 is_same<_ItValueType, __node_value_type>::value),
1675 "__assign_multi may only be called with the containers value type"
1676 " or the nodes value type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001677 if (size() != 0)
1678 {
1679 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001680#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001681 try
1682 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001683#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001684 for (; __cache != nullptr && __first != __last; ++__first)
1685 {
1686 __cache->__value_ = *__first;
1687 __node_pointer __next = __detach(__cache);
1688 __node_insert_multi(__cache);
1689 __cache = __next;
1690 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001691#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692 }
1693 catch (...)
1694 {
1695 while (__cache->__parent_ != nullptr)
1696 __cache = static_cast<__node_pointer>(__cache->__parent_);
1697 destroy(__cache);
1698 throw;
1699 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001700#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001701 if (__cache != nullptr)
1702 {
1703 while (__cache->__parent_ != nullptr)
1704 __cache = static_cast<__node_pointer>(__cache->__parent_);
1705 destroy(__cache);
1706 }
1707 }
1708 for (; __first != __last; ++__first)
Eric Fiselierd06276b2016-03-31 02:15:15 +00001709 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001710}
1711
1712template <class _Tp, class _Compare, class _Allocator>
1713__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001714 : __begin_node_(__iter_pointer()),
Eric Fiselier68b5f0b2017-04-13 00:34:24 +00001715 __pair1_(__second_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001716 __pair3_(0, __t.value_comp())
1717{
1718 __begin_node() = __end_node();
1719}
1720
Eric Fiselierb63508a2017-04-19 01:23:04 +00001721#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001722
1723template <class _Tp, class _Compare, class _Allocator>
1724__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001725 _NOEXCEPT_(
1726 is_nothrow_move_constructible<__node_allocator>::value &&
1727 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001728 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1729 __pair1_(_VSTD::move(__t.__pair1_)),
1730 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001731{
1732 if (size() == 0)
1733 __begin_node() = __end_node();
1734 else
1735 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001736 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001737 __t.__begin_node() = __t.__end_node();
1738 __t.__end_node()->__left_ = nullptr;
1739 __t.size() = 0;
1740 }
1741}
1742
1743template <class _Tp, class _Compare, class _Allocator>
1744__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
Eric Fiselier68b5f0b2017-04-13 00:34:24 +00001745 : __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001746 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001747{
1748 if (__a == __t.__alloc())
1749 {
1750 if (__t.size() == 0)
1751 __begin_node() = __end_node();
1752 else
1753 {
1754 __begin_node() = __t.__begin_node();
1755 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier70f5f872016-07-19 17:56:20 +00001756 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001757 size() = __t.size();
1758 __t.__begin_node() = __t.__end_node();
1759 __t.__end_node()->__left_ = nullptr;
1760 __t.size() = 0;
1761 }
1762 }
1763 else
1764 {
1765 __begin_node() = __end_node();
1766 }
1767}
1768
1769template <class _Tp, class _Compare, class _Allocator>
1770void
1771__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001772 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1773 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001774{
1775 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1776 __begin_node_ = __t.__begin_node_;
1777 __pair1_.first() = __t.__pair1_.first();
1778 __move_assign_alloc(__t);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001779 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001780 if (size() == 0)
1781 __begin_node() = __end_node();
1782 else
1783 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001784 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785 __t.__begin_node() = __t.__end_node();
1786 __t.__end_node()->__left_ = nullptr;
1787 __t.size() = 0;
1788 }
1789}
1790
1791template <class _Tp, class _Compare, class _Allocator>
1792void
1793__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1794{
1795 if (__node_alloc() == __t.__node_alloc())
1796 __move_assign(__t, true_type());
1797 else
1798 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001799 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001800 const_iterator __e = end();
1801 if (size() != 0)
1802 {
1803 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001804#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 try
1806 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001807#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808 while (__cache != nullptr && __t.size() != 0)
1809 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001810 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001811 __node_pointer __next = __detach(__cache);
1812 __node_insert_multi(__cache);
1813 __cache = __next;
1814 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001815#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001816 }
1817 catch (...)
1818 {
1819 while (__cache->__parent_ != nullptr)
1820 __cache = static_cast<__node_pointer>(__cache->__parent_);
1821 destroy(__cache);
1822 throw;
1823 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001824#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001825 if (__cache != nullptr)
1826 {
1827 while (__cache->__parent_ != nullptr)
1828 __cache = static_cast<__node_pointer>(__cache->__parent_);
1829 destroy(__cache);
1830 }
1831 }
1832 while (__t.size() != 0)
Eric Fiselierd06276b2016-03-31 02:15:15 +00001833 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001834 }
1835}
1836
1837template <class _Tp, class _Compare, class _Allocator>
1838__tree<_Tp, _Compare, _Allocator>&
1839__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001840 _NOEXCEPT_(
1841 __node_traits::propagate_on_container_move_assignment::value &&
1842 is_nothrow_move_assignable<value_compare>::value &&
1843 is_nothrow_move_assignable<__node_allocator>::value)
Louis Dionne44bcff92018-08-03 22:36:53 +00001844
Howard Hinnantc51e1022010-05-11 19:42:16 +00001845{
1846 __move_assign(__t, integral_constant<bool,
1847 __node_traits::propagate_on_container_move_assignment::value>());
1848 return *this;
1849}
1850
Eric Fiselierb63508a2017-04-19 01:23:04 +00001851#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001852
1853template <class _Tp, class _Compare, class _Allocator>
1854__tree<_Tp, _Compare, _Allocator>::~__tree()
1855{
Marshall Clow140d9432016-06-30 22:05:45 +00001856 static_assert((is_copy_constructible<value_compare>::value),
1857 "Comparator must be copy-constructible.");
Eric Fiseliera7a14ed2017-01-13 22:02:08 +00001858#ifndef _LIBCPP_CXX03_LANG
1859 static_assert((__diagnose_tree_helper<_Tp, _Compare, _Allocator>::
1860 __trigger_diagnostics()), "");
1861#endif
1862 destroy(__root());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001863}
1864
1865template <class _Tp, class _Compare, class _Allocator>
1866void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001867__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001868{
1869 if (__nd != nullptr)
1870 {
1871 destroy(static_cast<__node_pointer>(__nd->__left_));
1872 destroy(static_cast<__node_pointer>(__nd->__right_));
1873 __node_allocator& __na = __node_alloc();
Eric Fiselierd06276b2016-03-31 02:15:15 +00001874 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875 __node_traits::deallocate(__na, __nd, 1);
1876 }
1877}
1878
1879template <class _Tp, class _Compare, class _Allocator>
1880void
1881__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001882#if _LIBCPP_STD_VER <= 11
Marshall Clow8982dcd2015-07-13 20:04:56 +00001883 _NOEXCEPT_(
1884 __is_nothrow_swappable<value_compare>::value
Marshall Clow8982dcd2015-07-13 20:04:56 +00001885 && (!__node_traits::propagate_on_container_swap::value ||
1886 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001887 )
Dimitry Andrice3dda3f2016-08-27 19:32:03 +00001888#else
1889 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1890#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001891{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001892 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001893 swap(__begin_node_, __t.__begin_node_);
1894 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00001895 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001896 __pair3_.swap(__t.__pair3_);
1897 if (size() == 0)
1898 __begin_node() = __end_node();
1899 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00001900 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001901 if (__t.size() == 0)
1902 __t.__begin_node() = __t.__end_node();
1903 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00001904 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001905}
1906
1907template <class _Tp, class _Compare, class _Allocator>
1908void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001909__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001910{
1911 destroy(__root());
1912 size() = 0;
1913 __begin_node() = __end_node();
1914 __end_node()->__left_ = nullptr;
1915}
1916
1917// Find lower_bound place to insert
1918// Set __parent to parent of null leaf
1919// Return reference to null leaf
1920template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001921typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1922__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001923 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001924{
1925 __node_pointer __nd = __root();
1926 if (__nd != nullptr)
1927 {
1928 while (true)
1929 {
1930 if (value_comp()(__nd->__value_, __v))
1931 {
1932 if (__nd->__right_ != nullptr)
1933 __nd = static_cast<__node_pointer>(__nd->__right_);
1934 else
1935 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001936 __parent = static_cast<__parent_pointer>(__nd);
1937 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001938 }
1939 }
1940 else
1941 {
1942 if (__nd->__left_ != nullptr)
1943 __nd = static_cast<__node_pointer>(__nd->__left_);
1944 else
1945 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001946 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001947 return __parent->__left_;
1948 }
1949 }
1950 }
1951 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001952 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001953 return __parent->__left_;
1954}
1955
1956// Find upper_bound place to insert
1957// Set __parent to parent of null leaf
1958// Return reference to null leaf
1959template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001960typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1961__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001962 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001963{
1964 __node_pointer __nd = __root();
1965 if (__nd != nullptr)
1966 {
1967 while (true)
1968 {
1969 if (value_comp()(__v, __nd->__value_))
1970 {
1971 if (__nd->__left_ != nullptr)
1972 __nd = static_cast<__node_pointer>(__nd->__left_);
1973 else
1974 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001975 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001976 return __parent->__left_;
1977 }
1978 }
1979 else
1980 {
1981 if (__nd->__right_ != nullptr)
1982 __nd = static_cast<__node_pointer>(__nd->__right_);
1983 else
1984 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001985 __parent = static_cast<__parent_pointer>(__nd);
1986 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001987 }
1988 }
1989 }
1990 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001991 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001992 return __parent->__left_;
1993}
1994
1995// Find leaf place to insert closest to __hint
1996// First check prior to __hint.
1997// Next check after __hint.
1998// Next do O(log N) search.
1999// Set __parent to parent of null leaf
2000// Return reference to null leaf
2001template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00002002typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002003__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002004 __parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00002005 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002006{
2007 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
2008 {
2009 // __v <= *__hint
2010 const_iterator __prior = __hint;
2011 if (__prior == begin() || !value_comp()(__v, *--__prior))
2012 {
2013 // *prev(__hint) <= __v <= *__hint
2014 if (__hint.__ptr_->__left_ == nullptr)
2015 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002016 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002017 return __parent->__left_;
2018 }
2019 else
2020 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002021 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2022 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002023 }
2024 }
2025 // __v < *prev(__hint)
2026 return __find_leaf_high(__parent, __v);
2027 }
2028 // else __v > *__hint
2029 return __find_leaf_low(__parent, __v);
2030}
2031
2032// Find place to insert if __v doesn't exist
2033// Set __parent to parent of null leaf
2034// Return reference to null leaf
2035// If __v exists, set parent to node of __v and return reference to node of __v
2036template <class _Tp, class _Compare, class _Allocator>
2037template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00002038typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
2039__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002040 const _Key& __v)
2041{
2042 __node_pointer __nd = __root();
Eric Fiselier70f5f872016-07-19 17:56:20 +00002043 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002044 if (__nd != nullptr)
2045 {
2046 while (true)
2047 {
2048 if (value_comp()(__v, __nd->__value_))
2049 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002050 if (__nd->__left_ != nullptr) {
2051 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002053 } else {
2054 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002055 return __parent->__left_;
2056 }
2057 }
2058 else if (value_comp()(__nd->__value_, __v))
2059 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002060 if (__nd->__right_ != nullptr) {
2061 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002062 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002063 } else {
2064 __parent = static_cast<__parent_pointer>(__nd);
2065 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002066 }
2067 }
2068 else
2069 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002070 __parent = static_cast<__parent_pointer>(__nd);
2071 return *__nd_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002072 }
2073 }
2074 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00002075 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00002076 return __parent->__left_;
2077}
2078
2079// Find place to insert if __v doesn't exist
2080// First check prior to __hint.
2081// Next check after __hint.
2082// Next do O(log N) search.
2083// Set __parent to parent of null leaf
2084// Return reference to null leaf
2085// If __v exists, set parent to node of __v and return reference to node of __v
2086template <class _Tp, class _Compare, class _Allocator>
2087template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00002088typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002089__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002090 __parent_pointer& __parent,
2091 __node_base_pointer& __dummy,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002092 const _Key& __v)
2093{
2094 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2095 {
2096 // __v < *__hint
2097 const_iterator __prior = __hint;
2098 if (__prior == begin() || value_comp()(*--__prior, __v))
2099 {
2100 // *prev(__hint) < __v < *__hint
2101 if (__hint.__ptr_->__left_ == nullptr)
2102 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002103 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002104 return __parent->__left_;
2105 }
2106 else
2107 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002108 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2109 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002110 }
2111 }
2112 // __v <= *prev(__hint)
2113 return __find_equal(__parent, __v);
2114 }
2115 else if (value_comp()(*__hint, __v)) // check after
2116 {
2117 // *__hint < __v
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002118 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002119 if (__next == end() || value_comp()(__v, *__next))
2120 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002121 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002122 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002123 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002124 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2125 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002126 }
2127 else
2128 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002129 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002130 return __parent->__left_;
2131 }
2132 }
2133 // *next(__hint) <= __v
2134 return __find_equal(__parent, __v);
2135 }
2136 // else __v == *__hint
Eric Fiselier70f5f872016-07-19 17:56:20 +00002137 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2138 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2139 return __dummy;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002140}
2141
2142template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002143void __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
2144 __parent_pointer __parent, __node_base_pointer& __child,
2145 __node_base_pointer __new_node) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146{
2147 __new_node->__left_ = nullptr;
2148 __new_node->__right_ = nullptr;
2149 __new_node->__parent_ = __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002150 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151 __child = __new_node;
2152 if (__begin_node()->__left_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002153 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002154 __tree_balance_after_insert(__end_node()->__left_, __child);
2155 ++size();
2156}
2157
Eric Fiselierd06276b2016-03-31 02:15:15 +00002158#ifndef _LIBCPP_CXX03_LANG
2159template <class _Tp, class _Compare, class _Allocator>
2160template <class _Key, class... _Args>
2161pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2162__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2163#else
2164template <class _Tp, class _Compare, class _Allocator>
2165template <class _Key, class _Args>
2166pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2167__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2168#endif
2169{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002170 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002171 __node_base_pointer& __child = __find_equal(__parent, __k);
2172 __node_pointer __r = static_cast<__node_pointer>(__child);
2173 bool __inserted = false;
2174 if (__child == nullptr)
2175 {
2176#ifndef _LIBCPP_CXX03_LANG
2177 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2178#else
2179 __node_holder __h = __construct_node(__args);
2180#endif
2181 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2182 __r = __h.release();
2183 __inserted = true;
2184 }
2185 return pair<iterator, bool>(iterator(__r), __inserted);
2186}
2187
2188
2189#ifndef _LIBCPP_CXX03_LANG
2190template <class _Tp, class _Compare, class _Allocator>
2191template <class _Key, class... _Args>
2192typename __tree<_Tp, _Compare, _Allocator>::iterator
2193__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2194 const_iterator __p, _Key const& __k, _Args&&... __args)
2195#else
2196template <class _Tp, class _Compare, class _Allocator>
2197template <class _Key, class _Args>
2198typename __tree<_Tp, _Compare, _Allocator>::iterator
2199__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2200 const_iterator __p, _Key const& __k, _Args& __args)
2201#endif
2202{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002203 __parent_pointer __parent;
2204 __node_base_pointer __dummy;
2205 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierd06276b2016-03-31 02:15:15 +00002206 __node_pointer __r = static_cast<__node_pointer>(__child);
2207 if (__child == nullptr)
2208 {
2209#ifndef _LIBCPP_CXX03_LANG
2210 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2211#else
2212 __node_holder __h = __construct_node(__args);
2213#endif
2214 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2215 __r = __h.release();
2216 }
2217 return iterator(__r);
2218}
2219
2220
2221#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002222
2223template <class _Tp, class _Compare, class _Allocator>
2224template <class ..._Args>
2225typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2226__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2227{
Eric Fiselierd06276b2016-03-31 02:15:15 +00002228 static_assert(!__is_tree_value_type<_Args...>::value,
2229 "Cannot construct from __value_type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230 __node_allocator& __na = __node_alloc();
Howard Hinnantc834c512011-11-29 18:15:50 +00002231 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierd06276b2016-03-31 02:15:15 +00002232 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233 __h.get_deleter().__value_constructed = true;
2234 return __h;
2235}
2236
Eric Fiselierd06276b2016-03-31 02:15:15 +00002237
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238template <class _Tp, class _Compare, class _Allocator>
2239template <class... _Args>
2240pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00002241__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002242{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002243 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002244 __parent_pointer __parent;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002245 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2246 __node_pointer __r = static_cast<__node_pointer>(__child);
2247 bool __inserted = false;
2248 if (__child == nullptr)
2249 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002250 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002251 __r = __h.release();
2252 __inserted = true;
2253 }
2254 return pair<iterator, bool>(iterator(__r), __inserted);
2255}
2256
2257template <class _Tp, class _Compare, class _Allocator>
2258template <class... _Args>
2259typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier6cce51e2016-04-15 23:27:27 +00002260__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002261{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002262 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002263 __parent_pointer __parent;
2264 __node_base_pointer __dummy;
2265 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002266 __node_pointer __r = static_cast<__node_pointer>(__child);
2267 if (__child == nullptr)
2268 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002269 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002270 __r = __h.release();
2271 }
2272 return iterator(__r);
2273}
2274
2275template <class _Tp, class _Compare, class _Allocator>
2276template <class... _Args>
2277typename __tree<_Tp, _Compare, _Allocator>::iterator
2278__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2279{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002280 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002281 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002282 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002283 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002284 return iterator(static_cast<__node_pointer>(__h.release()));
2285}
2286
2287template <class _Tp, class _Compare, class _Allocator>
2288template <class... _Args>
2289typename __tree<_Tp, _Compare, _Allocator>::iterator
2290__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2291 _Args&&... __args)
2292{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002293 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002294 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002295 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002296 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002297 return iterator(static_cast<__node_pointer>(__h.release()));
2298}
2299
Howard Hinnant74279a52010-09-04 23:28:19 +00002300
Eric Fiselierd06276b2016-03-31 02:15:15 +00002301#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002302
2303template <class _Tp, class _Compare, class _Allocator>
2304typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierd06276b2016-03-31 02:15:15 +00002305__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002306{
2307 __node_allocator& __na = __node_alloc();
Howard Hinnantc834c512011-11-29 18:15:50 +00002308 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierd06276b2016-03-31 02:15:15 +00002309 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002310 __h.get_deleter().__value_constructed = true;
Dimitry Andric830fb602015-08-19 06:43:33 +00002311 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantc51e1022010-05-11 19:42:16 +00002312}
2313
Eric Fiselierd06276b2016-03-31 02:15:15 +00002314#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc5bc8672011-03-10 17:27:57 +00002315
Eric Fiselierd06276b2016-03-31 02:15:15 +00002316#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002317template <class _Tp, class _Compare, class _Allocator>
2318typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierd06276b2016-03-31 02:15:15 +00002319__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002320{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002321 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002322 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323 __node_holder __h = __construct_node(__v);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002324 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002325 return iterator(__h.release());
2326}
2327
2328template <class _Tp, class _Compare, class _Allocator>
2329typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierd06276b2016-03-31 02:15:15 +00002330__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002331{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002332 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002333 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334 __node_holder __h = __construct_node(__v);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002335 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002336 return iterator(__h.release());
2337}
Eric Fiselierd06276b2016-03-31 02:15:15 +00002338#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002339
Howard Hinnantc51e1022010-05-11 19:42:16 +00002340template <class _Tp, class _Compare, class _Allocator>
2341pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2342__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2343{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002344 __parent_pointer __parent;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002345 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2346 __node_pointer __r = static_cast<__node_pointer>(__child);
2347 bool __inserted = false;
2348 if (__child == nullptr)
2349 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002350 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002351 __r = __nd;
2352 __inserted = true;
2353 }
2354 return pair<iterator, bool>(iterator(__r), __inserted);
2355}
2356
2357template <class _Tp, class _Compare, class _Allocator>
2358typename __tree<_Tp, _Compare, _Allocator>::iterator
2359__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2360 __node_pointer __nd)
2361{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002362 __parent_pointer __parent;
2363 __node_base_pointer __dummy;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002364 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2365 __node_pointer __r = static_cast<__node_pointer>(__child);
2366 if (__child == nullptr)
2367 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002368 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002369 __r = __nd;
2370 }
2371 return iterator(__r);
2372}
2373
2374template <class _Tp, class _Compare, class _Allocator>
2375typename __tree<_Tp, _Compare, _Allocator>::iterator
2376__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2377{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002378 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002379 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002380 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002381 return iterator(__nd);
2382}
2383
2384template <class _Tp, class _Compare, class _Allocator>
2385typename __tree<_Tp, _Compare, _Allocator>::iterator
2386__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2387 __node_pointer __nd)
2388{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002389 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002390 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002391 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002392 return iterator(__nd);
2393}
2394
2395template <class _Tp, class _Compare, class _Allocator>
2396typename __tree<_Tp, _Compare, _Allocator>::iterator
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002397__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002398{
2399 iterator __r(__ptr);
2400 ++__r;
2401 if (__begin_node() == __ptr)
2402 __begin_node() = __r.__ptr_;
2403 --size();
2404 __tree_remove(__end_node()->__left_,
2405 static_cast<__node_base_pointer>(__ptr));
2406 return __r;
2407}
2408
2409#if _LIBCPP_STD_VER > 14
2410template <class _Tp, class _Compare, class _Allocator>
2411template <class _NodeHandle, class _InsertReturnType>
2412_LIBCPP_INLINE_VISIBILITY
2413_InsertReturnType
2414__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2415 _NodeHandle&& __nh)
2416{
2417 if (__nh.empty())
2418 return _InsertReturnType{end(), false, _NodeHandle()};
2419
2420 __node_pointer __ptr = __nh.__ptr_;
2421 __parent_pointer __parent;
2422 __node_base_pointer& __child = __find_equal(__parent,
2423 __ptr->__value_);
2424 if (__child != nullptr)
2425 return _InsertReturnType{
2426 iterator(static_cast<__node_pointer>(__child)),
2427 false, _VSTD::move(__nh)};
2428
2429 __insert_node_at(__parent, __child,
2430 static_cast<__node_base_pointer>(__ptr));
2431 __nh.__release();
2432 return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
2433}
2434
2435template <class _Tp, class _Compare, class _Allocator>
2436template <class _NodeHandle>
2437_LIBCPP_INLINE_VISIBILITY
2438typename __tree<_Tp, _Compare, _Allocator>::iterator
2439__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2440 const_iterator __hint, _NodeHandle&& __nh)
2441{
2442 if (__nh.empty())
2443 return end();
2444
2445 __node_pointer __ptr = __nh.__ptr_;
2446 __parent_pointer __parent;
2447 __node_base_pointer __dummy;
2448 __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy,
2449 __ptr->__value_);
2450 __node_pointer __r = static_cast<__node_pointer>(__child);
2451 if (__child == nullptr)
2452 {
2453 __insert_node_at(__parent, __child,
2454 static_cast<__node_base_pointer>(__ptr));
2455 __r = __ptr;
2456 __nh.__release();
2457 }
2458 return iterator(__r);
2459}
2460
2461template <class _Tp, class _Compare, class _Allocator>
2462template <class _NodeHandle>
2463_LIBCPP_INLINE_VISIBILITY
2464_NodeHandle
2465__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key)
2466{
2467 iterator __it = find(__key);
2468 if (__it == end())
2469 return _NodeHandle();
2470 return __node_handle_extract<_NodeHandle>(__it);
2471}
2472
2473template <class _Tp, class _Compare, class _Allocator>
2474template <class _NodeHandle>
2475_LIBCPP_INLINE_VISIBILITY
2476_NodeHandle
2477__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p)
2478{
2479 __node_pointer __np = __p.__get_np();
2480 __remove_node_pointer(__np);
2481 return _NodeHandle(__np, __alloc());
2482}
2483
2484template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002485template <class _Tree>
2486_LIBCPP_INLINE_VISIBILITY
2487void
2488__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(_Tree& __source)
2489{
2490 static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2491
2492 for (typename _Tree::iterator __i = __source.begin();
2493 __i != __source.end();)
2494 {
2495 __node_pointer __src_ptr = __i.__get_np();
2496 __parent_pointer __parent;
2497 __node_base_pointer& __child =
2498 __find_equal(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
2499 ++__i;
2500 if (__child != nullptr)
2501 continue;
2502 __source.__remove_node_pointer(__src_ptr);
2503 __insert_node_at(__parent, __child,
2504 static_cast<__node_base_pointer>(__src_ptr));
2505 }
2506}
2507
2508template <class _Tp, class _Compare, class _Allocator>
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002509template <class _NodeHandle>
2510_LIBCPP_INLINE_VISIBILITY
2511typename __tree<_Tp, _Compare, _Allocator>::iterator
2512__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh)
2513{
2514 if (__nh.empty())
2515 return end();
2516 __node_pointer __ptr = __nh.__ptr_;
2517 __parent_pointer __parent;
2518 __node_base_pointer& __child = __find_leaf_high(
2519 __parent, _NodeTypes::__get_key(__ptr->__value_));
2520 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2521 __nh.__release();
2522 return iterator(__ptr);
2523}
2524
2525template <class _Tp, class _Compare, class _Allocator>
2526template <class _NodeHandle>
2527_LIBCPP_INLINE_VISIBILITY
2528typename __tree<_Tp, _Compare, _Allocator>::iterator
2529__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(
2530 const_iterator __hint, _NodeHandle&& __nh)
2531{
2532 if (__nh.empty())
2533 return end();
2534
2535 __node_pointer __ptr = __nh.__ptr_;
2536 __parent_pointer __parent;
2537 __node_base_pointer& __child = __find_leaf(__hint, __parent,
2538 _NodeTypes::__get_key(__ptr->__value_));
2539 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2540 __nh.__release();
2541 return iterator(__ptr);
2542}
2543
Erik Pilkington82a65ad2018-10-31 17:31:35 +00002544template <class _Tp, class _Compare, class _Allocator>
2545template <class _Tree>
2546_LIBCPP_INLINE_VISIBILITY
2547void
2548__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(_Tree& __source)
2549{
2550 static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2551
2552 for (typename _Tree::iterator __i = __source.begin();
2553 __i != __source.end();)
2554 {
2555 __node_pointer __src_ptr = __i.__get_np();
2556 __parent_pointer __parent;
2557 __node_base_pointer& __child = __find_leaf_high(
2558 __parent, _NodeTypes::__get_key(__src_ptr->__value_));
2559 ++__i;
2560 __source.__remove_node_pointer(__src_ptr);
2561 __insert_node_at(__parent, __child,
2562 static_cast<__node_base_pointer>(__src_ptr));
2563 }
2564}
2565
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002566#endif // _LIBCPP_STD_VER > 14
2567
2568template <class _Tp, class _Compare, class _Allocator>
2569typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +00002570__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2571{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002572 __node_pointer __np = __p.__get_np();
Erik Pilkingtonc37a3d82018-08-01 01:33:38 +00002573 iterator __r = __remove_node_pointer(__np);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002574 __node_allocator& __na = __node_alloc();
Eric Fiselierd06276b2016-03-31 02:15:15 +00002575 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2576 const_cast<__node_value_type&>(*__p)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 __node_traits::deallocate(__na, __np, 1);
2578 return __r;
2579}
2580
2581template <class _Tp, class _Compare, class _Allocator>
2582typename __tree<_Tp, _Compare, _Allocator>::iterator
2583__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2584{
2585 while (__f != __l)
2586 __f = erase(__f);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002587 return iterator(__l.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002588}
2589
2590template <class _Tp, class _Compare, class _Allocator>
2591template <class _Key>
2592typename __tree<_Tp, _Compare, _Allocator>::size_type
2593__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2594{
2595 iterator __i = find(__k);
2596 if (__i == end())
2597 return 0;
2598 erase(__i);
2599 return 1;
2600}
2601
2602template <class _Tp, class _Compare, class _Allocator>
2603template <class _Key>
2604typename __tree<_Tp, _Compare, _Allocator>::size_type
2605__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2606{
2607 pair<iterator, iterator> __p = __equal_range_multi(__k);
2608 size_type __r = 0;
2609 for (; __p.first != __p.second; ++__r)
2610 __p.first = erase(__p.first);
2611 return __r;
2612}
2613
2614template <class _Tp, class _Compare, class _Allocator>
2615template <class _Key>
2616typename __tree<_Tp, _Compare, _Allocator>::iterator
2617__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2618{
2619 iterator __p = __lower_bound(__v, __root(), __end_node());
2620 if (__p != end() && !value_comp()(__v, *__p))
2621 return __p;
2622 return end();
2623}
2624
2625template <class _Tp, class _Compare, class _Allocator>
2626template <class _Key>
2627typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2628__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2629{
2630 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2631 if (__p != end() && !value_comp()(__v, *__p))
2632 return __p;
2633 return end();
2634}
2635
2636template <class _Tp, class _Compare, class _Allocator>
2637template <class _Key>
2638typename __tree<_Tp, _Compare, _Allocator>::size_type
2639__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2640{
Eric Fiseliera92b0732016-02-20 07:12:17 +00002641 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002642 while (__rt != nullptr)
2643 {
2644 if (value_comp()(__k, __rt->__value_))
2645 {
Eric Fiseliera92b0732016-02-20 07:12:17 +00002646 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002647 }
2648 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002649 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002650 else
2651 return 1;
2652 }
2653 return 0;
2654}
2655
2656template <class _Tp, class _Compare, class _Allocator>
2657template <class _Key>
2658typename __tree<_Tp, _Compare, _Allocator>::size_type
2659__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2660{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002661 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002662 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002663 while (__rt != nullptr)
2664 {
2665 if (value_comp()(__k, __rt->__value_))
2666 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002667 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002668 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002669 }
2670 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002671 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002672 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002673 return _VSTD::distance(
Eric Fiselier70f5f872016-07-19 17:56:20 +00002674 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiseliera92b0732016-02-20 07:12:17 +00002675 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002676 );
2677 }
2678 return 0;
2679}
2680
2681template <class _Tp, class _Compare, class _Allocator>
2682template <class _Key>
2683typename __tree<_Tp, _Compare, _Allocator>::iterator
2684__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2685 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002686 __iter_pointer __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002687{
2688 while (__root != nullptr)
2689 {
2690 if (!value_comp()(__root->__value_, __v))
2691 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002692 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002693 __root = static_cast<__node_pointer>(__root->__left_);
2694 }
2695 else
2696 __root = static_cast<__node_pointer>(__root->__right_);
2697 }
2698 return iterator(__result);
2699}
2700
2701template <class _Tp, class _Compare, class _Allocator>
2702template <class _Key>
2703typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2704__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00002705 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002706 __iter_pointer __result) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002707{
2708 while (__root != nullptr)
2709 {
2710 if (!value_comp()(__root->__value_, __v))
2711 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002712 __result = static_cast<__iter_pointer>(__root);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002713 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002714 }
2715 else
Eric Fiseliera92b0732016-02-20 07:12:17 +00002716 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002717 }
2718 return const_iterator(__result);
2719}
2720
2721template <class _Tp, class _Compare, class _Allocator>
2722template <class _Key>
2723typename __tree<_Tp, _Compare, _Allocator>::iterator
2724__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2725 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002726 __iter_pointer __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002727{
2728 while (__root != nullptr)
2729 {
2730 if (value_comp()(__v, __root->__value_))
2731 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002732 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002733 __root = static_cast<__node_pointer>(__root->__left_);
2734 }
2735 else
2736 __root = static_cast<__node_pointer>(__root->__right_);
2737 }
2738 return iterator(__result);
2739}
2740
2741template <class _Tp, class _Compare, class _Allocator>
2742template <class _Key>
2743typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2744__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00002745 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002746 __iter_pointer __result) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002747{
2748 while (__root != nullptr)
2749 {
2750 if (value_comp()(__v, __root->__value_))
2751 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002752 __result = static_cast<__iter_pointer>(__root);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002753 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002754 }
2755 else
Eric Fiseliera92b0732016-02-20 07:12:17 +00002756 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002757 }
2758 return const_iterator(__result);
2759}
2760
2761template <class _Tp, class _Compare, class _Allocator>
2762template <class _Key>
2763pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2764 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2765__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2766{
Howard Hinnantc834c512011-11-29 18:15:50 +00002767 typedef pair<iterator, iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002768 __iter_pointer __result = __end_node();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002769 __node_pointer __rt = __root();
2770 while (__rt != nullptr)
2771 {
2772 if (value_comp()(__k, __rt->__value_))
2773 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002774 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002775 __rt = static_cast<__node_pointer>(__rt->__left_);
2776 }
2777 else if (value_comp()(__rt->__value_, __k))
2778 __rt = static_cast<__node_pointer>(__rt->__right_);
2779 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002780 return _Pp(iterator(__rt),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002781 iterator(
2782 __rt->__right_ != nullptr ?
Eric Fiselier70f5f872016-07-19 17:56:20 +00002783 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002784 : __result));
2785 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002786 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002787}
2788
2789template <class _Tp, class _Compare, class _Allocator>
2790template <class _Key>
2791pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2792 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2793__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2794{
Howard Hinnantc834c512011-11-29 18:15:50 +00002795 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002796 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002797 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002798 while (__rt != nullptr)
2799 {
2800 if (value_comp()(__k, __rt->__value_))
2801 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002802 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002803 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002804 }
2805 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002806 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002807 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002808 return _Pp(const_iterator(__rt),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002809 const_iterator(
2810 __rt->__right_ != nullptr ?
Eric Fiselier70f5f872016-07-19 17:56:20 +00002811 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002812 : __result));
2813 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002814 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002815}
2816
2817template <class _Tp, class _Compare, class _Allocator>
2818template <class _Key>
2819pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2820 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2821__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2822{
Howard Hinnantc834c512011-11-29 18:15:50 +00002823 typedef pair<iterator, iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002824 __iter_pointer __result = __end_node();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002825 __node_pointer __rt = __root();
2826 while (__rt != nullptr)
2827 {
2828 if (value_comp()(__k, __rt->__value_))
2829 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002830 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002831 __rt = static_cast<__node_pointer>(__rt->__left_);
2832 }
2833 else if (value_comp()(__rt->__value_, __k))
2834 __rt = static_cast<__node_pointer>(__rt->__right_);
2835 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002836 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002837 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2838 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002839 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002840}
2841
2842template <class _Tp, class _Compare, class _Allocator>
2843template <class _Key>
2844pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2845 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2846__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2847{
Howard Hinnantc834c512011-11-29 18:15:50 +00002848 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002849 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002850 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002851 while (__rt != nullptr)
2852 {
2853 if (value_comp()(__k, __rt->__value_))
2854 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002855 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002856 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002857 }
2858 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002859 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002860 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002861 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiseliera92b0732016-02-20 07:12:17 +00002862 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002863 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002864 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002865}
2866
2867template <class _Tp, class _Compare, class _Allocator>
2868typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant1113b5e2011-06-04 17:10:24 +00002869__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002870{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002871 __node_pointer __np = __p.__get_np();
2872 if (__begin_node() == __p.__ptr_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002873 {
2874 if (__np->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002875 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002876 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002877 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002878 }
2879 --size();
2880 __tree_remove(__end_node()->__left_,
2881 static_cast<__node_base_pointer>(__np));
Marshall Clow95af65e2015-01-28 19:54:25 +00002882 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002883}
2884
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00002885template <class _Tp, class _Compare, class _Allocator>
2886inline _LIBCPP_INLINE_VISIBILITY
2887void
2888swap(__tree<_Tp, _Compare, _Allocator>& __x,
2889 __tree<_Tp, _Compare, _Allocator>& __y)
2890 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2891{
2892 __x.swap(__y);
2893}
2894
Howard Hinnantc51e1022010-05-11 19:42:16 +00002895_LIBCPP_END_NAMESPACE_STD
2896
Eric Fiselierf4433a32017-05-31 22:07:49 +00002897_LIBCPP_POP_MACROS
2898
Howard Hinnantc51e1022010-05-11 19:42:16 +00002899#endif // _LIBCPP___TREE