blob: d768b3837c5427ebf538fb57900d66007c152599 [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;
617 typedef typename __node::base __node_base;
618 typedef typename __node_base::pointer __node_base_pointer;
619
620 __node_pointer __ptr_;
621
622 typedef pointer_traits<__node_pointer> __pointer_traits;
623public:
624 typedef bidirectional_iterator_tag iterator_category;
625 typedef _Tp value_type;
626 typedef _DiffType difference_type;
627 typedef value_type& reference;
628 typedef typename pointer_traits<__node_pointer>::template
629#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
630 rebind<value_type>
631#else
632 rebind<value_type>::other
633#endif
634 pointer;
635
Marshall Clow051c8482013-08-08 21:52:50 +0000636 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
637#if _LIBCPP_STD_VER > 11
638 : __ptr_(nullptr)
639#endif
640 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641
Howard Hinnant333f50d2010-09-21 20:16:37 +0000642 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000643 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
644 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645
Howard Hinnant333f50d2010-09-21 20:16:37 +0000646 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647 __tree_iterator& operator++()
648 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
649 return *this;}
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
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000655 __tree_iterator& operator--()
656 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
657 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000658 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659 __tree_iterator operator--(int)
660 {__tree_iterator __t(*this); --(*this); return __t;}
661
Howard Hinnant333f50d2010-09-21 20:16:37 +0000662 friend _LIBCPP_INLINE_VISIBILITY
663 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000665 friend _LIBCPP_INLINE_VISIBILITY
666 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000667 {return !(__x == __y);}
668
669private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000670 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000671 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000673 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
674 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
675 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
676 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
677 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
678 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679};
680
681template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000682class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000683{
684 typedef _ConstNodePtr __node_pointer;
685 typedef typename pointer_traits<__node_pointer>::element_type __node;
Howard Hinnant70342b92013-06-19 21:29:40 +0000686 typedef typename __node::base __node_base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000687 typedef typename pointer_traits<__node_pointer>::template
688#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
689 rebind<__node_base>
690#else
691 rebind<__node_base>::other
692#endif
693 __node_base_pointer;
694
695 __node_pointer __ptr_;
696
697 typedef pointer_traits<__node_pointer> __pointer_traits;
698public:
699 typedef bidirectional_iterator_tag iterator_category;
700 typedef _Tp value_type;
701 typedef _DiffType difference_type;
702 typedef const value_type& reference;
703 typedef typename pointer_traits<__node_pointer>::template
704#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
705 rebind<const value_type>
706#else
707 rebind<const value_type>::other
708#endif
709 pointer;
710
Marshall Clow051c8482013-08-08 21:52:50 +0000711 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
712#if _LIBCPP_STD_VER > 11
713 : __ptr_(nullptr)
714#endif
715 {}
716
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000717private:
718 typedef typename remove_const<__node>::type __non_const_node;
719 typedef typename pointer_traits<__node_pointer>::template
720#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
721 rebind<__non_const_node>
722#else
723 rebind<__non_const_node>::other
724#endif
725 __non_const_node_pointer;
726 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
727 __non_const_iterator;
728public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000730 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
731 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732
Howard Hinnant333f50d2010-09-21 20:16:37 +0000733 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000734 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
735 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736
Howard Hinnant333f50d2010-09-21 20:16:37 +0000737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 __tree_const_iterator& operator++()
739 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
740 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000741 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742 __tree_const_iterator operator++(int)
743 {__tree_const_iterator __t(*this); ++(*this); return __t;}
744
Howard Hinnant333f50d2010-09-21 20:16:37 +0000745 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746 __tree_const_iterator& operator--()
747 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
748 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000750 __tree_const_iterator operator--(int)
751 {__tree_const_iterator __t(*this); --(*this); return __t;}
752
Howard Hinnant333f50d2010-09-21 20:16:37 +0000753 friend _LIBCPP_INLINE_VISIBILITY
754 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000755 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000756 friend _LIBCPP_INLINE_VISIBILITY
757 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758 {return !(__x == __y);}
759
760private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000761 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000762 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
763 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000764 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000765 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
766 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
767 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
768 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
769 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000770};
771
772template <class _Tp, class _Compare, class _Allocator>
773class __tree
774{
775public:
776 typedef _Tp value_type;
777 typedef _Compare value_compare;
778 typedef _Allocator allocator_type;
779 typedef allocator_traits<allocator_type> __alloc_traits;
780 typedef typename __alloc_traits::pointer pointer;
781 typedef typename __alloc_traits::const_pointer const_pointer;
782 typedef typename __alloc_traits::size_type size_type;
783 typedef typename __alloc_traits::difference_type difference_type;
784
Howard Hinnant70342b92013-06-19 21:29:40 +0000785 typedef typename __alloc_traits::void_pointer __void_pointer;
786
787 typedef __tree_node<value_type, __void_pointer> __node;
788 typedef __tree_node_base<__void_pointer> __node_base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789 typedef typename __alloc_traits::template
790#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
791 rebind_alloc<__node>
792#else
793 rebind_alloc<__node>::other
794#endif
795 __node_allocator;
796 typedef allocator_traits<__node_allocator> __node_traits;
797 typedef typename __node_traits::pointer __node_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000798 typedef typename __node_traits::pointer __node_const_pointer;
Howard Hinnantd615e472011-04-03 20:05:29 +0000799 typedef typename __node_base::pointer __node_base_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000800 typedef typename __node_base::pointer __node_base_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000801private:
Howard Hinnantd615e472011-04-03 20:05:29 +0000802 typedef typename __node_base::base __end_node_t;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000803 typedef typename pointer_traits<__node_pointer>::template
804#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
805 rebind<__end_node_t>
806#else
807 rebind<__end_node_t>::other
808#endif
809 __end_node_ptr;
810 typedef typename pointer_traits<__node_pointer>::template
811#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
Howard Hinnant70342b92013-06-19 21:29:40 +0000812 rebind<__end_node_t>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000813#else
Howard Hinnant70342b92013-06-19 21:29:40 +0000814 rebind<__end_node_t>::other
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815#endif
816 __end_node_const_ptr;
817
818 __node_pointer __begin_node_;
819 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
820 __compressed_pair<size_type, value_compare> __pair3_;
821
822public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000824 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825 {
826 return static_cast<__node_pointer>
827 (
828 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
829 );
830 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000832 __node_const_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000833 {
834 return static_cast<__node_const_pointer>
835 (
Howard Hinnant70342b92013-06-19 21:29:40 +0000836 pointer_traits<__end_node_const_ptr>::pointer_to(const_cast<__end_node_t&>(__pair1_.first()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000837 );
838 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000839 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000840 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000841private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000842 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000843 const __node_allocator& __node_alloc() const _NOEXCEPT
844 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000846 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000848 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000849public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000851 allocator_type __alloc() const _NOEXCEPT
852 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000855 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000858 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000860 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000861 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000862 const value_compare& value_comp() const _NOEXCEPT
863 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000864public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000866 __node_pointer __root() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867 {return static_cast<__node_pointer> (__end_node()->__left_);}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000868 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000869 __node_const_pointer __root() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000870 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
871
872 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +0000873 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000874
Howard Hinnant7686add2011-06-04 14:31:57 +0000875 explicit __tree(const value_compare& __comp)
876 _NOEXCEPT_(
877 is_nothrow_default_constructible<__node_allocator>::value &&
878 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879 explicit __tree(const allocator_type& __a);
880 __tree(const value_compare& __comp, const allocator_type& __a);
881 __tree(const __tree& __t);
882 __tree& operator=(const __tree& __t);
883 template <class _InputIterator>
884 void __assign_unique(_InputIterator __first, _InputIterator __last);
885 template <class _InputIterator>
886 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000887#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-06-04 14:31:57 +0000888 __tree(__tree&& __t)
889 _NOEXCEPT_(
890 is_nothrow_move_constructible<__node_allocator>::value &&
891 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +0000893 __tree& operator=(__tree&& __t)
894 _NOEXCEPT_(
895 __node_traits::propagate_on_container_move_assignment::value &&
896 is_nothrow_move_assignable<value_compare>::value &&
897 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000898#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000899
900 ~__tree();
901
Howard Hinnant333f50d2010-09-21 20:16:37 +0000902 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000903 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000904 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000905 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000906 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000907 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000908 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000909 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000910
Howard Hinnant333f50d2010-09-21 20:16:37 +0000911 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000912 size_type max_size() const _NOEXCEPT
913 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000914
Howard Hinnant7686add2011-06-04 14:31:57 +0000915 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000916
Howard Hinnant7686add2011-06-04 14:31:57 +0000917 void swap(__tree& __t)
918 _NOEXCEPT_(
919 __is_nothrow_swappable<value_compare>::value &&
920 (!__node_traits::propagate_on_container_swap::value ||
921 __is_nothrow_swappable<__node_allocator>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000922
Howard Hinnant73d21a42010-09-04 23:28:19 +0000923#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
924#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925 template <class... _Args>
926 pair<iterator, bool>
927 __emplace_unique(_Args&&... __args);
928 template <class... _Args>
929 iterator
930 __emplace_multi(_Args&&... __args);
931
932 template <class... _Args>
933 iterator
934 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
935 template <class... _Args>
936 iterator
937 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000938#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000939
Howard Hinnant99968442011-11-29 18:15:50 +0000940 template <class _Vp>
941 pair<iterator, bool> __insert_unique(_Vp&& __v);
942 template <class _Vp>
943 iterator __insert_unique(const_iterator __p, _Vp&& __v);
944 template <class _Vp>
945 iterator __insert_multi(_Vp&& __v);
946 template <class _Vp>
947 iterator __insert_multi(const_iterator __p, _Vp&& __v);
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +0000948#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000949
950 pair<iterator, bool> __insert_unique(const value_type& __v);
951 iterator __insert_unique(const_iterator __p, const value_type& __v);
952 iterator __insert_multi(const value_type& __v);
953 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000954
955 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
956 iterator __node_insert_unique(const_iterator __p,
957 __node_pointer __nd);
958
959 iterator __node_insert_multi(__node_pointer __nd);
960 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
961
962 iterator erase(const_iterator __p);
963 iterator erase(const_iterator __f, const_iterator __l);
964 template <class _Key>
965 size_type __erase_unique(const _Key& __k);
966 template <class _Key>
967 size_type __erase_multi(const _Key& __k);
968
969 void __insert_node_at(__node_base_pointer __parent,
970 __node_base_pointer& __child,
971 __node_base_pointer __new_node);
972
973 template <class _Key>
974 iterator find(const _Key& __v);
975 template <class _Key>
976 const_iterator find(const _Key& __v) const;
977
978 template <class _Key>
979 size_type __count_unique(const _Key& __k) const;
980 template <class _Key>
981 size_type __count_multi(const _Key& __k) const;
982
983 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000984 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985 iterator lower_bound(const _Key& __v)
986 {return __lower_bound(__v, __root(), __end_node());}
987 template <class _Key>
988 iterator __lower_bound(const _Key& __v,
989 __node_pointer __root,
990 __node_pointer __result);
991 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000993 const_iterator lower_bound(const _Key& __v) const
994 {return __lower_bound(__v, __root(), __end_node());}
995 template <class _Key>
996 const_iterator __lower_bound(const _Key& __v,
997 __node_const_pointer __root,
998 __node_const_pointer __result) const;
999 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001001 iterator upper_bound(const _Key& __v)
1002 {return __upper_bound(__v, __root(), __end_node());}
1003 template <class _Key>
1004 iterator __upper_bound(const _Key& __v,
1005 __node_pointer __root,
1006 __node_pointer __result);
1007 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001008 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001009 const_iterator upper_bound(const _Key& __v) const
1010 {return __upper_bound(__v, __root(), __end_node());}
1011 template <class _Key>
1012 const_iterator __upper_bound(const _Key& __v,
1013 __node_const_pointer __root,
1014 __node_const_pointer __result) const;
1015 template <class _Key>
1016 pair<iterator, iterator>
1017 __equal_range_unique(const _Key& __k);
1018 template <class _Key>
1019 pair<const_iterator, const_iterator>
1020 __equal_range_unique(const _Key& __k) const;
1021
1022 template <class _Key>
1023 pair<iterator, iterator>
1024 __equal_range_multi(const _Key& __k);
1025 template <class _Key>
1026 pair<const_iterator, const_iterator>
1027 __equal_range_multi(const _Key& __k) const;
1028
Howard Hinnant99968442011-11-29 18:15:50 +00001029 typedef __tree_node_destructor<__node_allocator> _Dp;
1030 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031
Howard Hinnant8b537682011-06-04 17:10:24 +00001032 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033private:
Howard Hinnantd615e472011-04-03 20:05:29 +00001034 typename __node_base::pointer&
1035 __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v);
1036 typename __node_base::pointer&
1037 __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v);
1038 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 __find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001040 typename __node_base::pointer& __parent, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001042 typename __node_base::pointer&
1043 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001044 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001045 typename __node_base::pointer&
1046 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047 const _Key& __v);
1048
Howard Hinnant73d21a42010-09-04 23:28:19 +00001049#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 template <class ..._Args>
1051 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001052#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053 __node_holder __construct_node(const value_type& __v);
1054#endif
1055
Howard Hinnant7686add2011-06-04 14:31:57 +00001056 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057
Howard Hinnant333f50d2010-09-21 20:16:37 +00001058 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 void __copy_assign_alloc(const __tree& __t)
1060 {__copy_assign_alloc(__t, integral_constant<bool,
1061 __node_traits::propagate_on_container_copy_assignment::value>());}
1062
Howard Hinnant333f50d2010-09-21 20:16:37 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064 void __copy_assign_alloc(const __tree& __t, true_type)
1065 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001067 void __copy_assign_alloc(const __tree& __t, false_type) {}
1068
1069 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001070 void __move_assign(__tree& __t, true_type)
1071 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1072 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073
Howard Hinnant333f50d2010-09-21 20:16:37 +00001074 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001075 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001076 _NOEXCEPT_(
1077 !__node_traits::propagate_on_container_move_assignment::value ||
1078 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079 {__move_assign_alloc(__t, integral_constant<bool,
1080 __node_traits::propagate_on_container_move_assignment::value>());}
1081
Howard Hinnant333f50d2010-09-21 20:16:37 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001084 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001085 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001086 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001087 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001088
Howard Hinnant333f50d2010-09-21 20:16:37 +00001089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00001091 _NOEXCEPT_(
1092 !__node_traits::propagate_on_container_swap::value ||
1093 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001094 {__swap_alloc(__x, __y, integral_constant<bool,
1095 __node_traits::propagate_on_container_swap::value>());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001096 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001098 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001099 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001100 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001101 swap(__x, __y);
1102 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001104 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001105 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001106 {}
1107
1108 __node_pointer __detach();
1109 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001110
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001111 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1112 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001113};
1114
1115template <class _Tp, class _Compare, class _Allocator>
1116__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001117 _NOEXCEPT_(
1118 is_nothrow_default_constructible<__node_allocator>::value &&
1119 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001120 : __pair3_(0, __comp)
1121{
1122 __begin_node() = __end_node();
1123}
1124
1125template <class _Tp, class _Compare, class _Allocator>
1126__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1127 : __pair1_(__node_allocator(__a)),
1128 __begin_node_(__node_pointer()),
1129 __pair3_(0)
1130{
1131 __begin_node() = __end_node();
1132}
1133
1134template <class _Tp, class _Compare, class _Allocator>
1135__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1136 const allocator_type& __a)
1137 : __pair1_(__node_allocator(__a)),
1138 __begin_node_(__node_pointer()),
1139 __pair3_(0, __comp)
1140{
1141 __begin_node() = __end_node();
1142}
1143
1144// Precondition: size() != 0
1145template <class _Tp, class _Compare, class _Allocator>
1146typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1147__tree<_Tp, _Compare, _Allocator>::__detach()
1148{
1149 __node_pointer __cache = __begin_node();
1150 __begin_node() = __end_node();
1151 __end_node()->__left_->__parent_ = nullptr;
1152 __end_node()->__left_ = nullptr;
1153 size() = 0;
1154 // __cache->__left_ == nullptr
1155 if (__cache->__right_ != nullptr)
1156 __cache = static_cast<__node_pointer>(__cache->__right_);
1157 // __cache->__left_ == nullptr
1158 // __cache->__right_ == nullptr
1159 return __cache;
1160}
1161
1162// Precondition: __cache != nullptr
1163// __cache->left_ == nullptr
1164// __cache->right_ == nullptr
1165// This is no longer a red-black tree
1166template <class _Tp, class _Compare, class _Allocator>
1167typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1168__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1169{
1170 if (__cache->__parent_ == nullptr)
1171 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001172 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001173 {
1174 __cache->__parent_->__left_ = nullptr;
1175 __cache = static_cast<__node_pointer>(__cache->__parent_);
1176 if (__cache->__right_ == nullptr)
1177 return __cache;
1178 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1179 }
1180 // __cache is right child
1181 __cache->__parent_->__right_ = nullptr;
1182 __cache = static_cast<__node_pointer>(__cache->__parent_);
1183 if (__cache->__left_ == nullptr)
1184 return __cache;
1185 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1186}
1187
1188template <class _Tp, class _Compare, class _Allocator>
1189__tree<_Tp, _Compare, _Allocator>&
1190__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1191{
1192 if (this != &__t)
1193 {
1194 value_comp() = __t.value_comp();
1195 __copy_assign_alloc(__t);
1196 __assign_multi(__t.begin(), __t.end());
1197 }
1198 return *this;
1199}
1200
1201template <class _Tp, class _Compare, class _Allocator>
1202template <class _InputIterator>
1203void
1204__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1205{
1206 if (size() != 0)
1207 {
1208 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001209#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210 try
1211 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001212#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213 for (; __cache != nullptr && __first != __last; ++__first)
1214 {
1215 __cache->__value_ = *__first;
1216 __node_pointer __next = __detach(__cache);
1217 __node_insert_unique(__cache);
1218 __cache = __next;
1219 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001220#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001221 }
1222 catch (...)
1223 {
1224 while (__cache->__parent_ != nullptr)
1225 __cache = static_cast<__node_pointer>(__cache->__parent_);
1226 destroy(__cache);
1227 throw;
1228 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001229#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001230 if (__cache != nullptr)
1231 {
1232 while (__cache->__parent_ != nullptr)
1233 __cache = static_cast<__node_pointer>(__cache->__parent_);
1234 destroy(__cache);
1235 }
1236 }
1237 for (; __first != __last; ++__first)
1238 __insert_unique(*__first);
1239}
1240
1241template <class _Tp, class _Compare, class _Allocator>
1242template <class _InputIterator>
1243void
1244__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1245{
1246 if (size() != 0)
1247 {
1248 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001249#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001250 try
1251 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001252#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253 for (; __cache != nullptr && __first != __last; ++__first)
1254 {
1255 __cache->__value_ = *__first;
1256 __node_pointer __next = __detach(__cache);
1257 __node_insert_multi(__cache);
1258 __cache = __next;
1259 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001260#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001261 }
1262 catch (...)
1263 {
1264 while (__cache->__parent_ != nullptr)
1265 __cache = static_cast<__node_pointer>(__cache->__parent_);
1266 destroy(__cache);
1267 throw;
1268 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001269#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001270 if (__cache != nullptr)
1271 {
1272 while (__cache->__parent_ != nullptr)
1273 __cache = static_cast<__node_pointer>(__cache->__parent_);
1274 destroy(__cache);
1275 }
1276 }
1277 for (; __first != __last; ++__first)
1278 __insert_multi(*__first);
1279}
1280
1281template <class _Tp, class _Compare, class _Allocator>
1282__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1283 : __begin_node_(__node_pointer()),
1284 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1285 __pair3_(0, __t.value_comp())
1286{
1287 __begin_node() = __end_node();
1288}
1289
Howard Hinnant73d21a42010-09-04 23:28:19 +00001290#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001291
1292template <class _Tp, class _Compare, class _Allocator>
1293__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001294 _NOEXCEPT_(
1295 is_nothrow_move_constructible<__node_allocator>::value &&
1296 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001297 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1298 __pair1_(_VSTD::move(__t.__pair1_)),
1299 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300{
1301 if (size() == 0)
1302 __begin_node() = __end_node();
1303 else
1304 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001305 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306 __t.__begin_node() = __t.__end_node();
1307 __t.__end_node()->__left_ = nullptr;
1308 __t.size() = 0;
1309 }
1310}
1311
1312template <class _Tp, class _Compare, class _Allocator>
1313__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1314 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001315 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001316{
1317 if (__a == __t.__alloc())
1318 {
1319 if (__t.size() == 0)
1320 __begin_node() = __end_node();
1321 else
1322 {
1323 __begin_node() = __t.__begin_node();
1324 __end_node()->__left_ = __t.__end_node()->__left_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001325 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326 size() = __t.size();
1327 __t.__begin_node() = __t.__end_node();
1328 __t.__end_node()->__left_ = nullptr;
1329 __t.size() = 0;
1330 }
1331 }
1332 else
1333 {
1334 __begin_node() = __end_node();
1335 }
1336}
1337
1338template <class _Tp, class _Compare, class _Allocator>
1339void
1340__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001341 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1342 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343{
1344 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1345 __begin_node_ = __t.__begin_node_;
1346 __pair1_.first() = __t.__pair1_.first();
1347 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001348 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 if (size() == 0)
1350 __begin_node() = __end_node();
1351 else
1352 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001353 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001354 __t.__begin_node() = __t.__end_node();
1355 __t.__end_node()->__left_ = nullptr;
1356 __t.size() = 0;
1357 }
1358}
1359
1360template <class _Tp, class _Compare, class _Allocator>
1361void
1362__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1363{
1364 if (__node_alloc() == __t.__node_alloc())
1365 __move_assign(__t, true_type());
1366 else
1367 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001368 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001369 const_iterator __e = end();
1370 if (size() != 0)
1371 {
1372 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001373#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374 try
1375 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001376#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377 while (__cache != nullptr && __t.size() != 0)
1378 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001379 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380 __node_pointer __next = __detach(__cache);
1381 __node_insert_multi(__cache);
1382 __cache = __next;
1383 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001384#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 }
1386 catch (...)
1387 {
1388 while (__cache->__parent_ != nullptr)
1389 __cache = static_cast<__node_pointer>(__cache->__parent_);
1390 destroy(__cache);
1391 throw;
1392 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001393#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001394 if (__cache != nullptr)
1395 {
1396 while (__cache->__parent_ != nullptr)
1397 __cache = static_cast<__node_pointer>(__cache->__parent_);
1398 destroy(__cache);
1399 }
1400 }
1401 while (__t.size() != 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001402 __insert_multi(__e, _VSTD::move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001403 }
1404}
1405
1406template <class _Tp, class _Compare, class _Allocator>
1407__tree<_Tp, _Compare, _Allocator>&
1408__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001409 _NOEXCEPT_(
1410 __node_traits::propagate_on_container_move_assignment::value &&
1411 is_nothrow_move_assignable<value_compare>::value &&
1412 is_nothrow_move_assignable<__node_allocator>::value)
1413
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414{
1415 __move_assign(__t, integral_constant<bool,
1416 __node_traits::propagate_on_container_move_assignment::value>());
1417 return *this;
1418}
1419
Howard Hinnant73d21a42010-09-04 23:28:19 +00001420#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421
1422template <class _Tp, class _Compare, class _Allocator>
1423__tree<_Tp, _Compare, _Allocator>::~__tree()
1424{
1425 destroy(__root());
1426}
1427
1428template <class _Tp, class _Compare, class _Allocator>
1429void
Howard Hinnant7686add2011-06-04 14:31:57 +00001430__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001431{
1432 if (__nd != nullptr)
1433 {
1434 destroy(static_cast<__node_pointer>(__nd->__left_));
1435 destroy(static_cast<__node_pointer>(__nd->__right_));
1436 __node_allocator& __na = __node_alloc();
Howard Hinnant0949eed2011-06-30 21:18:19 +00001437 __node_traits::destroy(__na, _VSTD::addressof(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001438 __node_traits::deallocate(__na, __nd, 1);
1439 }
1440}
1441
1442template <class _Tp, class _Compare, class _Allocator>
1443void
1444__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001445 _NOEXCEPT_(
1446 __is_nothrow_swappable<value_compare>::value &&
1447 (!__node_traits::propagate_on_container_swap::value ||
1448 __is_nothrow_swappable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001449{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001450 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451 swap(__begin_node_, __t.__begin_node_);
1452 swap(__pair1_.first(), __t.__pair1_.first());
1453 __swap_alloc(__node_alloc(), __t.__node_alloc());
1454 __pair3_.swap(__t.__pair3_);
1455 if (size() == 0)
1456 __begin_node() = __end_node();
1457 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001458 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001459 if (__t.size() == 0)
1460 __t.__begin_node() = __t.__end_node();
1461 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001462 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463}
1464
1465template <class _Tp, class _Compare, class _Allocator>
1466void
Howard Hinnant7686add2011-06-04 14:31:57 +00001467__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001468{
1469 destroy(__root());
1470 size() = 0;
1471 __begin_node() = __end_node();
1472 __end_node()->__left_ = nullptr;
1473}
1474
1475// Find lower_bound place to insert
1476// Set __parent to parent of null leaf
1477// Return reference to null leaf
1478template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001479typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1480__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 const value_type& __v)
1482{
1483 __node_pointer __nd = __root();
1484 if (__nd != nullptr)
1485 {
1486 while (true)
1487 {
1488 if (value_comp()(__nd->__value_, __v))
1489 {
1490 if (__nd->__right_ != nullptr)
1491 __nd = static_cast<__node_pointer>(__nd->__right_);
1492 else
1493 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001494 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495 return __parent->__right_;
1496 }
1497 }
1498 else
1499 {
1500 if (__nd->__left_ != nullptr)
1501 __nd = static_cast<__node_pointer>(__nd->__left_);
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->__left_;
1506 }
1507 }
1508 }
1509 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001510 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001511 return __parent->__left_;
1512}
1513
1514// Find upper_bound place to insert
1515// Set __parent to parent of null leaf
1516// Return reference to null leaf
1517template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001518typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1519__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001520 const value_type& __v)
1521{
1522 __node_pointer __nd = __root();
1523 if (__nd != nullptr)
1524 {
1525 while (true)
1526 {
1527 if (value_comp()(__v, __nd->__value_))
1528 {
1529 if (__nd->__left_ != nullptr)
1530 __nd = static_cast<__node_pointer>(__nd->__left_);
1531 else
1532 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001533 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001534 return __parent->__left_;
1535 }
1536 }
1537 else
1538 {
1539 if (__nd->__right_ != nullptr)
1540 __nd = static_cast<__node_pointer>(__nd->__right_);
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->__right_;
1545 }
1546 }
1547 }
1548 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001549 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001550 return __parent->__left_;
1551}
1552
1553// Find leaf place to insert closest to __hint
1554// First check prior to __hint.
1555// Next check after __hint.
1556// Next do O(log N) search.
1557// Set __parent to parent of null leaf
1558// Return reference to null leaf
1559template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001560typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001562 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001563 const value_type& __v)
1564{
1565 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1566 {
1567 // __v <= *__hint
1568 const_iterator __prior = __hint;
1569 if (__prior == begin() || !value_comp()(__v, *--__prior))
1570 {
1571 // *prev(__hint) <= __v <= *__hint
1572 if (__hint.__ptr_->__left_ == nullptr)
1573 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001574 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001575 return __parent->__left_;
1576 }
1577 else
1578 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001579 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001580 return __parent->__right_;
1581 }
1582 }
1583 // __v < *prev(__hint)
1584 return __find_leaf_high(__parent, __v);
1585 }
1586 // else __v > *__hint
1587 return __find_leaf_low(__parent, __v);
1588}
1589
1590// Find place to insert if __v doesn't exist
1591// Set __parent to parent of null leaf
1592// Return reference to null leaf
1593// If __v exists, set parent to node of __v and return reference to node of __v
1594template <class _Tp, class _Compare, class _Allocator>
1595template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001596typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1597__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598 const _Key& __v)
1599{
1600 __node_pointer __nd = __root();
1601 if (__nd != nullptr)
1602 {
1603 while (true)
1604 {
1605 if (value_comp()(__v, __nd->__value_))
1606 {
1607 if (__nd->__left_ != nullptr)
1608 __nd = static_cast<__node_pointer>(__nd->__left_);
1609 else
1610 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001611 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001612 return __parent->__left_;
1613 }
1614 }
1615 else if (value_comp()(__nd->__value_, __v))
1616 {
1617 if (__nd->__right_ != nullptr)
1618 __nd = static_cast<__node_pointer>(__nd->__right_);
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->__right_;
1623 }
1624 }
1625 else
1626 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001627 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001628 return __parent;
1629 }
1630 }
1631 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001632 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001633 return __parent->__left_;
1634}
1635
1636// Find place to insert if __v doesn't exist
1637// First check prior to __hint.
1638// Next check after __hint.
1639// Next do O(log N) search.
1640// Set __parent to parent of null leaf
1641// Return reference to null leaf
1642// If __v exists, set parent to node of __v and return reference to node of __v
1643template <class _Tp, class _Compare, class _Allocator>
1644template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001645typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001646__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001647 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001648 const _Key& __v)
1649{
1650 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1651 {
1652 // __v < *__hint
1653 const_iterator __prior = __hint;
1654 if (__prior == begin() || value_comp()(*--__prior, __v))
1655 {
1656 // *prev(__hint) < __v < *__hint
1657 if (__hint.__ptr_->__left_ == nullptr)
1658 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001659 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 return __parent->__left_;
1661 }
1662 else
1663 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001664 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 return __parent->__right_;
1666 }
1667 }
1668 // __v <= *prev(__hint)
1669 return __find_equal(__parent, __v);
1670 }
1671 else if (value_comp()(*__hint, __v)) // check after
1672 {
1673 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00001674 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 if (__next == end() || value_comp()(__v, *__next))
1676 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001677 // *__hint < __v < *_VSTD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678 if (__hint.__ptr_->__right_ == nullptr)
1679 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001680 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681 return __parent->__right_;
1682 }
1683 else
1684 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001685 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001686 return __parent->__left_;
1687 }
1688 }
1689 // *next(__hint) <= __v
1690 return __find_equal(__parent, __v);
1691 }
1692 // else __v == *__hint
Howard Hinnant70342b92013-06-19 21:29:40 +00001693 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 return __parent;
1695}
1696
1697template <class _Tp, class _Compare, class _Allocator>
1698void
1699__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1700 __node_base_pointer& __child,
1701 __node_base_pointer __new_node)
1702{
1703 __new_node->__left_ = nullptr;
1704 __new_node->__right_ = nullptr;
1705 __new_node->__parent_ = __parent;
1706 __child = __new_node;
1707 if (__begin_node()->__left_ != nullptr)
1708 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1709 __tree_balance_after_insert(__end_node()->__left_, __child);
1710 ++size();
1711}
1712
Howard Hinnant73d21a42010-09-04 23:28:19 +00001713#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1714#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001715
1716template <class _Tp, class _Compare, class _Allocator>
1717template <class ..._Args>
1718typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1719__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1720{
1721 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001722 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001723 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 __h.get_deleter().__value_constructed = true;
1725 return __h;
1726}
1727
1728template <class _Tp, class _Compare, class _Allocator>
1729template <class... _Args>
1730pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1731__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1732{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001733 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001734 __node_base_pointer __parent;
1735 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1736 __node_pointer __r = static_cast<__node_pointer>(__child);
1737 bool __inserted = false;
1738 if (__child == nullptr)
1739 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001740 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001741 __r = __h.release();
1742 __inserted = true;
1743 }
1744 return pair<iterator, bool>(iterator(__r), __inserted);
1745}
1746
1747template <class _Tp, class _Compare, class _Allocator>
1748template <class... _Args>
1749typename __tree<_Tp, _Compare, _Allocator>::iterator
1750__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1751{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001752 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753 __node_base_pointer __parent;
1754 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1755 __node_pointer __r = static_cast<__node_pointer>(__child);
1756 if (__child == nullptr)
1757 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001758 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 __r = __h.release();
1760 }
1761 return iterator(__r);
1762}
1763
1764template <class _Tp, class _Compare, class _Allocator>
1765template <class... _Args>
1766typename __tree<_Tp, _Compare, _Allocator>::iterator
1767__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1768{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 __node_base_pointer __parent;
1771 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001772 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001773 return iterator(static_cast<__node_pointer>(__h.release()));
1774}
1775
1776template <class _Tp, class _Compare, class _Allocator>
1777template <class... _Args>
1778typename __tree<_Tp, _Compare, _Allocator>::iterator
1779__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1780 _Args&&... __args)
1781{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001782 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001783 __node_base_pointer __parent;
1784 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001785 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786 return iterator(static_cast<__node_pointer>(__h.release()));
1787}
1788
Howard Hinnant73d21a42010-09-04 23:28:19 +00001789#endif // _LIBCPP_HAS_NO_VARIADICS
1790
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001791template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001792template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Howard Hinnant99968442011-11-29 18:15:50 +00001794__tree<_Tp, _Compare, _Allocator>::__insert_unique(_Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001795{
Howard Hinnant99968442011-11-29 18:15:50 +00001796 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001797 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1798 if (__r.second)
1799 __h.release();
1800 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001801}
1802
1803template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001804template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001806__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001807{
Howard Hinnant99968442011-11-29 18:15:50 +00001808 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001809 iterator __r = __node_insert_unique(__p, __h.get());
1810 if (__r.__ptr_ == __h.get())
1811 __h.release();
1812 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001813}
1814
1815template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001816template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001817typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001818__tree<_Tp, _Compare, _Allocator>::__insert_multi(_Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001819{
Howard Hinnant99968442011-11-29 18:15:50 +00001820 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001821 __node_base_pointer __parent;
1822 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001823 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 return iterator(__h.release());
1825}
1826
1827template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant99968442011-11-29 18:15:50 +00001828template <class _Vp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829typename __tree<_Tp, _Compare, _Allocator>::iterator
Howard Hinnant99968442011-11-29 18:15:50 +00001830__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _Vp&& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831{
Howard Hinnant99968442011-11-29 18:15:50 +00001832 __node_holder __h = __construct_node(_VSTD::forward<_Vp>(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001833 __node_base_pointer __parent;
1834 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001835 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001836 return iterator(__h.release());
1837}
1838
Howard Hinnant73d21a42010-09-04 23:28:19 +00001839#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840
1841template <class _Tp, class _Compare, class _Allocator>
1842typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1843__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1844{
1845 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001846 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Howard Hinnant0949eed2011-06-30 21:18:19 +00001847 __node_traits::construct(__na, _VSTD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001848 __h.get_deleter().__value_constructed = true;
Howard Hinnant9a894d92013-08-22 18:29:50 +00001849 return _VSTD::move(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850}
1851
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001852#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1853
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001854template <class _Tp, class _Compare, class _Allocator>
1855pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1856__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1857{
1858 __node_base_pointer __parent;
1859 __node_base_pointer& __child = __find_equal(__parent, __v);
1860 __node_pointer __r = static_cast<__node_pointer>(__child);
1861 bool __inserted = false;
1862 if (__child == nullptr)
1863 {
1864 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001865 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001866 __r = __h.release();
1867 __inserted = true;
1868 }
1869 return pair<iterator, bool>(iterator(__r), __inserted);
1870}
1871
1872template <class _Tp, class _Compare, class _Allocator>
1873typename __tree<_Tp, _Compare, _Allocator>::iterator
1874__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1875{
1876 __node_base_pointer __parent;
1877 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1878 __node_pointer __r = static_cast<__node_pointer>(__child);
1879 if (__child == nullptr)
1880 {
1881 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001882 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883 __r = __h.release();
1884 }
1885 return iterator(__r);
1886}
1887
1888template <class _Tp, class _Compare, class _Allocator>
1889typename __tree<_Tp, _Compare, _Allocator>::iterator
1890__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1891{
1892 __node_base_pointer __parent;
1893 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1894 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001895 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001896 return iterator(__h.release());
1897}
1898
1899template <class _Tp, class _Compare, class _Allocator>
1900typename __tree<_Tp, _Compare, _Allocator>::iterator
1901__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1902{
1903 __node_base_pointer __parent;
1904 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1905 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00001906 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001907 return iterator(__h.release());
1908}
1909
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001910template <class _Tp, class _Compare, class _Allocator>
1911pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1912__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1913{
1914 __node_base_pointer __parent;
1915 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1916 __node_pointer __r = static_cast<__node_pointer>(__child);
1917 bool __inserted = false;
1918 if (__child == nullptr)
1919 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001920 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001921 __r = __nd;
1922 __inserted = true;
1923 }
1924 return pair<iterator, bool>(iterator(__r), __inserted);
1925}
1926
1927template <class _Tp, class _Compare, class _Allocator>
1928typename __tree<_Tp, _Compare, _Allocator>::iterator
1929__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1930 __node_pointer __nd)
1931{
1932 __node_base_pointer __parent;
1933 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1934 __node_pointer __r = static_cast<__node_pointer>(__child);
1935 if (__child == nullptr)
1936 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001937 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001938 __r = __nd;
1939 }
1940 return iterator(__r);
1941}
1942
1943template <class _Tp, class _Compare, class _Allocator>
1944typename __tree<_Tp, _Compare, _Allocator>::iterator
1945__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1946{
1947 __node_base_pointer __parent;
1948 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001949 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001950 return iterator(__nd);
1951}
1952
1953template <class _Tp, class _Compare, class _Allocator>
1954typename __tree<_Tp, _Compare, _Allocator>::iterator
1955__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1956 __node_pointer __nd)
1957{
1958 __node_base_pointer __parent;
1959 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
Howard Hinnant70342b92013-06-19 21:29:40 +00001960 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001961 return iterator(__nd);
1962}
1963
1964template <class _Tp, class _Compare, class _Allocator>
1965typename __tree<_Tp, _Compare, _Allocator>::iterator
1966__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1967{
Howard Hinnant70342b92013-06-19 21:29:40 +00001968 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 iterator __r(__np);
1970 ++__r;
1971 if (__begin_node() == __np)
1972 __begin_node() = __r.__ptr_;
1973 --size();
1974 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975 __tree_remove(__end_node()->__left_,
1976 static_cast<__node_base_pointer>(__np));
Marshall Clow140e8f52014-04-11 08:22:42 +00001977 __node_traits::destroy(__na, const_cast<value_type*>(_VSTD::addressof(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001978 __node_traits::deallocate(__na, __np, 1);
1979 return __r;
1980}
1981
1982template <class _Tp, class _Compare, class _Allocator>
1983typename __tree<_Tp, _Compare, _Allocator>::iterator
1984__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1985{
1986 while (__f != __l)
1987 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00001988 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989}
1990
1991template <class _Tp, class _Compare, class _Allocator>
1992template <class _Key>
1993typename __tree<_Tp, _Compare, _Allocator>::size_type
1994__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1995{
1996 iterator __i = find(__k);
1997 if (__i == end())
1998 return 0;
1999 erase(__i);
2000 return 1;
2001}
2002
2003template <class _Tp, class _Compare, class _Allocator>
2004template <class _Key>
2005typename __tree<_Tp, _Compare, _Allocator>::size_type
2006__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2007{
2008 pair<iterator, iterator> __p = __equal_range_multi(__k);
2009 size_type __r = 0;
2010 for (; __p.first != __p.second; ++__r)
2011 __p.first = erase(__p.first);
2012 return __r;
2013}
2014
2015template <class _Tp, class _Compare, class _Allocator>
2016template <class _Key>
2017typename __tree<_Tp, _Compare, _Allocator>::iterator
2018__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2019{
2020 iterator __p = __lower_bound(__v, __root(), __end_node());
2021 if (__p != end() && !value_comp()(__v, *__p))
2022 return __p;
2023 return end();
2024}
2025
2026template <class _Tp, class _Compare, class _Allocator>
2027template <class _Key>
2028typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2029__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2030{
2031 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2032 if (__p != end() && !value_comp()(__v, *__p))
2033 return __p;
2034 return end();
2035}
2036
2037template <class _Tp, class _Compare, class _Allocator>
2038template <class _Key>
2039typename __tree<_Tp, _Compare, _Allocator>::size_type
2040__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2041{
2042 __node_const_pointer __result = __end_node();
2043 __node_const_pointer __rt = __root();
2044 while (__rt != nullptr)
2045 {
2046 if (value_comp()(__k, __rt->__value_))
2047 {
2048 __result = __rt;
2049 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2050 }
2051 else if (value_comp()(__rt->__value_, __k))
2052 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2053 else
2054 return 1;
2055 }
2056 return 0;
2057}
2058
2059template <class _Tp, class _Compare, class _Allocator>
2060template <class _Key>
2061typename __tree<_Tp, _Compare, _Allocator>::size_type
2062__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2063{
Howard Hinnant99968442011-11-29 18:15:50 +00002064 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002065 __node_const_pointer __result = __end_node();
2066 __node_const_pointer __rt = __root();
2067 while (__rt != nullptr)
2068 {
2069 if (value_comp()(__k, __rt->__value_))
2070 {
2071 __result = __rt;
2072 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2073 }
2074 else if (value_comp()(__rt->__value_, __k))
2075 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2076 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002077 return _VSTD::distance(
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2079 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
2080 );
2081 }
2082 return 0;
2083}
2084
2085template <class _Tp, class _Compare, class _Allocator>
2086template <class _Key>
2087typename __tree<_Tp, _Compare, _Allocator>::iterator
2088__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2089 __node_pointer __root,
2090 __node_pointer __result)
2091{
2092 while (__root != nullptr)
2093 {
2094 if (!value_comp()(__root->__value_, __v))
2095 {
2096 __result = __root;
2097 __root = static_cast<__node_pointer>(__root->__left_);
2098 }
2099 else
2100 __root = static_cast<__node_pointer>(__root->__right_);
2101 }
2102 return iterator(__result);
2103}
2104
2105template <class _Tp, class _Compare, class _Allocator>
2106template <class _Key>
2107typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2108__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2109 __node_const_pointer __root,
2110 __node_const_pointer __result) const
2111{
2112 while (__root != nullptr)
2113 {
2114 if (!value_comp()(__root->__value_, __v))
2115 {
2116 __result = __root;
2117 __root = static_cast<__node_const_pointer>(__root->__left_);
2118 }
2119 else
2120 __root = static_cast<__node_const_pointer>(__root->__right_);
2121 }
2122 return const_iterator(__result);
2123}
2124
2125template <class _Tp, class _Compare, class _Allocator>
2126template <class _Key>
2127typename __tree<_Tp, _Compare, _Allocator>::iterator
2128__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2129 __node_pointer __root,
2130 __node_pointer __result)
2131{
2132 while (__root != nullptr)
2133 {
2134 if (value_comp()(__v, __root->__value_))
2135 {
2136 __result = __root;
2137 __root = static_cast<__node_pointer>(__root->__left_);
2138 }
2139 else
2140 __root = static_cast<__node_pointer>(__root->__right_);
2141 }
2142 return iterator(__result);
2143}
2144
2145template <class _Tp, class _Compare, class _Allocator>
2146template <class _Key>
2147typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2148__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2149 __node_const_pointer __root,
2150 __node_const_pointer __result) const
2151{
2152 while (__root != nullptr)
2153 {
2154 if (value_comp()(__v, __root->__value_))
2155 {
2156 __result = __root;
2157 __root = static_cast<__node_const_pointer>(__root->__left_);
2158 }
2159 else
2160 __root = static_cast<__node_const_pointer>(__root->__right_);
2161 }
2162 return const_iterator(__result);
2163}
2164
2165template <class _Tp, class _Compare, class _Allocator>
2166template <class _Key>
2167pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2168 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2169__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2170{
Howard Hinnant99968442011-11-29 18:15:50 +00002171 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002172 __node_pointer __result = __end_node();
2173 __node_pointer __rt = __root();
2174 while (__rt != nullptr)
2175 {
2176 if (value_comp()(__k, __rt->__value_))
2177 {
2178 __result = __rt;
2179 __rt = static_cast<__node_pointer>(__rt->__left_);
2180 }
2181 else if (value_comp()(__rt->__value_, __k))
2182 __rt = static_cast<__node_pointer>(__rt->__right_);
2183 else
Howard Hinnant99968442011-11-29 18:15:50 +00002184 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002185 iterator(
2186 __rt->__right_ != nullptr ?
2187 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2188 : __result));
2189 }
Howard Hinnant99968442011-11-29 18:15:50 +00002190 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002191}
2192
2193template <class _Tp, class _Compare, class _Allocator>
2194template <class _Key>
2195pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2196 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2197__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2198{
Howard Hinnant99968442011-11-29 18:15:50 +00002199 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002200 __node_const_pointer __result = __end_node();
2201 __node_const_pointer __rt = __root();
2202 while (__rt != nullptr)
2203 {
2204 if (value_comp()(__k, __rt->__value_))
2205 {
2206 __result = __rt;
2207 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2208 }
2209 else if (value_comp()(__rt->__value_, __k))
2210 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2211 else
Howard Hinnant99968442011-11-29 18:15:50 +00002212 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002213 const_iterator(
2214 __rt->__right_ != nullptr ?
2215 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2216 : __result));
2217 }
Howard Hinnant99968442011-11-29 18:15:50 +00002218 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002219}
2220
2221template <class _Tp, class _Compare, class _Allocator>
2222template <class _Key>
2223pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2224 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2225__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2226{
Howard Hinnant99968442011-11-29 18:15:50 +00002227 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 __node_pointer __result = __end_node();
2229 __node_pointer __rt = __root();
2230 while (__rt != nullptr)
2231 {
2232 if (value_comp()(__k, __rt->__value_))
2233 {
2234 __result = __rt;
2235 __rt = static_cast<__node_pointer>(__rt->__left_);
2236 }
2237 else if (value_comp()(__rt->__value_, __k))
2238 __rt = static_cast<__node_pointer>(__rt->__right_);
2239 else
Howard Hinnant99968442011-11-29 18:15:50 +00002240 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002241 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2242 }
Howard Hinnant99968442011-11-29 18:15:50 +00002243 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244}
2245
2246template <class _Tp, class _Compare, class _Allocator>
2247template <class _Key>
2248pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2249 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2250__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2251{
Howard Hinnant99968442011-11-29 18:15:50 +00002252 typedef pair<const_iterator, const_iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 __node_const_pointer __result = __end_node();
2254 __node_const_pointer __rt = __root();
2255 while (__rt != nullptr)
2256 {
2257 if (value_comp()(__k, __rt->__value_))
2258 {
2259 __result = __rt;
2260 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2261 }
2262 else if (value_comp()(__rt->__value_, __k))
2263 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2264 else
Howard Hinnant99968442011-11-29 18:15:50 +00002265 return _Pp(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2267 }
Howard Hinnant99968442011-11-29 18:15:50 +00002268 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002269}
2270
2271template <class _Tp, class _Compare, class _Allocator>
2272typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002273__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002274{
Howard Hinnant70342b92013-06-19 21:29:40 +00002275 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002276 if (__begin_node() == __np)
2277 {
2278 if (__np->__right_ != nullptr)
2279 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2280 else
2281 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2282 }
2283 --size();
2284 __tree_remove(__end_node()->__left_,
2285 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002286 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002287}
2288
Howard Hinnant7686add2011-06-04 14:31:57 +00002289template <class _Tp, class _Compare, class _Allocator>
2290inline _LIBCPP_INLINE_VISIBILITY
2291void
2292swap(__tree<_Tp, _Compare, _Allocator>& __x,
2293 __tree<_Tp, _Compare, _Allocator>& __y)
2294 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2295{
2296 __x.swap(__y);
2297}
2298
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299_LIBCPP_END_NAMESPACE_STD
2300
2301#endif // _LIBCPP___TREE