blob: a325d9d5f08325800723f770a4de76b4fc248cc3 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
20#pragma GCC system_header
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24template <class, class, class> class __tree;
Howard Hinnant333f50d2010-09-21 20:16:37 +000025template <class, class, class> class _LIBCPP_VISIBLE __tree_iterator;
26template <class, class, class> class _LIBCPP_VISIBLE __tree_const_iterator;
27template <class, class, class, class> class _LIBCPP_VISIBLE map;
28template <class, class, class, class> class _LIBCPP_VISIBLE multimap;
29template <class, class, class> class _LIBCPP_VISIBLE set;
30template <class, class, class> class _LIBCPP_VISIBLE multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
32/*
33
34_NodePtr algorithms
35
36The algorithms taking _NodePtr are red black tree algorithms. Those
37algorithms taking a parameter named __root should assume that __root
38points to a proper red black tree (unless otherwise specified).
39
40Each algorithm herein assumes that __root->__parent_ points to a non-null
41structure which has a member __left_ which points back to __root. No other
42member is read or written to at __root->__parent_.
43
44__root->__parent_ will be referred to below (in comments only) as end_node.
45end_node->__left_ is an externably accessible lvalue for __root, and can be
46changed by node insertion and removal (without explicit reference to end_node).
47
48All nodes (with the exception of end_node), even the node referred to as
49__root, have a non-null __parent_ field.
50
51*/
52
53// Returns: true if __x is a left child of its parent, else false
54// Precondition: __x != nullptr.
55template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +000056inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057bool
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>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000122inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123_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>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000134inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135_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
Howard Hinnant333f50d2010-09-21 20:16:37 +0000516 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000517 explicit __tree_node_destructor(allocator_type& __na)
518 : __na_(__na),
519 __value_constructed(false)
520 {}
521
Howard Hinnant333f50d2010-09-21 20:16:37 +0000522 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000523 void operator()(pointer __p)
524 {
525 if (__value_constructed)
Howard Hinnant2529d022011-02-02 17:36:20 +0000526 __alloc_traits::destroy(__na_, _STD::addressof(__p->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000527 if (__p)
528 __alloc_traits::deallocate(__na_, __p, 1);
529 }
530
531 template <class> friend class __map_node_destructor;
532};
533
534// node
535
536template <class _Pointer>
537class __tree_end_node
538{
539public:
540 typedef _Pointer pointer;
541 pointer __left_;
542
Howard Hinnant333f50d2010-09-21 20:16:37 +0000543 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000544 __tree_end_node() : __left_() {}
545};
546
547template <class _VoidPtr>
548class __tree_node_base
549 : public __tree_end_node
550 <
551 typename pointer_traits<_VoidPtr>::template
552#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
553 rebind<__tree_node_base<_VoidPtr> >
554#else
555 rebind<__tree_node_base<_VoidPtr> >::other
556#endif
557 >
558{
559 __tree_node_base(const __tree_node_base&);
560 __tree_node_base& operator=(const __tree_node_base&);
561public:
562 typedef typename pointer_traits<_VoidPtr>::template
563#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
564 rebind<__tree_node_base>
565#else
566 rebind<__tree_node_base>::other
567#endif
568 pointer;
569 typedef typename pointer_traits<_VoidPtr>::template
570#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
571 rebind<const __tree_node_base>
572#else
573 rebind<const __tree_node_base>::other
574#endif
575 const_pointer;
576 typedef __tree_end_node<pointer> base;
577
578 pointer __right_;
579 pointer __parent_;
580 bool __is_black_;
581
Howard Hinnant333f50d2010-09-21 20:16:37 +0000582 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583 __tree_node_base() : __right_(), __parent_(), __is_black_(false) {}
584};
585
586template <class _Tp, class _VoidPtr>
587class __tree_node
588 : public __tree_node_base<_VoidPtr>
589{
590public:
591 typedef __tree_node_base<_VoidPtr> base;
592 typedef _Tp value_type;
593
594 value_type __value_;
595
Howard Hinnant73d21a42010-09-04 23:28:19 +0000596#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 template <class ..._Args>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000598 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 explicit __tree_node(_Args&& ...__args)
600 : __value_(_STD::forward<_Args>(__args)...) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000601#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnant333f50d2010-09-21 20:16:37 +0000602 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000603 explicit __tree_node(const value_type& __v)
604 : __value_(__v) {}
Howard Hinnant73d21a42010-09-04 23:28:19 +0000605#endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606};
607
608template <class> class __map_iterator;
609template <class> class __map_const_iterator;
610
611template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000612class _LIBCPP_VISIBLE __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000613{
614 typedef _NodePtr __node_pointer;
615 typedef typename pointer_traits<__node_pointer>::element_type __node;
616 typedef typename __node::base __node_base;
617 typedef typename __node_base::pointer __node_base_pointer;
618
619 __node_pointer __ptr_;
620
621 typedef pointer_traits<__node_pointer> __pointer_traits;
622public:
623 typedef bidirectional_iterator_tag iterator_category;
624 typedef _Tp value_type;
625 typedef _DiffType difference_type;
626 typedef value_type& reference;
627 typedef typename pointer_traits<__node_pointer>::template
628#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
629 rebind<value_type>
630#else
631 rebind<value_type>::other
632#endif
633 pointer;
634
Howard Hinnant7686add2011-06-04 14:31:57 +0000635 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636
Howard Hinnant333f50d2010-09-21 20:16:37 +0000637 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
638 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &__ptr_->__value_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000639
Howard Hinnant333f50d2010-09-21 20:16:37 +0000640 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000641 __tree_iterator& operator++()
642 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
643 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645 __tree_iterator operator++(int)
646 {__tree_iterator __t(*this); ++(*this); return __t;}
647
Howard Hinnant333f50d2010-09-21 20:16:37 +0000648 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000649 __tree_iterator& operator--()
650 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
651 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000652 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000653 __tree_iterator operator--(int)
654 {__tree_iterator __t(*this); --(*this); return __t;}
655
Howard Hinnant333f50d2010-09-21 20:16:37 +0000656 friend _LIBCPP_INLINE_VISIBILITY
657 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000659 friend _LIBCPP_INLINE_VISIBILITY
660 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000661 {return !(__x == __y);}
662
663private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000664 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000665 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000666 template <class, class, class> friend class __tree;
Howard Hinnant333f50d2010-09-21 20:16:37 +0000667 template <class, class, class> friend class _LIBCPP_VISIBLE __tree_const_iterator;
668 template <class> friend class _LIBCPP_VISIBLE __map_iterator;
669 template <class, class, class, class> friend class _LIBCPP_VISIBLE map;
670 template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap;
671 template <class, class, class> friend class _LIBCPP_VISIBLE set;
672 template <class, class, class> friend class _LIBCPP_VISIBLE multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000673};
674
675template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000676class _LIBCPP_VISIBLE __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677{
678 typedef _ConstNodePtr __node_pointer;
679 typedef typename pointer_traits<__node_pointer>::element_type __node;
680 typedef const typename __node::base __node_base;
681 typedef typename pointer_traits<__node_pointer>::template
682#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
683 rebind<__node_base>
684#else
685 rebind<__node_base>::other
686#endif
687 __node_base_pointer;
688
689 __node_pointer __ptr_;
690
691 typedef pointer_traits<__node_pointer> __pointer_traits;
692public:
693 typedef bidirectional_iterator_tag iterator_category;
694 typedef _Tp value_type;
695 typedef _DiffType difference_type;
696 typedef const value_type& reference;
697 typedef typename pointer_traits<__node_pointer>::template
698#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
699 rebind<const value_type>
700#else
701 rebind<const value_type>::other
702#endif
703 pointer;
704
Howard Hinnant333f50d2010-09-21 20:16:37 +0000705 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000706private:
707 typedef typename remove_const<__node>::type __non_const_node;
708 typedef typename pointer_traits<__node_pointer>::template
709#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
710 rebind<__non_const_node>
711#else
712 rebind<__non_const_node>::other
713#endif
714 __non_const_node_pointer;
715 typedef __tree_iterator<value_type, __non_const_node_pointer, difference_type>
716 __non_const_iterator;
717public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000718 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000719 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
720 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000721
Howard Hinnant333f50d2010-09-21 20:16:37 +0000722 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
723 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return &__ptr_->__value_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000724
Howard Hinnant333f50d2010-09-21 20:16:37 +0000725 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000726 __tree_const_iterator& operator++()
727 {__ptr_ = static_cast<__node_pointer>(__tree_next(static_cast<__node_base_pointer>(__ptr_)));
728 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730 __tree_const_iterator operator++(int)
731 {__tree_const_iterator __t(*this); ++(*this); return __t;}
732
Howard Hinnant333f50d2010-09-21 20:16:37 +0000733 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000734 __tree_const_iterator& operator--()
735 {__ptr_ = static_cast<__node_pointer>(__tree_prev(static_cast<__node_base_pointer>(__ptr_)));
736 return *this;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000737 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000738 __tree_const_iterator operator--(int)
739 {__tree_const_iterator __t(*this); --(*this); return __t;}
740
Howard Hinnant333f50d2010-09-21 20:16:37 +0000741 friend _LIBCPP_INLINE_VISIBILITY
742 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000743 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000744 friend _LIBCPP_INLINE_VISIBILITY
745 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000746 {return !(__x == __y);}
747
748private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000749 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000750 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
751 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000752 template <class, class, class> friend class __tree;
Howard Hinnant333f50d2010-09-21 20:16:37 +0000753 template <class, class, class, class> friend class _LIBCPP_VISIBLE map;
754 template <class, class, class, class> friend class _LIBCPP_VISIBLE multimap;
755 template <class, class, class> friend class _LIBCPP_VISIBLE set;
756 template <class, class, class> friend class _LIBCPP_VISIBLE multiset;
757 template <class> friend class _LIBCPP_VISIBLE __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758};
759
760template <class _Tp, class _Compare, class _Allocator>
761class __tree
762{
763public:
764 typedef _Tp value_type;
765 typedef _Compare value_compare;
766 typedef _Allocator allocator_type;
767 typedef allocator_traits<allocator_type> __alloc_traits;
768 typedef typename __alloc_traits::pointer pointer;
769 typedef typename __alloc_traits::const_pointer const_pointer;
770 typedef typename __alloc_traits::size_type size_type;
771 typedef typename __alloc_traits::difference_type difference_type;
772
773 typedef __tree_node<value_type, typename __alloc_traits::void_pointer> __node;
Howard Hinnantd615e472011-04-03 20:05:29 +0000774 typedef __tree_node_base<typename __alloc_traits::void_pointer> __node_base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000775 typedef typename __alloc_traits::template
776#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
777 rebind_alloc<__node>
778#else
779 rebind_alloc<__node>::other
780#endif
781 __node_allocator;
782 typedef allocator_traits<__node_allocator> __node_traits;
783 typedef typename __node_traits::pointer __node_pointer;
784 typedef typename __node_traits::const_pointer __node_const_pointer;
Howard Hinnantd615e472011-04-03 20:05:29 +0000785 typedef typename __node_base::pointer __node_base_pointer;
786 typedef typename __node_base::const_pointer __node_base_const_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000787private:
Howard Hinnantd615e472011-04-03 20:05:29 +0000788 typedef typename __node_base::base __end_node_t;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000789 typedef typename pointer_traits<__node_pointer>::template
790#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
791 rebind<__end_node_t>
792#else
793 rebind<__end_node_t>::other
794#endif
795 __end_node_ptr;
796 typedef typename pointer_traits<__node_pointer>::template
797#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
798 rebind<const __end_node_t>
799#else
800 rebind<const __end_node_t>::other
801#endif
802 __end_node_const_ptr;
803
804 __node_pointer __begin_node_;
805 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
806 __compressed_pair<size_type, value_compare> __pair3_;
807
808public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000810 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000811 {
812 return static_cast<__node_pointer>
813 (
814 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
815 );
816 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000818 __node_const_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819 {
820 return static_cast<__node_const_pointer>
821 (
822 pointer_traits<__end_node_const_ptr>::pointer_to(__pair1_.first())
823 );
824 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000825 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000826 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000827private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000828 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000829 const __node_allocator& __node_alloc() const _NOEXCEPT
830 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000832 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000834 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000835public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000836 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000837 allocator_type __alloc() const _NOEXCEPT
838 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000840 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000841 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000844 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000845 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000846 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000848 const value_compare& value_comp() const _NOEXCEPT
849 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000850public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000852 __node_pointer __root() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000853 {return static_cast<__node_pointer> (__end_node()->__left_);}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000854 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000855 __node_const_pointer __root() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000856 {return static_cast<__node_const_pointer>(__end_node()->__left_);}
857
858 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
859 typedef __tree_const_iterator<value_type, __node_const_pointer, difference_type> const_iterator;
860
Howard Hinnant7686add2011-06-04 14:31:57 +0000861 explicit __tree(const value_compare& __comp)
862 _NOEXCEPT_(
863 is_nothrow_default_constructible<__node_allocator>::value &&
864 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000865 explicit __tree(const allocator_type& __a);
866 __tree(const value_compare& __comp, const allocator_type& __a);
867 __tree(const __tree& __t);
868 __tree& operator=(const __tree& __t);
869 template <class _InputIterator>
870 void __assign_unique(_InputIterator __first, _InputIterator __last);
871 template <class _InputIterator>
872 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000873#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-06-04 14:31:57 +0000874 __tree(__tree&& __t)
875 _NOEXCEPT_(
876 is_nothrow_move_constructible<__node_allocator>::value &&
877 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +0000879 __tree& operator=(__tree&& __t)
880 _NOEXCEPT_(
881 __node_traits::propagate_on_container_move_assignment::value &&
882 is_nothrow_move_assignable<value_compare>::value &&
883 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000884#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000885
886 ~__tree();
887
Howard Hinnant333f50d2010-09-21 20:16:37 +0000888 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000889 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000890 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000891 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000892 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000893 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000894 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000895 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000896
Howard Hinnant333f50d2010-09-21 20:16:37 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000898 size_type max_size() const _NOEXCEPT
899 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900
Howard Hinnant7686add2011-06-04 14:31:57 +0000901 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000902
Howard Hinnant7686add2011-06-04 14:31:57 +0000903 void swap(__tree& __t)
904 _NOEXCEPT_(
905 __is_nothrow_swappable<value_compare>::value &&
906 (!__node_traits::propagate_on_container_swap::value ||
907 __is_nothrow_swappable<__node_allocator>::value));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908
Howard Hinnant73d21a42010-09-04 23:28:19 +0000909#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
910#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000911 template <class... _Args>
912 pair<iterator, bool>
913 __emplace_unique(_Args&&... __args);
914 template <class... _Args>
915 iterator
916 __emplace_multi(_Args&&... __args);
917
918 template <class... _Args>
919 iterator
920 __emplace_hint_unique(const_iterator __p, _Args&&... __args);
921 template <class... _Args>
922 iterator
923 __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Howard Hinnant73d21a42010-09-04 23:28:19 +0000924#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000925
926 template <class _V>
927 pair<iterator, bool> __insert_unique(_V&& __v);
928 template <class _V>
929 iterator __insert_unique(const_iterator __p, _V&& __v);
930 template <class _V>
931 iterator __insert_multi(_V&& __v);
932 template <class _V>
933 iterator __insert_multi(const_iterator __p, _V&& __v);
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +0000934#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000935
936 pair<iterator, bool> __insert_unique(const value_type& __v);
937 iterator __insert_unique(const_iterator __p, const value_type& __v);
938 iterator __insert_multi(const value_type& __v);
939 iterator __insert_multi(const_iterator __p, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000940
941 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
942 iterator __node_insert_unique(const_iterator __p,
943 __node_pointer __nd);
944
945 iterator __node_insert_multi(__node_pointer __nd);
946 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
947
948 iterator erase(const_iterator __p);
949 iterator erase(const_iterator __f, const_iterator __l);
950 template <class _Key>
951 size_type __erase_unique(const _Key& __k);
952 template <class _Key>
953 size_type __erase_multi(const _Key& __k);
954
955 void __insert_node_at(__node_base_pointer __parent,
956 __node_base_pointer& __child,
957 __node_base_pointer __new_node);
958
959 template <class _Key>
960 iterator find(const _Key& __v);
961 template <class _Key>
962 const_iterator find(const _Key& __v) const;
963
964 template <class _Key>
965 size_type __count_unique(const _Key& __k) const;
966 template <class _Key>
967 size_type __count_multi(const _Key& __k) const;
968
969 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000970 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 iterator lower_bound(const _Key& __v)
972 {return __lower_bound(__v, __root(), __end_node());}
973 template <class _Key>
974 iterator __lower_bound(const _Key& __v,
975 __node_pointer __root,
976 __node_pointer __result);
977 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 const_iterator lower_bound(const _Key& __v) const
980 {return __lower_bound(__v, __root(), __end_node());}
981 template <class _Key>
982 const_iterator __lower_bound(const _Key& __v,
983 __node_const_pointer __root,
984 __node_const_pointer __result) const;
985 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987 iterator upper_bound(const _Key& __v)
988 {return __upper_bound(__v, __root(), __end_node());}
989 template <class _Key>
990 iterator __upper_bound(const _Key& __v,
991 __node_pointer __root,
992 __node_pointer __result);
993 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000995 const_iterator upper_bound(const _Key& __v) const
996 {return __upper_bound(__v, __root(), __end_node());}
997 template <class _Key>
998 const_iterator __upper_bound(const _Key& __v,
999 __node_const_pointer __root,
1000 __node_const_pointer __result) const;
1001 template <class _Key>
1002 pair<iterator, iterator>
1003 __equal_range_unique(const _Key& __k);
1004 template <class _Key>
1005 pair<const_iterator, const_iterator>
1006 __equal_range_unique(const _Key& __k) const;
1007
1008 template <class _Key>
1009 pair<iterator, iterator>
1010 __equal_range_multi(const _Key& __k);
1011 template <class _Key>
1012 pair<const_iterator, const_iterator>
1013 __equal_range_multi(const _Key& __k) const;
1014
1015 typedef __tree_node_destructor<__node_allocator> _D;
1016 typedef unique_ptr<__node, _D> __node_holder;
1017
1018 __node_holder remove(const_iterator __p);
1019private:
Howard Hinnantd615e472011-04-03 20:05:29 +00001020 typename __node_base::pointer&
1021 __find_leaf_low(typename __node_base::pointer& __parent, const value_type& __v);
1022 typename __node_base::pointer&
1023 __find_leaf_high(typename __node_base::pointer& __parent, const value_type& __v);
1024 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025 __find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001026 typename __node_base::pointer& __parent, const value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001027 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001028 typename __node_base::pointer&
1029 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001031 typename __node_base::pointer&
1032 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033 const _Key& __v);
1034
Howard Hinnant73d21a42010-09-04 23:28:19 +00001035#if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001036 template <class ..._Args>
1037 __node_holder __construct_node(_Args&& ...__args);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001038#else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039 __node_holder __construct_node(const value_type& __v);
1040#endif
1041
Howard Hinnant7686add2011-06-04 14:31:57 +00001042 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001043
Howard Hinnant333f50d2010-09-21 20:16:37 +00001044 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045 void __copy_assign_alloc(const __tree& __t)
1046 {__copy_assign_alloc(__t, integral_constant<bool,
1047 __node_traits::propagate_on_container_copy_assignment::value>());}
1048
Howard Hinnant333f50d2010-09-21 20:16:37 +00001049 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 void __copy_assign_alloc(const __tree& __t, true_type)
1051 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053 void __copy_assign_alloc(const __tree& __t, false_type) {}
1054
1055 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001056 void __move_assign(__tree& __t, true_type)
1057 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1058 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059
Howard Hinnant333f50d2010-09-21 20:16:37 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001062 _NOEXCEPT_(
1063 !__node_traits::propagate_on_container_move_assignment::value ||
1064 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065 {__move_assign_alloc(__t, integral_constant<bool,
1066 __node_traits::propagate_on_container_move_assignment::value>());}
1067
Howard Hinnant333f50d2010-09-21 20:16:37 +00001068 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001070 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071 {__node_alloc() = _STD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001072 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001073 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074
Howard Hinnant333f50d2010-09-21 20:16:37 +00001075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001076 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y)
Howard Hinnant7686add2011-06-04 14:31:57 +00001077 _NOEXCEPT_(
1078 !__node_traits::propagate_on_container_swap::value ||
1079 __is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080 {__swap_alloc(__x, __y, integral_constant<bool,
1081 __node_traits::propagate_on_container_swap::value>());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001083 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001084 _NOEXCEPT_(__is_nothrow_swappable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085 {
1086 using _STD::swap;
1087 swap(__x, __y);
1088 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001089 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090 static void __swap_alloc(__node_allocator& __x, __node_allocator& __y, false_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001091 _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001092 {}
1093
1094 __node_pointer __detach();
1095 static __node_pointer __detach(__node_pointer);
1096};
1097
1098template <class _Tp, class _Compare, class _Allocator>
1099__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001100 _NOEXCEPT_(
1101 is_nothrow_default_constructible<__node_allocator>::value &&
1102 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001103 : __pair3_(0, __comp)
1104{
1105 __begin_node() = __end_node();
1106}
1107
1108template <class _Tp, class _Compare, class _Allocator>
1109__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
1110 : __pair1_(__node_allocator(__a)),
1111 __begin_node_(__node_pointer()),
1112 __pair3_(0)
1113{
1114 __begin_node() = __end_node();
1115}
1116
1117template <class _Tp, class _Compare, class _Allocator>
1118__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1119 const allocator_type& __a)
1120 : __pair1_(__node_allocator(__a)),
1121 __begin_node_(__node_pointer()),
1122 __pair3_(0, __comp)
1123{
1124 __begin_node() = __end_node();
1125}
1126
1127// Precondition: size() != 0
1128template <class _Tp, class _Compare, class _Allocator>
1129typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1130__tree<_Tp, _Compare, _Allocator>::__detach()
1131{
1132 __node_pointer __cache = __begin_node();
1133 __begin_node() = __end_node();
1134 __end_node()->__left_->__parent_ = nullptr;
1135 __end_node()->__left_ = nullptr;
1136 size() = 0;
1137 // __cache->__left_ == nullptr
1138 if (__cache->__right_ != nullptr)
1139 __cache = static_cast<__node_pointer>(__cache->__right_);
1140 // __cache->__left_ == nullptr
1141 // __cache->__right_ == nullptr
1142 return __cache;
1143}
1144
1145// Precondition: __cache != nullptr
1146// __cache->left_ == nullptr
1147// __cache->right_ == nullptr
1148// This is no longer a red-black tree
1149template <class _Tp, class _Compare, class _Allocator>
1150typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1151__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1152{
1153 if (__cache->__parent_ == nullptr)
1154 return nullptr;
1155 if (__tree_is_left_child(__cache))
1156 {
1157 __cache->__parent_->__left_ = nullptr;
1158 __cache = static_cast<__node_pointer>(__cache->__parent_);
1159 if (__cache->__right_ == nullptr)
1160 return __cache;
1161 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1162 }
1163 // __cache is right child
1164 __cache->__parent_->__right_ = nullptr;
1165 __cache = static_cast<__node_pointer>(__cache->__parent_);
1166 if (__cache->__left_ == nullptr)
1167 return __cache;
1168 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1169}
1170
1171template <class _Tp, class _Compare, class _Allocator>
1172__tree<_Tp, _Compare, _Allocator>&
1173__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1174{
1175 if (this != &__t)
1176 {
1177 value_comp() = __t.value_comp();
1178 __copy_assign_alloc(__t);
1179 __assign_multi(__t.begin(), __t.end());
1180 }
1181 return *this;
1182}
1183
1184template <class _Tp, class _Compare, class _Allocator>
1185template <class _InputIterator>
1186void
1187__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1188{
1189 if (size() != 0)
1190 {
1191 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001192#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001193 try
1194 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001195#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001196 for (; __cache != nullptr && __first != __last; ++__first)
1197 {
1198 __cache->__value_ = *__first;
1199 __node_pointer __next = __detach(__cache);
1200 __node_insert_unique(__cache);
1201 __cache = __next;
1202 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001203#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001204 }
1205 catch (...)
1206 {
1207 while (__cache->__parent_ != nullptr)
1208 __cache = static_cast<__node_pointer>(__cache->__parent_);
1209 destroy(__cache);
1210 throw;
1211 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001212#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001213 if (__cache != nullptr)
1214 {
1215 while (__cache->__parent_ != nullptr)
1216 __cache = static_cast<__node_pointer>(__cache->__parent_);
1217 destroy(__cache);
1218 }
1219 }
1220 for (; __first != __last; ++__first)
1221 __insert_unique(*__first);
1222}
1223
1224template <class _Tp, class _Compare, class _Allocator>
1225template <class _InputIterator>
1226void
1227__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1228{
1229 if (size() != 0)
1230 {
1231 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001232#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001233 try
1234 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001235#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001236 for (; __cache != nullptr && __first != __last; ++__first)
1237 {
1238 __cache->__value_ = *__first;
1239 __node_pointer __next = __detach(__cache);
1240 __node_insert_multi(__cache);
1241 __cache = __next;
1242 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001243#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001244 }
1245 catch (...)
1246 {
1247 while (__cache->__parent_ != nullptr)
1248 __cache = static_cast<__node_pointer>(__cache->__parent_);
1249 destroy(__cache);
1250 throw;
1251 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001252#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253 if (__cache != nullptr)
1254 {
1255 while (__cache->__parent_ != nullptr)
1256 __cache = static_cast<__node_pointer>(__cache->__parent_);
1257 destroy(__cache);
1258 }
1259 }
1260 for (; __first != __last; ++__first)
1261 __insert_multi(*__first);
1262}
1263
1264template <class _Tp, class _Compare, class _Allocator>
1265__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1266 : __begin_node_(__node_pointer()),
1267 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1268 __pair3_(0, __t.value_comp())
1269{
1270 __begin_node() = __end_node();
1271}
1272
Howard Hinnant73d21a42010-09-04 23:28:19 +00001273#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274
1275template <class _Tp, class _Compare, class _Allocator>
1276__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001277 _NOEXCEPT_(
1278 is_nothrow_move_constructible<__node_allocator>::value &&
1279 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 : __begin_node_(_STD::move(__t.__begin_node_)),
1281 __pair1_(_STD::move(__t.__pair1_)),
1282 __pair3_(_STD::move(__t.__pair3_))
1283{
1284 if (size() == 0)
1285 __begin_node() = __end_node();
1286 else
1287 {
1288 __end_node()->__left_->__parent_ = __end_node();
1289 __t.__begin_node() = __t.__end_node();
1290 __t.__end_node()->__left_ = nullptr;
1291 __t.size() = 0;
1292 }
1293}
1294
1295template <class _Tp, class _Compare, class _Allocator>
1296__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1297 : __pair1_(__node_allocator(__a)),
1298 __pair3_(0, _STD::move(__t.value_comp()))
1299{
1300 if (__a == __t.__alloc())
1301 {
1302 if (__t.size() == 0)
1303 __begin_node() = __end_node();
1304 else
1305 {
1306 __begin_node() = __t.__begin_node();
1307 __end_node()->__left_ = __t.__end_node()->__left_;
1308 __end_node()->__left_->__parent_ = __end_node();
1309 size() = __t.size();
1310 __t.__begin_node() = __t.__end_node();
1311 __t.__end_node()->__left_ = nullptr;
1312 __t.size() = 0;
1313 }
1314 }
1315 else
1316 {
1317 __begin_node() = __end_node();
1318 }
1319}
1320
1321template <class _Tp, class _Compare, class _Allocator>
1322void
1323__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001324 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1325 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001326{
1327 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1328 __begin_node_ = __t.__begin_node_;
1329 __pair1_.first() = __t.__pair1_.first();
1330 __move_assign_alloc(__t);
1331 __pair3_ = _STD::move(__t.__pair3_);
1332 if (size() == 0)
1333 __begin_node() = __end_node();
1334 else
1335 {
1336 __end_node()->__left_->__parent_ = __end_node();
1337 __t.__begin_node() = __t.__end_node();
1338 __t.__end_node()->__left_ = nullptr;
1339 __t.size() = 0;
1340 }
1341}
1342
1343template <class _Tp, class _Compare, class _Allocator>
1344void
1345__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1346{
1347 if (__node_alloc() == __t.__node_alloc())
1348 __move_assign(__t, true_type());
1349 else
1350 {
1351 value_comp() = _STD::move(__t.value_comp());
1352 const_iterator __e = end();
1353 if (size() != 0)
1354 {
1355 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001356#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001357 try
1358 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001359#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001360 while (__cache != nullptr && __t.size() != 0)
1361 {
1362 __cache->__value_ = _STD::move(__t.remove(__t.begin())->__value_);
1363 __node_pointer __next = __detach(__cache);
1364 __node_insert_multi(__cache);
1365 __cache = __next;
1366 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001367#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001368 }
1369 catch (...)
1370 {
1371 while (__cache->__parent_ != nullptr)
1372 __cache = static_cast<__node_pointer>(__cache->__parent_);
1373 destroy(__cache);
1374 throw;
1375 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001376#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377 if (__cache != nullptr)
1378 {
1379 while (__cache->__parent_ != nullptr)
1380 __cache = static_cast<__node_pointer>(__cache->__parent_);
1381 destroy(__cache);
1382 }
1383 }
1384 while (__t.size() != 0)
1385 __insert_multi(__e, _STD::move(__t.remove(__t.begin())->__value_));
1386 }
1387}
1388
1389template <class _Tp, class _Compare, class _Allocator>
1390__tree<_Tp, _Compare, _Allocator>&
1391__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001392 _NOEXCEPT_(
1393 __node_traits::propagate_on_container_move_assignment::value &&
1394 is_nothrow_move_assignable<value_compare>::value &&
1395 is_nothrow_move_assignable<__node_allocator>::value)
1396
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397{
1398 __move_assign(__t, integral_constant<bool,
1399 __node_traits::propagate_on_container_move_assignment::value>());
1400 return *this;
1401}
1402
Howard Hinnant73d21a42010-09-04 23:28:19 +00001403#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001404
1405template <class _Tp, class _Compare, class _Allocator>
1406__tree<_Tp, _Compare, _Allocator>::~__tree()
1407{
1408 destroy(__root());
1409}
1410
1411template <class _Tp, class _Compare, class _Allocator>
1412void
Howard Hinnant7686add2011-06-04 14:31:57 +00001413__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001414{
1415 if (__nd != nullptr)
1416 {
1417 destroy(static_cast<__node_pointer>(__nd->__left_));
1418 destroy(static_cast<__node_pointer>(__nd->__right_));
1419 __node_allocator& __na = __node_alloc();
Howard Hinnant2529d022011-02-02 17:36:20 +00001420 __node_traits::destroy(__na, _STD::addressof(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001421 __node_traits::deallocate(__na, __nd, 1);
1422 }
1423}
1424
1425template <class _Tp, class _Compare, class _Allocator>
1426void
1427__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001428 _NOEXCEPT_(
1429 __is_nothrow_swappable<value_compare>::value &&
1430 (!__node_traits::propagate_on_container_swap::value ||
1431 __is_nothrow_swappable<__node_allocator>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432{
1433 using _STD::swap;
1434 swap(__begin_node_, __t.__begin_node_);
1435 swap(__pair1_.first(), __t.__pair1_.first());
1436 __swap_alloc(__node_alloc(), __t.__node_alloc());
1437 __pair3_.swap(__t.__pair3_);
1438 if (size() == 0)
1439 __begin_node() = __end_node();
1440 else
1441 __end_node()->__left_->__parent_ = __end_node();
1442 if (__t.size() == 0)
1443 __t.__begin_node() = __t.__end_node();
1444 else
1445 __t.__end_node()->__left_->__parent_ = __t.__end_node();
1446}
1447
1448template <class _Tp, class _Compare, class _Allocator>
1449void
Howard Hinnant7686add2011-06-04 14:31:57 +00001450__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451{
1452 destroy(__root());
1453 size() = 0;
1454 __begin_node() = __end_node();
1455 __end_node()->__left_ = nullptr;
1456}
1457
1458// Find lower_bound place to insert
1459// Set __parent to parent of null leaf
1460// Return reference to null leaf
1461template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001462typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1463__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001464 const value_type& __v)
1465{
1466 __node_pointer __nd = __root();
1467 if (__nd != nullptr)
1468 {
1469 while (true)
1470 {
1471 if (value_comp()(__nd->__value_, __v))
1472 {
1473 if (__nd->__right_ != nullptr)
1474 __nd = static_cast<__node_pointer>(__nd->__right_);
1475 else
1476 {
1477 __parent = __nd;
1478 return __parent->__right_;
1479 }
1480 }
1481 else
1482 {
1483 if (__nd->__left_ != nullptr)
1484 __nd = static_cast<__node_pointer>(__nd->__left_);
1485 else
1486 {
1487 __parent = __nd;
1488 return __parent->__left_;
1489 }
1490 }
1491 }
1492 }
1493 __parent = __end_node();
1494 return __parent->__left_;
1495}
1496
1497// Find upper_bound place to insert
1498// Set __parent to parent of null leaf
1499// Return reference to null leaf
1500template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001501typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1502__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001503 const value_type& __v)
1504{
1505 __node_pointer __nd = __root();
1506 if (__nd != nullptr)
1507 {
1508 while (true)
1509 {
1510 if (value_comp()(__v, __nd->__value_))
1511 {
1512 if (__nd->__left_ != nullptr)
1513 __nd = static_cast<__node_pointer>(__nd->__left_);
1514 else
1515 {
1516 __parent = __nd;
1517 return __parent->__left_;
1518 }
1519 }
1520 else
1521 {
1522 if (__nd->__right_ != nullptr)
1523 __nd = static_cast<__node_pointer>(__nd->__right_);
1524 else
1525 {
1526 __parent = __nd;
1527 return __parent->__right_;
1528 }
1529 }
1530 }
1531 }
1532 __parent = __end_node();
1533 return __parent->__left_;
1534}
1535
1536// Find leaf place to insert closest to __hint
1537// First check prior to __hint.
1538// Next check after __hint.
1539// Next do O(log N) search.
1540// Set __parent to parent of null leaf
1541// Return reference to null leaf
1542template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001543typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001545 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001546 const value_type& __v)
1547{
1548 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1549 {
1550 // __v <= *__hint
1551 const_iterator __prior = __hint;
1552 if (__prior == begin() || !value_comp()(__v, *--__prior))
1553 {
1554 // *prev(__hint) <= __v <= *__hint
1555 if (__hint.__ptr_->__left_ == nullptr)
1556 {
1557 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1558 return __parent->__left_;
1559 }
1560 else
1561 {
1562 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1563 return __parent->__right_;
1564 }
1565 }
1566 // __v < *prev(__hint)
1567 return __find_leaf_high(__parent, __v);
1568 }
1569 // else __v > *__hint
1570 return __find_leaf_low(__parent, __v);
1571}
1572
1573// Find place to insert if __v doesn't exist
1574// Set __parent to parent of null leaf
1575// Return reference to null leaf
1576// If __v exists, set parent to node of __v and return reference to node of __v
1577template <class _Tp, class _Compare, class _Allocator>
1578template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001579typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1580__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001581 const _Key& __v)
1582{
1583 __node_pointer __nd = __root();
1584 if (__nd != nullptr)
1585 {
1586 while (true)
1587 {
1588 if (value_comp()(__v, __nd->__value_))
1589 {
1590 if (__nd->__left_ != nullptr)
1591 __nd = static_cast<__node_pointer>(__nd->__left_);
1592 else
1593 {
1594 __parent = __nd;
1595 return __parent->__left_;
1596 }
1597 }
1598 else if (value_comp()(__nd->__value_, __v))
1599 {
1600 if (__nd->__right_ != nullptr)
1601 __nd = static_cast<__node_pointer>(__nd->__right_);
1602 else
1603 {
1604 __parent = __nd;
1605 return __parent->__right_;
1606 }
1607 }
1608 else
1609 {
1610 __parent = __nd;
1611 return __parent;
1612 }
1613 }
1614 }
1615 __parent = __end_node();
1616 return __parent->__left_;
1617}
1618
1619// Find place to insert if __v doesn't exist
1620// First check prior to __hint.
1621// Next check after __hint.
1622// Next do O(log N) search.
1623// Set __parent to parent of null leaf
1624// Return reference to null leaf
1625// If __v exists, set parent to node of __v and return reference to node of __v
1626template <class _Tp, class _Compare, class _Allocator>
1627template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001628typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001629__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001630 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 const _Key& __v)
1632{
1633 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1634 {
1635 // __v < *__hint
1636 const_iterator __prior = __hint;
1637 if (__prior == begin() || value_comp()(*--__prior, __v))
1638 {
1639 // *prev(__hint) < __v < *__hint
1640 if (__hint.__ptr_->__left_ == nullptr)
1641 {
1642 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1643 return __parent->__left_;
1644 }
1645 else
1646 {
1647 __parent = const_cast<__node_pointer&>(__prior.__ptr_);
1648 return __parent->__right_;
1649 }
1650 }
1651 // __v <= *prev(__hint)
1652 return __find_equal(__parent, __v);
1653 }
1654 else if (value_comp()(*__hint, __v)) // check after
1655 {
1656 // *__hint < __v
1657 const_iterator __next = _STD::next(__hint);
1658 if (__next == end() || value_comp()(__v, *__next))
1659 {
Douglas Gregor7ac6af72011-04-29 16:20:26 +00001660 // *__hint < __v < *_STD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001661 if (__hint.__ptr_->__right_ == nullptr)
1662 {
1663 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1664 return __parent->__right_;
1665 }
1666 else
1667 {
1668 __parent = const_cast<__node_pointer&>(__next.__ptr_);
1669 return __parent->__left_;
1670 }
1671 }
1672 // *next(__hint) <= __v
1673 return __find_equal(__parent, __v);
1674 }
1675 // else __v == *__hint
1676 __parent = const_cast<__node_pointer&>(__hint.__ptr_);
1677 return __parent;
1678}
1679
1680template <class _Tp, class _Compare, class _Allocator>
1681void
1682__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1683 __node_base_pointer& __child,
1684 __node_base_pointer __new_node)
1685{
1686 __new_node->__left_ = nullptr;
1687 __new_node->__right_ = nullptr;
1688 __new_node->__parent_ = __parent;
1689 __child = __new_node;
1690 if (__begin_node()->__left_ != nullptr)
1691 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1692 __tree_balance_after_insert(__end_node()->__left_, __child);
1693 ++size();
1694}
1695
Howard Hinnant73d21a42010-09-04 23:28:19 +00001696#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1697#ifndef _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001698
1699template <class _Tp, class _Compare, class _Allocator>
1700template <class ..._Args>
1701typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1702__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1703{
1704 __node_allocator& __na = __node_alloc();
1705 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001706 __node_traits::construct(__na, _STD::addressof(__h->__value_), _STD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 __h.get_deleter().__value_constructed = true;
1708 return __h;
1709}
1710
1711template <class _Tp, class _Compare, class _Allocator>
1712template <class... _Args>
1713pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1714__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1715{
1716 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1717 __node_base_pointer __parent;
1718 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1719 __node_pointer __r = static_cast<__node_pointer>(__child);
1720 bool __inserted = false;
1721 if (__child == nullptr)
1722 {
1723 __insert_node_at(__parent, __child, __h.get());
1724 __r = __h.release();
1725 __inserted = true;
1726 }
1727 return pair<iterator, bool>(iterator(__r), __inserted);
1728}
1729
1730template <class _Tp, class _Compare, class _Allocator>
1731template <class... _Args>
1732typename __tree<_Tp, _Compare, _Allocator>::iterator
1733__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
1734{
1735 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1736 __node_base_pointer __parent;
1737 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
1738 __node_pointer __r = static_cast<__node_pointer>(__child);
1739 if (__child == nullptr)
1740 {
1741 __insert_node_at(__parent, __child, __h.get());
1742 __r = __h.release();
1743 }
1744 return iterator(__r);
1745}
1746
1747template <class _Tp, class _Compare, class _Allocator>
1748template <class... _Args>
1749typename __tree<_Tp, _Compare, _Allocator>::iterator
1750__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
1751{
1752 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1753 __node_base_pointer __parent;
1754 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1755 __insert_node_at(__parent, __child, __h.get());
1756 return iterator(static_cast<__node_pointer>(__h.release()));
1757}
1758
1759template <class _Tp, class _Compare, class _Allocator>
1760template <class... _Args>
1761typename __tree<_Tp, _Compare, _Allocator>::iterator
1762__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
1763 _Args&&... __args)
1764{
1765 __node_holder __h = __construct_node(_STD::forward<_Args>(__args)...);
1766 __node_base_pointer __parent;
1767 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1768 __insert_node_at(__parent, __child, __h.get());
1769 return iterator(static_cast<__node_pointer>(__h.release()));
1770}
1771
Howard Hinnant73d21a42010-09-04 23:28:19 +00001772#endif // _LIBCPP_HAS_NO_VARIADICS
1773
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001774template <class _Tp, class _Compare, class _Allocator>
1775template <class _V>
1776pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1777__tree<_Tp, _Compare, _Allocator>::__insert_unique(_V&& __v)
1778{
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001779 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1780 pair<iterator, bool> __r = __node_insert_unique(__h.get());
1781 if (__r.second)
1782 __h.release();
1783 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784}
1785
1786template <class _Tp, class _Compare, class _Allocator>
1787template <class _V>
1788typename __tree<_Tp, _Compare, _Allocator>::iterator
1789__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, _V&& __v)
1790{
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001791 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1792 iterator __r = __node_insert_unique(__p, __h.get());
1793 if (__r.__ptr_ == __h.get())
1794 __h.release();
1795 return __r;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001796}
1797
1798template <class _Tp, class _Compare, class _Allocator>
1799template <class _V>
1800typename __tree<_Tp, _Compare, _Allocator>::iterator
1801__tree<_Tp, _Compare, _Allocator>::__insert_multi(_V&& __v)
1802{
1803 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1804 __node_base_pointer __parent;
1805 __node_base_pointer& __child = __find_leaf_high(__parent, __h->__value_);
1806 __insert_node_at(__parent, __child, __h.get());
1807 return iterator(__h.release());
1808}
1809
1810template <class _Tp, class _Compare, class _Allocator>
1811template <class _V>
1812typename __tree<_Tp, _Compare, _Allocator>::iterator
1813__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, _V&& __v)
1814{
1815 __node_holder __h = __construct_node(_STD::forward<_V>(__v));
1816 __node_base_pointer __parent;
1817 __node_base_pointer& __child = __find_leaf(__p, __parent, __h->__value_);
1818 __insert_node_at(__parent, __child, __h.get());
1819 return iterator(__h.release());
1820}
1821
Howard Hinnant73d21a42010-09-04 23:28:19 +00001822#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001823
1824template <class _Tp, class _Compare, class _Allocator>
1825typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1826__tree<_Tp, _Compare, _Allocator>::__construct_node(const value_type& __v)
1827{
1828 __node_allocator& __na = __node_alloc();
1829 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnant2529d022011-02-02 17:36:20 +00001830 __node_traits::construct(__na, _STD::addressof(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001831 __h.get_deleter().__value_constructed = true;
1832 return _STD::move(__h);
1833}
1834
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00001835#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1836
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001837template <class _Tp, class _Compare, class _Allocator>
1838pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1839__tree<_Tp, _Compare, _Allocator>::__insert_unique(const value_type& __v)
1840{
1841 __node_base_pointer __parent;
1842 __node_base_pointer& __child = __find_equal(__parent, __v);
1843 __node_pointer __r = static_cast<__node_pointer>(__child);
1844 bool __inserted = false;
1845 if (__child == nullptr)
1846 {
1847 __node_holder __h = __construct_node(__v);
1848 __insert_node_at(__parent, __child, __h.get());
1849 __r = __h.release();
1850 __inserted = true;
1851 }
1852 return pair<iterator, bool>(iterator(__r), __inserted);
1853}
1854
1855template <class _Tp, class _Compare, class _Allocator>
1856typename __tree<_Tp, _Compare, _Allocator>::iterator
1857__tree<_Tp, _Compare, _Allocator>::__insert_unique(const_iterator __p, const value_type& __v)
1858{
1859 __node_base_pointer __parent;
1860 __node_base_pointer& __child = __find_equal(__p, __parent, __v);
1861 __node_pointer __r = static_cast<__node_pointer>(__child);
1862 if (__child == nullptr)
1863 {
1864 __node_holder __h = __construct_node(__v);
1865 __insert_node_at(__parent, __child, __h.get());
1866 __r = __h.release();
1867 }
1868 return iterator(__r);
1869}
1870
1871template <class _Tp, class _Compare, class _Allocator>
1872typename __tree<_Tp, _Compare, _Allocator>::iterator
1873__tree<_Tp, _Compare, _Allocator>::__insert_multi(const value_type& __v)
1874{
1875 __node_base_pointer __parent;
1876 __node_base_pointer& __child = __find_leaf_high(__parent, __v);
1877 __node_holder __h = __construct_node(__v);
1878 __insert_node_at(__parent, __child, __h.get());
1879 return iterator(__h.release());
1880}
1881
1882template <class _Tp, class _Compare, class _Allocator>
1883typename __tree<_Tp, _Compare, _Allocator>::iterator
1884__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const value_type& __v)
1885{
1886 __node_base_pointer __parent;
1887 __node_base_pointer& __child = __find_leaf(__p, __parent, __v);
1888 __node_holder __h = __construct_node(__v);
1889 __insert_node_at(__parent, __child, __h.get());
1890 return iterator(__h.release());
1891}
1892
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893template <class _Tp, class _Compare, class _Allocator>
1894pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1895__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
1896{
1897 __node_base_pointer __parent;
1898 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
1899 __node_pointer __r = static_cast<__node_pointer>(__child);
1900 bool __inserted = false;
1901 if (__child == nullptr)
1902 {
1903 __insert_node_at(__parent, __child, __nd);
1904 __r = __nd;
1905 __inserted = true;
1906 }
1907 return pair<iterator, bool>(iterator(__r), __inserted);
1908}
1909
1910template <class _Tp, class _Compare, class _Allocator>
1911typename __tree<_Tp, _Compare, _Allocator>::iterator
1912__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
1913 __node_pointer __nd)
1914{
1915 __node_base_pointer __parent;
1916 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
1917 __node_pointer __r = static_cast<__node_pointer>(__child);
1918 if (__child == nullptr)
1919 {
1920 __insert_node_at(__parent, __child, __nd);
1921 __r = __nd;
1922 }
1923 return iterator(__r);
1924}
1925
1926template <class _Tp, class _Compare, class _Allocator>
1927typename __tree<_Tp, _Compare, _Allocator>::iterator
1928__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
1929{
1930 __node_base_pointer __parent;
1931 __node_base_pointer& __child = __find_leaf_high(__parent, __nd->__value_);
1932 __insert_node_at(__parent, __child, __nd);
1933 return iterator(__nd);
1934}
1935
1936template <class _Tp, class _Compare, class _Allocator>
1937typename __tree<_Tp, _Compare, _Allocator>::iterator
1938__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
1939 __node_pointer __nd)
1940{
1941 __node_base_pointer __parent;
1942 __node_base_pointer& __child = __find_leaf(__p, __parent, __nd->__value_);
1943 __insert_node_at(__parent, __child, __nd);
1944 return iterator(__nd);
1945}
1946
1947template <class _Tp, class _Compare, class _Allocator>
1948typename __tree<_Tp, _Compare, _Allocator>::iterator
1949__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
1950{
1951 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
1952 iterator __r(__np);
1953 ++__r;
1954 if (__begin_node() == __np)
1955 __begin_node() = __r.__ptr_;
1956 --size();
1957 __node_allocator& __na = __node_alloc();
Howard Hinnant2529d022011-02-02 17:36:20 +00001958 __node_traits::destroy(__na, const_cast<value_type*>(_STD::addressof(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 __tree_remove(__end_node()->__left_,
1960 static_cast<__node_base_pointer>(__np));
1961 __node_traits::deallocate(__na, __np, 1);
1962 return __r;
1963}
1964
1965template <class _Tp, class _Compare, class _Allocator>
1966typename __tree<_Tp, _Compare, _Allocator>::iterator
1967__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
1968{
1969 while (__f != __l)
1970 __f = erase(__f);
1971 return iterator(const_cast<__node_pointer>(__l.__ptr_));
1972}
1973
1974template <class _Tp, class _Compare, class _Allocator>
1975template <class _Key>
1976typename __tree<_Tp, _Compare, _Allocator>::size_type
1977__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
1978{
1979 iterator __i = find(__k);
1980 if (__i == end())
1981 return 0;
1982 erase(__i);
1983 return 1;
1984}
1985
1986template <class _Tp, class _Compare, class _Allocator>
1987template <class _Key>
1988typename __tree<_Tp, _Compare, _Allocator>::size_type
1989__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
1990{
1991 pair<iterator, iterator> __p = __equal_range_multi(__k);
1992 size_type __r = 0;
1993 for (; __p.first != __p.second; ++__r)
1994 __p.first = erase(__p.first);
1995 return __r;
1996}
1997
1998template <class _Tp, class _Compare, class _Allocator>
1999template <class _Key>
2000typename __tree<_Tp, _Compare, _Allocator>::iterator
2001__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2002{
2003 iterator __p = __lower_bound(__v, __root(), __end_node());
2004 if (__p != end() && !value_comp()(__v, *__p))
2005 return __p;
2006 return end();
2007}
2008
2009template <class _Tp, class _Compare, class _Allocator>
2010template <class _Key>
2011typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2012__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2013{
2014 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2015 if (__p != end() && !value_comp()(__v, *__p))
2016 return __p;
2017 return end();
2018}
2019
2020template <class _Tp, class _Compare, class _Allocator>
2021template <class _Key>
2022typename __tree<_Tp, _Compare, _Allocator>::size_type
2023__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2024{
2025 __node_const_pointer __result = __end_node();
2026 __node_const_pointer __rt = __root();
2027 while (__rt != nullptr)
2028 {
2029 if (value_comp()(__k, __rt->__value_))
2030 {
2031 __result = __rt;
2032 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2033 }
2034 else if (value_comp()(__rt->__value_, __k))
2035 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2036 else
2037 return 1;
2038 }
2039 return 0;
2040}
2041
2042template <class _Tp, class _Compare, class _Allocator>
2043template <class _Key>
2044typename __tree<_Tp, _Compare, _Allocator>::size_type
2045__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2046{
2047 typedef pair<const_iterator, const_iterator> _P;
2048 __node_const_pointer __result = __end_node();
2049 __node_const_pointer __rt = __root();
2050 while (__rt != nullptr)
2051 {
2052 if (value_comp()(__k, __rt->__value_))
2053 {
2054 __result = __rt;
2055 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2056 }
2057 else if (value_comp()(__rt->__value_, __k))
2058 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2059 else
2060 return _STD::distance(
2061 __lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2062 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result)
2063 );
2064 }
2065 return 0;
2066}
2067
2068template <class _Tp, class _Compare, class _Allocator>
2069template <class _Key>
2070typename __tree<_Tp, _Compare, _Allocator>::iterator
2071__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2072 __node_pointer __root,
2073 __node_pointer __result)
2074{
2075 while (__root != nullptr)
2076 {
2077 if (!value_comp()(__root->__value_, __v))
2078 {
2079 __result = __root;
2080 __root = static_cast<__node_pointer>(__root->__left_);
2081 }
2082 else
2083 __root = static_cast<__node_pointer>(__root->__right_);
2084 }
2085 return iterator(__result);
2086}
2087
2088template <class _Tp, class _Compare, class _Allocator>
2089template <class _Key>
2090typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2091__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2092 __node_const_pointer __root,
2093 __node_const_pointer __result) const
2094{
2095 while (__root != nullptr)
2096 {
2097 if (!value_comp()(__root->__value_, __v))
2098 {
2099 __result = __root;
2100 __root = static_cast<__node_const_pointer>(__root->__left_);
2101 }
2102 else
2103 __root = static_cast<__node_const_pointer>(__root->__right_);
2104 }
2105 return const_iterator(__result);
2106}
2107
2108template <class _Tp, class _Compare, class _Allocator>
2109template <class _Key>
2110typename __tree<_Tp, _Compare, _Allocator>::iterator
2111__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2112 __node_pointer __root,
2113 __node_pointer __result)
2114{
2115 while (__root != nullptr)
2116 {
2117 if (value_comp()(__v, __root->__value_))
2118 {
2119 __result = __root;
2120 __root = static_cast<__node_pointer>(__root->__left_);
2121 }
2122 else
2123 __root = static_cast<__node_pointer>(__root->__right_);
2124 }
2125 return iterator(__result);
2126}
2127
2128template <class _Tp, class _Compare, class _Allocator>
2129template <class _Key>
2130typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2131__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2132 __node_const_pointer __root,
2133 __node_const_pointer __result) const
2134{
2135 while (__root != nullptr)
2136 {
2137 if (value_comp()(__v, __root->__value_))
2138 {
2139 __result = __root;
2140 __root = static_cast<__node_const_pointer>(__root->__left_);
2141 }
2142 else
2143 __root = static_cast<__node_const_pointer>(__root->__right_);
2144 }
2145 return const_iterator(__result);
2146}
2147
2148template <class _Tp, class _Compare, class _Allocator>
2149template <class _Key>
2150pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2151 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2152__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2153{
2154 typedef pair<iterator, iterator> _P;
2155 __node_pointer __result = __end_node();
2156 __node_pointer __rt = __root();
2157 while (__rt != nullptr)
2158 {
2159 if (value_comp()(__k, __rt->__value_))
2160 {
2161 __result = __rt;
2162 __rt = static_cast<__node_pointer>(__rt->__left_);
2163 }
2164 else if (value_comp()(__rt->__value_, __k))
2165 __rt = static_cast<__node_pointer>(__rt->__right_);
2166 else
2167 return _P(iterator(__rt),
2168 iterator(
2169 __rt->__right_ != nullptr ?
2170 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2171 : __result));
2172 }
2173 return _P(iterator(__result), iterator(__result));
2174}
2175
2176template <class _Tp, class _Compare, class _Allocator>
2177template <class _Key>
2178pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2179 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2180__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2181{
2182 typedef pair<const_iterator, const_iterator> _P;
2183 __node_const_pointer __result = __end_node();
2184 __node_const_pointer __rt = __root();
2185 while (__rt != nullptr)
2186 {
2187 if (value_comp()(__k, __rt->__value_))
2188 {
2189 __result = __rt;
2190 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2191 }
2192 else if (value_comp()(__rt->__value_, __k))
2193 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2194 else
2195 return _P(const_iterator(__rt),
2196 const_iterator(
2197 __rt->__right_ != nullptr ?
2198 static_cast<__node_const_pointer>(__tree_min(__rt->__right_))
2199 : __result));
2200 }
2201 return _P(const_iterator(__result), const_iterator(__result));
2202}
2203
2204template <class _Tp, class _Compare, class _Allocator>
2205template <class _Key>
2206pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2207 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2208__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2209{
2210 typedef pair<iterator, iterator> _P;
2211 __node_pointer __result = __end_node();
2212 __node_pointer __rt = __root();
2213 while (__rt != nullptr)
2214 {
2215 if (value_comp()(__k, __rt->__value_))
2216 {
2217 __result = __rt;
2218 __rt = static_cast<__node_pointer>(__rt->__left_);
2219 }
2220 else if (value_comp()(__rt->__value_, __k))
2221 __rt = static_cast<__node_pointer>(__rt->__right_);
2222 else
2223 return _P(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2224 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2225 }
2226 return _P(iterator(__result), iterator(__result));
2227}
2228
2229template <class _Tp, class _Compare, class _Allocator>
2230template <class _Key>
2231pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2232 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2233__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2234{
2235 typedef pair<const_iterator, const_iterator> _P;
2236 __node_const_pointer __result = __end_node();
2237 __node_const_pointer __rt = __root();
2238 while (__rt != nullptr)
2239 {
2240 if (value_comp()(__k, __rt->__value_))
2241 {
2242 __result = __rt;
2243 __rt = static_cast<__node_const_pointer>(__rt->__left_);
2244 }
2245 else if (value_comp()(__rt->__value_, __k))
2246 __rt = static_cast<__node_const_pointer>(__rt->__right_);
2247 else
2248 return _P(__lower_bound(__k, static_cast<__node_const_pointer>(__rt->__left_), __rt),
2249 __upper_bound(__k, static_cast<__node_const_pointer>(__rt->__right_), __result));
2250 }
2251 return _P(const_iterator(__result), const_iterator(__result));
2252}
2253
2254template <class _Tp, class _Compare, class _Allocator>
2255typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2256__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p)
2257{
2258 __node_pointer __np = const_cast<__node_pointer>(__p.__ptr_);
2259 if (__begin_node() == __np)
2260 {
2261 if (__np->__right_ != nullptr)
2262 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2263 else
2264 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2265 }
2266 --size();
2267 __tree_remove(__end_node()->__left_,
2268 static_cast<__node_base_pointer>(__np));
2269 return __node_holder(__np, _D(__node_alloc()));
2270}
2271
Howard Hinnant7686add2011-06-04 14:31:57 +00002272template <class _Tp, class _Compare, class _Allocator>
2273inline _LIBCPP_INLINE_VISIBILITY
2274void
2275swap(__tree<_Tp, _Compare, _Allocator>& __x,
2276 __tree<_Tp, _Compare, _Allocator>& __y)
2277 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2278{
2279 __x.swap(__y);
2280}
2281
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002282_LIBCPP_END_NAMESPACE_STD
2283
2284#endif // _LIBCPP___TREE