blob: 7def15492def613929521b6da57c6e98411e5378 [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
592#ifdef _LIBCPP_MOVE
593 template <class ..._Args>
594 explicit __tree_node(_Args&& ...__args)
595 : __value_(_STD::forward<_Args>(__args)...) {}
Howard Hinnant324bb032010-08-22 00:02:43 +0000596#else // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 explicit __tree_node(const value_type& __v)
598 : __value_(__v) {}
Howard Hinnant324bb032010-08-22 00:02:43 +0000599#endif // _LIBCPP_MOVE
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);
830#ifdef _LIBCPP_MOVE
831 __tree(__tree&& __t);
832 __tree(__tree&& __t, const allocator_type& __a);
833 __tree& operator=(__tree&& __t);
Howard Hinnant324bb032010-08-22 00:02:43 +0000834#endif // _LIBCPP_MOVE
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
849#ifdef _LIBCPP_MOVE
850 template <class... _Args>
851 pair<iterator, bool>
852 __emplace_unique(_Args&&... __args);
853 template <class... _Args>
854 iterator
855 __emplace_multi(_Args&&... __args);
856
857 template <class... _Args>
858 iterator
859 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
860 template <class... _Args>
861 iterator
862 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
863
864 template <class _V>
865 pair<iterator, bool> __insert_unique(_V&& __v);
866 template <class _V>
867 iterator __insert_unique(const_iterator __p, _V&& __v);
868 template <class _V>
869 iterator __insert_multi(_V&& __v);
870 template <class _V>
871 iterator __insert_multi(const_iterator __p, _V&& __v);
Howard Hinnant324bb032010-08-22 00:02:43 +0000872#else // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000873
874 pair<iterator, bool> __insert_unique(const value_type& __v);
875 iterator __insert_unique(const_iterator __p, const value_type& __v);
876 iterator __insert_multi(const value_type& __v);
877 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnant324bb032010-08-22 00:02:43 +0000878#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879
880 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
881 iterator __node_insert_unique(const_iterator __p,
882 __node_pointer __nd);
883
884 iterator __node_insert_multi(__node_pointer __nd);
885 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
886
887 iterator erase(const_iterator __p);
888 iterator erase(const_iterator __f, const_iterator __l);
889 template <class _Key>
890 size_type __erase_unique(const _Key& __k);
891 template <class _Key>
892 size_type __erase_multi(const _Key& __k);
893
894 void __insert_node_at(__node_base_pointer __parent,
895 __node_base_pointer& __child,
896 __node_base_pointer __new_node);
897
898 template <class _Key>
899 iterator find(const _Key& __v);
900 template <class _Key>
901 const_iterator find(const _Key& __v) const;
902
903 template <class _Key>
904 size_type __count_unique(const _Key& __k) const;
905 template <class _Key>
906 size_type __count_multi(const _Key& __k) const;
907
908 template <class _Key>
909 iterator lower_bound(const _Key& __v)
910 {return __lower_bound(__v, __root(), __end_node());}
911 template <class _Key>
912 iterator __lower_bound(const _Key& __v,
913 __node_pointer __root,
914 __node_pointer __result);
915 template <class _Key>
916 const_iterator lower_bound(const _Key& __v) const
917 {return __lower_bound(__v, __root(), __end_node());}
918 template <class _Key>
919 const_iterator __lower_bound(const _Key& __v,
920 __node_const_pointer __root,
921 __node_const_pointer __result) const;
922 template <class _Key>
923 iterator upper_bound(const _Key& __v)
924 {return __upper_bound(__v, __root(), __end_node());}
925 template <class _Key>
926 iterator __upper_bound(const _Key& __v,
927 __node_pointer __root,
928 __node_pointer __result);
929 template <class _Key>
930 const_iterator upper_bound(const _Key& __v) const
931 {return __upper_bound(__v, __root(), __end_node());}
932 template <class _Key>
933 const_iterator __upper_bound(const _Key& __v,
934 __node_const_pointer __root,
935 __node_const_pointer __result) const;
936 template <class _Key>
937 pair<iterator, iterator>
938 __equal_range_unique(const _Key& __k);
939 template <class _Key>
940 pair<const_iterator, const_iterator>
941 __equal_range_unique(const _Key& __k) const;
942
943 template <class _Key>
944 pair<iterator, iterator>
945 __equal_range_multi(const _Key& __k);
946 template <class _Key>
947 pair<const_iterator, const_iterator>
948 __equal_range_multi(const _Key& __k) const;
949
950 typedef __tree_node_destructor<__node_allocator> _D;
951 typedef unique_ptr<__node, _D> __node_holder;
952
953 __node_holder remove(const_iterator __p);
954private:
955 typename __node::base::pointer&
956 __find_leaf_low(typename __node::base::pointer& __parent, const value_type& __v);
957 typename __node::base::pointer&
958 __find_leaf_high(typename __node::base::pointer& __parent, const value_type& __v);
959 typename __node::base::pointer&
960 __find_leaf(const_iterator __hint,
961 typename __node::base::pointer& __parent, const value_type& __v);
962 template <class _Key>
963 typename __node::base::pointer&
964 __find_equal(typename __node::base::pointer& __parent, const _Key& __v);
965 template <class _Key>
966 typename __node::base::pointer&
967 __find_equal(const_iterator __hint, typename __node::base::pointer& __parent,
968 const _Key& __v);
969
970#ifdef _LIBCPP_MOVE
971 template <class ..._Args>
972 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant324bb032010-08-22 00:02:43 +0000973#else // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974 __node_holder __construct_node(const value_type& __v);
975#endif
976
977 void destroy(__node_pointer __nd);
978
979 void __copy_assign_alloc(const __tree& __t)
980 {__copy_assign_alloc(__t, integral_constant<bool,
981 __node_traits::propagate_on_container_copy_assignment::value>());}
982
983 void __copy_assign_alloc(const __tree& __t, true_type)
984 {__node_alloc() = __t.__node_alloc();}
985 void __copy_assign_alloc(const __tree& __t, false_type) {}
986
987 void __move_assign(__tree& __t, false_type);
988 void __move_assign(__tree& __t, true_type);
989
990 void __move_assign_alloc(__tree& __t)
991 {__move_assign_alloc(__t, integral_constant<bool,
992 __node_traits::propagate_on_container_move_assignment::value>());}
993
994 void __move_assign_alloc(__tree& __t, true_type)
995 {__node_alloc() = _STD::move(__t.__node_alloc());}
996 void __move_assign_alloc(__tree& __t, false_type) {}
997
998 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
999 {__swap_alloc(__x, __y, integral_constant<bool,
1000 __node_traits::propagate_on_container_swap::value>());}
1001 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
1002 {
1003 using _STD::swap;
1004 swap(__x, __y);
1005 }
1006 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
1007 {}
1008
1009 __node_pointer __detach();
1010 static __node_pointer __detach(__node_pointer);
1011};
1012
1013template <class _Tp, class _Compare, class _Allocator>
1014__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
1015 : __pair3_(0, __comp)
1016{
1017 __begin_node() = __end_node();
1018}
1019
1020template <class _Tp, class _Compare, class _Allocator>
1021__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1022 : __pair1_(__node_allocator(__a)),
1023 __begin_node_(__node_pointer()),
1024 __pair3_(0)
1025{
1026 __begin_node() = __end_node();
1027}
1028
1029template <class _Tp, class _Compare, class _Allocator>
1030__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1031 const allocator_type& __a)
1032 : __pair1_(__node_allocator(__a)),
1033 __begin_node_(__node_pointer()),
1034 __pair3_(0, __comp)
1035{
1036 __begin_node() = __end_node();
1037}
1038
1039// Precondition: size() != 0
1040template <class _Tp, class _Compare, class _Allocator>
1041typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1042__tree<_Tp, _Compare, _Allocator>::__detach()
1043{
1044 __node_pointer __cache = __begin_node();
1045 __begin_node() = __end_node();
1046 __end_node()->__left_->__parent_ = nullptr;
1047 __end_node()->__left_ = nullptr;
1048 size() = 0;
1049 // __cache->__left_ == nullptr
1050 if (__cache->__right_ != nullptr)
1051 __cache = static_cast<__node_pointer>(__cache->__right_);
1052 // __cache->__left_ == nullptr
1053 // __cache->__right_ == nullptr
1054 return __cache;
1055}
1056
1057// Precondition: __cache != nullptr
1058// __cache->left_ == nullptr
1059// __cache->right_ == nullptr
1060// This is no longer a red-black tree
1061template <class _Tp, class _Compare, class _Allocator>
1062typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1063__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1064{
1065 if (__cache->__parent_ == nullptr)
1066 return nullptr;
1067 if (__tree_is_left_child(__cache))
1068 {
1069 __cache->__parent_->__left_ = nullptr;
1070 __cache = static_cast<__node_pointer>(__cache->__parent_);
1071 if (__cache->__right_ == nullptr)
1072 return __cache;
1073 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1074 }
1075 // __cache is right child
1076 __cache->__parent_->__right_ = nullptr;
1077 __cache = static_cast<__node_pointer>(__cache->__parent_);
1078 if (__cache->__left_ == nullptr)
1079 return __cache;
1080 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1081}
1082
1083template <class _Tp, class _Compare, class _Allocator>
1084__tree<_Tp, _Compare, _Allocator>&
1085__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1086{
1087 if (this != &__t)
1088 {
1089 value_comp() = __t.value_comp();
1090 __copy_assign_alloc(__t);
1091 __assign_multi(__t.begin(), __t.end());
1092 }
1093 return *this;
1094}
1095
1096template <class _Tp, class _Compare, class _Allocator>
1097template <class _InputIterator>
1098void
1099__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1100{
1101 if (size() != 0)
1102 {
1103 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001104#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001105 try
1106 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001107#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001108 for (; __cache != nullptr && __first != __last; ++__first)
1109 {
1110 __cache->__value_ = *__first;
1111 __node_pointer __next = __detach(__cache);
1112 __node_insert_unique(__cache);
1113 __cache = __next;
1114 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001115#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001116 }
1117 catch (...)
1118 {
1119 while (__cache->__parent_ != nullptr)
1120 __cache = static_cast<__node_pointer>(__cache->__parent_);
1121 destroy(__cache);
1122 throw;
1123 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001124#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001125 if (__cache != nullptr)
1126 {
1127 while (__cache->__parent_ != nullptr)
1128 __cache = static_cast<__node_pointer>(__cache->__parent_);
1129 destroy(__cache);
1130 }
1131 }
1132 for (; __first != __last; ++__first)
1133 __insert_unique(*__first);
1134}
1135
1136template <class _Tp, class _Compare, class _Allocator>
1137template <class _InputIterator>
1138void
1139__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1140{
1141 if (size() != 0)
1142 {
1143 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001144#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001145 try
1146 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001147#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 for (; __cache != nullptr && __first != __last; ++__first)
1149 {
1150 __cache->__value_ = *__first;
1151 __node_pointer __next = __detach(__cache);
1152 __node_insert_multi(__cache);
1153 __cache = __next;
1154 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001155#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156 }
1157 catch (...)
1158 {
1159 while (__cache->__parent_ != nullptr)
1160 __cache = static_cast<__node_pointer>(__cache->__parent_);
1161 destroy(__cache);
1162 throw;
1163 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001164#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001165 if (__cache != nullptr)
1166 {
1167 while (__cache->__parent_ != nullptr)
1168 __cache = static_cast<__node_pointer>(__cache->__parent_);
1169 destroy(__cache);
1170 }
1171 }
1172 for (; __first != __last; ++__first)
1173 __insert_multi(*__first);
1174}
1175
1176template <class _Tp, class _Compare, class _Allocator>
1177__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1178 : __begin_node_(__node_pointer()),
1179 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1180 __pair3_(0, __t.value_comp())
1181{
1182 __begin_node() = __end_node();
1183}
1184
1185#ifdef _LIBCPP_MOVE
1186
1187template <class _Tp, class _Compare, class _Allocator>
1188__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
1189 : __begin_node_(_STD::move(__t.__begin_node_)),
1190 __pair1_(_STD::move(__t.__pair1_)),
1191 __pair3_(_STD::move(__t.__pair3_))
1192{
1193 if (size() == 0)
1194 __begin_node() = __end_node();
1195 else
1196 {
1197 __end_node()->__left_->__parent_ = __end_node();
1198 __t.__begin_node() = __t.__end_node();
1199 __t.__end_node()->__left_ = nullptr;
1200 __t.size() = 0;
1201 }
1202}
1203
1204template <class _Tp, class _Compare, class _Allocator>
1205__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1206 : __pair1_(__node_allocator(__a)),
1207 __pair3_(0, _STD::move(__t.value_comp()))
1208{
1209 if (__a == __t.__alloc())
1210 {
1211 if (__t.size() == 0)
1212 __begin_node() = __end_node();
1213 else
1214 {
1215 __begin_node() = __t.__begin_node();
1216 __end_node()->__left_ = __t.__end_node()->__left_;
1217 __end_node()->__left_->__parent_ = __end_node();
1218 size() = __t.size();
1219 __t.__begin_node() = __t.__end_node();
1220 __t.__end_node()->__left_ = nullptr;
1221 __t.size() = 0;
1222 }
1223 }
1224 else
1225 {
1226 __begin_node() = __end_node();
1227 }
1228}
1229
1230template <class _Tp, class _Compare, class _Allocator>
1231void
1232__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
1233{
1234 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1235 __begin_node_ = __t.__begin_node_;
1236 __pair1_.first() = __t.__pair1_.first();
1237 __move_assign_alloc(__t);
1238 __pair3_ = _STD::move(__t.__pair3_);
1239 if (size() == 0)
1240 __begin_node() = __end_node();
1241 else
1242 {
1243 __end_node()->__left_->__parent_ = __end_node();
1244 __t.__begin_node() = __t.__end_node();
1245 __t.__end_node()->__left_ = nullptr;
1246 __t.size() = 0;
1247 }
1248}
1249
1250template <class _Tp, class _Compare, class _Allocator>
1251void
1252__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1253{
1254 if (__node_alloc() == __t.__node_alloc())
1255 __move_assign(__t, true_type());
1256 else
1257 {
1258 value_comp() = _STD::move(__t.value_comp());
1259 const_iterator __e = end();
1260 if (size() != 0)
1261 {
1262 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001263#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001264 try
1265 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001266#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001267 while (__cache != nullptr && __t.size() != 0)
1268 {
1269 __cache->__value_ = _STD::move(__t.remove(__t.begin())->__value_);
1270 __node_pointer __next = __detach(__cache);
1271 __node_insert_multi(__cache);
1272 __cache = __next;
1273 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001274#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001275 }
1276 catch (...)
1277 {
1278 while (__cache->__parent_ != nullptr)
1279 __cache = static_cast<__node_pointer>(__cache->__parent_);
1280 destroy(__cache);
1281 throw;
1282 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001283#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 if (__cache != nullptr)
1285 {
1286 while (__cache->__parent_ != nullptr)
1287 __cache = static_cast<__node_pointer>(__cache->__parent_);
1288 destroy(__cache);
1289 }
1290 }
1291 while (__t.size() != 0)
1292 __insert_multi(__e, _STD::move(__t.remove(__t.begin())->__value_));
1293 }
1294}
1295
1296template <class _Tp, class _Compare, class _Allocator>
1297__tree<_Tp, _Compare, _Allocator>&
1298__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
1299{
1300 __move_assign(__t, integral_constant<bool,
1301 __node_traits::propagate_on_container_move_assignment::value>());
1302 return *this;
1303}
1304
Howard Hinnant324bb032010-08-22 00:02:43 +00001305#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001306
1307template <class _Tp, class _Compare, class _Allocator>
1308__tree<_Tp, _Compare, _Allocator>::~__tree()
1309{
1310 destroy(__root());
1311}
1312
1313template <class _Tp, class _Compare, class _Allocator>
1314void
1315__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd)
1316{
1317 if (__nd != nullptr)
1318 {
1319 destroy(static_cast<__node_pointer>(__nd->__left_));
1320 destroy(static_cast<__node_pointer>(__nd->__right_));
1321 __node_allocator& __na = __node_alloc();
1322 __node_traits::destroy(__na, addressof(__nd->__value_));
1323 __node_traits::deallocate(__na, __nd, 1);
1324 }
1325}
1326
1327template <class _Tp, class _Compare, class _Allocator>
1328void
1329__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
1330{
1331 using _STD::swap;
1332 swap(__begin_node_, __t.__begin_node_);
1333 swap(__pair1_.first(), __t.__pair1_.first());
1334 __swap_alloc(__node_alloc(), __t.__node_alloc());
1335 __pair3_.swap(__t.__pair3_);
1336 if (size() == 0)
1337 __begin_node() = __end_node();
1338 else
1339 __end_node()->__left_->__parent_ = __end_node();
1340 if (__t.size() == 0)
1341 __t.__begin_node() = __t.__end_node();
1342 else
1343 __t.__end_node()->__left_->__parent_ = __t.__end_node();
1344}
1345
1346template <class _Tp, class _Compare, class _Allocator>
1347void
1348__tree<_Tp, _Compare, _Allocator>::clear()
1349{
1350 destroy(__root());
1351 size() = 0;
1352 __begin_node() = __end_node();
1353 __end_node()->__left_ = nullptr;
1354}
1355
1356// Find lower_bound place to insert
1357// Set __parent to parent of null leaf
1358// Return reference to null leaf
1359template <class _Tp, class _Compare, class _Allocator>
1360typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1361__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node::base::pointer& __parent,
1362 const value_type& __v)
1363{
1364 __node_pointer __nd = __root();
1365 if (__nd != nullptr)
1366 {
1367 while (true)
1368 {
1369 if (value_comp()(__nd->__value_, __v))
1370 {
1371 if (__nd->__right_ != nullptr)
1372 __nd = static_cast<__node_pointer>(__nd->__right_);
1373 else
1374 {
1375 __parent = __nd;
1376 return __parent->__right_;
1377 }
1378 }
1379 else
1380 {
1381 if (__nd->__left_ != nullptr)
1382 __nd = static_cast<__node_pointer>(__nd->__left_);
1383 else
1384 {
1385 __parent = __nd;
1386 return __parent->__left_;
1387 }
1388 }
1389 }
1390 }
1391 __parent = __end_node();
1392 return __parent->__left_;
1393}
1394
1395// Find upper_bound place to insert
1396// Set __parent to parent of null leaf
1397// Return reference to null leaf
1398template <class _Tp, class _Compare, class _Allocator>
1399typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1400__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node::base::pointer& __parent,
1401 const value_type& __v)
1402{
1403 __node_pointer __nd = __root();
1404 if (__nd != nullptr)
1405 {
1406 while (true)
1407 {
1408 if (value_comp()(__v, __nd->__value_))
1409 {
1410 if (__nd->__left_ != nullptr)
1411 __nd = static_cast<__node_pointer>(__nd->__left_);
1412 else
1413 {
1414 __parent = __nd;
1415 return __parent->__left_;
1416 }
1417 }
1418 else
1419 {
1420 if (__nd->__right_ != nullptr)
1421 __nd = static_cast<__node_pointer>(__nd->__right_);
1422 else
1423 {
1424 __parent = __nd;
1425 return __parent->__right_;
1426 }
1427 }
1428 }
1429 }
1430 __parent = __end_node();
1431 return __parent->__left_;
1432}
1433
1434// Find leaf place to insert closest to __hint
1435// First check prior to __hint.
1436// Next check after __hint.
1437// Next do O(log N) search.
1438// Set __parent to parent of null leaf
1439// Return reference to null leaf
1440template <class _Tp, class _Compare, class _Allocator>
1441typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1442__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
1443 typename __node::base::pointer& __parent,
1444 const value_type& __v)
1445{
1446 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1447 {
1448 // __v <= *__hint
1449 const_iterator __prior = __hint;
1450 if (__prior == begin() || !value_comp()(__v, *--__prior))
1451 {
1452 // *prev(__hint) <= __v <= *__hint
1453 if (__hint.__ptr_->__left_ == nullptr)
1454 {
1455 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1456 return __parent->__left_;
1457 }
1458 else
1459 {
1460 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1461 return __parent->__right_;
1462 }
1463 }
1464 // __v < *prev(__hint)
1465 return __find_leaf_high(__parent, __v);
1466 }
1467 // else __v > *__hint
1468 return __find_leaf_low(__parent, __v);
1469}
1470
1471// Find place to insert if __v doesn't exist
1472// Set __parent to parent of null leaf
1473// Return reference to null leaf
1474// If __v exists, set parent to node of __v and return reference to node of __v
1475template <class _Tp, class _Compare, class _Allocator>
1476template <class _Key>
1477typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1478__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node::base::pointer& __parent,
1479 const _Key& __v)
1480{
1481 __node_pointer __nd = __root();
1482 if (__nd != nullptr)
1483 {
1484 while (true)
1485 {
1486 if (value_comp()(__v, __nd->__value_))
1487 {
1488 if (__nd->__left_ != nullptr)
1489 __nd = static_cast<__node_pointer>(__nd->__left_);
1490 else
1491 {
1492 __parent = __nd;
1493 return __parent->__left_;
1494 }
1495 }
1496 else if (value_comp()(__nd->__value_, __v))
1497 {
1498 if (__nd->__right_ != nullptr)
1499 __nd = static_cast<__node_pointer>(__nd->__right_);
1500 else
1501 {
1502 __parent = __nd;
1503 return __parent->__right_;
1504 }
1505 }
1506 else
1507 {
1508 __parent = __nd;
1509 return __parent;
1510 }
1511 }
1512 }
1513 __parent = __end_node();
1514 return __parent->__left_;
1515}
1516
1517// Find place to insert if __v doesn't exist
1518// First check prior to __hint.
1519// Next check after __hint.
1520// Next do O(log N) search.
1521// Set __parent to parent of null leaf
1522// Return reference to null leaf
1523// If __v exists, set parent to node of __v and return reference to node of __v
1524template <class _Tp, class _Compare, class _Allocator>
1525template <class _Key>
1526typename __tree<_Tp, _Compare, _Allocator>::__node::base::pointer&
1527__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
1528 typename __node::base::pointer& __parent,
1529 const _Key& __v)
1530{
1531 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1532 {
1533 // __v < *__hint
1534 const_iterator __prior = __hint;
1535 if (__prior == begin() || value_comp()(*--__prior, __v))
1536 {
1537 // *prev(__hint) < __v < *__hint
1538 if (__hint.__ptr_->__left_ == nullptr)
1539 {
1540 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1541 return __parent->__left_;
1542 }
1543 else
1544 {
1545 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1546 return __parent->__right_;
1547 }
1548 }
1549 // __v <= *prev(__hint)
1550 return __find_equal(__parent, __v);
1551 }
1552 else if (value_comp()(*__hint, __v)) // check after
1553 {
1554 // *__hint < __v
1555 const_iterator __next = _STD::next(__hint);
1556 if (__next == end() || value_comp()(__v, *__next))
1557 {
1558 // *__hint < __v < *next(__hint)
1559 if (__hint.__ptr_->__right_ == nullptr)
1560 {
1561 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1562 return __parent->__right_;
1563 }
1564 else
1565 {
1566 __parent = const_cast<__node_pointer&>(__next.__ptr_);
1567 return __parent->__left_;
1568 }
1569 }
1570 // *next(__hint) <= __v
1571 return __find_equal(__parent, __v);
1572 }
1573 // else __v == *__hint
1574 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1575 return __parent;
1576}
1577
1578template <class _Tp, class _Compare, class _Allocator>
1579void
1580__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1581 __node_base_pointer& __child,
1582 __node_base_pointer __new_node)
1583{
1584 __new_node->__left_ = nullptr;
1585 __new_node->__right_ = nullptr;
1586 __new_node->__parent_ = __parent;
1587 __child = __new_node;
1588 if (__begin_node()->__left_ != nullptr)
1589 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1590 __tree_balance_after_insert(__end_node()->__left_, __child);
1591 ++size();
1592}
1593
1594#ifdef _LIBCPP_MOVE
1595
1596template <class _Tp, class _Compare, class _Allocator>
1597template <class ..._Args>
1598typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1599__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1600{
1601 __node_allocator& __na = __node_alloc();
1602 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1603 __node_traits::construct(__na, addressof(__h->__value_), _STD::forward<_Args>(__args)...);
1604 __h.get_deleter().__value_constructed = true;
1605 return __h;
1606}
1607
1608template <class _Tp, class _Compare, class _Allocator>
1609template <class... _Args>
1610pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1611__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1612{
1613 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1614 __node_base_pointer __parent;
1615 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1616 __node_pointer __r = static_cast<__node_pointer>(__child);
1617 bool __inserted = false;
1618 if (__child == nullptr)
1619 {
1620 __insert_node_at(__parent, __child, __h.get());
1621 __r = __h.release();
1622 __inserted = true;
1623 }
1624 return pair<iterator, bool>(iterator(__r), __inserted);
1625}
1626
1627template <class _Tp, class _Compare, class _Allocator>
1628template <class... _Args>
1629typename __tree<_Tp, _Compare, _Allocator>::iterator
1630__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1631{
1632 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1633 __node_base_pointer __parent;
1634 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1635 __node_pointer __r = static_cast<__node_pointer>(__child);
1636 if (__child == nullptr)
1637 {
1638 __insert_node_at(__parent, __child, __h.get());
1639 __r = __h.release();
1640 }
1641 return iterator(__r);
1642}
1643
1644template <class _Tp, class _Compare, class _Allocator>
1645template <class... _Args>
1646typename __tree<_Tp, _Compare, _Allocator>::iterator
1647__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1648{
1649 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1650 __node_base_pointer __parent;
1651 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1652 __insert_node_at(__parent, __child, __h.get());
1653 return iterator(static_cast<__node_pointer>(__h.release()));
1654}
1655
1656template <class _Tp, class _Compare, class _Allocator>
1657template <class... _Args>
1658typename __tree<_Tp, _Compare, _Allocator>::iterator
1659__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1660 _Args&&... __args)
1661{
1662 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1663 __node_base_pointer __parent;
1664 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1665 __insert_node_at(__parent, __child, __h.get());
1666 return iterator(static_cast<__node_pointer>(__h.release()));
1667}
1668
1669template <class _Tp, class _Compare, class _Allocator>
1670template <class _V>
1671pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1672__tree<_Tp, _Compare, _Allocator>::__insert_unique(_V&& __v)
1673{
1674 __node_base_pointer __parent;
1675 __node_base_pointer& __child = __find_equal(__parent, __v);
1676 __node_pointer __r = static_cast<__node_pointer>(__child);
1677 bool __inserted = false;
1678 if (__child == nullptr)
1679 {
1680 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1681 __insert_node_at(__parent, __child, __h.get());
1682 __r = __h.release();
1683 __inserted = true;
1684 }
1685 return pair<iterator, bool>(iterator(__r), __inserted);
1686}
1687
1688template <class _Tp, class _Compare, class _Allocator>
1689template <class _V>
1690typename __tree<_Tp, _Compare, _Allocator>::iterator
1691__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _V&& __v)
1692{
1693 __node_base_pointer __parent;
1694 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1695 __node_pointer __r = static_cast<__node_pointer>(__child);
1696 if (__child == nullptr)
1697 {
1698 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1699 __insert_node_at(__parent, __child, __h.get());
1700 __r = __h.release();
1701 }
1702 return iterator(__r);
1703}
1704
1705template <class _Tp, class _Compare, class _Allocator>
1706template <class _V>
1707typename __tree<_Tp, _Compare, _Allocator>::iterator
1708__tree<_Tp, _Compare, _Allocator>::__insert_multi(_V&& __v)
1709{
1710 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1711 __node_base_pointer __parent;
1712 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1713 __insert_node_at(__parent, __child, __h.get());
1714 return iterator(__h.release());
1715}
1716
1717template <class _Tp, class _Compare, class _Allocator>
1718template <class _V>
1719typename __tree<_Tp, _Compare, _Allocator>::iterator
1720__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _V&& __v)
1721{
1722 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1723 __node_base_pointer __parent;
1724 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1725 __insert_node_at(__parent, __child, __h.get());
1726 return iterator(__h.release());
1727}
1728
Howard Hinnant324bb032010-08-22 00:02:43 +00001729#else // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730
1731template <class _Tp, class _Compare, class _Allocator>
1732typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1733__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1734{
1735 __node_allocator& __na = __node_alloc();
1736 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
1737 __node_traits::construct(__na, addressof(__h->__value_), __v);
1738 __h.get_deleter().__value_constructed = true;
1739 return _STD::move(__h);
1740}
1741
1742template <class _Tp, class _Compare, class _Allocator>
1743pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1744__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1745{
1746 __node_base_pointer __parent;
1747 __node_base_pointer& __child = __find_equal(__parent, __v);
1748 __node_pointer __r = static_cast<__node_pointer>(__child);
1749 bool __inserted = false;
1750 if (__child == nullptr)
1751 {
1752 __node_holder __h = __construct_node(__v);
1753 __insert_node_at(__parent, __child, __h.get());
1754 __r = __h.release();
1755 __inserted = true;
1756 }
1757 return pair<iterator, bool>(iterator(__r), __inserted);
1758}
1759
1760template <class _Tp, class _Compare, class _Allocator>
1761typename __tree<_Tp, _Compare, _Allocator>::iterator
1762__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1763{
1764 __node_base_pointer __parent;
1765 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1766 __node_pointer __r = static_cast<__node_pointer>(__child);
1767 if (__child == nullptr)
1768 {
1769 __node_holder __h = __construct_node(__v);
1770 __insert_node_at(__parent, __child, __h.get());
1771 __r = __h.release();
1772 }
1773 return iterator(__r);
1774}
1775
1776template <class _Tp, class _Compare, class _Allocator>
1777typename __tree<_Tp, _Compare, _Allocator>::iterator
1778__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1779{
1780 __node_base_pointer __parent;
1781 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1782 __node_holder __h = __construct_node(__v);
1783 __insert_node_at(__parent, __child, __h.get());
1784 return iterator(__h.release());
1785}
1786
1787template <class _Tp, class _Compare, class _Allocator>
1788typename __tree<_Tp, _Compare, _Allocator>::iterator
1789__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1790{
1791 __node_base_pointer __parent;
1792 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1793 __node_holder __h = __construct_node(__v);
1794 __insert_node_at(__parent, __child, __h.get());
1795 return iterator(__h.release());
1796}
1797
Howard Hinnant324bb032010-08-22 00:02:43 +00001798#endif // _LIBCPP_MOVE
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799
1800template <class _Tp, class _Compare, class _Allocator>
1801pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1802__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1803{
1804 __node_base_pointer __parent;
1805 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1806 __node_pointer __r = static_cast<__node_pointer>(__child);
1807 bool __inserted = false;
1808 if (__child == nullptr)
1809 {
1810 __insert_node_at(__parent, __child, __nd);
1811 __r = __nd;
1812 __inserted = true;
1813 }
1814 return pair<iterator, bool>(iterator(__r), __inserted);
1815}
1816
1817template <class _Tp, class _Compare, class _Allocator>
1818typename __tree<_Tp, _Compare, _Allocator>::iterator
1819__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1820 __node_pointer __nd)
1821{
1822 __node_base_pointer __parent;
1823 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1824 __node_pointer __r = static_cast<__node_pointer>(__child);
1825 if (__child == nullptr)
1826 {
1827 __insert_node_at(__parent, __child, __nd);
1828 __r = __nd;
1829 }
1830 return iterator(__r);
1831}
1832
1833template <class _Tp, class _Compare, class _Allocator>
1834typename __tree<_Tp, _Compare, _Allocator>::iterator
1835__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1836{
1837 __node_base_pointer __parent;
1838 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
1839 __insert_node_at(__parent, __child, __nd);
1840 return iterator(__nd);
1841}
1842
1843template <class _Tp, class _Compare, class _Allocator>
1844typename __tree<_Tp, _Compare, _Allocator>::iterator
1845__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1846 __node_pointer __nd)
1847{
1848 __node_base_pointer __parent;
1849 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
1850 __insert_node_at(__parent, __child, __nd);
1851 return iterator(__nd);
1852}
1853
1854template <class _Tp, class _Compare, class _Allocator>
1855typename __tree<_Tp, _Compare, _Allocator>::iterator
1856__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1857{
1858 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
1859 iterator __r(__np);
1860 ++__r;
1861 if (__begin_node() == __np)
1862 __begin_node() = __r.__ptr_;
1863 --size();
1864 __node_allocator& __na = __node_alloc();
1865 __node_traits::destroy(__na, const_cast<value_type*>(addressof(*__p)));
1866 __tree_remove(__end_node()->__left_,
1867 static_cast<__node_base_pointer>(__np));
1868 __node_traits::deallocate(__na, __np, 1);
1869 return __r;
1870}
1871
1872template <class _Tp, class _Compare, class _Allocator>
1873typename __tree<_Tp, _Compare, _Allocator>::iterator
1874__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1875{
1876 while (__f != __l)
1877 __f = erase(__f);
1878 return iterator(const_cast<__node_pointer>(__l.__ptr_));
1879}
1880
1881template <class _Tp, class _Compare, class _Allocator>
1882template <class _Key>
1883typename __tree<_Tp, _Compare, _Allocator>::size_type
1884__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1885{
1886 iterator __i = find(__k);
1887 if (__i == end())
1888 return 0;
1889 erase(__i);
1890 return 1;
1891}
1892
1893template <class _Tp, class _Compare, class _Allocator>
1894template <class _Key>
1895typename __tree<_Tp, _Compare, _Allocator>::size_type
1896__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
1897{
1898 pair<iterator, iterator> __p = __equal_range_multi(__k);
1899 size_type __r = 0;
1900 for (; __p.first != __p.second; ++__r)
1901 __p.first = erase(__p.first);
1902 return __r;
1903}
1904
1905template <class _Tp, class _Compare, class _Allocator>
1906template <class _Key>
1907typename __tree<_Tp, _Compare, _Allocator>::iterator
1908__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
1909{
1910 iterator __p = __lower_bound(__v, __root(), __end_node());
1911 if (__p != end() && !value_comp()(__v, *__p))
1912 return __p;
1913 return end();
1914}
1915
1916template <class _Tp, class _Compare, class _Allocator>
1917template <class _Key>
1918typename __tree<_Tp, _Compare, _Allocator>::const_iterator
1919__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
1920{
1921 const_iterator __p = __lower_bound(__v, __root(), __end_node());
1922 if (__p != end() && !value_comp()(__v, *__p))
1923 return __p;
1924 return end();
1925}
1926
1927template <class _Tp, class _Compare, class _Allocator>
1928template <class _Key>
1929typename __tree<_Tp, _Compare, _Allocator>::size_type
1930__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
1931{
1932 __node_const_pointer __result = __end_node();
1933 __node_const_pointer __rt = __root();
1934 while (__rt != nullptr)
1935 {
1936 if (value_comp()(__k, __rt->__value_))
1937 {
1938 __result = __rt;
1939 __rt = static_cast<__node_const_pointer>(__rt->__left_);
1940 }
1941 else if (value_comp()(__rt->__value_, __k))
1942 __rt = static_cast<__node_const_pointer>(__rt->__right_);
1943 else
1944 return 1;
1945 }
1946 return 0;
1947}
1948
1949template <class _Tp, class _Compare, class _Allocator>
1950template <class _Key>
1951typename __tree<_Tp, _Compare, _Allocator>::size_type
1952__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
1953{
1954 typedef pair<const_iterator, const_iterator> _P;
1955 __node_const_pointer __result = __end_node();
1956 __node_const_pointer __rt = __root();
1957 while (__rt != nullptr)
1958 {
1959 if (value_comp()(__k, __rt->__value_))
1960 {
1961 __result = __rt;
1962 __rt = static_cast<__node_const_pointer>(__rt->__left_);
1963 }
1964 else if (value_comp()(__rt->__value_, __k))
1965 __rt = static_cast<__node_const_pointer>(__rt->__right_);
1966 else
1967 return _STD::distance(
1968 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
1969 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
1970 );
1971 }
1972 return 0;
1973}
1974
1975template <class _Tp, class _Compare, class _Allocator>
1976template <class _Key>
1977typename __tree<_Tp, _Compare, _Allocator>::iterator
1978__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
1979 __node_pointer __root,
1980 __node_pointer __result)
1981{
1982 while (__root != nullptr)
1983 {
1984 if (!value_comp()(__root->__value_, __v))
1985 {
1986 __result = __root;
1987 __root = static_cast<__node_pointer>(__root->__left_);
1988 }
1989 else
1990 __root = static_cast<__node_pointer>(__root->__right_);
1991 }
1992 return iterator(__result);
1993}
1994
1995template <class _Tp, class _Compare, class _Allocator>
1996template <class _Key>
1997typename __tree<_Tp, _Compare, _Allocator>::const_iterator
1998__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
1999 __node_const_pointer __root,
2000 __node_const_pointer __result) const
2001{
2002 while (__root != nullptr)
2003 {
2004 if (!value_comp()(__root->__value_, __v))
2005 {
2006 __result = __root;
2007 __root = static_cast<__node_const_pointer>(__root->__left_);
2008 }
2009 else
2010 __root = static_cast<__node_const_pointer>(__root->__right_);
2011 }
2012 return const_iterator(__result);
2013}
2014
2015template <class _Tp, class _Compare, class _Allocator>
2016template <class _Key>
2017typename __tree<_Tp, _Compare, _Allocator>::iterator
2018__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2019 __node_pointer __root,
2020 __node_pointer __result)
2021{
2022 while (__root != nullptr)
2023 {
2024 if (value_comp()(__v, __root->__value_))
2025 {
2026 __result = __root;
2027 __root = static_cast<__node_pointer>(__root->__left_);
2028 }
2029 else
2030 __root = static_cast<__node_pointer>(__root->__right_);
2031 }
2032 return iterator(__result);
2033}
2034
2035template <class _Tp, class _Compare, class _Allocator>
2036template <class _Key>
2037typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2038__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2039 __node_const_pointer __root,
2040 __node_const_pointer __result) const
2041{
2042 while (__root != nullptr)
2043 {
2044 if (value_comp()(__v, __root->__value_))
2045 {
2046 __result = __root;
2047 __root = static_cast<__node_const_pointer>(__root->__left_);
2048 }
2049 else
2050 __root = static_cast<__node_const_pointer>(__root->__right_);
2051 }
2052 return const_iterator(__result);
2053}
2054
2055template <class _Tp, class _Compare, class _Allocator>
2056template <class _Key>
2057pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2058 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2059__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2060{
2061 typedef pair<iterator, iterator> _P;
2062 __node_pointer __result = __end_node();
2063 __node_pointer __rt = __root();
2064 while (__rt != nullptr)
2065 {
2066 if (value_comp()(__k, __rt->__value_))
2067 {
2068 __result = __rt;
2069 __rt = static_cast<__node_pointer>(__rt->__left_);
2070 }
2071 else if (value_comp()(__rt->__value_, __k))
2072 __rt = static_cast<__node_pointer>(__rt->__right_);
2073 else
2074 return _P(iterator(__rt),
2075 iterator(
2076 __rt->__right_ != nullptr ?
2077 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2078 : __result));
2079 }
2080 return _P(iterator(__result), iterator(__result));
2081}
2082
2083template <class _Tp, class _Compare, class _Allocator>
2084template <class _Key>
2085pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2086 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2087__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2088{
2089 typedef pair<const_iterator, const_iterator> _P;
2090 __node_const_pointer __result = __end_node();
2091 __node_const_pointer __rt = __root();
2092 while (__rt != nullptr)
2093 {
2094 if (value_comp()(__k, __rt->__value_))
2095 {
2096 __result = __rt;
2097 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2098 }
2099 else if (value_comp()(__rt->__value_, __k))
2100 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2101 else
2102 return _P(const_iterator(__rt),
2103 const_iterator(
2104 __rt->__right_ != nullptr ?
2105 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2106 : __result));
2107 }
2108 return _P(const_iterator(__result), const_iterator(__result));
2109}
2110
2111template <class _Tp, class _Compare, class _Allocator>
2112template <class _Key>
2113pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2114 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2115__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2116{
2117 typedef pair<iterator, iterator> _P;
2118 __node_pointer __result = __end_node();
2119 __node_pointer __rt = __root();
2120 while (__rt != nullptr)
2121 {
2122 if (value_comp()(__k, __rt->__value_))
2123 {
2124 __result = __rt;
2125 __rt = static_cast<__node_pointer>(__rt->__left_);
2126 }
2127 else if (value_comp()(__rt->__value_, __k))
2128 __rt = static_cast<__node_pointer>(__rt->__right_);
2129 else
2130 return _P(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2131 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2132 }
2133 return _P(iterator(__result), iterator(__result));
2134}
2135
2136template <class _Tp, class _Compare, class _Allocator>
2137template <class _Key>
2138pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2139 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2140__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2141{
2142 typedef pair<const_iterator, const_iterator> _P;
2143 __node_const_pointer __result = __end_node();
2144 __node_const_pointer __rt = __root();
2145 while (__rt != nullptr)
2146 {
2147 if (value_comp()(__k, __rt->__value_))
2148 {
2149 __result = __rt;
2150 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2151 }
2152 else if (value_comp()(__rt->__value_, __k))
2153 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2154 else
2155 return _P(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2156 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2157 }
2158 return _P(const_iterator(__result), const_iterator(__result));
2159}
2160
2161template <class _Tp, class _Compare, class _Allocator>
2162typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2163__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p)
2164{
2165 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
2166 if (__begin_node() == __np)
2167 {
2168 if (__np->__right_ != nullptr)
2169 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2170 else
2171 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2172 }
2173 --size();
2174 __tree_remove(__end_node()->__left_,
2175 static_cast<__node_base_pointer>(__np));
2176 return __node_holder(__np, _D(__node_alloc()));
2177}
2178
2179_LIBCPP_END_NAMESPACE_STD
2180
2181#endif // _LIBCPP___TREE