blob: dd32f70058662de0637b940f5d7a4652801872fe [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
Howard Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000026template <class _Tp, class _Compare, class _Allocator> class __tree;
27template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000028 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000029template <class _Tp, class _ConstNodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000030 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
Eric Fiselier55263482016-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;
Eric Fiselierc3589a82017-01-04 23:56:00 +000045template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
46template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier55263482016-02-20 05:28:30 +000047
Howard Hinnantbc8d3f92010-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 Hinnant333f50d2010-09-21 20:16:37 +000072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073bool
Howard Hinnant8b537682011-06-04 17:10:24 +000074__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Hinnant333f50d2010-09-21 20:16:37 +0000138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000140__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Hinnant333f50d2010-09-21 20:16:37 +0000150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000152__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Hinnant8b537682011-06-04 17:10:24 +0000163__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000182}
183
184// Returns: pointer to the previous in-order node before __x.
185// Precondition: __x != nullptr.
Eric Fiselier7310ec82016-07-19 17:56:20 +0000186// Note: __x may be the end node.
187template <class _NodePtr, class _EndNodePtr>
188inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189_NodePtr
Eric Fiselier7310ec82016-07-19 17:56:20 +0000190__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191{
192 if (__x->__left_ != nullptr)
193 return __tree_max(__x->__left_);
Eric Fiselier7310ec82016-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 Hinnantbc8d3f92010-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 Hinnant8b537682011-06-04 17:10:24 +0000204__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Hinnant8b537682011-06-04 17:10:24 +0000228__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229{
230 _NodePtr __y = __x->__right_;
231 __x->__right_ = __y->__left_;
232 if (__x->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000233 __x->__right_->__set_parent(__x);
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000238 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000239 __y->__left_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000240 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-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 Hinnant8b537682011-06-04 17:10:24 +0000248__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249{
250 _NodePtr __y = __x->__left_;
251 __x->__left_ = __y->__right_;
252 if (__x->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000253 __x->__left_->__set_parent(__x);
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000258 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259 __y->__right_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000260 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-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 Hinnant8b537682011-06-04 17:10:24 +0000273__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274{
275 __x->__is_black_ = __x == __root;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000276 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277 {
278 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier7310ec82016-07-19 17:56:20 +0000279 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000281 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282 if (__y != nullptr && !__y->__is_black_)
283 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000284 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000286 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000294 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295 __tree_left_rotate(__x);
296 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000297 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000299 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300 __x->__is_black_ = false;
301 __tree_right_rotate(__x);
302 break;
303 }
304 }
305 else
306 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000307 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 if (__y != nullptr && !__y->__is_black_)
309 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000310 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000312 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000320 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 __tree_right_rotate(__x);
322 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000323 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000324 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000325 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-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 Hinnant8b537682011-06-04 17:10:24 +0000343__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000362 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363 else
364 __root = __x; // __w == nullptr
365 }
366 else
367 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000368 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantbc8d3f92010-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 Hinnant324bb032010-08-22 00:02:43 +0000377 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000382 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000383 __y->__left_ = __z->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000384 __y->__left_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 __y->__right_ = __z->__right_;
386 if (__y->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000387 __y->__right_->__set_parent(__y);
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000424 __w->__parent_unsafe()->__is_black_ = false;
425 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000438 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000447 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000448 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000461 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000462 }
463 // __w has a right red child, left child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000464 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
465 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466 __w->__right_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000467 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468 break;
469 }
470 }
471 else
472 {
473 if (!__w->__is_black_)
474 {
475 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000476 __w->__parent_unsafe()->__is_black_ = false;
477 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000490 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000499 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000500 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-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 Fiselier7310ec82016-07-19 17:56:20 +0000513 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514 }
515 // __w has a left red child, right child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000516 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
517 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518 __w->__left_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000519 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 break;
521 }
522 }
523 }
524 }
525 }
526}
527
Eric Fiselier55263482016-02-20 05:28:30 +0000528// node traits
529
Eric Fiselierdb215062016-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 Fiselier55263482016-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 Fiselierdb215062016-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 Fiselier55263482016-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 Fiselierdb215062016-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 Fiselier55263482016-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 Fiselier7310ec82016-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 Fiselier55263482016-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 Fiselier7310ec82016-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 Fiselier55263482016-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 Hinnantbc8d3f92010-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 Hinnant333f50d2010-09-21 20:16:37 +0000715 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000716 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717};
718
719template <class _VoidPtr>
720class __tree_node_base
Eric Fiselier55263482016-02-20 05:28:30 +0000721 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722{
Eric Fiselier55263482016-02-20 05:28:30 +0000723 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
724
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725public:
Eric Fiselier55263482016-02-20 05:28:30 +0000726 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000727 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000728
Eric Fiselier7310ec82016-07-19 17:56:20 +0000729 pointer __right_;
730 __parent_pointer __parent_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000731 bool __is_black_;
732
Eric Fiselier7310ec82016-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 Fiselierdb215062016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000745};
746
747template <class _Tp, class _VoidPtr>
748class __tree_node
749 : public __tree_node_base<_VoidPtr>
750{
751public:
Eric Fiselier55263482016-02-20 05:28:30 +0000752 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753
Eric Fiselier55263482016-02-20 05:28:30 +0000754 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755
Eric Fiselierdb215062016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000760};
761
Eric Fiselierdb215062016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000799template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000800class _LIBCPP_TEMPLATE_VIS __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000801{
Eric Fiselier55263482016-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 Fiselier7310ec82016-07-19 17:56:20 +0000805 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
806 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000807 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000808
Eric Fiselier7310ec82016-07-19 17:56:20 +0000809 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811public:
Eric Fiselier55263482016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000817
Marshall Clow051c8482013-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000823
Eric Fiselier7310ec82016-07-19 17:56:20 +0000824 _LIBCPP_INLINE_VISIBILITY reference operator*() const
825 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000826 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000827 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
Howard Hinnant333f50d2010-09-21 20:16:37 +0000829 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000830 __tree_iterator& operator++() {
Eric Fiselier7310ec82016-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 Fiselier9c8e6632015-03-03 20:10:01 +0000833 return *this;
834 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836 __tree_iterator operator++(int)
837 {__tree_iterator __t(*this); ++(*this); return __t;}
838
Howard Hinnant333f50d2010-09-21 20:16:37 +0000839 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000840 __tree_iterator& operator--() {
Eric Fiselier7310ec82016-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 Fiselier9c8e6632015-03-03 20:10:01 +0000843 return *this;
844 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000846 __tree_iterator operator--(int)
847 {__tree_iterator __t(*this); --(*this); return __t;}
848
Howard Hinnant333f50d2010-09-21 20:16:37 +0000849 friend _LIBCPP_INLINE_VISIBILITY
850 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000852 friend _LIBCPP_INLINE_VISIBILITY
853 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854 {return !(__x == __y);}
855
856private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000858 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier7310ec82016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000863 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000864 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
865 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
866 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
867 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
868 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
869 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870};
871
Eric Fiselier55263482016-02-20 05:28:30 +0000872template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000873class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874{
Eric Fiselier55263482016-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 Fiselier7310ec82016-07-19 17:56:20 +0000878 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
879 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000880 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881
Eric Fiselier7310ec82016-07-19 17:56:20 +0000882 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884public:
Eric Fiselier55263482016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000890
Marshall Clow051c8482013-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000897private:
Eric Fiselier55263482016-02-20 05:28:30 +0000898 typedef __tree_iterator<value_type, __node_pointer, difference_type>
899 __non_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000901 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000902 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
903 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000904
Eric Fiselier7310ec82016-07-19 17:56:20 +0000905 _LIBCPP_INLINE_VISIBILITY reference operator*() const
906 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000907 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000908 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
Howard Hinnant333f50d2010-09-21 20:16:37 +0000910 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000911 __tree_const_iterator& operator++() {
Eric Fiselier7310ec82016-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 Fiselier9c8e6632015-03-03 20:10:01 +0000914 return *this;
915 }
916
Howard Hinnant333f50d2010-09-21 20:16:37 +0000917 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 __tree_const_iterator operator++(int)
919 {__tree_const_iterator __t(*this); ++(*this); return __t;}
920
Howard Hinnant333f50d2010-09-21 20:16:37 +0000921 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000922 __tree_const_iterator& operator--() {
Eric Fiselier7310ec82016-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 Fiselier9c8e6632015-03-03 20:10:01 +0000925 return *this;
926 }
927
Howard Hinnant333f50d2010-09-21 20:16:37 +0000928 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000929 __tree_const_iterator operator--(int)
930 {__tree_const_iterator __t(*this); --(*this); return __t;}
931
Howard Hinnant333f50d2010-09-21 20:16:37 +0000932 friend _LIBCPP_INLINE_VISIBILITY
933 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000934 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000935 friend _LIBCPP_INLINE_VISIBILITY
936 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 {return !(__x == __y);}
938
939private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000940 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000941 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
942 : __ptr_(__p) {}
Eric Fiselier7310ec82016-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 Hinnantbc8d3f92010-05-11 19:42:16 +0000949 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000950 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
951 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
952 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
953 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
954 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000955
Howard Hinnantbc8d3f92010-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 Fiselier55263482016-02-20 05:28:30 +0000965
966private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier55263482016-02-20 05:28:30 +0000968 typedef typename __make_tree_node_types<value_type,
969 typename __alloc_traits::void_pointer>::type
970 _NodeTypes;
Eric Fiselierdb215062016-03-31 02:15:15 +0000971 typedef typename _NodeTypes::key_type key_type;
Eric Fiselier55263482016-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 Hinnantbc8d3f92010-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 Fiselier55263482016-02-20 05:28:30 +0000981public:
982 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000983
Eric Fiselier55263482016-02-20 05:28:30 +0000984 typedef typename _NodeTypes::__node_type __node;
985 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiselier55263482016-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 Fiselier55263482016-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 Fiselier55263482016-02-20 05:28:30 +0000992
Eric Fiselier7310ec82016-07-19 17:56:20 +0000993 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
994 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
995
Marshall Clow66302c62015-04-07 05:21:38 +0000996 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiselier55263482016-02-20 05:28:30 +0000997 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998
Eric Fiselier55263482016-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 Fiselier7310ec82016-07-19 17:56:20 +00001012 __iter_pointer __begin_node_;
Howard Hinnantbc8d3f92010-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 Hinnant333f50d2010-09-21 20:16:37 +00001017 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001018 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001020 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001021 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1022 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001024 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001025 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001026 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001027 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001028 pointer_traits<__end_node_ptr>::pointer_to(
1029 const_cast<__end_node_t&>(__pair1_.first())
1030 )
1031 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001034 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001035private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001036 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001037 const __node_allocator& __node_alloc() const _NOEXCEPT
1038 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001039 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001040 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001041 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001042 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001045 allocator_type __alloc() const _NOEXCEPT
1046 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001048 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001049 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001051 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001052 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001054 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001055 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001056 const value_compare& value_comp() const _NOEXCEPT
1057 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058public:
Eric Fiselier227b47c2016-02-20 07:12:17 +00001059
Howard Hinnant333f50d2010-09-21 20:16:37 +00001060 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +00001061 __node_pointer __root() const _NOEXCEPT
1062 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063
Eric Fiselier7310ec82016-07-19 17:56:20 +00001064 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1065 return _VSTD::addressof(__end_node()->__left_);
1066 }
1067
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001068 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +00001069 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070
Howard Hinnant7686add2011-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 Hinnantbc8d3f92010-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 Hinnant73d21a42010-09-04 23:28:19 +00001083#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-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 Hinnantbc8d3f92010-05-11 19:42:16 +00001088 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-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 Hinnant73d21a42010-09-04 23:28:19 +00001094#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095
1096 ~__tree();
1097
Howard Hinnant333f50d2010-09-21 20:16:37 +00001098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001099 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001100 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001101 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001102 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001103 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001104 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001105 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106
Howard Hinnant333f50d2010-09-21 20:16:37 +00001107 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001108 size_type max_size() const _NOEXCEPT
Eric Fiselieref3060e2016-11-23 01:18:56 +00001109 {return std::min<size_type>(
1110 __node_traits::max_size(__node_alloc()),
1111 numeric_limits<difference_type >::max());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001112
Howard Hinnant7686add2011-06-04 14:31:57 +00001113 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114
Howard Hinnant7686add2011-06-04 14:31:57 +00001115 void swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001116#if _LIBCPP_STD_VER <= 11
Howard Hinnant7686add2011-06-04 14:31:57 +00001117 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001118 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001119 && (!__node_traits::propagate_on_container_swap::value ||
1120 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001121 );
Dimitry Andric1421cf02016-08-27 19:32:03 +00001122#else
1123 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1124#endif
Eric Fiselierdb215062016-03-31 02:15:15 +00001125
1126#ifndef _LIBCPP_CXX03_LANG
1127 template <class _Key, class ..._Args>
1128 pair<iterator, bool>
1129 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1130 template <class _Key, class ..._Args>
1131 iterator
1132 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133
1134 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001135 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierdb215062016-03-31 02:15:15 +00001136
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001137 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001138 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139
Eric Fiselierdb215062016-03-31 02:15:15 +00001140 template <class... _Args>
1141 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
Eric Fiselierdb215062016-03-31 02:15:15 +00001143 template <class... _Args>
1144 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001145
1146 template <class _Pp>
1147 _LIBCPP_INLINE_VISIBILITY
1148 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1149 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1150 __can_extract_key<_Pp, key_type>());
1151 }
1152
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001153 template <class _First, class _Second>
1154 _LIBCPP_INLINE_VISIBILITY
1155 typename enable_if<
1156 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1157 pair<iterator, bool>
1158 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1159 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1160 _VSTD::forward<_Second>(__s));
1161 }
1162
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001163 template <class... _Args>
1164 _LIBCPP_INLINE_VISIBILITY
1165 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1166 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1167 }
1168
1169 template <class _Pp>
1170 _LIBCPP_INLINE_VISIBILITY
1171 pair<iterator, bool>
1172 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1173 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1174 }
1175
1176 template <class _Pp>
1177 _LIBCPP_INLINE_VISIBILITY
1178 pair<iterator, bool>
1179 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1180 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1181 }
1182
1183 template <class _Pp>
1184 _LIBCPP_INLINE_VISIBILITY
1185 pair<iterator, bool>
1186 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1187 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1188 }
1189
1190 template <class _Pp>
1191 _LIBCPP_INLINE_VISIBILITY
1192 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1193 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1194 __can_extract_key<_Pp, key_type>());
1195 }
1196
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001197 template <class _First, class _Second>
1198 _LIBCPP_INLINE_VISIBILITY
1199 typename enable_if<
1200 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1201 iterator
1202 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1203 return __emplace_hint_unique_key_args(__p, __f,
1204 _VSTD::forward<_First>(__f),
1205 _VSTD::forward<_Second>(__s));
1206 }
1207
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001208 template <class... _Args>
1209 _LIBCPP_INLINE_VISIBILITY
1210 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1211 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1212 }
1213
1214 template <class _Pp>
1215 _LIBCPP_INLINE_VISIBILITY
1216 iterator
1217 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1218 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1219 }
1220
1221 template <class _Pp>
1222 _LIBCPP_INLINE_VISIBILITY
1223 iterator
1224 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1225 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1226 }
1227
1228 template <class _Pp>
1229 _LIBCPP_INLINE_VISIBILITY
1230 iterator
1231 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1232 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1233 }
1234
Eric Fiselierdb215062016-03-31 02:15:15 +00001235#else
1236 template <class _Key, class _Args>
1237 _LIBCPP_INLINE_VISIBILITY
1238 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1239 template <class _Key, class _Args>
1240 _LIBCPP_INLINE_VISIBILITY
1241 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001242#endif
1243
Eric Fiselierdb215062016-03-31 02:15:15 +00001244 _LIBCPP_INLINE_VISIBILITY
1245 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1246 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1247 }
1248
1249 _LIBCPP_INLINE_VISIBILITY
1250 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1251 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1252 }
1253
1254#ifdef _LIBCPP_CXX03_LANG
1255 _LIBCPP_INLINE_VISIBILITY
1256 iterator __insert_multi(const __container_value_type& __v);
1257 _LIBCPP_INLINE_VISIBILITY
1258 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1259#else
1260 _LIBCPP_INLINE_VISIBILITY
1261 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1262 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1263 }
1264
1265 _LIBCPP_INLINE_VISIBILITY
1266 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1267 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1268 }
1269
1270 template <class _Vp, class = typename enable_if<
1271 !is_same<typename __unconstref<_Vp>::type,
1272 __container_value_type
1273 >::value
1274 >::type>
1275 _LIBCPP_INLINE_VISIBILITY
1276 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1277 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1278 }
1279
1280 template <class _Vp, class = typename enable_if<
1281 !is_same<typename __unconstref<_Vp>::type,
1282 __container_value_type
1283 >::value
1284 >::type>
1285 _LIBCPP_INLINE_VISIBILITY
1286 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1287 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1288 }
1289
1290 _LIBCPP_INLINE_VISIBILITY
1291 iterator __insert_multi(__container_value_type&& __v) {
1292 return __emplace_multi(_VSTD::move(__v));
1293 }
1294
1295 _LIBCPP_INLINE_VISIBILITY
1296 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1297 return __emplace_hint_multi(__p, _VSTD::move(__v));
1298 }
1299
1300 template <class _Vp>
1301 _LIBCPP_INLINE_VISIBILITY
1302 iterator __insert_multi(_Vp&& __v) {
1303 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1304 }
1305
1306 template <class _Vp>
1307 _LIBCPP_INLINE_VISIBILITY
1308 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1309 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1310 }
1311
1312#endif // !_LIBCPP_CXX03_LANG
1313
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1315 iterator __node_insert_unique(const_iterator __p,
1316 __node_pointer __nd);
1317
1318 iterator __node_insert_multi(__node_pointer __nd);
1319 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1320
1321 iterator erase(const_iterator __p);
1322 iterator erase(const_iterator __f, const_iterator __l);
1323 template <class _Key>
1324 size_type __erase_unique(const _Key& __k);
1325 template <class _Key>
1326 size_type __erase_multi(const _Key& __k);
1327
Eric Fiselier7310ec82016-07-19 17:56:20 +00001328 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 __node_base_pointer& __child,
1330 __node_base_pointer __new_node);
1331
1332 template <class _Key>
1333 iterator find(const _Key& __v);
1334 template <class _Key>
1335 const_iterator find(const _Key& __v) const;
1336
1337 template <class _Key>
1338 size_type __count_unique(const _Key& __k) const;
1339 template <class _Key>
1340 size_type __count_multi(const _Key& __k) const;
1341
1342 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001343 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 iterator lower_bound(const _Key& __v)
1345 {return __lower_bound(__v, __root(), __end_node());}
1346 template <class _Key>
1347 iterator __lower_bound(const _Key& __v,
1348 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001349 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001350 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001351 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352 const_iterator lower_bound(const _Key& __v) const
1353 {return __lower_bound(__v, __root(), __end_node());}
1354 template <class _Key>
1355 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001356 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001357 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 iterator upper_bound(const _Key& __v)
1361 {return __upper_bound(__v, __root(), __end_node());}
1362 template <class _Key>
1363 iterator __upper_bound(const _Key& __v,
1364 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001365 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001367 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368 const_iterator upper_bound(const _Key& __v) const
1369 {return __upper_bound(__v, __root(), __end_node());}
1370 template <class _Key>
1371 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001372 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001373 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374 template <class _Key>
1375 pair<iterator, iterator>
1376 __equal_range_unique(const _Key& __k);
1377 template <class _Key>
1378 pair<const_iterator, const_iterator>
1379 __equal_range_unique(const _Key& __k) const;
1380
1381 template <class _Key>
1382 pair<iterator, iterator>
1383 __equal_range_multi(const _Key& __k);
1384 template <class _Key>
1385 pair<const_iterator, const_iterator>
1386 __equal_range_multi(const _Key& __k) const;
1387
Howard Hinnant99968442011-11-29 18:15:50 +00001388 typedef __tree_node_destructor<__node_allocator> _Dp;
1389 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390
Howard Hinnant8b537682011-06-04 17:10:24 +00001391 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001393 __node_base_pointer&
1394 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1395 __node_base_pointer&
1396 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1397 __node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001398 __find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001399 __parent_pointer& __parent, const key_type& __v);
Eric Fiselier856712b2017-01-05 06:06:18 +00001400 // FIXME: Make this function const qualified. Unfortunetly doing so
1401 // breaks existing code which uses non-const callable comparators.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001402 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001403 __node_base_pointer&
1404 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001405 template <class _Key>
Eric Fiselier856712b2017-01-05 06:06:18 +00001406 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1407 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1408 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1409 }
1410 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001411 __node_base_pointer&
1412 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1413 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414 const _Key& __v);
1415
Eric Fiselierdb215062016-03-31 02:15:15 +00001416#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001418 __node_holder __construct_node(_Args&& ...__args);
1419#else
1420 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421#endif
1422
Howard Hinnant7686add2011-06-04 14:31:57 +00001423 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424
Howard Hinnant333f50d2010-09-21 20:16:37 +00001425 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001426 void __copy_assign_alloc(const __tree& __t)
1427 {__copy_assign_alloc(__t, integral_constant<bool,
1428 __node_traits::propagate_on_container_copy_assignment::value>());}
1429
Howard Hinnant333f50d2010-09-21 20:16:37 +00001430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 void __copy_assign_alloc(const __tree& __t, true_type)
Marshall Clow546498c2016-08-17 23:24:02 +00001432 {
1433 if (__node_alloc() != __t.__node_alloc())
1434 clear();
1435 __node_alloc() = __t.__node_alloc();
1436 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001437 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001438 void __copy_assign_alloc(const __tree&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439
1440 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001441 void __move_assign(__tree& __t, true_type)
1442 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1443 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444
Howard Hinnant333f50d2010-09-21 20:16:37 +00001445 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001447 _NOEXCEPT_(
1448 !__node_traits::propagate_on_container_move_assignment::value ||
1449 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001450 {__move_assign_alloc(__t, integral_constant<bool,
1451 __node_traits::propagate_on_container_move_assignment::value>());}
1452
Howard Hinnant333f50d2010-09-21 20:16:37 +00001453 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001455 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001456 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001457 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001458 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001460 __node_pointer __detach();
1461 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001462
Eric Fiselierc3589a82017-01-04 23:56:00 +00001463 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1464 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001465};
1466
1467template <class _Tp, class _Compare, class _Allocator>
1468__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001469 _NOEXCEPT_(
1470 is_nothrow_default_constructible<__node_allocator>::value &&
1471 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472 : __pair3_(0, __comp)
1473{
1474 __begin_node() = __end_node();
1475}
1476
1477template <class _Tp, class _Compare, class _Allocator>
1478__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001479 : __begin_node_(__iter_pointer()),
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001480 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 __pair3_(0)
1482{
1483 __begin_node() = __end_node();
1484}
1485
1486template <class _Tp, class _Compare, class _Allocator>
1487__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1488 const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001489 : __begin_node_(__iter_pointer()),
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001490 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 __pair3_(0, __comp)
1492{
1493 __begin_node() = __end_node();
1494}
1495
1496// Precondition: size() != 0
1497template <class _Tp, class _Compare, class _Allocator>
1498typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1499__tree<_Tp, _Compare, _Allocator>::__detach()
1500{
Eric Fiselier7310ec82016-07-19 17:56:20 +00001501 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502 __begin_node() = __end_node();
1503 __end_node()->__left_->__parent_ = nullptr;
1504 __end_node()->__left_ = nullptr;
1505 size() = 0;
1506 // __cache->__left_ == nullptr
1507 if (__cache->__right_ != nullptr)
1508 __cache = static_cast<__node_pointer>(__cache->__right_);
1509 // __cache->__left_ == nullptr
1510 // __cache->__right_ == nullptr
1511 return __cache;
1512}
1513
1514// Precondition: __cache != nullptr
1515// __cache->left_ == nullptr
1516// __cache->right_ == nullptr
1517// This is no longer a red-black tree
1518template <class _Tp, class _Compare, class _Allocator>
1519typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1520__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1521{
1522 if (__cache->__parent_ == nullptr)
1523 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001524 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001525 {
1526 __cache->__parent_->__left_ = nullptr;
1527 __cache = static_cast<__node_pointer>(__cache->__parent_);
1528 if (__cache->__right_ == nullptr)
1529 return __cache;
1530 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1531 }
1532 // __cache is right child
Eric Fiselier7310ec82016-07-19 17:56:20 +00001533 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 __cache = static_cast<__node_pointer>(__cache->__parent_);
1535 if (__cache->__left_ == nullptr)
1536 return __cache;
1537 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1538}
1539
1540template <class _Tp, class _Compare, class _Allocator>
1541__tree<_Tp, _Compare, _Allocator>&
1542__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1543{
1544 if (this != &__t)
1545 {
1546 value_comp() = __t.value_comp();
1547 __copy_assign_alloc(__t);
1548 __assign_multi(__t.begin(), __t.end());
1549 }
1550 return *this;
1551}
1552
1553template <class _Tp, class _Compare, class _Allocator>
1554template <class _InputIterator>
1555void
1556__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1557{
Eric Fiselierdb215062016-03-31 02:15:15 +00001558 typedef iterator_traits<_InputIterator> _ITraits;
1559 typedef typename _ITraits::value_type _ItValueType;
1560 static_assert((is_same<_ItValueType, __container_value_type>::value),
1561 "__assign_unique may only be called with the containers value type");
1562
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 if (size() != 0)
1564 {
1565 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001566#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567 try
1568 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001569#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 for (; __cache != nullptr && __first != __last; ++__first)
1571 {
1572 __cache->__value_ = *__first;
1573 __node_pointer __next = __detach(__cache);
1574 __node_insert_unique(__cache);
1575 __cache = __next;
1576 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001577#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578 }
1579 catch (...)
1580 {
1581 while (__cache->__parent_ != nullptr)
1582 __cache = static_cast<__node_pointer>(__cache->__parent_);
1583 destroy(__cache);
1584 throw;
1585 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001586#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 if (__cache != nullptr)
1588 {
1589 while (__cache->__parent_ != nullptr)
1590 __cache = static_cast<__node_pointer>(__cache->__parent_);
1591 destroy(__cache);
1592 }
1593 }
1594 for (; __first != __last; ++__first)
1595 __insert_unique(*__first);
1596}
1597
1598template <class _Tp, class _Compare, class _Allocator>
1599template <class _InputIterator>
1600void
1601__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1602{
Eric Fiselierdb215062016-03-31 02:15:15 +00001603 typedef iterator_traits<_InputIterator> _ITraits;
1604 typedef typename _ITraits::value_type _ItValueType;
1605 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1606 is_same<_ItValueType, __node_value_type>::value),
1607 "__assign_multi may only be called with the containers value type"
1608 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001609 if (size() != 0)
1610 {
1611 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001612#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001613 try
1614 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001615#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 for (; __cache != nullptr && __first != __last; ++__first)
1617 {
1618 __cache->__value_ = *__first;
1619 __node_pointer __next = __detach(__cache);
1620 __node_insert_multi(__cache);
1621 __cache = __next;
1622 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001623#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 }
1625 catch (...)
1626 {
1627 while (__cache->__parent_ != nullptr)
1628 __cache = static_cast<__node_pointer>(__cache->__parent_);
1629 destroy(__cache);
1630 throw;
1631 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001632#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 if (__cache != nullptr)
1634 {
1635 while (__cache->__parent_ != nullptr)
1636 __cache = static_cast<__node_pointer>(__cache->__parent_);
1637 destroy(__cache);
1638 }
1639 }
1640 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001641 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642}
1643
1644template <class _Tp, class _Compare, class _Allocator>
1645__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001646 : __begin_node_(__iter_pointer()),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1648 __pair3_(0, __t.value_comp())
1649{
1650 __begin_node() = __end_node();
1651}
1652
Howard Hinnant73d21a42010-09-04 23:28:19 +00001653#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001654
1655template <class _Tp, class _Compare, class _Allocator>
1656__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001657 _NOEXCEPT_(
1658 is_nothrow_move_constructible<__node_allocator>::value &&
1659 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001660 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1661 __pair1_(_VSTD::move(__t.__pair1_)),
1662 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001663{
1664 if (size() == 0)
1665 __begin_node() = __end_node();
1666 else
1667 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001668 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669 __t.__begin_node() = __t.__end_node();
1670 __t.__end_node()->__left_ = nullptr;
1671 __t.size() = 0;
1672 }
1673}
1674
1675template <class _Tp, class _Compare, class _Allocator>
1676__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1677 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001678 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001679{
1680 if (__a == __t.__alloc())
1681 {
1682 if (__t.size() == 0)
1683 __begin_node() = __end_node();
1684 else
1685 {
1686 __begin_node() = __t.__begin_node();
1687 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +00001688 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001689 size() = __t.size();
1690 __t.__begin_node() = __t.__end_node();
1691 __t.__end_node()->__left_ = nullptr;
1692 __t.size() = 0;
1693 }
1694 }
1695 else
1696 {
1697 __begin_node() = __end_node();
1698 }
1699}
1700
1701template <class _Tp, class _Compare, class _Allocator>
1702void
1703__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001704 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1705 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706{
1707 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1708 __begin_node_ = __t.__begin_node_;
1709 __pair1_.first() = __t.__pair1_.first();
1710 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001711 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712 if (size() == 0)
1713 __begin_node() = __end_node();
1714 else
1715 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001716 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717 __t.__begin_node() = __t.__end_node();
1718 __t.__end_node()->__left_ = nullptr;
1719 __t.size() = 0;
1720 }
1721}
1722
1723template <class _Tp, class _Compare, class _Allocator>
1724void
1725__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1726{
1727 if (__node_alloc() == __t.__node_alloc())
1728 __move_assign(__t, true_type());
1729 else
1730 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001731 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732 const_iterator __e = end();
1733 if (size() != 0)
1734 {
1735 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001736#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001737 try
1738 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001739#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 while (__cache != nullptr && __t.size() != 0)
1741 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001742 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743 __node_pointer __next = __detach(__cache);
1744 __node_insert_multi(__cache);
1745 __cache = __next;
1746 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001747#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748 }
1749 catch (...)
1750 {
1751 while (__cache->__parent_ != nullptr)
1752 __cache = static_cast<__node_pointer>(__cache->__parent_);
1753 destroy(__cache);
1754 throw;
1755 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001756#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757 if (__cache != nullptr)
1758 {
1759 while (__cache->__parent_ != nullptr)
1760 __cache = static_cast<__node_pointer>(__cache->__parent_);
1761 destroy(__cache);
1762 }
1763 }
1764 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001765 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001766 }
1767}
1768
1769template <class _Tp, class _Compare, class _Allocator>
1770__tree<_Tp, _Compare, _Allocator>&
1771__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001772 _NOEXCEPT_(
1773 __node_traits::propagate_on_container_move_assignment::value &&
1774 is_nothrow_move_assignable<value_compare>::value &&
1775 is_nothrow_move_assignable<__node_allocator>::value)
1776
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777{
1778 __move_assign(__t, integral_constant<bool,
1779 __node_traits::propagate_on_container_move_assignment::value>());
1780 return *this;
1781}
1782
Howard Hinnant73d21a42010-09-04 23:28:19 +00001783#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784
1785template <class _Tp, class _Compare, class _Allocator>
1786__tree<_Tp, _Compare, _Allocator>::~__tree()
1787{
Marshall Clow1a933122016-06-30 22:05:45 +00001788 static_assert((is_copy_constructible<value_compare>::value),
1789 "Comparator must be copy-constructible.");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001790 destroy(__root());
1791}
1792
1793template <class _Tp, class _Compare, class _Allocator>
1794void
Howard Hinnant7686add2011-06-04 14:31:57 +00001795__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796{
1797 if (__nd != nullptr)
1798 {
1799 destroy(static_cast<__node_pointer>(__nd->__left_));
1800 destroy(static_cast<__node_pointer>(__nd->__right_));
1801 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001802 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803 __node_traits::deallocate(__na, __nd, 1);
1804 }
1805}
1806
1807template <class _Tp, class _Compare, class _Allocator>
1808void
1809__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001810#if _LIBCPP_STD_VER <= 11
Marshall Clow7d914d12015-07-13 20:04:56 +00001811 _NOEXCEPT_(
1812 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001813 && (!__node_traits::propagate_on_container_swap::value ||
1814 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001815 )
Dimitry Andric1421cf02016-08-27 19:32:03 +00001816#else
1817 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1818#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001820 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 swap(__begin_node_, __t.__begin_node_);
1822 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001823 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 __pair3_.swap(__t.__pair3_);
1825 if (size() == 0)
1826 __begin_node() = __end_node();
1827 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001828 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 if (__t.size() == 0)
1830 __t.__begin_node() = __t.__end_node();
1831 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001832 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833}
1834
1835template <class _Tp, class _Compare, class _Allocator>
1836void
Howard Hinnant7686add2011-06-04 14:31:57 +00001837__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838{
1839 destroy(__root());
1840 size() = 0;
1841 __begin_node() = __end_node();
1842 __end_node()->__left_ = nullptr;
1843}
1844
1845// Find lower_bound place to insert
1846// Set __parent to parent of null leaf
1847// Return reference to null leaf
1848template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001849typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1850__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001851 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852{
1853 __node_pointer __nd = __root();
1854 if (__nd != nullptr)
1855 {
1856 while (true)
1857 {
1858 if (value_comp()(__nd->__value_, __v))
1859 {
1860 if (__nd->__right_ != nullptr)
1861 __nd = static_cast<__node_pointer>(__nd->__right_);
1862 else
1863 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001864 __parent = static_cast<__parent_pointer>(__nd);
1865 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866 }
1867 }
1868 else
1869 {
1870 if (__nd->__left_ != nullptr)
1871 __nd = static_cast<__node_pointer>(__nd->__left_);
1872 else
1873 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001874 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001875 return __parent->__left_;
1876 }
1877 }
1878 }
1879 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001880 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001881 return __parent->__left_;
1882}
1883
1884// Find upper_bound place to insert
1885// Set __parent to parent of null leaf
1886// Return reference to null leaf
1887template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001888typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1889__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001890 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891{
1892 __node_pointer __nd = __root();
1893 if (__nd != nullptr)
1894 {
1895 while (true)
1896 {
1897 if (value_comp()(__v, __nd->__value_))
1898 {
1899 if (__nd->__left_ != nullptr)
1900 __nd = static_cast<__node_pointer>(__nd->__left_);
1901 else
1902 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001903 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001904 return __parent->__left_;
1905 }
1906 }
1907 else
1908 {
1909 if (__nd->__right_ != nullptr)
1910 __nd = static_cast<__node_pointer>(__nd->__right_);
1911 else
1912 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001913 __parent = static_cast<__parent_pointer>(__nd);
1914 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001915 }
1916 }
1917 }
1918 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001919 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920 return __parent->__left_;
1921}
1922
1923// Find leaf place to insert closest to __hint
1924// First check prior to __hint.
1925// Next check after __hint.
1926// Next do O(log N) search.
1927// Set __parent to parent of null leaf
1928// Return reference to null leaf
1929template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001930typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001932 __parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001933 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001934{
1935 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1936 {
1937 // __v <= *__hint
1938 const_iterator __prior = __hint;
1939 if (__prior == begin() || !value_comp()(__v, *--__prior))
1940 {
1941 // *prev(__hint) <= __v <= *__hint
1942 if (__hint.__ptr_->__left_ == nullptr)
1943 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001944 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001945 return __parent->__left_;
1946 }
1947 else
1948 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001949 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1950 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 }
1952 }
1953 // __v < *prev(__hint)
1954 return __find_leaf_high(__parent, __v);
1955 }
1956 // else __v > *__hint
1957 return __find_leaf_low(__parent, __v);
1958}
1959
1960// Find place to insert if __v doesn't exist
1961// Set __parent to parent of null leaf
1962// Return reference to null leaf
1963// If __v exists, set parent to node of __v and return reference to node of __v
1964template <class _Tp, class _Compare, class _Allocator>
1965template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001966typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1967__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001968 const _Key& __v)
1969{
1970 __node_pointer __nd = __root();
Eric Fiselier7310ec82016-07-19 17:56:20 +00001971 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 if (__nd != nullptr)
1973 {
1974 while (true)
1975 {
1976 if (value_comp()(__v, __nd->__value_))
1977 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001978 if (__nd->__left_ != nullptr) {
1979 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00001981 } else {
1982 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001983 return __parent->__left_;
1984 }
1985 }
1986 else if (value_comp()(__nd->__value_, __v))
1987 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001988 if (__nd->__right_ != nullptr) {
1989 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001990 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00001991 } else {
1992 __parent = static_cast<__parent_pointer>(__nd);
1993 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001994 }
1995 }
1996 else
1997 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001998 __parent = static_cast<__parent_pointer>(__nd);
1999 return *__nd_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002000 }
2001 }
2002 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00002003 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002004 return __parent->__left_;
2005}
2006
2007// Find place to insert if __v doesn't exist
2008// First check prior to __hint.
2009// Next check after __hint.
2010// Next do O(log N) search.
2011// Set __parent to parent of null leaf
2012// Return reference to null leaf
2013// If __v exists, set parent to node of __v and return reference to node of __v
2014template <class _Tp, class _Compare, class _Allocator>
2015template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00002016typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002017__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002018 __parent_pointer& __parent,
2019 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 const _Key& __v)
2021{
2022 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2023 {
2024 // __v < *__hint
2025 const_iterator __prior = __hint;
2026 if (__prior == begin() || value_comp()(*--__prior, __v))
2027 {
2028 // *prev(__hint) < __v < *__hint
2029 if (__hint.__ptr_->__left_ == nullptr)
2030 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002031 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002032 return __parent->__left_;
2033 }
2034 else
2035 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002036 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2037 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002038 }
2039 }
2040 // __v <= *prev(__hint)
2041 return __find_equal(__parent, __v);
2042 }
2043 else if (value_comp()(*__hint, __v)) // check after
2044 {
2045 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00002046 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 if (__next == end() || value_comp()(__v, *__next))
2048 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002049 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002050 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002052 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2053 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002054 }
2055 else
2056 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002057 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058 return __parent->__left_;
2059 }
2060 }
2061 // *next(__hint) <= __v
2062 return __find_equal(__parent, __v);
2063 }
2064 // else __v == *__hint
Eric Fiselier7310ec82016-07-19 17:56:20 +00002065 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2066 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2067 return __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002068}
2069
2070template <class _Tp, class _Compare, class _Allocator>
2071void
Eric Fiselier7310ec82016-07-19 17:56:20 +00002072__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 __node_base_pointer& __child,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002074 __node_base_pointer __new_node)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075{
2076 __new_node->__left_ = nullptr;
2077 __new_node->__right_ = nullptr;
2078 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002079 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002080 __child = __new_node;
2081 if (__begin_node()->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002082 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 __tree_balance_after_insert(__end_node()->__left_, __child);
2084 ++size();
2085}
2086
Eric Fiselierdb215062016-03-31 02:15:15 +00002087#ifndef _LIBCPP_CXX03_LANG
2088template <class _Tp, class _Compare, class _Allocator>
2089template <class _Key, class... _Args>
2090pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2091__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2092#else
2093template <class _Tp, class _Compare, class _Allocator>
2094template <class _Key, class _Args>
2095pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2096__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2097#endif
2098{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002099 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002100 __node_base_pointer& __child = __find_equal(__parent, __k);
2101 __node_pointer __r = static_cast<__node_pointer>(__child);
2102 bool __inserted = false;
2103 if (__child == nullptr)
2104 {
2105#ifndef _LIBCPP_CXX03_LANG
2106 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2107#else
2108 __node_holder __h = __construct_node(__args);
2109#endif
2110 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2111 __r = __h.release();
2112 __inserted = true;
2113 }
2114 return pair<iterator, bool>(iterator(__r), __inserted);
2115}
2116
2117
2118#ifndef _LIBCPP_CXX03_LANG
2119template <class _Tp, class _Compare, class _Allocator>
2120template <class _Key, class... _Args>
2121typename __tree<_Tp, _Compare, _Allocator>::iterator
2122__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2123 const_iterator __p, _Key const& __k, _Args&&... __args)
2124#else
2125template <class _Tp, class _Compare, class _Allocator>
2126template <class _Key, class _Args>
2127typename __tree<_Tp, _Compare, _Allocator>::iterator
2128__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2129 const_iterator __p, _Key const& __k, _Args& __args)
2130#endif
2131{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002132 __parent_pointer __parent;
2133 __node_base_pointer __dummy;
2134 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierdb215062016-03-31 02:15:15 +00002135 __node_pointer __r = static_cast<__node_pointer>(__child);
2136 if (__child == nullptr)
2137 {
2138#ifndef _LIBCPP_CXX03_LANG
2139 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2140#else
2141 __node_holder __h = __construct_node(__args);
2142#endif
2143 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2144 __r = __h.release();
2145 }
2146 return iterator(__r);
2147}
2148
2149
2150#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002151
2152template <class _Tp, class _Compare, class _Allocator>
2153template <class ..._Args>
2154typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2155__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2156{
Eric Fiselierdb215062016-03-31 02:15:15 +00002157 static_assert(!__is_tree_value_type<_Args...>::value,
2158 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002160 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002161 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002162 __h.get_deleter().__value_constructed = true;
2163 return __h;
2164}
2165
Eric Fiselierdb215062016-03-31 02:15:15 +00002166
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002167template <class _Tp, class _Compare, class _Allocator>
2168template <class... _Args>
2169pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002170__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002172 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002173 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002174 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2175 __node_pointer __r = static_cast<__node_pointer>(__child);
2176 bool __inserted = false;
2177 if (__child == nullptr)
2178 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002179 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002180 __r = __h.release();
2181 __inserted = true;
2182 }
2183 return pair<iterator, bool>(iterator(__r), __inserted);
2184}
2185
2186template <class _Tp, class _Compare, class _Allocator>
2187template <class... _Args>
2188typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002189__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002191 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002192 __parent_pointer __parent;
2193 __node_base_pointer __dummy;
2194 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002195 __node_pointer __r = static_cast<__node_pointer>(__child);
2196 if (__child == nullptr)
2197 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002198 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 __r = __h.release();
2200 }
2201 return iterator(__r);
2202}
2203
2204template <class _Tp, class _Compare, class _Allocator>
2205template <class... _Args>
2206typename __tree<_Tp, _Compare, _Allocator>::iterator
2207__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2208{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002209 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002210 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002211 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002212 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 return iterator(static_cast<__node_pointer>(__h.release()));
2214}
2215
2216template <class _Tp, class _Compare, class _Allocator>
2217template <class... _Args>
2218typename __tree<_Tp, _Compare, _Allocator>::iterator
2219__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2220 _Args&&... __args)
2221{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002222 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002223 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002224 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002225 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 return iterator(static_cast<__node_pointer>(__h.release()));
2227}
2228
Howard Hinnant73d21a42010-09-04 23:28:19 +00002229
Eric Fiselierdb215062016-03-31 02:15:15 +00002230#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231
2232template <class _Tp, class _Compare, class _Allocator>
2233typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002234__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235{
2236 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002237 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002238 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002240 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241}
2242
Eric Fiselierdb215062016-03-31 02:15:15 +00002243#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002244
Eric Fiselierdb215062016-03-31 02:15:15 +00002245#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002246template <class _Tp, class _Compare, class _Allocator>
2247typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002248__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002249{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002250 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002251 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002252 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002253 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002254 return iterator(__h.release());
2255}
2256
2257template <class _Tp, class _Compare, class _Allocator>
2258typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002259__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002261 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002262 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002263 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002264 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002265 return iterator(__h.release());
2266}
Eric Fiselierdb215062016-03-31 02:15:15 +00002267#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269template <class _Tp, class _Compare, class _Allocator>
2270pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2271__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2272{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002273 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2275 __node_pointer __r = static_cast<__node_pointer>(__child);
2276 bool __inserted = false;
2277 if (__child == nullptr)
2278 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002279 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280 __r = __nd;
2281 __inserted = true;
2282 }
2283 return pair<iterator, bool>(iterator(__r), __inserted);
2284}
2285
2286template <class _Tp, class _Compare, class _Allocator>
2287typename __tree<_Tp, _Compare, _Allocator>::iterator
2288__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2289 __node_pointer __nd)
2290{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002291 __parent_pointer __parent;
2292 __node_base_pointer __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002293 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2294 __node_pointer __r = static_cast<__node_pointer>(__child);
2295 if (__child == nullptr)
2296 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002297 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298 __r = __nd;
2299 }
2300 return iterator(__r);
2301}
2302
2303template <class _Tp, class _Compare, class _Allocator>
2304typename __tree<_Tp, _Compare, _Allocator>::iterator
2305__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2306{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002307 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002308 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002309 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310 return iterator(__nd);
2311}
2312
2313template <class _Tp, class _Compare, class _Allocator>
2314typename __tree<_Tp, _Compare, _Allocator>::iterator
2315__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2316 __node_pointer __nd)
2317{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002318 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002319 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002320 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321 return iterator(__nd);
2322}
2323
2324template <class _Tp, class _Compare, class _Allocator>
2325typename __tree<_Tp, _Compare, _Allocator>::iterator
2326__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2327{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002328 __node_pointer __np = __p.__get_np();
2329 iterator __r(__p.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330 ++__r;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002331 if (__begin_node() == __p.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 __begin_node() = __r.__ptr_;
2333 --size();
2334 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 __tree_remove(__end_node()->__left_,
2336 static_cast<__node_base_pointer>(__np));
Eric Fiselierdb215062016-03-31 02:15:15 +00002337 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2338 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002339 __node_traits::deallocate(__na, __np, 1);
2340 return __r;
2341}
2342
2343template <class _Tp, class _Compare, class _Allocator>
2344typename __tree<_Tp, _Compare, _Allocator>::iterator
2345__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2346{
2347 while (__f != __l)
2348 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002349 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002350}
2351
2352template <class _Tp, class _Compare, class _Allocator>
2353template <class _Key>
2354typename __tree<_Tp, _Compare, _Allocator>::size_type
2355__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2356{
2357 iterator __i = find(__k);
2358 if (__i == end())
2359 return 0;
2360 erase(__i);
2361 return 1;
2362}
2363
2364template <class _Tp, class _Compare, class _Allocator>
2365template <class _Key>
2366typename __tree<_Tp, _Compare, _Allocator>::size_type
2367__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2368{
2369 pair<iterator, iterator> __p = __equal_range_multi(__k);
2370 size_type __r = 0;
2371 for (; __p.first != __p.second; ++__r)
2372 __p.first = erase(__p.first);
2373 return __r;
2374}
2375
2376template <class _Tp, class _Compare, class _Allocator>
2377template <class _Key>
2378typename __tree<_Tp, _Compare, _Allocator>::iterator
2379__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2380{
2381 iterator __p = __lower_bound(__v, __root(), __end_node());
2382 if (__p != end() && !value_comp()(__v, *__p))
2383 return __p;
2384 return end();
2385}
2386
2387template <class _Tp, class _Compare, class _Allocator>
2388template <class _Key>
2389typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2390__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2391{
2392 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2393 if (__p != end() && !value_comp()(__v, *__p))
2394 return __p;
2395 return end();
2396}
2397
2398template <class _Tp, class _Compare, class _Allocator>
2399template <class _Key>
2400typename __tree<_Tp, _Compare, _Allocator>::size_type
2401__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2402{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002403 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002404 while (__rt != nullptr)
2405 {
2406 if (value_comp()(__k, __rt->__value_))
2407 {
Eric Fiselier227b47c2016-02-20 07:12:17 +00002408 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002409 }
2410 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002411 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002412 else
2413 return 1;
2414 }
2415 return 0;
2416}
2417
2418template <class _Tp, class _Compare, class _Allocator>
2419template <class _Key>
2420typename __tree<_Tp, _Compare, _Allocator>::size_type
2421__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2422{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002423 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002424 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002425 while (__rt != nullptr)
2426 {
2427 if (value_comp()(__k, __rt->__value_))
2428 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002429 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002430 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002431 }
2432 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002433 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002435 return _VSTD::distance(
Eric Fiselier7310ec82016-07-19 17:56:20 +00002436 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002437 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002438 );
2439 }
2440 return 0;
2441}
2442
2443template <class _Tp, class _Compare, class _Allocator>
2444template <class _Key>
2445typename __tree<_Tp, _Compare, _Allocator>::iterator
2446__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2447 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002448 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449{
2450 while (__root != nullptr)
2451 {
2452 if (!value_comp()(__root->__value_, __v))
2453 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002454 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002455 __root = static_cast<__node_pointer>(__root->__left_);
2456 }
2457 else
2458 __root = static_cast<__node_pointer>(__root->__right_);
2459 }
2460 return iterator(__result);
2461}
2462
2463template <class _Tp, class _Compare, class _Allocator>
2464template <class _Key>
2465typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2466__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002467 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002468 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469{
2470 while (__root != nullptr)
2471 {
2472 if (!value_comp()(__root->__value_, __v))
2473 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002474 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002475 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002476 }
2477 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002478 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479 }
2480 return const_iterator(__result);
2481}
2482
2483template <class _Tp, class _Compare, class _Allocator>
2484template <class _Key>
2485typename __tree<_Tp, _Compare, _Allocator>::iterator
2486__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2487 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002488 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489{
2490 while (__root != nullptr)
2491 {
2492 if (value_comp()(__v, __root->__value_))
2493 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002494 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002495 __root = static_cast<__node_pointer>(__root->__left_);
2496 }
2497 else
2498 __root = static_cast<__node_pointer>(__root->__right_);
2499 }
2500 return iterator(__result);
2501}
2502
2503template <class _Tp, class _Compare, class _Allocator>
2504template <class _Key>
2505typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2506__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002507 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002508 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002509{
2510 while (__root != nullptr)
2511 {
2512 if (value_comp()(__v, __root->__value_))
2513 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002514 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002515 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 }
2517 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002518 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519 }
2520 return const_iterator(__result);
2521}
2522
2523template <class _Tp, class _Compare, class _Allocator>
2524template <class _Key>
2525pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2526 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2527__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2528{
Howard Hinnant99968442011-11-29 18:15:50 +00002529 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002530 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002531 __node_pointer __rt = __root();
2532 while (__rt != nullptr)
2533 {
2534 if (value_comp()(__k, __rt->__value_))
2535 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002536 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002537 __rt = static_cast<__node_pointer>(__rt->__left_);
2538 }
2539 else if (value_comp()(__rt->__value_, __k))
2540 __rt = static_cast<__node_pointer>(__rt->__right_);
2541 else
Howard Hinnant99968442011-11-29 18:15:50 +00002542 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002543 iterator(
2544 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002545 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002546 : __result));
2547 }
Howard Hinnant99968442011-11-29 18:15:50 +00002548 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002549}
2550
2551template <class _Tp, class _Compare, class _Allocator>
2552template <class _Key>
2553pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2554 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2555__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2556{
Howard Hinnant99968442011-11-29 18:15:50 +00002557 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002558 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002559 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002560 while (__rt != nullptr)
2561 {
2562 if (value_comp()(__k, __rt->__value_))
2563 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002564 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002565 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002566 }
2567 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002568 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002569 else
Howard Hinnant99968442011-11-29 18:15:50 +00002570 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002571 const_iterator(
2572 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002573 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 : __result));
2575 }
Howard Hinnant99968442011-11-29 18:15:50 +00002576 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577}
2578
2579template <class _Tp, class _Compare, class _Allocator>
2580template <class _Key>
2581pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2582 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2583__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2584{
Howard Hinnant99968442011-11-29 18:15:50 +00002585 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002586 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002587 __node_pointer __rt = __root();
2588 while (__rt != nullptr)
2589 {
2590 if (value_comp()(__k, __rt->__value_))
2591 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002592 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002593 __rt = static_cast<__node_pointer>(__rt->__left_);
2594 }
2595 else if (value_comp()(__rt->__value_, __k))
2596 __rt = static_cast<__node_pointer>(__rt->__right_);
2597 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002598 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002599 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2600 }
Howard Hinnant99968442011-11-29 18:15:50 +00002601 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602}
2603
2604template <class _Tp, class _Compare, class _Allocator>
2605template <class _Key>
2606pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2607 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2608__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2609{
Howard Hinnant99968442011-11-29 18:15:50 +00002610 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002611 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002612 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613 while (__rt != nullptr)
2614 {
2615 if (value_comp()(__k, __rt->__value_))
2616 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002617 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002618 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002619 }
2620 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002621 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002622 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002623 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002624 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002625 }
Howard Hinnant99968442011-11-29 18:15:50 +00002626 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002627}
2628
2629template <class _Tp, class _Compare, class _Allocator>
2630typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002631__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002632{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002633 __node_pointer __np = __p.__get_np();
2634 if (__begin_node() == __p.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002635 {
2636 if (__np->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002637 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002638 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002639 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002640 }
2641 --size();
2642 __tree_remove(__end_node()->__left_,
2643 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002644 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002645}
2646
Howard Hinnant7686add2011-06-04 14:31:57 +00002647template <class _Tp, class _Compare, class _Allocator>
2648inline _LIBCPP_INLINE_VISIBILITY
2649void
2650swap(__tree<_Tp, _Compare, _Allocator>& __x,
2651 __tree<_Tp, _Compare, _Allocator>& __y)
2652 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2653{
2654 __x.swap(__y);
2655}
2656
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002657_LIBCPP_END_NAMESPACE_STD
2658
2659#endif // _LIBCPP___TREE