blob: bfcec78d4c9b1d951c9c3e61506628ae6da9cbb4 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000022#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
Howard Hinnant944510a2011-06-14 19:58:17 +000026template <class _Tp, class _Compare, class _Allocator> class __tree;
27template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +000028 class _LIBCPP_TYPE_VIS_ONLY __tree_iterator;
Howard Hinnant944510a2011-06-14 19:58:17 +000029template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +000030 class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +000031
Eric Fiseliera00b4842016-02-20 05:28:30 +000032template <class _Pointer> class __tree_end_node;
33template <class _VoidPtr> class __tree_node_base;
34template <class _Tp, class _VoidPtr> class __tree_node;
35
36#ifndef _LIBCPP_CXX03_LANG
37template <class _Key, class _Value>
38union __value_type;
39#else
40template <class _Key, class _Value>
41struct __value_type;
42#endif
43
44template <class _Allocator> class __map_node_destructor;
45template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
46template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
47
Howard Hinnantc51e1022010-05-11 19:42:16 +000048/*
49
50_NodePtr algorithms
51
52The algorithms taking _NodePtr are red black tree algorithms. Those
53algorithms taking a parameter named __root should assume that __root
54points to a proper red black tree (unless otherwise specified).
55
56Each algorithm herein assumes that __root->__parent_ points to a non-null
57structure which has a member __left_ which points back to __root. No other
58member is read or written to at __root->__parent_.
59
60__root->__parent_ will be referred to below (in comments only) as end_node.
61end_node->__left_ is an externably accessible lvalue for __root, and can be
62changed by node insertion and removal (without explicit reference to end_node).
63
64All nodes (with the exception of end_node), even the node referred to as
65__root, have a non-null __parent_ field.
66
67*/
68
69// Returns: true if __x is a left child of its parent, else false
70// Precondition: __x != nullptr.
71template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +000072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000073bool
Howard Hinnant1113b5e2011-06-04 17:10:24 +000074__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +000075{
76 return __x == __x->__parent_->__left_;
77}
78
79// Determintes if the subtree rooted at __x is a proper red black subtree. If
80// __x is a proper subtree, returns the black height (null counts as 1). If
81// __x is an improper subtree, returns 0.
82template <class _NodePtr>
83unsigned
84__tree_sub_invariant(_NodePtr __x)
85{
86 if (__x == nullptr)
87 return 1;
88 // parent consistency checked by caller
89 // check __x->__left_ consistency
90 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
91 return 0;
92 // check __x->__right_ consistency
93 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
94 return 0;
95 // check __x->__left_ != __x->__right_ unless both are nullptr
96 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
97 return 0;
98 // If this is red, neither child can be red
99 if (!__x->__is_black_)
100 {
101 if (__x->__left_ && !__x->__left_->__is_black_)
102 return 0;
103 if (__x->__right_ && !__x->__right_->__is_black_)
104 return 0;
105 }
106 unsigned __h = __tree_sub_invariant(__x->__left_);
107 if (__h == 0)
108 return 0; // invalid left subtree
109 if (__h != __tree_sub_invariant(__x->__right_))
110 return 0; // invalid or different height right subtree
111 return __h + __x->__is_black_; // return black height of this node
112}
113
114// Determintes if the red black tree rooted at __root is a proper red black tree.
115// __root == nullptr is a proper tree. Returns true is __root is a proper
116// red black tree, else returns false.
117template <class _NodePtr>
118bool
119__tree_invariant(_NodePtr __root)
120{
121 if (__root == nullptr)
122 return true;
123 // check __x->__parent_ consistency
124 if (__root->__parent_ == nullptr)
125 return false;
126 if (!__tree_is_left_child(__root))
127 return false;
128 // root must be black
129 if (!__root->__is_black_)
130 return false;
131 // do normal node checks
132 return __tree_sub_invariant(__root) != 0;
133}
134
135// Returns: pointer to the left-most node under __x.
136// Precondition: __x != nullptr.
137template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000139_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000140__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141{
142 while (__x->__left_ != nullptr)
143 __x = __x->__left_;
144 return __x;
145}
146
147// Returns: pointer to the right-most node under __x.
148// Precondition: __x != nullptr.
149template <class _NodePtr>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000151_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000152__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153{
154 while (__x->__right_ != nullptr)
155 __x = __x->__right_;
156 return __x;
157}
158
159// Returns: pointer to the next in-order node after __x.
160// Precondition: __x != nullptr.
161template <class _NodePtr>
162_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000163__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164{
165 if (__x->__right_ != nullptr)
166 return __tree_min(__x->__right_);
167 while (!__tree_is_left_child(__x))
Eric Fiselier70f5f872016-07-19 17:56:20 +0000168 __x = __x->__parent_unsafe();
169 return __x->__parent_unsafe();
170}
171
172template <class _EndNodePtr, class _NodePtr>
173inline _LIBCPP_INLINE_VISIBILITY
174_EndNodePtr
175__tree_next_iter(_NodePtr __x) _NOEXCEPT
176{
177 if (__x->__right_ != nullptr)
178 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
179 while (!__tree_is_left_child(__x))
180 __x = __x->__parent_unsafe();
181 return static_cast<_EndNodePtr>(__x->__parent_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182}
183
184// Returns: pointer to the previous in-order node before __x.
185// Precondition: __x != nullptr.
Eric Fiselier70f5f872016-07-19 17:56:20 +0000186// Note: __x may be the end node.
187template <class _NodePtr, class _EndNodePtr>
188inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189_NodePtr
Eric Fiselier70f5f872016-07-19 17:56:20 +0000190__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191{
192 if (__x->__left_ != nullptr)
193 return __tree_max(__x->__left_);
Eric Fiselier70f5f872016-07-19 17:56:20 +0000194 _NodePtr __xx = static_cast<_NodePtr>(__x);
195 while (__tree_is_left_child(__xx))
196 __xx = __xx->__parent_unsafe();
197 return __xx->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198}
199
200// Returns: pointer to a node which has no children
201// Precondition: __x != nullptr.
202template <class _NodePtr>
203_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000204__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205{
206 while (true)
207 {
208 if (__x->__left_ != nullptr)
209 {
210 __x = __x->__left_;
211 continue;
212 }
213 if (__x->__right_ != nullptr)
214 {
215 __x = __x->__right_;
216 continue;
217 }
218 break;
219 }
220 return __x;
221}
222
223// Effects: Makes __x->__right_ the subtree root with __x as its left child
224// while preserving in-order order.
225// Precondition: __x->__right_ != nullptr
226template <class _NodePtr>
227void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000228__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229{
230 _NodePtr __y = __x->__right_;
231 __x->__right_ = __y->__left_;
232 if (__x->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000233 __x->__right_->__set_parent(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 __y->__parent_ = __x->__parent_;
235 if (__tree_is_left_child(__x))
236 __x->__parent_->__left_ = __y;
237 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000238 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000239 __y->__left_ = __x;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000240 __x->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241}
242
243// Effects: Makes __x->__left_ the subtree root with __x as its right child
244// while preserving in-order order.
245// Precondition: __x->__left_ != nullptr
246template <class _NodePtr>
247void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000248__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249{
250 _NodePtr __y = __x->__left_;
251 __x->__left_ = __y->__right_;
252 if (__x->__left_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000253 __x->__left_->__set_parent(__x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 __y->__parent_ = __x->__parent_;
255 if (__tree_is_left_child(__x))
256 __x->__parent_->__left_ = __y;
257 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000258 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 __y->__right_ = __x;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000260 __x->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261}
262
263// Effects: Rebalances __root after attaching __x to a leaf.
264// Precondition: __root != nulptr && __x != nullptr.
265// __x has no children.
266// __x == __root or == a direct or indirect child of __root.
267// If __x were to be unlinked from __root (setting __root to
268// nullptr if __root == __x), __tree_invariant(__root) == true.
269// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
270// may be different than the value passed in as __root.
271template <class _NodePtr>
272void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000273__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274{
275 __x->__is_black_ = __x == __root;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000276 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277 {
278 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier70f5f872016-07-19 17:56:20 +0000279 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000281 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000282 if (__y != nullptr && !__y->__is_black_)
283 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000284 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000286 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000287 __x->__is_black_ = __x == __root;
288 __y->__is_black_ = true;
289 }
290 else
291 {
292 if (!__tree_is_left_child(__x))
293 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000294 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295 __tree_left_rotate(__x);
296 }
Eric Fiselier70f5f872016-07-19 17:56:20 +0000297 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000299 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 __x->__is_black_ = false;
301 __tree_right_rotate(__x);
302 break;
303 }
304 }
305 else
306 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000307 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308 if (__y != nullptr && !__y->__is_black_)
309 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000310 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000312 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313 __x->__is_black_ = __x == __root;
314 __y->__is_black_ = true;
315 }
316 else
317 {
318 if (__tree_is_left_child(__x))
319 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000320 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000321 __tree_right_rotate(__x);
322 }
Eric Fiselier70f5f872016-07-19 17:56:20 +0000323 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 __x->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000325 __x = __x->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000326 __x->__is_black_ = false;
327 __tree_left_rotate(__x);
328 break;
329 }
330 }
331 }
332}
333
334// Precondition: __root != nullptr && __z != nullptr.
335// __tree_invariant(__root) == true.
336// __z == __root or == a direct or indirect child of __root.
337// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
338// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
339// nor any of its children refer to __z. end_node->__left_
340// may be different than the value passed in as __root.
341template <class _NodePtr>
342void
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000343__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000344{
345 // __z will be removed from the tree. Client still needs to destruct/deallocate it
346 // __y is either __z, or if __z has two children, __tree_next(__z).
347 // __y will have at most one child.
348 // __y will be the initial hole in the tree (make the hole at a leaf)
349 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
350 __z : __tree_next(__z);
351 // __x is __y's possibly null single child
352 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
353 // __w is __x's possibly null uncle (will become __x's sibling)
354 _NodePtr __w = nullptr;
355 // link __x to __y's parent, and find __w
356 if (__x != nullptr)
357 __x->__parent_ = __y->__parent_;
358 if (__tree_is_left_child(__y))
359 {
360 __y->__parent_->__left_ = __x;
361 if (__y != __root)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000362 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363 else
364 __root = __x; // __w == nullptr
365 }
366 else
367 {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000368 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369 // __y can't be root if it is a right child
370 __w = __y->__parent_->__left_;
371 }
372 bool __removed_black = __y->__is_black_;
373 // If we didn't remove __z, do so now by splicing in __y for __z,
374 // but copy __z's color. This does not impact __x or __w.
375 if (__y != __z)
376 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000377 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000378 __y->__parent_ = __z->__parent_;
379 if (__tree_is_left_child(__z))
380 __y->__parent_->__left_ = __y;
381 else
Eric Fiselier70f5f872016-07-19 17:56:20 +0000382 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 __y->__left_ = __z->__left_;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000384 __y->__left_->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385 __y->__right_ = __z->__right_;
386 if (__y->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +0000387 __y->__right_->__set_parent(__y);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000388 __y->__is_black_ = __z->__is_black_;
389 if (__root == __z)
390 __root = __y;
391 }
392 // There is no need to rebalance if we removed a red, or if we removed
393 // the last node.
394 if (__removed_black && __root != nullptr)
395 {
396 // Rebalance:
397 // __x has an implicit black color (transferred from the removed __y)
398 // associated with it, no matter what its color is.
399 // If __x is __root (in which case it can't be null), it is supposed
400 // to be black anyway, and if it is doubly black, then the double
401 // can just be ignored.
402 // If __x is red (in which case it can't be null), then it can absorb
403 // the implicit black just by setting its color to black.
404 // Since __y was black and only had one child (which __x points to), __x
405 // is either red with no children, else null, otherwise __y would have
406 // different black heights under left and right pointers.
407 // if (__x == __root || __x != nullptr && !__x->__is_black_)
408 if (__x != nullptr)
409 __x->__is_black_ = true;
410 else
411 {
412 // Else __x isn't root, and is "doubly black", even though it may
413 // be null. __w can not be null here, else the parent would
414 // see a black height >= 2 on the __x side and a black height
415 // of 1 on the __w side (__w must be a non-null black or a red
416 // with a non-null black child).
417 while (true)
418 {
419 if (!__tree_is_left_child(__w)) // if x is left child
420 {
421 if (!__w->__is_black_)
422 {
423 __w->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000424 __w->__parent_unsafe()->__is_black_ = false;
425 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426 // __x is still valid
427 // reset __root only if necessary
428 if (__root == __w->__left_)
429 __root = __w;
430 // reset sibling, and it still can't be null
431 __w = __w->__left_->__right_;
432 }
433 // __w->__is_black_ is now true, __w may have null children
434 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
435 (__w->__right_ == nullptr || __w->__right_->__is_black_))
436 {
437 __w->__is_black_ = false;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000438 __x = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 // __x can no longer be null
440 if (__x == __root || !__x->__is_black_)
441 {
442 __x->__is_black_ = true;
443 break;
444 }
445 // reset sibling, and it still can't be null
446 __w = __tree_is_left_child(__x) ?
Eric Fiselier70f5f872016-07-19 17:56:20 +0000447 __x->__parent_unsafe()->__right_ :
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000448 __x->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000449 // continue;
450 }
451 else // __w has a red child
452 {
453 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
454 {
455 // __w left child is non-null and red
456 __w->__left_->__is_black_ = true;
457 __w->__is_black_ = false;
458 __tree_right_rotate(__w);
459 // __w is known not to be root, so root hasn't changed
460 // reset sibling, and it still can't be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000461 __w = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000462 }
463 // __w has a right red child, left child may be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000464 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
465 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000466 __w->__right_->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000467 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000468 break;
469 }
470 }
471 else
472 {
473 if (!__w->__is_black_)
474 {
475 __w->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000476 __w->__parent_unsafe()->__is_black_ = false;
477 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478 // __x is still valid
479 // reset __root only if necessary
480 if (__root == __w->__right_)
481 __root = __w;
482 // reset sibling, and it still can't be null
483 __w = __w->__right_->__left_;
484 }
485 // __w->__is_black_ is now true, __w may have null children
486 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
487 (__w->__right_ == nullptr || __w->__right_->__is_black_))
488 {
489 __w->__is_black_ = false;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000490 __x = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491 // __x can no longer be null
492 if (!__x->__is_black_ || __x == __root)
493 {
494 __x->__is_black_ = true;
495 break;
496 }
497 // reset sibling, and it still can't be null
498 __w = __tree_is_left_child(__x) ?
Eric Fiselier70f5f872016-07-19 17:56:20 +0000499 __x->__parent_unsafe()->__right_ :
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000500 __x->__parent_->__left_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000501 // continue;
502 }
503 else // __w has a red child
504 {
505 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
506 {
507 // __w right child is non-null and red
508 __w->__right_->__is_black_ = true;
509 __w->__is_black_ = false;
510 __tree_left_rotate(__w);
511 // __w is known not to be root, so root hasn't changed
512 // reset sibling, and it still can't be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000513 __w = __w->__parent_unsafe();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000514 }
515 // __w has a left red child, right child may be null
Eric Fiselier70f5f872016-07-19 17:56:20 +0000516 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
517 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518 __w->__left_->__is_black_ = true;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000519 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520 break;
521 }
522 }
523 }
524 }
525 }
526}
527
Eric Fiseliera00b4842016-02-20 05:28:30 +0000528// node traits
529
Eric Fiselierd06276b2016-03-31 02:15:15 +0000530
531#ifndef _LIBCPP_CXX03_LANG
532template <class _Tp>
533struct __is_tree_value_type_imp : false_type {};
534
535template <class _Key, class _Value>
536struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
537
538template <class ..._Args>
539struct __is_tree_value_type : false_type {};
540
541template <class _One>
542struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
543#endif
544
Eric Fiseliera00b4842016-02-20 05:28:30 +0000545template <class _Tp>
546struct __tree_key_value_types {
547 typedef _Tp key_type;
548 typedef _Tp __node_value_type;
549 typedef _Tp __container_value_type;
550 static const bool __is_map = false;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000551
552 _LIBCPP_INLINE_VISIBILITY
553 static key_type const& __get_key(_Tp const& __v) {
554 return __v;
555 }
556 _LIBCPP_INLINE_VISIBILITY
557 static __container_value_type const& __get_value(__node_value_type const& __v) {
558 return __v;
559 }
560 _LIBCPP_INLINE_VISIBILITY
561 static __container_value_type* __get_ptr(__node_value_type& __n) {
562 return _VSTD::addressof(__n);
563 }
564
565#ifndef _LIBCPP_CXX03_LANG
566 _LIBCPP_INLINE_VISIBILITY
567 static __container_value_type&& __move(__node_value_type& __v) {
568 return _VSTD::move(__v);
569 }
570#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000571};
572
573template <class _Key, class _Tp>
574struct __tree_key_value_types<__value_type<_Key, _Tp> > {
575 typedef _Key key_type;
576 typedef _Tp mapped_type;
577 typedef __value_type<_Key, _Tp> __node_value_type;
578 typedef pair<const _Key, _Tp> __container_value_type;
579 typedef pair<_Key, _Tp> __nc_value_type;
580 typedef __container_value_type __map_value_type;
581 static const bool __is_map = true;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000582
583 _LIBCPP_INLINE_VISIBILITY
584 static key_type const&
585 __get_key(__node_value_type const& __t) {
586 return __t.__cc.first;
587 }
588
589 template <class _Up>
590 _LIBCPP_INLINE_VISIBILITY
591 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
592 key_type const&>::type
593 __get_key(_Up& __t) {
594 return __t.first;
595 }
596
597 _LIBCPP_INLINE_VISIBILITY
598 static __container_value_type const&
599 __get_value(__node_value_type const& __t) {
600 return __t.__cc;
601 }
602
603 template <class _Up>
604 _LIBCPP_INLINE_VISIBILITY
605 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
606 __container_value_type const&>::type
607 __get_value(_Up& __t) {
608 return __t;
609 }
610
611 _LIBCPP_INLINE_VISIBILITY
612 static __container_value_type* __get_ptr(__node_value_type& __n) {
613 return _VSTD::addressof(__n.__cc);
614 }
615
616#ifndef _LIBCPP_CXX03_LANG
617 _LIBCPP_INLINE_VISIBILITY
618 static __nc_value_type&& __move(__node_value_type& __v) {
619 return _VSTD::move(__v.__nc);
620 }
621#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000622};
623
624template <class _VoidPtr>
625struct __tree_node_base_types {
626 typedef _VoidPtr __void_pointer;
627
628 typedef __tree_node_base<__void_pointer> __node_base_type;
629 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
630 __node_base_pointer;
631
632 typedef __tree_end_node<__node_base_pointer> __end_node_type;
633 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
634 __end_node_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000635#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
636 typedef __end_node_pointer __parent_pointer;
637#else
638 typedef typename conditional<
639 is_pointer<__end_node_pointer>::value,
640 __end_node_pointer,
641 __node_base_pointer>::type __parent_pointer;
642#endif
643
Eric Fiseliera00b4842016-02-20 05:28:30 +0000644private:
645 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
646 "_VoidPtr does not point to unqualified void type");
647};
648
649template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
650 bool = _KVTypes::__is_map>
651struct __tree_map_pointer_types {};
652
653template <class _Tp, class _AllocPtr, class _KVTypes>
654struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
655 typedef typename _KVTypes::__map_value_type _Mv;
656 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
657 __map_value_type_pointer;
658 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
659 __const_map_value_type_pointer;
660};
661
662template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
663struct __tree_node_types;
664
665template <class _NodePtr, class _Tp, class _VoidPtr>
666struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
667 : public __tree_node_base_types<_VoidPtr>,
668 __tree_key_value_types<_Tp>,
669 __tree_map_pointer_types<_Tp, _VoidPtr>
670{
671 typedef __tree_node_base_types<_VoidPtr> __base;
672 typedef __tree_key_value_types<_Tp> __key_base;
673 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
674public:
675
676 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
677 typedef _NodePtr __node_pointer;
678
679 typedef _Tp __node_value_type;
680 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
681 __node_value_type_pointer;
682 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
683 __const_node_value_type_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000684#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
685 typedef typename __base::__end_node_pointer __iter_pointer;
686#else
687 typedef typename conditional<
688 is_pointer<__node_pointer>::value,
689 typename __base::__end_node_pointer,
690 __node_pointer>::type __iter_pointer;
691#endif
Eric Fiseliera00b4842016-02-20 05:28:30 +0000692private:
693 static_assert(!is_const<__node_type>::value,
694 "_NodePtr should never be a pointer to const");
695 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
696 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
697};
698
699template <class _ValueTp, class _VoidPtr>
700struct __make_tree_node_types {
701 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
702 _NodePtr;
703 typedef __tree_node_types<_NodePtr> type;
704};
705
Howard Hinnantc51e1022010-05-11 19:42:16 +0000706// node
707
708template <class _Pointer>
709class __tree_end_node
710{
711public:
712 typedef _Pointer pointer;
713 pointer __left_;
714
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000716 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000717};
718
719template <class _VoidPtr>
720class __tree_node_base
Eric Fiseliera00b4842016-02-20 05:28:30 +0000721 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000723 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
724
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000726 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000727 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000728
Eric Fiselier70f5f872016-07-19 17:56:20 +0000729 pointer __right_;
730 __parent_pointer __parent_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 bool __is_black_;
732
Eric Fiselier70f5f872016-07-19 17:56:20 +0000733 _LIBCPP_INLINE_VISIBILITY
734 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
735
736 _LIBCPP_INLINE_VISIBILITY
737 void __set_parent(pointer __p) {
738 __parent_ = static_cast<__parent_pointer>(__p);
739 }
740
Eric Fiselierd06276b2016-03-31 02:15:15 +0000741private:
742 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
743 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
744 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745};
746
747template <class _Tp, class _VoidPtr>
748class __tree_node
749 : public __tree_node_base<_VoidPtr>
750{
751public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000752 typedef _Tp __node_value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753
Eric Fiseliera00b4842016-02-20 05:28:30 +0000754 __node_value_type __value_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000755
Eric Fiselierd06276b2016-03-31 02:15:15 +0000756private:
757 ~__tree_node() _LIBCPP_EQUAL_DELETE;
758 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
759 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000760};
761
Eric Fiselierd06276b2016-03-31 02:15:15 +0000762
763template <class _Allocator>
764class __tree_node_destructor
765{
766 typedef _Allocator allocator_type;
767 typedef allocator_traits<allocator_type> __alloc_traits;
768
769public:
770 typedef typename __alloc_traits::pointer pointer;
771private:
772 typedef __tree_node_types<pointer> _NodeTypes;
773 allocator_type& __na_;
774
775 __tree_node_destructor& operator=(const __tree_node_destructor&);
776
777public:
778 bool __value_constructed;
779
780 _LIBCPP_INLINE_VISIBILITY
781 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
782 : __na_(__na),
783 __value_constructed(__val)
784 {}
785
786 _LIBCPP_INLINE_VISIBILITY
787 void operator()(pointer __p) _NOEXCEPT
788 {
789 if (__value_constructed)
790 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
791 if (__p)
792 __alloc_traits::deallocate(__na_, __p, 1);
793 }
794
795 template <class> friend class __map_node_destructor;
796};
797
798
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000800class _LIBCPP_TYPE_VIS_ONLY __tree_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000802 typedef __tree_node_types<_NodePtr> _NodeTypes;
803 typedef _NodePtr __node_pointer;
804 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000805 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
806 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000807 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808
Eric Fiselier70f5f872016-07-19 17:56:20 +0000809 __iter_pointer __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810
Howard Hinnantc51e1022010-05-11 19:42:16 +0000811public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000812 typedef bidirectional_iterator_tag iterator_category;
813 typedef _Tp value_type;
814 typedef _DiffType difference_type;
815 typedef value_type& reference;
816 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817
Marshall Clow8fc07302013-08-08 21:52:50 +0000818 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
819#if _LIBCPP_STD_VER > 11
820 : __ptr_(nullptr)
821#endif
822 {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000823
Eric Fiselier70f5f872016-07-19 17:56:20 +0000824 _LIBCPP_INLINE_VISIBILITY reference operator*() const
825 {return __get_np()->__value_;}
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000826 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier70f5f872016-07-19 17:56:20 +0000827 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000829 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000830 __tree_iterator& operator++() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000831 __ptr_ = static_cast<__iter_pointer>(
832 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000833 return *this;
834 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836 __tree_iterator operator++(int)
837 {__tree_iterator __t(*this); ++(*this); return __t;}
838
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000839 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000840 __tree_iterator& operator--() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000841 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
842 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000843 return *this;
844 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846 __tree_iterator operator--(int)
847 {__tree_iterator __t(*this); --(*this); return __t;}
848
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000849 friend _LIBCPP_INLINE_VISIBILITY
850 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000852 friend _LIBCPP_INLINE_VISIBILITY
853 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 {return !(__x == __y);}
855
856private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000858 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier70f5f872016-07-19 17:56:20 +0000859 _LIBCPP_INLINE_VISIBILITY
860 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
861 _LIBCPP_INLINE_VISIBILITY
862 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863 template <class, class, class> friend class __tree;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000864 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
865 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
866 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
867 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
868 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
869 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870};
871
Eric Fiseliera00b4842016-02-20 05:28:30 +0000872template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000873class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874{
Eric Fiseliera00b4842016-02-20 05:28:30 +0000875 typedef __tree_node_types<_NodePtr> _NodeTypes;
876 typedef typename _NodeTypes::__node_pointer __node_pointer;
877 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000878 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
879 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000880 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881
Eric Fiselier70f5f872016-07-19 17:56:20 +0000882 __iter_pointer __ptr_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000883
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884public:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000885 typedef bidirectional_iterator_tag iterator_category;
886 typedef _Tp value_type;
887 typedef _DiffType difference_type;
888 typedef const value_type& reference;
889 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890
Marshall Clow8fc07302013-08-08 21:52:50 +0000891 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
892#if _LIBCPP_STD_VER > 11
893 : __ptr_(nullptr)
894#endif
895 {}
896
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897private:
Eric Fiseliera00b4842016-02-20 05:28:30 +0000898 typedef __tree_iterator<value_type, __node_pointer, difference_type>
899 __non_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000900public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000902 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
903 : __ptr_(__p.__ptr_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000904
Eric Fiselier70f5f872016-07-19 17:56:20 +0000905 _LIBCPP_INLINE_VISIBILITY reference operator*() const
906 {return __get_np()->__value_;}
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000907 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier70f5f872016-07-19 17:56:20 +0000908 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000910 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000911 __tree_const_iterator& operator++() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000912 __ptr_ = static_cast<__iter_pointer>(
913 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000914 return *this;
915 }
916
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000918 __tree_const_iterator operator++(int)
919 {__tree_const_iterator __t(*this); ++(*this); return __t;}
920
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000921 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000922 __tree_const_iterator& operator--() {
Eric Fiselier70f5f872016-07-19 17:56:20 +0000923 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
924 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier44f9fd02015-03-03 20:10:01 +0000925 return *this;
926 }
927
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000929 __tree_const_iterator operator--(int)
930 {__tree_const_iterator __t(*this); --(*this); return __t;}
931
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000932 friend _LIBCPP_INLINE_VISIBILITY
933 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000935 friend _LIBCPP_INLINE_VISIBILITY
936 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937 {return !(__x == __y);}
938
939private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000941 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
942 : __ptr_(__p) {}
Eric Fiselier70f5f872016-07-19 17:56:20 +0000943 _LIBCPP_INLINE_VISIBILITY
944 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
945 : __ptr_(__p) {}
946 _LIBCPP_INLINE_VISIBILITY
947 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
948
Howard Hinnantc51e1022010-05-11 19:42:16 +0000949 template <class, class, class> friend class __tree;
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000950 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
951 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
952 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
953 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
954 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Eric Fiselier70f5f872016-07-19 17:56:20 +0000955
Howard Hinnantc51e1022010-05-11 19:42:16 +0000956};
957
958template <class _Tp, class _Compare, class _Allocator>
959class __tree
960{
961public:
962 typedef _Tp value_type;
963 typedef _Compare value_compare;
964 typedef _Allocator allocator_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000965
966private:
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000968 typedef typename __make_tree_node_types<value_type,
969 typename __alloc_traits::void_pointer>::type
970 _NodeTypes;
Eric Fiselierd06276b2016-03-31 02:15:15 +0000971 typedef typename _NodeTypes::key_type key_type;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000972public:
973 typedef typename _NodeTypes::__node_value_type __node_value_type;
974 typedef typename _NodeTypes::__container_value_type __container_value_type;
975
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976 typedef typename __alloc_traits::pointer pointer;
977 typedef typename __alloc_traits::const_pointer const_pointer;
978 typedef typename __alloc_traits::size_type size_type;
979 typedef typename __alloc_traits::difference_type difference_type;
980
Eric Fiseliera00b4842016-02-20 05:28:30 +0000981public:
982 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant2d0046b2013-06-19 21:29:40 +0000983
Eric Fiseliera00b4842016-02-20 05:28:30 +0000984 typedef typename _NodeTypes::__node_type __node;
985 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000986
987 typedef typename _NodeTypes::__node_base_type __node_base;
988 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000989
990 typedef typename _NodeTypes::__end_node_type __end_node_t;
991 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000992
Eric Fiselier70f5f872016-07-19 17:56:20 +0000993 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
994 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
995
Marshall Clow940e01c2015-04-07 05:21:38 +0000996 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiseliera00b4842016-02-20 05:28:30 +0000997 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998
Eric Fiseliera00b4842016-02-20 05:28:30 +0000999private:
1000 // check for sane allocator pointer rebinding semantics. Rebinding the
1001 // allocator for a new pointer type should be exactly the same as rebinding
1002 // the pointer using 'pointer_traits'.
1003 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1004 "Allocator does not rebind pointers in a sane manner.");
1005 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1006 __node_base_allocator;
1007 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1008 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1009 "Allocator does not rebind pointers in a sane manner.");
1010
1011private:
Eric Fiselier70f5f872016-07-19 17:56:20 +00001012 __iter_pointer __begin_node_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1014 __compressed_pair<size_type, value_compare> __pair3_;
1015
1016public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001017 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001018 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001020 return static_cast<__iter_pointer>(
Eric Fiseliera92b0732016-02-20 07:12:17 +00001021 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1022 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001023 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001024 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001025 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001027 return static_cast<__iter_pointer>(
Eric Fiseliera92b0732016-02-20 07:12:17 +00001028 pointer_traits<__end_node_ptr>::pointer_to(
1029 const_cast<__end_node_t&>(__pair1_.first())
1030 )
1031 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001034 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001035private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001037 const __node_allocator& __node_alloc() const _NOEXCEPT
1038 {return __pair1_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001039 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001040 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001041 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier70f5f872016-07-19 17:56:20 +00001042 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001045 allocator_type __alloc() const _NOEXCEPT
1046 {return allocator_type(__node_alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001047private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001049 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001052 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001054 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001056 const value_compare& value_comp() const _NOEXCEPT
1057 {return __pair3_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058public:
Eric Fiseliera92b0732016-02-20 07:12:17 +00001059
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001060 _LIBCPP_INLINE_VISIBILITY
Eric Fiseliera92b0732016-02-20 07:12:17 +00001061 __node_pointer __root() const _NOEXCEPT
1062 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063
Eric Fiselier70f5f872016-07-19 17:56:20 +00001064 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1065 return _VSTD::addressof(__end_node()->__left_);
1066 }
1067
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001069 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001071 explicit __tree(const value_compare& __comp)
1072 _NOEXCEPT_(
1073 is_nothrow_default_constructible<__node_allocator>::value &&
1074 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075 explicit __tree(const allocator_type& __a);
1076 __tree(const value_compare& __comp, const allocator_type& __a);
1077 __tree(const __tree& __t);
1078 __tree& operator=(const __tree& __t);
1079 template <class _InputIterator>
1080 void __assign_unique(_InputIterator __first, _InputIterator __last);
1081 template <class _InputIterator>
1082 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant74279a52010-09-04 23:28:19 +00001083#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001084 __tree(__tree&& __t)
1085 _NOEXCEPT_(
1086 is_nothrow_move_constructible<__node_allocator>::value &&
1087 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001088 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001089 __tree& operator=(__tree&& __t)
1090 _NOEXCEPT_(
1091 __node_traits::propagate_on_container_move_assignment::value &&
1092 is_nothrow_move_assignable<value_compare>::value &&
1093 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +00001094#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001095
1096 ~__tree();
1097
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001099 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001101 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001103 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001105 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001106
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001108 size_type max_size() const _NOEXCEPT
1109 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001110
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001111 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001112
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001113 void swap(__tree& __t)
1114 _NOEXCEPT_(
Marshall Clow8982dcd2015-07-13 20:04:56 +00001115 __is_nothrow_swappable<value_compare>::value
1116#if _LIBCPP_STD_VER <= 11
1117 && (!__node_traits::propagate_on_container_swap::value ||
1118 __is_nothrow_swappable<__node_allocator>::value)
1119#endif
1120 );
Howard Hinnantc51e1022010-05-11 19:42:16 +00001121
Eric Fiselierd06276b2016-03-31 02:15:15 +00001122
1123#ifndef _LIBCPP_CXX03_LANG
1124 template <class _Key, class ..._Args>
1125 pair<iterator, bool>
1126 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1127 template <class _Key, class ..._Args>
1128 iterator
1129 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001130
1131 template <class... _Args>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001132 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierd06276b2016-03-31 02:15:15 +00001133
Howard Hinnantc51e1022010-05-11 19:42:16 +00001134 template <class... _Args>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001135 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001136
Eric Fiselierd06276b2016-03-31 02:15:15 +00001137 template <class... _Args>
1138 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001139
Eric Fiselierd06276b2016-03-31 02:15:15 +00001140 template <class... _Args>
1141 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001142
1143 template <class _Pp>
1144 _LIBCPP_INLINE_VISIBILITY
1145 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1146 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1147 __can_extract_key<_Pp, key_type>());
1148 }
1149
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001150 template <class _First, class _Second>
1151 _LIBCPP_INLINE_VISIBILITY
1152 typename enable_if<
1153 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1154 pair<iterator, bool>
1155 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1156 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1157 _VSTD::forward<_Second>(__s));
1158 }
1159
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001160 template <class... _Args>
1161 _LIBCPP_INLINE_VISIBILITY
1162 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1163 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1164 }
1165
1166 template <class _Pp>
1167 _LIBCPP_INLINE_VISIBILITY
1168 pair<iterator, bool>
1169 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1170 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1171 }
1172
1173 template <class _Pp>
1174 _LIBCPP_INLINE_VISIBILITY
1175 pair<iterator, bool>
1176 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1177 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1178 }
1179
1180 template <class _Pp>
1181 _LIBCPP_INLINE_VISIBILITY
1182 pair<iterator, bool>
1183 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1184 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1185 }
1186
1187 template <class _Pp>
1188 _LIBCPP_INLINE_VISIBILITY
1189 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1190 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1191 __can_extract_key<_Pp, key_type>());
1192 }
1193
Eric Fiselierf1e45ce2016-04-16 00:23:12 +00001194 template <class _First, class _Second>
1195 _LIBCPP_INLINE_VISIBILITY
1196 typename enable_if<
1197 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1198 iterator
1199 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1200 return __emplace_hint_unique_key_args(__p, __f,
1201 _VSTD::forward<_First>(__f),
1202 _VSTD::forward<_Second>(__s));
1203 }
1204
Eric Fiselier6cce51e2016-04-15 23:27:27 +00001205 template <class... _Args>
1206 _LIBCPP_INLINE_VISIBILITY
1207 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1208 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1209 }
1210
1211 template <class _Pp>
1212 _LIBCPP_INLINE_VISIBILITY
1213 iterator
1214 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1215 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1216 }
1217
1218 template <class _Pp>
1219 _LIBCPP_INLINE_VISIBILITY
1220 iterator
1221 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1222 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1223 }
1224
1225 template <class _Pp>
1226 _LIBCPP_INLINE_VISIBILITY
1227 iterator
1228 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1229 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1230 }
1231
Eric Fiselierd06276b2016-03-31 02:15:15 +00001232#else
1233 template <class _Key, class _Args>
1234 _LIBCPP_INLINE_VISIBILITY
1235 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1236 template <class _Key, class _Args>
1237 _LIBCPP_INLINE_VISIBILITY
1238 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clowd430d022016-01-05 19:32:41 +00001239#endif
1240
Eric Fiselierd06276b2016-03-31 02:15:15 +00001241 _LIBCPP_INLINE_VISIBILITY
1242 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1243 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1244 }
1245
1246 _LIBCPP_INLINE_VISIBILITY
1247 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1248 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1249 }
1250
1251#ifdef _LIBCPP_CXX03_LANG
1252 _LIBCPP_INLINE_VISIBILITY
1253 iterator __insert_multi(const __container_value_type& __v);
1254 _LIBCPP_INLINE_VISIBILITY
1255 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1256#else
1257 _LIBCPP_INLINE_VISIBILITY
1258 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1259 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1260 }
1261
1262 _LIBCPP_INLINE_VISIBILITY
1263 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1264 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1265 }
1266
1267 template <class _Vp, class = typename enable_if<
1268 !is_same<typename __unconstref<_Vp>::type,
1269 __container_value_type
1270 >::value
1271 >::type>
1272 _LIBCPP_INLINE_VISIBILITY
1273 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1274 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1275 }
1276
1277 template <class _Vp, class = typename enable_if<
1278 !is_same<typename __unconstref<_Vp>::type,
1279 __container_value_type
1280 >::value
1281 >::type>
1282 _LIBCPP_INLINE_VISIBILITY
1283 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1284 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1285 }
1286
1287 _LIBCPP_INLINE_VISIBILITY
1288 iterator __insert_multi(__container_value_type&& __v) {
1289 return __emplace_multi(_VSTD::move(__v));
1290 }
1291
1292 _LIBCPP_INLINE_VISIBILITY
1293 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1294 return __emplace_hint_multi(__p, _VSTD::move(__v));
1295 }
1296
1297 template <class _Vp>
1298 _LIBCPP_INLINE_VISIBILITY
1299 iterator __insert_multi(_Vp&& __v) {
1300 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1301 }
1302
1303 template <class _Vp>
1304 _LIBCPP_INLINE_VISIBILITY
1305 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1306 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1307 }
1308
1309#endif // !_LIBCPP_CXX03_LANG
1310
Howard Hinnantc51e1022010-05-11 19:42:16 +00001311 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1312 iterator __node_insert_unique(const_iterator __p,
1313 __node_pointer __nd);
1314
1315 iterator __node_insert_multi(__node_pointer __nd);
1316 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1317
1318 iterator erase(const_iterator __p);
1319 iterator erase(const_iterator __f, const_iterator __l);
1320 template <class _Key>
1321 size_type __erase_unique(const _Key& __k);
1322 template <class _Key>
1323 size_type __erase_multi(const _Key& __k);
1324
Eric Fiselier70f5f872016-07-19 17:56:20 +00001325 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001326 __node_base_pointer& __child,
1327 __node_base_pointer __new_node);
1328
1329 template <class _Key>
1330 iterator find(const _Key& __v);
1331 template <class _Key>
1332 const_iterator find(const _Key& __v) const;
1333
1334 template <class _Key>
1335 size_type __count_unique(const _Key& __k) const;
1336 template <class _Key>
1337 size_type __count_multi(const _Key& __k) const;
1338
1339 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001340 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001341 iterator lower_bound(const _Key& __v)
1342 {return __lower_bound(__v, __root(), __end_node());}
1343 template <class _Key>
1344 iterator __lower_bound(const _Key& __v,
1345 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001346 __iter_pointer __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001347 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001348 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001349 const_iterator lower_bound(const _Key& __v) const
1350 {return __lower_bound(__v, __root(), __end_node());}
1351 template <class _Key>
1352 const_iterator __lower_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00001353 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001354 __iter_pointer __result) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001355 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001357 iterator upper_bound(const _Key& __v)
1358 {return __upper_bound(__v, __root(), __end_node());}
1359 template <class _Key>
1360 iterator __upper_bound(const _Key& __v,
1361 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001362 __iter_pointer __result);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001363 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001365 const_iterator upper_bound(const _Key& __v) const
1366 {return __upper_bound(__v, __root(), __end_node());}
1367 template <class _Key>
1368 const_iterator __upper_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00001369 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001370 __iter_pointer __result) const;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001371 template <class _Key>
1372 pair<iterator, iterator>
1373 __equal_range_unique(const _Key& __k);
1374 template <class _Key>
1375 pair<const_iterator, const_iterator>
1376 __equal_range_unique(const _Key& __k) const;
1377
1378 template <class _Key>
1379 pair<iterator, iterator>
1380 __equal_range_multi(const _Key& __k);
1381 template <class _Key>
1382 pair<const_iterator, const_iterator>
1383 __equal_range_multi(const _Key& __k) const;
1384
Howard Hinnantc834c512011-11-29 18:15:50 +00001385 typedef __tree_node_destructor<__node_allocator> _Dp;
1386 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001387
Howard Hinnant1113b5e2011-06-04 17:10:24 +00001388 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001389private:
Eric Fiselier70f5f872016-07-19 17:56:20 +00001390 __node_base_pointer&
1391 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1392 __node_base_pointer&
1393 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1394 __node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001395 __find_leaf(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001396 __parent_pointer& __parent, const key_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001397 template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001398 __node_base_pointer&
1399 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001400 template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001401 __node_base_pointer&
1402 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1403 __node_base_pointer& __dummy,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001404 const _Key& __v);
1405
Eric Fiselierd06276b2016-03-31 02:15:15 +00001406#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001407 template <class ..._Args>
Eric Fiselierd06276b2016-03-31 02:15:15 +00001408 __node_holder __construct_node(_Args&& ...__args);
1409#else
1410 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001411#endif
1412
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001413 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001414
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001415 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001416 void __copy_assign_alloc(const __tree& __t)
1417 {__copy_assign_alloc(__t, integral_constant<bool,
1418 __node_traits::propagate_on_container_copy_assignment::value>());}
1419
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001420 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001421 void __copy_assign_alloc(const __tree& __t, true_type)
1422 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001423 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001424 void __copy_assign_alloc(const __tree& __t, false_type) {}
1425
1426 void __move_assign(__tree& __t, false_type);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001427 void __move_assign(__tree& __t, true_type)
1428 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1429 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001430
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001431 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001432 void __move_assign_alloc(__tree& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001433 _NOEXCEPT_(
1434 !__node_traits::propagate_on_container_move_assignment::value ||
1435 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001436 {__move_assign_alloc(__t, integral_constant<bool,
1437 __node_traits::propagate_on_container_move_assignment::value>());}
1438
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001439 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001440 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001441 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001442 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001443 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001444 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001445
Howard Hinnantc51e1022010-05-11 19:42:16 +00001446 __node_pointer __detach();
1447 static __node_pointer __detach(__node_pointer);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001448
Howard Hinnanta37d3cf2013-08-12 18:38:34 +00001449 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1450 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001451};
1452
1453template <class _Tp, class _Compare, class _Allocator>
1454__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001455 _NOEXCEPT_(
1456 is_nothrow_default_constructible<__node_allocator>::value &&
1457 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001458 : __pair3_(0, __comp)
1459{
1460 __begin_node() = __end_node();
1461}
1462
1463template <class _Tp, class _Compare, class _Allocator>
1464__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001465 : __begin_node_(__iter_pointer()),
Eric Fiselier9975e702015-07-18 23:56:04 +00001466 __pair1_(__node_allocator(__a)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001467 __pair3_(0)
1468{
1469 __begin_node() = __end_node();
1470}
1471
1472template <class _Tp, class _Compare, class _Allocator>
1473__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1474 const allocator_type& __a)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001475 : __begin_node_(__iter_pointer()),
Eric Fiselier9975e702015-07-18 23:56:04 +00001476 __pair1_(__node_allocator(__a)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001477 __pair3_(0, __comp)
1478{
1479 __begin_node() = __end_node();
1480}
1481
1482// Precondition: size() != 0
1483template <class _Tp, class _Compare, class _Allocator>
1484typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1485__tree<_Tp, _Compare, _Allocator>::__detach()
1486{
Eric Fiselier70f5f872016-07-19 17:56:20 +00001487 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001488 __begin_node() = __end_node();
1489 __end_node()->__left_->__parent_ = nullptr;
1490 __end_node()->__left_ = nullptr;
1491 size() = 0;
1492 // __cache->__left_ == nullptr
1493 if (__cache->__right_ != nullptr)
1494 __cache = static_cast<__node_pointer>(__cache->__right_);
1495 // __cache->__left_ == nullptr
1496 // __cache->__right_ == nullptr
1497 return __cache;
1498}
1499
1500// Precondition: __cache != nullptr
1501// __cache->left_ == nullptr
1502// __cache->right_ == nullptr
1503// This is no longer a red-black tree
1504template <class _Tp, class _Compare, class _Allocator>
1505typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1506__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1507{
1508 if (__cache->__parent_ == nullptr)
1509 return nullptr;
Howard Hinnant2d0046b2013-06-19 21:29:40 +00001510 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001511 {
1512 __cache->__parent_->__left_ = nullptr;
1513 __cache = static_cast<__node_pointer>(__cache->__parent_);
1514 if (__cache->__right_ == nullptr)
1515 return __cache;
1516 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1517 }
1518 // __cache is right child
Eric Fiselier70f5f872016-07-19 17:56:20 +00001519 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001520 __cache = static_cast<__node_pointer>(__cache->__parent_);
1521 if (__cache->__left_ == nullptr)
1522 return __cache;
1523 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1524}
1525
1526template <class _Tp, class _Compare, class _Allocator>
1527__tree<_Tp, _Compare, _Allocator>&
1528__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1529{
1530 if (this != &__t)
1531 {
1532 value_comp() = __t.value_comp();
1533 __copy_assign_alloc(__t);
1534 __assign_multi(__t.begin(), __t.end());
1535 }
1536 return *this;
1537}
1538
1539template <class _Tp, class _Compare, class _Allocator>
1540template <class _InputIterator>
1541void
1542__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1543{
Eric Fiselierd06276b2016-03-31 02:15:15 +00001544 typedef iterator_traits<_InputIterator> _ITraits;
1545 typedef typename _ITraits::value_type _ItValueType;
1546 static_assert((is_same<_ItValueType, __container_value_type>::value),
1547 "__assign_unique may only be called with the containers value type");
1548
Howard Hinnantc51e1022010-05-11 19:42:16 +00001549 if (size() != 0)
1550 {
1551 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001552#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001553 try
1554 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001555#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001556 for (; __cache != nullptr && __first != __last; ++__first)
1557 {
1558 __cache->__value_ = *__first;
1559 __node_pointer __next = __detach(__cache);
1560 __node_insert_unique(__cache);
1561 __cache = __next;
1562 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001563#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001564 }
1565 catch (...)
1566 {
1567 while (__cache->__parent_ != nullptr)
1568 __cache = static_cast<__node_pointer>(__cache->__parent_);
1569 destroy(__cache);
1570 throw;
1571 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001572#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001573 if (__cache != nullptr)
1574 {
1575 while (__cache->__parent_ != nullptr)
1576 __cache = static_cast<__node_pointer>(__cache->__parent_);
1577 destroy(__cache);
1578 }
1579 }
1580 for (; __first != __last; ++__first)
1581 __insert_unique(*__first);
1582}
1583
1584template <class _Tp, class _Compare, class _Allocator>
1585template <class _InputIterator>
1586void
1587__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1588{
Eric Fiselierd06276b2016-03-31 02:15:15 +00001589 typedef iterator_traits<_InputIterator> _ITraits;
1590 typedef typename _ITraits::value_type _ItValueType;
1591 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1592 is_same<_ItValueType, __node_value_type>::value),
1593 "__assign_multi may only be called with the containers value type"
1594 " or the nodes value type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001595 if (size() != 0)
1596 {
1597 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001598#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001599 try
1600 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001601#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001602 for (; __cache != nullptr && __first != __last; ++__first)
1603 {
1604 __cache->__value_ = *__first;
1605 __node_pointer __next = __detach(__cache);
1606 __node_insert_multi(__cache);
1607 __cache = __next;
1608 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001609#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001610 }
1611 catch (...)
1612 {
1613 while (__cache->__parent_ != nullptr)
1614 __cache = static_cast<__node_pointer>(__cache->__parent_);
1615 destroy(__cache);
1616 throw;
1617 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001618#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001619 if (__cache != nullptr)
1620 {
1621 while (__cache->__parent_ != nullptr)
1622 __cache = static_cast<__node_pointer>(__cache->__parent_);
1623 destroy(__cache);
1624 }
1625 }
1626 for (; __first != __last; ++__first)
Eric Fiselierd06276b2016-03-31 02:15:15 +00001627 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001628}
1629
1630template <class _Tp, class _Compare, class _Allocator>
1631__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier70f5f872016-07-19 17:56:20 +00001632 : __begin_node_(__iter_pointer()),
Howard Hinnantc51e1022010-05-11 19:42:16 +00001633 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1634 __pair3_(0, __t.value_comp())
1635{
1636 __begin_node() = __end_node();
1637}
1638
Howard Hinnant74279a52010-09-04 23:28:19 +00001639#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001640
1641template <class _Tp, class _Compare, class _Allocator>
1642__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001643 _NOEXCEPT_(
1644 is_nothrow_move_constructible<__node_allocator>::value &&
1645 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001646 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1647 __pair1_(_VSTD::move(__t.__pair1_)),
1648 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001649{
1650 if (size() == 0)
1651 __begin_node() = __end_node();
1652 else
1653 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001654 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001655 __t.__begin_node() = __t.__end_node();
1656 __t.__end_node()->__left_ = nullptr;
1657 __t.size() = 0;
1658 }
1659}
1660
1661template <class _Tp, class _Compare, class _Allocator>
1662__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1663 : __pair1_(__node_allocator(__a)),
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001664 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001665{
1666 if (__a == __t.__alloc())
1667 {
1668 if (__t.size() == 0)
1669 __begin_node() = __end_node();
1670 else
1671 {
1672 __begin_node() = __t.__begin_node();
1673 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier70f5f872016-07-19 17:56:20 +00001674 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001675 size() = __t.size();
1676 __t.__begin_node() = __t.__end_node();
1677 __t.__end_node()->__left_ = nullptr;
1678 __t.size() = 0;
1679 }
1680 }
1681 else
1682 {
1683 __begin_node() = __end_node();
1684 }
1685}
1686
1687template <class _Tp, class _Compare, class _Allocator>
1688void
1689__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001690 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1691 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001692{
1693 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1694 __begin_node_ = __t.__begin_node_;
1695 __pair1_.first() = __t.__pair1_.first();
1696 __move_assign_alloc(__t);
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001697 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001698 if (size() == 0)
1699 __begin_node() = __end_node();
1700 else
1701 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001702 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001703 __t.__begin_node() = __t.__end_node();
1704 __t.__end_node()->__left_ = nullptr;
1705 __t.size() = 0;
1706 }
1707}
1708
1709template <class _Tp, class _Compare, class _Allocator>
1710void
1711__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1712{
1713 if (__node_alloc() == __t.__node_alloc())
1714 __move_assign(__t, true_type());
1715 else
1716 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001717 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001718 const_iterator __e = end();
1719 if (size() != 0)
1720 {
1721 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001722#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001723 try
1724 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001725#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001726 while (__cache != nullptr && __t.size() != 0)
1727 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001728 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001729 __node_pointer __next = __detach(__cache);
1730 __node_insert_multi(__cache);
1731 __cache = __next;
1732 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001733#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001734 }
1735 catch (...)
1736 {
1737 while (__cache->__parent_ != nullptr)
1738 __cache = static_cast<__node_pointer>(__cache->__parent_);
1739 destroy(__cache);
1740 throw;
1741 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001742#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001743 if (__cache != nullptr)
1744 {
1745 while (__cache->__parent_ != nullptr)
1746 __cache = static_cast<__node_pointer>(__cache->__parent_);
1747 destroy(__cache);
1748 }
1749 }
1750 while (__t.size() != 0)
Eric Fiselierd06276b2016-03-31 02:15:15 +00001751 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001752 }
1753}
1754
1755template <class _Tp, class _Compare, class _Allocator>
1756__tree<_Tp, _Compare, _Allocator>&
1757__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001758 _NOEXCEPT_(
1759 __node_traits::propagate_on_container_move_assignment::value &&
1760 is_nothrow_move_assignable<value_compare>::value &&
1761 is_nothrow_move_assignable<__node_allocator>::value)
1762
Howard Hinnantc51e1022010-05-11 19:42:16 +00001763{
1764 __move_assign(__t, integral_constant<bool,
1765 __node_traits::propagate_on_container_move_assignment::value>());
1766 return *this;
1767}
1768
Howard Hinnant74279a52010-09-04 23:28:19 +00001769#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001770
1771template <class _Tp, class _Compare, class _Allocator>
1772__tree<_Tp, _Compare, _Allocator>::~__tree()
1773{
Marshall Clow140d9432016-06-30 22:05:45 +00001774 static_assert((is_copy_constructible<value_compare>::value),
1775 "Comparator must be copy-constructible.");
Howard Hinnantc51e1022010-05-11 19:42:16 +00001776 destroy(__root());
1777}
1778
1779template <class _Tp, class _Compare, class _Allocator>
1780void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001781__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001782{
1783 if (__nd != nullptr)
1784 {
1785 destroy(static_cast<__node_pointer>(__nd->__left_));
1786 destroy(static_cast<__node_pointer>(__nd->__right_));
1787 __node_allocator& __na = __node_alloc();
Eric Fiselierd06276b2016-03-31 02:15:15 +00001788 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001789 __node_traits::deallocate(__na, __nd, 1);
1790 }
1791}
1792
1793template <class _Tp, class _Compare, class _Allocator>
1794void
1795__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Marshall Clow8982dcd2015-07-13 20:04:56 +00001796 _NOEXCEPT_(
1797 __is_nothrow_swappable<value_compare>::value
1798#if _LIBCPP_STD_VER <= 11
1799 && (!__node_traits::propagate_on_container_swap::value ||
1800 __is_nothrow_swappable<__node_allocator>::value)
1801#endif
1802 )
Howard Hinnantc51e1022010-05-11 19:42:16 +00001803{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00001804 using _VSTD::swap;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001805 swap(__begin_node_, __t.__begin_node_);
1806 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow8982dcd2015-07-13 20:04:56 +00001807 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001808 __pair3_.swap(__t.__pair3_);
1809 if (size() == 0)
1810 __begin_node() = __end_node();
1811 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00001812 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001813 if (__t.size() == 0)
1814 __t.__begin_node() = __t.__end_node();
1815 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00001816 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001817}
1818
1819template <class _Tp, class _Compare, class _Allocator>
1820void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001821__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001822{
1823 destroy(__root());
1824 size() = 0;
1825 __begin_node() = __end_node();
1826 __end_node()->__left_ = nullptr;
1827}
1828
1829// Find lower_bound place to insert
1830// Set __parent to parent of null leaf
1831// Return reference to null leaf
1832template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001833typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1834__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001835 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001836{
1837 __node_pointer __nd = __root();
1838 if (__nd != nullptr)
1839 {
1840 while (true)
1841 {
1842 if (value_comp()(__nd->__value_, __v))
1843 {
1844 if (__nd->__right_ != nullptr)
1845 __nd = static_cast<__node_pointer>(__nd->__right_);
1846 else
1847 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001848 __parent = static_cast<__parent_pointer>(__nd);
1849 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001850 }
1851 }
1852 else
1853 {
1854 if (__nd->__left_ != nullptr)
1855 __nd = static_cast<__node_pointer>(__nd->__left_);
1856 else
1857 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001858 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001859 return __parent->__left_;
1860 }
1861 }
1862 }
1863 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001864 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001865 return __parent->__left_;
1866}
1867
1868// Find upper_bound place to insert
1869// Set __parent to parent of null leaf
1870// Return reference to null leaf
1871template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001872typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1873__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001874 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001875{
1876 __node_pointer __nd = __root();
1877 if (__nd != nullptr)
1878 {
1879 while (true)
1880 {
1881 if (value_comp()(__v, __nd->__value_))
1882 {
1883 if (__nd->__left_ != nullptr)
1884 __nd = static_cast<__node_pointer>(__nd->__left_);
1885 else
1886 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001887 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001888 return __parent->__left_;
1889 }
1890 }
1891 else
1892 {
1893 if (__nd->__right_ != nullptr)
1894 __nd = static_cast<__node_pointer>(__nd->__right_);
1895 else
1896 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001897 __parent = static_cast<__parent_pointer>(__nd);
1898 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001899 }
1900 }
1901 }
1902 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001903 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001904 return __parent->__left_;
1905}
1906
1907// Find leaf place to insert closest to __hint
1908// First check prior to __hint.
1909// Next check after __hint.
1910// Next do O(log N) search.
1911// Set __parent to parent of null leaf
1912// Return reference to null leaf
1913template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001914typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001915__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00001916 __parent_pointer& __parent,
Eric Fiselierd06276b2016-03-31 02:15:15 +00001917 const key_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001918{
1919 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1920 {
1921 // __v <= *__hint
1922 const_iterator __prior = __hint;
1923 if (__prior == begin() || !value_comp()(__v, *--__prior))
1924 {
1925 // *prev(__hint) <= __v <= *__hint
1926 if (__hint.__ptr_->__left_ == nullptr)
1927 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001928 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001929 return __parent->__left_;
1930 }
1931 else
1932 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001933 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1934 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001935 }
1936 }
1937 // __v < *prev(__hint)
1938 return __find_leaf_high(__parent, __v);
1939 }
1940 // else __v > *__hint
1941 return __find_leaf_low(__parent, __v);
1942}
1943
1944// Find place to insert if __v doesn't exist
1945// Set __parent to parent of null leaf
1946// Return reference to null leaf
1947// If __v exists, set parent to node of __v and return reference to node of __v
1948template <class _Tp, class _Compare, class _Allocator>
1949template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00001950typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1951__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001952 const _Key& __v)
1953{
1954 __node_pointer __nd = __root();
Eric Fiselier70f5f872016-07-19 17:56:20 +00001955 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantc51e1022010-05-11 19:42:16 +00001956 if (__nd != nullptr)
1957 {
1958 while (true)
1959 {
1960 if (value_comp()(__v, __nd->__value_))
1961 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001962 if (__nd->__left_ != nullptr) {
1963 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001964 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier70f5f872016-07-19 17:56:20 +00001965 } else {
1966 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001967 return __parent->__left_;
1968 }
1969 }
1970 else if (value_comp()(__nd->__value_, __v))
1971 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001972 if (__nd->__right_ != nullptr) {
1973 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001974 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier70f5f872016-07-19 17:56:20 +00001975 } else {
1976 __parent = static_cast<__parent_pointer>(__nd);
1977 return __nd->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001978 }
1979 }
1980 else
1981 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00001982 __parent = static_cast<__parent_pointer>(__nd);
1983 return *__nd_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001984 }
1985 }
1986 }
Eric Fiselier70f5f872016-07-19 17:56:20 +00001987 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001988 return __parent->__left_;
1989}
1990
1991// Find place to insert if __v doesn't exist
1992// First check prior to __hint.
1993// Next check after __hint.
1994// Next do O(log N) search.
1995// Set __parent to parent of null leaf
1996// Return reference to null leaf
1997// If __v exists, set parent to node of __v and return reference to node of __v
1998template <class _Tp, class _Compare, class _Allocator>
1999template <class _Key>
Eric Fiselier70f5f872016-07-19 17:56:20 +00002000typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00002001__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002002 __parent_pointer& __parent,
2003 __node_base_pointer& __dummy,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002004 const _Key& __v)
2005{
2006 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2007 {
2008 // __v < *__hint
2009 const_iterator __prior = __hint;
2010 if (__prior == begin() || value_comp()(*--__prior, __v))
2011 {
2012 // *prev(__hint) < __v < *__hint
2013 if (__hint.__ptr_->__left_ == nullptr)
2014 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002015 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002016 return __parent->__left_;
2017 }
2018 else
2019 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002020 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2021 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002022 }
2023 }
2024 // __v <= *prev(__hint)
2025 return __find_equal(__parent, __v);
2026 }
2027 else if (value_comp()(*__hint, __v)) // check after
2028 {
2029 // *__hint < __v
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002030 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002031 if (__next == end() || value_comp()(__v, *__next))
2032 {
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002033 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002034 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002035 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002036 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2037 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002038 }
2039 else
2040 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002041 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002042 return __parent->__left_;
2043 }
2044 }
2045 // *next(__hint) <= __v
2046 return __find_equal(__parent, __v);
2047 }
2048 // else __v == *__hint
Eric Fiselier70f5f872016-07-19 17:56:20 +00002049 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2050 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2051 return __dummy;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002052}
2053
2054template <class _Tp, class _Compare, class _Allocator>
2055void
Eric Fiselier70f5f872016-07-19 17:56:20 +00002056__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__parent_pointer __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00002057 __node_base_pointer& __child,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002058 __node_base_pointer __new_node)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002059{
2060 __new_node->__left_ = nullptr;
2061 __new_node->__right_ = nullptr;
2062 __new_node->__parent_ = __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002063 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantc51e1022010-05-11 19:42:16 +00002064 __child = __new_node;
2065 if (__begin_node()->__left_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002066 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002067 __tree_balance_after_insert(__end_node()->__left_, __child);
2068 ++size();
2069}
2070
Eric Fiselierd06276b2016-03-31 02:15:15 +00002071#ifndef _LIBCPP_CXX03_LANG
2072template <class _Tp, class _Compare, class _Allocator>
2073template <class _Key, class... _Args>
2074pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2075__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2076#else
2077template <class _Tp, class _Compare, class _Allocator>
2078template <class _Key, class _Args>
2079pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2080__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2081#endif
2082{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002083 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002084 __node_base_pointer& __child = __find_equal(__parent, __k);
2085 __node_pointer __r = static_cast<__node_pointer>(__child);
2086 bool __inserted = false;
2087 if (__child == nullptr)
2088 {
2089#ifndef _LIBCPP_CXX03_LANG
2090 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2091#else
2092 __node_holder __h = __construct_node(__args);
2093#endif
2094 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2095 __r = __h.release();
2096 __inserted = true;
2097 }
2098 return pair<iterator, bool>(iterator(__r), __inserted);
2099}
2100
2101
2102#ifndef _LIBCPP_CXX03_LANG
2103template <class _Tp, class _Compare, class _Allocator>
2104template <class _Key, class... _Args>
2105typename __tree<_Tp, _Compare, _Allocator>::iterator
2106__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2107 const_iterator __p, _Key const& __k, _Args&&... __args)
2108#else
2109template <class _Tp, class _Compare, class _Allocator>
2110template <class _Key, class _Args>
2111typename __tree<_Tp, _Compare, _Allocator>::iterator
2112__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2113 const_iterator __p, _Key const& __k, _Args& __args)
2114#endif
2115{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002116 __parent_pointer __parent;
2117 __node_base_pointer __dummy;
2118 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierd06276b2016-03-31 02:15:15 +00002119 __node_pointer __r = static_cast<__node_pointer>(__child);
2120 if (__child == nullptr)
2121 {
2122#ifndef _LIBCPP_CXX03_LANG
2123 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2124#else
2125 __node_holder __h = __construct_node(__args);
2126#endif
2127 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2128 __r = __h.release();
2129 }
2130 return iterator(__r);
2131}
2132
2133
2134#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002135
2136template <class _Tp, class _Compare, class _Allocator>
2137template <class ..._Args>
2138typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2139__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2140{
Eric Fiselierd06276b2016-03-31 02:15:15 +00002141 static_assert(!__is_tree_value_type<_Args...>::value,
2142 "Cannot construct from __value_type");
Howard Hinnantc51e1022010-05-11 19:42:16 +00002143 __node_allocator& __na = __node_alloc();
Howard Hinnantc834c512011-11-29 18:15:50 +00002144 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierd06276b2016-03-31 02:15:15 +00002145 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002146 __h.get_deleter().__value_constructed = true;
2147 return __h;
2148}
2149
Eric Fiselierd06276b2016-03-31 02:15:15 +00002150
Howard Hinnantc51e1022010-05-11 19:42:16 +00002151template <class _Tp, class _Compare, class _Allocator>
2152template <class... _Args>
2153pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier6cce51e2016-04-15 23:27:27 +00002154__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002155{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002156 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002157 __parent_pointer __parent;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002158 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2159 __node_pointer __r = static_cast<__node_pointer>(__child);
2160 bool __inserted = false;
2161 if (__child == nullptr)
2162 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002163 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002164 __r = __h.release();
2165 __inserted = true;
2166 }
2167 return pair<iterator, bool>(iterator(__r), __inserted);
2168}
2169
2170template <class _Tp, class _Compare, class _Allocator>
2171template <class... _Args>
2172typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier6cce51e2016-04-15 23:27:27 +00002173__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002174{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002175 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002176 __parent_pointer __parent;
2177 __node_base_pointer __dummy;
2178 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002179 __node_pointer __r = static_cast<__node_pointer>(__child);
2180 if (__child == nullptr)
2181 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002182 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002183 __r = __h.release();
2184 }
2185 return iterator(__r);
2186}
2187
2188template <class _Tp, class _Compare, class _Allocator>
2189template <class... _Args>
2190typename __tree<_Tp, _Compare, _Allocator>::iterator
2191__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2192{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002193 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002194 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002195 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002196 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002197 return iterator(static_cast<__node_pointer>(__h.release()));
2198}
2199
2200template <class _Tp, class _Compare, class _Allocator>
2201template <class... _Args>
2202typename __tree<_Tp, _Compare, _Allocator>::iterator
2203__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2204 _Args&&... __args)
2205{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002206 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier70f5f872016-07-19 17:56:20 +00002207 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002208 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002209 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002210 return iterator(static_cast<__node_pointer>(__h.release()));
2211}
2212
Howard Hinnant74279a52010-09-04 23:28:19 +00002213
Eric Fiselierd06276b2016-03-31 02:15:15 +00002214#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002215
2216template <class _Tp, class _Compare, class _Allocator>
2217typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierd06276b2016-03-31 02:15:15 +00002218__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002219{
2220 __node_allocator& __na = __node_alloc();
Howard Hinnantc834c512011-11-29 18:15:50 +00002221 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierd06276b2016-03-31 02:15:15 +00002222 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002223 __h.get_deleter().__value_constructed = true;
Dimitry Andric830fb602015-08-19 06:43:33 +00002224 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantc51e1022010-05-11 19:42:16 +00002225}
2226
Eric Fiselierd06276b2016-03-31 02:15:15 +00002227#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc5bc8672011-03-10 17:27:57 +00002228
Eric Fiselierd06276b2016-03-31 02:15:15 +00002229#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00002230template <class _Tp, class _Compare, class _Allocator>
2231typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierd06276b2016-03-31 02:15:15 +00002232__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002233{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002234 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002235 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002236 __node_holder __h = __construct_node(__v);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002237 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002238 return iterator(__h.release());
2239}
2240
2241template <class _Tp, class _Compare, class _Allocator>
2242typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierd06276b2016-03-31 02:15:15 +00002243__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002244{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002245 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002246 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002247 __node_holder __h = __construct_node(__v);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002248 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002249 return iterator(__h.release());
2250}
Eric Fiselierd06276b2016-03-31 02:15:15 +00002251#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00002252
Howard Hinnantc51e1022010-05-11 19:42:16 +00002253template <class _Tp, class _Compare, class _Allocator>
2254pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2255__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2256{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002257 __parent_pointer __parent;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2259 __node_pointer __r = static_cast<__node_pointer>(__child);
2260 bool __inserted = false;
2261 if (__child == nullptr)
2262 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002263 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002264 __r = __nd;
2265 __inserted = true;
2266 }
2267 return pair<iterator, bool>(iterator(__r), __inserted);
2268}
2269
2270template <class _Tp, class _Compare, class _Allocator>
2271typename __tree<_Tp, _Compare, _Allocator>::iterator
2272__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2273 __node_pointer __nd)
2274{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002275 __parent_pointer __parent;
2276 __node_base_pointer __dummy;
Howard Hinnantc51e1022010-05-11 19:42:16 +00002277 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2278 __node_pointer __r = static_cast<__node_pointer>(__child);
2279 if (__child == nullptr)
2280 {
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002281 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002282 __r = __nd;
2283 }
2284 return iterator(__r);
2285}
2286
2287template <class _Tp, class _Compare, class _Allocator>
2288typename __tree<_Tp, _Compare, _Allocator>::iterator
2289__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2290{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002291 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002292 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002293 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002294 return iterator(__nd);
2295}
2296
2297template <class _Tp, class _Compare, class _Allocator>
2298typename __tree<_Tp, _Compare, _Allocator>::iterator
2299__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2300 __node_pointer __nd)
2301{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002302 __parent_pointer __parent;
Eric Fiselierd06276b2016-03-31 02:15:15 +00002303 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002304 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002305 return iterator(__nd);
2306}
2307
2308template <class _Tp, class _Compare, class _Allocator>
2309typename __tree<_Tp, _Compare, _Allocator>::iterator
2310__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2311{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002312 __node_pointer __np = __p.__get_np();
2313 iterator __r(__p.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002314 ++__r;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002315 if (__begin_node() == __p.__ptr_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002316 __begin_node() = __r.__ptr_;
2317 --size();
2318 __node_allocator& __na = __node_alloc();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002319 __tree_remove(__end_node()->__left_,
2320 static_cast<__node_base_pointer>(__np));
Eric Fiselierd06276b2016-03-31 02:15:15 +00002321 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2322 const_cast<__node_value_type&>(*__p)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002323 __node_traits::deallocate(__na, __np, 1);
2324 return __r;
2325}
2326
2327template <class _Tp, class _Compare, class _Allocator>
2328typename __tree<_Tp, _Compare, _Allocator>::iterator
2329__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2330{
2331 while (__f != __l)
2332 __f = erase(__f);
Howard Hinnant2d0046b2013-06-19 21:29:40 +00002333 return iterator(__l.__ptr_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002334}
2335
2336template <class _Tp, class _Compare, class _Allocator>
2337template <class _Key>
2338typename __tree<_Tp, _Compare, _Allocator>::size_type
2339__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2340{
2341 iterator __i = find(__k);
2342 if (__i == end())
2343 return 0;
2344 erase(__i);
2345 return 1;
2346}
2347
2348template <class _Tp, class _Compare, class _Allocator>
2349template <class _Key>
2350typename __tree<_Tp, _Compare, _Allocator>::size_type
2351__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2352{
2353 pair<iterator, iterator> __p = __equal_range_multi(__k);
2354 size_type __r = 0;
2355 for (; __p.first != __p.second; ++__r)
2356 __p.first = erase(__p.first);
2357 return __r;
2358}
2359
2360template <class _Tp, class _Compare, class _Allocator>
2361template <class _Key>
2362typename __tree<_Tp, _Compare, _Allocator>::iterator
2363__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2364{
2365 iterator __p = __lower_bound(__v, __root(), __end_node());
2366 if (__p != end() && !value_comp()(__v, *__p))
2367 return __p;
2368 return end();
2369}
2370
2371template <class _Tp, class _Compare, class _Allocator>
2372template <class _Key>
2373typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2374__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2375{
2376 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2377 if (__p != end() && !value_comp()(__v, *__p))
2378 return __p;
2379 return end();
2380}
2381
2382template <class _Tp, class _Compare, class _Allocator>
2383template <class _Key>
2384typename __tree<_Tp, _Compare, _Allocator>::size_type
2385__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2386{
Eric Fiseliera92b0732016-02-20 07:12:17 +00002387 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002388 while (__rt != nullptr)
2389 {
2390 if (value_comp()(__k, __rt->__value_))
2391 {
Eric Fiseliera92b0732016-02-20 07:12:17 +00002392 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002393 }
2394 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002395 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002396 else
2397 return 1;
2398 }
2399 return 0;
2400}
2401
2402template <class _Tp, class _Compare, class _Allocator>
2403template <class _Key>
2404typename __tree<_Tp, _Compare, _Allocator>::size_type
2405__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2406{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002407 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002408 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002409 while (__rt != nullptr)
2410 {
2411 if (value_comp()(__k, __rt->__value_))
2412 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002413 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002414 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002415 }
2416 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002417 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002418 else
Howard Hinnantb1ad5a82011-06-30 21:18:19 +00002419 return _VSTD::distance(
Eric Fiselier70f5f872016-07-19 17:56:20 +00002420 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiseliera92b0732016-02-20 07:12:17 +00002421 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002422 );
2423 }
2424 return 0;
2425}
2426
2427template <class _Tp, class _Compare, class _Allocator>
2428template <class _Key>
2429typename __tree<_Tp, _Compare, _Allocator>::iterator
2430__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2431 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002432 __iter_pointer __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002433{
2434 while (__root != nullptr)
2435 {
2436 if (!value_comp()(__root->__value_, __v))
2437 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002438 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002439 __root = static_cast<__node_pointer>(__root->__left_);
2440 }
2441 else
2442 __root = static_cast<__node_pointer>(__root->__right_);
2443 }
2444 return iterator(__result);
2445}
2446
2447template <class _Tp, class _Compare, class _Allocator>
2448template <class _Key>
2449typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2450__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00002451 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002452 __iter_pointer __result) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002453{
2454 while (__root != nullptr)
2455 {
2456 if (!value_comp()(__root->__value_, __v))
2457 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002458 __result = static_cast<__iter_pointer>(__root);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002459 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002460 }
2461 else
Eric Fiseliera92b0732016-02-20 07:12:17 +00002462 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002463 }
2464 return const_iterator(__result);
2465}
2466
2467template <class _Tp, class _Compare, class _Allocator>
2468template <class _Key>
2469typename __tree<_Tp, _Compare, _Allocator>::iterator
2470__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2471 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002472 __iter_pointer __result)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002473{
2474 while (__root != nullptr)
2475 {
2476 if (value_comp()(__v, __root->__value_))
2477 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002478 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002479 __root = static_cast<__node_pointer>(__root->__left_);
2480 }
2481 else
2482 __root = static_cast<__node_pointer>(__root->__right_);
2483 }
2484 return iterator(__result);
2485}
2486
2487template <class _Tp, class _Compare, class _Allocator>
2488template <class _Key>
2489typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2490__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiseliera92b0732016-02-20 07:12:17 +00002491 __node_pointer __root,
Eric Fiselier70f5f872016-07-19 17:56:20 +00002492 __iter_pointer __result) const
Howard Hinnantc51e1022010-05-11 19:42:16 +00002493{
2494 while (__root != nullptr)
2495 {
2496 if (value_comp()(__v, __root->__value_))
2497 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002498 __result = static_cast<__iter_pointer>(__root);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002499 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002500 }
2501 else
Eric Fiseliera92b0732016-02-20 07:12:17 +00002502 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002503 }
2504 return const_iterator(__result);
2505}
2506
2507template <class _Tp, class _Compare, class _Allocator>
2508template <class _Key>
2509pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2510 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2511__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2512{
Howard Hinnantc834c512011-11-29 18:15:50 +00002513 typedef pair<iterator, iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002514 __iter_pointer __result = __end_node();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002515 __node_pointer __rt = __root();
2516 while (__rt != nullptr)
2517 {
2518 if (value_comp()(__k, __rt->__value_))
2519 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002520 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002521 __rt = static_cast<__node_pointer>(__rt->__left_);
2522 }
2523 else if (value_comp()(__rt->__value_, __k))
2524 __rt = static_cast<__node_pointer>(__rt->__right_);
2525 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002526 return _Pp(iterator(__rt),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002527 iterator(
2528 __rt->__right_ != nullptr ?
Eric Fiselier70f5f872016-07-19 17:56:20 +00002529 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002530 : __result));
2531 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002532 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002533}
2534
2535template <class _Tp, class _Compare, class _Allocator>
2536template <class _Key>
2537pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2538 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2539__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2540{
Howard Hinnantc834c512011-11-29 18:15:50 +00002541 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002542 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002543 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002544 while (__rt != nullptr)
2545 {
2546 if (value_comp()(__k, __rt->__value_))
2547 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002548 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002549 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002550 }
2551 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002552 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002553 else
Howard Hinnantc834c512011-11-29 18:15:50 +00002554 return _Pp(const_iterator(__rt),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002555 const_iterator(
2556 __rt->__right_ != nullptr ?
Eric Fiselier70f5f872016-07-19 17:56:20 +00002557 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantc51e1022010-05-11 19:42:16 +00002558 : __result));
2559 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002560 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002561}
2562
2563template <class _Tp, class _Compare, class _Allocator>
2564template <class _Key>
2565pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2566 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2567__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2568{
Howard Hinnantc834c512011-11-29 18:15:50 +00002569 typedef pair<iterator, iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002570 __iter_pointer __result = __end_node();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002571 __node_pointer __rt = __root();
2572 while (__rt != nullptr)
2573 {
2574 if (value_comp()(__k, __rt->__value_))
2575 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002576 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002577 __rt = static_cast<__node_pointer>(__rt->__left_);
2578 }
2579 else if (value_comp()(__rt->__value_, __k))
2580 __rt = static_cast<__node_pointer>(__rt->__right_);
2581 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002582 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantc51e1022010-05-11 19:42:16 +00002583 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2584 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002585 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002586}
2587
2588template <class _Tp, class _Compare, class _Allocator>
2589template <class _Key>
2590pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2591 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2592__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2593{
Howard Hinnantc834c512011-11-29 18:15:50 +00002594 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier70f5f872016-07-19 17:56:20 +00002595 __iter_pointer __result = __end_node();
Eric Fiseliera92b0732016-02-20 07:12:17 +00002596 __node_pointer __rt = __root();
Howard Hinnantc51e1022010-05-11 19:42:16 +00002597 while (__rt != nullptr)
2598 {
2599 if (value_comp()(__k, __rt->__value_))
2600 {
Eric Fiselier70f5f872016-07-19 17:56:20 +00002601 __result = static_cast<__iter_pointer>(__rt);
Eric Fiseliera92b0732016-02-20 07:12:17 +00002602 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002603 }
2604 else if (value_comp()(__rt->__value_, __k))
Eric Fiseliera92b0732016-02-20 07:12:17 +00002605 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002606 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002607 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiseliera92b0732016-02-20 07:12:17 +00002608 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002609 }
Howard Hinnantc834c512011-11-29 18:15:50 +00002610 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002611}
2612
2613template <class _Tp, class _Compare, class _Allocator>
2614typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant1113b5e2011-06-04 17:10:24 +00002615__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002616{
Eric Fiselier70f5f872016-07-19 17:56:20 +00002617 __node_pointer __np = __p.__get_np();
2618 if (__begin_node() == __p.__ptr_)
Howard Hinnantc51e1022010-05-11 19:42:16 +00002619 {
2620 if (__np->__right_ != nullptr)
Eric Fiselier70f5f872016-07-19 17:56:20 +00002621 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002622 else
Eric Fiselier70f5f872016-07-19 17:56:20 +00002623 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantc51e1022010-05-11 19:42:16 +00002624 }
2625 --size();
2626 __tree_remove(__end_node()->__left_,
2627 static_cast<__node_base_pointer>(__np));
Marshall Clow95af65e2015-01-28 19:54:25 +00002628 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantc51e1022010-05-11 19:42:16 +00002629}
2630
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00002631template <class _Tp, class _Compare, class _Allocator>
2632inline _LIBCPP_INLINE_VISIBILITY
2633void
2634swap(__tree<_Tp, _Compare, _Allocator>& __x,
2635 __tree<_Tp, _Compare, _Allocator>& __y)
2636 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2637{
2638 __x.swap(__y);
2639}
2640
Howard Hinnantc51e1022010-05-11 19:42:16 +00002641_LIBCPP_END_NAMESPACE_STD
2642
2643#endif // _LIBCPP___TREE