blob: 588f4380766766ba903e71cf48daa917400ef564 [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 <
551 typename pointer_traits<_VoidPtr>::template
552#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
553 rebind<__tree_node_base<_VoidPtr> >
554#else
555 rebind<__tree_node_base<_VoidPtr> >::other
556#endif
557 >
558{
559 __tree_node_base(const __tree_node_base&);
560 __tree_node_base& operator=(const __tree_node_base&);
561public:
562 typedef typename pointer_traits<_VoidPtr>::template
563#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
564 rebind<__tree_node_base>
565#else
566 rebind<__tree_node_base>::other
567#endif
568 pointer;
569 typedef typename pointer_traits<_VoidPtr>::template
570#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
571 rebind<const __tree_node_base>
572#else
573 rebind<const __tree_node_base>::other
574#endif
575 const_pointer;
576 typedef __tree_end_node<pointer> base;
577
578 pointer __right_;
579 pointer __parent_;
580 bool __is_black_;
581
Howard Hinnant333f50d2010-09-21 20:16:37 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000583 __tree_node_base() _NOEXCEPT
584 : __right_(), __parent_(), __is_black_(false) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000585};
586
587template <class _Tp, class _VoidPtr>
588class __tree_node
589 : public __tree_node_base<_VoidPtr>
590{
591public:
592 typedef __tree_node_base<_VoidPtr> base;
593 typedef _Tp value_type;
594
595 value_type __value_;
596
Howard Hinnant73d21a42010-09-04 23:28:19 +0000597#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598 template <class ..._Args>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600 explicit __tree_node(_Args&& ...__args)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000601 : __value_(_VSTD::forward<_Args>(__args)...) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000602#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnant333f50d2010-09-21 20:16:37 +0000603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000604 explicit __tree_node(const value_type& __v)
605 : __value_(__v) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000606#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000607};
608
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000609template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
610template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000611
612template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000613class _LIBCPP_TYPE_VIS_ONLY __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614{
615 typedef _NodePtr __node_pointer;
616 typedef typename pointer_traits<__node_pointer>::element_type __node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617
618 __node_pointer __ptr_;
619
620 typedef pointer_traits<__node_pointer> __pointer_traits;
621public:
622 typedef bidirectional_iterator_tag iterator_category;
623 typedef _Tp value_type;
624 typedef _DiffType difference_type;
625 typedef value_type& reference;
626 typedef typename pointer_traits<__node_pointer>::template
627#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
628 rebind<value_type>
629#else
630 rebind<value_type>::other
631#endif
632 pointer;
633
Marshall Clow051c8482013-08-08 21:52:50 +0000634 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
635#if _LIBCPP_STD_VER > 11
636 : __ptr_(nullptr)
637#endif
638 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639
Howard Hinnant333f50d2010-09-21 20:16:37 +0000640 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000641 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
642 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000643
Howard Hinnant333f50d2010-09-21 20:16:37 +0000644 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000645 __tree_iterator& operator++() {
646 __ptr_ = static_cast<__node_pointer>(
647 __tree_next(static_cast<typename __node::base::pointer>(__ptr_)));
648 return *this;
649 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000650 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000651 __tree_iterator operator++(int)
652 {__tree_iterator __t(*this); ++(*this); return __t;}
653
Howard Hinnant333f50d2010-09-21 20:16:37 +0000654 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000655 __tree_iterator& operator--() {
656 __ptr_ = static_cast<__node_pointer>(
657 __tree_prev(static_cast<typename __node::base::pointer>(__ptr_)));
658 return *this;
659 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000660 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 __tree_iterator operator--(int)
662 {__tree_iterator __t(*this); --(*this); return __t;}
663
Howard Hinnant333f50d2010-09-21 20:16:37 +0000664 friend _LIBCPP_INLINE_VISIBILITY
665 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000667 friend _LIBCPP_INLINE_VISIBILITY
668 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000669 {return !(__x == __y);}
670
671private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000672 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000673 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000675 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
676 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
677 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
678 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
679 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
680 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000681};
682
683template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000684class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685{
686 typedef _ConstNodePtr __node_pointer;
687 typedef typename pointer_traits<__node_pointer>::element_type __node;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000688
689 __node_pointer __ptr_;
690
691 typedef pointer_traits<__node_pointer> __pointer_traits;
692public:
693 typedef bidirectional_iterator_tag iterator_category;
694 typedef _Tp value_type;
695 typedef _DiffType difference_type;
696 typedef const value_type& reference;
697 typedef typename pointer_traits<__node_pointer>::template
698#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
699 rebind<const value_type>
700#else
701 rebind<const value_type>::other
702#endif
703 pointer;
704
Marshall Clow051c8482013-08-08 21:52:50 +0000705 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
706#if _LIBCPP_STD_VER > 11
707 : __ptr_(nullptr)
708#endif
709 {}
710
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000711private:
712 typedef typename remove_const<__node>::type __non_const_node;
713 typedef typename pointer_traits<__node_pointer>::template
714#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
715 rebind<__non_const_node>
716#else
717 rebind<__non_const_node>::other
718#endif
719 __non_const_node_pointer;
720 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
721 __non_const_iterator;
722public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000724 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
725 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726
Howard Hinnant333f50d2010-09-21 20:16:37 +0000727 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000728 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
729 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730
Howard Hinnant333f50d2010-09-21 20:16:37 +0000731 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000732 __tree_const_iterator& operator++() {
733 typedef typename pointer_traits<__node_pointer>::template
734#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
735 rebind<typename __node::base>
736#else
737 rebind<typename __node::base>::other
738#endif
739 __node_base_pointer;
740
741 __ptr_ = static_cast<__node_pointer>(
742 __tree_next(static_cast<__node_base_pointer>(__ptr_)));
743 return *this;
744 }
745
Howard Hinnant333f50d2010-09-21 20:16:37 +0000746 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747 __tree_const_iterator operator++(int)
748 {__tree_const_iterator __t(*this); ++(*this); return __t;}
749
Howard Hinnant333f50d2010-09-21 20:16:37 +0000750 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000751 __tree_const_iterator& operator--() {
752 typedef typename pointer_traits<__node_pointer>::template
753#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
754 rebind<typename __node::base>
755#else
756 rebind<typename __node::base>::other
757#endif
758 __node_base_pointer;
759
760 __ptr_ = static_cast<__node_pointer>(
761 __tree_prev(static_cast<__node_base_pointer>(__ptr_)));
762 return *this;
763 }
764
Howard Hinnant333f50d2010-09-21 20:16:37 +0000765 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000766 __tree_const_iterator operator--(int)
767 {__tree_const_iterator __t(*this); --(*this); return __t;}
768
Howard Hinnant333f50d2010-09-21 20:16:37 +0000769 friend _LIBCPP_INLINE_VISIBILITY
770 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000771 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000772 friend _LIBCPP_INLINE_VISIBILITY
773 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774 {return !(__x == __y);}
775
776private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000777 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000778 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
779 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000781 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
782 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
783 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
784 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
785 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786};
787
788template <class _Tp, class _Compare, class _Allocator>
789class __tree
790{
791public:
792 typedef _Tp value_type;
793 typedef _Compare value_compare;
794 typedef _Allocator allocator_type;
795 typedef allocator_traits<allocator_type> __alloc_traits;
796 typedef typename __alloc_traits::pointer pointer;
797 typedef typename __alloc_traits::const_pointer const_pointer;
798 typedef typename __alloc_traits::size_type size_type;
799 typedef typename __alloc_traits::difference_type difference_type;
800
Howard Hinnant70342b92013-06-19 21:29:40 +0000801 typedef typename __alloc_traits::void_pointer __void_pointer;
802
803 typedef __tree_node<value_type, __void_pointer> __node;
804 typedef __tree_node_base<__void_pointer> __node_base;
Marshall Clow66302c62015-04-07 05:21:38 +0000805 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000806 typedef allocator_traits<__node_allocator> __node_traits;
807 typedef typename __node_traits::pointer __node_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000808 typedef typename __node_traits::pointer __node_const_pointer;
Howard Hinnantd615e472011-04-03 20:05:29 +0000809 typedef typename __node_base::pointer __node_base_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000810 typedef typename __node_base::pointer __node_base_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811private:
Howard Hinnantd615e472011-04-03 20:05:29 +0000812 typedef typename __node_base::base __end_node_t;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813 typedef typename pointer_traits<__node_pointer>::template
814#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
815 rebind<__end_node_t>
816#else
817 rebind<__end_node_t>::other
818#endif
819 __end_node_ptr;
820 typedef typename pointer_traits<__node_pointer>::template
821#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant70342b92013-06-19 21:29:40 +0000822 rebind<__end_node_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000823#else
Howard Hinnant70342b92013-06-19 21:29:40 +0000824 rebind<__end_node_t>::other
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825#endif
826 __end_node_const_ptr;
827
828 __node_pointer __begin_node_;
829 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
830 __compressed_pair<size_type, value_compare> __pair3_;
831
832public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000834 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835 {
836 return static_cast<__node_pointer>
837 (
838 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
839 );
840 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000842 __node_const_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000843 {
844 return static_cast<__node_const_pointer>
845 (
Howard Hinnant70342b92013-06-19 21:29:40 +0000846 pointer_traits<__end_node_const_ptr>::pointer_to(const_cast<__end_node_t&>(__pair1_.first()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847 );
848 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000849 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000850 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000853 const __node_allocator& __node_alloc() const _NOEXCEPT
854 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000856 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000858 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000859public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000861 allocator_type __alloc() const _NOEXCEPT
862 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000863private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000865 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000867 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000868 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000870 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000871 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000872 const value_compare& value_comp() const _NOEXCEPT
873 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000875 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000876 __node_pointer __root() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000877 {return static_cast<__node_pointer> (__end_node()->__left_);}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000878 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000879 __node_const_pointer __root() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000880 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
881
882 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +0000883 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000884
Howard Hinnant7686add2011-06-04 14:31:57 +0000885 explicit __tree(const value_compare& __comp)
886 _NOEXCEPT_(
887 is_nothrow_default_constructible<__node_allocator>::value &&
888 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889 explicit __tree(const allocator_type& __a);
890 __tree(const value_compare& __comp, const allocator_type& __a);
891 __tree(const __tree& __t);
892 __tree& operator=(const __tree& __t);
893 template <class _InputIterator>
894 void __assign_unique(_InputIterator __first, _InputIterator __last);
895 template <class _InputIterator>
896 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000897#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-06-04 14:31:57 +0000898 __tree(__tree&& __t)
899 _NOEXCEPT_(
900 is_nothrow_move_constructible<__node_allocator>::value &&
901 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +0000903 __tree& operator=(__tree&& __t)
904 _NOEXCEPT_(
905 __node_traits::propagate_on_container_move_assignment::value &&
906 is_nothrow_move_assignable<value_compare>::value &&
907 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000908#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909
910 ~__tree();
911
Howard Hinnant333f50d2010-09-21 20:16:37 +0000912 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000913 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000914 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000915 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000916 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000917 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000918 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000919 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000920
Howard Hinnant333f50d2010-09-21 20:16:37 +0000921 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000922 size_type max_size() const _NOEXCEPT
923 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000924
Howard Hinnant7686add2011-06-04 14:31:57 +0000925 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926
Howard Hinnant7686add2011-06-04 14:31:57 +0000927 void swap(__tree& __t)
928 _NOEXCEPT_(
929 __is_nothrow_swappable<value_compare>::value &&
930 (!__node_traits::propagate_on_container_swap::value ||
931 __is_nothrow_swappable<__node_allocator>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000932
Howard Hinnant73d21a42010-09-04 23:28:19 +0000933#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
934#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935 template <class... _Args>
936 pair<iterator, bool>
937 __emplace_unique(_Args&&... __args);
938 template <class... _Args>
939 iterator
940 __emplace_multi(_Args&&... __args);
941
942 template <class... _Args>
943 iterator
944 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
945 template <class... _Args>
946 iterator
947 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000948#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949
Howard Hinnant99968442011-11-29 18:15:50 +0000950 template <class _Vp>
951 pair<iterator, bool> __insert_unique(_Vp&& __v);
952 template <class _Vp>
953 iterator __insert_unique(const_iterator __p, _Vp&& __v);
954 template <class _Vp>
955 iterator __insert_multi(_Vp&& __v);
956 template <class _Vp>
957 iterator __insert_multi(const_iterator __p, _Vp&& __v);
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +0000958#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000959
960 pair<iterator, bool> __insert_unique(const value_type& __v);
961 iterator __insert_unique(const_iterator __p, const value_type& __v);
962 iterator __insert_multi(const value_type& __v);
963 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964
965 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
966 iterator __node_insert_unique(const_iterator __p,
967 __node_pointer __nd);
968
969 iterator __node_insert_multi(__node_pointer __nd);
970 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
971
972 iterator erase(const_iterator __p);
973 iterator erase(const_iterator __f, const_iterator __l);
974 template <class _Key>
975 size_type __erase_unique(const _Key& __k);
976 template <class _Key>
977 size_type __erase_multi(const _Key& __k);
978
979 void __insert_node_at(__node_base_pointer __parent,
980 __node_base_pointer& __child,
981 __node_base_pointer __new_node);
982
983 template <class _Key>
984 iterator find(const _Key& __v);
985 template <class _Key>
986 const_iterator find(const _Key& __v) const;
987
988 template <class _Key>
989 size_type __count_unique(const _Key& __k) const;
990 template <class _Key>
991 size_type __count_multi(const _Key& __k) const;
992
993 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995 iterator lower_bound(const _Key& __v)
996 {return __lower_bound(__v, __root(), __end_node());}
997 template <class _Key>
998 iterator __lower_bound(const _Key& __v,
999 __node_pointer __root,
1000 __node_pointer __result);
1001 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 const_iterator lower_bound(const _Key& __v) const
1004 {return __lower_bound(__v, __root(), __end_node());}
1005 template <class _Key>
1006 const_iterator __lower_bound(const _Key& __v,
1007 __node_const_pointer __root,
1008 __node_const_pointer __result) const;
1009 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001011 iterator upper_bound(const _Key& __v)
1012 {return __upper_bound(__v, __root(), __end_node());}
1013 template <class _Key>
1014 iterator __upper_bound(const _Key& __v,
1015 __node_pointer __root,
1016 __node_pointer __result);
1017 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001019 const_iterator upper_bound(const _Key& __v) const
1020 {return __upper_bound(__v, __root(), __end_node());}
1021 template <class _Key>
1022 const_iterator __upper_bound(const _Key& __v,
1023 __node_const_pointer __root,
1024 __node_const_pointer __result) const;
1025 template <class _Key>
1026 pair<iterator, iterator>
1027 __equal_range_unique(const _Key& __k);
1028 template <class _Key>
1029 pair<const_iterator, const_iterator>
1030 __equal_range_unique(const _Key& __k) const;
1031
1032 template <class _Key>
1033 pair<iterator, iterator>
1034 __equal_range_multi(const _Key& __k);
1035 template <class _Key>
1036 pair<const_iterator, const_iterator>
1037 __equal_range_multi(const _Key& __k) const;
1038
Howard Hinnant99968442011-11-29 18:15:50 +00001039 typedef __tree_node_destructor<__node_allocator> _Dp;
1040 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041
Howard Hinnant8b537682011-06-04 17:10:24 +00001042 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043private:
Howard Hinnantd615e472011-04-03 20:05:29 +00001044 typename __node_base::pointer&
1045 __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v);
1046 typename __node_base::pointer&
1047 __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v);
1048 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049 __find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001050 typename __node_base::pointer& __parent, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001051 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001052 typename __node_base::pointer&
1053 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001054 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001055 typename __node_base::pointer&
1056 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057 const _Key& __v);
1058
Howard Hinnant73d21a42010-09-04 23:28:19 +00001059#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001060 template <class ..._Args>
1061 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001062#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001063 __node_holder __construct_node(const value_type& __v);
1064#endif
1065
Howard Hinnant7686add2011-06-04 14:31:57 +00001066 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067
Howard Hinnant333f50d2010-09-21 20:16:37 +00001068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 void __copy_assign_alloc(const __tree& __t)
1070 {__copy_assign_alloc(__t, integral_constant<bool,
1071 __node_traits::propagate_on_container_copy_assignment::value>());}
1072
Howard Hinnant333f50d2010-09-21 20:16:37 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 void __copy_assign_alloc(const __tree& __t, true_type)
1075 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077 void __copy_assign_alloc(const __tree& __t, false_type) {}
1078
1079 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001080 void __move_assign(__tree& __t, true_type)
1081 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1082 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083
Howard Hinnant333f50d2010-09-21 20:16:37 +00001084 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001086 _NOEXCEPT_(
1087 !__node_traits::propagate_on_container_move_assignment::value ||
1088 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001089 {__move_assign_alloc(__t, integral_constant<bool,
1090 __node_traits::propagate_on_container_move_assignment::value>());}
1091
Howard Hinnant333f50d2010-09-21 20:16:37 +00001092 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001093 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001094 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001095 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001097 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001098
Howard Hinnant333f50d2010-09-21 20:16:37 +00001099 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001100 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00001101 _NOEXCEPT_(
1102 !__node_traits::propagate_on_container_swap::value ||
1103 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104 {__swap_alloc(__x, __y, integral_constant<bool,
1105 __node_traits::propagate_on_container_swap::value>());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001106 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001108 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001109 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001110 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001111 swap(__x, __y);
1112 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001114 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001115 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116 {}
1117
1118 __node_pointer __detach();
1119 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001120
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001121 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1122 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001123};
1124
1125template <class _Tp, class _Compare, class _Allocator>
1126__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001127 _NOEXCEPT_(
1128 is_nothrow_default_constructible<__node_allocator>::value &&
1129 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001130 : __pair3_(0, __comp)
1131{
1132 __begin_node() = __end_node();
1133}
1134
1135template <class _Tp, class _Compare, class _Allocator>
1136__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1137 : __pair1_(__node_allocator(__a)),
1138 __begin_node_(__node_pointer()),
1139 __pair3_(0)
1140{
1141 __begin_node() = __end_node();
1142}
1143
1144template <class _Tp, class _Compare, class _Allocator>
1145__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1146 const allocator_type& __a)
1147 : __pair1_(__node_allocator(__a)),
1148 __begin_node_(__node_pointer()),
1149 __pair3_(0, __comp)
1150{
1151 __begin_node() = __end_node();
1152}
1153
1154// Precondition: size() != 0
1155template <class _Tp, class _Compare, class _Allocator>
1156typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1157__tree<_Tp, _Compare, _Allocator>::__detach()
1158{
1159 __node_pointer __cache = __begin_node();
1160 __begin_node() = __end_node();
1161 __end_node()->__left_->__parent_ = nullptr;
1162 __end_node()->__left_ = nullptr;
1163 size() = 0;
1164 // __cache->__left_ == nullptr
1165 if (__cache->__right_ != nullptr)
1166 __cache = static_cast<__node_pointer>(__cache->__right_);
1167 // __cache->__left_ == nullptr
1168 // __cache->__right_ == nullptr
1169 return __cache;
1170}
1171
1172// Precondition: __cache != nullptr
1173// __cache->left_ == nullptr
1174// __cache->right_ == nullptr
1175// This is no longer a red-black tree
1176template <class _Tp, class _Compare, class _Allocator>
1177typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1178__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1179{
1180 if (__cache->__parent_ == nullptr)
1181 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001182 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001183 {
1184 __cache->__parent_->__left_ = nullptr;
1185 __cache = static_cast<__node_pointer>(__cache->__parent_);
1186 if (__cache->__right_ == nullptr)
1187 return __cache;
1188 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1189 }
1190 // __cache is right child
1191 __cache->__parent_->__right_ = nullptr;
1192 __cache = static_cast<__node_pointer>(__cache->__parent_);
1193 if (__cache->__left_ == nullptr)
1194 return __cache;
1195 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1196}
1197
1198template <class _Tp, class _Compare, class _Allocator>
1199__tree<_Tp, _Compare, _Allocator>&
1200__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1201{
1202 if (this != &__t)
1203 {
1204 value_comp() = __t.value_comp();
1205 __copy_assign_alloc(__t);
1206 __assign_multi(__t.begin(), __t.end());
1207 }
1208 return *this;
1209}
1210
1211template <class _Tp, class _Compare, class _Allocator>
1212template <class _InputIterator>
1213void
1214__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1215{
1216 if (size() != 0)
1217 {
1218 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001219#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001220 try
1221 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001222#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001223 for (; __cache != nullptr && __first != __last; ++__first)
1224 {
1225 __cache->__value_ = *__first;
1226 __node_pointer __next = __detach(__cache);
1227 __node_insert_unique(__cache);
1228 __cache = __next;
1229 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001230#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001231 }
1232 catch (...)
1233 {
1234 while (__cache->__parent_ != nullptr)
1235 __cache = static_cast<__node_pointer>(__cache->__parent_);
1236 destroy(__cache);
1237 throw;
1238 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001239#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240 if (__cache != nullptr)
1241 {
1242 while (__cache->__parent_ != nullptr)
1243 __cache = static_cast<__node_pointer>(__cache->__parent_);
1244 destroy(__cache);
1245 }
1246 }
1247 for (; __first != __last; ++__first)
1248 __insert_unique(*__first);
1249}
1250
1251template <class _Tp, class _Compare, class _Allocator>
1252template <class _InputIterator>
1253void
1254__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1255{
1256 if (size() != 0)
1257 {
1258 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001259#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260 try
1261 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001262#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263 for (; __cache != nullptr && __first != __last; ++__first)
1264 {
1265 __cache->__value_ = *__first;
1266 __node_pointer __next = __detach(__cache);
1267 __node_insert_multi(__cache);
1268 __cache = __next;
1269 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001270#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 }
1272 catch (...)
1273 {
1274 while (__cache->__parent_ != nullptr)
1275 __cache = static_cast<__node_pointer>(__cache->__parent_);
1276 destroy(__cache);
1277 throw;
1278 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001279#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 if (__cache != nullptr)
1281 {
1282 while (__cache->__parent_ != nullptr)
1283 __cache = static_cast<__node_pointer>(__cache->__parent_);
1284 destroy(__cache);
1285 }
1286 }
1287 for (; __first != __last; ++__first)
1288 __insert_multi(*__first);
1289}
1290
1291template <class _Tp, class _Compare, class _Allocator>
1292__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1293 : __begin_node_(__node_pointer()),
1294 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1295 __pair3_(0, __t.value_comp())
1296{
1297 __begin_node() = __end_node();
1298}
1299
Howard Hinnant73d21a42010-09-04 23:28:19 +00001300#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301
1302template <class _Tp, class _Compare, class _Allocator>
1303__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001304 _NOEXCEPT_(
1305 is_nothrow_move_constructible<__node_allocator>::value &&
1306 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001307 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1308 __pair1_(_VSTD::move(__t.__pair1_)),
1309 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001310{
1311 if (size() == 0)
1312 __begin_node() = __end_node();
1313 else
1314 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001315 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316 __t.__begin_node() = __t.__end_node();
1317 __t.__end_node()->__left_ = nullptr;
1318 __t.size() = 0;
1319 }
1320}
1321
1322template <class _Tp, class _Compare, class _Allocator>
1323__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1324 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001325 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326{
1327 if (__a == __t.__alloc())
1328 {
1329 if (__t.size() == 0)
1330 __begin_node() = __end_node();
1331 else
1332 {
1333 __begin_node() = __t.__begin_node();
1334 __end_node()->__left_ = __t.__end_node()->__left_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001335 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001336 size() = __t.size();
1337 __t.__begin_node() = __t.__end_node();
1338 __t.__end_node()->__left_ = nullptr;
1339 __t.size() = 0;
1340 }
1341 }
1342 else
1343 {
1344 __begin_node() = __end_node();
1345 }
1346}
1347
1348template <class _Tp, class _Compare, class _Allocator>
1349void
1350__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001351 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1352 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353{
1354 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1355 __begin_node_ = __t.__begin_node_;
1356 __pair1_.first() = __t.__pair1_.first();
1357 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001358 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359 if (size() == 0)
1360 __begin_node() = __end_node();
1361 else
1362 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001363 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364 __t.__begin_node() = __t.__end_node();
1365 __t.__end_node()->__left_ = nullptr;
1366 __t.size() = 0;
1367 }
1368}
1369
1370template <class _Tp, class _Compare, class _Allocator>
1371void
1372__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1373{
1374 if (__node_alloc() == __t.__node_alloc())
1375 __move_assign(__t, true_type());
1376 else
1377 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001378 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 const_iterator __e = end();
1380 if (size() != 0)
1381 {
1382 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001383#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001384 try
1385 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001386#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 while (__cache != nullptr && __t.size() != 0)
1388 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001389 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 __node_pointer __next = __detach(__cache);
1391 __node_insert_multi(__cache);
1392 __cache = __next;
1393 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001394#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 }
1396 catch (...)
1397 {
1398 while (__cache->__parent_ != nullptr)
1399 __cache = static_cast<__node_pointer>(__cache->__parent_);
1400 destroy(__cache);
1401 throw;
1402 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001403#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404 if (__cache != nullptr)
1405 {
1406 while (__cache->__parent_ != nullptr)
1407 __cache = static_cast<__node_pointer>(__cache->__parent_);
1408 destroy(__cache);
1409 }
1410 }
1411 while (__t.size() != 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001412 __insert_multi(__e, _VSTD::move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001413 }
1414}
1415
1416template <class _Tp, class _Compare, class _Allocator>
1417__tree<_Tp, _Compare, _Allocator>&
1418__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001419 _NOEXCEPT_(
1420 __node_traits::propagate_on_container_move_assignment::value &&
1421 is_nothrow_move_assignable<value_compare>::value &&
1422 is_nothrow_move_assignable<__node_allocator>::value)
1423
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424{
1425 __move_assign(__t, integral_constant<bool,
1426 __node_traits::propagate_on_container_move_assignment::value>());
1427 return *this;
1428}
1429
Howard Hinnant73d21a42010-09-04 23:28:19 +00001430#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431
1432template <class _Tp, class _Compare, class _Allocator>
1433__tree<_Tp, _Compare, _Allocator>::~__tree()
1434{
1435 destroy(__root());
1436}
1437
1438template <class _Tp, class _Compare, class _Allocator>
1439void
Howard Hinnant7686add2011-06-04 14:31:57 +00001440__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441{
1442 if (__nd != nullptr)
1443 {
1444 destroy(static_cast<__node_pointer>(__nd->__left_));
1445 destroy(static_cast<__node_pointer>(__nd->__right_));
1446 __node_allocator& __na = __node_alloc();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001447 __node_traits::destroy(__na, _VSTD::addressof(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448 __node_traits::deallocate(__na, __nd, 1);
1449 }
1450}
1451
1452template <class _Tp, class _Compare, class _Allocator>
1453void
1454__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001455 _NOEXCEPT_(
1456 __is_nothrow_swappable<value_compare>::value &&
1457 (!__node_traits::propagate_on_container_swap::value ||
1458 __is_nothrow_swappable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001460 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001461 swap(__begin_node_, __t.__begin_node_);
1462 swap(__pair1_.first(), __t.__pair1_.first());
1463 __swap_alloc(__node_alloc(), __t.__node_alloc());
1464 __pair3_.swap(__t.__pair3_);
1465 if (size() == 0)
1466 __begin_node() = __end_node();
1467 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001468 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 if (__t.size() == 0)
1470 __t.__begin_node() = __t.__end_node();
1471 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001472 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473}
1474
1475template <class _Tp, class _Compare, class _Allocator>
1476void
Howard Hinnant7686add2011-06-04 14:31:57 +00001477__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001478{
1479 destroy(__root());
1480 size() = 0;
1481 __begin_node() = __end_node();
1482 __end_node()->__left_ = nullptr;
1483}
1484
1485// Find lower_bound place to insert
1486// Set __parent to parent of null leaf
1487// Return reference to null leaf
1488template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001489typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1490__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 const value_type& __v)
1492{
1493 __node_pointer __nd = __root();
1494 if (__nd != nullptr)
1495 {
1496 while (true)
1497 {
1498 if (value_comp()(__nd->__value_, __v))
1499 {
1500 if (__nd->__right_ != nullptr)
1501 __nd = static_cast<__node_pointer>(__nd->__right_);
1502 else
1503 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001504 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001505 return __parent->__right_;
1506 }
1507 }
1508 else
1509 {
1510 if (__nd->__left_ != nullptr)
1511 __nd = static_cast<__node_pointer>(__nd->__left_);
1512 else
1513 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001514 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 return __parent->__left_;
1516 }
1517 }
1518 }
1519 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001520 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001521 return __parent->__left_;
1522}
1523
1524// Find upper_bound place to insert
1525// Set __parent to parent of null leaf
1526// Return reference to null leaf
1527template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001528typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1529__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001530 const value_type& __v)
1531{
1532 __node_pointer __nd = __root();
1533 if (__nd != nullptr)
1534 {
1535 while (true)
1536 {
1537 if (value_comp()(__v, __nd->__value_))
1538 {
1539 if (__nd->__left_ != nullptr)
1540 __nd = static_cast<__node_pointer>(__nd->__left_);
1541 else
1542 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001543 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 return __parent->__left_;
1545 }
1546 }
1547 else
1548 {
1549 if (__nd->__right_ != nullptr)
1550 __nd = static_cast<__node_pointer>(__nd->__right_);
1551 else
1552 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001553 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001554 return __parent->__right_;
1555 }
1556 }
1557 }
1558 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001559 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001560 return __parent->__left_;
1561}
1562
1563// Find leaf place to insert closest to __hint
1564// First check prior to __hint.
1565// Next check after __hint.
1566// Next do O(log N) search.
1567// Set __parent to parent of null leaf
1568// Return reference to null leaf
1569template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001570typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001571__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001572 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 const value_type& __v)
1574{
1575 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1576 {
1577 // __v <= *__hint
1578 const_iterator __prior = __hint;
1579 if (__prior == begin() || !value_comp()(__v, *--__prior))
1580 {
1581 // *prev(__hint) <= __v <= *__hint
1582 if (__hint.__ptr_->__left_ == nullptr)
1583 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001584 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001585 return __parent->__left_;
1586 }
1587 else
1588 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001589 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 return __parent->__right_;
1591 }
1592 }
1593 // __v < *prev(__hint)
1594 return __find_leaf_high(__parent, __v);
1595 }
1596 // else __v > *__hint
1597 return __find_leaf_low(__parent, __v);
1598}
1599
1600// Find place to insert if __v doesn't exist
1601// Set __parent to parent of null leaf
1602// Return reference to null leaf
1603// If __v exists, set parent to node of __v and return reference to node of __v
1604template <class _Tp, class _Compare, class _Allocator>
1605template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001606typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1607__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001608 const _Key& __v)
1609{
1610 __node_pointer __nd = __root();
1611 if (__nd != nullptr)
1612 {
1613 while (true)
1614 {
1615 if (value_comp()(__v, __nd->__value_))
1616 {
1617 if (__nd->__left_ != nullptr)
1618 __nd = static_cast<__node_pointer>(__nd->__left_);
1619 else
1620 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001621 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001622 return __parent->__left_;
1623 }
1624 }
1625 else if (value_comp()(__nd->__value_, __v))
1626 {
1627 if (__nd->__right_ != nullptr)
1628 __nd = static_cast<__node_pointer>(__nd->__right_);
1629 else
1630 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001631 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001632 return __parent->__right_;
1633 }
1634 }
1635 else
1636 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001637 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001638 return __parent;
1639 }
1640 }
1641 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001642 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643 return __parent->__left_;
1644}
1645
1646// Find place to insert if __v doesn't exist
1647// First check prior to __hint.
1648// Next check after __hint.
1649// Next do O(log N) search.
1650// Set __parent to parent of null leaf
1651// Return reference to null leaf
1652// If __v exists, set parent to node of __v and return reference to node of __v
1653template <class _Tp, class _Compare, class _Allocator>
1654template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001655typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001657 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001658 const _Key& __v)
1659{
1660 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1661 {
1662 // __v < *__hint
1663 const_iterator __prior = __hint;
1664 if (__prior == begin() || value_comp()(*--__prior, __v))
1665 {
1666 // *prev(__hint) < __v < *__hint
1667 if (__hint.__ptr_->__left_ == nullptr)
1668 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001669 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001670 return __parent->__left_;
1671 }
1672 else
1673 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001674 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 return __parent->__right_;
1676 }
1677 }
1678 // __v <= *prev(__hint)
1679 return __find_equal(__parent, __v);
1680 }
1681 else if (value_comp()(*__hint, __v)) // check after
1682 {
1683 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00001684 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 if (__next == end() || value_comp()(__v, *__next))
1686 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001687 // *__hint < __v < *_VSTD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001688 if (__hint.__ptr_->__right_ == nullptr)
1689 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001690 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691 return __parent->__right_;
1692 }
1693 else
1694 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001695 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 return __parent->__left_;
1697 }
1698 }
1699 // *next(__hint) <= __v
1700 return __find_equal(__parent, __v);
1701 }
1702 // else __v == *__hint
Howard Hinnant70342b92013-06-19 21:29:40 +00001703 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001704 return __parent;
1705}
1706
1707template <class _Tp, class _Compare, class _Allocator>
1708void
1709__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1710 __node_base_pointer& __child,
1711 __node_base_pointer __new_node)
1712{
1713 __new_node->__left_ = nullptr;
1714 __new_node->__right_ = nullptr;
1715 __new_node->__parent_ = __parent;
1716 __child = __new_node;
1717 if (__begin_node()->__left_ != nullptr)
1718 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1719 __tree_balance_after_insert(__end_node()->__left_, __child);
1720 ++size();
1721}
1722
Howard Hinnant73d21a42010-09-04 23:28:19 +00001723#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1724#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001725
1726template <class _Tp, class _Compare, class _Allocator>
1727template <class ..._Args>
1728typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1729__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1730{
1731 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001732 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001733 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734 __h.get_deleter().__value_constructed = true;
1735 return __h;
1736}
1737
1738template <class _Tp, class _Compare, class _Allocator>
1739template <class... _Args>
1740pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1741__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1742{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001743 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 __node_base_pointer __parent;
1745 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1746 __node_pointer __r = static_cast<__node_pointer>(__child);
1747 bool __inserted = false;
1748 if (__child == nullptr)
1749 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001750 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001751 __r = __h.release();
1752 __inserted = true;
1753 }
1754 return pair<iterator, bool>(iterator(__r), __inserted);
1755}
1756
1757template <class _Tp, class _Compare, class _Allocator>
1758template <class... _Args>
1759typename __tree<_Tp, _Compare, _Allocator>::iterator
1760__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1761{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001762 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001763 __node_base_pointer __parent;
1764 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1765 __node_pointer __r = static_cast<__node_pointer>(__child);
1766 if (__child == nullptr)
1767 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001768 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001769 __r = __h.release();
1770 }
1771 return iterator(__r);
1772}
1773
1774template <class _Tp, class _Compare, class _Allocator>
1775template <class... _Args>
1776typename __tree<_Tp, _Compare, _Allocator>::iterator
1777__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1778{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001779 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001780 __node_base_pointer __parent;
1781 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001782 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 return iterator(static_cast<__node_pointer>(__h.release()));
1784}
1785
1786template <class _Tp, class _Compare, class _Allocator>
1787template <class... _Args>
1788typename __tree<_Tp, _Compare, _Allocator>::iterator
1789__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1790 _Args&&... __args)
1791{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001792 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793 __node_base_pointer __parent;
1794 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001795 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796 return iterator(static_cast<__node_pointer>(__h.release()));
1797}
1798
Howard Hinnant73d21a42010-09-04 23:28:19 +00001799#endif // _LIBCPP_HAS_NO_VARIADICS
1800
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001802template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001803pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Howard Hinnant99968442011-11-29 18:15:50 +00001804__tree<_Tp, _Compare, _Allocator>::__insert_unique(_Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805{
Howard Hinnant99968442011-11-29 18:15:50 +00001806 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001807 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1808 if (__r.second)
1809 __h.release();
1810 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811}
1812
1813template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001814template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001816__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817{
Howard Hinnant99968442011-11-29 18:15:50 +00001818 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001819 iterator __r = __node_insert_unique(__p, __h.get());
1820 if (__r.__ptr_ == __h.get())
1821 __h.release();
1822 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823}
1824
1825template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001826template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001828__tree<_Tp, _Compare, _Allocator>::__insert_multi(_Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829{
Howard Hinnant99968442011-11-29 18:15:50 +00001830 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 __node_base_pointer __parent;
1832 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001833 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834 return iterator(__h.release());
1835}
1836
1837template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001838template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001839typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001840__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001841{
Howard Hinnant99968442011-11-29 18:15:50 +00001842 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001843 __node_base_pointer __parent;
1844 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001845 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001846 return iterator(__h.release());
1847}
1848
Howard Hinnant73d21a42010-09-04 23:28:19 +00001849#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850
1851template <class _Tp, class _Compare, class _Allocator>
1852typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1853__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1854{
1855 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001856 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001857 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858 __h.get_deleter().__value_constructed = true;
Howard Hinnant9a894d92013-08-22 18:29:50 +00001859 return _VSTD::move(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860}
1861
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001862#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1863
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864template <class _Tp, class _Compare, class _Allocator>
1865pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1866__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1867{
1868 __node_base_pointer __parent;
1869 __node_base_pointer& __child = __find_equal(__parent, __v);
1870 __node_pointer __r = static_cast<__node_pointer>(__child);
1871 bool __inserted = false;
1872 if (__child == nullptr)
1873 {
1874 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001875 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001876 __r = __h.release();
1877 __inserted = true;
1878 }
1879 return pair<iterator, bool>(iterator(__r), __inserted);
1880}
1881
1882template <class _Tp, class _Compare, class _Allocator>
1883typename __tree<_Tp, _Compare, _Allocator>::iterator
1884__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1885{
1886 __node_base_pointer __parent;
1887 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1888 __node_pointer __r = static_cast<__node_pointer>(__child);
1889 if (__child == nullptr)
1890 {
1891 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001892 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 __r = __h.release();
1894 }
1895 return iterator(__r);
1896}
1897
1898template <class _Tp, class _Compare, class _Allocator>
1899typename __tree<_Tp, _Compare, _Allocator>::iterator
1900__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1901{
1902 __node_base_pointer __parent;
1903 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1904 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001905 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 return iterator(__h.release());
1907}
1908
1909template <class _Tp, class _Compare, class _Allocator>
1910typename __tree<_Tp, _Compare, _Allocator>::iterator
1911__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1912{
1913 __node_base_pointer __parent;
1914 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1915 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001916 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001917 return iterator(__h.release());
1918}
1919
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001920template <class _Tp, class _Compare, class _Allocator>
1921pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1922__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1923{
1924 __node_base_pointer __parent;
1925 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1926 __node_pointer __r = static_cast<__node_pointer>(__child);
1927 bool __inserted = false;
1928 if (__child == nullptr)
1929 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001930 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001931 __r = __nd;
1932 __inserted = true;
1933 }
1934 return pair<iterator, bool>(iterator(__r), __inserted);
1935}
1936
1937template <class _Tp, class _Compare, class _Allocator>
1938typename __tree<_Tp, _Compare, _Allocator>::iterator
1939__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1940 __node_pointer __nd)
1941{
1942 __node_base_pointer __parent;
1943 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1944 __node_pointer __r = static_cast<__node_pointer>(__child);
1945 if (__child == nullptr)
1946 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001947 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001948 __r = __nd;
1949 }
1950 return iterator(__r);
1951}
1952
1953template <class _Tp, class _Compare, class _Allocator>
1954typename __tree<_Tp, _Compare, _Allocator>::iterator
1955__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1956{
1957 __node_base_pointer __parent;
1958 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001959 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001960 return iterator(__nd);
1961}
1962
1963template <class _Tp, class _Compare, class _Allocator>
1964typename __tree<_Tp, _Compare, _Allocator>::iterator
1965__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1966 __node_pointer __nd)
1967{
1968 __node_base_pointer __parent;
1969 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001970 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001971 return iterator(__nd);
1972}
1973
1974template <class _Tp, class _Compare, class _Allocator>
1975typename __tree<_Tp, _Compare, _Allocator>::iterator
1976__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1977{
Howard Hinnant70342b92013-06-19 21:29:40 +00001978 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001979 iterator __r(__np);
1980 ++__r;
1981 if (__begin_node() == __np)
1982 __begin_node() = __r.__ptr_;
1983 --size();
1984 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001985 __tree_remove(__end_node()->__left_,
1986 static_cast<__node_base_pointer>(__np));
Marshall Clow140e8f52014-04-11 08:22:42 +00001987 __node_traits::destroy(__na, const_cast<value_type*>(_VSTD::addressof(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 __node_traits::deallocate(__na, __np, 1);
1989 return __r;
1990}
1991
1992template <class _Tp, class _Compare, class _Allocator>
1993typename __tree<_Tp, _Compare, _Allocator>::iterator
1994__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1995{
1996 while (__f != __l)
1997 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00001998 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999}
2000
2001template <class _Tp, class _Compare, class _Allocator>
2002template <class _Key>
2003typename __tree<_Tp, _Compare, _Allocator>::size_type
2004__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2005{
2006 iterator __i = find(__k);
2007 if (__i == end())
2008 return 0;
2009 erase(__i);
2010 return 1;
2011}
2012
2013template <class _Tp, class _Compare, class _Allocator>
2014template <class _Key>
2015typename __tree<_Tp, _Compare, _Allocator>::size_type
2016__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2017{
2018 pair<iterator, iterator> __p = __equal_range_multi(__k);
2019 size_type __r = 0;
2020 for (; __p.first != __p.second; ++__r)
2021 __p.first = erase(__p.first);
2022 return __r;
2023}
2024
2025template <class _Tp, class _Compare, class _Allocator>
2026template <class _Key>
2027typename __tree<_Tp, _Compare, _Allocator>::iterator
2028__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2029{
2030 iterator __p = __lower_bound(__v, __root(), __end_node());
2031 if (__p != end() && !value_comp()(__v, *__p))
2032 return __p;
2033 return end();
2034}
2035
2036template <class _Tp, class _Compare, class _Allocator>
2037template <class _Key>
2038typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2039__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2040{
2041 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2042 if (__p != end() && !value_comp()(__v, *__p))
2043 return __p;
2044 return end();
2045}
2046
2047template <class _Tp, class _Compare, class _Allocator>
2048template <class _Key>
2049typename __tree<_Tp, _Compare, _Allocator>::size_type
2050__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2051{
2052 __node_const_pointer __result = __end_node();
2053 __node_const_pointer __rt = __root();
2054 while (__rt != nullptr)
2055 {
2056 if (value_comp()(__k, __rt->__value_))
2057 {
2058 __result = __rt;
2059 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2060 }
2061 else if (value_comp()(__rt->__value_, __k))
2062 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2063 else
2064 return 1;
2065 }
2066 return 0;
2067}
2068
2069template <class _Tp, class _Compare, class _Allocator>
2070template <class _Key>
2071typename __tree<_Tp, _Compare, _Allocator>::size_type
2072__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2073{
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002074 __node_const_pointer __result = __end_node();
2075 __node_const_pointer __rt = __root();
2076 while (__rt != nullptr)
2077 {
2078 if (value_comp()(__k, __rt->__value_))
2079 {
2080 __result = __rt;
2081 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2082 }
2083 else if (value_comp()(__rt->__value_, __k))
2084 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2085 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002086 return _VSTD::distance(
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2088 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
2089 );
2090 }
2091 return 0;
2092}
2093
2094template <class _Tp, class _Compare, class _Allocator>
2095template <class _Key>
2096typename __tree<_Tp, _Compare, _Allocator>::iterator
2097__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2098 __node_pointer __root,
2099 __node_pointer __result)
2100{
2101 while (__root != nullptr)
2102 {
2103 if (!value_comp()(__root->__value_, __v))
2104 {
2105 __result = __root;
2106 __root = static_cast<__node_pointer>(__root->__left_);
2107 }
2108 else
2109 __root = static_cast<__node_pointer>(__root->__right_);
2110 }
2111 return iterator(__result);
2112}
2113
2114template <class _Tp, class _Compare, class _Allocator>
2115template <class _Key>
2116typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2117__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2118 __node_const_pointer __root,
2119 __node_const_pointer __result) const
2120{
2121 while (__root != nullptr)
2122 {
2123 if (!value_comp()(__root->__value_, __v))
2124 {
2125 __result = __root;
2126 __root = static_cast<__node_const_pointer>(__root->__left_);
2127 }
2128 else
2129 __root = static_cast<__node_const_pointer>(__root->__right_);
2130 }
2131 return const_iterator(__result);
2132}
2133
2134template <class _Tp, class _Compare, class _Allocator>
2135template <class _Key>
2136typename __tree<_Tp, _Compare, _Allocator>::iterator
2137__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2138 __node_pointer __root,
2139 __node_pointer __result)
2140{
2141 while (__root != nullptr)
2142 {
2143 if (value_comp()(__v, __root->__value_))
2144 {
2145 __result = __root;
2146 __root = static_cast<__node_pointer>(__root->__left_);
2147 }
2148 else
2149 __root = static_cast<__node_pointer>(__root->__right_);
2150 }
2151 return iterator(__result);
2152}
2153
2154template <class _Tp, class _Compare, class _Allocator>
2155template <class _Key>
2156typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2157__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2158 __node_const_pointer __root,
2159 __node_const_pointer __result) const
2160{
2161 while (__root != nullptr)
2162 {
2163 if (value_comp()(__v, __root->__value_))
2164 {
2165 __result = __root;
2166 __root = static_cast<__node_const_pointer>(__root->__left_);
2167 }
2168 else
2169 __root = static_cast<__node_const_pointer>(__root->__right_);
2170 }
2171 return const_iterator(__result);
2172}
2173
2174template <class _Tp, class _Compare, class _Allocator>
2175template <class _Key>
2176pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2177 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2178__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2179{
Howard Hinnant99968442011-11-29 18:15:50 +00002180 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002181 __node_pointer __result = __end_node();
2182 __node_pointer __rt = __root();
2183 while (__rt != nullptr)
2184 {
2185 if (value_comp()(__k, __rt->__value_))
2186 {
2187 __result = __rt;
2188 __rt = static_cast<__node_pointer>(__rt->__left_);
2189 }
2190 else if (value_comp()(__rt->__value_, __k))
2191 __rt = static_cast<__node_pointer>(__rt->__right_);
2192 else
Howard Hinnant99968442011-11-29 18:15:50 +00002193 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002194 iterator(
2195 __rt->__right_ != nullptr ?
2196 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2197 : __result));
2198 }
Howard Hinnant99968442011-11-29 18:15:50 +00002199 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200}
2201
2202template <class _Tp, class _Compare, class _Allocator>
2203template <class _Key>
2204pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2205 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2206__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2207{
Howard Hinnant99968442011-11-29 18:15:50 +00002208 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002209 __node_const_pointer __result = __end_node();
2210 __node_const_pointer __rt = __root();
2211 while (__rt != nullptr)
2212 {
2213 if (value_comp()(__k, __rt->__value_))
2214 {
2215 __result = __rt;
2216 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2217 }
2218 else if (value_comp()(__rt->__value_, __k))
2219 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2220 else
Howard Hinnant99968442011-11-29 18:15:50 +00002221 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002222 const_iterator(
2223 __rt->__right_ != nullptr ?
2224 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2225 : __result));
2226 }
Howard Hinnant99968442011-11-29 18:15:50 +00002227 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228}
2229
2230template <class _Tp, class _Compare, class _Allocator>
2231template <class _Key>
2232pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2233 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2234__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2235{
Howard Hinnant99968442011-11-29 18:15:50 +00002236 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002237 __node_pointer __result = __end_node();
2238 __node_pointer __rt = __root();
2239 while (__rt != nullptr)
2240 {
2241 if (value_comp()(__k, __rt->__value_))
2242 {
2243 __result = __rt;
2244 __rt = static_cast<__node_pointer>(__rt->__left_);
2245 }
2246 else if (value_comp()(__rt->__value_, __k))
2247 __rt = static_cast<__node_pointer>(__rt->__right_);
2248 else
Howard Hinnant99968442011-11-29 18:15:50 +00002249 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002250 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2251 }
Howard Hinnant99968442011-11-29 18:15:50 +00002252 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253}
2254
2255template <class _Tp, class _Compare, class _Allocator>
2256template <class _Key>
2257pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2258 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2259__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2260{
Howard Hinnant99968442011-11-29 18:15:50 +00002261 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262 __node_const_pointer __result = __end_node();
2263 __node_const_pointer __rt = __root();
2264 while (__rt != nullptr)
2265 {
2266 if (value_comp()(__k, __rt->__value_))
2267 {
2268 __result = __rt;
2269 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2270 }
2271 else if (value_comp()(__rt->__value_, __k))
2272 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2273 else
Howard Hinnant99968442011-11-29 18:15:50 +00002274 return _Pp(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002275 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2276 }
Howard Hinnant99968442011-11-29 18:15:50 +00002277 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002278}
2279
2280template <class _Tp, class _Compare, class _Allocator>
2281typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002282__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002283{
Howard Hinnant70342b92013-06-19 21:29:40 +00002284 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 if (__begin_node() == __np)
2286 {
2287 if (__np->__right_ != nullptr)
2288 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2289 else
2290 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2291 }
2292 --size();
2293 __tree_remove(__end_node()->__left_,
2294 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002295 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296}
2297
Howard Hinnant7686add2011-06-04 14:31:57 +00002298template <class _Tp, class _Compare, class _Allocator>
2299inline _LIBCPP_INLINE_VISIBILITY
2300void
2301swap(__tree<_Tp, _Compare, _Allocator>& __x,
2302 __tree<_Tp, _Compare, _Allocator>& __y)
2303 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2304{
2305 __x.swap(__y);
2306}
2307
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002308_LIBCPP_END_NAMESPACE_STD
2309
2310#endif // _LIBCPP___TREE