blob: 3b3586ca697d10b275eb5beaaff3ed16a0878050 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
Howard Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
Eric Fiselier018a3d52017-05-31 22:07:49 +000024_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000030template <class _Tp, class _Compare, class _Allocator> class __tree;
31template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000032 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000033template <class _Tp, class _ConstNodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000034 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035
Eric Fiselier55263482016-02-20 05:28:30 +000036template <class _Pointer> class __tree_end_node;
37template <class _VoidPtr> class __tree_node_base;
38template <class _Tp, class _VoidPtr> class __tree_node;
39
Eric Fiselier55263482016-02-20 05:28:30 +000040template <class _Key, class _Value>
41struct __value_type;
Eric Fiselier55263482016-02-20 05:28:30 +000042
Eric Fiselierebaf7da2017-01-13 22:02:08 +000043template <class _Key, class _CP, class _Compare,
44 bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
45class __map_value_compare;
46
Eric Fiselier55263482016-02-20 05:28:30 +000047template <class _Allocator> class __map_node_destructor;
Eric Fiselierc3589a82017-01-04 23:56:00 +000048template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
49template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier55263482016-02-20 05:28:30 +000050
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000051/*
52
53_NodePtr algorithms
54
55The algorithms taking _NodePtr are red black tree algorithms. Those
56algorithms taking a parameter named __root should assume that __root
57points to a proper red black tree (unless otherwise specified).
58
59Each algorithm herein assumes that __root->__parent_ points to a non-null
60structure which has a member __left_ which points back to __root. No other
61member is read or written to at __root->__parent_.
62
63__root->__parent_ will be referred to below (in comments only) as end_node.
64end_node->__left_ is an externably accessible lvalue for __root, and can be
65changed by node insertion and removal (without explicit reference to end_node).
66
67All nodes (with the exception of end_node), even the node referred to as
68__root, have a non-null __parent_ field.
69
70*/
71
72// Returns: true if __x is a left child of its parent, else false
73// Precondition: __x != nullptr.
74template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +000075inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000076bool
Howard Hinnant8b537682011-06-04 17:10:24 +000077__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078{
79 return __x == __x->__parent_->__left_;
80}
81
Joerg Sonnenberger9b69be42017-08-18 12:57:36 +000082// Determines if the subtree rooted at __x is a proper red black subtree. If
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083// __x is a proper subtree, returns the black height (null counts as 1). If
84// __x is an improper subtree, returns 0.
85template <class _NodePtr>
86unsigned
87__tree_sub_invariant(_NodePtr __x)
88{
89 if (__x == nullptr)
90 return 1;
91 // parent consistency checked by caller
92 // check __x->__left_ consistency
93 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
94 return 0;
95 // check __x->__right_ consistency
96 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
97 return 0;
98 // check __x->__left_ != __x->__right_ unless both are nullptr
99 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
100 return 0;
101 // If this is red, neither child can be red
102 if (!__x->__is_black_)
103 {
104 if (__x->__left_ && !__x->__left_->__is_black_)
105 return 0;
106 if (__x->__right_ && !__x->__right_->__is_black_)
107 return 0;
108 }
109 unsigned __h = __tree_sub_invariant(__x->__left_);
110 if (__h == 0)
111 return 0; // invalid left subtree
112 if (__h != __tree_sub_invariant(__x->__right_))
113 return 0; // invalid or different height right subtree
114 return __h + __x->__is_black_; // return black height of this node
115}
116
Joerg Sonnenberger9b69be42017-08-18 12:57:36 +0000117// Determines if the red black tree rooted at __root is a proper red black tree.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118// __root == nullptr is a proper tree. Returns true is __root is a proper
119// red black tree, else returns false.
120template <class _NodePtr>
121bool
122__tree_invariant(_NodePtr __root)
123{
124 if (__root == nullptr)
125 return true;
126 // check __x->__parent_ consistency
127 if (__root->__parent_ == nullptr)
128 return false;
129 if (!__tree_is_left_child(__root))
130 return false;
131 // root must be black
132 if (!__root->__is_black_)
133 return false;
134 // do normal node checks
135 return __tree_sub_invariant(__root) != 0;
136}
137
138// Returns: pointer to the left-most node under __x.
139// Precondition: __x != nullptr.
140template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000141inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000142_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000143__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000144{
145 while (__x->__left_ != nullptr)
146 __x = __x->__left_;
147 return __x;
148}
149
150// Returns: pointer to the right-most node under __x.
151// Precondition: __x != nullptr.
152template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000153inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000155__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156{
157 while (__x->__right_ != nullptr)
158 __x = __x->__right_;
159 return __x;
160}
161
162// Returns: pointer to the next in-order node after __x.
163// Precondition: __x != nullptr.
164template <class _NodePtr>
165_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000166__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167{
168 if (__x->__right_ != nullptr)
169 return __tree_min(__x->__right_);
170 while (!__tree_is_left_child(__x))
Eric Fiselier7310ec82016-07-19 17:56:20 +0000171 __x = __x->__parent_unsafe();
172 return __x->__parent_unsafe();
173}
174
175template <class _EndNodePtr, class _NodePtr>
176inline _LIBCPP_INLINE_VISIBILITY
177_EndNodePtr
178__tree_next_iter(_NodePtr __x) _NOEXCEPT
179{
180 if (__x->__right_ != nullptr)
181 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
182 while (!__tree_is_left_child(__x))
183 __x = __x->__parent_unsafe();
184 return static_cast<_EndNodePtr>(__x->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185}
186
187// Returns: pointer to the previous in-order node before __x.
188// Precondition: __x != nullptr.
Eric Fiselier7310ec82016-07-19 17:56:20 +0000189// Note: __x may be the end node.
190template <class _NodePtr, class _EndNodePtr>
191inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192_NodePtr
Eric Fiselier7310ec82016-07-19 17:56:20 +0000193__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194{
195 if (__x->__left_ != nullptr)
196 return __tree_max(__x->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +0000197 _NodePtr __xx = static_cast<_NodePtr>(__x);
198 while (__tree_is_left_child(__xx))
199 __xx = __xx->__parent_unsafe();
200 return __xx->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201}
202
203// Returns: pointer to a node which has no children
204// Precondition: __x != nullptr.
205template <class _NodePtr>
206_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000207__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208{
209 while (true)
210 {
211 if (__x->__left_ != nullptr)
212 {
213 __x = __x->__left_;
214 continue;
215 }
216 if (__x->__right_ != nullptr)
217 {
218 __x = __x->__right_;
219 continue;
220 }
221 break;
222 }
223 return __x;
224}
225
226// Effects: Makes __x->__right_ the subtree root with __x as its left child
227// while preserving in-order order.
228// Precondition: __x->__right_ != nullptr
229template <class _NodePtr>
230void
Howard Hinnant8b537682011-06-04 17:10:24 +0000231__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232{
233 _NodePtr __y = __x->__right_;
234 __x->__right_ = __y->__left_;
235 if (__x->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000236 __x->__right_->__set_parent(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237 __y->__parent_ = __x->__parent_;
238 if (__tree_is_left_child(__x))
239 __x->__parent_->__left_ = __y;
240 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000241 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242 __y->__left_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000243 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244}
245
246// Effects: Makes __x->__left_ the subtree root with __x as its right child
247// while preserving in-order order.
248// Precondition: __x->__left_ != nullptr
249template <class _NodePtr>
250void
Howard Hinnant8b537682011-06-04 17:10:24 +0000251__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252{
253 _NodePtr __y = __x->__left_;
254 __x->__left_ = __y->__right_;
255 if (__x->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000256 __x->__left_->__set_parent(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257 __y->__parent_ = __x->__parent_;
258 if (__tree_is_left_child(__x))
259 __x->__parent_->__left_ = __y;
260 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000261 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 __y->__right_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000263 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264}
265
266// Effects: Rebalances __root after attaching __x to a leaf.
267// Precondition: __root != nulptr && __x != nullptr.
268// __x has no children.
269// __x == __root or == a direct or indirect child of __root.
270// If __x were to be unlinked from __root (setting __root to
271// nullptr if __root == __x), __tree_invariant(__root) == true.
272// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
273// may be different than the value passed in as __root.
274template <class _NodePtr>
275void
Howard Hinnant8b537682011-06-04 17:10:24 +0000276__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277{
278 __x->__is_black_ = __x == __root;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000279 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280 {
281 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier7310ec82016-07-19 17:56:20 +0000282 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000284 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285 if (__y != nullptr && !__y->__is_black_)
286 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000287 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000289 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290 __x->__is_black_ = __x == __root;
291 __y->__is_black_ = true;
292 }
293 else
294 {
295 if (!__tree_is_left_child(__x))
296 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000297 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 __tree_left_rotate(__x);
299 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000300 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000302 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303 __x->__is_black_ = false;
304 __tree_right_rotate(__x);
305 break;
306 }
307 }
308 else
309 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000310 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 if (__y != nullptr && !__y->__is_black_)
312 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000313 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000314 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000315 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316 __x->__is_black_ = __x == __root;
317 __y->__is_black_ = true;
318 }
319 else
320 {
321 if (__tree_is_left_child(__x))
322 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000323 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000324 __tree_right_rotate(__x);
325 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000326 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000327 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000328 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 __x->__is_black_ = false;
330 __tree_left_rotate(__x);
331 break;
332 }
333 }
334 }
335}
336
337// Precondition: __root != nullptr && __z != nullptr.
338// __tree_invariant(__root) == true.
339// __z == __root or == a direct or indirect child of __root.
340// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
341// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
342// nor any of its children refer to __z. end_node->__left_
343// may be different than the value passed in as __root.
344template <class _NodePtr>
345void
Howard Hinnant8b537682011-06-04 17:10:24 +0000346__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347{
348 // __z will be removed from the tree. Client still needs to destruct/deallocate it
349 // __y is either __z, or if __z has two children, __tree_next(__z).
350 // __y will have at most one child.
351 // __y will be the initial hole in the tree (make the hole at a leaf)
352 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
353 __z : __tree_next(__z);
354 // __x is __y's possibly null single child
355 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
356 // __w is __x's possibly null uncle (will become __x's sibling)
357 _NodePtr __w = nullptr;
358 // link __x to __y's parent, and find __w
359 if (__x != nullptr)
360 __x->__parent_ = __y->__parent_;
361 if (__tree_is_left_child(__y))
362 {
363 __y->__parent_->__left_ = __x;
364 if (__y != __root)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000365 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366 else
367 __root = __x; // __w == nullptr
368 }
369 else
370 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000371 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 // __y can't be root if it is a right child
373 __w = __y->__parent_->__left_;
374 }
375 bool __removed_black = __y->__is_black_;
376 // If we didn't remove __z, do so now by splicing in __y for __z,
377 // but copy __z's color. This does not impact __x or __w.
378 if (__y != __z)
379 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000380 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000381 __y->__parent_ = __z->__parent_;
382 if (__tree_is_left_child(__z))
383 __y->__parent_->__left_ = __y;
384 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000385 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386 __y->__left_ = __z->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000387 __y->__left_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000388 __y->__right_ = __z->__right_;
389 if (__y->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000390 __y->__right_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 __y->__is_black_ = __z->__is_black_;
392 if (__root == __z)
393 __root = __y;
394 }
395 // There is no need to rebalance if we removed a red, or if we removed
396 // the last node.
397 if (__removed_black && __root != nullptr)
398 {
399 // Rebalance:
400 // __x has an implicit black color (transferred from the removed __y)
401 // associated with it, no matter what its color is.
402 // If __x is __root (in which case it can't be null), it is supposed
403 // to be black anyway, and if it is doubly black, then the double
404 // can just be ignored.
405 // If __x is red (in which case it can't be null), then it can absorb
406 // the implicit black just by setting its color to black.
407 // Since __y was black and only had one child (which __x points to), __x
408 // is either red with no children, else null, otherwise __y would have
409 // different black heights under left and right pointers.
410 // if (__x == __root || __x != nullptr && !__x->__is_black_)
411 if (__x != nullptr)
412 __x->__is_black_ = true;
413 else
414 {
415 // Else __x isn't root, and is "doubly black", even though it may
416 // be null. __w can not be null here, else the parent would
417 // see a black height >= 2 on the __x side and a black height
418 // of 1 on the __w side (__w must be a non-null black or a red
419 // with a non-null black child).
420 while (true)
421 {
422 if (!__tree_is_left_child(__w)) // if x is left child
423 {
424 if (!__w->__is_black_)
425 {
426 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000427 __w->__parent_unsafe()->__is_black_ = false;
428 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000429 // __x is still valid
430 // reset __root only if necessary
431 if (__root == __w->__left_)
432 __root = __w;
433 // reset sibling, and it still can't be null
434 __w = __w->__left_->__right_;
435 }
436 // __w->__is_black_ is now true, __w may have null children
437 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
438 (__w->__right_ == nullptr || __w->__right_->__is_black_))
439 {
440 __w->__is_black_ = false;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000441 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 // __x can no longer be null
443 if (__x == __root || !__x->__is_black_)
444 {
445 __x->__is_black_ = true;
446 break;
447 }
448 // reset sibling, and it still can't be null
449 __w = __tree_is_left_child(__x) ?
Eric Fiselier7310ec82016-07-19 17:56:20 +0000450 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000451 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000452 // continue;
453 }
454 else // __w has a red child
455 {
456 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
457 {
458 // __w left child is non-null and red
459 __w->__left_->__is_black_ = true;
460 __w->__is_black_ = false;
461 __tree_right_rotate(__w);
462 // __w is known not to be root, so root hasn't changed
463 // reset sibling, and it still can't be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000464 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000465 }
466 // __w has a right red child, left child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000467 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
468 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000469 __w->__right_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000470 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000471 break;
472 }
473 }
474 else
475 {
476 if (!__w->__is_black_)
477 {
478 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000479 __w->__parent_unsafe()->__is_black_ = false;
480 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000481 // __x is still valid
482 // reset __root only if necessary
483 if (__root == __w->__right_)
484 __root = __w;
485 // reset sibling, and it still can't be null
486 __w = __w->__right_->__left_;
487 }
488 // __w->__is_black_ is now true, __w may have null children
489 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
490 (__w->__right_ == nullptr || __w->__right_->__is_black_))
491 {
492 __w->__is_black_ = false;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000493 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494 // __x can no longer be null
495 if (!__x->__is_black_ || __x == __root)
496 {
497 __x->__is_black_ = true;
498 break;
499 }
500 // reset sibling, and it still can't be null
501 __w = __tree_is_left_child(__x) ?
Eric Fiselier7310ec82016-07-19 17:56:20 +0000502 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000503 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000504 // continue;
505 }
506 else // __w has a red child
507 {
508 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
509 {
510 // __w right child is non-null and red
511 __w->__right_->__is_black_ = true;
512 __w->__is_black_ = false;
513 __tree_left_rotate(__w);
514 // __w is known not to be root, so root hasn't changed
515 // reset sibling, and it still can't be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000516 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 }
518 // __w has a left red child, right child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000519 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
520 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000521 __w->__left_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000522 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 break;
524 }
525 }
526 }
527 }
528 }
529}
530
Eric Fiselier55263482016-02-20 05:28:30 +0000531// node traits
532
Eric Fiselierdb215062016-03-31 02:15:15 +0000533
534#ifndef _LIBCPP_CXX03_LANG
535template <class _Tp>
536struct __is_tree_value_type_imp : false_type {};
537
538template <class _Key, class _Value>
539struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
540
541template <class ..._Args>
542struct __is_tree_value_type : false_type {};
543
544template <class _One>
545struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
546#endif
547
Eric Fiselier55263482016-02-20 05:28:30 +0000548template <class _Tp>
549struct __tree_key_value_types {
550 typedef _Tp key_type;
551 typedef _Tp __node_value_type;
552 typedef _Tp __container_value_type;
553 static const bool __is_map = false;
Eric Fiselierdb215062016-03-31 02:15:15 +0000554
555 _LIBCPP_INLINE_VISIBILITY
556 static key_type const& __get_key(_Tp const& __v) {
557 return __v;
558 }
559 _LIBCPP_INLINE_VISIBILITY
560 static __container_value_type const& __get_value(__node_value_type const& __v) {
561 return __v;
562 }
563 _LIBCPP_INLINE_VISIBILITY
564 static __container_value_type* __get_ptr(__node_value_type& __n) {
565 return _VSTD::addressof(__n);
566 }
Eric Fiselierdb215062016-03-31 02:15:15 +0000567#ifndef _LIBCPP_CXX03_LANG
568 _LIBCPP_INLINE_VISIBILITY
Erik Pilkington55513c82018-06-04 20:38:23 +0000569 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierdb215062016-03-31 02:15:15 +0000570 return _VSTD::move(__v);
571 }
572#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000573};
574
575template <class _Key, class _Tp>
576struct __tree_key_value_types<__value_type<_Key, _Tp> > {
577 typedef _Key key_type;
578 typedef _Tp mapped_type;
579 typedef __value_type<_Key, _Tp> __node_value_type;
580 typedef pair<const _Key, _Tp> __container_value_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000581 typedef __container_value_type __map_value_type;
582 static const bool __is_map = true;
Eric Fiselierdb215062016-03-31 02:15:15 +0000583
584 _LIBCPP_INLINE_VISIBILITY
585 static key_type const&
586 __get_key(__node_value_type const& __t) {
Erik Pilkington55513c82018-06-04 20:38:23 +0000587 return __t.__get_value().first;
Eric Fiselierdb215062016-03-31 02:15:15 +0000588 }
589
590 template <class _Up>
591 _LIBCPP_INLINE_VISIBILITY
592 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
593 key_type const&>::type
594 __get_key(_Up& __t) {
595 return __t.first;
596 }
597
598 _LIBCPP_INLINE_VISIBILITY
599 static __container_value_type const&
600 __get_value(__node_value_type const& __t) {
Erik Pilkington55513c82018-06-04 20:38:23 +0000601 return __t.__get_value();
Eric Fiselierdb215062016-03-31 02:15:15 +0000602 }
603
604 template <class _Up>
605 _LIBCPP_INLINE_VISIBILITY
606 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
607 __container_value_type const&>::type
608 __get_value(_Up& __t) {
609 return __t;
610 }
611
612 _LIBCPP_INLINE_VISIBILITY
613 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkington55513c82018-06-04 20:38:23 +0000614 return _VSTD::addressof(__n.__get_value());
Eric Fiselierdb215062016-03-31 02:15:15 +0000615 }
616
617#ifndef _LIBCPP_CXX03_LANG
618 _LIBCPP_INLINE_VISIBILITY
Erik Pilkington55513c82018-06-04 20:38:23 +0000619 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
620 return __v.__move();
Eric Fiselierdb215062016-03-31 02:15:15 +0000621 }
622#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000623};
624
625template <class _VoidPtr>
626struct __tree_node_base_types {
627 typedef _VoidPtr __void_pointer;
628
629 typedef __tree_node_base<__void_pointer> __node_base_type;
630 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
631 __node_base_pointer;
632
633 typedef __tree_end_node<__node_base_pointer> __end_node_type;
634 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
635 __end_node_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000636#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
637 typedef __end_node_pointer __parent_pointer;
638#else
639 typedef typename conditional<
640 is_pointer<__end_node_pointer>::value,
641 __end_node_pointer,
642 __node_base_pointer>::type __parent_pointer;
643#endif
644
Eric Fiselier55263482016-02-20 05:28:30 +0000645private:
646 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
647 "_VoidPtr does not point to unqualified void type");
648};
649
650template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
651 bool = _KVTypes::__is_map>
652struct __tree_map_pointer_types {};
653
654template <class _Tp, class _AllocPtr, class _KVTypes>
655struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
656 typedef typename _KVTypes::__map_value_type _Mv;
657 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
658 __map_value_type_pointer;
659 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
660 __const_map_value_type_pointer;
661};
662
663template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
664struct __tree_node_types;
665
666template <class _NodePtr, class _Tp, class _VoidPtr>
667struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
668 : public __tree_node_base_types<_VoidPtr>,
669 __tree_key_value_types<_Tp>,
670 __tree_map_pointer_types<_Tp, _VoidPtr>
671{
672 typedef __tree_node_base_types<_VoidPtr> __base;
673 typedef __tree_key_value_types<_Tp> __key_base;
674 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
675public:
676
677 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
678 typedef _NodePtr __node_pointer;
679
680 typedef _Tp __node_value_type;
681 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
682 __node_value_type_pointer;
683 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
684 __const_node_value_type_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000685#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
686 typedef typename __base::__end_node_pointer __iter_pointer;
687#else
688 typedef typename conditional<
689 is_pointer<__node_pointer>::value,
690 typename __base::__end_node_pointer,
691 __node_pointer>::type __iter_pointer;
692#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000693private:
694 static_assert(!is_const<__node_type>::value,
695 "_NodePtr should never be a pointer to const");
696 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
697 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
698};
699
700template <class _ValueTp, class _VoidPtr>
701struct __make_tree_node_types {
702 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
703 _NodePtr;
704 typedef __tree_node_types<_NodePtr> type;
705};
706
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000707// node
708
709template <class _Pointer>
710class __tree_end_node
711{
712public:
713 typedef _Pointer pointer;
714 pointer __left_;
715
Howard Hinnant333f50d2010-09-21 20:16:37 +0000716 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000717 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000718};
719
720template <class _VoidPtr>
721class __tree_node_base
Eric Fiselier55263482016-02-20 05:28:30 +0000722 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000723{
Eric Fiselier55263482016-02-20 05:28:30 +0000724 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
725
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726public:
Eric Fiselier55263482016-02-20 05:28:30 +0000727 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000728 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729
Eric Fiselier7310ec82016-07-19 17:56:20 +0000730 pointer __right_;
731 __parent_pointer __parent_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732 bool __is_black_;
733
Eric Fiselier7310ec82016-07-19 17:56:20 +0000734 _LIBCPP_INLINE_VISIBILITY
735 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
736
737 _LIBCPP_INLINE_VISIBILITY
738 void __set_parent(pointer __p) {
739 __parent_ = static_cast<__parent_pointer>(__p);
740 }
741
Eric Fiselierdb215062016-03-31 02:15:15 +0000742private:
743 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
744 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
745 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746};
747
748template <class _Tp, class _VoidPtr>
749class __tree_node
750 : public __tree_node_base<_VoidPtr>
751{
752public:
Eric Fiselier55263482016-02-20 05:28:30 +0000753 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000754
Eric Fiselier55263482016-02-20 05:28:30 +0000755 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000756
Eric Fiselierdb215062016-03-31 02:15:15 +0000757private:
758 ~__tree_node() _LIBCPP_EQUAL_DELETE;
759 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
760 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761};
762
Eric Fiselierdb215062016-03-31 02:15:15 +0000763
764template <class _Allocator>
765class __tree_node_destructor
766{
767 typedef _Allocator allocator_type;
768 typedef allocator_traits<allocator_type> __alloc_traits;
769
770public:
771 typedef typename __alloc_traits::pointer pointer;
772private:
773 typedef __tree_node_types<pointer> _NodeTypes;
774 allocator_type& __na_;
775
776 __tree_node_destructor& operator=(const __tree_node_destructor&);
777
778public:
779 bool __value_constructed;
780
781 _LIBCPP_INLINE_VISIBILITY
782 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
783 : __na_(__na),
784 __value_constructed(__val)
785 {}
786
787 _LIBCPP_INLINE_VISIBILITY
788 void operator()(pointer __p) _NOEXCEPT
789 {
790 if (__value_constructed)
791 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
792 if (__p)
793 __alloc_traits::deallocate(__na_, __p, 1);
794 }
795
796 template <class> friend class __map_node_destructor;
797};
798
Erik Pilkington36fc7372018-08-01 01:33:38 +0000799#if _LIBCPP_STD_VER > 14
800template <class _NodeType, class _Alloc>
801struct __generic_container_node_destructor;
802template <class _Tp, class _VoidPtr, class _Alloc>
803struct __generic_container_node_destructor<__tree_node<_Tp, _VoidPtr>, _Alloc>
804 : __tree_node_destructor<_Alloc>
805{
806 using __tree_node_destructor<_Alloc>::__tree_node_destructor;
807};
808#endif
Eric Fiselierdb215062016-03-31 02:15:15 +0000809
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000811class _LIBCPP_TEMPLATE_VIS __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000812{
Eric Fiselier55263482016-02-20 05:28:30 +0000813 typedef __tree_node_types<_NodePtr> _NodeTypes;
814 typedef _NodePtr __node_pointer;
815 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000816 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
817 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000818 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819
Eric Fiselier7310ec82016-07-19 17:56:20 +0000820 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822public:
Eric Fiselier55263482016-02-20 05:28:30 +0000823 typedef bidirectional_iterator_tag iterator_category;
824 typedef _Tp value_type;
825 typedef _DiffType difference_type;
826 typedef value_type& reference;
827 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
Marshall Clow051c8482013-08-08 21:52:50 +0000829 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
830#if _LIBCPP_STD_VER > 11
831 : __ptr_(nullptr)
832#endif
833 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834
Eric Fiselier7310ec82016-07-19 17:56:20 +0000835 _LIBCPP_INLINE_VISIBILITY reference operator*() const
836 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000837 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000838 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
Howard Hinnant333f50d2010-09-21 20:16:37 +0000840 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000841 __tree_iterator& operator++() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000842 __ptr_ = static_cast<__iter_pointer>(
843 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000844 return *this;
845 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847 __tree_iterator operator++(int)
848 {__tree_iterator __t(*this); ++(*this); return __t;}
849
Howard Hinnant333f50d2010-09-21 20:16:37 +0000850 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000851 __tree_iterator& operator--() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000852 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
853 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000854 return *this;
855 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000857 __tree_iterator operator--(int)
858 {__tree_iterator __t(*this); --(*this); return __t;}
859
Louis Dionne2580fdb2018-08-03 22:36:53 +0000860 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant333f50d2010-09-21 20:16:37 +0000861 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000863 friend _LIBCPP_INLINE_VISIBILITY
864 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000865 {return !(__x == __y);}
866
867private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000869 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier7310ec82016-07-19 17:56:20 +0000870 _LIBCPP_INLINE_VISIBILITY
871 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
872 _LIBCPP_INLINE_VISIBILITY
873 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000875 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
876 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
877 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
878 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
879 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
880 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881};
882
Eric Fiselier55263482016-02-20 05:28:30 +0000883template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000884class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885{
Eric Fiselier55263482016-02-20 05:28:30 +0000886 typedef __tree_node_types<_NodePtr> _NodeTypes;
887 typedef typename _NodeTypes::__node_pointer __node_pointer;
888 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000889 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
890 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000891 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892
Eric Fiselier7310ec82016-07-19 17:56:20 +0000893 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000895public:
Eric Fiselier55263482016-02-20 05:28:30 +0000896 typedef bidirectional_iterator_tag iterator_category;
897 typedef _Tp value_type;
898 typedef _DiffType difference_type;
899 typedef const value_type& reference;
900 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000901
Marshall Clow051c8482013-08-08 21:52:50 +0000902 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
903#if _LIBCPP_STD_VER > 11
904 : __ptr_(nullptr)
905#endif
906 {}
907
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908private:
Eric Fiselier55263482016-02-20 05:28:30 +0000909 typedef __tree_iterator<value_type, __node_pointer, difference_type>
910 __non_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000913 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
914 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000915
Eric Fiselier7310ec82016-07-19 17:56:20 +0000916 _LIBCPP_INLINE_VISIBILITY reference operator*() const
917 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000918 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000919 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920
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>(
924 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_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 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000933 __tree_const_iterator& operator--() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000934 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
935 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000936 return *this;
937 }
938
Howard Hinnant333f50d2010-09-21 20:16:37 +0000939 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940 __tree_const_iterator operator--(int)
941 {__tree_const_iterator __t(*this); --(*this); return __t;}
942
Howard Hinnant333f50d2010-09-21 20:16:37 +0000943 friend _LIBCPP_INLINE_VISIBILITY
944 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000946 friend _LIBCPP_INLINE_VISIBILITY
947 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000948 {return !(__x == __y);}
949
950private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000951 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000952 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
953 : __ptr_(__p) {}
Eric Fiselier7310ec82016-07-19 17:56:20 +0000954 _LIBCPP_INLINE_VISIBILITY
955 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
956 : __ptr_(__p) {}
957 _LIBCPP_INLINE_VISIBILITY
958 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
959
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000961 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
962 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
963 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
964 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
965 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000966
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967};
968
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000969#ifndef _LIBCPP_CXX03_LANG
970template <class _Tp, class _Compare, class _Allocator>
971struct __diagnose_tree_helper {
972 static constexpr bool __trigger_diagnostics()
Eric Fiseliereaf29202017-01-13 22:42:53 +0000973 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000974 "the specified comparator type does not provide a const call operator")
975 { return true; }
976};
977
978template <class _Key, class _Value, class _KeyComp, class _Alloc>
979struct __diagnose_tree_helper<
980 __value_type<_Key, _Value>,
981 __map_value_compare<_Key, __value_type<_Key, _Value>, _KeyComp>,
982 _Alloc
Eric Fiseliereaf29202017-01-13 22:42:53 +0000983> : __diagnose_tree_helper<_Key, _KeyComp, _Alloc>
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000984{
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000985};
Eric Fiseliereaf29202017-01-13 22:42:53 +0000986#endif // !_LIBCPP_CXX03_LANG
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000987
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988template <class _Tp, class _Compare, class _Allocator>
989class __tree
990{
991public:
992 typedef _Tp value_type;
993 typedef _Compare value_compare;
994 typedef _Allocator allocator_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000995
996private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier55263482016-02-20 05:28:30 +0000998 typedef typename __make_tree_node_types<value_type,
999 typename __alloc_traits::void_pointer>::type
1000 _NodeTypes;
Eric Fiselierdb215062016-03-31 02:15:15 +00001001 typedef typename _NodeTypes::key_type key_type;
Eric Fiselier55263482016-02-20 05:28:30 +00001002public:
1003 typedef typename _NodeTypes::__node_value_type __node_value_type;
1004 typedef typename _NodeTypes::__container_value_type __container_value_type;
1005
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001006 typedef typename __alloc_traits::pointer pointer;
1007 typedef typename __alloc_traits::const_pointer const_pointer;
1008 typedef typename __alloc_traits::size_type size_type;
1009 typedef typename __alloc_traits::difference_type difference_type;
1010
Eric Fiselier55263482016-02-20 05:28:30 +00001011public:
1012 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +00001013
Eric Fiselier55263482016-02-20 05:28:30 +00001014 typedef typename _NodeTypes::__node_type __node;
1015 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +00001016
1017 typedef typename _NodeTypes::__node_base_type __node_base;
1018 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +00001019
1020 typedef typename _NodeTypes::__end_node_type __end_node_t;
1021 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiselier55263482016-02-20 05:28:30 +00001022
Eric Fiselier7310ec82016-07-19 17:56:20 +00001023 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
1024 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
1025
Marshall Clow66302c62015-04-07 05:21:38 +00001026 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiselier55263482016-02-20 05:28:30 +00001027 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028
Eric Fiselier55263482016-02-20 05:28:30 +00001029private:
1030 // check for sane allocator pointer rebinding semantics. Rebinding the
1031 // allocator for a new pointer type should be exactly the same as rebinding
1032 // the pointer using 'pointer_traits'.
1033 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1034 "Allocator does not rebind pointers in a sane manner.");
1035 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1036 __node_base_allocator;
1037 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1038 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1039 "Allocator does not rebind pointers in a sane manner.");
1040
1041private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001042 __iter_pointer __begin_node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1044 __compressed_pair<size_type, value_compare> __pair3_;
1045
1046public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001047 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001048 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001050 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001051 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1052 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001054 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001055 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001057 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001058 pointer_traits<__end_node_ptr>::pointer_to(
1059 const_cast<__end_node_t&>(__pair1_.first())
1060 )
1061 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001064 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001067 const __node_allocator& __node_alloc() const _NOEXCEPT
1068 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001069 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001070 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001071 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001072 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001075 allocator_type __alloc() const _NOEXCEPT
1076 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001079 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001081 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001082 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001084 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001085 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001086 const value_compare& value_comp() const _NOEXCEPT
1087 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088public:
Eric Fiselier227b47c2016-02-20 07:12:17 +00001089
Howard Hinnant333f50d2010-09-21 20:16:37 +00001090 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +00001091 __node_pointer __root() const _NOEXCEPT
1092 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093
Eric Fiselier7310ec82016-07-19 17:56:20 +00001094 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1095 return _VSTD::addressof(__end_node()->__left_);
1096 }
1097
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +00001099 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100
Howard Hinnant7686add2011-06-04 14:31:57 +00001101 explicit __tree(const value_compare& __comp)
1102 _NOEXCEPT_(
1103 is_nothrow_default_constructible<__node_allocator>::value &&
1104 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105 explicit __tree(const allocator_type& __a);
1106 __tree(const value_compare& __comp, const allocator_type& __a);
1107 __tree(const __tree& __t);
1108 __tree& operator=(const __tree& __t);
1109 template <class _InputIterator>
1110 void __assign_unique(_InputIterator __first, _InputIterator __last);
1111 template <class _InputIterator>
1112 void __assign_multi(_InputIterator __first, _InputIterator __last);
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001113#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7686add2011-06-04 14:31:57 +00001114 __tree(__tree&& __t)
1115 _NOEXCEPT_(
1116 is_nothrow_move_constructible<__node_allocator>::value &&
1117 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +00001119 __tree& operator=(__tree&& __t)
1120 _NOEXCEPT_(
1121 __node_traits::propagate_on_container_move_assignment::value &&
1122 is_nothrow_move_assignable<value_compare>::value &&
1123 is_nothrow_move_assignable<__node_allocator>::value);
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001124#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125
1126 ~__tree();
1127
Howard Hinnant333f50d2010-09-21 20:16:37 +00001128 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001129 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001130 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001131 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001133 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001135 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001136
Howard Hinnant333f50d2010-09-21 20:16:37 +00001137 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001138 size_type max_size() const _NOEXCEPT
Eric Fiselieref3060e2016-11-23 01:18:56 +00001139 {return std::min<size_type>(
1140 __node_traits::max_size(__node_alloc()),
1141 numeric_limits<difference_type >::max());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001142
Howard Hinnant7686add2011-06-04 14:31:57 +00001143 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001144
Howard Hinnant7686add2011-06-04 14:31:57 +00001145 void swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001146#if _LIBCPP_STD_VER <= 11
Howard Hinnant7686add2011-06-04 14:31:57 +00001147 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001148 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001149 && (!__node_traits::propagate_on_container_swap::value ||
1150 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001151 );
Dimitry Andric1421cf02016-08-27 19:32:03 +00001152#else
1153 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1154#endif
Eric Fiselierdb215062016-03-31 02:15:15 +00001155
1156#ifndef _LIBCPP_CXX03_LANG
1157 template <class _Key, class ..._Args>
1158 pair<iterator, bool>
1159 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1160 template <class _Key, class ..._Args>
1161 iterator
1162 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163
1164 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001165 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierdb215062016-03-31 02:15:15 +00001166
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001168 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169
Eric Fiselierdb215062016-03-31 02:15:15 +00001170 template <class... _Args>
1171 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001172
Eric Fiselierdb215062016-03-31 02:15:15 +00001173 template <class... _Args>
1174 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001175
1176 template <class _Pp>
1177 _LIBCPP_INLINE_VISIBILITY
1178 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1179 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1180 __can_extract_key<_Pp, key_type>());
1181 }
1182
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001183 template <class _First, class _Second>
1184 _LIBCPP_INLINE_VISIBILITY
1185 typename enable_if<
1186 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1187 pair<iterator, bool>
1188 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1189 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1190 _VSTD::forward<_Second>(__s));
1191 }
1192
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001193 template <class... _Args>
1194 _LIBCPP_INLINE_VISIBILITY
1195 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1196 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1197 }
1198
1199 template <class _Pp>
1200 _LIBCPP_INLINE_VISIBILITY
1201 pair<iterator, bool>
1202 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1203 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1204 }
1205
1206 template <class _Pp>
1207 _LIBCPP_INLINE_VISIBILITY
1208 pair<iterator, bool>
1209 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1210 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1211 }
1212
1213 template <class _Pp>
1214 _LIBCPP_INLINE_VISIBILITY
1215 pair<iterator, bool>
1216 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1217 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1218 }
1219
1220 template <class _Pp>
1221 _LIBCPP_INLINE_VISIBILITY
1222 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1223 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1224 __can_extract_key<_Pp, key_type>());
1225 }
1226
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001227 template <class _First, class _Second>
1228 _LIBCPP_INLINE_VISIBILITY
1229 typename enable_if<
1230 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1231 iterator
1232 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1233 return __emplace_hint_unique_key_args(__p, __f,
1234 _VSTD::forward<_First>(__f),
1235 _VSTD::forward<_Second>(__s));
1236 }
1237
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001238 template <class... _Args>
1239 _LIBCPP_INLINE_VISIBILITY
1240 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1241 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1242 }
1243
1244 template <class _Pp>
1245 _LIBCPP_INLINE_VISIBILITY
1246 iterator
1247 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1248 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1249 }
1250
1251 template <class _Pp>
1252 _LIBCPP_INLINE_VISIBILITY
1253 iterator
1254 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1255 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1256 }
1257
1258 template <class _Pp>
1259 _LIBCPP_INLINE_VISIBILITY
1260 iterator
1261 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1262 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1263 }
1264
Eric Fiselierdb215062016-03-31 02:15:15 +00001265#else
1266 template <class _Key, class _Args>
1267 _LIBCPP_INLINE_VISIBILITY
1268 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1269 template <class _Key, class _Args>
1270 _LIBCPP_INLINE_VISIBILITY
1271 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001272#endif
1273
Eric Fiselierdb215062016-03-31 02:15:15 +00001274 _LIBCPP_INLINE_VISIBILITY
1275 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1276 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1277 }
1278
1279 _LIBCPP_INLINE_VISIBILITY
1280 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1281 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1282 }
1283
1284#ifdef _LIBCPP_CXX03_LANG
1285 _LIBCPP_INLINE_VISIBILITY
1286 iterator __insert_multi(const __container_value_type& __v);
1287 _LIBCPP_INLINE_VISIBILITY
1288 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1289#else
1290 _LIBCPP_INLINE_VISIBILITY
1291 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1292 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1293 }
1294
1295 _LIBCPP_INLINE_VISIBILITY
1296 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1297 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1298 }
1299
1300 template <class _Vp, class = typename enable_if<
1301 !is_same<typename __unconstref<_Vp>::type,
1302 __container_value_type
1303 >::value
1304 >::type>
1305 _LIBCPP_INLINE_VISIBILITY
1306 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1307 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1308 }
1309
1310 template <class _Vp, class = typename enable_if<
1311 !is_same<typename __unconstref<_Vp>::type,
1312 __container_value_type
1313 >::value
1314 >::type>
1315 _LIBCPP_INLINE_VISIBILITY
1316 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1317 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1318 }
1319
1320 _LIBCPP_INLINE_VISIBILITY
1321 iterator __insert_multi(__container_value_type&& __v) {
1322 return __emplace_multi(_VSTD::move(__v));
1323 }
1324
1325 _LIBCPP_INLINE_VISIBILITY
1326 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1327 return __emplace_hint_multi(__p, _VSTD::move(__v));
1328 }
1329
1330 template <class _Vp>
1331 _LIBCPP_INLINE_VISIBILITY
1332 iterator __insert_multi(_Vp&& __v) {
1333 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1334 }
1335
1336 template <class _Vp>
1337 _LIBCPP_INLINE_VISIBILITY
1338 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1339 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1340 }
1341
1342#endif // !_LIBCPP_CXX03_LANG
1343
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001344 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1345 iterator __node_insert_unique(const_iterator __p,
1346 __node_pointer __nd);
1347
1348 iterator __node_insert_multi(__node_pointer __nd);
1349 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1350
Erik Pilkington36fc7372018-08-01 01:33:38 +00001351
1352 _LIBCPP_INLINE_VISIBILITY iterator __remove_node_pointer(__node_pointer);
1353
1354#if _LIBCPP_STD_VER > 14
1355 template <class _NodeHandle, class _InsertReturnType>
1356 _LIBCPP_INLINE_VISIBILITY
1357 _InsertReturnType __node_handle_insert_unique(_NodeHandle&&);
1358 template <class _NodeHandle>
1359 _LIBCPP_INLINE_VISIBILITY
1360 iterator __node_handle_insert_unique(const_iterator, _NodeHandle&&);
1361
1362 template <class _NodeHandle>
1363 _LIBCPP_INLINE_VISIBILITY
1364 iterator __node_handle_insert_multi(_NodeHandle&&);
1365 template <class _NodeHandle>
1366 _LIBCPP_INLINE_VISIBILITY
1367 iterator __node_handle_insert_multi(const_iterator, _NodeHandle&&);
1368
1369
1370 template <class _NodeHandle>
1371 _LIBCPP_INLINE_VISIBILITY
1372 _NodeHandle __node_handle_extract(key_type const&);
1373 template <class _NodeHandle>
1374 _LIBCPP_INLINE_VISIBILITY
1375 _NodeHandle __node_handle_extract(const_iterator);
1376#endif
1377
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378 iterator erase(const_iterator __p);
1379 iterator erase(const_iterator __f, const_iterator __l);
1380 template <class _Key>
1381 size_type __erase_unique(const _Key& __k);
1382 template <class _Key>
1383 size_type __erase_multi(const _Key& __k);
1384
Eric Fiselier7310ec82016-07-19 17:56:20 +00001385 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001386 __node_base_pointer& __child,
1387 __node_base_pointer __new_node);
1388
1389 template <class _Key>
1390 iterator find(const _Key& __v);
1391 template <class _Key>
1392 const_iterator find(const _Key& __v) const;
1393
1394 template <class _Key>
1395 size_type __count_unique(const _Key& __k) const;
1396 template <class _Key>
1397 size_type __count_multi(const _Key& __k) const;
1398
1399 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001400 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 iterator lower_bound(const _Key& __v)
1402 {return __lower_bound(__v, __root(), __end_node());}
1403 template <class _Key>
1404 iterator __lower_bound(const _Key& __v,
1405 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001406 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001407 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409 const_iterator lower_bound(const _Key& __v) const
1410 {return __lower_bound(__v, __root(), __end_node());}
1411 template <class _Key>
1412 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001413 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001414 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001415 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001416 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 iterator upper_bound(const _Key& __v)
1418 {return __upper_bound(__v, __root(), __end_node());}
1419 template <class _Key>
1420 iterator __upper_bound(const _Key& __v,
1421 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001422 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001423 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001424 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425 const_iterator upper_bound(const _Key& __v) const
1426 {return __upper_bound(__v, __root(), __end_node());}
1427 template <class _Key>
1428 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001429 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001430 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431 template <class _Key>
1432 pair<iterator, iterator>
1433 __equal_range_unique(const _Key& __k);
1434 template <class _Key>
1435 pair<const_iterator, const_iterator>
1436 __equal_range_unique(const _Key& __k) const;
1437
1438 template <class _Key>
1439 pair<iterator, iterator>
1440 __equal_range_multi(const _Key& __k);
1441 template <class _Key>
1442 pair<const_iterator, const_iterator>
1443 __equal_range_multi(const _Key& __k) const;
1444
Howard Hinnant99968442011-11-29 18:15:50 +00001445 typedef __tree_node_destructor<__node_allocator> _Dp;
1446 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001447
Howard Hinnant8b537682011-06-04 17:10:24 +00001448 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001450 __node_base_pointer&
1451 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1452 __node_base_pointer&
1453 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1454 __node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001455 __find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001456 __parent_pointer& __parent, const key_type& __v);
Eric Fiselier856712b2017-01-05 06:06:18 +00001457 // FIXME: Make this function const qualified. Unfortunetly doing so
1458 // breaks existing code which uses non-const callable comparators.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001460 __node_base_pointer&
1461 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 template <class _Key>
Eric Fiselier856712b2017-01-05 06:06:18 +00001463 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1464 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1465 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1466 }
1467 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001468 __node_base_pointer&
1469 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1470 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471 const _Key& __v);
1472
Eric Fiselierdb215062016-03-31 02:15:15 +00001473#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001474 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001475 __node_holder __construct_node(_Args&& ...__args);
1476#else
1477 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478#endif
1479
Howard Hinnant7686add2011-06-04 14:31:57 +00001480 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481
Howard Hinnant333f50d2010-09-21 20:16:37 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001483 void __copy_assign_alloc(const __tree& __t)
1484 {__copy_assign_alloc(__t, integral_constant<bool,
1485 __node_traits::propagate_on_container_copy_assignment::value>());}
1486
Howard Hinnant333f50d2010-09-21 20:16:37 +00001487 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001488 void __copy_assign_alloc(const __tree& __t, true_type)
Marshall Clow546498c2016-08-17 23:24:02 +00001489 {
1490 if (__node_alloc() != __t.__node_alloc())
Louis Dionne2580fdb2018-08-03 22:36:53 +00001491 clear();
Marshall Clow546498c2016-08-17 23:24:02 +00001492 __node_alloc() = __t.__node_alloc();
1493 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001494 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001495 void __copy_assign_alloc(const __tree&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001496
1497 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001498 void __move_assign(__tree& __t, true_type)
1499 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1500 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001501
Howard Hinnant333f50d2010-09-21 20:16:37 +00001502 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001504 _NOEXCEPT_(
1505 !__node_traits::propagate_on_container_move_assignment::value ||
1506 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001507 {__move_assign_alloc(__t, integral_constant<bool,
1508 __node_traits::propagate_on_container_move_assignment::value>());}
1509
Howard Hinnant333f50d2010-09-21 20:16:37 +00001510 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001512 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001513 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001514 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001515 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001517 __node_pointer __detach();
1518 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001519
Eric Fiselierc3589a82017-01-04 23:56:00 +00001520 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1521 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001522};
1523
1524template <class _Tp, class _Compare, class _Allocator>
1525__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001526 _NOEXCEPT_(
1527 is_nothrow_default_constructible<__node_allocator>::value &&
1528 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 : __pair3_(0, __comp)
1530{
1531 __begin_node() = __end_node();
1532}
1533
1534template <class _Tp, class _Compare, class _Allocator>
1535__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001536 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001537 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001538 __pair3_(0)
1539{
1540 __begin_node() = __end_node();
1541}
1542
1543template <class _Tp, class _Compare, class _Allocator>
1544__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1545 const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001546 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001547 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001548 __pair3_(0, __comp)
1549{
1550 __begin_node() = __end_node();
1551}
1552
1553// Precondition: size() != 0
1554template <class _Tp, class _Compare, class _Allocator>
1555typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1556__tree<_Tp, _Compare, _Allocator>::__detach()
1557{
Eric Fiselier7310ec82016-07-19 17:56:20 +00001558 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001559 __begin_node() = __end_node();
1560 __end_node()->__left_->__parent_ = nullptr;
1561 __end_node()->__left_ = nullptr;
1562 size() = 0;
1563 // __cache->__left_ == nullptr
1564 if (__cache->__right_ != nullptr)
1565 __cache = static_cast<__node_pointer>(__cache->__right_);
1566 // __cache->__left_ == nullptr
1567 // __cache->__right_ == nullptr
1568 return __cache;
1569}
1570
1571// Precondition: __cache != nullptr
1572// __cache->left_ == nullptr
1573// __cache->right_ == nullptr
1574// This is no longer a red-black tree
1575template <class _Tp, class _Compare, class _Allocator>
1576typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1577__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1578{
1579 if (__cache->__parent_ == nullptr)
1580 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001581 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582 {
1583 __cache->__parent_->__left_ = nullptr;
1584 __cache = static_cast<__node_pointer>(__cache->__parent_);
1585 if (__cache->__right_ == nullptr)
1586 return __cache;
1587 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1588 }
1589 // __cache is right child
Eric Fiselier7310ec82016-07-19 17:56:20 +00001590 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591 __cache = static_cast<__node_pointer>(__cache->__parent_);
1592 if (__cache->__left_ == nullptr)
1593 return __cache;
1594 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1595}
1596
1597template <class _Tp, class _Compare, class _Allocator>
1598__tree<_Tp, _Compare, _Allocator>&
1599__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1600{
1601 if (this != &__t)
1602 {
1603 value_comp() = __t.value_comp();
1604 __copy_assign_alloc(__t);
1605 __assign_multi(__t.begin(), __t.end());
1606 }
1607 return *this;
1608}
1609
1610template <class _Tp, class _Compare, class _Allocator>
1611template <class _InputIterator>
1612void
1613__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1614{
Eric Fiselierdb215062016-03-31 02:15:15 +00001615 typedef iterator_traits<_InputIterator> _ITraits;
1616 typedef typename _ITraits::value_type _ItValueType;
1617 static_assert((is_same<_ItValueType, __container_value_type>::value),
1618 "__assign_unique may only be called with the containers value type");
1619
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001620 if (size() != 0)
1621 {
1622 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001623#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624 try
1625 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001626#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 for (; __cache != nullptr && __first != __last; ++__first)
1628 {
1629 __cache->__value_ = *__first;
1630 __node_pointer __next = __detach(__cache);
1631 __node_insert_unique(__cache);
1632 __cache = __next;
1633 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001634#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635 }
1636 catch (...)
1637 {
1638 while (__cache->__parent_ != nullptr)
1639 __cache = static_cast<__node_pointer>(__cache->__parent_);
1640 destroy(__cache);
1641 throw;
1642 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001643#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001644 if (__cache != nullptr)
1645 {
1646 while (__cache->__parent_ != nullptr)
1647 __cache = static_cast<__node_pointer>(__cache->__parent_);
1648 destroy(__cache);
1649 }
1650 }
1651 for (; __first != __last; ++__first)
1652 __insert_unique(*__first);
1653}
1654
1655template <class _Tp, class _Compare, class _Allocator>
1656template <class _InputIterator>
1657void
1658__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1659{
Eric Fiselierdb215062016-03-31 02:15:15 +00001660 typedef iterator_traits<_InputIterator> _ITraits;
1661 typedef typename _ITraits::value_type _ItValueType;
1662 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1663 is_same<_ItValueType, __node_value_type>::value),
1664 "__assign_multi may only be called with the containers value type"
1665 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001666 if (size() != 0)
1667 {
1668 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001669#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 try
1671 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001672#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001673 for (; __cache != nullptr && __first != __last; ++__first)
1674 {
1675 __cache->__value_ = *__first;
1676 __node_pointer __next = __detach(__cache);
1677 __node_insert_multi(__cache);
1678 __cache = __next;
1679 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001680#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 }
1682 catch (...)
1683 {
1684 while (__cache->__parent_ != nullptr)
1685 __cache = static_cast<__node_pointer>(__cache->__parent_);
1686 destroy(__cache);
1687 throw;
1688 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001689#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690 if (__cache != nullptr)
1691 {
1692 while (__cache->__parent_ != nullptr)
1693 __cache = static_cast<__node_pointer>(__cache->__parent_);
1694 destroy(__cache);
1695 }
1696 }
1697 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001698 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001699}
1700
1701template <class _Tp, class _Compare, class _Allocator>
1702__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001703 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001704 __pair1_(__second_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705 __pair3_(0, __t.value_comp())
1706{
1707 __begin_node() = __end_node();
1708}
1709
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001710#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711
1712template <class _Tp, class _Compare, class _Allocator>
1713__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001714 _NOEXCEPT_(
1715 is_nothrow_move_constructible<__node_allocator>::value &&
1716 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001717 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1718 __pair1_(_VSTD::move(__t.__pair1_)),
1719 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001720{
1721 if (size() == 0)
1722 __begin_node() = __end_node();
1723 else
1724 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001725 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001726 __t.__begin_node() = __t.__end_node();
1727 __t.__end_node()->__left_ = nullptr;
1728 __t.size() = 0;
1729 }
1730}
1731
1732template <class _Tp, class _Compare, class _Allocator>
1733__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
Eric Fiselier161ccc12017-04-13 00:34:24 +00001734 : __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001735 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736{
1737 if (__a == __t.__alloc())
1738 {
1739 if (__t.size() == 0)
1740 __begin_node() = __end_node();
1741 else
1742 {
1743 __begin_node() = __t.__begin_node();
1744 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +00001745 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746 size() = __t.size();
1747 __t.__begin_node() = __t.__end_node();
1748 __t.__end_node()->__left_ = nullptr;
1749 __t.size() = 0;
1750 }
1751 }
1752 else
1753 {
1754 __begin_node() = __end_node();
1755 }
1756}
1757
1758template <class _Tp, class _Compare, class _Allocator>
1759void
1760__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001761 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1762 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763{
1764 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1765 __begin_node_ = __t.__begin_node_;
1766 __pair1_.first() = __t.__pair1_.first();
1767 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001768 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 if (size() == 0)
1770 __begin_node() = __end_node();
1771 else
1772 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001773 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 __t.__begin_node() = __t.__end_node();
1775 __t.__end_node()->__left_ = nullptr;
1776 __t.size() = 0;
1777 }
1778}
1779
1780template <class _Tp, class _Compare, class _Allocator>
1781void
1782__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1783{
1784 if (__node_alloc() == __t.__node_alloc())
1785 __move_assign(__t, true_type());
1786 else
1787 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001788 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 const_iterator __e = end();
1790 if (size() != 0)
1791 {
1792 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001793#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 try
1795 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001796#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001797 while (__cache != nullptr && __t.size() != 0)
1798 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001799 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001800 __node_pointer __next = __detach(__cache);
1801 __node_insert_multi(__cache);
1802 __cache = __next;
1803 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001804#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 }
1806 catch (...)
1807 {
1808 while (__cache->__parent_ != nullptr)
1809 __cache = static_cast<__node_pointer>(__cache->__parent_);
1810 destroy(__cache);
1811 throw;
1812 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001813#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001814 if (__cache != nullptr)
1815 {
1816 while (__cache->__parent_ != nullptr)
1817 __cache = static_cast<__node_pointer>(__cache->__parent_);
1818 destroy(__cache);
1819 }
1820 }
1821 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001822 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823 }
1824}
1825
1826template <class _Tp, class _Compare, class _Allocator>
1827__tree<_Tp, _Compare, _Allocator>&
1828__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001829 _NOEXCEPT_(
1830 __node_traits::propagate_on_container_move_assignment::value &&
1831 is_nothrow_move_assignable<value_compare>::value &&
1832 is_nothrow_move_assignable<__node_allocator>::value)
Louis Dionne2580fdb2018-08-03 22:36:53 +00001833
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834{
1835 __move_assign(__t, integral_constant<bool,
1836 __node_traits::propagate_on_container_move_assignment::value>());
1837 return *this;
1838}
1839
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001840#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841
1842template <class _Tp, class _Compare, class _Allocator>
1843__tree<_Tp, _Compare, _Allocator>::~__tree()
1844{
Marshall Clow1a933122016-06-30 22:05:45 +00001845 static_assert((is_copy_constructible<value_compare>::value),
1846 "Comparator must be copy-constructible.");
Eric Fiselierebaf7da2017-01-13 22:02:08 +00001847#ifndef _LIBCPP_CXX03_LANG
1848 static_assert((__diagnose_tree_helper<_Tp, _Compare, _Allocator>::
1849 __trigger_diagnostics()), "");
1850#endif
1851 destroy(__root());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852}
1853
1854template <class _Tp, class _Compare, class _Allocator>
1855void
Howard Hinnant7686add2011-06-04 14:31:57 +00001856__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001857{
1858 if (__nd != nullptr)
1859 {
1860 destroy(static_cast<__node_pointer>(__nd->__left_));
1861 destroy(static_cast<__node_pointer>(__nd->__right_));
1862 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001863 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864 __node_traits::deallocate(__na, __nd, 1);
1865 }
1866}
1867
1868template <class _Tp, class _Compare, class _Allocator>
1869void
1870__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001871#if _LIBCPP_STD_VER <= 11
Marshall Clow7d914d12015-07-13 20:04:56 +00001872 _NOEXCEPT_(
1873 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001874 && (!__node_traits::propagate_on_container_swap::value ||
1875 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001876 )
Dimitry Andric1421cf02016-08-27 19:32:03 +00001877#else
1878 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1879#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001880{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001881 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001882 swap(__begin_node_, __t.__begin_node_);
1883 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001884 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001885 __pair3_.swap(__t.__pair3_);
1886 if (size() == 0)
1887 __begin_node() = __end_node();
1888 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001889 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 if (__t.size() == 0)
1891 __t.__begin_node() = __t.__end_node();
1892 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001893 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001894}
1895
1896template <class _Tp, class _Compare, class _Allocator>
1897void
Howard Hinnant7686add2011-06-04 14:31:57 +00001898__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001899{
1900 destroy(__root());
1901 size() = 0;
1902 __begin_node() = __end_node();
1903 __end_node()->__left_ = nullptr;
1904}
1905
1906// Find lower_bound place to insert
1907// Set __parent to parent of null leaf
1908// Return reference to null leaf
1909template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001910typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1911__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001912 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913{
1914 __node_pointer __nd = __root();
1915 if (__nd != nullptr)
1916 {
1917 while (true)
1918 {
1919 if (value_comp()(__nd->__value_, __v))
1920 {
1921 if (__nd->__right_ != nullptr)
1922 __nd = static_cast<__node_pointer>(__nd->__right_);
1923 else
1924 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001925 __parent = static_cast<__parent_pointer>(__nd);
1926 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 }
1928 }
1929 else
1930 {
1931 if (__nd->__left_ != nullptr)
1932 __nd = static_cast<__node_pointer>(__nd->__left_);
1933 else
1934 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001935 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001936 return __parent->__left_;
1937 }
1938 }
1939 }
1940 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001941 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 return __parent->__left_;
1943}
1944
1945// Find upper_bound place to insert
1946// Set __parent to parent of null leaf
1947// Return reference to null leaf
1948template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001949typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1950__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001951 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001952{
1953 __node_pointer __nd = __root();
1954 if (__nd != nullptr)
1955 {
1956 while (true)
1957 {
1958 if (value_comp()(__v, __nd->__value_))
1959 {
1960 if (__nd->__left_ != nullptr)
1961 __nd = static_cast<__node_pointer>(__nd->__left_);
1962 else
1963 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001964 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965 return __parent->__left_;
1966 }
1967 }
1968 else
1969 {
1970 if (__nd->__right_ != nullptr)
1971 __nd = static_cast<__node_pointer>(__nd->__right_);
1972 else
1973 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001974 __parent = static_cast<__parent_pointer>(__nd);
1975 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 }
1977 }
1978 }
1979 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001980 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 return __parent->__left_;
1982}
1983
1984// Find leaf place to insert closest to __hint
1985// First check prior to __hint.
1986// Next check after __hint.
1987// Next do O(log N) search.
1988// Set __parent to parent of null leaf
1989// Return reference to null leaf
1990template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001991typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001992__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001993 __parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001994 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995{
1996 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1997 {
1998 // __v <= *__hint
1999 const_iterator __prior = __hint;
2000 if (__prior == begin() || !value_comp()(__v, *--__prior))
2001 {
2002 // *prev(__hint) <= __v <= *__hint
2003 if (__hint.__ptr_->__left_ == nullptr)
2004 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002005 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002006 return __parent->__left_;
2007 }
2008 else
2009 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002010 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2011 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002012 }
2013 }
2014 // __v < *prev(__hint)
2015 return __find_leaf_high(__parent, __v);
2016 }
2017 // else __v > *__hint
2018 return __find_leaf_low(__parent, __v);
2019}
2020
2021// Find place to insert if __v doesn't exist
2022// Set __parent to parent of null leaf
2023// Return reference to null leaf
2024// If __v exists, set parent to node of __v and return reference to node of __v
2025template <class _Tp, class _Compare, class _Allocator>
2026template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00002027typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
2028__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002029 const _Key& __v)
2030{
2031 __node_pointer __nd = __root();
Eric Fiselier7310ec82016-07-19 17:56:20 +00002032 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002033 if (__nd != nullptr)
2034 {
2035 while (true)
2036 {
2037 if (value_comp()(__v, __nd->__value_))
2038 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002039 if (__nd->__left_ != nullptr) {
2040 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002041 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002042 } else {
2043 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044 return __parent->__left_;
2045 }
2046 }
2047 else if (value_comp()(__nd->__value_, __v))
2048 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002049 if (__nd->__right_ != nullptr) {
2050 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002052 } else {
2053 __parent = static_cast<__parent_pointer>(__nd);
2054 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002055 }
2056 }
2057 else
2058 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002059 __parent = static_cast<__parent_pointer>(__nd);
2060 return *__nd_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002061 }
2062 }
2063 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00002064 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 return __parent->__left_;
2066}
2067
2068// Find place to insert if __v doesn't exist
2069// First check prior to __hint.
2070// Next check after __hint.
2071// Next do O(log N) search.
2072// Set __parent to parent of null leaf
2073// Return reference to null leaf
2074// If __v exists, set parent to node of __v and return reference to node of __v
2075template <class _Tp, class _Compare, class _Allocator>
2076template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00002077typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002079 __parent_pointer& __parent,
2080 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081 const _Key& __v)
2082{
2083 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2084 {
2085 // __v < *__hint
2086 const_iterator __prior = __hint;
2087 if (__prior == begin() || value_comp()(*--__prior, __v))
2088 {
2089 // *prev(__hint) < __v < *__hint
2090 if (__hint.__ptr_->__left_ == nullptr)
2091 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002092 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093 return __parent->__left_;
2094 }
2095 else
2096 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002097 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2098 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099 }
2100 }
2101 // __v <= *prev(__hint)
2102 return __find_equal(__parent, __v);
2103 }
2104 else if (value_comp()(*__hint, __v)) // check after
2105 {
2106 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00002107 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002108 if (__next == end() || value_comp()(__v, *__next))
2109 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002110 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002111 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002113 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2114 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002115 }
2116 else
2117 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002118 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002119 return __parent->__left_;
2120 }
2121 }
2122 // *next(__hint) <= __v
2123 return __find_equal(__parent, __v);
2124 }
2125 // else __v == *__hint
Eric Fiselier7310ec82016-07-19 17:56:20 +00002126 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2127 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2128 return __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129}
2130
2131template <class _Tp, class _Compare, class _Allocator>
2132void
Eric Fiselier7310ec82016-07-19 17:56:20 +00002133__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002134 __node_base_pointer& __child,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002135 __node_base_pointer __new_node)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136{
2137 __new_node->__left_ = nullptr;
2138 __new_node->__right_ = nullptr;
2139 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002140 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 __child = __new_node;
2142 if (__begin_node()->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002143 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 __tree_balance_after_insert(__end_node()->__left_, __child);
2145 ++size();
2146}
2147
Eric Fiselierdb215062016-03-31 02:15:15 +00002148#ifndef _LIBCPP_CXX03_LANG
2149template <class _Tp, class _Compare, class _Allocator>
2150template <class _Key, class... _Args>
2151pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2152__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2153#else
2154template <class _Tp, class _Compare, class _Allocator>
2155template <class _Key, class _Args>
2156pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2157__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2158#endif
2159{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002160 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002161 __node_base_pointer& __child = __find_equal(__parent, __k);
2162 __node_pointer __r = static_cast<__node_pointer>(__child);
2163 bool __inserted = false;
2164 if (__child == nullptr)
2165 {
2166#ifndef _LIBCPP_CXX03_LANG
2167 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2168#else
2169 __node_holder __h = __construct_node(__args);
2170#endif
2171 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2172 __r = __h.release();
2173 __inserted = true;
2174 }
2175 return pair<iterator, bool>(iterator(__r), __inserted);
2176}
2177
2178
2179#ifndef _LIBCPP_CXX03_LANG
2180template <class _Tp, class _Compare, class _Allocator>
2181template <class _Key, class... _Args>
2182typename __tree<_Tp, _Compare, _Allocator>::iterator
2183__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2184 const_iterator __p, _Key const& __k, _Args&&... __args)
2185#else
2186template <class _Tp, class _Compare, class _Allocator>
2187template <class _Key, class _Args>
2188typename __tree<_Tp, _Compare, _Allocator>::iterator
2189__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2190 const_iterator __p, _Key const& __k, _Args& __args)
2191#endif
2192{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002193 __parent_pointer __parent;
2194 __node_base_pointer __dummy;
2195 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierdb215062016-03-31 02:15:15 +00002196 __node_pointer __r = static_cast<__node_pointer>(__child);
2197 if (__child == nullptr)
2198 {
2199#ifndef _LIBCPP_CXX03_LANG
2200 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2201#else
2202 __node_holder __h = __construct_node(__args);
2203#endif
2204 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2205 __r = __h.release();
2206 }
2207 return iterator(__r);
2208}
2209
2210
2211#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002212
2213template <class _Tp, class _Compare, class _Allocator>
2214template <class ..._Args>
2215typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2216__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2217{
Eric Fiselierdb215062016-03-31 02:15:15 +00002218 static_assert(!__is_tree_value_type<_Args...>::value,
2219 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002220 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002221 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002222 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002223 __h.get_deleter().__value_constructed = true;
2224 return __h;
2225}
2226
Eric Fiselierdb215062016-03-31 02:15:15 +00002227
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228template <class _Tp, class _Compare, class _Allocator>
2229template <class... _Args>
2230pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002231__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002232{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002233 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002234 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002235 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2236 __node_pointer __r = static_cast<__node_pointer>(__child);
2237 bool __inserted = false;
2238 if (__child == nullptr)
2239 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002240 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 __r = __h.release();
2242 __inserted = true;
2243 }
2244 return pair<iterator, bool>(iterator(__r), __inserted);
2245}
2246
2247template <class _Tp, class _Compare, class _Allocator>
2248template <class... _Args>
2249typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002250__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002251{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002252 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002253 __parent_pointer __parent;
2254 __node_base_pointer __dummy;
2255 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 __node_pointer __r = static_cast<__node_pointer>(__child);
2257 if (__child == nullptr)
2258 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002259 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260 __r = __h.release();
2261 }
2262 return iterator(__r);
2263}
2264
2265template <class _Tp, class _Compare, class _Allocator>
2266template <class... _Args>
2267typename __tree<_Tp, _Compare, _Allocator>::iterator
2268__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2269{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002270 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002271 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002272 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002273 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274 return iterator(static_cast<__node_pointer>(__h.release()));
2275}
2276
2277template <class _Tp, class _Compare, class _Allocator>
2278template <class... _Args>
2279typename __tree<_Tp, _Compare, _Allocator>::iterator
2280__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2281 _Args&&... __args)
2282{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002283 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002284 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002285 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002286 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287 return iterator(static_cast<__node_pointer>(__h.release()));
2288}
2289
Howard Hinnant73d21a42010-09-04 23:28:19 +00002290
Eric Fiselierdb215062016-03-31 02:15:15 +00002291#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002292
2293template <class _Tp, class _Compare, class _Allocator>
2294typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002295__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296{
2297 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002298 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002299 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002301 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002302}
2303
Eric Fiselierdb215062016-03-31 02:15:15 +00002304#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002305
Eric Fiselierdb215062016-03-31 02:15:15 +00002306#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002307template <class _Tp, class _Compare, class _Allocator>
2308typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002309__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002310{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002311 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002312 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002313 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002314 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002315 return iterator(__h.release());
2316}
2317
2318template <class _Tp, class _Compare, class _Allocator>
2319typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002320__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002321{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002322 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002323 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002325 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002326 return iterator(__h.release());
2327}
Eric Fiselierdb215062016-03-31 02:15:15 +00002328#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002330template <class _Tp, class _Compare, class _Allocator>
2331pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2332__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2333{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002334 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002335 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2336 __node_pointer __r = static_cast<__node_pointer>(__child);
2337 bool __inserted = false;
2338 if (__child == nullptr)
2339 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002340 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 __r = __nd;
2342 __inserted = true;
2343 }
2344 return pair<iterator, bool>(iterator(__r), __inserted);
2345}
2346
2347template <class _Tp, class _Compare, class _Allocator>
2348typename __tree<_Tp, _Compare, _Allocator>::iterator
2349__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2350 __node_pointer __nd)
2351{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002352 __parent_pointer __parent;
2353 __node_base_pointer __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2355 __node_pointer __r = static_cast<__node_pointer>(__child);
2356 if (__child == nullptr)
2357 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002358 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002359 __r = __nd;
2360 }
2361 return iterator(__r);
2362}
2363
2364template <class _Tp, class _Compare, class _Allocator>
2365typename __tree<_Tp, _Compare, _Allocator>::iterator
2366__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2367{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002368 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002369 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002370 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371 return iterator(__nd);
2372}
2373
2374template <class _Tp, class _Compare, class _Allocator>
2375typename __tree<_Tp, _Compare, _Allocator>::iterator
2376__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2377 __node_pointer __nd)
2378{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002379 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002380 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002381 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382 return iterator(__nd);
2383}
2384
2385template <class _Tp, class _Compare, class _Allocator>
2386typename __tree<_Tp, _Compare, _Allocator>::iterator
Erik Pilkington36fc7372018-08-01 01:33:38 +00002387__tree<_Tp, _Compare, _Allocator>::__remove_node_pointer(__node_pointer __ptr)
2388{
2389 iterator __r(__ptr);
2390 ++__r;
2391 if (__begin_node() == __ptr)
2392 __begin_node() = __r.__ptr_;
2393 --size();
2394 __tree_remove(__end_node()->__left_,
2395 static_cast<__node_base_pointer>(__ptr));
2396 return __r;
2397}
2398
2399#if _LIBCPP_STD_VER > 14
2400template <class _Tp, class _Compare, class _Allocator>
2401template <class _NodeHandle, class _InsertReturnType>
2402_LIBCPP_INLINE_VISIBILITY
2403_InsertReturnType
2404__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2405 _NodeHandle&& __nh)
2406{
2407 if (__nh.empty())
2408 return _InsertReturnType{end(), false, _NodeHandle()};
2409
2410 __node_pointer __ptr = __nh.__ptr_;
2411 __parent_pointer __parent;
2412 __node_base_pointer& __child = __find_equal(__parent,
2413 __ptr->__value_);
2414 if (__child != nullptr)
2415 return _InsertReturnType{
2416 iterator(static_cast<__node_pointer>(__child)),
2417 false, _VSTD::move(__nh)};
2418
2419 __insert_node_at(__parent, __child,
2420 static_cast<__node_base_pointer>(__ptr));
2421 __nh.__release();
2422 return _InsertReturnType{iterator(__ptr), true, _NodeHandle()};
2423}
2424
2425template <class _Tp, class _Compare, class _Allocator>
2426template <class _NodeHandle>
2427_LIBCPP_INLINE_VISIBILITY
2428typename __tree<_Tp, _Compare, _Allocator>::iterator
2429__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_unique(
2430 const_iterator __hint, _NodeHandle&& __nh)
2431{
2432 if (__nh.empty())
2433 return end();
2434
2435 __node_pointer __ptr = __nh.__ptr_;
2436 __parent_pointer __parent;
2437 __node_base_pointer __dummy;
2438 __node_base_pointer& __child = __find_equal(__hint, __parent, __dummy,
2439 __ptr->__value_);
2440 __node_pointer __r = static_cast<__node_pointer>(__child);
2441 if (__child == nullptr)
2442 {
2443 __insert_node_at(__parent, __child,
2444 static_cast<__node_base_pointer>(__ptr));
2445 __r = __ptr;
2446 __nh.__release();
2447 }
2448 return iterator(__r);
2449}
2450
2451template <class _Tp, class _Compare, class _Allocator>
2452template <class _NodeHandle>
2453_LIBCPP_INLINE_VISIBILITY
2454_NodeHandle
2455__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(key_type const& __key)
2456{
2457 iterator __it = find(__key);
2458 if (__it == end())
2459 return _NodeHandle();
2460 return __node_handle_extract<_NodeHandle>(__it);
2461}
2462
2463template <class _Tp, class _Compare, class _Allocator>
2464template <class _NodeHandle>
2465_LIBCPP_INLINE_VISIBILITY
2466_NodeHandle
2467__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p)
2468{
2469 __node_pointer __np = __p.__get_np();
2470 __remove_node_pointer(__np);
2471 return _NodeHandle(__np, __alloc());
2472}
2473
2474template <class _Tp, class _Compare, class _Allocator>
2475template <class _NodeHandle>
2476_LIBCPP_INLINE_VISIBILITY
2477typename __tree<_Tp, _Compare, _Allocator>::iterator
2478__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(_NodeHandle&& __nh)
2479{
2480 if (__nh.empty())
2481 return end();
2482 __node_pointer __ptr = __nh.__ptr_;
2483 __parent_pointer __parent;
2484 __node_base_pointer& __child = __find_leaf_high(
2485 __parent, _NodeTypes::__get_key(__ptr->__value_));
2486 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2487 __nh.__release();
2488 return iterator(__ptr);
2489}
2490
2491template <class _Tp, class _Compare, class _Allocator>
2492template <class _NodeHandle>
2493_LIBCPP_INLINE_VISIBILITY
2494typename __tree<_Tp, _Compare, _Allocator>::iterator
2495__tree<_Tp, _Compare, _Allocator>::__node_handle_insert_multi(
2496 const_iterator __hint, _NodeHandle&& __nh)
2497{
2498 if (__nh.empty())
2499 return end();
2500
2501 __node_pointer __ptr = __nh.__ptr_;
2502 __parent_pointer __parent;
2503 __node_base_pointer& __child = __find_leaf(__hint, __parent,
2504 _NodeTypes::__get_key(__ptr->__value_));
2505 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__ptr));
2506 __nh.__release();
2507 return iterator(__ptr);
2508}
2509
2510#endif // _LIBCPP_STD_VER > 14
2511
2512template <class _Tp, class _Compare, class _Allocator>
2513typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002514__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2515{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002516 __node_pointer __np = __p.__get_np();
Erik Pilkington36fc7372018-08-01 01:33:38 +00002517 iterator __r = __remove_node_pointer(__np);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00002519 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2520 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002521 __node_traits::deallocate(__na, __np, 1);
2522 return __r;
2523}
2524
2525template <class _Tp, class _Compare, class _Allocator>
2526typename __tree<_Tp, _Compare, _Allocator>::iterator
2527__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2528{
2529 while (__f != __l)
2530 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002531 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002532}
2533
2534template <class _Tp, class _Compare, class _Allocator>
2535template <class _Key>
2536typename __tree<_Tp, _Compare, _Allocator>::size_type
2537__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2538{
2539 iterator __i = find(__k);
2540 if (__i == end())
2541 return 0;
2542 erase(__i);
2543 return 1;
2544}
2545
2546template <class _Tp, class _Compare, class _Allocator>
2547template <class _Key>
2548typename __tree<_Tp, _Compare, _Allocator>::size_type
2549__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2550{
2551 pair<iterator, iterator> __p = __equal_range_multi(__k);
2552 size_type __r = 0;
2553 for (; __p.first != __p.second; ++__r)
2554 __p.first = erase(__p.first);
2555 return __r;
2556}
2557
2558template <class _Tp, class _Compare, class _Allocator>
2559template <class _Key>
2560typename __tree<_Tp, _Compare, _Allocator>::iterator
2561__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2562{
2563 iterator __p = __lower_bound(__v, __root(), __end_node());
2564 if (__p != end() && !value_comp()(__v, *__p))
2565 return __p;
2566 return end();
2567}
2568
2569template <class _Tp, class _Compare, class _Allocator>
2570template <class _Key>
2571typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2572__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2573{
2574 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2575 if (__p != end() && !value_comp()(__v, *__p))
2576 return __p;
2577 return end();
2578}
2579
2580template <class _Tp, class _Compare, class _Allocator>
2581template <class _Key>
2582typename __tree<_Tp, _Compare, _Allocator>::size_type
2583__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2584{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002585 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002586 while (__rt != nullptr)
2587 {
2588 if (value_comp()(__k, __rt->__value_))
2589 {
Eric Fiselier227b47c2016-02-20 07:12:17 +00002590 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 }
2592 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002593 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002594 else
2595 return 1;
2596 }
2597 return 0;
2598}
2599
2600template <class _Tp, class _Compare, class _Allocator>
2601template <class _Key>
2602typename __tree<_Tp, _Compare, _Allocator>::size_type
2603__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2604{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002605 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002606 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002607 while (__rt != nullptr)
2608 {
2609 if (value_comp()(__k, __rt->__value_))
2610 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002611 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002612 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002613 }
2614 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002615 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002616 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002617 return _VSTD::distance(
Eric Fiselier7310ec82016-07-19 17:56:20 +00002618 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002619 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002620 );
2621 }
2622 return 0;
2623}
2624
2625template <class _Tp, class _Compare, class _Allocator>
2626template <class _Key>
2627typename __tree<_Tp, _Compare, _Allocator>::iterator
2628__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2629 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002630 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002631{
2632 while (__root != nullptr)
2633 {
2634 if (!value_comp()(__root->__value_, __v))
2635 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002636 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002637 __root = static_cast<__node_pointer>(__root->__left_);
2638 }
2639 else
2640 __root = static_cast<__node_pointer>(__root->__right_);
2641 }
2642 return iterator(__result);
2643}
2644
2645template <class _Tp, class _Compare, class _Allocator>
2646template <class _Key>
2647typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2648__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002649 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002650 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002651{
2652 while (__root != nullptr)
2653 {
2654 if (!value_comp()(__root->__value_, __v))
2655 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002656 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002657 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658 }
2659 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002660 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002661 }
2662 return const_iterator(__result);
2663}
2664
2665template <class _Tp, class _Compare, class _Allocator>
2666template <class _Key>
2667typename __tree<_Tp, _Compare, _Allocator>::iterator
2668__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2669 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002670 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671{
2672 while (__root != nullptr)
2673 {
2674 if (value_comp()(__v, __root->__value_))
2675 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002676 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002677 __root = static_cast<__node_pointer>(__root->__left_);
2678 }
2679 else
2680 __root = static_cast<__node_pointer>(__root->__right_);
2681 }
2682 return iterator(__result);
2683}
2684
2685template <class _Tp, class _Compare, class _Allocator>
2686template <class _Key>
2687typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2688__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002689 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002690 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002691{
2692 while (__root != nullptr)
2693 {
2694 if (value_comp()(__v, __root->__value_))
2695 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002696 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002697 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002698 }
2699 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002700 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002701 }
2702 return const_iterator(__result);
2703}
2704
2705template <class _Tp, class _Compare, class _Allocator>
2706template <class _Key>
2707pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2708 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2709__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2710{
Howard Hinnant99968442011-11-29 18:15:50 +00002711 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002712 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002713 __node_pointer __rt = __root();
2714 while (__rt != nullptr)
2715 {
2716 if (value_comp()(__k, __rt->__value_))
2717 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002718 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002719 __rt = static_cast<__node_pointer>(__rt->__left_);
2720 }
2721 else if (value_comp()(__rt->__value_, __k))
2722 __rt = static_cast<__node_pointer>(__rt->__right_);
2723 else
Howard Hinnant99968442011-11-29 18:15:50 +00002724 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002725 iterator(
2726 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002727 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002728 : __result));
2729 }
Howard Hinnant99968442011-11-29 18:15:50 +00002730 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002731}
2732
2733template <class _Tp, class _Compare, class _Allocator>
2734template <class _Key>
2735pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2736 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2737__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2738{
Howard Hinnant99968442011-11-29 18:15:50 +00002739 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002740 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002741 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002742 while (__rt != nullptr)
2743 {
2744 if (value_comp()(__k, __rt->__value_))
2745 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002746 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002747 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002748 }
2749 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002750 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002751 else
Howard Hinnant99968442011-11-29 18:15:50 +00002752 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002753 const_iterator(
2754 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002755 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002756 : __result));
2757 }
Howard Hinnant99968442011-11-29 18:15:50 +00002758 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002759}
2760
2761template <class _Tp, class _Compare, class _Allocator>
2762template <class _Key>
2763pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2764 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2765__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2766{
Howard Hinnant99968442011-11-29 18:15:50 +00002767 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002768 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002769 __node_pointer __rt = __root();
2770 while (__rt != nullptr)
2771 {
2772 if (value_comp()(__k, __rt->__value_))
2773 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002774 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002775 __rt = static_cast<__node_pointer>(__rt->__left_);
2776 }
2777 else if (value_comp()(__rt->__value_, __k))
2778 __rt = static_cast<__node_pointer>(__rt->__right_);
2779 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002780 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002781 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2782 }
Howard Hinnant99968442011-11-29 18:15:50 +00002783 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002784}
2785
2786template <class _Tp, class _Compare, class _Allocator>
2787template <class _Key>
2788pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2789 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2790__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2791{
Howard Hinnant99968442011-11-29 18:15:50 +00002792 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002793 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002794 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002795 while (__rt != nullptr)
2796 {
2797 if (value_comp()(__k, __rt->__value_))
2798 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002799 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002800 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002801 }
2802 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002803 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002804 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002805 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002806 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002807 }
Howard Hinnant99968442011-11-29 18:15:50 +00002808 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002809}
2810
2811template <class _Tp, class _Compare, class _Allocator>
2812typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002813__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002814{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002815 __node_pointer __np = __p.__get_np();
2816 if (__begin_node() == __p.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002817 {
2818 if (__np->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002819 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002820 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002821 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002822 }
2823 --size();
2824 __tree_remove(__end_node()->__left_,
2825 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002826 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002827}
2828
Howard Hinnant7686add2011-06-04 14:31:57 +00002829template <class _Tp, class _Compare, class _Allocator>
2830inline _LIBCPP_INLINE_VISIBILITY
2831void
2832swap(__tree<_Tp, _Compare, _Allocator>& __x,
2833 __tree<_Tp, _Compare, _Allocator>& __y)
2834 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2835{
2836 __x.swap(__y);
2837}
2838
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002839_LIBCPP_END_NAMESPACE_STD
2840
Eric Fiselier018a3d52017-05-31 22:07:49 +00002841_LIBCPP_POP_MACROS
2842
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002843#endif // _LIBCPP___TREE