blob: 018d729092e90467863d067bb9fd5a349e64f581 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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
20#pragma GCC system_header
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24template <class, class, class> class __tree;
Howard Hinnant8e29cda2010-09-21 20:16:37 +000025template <class, class, class> class _LIBCPP_VISIBLE __tree_iterator;
26template <class, class, class> class _LIBCPP_VISIBLE __tree_const_iterator;
27template <class, class, class, class> class _LIBCPP_VISIBLE map;
28template <class, class, class, class> class _LIBCPP_VISIBLE multimap;
29template <class, class, class> class _LIBCPP_VISIBLE set;
30template <class, class, class> class _LIBCPP_VISIBLE multiset;
Howard Hinnantc51e1022010-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 Hinnant8e29cda2010-09-21 20:16:37 +000056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000057bool
Howard Hinnant1113b5e2011-06-04 17:10:24 +000058__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant8e29cda2010-09-21 20:16:37 +0000122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000124__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant8e29cda2010-09-21 20:16:37 +0000134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135_NodePtr
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000136__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000147__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000160__tree_prev(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000173__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000197__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000217__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000242__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant1113b5e2011-06-04 17:10:24 +0000312__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +0000346 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +0000416 __x->__parent_->__right_ :
417 __x->__parent_->__left_;
Howard Hinnantc51e1022010-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 Hinnant3b6579a2010-08-22 00:02:43 +0000468 __x->__parent_->__right_ :
469 __x->__parent_->__left_;
Howard Hinnantc51e1022010-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
497template <class> class __map_node_destructor;
498
Howard Hinnantc51e1022010-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 Hinnant8e29cda2010-09-21 20:16:37 +0000516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000517 explicit __tree_node_destructor(allocator_type& __na) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518 : __na_(__na),
519 __value_constructed(false)
520 {}
521
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000523 void operator()(pointer __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000524 {
525 if (__value_constructed)
Howard Hinnant9a242b02011-02-02 17:36:20 +0000526 __alloc_traits::destroy(__na_, _STD::addressof(__p->__value_));
Howard Hinnantc51e1022010-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 Hinnant8e29cda2010-09-21 20:16:37 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000544 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantc51e1022010-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 Hinnant8e29cda2010-09-21 20:16:37 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1113b5e2011-06-04 17:10:24 +0000583 __tree_node_base() _NOEXCEPT
584 : __right_(), __parent_(), __is_black_(false) {}
Howard Hinnantc51e1022010-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 Hinnant74279a52010-09-04 23:28:19 +0000597#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598 template <class ..._Args>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000599 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600 explicit __tree_node(_Args&& ...__args)
601 : __value_(_STD::forward<_Args>(__args)...) {}
Howard Hinnant74279a52010-09-04 23:28:19 +0000602#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000603 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000604 explicit __tree_node(const value_type& __v)
605 : __value_(__v) {}
Howard Hinnant74279a52010-09-04 23:28:19 +0000606#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607};
608
609template <class> class __map_iterator;
610template <class> class __map_const_iterator;
611
612template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000613class _LIBCPP_VISIBLE __tree_iterator
Howard Hinnantc51e1022010-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
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000636 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000638 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
639 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &__ptr_->__value_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000641 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000642 __tree_iterator& operator++()
643 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
644 return *this;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000645 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000646 __tree_iterator operator++(int)
647 {__tree_iterator __t(*this); ++(*this); return __t;}
648
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000649 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 __tree_iterator& operator--()
651 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
652 return *this;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000653 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654 __tree_iterator operator--(int)
655 {__tree_iterator __t(*this); --(*this); return __t;}
656
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000657 friend _LIBCPP_INLINE_VISIBILITY
658 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000660 friend _LIBCPP_INLINE_VISIBILITY
661 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 {return !(__x == __y);}
663
664private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000665 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000666 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000667 template <class, class, class> friend class __tree;
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000668 template <class, class, class> friend class _LIBCPP_VISIBLE __tree_const_iterator;
669 template <class> friend class _LIBCPP_VISIBLE __map_iterator;
670 template <class, class, class, class> friend class _LIBCPP_VISIBLE map;
671 template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap;
672 template <class, class, class> friend class _LIBCPP_VISIBLE set;
673 template <class, class, class> friend class _LIBCPP_VISIBLE multiset;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000674};
675
676template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000677class _LIBCPP_VISIBLE __tree_const_iterator
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678{
679 typedef _ConstNodePtr __node_pointer;
680 typedef typename pointer_traits<__node_pointer>::element_type __node;
681 typedef const typename __node::base __node_base;
682 typedef typename pointer_traits<__node_pointer>::template
683#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
684 rebind<__node_base>
685#else
686 rebind<__node_base>::other
687#endif
688 __node_base_pointer;
689
690 __node_pointer __ptr_;
691
692 typedef pointer_traits<__node_pointer> __pointer_traits;
693public:
694 typedef bidirectional_iterator_tag iterator_category;
695 typedef _Tp value_type;
696 typedef _DiffType difference_type;
697 typedef const value_type& reference;
698 typedef typename pointer_traits<__node_pointer>::template
699#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
700 rebind<const value_type>
701#else
702 rebind<const value_type>::other
703#endif
704 pointer;
705
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000706 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707private:
708 typedef typename remove_const<__node>::type __non_const_node;
709 typedef typename pointer_traits<__node_pointer>::template
710#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
711 rebind<__non_const_node>
712#else
713 rebind<__non_const_node>::other
714#endif
715 __non_const_node_pointer;
716 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
717 __non_const_iterator;
718public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000719 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000720 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
721 : __ptr_(__p.__ptr_) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000723 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
724 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &__ptr_->__value_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000725
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000726 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000727 __tree_const_iterator& operator++()
728 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
729 return *this;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000730 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000731 __tree_const_iterator operator++(int)
732 {__tree_const_iterator __t(*this); ++(*this); return __t;}
733
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000734 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000735 __tree_const_iterator& operator--()
736 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
737 return *this;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000738 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000739 __tree_const_iterator operator--(int)
740 {__tree_const_iterator __t(*this); --(*this); return __t;}
741
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000742 friend _LIBCPP_INLINE_VISIBILITY
743 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000744 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000745 friend _LIBCPP_INLINE_VISIBILITY
746 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 {return !(__x == __y);}
748
749private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000750 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000751 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
752 : __ptr_(__p) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000753 template <class, class, class> friend class __tree;
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000754 template <class, class, class, class> friend class _LIBCPP_VISIBLE map;
755 template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap;
756 template <class, class, class> friend class _LIBCPP_VISIBLE set;
757 template <class, class, class> friend class _LIBCPP_VISIBLE multiset;
758 template <class> friend class _LIBCPP_VISIBLE __map_const_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000759};
760
761template <class _Tp, class _Compare, class _Allocator>
762class __tree
763{
764public:
765 typedef _Tp value_type;
766 typedef _Compare value_compare;
767 typedef _Allocator allocator_type;
768 typedef allocator_traits<allocator_type> __alloc_traits;
769 typedef typename __alloc_traits::pointer pointer;
770 typedef typename __alloc_traits::const_pointer const_pointer;
771 typedef typename __alloc_traits::size_type size_type;
772 typedef typename __alloc_traits::difference_type difference_type;
773
774 typedef __tree_node<value_type, typename __alloc_traits::void_pointer> __node;
Howard Hinnant6108fd72011-04-03 20:05:29 +0000775 typedef __tree_node_base<typename __alloc_traits::void_pointer> __node_base;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776 typedef typename __alloc_traits::template
777#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
778 rebind_alloc<__node>
779#else
780 rebind_alloc<__node>::other
781#endif
782 __node_allocator;
783 typedef allocator_traits<__node_allocator> __node_traits;
784 typedef typename __node_traits::pointer __node_pointer;
785 typedef typename __node_traits::const_pointer __node_const_pointer;
Howard Hinnant6108fd72011-04-03 20:05:29 +0000786 typedef typename __node_base::pointer __node_base_pointer;
787 typedef typename __node_base::const_pointer __node_base_const_pointer;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788private:
Howard Hinnant6108fd72011-04-03 20:05:29 +0000789 typedef typename __node_base::base __end_node_t;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000790 typedef typename pointer_traits<__node_pointer>::template
791#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
792 rebind<__end_node_t>
793#else
794 rebind<__end_node_t>::other
795#endif
796 __end_node_ptr;
797 typedef typename pointer_traits<__node_pointer>::template
798#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
799 rebind<const __end_node_t>
800#else
801 rebind<const __end_node_t>::other
802#endif
803 __end_node_const_ptr;
804
805 __node_pointer __begin_node_;
806 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
807 __compressed_pair<size_type, value_compare> __pair3_;
808
809public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000811 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000812 {
813 return static_cast<__node_pointer>
814 (
815 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
816 );
817 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000818 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000819 __node_const_pointer __end_node() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000820 {
821 return static_cast<__node_const_pointer>
822 (
823 pointer_traits<__end_node_const_ptr>::pointer_to(__pair1_.first())
824 );
825 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000827 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000828private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000829 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000830 const __node_allocator& __node_alloc() const _NOEXCEPT
831 {return __pair1_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000832 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000833 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000834 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000835 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000836public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000837 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000838 allocator_type __alloc() const _NOEXCEPT
839 {return allocator_type(__node_alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000840private:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000842 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000843public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000845 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000846 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000847 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000848 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000849 const value_compare& value_comp() const _NOEXCEPT
850 {return __pair3_.second();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000851public:
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000852 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000853 __node_pointer __root() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000854 {return static_cast<__node_pointer> (__end_node()->__left_);}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000856 __node_const_pointer __root() const _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
858
859 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
860 typedef __tree_const_iterator<value_type, __node_const_pointer, difference_type> const_iterator;
861
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000862 explicit __tree(const value_compare& __comp)
863 _NOEXCEPT_(
864 is_nothrow_default_constructible<__node_allocator>::value &&
865 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000866 explicit __tree(const allocator_type& __a);
867 __tree(const value_compare& __comp, const allocator_type& __a);
868 __tree(const __tree& __t);
869 __tree& operator=(const __tree& __t);
870 template <class _InputIterator>
871 void __assign_unique(_InputIterator __first, _InputIterator __last);
872 template <class _InputIterator>
873 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant74279a52010-09-04 23:28:19 +0000874#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000875 __tree(__tree&& __t)
876 _NOEXCEPT_(
877 is_nothrow_move_constructible<__node_allocator>::value &&
878 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000879 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000880 __tree& operator=(__tree&& __t)
881 _NOEXCEPT_(
882 __node_traits::propagate_on_container_move_assignment::value &&
883 is_nothrow_move_assignable<value_compare>::value &&
884 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant74279a52010-09-04 23:28:19 +0000885#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886
887 ~__tree();
888
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000890 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000891 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000892 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000893 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000894 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000895 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000896 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000897
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000898 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000899 size_type max_size() const _NOEXCEPT
900 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000902 void clear() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000903
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +0000904 void swap(__tree& __t)
905 _NOEXCEPT_(
906 __is_nothrow_swappable<value_compare>::value &&
907 (!__node_traits::propagate_on_container_swap::value ||
908 __is_nothrow_swappable<__node_allocator>::value));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000909
Howard Hinnant74279a52010-09-04 23:28:19 +0000910#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
911#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 template <class... _Args>
913 pair<iterator, bool>
914 __emplace_unique(_Args&&... __args);
915 template <class... _Args>
916 iterator
917 __emplace_multi(_Args&&... __args);
918
919 template <class... _Args>
920 iterator
921 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
922 template <class... _Args>
923 iterator
924 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant74279a52010-09-04 23:28:19 +0000925#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926
927 template <class _V>
928 pair<iterator, bool> __insert_unique(_V&& __v);
929 template <class _V>
930 iterator __insert_unique(const_iterator __p, _V&& __v);
931 template <class _V>
932 iterator __insert_multi(_V&& __v);
933 template <class _V>
934 iterator __insert_multi(const_iterator __p, _V&& __v);
Howard Hinnantc5bc8672011-03-10 17:27:57 +0000935#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936
937 pair<iterator, bool> __insert_unique(const value_type& __v);
938 iterator __insert_unique(const_iterator __p, const value_type& __v);
939 iterator __insert_multi(const value_type& __v);
940 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000941
942 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
943 iterator __node_insert_unique(const_iterator __p,
944 __node_pointer __nd);
945
946 iterator __node_insert_multi(__node_pointer __nd);
947 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
948
949 iterator erase(const_iterator __p);
950 iterator erase(const_iterator __f, const_iterator __l);
951 template <class _Key>
952 size_type __erase_unique(const _Key& __k);
953 template <class _Key>
954 size_type __erase_multi(const _Key& __k);
955
956 void __insert_node_at(__node_base_pointer __parent,
957 __node_base_pointer& __child,
958 __node_base_pointer __new_node);
959
960 template <class _Key>
961 iterator find(const _Key& __v);
962 template <class _Key>
963 const_iterator find(const _Key& __v) const;
964
965 template <class _Key>
966 size_type __count_unique(const _Key& __k) const;
967 template <class _Key>
968 size_type __count_multi(const _Key& __k) const;
969
970 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000971 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000972 iterator lower_bound(const _Key& __v)
973 {return __lower_bound(__v, __root(), __end_node());}
974 template <class _Key>
975 iterator __lower_bound(const _Key& __v,
976 __node_pointer __root,
977 __node_pointer __result);
978 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000979 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000980 const_iterator lower_bound(const _Key& __v) const
981 {return __lower_bound(__v, __root(), __end_node());}
982 template <class _Key>
983 const_iterator __lower_bound(const _Key& __v,
984 __node_const_pointer __root,
985 __node_const_pointer __result) const;
986 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988 iterator upper_bound(const _Key& __v)
989 {return __upper_bound(__v, __root(), __end_node());}
990 template <class _Key>
991 iterator __upper_bound(const _Key& __v,
992 __node_pointer __root,
993 __node_pointer __result);
994 template <class _Key>
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 const_iterator upper_bound(const _Key& __v) const
997 {return __upper_bound(__v, __root(), __end_node());}
998 template <class _Key>
999 const_iterator __upper_bound(const _Key& __v,
1000 __node_const_pointer __root,
1001 __node_const_pointer __result) const;
1002 template <class _Key>
1003 pair<iterator, iterator>
1004 __equal_range_unique(const _Key& __k);
1005 template <class _Key>
1006 pair<const_iterator, const_iterator>
1007 __equal_range_unique(const _Key& __k) const;
1008
1009 template <class _Key>
1010 pair<iterator, iterator>
1011 __equal_range_multi(const _Key& __k);
1012 template <class _Key>
1013 pair<const_iterator, const_iterator>
1014 __equal_range_multi(const _Key& __k) const;
1015
1016 typedef __tree_node_destructor<__node_allocator> _D;
1017 typedef unique_ptr<__node, _D> __node_holder;
1018
Howard Hinnant1113b5e2011-06-04 17:10:24 +00001019 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020private:
Howard Hinnant6108fd72011-04-03 20:05:29 +00001021 typename __node_base::pointer&
1022 __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v);
1023 typename __node_base::pointer&
1024 __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v);
1025 typename __node_base::pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001026 __find_leaf(const_iterator __hint,
Howard Hinnant6108fd72011-04-03 20:05:29 +00001027 typename __node_base::pointer& __parent, const value_type& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028 template <class _Key>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001029 typename __node_base::pointer&
1030 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001031 template <class _Key>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001032 typename __node_base::pointer&
1033 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034 const _Key& __v);
1035
Howard Hinnant74279a52010-09-04 23:28:19 +00001036#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001037 template <class ..._Args>
1038 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant74279a52010-09-04 23:28:19 +00001039#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040 __node_holder __construct_node(const value_type& __v);
1041#endif
1042
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001043 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001044
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046 void __copy_assign_alloc(const __tree& __t)
1047 {__copy_assign_alloc(__t, integral_constant<bool,
1048 __node_traits::propagate_on_container_copy_assignment::value>());}
1049
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051 void __copy_assign_alloc(const __tree& __t, true_type)
1052 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001053 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054 void __copy_assign_alloc(const __tree& __t, false_type) {}
1055
1056 void __move_assign(__tree& __t, false_type);
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001057 void __move_assign(__tree& __t, true_type)
1058 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1059 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001060
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062 void __move_assign_alloc(__tree& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001063 _NOEXCEPT_(
1064 !__node_traits::propagate_on_container_move_assignment::value ||
1065 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066 {__move_assign_alloc(__t, integral_constant<bool,
1067 __node_traits::propagate_on_container_move_assignment::value>());}
1068
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001069 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001070 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001071 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001072 {__node_alloc() = _STD::move(__t.__node_alloc());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001073 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001074 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001077 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001078 _NOEXCEPT_(
1079 !__node_traits::propagate_on_container_swap::value ||
1080 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081 {__swap_alloc(__x, __y, integral_constant<bool,
1082 __node_traits::propagate_on_container_swap::value>());}
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001083 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001084 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001085 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001086 {
1087 using _STD::swap;
1088 swap(__x, __y);
1089 }
Howard Hinnant8e29cda2010-09-21 20:16:37 +00001090 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001092 _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001093 {}
1094
1095 __node_pointer __detach();
1096 static __node_pointer __detach(__node_pointer);
1097};
1098
1099template <class _Tp, class _Compare, class _Allocator>
1100__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001101 _NOEXCEPT_(
1102 is_nothrow_default_constructible<__node_allocator>::value &&
1103 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104 : __pair3_(0, __comp)
1105{
1106 __begin_node() = __end_node();
1107}
1108
1109template <class _Tp, class _Compare, class _Allocator>
1110__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1111 : __pair1_(__node_allocator(__a)),
1112 __begin_node_(__node_pointer()),
1113 __pair3_(0)
1114{
1115 __begin_node() = __end_node();
1116}
1117
1118template <class _Tp, class _Compare, class _Allocator>
1119__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1120 const allocator_type& __a)
1121 : __pair1_(__node_allocator(__a)),
1122 __begin_node_(__node_pointer()),
1123 __pair3_(0, __comp)
1124{
1125 __begin_node() = __end_node();
1126}
1127
1128// Precondition: size() != 0
1129template <class _Tp, class _Compare, class _Allocator>
1130typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1131__tree<_Tp, _Compare, _Allocator>::__detach()
1132{
1133 __node_pointer __cache = __begin_node();
1134 __begin_node() = __end_node();
1135 __end_node()->__left_->__parent_ = nullptr;
1136 __end_node()->__left_ = nullptr;
1137 size() = 0;
1138 // __cache->__left_ == nullptr
1139 if (__cache->__right_ != nullptr)
1140 __cache = static_cast<__node_pointer>(__cache->__right_);
1141 // __cache->__left_ == nullptr
1142 // __cache->__right_ == nullptr
1143 return __cache;
1144}
1145
1146// Precondition: __cache != nullptr
1147// __cache->left_ == nullptr
1148// __cache->right_ == nullptr
1149// This is no longer a red-black tree
1150template <class _Tp, class _Compare, class _Allocator>
1151typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1152__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1153{
1154 if (__cache->__parent_ == nullptr)
1155 return nullptr;
1156 if (__tree_is_left_child(__cache))
1157 {
1158 __cache->__parent_->__left_ = nullptr;
1159 __cache = static_cast<__node_pointer>(__cache->__parent_);
1160 if (__cache->__right_ == nullptr)
1161 return __cache;
1162 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1163 }
1164 // __cache is right child
1165 __cache->__parent_->__right_ = nullptr;
1166 __cache = static_cast<__node_pointer>(__cache->__parent_);
1167 if (__cache->__left_ == nullptr)
1168 return __cache;
1169 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1170}
1171
1172template <class _Tp, class _Compare, class _Allocator>
1173__tree<_Tp, _Compare, _Allocator>&
1174__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1175{
1176 if (this != &__t)
1177 {
1178 value_comp() = __t.value_comp();
1179 __copy_assign_alloc(__t);
1180 __assign_multi(__t.begin(), __t.end());
1181 }
1182 return *this;
1183}
1184
1185template <class _Tp, class _Compare, class _Allocator>
1186template <class _InputIterator>
1187void
1188__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1189{
1190 if (size() != 0)
1191 {
1192 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001193#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001194 try
1195 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001196#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001197 for (; __cache != nullptr && __first != __last; ++__first)
1198 {
1199 __cache->__value_ = *__first;
1200 __node_pointer __next = __detach(__cache);
1201 __node_insert_unique(__cache);
1202 __cache = __next;
1203 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001204#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001205 }
1206 catch (...)
1207 {
1208 while (__cache->__parent_ != nullptr)
1209 __cache = static_cast<__node_pointer>(__cache->__parent_);
1210 destroy(__cache);
1211 throw;
1212 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001213#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001214 if (__cache != nullptr)
1215 {
1216 while (__cache->__parent_ != nullptr)
1217 __cache = static_cast<__node_pointer>(__cache->__parent_);
1218 destroy(__cache);
1219 }
1220 }
1221 for (; __first != __last; ++__first)
1222 __insert_unique(*__first);
1223}
1224
1225template <class _Tp, class _Compare, class _Allocator>
1226template <class _InputIterator>
1227void
1228__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1229{
1230 if (size() != 0)
1231 {
1232 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001233#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001234 try
1235 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001236#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001237 for (; __cache != nullptr && __first != __last; ++__first)
1238 {
1239 __cache->__value_ = *__first;
1240 __node_pointer __next = __detach(__cache);
1241 __node_insert_multi(__cache);
1242 __cache = __next;
1243 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001244#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001245 }
1246 catch (...)
1247 {
1248 while (__cache->__parent_ != nullptr)
1249 __cache = static_cast<__node_pointer>(__cache->__parent_);
1250 destroy(__cache);
1251 throw;
1252 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001253#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001254 if (__cache != nullptr)
1255 {
1256 while (__cache->__parent_ != nullptr)
1257 __cache = static_cast<__node_pointer>(__cache->__parent_);
1258 destroy(__cache);
1259 }
1260 }
1261 for (; __first != __last; ++__first)
1262 __insert_multi(*__first);
1263}
1264
1265template <class _Tp, class _Compare, class _Allocator>
1266__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1267 : __begin_node_(__node_pointer()),
1268 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1269 __pair3_(0, __t.value_comp())
1270{
1271 __begin_node() = __end_node();
1272}
1273
Howard Hinnant74279a52010-09-04 23:28:19 +00001274#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001275
1276template <class _Tp, class _Compare, class _Allocator>
1277__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001278 _NOEXCEPT_(
1279 is_nothrow_move_constructible<__node_allocator>::value &&
1280 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001281 : __begin_node_(_STD::move(__t.__begin_node_)),
1282 __pair1_(_STD::move(__t.__pair1_)),
1283 __pair3_(_STD::move(__t.__pair3_))
1284{
1285 if (size() == 0)
1286 __begin_node() = __end_node();
1287 else
1288 {
1289 __end_node()->__left_->__parent_ = __end_node();
1290 __t.__begin_node() = __t.__end_node();
1291 __t.__end_node()->__left_ = nullptr;
1292 __t.size() = 0;
1293 }
1294}
1295
1296template <class _Tp, class _Compare, class _Allocator>
1297__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1298 : __pair1_(__node_allocator(__a)),
1299 __pair3_(0, _STD::move(__t.value_comp()))
1300{
1301 if (__a == __t.__alloc())
1302 {
1303 if (__t.size() == 0)
1304 __begin_node() = __end_node();
1305 else
1306 {
1307 __begin_node() = __t.__begin_node();
1308 __end_node()->__left_ = __t.__end_node()->__left_;
1309 __end_node()->__left_->__parent_ = __end_node();
1310 size() = __t.size();
1311 __t.__begin_node() = __t.__end_node();
1312 __t.__end_node()->__left_ = nullptr;
1313 __t.size() = 0;
1314 }
1315 }
1316 else
1317 {
1318 __begin_node() = __end_node();
1319 }
1320}
1321
1322template <class _Tp, class _Compare, class _Allocator>
1323void
1324__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001325 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1326 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001327{
1328 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1329 __begin_node_ = __t.__begin_node_;
1330 __pair1_.first() = __t.__pair1_.first();
1331 __move_assign_alloc(__t);
1332 __pair3_ = _STD::move(__t.__pair3_);
1333 if (size() == 0)
1334 __begin_node() = __end_node();
1335 else
1336 {
1337 __end_node()->__left_->__parent_ = __end_node();
1338 __t.__begin_node() = __t.__end_node();
1339 __t.__end_node()->__left_ = nullptr;
1340 __t.size() = 0;
1341 }
1342}
1343
1344template <class _Tp, class _Compare, class _Allocator>
1345void
1346__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1347{
1348 if (__node_alloc() == __t.__node_alloc())
1349 __move_assign(__t, true_type());
1350 else
1351 {
1352 value_comp() = _STD::move(__t.value_comp());
1353 const_iterator __e = end();
1354 if (size() != 0)
1355 {
1356 __node_pointer __cache = __detach();
Howard Hinnant72f73582010-08-11 17:04:31 +00001357#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001358 try
1359 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001360#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001361 while (__cache != nullptr && __t.size() != 0)
1362 {
1363 __cache->__value_ = _STD::move(__t.remove(__t.begin())->__value_);
1364 __node_pointer __next = __detach(__cache);
1365 __node_insert_multi(__cache);
1366 __cache = __next;
1367 }
Howard Hinnant72f73582010-08-11 17:04:31 +00001368#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001369 }
1370 catch (...)
1371 {
1372 while (__cache->__parent_ != nullptr)
1373 __cache = static_cast<__node_pointer>(__cache->__parent_);
1374 destroy(__cache);
1375 throw;
1376 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +00001377#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001378 if (__cache != nullptr)
1379 {
1380 while (__cache->__parent_ != nullptr)
1381 __cache = static_cast<__node_pointer>(__cache->__parent_);
1382 destroy(__cache);
1383 }
1384 }
1385 while (__t.size() != 0)
1386 __insert_multi(__e, _STD::move(__t.remove(__t.begin())->__value_));
1387 }
1388}
1389
1390template <class _Tp, class _Compare, class _Allocator>
1391__tree<_Tp, _Compare, _Allocator>&
1392__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001393 _NOEXCEPT_(
1394 __node_traits::propagate_on_container_move_assignment::value &&
1395 is_nothrow_move_assignable<value_compare>::value &&
1396 is_nothrow_move_assignable<__node_allocator>::value)
1397
Howard Hinnantc51e1022010-05-11 19:42:16 +00001398{
1399 __move_assign(__t, integral_constant<bool,
1400 __node_traits::propagate_on_container_move_assignment::value>());
1401 return *this;
1402}
1403
Howard Hinnant74279a52010-09-04 23:28:19 +00001404#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001405
1406template <class _Tp, class _Compare, class _Allocator>
1407__tree<_Tp, _Compare, _Allocator>::~__tree()
1408{
1409 destroy(__root());
1410}
1411
1412template <class _Tp, class _Compare, class _Allocator>
1413void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001414__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001415{
1416 if (__nd != nullptr)
1417 {
1418 destroy(static_cast<__node_pointer>(__nd->__left_));
1419 destroy(static_cast<__node_pointer>(__nd->__right_));
1420 __node_allocator& __na = __node_alloc();
Howard Hinnant9a242b02011-02-02 17:36:20 +00001421 __node_traits::destroy(__na, _STD::addressof(__nd->__value_));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001422 __node_traits::deallocate(__na, __nd, 1);
1423 }
1424}
1425
1426template <class _Tp, class _Compare, class _Allocator>
1427void
1428__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001429 _NOEXCEPT_(
1430 __is_nothrow_swappable<value_compare>::value &&
1431 (!__node_traits::propagate_on_container_swap::value ||
1432 __is_nothrow_swappable<__node_allocator>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +00001433{
1434 using _STD::swap;
1435 swap(__begin_node_, __t.__begin_node_);
1436 swap(__pair1_.first(), __t.__pair1_.first());
1437 __swap_alloc(__node_alloc(), __t.__node_alloc());
1438 __pair3_.swap(__t.__pair3_);
1439 if (size() == 0)
1440 __begin_node() = __end_node();
1441 else
1442 __end_node()->__left_->__parent_ = __end_node();
1443 if (__t.size() == 0)
1444 __t.__begin_node() = __t.__end_node();
1445 else
1446 __t.__end_node()->__left_->__parent_ = __t.__end_node();
1447}
1448
1449template <class _Tp, class _Compare, class _Allocator>
1450void
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00001451__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00001452{
1453 destroy(__root());
1454 size() = 0;
1455 __begin_node() = __end_node();
1456 __end_node()->__left_ = nullptr;
1457}
1458
1459// Find lower_bound place to insert
1460// Set __parent to parent of null leaf
1461// Return reference to null leaf
1462template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001463typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1464__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001465 const value_type& __v)
1466{
1467 __node_pointer __nd = __root();
1468 if (__nd != nullptr)
1469 {
1470 while (true)
1471 {
1472 if (value_comp()(__nd->__value_, __v))
1473 {
1474 if (__nd->__right_ != nullptr)
1475 __nd = static_cast<__node_pointer>(__nd->__right_);
1476 else
1477 {
1478 __parent = __nd;
1479 return __parent->__right_;
1480 }
1481 }
1482 else
1483 {
1484 if (__nd->__left_ != nullptr)
1485 __nd = static_cast<__node_pointer>(__nd->__left_);
1486 else
1487 {
1488 __parent = __nd;
1489 return __parent->__left_;
1490 }
1491 }
1492 }
1493 }
1494 __parent = __end_node();
1495 return __parent->__left_;
1496}
1497
1498// Find upper_bound place to insert
1499// Set __parent to parent of null leaf
1500// Return reference to null leaf
1501template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001502typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1503__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001504 const value_type& __v)
1505{
1506 __node_pointer __nd = __root();
1507 if (__nd != nullptr)
1508 {
1509 while (true)
1510 {
1511 if (value_comp()(__v, __nd->__value_))
1512 {
1513 if (__nd->__left_ != nullptr)
1514 __nd = static_cast<__node_pointer>(__nd->__left_);
1515 else
1516 {
1517 __parent = __nd;
1518 return __parent->__left_;
1519 }
1520 }
1521 else
1522 {
1523 if (__nd->__right_ != nullptr)
1524 __nd = static_cast<__node_pointer>(__nd->__right_);
1525 else
1526 {
1527 __parent = __nd;
1528 return __parent->__right_;
1529 }
1530 }
1531 }
1532 }
1533 __parent = __end_node();
1534 return __parent->__left_;
1535}
1536
1537// Find leaf place to insert closest to __hint
1538// First check prior to __hint.
1539// Next check after __hint.
1540// Next do O(log N) search.
1541// Set __parent to parent of null leaf
1542// Return reference to null leaf
1543template <class _Tp, class _Compare, class _Allocator>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001544typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001545__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnant6108fd72011-04-03 20:05:29 +00001546 typename __node_base::pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001547 const value_type& __v)
1548{
1549 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1550 {
1551 // __v <= *__hint
1552 const_iterator __prior = __hint;
1553 if (__prior == begin() || !value_comp()(__v, *--__prior))
1554 {
1555 // *prev(__hint) <= __v <= *__hint
1556 if (__hint.__ptr_->__left_ == nullptr)
1557 {
1558 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1559 return __parent->__left_;
1560 }
1561 else
1562 {
1563 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1564 return __parent->__right_;
1565 }
1566 }
1567 // __v < *prev(__hint)
1568 return __find_leaf_high(__parent, __v);
1569 }
1570 // else __v > *__hint
1571 return __find_leaf_low(__parent, __v);
1572}
1573
1574// Find place to insert if __v doesn't exist
1575// Set __parent to parent of null leaf
1576// Return reference to null leaf
1577// If __v exists, set parent to node of __v and return reference to node of __v
1578template <class _Tp, class _Compare, class _Allocator>
1579template <class _Key>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001580typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1581__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001582 const _Key& __v)
1583{
1584 __node_pointer __nd = __root();
1585 if (__nd != nullptr)
1586 {
1587 while (true)
1588 {
1589 if (value_comp()(__v, __nd->__value_))
1590 {
1591 if (__nd->__left_ != nullptr)
1592 __nd = static_cast<__node_pointer>(__nd->__left_);
1593 else
1594 {
1595 __parent = __nd;
1596 return __parent->__left_;
1597 }
1598 }
1599 else if (value_comp()(__nd->__value_, __v))
1600 {
1601 if (__nd->__right_ != nullptr)
1602 __nd = static_cast<__node_pointer>(__nd->__right_);
1603 else
1604 {
1605 __parent = __nd;
1606 return __parent->__right_;
1607 }
1608 }
1609 else
1610 {
1611 __parent = __nd;
1612 return __parent;
1613 }
1614 }
1615 }
1616 __parent = __end_node();
1617 return __parent->__left_;
1618}
1619
1620// Find place to insert if __v doesn't exist
1621// First check prior to __hint.
1622// Next check after __hint.
1623// Next do O(log N) search.
1624// Set __parent to parent of null leaf
1625// Return reference to null leaf
1626// If __v exists, set parent to node of __v and return reference to node of __v
1627template <class _Tp, class _Compare, class _Allocator>
1628template <class _Key>
Howard Hinnant6108fd72011-04-03 20:05:29 +00001629typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001630__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnant6108fd72011-04-03 20:05:29 +00001631 typename __node_base::pointer& __parent,
Howard Hinnantc51e1022010-05-11 19:42:16 +00001632 const _Key& __v)
1633{
1634 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1635 {
1636 // __v < *__hint
1637 const_iterator __prior = __hint;
1638 if (__prior == begin() || value_comp()(*--__prior, __v))
1639 {
1640 // *prev(__hint) < __v < *__hint
1641 if (__hint.__ptr_->__left_ == nullptr)
1642 {
1643 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1644 return __parent->__left_;
1645 }
1646 else
1647 {
1648 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1649 return __parent->__right_;
1650 }
1651 }
1652 // __v <= *prev(__hint)
1653 return __find_equal(__parent, __v);
1654 }
1655 else if (value_comp()(*__hint, __v)) // check after
1656 {
1657 // *__hint < __v
1658 const_iterator __next = _STD::next(__hint);
1659 if (__next == end() || value_comp()(__v, *__next))
1660 {
Douglas Gregor27e031a2011-04-29 16:20:26 +00001661 // *__hint < __v < *_STD::next(__hint)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001662 if (__hint.__ptr_->__right_ == nullptr)
1663 {
1664 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1665 return __parent->__right_;
1666 }
1667 else
1668 {
1669 __parent = const_cast<__node_pointer&>(__next.__ptr_);
1670 return __parent->__left_;
1671 }
1672 }
1673 // *next(__hint) <= __v
1674 return __find_equal(__parent, __v);
1675 }
1676 // else __v == *__hint
1677 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1678 return __parent;
1679}
1680
1681template <class _Tp, class _Compare, class _Allocator>
1682void
1683__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1684 __node_base_pointer& __child,
1685 __node_base_pointer __new_node)
1686{
1687 __new_node->__left_ = nullptr;
1688 __new_node->__right_ = nullptr;
1689 __new_node->__parent_ = __parent;
1690 __child = __new_node;
1691 if (__begin_node()->__left_ != nullptr)
1692 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1693 __tree_balance_after_insert(__end_node()->__left_, __child);
1694 ++size();
1695}
1696
Howard Hinnant74279a52010-09-04 23:28:19 +00001697#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1698#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +00001699
1700template <class _Tp, class _Compare, class _Allocator>
1701template <class ..._Args>
1702typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1703__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1704{
1705 __node_allocator& __na = __node_alloc();
1706 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant9a242b02011-02-02 17:36:20 +00001707 __node_traits::construct(__na, _STD::addressof(__h->__value_), _STD::forward<_Args>(__args)...);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001708 __h.get_deleter().__value_constructed = true;
1709 return __h;
1710}
1711
1712template <class _Tp, class _Compare, class _Allocator>
1713template <class... _Args>
1714pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1715__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1716{
1717 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1718 __node_base_pointer __parent;
1719 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1720 __node_pointer __r = static_cast<__node_pointer>(__child);
1721 bool __inserted = false;
1722 if (__child == nullptr)
1723 {
1724 __insert_node_at(__parent, __child, __h.get());
1725 __r = __h.release();
1726 __inserted = true;
1727 }
1728 return pair<iterator, bool>(iterator(__r), __inserted);
1729}
1730
1731template <class _Tp, class _Compare, class _Allocator>
1732template <class... _Args>
1733typename __tree<_Tp, _Compare, _Allocator>::iterator
1734__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1735{
1736 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1737 __node_base_pointer __parent;
1738 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1739 __node_pointer __r = static_cast<__node_pointer>(__child);
1740 if (__child == nullptr)
1741 {
1742 __insert_node_at(__parent, __child, __h.get());
1743 __r = __h.release();
1744 }
1745 return iterator(__r);
1746}
1747
1748template <class _Tp, class _Compare, class _Allocator>
1749template <class... _Args>
1750typename __tree<_Tp, _Compare, _Allocator>::iterator
1751__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1752{
1753 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1754 __node_base_pointer __parent;
1755 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1756 __insert_node_at(__parent, __child, __h.get());
1757 return iterator(static_cast<__node_pointer>(__h.release()));
1758}
1759
1760template <class _Tp, class _Compare, class _Allocator>
1761template <class... _Args>
1762typename __tree<_Tp, _Compare, _Allocator>::iterator
1763__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1764 _Args&&... __args)
1765{
1766 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1767 __node_base_pointer __parent;
1768 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1769 __insert_node_at(__parent, __child, __h.get());
1770 return iterator(static_cast<__node_pointer>(__h.release()));
1771}
1772
Howard Hinnant74279a52010-09-04 23:28:19 +00001773#endif // _LIBCPP_HAS_NO_VARIADICS
1774
Howard Hinnantc51e1022010-05-11 19:42:16 +00001775template <class _Tp, class _Compare, class _Allocator>
1776template <class _V>
1777pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1778__tree<_Tp, _Compare, _Allocator>::__insert_unique(_V&& __v)
1779{
Howard Hinnantc5bc8672011-03-10 17:27:57 +00001780 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1781 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1782 if (__r.second)
1783 __h.release();
1784 return __r;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001785}
1786
1787template <class _Tp, class _Compare, class _Allocator>
1788template <class _V>
1789typename __tree<_Tp, _Compare, _Allocator>::iterator
1790__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _V&& __v)
1791{
Howard Hinnantc5bc8672011-03-10 17:27:57 +00001792 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1793 iterator __r = __node_insert_unique(__p, __h.get());
1794 if (__r.__ptr_ == __h.get())
1795 __h.release();
1796 return __r;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001797}
1798
1799template <class _Tp, class _Compare, class _Allocator>
1800template <class _V>
1801typename __tree<_Tp, _Compare, _Allocator>::iterator
1802__tree<_Tp, _Compare, _Allocator>::__insert_multi(_V&& __v)
1803{
1804 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1805 __node_base_pointer __parent;
1806 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1807 __insert_node_at(__parent, __child, __h.get());
1808 return iterator(__h.release());
1809}
1810
1811template <class _Tp, class _Compare, class _Allocator>
1812template <class _V>
1813typename __tree<_Tp, _Compare, _Allocator>::iterator
1814__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _V&& __v)
1815{
1816 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1817 __node_base_pointer __parent;
1818 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1819 __insert_node_at(__parent, __child, __h.get());
1820 return iterator(__h.release());
1821}
1822
Howard Hinnant74279a52010-09-04 23:28:19 +00001823#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc51e1022010-05-11 19:42:16 +00001824
1825template <class _Tp, class _Compare, class _Allocator>
1826typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1827__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1828{
1829 __node_allocator& __na = __node_alloc();
1830 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant9a242b02011-02-02 17:36:20 +00001831 __node_traits::construct(__na, _STD::addressof(__h->__value_), __v);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001832 __h.get_deleter().__value_constructed = true;
1833 return _STD::move(__h);
1834}
1835
Howard Hinnantc5bc8672011-03-10 17:27:57 +00001836#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1837
Howard Hinnantc51e1022010-05-11 19:42:16 +00001838template <class _Tp, class _Compare, class _Allocator>
1839pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1840__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1841{
1842 __node_base_pointer __parent;
1843 __node_base_pointer& __child = __find_equal(__parent, __v);
1844 __node_pointer __r = static_cast<__node_pointer>(__child);
1845 bool __inserted = false;
1846 if (__child == nullptr)
1847 {
1848 __node_holder __h = __construct_node(__v);
1849 __insert_node_at(__parent, __child, __h.get());
1850 __r = __h.release();
1851 __inserted = true;
1852 }
1853 return pair<iterator, bool>(iterator(__r), __inserted);
1854}
1855
1856template <class _Tp, class _Compare, class _Allocator>
1857typename __tree<_Tp, _Compare, _Allocator>::iterator
1858__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1859{
1860 __node_base_pointer __parent;
1861 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1862 __node_pointer __r = static_cast<__node_pointer>(__child);
1863 if (__child == nullptr)
1864 {
1865 __node_holder __h = __construct_node(__v);
1866 __insert_node_at(__parent, __child, __h.get());
1867 __r = __h.release();
1868 }
1869 return iterator(__r);
1870}
1871
1872template <class _Tp, class _Compare, class _Allocator>
1873typename __tree<_Tp, _Compare, _Allocator>::iterator
1874__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1875{
1876 __node_base_pointer __parent;
1877 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1878 __node_holder __h = __construct_node(__v);
1879 __insert_node_at(__parent, __child, __h.get());
1880 return iterator(__h.release());
1881}
1882
1883template <class _Tp, class _Compare, class _Allocator>
1884typename __tree<_Tp, _Compare, _Allocator>::iterator
1885__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1886{
1887 __node_base_pointer __parent;
1888 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1889 __node_holder __h = __construct_node(__v);
1890 __insert_node_at(__parent, __child, __h.get());
1891 return iterator(__h.release());
1892}
1893
Howard Hinnantc51e1022010-05-11 19:42:16 +00001894template <class _Tp, class _Compare, class _Allocator>
1895pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1896__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1897{
1898 __node_base_pointer __parent;
1899 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1900 __node_pointer __r = static_cast<__node_pointer>(__child);
1901 bool __inserted = false;
1902 if (__child == nullptr)
1903 {
1904 __insert_node_at(__parent, __child, __nd);
1905 __r = __nd;
1906 __inserted = true;
1907 }
1908 return pair<iterator, bool>(iterator(__r), __inserted);
1909}
1910
1911template <class _Tp, class _Compare, class _Allocator>
1912typename __tree<_Tp, _Compare, _Allocator>::iterator
1913__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1914 __node_pointer __nd)
1915{
1916 __node_base_pointer __parent;
1917 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1918 __node_pointer __r = static_cast<__node_pointer>(__child);
1919 if (__child == nullptr)
1920 {
1921 __insert_node_at(__parent, __child, __nd);
1922 __r = __nd;
1923 }
1924 return iterator(__r);
1925}
1926
1927template <class _Tp, class _Compare, class _Allocator>
1928typename __tree<_Tp, _Compare, _Allocator>::iterator
1929__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1930{
1931 __node_base_pointer __parent;
1932 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
1933 __insert_node_at(__parent, __child, __nd);
1934 return iterator(__nd);
1935}
1936
1937template <class _Tp, class _Compare, class _Allocator>
1938typename __tree<_Tp, _Compare, _Allocator>::iterator
1939__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1940 __node_pointer __nd)
1941{
1942 __node_base_pointer __parent;
1943 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
1944 __insert_node_at(__parent, __child, __nd);
1945 return iterator(__nd);
1946}
1947
1948template <class _Tp, class _Compare, class _Allocator>
1949typename __tree<_Tp, _Compare, _Allocator>::iterator
1950__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1951{
1952 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
1953 iterator __r(__np);
1954 ++__r;
1955 if (__begin_node() == __np)
1956 __begin_node() = __r.__ptr_;
1957 --size();
1958 __node_allocator& __na = __node_alloc();
Howard Hinnant9a242b02011-02-02 17:36:20 +00001959 __node_traits::destroy(__na, const_cast<value_type*>(_STD::addressof(*__p)));
Howard Hinnantc51e1022010-05-11 19:42:16 +00001960 __tree_remove(__end_node()->__left_,
1961 static_cast<__node_base_pointer>(__np));
1962 __node_traits::deallocate(__na, __np, 1);
1963 return __r;
1964}
1965
1966template <class _Tp, class _Compare, class _Allocator>
1967typename __tree<_Tp, _Compare, _Allocator>::iterator
1968__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1969{
1970 while (__f != __l)
1971 __f = erase(__f);
1972 return iterator(const_cast<__node_pointer>(__l.__ptr_));
1973}
1974
1975template <class _Tp, class _Compare, class _Allocator>
1976template <class _Key>
1977typename __tree<_Tp, _Compare, _Allocator>::size_type
1978__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1979{
1980 iterator __i = find(__k);
1981 if (__i == end())
1982 return 0;
1983 erase(__i);
1984 return 1;
1985}
1986
1987template <class _Tp, class _Compare, class _Allocator>
1988template <class _Key>
1989typename __tree<_Tp, _Compare, _Allocator>::size_type
1990__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
1991{
1992 pair<iterator, iterator> __p = __equal_range_multi(__k);
1993 size_type __r = 0;
1994 for (; __p.first != __p.second; ++__r)
1995 __p.first = erase(__p.first);
1996 return __r;
1997}
1998
1999template <class _Tp, class _Compare, class _Allocator>
2000template <class _Key>
2001typename __tree<_Tp, _Compare, _Allocator>::iterator
2002__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2003{
2004 iterator __p = __lower_bound(__v, __root(), __end_node());
2005 if (__p != end() && !value_comp()(__v, *__p))
2006 return __p;
2007 return end();
2008}
2009
2010template <class _Tp, class _Compare, class _Allocator>
2011template <class _Key>
2012typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2013__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2014{
2015 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2016 if (__p != end() && !value_comp()(__v, *__p))
2017 return __p;
2018 return end();
2019}
2020
2021template <class _Tp, class _Compare, class _Allocator>
2022template <class _Key>
2023typename __tree<_Tp, _Compare, _Allocator>::size_type
2024__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2025{
2026 __node_const_pointer __result = __end_node();
2027 __node_const_pointer __rt = __root();
2028 while (__rt != nullptr)
2029 {
2030 if (value_comp()(__k, __rt->__value_))
2031 {
2032 __result = __rt;
2033 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2034 }
2035 else if (value_comp()(__rt->__value_, __k))
2036 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2037 else
2038 return 1;
2039 }
2040 return 0;
2041}
2042
2043template <class _Tp, class _Compare, class _Allocator>
2044template <class _Key>
2045typename __tree<_Tp, _Compare, _Allocator>::size_type
2046__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2047{
2048 typedef pair<const_iterator, const_iterator> _P;
2049 __node_const_pointer __result = __end_node();
2050 __node_const_pointer __rt = __root();
2051 while (__rt != nullptr)
2052 {
2053 if (value_comp()(__k, __rt->__value_))
2054 {
2055 __result = __rt;
2056 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2057 }
2058 else if (value_comp()(__rt->__value_, __k))
2059 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2060 else
2061 return _STD::distance(
2062 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2063 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
2064 );
2065 }
2066 return 0;
2067}
2068
2069template <class _Tp, class _Compare, class _Allocator>
2070template <class _Key>
2071typename __tree<_Tp, _Compare, _Allocator>::iterator
2072__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2073 __node_pointer __root,
2074 __node_pointer __result)
2075{
2076 while (__root != nullptr)
2077 {
2078 if (!value_comp()(__root->__value_, __v))
2079 {
2080 __result = __root;
2081 __root = static_cast<__node_pointer>(__root->__left_);
2082 }
2083 else
2084 __root = static_cast<__node_pointer>(__root->__right_);
2085 }
2086 return iterator(__result);
2087}
2088
2089template <class _Tp, class _Compare, class _Allocator>
2090template <class _Key>
2091typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2092__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2093 __node_const_pointer __root,
2094 __node_const_pointer __result) const
2095{
2096 while (__root != nullptr)
2097 {
2098 if (!value_comp()(__root->__value_, __v))
2099 {
2100 __result = __root;
2101 __root = static_cast<__node_const_pointer>(__root->__left_);
2102 }
2103 else
2104 __root = static_cast<__node_const_pointer>(__root->__right_);
2105 }
2106 return const_iterator(__result);
2107}
2108
2109template <class _Tp, class _Compare, class _Allocator>
2110template <class _Key>
2111typename __tree<_Tp, _Compare, _Allocator>::iterator
2112__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2113 __node_pointer __root,
2114 __node_pointer __result)
2115{
2116 while (__root != nullptr)
2117 {
2118 if (value_comp()(__v, __root->__value_))
2119 {
2120 __result = __root;
2121 __root = static_cast<__node_pointer>(__root->__left_);
2122 }
2123 else
2124 __root = static_cast<__node_pointer>(__root->__right_);
2125 }
2126 return iterator(__result);
2127}
2128
2129template <class _Tp, class _Compare, class _Allocator>
2130template <class _Key>
2131typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2132__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2133 __node_const_pointer __root,
2134 __node_const_pointer __result) const
2135{
2136 while (__root != nullptr)
2137 {
2138 if (value_comp()(__v, __root->__value_))
2139 {
2140 __result = __root;
2141 __root = static_cast<__node_const_pointer>(__root->__left_);
2142 }
2143 else
2144 __root = static_cast<__node_const_pointer>(__root->__right_);
2145 }
2146 return const_iterator(__result);
2147}
2148
2149template <class _Tp, class _Compare, class _Allocator>
2150template <class _Key>
2151pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2152 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2153__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2154{
2155 typedef pair<iterator, iterator> _P;
2156 __node_pointer __result = __end_node();
2157 __node_pointer __rt = __root();
2158 while (__rt != nullptr)
2159 {
2160 if (value_comp()(__k, __rt->__value_))
2161 {
2162 __result = __rt;
2163 __rt = static_cast<__node_pointer>(__rt->__left_);
2164 }
2165 else if (value_comp()(__rt->__value_, __k))
2166 __rt = static_cast<__node_pointer>(__rt->__right_);
2167 else
2168 return _P(iterator(__rt),
2169 iterator(
2170 __rt->__right_ != nullptr ?
2171 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2172 : __result));
2173 }
2174 return _P(iterator(__result), iterator(__result));
2175}
2176
2177template <class _Tp, class _Compare, class _Allocator>
2178template <class _Key>
2179pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2180 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2181__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2182{
2183 typedef pair<const_iterator, const_iterator> _P;
2184 __node_const_pointer __result = __end_node();
2185 __node_const_pointer __rt = __root();
2186 while (__rt != nullptr)
2187 {
2188 if (value_comp()(__k, __rt->__value_))
2189 {
2190 __result = __rt;
2191 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2192 }
2193 else if (value_comp()(__rt->__value_, __k))
2194 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2195 else
2196 return _P(const_iterator(__rt),
2197 const_iterator(
2198 __rt->__right_ != nullptr ?
2199 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2200 : __result));
2201 }
2202 return _P(const_iterator(__result), const_iterator(__result));
2203}
2204
2205template <class _Tp, class _Compare, class _Allocator>
2206template <class _Key>
2207pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2208 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2209__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2210{
2211 typedef pair<iterator, iterator> _P;
2212 __node_pointer __result = __end_node();
2213 __node_pointer __rt = __root();
2214 while (__rt != nullptr)
2215 {
2216 if (value_comp()(__k, __rt->__value_))
2217 {
2218 __result = __rt;
2219 __rt = static_cast<__node_pointer>(__rt->__left_);
2220 }
2221 else if (value_comp()(__rt->__value_, __k))
2222 __rt = static_cast<__node_pointer>(__rt->__right_);
2223 else
2224 return _P(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2225 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2226 }
2227 return _P(iterator(__result), iterator(__result));
2228}
2229
2230template <class _Tp, class _Compare, class _Allocator>
2231template <class _Key>
2232pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2233 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2234__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2235{
2236 typedef pair<const_iterator, const_iterator> _P;
2237 __node_const_pointer __result = __end_node();
2238 __node_const_pointer __rt = __root();
2239 while (__rt != nullptr)
2240 {
2241 if (value_comp()(__k, __rt->__value_))
2242 {
2243 __result = __rt;
2244 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2245 }
2246 else if (value_comp()(__rt->__value_, __k))
2247 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2248 else
2249 return _P(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2250 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2251 }
2252 return _P(const_iterator(__result), const_iterator(__result));
2253}
2254
2255template <class _Tp, class _Compare, class _Allocator>
2256typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant1113b5e2011-06-04 17:10:24 +00002257__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +00002258{
2259 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
2260 if (__begin_node() == __np)
2261 {
2262 if (__np->__right_ != nullptr)
2263 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2264 else
2265 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2266 }
2267 --size();
2268 __tree_remove(__end_node()->__left_,
2269 static_cast<__node_base_pointer>(__np));
2270 return __node_holder(__np, _D(__node_alloc()));
2271}
2272
Howard Hinnant2f1ea9c2011-06-04 14:31:57 +00002273template <class _Tp, class _Compare, class _Allocator>
2274inline _LIBCPP_INLINE_VISIBILITY
2275void
2276swap(__tree<_Tp, _Compare, _Allocator>& __x,
2277 __tree<_Tp, _Compare, _Allocator>& __y)
2278 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2279{
2280 __x.swap(__y);
2281}
2282
Howard Hinnantc51e1022010-05-11 19:42:16 +00002283_LIBCPP_END_NAMESPACE_STD
2284
2285#endif // _LIBCPP___TREE