blob: 814851085b9834435395a0e592231ef1d987c979 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-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 Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
Eric Fiselier018a3d52017-05-31 22:07:49 +000024_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000030template <class _Tp, class _Compare, class _Allocator> class __tree;
31template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000032 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000033template <class _Tp, class _ConstNodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000034 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035
Eric Fiselier55263482016-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 Fiselier55263482016-02-20 05:28:30 +000040template <class _Key, class _Value>
41struct __value_type;
Eric Fiselier55263482016-02-20 05:28:30 +000042
43template <class _Allocator> class __map_node_destructor;
Eric Fiselierc3589a82017-01-04 23:56:00 +000044template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
45template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier55263482016-02-20 05:28:30 +000046
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000047/*
48
49_NodePtr algorithms
50
51The algorithms taking _NodePtr are red black tree algorithms. Those
52algorithms taking a parameter named __root should assume that __root
53points to a proper red black tree (unless otherwise specified).
54
55Each algorithm herein assumes that __root->__parent_ points to a non-null
56structure which has a member __left_ which points back to __root. No other
57member is read or written to at __root->__parent_.
58
59__root->__parent_ will be referred to below (in comments only) as end_node.
60end_node->__left_ is an externably accessible lvalue for __root, and can be
61changed by node insertion and removal (without explicit reference to end_node).
62
63All nodes (with the exception of end_node), even the node referred to as
64__root, have a non-null __parent_ field.
65
66*/
67
68// Returns: true if __x is a left child of its parent, else false
69// Precondition: __x != nullptr.
70template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +000071inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072bool
Howard Hinnant8b537682011-06-04 17:10:24 +000073__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074{
75 return __x == __x->__parent_->__left_;
76}
77
Joerg Sonnenberger9b69be42017-08-18 12:57:36 +000078// Determines if the subtree rooted at __x is a proper red black subtree. If
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000079// __x is a proper subtree, returns the black height (null counts as 1). If
80// __x is an improper subtree, returns 0.
81template <class _NodePtr>
82unsigned
83__tree_sub_invariant(_NodePtr __x)
84{
85 if (__x == nullptr)
86 return 1;
87 // parent consistency checked by caller
88 // check __x->__left_ consistency
89 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
90 return 0;
91 // check __x->__right_ consistency
92 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
93 return 0;
94 // check __x->__left_ != __x->__right_ unless both are nullptr
95 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
96 return 0;
97 // If this is red, neither child can be red
98 if (!__x->__is_black_)
99 {
100 if (__x->__left_ && !__x->__left_->__is_black_)
101 return 0;
102 if (__x->__right_ && !__x->__right_->__is_black_)
103 return 0;
104 }
105 unsigned __h = __tree_sub_invariant(__x->__left_);
106 if (__h == 0)
107 return 0; // invalid left subtree
108 if (__h != __tree_sub_invariant(__x->__right_))
109 return 0; // invalid or different height right subtree
110 return __h + __x->__is_black_; // return black height of this node
111}
112
Joerg Sonnenberger9b69be42017-08-18 12:57:36 +0000113// Determines if the red black tree rooted at __root is a proper red black tree.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114// __root == nullptr is a proper tree. Returns true is __root is a proper
115// red black tree, else returns false.
116template <class _NodePtr>
117bool
118__tree_invariant(_NodePtr __root)
119{
120 if (__root == nullptr)
121 return true;
122 // check __x->__parent_ consistency
123 if (__root->__parent_ == nullptr)
124 return false;
125 if (!__tree_is_left_child(__root))
126 return false;
127 // root must be black
128 if (!__root->__is_black_)
129 return false;
130 // do normal node checks
131 return __tree_sub_invariant(__root) != 0;
132}
133
134// Returns: pointer to the left-most node under __x.
135// Precondition: __x != nullptr.
136template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000137inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000138_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000139__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000140{
141 while (__x->__left_ != nullptr)
142 __x = __x->__left_;
143 return __x;
144}
145
146// Returns: pointer to the right-most node under __x.
147// Precondition: __x != nullptr.
148template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000149inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000151__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152{
153 while (__x->__right_ != nullptr)
154 __x = __x->__right_;
155 return __x;
156}
157
158// Returns: pointer to the next in-order node after __x.
159// Precondition: __x != nullptr.
160template <class _NodePtr>
161_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000162__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163{
164 if (__x->__right_ != nullptr)
165 return __tree_min(__x->__right_);
166 while (!__tree_is_left_child(__x))
Eric Fiselier7310ec82016-07-19 17:56:20 +0000167 __x = __x->__parent_unsafe();
168 return __x->__parent_unsafe();
169}
170
171template <class _EndNodePtr, class _NodePtr>
172inline _LIBCPP_INLINE_VISIBILITY
173_EndNodePtr
174__tree_next_iter(_NodePtr __x) _NOEXCEPT
175{
176 if (__x->__right_ != nullptr)
177 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
178 while (!__tree_is_left_child(__x))
179 __x = __x->__parent_unsafe();
180 return static_cast<_EndNodePtr>(__x->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000181}
182
183// Returns: pointer to the previous in-order node before __x.
184// Precondition: __x != nullptr.
Eric Fiselier7310ec82016-07-19 17:56:20 +0000185// Note: __x may be the end node.
186template <class _NodePtr, class _EndNodePtr>
187inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188_NodePtr
Eric Fiselier7310ec82016-07-19 17:56:20 +0000189__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190{
191 if (__x->__left_ != nullptr)
192 return __tree_max(__x->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +0000193 _NodePtr __xx = static_cast<_NodePtr>(__x);
194 while (__tree_is_left_child(__xx))
195 __xx = __xx->__parent_unsafe();
196 return __xx->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197}
198
199// Returns: pointer to a node which has no children
200// Precondition: __x != nullptr.
201template <class _NodePtr>
202_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000203__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204{
205 while (true)
206 {
207 if (__x->__left_ != nullptr)
208 {
209 __x = __x->__left_;
210 continue;
211 }
212 if (__x->__right_ != nullptr)
213 {
214 __x = __x->__right_;
215 continue;
216 }
217 break;
218 }
219 return __x;
220}
221
222// Effects: Makes __x->__right_ the subtree root with __x as its left child
223// while preserving in-order order.
224// Precondition: __x->__right_ != nullptr
225template <class _NodePtr>
226void
Howard Hinnant8b537682011-06-04 17:10:24 +0000227__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228{
229 _NodePtr __y = __x->__right_;
230 __x->__right_ = __y->__left_;
231 if (__x->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000232 __x->__right_->__set_parent(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233 __y->__parent_ = __x->__parent_;
234 if (__tree_is_left_child(__x))
235 __x->__parent_->__left_ = __y;
236 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000237 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000238 __y->__left_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000239 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000240}
241
242// Effects: Makes __x->__left_ the subtree root with __x as its right child
243// while preserving in-order order.
244// Precondition: __x->__left_ != nullptr
245template <class _NodePtr>
246void
Howard Hinnant8b537682011-06-04 17:10:24 +0000247__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248{
249 _NodePtr __y = __x->__left_;
250 __x->__left_ = __y->__right_;
251 if (__x->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000252 __x->__left_->__set_parent(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253 __y->__parent_ = __x->__parent_;
254 if (__tree_is_left_child(__x))
255 __x->__parent_->__left_ = __y;
256 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000257 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000258 __y->__right_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000259 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000260}
261
262// Effects: Rebalances __root after attaching __x to a leaf.
263// Precondition: __root != nulptr && __x != nullptr.
264// __x has no children.
265// __x == __root or == a direct or indirect child of __root.
266// If __x were to be unlinked from __root (setting __root to
267// nullptr if __root == __x), __tree_invariant(__root) == true.
268// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
269// may be different than the value passed in as __root.
270template <class _NodePtr>
271void
Howard Hinnant8b537682011-06-04 17:10:24 +0000272__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273{
274 __x->__is_black_ = __x == __root;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000275 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276 {
277 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier7310ec82016-07-19 17:56:20 +0000278 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000280 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000281 if (__y != nullptr && !__y->__is_black_)
282 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000283 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000285 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000286 __x->__is_black_ = __x == __root;
287 __y->__is_black_ = true;
288 }
289 else
290 {
291 if (!__tree_is_left_child(__x))
292 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000293 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 __tree_left_rotate(__x);
295 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000296 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000298 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299 __x->__is_black_ = false;
300 __tree_right_rotate(__x);
301 break;
302 }
303 }
304 else
305 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000306 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000307 if (__y != nullptr && !__y->__is_black_)
308 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000309 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000311 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000312 __x->__is_black_ = __x == __root;
313 __y->__is_black_ = true;
314 }
315 else
316 {
317 if (__tree_is_left_child(__x))
318 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000319 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000320 __tree_right_rotate(__x);
321 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000322 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000323 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000324 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000325 __x->__is_black_ = false;
326 __tree_left_rotate(__x);
327 break;
328 }
329 }
330 }
331}
332
333// Precondition: __root != nullptr && __z != nullptr.
334// __tree_invariant(__root) == true.
335// __z == __root or == a direct or indirect child of __root.
336// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
337// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
338// nor any of its children refer to __z. end_node->__left_
339// may be different than the value passed in as __root.
340template <class _NodePtr>
341void
Howard Hinnant8b537682011-06-04 17:10:24 +0000342__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343{
344 // __z will be removed from the tree. Client still needs to destruct/deallocate it
345 // __y is either __z, or if __z has two children, __tree_next(__z).
346 // __y will have at most one child.
347 // __y will be the initial hole in the tree (make the hole at a leaf)
348 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
349 __z : __tree_next(__z);
350 // __x is __y's possibly null single child
351 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
352 // __w is __x's possibly null uncle (will become __x's sibling)
353 _NodePtr __w = nullptr;
354 // link __x to __y's parent, and find __w
355 if (__x != nullptr)
356 __x->__parent_ = __y->__parent_;
357 if (__tree_is_left_child(__y))
358 {
359 __y->__parent_->__left_ = __x;
360 if (__y != __root)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000361 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362 else
363 __root = __x; // __w == nullptr
364 }
365 else
366 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000367 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000368 // __y can't be root if it is a right child
369 __w = __y->__parent_->__left_;
370 }
371 bool __removed_black = __y->__is_black_;
372 // If we didn't remove __z, do so now by splicing in __y for __z,
373 // but copy __z's color. This does not impact __x or __w.
374 if (__y != __z)
375 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000376 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 __y->__parent_ = __z->__parent_;
378 if (__tree_is_left_child(__z))
379 __y->__parent_->__left_ = __y;
380 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000381 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382 __y->__left_ = __z->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000383 __y->__left_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 __y->__right_ = __z->__right_;
385 if (__y->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000386 __y->__right_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 __y->__is_black_ = __z->__is_black_;
388 if (__root == __z)
389 __root = __y;
390 }
391 // There is no need to rebalance if we removed a red, or if we removed
392 // the last node.
393 if (__removed_black && __root != nullptr)
394 {
395 // Rebalance:
396 // __x has an implicit black color (transferred from the removed __y)
397 // associated with it, no matter what its color is.
398 // If __x is __root (in which case it can't be null), it is supposed
399 // to be black anyway, and if it is doubly black, then the double
400 // can just be ignored.
401 // If __x is red (in which case it can't be null), then it can absorb
402 // the implicit black just by setting its color to black.
403 // Since __y was black and only had one child (which __x points to), __x
404 // is either red with no children, else null, otherwise __y would have
405 // different black heights under left and right pointers.
406 // if (__x == __root || __x != nullptr && !__x->__is_black_)
407 if (__x != nullptr)
408 __x->__is_black_ = true;
409 else
410 {
411 // Else __x isn't root, and is "doubly black", even though it may
412 // be null. __w can not be null here, else the parent would
413 // see a black height >= 2 on the __x side and a black height
414 // of 1 on the __w side (__w must be a non-null black or a red
415 // with a non-null black child).
416 while (true)
417 {
418 if (!__tree_is_left_child(__w)) // if x is left child
419 {
420 if (!__w->__is_black_)
421 {
422 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000423 __w->__parent_unsafe()->__is_black_ = false;
424 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000425 // __x is still valid
426 // reset __root only if necessary
427 if (__root == __w->__left_)
428 __root = __w;
429 // reset sibling, and it still can't be null
430 __w = __w->__left_->__right_;
431 }
432 // __w->__is_black_ is now true, __w may have null children
433 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
434 (__w->__right_ == nullptr || __w->__right_->__is_black_))
435 {
436 __w->__is_black_ = false;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000437 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438 // __x can no longer be null
439 if (__x == __root || !__x->__is_black_)
440 {
441 __x->__is_black_ = true;
442 break;
443 }
444 // reset sibling, and it still can't be null
445 __w = __tree_is_left_child(__x) ?
Eric Fiselier7310ec82016-07-19 17:56:20 +0000446 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000447 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000448 // continue;
449 }
450 else // __w has a red child
451 {
452 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
453 {
454 // __w left child is non-null and red
455 __w->__left_->__is_black_ = true;
456 __w->__is_black_ = false;
457 __tree_right_rotate(__w);
458 // __w is known not to be root, so root hasn't changed
459 // reset sibling, and it still can't be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000460 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000461 }
462 // __w has a right red child, left child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000463 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
464 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 __w->__right_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000466 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000467 break;
468 }
469 }
470 else
471 {
472 if (!__w->__is_black_)
473 {
474 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000475 __w->__parent_unsafe()->__is_black_ = false;
476 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000477 // __x is still valid
478 // reset __root only if necessary
479 if (__root == __w->__right_)
480 __root = __w;
481 // reset sibling, and it still can't be null
482 __w = __w->__right_->__left_;
483 }
484 // __w->__is_black_ is now true, __w may have null children
485 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
486 (__w->__right_ == nullptr || __w->__right_->__is_black_))
487 {
488 __w->__is_black_ = false;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000489 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490 // __x can no longer be null
491 if (!__x->__is_black_ || __x == __root)
492 {
493 __x->__is_black_ = true;
494 break;
495 }
496 // reset sibling, and it still can't be null
497 __w = __tree_is_left_child(__x) ?
Eric Fiselier7310ec82016-07-19 17:56:20 +0000498 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000499 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000500 // continue;
501 }
502 else // __w has a red child
503 {
504 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
505 {
506 // __w right child is non-null and red
507 __w->__right_->__is_black_ = true;
508 __w->__is_black_ = false;
509 __tree_left_rotate(__w);
510 // __w is known not to be root, so root hasn't changed
511 // reset sibling, and it still can't be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000512 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000513 }
514 // __w has a left red child, right child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000515 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
516 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 __w->__left_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000518 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000519 break;
520 }
521 }
522 }
523 }
524 }
525}
526
Eric Fiselier55263482016-02-20 05:28:30 +0000527// node traits
528
Eric Fiselierdb215062016-03-31 02:15:15 +0000529
530#ifndef _LIBCPP_CXX03_LANG
531template <class _Tp>
532struct __is_tree_value_type_imp : false_type {};
533
534template <class _Key, class _Value>
535struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
536
537template <class ..._Args>
538struct __is_tree_value_type : false_type {};
539
540template <class _One>
541struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
542#endif
543
Eric Fiselier55263482016-02-20 05:28:30 +0000544template <class _Tp>
545struct __tree_key_value_types {
546 typedef _Tp key_type;
547 typedef _Tp __node_value_type;
548 typedef _Tp __container_value_type;
549 static const bool __is_map = false;
Eric Fiselierdb215062016-03-31 02:15:15 +0000550
551 _LIBCPP_INLINE_VISIBILITY
552 static key_type const& __get_key(_Tp const& __v) {
553 return __v;
554 }
555 _LIBCPP_INLINE_VISIBILITY
556 static __container_value_type const& __get_value(__node_value_type const& __v) {
557 return __v;
558 }
559 _LIBCPP_INLINE_VISIBILITY
560 static __container_value_type* __get_ptr(__node_value_type& __n) {
561 return _VSTD::addressof(__n);
562 }
Eric Fiselierdb215062016-03-31 02:15:15 +0000563#ifndef _LIBCPP_CXX03_LANG
564 _LIBCPP_INLINE_VISIBILITY
Erik Pilkington55513c82018-06-04 20:38:23 +0000565 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierdb215062016-03-31 02:15:15 +0000566 return _VSTD::move(__v);
567 }
568#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000569};
570
571template <class _Key, class _Tp>
572struct __tree_key_value_types<__value_type<_Key, _Tp> > {
573 typedef _Key key_type;
574 typedef _Tp mapped_type;
575 typedef __value_type<_Key, _Tp> __node_value_type;
576 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000577 typedef __container_value_type __map_value_type;
578 static const bool __is_map = true;
Eric Fiselierdb215062016-03-31 02:15:15 +0000579
580 _LIBCPP_INLINE_VISIBILITY
581 static key_type const&
582 __get_key(__node_value_type const& __t) {
Erik Pilkington55513c82018-06-04 20:38:23 +0000583 return __t.__get_value().first;
Eric Fiselierdb215062016-03-31 02:15:15 +0000584 }
585
586 template <class _Up>
587 _LIBCPP_INLINE_VISIBILITY
588 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
589 key_type const&>::type
590 __get_key(_Up& __t) {
591 return __t.first;
592 }
593
594 _LIBCPP_INLINE_VISIBILITY
595 static __container_value_type const&
596 __get_value(__node_value_type const& __t) {
Erik Pilkington55513c82018-06-04 20:38:23 +0000597 return __t.__get_value();
Eric Fiselierdb215062016-03-31 02:15:15 +0000598 }
599
600 template <class _Up>
601 _LIBCPP_INLINE_VISIBILITY
602 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
603 __container_value_type const&>::type
604 __get_value(_Up& __t) {
605 return __t;
606 }
607
608 _LIBCPP_INLINE_VISIBILITY
609 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkington55513c82018-06-04 20:38:23 +0000610 return _VSTD::addressof(__n.__get_value());
Eric Fiselierdb215062016-03-31 02:15:15 +0000611 }
612
613#ifndef _LIBCPP_CXX03_LANG
614 _LIBCPP_INLINE_VISIBILITY
Erik Pilkington55513c82018-06-04 20:38:23 +0000615 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
616 return __v.__move();
Eric Fiselierdb215062016-03-31 02:15:15 +0000617 }
618#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000619};
620
621template <class _VoidPtr>
622struct __tree_node_base_types {
623 typedef _VoidPtr __void_pointer;
624
625 typedef __tree_node_base<__void_pointer> __node_base_type;
626 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
627 __node_base_pointer;
628
629 typedef __tree_end_node<__node_base_pointer> __end_node_type;
630 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
631 __end_node_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000632#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
633 typedef __end_node_pointer __parent_pointer;
634#else
635 typedef typename conditional<
636 is_pointer<__end_node_pointer>::value,
637 __end_node_pointer,
638 __node_base_pointer>::type __parent_pointer;
639#endif
640
Eric Fiselier55263482016-02-20 05:28:30 +0000641private:
642 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
643 "_VoidPtr does not point to unqualified void type");
644};
645
646template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
647 bool = _KVTypes::__is_map>
648struct __tree_map_pointer_types {};
649
650template <class _Tp, class _AllocPtr, class _KVTypes>
651struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
652 typedef typename _KVTypes::__map_value_type _Mv;
653 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
654 __map_value_type_pointer;
655 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
656 __const_map_value_type_pointer;
657};
658
659template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
660struct __tree_node_types;
661
662template <class _NodePtr, class _Tp, class _VoidPtr>
663struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
664 : public __tree_node_base_types<_VoidPtr>,
665 __tree_key_value_types<_Tp>,
666 __tree_map_pointer_types<_Tp, _VoidPtr>
667{
668 typedef __tree_node_base_types<_VoidPtr> __base;
669 typedef __tree_key_value_types<_Tp> __key_base;
670 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
671public:
672
673 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
674 typedef _NodePtr __node_pointer;
675
676 typedef _Tp __node_value_type;
677 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
678 __node_value_type_pointer;
679 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
680 __const_node_value_type_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000681#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
682 typedef typename __base::__end_node_pointer __iter_pointer;
683#else
684 typedef typename conditional<
685 is_pointer<__node_pointer>::value,
686 typename __base::__end_node_pointer,
687 __node_pointer>::type __iter_pointer;
688#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000689private:
690 static_assert(!is_const<__node_type>::value,
691 "_NodePtr should never be a pointer to const");
692 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
693 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
694};
695
696template <class _ValueTp, class _VoidPtr>
697struct __make_tree_node_types {
698 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
699 _NodePtr;
700 typedef __tree_node_types<_NodePtr> type;
701};
702
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703// node
704
705template <class _Pointer>
706class __tree_end_node
707{
708public:
709 typedef _Pointer pointer;
710 pointer __left_;
711
Howard Hinnant333f50d2010-09-21 20:16:37 +0000712 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000713 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714};
715
716template <class _VoidPtr>
717class __tree_node_base
Eric Fiselier55263482016-02-20 05:28:30 +0000718 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719{
Eric Fiselier55263482016-02-20 05:28:30 +0000720 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
721
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722public:
Eric Fiselier55263482016-02-20 05:28:30 +0000723 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000724 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725
Eric Fiselier7310ec82016-07-19 17:56:20 +0000726 pointer __right_;
727 __parent_pointer __parent_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728 bool __is_black_;
729
Eric Fiselier7310ec82016-07-19 17:56:20 +0000730 _LIBCPP_INLINE_VISIBILITY
731 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
732
733 _LIBCPP_INLINE_VISIBILITY
734 void __set_parent(pointer __p) {
735 __parent_ = static_cast<__parent_pointer>(__p);
736 }
737
Eric Fiselierdb215062016-03-31 02:15:15 +0000738private:
739 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
740 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
741 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742};
743
744template <class _Tp, class _VoidPtr>
745class __tree_node
746 : public __tree_node_base<_VoidPtr>
747{
748public:
Eric Fiselier55263482016-02-20 05:28:30 +0000749 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750
Eric Fiselier55263482016-02-20 05:28:30 +0000751 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752
Eric Fiselierdb215062016-03-31 02:15:15 +0000753private:
754 ~__tree_node() _LIBCPP_EQUAL_DELETE;
755 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
756 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000757};
758
Eric Fiselierdb215062016-03-31 02:15:15 +0000759
760template <class _Allocator>
761class __tree_node_destructor
762{
763 typedef _Allocator allocator_type;
764 typedef allocator_traits<allocator_type> __alloc_traits;
765
766public:
767 typedef typename __alloc_traits::pointer pointer;
768private:
769 typedef __tree_node_types<pointer> _NodeTypes;
770 allocator_type& __na_;
771
772 __tree_node_destructor& operator=(const __tree_node_destructor&);
773
774public:
775 bool __value_constructed;
776
777 _LIBCPP_INLINE_VISIBILITY
778 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
779 : __na_(__na),
780 __value_constructed(__val)
781 {}
782
783 _LIBCPP_INLINE_VISIBILITY
784 void operator()(pointer __p) _NOEXCEPT
785 {
786 if (__value_constructed)
787 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
788 if (__p)
789 __alloc_traits::deallocate(__na_, __p, 1);
790 }
791
792 template <class> friend class __map_node_destructor;
793};
794
Erik Pilkington36fc7372018-08-01 01:33:38 +0000795#if _LIBCPP_STD_VER > 14
796template <class _NodeType, class _Alloc>
797struct __generic_container_node_destructor;
798template <class _Tp, class _VoidPtr, class _Alloc>
799struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc>
800 : __tree_node_destructor<_Alloc>
801{
802 using __tree_node_destructor<_Alloc>::__tree_node_destructor;
803};
804#endif
Eric Fiselierdb215062016-03-31 02:15:15 +0000805
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000807class _LIBCPP_TEMPLATE_VIS __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808{
Eric Fiselier55263482016-02-20 05:28:30 +0000809 typedef __tree_node_types<_NodePtr> _NodeTypes;
810 typedef _NodePtr __node_pointer;
811 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000812 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
813 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000814 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815
Eric Fiselier7310ec82016-07-19 17:56:20 +0000816 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000817
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818public:
Eric Fiselier55263482016-02-20 05:28:30 +0000819 typedef bidirectional_iterator_tag iterator_category;
820 typedef _Tp value_type;
821 typedef _DiffType difference_type;
822 typedef value_type& reference;
823 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824
Marshall Clow051c8482013-08-08 21:52:50 +0000825 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
826#if _LIBCPP_STD_VER > 11
827 : __ptr_(nullptr)
828#endif
829 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830
Eric Fiselier7310ec82016-07-19 17:56:20 +0000831 _LIBCPP_INLINE_VISIBILITY reference operator*() const
832 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000833 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000834 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835
Howard Hinnant333f50d2010-09-21 20:16:37 +0000836 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000837 __tree_iterator& operator++() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000838 __ptr_ = static_cast<__iter_pointer>(
839 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000840 return *this;
841 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843 __tree_iterator operator++(int)
844 {__tree_iterator __t(*this); ++(*this); return __t;}
845
Howard Hinnant333f50d2010-09-21 20:16:37 +0000846 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000847 __tree_iterator& operator--() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000848 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
849 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000850 return *this;
851 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 __tree_iterator operator--(int)
854 {__tree_iterator __t(*this); --(*this); return __t;}
855
Louis Dionne2580fdb2018-08-03 22:36:53 +0000856 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant333f50d2010-09-21 20:16:37 +0000857 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000859 friend _LIBCPP_INLINE_VISIBILITY
860 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000861 {return !(__x == __y);}
862
863private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000865 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier7310ec82016-07-19 17:56:20 +0000866 _LIBCPP_INLINE_VISIBILITY
867 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
868 _LIBCPP_INLINE_VISIBILITY
869 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000871 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
872 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
873 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
874 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
875 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
876 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877};
878
Eric Fiselier55263482016-02-20 05:28:30 +0000879template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000880class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881{
Eric Fiselier55263482016-02-20 05:28:30 +0000882 typedef __tree_node_types<_NodePtr> _NodeTypes;
883 typedef typename _NodeTypes::__node_pointer __node_pointer;
884 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000885 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
886 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000887 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000888
Eric Fiselier7310ec82016-07-19 17:56:20 +0000889 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000890
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891public:
Eric Fiselier55263482016-02-20 05:28:30 +0000892 typedef bidirectional_iterator_tag iterator_category;
893 typedef _Tp value_type;
894 typedef _DiffType difference_type;
895 typedef const value_type& reference;
896 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000897
Marshall Clow051c8482013-08-08 21:52:50 +0000898 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
899#if _LIBCPP_STD_VER > 11
900 : __ptr_(nullptr)
901#endif
902 {}
903
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904private:
Eric Fiselier55263482016-02-20 05:28:30 +0000905 typedef __tree_iterator<value_type, __node_pointer, difference_type>
906 __non_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000907public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000909 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
910 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911
Eric Fiselier7310ec82016-07-19 17:56:20 +0000912 _LIBCPP_INLINE_VISIBILITY reference operator*() const
913 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000914 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000915 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916
Howard Hinnant333f50d2010-09-21 20:16:37 +0000917 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000918 __tree_const_iterator& operator++() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000919 __ptr_ = static_cast<__iter_pointer>(
920 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000921 return *this;
922 }
923
Howard Hinnant333f50d2010-09-21 20:16:37 +0000924 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925 __tree_const_iterator operator++(int)
926 {__tree_const_iterator __t(*this); ++(*this); return __t;}
927
Howard Hinnant333f50d2010-09-21 20:16:37 +0000928 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000929 __tree_const_iterator& operator--() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000930 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
931 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000932 return *this;
933 }
934
Howard Hinnant333f50d2010-09-21 20:16:37 +0000935 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000936 __tree_const_iterator operator--(int)
937 {__tree_const_iterator __t(*this); --(*this); return __t;}
938
Howard Hinnant333f50d2010-09-21 20:16:37 +0000939 friend _LIBCPP_INLINE_VISIBILITY
940 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000941 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000942 friend _LIBCPP_INLINE_VISIBILITY
943 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000944 {return !(__x == __y);}
945
946private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000947 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000948 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
949 : __ptr_(__p) {}
Eric Fiselier7310ec82016-07-19 17:56:20 +0000950 _LIBCPP_INLINE_VISIBILITY
951 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
952 : __ptr_(__p) {}
953 _LIBCPP_INLINE_VISIBILITY
954 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
955
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000956 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000957 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
958 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
959 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
960 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
961 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000962
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000963};
964
Louis Dionne5fe0a6a2018-12-06 21:46:17 +0000965template<class _Tp, class _Compare>
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000966#ifndef _LIBCPP_CXX03_LANG
Louis Dionne5fe0a6a2018-12-06 21:46:17 +0000967 _LIBCPP_DIAGNOSE_WARNING(!std::__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
968 "the specified comparator type does not provide a const call operator")
969#endif
970int __diagnose_non_const_comparator();
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000971
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000972template <class _Tp, class _Compare, class _Allocator>
973class __tree
974{
975public:
976 typedef _Tp value_type;
977 typedef _Compare value_compare;
978 typedef _Allocator allocator_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000979
980private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000981 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier55263482016-02-20 05:28:30 +0000982 typedef typename __make_tree_node_types<value_type,
983 typename __alloc_traits::void_pointer>::type
984 _NodeTypes;
Eric Fiselierdb215062016-03-31 02:15:15 +0000985 typedef typename _NodeTypes::key_type key_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000986public:
987 typedef typename _NodeTypes::__node_value_type __node_value_type;
988 typedef typename _NodeTypes::__container_value_type __container_value_type;
989
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990 typedef typename __alloc_traits::pointer pointer;
991 typedef typename __alloc_traits::const_pointer const_pointer;
992 typedef typename __alloc_traits::size_type size_type;
993 typedef typename __alloc_traits::difference_type difference_type;
994
Eric Fiselier55263482016-02-20 05:28:30 +0000995public:
996 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000997
Eric Fiselier55263482016-02-20 05:28:30 +0000998 typedef typename _NodeTypes::__node_type __node;
999 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +00001000
1001 typedef typename _NodeTypes::__node_base_type __node_base;
1002 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +00001003
1004 typedef typename _NodeTypes::__end_node_type __end_node_t;
1005 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiselier55263482016-02-20 05:28:30 +00001006
Eric Fiselier7310ec82016-07-19 17:56:20 +00001007 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
1008 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
1009
Marshall Clow66302c62015-04-07 05:21:38 +00001010 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiselier55263482016-02-20 05:28:30 +00001011 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012
Eric Fiselier55263482016-02-20 05:28:30 +00001013private:
1014 // check for sane allocator pointer rebinding semantics. Rebinding the
1015 // allocator for a new pointer type should be exactly the same as rebinding
1016 // the pointer using 'pointer_traits'.
1017 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1018 "Allocator does not rebind pointers in a sane manner.");
1019 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1020 __node_base_allocator;
1021 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1022 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1023 "Allocator does not rebind pointers in a sane manner.");
1024
1025private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001026 __iter_pointer __begin_node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1028 __compressed_pair<size_type, value_compare> __pair3_;
1029
1030public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001031 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001032 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001034 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001035 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1036 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001037 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001038 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001039 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001041 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001042 pointer_traits<__end_node_ptr>::pointer_to(
1043 const_cast<__end_node_t&>(__pair1_.first())
1044 )
1045 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001048 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001051 const __node_allocator& __node_alloc() const _NOEXCEPT
1052 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001053 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001054 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001055 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001056 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001059 allocator_type __alloc() const _NOEXCEPT
1060 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001062 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001063 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001065 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001066 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001067 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001068 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001070 const value_compare& value_comp() const _NOEXCEPT
1071 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072public:
Eric Fiselier227b47c2016-02-20 07:12:17 +00001073
Howard Hinnant333f50d2010-09-21 20:16:37 +00001074 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +00001075 __node_pointer __root() const _NOEXCEPT
1076 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077
Eric Fiselier7310ec82016-07-19 17:56:20 +00001078 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1079 return _VSTD::addressof(__end_node()->__left_);
1080 }
1081
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +00001083 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001084
Howard Hinnant7686add2011-06-04 14:31:57 +00001085 explicit __tree(const value_compare& __comp)
1086 _NOEXCEPT_(
1087 is_nothrow_default_constructible<__node_allocator>::value &&
1088 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089 explicit __tree(const allocator_type& __a);
1090 __tree(const value_compare& __comp, const allocator_type& __a);
1091 __tree(const __tree& __t);
1092 __tree& operator=(const __tree& __t);
1093 template <class _InputIterator>
1094 void __assign_unique(_InputIterator __first, _InputIterator __last);
1095 template <class _InputIterator>
1096 void __assign_multi(_InputIterator __first, _InputIterator __last);
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001097#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7686add2011-06-04 14:31:57 +00001098 __tree(__tree&& __t)
1099 _NOEXCEPT_(
1100 is_nothrow_move_constructible<__node_allocator>::value &&
1101 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +00001103 __tree& operator=(__tree&& __t)
1104 _NOEXCEPT_(
1105 __node_traits::propagate_on_container_move_assignment::value &&
1106 is_nothrow_move_assignable<value_compare>::value &&
1107 is_nothrow_move_assignable<__node_allocator>::value);
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001108#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109
1110 ~__tree();
1111
Howard Hinnant333f50d2010-09-21 20:16:37 +00001112 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001113 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001114 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001115 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001116 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001117 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001118 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001119 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120
Howard Hinnant333f50d2010-09-21 20:16:37 +00001121 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001122 size_type max_size() const _NOEXCEPT
Eric Fiselieref3060e2016-11-23 01:18:56 +00001123 {return std::min<size_type>(
1124 __node_traits::max_size(__node_alloc()),
1125 numeric_limits<difference_type >::max());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001126
Howard Hinnant7686add2011-06-04 14:31:57 +00001127 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001128
Howard Hinnant7686add2011-06-04 14:31:57 +00001129 void swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001130#if _LIBCPP_STD_VER <= 11
Howard Hinnant7686add2011-06-04 14:31:57 +00001131 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001132 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001133 && (!__node_traits::propagate_on_container_swap::value ||
1134 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001135 );
Dimitry Andric1421cf02016-08-27 19:32:03 +00001136#else
1137 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1138#endif
Eric Fiselierdb215062016-03-31 02:15:15 +00001139
1140#ifndef _LIBCPP_CXX03_LANG
1141 template <class _Key, class ..._Args>
1142 pair<iterator, bool>
1143 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1144 template <class _Key, class ..._Args>
1145 iterator
1146 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147
1148 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001149 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierdb215062016-03-31 02:15:15 +00001150
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001152 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001153
Eric Fiselierdb215062016-03-31 02:15:15 +00001154 template <class... _Args>
1155 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156
Eric Fiselierdb215062016-03-31 02:15:15 +00001157 template <class... _Args>
1158 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001159
1160 template <class _Pp>
1161 _LIBCPP_INLINE_VISIBILITY
1162 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1163 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1164 __can_extract_key<_Pp, key_type>());
1165 }
1166
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001167 template <class _First, class _Second>
1168 _LIBCPP_INLINE_VISIBILITY
1169 typename enable_if<
1170 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1171 pair<iterator, bool>
1172 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1173 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1174 _VSTD::forward<_Second>(__s));
1175 }
1176
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001177 template <class... _Args>
1178 _LIBCPP_INLINE_VISIBILITY
1179 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1180 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1181 }
1182
1183 template <class _Pp>
1184 _LIBCPP_INLINE_VISIBILITY
1185 pair<iterator, bool>
1186 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1187 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1188 }
1189
1190 template <class _Pp>
1191 _LIBCPP_INLINE_VISIBILITY
1192 pair<iterator, bool>
1193 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1194 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1195 }
1196
1197 template <class _Pp>
1198 _LIBCPP_INLINE_VISIBILITY
1199 pair<iterator, bool>
1200 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1201 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1202 }
1203
1204 template <class _Pp>
1205 _LIBCPP_INLINE_VISIBILITY
1206 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1207 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1208 __can_extract_key<_Pp, key_type>());
1209 }
1210
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001211 template <class _First, class _Second>
1212 _LIBCPP_INLINE_VISIBILITY
1213 typename enable_if<
1214 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1215 iterator
1216 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1217 return __emplace_hint_unique_key_args(__p, __f,
1218 _VSTD::forward<_First>(__f),
1219 _VSTD::forward<_Second>(__s));
1220 }
1221
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001222 template <class... _Args>
1223 _LIBCPP_INLINE_VISIBILITY
1224 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1225 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1226 }
1227
1228 template <class _Pp>
1229 _LIBCPP_INLINE_VISIBILITY
1230 iterator
1231 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1232 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1233 }
1234
1235 template <class _Pp>
1236 _LIBCPP_INLINE_VISIBILITY
1237 iterator
1238 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1239 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1240 }
1241
1242 template <class _Pp>
1243 _LIBCPP_INLINE_VISIBILITY
1244 iterator
1245 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1246 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1247 }
1248
Eric Fiselierdb215062016-03-31 02:15:15 +00001249#else
1250 template <class _Key, class _Args>
1251 _LIBCPP_INLINE_VISIBILITY
1252 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1253 template <class _Key, class _Args>
1254 _LIBCPP_INLINE_VISIBILITY
1255 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001256#endif
1257
Eric Fiselierdb215062016-03-31 02:15:15 +00001258 _LIBCPP_INLINE_VISIBILITY
1259 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1260 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1261 }
1262
1263 _LIBCPP_INLINE_VISIBILITY
1264 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1265 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1266 }
1267
1268#ifdef _LIBCPP_CXX03_LANG
1269 _LIBCPP_INLINE_VISIBILITY
1270 iterator __insert_multi(const __container_value_type& __v);
1271 _LIBCPP_INLINE_VISIBILITY
1272 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1273#else
1274 _LIBCPP_INLINE_VISIBILITY
1275 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1276 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1277 }
1278
1279 _LIBCPP_INLINE_VISIBILITY
1280 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1281 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1282 }
1283
1284 template <class _Vp, class = typename enable_if<
1285 !is_same<typename __unconstref<_Vp>::type,
1286 __container_value_type
1287 >::value
1288 >::type>
1289 _LIBCPP_INLINE_VISIBILITY
1290 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1291 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1292 }
1293
1294 template <class _Vp, class = typename enable_if<
1295 !is_same<typename __unconstref<_Vp>::type,
1296 __container_value_type
1297 >::value
1298 >::type>
1299 _LIBCPP_INLINE_VISIBILITY
1300 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1301 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1302 }
1303
1304 _LIBCPP_INLINE_VISIBILITY
1305 iterator __insert_multi(__container_value_type&& __v) {
1306 return __emplace_multi(_VSTD::move(__v));
1307 }
1308
1309 _LIBCPP_INLINE_VISIBILITY
1310 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1311 return __emplace_hint_multi(__p, _VSTD::move(__v));
1312 }
1313
1314 template <class _Vp>
1315 _LIBCPP_INLINE_VISIBILITY
1316 iterator __insert_multi(_Vp&& __v) {
1317 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1318 }
1319
1320 template <class _Vp>
1321 _LIBCPP_INLINE_VISIBILITY
1322 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1323 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1324 }
1325
1326#endif // !_LIBCPP_CXX03_LANG
1327
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001330 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001331 iterator __node_insert_unique(const_iterator __p,
1332 __node_pointer __nd);
1333
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001334 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001335 iterator __node_insert_multi(__node_pointer __nd);
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1338
Erik Pilkington36fc7372018-08-01 01:33:38 +00001339
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001340 _LIBCPP_INLINE_VISIBILITY iterator
1341 __remove_node_pointer(__node_pointer) _NOEXCEPT;
Erik Pilkington36fc7372018-08-01 01:33:38 +00001342
1343#if _LIBCPP_STD_VER > 14
1344 template <class _NodeHandle, class _InsertReturnType>
1345 _LIBCPP_INLINE_VISIBILITY
1346 _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1347 template <class _NodeHandle>
1348 _LIBCPP_INLINE_VISIBILITY
1349 iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001350 template <class _Tree>
1351 _LIBCPP_INLINE_VISIBILITY
1352 void __node_handle_merge_unique(_Tree& __source);
Erik Pilkington36fc7372018-08-01 01:33:38 +00001353
1354 template <class _NodeHandle>
1355 _LIBCPP_INLINE_VISIBILITY
1356 iterator __node_handle_insert_multi(_NodeHandle&&);
1357 template <class _NodeHandle>
1358 _LIBCPP_INLINE_VISIBILITY
1359 iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001360 template <class _Tree>
1361 _LIBCPP_INLINE_VISIBILITY
1362 void __node_handle_merge_multi(_Tree& __source);
Erik Pilkington36fc7372018-08-01 01:33:38 +00001363
1364
1365 template <class _NodeHandle>
1366 _LIBCPP_INLINE_VISIBILITY
1367 _NodeHandle __node_handle_extract(key_type const&);
1368 template <class _NodeHandle>
1369 _LIBCPP_INLINE_VISIBILITY
1370 _NodeHandle __node_handle_extract(const_iterator);
1371#endif
1372
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001373 iterator erase(const_iterator __p);
1374 iterator erase(const_iterator __f, const_iterator __l);
1375 template <class _Key>
1376 size_type __erase_unique(const _Key& __k);
1377 template <class _Key>
1378 size_type __erase_multi(const _Key& __k);
1379
Eric Fiselier7310ec82016-07-19 17:56:20 +00001380 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001381 __node_base_pointer& __child,
Erik Pilkington71ac96a2018-10-31 17:31:35 +00001382 __node_base_pointer __new_node) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001383
1384 template <class _Key>
1385 iterator find(const _Key& __v);
1386 template <class _Key>
1387 const_iterator find(const _Key& __v) const;
1388
1389 template <class _Key>
1390 size_type __count_unique(const _Key& __k) const;
1391 template <class _Key>
1392 size_type __count_multi(const _Key& __k) const;
1393
1394 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001395 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001396 iterator lower_bound(const _Key& __v)
1397 {return __lower_bound(__v, __root(), __end_node());}
1398 template <class _Key>
1399 iterator __lower_bound(const _Key& __v,
1400 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001401 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001403 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 const_iterator lower_bound(const _Key& __v) const
1405 {return __lower_bound(__v, __root(), __end_node());}
1406 template <class _Key>
1407 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001408 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001409 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001410 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001411 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001412 iterator upper_bound(const _Key& __v)
1413 {return __upper_bound(__v, __root(), __end_node());}
1414 template <class _Key>
1415 iterator __upper_bound(const _Key& __v,
1416 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001417 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001418 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001419 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001420 const_iterator upper_bound(const _Key& __v) const
1421 {return __upper_bound(__v, __root(), __end_node());}
1422 template <class _Key>
1423 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001424 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001425 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 template <class _Key>
1427 pair<iterator, iterator>
1428 __equal_range_unique(const _Key& __k);
1429 template <class _Key>
1430 pair<const_iterator, const_iterator>
1431 __equal_range_unique(const _Key& __k) const;
1432
1433 template <class _Key>
1434 pair<iterator, iterator>
1435 __equal_range_multi(const _Key& __k);
1436 template <class _Key>
1437 pair<const_iterator, const_iterator>
1438 __equal_range_multi(const _Key& __k) const;
1439
Howard Hinnant99968442011-11-29 18:15:50 +00001440 typedef __tree_node_destructor<__node_allocator> _Dp;
1441 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001442
Howard Hinnant8b537682011-06-04 17:10:24 +00001443 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001445 __node_base_pointer&
1446 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1447 __node_base_pointer&
1448 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1449 __node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 __find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001451 __parent_pointer& __parent, const key_type& __v);
Eric Fiselier856712b2017-01-05 06:06:18 +00001452 // FIXME: Make this function const qualified. Unfortunetly doing so
1453 // breaks existing code which uses non-const callable comparators.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001455 __node_base_pointer&
1456 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001457 template <class _Key>
Eric Fiselier856712b2017-01-05 06:06:18 +00001458 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1459 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1460 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1461 }
1462 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001463 __node_base_pointer&
1464 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1465 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 const _Key& __v);
1467
Eric Fiselierdb215062016-03-31 02:15:15 +00001468#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001470 __node_holder __construct_node(_Args&& ...__args);
1471#else
1472 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473#endif
1474
Howard Hinnant7686add2011-06-04 14:31:57 +00001475 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001476
Howard Hinnant333f50d2010-09-21 20:16:37 +00001477 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478 void __copy_assign_alloc(const __tree& __t)
1479 {__copy_assign_alloc(__t, integral_constant<bool,
1480 __node_traits::propagate_on_container_copy_assignment::value>());}
1481
Howard Hinnant333f50d2010-09-21 20:16:37 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 void __copy_assign_alloc(const __tree& __t, true_type)
Marshall Clow546498c2016-08-17 23:24:02 +00001484 {
1485 if (__node_alloc() != __t.__node_alloc())
Louis Dionne2580fdb2018-08-03 22:36:53 +00001486 clear();
Marshall Clow546498c2016-08-17 23:24:02 +00001487 __node_alloc() = __t.__node_alloc();
1488 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001489 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001490 void __copy_assign_alloc(const __tree&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491
1492 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001493 void __move_assign(__tree& __t, true_type)
1494 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1495 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496
Howard Hinnant333f50d2010-09-21 20:16:37 +00001497 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001499 _NOEXCEPT_(
1500 !__node_traits::propagate_on_container_move_assignment::value ||
1501 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 {__move_assign_alloc(__t, integral_constant<bool,
1503 __node_traits::propagate_on_container_move_assignment::value>());}
1504
Howard Hinnant333f50d2010-09-21 20:16:37 +00001505 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001507 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001508 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001509 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001510 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512 __node_pointer __detach();
1513 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001514
Eric Fiselierc3589a82017-01-04 23:56:00 +00001515 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1516 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517};
1518
1519template <class _Tp, class _Compare, class _Allocator>
1520__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001521 _NOEXCEPT_(
1522 is_nothrow_default_constructible<__node_allocator>::value &&
1523 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001524 : __pair3_(0, __comp)
1525{
1526 __begin_node() = __end_node();
1527}
1528
1529template <class _Tp, class _Compare, class _Allocator>
1530__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001531 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001532 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001533 __pair3_(0)
1534{
1535 __begin_node() = __end_node();
1536}
1537
1538template <class _Tp, class _Compare, class _Allocator>
1539__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1540 const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001541 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001542 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001543 __pair3_(0, __comp)
1544{
1545 __begin_node() = __end_node();
1546}
1547
1548// Precondition: size() != 0
1549template <class _Tp, class _Compare, class _Allocator>
1550typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1551__tree<_Tp, _Compare, _Allocator>::__detach()
1552{
Eric Fiselier7310ec82016-07-19 17:56:20 +00001553 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554 __begin_node() = __end_node();
1555 __end_node()->__left_->__parent_ = nullptr;
1556 __end_node()->__left_ = nullptr;
1557 size() = 0;
1558 // __cache->__left_ == nullptr
1559 if (__cache->__right_ != nullptr)
1560 __cache = static_cast<__node_pointer>(__cache->__right_);
1561 // __cache->__left_ == nullptr
1562 // __cache->__right_ == nullptr
1563 return __cache;
1564}
1565
1566// Precondition: __cache != nullptr
1567// __cache->left_ == nullptr
1568// __cache->right_ == nullptr
1569// This is no longer a red-black tree
1570template <class _Tp, class _Compare, class _Allocator>
1571typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1572__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1573{
1574 if (__cache->__parent_ == nullptr)
1575 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001576 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001577 {
1578 __cache->__parent_->__left_ = nullptr;
1579 __cache = static_cast<__node_pointer>(__cache->__parent_);
1580 if (__cache->__right_ == nullptr)
1581 return __cache;
1582 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1583 }
1584 // __cache is right child
Eric Fiselier7310ec82016-07-19 17:56:20 +00001585 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001586 __cache = static_cast<__node_pointer>(__cache->__parent_);
1587 if (__cache->__left_ == nullptr)
1588 return __cache;
1589 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1590}
1591
1592template <class _Tp, class _Compare, class _Allocator>
1593__tree<_Tp, _Compare, _Allocator>&
1594__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1595{
1596 if (this != &__t)
1597 {
1598 value_comp() = __t.value_comp();
1599 __copy_assign_alloc(__t);
1600 __assign_multi(__t.begin(), __t.end());
1601 }
1602 return *this;
1603}
1604
1605template <class _Tp, class _Compare, class _Allocator>
1606template <class _InputIterator>
1607void
1608__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1609{
Eric Fiselierdb215062016-03-31 02:15:15 +00001610 typedef iterator_traits<_InputIterator> _ITraits;
1611 typedef typename _ITraits::value_type _ItValueType;
1612 static_assert((is_same<_ItValueType, __container_value_type>::value),
1613 "__assign_unique may only be called with the containers value type");
1614
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001615 if (size() != 0)
1616 {
1617 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001618#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619 try
1620 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001621#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 for (; __cache != nullptr && __first != __last; ++__first)
1623 {
1624 __cache->__value_ = *__first;
1625 __node_pointer __next = __detach(__cache);
1626 __node_insert_unique(__cache);
1627 __cache = __next;
1628 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001629#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001630 }
1631 catch (...)
1632 {
1633 while (__cache->__parent_ != nullptr)
1634 __cache = static_cast<__node_pointer>(__cache->__parent_);
1635 destroy(__cache);
1636 throw;
1637 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001638#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 if (__cache != nullptr)
1640 {
1641 while (__cache->__parent_ != nullptr)
1642 __cache = static_cast<__node_pointer>(__cache->__parent_);
1643 destroy(__cache);
1644 }
1645 }
1646 for (; __first != __last; ++__first)
1647 __insert_unique(*__first);
1648}
1649
1650template <class _Tp, class _Compare, class _Allocator>
1651template <class _InputIterator>
1652void
1653__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1654{
Eric Fiselierdb215062016-03-31 02:15:15 +00001655 typedef iterator_traits<_InputIterator> _ITraits;
1656 typedef typename _ITraits::value_type _ItValueType;
1657 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1658 is_same<_ItValueType, __node_value_type>::value),
1659 "__assign_multi may only be called with the containers value type"
1660 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 if (size() != 0)
1662 {
1663 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001664#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 try
1666 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001667#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 for (; __cache != nullptr && __first != __last; ++__first)
1669 {
1670 __cache->__value_ = *__first;
1671 __node_pointer __next = __detach(__cache);
1672 __node_insert_multi(__cache);
1673 __cache = __next;
1674 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001675#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 }
1677 catch (...)
1678 {
1679 while (__cache->__parent_ != nullptr)
1680 __cache = static_cast<__node_pointer>(__cache->__parent_);
1681 destroy(__cache);
1682 throw;
1683 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001684#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 if (__cache != nullptr)
1686 {
1687 while (__cache->__parent_ != nullptr)
1688 __cache = static_cast<__node_pointer>(__cache->__parent_);
1689 destroy(__cache);
1690 }
1691 }
1692 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001693 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694}
1695
1696template <class _Tp, class _Compare, class _Allocator>
1697__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001698 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001699 __pair1_(__second_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 __pair3_(0, __t.value_comp())
1701{
1702 __begin_node() = __end_node();
1703}
1704
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001705#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706
1707template <class _Tp, class _Compare, class _Allocator>
1708__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001709 _NOEXCEPT_(
1710 is_nothrow_move_constructible<__node_allocator>::value &&
1711 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001712 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1713 __pair1_(_VSTD::move(__t.__pair1_)),
1714 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715{
1716 if (size() == 0)
1717 __begin_node() = __end_node();
1718 else
1719 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001720 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001721 __t.__begin_node() = __t.__end_node();
1722 __t.__end_node()->__left_ = nullptr;
1723 __t.size() = 0;
1724 }
1725}
1726
1727template <class _Tp, class _Compare, class _Allocator>
1728__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
Eric Fiselier161ccc12017-04-13 00:34:24 +00001729 : __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001730 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001731{
1732 if (__a == __t.__alloc())
1733 {
1734 if (__t.size() == 0)
1735 __begin_node() = __end_node();
1736 else
1737 {
1738 __begin_node() = __t.__begin_node();
1739 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +00001740 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741 size() = __t.size();
1742 __t.__begin_node() = __t.__end_node();
1743 __t.__end_node()->__left_ = nullptr;
1744 __t.size() = 0;
1745 }
1746 }
1747 else
1748 {
1749 __begin_node() = __end_node();
1750 }
1751}
1752
1753template <class _Tp, class _Compare, class _Allocator>
1754void
1755__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001756 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1757 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758{
1759 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1760 __begin_node_ = __t.__begin_node_;
1761 __pair1_.first() = __t.__pair1_.first();
1762 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001763 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 if (size() == 0)
1765 __begin_node() = __end_node();
1766 else
1767 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001768 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 __t.__begin_node() = __t.__end_node();
1770 __t.__end_node()->__left_ = nullptr;
1771 __t.size() = 0;
1772 }
1773}
1774
1775template <class _Tp, class _Compare, class _Allocator>
1776void
1777__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1778{
1779 if (__node_alloc() == __t.__node_alloc())
1780 __move_assign(__t, true_type());
1781 else
1782 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001783 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784 const_iterator __e = end();
1785 if (size() != 0)
1786 {
1787 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001788#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 try
1790 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001791#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001792 while (__cache != nullptr && __t.size() != 0)
1793 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001794 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795 __node_pointer __next = __detach(__cache);
1796 __node_insert_multi(__cache);
1797 __cache = __next;
1798 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001799#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 }
1801 catch (...)
1802 {
1803 while (__cache->__parent_ != nullptr)
1804 __cache = static_cast<__node_pointer>(__cache->__parent_);
1805 destroy(__cache);
1806 throw;
1807 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001808#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 if (__cache != nullptr)
1810 {
1811 while (__cache->__parent_ != nullptr)
1812 __cache = static_cast<__node_pointer>(__cache->__parent_);
1813 destroy(__cache);
1814 }
1815 }
1816 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001817 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 }
1819}
1820
1821template <class _Tp, class _Compare, class _Allocator>
1822__tree<_Tp, _Compare, _Allocator>&
1823__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001824 _NOEXCEPT_(
1825 __node_traits::propagate_on_container_move_assignment::value &&
1826 is_nothrow_move_assignable<value_compare>::value &&
1827 is_nothrow_move_assignable<__node_allocator>::value)
Louis Dionne2580fdb2018-08-03 22:36:53 +00001828
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829{
1830 __move_assign(__t, integral_constant<bool,
1831 __node_traits::propagate_on_container_move_assignment::value>());
1832 return *this;
1833}
1834
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001835#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836
1837template <class _Tp, class _Compare, class _Allocator>
1838__tree<_Tp, _Compare, _Allocator>::~__tree()
1839{
Marshall Clow1a933122016-06-30 22:05:45 +00001840 static_assert((is_copy_constructible<value_compare>::value),
1841 "Comparator must be copy-constructible.");
Eric Fiselierebaf7da2017-01-13 22:02:08 +00001842 destroy(__root());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843}
1844
1845template <class _Tp, class _Compare, class _Allocator>
1846void
Howard Hinnant7686add2011-06-04 14:31:57 +00001847__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848{
1849 if (__nd != nullptr)
1850 {
1851 destroy(static_cast<__node_pointer>(__nd->__left_));
1852 destroy(static_cast<__node_pointer>(__nd->__right_));
1853 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001854 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 __node_traits::deallocate(__na, __nd, 1);
1856 }
1857}
1858
1859template <class _Tp, class _Compare, class _Allocator>
1860void
1861__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001862#if _LIBCPP_STD_VER <= 11
Marshall Clow7d914d12015-07-13 20:04:56 +00001863 _NOEXCEPT_(
1864 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001865 && (!__node_traits::propagate_on_container_swap::value ||
1866 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001867 )
Dimitry Andric1421cf02016-08-27 19:32:03 +00001868#else
1869 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1870#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001872 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001873 swap(__begin_node_, __t.__begin_node_);
1874 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001875 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 __pair3_.swap(__t.__pair3_);
1877 if (size() == 0)
1878 __begin_node() = __end_node();
1879 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001880 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881 if (__t.size() == 0)
1882 __t.__begin_node() = __t.__end_node();
1883 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001884 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885}
1886
1887template <class _Tp, class _Compare, class _Allocator>
1888void
Howard Hinnant7686add2011-06-04 14:31:57 +00001889__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890{
1891 destroy(__root());
1892 size() = 0;
1893 __begin_node() = __end_node();
1894 __end_node()->__left_ = nullptr;
1895}
1896
1897// Find lower_bound place to insert
1898// Set __parent to parent of null leaf
1899// Return reference to null leaf
1900template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001901typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1902__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001903 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904{
1905 __node_pointer __nd = __root();
1906 if (__nd != nullptr)
1907 {
1908 while (true)
1909 {
1910 if (value_comp()(__nd->__value_, __v))
1911 {
1912 if (__nd->__right_ != nullptr)
1913 __nd = static_cast<__node_pointer>(__nd->__right_);
1914 else
1915 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001916 __parent = static_cast<__parent_pointer>(__nd);
1917 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001918 }
1919 }
1920 else
1921 {
1922 if (__nd->__left_ != nullptr)
1923 __nd = static_cast<__node_pointer>(__nd->__left_);
1924 else
1925 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001926 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 return __parent->__left_;
1928 }
1929 }
1930 }
1931 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001932 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001933 return __parent->__left_;
1934}
1935
1936// Find upper_bound place to insert
1937// Set __parent to parent of null leaf
1938// Return reference to null leaf
1939template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001940typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1941__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001942 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943{
1944 __node_pointer __nd = __root();
1945 if (__nd != nullptr)
1946 {
1947 while (true)
1948 {
1949 if (value_comp()(__v, __nd->__value_))
1950 {
1951 if (__nd->__left_ != nullptr)
1952 __nd = static_cast<__node_pointer>(__nd->__left_);
1953 else
1954 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001955 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001956 return __parent->__left_;
1957 }
1958 }
1959 else
1960 {
1961 if (__nd->__right_ != nullptr)
1962 __nd = static_cast<__node_pointer>(__nd->__right_);
1963 else
1964 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001965 __parent = static_cast<__parent_pointer>(__nd);
1966 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001967 }
1968 }
1969 }
1970 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001971 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 return __parent->__left_;
1973}
1974
1975// Find leaf place to insert closest to __hint
1976// First check prior to __hint.
1977// Next check after __hint.
1978// Next do O(log N) search.
1979// Set __parent to parent of null leaf
1980// Return reference to null leaf
1981template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001982typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001984 __parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001985 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001986{
1987 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1988 {
1989 // __v <= *__hint
1990 const_iterator __prior = __hint;
1991 if (__prior == begin() || !value_comp()(__v, *--__prior))
1992 {
1993 // *prev(__hint) <= __v <= *__hint
1994 if (__hint.__ptr_->__left_ == nullptr)
1995 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001996 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001997 return __parent->__left_;
1998 }
1999 else
2000 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002001 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2002 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 }
2004 }
2005 // __v < *prev(__hint)
2006 return __find_leaf_high(__parent, __v);
2007 }
2008 // else __v > *__hint
2009 return __find_leaf_low(__parent, __v);
2010}
2011
2012// Find place to insert if __v doesn't exist
2013// Set __parent to parent of null leaf
2014// Return reference to null leaf
2015// If __v exists, set parent to node of __v and return reference to node of __v
2016template <class _Tp, class _Compare, class _Allocator>
2017template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00002018typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
2019__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 const _Key& __v)
2021{
2022 __node_pointer __nd = __root();
Eric Fiselier7310ec82016-07-19 17:56:20 +00002023 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002024 if (__nd != nullptr)
2025 {
2026 while (true)
2027 {
2028 if (value_comp()(__v, __nd->__value_))
2029 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002030 if (__nd->__left_ != nullptr) {
2031 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002033 } else {
2034 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 return __parent->__left_;
2036 }
2037 }
2038 else if (value_comp()(__nd->__value_, __v))
2039 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002040 if (__nd->__right_ != nullptr) {
2041 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002043 } else {
2044 __parent = static_cast<__parent_pointer>(__nd);
2045 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002046 }
2047 }
2048 else
2049 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002050 __parent = static_cast<__parent_pointer>(__nd);
2051 return *__nd_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052 }
2053 }
2054 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00002055 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056 return __parent->__left_;
2057}
2058
2059// Find place to insert if __v doesn't exist
2060// First check prior to __hint.
2061// Next check after __hint.
2062// Next do O(log N) search.
2063// Set __parent to parent of null leaf
2064// Return reference to null leaf
2065// If __v exists, set parent to node of __v and return reference to node of __v
2066template <class _Tp, class _Compare, class _Allocator>
2067template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00002068typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002069__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002070 __parent_pointer& __parent,
2071 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002072 const _Key& __v)
2073{
2074 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2075 {
2076 // __v < *__hint
2077 const_iterator __prior = __hint;
2078 if (__prior == begin() || value_comp()(*--__prior, __v))
2079 {
2080 // *prev(__hint) < __v < *__hint
2081 if (__hint.__ptr_->__left_ == nullptr)
2082 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002083 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084 return __parent->__left_;
2085 }
2086 else
2087 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002088 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2089 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090 }
2091 }
2092 // __v <= *prev(__hint)
2093 return __find_equal(__parent, __v);
2094 }
2095 else if (value_comp()(*__hint, __v)) // check after
2096 {
2097 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00002098 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099 if (__next == end() || value_comp()(__v, *__next))
2100 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002101 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002102 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002104 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2105 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106 }
2107 else
2108 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002109 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110 return __parent->__left_;
2111 }
2112 }
2113 // *next(__hint) <= __v
2114 return __find_equal(__parent, __v);
2115 }
2116 // else __v == *__hint
Eric Fiselier7310ec82016-07-19 17:56:20 +00002117 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2118 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2119 return __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002120}
2121
2122template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington71ac96a2018-10-31 17:31:35 +00002123void __tree<_Tp, _Compare, _Allocator>::__insert_node_at(
2124 __parent_pointer __parent, __node_base_pointer& __child,
2125 __node_base_pointer __new_node) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002126{
2127 __new_node->__left_ = nullptr;
2128 __new_node->__right_ = nullptr;
2129 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002130 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131 __child = __new_node;
2132 if (__begin_node()->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002133 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 __tree_balance_after_insert(__end_node()->__left_, __child);
2135 ++size();
2136}
2137
Eric Fiselierdb215062016-03-31 02:15:15 +00002138#ifndef _LIBCPP_CXX03_LANG
2139template <class _Tp, class _Compare, class _Allocator>
2140template <class _Key, class... _Args>
2141pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2142__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2143#else
2144template <class _Tp, class _Compare, class _Allocator>
2145template <class _Key, class _Args>
2146pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2147__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2148#endif
2149{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002150 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002151 __node_base_pointer& __child = __find_equal(__parent, __k);
2152 __node_pointer __r = static_cast<__node_pointer>(__child);
2153 bool __inserted = false;
2154 if (__child == nullptr)
2155 {
2156#ifndef _LIBCPP_CXX03_LANG
2157 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2158#else
2159 __node_holder __h = __construct_node(__args);
2160#endif
2161 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2162 __r = __h.release();
2163 __inserted = true;
2164 }
2165 return pair<iterator, bool>(iterator(__r), __inserted);
2166}
2167
2168
2169#ifndef _LIBCPP_CXX03_LANG
2170template <class _Tp, class _Compare, class _Allocator>
2171template <class _Key, class... _Args>
2172typename __tree<_Tp, _Compare, _Allocator>::iterator
2173__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2174 const_iterator __p, _Key const& __k, _Args&&... __args)
2175#else
2176template <class _Tp, class _Compare, class _Allocator>
2177template <class _Key, class _Args>
2178typename __tree<_Tp, _Compare, _Allocator>::iterator
2179__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2180 const_iterator __p, _Key const& __k, _Args& __args)
2181#endif
2182{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002183 __parent_pointer __parent;
2184 __node_base_pointer __dummy;
2185 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierdb215062016-03-31 02:15:15 +00002186 __node_pointer __r = static_cast<__node_pointer>(__child);
2187 if (__child == nullptr)
2188 {
2189#ifndef _LIBCPP_CXX03_LANG
2190 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2191#else
2192 __node_holder __h = __construct_node(__args);
2193#endif
2194 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2195 __r = __h.release();
2196 }
2197 return iterator(__r);
2198}
2199
2200
2201#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202
2203template <class _Tp, class _Compare, class _Allocator>
2204template <class ..._Args>
2205typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2206__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2207{
Eric Fiselierdb215062016-03-31 02:15:15 +00002208 static_assert(!__is_tree_value_type<_Args...>::value,
2209 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002211 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002212 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 __h.get_deleter().__value_constructed = true;
2214 return __h;
2215}
2216
Eric Fiselierdb215062016-03-31 02:15:15 +00002217
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218template <class _Tp, class _Compare, class _Allocator>
2219template <class... _Args>
2220pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002221__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002223 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002224 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2226 __node_pointer __r = static_cast<__node_pointer>(__child);
2227 bool __inserted = false;
2228 if (__child == nullptr)
2229 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002230 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 __r = __h.release();
2232 __inserted = true;
2233 }
2234 return pair<iterator, bool>(iterator(__r), __inserted);
2235}
2236
2237template <class _Tp, class _Compare, class _Allocator>
2238template <class... _Args>
2239typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002240__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002242 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002243 __parent_pointer __parent;
2244 __node_base_pointer __dummy;
2245 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246 __node_pointer __r = static_cast<__node_pointer>(__child);
2247 if (__child == nullptr)
2248 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002249 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 __r = __h.release();
2251 }
2252 return iterator(__r);
2253}
2254
2255template <class _Tp, class _Compare, class _Allocator>
2256template <class... _Args>
2257typename __tree<_Tp, _Compare, _Allocator>::iterator
2258__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2259{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002260 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002261 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002262 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002263 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002264 return iterator(static_cast<__node_pointer>(__h.release()));
2265}
2266
2267template <class _Tp, class _Compare, class _Allocator>
2268template <class... _Args>
2269typename __tree<_Tp, _Compare, _Allocator>::iterator
2270__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2271 _Args&&... __args)
2272{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002273 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002274 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002275 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002276 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277 return iterator(static_cast<__node_pointer>(__h.release()));
2278}
2279
Howard Hinnant73d21a42010-09-04 23:28:19 +00002280
Eric Fiselierdb215062016-03-31 02:15:15 +00002281#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282
2283template <class _Tp, class _Compare, class _Allocator>
2284typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002285__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002286{
2287 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002288 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002289 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002290 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002291 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292}
2293
Eric Fiselierdb215062016-03-31 02:15:15 +00002294#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002295
Eric Fiselierdb215062016-03-31 02:15:15 +00002296#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002297template <class _Tp, class _Compare, class _Allocator>
2298typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002299__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002301 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002302 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002304 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 return iterator(__h.release());
2306}
2307
2308template <class _Tp, class _Compare, class _Allocator>
2309typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002310__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002312 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002313 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002314 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002315 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 return iterator(__h.release());
2317}
Eric Fiselierdb215062016-03-31 02:15:15 +00002318#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002319
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002320template <class _Tp, class _Compare, class _Allocator>
2321pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2322__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2323{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002324 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2326 __node_pointer __r = static_cast<__node_pointer>(__child);
2327 bool __inserted = false;
2328 if (__child == nullptr)
2329 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002330 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331 __r = __nd;
2332 __inserted = true;
2333 }
2334 return pair<iterator, bool>(iterator(__r), __inserted);
2335}
2336
2337template <class _Tp, class _Compare, class _Allocator>
2338typename __tree<_Tp, _Compare, _Allocator>::iterator
2339__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2340 __node_pointer __nd)
2341{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002342 __parent_pointer __parent;
2343 __node_base_pointer __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002344 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2345 __node_pointer __r = static_cast<__node_pointer>(__child);
2346 if (__child == nullptr)
2347 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002348 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002349 __r = __nd;
2350 }
2351 return iterator(__r);
2352}
2353
2354template <class _Tp, class _Compare, class _Allocator>
2355typename __tree<_Tp, _Compare, _Allocator>::iterator
2356__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2357{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002358 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002359 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002360 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002361 return iterator(__nd);
2362}
2363
2364template <class _Tp, class _Compare, class _Allocator>
2365typename __tree<_Tp, _Compare, _Allocator>::iterator
2366__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2367 __node_pointer __nd)
2368{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002369 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002370 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002371 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002372 return iterator(__nd);
2373}
2374
2375template <class _Tp, class _Compare, class _Allocator>
2376typename __tree<_Tp, _Compare, _Allocator>::iterator
Erik Pilkington71ac96a2018-10-31 17:31:35 +00002377__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr) _NOEXCEPT
Erik Pilkington36fc7372018-08-01 01:33:38 +00002378{
2379 iterator __r(__ptr);
2380 ++__r;
2381 if (__begin_node() == __ptr)
2382 __begin_node() = __r.__ptr_;
2383 --size();
2384 __tree_remove(__end_node()->__left_,
2385 static_cast<__node_base_pointer>(__ptr));
2386 return __r;
2387}
2388
2389#if _LIBCPP_STD_VER > 14
2390template <class _Tp, class _Compare, class _Allocator>
2391template <class _NodeHandle, class _InsertReturnType>
2392_LIBCPP_INLINE_VISIBILITY
2393_InsertReturnType
2394__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2395 _NodeHandle&& __nh)
2396{
2397 if (__nh.empty())
2398 return _InsertReturnType{end(), false, _NodeHandle()};
2399
2400 __node_pointer __ptr = __nh.__ptr_;
2401 __parent_pointer __parent;
2402 __node_base_pointer& __child = __find_equal(__parent,
2403 __ptr->__value_);
2404 if (__child != nullptr)
2405 return _InsertReturnType{
2406 iterator(static_cast<__node_pointer>(__child)),
2407 false, _VSTD::move(__nh)};
2408
2409 __insert_node_at(__parent, __child,
2410 static_cast<__node_base_pointer>(__ptr));
2411 __nh.__release();
2412 return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
2413}
2414
2415template <class _Tp, class _Compare, class _Allocator>
2416template <class _NodeHandle>
2417_LIBCPP_INLINE_VISIBILITY
2418typename __tree<_Tp, _Compare, _Allocator>::iterator
2419__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2420 const_iterator __hint, _NodeHandle&& __nh)
2421{
2422 if (__nh.empty())
2423 return end();
2424
2425 __node_pointer __ptr = __nh.__ptr_;
2426 __parent_pointer __parent;
2427 __node_base_pointer __dummy;
2428 __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy,
2429 __ptr->__value_);
2430 __node_pointer __r = static_cast<__node_pointer>(__child);
2431 if (__child == nullptr)
2432 {
2433 __insert_node_at(__parent, __child,
2434 static_cast<__node_base_pointer>(__ptr));
2435 __r = __ptr;
2436 __nh.__release();
2437 }
2438 return iterator(__r);
2439}
2440
2441template <class _Tp, class _Compare, class _Allocator>
2442template <class _NodeHandle>
2443_LIBCPP_INLINE_VISIBILITY
2444_NodeHandle
2445__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key)
2446{
2447 iterator __it = find(__key);
2448 if (__it == end())
2449 return _NodeHandle();
2450 return __node_handle_extract<_NodeHandle>(__it);
2451}
2452
2453template <class _Tp, class _Compare, class _Allocator>
2454template <class _NodeHandle>
2455_LIBCPP_INLINE_VISIBILITY
2456_NodeHandle
2457__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p)
2458{
2459 __node_pointer __np = __p.__get_np();
2460 __remove_node_pointer(__np);
2461 return _NodeHandle(__np, __alloc());
2462}
2463
2464template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington71ac96a2018-10-31 17:31:35 +00002465template <class _Tree>
2466_LIBCPP_INLINE_VISIBILITY
2467void
2468__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_unique(_Tree& __source)
2469{
2470 static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2471
2472 for (typename _Tree::iterator __i = __source.begin();
2473 __i != __source.end();)
2474 {
2475 __node_pointer __src_ptr = __i.__get_np();
2476 __parent_pointer __parent;
2477 __node_base_pointer& __child =
2478 __find_equal(__parent, _NodeTypes::__get_key(__src_ptr->__value_));
2479 ++__i;
2480 if (__child != nullptr)
2481 continue;
2482 __source.__remove_node_pointer(__src_ptr);
2483 __insert_node_at(__parent, __child,
2484 static_cast<__node_base_pointer>(__src_ptr));
2485 }
2486}
2487
2488template <class _Tp, class _Compare, class _Allocator>
Erik Pilkington36fc7372018-08-01 01:33:38 +00002489template <class _NodeHandle>
2490_LIBCPP_INLINE_VISIBILITY
2491typename __tree<_Tp, _Compare, _Allocator>::iterator
2492__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh)
2493{
2494 if (__nh.empty())
2495 return end();
2496 __node_pointer __ptr = __nh.__ptr_;
2497 __parent_pointer __parent;
2498 __node_base_pointer& __child = __find_leaf_high(
2499 __parent, _NodeTypes::__get_key(__ptr->__value_));
2500 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2501 __nh.__release();
2502 return iterator(__ptr);
2503}
2504
2505template <class _Tp, class _Compare, class _Allocator>
2506template <class _NodeHandle>
2507_LIBCPP_INLINE_VISIBILITY
2508typename __tree<_Tp, _Compare, _Allocator>::iterator
2509__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(
2510 const_iterator __hint, _NodeHandle&& __nh)
2511{
2512 if (__nh.empty())
2513 return end();
2514
2515 __node_pointer __ptr = __nh.__ptr_;
2516 __parent_pointer __parent;
2517 __node_base_pointer& __child = __find_leaf(__hint, __parent,
2518 _NodeTypes::__get_key(__ptr->__value_));
2519 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2520 __nh.__release();
2521 return iterator(__ptr);
2522}
2523
Erik Pilkington71ac96a2018-10-31 17:31:35 +00002524template <class _Tp, class _Compare, class _Allocator>
2525template <class _Tree>
2526_LIBCPP_INLINE_VISIBILITY
2527void
2528__tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(_Tree& __source)
2529{
2530 static_assert(is_same<typename _Tree::__node_pointer, __node_pointer>::value, "");
2531
2532 for (typename _Tree::iterator __i = __source.begin();
2533 __i != __source.end();)
2534 {
2535 __node_pointer __src_ptr = __i.__get_np();
2536 __parent_pointer __parent;
2537 __node_base_pointer& __child = __find_leaf_high(
2538 __parent, _NodeTypes::__get_key(__src_ptr->__value_));
2539 ++__i;
2540 __source.__remove_node_pointer(__src_ptr);
2541 __insert_node_at(__parent, __child,
2542 static_cast<__node_base_pointer>(__src_ptr));
2543 }
2544}
2545
Erik Pilkington36fc7372018-08-01 01:33:38 +00002546#endif // _LIBCPP_STD_VER > 14
2547
2548template <class _Tp, class _Compare, class _Allocator>
2549typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2551{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002552 __node_pointer __np = __p.__get_np();
Erik Pilkington36fc7372018-08-01 01:33:38 +00002553 iterator __r = __remove_node_pointer(__np);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00002555 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2556 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002557 __node_traits::deallocate(__na, __np, 1);
2558 return __r;
2559}
2560
2561template <class _Tp, class _Compare, class _Allocator>
2562typename __tree<_Tp, _Compare, _Allocator>::iterator
2563__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2564{
2565 while (__f != __l)
2566 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002567 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568}
2569
2570template <class _Tp, class _Compare, class _Allocator>
2571template <class _Key>
2572typename __tree<_Tp, _Compare, _Allocator>::size_type
2573__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2574{
2575 iterator __i = find(__k);
2576 if (__i == end())
2577 return 0;
2578 erase(__i);
2579 return 1;
2580}
2581
2582template <class _Tp, class _Compare, class _Allocator>
2583template <class _Key>
2584typename __tree<_Tp, _Compare, _Allocator>::size_type
2585__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2586{
2587 pair<iterator, iterator> __p = __equal_range_multi(__k);
2588 size_type __r = 0;
2589 for (; __p.first != __p.second; ++__r)
2590 __p.first = erase(__p.first);
2591 return __r;
2592}
2593
2594template <class _Tp, class _Compare, class _Allocator>
2595template <class _Key>
2596typename __tree<_Tp, _Compare, _Allocator>::iterator
2597__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2598{
2599 iterator __p = __lower_bound(__v, __root(), __end_node());
2600 if (__p != end() && !value_comp()(__v, *__p))
2601 return __p;
2602 return end();
2603}
2604
2605template <class _Tp, class _Compare, class _Allocator>
2606template <class _Key>
2607typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2608__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2609{
2610 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2611 if (__p != end() && !value_comp()(__v, *__p))
2612 return __p;
2613 return end();
2614}
2615
2616template <class _Tp, class _Compare, class _Allocator>
2617template <class _Key>
2618typename __tree<_Tp, _Compare, _Allocator>::size_type
2619__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2620{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002621 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 while (__rt != nullptr)
2623 {
2624 if (value_comp()(__k, __rt->__value_))
2625 {
Eric Fiselier227b47c2016-02-20 07:12:17 +00002626 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627 }
2628 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002629 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 else
2631 return 1;
2632 }
2633 return 0;
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_multi(const _Key& __k) const
2640{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002641 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002642 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002643 while (__rt != nullptr)
2644 {
2645 if (value_comp()(__k, __rt->__value_))
2646 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002647 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002648 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002649 }
2650 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002651 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002652 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002653 return _VSTD::distance(
Eric Fiselier7310ec82016-07-19 17:56:20 +00002654 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002655 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 );
2657 }
2658 return 0;
2659}
2660
2661template <class _Tp, class _Compare, class _Allocator>
2662template <class _Key>
2663typename __tree<_Tp, _Compare, _Allocator>::iterator
2664__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2665 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002666 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002667{
2668 while (__root != nullptr)
2669 {
2670 if (!value_comp()(__root->__value_, __v))
2671 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002672 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002673 __root = static_cast<__node_pointer>(__root->__left_);
2674 }
2675 else
2676 __root = static_cast<__node_pointer>(__root->__right_);
2677 }
2678 return iterator(__result);
2679}
2680
2681template <class _Tp, class _Compare, class _Allocator>
2682template <class _Key>
2683typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2684__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002685 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002686 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002687{
2688 while (__root != nullptr)
2689 {
2690 if (!value_comp()(__root->__value_, __v))
2691 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002692 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002693 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002694 }
2695 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002696 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002697 }
2698 return const_iterator(__result);
2699}
2700
2701template <class _Tp, class _Compare, class _Allocator>
2702template <class _Key>
2703typename __tree<_Tp, _Compare, _Allocator>::iterator
2704__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2705 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002706 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002707{
2708 while (__root != nullptr)
2709 {
2710 if (value_comp()(__v, __root->__value_))
2711 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002712 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 __root = static_cast<__node_pointer>(__root->__left_);
2714 }
2715 else
2716 __root = static_cast<__node_pointer>(__root->__right_);
2717 }
2718 return iterator(__result);
2719}
2720
2721template <class _Tp, class _Compare, class _Allocator>
2722template <class _Key>
2723typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2724__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002725 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002726 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002727{
2728 while (__root != nullptr)
2729 {
2730 if (value_comp()(__v, __root->__value_))
2731 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002732 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002733 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002734 }
2735 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002736 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002737 }
2738 return const_iterator(__result);
2739}
2740
2741template <class _Tp, class _Compare, class _Allocator>
2742template <class _Key>
2743pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2744 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2745__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2746{
Howard Hinnant99968442011-11-29 18:15:50 +00002747 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002748 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002749 __node_pointer __rt = __root();
2750 while (__rt != nullptr)
2751 {
2752 if (value_comp()(__k, __rt->__value_))
2753 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002754 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002755 __rt = static_cast<__node_pointer>(__rt->__left_);
2756 }
2757 else if (value_comp()(__rt->__value_, __k))
2758 __rt = static_cast<__node_pointer>(__rt->__right_);
2759 else
Howard Hinnant99968442011-11-29 18:15:50 +00002760 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002761 iterator(
2762 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002763 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002764 : __result));
2765 }
Howard Hinnant99968442011-11-29 18:15:50 +00002766 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002767}
2768
2769template <class _Tp, class _Compare, class _Allocator>
2770template <class _Key>
2771pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2772 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2773__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2774{
Howard Hinnant99968442011-11-29 18:15:50 +00002775 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002776 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002777 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002778 while (__rt != nullptr)
2779 {
2780 if (value_comp()(__k, __rt->__value_))
2781 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002782 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002783 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784 }
2785 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002786 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002787 else
Howard Hinnant99968442011-11-29 18:15:50 +00002788 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002789 const_iterator(
2790 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002791 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002792 : __result));
2793 }
Howard Hinnant99968442011-11-29 18:15:50 +00002794 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795}
2796
2797template <class _Tp, class _Compare, class _Allocator>
2798template <class _Key>
2799pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2800 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2801__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2802{
Howard Hinnant99968442011-11-29 18:15:50 +00002803 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002804 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002805 __node_pointer __rt = __root();
2806 while (__rt != nullptr)
2807 {
2808 if (value_comp()(__k, __rt->__value_))
2809 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002810 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002811 __rt = static_cast<__node_pointer>(__rt->__left_);
2812 }
2813 else if (value_comp()(__rt->__value_, __k))
2814 __rt = static_cast<__node_pointer>(__rt->__right_);
2815 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002816 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2818 }
Howard Hinnant99968442011-11-29 18:15:50 +00002819 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820}
2821
2822template <class _Tp, class _Compare, class _Allocator>
2823template <class _Key>
2824pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2825 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2826__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2827{
Howard Hinnant99968442011-11-29 18:15:50 +00002828 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002829 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002830 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002831 while (__rt != nullptr)
2832 {
2833 if (value_comp()(__k, __rt->__value_))
2834 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002835 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002836 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002837 }
2838 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002839 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002840 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002841 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002842 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843 }
Howard Hinnant99968442011-11-29 18:15:50 +00002844 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002845}
2846
2847template <class _Tp, class _Compare, class _Allocator>
2848typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002849__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002850{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002851 __node_pointer __np = __p.__get_np();
2852 if (__begin_node() == __p.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002853 {
2854 if (__np->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002855 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002856 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002857 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002858 }
2859 --size();
2860 __tree_remove(__end_node()->__left_,
2861 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002862 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002863}
2864
Howard Hinnant7686add2011-06-04 14:31:57 +00002865template <class _Tp, class _Compare, class _Allocator>
2866inline _LIBCPP_INLINE_VISIBILITY
2867void
2868swap(__tree<_Tp, _Compare, _Allocator>& __x,
2869 __tree<_Tp, _Compare, _Allocator>& __y)
2870 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2871{
2872 __x.swap(__y);
2873}
2874
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002875_LIBCPP_END_NAMESPACE_STD
2876
Eric Fiselier018a3d52017-05-31 22:07:49 +00002877_LIBCPP_POP_MACROS
2878
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002879#endif // _LIBCPP___TREE