blob: 10b5f3e51585577c2dd2ed3a016c0e397153df47 [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//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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;
25template <class, class, class> class __tree_iterator;
26template <class, class, class> class __tree_const_iterator;
27template <class, class, class, class> class map;
28template <class, class, class, class> class multimap;
29template <class, class, class> class set;
30template <class, class, class> class multiset;
31
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>
56inline
57bool
58__tree_is_left_child(_NodePtr __x)
59{
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>
122inline
123_NodePtr
124__tree_min(_NodePtr __x)
125{
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>
134inline
135_NodePtr
136__tree_max(_NodePtr __x)
137{
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
147__tree_next(_NodePtr __x)
148{
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
160__tree_prev(_NodePtr __x)
161{
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
173__tree_leaf(_NodePtr __x)
174{
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
197__tree_left_rotate(_NodePtr __x)
198{
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
217__tree_right_rotate(_NodePtr __x)
218{
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
242__tree_balance_after_insert(_NodePtr __root, _NodePtr __x)
243{
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
312__tree_remove(_NodePtr __root, _NodePtr __z)
313{
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
497template <class> class __map_node_destructor;
498
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
516 explicit __tree_node_destructor(allocator_type& __na)
517 : __na_(__na),
518 __value_constructed(false)
519 {}
520
521 void operator()(pointer __p)
522 {
523 if (__value_constructed)
524 __alloc_traits::destroy(__na_, addressof(__p->__value_));
525 if (__p)
526 __alloc_traits::deallocate(__na_, __p, 1);
527 }
528
529 template <class> friend class __map_node_destructor;
530};
531
532// node
533
534template <class _Pointer>
535class __tree_end_node
536{
537public:
538 typedef _Pointer pointer;
539 pointer __left_;
540
541 __tree_end_node() : __left_() {}
542};
543
544template <class _VoidPtr>
545class __tree_node_base
546 : public __tree_end_node
547 <
548 typename pointer_traits<_VoidPtr>::template
549#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
550 rebind<__tree_node_base<_VoidPtr> >
551#else
552 rebind<__tree_node_base<_VoidPtr> >::other
553#endif
554 >
555{
556 __tree_node_base(const __tree_node_base&);
557 __tree_node_base& operator=(const __tree_node_base&);
558public:
559 typedef typename pointer_traits<_VoidPtr>::template
560#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
561 rebind<__tree_node_base>
562#else
563 rebind<__tree_node_base>::other
564#endif
565 pointer;
566 typedef typename pointer_traits<_VoidPtr>::template
567#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
568 rebind<const __tree_node_base>
569#else
570 rebind<const __tree_node_base>::other
571#endif
572 const_pointer;
573 typedef __tree_end_node<pointer> base;
574
575 pointer __right_;
576 pointer __parent_;
577 bool __is_black_;
578
579 __tree_node_base() : __right_(), __parent_(), __is_black_(false) {}
580};
581
582template <class _Tp, class _VoidPtr>
583class __tree_node
584 : public __tree_node_base<_VoidPtr>
585{
586public:
587 typedef __tree_node_base<_VoidPtr> base;
588 typedef _Tp value_type;
589
590 value_type __value_;
591
Howard Hinnant73d21a42010-09-04 23:28:19 +0000592#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593 template <class ..._Args>
594 explicit __tree_node(_Args&& ...__args)
595 : __value_(_STD::forward<_Args>(__args)...) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000596#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 explicit __tree_node(const value_type& __v)
598 : __value_(__v) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000599#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000600};
601
602template <class> class __map_iterator;
603template <class> class __map_const_iterator;
604
605template <class _Tp, class _NodePtr, class _DiffType>
606class __tree_iterator
607{
608 typedef _NodePtr __node_pointer;
609 typedef typename pointer_traits<__node_pointer>::element_type __node;
610 typedef typename __node::base __node_base;
611 typedef typename __node_base::pointer __node_base_pointer;
612
613 __node_pointer __ptr_;
614
615 typedef pointer_traits<__node_pointer> __pointer_traits;
616public:
617 typedef bidirectional_iterator_tag iterator_category;
618 typedef _Tp value_type;
619 typedef _DiffType difference_type;
620 typedef value_type& reference;
621 typedef typename pointer_traits<__node_pointer>::template
622#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
623 rebind<value_type>
624#else
625 rebind<value_type>::other
626#endif
627 pointer;
628
629 __tree_iterator() {}
630
631 reference operator*() const {return __ptr_->__value_;}
632 pointer operator->() const {return &__ptr_->__value_;}
633
634 __tree_iterator& operator++()
635 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
636 return *this;}
637 __tree_iterator operator++(int)
638 {__tree_iterator __t(*this); ++(*this); return __t;}
639
640 __tree_iterator& operator--()
641 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
642 return *this;}
643 __tree_iterator operator--(int)
644 {__tree_iterator __t(*this); --(*this); return __t;}
645
646 friend bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
647 {return __x.__ptr_ == __y.__ptr_;}
648 friend bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
649 {return !(__x == __y);}
650
651private:
652 explicit __tree_iterator(__node_pointer __p) : __ptr_(__p) {}
653 template <class, class, class> friend class __tree;
654 template <class, class, class> friend class __tree_const_iterator;
655 template <class> friend class __map_iterator;
656 template <class, class, class, class> friend class map;
657 template <class, class, class, class> friend class multimap;
658 template <class, class, class> friend class set;
659 template <class, class, class> friend class multiset;
660};
661
662template <class _Tp, class _ConstNodePtr, class _DiffType>
663class __tree_const_iterator
664{
665 typedef _ConstNodePtr __node_pointer;
666 typedef typename pointer_traits<__node_pointer>::element_type __node;
667 typedef const typename __node::base __node_base;
668 typedef typename pointer_traits<__node_pointer>::template
669#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
670 rebind<__node_base>
671#else
672 rebind<__node_base>::other
673#endif
674 __node_base_pointer;
675
676 __node_pointer __ptr_;
677
678 typedef pointer_traits<__node_pointer> __pointer_traits;
679public:
680 typedef bidirectional_iterator_tag iterator_category;
681 typedef _Tp value_type;
682 typedef _DiffType difference_type;
683 typedef const value_type& reference;
684 typedef typename pointer_traits<__node_pointer>::template
685#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
686 rebind<const value_type>
687#else
688 rebind<const value_type>::other
689#endif
690 pointer;
691
692 __tree_const_iterator() {}
693private:
694 typedef typename remove_const<__node>::type __non_const_node;
695 typedef typename pointer_traits<__node_pointer>::template
696#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
697 rebind<__non_const_node>
698#else
699 rebind<__non_const_node>::other
700#endif
701 __non_const_node_pointer;
702 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
703 __non_const_iterator;
704public:
705 __tree_const_iterator(__non_const_iterator __p) : __ptr_(__p.__ptr_) {}
706
707 reference operator*() const {return __ptr_->__value_;}
708 pointer operator->() const {return &__ptr_->__value_;}
709
710 __tree_const_iterator& operator++()
711 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
712 return *this;}
713 __tree_const_iterator operator++(int)
714 {__tree_const_iterator __t(*this); ++(*this); return __t;}
715
716 __tree_const_iterator& operator--()
717 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
718 return *this;}
719 __tree_const_iterator operator--(int)
720 {__tree_const_iterator __t(*this); --(*this); return __t;}
721
722 friend bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
723 {return __x.__ptr_ == __y.__ptr_;}
724 friend bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
725 {return !(__x == __y);}
726
727private:
728 explicit __tree_const_iterator(__node_pointer __p) : __ptr_(__p) {}
729 template <class, class, class> friend class __tree;
730 template <class, class, class, class> friend class map;
731 template <class, class, class, class> friend class multimap;
732 template <class, class, class> friend class set;
733 template <class, class, class> friend class multiset;
734 template <class> friend class __map_const_iterator;
735};
736
737template <class _Tp, class _Compare, class _Allocator>
738class __tree
739{
740public:
741 typedef _Tp value_type;
742 typedef _Compare value_compare;
743 typedef _Allocator allocator_type;
744 typedef allocator_traits<allocator_type> __alloc_traits;
745 typedef typename __alloc_traits::pointer pointer;
746 typedef typename __alloc_traits::const_pointer const_pointer;
747 typedef typename __alloc_traits::size_type size_type;
748 typedef typename __alloc_traits::difference_type difference_type;
749
750 typedef __tree_node<value_type, typename __alloc_traits::void_pointer> __node;
751 typedef typename __alloc_traits::template
752#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
753 rebind_alloc<__node>
754#else
755 rebind_alloc<__node>::other
756#endif
757 __node_allocator;
758 typedef allocator_traits<__node_allocator> __node_traits;
759 typedef typename __node_traits::pointer __node_pointer;
760 typedef typename __node_traits::const_pointer __node_const_pointer;
761 typedef typename __node::base::pointer __node_base_pointer;
762 typedef typename __node::base::const_pointer __node_base_const_pointer;
763private:
764 typedef typename __node::base::base __end_node_t;
765 typedef typename pointer_traits<__node_pointer>::template
766#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
767 rebind<__end_node_t>
768#else
769 rebind<__end_node_t>::other
770#endif
771 __end_node_ptr;
772 typedef typename pointer_traits<__node_pointer>::template
773#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
774 rebind<const __end_node_t>
775#else
776 rebind<const __end_node_t>::other
777#endif
778 __end_node_const_ptr;
779
780 __node_pointer __begin_node_;
781 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
782 __compressed_pair<size_type, value_compare> __pair3_;
783
784public:
785 __node_pointer __end_node()
786 {
787 return static_cast<__node_pointer>
788 (
789 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
790 );
791 }
792 __node_const_pointer __end_node() const
793 {
794 return static_cast<__node_const_pointer>
795 (
796 pointer_traits<__end_node_const_ptr>::pointer_to(__pair1_.first())
797 );
798 }
799 __node_allocator& __node_alloc() {return __pair1_.second();}
800private:
801 const __node_allocator& __node_alloc() const {return __pair1_.second();}
802 __node_pointer& __begin_node() {return __begin_node_;}
803 const __node_pointer& __begin_node() const {return __begin_node_;}
804public:
805 allocator_type __alloc() const {return allocator_type(__node_alloc());}
806private:
807 size_type& size() {return __pair3_.first();}
808public:
809 const size_type& size() const {return __pair3_.first();}
810 value_compare& value_comp() {return __pair3_.second();}
811 const value_compare& value_comp() const {return __pair3_.second();}
812public:
813 __node_pointer __root()
814 {return static_cast<__node_pointer> (__end_node()->__left_);}
815 __node_const_pointer __root() const
816 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
817
818 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
819 typedef __tree_const_iterator<value_type, __node_const_pointer, difference_type> const_iterator;
820
821 explicit __tree(const value_compare& __comp);
822 explicit __tree(const allocator_type& __a);
823 __tree(const value_compare& __comp, const allocator_type& __a);
824 __tree(const __tree& __t);
825 __tree& operator=(const __tree& __t);
826 template <class _InputIterator>
827 void __assign_unique(_InputIterator __first, _InputIterator __last);
828 template <class _InputIterator>
829 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000830#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831 __tree(__tree&& __t);
832 __tree(__tree&& __t, const allocator_type& __a);
833 __tree& operator=(__tree&& __t);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000834#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835
836 ~__tree();
837
838 iterator begin() {return iterator(__begin_node());}
839 const_iterator begin() const {return const_iterator(__begin_node());}
840 iterator end() {return iterator(__end_node());}
841 const_iterator end() const {return const_iterator(__end_node());}
842
843 size_type max_size() const {return __node_traits::max_size(__node_alloc());}
844
845 void clear();
846
847 void swap(__tree& __t);
848
Howard Hinnant73d21a42010-09-04 23:28:19 +0000849#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
850#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000851 template <class... _Args>
852 pair<iterator, bool>
853 __emplace_unique(_Args&&... __args);
854 template <class... _Args>
855 iterator
856 __emplace_multi(_Args&&... __args);
857
858 template <class... _Args>
859 iterator
860 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
861 template <class... _Args>
862 iterator
863 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000864#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000865
866 template <class _V>
867 pair<iterator, bool> __insert_unique(_V&& __v);
868 template <class _V>
869 iterator __insert_unique(const_iterator __p, _V&& __v);
870 template <class _V>
871 iterator __insert_multi(_V&& __v);
872 template <class _V>
873 iterator __insert_multi(const_iterator __p, _V&& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000874#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875
876 pair<iterator, bool> __insert_unique(const value_type& __v);
877 iterator __insert_unique(const_iterator __p, const value_type& __v);
878 iterator __insert_multi(const value_type& __v);
879 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000880#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000881
882 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
883 iterator __node_insert_unique(const_iterator __p,
884 __node_pointer __nd);
885
886 iterator __node_insert_multi(__node_pointer __nd);
887 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
888
889 iterator erase(const_iterator __p);
890 iterator erase(const_iterator __f, const_iterator __l);
891 template <class _Key>
892 size_type __erase_unique(const _Key& __k);
893 template <class _Key>
894 size_type __erase_multi(const _Key& __k);
895
896 void __insert_node_at(__node_base_pointer __parent,
897 __node_base_pointer& __child,
898 __node_base_pointer __new_node);
899
900 template <class _Key>
901 iterator find(const _Key& __v);
902 template <class _Key>
903 const_iterator find(const _Key& __v) const;
904
905 template <class _Key>
906 size_type __count_unique(const _Key& __k) const;
907 template <class _Key>
908 size_type __count_multi(const _Key& __k) const;
909
910 template <class _Key>
911 iterator lower_bound(const _Key& __v)
912 {return __lower_bound(__v, __root(), __end_node());}
913 template <class _Key>
914 iterator __lower_bound(const _Key& __v,
915 __node_pointer __root,
916 __node_pointer __result);
917 template <class _Key>
918 const_iterator lower_bound(const _Key& __v) const
919 {return __lower_bound(__v, __root(), __end_node());}
920 template <class _Key>
921 const_iterator __lower_bound(const _Key& __v,
922 __node_const_pointer __root,
923 __node_const_pointer __result) const;
924 template <class _Key>
925 iterator upper_bound(const _Key& __v)
926 {return __upper_bound(__v, __root(), __end_node());}
927 template <class _Key>
928 iterator __upper_bound(const _Key& __v,
929 __node_pointer __root,
930 __node_pointer __result);
931 template <class _Key>
932 const_iterator upper_bound(const _Key& __v) const
933 {return __upper_bound(__v, __root(), __end_node());}
934 template <class _Key>
935 const_iterator __upper_bound(const _Key& __v,
936 __node_const_pointer __root,
937 __node_const_pointer __result) const;
938 template <class _Key>
939 pair<iterator, iterator>
940 __equal_range_unique(const _Key& __k);
941 template <class _Key>
942 pair<const_iterator, const_iterator>
943 __equal_range_unique(const _Key& __k) const;
944
945 template <class _Key>
946 pair<iterator, iterator>
947 __equal_range_multi(const _Key& __k);
948 template <class _Key>
949 pair<const_iterator, const_iterator>
950 __equal_range_multi(const _Key& __k) const;
951
952 typedef __tree_node_destructor<__node_allocator> _D;
953 typedef unique_ptr<__node, _D> __node_holder;
954
955 __node_holder remove(const_iterator __p);
956private:
957 typename __node::base::pointer&
958 __find_leaf_low(typename __node::base::pointer& __parent, const value_type& __v);
959 typename __node::base::pointer&
960 __find_leaf_high(typename __node::base::pointer& __parent, const value_type& __v);
961 typename __node::base::pointer&
962 __find_leaf(const_iterator __hint,
963 typename __node::base::pointer& __parent, const value_type& __v);
964 template <class _Key>
965 typename __node::base::pointer&
966 __find_equal(typename __node::base::pointer& __parent, const _Key& __v);
967 template <class _Key>
968 typename __node::base::pointer&
969 __find_equal(const_iterator __hint, typename __node::base::pointer& __parent,
970 const _Key& __v);
971
Howard Hinnant73d21a42010-09-04 23:28:19 +0000972#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 template <class ..._Args>
974 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000975#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000976 __node_holder __construct_node(const value_type& __v);
977#endif
978
979 void destroy(__node_pointer __nd);
980
981 void __copy_assign_alloc(const __tree& __t)
982 {__copy_assign_alloc(__t, integral_constant<bool,
983 __node_traits::propagate_on_container_copy_assignment::value>());}
984
985 void __copy_assign_alloc(const __tree& __t, true_type)
986 {__node_alloc() = __t.__node_alloc();}
987 void __copy_assign_alloc(const __tree& __t, false_type) {}
988
989 void __move_assign(__tree& __t, false_type);
990 void __move_assign(__tree& __t, true_type);
991
992 void __move_assign_alloc(__tree& __t)
993 {__move_assign_alloc(__t, integral_constant<bool,
994 __node_traits::propagate_on_container_move_assignment::value>());}
995
996 void __move_assign_alloc(__tree& __t, true_type)
997 {__node_alloc() = _STD::move(__t.__node_alloc());}
998 void __move_assign_alloc(__tree& __t, false_type) {}
999
1000 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
1001 {__swap_alloc(__x, __y, integral_constant<bool,
1002 __node_traits::propagate_on_container_swap::value>());}
1003 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
1004 {
1005 using _STD::swap;
1006 swap(__x, __y);
1007 }
1008 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
1009 {}
1010
1011 __node_pointer __detach();
1012 static __node_pointer __detach(__node_pointer);
1013};
1014
1015template <class _Tp, class _Compare, class _Allocator>
1016__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
1017 : __pair3_(0, __comp)
1018{
1019 __begin_node() = __end_node();
1020}
1021
1022template <class _Tp, class _Compare, class _Allocator>
1023__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1024 : __pair1_(__node_allocator(__a)),
1025 __begin_node_(__node_pointer()),
1026 __pair3_(0)
1027{
1028 __begin_node() = __end_node();
1029}
1030
1031template <class _Tp, class _Compare, class _Allocator>
1032__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1033 const allocator_type& __a)
1034 : __pair1_(__node_allocator(__a)),
1035 __begin_node_(__node_pointer()),
1036 __pair3_(0, __comp)
1037{
1038 __begin_node() = __end_node();
1039}
1040
1041// Precondition: size() != 0
1042template <class _Tp, class _Compare, class _Allocator>
1043typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1044__tree<_Tp, _Compare, _Allocator>::__detach()
1045{
1046 __node_pointer __cache = __begin_node();
1047 __begin_node() = __end_node();
1048 __end_node()->__left_->__parent_ = nullptr;
1049 __end_node()->__left_ = nullptr;
1050 size() = 0;
1051 // __cache->__left_ == nullptr
1052 if (__cache->__right_ != nullptr)
1053 __cache = static_cast<__node_pointer>(__cache->__right_);
1054 // __cache->__left_ == nullptr
1055 // __cache->__right_ == nullptr
1056 return __cache;
1057}
1058
1059// Precondition: __cache != nullptr
1060// __cache->left_ == nullptr
1061// __cache->right_ == nullptr
1062// This is no longer a red-black tree
1063template <class _Tp, class _Compare, class _Allocator>
1064typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1065__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1066{
1067 if (__cache->__parent_ == nullptr)
1068 return nullptr;
1069 if (__tree_is_left_child(__cache))
1070 {
1071 __cache->__parent_->__left_ = nullptr;
1072 __cache = static_cast<__node_pointer>(__cache->__parent_);
1073 if (__cache->__right_ == nullptr)
1074 return __cache;
1075 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1076 }
1077 // __cache is right child
1078 __cache->__parent_->__right_ = nullptr;
1079 __cache = static_cast<__node_pointer>(__cache->__parent_);
1080 if (__cache->__left_ == nullptr)
1081 return __cache;
1082 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1083}
1084
1085template <class _Tp, class _Compare, class _Allocator>
1086__tree<_Tp, _Compare, _Allocator>&
1087__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1088{
1089 if (this != &__t)
1090 {
1091 value_comp() = __t.value_comp();
1092 __copy_assign_alloc(__t);
1093 __assign_multi(__t.begin(), __t.end());
1094 }
1095 return *this;
1096}
1097
1098template <class _Tp, class _Compare, class _Allocator>
1099template <class _InputIterator>
1100void
1101__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1102{
1103 if (size() != 0)
1104 {
1105 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001106#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001107 try
1108 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001109#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001110 for (; __cache != nullptr && __first != __last; ++__first)
1111 {
1112 __cache->__value_ = *__first;
1113 __node_pointer __next = __detach(__cache);
1114 __node_insert_unique(__cache);
1115 __cache = __next;
1116 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001117#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001118 }
1119 catch (...)
1120 {
1121 while (__cache->__parent_ != nullptr)
1122 __cache = static_cast<__node_pointer>(__cache->__parent_);
1123 destroy(__cache);
1124 throw;
1125 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001126#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001127 if (__cache != nullptr)
1128 {
1129 while (__cache->__parent_ != nullptr)
1130 __cache = static_cast<__node_pointer>(__cache->__parent_);
1131 destroy(__cache);
1132 }
1133 }
1134 for (; __first != __last; ++__first)
1135 __insert_unique(*__first);
1136}
1137
1138template <class _Tp, class _Compare, class _Allocator>
1139template <class _InputIterator>
1140void
1141__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1142{
1143 if (size() != 0)
1144 {
1145 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001146#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001147 try
1148 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001149#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001150 for (; __cache != nullptr && __first != __last; ++__first)
1151 {
1152 __cache->__value_ = *__first;
1153 __node_pointer __next = __detach(__cache);
1154 __node_insert_multi(__cache);
1155 __cache = __next;
1156 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001157#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001158 }
1159 catch (...)
1160 {
1161 while (__cache->__parent_ != nullptr)
1162 __cache = static_cast<__node_pointer>(__cache->__parent_);
1163 destroy(__cache);
1164 throw;
1165 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001166#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167 if (__cache != nullptr)
1168 {
1169 while (__cache->__parent_ != nullptr)
1170 __cache = static_cast<__node_pointer>(__cache->__parent_);
1171 destroy(__cache);
1172 }
1173 }
1174 for (; __first != __last; ++__first)
1175 __insert_multi(*__first);
1176}
1177
1178template <class _Tp, class _Compare, class _Allocator>
1179__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1180 : __begin_node_(__node_pointer()),
1181 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1182 __pair3_(0, __t.value_comp())
1183{
1184 __begin_node() = __end_node();
1185}
1186
Howard Hinnant73d21a42010-09-04 23:28:19 +00001187#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001188
1189template <class _Tp, class _Compare, class _Allocator>
1190__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
1191 : __begin_node_(_STD::move(__t.__begin_node_)),
1192 __pair1_(_STD::move(__t.__pair1_)),
1193 __pair3_(_STD::move(__t.__pair3_))
1194{
1195 if (size() == 0)
1196 __begin_node() = __end_node();
1197 else
1198 {
1199 __end_node()->__left_->__parent_ = __end_node();
1200 __t.__begin_node() = __t.__end_node();
1201 __t.__end_node()->__left_ = nullptr;
1202 __t.size() = 0;
1203 }
1204}
1205
1206template <class _Tp, class _Compare, class _Allocator>
1207__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1208 : __pair1_(__node_allocator(__a)),
1209 __pair3_(0, _STD::move(__t.value_comp()))
1210{
1211 if (__a == __t.__alloc())
1212 {
1213 if (__t.size() == 0)
1214 __begin_node() = __end_node();
1215 else
1216 {
1217 __begin_node() = __t.__begin_node();
1218 __end_node()->__left_ = __t.__end_node()->__left_;
1219 __end_node()->__left_->__parent_ = __end_node();
1220 size() = __t.size();
1221 __t.__begin_node() = __t.__end_node();
1222 __t.__end_node()->__left_ = nullptr;
1223 __t.size() = 0;
1224 }
1225 }
1226 else
1227 {
1228 __begin_node() = __end_node();
1229 }
1230}
1231
1232template <class _Tp, class _Compare, class _Allocator>
1233void
1234__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1235{
1236 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1237 __begin_node_ = __t.__begin_node_;
1238 __pair1_.first() = __t.__pair1_.first();
1239 __move_assign_alloc(__t);
1240 __pair3_ = _STD::move(__t.__pair3_);
1241 if (size() == 0)
1242 __begin_node() = __end_node();
1243 else
1244 {
1245 __end_node()->__left_->__parent_ = __end_node();
1246 __t.__begin_node() = __t.__end_node();
1247 __t.__end_node()->__left_ = nullptr;
1248 __t.size() = 0;
1249 }
1250}
1251
1252template <class _Tp, class _Compare, class _Allocator>
1253void
1254__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1255{
1256 if (__node_alloc() == __t.__node_alloc())
1257 __move_assign(__t, true_type());
1258 else
1259 {
1260 value_comp() = _STD::move(__t.value_comp());
1261 const_iterator __e = end();
1262 if (size() != 0)
1263 {
1264 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001265#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001266 try
1267 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001268#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 while (__cache != nullptr && __t.size() != 0)
1270 {
1271 __cache->__value_ = _STD::move(__t.remove(__t.begin())->__value_);
1272 __node_pointer __next = __detach(__cache);
1273 __node_insert_multi(__cache);
1274 __cache = __next;
1275 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001276#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001277 }
1278 catch (...)
1279 {
1280 while (__cache->__parent_ != nullptr)
1281 __cache = static_cast<__node_pointer>(__cache->__parent_);
1282 destroy(__cache);
1283 throw;
1284 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001285#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001286 if (__cache != nullptr)
1287 {
1288 while (__cache->__parent_ != nullptr)
1289 __cache = static_cast<__node_pointer>(__cache->__parent_);
1290 destroy(__cache);
1291 }
1292 }
1293 while (__t.size() != 0)
1294 __insert_multi(__e, _STD::move(__t.remove(__t.begin())->__value_));
1295 }
1296}
1297
1298template <class _Tp, class _Compare, class _Allocator>
1299__tree<_Tp, _Compare, _Allocator>&
1300__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
1301{
1302 __move_assign(__t, integral_constant<bool,
1303 __node_traits::propagate_on_container_move_assignment::value>());
1304 return *this;
1305}
1306
Howard Hinnant73d21a42010-09-04 23:28:19 +00001307#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308
1309template <class _Tp, class _Compare, class _Allocator>
1310__tree<_Tp, _Compare, _Allocator>::~__tree()
1311{
1312 destroy(__root());
1313}
1314
1315template <class _Tp, class _Compare, class _Allocator>
1316void
1317__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd)
1318{
1319 if (__nd != nullptr)
1320 {
1321 destroy(static_cast<__node_pointer>(__nd->__left_));
1322 destroy(static_cast<__node_pointer>(__nd->__right_));
1323 __node_allocator& __na = __node_alloc();
1324 __node_traits::destroy(__na, addressof(__nd->__value_));
1325 __node_traits::deallocate(__na, __nd, 1);
1326 }
1327}
1328
1329template <class _Tp, class _Compare, class _Allocator>
1330void
1331__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1332{
1333 using _STD::swap;
1334 swap(__begin_node_, __t.__begin_node_);
1335 swap(__pair1_.first(), __t.__pair1_.first());
1336 __swap_alloc(__node_alloc(), __t.__node_alloc());
1337 __pair3_.swap(__t.__pair3_);
1338 if (size() == 0)
1339 __begin_node() = __end_node();
1340 else
1341 __end_node()->__left_->__parent_ = __end_node();
1342 if (__t.size() == 0)
1343 __t.__begin_node() = __t.__end_node();
1344 else
1345 __t.__end_node()->__left_->__parent_ = __t.__end_node();
1346}
1347
1348template <class _Tp, class _Compare, class _Allocator>
1349void
1350__tree<_Tp, _Compare, _Allocator>::clear()
1351{
1352 destroy(__root());
1353 size() = 0;
1354 __begin_node() = __end_node();
1355 __end_node()->__left_ = nullptr;
1356}
1357
1358// Find lower_bound place to insert
1359// Set __parent to parent of null leaf
1360// Return reference to null leaf
1361template <class _Tp, class _Compare, class _Allocator>
1362typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1363__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node::base::pointer& __parent,
1364 const value_type& __v)
1365{
1366 __node_pointer __nd = __root();
1367 if (__nd != nullptr)
1368 {
1369 while (true)
1370 {
1371 if (value_comp()(__nd->__value_, __v))
1372 {
1373 if (__nd->__right_ != nullptr)
1374 __nd = static_cast<__node_pointer>(__nd->__right_);
1375 else
1376 {
1377 __parent = __nd;
1378 return __parent->__right_;
1379 }
1380 }
1381 else
1382 {
1383 if (__nd->__left_ != nullptr)
1384 __nd = static_cast<__node_pointer>(__nd->__left_);
1385 else
1386 {
1387 __parent = __nd;
1388 return __parent->__left_;
1389 }
1390 }
1391 }
1392 }
1393 __parent = __end_node();
1394 return __parent->__left_;
1395}
1396
1397// Find upper_bound place to insert
1398// Set __parent to parent of null leaf
1399// Return reference to null leaf
1400template <class _Tp, class _Compare, class _Allocator>
1401typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1402__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node::base::pointer& __parent,
1403 const value_type& __v)
1404{
1405 __node_pointer __nd = __root();
1406 if (__nd != nullptr)
1407 {
1408 while (true)
1409 {
1410 if (value_comp()(__v, __nd->__value_))
1411 {
1412 if (__nd->__left_ != nullptr)
1413 __nd = static_cast<__node_pointer>(__nd->__left_);
1414 else
1415 {
1416 __parent = __nd;
1417 return __parent->__left_;
1418 }
1419 }
1420 else
1421 {
1422 if (__nd->__right_ != nullptr)
1423 __nd = static_cast<__node_pointer>(__nd->__right_);
1424 else
1425 {
1426 __parent = __nd;
1427 return __parent->__right_;
1428 }
1429 }
1430 }
1431 }
1432 __parent = __end_node();
1433 return __parent->__left_;
1434}
1435
1436// Find leaf place to insert closest to __hint
1437// First check prior to __hint.
1438// Next check after __hint.
1439// Next do O(log N) search.
1440// Set __parent to parent of null leaf
1441// Return reference to null leaf
1442template <class _Tp, class _Compare, class _Allocator>
1443typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1444__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
1445 typename __node::base::pointer& __parent,
1446 const value_type& __v)
1447{
1448 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1449 {
1450 // __v <= *__hint
1451 const_iterator __prior = __hint;
1452 if (__prior == begin() || !value_comp()(__v, *--__prior))
1453 {
1454 // *prev(__hint) <= __v <= *__hint
1455 if (__hint.__ptr_->__left_ == nullptr)
1456 {
1457 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1458 return __parent->__left_;
1459 }
1460 else
1461 {
1462 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1463 return __parent->__right_;
1464 }
1465 }
1466 // __v < *prev(__hint)
1467 return __find_leaf_high(__parent, __v);
1468 }
1469 // else __v > *__hint
1470 return __find_leaf_low(__parent, __v);
1471}
1472
1473// Find place to insert if __v doesn't exist
1474// Set __parent to parent of null leaf
1475// Return reference to null leaf
1476// If __v exists, set parent to node of __v and return reference to node of __v
1477template <class _Tp, class _Compare, class _Allocator>
1478template <class _Key>
1479typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1480__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node::base::pointer& __parent,
1481 const _Key& __v)
1482{
1483 __node_pointer __nd = __root();
1484 if (__nd != nullptr)
1485 {
1486 while (true)
1487 {
1488 if (value_comp()(__v, __nd->__value_))
1489 {
1490 if (__nd->__left_ != nullptr)
1491 __nd = static_cast<__node_pointer>(__nd->__left_);
1492 else
1493 {
1494 __parent = __nd;
1495 return __parent->__left_;
1496 }
1497 }
1498 else if (value_comp()(__nd->__value_, __v))
1499 {
1500 if (__nd->__right_ != nullptr)
1501 __nd = static_cast<__node_pointer>(__nd->__right_);
1502 else
1503 {
1504 __parent = __nd;
1505 return __parent->__right_;
1506 }
1507 }
1508 else
1509 {
1510 __parent = __nd;
1511 return __parent;
1512 }
1513 }
1514 }
1515 __parent = __end_node();
1516 return __parent->__left_;
1517}
1518
1519// Find place to insert if __v doesn't exist
1520// First check prior to __hint.
1521// Next check after __hint.
1522// Next do O(log N) search.
1523// Set __parent to parent of null leaf
1524// Return reference to null leaf
1525// If __v exists, set parent to node of __v and return reference to node of __v
1526template <class _Tp, class _Compare, class _Allocator>
1527template <class _Key>
1528typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1529__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
1530 typename __node::base::pointer& __parent,
1531 const _Key& __v)
1532{
1533 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1534 {
1535 // __v < *__hint
1536 const_iterator __prior = __hint;
1537 if (__prior == begin() || value_comp()(*--__prior, __v))
1538 {
1539 // *prev(__hint) < __v < *__hint
1540 if (__hint.__ptr_->__left_ == nullptr)
1541 {
1542 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1543 return __parent->__left_;
1544 }
1545 else
1546 {
1547 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1548 return __parent->__right_;
1549 }
1550 }
1551 // __v <= *prev(__hint)
1552 return __find_equal(__parent, __v);
1553 }
1554 else if (value_comp()(*__hint, __v)) // check after
1555 {
1556 // *__hint < __v
1557 const_iterator __next = _STD::next(__hint);
1558 if (__next == end() || value_comp()(__v, *__next))
1559 {
1560 // *__hint < __v < *next(__hint)
1561 if (__hint.__ptr_->__right_ == nullptr)
1562 {
1563 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1564 return __parent->__right_;
1565 }
1566 else
1567 {
1568 __parent = const_cast<__node_pointer&>(__next.__ptr_);
1569 return __parent->__left_;
1570 }
1571 }
1572 // *next(__hint) <= __v
1573 return __find_equal(__parent, __v);
1574 }
1575 // else __v == *__hint
1576 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1577 return __parent;
1578}
1579
1580template <class _Tp, class _Compare, class _Allocator>
1581void
1582__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1583 __node_base_pointer& __child,
1584 __node_base_pointer __new_node)
1585{
1586 __new_node->__left_ = nullptr;
1587 __new_node->__right_ = nullptr;
1588 __new_node->__parent_ = __parent;
1589 __child = __new_node;
1590 if (__begin_node()->__left_ != nullptr)
1591 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1592 __tree_balance_after_insert(__end_node()->__left_, __child);
1593 ++size();
1594}
1595
Howard Hinnant73d21a42010-09-04 23:28:19 +00001596#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1597#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001598
1599template <class _Tp, class _Compare, class _Allocator>
1600template <class ..._Args>
1601typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1602__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1603{
1604 __node_allocator& __na = __node_alloc();
1605 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1606 __node_traits::construct(__na, addressof(__h->__value_), _STD::forward<_Args>(__args)...);
1607 __h.get_deleter().__value_constructed = true;
1608 return __h;
1609}
1610
1611template <class _Tp, class _Compare, class _Allocator>
1612template <class... _Args>
1613pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1614__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1615{
1616 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1617 __node_base_pointer __parent;
1618 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1619 __node_pointer __r = static_cast<__node_pointer>(__child);
1620 bool __inserted = false;
1621 if (__child == nullptr)
1622 {
1623 __insert_node_at(__parent, __child, __h.get());
1624 __r = __h.release();
1625 __inserted = true;
1626 }
1627 return pair<iterator, bool>(iterator(__r), __inserted);
1628}
1629
1630template <class _Tp, class _Compare, class _Allocator>
1631template <class... _Args>
1632typename __tree<_Tp, _Compare, _Allocator>::iterator
1633__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1634{
1635 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1636 __node_base_pointer __parent;
1637 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1638 __node_pointer __r = static_cast<__node_pointer>(__child);
1639 if (__child == nullptr)
1640 {
1641 __insert_node_at(__parent, __child, __h.get());
1642 __r = __h.release();
1643 }
1644 return iterator(__r);
1645}
1646
1647template <class _Tp, class _Compare, class _Allocator>
1648template <class... _Args>
1649typename __tree<_Tp, _Compare, _Allocator>::iterator
1650__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1651{
1652 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1653 __node_base_pointer __parent;
1654 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1655 __insert_node_at(__parent, __child, __h.get());
1656 return iterator(static_cast<__node_pointer>(__h.release()));
1657}
1658
1659template <class _Tp, class _Compare, class _Allocator>
1660template <class... _Args>
1661typename __tree<_Tp, _Compare, _Allocator>::iterator
1662__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1663 _Args&&... __args)
1664{
1665 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1666 __node_base_pointer __parent;
1667 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1668 __insert_node_at(__parent, __child, __h.get());
1669 return iterator(static_cast<__node_pointer>(__h.release()));
1670}
1671
Howard Hinnant73d21a42010-09-04 23:28:19 +00001672#endif // _LIBCPP_HAS_NO_VARIADICS
1673
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001674template <class _Tp, class _Compare, class _Allocator>
1675template <class _V>
1676pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1677__tree<_Tp, _Compare, _Allocator>::__insert_unique(_V&& __v)
1678{
1679 __node_base_pointer __parent;
1680 __node_base_pointer& __child = __find_equal(__parent, __v);
1681 __node_pointer __r = static_cast<__node_pointer>(__child);
1682 bool __inserted = false;
1683 if (__child == nullptr)
1684 {
1685 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1686 __insert_node_at(__parent, __child, __h.get());
1687 __r = __h.release();
1688 __inserted = true;
1689 }
1690 return pair<iterator, bool>(iterator(__r), __inserted);
1691}
1692
1693template <class _Tp, class _Compare, class _Allocator>
1694template <class _V>
1695typename __tree<_Tp, _Compare, _Allocator>::iterator
1696__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _V&& __v)
1697{
1698 __node_base_pointer __parent;
1699 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1700 __node_pointer __r = static_cast<__node_pointer>(__child);
1701 if (__child == nullptr)
1702 {
1703 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1704 __insert_node_at(__parent, __child, __h.get());
1705 __r = __h.release();
1706 }
1707 return iterator(__r);
1708}
1709
1710template <class _Tp, class _Compare, class _Allocator>
1711template <class _V>
1712typename __tree<_Tp, _Compare, _Allocator>::iterator
1713__tree<_Tp, _Compare, _Allocator>::__insert_multi(_V&& __v)
1714{
1715 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1716 __node_base_pointer __parent;
1717 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1718 __insert_node_at(__parent, __child, __h.get());
1719 return iterator(__h.release());
1720}
1721
1722template <class _Tp, class _Compare, class _Allocator>
1723template <class _V>
1724typename __tree<_Tp, _Compare, _Allocator>::iterator
1725__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _V&& __v)
1726{
1727 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1728 __node_base_pointer __parent;
1729 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1730 __insert_node_at(__parent, __child, __h.get());
1731 return iterator(__h.release());
1732}
1733
Howard Hinnant73d21a42010-09-04 23:28:19 +00001734#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001735
1736template <class _Tp, class _Compare, class _Allocator>
1737typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1738__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1739{
1740 __node_allocator& __na = __node_alloc();
1741 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1742 __node_traits::construct(__na, addressof(__h->__value_), __v);
1743 __h.get_deleter().__value_constructed = true;
1744 return _STD::move(__h);
1745}
1746
1747template <class _Tp, class _Compare, class _Allocator>
1748pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1749__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1750{
1751 __node_base_pointer __parent;
1752 __node_base_pointer& __child = __find_equal(__parent, __v);
1753 __node_pointer __r = static_cast<__node_pointer>(__child);
1754 bool __inserted = false;
1755 if (__child == nullptr)
1756 {
1757 __node_holder __h = __construct_node(__v);
1758 __insert_node_at(__parent, __child, __h.get());
1759 __r = __h.release();
1760 __inserted = true;
1761 }
1762 return pair<iterator, bool>(iterator(__r), __inserted);
1763}
1764
1765template <class _Tp, class _Compare, class _Allocator>
1766typename __tree<_Tp, _Compare, _Allocator>::iterator
1767__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1768{
1769 __node_base_pointer __parent;
1770 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1771 __node_pointer __r = static_cast<__node_pointer>(__child);
1772 if (__child == nullptr)
1773 {
1774 __node_holder __h = __construct_node(__v);
1775 __insert_node_at(__parent, __child, __h.get());
1776 __r = __h.release();
1777 }
1778 return iterator(__r);
1779}
1780
1781template <class _Tp, class _Compare, class _Allocator>
1782typename __tree<_Tp, _Compare, _Allocator>::iterator
1783__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1784{
1785 __node_base_pointer __parent;
1786 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1787 __node_holder __h = __construct_node(__v);
1788 __insert_node_at(__parent, __child, __h.get());
1789 return iterator(__h.release());
1790}
1791
1792template <class _Tp, class _Compare, class _Allocator>
1793typename __tree<_Tp, _Compare, _Allocator>::iterator
1794__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1795{
1796 __node_base_pointer __parent;
1797 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1798 __node_holder __h = __construct_node(__v);
1799 __insert_node_at(__parent, __child, __h.get());
1800 return iterator(__h.release());
1801}
1802
Howard Hinnant73d21a42010-09-04 23:28:19 +00001803#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804
1805template <class _Tp, class _Compare, class _Allocator>
1806pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1807__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1808{
1809 __node_base_pointer __parent;
1810 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1811 __node_pointer __r = static_cast<__node_pointer>(__child);
1812 bool __inserted = false;
1813 if (__child == nullptr)
1814 {
1815 __insert_node_at(__parent, __child, __nd);
1816 __r = __nd;
1817 __inserted = true;
1818 }
1819 return pair<iterator, bool>(iterator(__r), __inserted);
1820}
1821
1822template <class _Tp, class _Compare, class _Allocator>
1823typename __tree<_Tp, _Compare, _Allocator>::iterator
1824__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1825 __node_pointer __nd)
1826{
1827 __node_base_pointer __parent;
1828 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1829 __node_pointer __r = static_cast<__node_pointer>(__child);
1830 if (__child == nullptr)
1831 {
1832 __insert_node_at(__parent, __child, __nd);
1833 __r = __nd;
1834 }
1835 return iterator(__r);
1836}
1837
1838template <class _Tp, class _Compare, class _Allocator>
1839typename __tree<_Tp, _Compare, _Allocator>::iterator
1840__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1841{
1842 __node_base_pointer __parent;
1843 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
1844 __insert_node_at(__parent, __child, __nd);
1845 return iterator(__nd);
1846}
1847
1848template <class _Tp, class _Compare, class _Allocator>
1849typename __tree<_Tp, _Compare, _Allocator>::iterator
1850__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1851 __node_pointer __nd)
1852{
1853 __node_base_pointer __parent;
1854 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
1855 __insert_node_at(__parent, __child, __nd);
1856 return iterator(__nd);
1857}
1858
1859template <class _Tp, class _Compare, class _Allocator>
1860typename __tree<_Tp, _Compare, _Allocator>::iterator
1861__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1862{
1863 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
1864 iterator __r(__np);
1865 ++__r;
1866 if (__begin_node() == __np)
1867 __begin_node() = __r.__ptr_;
1868 --size();
1869 __node_allocator& __na = __node_alloc();
1870 __node_traits::destroy(__na, const_cast<value_type*>(addressof(*__p)));
1871 __tree_remove(__end_node()->__left_,
1872 static_cast<__node_base_pointer>(__np));
1873 __node_traits::deallocate(__na, __np, 1);
1874 return __r;
1875}
1876
1877template <class _Tp, class _Compare, class _Allocator>
1878typename __tree<_Tp, _Compare, _Allocator>::iterator
1879__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1880{
1881 while (__f != __l)
1882 __f = erase(__f);
1883 return iterator(const_cast<__node_pointer>(__l.__ptr_));
1884}
1885
1886template <class _Tp, class _Compare, class _Allocator>
1887template <class _Key>
1888typename __tree<_Tp, _Compare, _Allocator>::size_type
1889__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1890{
1891 iterator __i = find(__k);
1892 if (__i == end())
1893 return 0;
1894 erase(__i);
1895 return 1;
1896}
1897
1898template <class _Tp, class _Compare, class _Allocator>
1899template <class _Key>
1900typename __tree<_Tp, _Compare, _Allocator>::size_type
1901__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
1902{
1903 pair<iterator, iterator> __p = __equal_range_multi(__k);
1904 size_type __r = 0;
1905 for (; __p.first != __p.second; ++__r)
1906 __p.first = erase(__p.first);
1907 return __r;
1908}
1909
1910template <class _Tp, class _Compare, class _Allocator>
1911template <class _Key>
1912typename __tree<_Tp, _Compare, _Allocator>::iterator
1913__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
1914{
1915 iterator __p = __lower_bound(__v, __root(), __end_node());
1916 if (__p != end() && !value_comp()(__v, *__p))
1917 return __p;
1918 return end();
1919}
1920
1921template <class _Tp, class _Compare, class _Allocator>
1922template <class _Key>
1923typename __tree<_Tp, _Compare, _Allocator>::const_iterator
1924__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
1925{
1926 const_iterator __p = __lower_bound(__v, __root(), __end_node());
1927 if (__p != end() && !value_comp()(__v, *__p))
1928 return __p;
1929 return end();
1930}
1931
1932template <class _Tp, class _Compare, class _Allocator>
1933template <class _Key>
1934typename __tree<_Tp, _Compare, _Allocator>::size_type
1935__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
1936{
1937 __node_const_pointer __result = __end_node();
1938 __node_const_pointer __rt = __root();
1939 while (__rt != nullptr)
1940 {
1941 if (value_comp()(__k, __rt->__value_))
1942 {
1943 __result = __rt;
1944 __rt = static_cast<__node_const_pointer>(__rt->__left_);
1945 }
1946 else if (value_comp()(__rt->__value_, __k))
1947 __rt = static_cast<__node_const_pointer>(__rt->__right_);
1948 else
1949 return 1;
1950 }
1951 return 0;
1952}
1953
1954template <class _Tp, class _Compare, class _Allocator>
1955template <class _Key>
1956typename __tree<_Tp, _Compare, _Allocator>::size_type
1957__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
1958{
1959 typedef pair<const_iterator, const_iterator> _P;
1960 __node_const_pointer __result = __end_node();
1961 __node_const_pointer __rt = __root();
1962 while (__rt != nullptr)
1963 {
1964 if (value_comp()(__k, __rt->__value_))
1965 {
1966 __result = __rt;
1967 __rt = static_cast<__node_const_pointer>(__rt->__left_);
1968 }
1969 else if (value_comp()(__rt->__value_, __k))
1970 __rt = static_cast<__node_const_pointer>(__rt->__right_);
1971 else
1972 return _STD::distance(
1973 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
1974 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
1975 );
1976 }
1977 return 0;
1978}
1979
1980template <class _Tp, class _Compare, class _Allocator>
1981template <class _Key>
1982typename __tree<_Tp, _Compare, _Allocator>::iterator
1983__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
1984 __node_pointer __root,
1985 __node_pointer __result)
1986{
1987 while (__root != nullptr)
1988 {
1989 if (!value_comp()(__root->__value_, __v))
1990 {
1991 __result = __root;
1992 __root = static_cast<__node_pointer>(__root->__left_);
1993 }
1994 else
1995 __root = static_cast<__node_pointer>(__root->__right_);
1996 }
1997 return iterator(__result);
1998}
1999
2000template <class _Tp, class _Compare, class _Allocator>
2001template <class _Key>
2002typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2003__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2004 __node_const_pointer __root,
2005 __node_const_pointer __result) const
2006{
2007 while (__root != nullptr)
2008 {
2009 if (!value_comp()(__root->__value_, __v))
2010 {
2011 __result = __root;
2012 __root = static_cast<__node_const_pointer>(__root->__left_);
2013 }
2014 else
2015 __root = static_cast<__node_const_pointer>(__root->__right_);
2016 }
2017 return const_iterator(__result);
2018}
2019
2020template <class _Tp, class _Compare, class _Allocator>
2021template <class _Key>
2022typename __tree<_Tp, _Compare, _Allocator>::iterator
2023__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2024 __node_pointer __root,
2025 __node_pointer __result)
2026{
2027 while (__root != nullptr)
2028 {
2029 if (value_comp()(__v, __root->__value_))
2030 {
2031 __result = __root;
2032 __root = static_cast<__node_pointer>(__root->__left_);
2033 }
2034 else
2035 __root = static_cast<__node_pointer>(__root->__right_);
2036 }
2037 return iterator(__result);
2038}
2039
2040template <class _Tp, class _Compare, class _Allocator>
2041template <class _Key>
2042typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2043__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2044 __node_const_pointer __root,
2045 __node_const_pointer __result) const
2046{
2047 while (__root != nullptr)
2048 {
2049 if (value_comp()(__v, __root->__value_))
2050 {
2051 __result = __root;
2052 __root = static_cast<__node_const_pointer>(__root->__left_);
2053 }
2054 else
2055 __root = static_cast<__node_const_pointer>(__root->__right_);
2056 }
2057 return const_iterator(__result);
2058}
2059
2060template <class _Tp, class _Compare, class _Allocator>
2061template <class _Key>
2062pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2063 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2064__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2065{
2066 typedef pair<iterator, iterator> _P;
2067 __node_pointer __result = __end_node();
2068 __node_pointer __rt = __root();
2069 while (__rt != nullptr)
2070 {
2071 if (value_comp()(__k, __rt->__value_))
2072 {
2073 __result = __rt;
2074 __rt = static_cast<__node_pointer>(__rt->__left_);
2075 }
2076 else if (value_comp()(__rt->__value_, __k))
2077 __rt = static_cast<__node_pointer>(__rt->__right_);
2078 else
2079 return _P(iterator(__rt),
2080 iterator(
2081 __rt->__right_ != nullptr ?
2082 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2083 : __result));
2084 }
2085 return _P(iterator(__result), iterator(__result));
2086}
2087
2088template <class _Tp, class _Compare, class _Allocator>
2089template <class _Key>
2090pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2091 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2092__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2093{
2094 typedef pair<const_iterator, const_iterator> _P;
2095 __node_const_pointer __result = __end_node();
2096 __node_const_pointer __rt = __root();
2097 while (__rt != nullptr)
2098 {
2099 if (value_comp()(__k, __rt->__value_))
2100 {
2101 __result = __rt;
2102 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2103 }
2104 else if (value_comp()(__rt->__value_, __k))
2105 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2106 else
2107 return _P(const_iterator(__rt),
2108 const_iterator(
2109 __rt->__right_ != nullptr ?
2110 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2111 : __result));
2112 }
2113 return _P(const_iterator(__result), const_iterator(__result));
2114}
2115
2116template <class _Tp, class _Compare, class _Allocator>
2117template <class _Key>
2118pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2119 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2120__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2121{
2122 typedef pair<iterator, iterator> _P;
2123 __node_pointer __result = __end_node();
2124 __node_pointer __rt = __root();
2125 while (__rt != nullptr)
2126 {
2127 if (value_comp()(__k, __rt->__value_))
2128 {
2129 __result = __rt;
2130 __rt = static_cast<__node_pointer>(__rt->__left_);
2131 }
2132 else if (value_comp()(__rt->__value_, __k))
2133 __rt = static_cast<__node_pointer>(__rt->__right_);
2134 else
2135 return _P(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2136 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2137 }
2138 return _P(iterator(__result), iterator(__result));
2139}
2140
2141template <class _Tp, class _Compare, class _Allocator>
2142template <class _Key>
2143pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2144 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2145__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2146{
2147 typedef pair<const_iterator, const_iterator> _P;
2148 __node_const_pointer __result = __end_node();
2149 __node_const_pointer __rt = __root();
2150 while (__rt != nullptr)
2151 {
2152 if (value_comp()(__k, __rt->__value_))
2153 {
2154 __result = __rt;
2155 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2156 }
2157 else if (value_comp()(__rt->__value_, __k))
2158 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2159 else
2160 return _P(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2161 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2162 }
2163 return _P(const_iterator(__result), const_iterator(__result));
2164}
2165
2166template <class _Tp, class _Compare, class _Allocator>
2167typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2168__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p)
2169{
2170 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
2171 if (__begin_node() == __np)
2172 {
2173 if (__np->__right_ != nullptr)
2174 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2175 else
2176 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2177 }
2178 --size();
2179 __tree_remove(__end_node()->__left_,
2180 static_cast<__node_base_pointer>(__np));
2181 return __node_holder(__np, _D(__node_alloc()));
2182}
2183
2184_LIBCPP_END_NAMESPACE_STD
2185
2186#endif // _LIBCPP___TREE