blob: cb58a90781445190b8a0aaf763d5b8da344e8309 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
Howard Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000026template <class _Tp, class _Compare, class _Allocator> class __tree;
27template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000028 class _LIBCPP_TYPE_VIS_ONLY __tree_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000029template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000030 class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
32/*
33
34_NodePtr algorithms
35
36The algorithms taking _NodePtr are red black tree algorithms. Those
37algorithms taking a parameter named __root should assume that __root
38points to a proper red black tree (unless otherwise specified).
39
40Each algorithm herein assumes that __root->__parent_ points to a non-null
41structure which has a member __left_ which points back to __root. No other
42member is read or written to at __root->__parent_.
43
44__root->__parent_ will be referred to below (in comments only) as end_node.
45end_node->__left_ is an externably accessible lvalue for __root, and can be
46changed by node insertion and removal (without explicit reference to end_node).
47
48All nodes (with the exception of end_node), even the node referred to as
49__root, have a non-null __parent_ field.
50
51*/
52
53// Returns: true if __x is a left child of its parent, else false
54// Precondition: __x != nullptr.
55template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +000056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057bool
Howard Hinnant8b537682011-06-04 17:10:24 +000058__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000059{
60 return __x == __x->__parent_->__left_;
61}
62
63// Determintes if the subtree rooted at __x is a proper red black subtree. If
64// __x is a proper subtree, returns the black height (null counts as 1). If
65// __x is an improper subtree, returns 0.
66template <class _NodePtr>
67unsigned
68__tree_sub_invariant(_NodePtr __x)
69{
70 if (__x == nullptr)
71 return 1;
72 // parent consistency checked by caller
73 // check __x->__left_ consistency
74 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
75 return 0;
76 // check __x->__right_ consistency
77 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
78 return 0;
79 // check __x->__left_ != __x->__right_ unless both are nullptr
80 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
81 return 0;
82 // If this is red, neither child can be red
83 if (!__x->__is_black_)
84 {
85 if (__x->__left_ && !__x->__left_->__is_black_)
86 return 0;
87 if (__x->__right_ && !__x->__right_->__is_black_)
88 return 0;
89 }
90 unsigned __h = __tree_sub_invariant(__x->__left_);
91 if (__h == 0)
92 return 0; // invalid left subtree
93 if (__h != __tree_sub_invariant(__x->__right_))
94 return 0; // invalid or different height right subtree
95 return __h + __x->__is_black_; // return black height of this node
96}
97
98// Determintes if the red black tree rooted at __root is a proper red black tree.
99// __root == nullptr is a proper tree. Returns true is __root is a proper
100// red black tree, else returns false.
101template <class _NodePtr>
102bool
103__tree_invariant(_NodePtr __root)
104{
105 if (__root == nullptr)
106 return true;
107 // check __x->__parent_ consistency
108 if (__root->__parent_ == nullptr)
109 return false;
110 if (!__tree_is_left_child(__root))
111 return false;
112 // root must be black
113 if (!__root->__is_black_)
114 return false;
115 // do normal node checks
116 return __tree_sub_invariant(__root) != 0;
117}
118
119// Returns: pointer to the left-most node under __x.
120// Precondition: __x != nullptr.
121template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000124__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125{
126 while (__x->__left_ != nullptr)
127 __x = __x->__left_;
128 return __x;
129}
130
131// Returns: pointer to the right-most node under __x.
132// Precondition: __x != nullptr.
133template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000136__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137{
138 while (__x->__right_ != nullptr)
139 __x = __x->__right_;
140 return __x;
141}
142
143// Returns: pointer to the next in-order node after __x.
144// Precondition: __x != nullptr.
145template <class _NodePtr>
146_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000147__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148{
149 if (__x->__right_ != nullptr)
150 return __tree_min(__x->__right_);
151 while (!__tree_is_left_child(__x))
152 __x = __x->__parent_;
153 return __x->__parent_;
154}
155
156// Returns: pointer to the previous in-order node before __x.
157// Precondition: __x != nullptr.
158template <class _NodePtr>
159_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000160__tree_prev(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161{
162 if (__x->__left_ != nullptr)
163 return __tree_max(__x->__left_);
164 while (__tree_is_left_child(__x))
165 __x = __x->__parent_;
166 return __x->__parent_;
167}
168
169// Returns: pointer to a node which has no children
170// Precondition: __x != nullptr.
171template <class _NodePtr>
172_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000173__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000174{
175 while (true)
176 {
177 if (__x->__left_ != nullptr)
178 {
179 __x = __x->__left_;
180 continue;
181 }
182 if (__x->__right_ != nullptr)
183 {
184 __x = __x->__right_;
185 continue;
186 }
187 break;
188 }
189 return __x;
190}
191
192// Effects: Makes __x->__right_ the subtree root with __x as its left child
193// while preserving in-order order.
194// Precondition: __x->__right_ != nullptr
195template <class _NodePtr>
196void
Howard Hinnant8b537682011-06-04 17:10:24 +0000197__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198{
199 _NodePtr __y = __x->__right_;
200 __x->__right_ = __y->__left_;
201 if (__x->__right_ != nullptr)
202 __x->__right_->__parent_ = __x;
203 __y->__parent_ = __x->__parent_;
204 if (__tree_is_left_child(__x))
205 __x->__parent_->__left_ = __y;
206 else
207 __x->__parent_->__right_ = __y;
208 __y->__left_ = __x;
209 __x->__parent_ = __y;
210}
211
212// Effects: Makes __x->__left_ the subtree root with __x as its right child
213// while preserving in-order order.
214// Precondition: __x->__left_ != nullptr
215template <class _NodePtr>
216void
Howard Hinnant8b537682011-06-04 17:10:24 +0000217__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000218{
219 _NodePtr __y = __x->__left_;
220 __x->__left_ = __y->__right_;
221 if (__x->__left_ != nullptr)
222 __x->__left_->__parent_ = __x;
223 __y->__parent_ = __x->__parent_;
224 if (__tree_is_left_child(__x))
225 __x->__parent_->__left_ = __y;
226 else
227 __x->__parent_->__right_ = __y;
228 __y->__right_ = __x;
229 __x->__parent_ = __y;
230}
231
232// Effects: Rebalances __root after attaching __x to a leaf.
233// Precondition: __root != nulptr && __x != nullptr.
234// __x has no children.
235// __x == __root or == a direct or indirect child of __root.
236// If __x were to be unlinked from __root (setting __root to
237// nullptr if __root == __x), __tree_invariant(__root) == true.
238// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
239// may be different than the value passed in as __root.
240template <class _NodePtr>
241void
Howard Hinnant8b537682011-06-04 17:10:24 +0000242__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000243{
244 __x->__is_black_ = __x == __root;
245 while (__x != __root && !__x->__parent_->__is_black_)
246 {
247 // __x->__parent_ != __root because __x->__parent_->__is_black == false
248 if (__tree_is_left_child(__x->__parent_))
249 {
250 _NodePtr __y = __x->__parent_->__parent_->__right_;
251 if (__y != nullptr && !__y->__is_black_)
252 {
253 __x = __x->__parent_;
254 __x->__is_black_ = true;
255 __x = __x->__parent_;
256 __x->__is_black_ = __x == __root;
257 __y->__is_black_ = true;
258 }
259 else
260 {
261 if (!__tree_is_left_child(__x))
262 {
263 __x = __x->__parent_;
264 __tree_left_rotate(__x);
265 }
266 __x = __x->__parent_;
267 __x->__is_black_ = true;
268 __x = __x->__parent_;
269 __x->__is_black_ = false;
270 __tree_right_rotate(__x);
271 break;
272 }
273 }
274 else
275 {
276 _NodePtr __y = __x->__parent_->__parent_->__left_;
277 if (__y != nullptr && !__y->__is_black_)
278 {
279 __x = __x->__parent_;
280 __x->__is_black_ = true;
281 __x = __x->__parent_;
282 __x->__is_black_ = __x == __root;
283 __y->__is_black_ = true;
284 }
285 else
286 {
287 if (__tree_is_left_child(__x))
288 {
289 __x = __x->__parent_;
290 __tree_right_rotate(__x);
291 }
292 __x = __x->__parent_;
293 __x->__is_black_ = true;
294 __x = __x->__parent_;
295 __x->__is_black_ = false;
296 __tree_left_rotate(__x);
297 break;
298 }
299 }
300 }
301}
302
303// Precondition: __root != nullptr && __z != nullptr.
304// __tree_invariant(__root) == true.
305// __z == __root or == a direct or indirect child of __root.
306// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
307// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
308// nor any of its children refer to __z. end_node->__left_
309// may be different than the value passed in as __root.
310template <class _NodePtr>
311void
Howard Hinnant8b537682011-06-04 17:10:24 +0000312__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313{
314 // __z will be removed from the tree. Client still needs to destruct/deallocate it
315 // __y is either __z, or if __z has two children, __tree_next(__z).
316 // __y will have at most one child.
317 // __y will be the initial hole in the tree (make the hole at a leaf)
318 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
319 __z : __tree_next(__z);
320 // __x is __y's possibly null single child
321 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
322 // __w is __x's possibly null uncle (will become __x's sibling)
323 _NodePtr __w = nullptr;
324 // link __x to __y's parent, and find __w
325 if (__x != nullptr)
326 __x->__parent_ = __y->__parent_;
327 if (__tree_is_left_child(__y))
328 {
329 __y->__parent_->__left_ = __x;
330 if (__y != __root)
331 __w = __y->__parent_->__right_;
332 else
333 __root = __x; // __w == nullptr
334 }
335 else
336 {
337 __y->__parent_->__right_ = __x;
338 // __y can't be root if it is a right child
339 __w = __y->__parent_->__left_;
340 }
341 bool __removed_black = __y->__is_black_;
342 // If we didn't remove __z, do so now by splicing in __y for __z,
343 // but copy __z's color. This does not impact __x or __w.
344 if (__y != __z)
345 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000346 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000347 __y->__parent_ = __z->__parent_;
348 if (__tree_is_left_child(__z))
349 __y->__parent_->__left_ = __y;
350 else
351 __y->__parent_->__right_ = __y;
352 __y->__left_ = __z->__left_;
353 __y->__left_->__parent_ = __y;
354 __y->__right_ = __z->__right_;
355 if (__y->__right_ != nullptr)
356 __y->__right_->__parent_ = __y;
357 __y->__is_black_ = __z->__is_black_;
358 if (__root == __z)
359 __root = __y;
360 }
361 // There is no need to rebalance if we removed a red, or if we removed
362 // the last node.
363 if (__removed_black && __root != nullptr)
364 {
365 // Rebalance:
366 // __x has an implicit black color (transferred from the removed __y)
367 // associated with it, no matter what its color is.
368 // If __x is __root (in which case it can't be null), it is supposed
369 // to be black anyway, and if it is doubly black, then the double
370 // can just be ignored.
371 // If __x is red (in which case it can't be null), then it can absorb
372 // the implicit black just by setting its color to black.
373 // Since __y was black and only had one child (which __x points to), __x
374 // is either red with no children, else null, otherwise __y would have
375 // different black heights under left and right pointers.
376 // if (__x == __root || __x != nullptr && !__x->__is_black_)
377 if (__x != nullptr)
378 __x->__is_black_ = true;
379 else
380 {
381 // Else __x isn't root, and is "doubly black", even though it may
382 // be null. __w can not be null here, else the parent would
383 // see a black height >= 2 on the __x side and a black height
384 // of 1 on the __w side (__w must be a non-null black or a red
385 // with a non-null black child).
386 while (true)
387 {
388 if (!__tree_is_left_child(__w)) // if x is left child
389 {
390 if (!__w->__is_black_)
391 {
392 __w->__is_black_ = true;
393 __w->__parent_->__is_black_ = false;
394 __tree_left_rotate(__w->__parent_);
395 // __x is still valid
396 // reset __root only if necessary
397 if (__root == __w->__left_)
398 __root = __w;
399 // reset sibling, and it still can't be null
400 __w = __w->__left_->__right_;
401 }
402 // __w->__is_black_ is now true, __w may have null children
403 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
404 (__w->__right_ == nullptr || __w->__right_->__is_black_))
405 {
406 __w->__is_black_ = false;
407 __x = __w->__parent_;
408 // __x can no longer be null
409 if (__x == __root || !__x->__is_black_)
410 {
411 __x->__is_black_ = true;
412 break;
413 }
414 // reset sibling, and it still can't be null
415 __w = __tree_is_left_child(__x) ?
Howard Hinnant324bb032010-08-22 00:02:43 +0000416 __x->__parent_->__right_ :
417 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000418 // continue;
419 }
420 else // __w has a red child
421 {
422 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
423 {
424 // __w left child is non-null and red
425 __w->__left_->__is_black_ = true;
426 __w->__is_black_ = false;
427 __tree_right_rotate(__w);
428 // __w is known not to be root, so root hasn't changed
429 // reset sibling, and it still can't be null
430 __w = __w->__parent_;
431 }
432 // __w has a right red child, left child may be null
433 __w->__is_black_ = __w->__parent_->__is_black_;
434 __w->__parent_->__is_black_ = true;
435 __w->__right_->__is_black_ = true;
436 __tree_left_rotate(__w->__parent_);
437 break;
438 }
439 }
440 else
441 {
442 if (!__w->__is_black_)
443 {
444 __w->__is_black_ = true;
445 __w->__parent_->__is_black_ = false;
446 __tree_right_rotate(__w->__parent_);
447 // __x is still valid
448 // reset __root only if necessary
449 if (__root == __w->__right_)
450 __root = __w;
451 // reset sibling, and it still can't be null
452 __w = __w->__right_->__left_;
453 }
454 // __w->__is_black_ is now true, __w may have null children
455 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
456 (__w->__right_ == nullptr || __w->__right_->__is_black_))
457 {
458 __w->__is_black_ = false;
459 __x = __w->__parent_;
460 // __x can no longer be null
461 if (!__x->__is_black_ || __x == __root)
462 {
463 __x->__is_black_ = true;
464 break;
465 }
466 // reset sibling, and it still can't be null
467 __w = __tree_is_left_child(__x) ?
Howard Hinnant324bb032010-08-22 00:02:43 +0000468 __x->__parent_->__right_ :
469 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 // continue;
471 }
472 else // __w has a red child
473 {
474 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
475 {
476 // __w right child is non-null and red
477 __w->__right_->__is_black_ = true;
478 __w->__is_black_ = false;
479 __tree_left_rotate(__w);
480 // __w is known not to be root, so root hasn't changed
481 // reset sibling, and it still can't be null
482 __w = __w->__parent_;
483 }
484 // __w has a left red child, right child may be null
485 __w->__is_black_ = __w->__parent_->__is_black_;
486 __w->__parent_->__is_black_ = true;
487 __w->__left_->__is_black_ = true;
488 __tree_right_rotate(__w->__parent_);
489 break;
490 }
491 }
492 }
493 }
494 }
495}
496
Howard Hinnant2b1b2d42011-06-14 19:58:17 +0000497template <class _Allocator> class __map_node_destructor;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499template <class _Allocator>
500class __tree_node_destructor
501{
502 typedef _Allocator allocator_type;
503 typedef allocator_traits<allocator_type> __alloc_traits;
504 typedef typename __alloc_traits::value_type::value_type value_type;
505public:
506 typedef typename __alloc_traits::pointer pointer;
507private:
508
509 allocator_type& __na_;
510
511 __tree_node_destructor& operator=(const __tree_node_destructor&);
512
513public:
514 bool __value_constructed;
515
Howard Hinnant333f50d2010-09-21 20:16:37 +0000516 _LIBCPP_INLINE_VISIBILITY
Marshall Clow01c1c6f2015-01-28 19:54:25 +0000517 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518 : __na_(__na),
Marshall Clow01c1c6f2015-01-28 19:54:25 +0000519 __value_constructed(__val)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000520 {}
521
Howard Hinnant333f50d2010-09-21 20:16:37 +0000522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000523 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000524 {
525 if (__value_constructed)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000526 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527 if (__p)
528 __alloc_traits::deallocate(__na_, __p, 1);
529 }
530
531 template <class> friend class __map_node_destructor;
532};
533
534// node
535
536template <class _Pointer>
537class __tree_end_node
538{
539public:
540 typedef _Pointer pointer;
541 pointer __left_;
542
Howard Hinnant333f50d2010-09-21 20:16:37 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000544 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000545};
546
547template <class _VoidPtr>
548class __tree_node_base
549 : public __tree_end_node
550 <
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000551 typename __rebind_pointer<_VoidPtr, __tree_node_base<_VoidPtr> >::type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552 >
553{
554 __tree_node_base(const __tree_node_base&);
555 __tree_node_base& operator=(const __tree_node_base&);
556public:
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000557 typedef typename __rebind_pointer<_VoidPtr, __tree_node_base>::type pointer;
558 typedef typename __rebind_pointer<_VoidPtr, const __tree_node_base>::type const_pointer;
559
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560 typedef __tree_end_node<pointer> base;
561
562 pointer __right_;
563 pointer __parent_;
564 bool __is_black_;
565
Howard Hinnant333f50d2010-09-21 20:16:37 +0000566 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000567 __tree_node_base() _NOEXCEPT
568 : __right_(), __parent_(), __is_black_(false) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000569};
570
571template <class _Tp, class _VoidPtr>
572class __tree_node
573 : public __tree_node_base<_VoidPtr>
574{
575public:
576 typedef __tree_node_base<_VoidPtr> base;
577 typedef _Tp value_type;
578
579 value_type __value_;
580
Howard Hinnant73d21a42010-09-04 23:28:19 +0000581#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000582 template <class ..._Args>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 explicit __tree_node(_Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000585 : __value_(_VSTD::forward<_Args>(__args)...) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000586#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnant333f50d2010-09-21 20:16:37 +0000587 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588 explicit __tree_node(const value_type& __v)
589 : __value_(__v) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000590#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000591};
592
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000593template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
594template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595
596template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000597class _LIBCPP_TYPE_VIS_ONLY __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598{
599 typedef _NodePtr __node_pointer;
600 typedef typename pointer_traits<__node_pointer>::element_type __node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000601
602 __node_pointer __ptr_;
603
604 typedef pointer_traits<__node_pointer> __pointer_traits;
605public:
606 typedef bidirectional_iterator_tag iterator_category;
607 typedef _Tp value_type;
608 typedef _DiffType difference_type;
609 typedef value_type& reference;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000610 typedef typename __rebind_pointer<__node_pointer, value_type>::type pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
Marshall Clow051c8482013-08-08 21:52:50 +0000612 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
613#if _LIBCPP_STD_VER > 11
614 : __ptr_(nullptr)
615#endif
616 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617
Howard Hinnant333f50d2010-09-21 20:16:37 +0000618 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000619 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
620 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000621
Howard Hinnant333f50d2010-09-21 20:16:37 +0000622 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000623 __tree_iterator& operator++() {
624 __ptr_ = static_cast<__node_pointer>(
625 __tree_next(static_cast<typename __node::base::pointer>(__ptr_)));
626 return *this;
627 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000628 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629 __tree_iterator operator++(int)
630 {__tree_iterator __t(*this); ++(*this); return __t;}
631
Howard Hinnant333f50d2010-09-21 20:16:37 +0000632 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000633 __tree_iterator& operator--() {
634 __ptr_ = static_cast<__node_pointer>(
635 __tree_prev(static_cast<typename __node::base::pointer>(__ptr_)));
636 return *this;
637 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000638 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639 __tree_iterator operator--(int)
640 {__tree_iterator __t(*this); --(*this); return __t;}
641
Howard Hinnant333f50d2010-09-21 20:16:37 +0000642 friend _LIBCPP_INLINE_VISIBILITY
643 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000644 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000645 friend _LIBCPP_INLINE_VISIBILITY
646 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647 {return !(__x == __y);}
648
649private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000651 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000652 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000653 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
654 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
655 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
656 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
657 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
658 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659};
660
661template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000662class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663{
664 typedef _ConstNodePtr __node_pointer;
665 typedef typename pointer_traits<__node_pointer>::element_type __node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666
667 __node_pointer __ptr_;
668
669 typedef pointer_traits<__node_pointer> __pointer_traits;
670public:
671 typedef bidirectional_iterator_tag iterator_category;
672 typedef _Tp value_type;
673 typedef _DiffType difference_type;
674 typedef const value_type& reference;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000675 typedef typename __rebind_pointer<__node_pointer, const value_type>::type pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000676
Marshall Clow051c8482013-08-08 21:52:50 +0000677 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
678#if _LIBCPP_STD_VER > 11
679 : __ptr_(nullptr)
680#endif
681 {}
682
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683private:
684 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000685 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
686 __non_const_node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
688 __non_const_iterator;
689public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000690 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000691 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
692 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693
Howard Hinnant333f50d2010-09-21 20:16:37 +0000694 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000695 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
696 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697
Howard Hinnant333f50d2010-09-21 20:16:37 +0000698 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000699 __tree_const_iterator& operator++() {
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000700 typedef typename __rebind_pointer<__node_pointer, typename __node::base>::type
701 __node_base_pointer;
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000702 __ptr_ = static_cast<__node_pointer>(
703 __tree_next(static_cast<__node_base_pointer>(__ptr_)));
704 return *this;
705 }
706
Howard Hinnant333f50d2010-09-21 20:16:37 +0000707 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708 __tree_const_iterator operator++(int)
709 {__tree_const_iterator __t(*this); ++(*this); return __t;}
710
Howard Hinnant333f50d2010-09-21 20:16:37 +0000711 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000712 __tree_const_iterator& operator--() {
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000713 typedef typename __rebind_pointer<__node_pointer, typename __node::base>::type
714 __node_base_pointer;
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000715 __ptr_ = static_cast<__node_pointer>(
716 __tree_prev(static_cast<__node_base_pointer>(__ptr_)));
717 return *this;
718 }
719
Howard Hinnant333f50d2010-09-21 20:16:37 +0000720 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721 __tree_const_iterator operator--(int)
722 {__tree_const_iterator __t(*this); --(*this); return __t;}
723
Howard Hinnant333f50d2010-09-21 20:16:37 +0000724 friend _LIBCPP_INLINE_VISIBILITY
725 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000727 friend _LIBCPP_INLINE_VISIBILITY
728 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000729 {return !(__x == __y);}
730
731private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000732 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000733 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
734 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000735 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000736 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
737 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
738 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
739 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
740 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000741};
742
743template <class _Tp, class _Compare, class _Allocator>
744class __tree
745{
746public:
747 typedef _Tp value_type;
748 typedef _Compare value_compare;
749 typedef _Allocator allocator_type;
750 typedef allocator_traits<allocator_type> __alloc_traits;
751 typedef typename __alloc_traits::pointer pointer;
752 typedef typename __alloc_traits::const_pointer const_pointer;
753 typedef typename __alloc_traits::size_type size_type;
754 typedef typename __alloc_traits::difference_type difference_type;
755
Howard Hinnant70342b92013-06-19 21:29:40 +0000756 typedef typename __alloc_traits::void_pointer __void_pointer;
757
758 typedef __tree_node<value_type, __void_pointer> __node;
759 typedef __tree_node_base<__void_pointer> __node_base;
Marshall Clow66302c62015-04-07 05:21:38 +0000760 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761 typedef allocator_traits<__node_allocator> __node_traits;
762 typedef typename __node_traits::pointer __node_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000763 typedef typename __node_traits::pointer __node_const_pointer;
Howard Hinnantd615e472011-04-03 20:05:29 +0000764 typedef typename __node_base::pointer __node_base_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000765 typedef typename __node_base::pointer __node_base_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766private:
Howard Hinnantd615e472011-04-03 20:05:29 +0000767 typedef typename __node_base::base __end_node_t;
Eric Fiselier5cf84e02015-12-30 21:52:00 +0000768 typedef typename __rebind_pointer<__node_pointer, __end_node_t>::type
769 __end_node_ptr;
770 typedef __end_node_ptr __end_node_const_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771
772 __node_pointer __begin_node_;
773 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
774 __compressed_pair<size_type, value_compare> __pair3_;
775
776public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000778 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000779 {
780 return static_cast<__node_pointer>
781 (
782 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
783 );
784 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000785 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000786 __node_const_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787 {
788 return static_cast<__node_const_pointer>
789 (
Howard Hinnant70342b92013-06-19 21:29:40 +0000790 pointer_traits<__end_node_const_ptr>::pointer_to(const_cast<__end_node_t&>(__pair1_.first()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000791 );
792 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000793 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000794 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000795private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000796 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000797 const __node_allocator& __node_alloc() const _NOEXCEPT
798 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000800 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000802 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000804 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000805 allocator_type __alloc() const _NOEXCEPT
806 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000808 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000809 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000812 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000814 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000815 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000816 const value_compare& value_comp() const _NOEXCEPT
817 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000820 __node_pointer __root() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000821 {return static_cast<__node_pointer> (__end_node()->__left_);}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000822 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000823 __node_const_pointer __root() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000824 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
825
826 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +0000827 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000828
Howard Hinnant7686add2011-06-04 14:31:57 +0000829 explicit __tree(const value_compare& __comp)
830 _NOEXCEPT_(
831 is_nothrow_default_constructible<__node_allocator>::value &&
832 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 explicit __tree(const allocator_type& __a);
834 __tree(const value_compare& __comp, const allocator_type& __a);
835 __tree(const __tree& __t);
836 __tree& operator=(const __tree& __t);
837 template <class _InputIterator>
838 void __assign_unique(_InputIterator __first, _InputIterator __last);
839 template <class _InputIterator>
840 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000841#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-06-04 14:31:57 +0000842 __tree(__tree&& __t)
843 _NOEXCEPT_(
844 is_nothrow_move_constructible<__node_allocator>::value &&
845 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000846 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +0000847 __tree& operator=(__tree&& __t)
848 _NOEXCEPT_(
849 __node_traits::propagate_on_container_move_assignment::value &&
850 is_nothrow_move_assignable<value_compare>::value &&
851 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000852#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853
854 ~__tree();
855
Howard Hinnant333f50d2010-09-21 20:16:37 +0000856 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000857 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000858 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000859 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000861 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000862 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000863 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864
Howard Hinnant333f50d2010-09-21 20:16:37 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000866 size_type max_size() const _NOEXCEPT
867 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000868
Howard Hinnant7686add2011-06-04 14:31:57 +0000869 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870
Howard Hinnant7686add2011-06-04 14:31:57 +0000871 void swap(__tree& __t)
872 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +0000873 __is_nothrow_swappable<value_compare>::value
874#if _LIBCPP_STD_VER <= 11
875 && (!__node_traits::propagate_on_container_swap::value ||
876 __is_nothrow_swappable<__node_allocator>::value)
877#endif
878 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879
Howard Hinnant73d21a42010-09-04 23:28:19 +0000880#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
881#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882 template <class... _Args>
883 pair<iterator, bool>
884 __emplace_unique(_Args&&... __args);
885 template <class... _Args>
886 iterator
887 __emplace_multi(_Args&&... __args);
888
889 template <class... _Args>
890 iterator
891 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
892 template <class... _Args>
893 iterator
894 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000895#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896
Howard Hinnant99968442011-11-29 18:15:50 +0000897 template <class _Vp>
898 pair<iterator, bool> __insert_unique(_Vp&& __v);
899 template <class _Vp>
900 iterator __insert_unique(const_iterator __p, _Vp&& __v);
901 template <class _Vp>
902 iterator __insert_multi(_Vp&& __v);
903 template <class _Vp>
904 iterator __insert_multi(const_iterator __p, _Vp&& __v);
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +0000905#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906
907 pair<iterator, bool> __insert_unique(const value_type& __v);
908 iterator __insert_unique(const_iterator __p, const value_type& __v);
909 iterator __insert_multi(const value_type& __v);
910 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911
912 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
913 iterator __node_insert_unique(const_iterator __p,
914 __node_pointer __nd);
915
916 iterator __node_insert_multi(__node_pointer __nd);
917 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
918
919 iterator erase(const_iterator __p);
920 iterator erase(const_iterator __f, const_iterator __l);
921 template <class _Key>
922 size_type __erase_unique(const _Key& __k);
923 template <class _Key>
924 size_type __erase_multi(const _Key& __k);
925
926 void __insert_node_at(__node_base_pointer __parent,
927 __node_base_pointer& __child,
928 __node_base_pointer __new_node);
929
930 template <class _Key>
931 iterator find(const _Key& __v);
932 template <class _Key>
933 const_iterator find(const _Key& __v) const;
934
935 template <class _Key>
936 size_type __count_unique(const _Key& __k) const;
937 template <class _Key>
938 size_type __count_multi(const _Key& __k) const;
939
940 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000941 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 iterator lower_bound(const _Key& __v)
943 {return __lower_bound(__v, __root(), __end_node());}
944 template <class _Key>
945 iterator __lower_bound(const _Key& __v,
946 __node_pointer __root,
947 __node_pointer __result);
948 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000949 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000950 const_iterator lower_bound(const _Key& __v) const
951 {return __lower_bound(__v, __root(), __end_node());}
952 template <class _Key>
953 const_iterator __lower_bound(const _Key& __v,
954 __node_const_pointer __root,
955 __node_const_pointer __result) const;
956 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000957 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 iterator upper_bound(const _Key& __v)
959 {return __upper_bound(__v, __root(), __end_node());}
960 template <class _Key>
961 iterator __upper_bound(const _Key& __v,
962 __node_pointer __root,
963 __node_pointer __result);
964 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000965 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966 const_iterator upper_bound(const _Key& __v) const
967 {return __upper_bound(__v, __root(), __end_node());}
968 template <class _Key>
969 const_iterator __upper_bound(const _Key& __v,
970 __node_const_pointer __root,
971 __node_const_pointer __result) const;
972 template <class _Key>
973 pair<iterator, iterator>
974 __equal_range_unique(const _Key& __k);
975 template <class _Key>
976 pair<const_iterator, const_iterator>
977 __equal_range_unique(const _Key& __k) const;
978
979 template <class _Key>
980 pair<iterator, iterator>
981 __equal_range_multi(const _Key& __k);
982 template <class _Key>
983 pair<const_iterator, const_iterator>
984 __equal_range_multi(const _Key& __k) const;
985
Howard Hinnant99968442011-11-29 18:15:50 +0000986 typedef __tree_node_destructor<__node_allocator> _Dp;
987 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000988
Howard Hinnant8b537682011-06-04 17:10:24 +0000989 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990private:
Howard Hinnantd615e472011-04-03 20:05:29 +0000991 typename __node_base::pointer&
992 __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v);
993 typename __node_base::pointer&
994 __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v);
995 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000996 __find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +0000997 typename __node_base::pointer& __parent, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000998 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +0000999 typename __node_base::pointer&
1000 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001002 typename __node_base::pointer&
1003 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001004 const _Key& __v);
1005
Howard Hinnant73d21a42010-09-04 23:28:19 +00001006#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007 template <class ..._Args>
1008 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001009#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 __node_holder __construct_node(const value_type& __v);
1011#endif
1012
Howard Hinnant7686add2011-06-04 14:31:57 +00001013 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001014
Howard Hinnant333f50d2010-09-21 20:16:37 +00001015 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001016 void __copy_assign_alloc(const __tree& __t)
1017 {__copy_assign_alloc(__t, integral_constant<bool,
1018 __node_traits::propagate_on_container_copy_assignment::value>());}
1019
Howard Hinnant333f50d2010-09-21 20:16:37 +00001020 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001021 void __copy_assign_alloc(const __tree& __t, true_type)
1022 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001023 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001024 void __copy_assign_alloc(const __tree& __t, false_type) {}
1025
1026 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001027 void __move_assign(__tree& __t, true_type)
1028 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1029 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030
Howard Hinnant333f50d2010-09-21 20:16:37 +00001031 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001032 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001033 _NOEXCEPT_(
1034 !__node_traits::propagate_on_container_move_assignment::value ||
1035 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 {__move_assign_alloc(__t, integral_constant<bool,
1037 __node_traits::propagate_on_container_move_assignment::value>());}
1038
Howard Hinnant333f50d2010-09-21 20:16:37 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001041 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001042 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001044 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046 __node_pointer __detach();
1047 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001048
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001049 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1050 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051};
1052
1053template <class _Tp, class _Compare, class _Allocator>
1054__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001055 _NOEXCEPT_(
1056 is_nothrow_default_constructible<__node_allocator>::value &&
1057 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001058 : __pair3_(0, __comp)
1059{
1060 __begin_node() = __end_node();
1061}
1062
1063template <class _Tp, class _Compare, class _Allocator>
1064__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001065 : __begin_node_(__node_pointer()),
1066 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 __pair3_(0)
1068{
1069 __begin_node() = __end_node();
1070}
1071
1072template <class _Tp, class _Compare, class _Allocator>
1073__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1074 const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001075 : __begin_node_(__node_pointer()),
1076 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077 __pair3_(0, __comp)
1078{
1079 __begin_node() = __end_node();
1080}
1081
1082// Precondition: size() != 0
1083template <class _Tp, class _Compare, class _Allocator>
1084typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1085__tree<_Tp, _Compare, _Allocator>::__detach()
1086{
1087 __node_pointer __cache = __begin_node();
1088 __begin_node() = __end_node();
1089 __end_node()->__left_->__parent_ = nullptr;
1090 __end_node()->__left_ = nullptr;
1091 size() = 0;
1092 // __cache->__left_ == nullptr
1093 if (__cache->__right_ != nullptr)
1094 __cache = static_cast<__node_pointer>(__cache->__right_);
1095 // __cache->__left_ == nullptr
1096 // __cache->__right_ == nullptr
1097 return __cache;
1098}
1099
1100// Precondition: __cache != nullptr
1101// __cache->left_ == nullptr
1102// __cache->right_ == nullptr
1103// This is no longer a red-black tree
1104template <class _Tp, class _Compare, class _Allocator>
1105typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1106__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1107{
1108 if (__cache->__parent_ == nullptr)
1109 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001110 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 {
1112 __cache->__parent_->__left_ = nullptr;
1113 __cache = static_cast<__node_pointer>(__cache->__parent_);
1114 if (__cache->__right_ == nullptr)
1115 return __cache;
1116 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1117 }
1118 // __cache is right child
1119 __cache->__parent_->__right_ = nullptr;
1120 __cache = static_cast<__node_pointer>(__cache->__parent_);
1121 if (__cache->__left_ == nullptr)
1122 return __cache;
1123 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1124}
1125
1126template <class _Tp, class _Compare, class _Allocator>
1127__tree<_Tp, _Compare, _Allocator>&
1128__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1129{
1130 if (this != &__t)
1131 {
1132 value_comp() = __t.value_comp();
1133 __copy_assign_alloc(__t);
1134 __assign_multi(__t.begin(), __t.end());
1135 }
1136 return *this;
1137}
1138
1139template <class _Tp, class _Compare, class _Allocator>
1140template <class _InputIterator>
1141void
1142__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1143{
1144 if (size() != 0)
1145 {
1146 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001147#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 try
1149 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001150#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001151 for (; __cache != nullptr && __first != __last; ++__first)
1152 {
1153 __cache->__value_ = *__first;
1154 __node_pointer __next = __detach(__cache);
1155 __node_insert_unique(__cache);
1156 __cache = __next;
1157 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001158#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001159 }
1160 catch (...)
1161 {
1162 while (__cache->__parent_ != nullptr)
1163 __cache = static_cast<__node_pointer>(__cache->__parent_);
1164 destroy(__cache);
1165 throw;
1166 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001167#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001168 if (__cache != nullptr)
1169 {
1170 while (__cache->__parent_ != nullptr)
1171 __cache = static_cast<__node_pointer>(__cache->__parent_);
1172 destroy(__cache);
1173 }
1174 }
1175 for (; __first != __last; ++__first)
1176 __insert_unique(*__first);
1177}
1178
1179template <class _Tp, class _Compare, class _Allocator>
1180template <class _InputIterator>
1181void
1182__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1183{
1184 if (size() != 0)
1185 {
1186 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001187#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188 try
1189 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001190#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001191 for (; __cache != nullptr && __first != __last; ++__first)
1192 {
1193 __cache->__value_ = *__first;
1194 __node_pointer __next = __detach(__cache);
1195 __node_insert_multi(__cache);
1196 __cache = __next;
1197 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001198#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001199 }
1200 catch (...)
1201 {
1202 while (__cache->__parent_ != nullptr)
1203 __cache = static_cast<__node_pointer>(__cache->__parent_);
1204 destroy(__cache);
1205 throw;
1206 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001207#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001208 if (__cache != nullptr)
1209 {
1210 while (__cache->__parent_ != nullptr)
1211 __cache = static_cast<__node_pointer>(__cache->__parent_);
1212 destroy(__cache);
1213 }
1214 }
1215 for (; __first != __last; ++__first)
1216 __insert_multi(*__first);
1217}
1218
1219template <class _Tp, class _Compare, class _Allocator>
1220__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1221 : __begin_node_(__node_pointer()),
1222 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1223 __pair3_(0, __t.value_comp())
1224{
1225 __begin_node() = __end_node();
1226}
1227
Howard Hinnant73d21a42010-09-04 23:28:19 +00001228#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001229
1230template <class _Tp, class _Compare, class _Allocator>
1231__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001232 _NOEXCEPT_(
1233 is_nothrow_move_constructible<__node_allocator>::value &&
1234 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001235 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1236 __pair1_(_VSTD::move(__t.__pair1_)),
1237 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001238{
1239 if (size() == 0)
1240 __begin_node() = __end_node();
1241 else
1242 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001243 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244 __t.__begin_node() = __t.__end_node();
1245 __t.__end_node()->__left_ = nullptr;
1246 __t.size() = 0;
1247 }
1248}
1249
1250template <class _Tp, class _Compare, class _Allocator>
1251__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1252 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001253 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001254{
1255 if (__a == __t.__alloc())
1256 {
1257 if (__t.size() == 0)
1258 __begin_node() = __end_node();
1259 else
1260 {
1261 __begin_node() = __t.__begin_node();
1262 __end_node()->__left_ = __t.__end_node()->__left_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001263 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 size() = __t.size();
1265 __t.__begin_node() = __t.__end_node();
1266 __t.__end_node()->__left_ = nullptr;
1267 __t.size() = 0;
1268 }
1269 }
1270 else
1271 {
1272 __begin_node() = __end_node();
1273 }
1274}
1275
1276template <class _Tp, class _Compare, class _Allocator>
1277void
1278__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001279 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1280 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001281{
1282 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1283 __begin_node_ = __t.__begin_node_;
1284 __pair1_.first() = __t.__pair1_.first();
1285 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001286 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001287 if (size() == 0)
1288 __begin_node() = __end_node();
1289 else
1290 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001291 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 __t.__begin_node() = __t.__end_node();
1293 __t.__end_node()->__left_ = nullptr;
1294 __t.size() = 0;
1295 }
1296}
1297
1298template <class _Tp, class _Compare, class _Allocator>
1299void
1300__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1301{
1302 if (__node_alloc() == __t.__node_alloc())
1303 __move_assign(__t, true_type());
1304 else
1305 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001306 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001307 const_iterator __e = end();
1308 if (size() != 0)
1309 {
1310 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001311#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001312 try
1313 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001314#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001315 while (__cache != nullptr && __t.size() != 0)
1316 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001317 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001318 __node_pointer __next = __detach(__cache);
1319 __node_insert_multi(__cache);
1320 __cache = __next;
1321 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001322#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001323 }
1324 catch (...)
1325 {
1326 while (__cache->__parent_ != nullptr)
1327 __cache = static_cast<__node_pointer>(__cache->__parent_);
1328 destroy(__cache);
1329 throw;
1330 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001331#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332 if (__cache != nullptr)
1333 {
1334 while (__cache->__parent_ != nullptr)
1335 __cache = static_cast<__node_pointer>(__cache->__parent_);
1336 destroy(__cache);
1337 }
1338 }
1339 while (__t.size() != 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001340 __insert_multi(__e, _VSTD::move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 }
1342}
1343
1344template <class _Tp, class _Compare, class _Allocator>
1345__tree<_Tp, _Compare, _Allocator>&
1346__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001347 _NOEXCEPT_(
1348 __node_traits::propagate_on_container_move_assignment::value &&
1349 is_nothrow_move_assignable<value_compare>::value &&
1350 is_nothrow_move_assignable<__node_allocator>::value)
1351
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001352{
1353 __move_assign(__t, integral_constant<bool,
1354 __node_traits::propagate_on_container_move_assignment::value>());
1355 return *this;
1356}
1357
Howard Hinnant73d21a42010-09-04 23:28:19 +00001358#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359
1360template <class _Tp, class _Compare, class _Allocator>
1361__tree<_Tp, _Compare, _Allocator>::~__tree()
1362{
1363 destroy(__root());
1364}
1365
1366template <class _Tp, class _Compare, class _Allocator>
1367void
Howard Hinnant7686add2011-06-04 14:31:57 +00001368__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369{
1370 if (__nd != nullptr)
1371 {
1372 destroy(static_cast<__node_pointer>(__nd->__left_));
1373 destroy(static_cast<__node_pointer>(__nd->__right_));
1374 __node_allocator& __na = __node_alloc();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001375 __node_traits::destroy(__na, _VSTD::addressof(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001376 __node_traits::deallocate(__na, __nd, 1);
1377 }
1378}
1379
1380template <class _Tp, class _Compare, class _Allocator>
1381void
1382__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Marshall Clow7d914d12015-07-13 20:04:56 +00001383 _NOEXCEPT_(
1384 __is_nothrow_swappable<value_compare>::value
1385#if _LIBCPP_STD_VER <= 11
1386 && (!__node_traits::propagate_on_container_swap::value ||
1387 __is_nothrow_swappable<__node_allocator>::value)
1388#endif
1389 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001391 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001392 swap(__begin_node_, __t.__begin_node_);
1393 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001394 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 __pair3_.swap(__t.__pair3_);
1396 if (size() == 0)
1397 __begin_node() = __end_node();
1398 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001399 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400 if (__t.size() == 0)
1401 __t.__begin_node() = __t.__end_node();
1402 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001403 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404}
1405
1406template <class _Tp, class _Compare, class _Allocator>
1407void
Howard Hinnant7686add2011-06-04 14:31:57 +00001408__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409{
1410 destroy(__root());
1411 size() = 0;
1412 __begin_node() = __end_node();
1413 __end_node()->__left_ = nullptr;
1414}
1415
1416// Find lower_bound place to insert
1417// Set __parent to parent of null leaf
1418// Return reference to null leaf
1419template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001420typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1421__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001422 const value_type& __v)
1423{
1424 __node_pointer __nd = __root();
1425 if (__nd != nullptr)
1426 {
1427 while (true)
1428 {
1429 if (value_comp()(__nd->__value_, __v))
1430 {
1431 if (__nd->__right_ != nullptr)
1432 __nd = static_cast<__node_pointer>(__nd->__right_);
1433 else
1434 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001435 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001436 return __parent->__right_;
1437 }
1438 }
1439 else
1440 {
1441 if (__nd->__left_ != nullptr)
1442 __nd = static_cast<__node_pointer>(__nd->__left_);
1443 else
1444 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001445 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 return __parent->__left_;
1447 }
1448 }
1449 }
1450 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001451 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001452 return __parent->__left_;
1453}
1454
1455// Find upper_bound place to insert
1456// Set __parent to parent of null leaf
1457// Return reference to null leaf
1458template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001459typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1460__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461 const value_type& __v)
1462{
1463 __node_pointer __nd = __root();
1464 if (__nd != nullptr)
1465 {
1466 while (true)
1467 {
1468 if (value_comp()(__v, __nd->__value_))
1469 {
1470 if (__nd->__left_ != nullptr)
1471 __nd = static_cast<__node_pointer>(__nd->__left_);
1472 else
1473 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001474 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001475 return __parent->__left_;
1476 }
1477 }
1478 else
1479 {
1480 if (__nd->__right_ != nullptr)
1481 __nd = static_cast<__node_pointer>(__nd->__right_);
1482 else
1483 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001484 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001485 return __parent->__right_;
1486 }
1487 }
1488 }
1489 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001490 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 return __parent->__left_;
1492}
1493
1494// Find leaf place to insert closest to __hint
1495// First check prior to __hint.
1496// Next check after __hint.
1497// Next do O(log N) search.
1498// Set __parent to parent of null leaf
1499// Return reference to null leaf
1500template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001501typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001502__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001503 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001504 const value_type& __v)
1505{
1506 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1507 {
1508 // __v <= *__hint
1509 const_iterator __prior = __hint;
1510 if (__prior == begin() || !value_comp()(__v, *--__prior))
1511 {
1512 // *prev(__hint) <= __v <= *__hint
1513 if (__hint.__ptr_->__left_ == nullptr)
1514 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001515 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001516 return __parent->__left_;
1517 }
1518 else
1519 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001520 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521 return __parent->__right_;
1522 }
1523 }
1524 // __v < *prev(__hint)
1525 return __find_leaf_high(__parent, __v);
1526 }
1527 // else __v > *__hint
1528 return __find_leaf_low(__parent, __v);
1529}
1530
1531// Find place to insert if __v doesn't exist
1532// Set __parent to parent of null leaf
1533// Return reference to null leaf
1534// If __v exists, set parent to node of __v and return reference to node of __v
1535template <class _Tp, class _Compare, class _Allocator>
1536template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001537typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1538__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001539 const _Key& __v)
1540{
1541 __node_pointer __nd = __root();
1542 if (__nd != nullptr)
1543 {
1544 while (true)
1545 {
1546 if (value_comp()(__v, __nd->__value_))
1547 {
1548 if (__nd->__left_ != nullptr)
1549 __nd = static_cast<__node_pointer>(__nd->__left_);
1550 else
1551 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001552 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553 return __parent->__left_;
1554 }
1555 }
1556 else if (value_comp()(__nd->__value_, __v))
1557 {
1558 if (__nd->__right_ != nullptr)
1559 __nd = static_cast<__node_pointer>(__nd->__right_);
1560 else
1561 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001562 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 return __parent->__right_;
1564 }
1565 }
1566 else
1567 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001568 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001569 return __parent;
1570 }
1571 }
1572 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001573 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001574 return __parent->__left_;
1575}
1576
1577// Find place to insert if __v doesn't exist
1578// First check prior to __hint.
1579// Next check after __hint.
1580// Next do O(log N) search.
1581// Set __parent to parent of null leaf
1582// Return reference to null leaf
1583// If __v exists, set parent to node of __v and return reference to node of __v
1584template <class _Tp, class _Compare, class _Allocator>
1585template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001586typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001588 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001589 const _Key& __v)
1590{
1591 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1592 {
1593 // __v < *__hint
1594 const_iterator __prior = __hint;
1595 if (__prior == begin() || value_comp()(*--__prior, __v))
1596 {
1597 // *prev(__hint) < __v < *__hint
1598 if (__hint.__ptr_->__left_ == nullptr)
1599 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001600 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001601 return __parent->__left_;
1602 }
1603 else
1604 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001605 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001606 return __parent->__right_;
1607 }
1608 }
1609 // __v <= *prev(__hint)
1610 return __find_equal(__parent, __v);
1611 }
1612 else if (value_comp()(*__hint, __v)) // check after
1613 {
1614 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00001615 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 if (__next == end() || value_comp()(__v, *__next))
1617 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001618 // *__hint < __v < *_VSTD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001619 if (__hint.__ptr_->__right_ == nullptr)
1620 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001621 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 return __parent->__right_;
1623 }
1624 else
1625 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001626 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001627 return __parent->__left_;
1628 }
1629 }
1630 // *next(__hint) <= __v
1631 return __find_equal(__parent, __v);
1632 }
1633 // else __v == *__hint
Howard Hinnant70342b92013-06-19 21:29:40 +00001634 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001635 return __parent;
1636}
1637
1638template <class _Tp, class _Compare, class _Allocator>
1639void
1640__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1641 __node_base_pointer& __child,
1642 __node_base_pointer __new_node)
1643{
1644 __new_node->__left_ = nullptr;
1645 __new_node->__right_ = nullptr;
1646 __new_node->__parent_ = __parent;
1647 __child = __new_node;
1648 if (__begin_node()->__left_ != nullptr)
1649 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1650 __tree_balance_after_insert(__end_node()->__left_, __child);
1651 ++size();
1652}
1653
Howard Hinnant73d21a42010-09-04 23:28:19 +00001654#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1655#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656
1657template <class _Tp, class _Compare, class _Allocator>
1658template <class ..._Args>
1659typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1660__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1661{
1662 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001663 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001664 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 __h.get_deleter().__value_constructed = true;
1666 return __h;
1667}
1668
1669template <class _Tp, class _Compare, class _Allocator>
1670template <class... _Args>
1671pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1672__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1673{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001674 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 __node_base_pointer __parent;
1676 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1677 __node_pointer __r = static_cast<__node_pointer>(__child);
1678 bool __inserted = false;
1679 if (__child == nullptr)
1680 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001681 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001682 __r = __h.release();
1683 __inserted = true;
1684 }
1685 return pair<iterator, bool>(iterator(__r), __inserted);
1686}
1687
1688template <class _Tp, class _Compare, class _Allocator>
1689template <class... _Args>
1690typename __tree<_Tp, _Compare, _Allocator>::iterator
1691__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1692{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001693 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 __node_base_pointer __parent;
1695 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1696 __node_pointer __r = static_cast<__node_pointer>(__child);
1697 if (__child == nullptr)
1698 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001699 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 __r = __h.release();
1701 }
1702 return iterator(__r);
1703}
1704
1705template <class _Tp, class _Compare, class _Allocator>
1706template <class... _Args>
1707typename __tree<_Tp, _Compare, _Allocator>::iterator
1708__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1709{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001710 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001711 __node_base_pointer __parent;
1712 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001713 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714 return iterator(static_cast<__node_pointer>(__h.release()));
1715}
1716
1717template <class _Tp, class _Compare, class _Allocator>
1718template <class... _Args>
1719typename __tree<_Tp, _Compare, _Allocator>::iterator
1720__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1721 _Args&&... __args)
1722{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001723 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 __node_base_pointer __parent;
1725 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001726 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001727 return iterator(static_cast<__node_pointer>(__h.release()));
1728}
1729
Howard Hinnant73d21a42010-09-04 23:28:19 +00001730#endif // _LIBCPP_HAS_NO_VARIADICS
1731
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001732template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001733template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Howard Hinnant99968442011-11-29 18:15:50 +00001735__tree<_Tp, _Compare, _Allocator>::__insert_unique(_Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001736{
Howard Hinnant99968442011-11-29 18:15:50 +00001737 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001738 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1739 if (__r.second)
1740 __h.release();
1741 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001742}
1743
1744template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001745template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001747__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748{
Howard Hinnant99968442011-11-29 18:15:50 +00001749 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001750 iterator __r = __node_insert_unique(__p, __h.get());
1751 if (__r.__ptr_ == __h.get())
1752 __h.release();
1753 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001754}
1755
1756template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001757template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001758typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001759__tree<_Tp, _Compare, _Allocator>::__insert_multi(_Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760{
Howard Hinnant99968442011-11-29 18:15:50 +00001761 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762 __node_base_pointer __parent;
1763 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001764 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001765 return iterator(__h.release());
1766}
1767
1768template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001769template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001771__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001772{
Howard Hinnant99968442011-11-29 18:15:50 +00001773 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774 __node_base_pointer __parent;
1775 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001776 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001777 return iterator(__h.release());
1778}
1779
Howard Hinnant73d21a42010-09-04 23:28:19 +00001780#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001781
1782template <class _Tp, class _Compare, class _Allocator>
1783typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1784__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1785{
1786 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001787 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001788 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00001790 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791}
1792
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001793#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1794
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795template <class _Tp, class _Compare, class _Allocator>
1796pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1797__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1798{
1799 __node_base_pointer __parent;
1800 __node_base_pointer& __child = __find_equal(__parent, __v);
1801 __node_pointer __r = static_cast<__node_pointer>(__child);
1802 bool __inserted = false;
1803 if (__child == nullptr)
1804 {
1805 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001806 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807 __r = __h.release();
1808 __inserted = true;
1809 }
1810 return pair<iterator, bool>(iterator(__r), __inserted);
1811}
1812
1813template <class _Tp, class _Compare, class _Allocator>
1814typename __tree<_Tp, _Compare, _Allocator>::iterator
1815__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1816{
1817 __node_base_pointer __parent;
1818 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1819 __node_pointer __r = static_cast<__node_pointer>(__child);
1820 if (__child == nullptr)
1821 {
1822 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001823 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 __r = __h.release();
1825 }
1826 return iterator(__r);
1827}
1828
1829template <class _Tp, class _Compare, class _Allocator>
1830typename __tree<_Tp, _Compare, _Allocator>::iterator
1831__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1832{
1833 __node_base_pointer __parent;
1834 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1835 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001836 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837 return iterator(__h.release());
1838}
1839
1840template <class _Tp, class _Compare, class _Allocator>
1841typename __tree<_Tp, _Compare, _Allocator>::iterator
1842__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1843{
1844 __node_base_pointer __parent;
1845 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1846 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001847 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 return iterator(__h.release());
1849}
1850
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001851template <class _Tp, class _Compare, class _Allocator>
1852pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1853__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1854{
1855 __node_base_pointer __parent;
1856 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1857 __node_pointer __r = static_cast<__node_pointer>(__child);
1858 bool __inserted = false;
1859 if (__child == nullptr)
1860 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001861 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001862 __r = __nd;
1863 __inserted = true;
1864 }
1865 return pair<iterator, bool>(iterator(__r), __inserted);
1866}
1867
1868template <class _Tp, class _Compare, class _Allocator>
1869typename __tree<_Tp, _Compare, _Allocator>::iterator
1870__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1871 __node_pointer __nd)
1872{
1873 __node_base_pointer __parent;
1874 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1875 __node_pointer __r = static_cast<__node_pointer>(__child);
1876 if (__child == nullptr)
1877 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001878 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001879 __r = __nd;
1880 }
1881 return iterator(__r);
1882}
1883
1884template <class _Tp, class _Compare, class _Allocator>
1885typename __tree<_Tp, _Compare, _Allocator>::iterator
1886__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1887{
1888 __node_base_pointer __parent;
1889 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001890 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001891 return iterator(__nd);
1892}
1893
1894template <class _Tp, class _Compare, class _Allocator>
1895typename __tree<_Tp, _Compare, _Allocator>::iterator
1896__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1897 __node_pointer __nd)
1898{
1899 __node_base_pointer __parent;
1900 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001901 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001902 return iterator(__nd);
1903}
1904
1905template <class _Tp, class _Compare, class _Allocator>
1906typename __tree<_Tp, _Compare, _Allocator>::iterator
1907__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1908{
Howard Hinnant70342b92013-06-19 21:29:40 +00001909 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910 iterator __r(__np);
1911 ++__r;
1912 if (__begin_node() == __np)
1913 __begin_node() = __r.__ptr_;
1914 --size();
1915 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 __tree_remove(__end_node()->__left_,
1917 static_cast<__node_base_pointer>(__np));
Marshall Clow140e8f52014-04-11 08:22:42 +00001918 __node_traits::destroy(__na, const_cast<value_type*>(_VSTD::addressof(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001919 __node_traits::deallocate(__na, __np, 1);
1920 return __r;
1921}
1922
1923template <class _Tp, class _Compare, class _Allocator>
1924typename __tree<_Tp, _Compare, _Allocator>::iterator
1925__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1926{
1927 while (__f != __l)
1928 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00001929 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930}
1931
1932template <class _Tp, class _Compare, class _Allocator>
1933template <class _Key>
1934typename __tree<_Tp, _Compare, _Allocator>::size_type
1935__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1936{
1937 iterator __i = find(__k);
1938 if (__i == end())
1939 return 0;
1940 erase(__i);
1941 return 1;
1942}
1943
1944template <class _Tp, class _Compare, class _Allocator>
1945template <class _Key>
1946typename __tree<_Tp, _Compare, _Allocator>::size_type
1947__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
1948{
1949 pair<iterator, iterator> __p = __equal_range_multi(__k);
1950 size_type __r = 0;
1951 for (; __p.first != __p.second; ++__r)
1952 __p.first = erase(__p.first);
1953 return __r;
1954}
1955
1956template <class _Tp, class _Compare, class _Allocator>
1957template <class _Key>
1958typename __tree<_Tp, _Compare, _Allocator>::iterator
1959__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
1960{
1961 iterator __p = __lower_bound(__v, __root(), __end_node());
1962 if (__p != end() && !value_comp()(__v, *__p))
1963 return __p;
1964 return end();
1965}
1966
1967template <class _Tp, class _Compare, class _Allocator>
1968template <class _Key>
1969typename __tree<_Tp, _Compare, _Allocator>::const_iterator
1970__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
1971{
1972 const_iterator __p = __lower_bound(__v, __root(), __end_node());
1973 if (__p != end() && !value_comp()(__v, *__p))
1974 return __p;
1975 return end();
1976}
1977
1978template <class _Tp, class _Compare, class _Allocator>
1979template <class _Key>
1980typename __tree<_Tp, _Compare, _Allocator>::size_type
1981__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
1982{
1983 __node_const_pointer __result = __end_node();
1984 __node_const_pointer __rt = __root();
1985 while (__rt != nullptr)
1986 {
1987 if (value_comp()(__k, __rt->__value_))
1988 {
1989 __result = __rt;
1990 __rt = static_cast<__node_const_pointer>(__rt->__left_);
1991 }
1992 else if (value_comp()(__rt->__value_, __k))
1993 __rt = static_cast<__node_const_pointer>(__rt->__right_);
1994 else
1995 return 1;
1996 }
1997 return 0;
1998}
1999
2000template <class _Tp, class _Compare, class _Allocator>
2001template <class _Key>
2002typename __tree<_Tp, _Compare, _Allocator>::size_type
2003__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2004{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002005 __node_const_pointer __result = __end_node();
2006 __node_const_pointer __rt = __root();
2007 while (__rt != nullptr)
2008 {
2009 if (value_comp()(__k, __rt->__value_))
2010 {
2011 __result = __rt;
2012 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2013 }
2014 else if (value_comp()(__rt->__value_, __k))
2015 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2016 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002017 return _VSTD::distance(
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002018 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2019 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
2020 );
2021 }
2022 return 0;
2023}
2024
2025template <class _Tp, class _Compare, class _Allocator>
2026template <class _Key>
2027typename __tree<_Tp, _Compare, _Allocator>::iterator
2028__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2029 __node_pointer __root,
2030 __node_pointer __result)
2031{
2032 while (__root != nullptr)
2033 {
2034 if (!value_comp()(__root->__value_, __v))
2035 {
2036 __result = __root;
2037 __root = static_cast<__node_pointer>(__root->__left_);
2038 }
2039 else
2040 __root = static_cast<__node_pointer>(__root->__right_);
2041 }
2042 return iterator(__result);
2043}
2044
2045template <class _Tp, class _Compare, class _Allocator>
2046template <class _Key>
2047typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2048__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2049 __node_const_pointer __root,
2050 __node_const_pointer __result) const
2051{
2052 while (__root != nullptr)
2053 {
2054 if (!value_comp()(__root->__value_, __v))
2055 {
2056 __result = __root;
2057 __root = static_cast<__node_const_pointer>(__root->__left_);
2058 }
2059 else
2060 __root = static_cast<__node_const_pointer>(__root->__right_);
2061 }
2062 return const_iterator(__result);
2063}
2064
2065template <class _Tp, class _Compare, class _Allocator>
2066template <class _Key>
2067typename __tree<_Tp, _Compare, _Allocator>::iterator
2068__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2069 __node_pointer __root,
2070 __node_pointer __result)
2071{
2072 while (__root != nullptr)
2073 {
2074 if (value_comp()(__v, __root->__value_))
2075 {
2076 __result = __root;
2077 __root = static_cast<__node_pointer>(__root->__left_);
2078 }
2079 else
2080 __root = static_cast<__node_pointer>(__root->__right_);
2081 }
2082 return iterator(__result);
2083}
2084
2085template <class _Tp, class _Compare, class _Allocator>
2086template <class _Key>
2087typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2088__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2089 __node_const_pointer __root,
2090 __node_const_pointer __result) const
2091{
2092 while (__root != nullptr)
2093 {
2094 if (value_comp()(__v, __root->__value_))
2095 {
2096 __result = __root;
2097 __root = static_cast<__node_const_pointer>(__root->__left_);
2098 }
2099 else
2100 __root = static_cast<__node_const_pointer>(__root->__right_);
2101 }
2102 return const_iterator(__result);
2103}
2104
2105template <class _Tp, class _Compare, class _Allocator>
2106template <class _Key>
2107pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2108 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2109__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2110{
Howard Hinnant99968442011-11-29 18:15:50 +00002111 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 __node_pointer __result = __end_node();
2113 __node_pointer __rt = __root();
2114 while (__rt != nullptr)
2115 {
2116 if (value_comp()(__k, __rt->__value_))
2117 {
2118 __result = __rt;
2119 __rt = static_cast<__node_pointer>(__rt->__left_);
2120 }
2121 else if (value_comp()(__rt->__value_, __k))
2122 __rt = static_cast<__node_pointer>(__rt->__right_);
2123 else
Howard Hinnant99968442011-11-29 18:15:50 +00002124 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125 iterator(
2126 __rt->__right_ != nullptr ?
2127 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2128 : __result));
2129 }
Howard Hinnant99968442011-11-29 18:15:50 +00002130 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131}
2132
2133template <class _Tp, class _Compare, class _Allocator>
2134template <class _Key>
2135pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2136 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2137__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2138{
Howard Hinnant99968442011-11-29 18:15:50 +00002139 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002140 __node_const_pointer __result = __end_node();
2141 __node_const_pointer __rt = __root();
2142 while (__rt != nullptr)
2143 {
2144 if (value_comp()(__k, __rt->__value_))
2145 {
2146 __result = __rt;
2147 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2148 }
2149 else if (value_comp()(__rt->__value_, __k))
2150 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2151 else
Howard Hinnant99968442011-11-29 18:15:50 +00002152 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 const_iterator(
2154 __rt->__right_ != nullptr ?
2155 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2156 : __result));
2157 }
Howard Hinnant99968442011-11-29 18:15:50 +00002158 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159}
2160
2161template <class _Tp, class _Compare, class _Allocator>
2162template <class _Key>
2163pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2164 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2165__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2166{
Howard Hinnant99968442011-11-29 18:15:50 +00002167 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168 __node_pointer __result = __end_node();
2169 __node_pointer __rt = __root();
2170 while (__rt != nullptr)
2171 {
2172 if (value_comp()(__k, __rt->__value_))
2173 {
2174 __result = __rt;
2175 __rt = static_cast<__node_pointer>(__rt->__left_);
2176 }
2177 else if (value_comp()(__rt->__value_, __k))
2178 __rt = static_cast<__node_pointer>(__rt->__right_);
2179 else
Howard Hinnant99968442011-11-29 18:15:50 +00002180 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2182 }
Howard Hinnant99968442011-11-29 18:15:50 +00002183 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184}
2185
2186template <class _Tp, class _Compare, class _Allocator>
2187template <class _Key>
2188pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2189 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2190__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2191{
Howard Hinnant99968442011-11-29 18:15:50 +00002192 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 __node_const_pointer __result = __end_node();
2194 __node_const_pointer __rt = __root();
2195 while (__rt != nullptr)
2196 {
2197 if (value_comp()(__k, __rt->__value_))
2198 {
2199 __result = __rt;
2200 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2201 }
2202 else if (value_comp()(__rt->__value_, __k))
2203 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2204 else
Howard Hinnant99968442011-11-29 18:15:50 +00002205 return _Pp(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002206 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2207 }
Howard Hinnant99968442011-11-29 18:15:50 +00002208 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209}
2210
2211template <class _Tp, class _Compare, class _Allocator>
2212typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002213__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002214{
Howard Hinnant70342b92013-06-19 21:29:40 +00002215 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 if (__begin_node() == __np)
2217 {
2218 if (__np->__right_ != nullptr)
2219 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2220 else
2221 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2222 }
2223 --size();
2224 __tree_remove(__end_node()->__left_,
2225 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002226 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002227}
2228
Howard Hinnant7686add2011-06-04 14:31:57 +00002229template <class _Tp, class _Compare, class _Allocator>
2230inline _LIBCPP_INLINE_VISIBILITY
2231void
2232swap(__tree<_Tp, _Compare, _Allocator>& __x,
2233 __tree<_Tp, _Compare, _Allocator>& __y)
2234 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2235{
2236 __x.swap(__y);
2237}
2238
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239_LIBCPP_END_NAMESPACE_STD
2240
2241#endif // _LIBCPP___TREE