blob: 314d5eb023438dfeaa05398174dc3a5f31bc7914 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP___TREE
12#define _LIBCPP___TREE
13
14#include <__config>
15#include <iterator>
16#include <memory>
17#include <stdexcept>
18#include <algorithm>
19
Howard Hinnant08e17472011-10-17 20:05:10 +000020#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000022#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023
24_LIBCPP_BEGIN_NAMESPACE_STD
25
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000026template <class _Tp, class _Compare, class _Allocator> class __tree;
27template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000028 class _LIBCPP_TYPE_VIS_ONLY __tree_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000029template <class _Tp, class _ConstNodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +000030 class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000031
Eric Fiselier55263482016-02-20 05:28:30 +000032template <class _Pointer> class __tree_end_node;
33template <class _VoidPtr> class __tree_node_base;
34template <class _Tp, class _VoidPtr> class __tree_node;
35
36#ifndef _LIBCPP_CXX03_LANG
37template <class _Key, class _Value>
38union __value_type;
39#else
40template <class _Key, class _Value>
41struct __value_type;
42#endif
43
44template <class _Allocator> class __map_node_destructor;
45template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
46template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
47
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048/*
49
50_NodePtr algorithms
51
52The algorithms taking _NodePtr are red black tree algorithms. Those
53algorithms taking a parameter named __root should assume that __root
54points to a proper red black tree (unless otherwise specified).
55
56Each algorithm herein assumes that __root->__parent_ points to a non-null
57structure which has a member __left_ which points back to __root. No other
58member is read or written to at __root->__parent_.
59
60__root->__parent_ will be referred to below (in comments only) as end_node.
61end_node->__left_ is an externably accessible lvalue for __root, and can be
62changed by node insertion and removal (without explicit reference to end_node).
63
64All nodes (with the exception of end_node), even the node referred to as
65__root, have a non-null __parent_ field.
66
67*/
68
69// Returns: true if __x is a left child of its parent, else false
70// Precondition: __x != nullptr.
71template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +000072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073bool
Howard Hinnant8b537682011-06-04 17:10:24 +000074__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075{
76 return __x == __x->__parent_->__left_;
77}
78
79// Determintes if the subtree rooted at __x is a proper red black subtree. If
80// __x is a proper subtree, returns the black height (null counts as 1). If
81// __x is an improper subtree, returns 0.
82template <class _NodePtr>
83unsigned
84__tree_sub_invariant(_NodePtr __x)
85{
86 if (__x == nullptr)
87 return 1;
88 // parent consistency checked by caller
89 // check __x->__left_ consistency
90 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
91 return 0;
92 // check __x->__right_ consistency
93 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
94 return 0;
95 // check __x->__left_ != __x->__right_ unless both are nullptr
96 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
97 return 0;
98 // If this is red, neither child can be red
99 if (!__x->__is_black_)
100 {
101 if (__x->__left_ && !__x->__left_->__is_black_)
102 return 0;
103 if (__x->__right_ && !__x->__right_->__is_black_)
104 return 0;
105 }
106 unsigned __h = __tree_sub_invariant(__x->__left_);
107 if (__h == 0)
108 return 0; // invalid left subtree
109 if (__h != __tree_sub_invariant(__x->__right_))
110 return 0; // invalid or different height right subtree
111 return __h + __x->__is_black_; // return black height of this node
112}
113
114// Determintes if the red black tree rooted at __root is a proper red black tree.
115// __root == nullptr is a proper tree. Returns true is __root is a proper
116// red black tree, else returns false.
117template <class _NodePtr>
118bool
119__tree_invariant(_NodePtr __root)
120{
121 if (__root == nullptr)
122 return true;
123 // check __x->__parent_ consistency
124 if (__root->__parent_ == nullptr)
125 return false;
126 if (!__tree_is_left_child(__root))
127 return false;
128 // root must be black
129 if (!__root->__is_black_)
130 return false;
131 // do normal node checks
132 return __tree_sub_invariant(__root) != 0;
133}
134
135// Returns: pointer to the left-most node under __x.
136// Precondition: __x != nullptr.
137template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000138inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000139_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000140__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141{
142 while (__x->__left_ != nullptr)
143 __x = __x->__left_;
144 return __x;
145}
146
147// Returns: pointer to the right-most node under __x.
148// Precondition: __x != nullptr.
149template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000150inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000151_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000152__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153{
154 while (__x->__right_ != nullptr)
155 __x = __x->__right_;
156 return __x;
157}
158
159// Returns: pointer to the next in-order node after __x.
160// Precondition: __x != nullptr.
161template <class _NodePtr>
162_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000163__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000164{
165 if (__x->__right_ != nullptr)
166 return __tree_min(__x->__right_);
167 while (!__tree_is_left_child(__x))
168 __x = __x->__parent_;
169 return __x->__parent_;
170}
171
172// Returns: pointer to the previous in-order node before __x.
173// Precondition: __x != nullptr.
174template <class _NodePtr>
175_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000176__tree_prev(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000177{
178 if (__x->__left_ != nullptr)
179 return __tree_max(__x->__left_);
180 while (__tree_is_left_child(__x))
181 __x = __x->__parent_;
182 return __x->__parent_;
183}
184
185// Returns: pointer to a node which has no children
186// Precondition: __x != nullptr.
187template <class _NodePtr>
188_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000189__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190{
191 while (true)
192 {
193 if (__x->__left_ != nullptr)
194 {
195 __x = __x->__left_;
196 continue;
197 }
198 if (__x->__right_ != nullptr)
199 {
200 __x = __x->__right_;
201 continue;
202 }
203 break;
204 }
205 return __x;
206}
207
208// Effects: Makes __x->__right_ the subtree root with __x as its left child
209// while preserving in-order order.
210// Precondition: __x->__right_ != nullptr
211template <class _NodePtr>
212void
Howard Hinnant8b537682011-06-04 17:10:24 +0000213__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000214{
215 _NodePtr __y = __x->__right_;
216 __x->__right_ = __y->__left_;
217 if (__x->__right_ != nullptr)
218 __x->__right_->__parent_ = __x;
219 __y->__parent_ = __x->__parent_;
220 if (__tree_is_left_child(__x))
221 __x->__parent_->__left_ = __y;
222 else
223 __x->__parent_->__right_ = __y;
224 __y->__left_ = __x;
225 __x->__parent_ = __y;
226}
227
228// Effects: Makes __x->__left_ the subtree root with __x as its right child
229// while preserving in-order order.
230// Precondition: __x->__left_ != nullptr
231template <class _NodePtr>
232void
Howard Hinnant8b537682011-06-04 17:10:24 +0000233__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000234{
235 _NodePtr __y = __x->__left_;
236 __x->__left_ = __y->__right_;
237 if (__x->__left_ != nullptr)
238 __x->__left_->__parent_ = __x;
239 __y->__parent_ = __x->__parent_;
240 if (__tree_is_left_child(__x))
241 __x->__parent_->__left_ = __y;
242 else
243 __x->__parent_->__right_ = __y;
244 __y->__right_ = __x;
245 __x->__parent_ = __y;
246}
247
248// Effects: Rebalances __root after attaching __x to a leaf.
249// Precondition: __root != nulptr && __x != nullptr.
250// __x has no children.
251// __x == __root or == a direct or indirect child of __root.
252// If __x were to be unlinked from __root (setting __root to
253// nullptr if __root == __x), __tree_invariant(__root) == true.
254// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
255// may be different than the value passed in as __root.
256template <class _NodePtr>
257void
Howard Hinnant8b537682011-06-04 17:10:24 +0000258__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259{
260 __x->__is_black_ = __x == __root;
261 while (__x != __root && !__x->__parent_->__is_black_)
262 {
263 // __x->__parent_ != __root because __x->__parent_->__is_black == false
264 if (__tree_is_left_child(__x->__parent_))
265 {
266 _NodePtr __y = __x->__parent_->__parent_->__right_;
267 if (__y != nullptr && !__y->__is_black_)
268 {
269 __x = __x->__parent_;
270 __x->__is_black_ = true;
271 __x = __x->__parent_;
272 __x->__is_black_ = __x == __root;
273 __y->__is_black_ = true;
274 }
275 else
276 {
277 if (!__tree_is_left_child(__x))
278 {
279 __x = __x->__parent_;
280 __tree_left_rotate(__x);
281 }
282 __x = __x->__parent_;
283 __x->__is_black_ = true;
284 __x = __x->__parent_;
285 __x->__is_black_ = false;
286 __tree_right_rotate(__x);
287 break;
288 }
289 }
290 else
291 {
292 _NodePtr __y = __x->__parent_->__parent_->__left_;
293 if (__y != nullptr && !__y->__is_black_)
294 {
295 __x = __x->__parent_;
296 __x->__is_black_ = true;
297 __x = __x->__parent_;
298 __x->__is_black_ = __x == __root;
299 __y->__is_black_ = true;
300 }
301 else
302 {
303 if (__tree_is_left_child(__x))
304 {
305 __x = __x->__parent_;
306 __tree_right_rotate(__x);
307 }
308 __x = __x->__parent_;
309 __x->__is_black_ = true;
310 __x = __x->__parent_;
311 __x->__is_black_ = false;
312 __tree_left_rotate(__x);
313 break;
314 }
315 }
316 }
317}
318
319// Precondition: __root != nullptr && __z != nullptr.
320// __tree_invariant(__root) == true.
321// __z == __root or == a direct or indirect child of __root.
322// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
323// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
324// nor any of its children refer to __z. end_node->__left_
325// may be different than the value passed in as __root.
326template <class _NodePtr>
327void
Howard Hinnant8b537682011-06-04 17:10:24 +0000328__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329{
330 // __z will be removed from the tree. Client still needs to destruct/deallocate it
331 // __y is either __z, or if __z has two children, __tree_next(__z).
332 // __y will have at most one child.
333 // __y will be the initial hole in the tree (make the hole at a leaf)
334 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
335 __z : __tree_next(__z);
336 // __x is __y's possibly null single child
337 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
338 // __w is __x's possibly null uncle (will become __x's sibling)
339 _NodePtr __w = nullptr;
340 // link __x to __y's parent, and find __w
341 if (__x != nullptr)
342 __x->__parent_ = __y->__parent_;
343 if (__tree_is_left_child(__y))
344 {
345 __y->__parent_->__left_ = __x;
346 if (__y != __root)
347 __w = __y->__parent_->__right_;
348 else
349 __root = __x; // __w == nullptr
350 }
351 else
352 {
353 __y->__parent_->__right_ = __x;
354 // __y can't be root if it is a right child
355 __w = __y->__parent_->__left_;
356 }
357 bool __removed_black = __y->__is_black_;
358 // If we didn't remove __z, do so now by splicing in __y for __z,
359 // but copy __z's color. This does not impact __x or __w.
360 if (__y != __z)
361 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000362 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000363 __y->__parent_ = __z->__parent_;
364 if (__tree_is_left_child(__z))
365 __y->__parent_->__left_ = __y;
366 else
367 __y->__parent_->__right_ = __y;
368 __y->__left_ = __z->__left_;
369 __y->__left_->__parent_ = __y;
370 __y->__right_ = __z->__right_;
371 if (__y->__right_ != nullptr)
372 __y->__right_->__parent_ = __y;
373 __y->__is_black_ = __z->__is_black_;
374 if (__root == __z)
375 __root = __y;
376 }
377 // There is no need to rebalance if we removed a red, or if we removed
378 // the last node.
379 if (__removed_black && __root != nullptr)
380 {
381 // Rebalance:
382 // __x has an implicit black color (transferred from the removed __y)
383 // associated with it, no matter what its color is.
384 // If __x is __root (in which case it can't be null), it is supposed
385 // to be black anyway, and if it is doubly black, then the double
386 // can just be ignored.
387 // If __x is red (in which case it can't be null), then it can absorb
388 // the implicit black just by setting its color to black.
389 // Since __y was black and only had one child (which __x points to), __x
390 // is either red with no children, else null, otherwise __y would have
391 // different black heights under left and right pointers.
392 // if (__x == __root || __x != nullptr && !__x->__is_black_)
393 if (__x != nullptr)
394 __x->__is_black_ = true;
395 else
396 {
397 // Else __x isn't root, and is "doubly black", even though it may
398 // be null. __w can not be null here, else the parent would
399 // see a black height >= 2 on the __x side and a black height
400 // of 1 on the __w side (__w must be a non-null black or a red
401 // with a non-null black child).
402 while (true)
403 {
404 if (!__tree_is_left_child(__w)) // if x is left child
405 {
406 if (!__w->__is_black_)
407 {
408 __w->__is_black_ = true;
409 __w->__parent_->__is_black_ = false;
410 __tree_left_rotate(__w->__parent_);
411 // __x is still valid
412 // reset __root only if necessary
413 if (__root == __w->__left_)
414 __root = __w;
415 // reset sibling, and it still can't be null
416 __w = __w->__left_->__right_;
417 }
418 // __w->__is_black_ is now true, __w may have null children
419 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
420 (__w->__right_ == nullptr || __w->__right_->__is_black_))
421 {
422 __w->__is_black_ = false;
423 __x = __w->__parent_;
424 // __x can no longer be null
425 if (__x == __root || !__x->__is_black_)
426 {
427 __x->__is_black_ = true;
428 break;
429 }
430 // reset sibling, and it still can't be null
431 __w = __tree_is_left_child(__x) ?
Howard Hinnant324bb032010-08-22 00:02:43 +0000432 __x->__parent_->__right_ :
433 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 // continue;
435 }
436 else // __w has a red child
437 {
438 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
439 {
440 // __w left child is non-null and red
441 __w->__left_->__is_black_ = true;
442 __w->__is_black_ = false;
443 __tree_right_rotate(__w);
444 // __w is known not to be root, so root hasn't changed
445 // reset sibling, and it still can't be null
446 __w = __w->__parent_;
447 }
448 // __w has a right red child, left child may be null
449 __w->__is_black_ = __w->__parent_->__is_black_;
450 __w->__parent_->__is_black_ = true;
451 __w->__right_->__is_black_ = true;
452 __tree_left_rotate(__w->__parent_);
453 break;
454 }
455 }
456 else
457 {
458 if (!__w->__is_black_)
459 {
460 __w->__is_black_ = true;
461 __w->__parent_->__is_black_ = false;
462 __tree_right_rotate(__w->__parent_);
463 // __x is still valid
464 // reset __root only if necessary
465 if (__root == __w->__right_)
466 __root = __w;
467 // reset sibling, and it still can't be null
468 __w = __w->__right_->__left_;
469 }
470 // __w->__is_black_ is now true, __w may have null children
471 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
472 (__w->__right_ == nullptr || __w->__right_->__is_black_))
473 {
474 __w->__is_black_ = false;
475 __x = __w->__parent_;
476 // __x can no longer be null
477 if (!__x->__is_black_ || __x == __root)
478 {
479 __x->__is_black_ = true;
480 break;
481 }
482 // reset sibling, and it still can't be null
483 __w = __tree_is_left_child(__x) ?
Howard Hinnant324bb032010-08-22 00:02:43 +0000484 __x->__parent_->__right_ :
485 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486 // continue;
487 }
488 else // __w has a red child
489 {
490 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
491 {
492 // __w right child is non-null and red
493 __w->__right_->__is_black_ = true;
494 __w->__is_black_ = false;
495 __tree_left_rotate(__w);
496 // __w is known not to be root, so root hasn't changed
497 // reset sibling, and it still can't be null
498 __w = __w->__parent_;
499 }
500 // __w has a left red child, right child may be null
501 __w->__is_black_ = __w->__parent_->__is_black_;
502 __w->__parent_->__is_black_ = true;
503 __w->__left_->__is_black_ = true;
504 __tree_right_rotate(__w->__parent_);
505 break;
506 }
507 }
508 }
509 }
510 }
511}
512
Eric Fiselier55263482016-02-20 05:28:30 +0000513// node traits
514
Eric Fiselierdb215062016-03-31 02:15:15 +0000515
516#ifndef _LIBCPP_CXX03_LANG
517template <class _Tp>
518struct __is_tree_value_type_imp : false_type {};
519
520template <class _Key, class _Value>
521struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
522
523template <class ..._Args>
524struct __is_tree_value_type : false_type {};
525
526template <class _One>
527struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
528#endif
529
Eric Fiselier55263482016-02-20 05:28:30 +0000530template <class _Tp>
531struct __tree_key_value_types {
532 typedef _Tp key_type;
533 typedef _Tp __node_value_type;
534 typedef _Tp __container_value_type;
535 static const bool __is_map = false;
Eric Fiselierdb215062016-03-31 02:15:15 +0000536
537 _LIBCPP_INLINE_VISIBILITY
538 static key_type const& __get_key(_Tp const& __v) {
539 return __v;
540 }
541 _LIBCPP_INLINE_VISIBILITY
542 static __container_value_type const& __get_value(__node_value_type const& __v) {
543 return __v;
544 }
545 _LIBCPP_INLINE_VISIBILITY
546 static __container_value_type* __get_ptr(__node_value_type& __n) {
547 return _VSTD::addressof(__n);
548 }
549
550#ifndef _LIBCPP_CXX03_LANG
551 _LIBCPP_INLINE_VISIBILITY
552 static __container_value_type&& __move(__node_value_type& __v) {
553 return _VSTD::move(__v);
554 }
555#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000556};
557
558template <class _Key, class _Tp>
559struct __tree_key_value_types<__value_type<_Key, _Tp> > {
560 typedef _Key key_type;
561 typedef _Tp mapped_type;
562 typedef __value_type<_Key, _Tp> __node_value_type;
563 typedef pair<const _Key, _Tp> __container_value_type;
564 typedef pair<_Key, _Tp> __nc_value_type;
565 typedef __container_value_type __map_value_type;
566 static const bool __is_map = true;
Eric Fiselierdb215062016-03-31 02:15:15 +0000567
568 _LIBCPP_INLINE_VISIBILITY
569 static key_type const&
570 __get_key(__node_value_type const& __t) {
571 return __t.__cc.first;
572 }
573
574 template <class _Up>
575 _LIBCPP_INLINE_VISIBILITY
576 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
577 key_type const&>::type
578 __get_key(_Up& __t) {
579 return __t.first;
580 }
581
582 _LIBCPP_INLINE_VISIBILITY
583 static __container_value_type const&
584 __get_value(__node_value_type const& __t) {
585 return __t.__cc;
586 }
587
588 template <class _Up>
589 _LIBCPP_INLINE_VISIBILITY
590 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
591 __container_value_type const&>::type
592 __get_value(_Up& __t) {
593 return __t;
594 }
595
596 _LIBCPP_INLINE_VISIBILITY
597 static __container_value_type* __get_ptr(__node_value_type& __n) {
598 return _VSTD::addressof(__n.__cc);
599 }
600
601#ifndef _LIBCPP_CXX03_LANG
602 _LIBCPP_INLINE_VISIBILITY
603 static __nc_value_type&& __move(__node_value_type& __v) {
604 return _VSTD::move(__v.__nc);
605 }
606#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000607};
608
609template <class _VoidPtr>
610struct __tree_node_base_types {
611 typedef _VoidPtr __void_pointer;
612
613 typedef __tree_node_base<__void_pointer> __node_base_type;
614 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
615 __node_base_pointer;
616
617 typedef __tree_end_node<__node_base_pointer> __end_node_type;
618 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
619 __end_node_pointer;
620private:
621 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
622 "_VoidPtr does not point to unqualified void type");
623};
624
625template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
626 bool = _KVTypes::__is_map>
627struct __tree_map_pointer_types {};
628
629template <class _Tp, class _AllocPtr, class _KVTypes>
630struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
631 typedef typename _KVTypes::__map_value_type _Mv;
632 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
633 __map_value_type_pointer;
634 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
635 __const_map_value_type_pointer;
636};
637
638template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
639struct __tree_node_types;
640
641template <class _NodePtr, class _Tp, class _VoidPtr>
642struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
643 : public __tree_node_base_types<_VoidPtr>,
644 __tree_key_value_types<_Tp>,
645 __tree_map_pointer_types<_Tp, _VoidPtr>
646{
647 typedef __tree_node_base_types<_VoidPtr> __base;
648 typedef __tree_key_value_types<_Tp> __key_base;
649 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
650public:
651
652 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
653 typedef _NodePtr __node_pointer;
654
655 typedef _Tp __node_value_type;
656 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
657 __node_value_type_pointer;
658 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
659 __const_node_value_type_pointer;
660private:
661 static_assert(!is_const<__node_type>::value,
662 "_NodePtr should never be a pointer to const");
663 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
664 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
665};
666
667template <class _ValueTp, class _VoidPtr>
668struct __make_tree_node_types {
669 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
670 _NodePtr;
671 typedef __tree_node_types<_NodePtr> type;
672};
673
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674// node
675
676template <class _Pointer>
677class __tree_end_node
678{
679public:
680 typedef _Pointer pointer;
681 pointer __left_;
682
Howard Hinnant333f50d2010-09-21 20:16:37 +0000683 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000684 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000685};
686
687template <class _VoidPtr>
688class __tree_node_base
Eric Fiselier55263482016-02-20 05:28:30 +0000689 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000690{
Eric Fiselier55263482016-02-20 05:28:30 +0000691 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
692
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693public:
Eric Fiselier55263482016-02-20 05:28:30 +0000694 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000695
696 pointer __right_;
697 pointer __parent_;
698 bool __is_black_;
699
Eric Fiselierdb215062016-03-31 02:15:15 +0000700private:
701 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
702 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
703 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000704};
705
706template <class _Tp, class _VoidPtr>
707class __tree_node
708 : public __tree_node_base<_VoidPtr>
709{
710public:
Eric Fiselier55263482016-02-20 05:28:30 +0000711 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712
Eric Fiselier55263482016-02-20 05:28:30 +0000713 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714
Eric Fiselierdb215062016-03-31 02:15:15 +0000715private:
716 ~__tree_node() _LIBCPP_EQUAL_DELETE;
717 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
718 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000719};
720
Eric Fiselierdb215062016-03-31 02:15:15 +0000721
722template <class _Allocator>
723class __tree_node_destructor
724{
725 typedef _Allocator allocator_type;
726 typedef allocator_traits<allocator_type> __alloc_traits;
727
728public:
729 typedef typename __alloc_traits::pointer pointer;
730private:
731 typedef __tree_node_types<pointer> _NodeTypes;
732 allocator_type& __na_;
733
734 __tree_node_destructor& operator=(const __tree_node_destructor&);
735
736public:
737 bool __value_constructed;
738
739 _LIBCPP_INLINE_VISIBILITY
740 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
741 : __na_(__na),
742 __value_constructed(__val)
743 {}
744
745 _LIBCPP_INLINE_VISIBILITY
746 void operator()(pointer __p) _NOEXCEPT
747 {
748 if (__value_constructed)
749 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
750 if (__p)
751 __alloc_traits::deallocate(__na_, __p, 1);
752 }
753
754 template <class> friend class __map_node_destructor;
755};
756
757
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000758template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000759class _LIBCPP_TYPE_VIS_ONLY __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000760{
Eric Fiselier55263482016-02-20 05:28:30 +0000761 typedef __tree_node_types<_NodePtr> _NodeTypes;
762 typedef _NodePtr __node_pointer;
763 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
764 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765
766 __node_pointer __ptr_;
767
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768public:
Eric Fiselier55263482016-02-20 05:28:30 +0000769 typedef bidirectional_iterator_tag iterator_category;
770 typedef _Tp value_type;
771 typedef _DiffType difference_type;
772 typedef value_type& reference;
773 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000774
Marshall Clow051c8482013-08-08 21:52:50 +0000775 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
776#if _LIBCPP_STD_VER > 11
777 : __ptr_(nullptr)
778#endif
779 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000780
Howard Hinnant333f50d2010-09-21 20:16:37 +0000781 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000782 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
783 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000784
Howard Hinnant333f50d2010-09-21 20:16:37 +0000785 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000786 __tree_iterator& operator++() {
787 __ptr_ = static_cast<__node_pointer>(
Eric Fiselier55263482016-02-20 05:28:30 +0000788 __tree_next(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000789 return *this;
790 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000792 __tree_iterator operator++(int)
793 {__tree_iterator __t(*this); ++(*this); return __t;}
794
Howard Hinnant333f50d2010-09-21 20:16:37 +0000795 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000796 __tree_iterator& operator--() {
797 __ptr_ = static_cast<__node_pointer>(
Eric Fiselier55263482016-02-20 05:28:30 +0000798 __tree_prev(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000799 return *this;
800 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000801 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000802 __tree_iterator operator--(int)
803 {__tree_iterator __t(*this); --(*this); return __t;}
804
Howard Hinnant333f50d2010-09-21 20:16:37 +0000805 friend _LIBCPP_INLINE_VISIBILITY
806 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000808 friend _LIBCPP_INLINE_VISIBILITY
809 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 {return !(__x == __y);}
811
812private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000813 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000814 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000816 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
817 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
818 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
819 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
820 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
821 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000822};
823
Eric Fiselier55263482016-02-20 05:28:30 +0000824template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000825class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826{
Eric Fiselier55263482016-02-20 05:28:30 +0000827 typedef __tree_node_types<_NodePtr> _NodeTypes;
828 typedef typename _NodeTypes::__node_pointer __node_pointer;
829 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
830 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831
832 __node_pointer __ptr_;
833
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834public:
Eric Fiselier55263482016-02-20 05:28:30 +0000835 typedef bidirectional_iterator_tag iterator_category;
836 typedef _Tp value_type;
837 typedef _DiffType difference_type;
838 typedef const value_type& reference;
839 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000840
Marshall Clow051c8482013-08-08 21:52:50 +0000841 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
842#if _LIBCPP_STD_VER > 11
843 : __ptr_(nullptr)
844#endif
845 {}
846
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000847private:
Eric Fiselier55263482016-02-20 05:28:30 +0000848 typedef __tree_iterator<value_type, __node_pointer, difference_type>
849 __non_const_iterator;
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 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
853 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854
Howard Hinnant333f50d2010-09-21 20:16:37 +0000855 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000856 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
857 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858
Howard Hinnant333f50d2010-09-21 20:16:37 +0000859 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000860 __tree_const_iterator& operator++() {
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000861 __ptr_ = static_cast<__node_pointer>(
862 __tree_next(static_cast<__node_base_pointer>(__ptr_)));
863 return *this;
864 }
865
Howard Hinnant333f50d2010-09-21 20:16:37 +0000866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867 __tree_const_iterator operator++(int)
868 {__tree_const_iterator __t(*this); ++(*this); return __t;}
869
Howard Hinnant333f50d2010-09-21 20:16:37 +0000870 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000871 __tree_const_iterator& operator--() {
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000872 __ptr_ = static_cast<__node_pointer>(
873 __tree_prev(static_cast<__node_base_pointer>(__ptr_)));
874 return *this;
875 }
876
Howard Hinnant333f50d2010-09-21 20:16:37 +0000877 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878 __tree_const_iterator operator--(int)
879 {__tree_const_iterator __t(*this); --(*this); return __t;}
880
Howard Hinnant333f50d2010-09-21 20:16:37 +0000881 friend _LIBCPP_INLINE_VISIBILITY
882 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000883 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000884 friend _LIBCPP_INLINE_VISIBILITY
885 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 {return !(__x == __y);}
887
888private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000889 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000890 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
891 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000893 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
894 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
895 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
896 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
897 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898};
899
900template <class _Tp, class _Compare, class _Allocator>
901class __tree
902{
903public:
904 typedef _Tp value_type;
905 typedef _Compare value_compare;
906 typedef _Allocator allocator_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000907
908private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000909 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier55263482016-02-20 05:28:30 +0000910 typedef typename __make_tree_node_types<value_type,
911 typename __alloc_traits::void_pointer>::type
912 _NodeTypes;
Eric Fiselierdb215062016-03-31 02:15:15 +0000913 typedef typename _NodeTypes::key_type key_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000914public:
915 typedef typename _NodeTypes::__node_value_type __node_value_type;
916 typedef typename _NodeTypes::__container_value_type __container_value_type;
917
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000918 typedef typename __alloc_traits::pointer pointer;
919 typedef typename __alloc_traits::const_pointer const_pointer;
920 typedef typename __alloc_traits::size_type size_type;
921 typedef typename __alloc_traits::difference_type difference_type;
922
Eric Fiselier55263482016-02-20 05:28:30 +0000923public:
924 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000925
Eric Fiselier55263482016-02-20 05:28:30 +0000926 typedef typename _NodeTypes::__node_type __node;
927 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000928
929 typedef typename _NodeTypes::__node_base_type __node_base;
930 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000931
932 typedef typename _NodeTypes::__end_node_type __end_node_t;
933 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiselier55263482016-02-20 05:28:30 +0000934
Marshall Clow66302c62015-04-07 05:21:38 +0000935 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiselier55263482016-02-20 05:28:30 +0000936 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937
Eric Fiselier55263482016-02-20 05:28:30 +0000938private:
939 // check for sane allocator pointer rebinding semantics. Rebinding the
940 // allocator for a new pointer type should be exactly the same as rebinding
941 // the pointer using 'pointer_traits'.
942 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
943 "Allocator does not rebind pointers in a sane manner.");
944 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
945 __node_base_allocator;
946 typedef allocator_traits<__node_base_allocator> __node_base_traits;
947 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
948 "Allocator does not rebind pointers in a sane manner.");
949
950private:
Eric Fiselier227b47c2016-02-20 07:12:17 +0000951 __node_pointer __begin_node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000952 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
953 __compressed_pair<size_type, value_compare> __pair3_;
954
955public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000956 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000957 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000958 {
Eric Fiselier227b47c2016-02-20 07:12:17 +0000959 return static_cast<__node_pointer>(
960 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
961 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000962 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000963 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +0000964 __node_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000965 {
Eric Fiselier227b47c2016-02-20 07:12:17 +0000966 return static_cast<__node_pointer>(
967 pointer_traits<__end_node_ptr>::pointer_to(
968 const_cast<__end_node_t&>(__pair1_.first())
969 )
970 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000971 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000972 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000973 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000974private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000975 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000976 const __node_allocator& __node_alloc() const _NOEXCEPT
977 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000978 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000979 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000981 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000984 allocator_type __alloc() const _NOEXCEPT
985 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000986private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000987 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000988 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000989public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000990 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000991 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000992 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000993 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000994 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000995 const value_compare& value_comp() const _NOEXCEPT
996 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997public:
Eric Fiselier227b47c2016-02-20 07:12:17 +0000998
Howard Hinnant333f50d2010-09-21 20:16:37 +0000999 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +00001000 __node_pointer __root() const _NOEXCEPT
1001 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001002
1003 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +00001004 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005
Howard Hinnant7686add2011-06-04 14:31:57 +00001006 explicit __tree(const value_compare& __comp)
1007 _NOEXCEPT_(
1008 is_nothrow_default_constructible<__node_allocator>::value &&
1009 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010 explicit __tree(const allocator_type& __a);
1011 __tree(const value_compare& __comp, const allocator_type& __a);
1012 __tree(const __tree& __t);
1013 __tree& operator=(const __tree& __t);
1014 template <class _InputIterator>
1015 void __assign_unique(_InputIterator __first, _InputIterator __last);
1016 template <class _InputIterator>
1017 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001018#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-06-04 14:31:57 +00001019 __tree(__tree&& __t)
1020 _NOEXCEPT_(
1021 is_nothrow_move_constructible<__node_allocator>::value &&
1022 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001023 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +00001024 __tree& operator=(__tree&& __t)
1025 _NOEXCEPT_(
1026 __node_traits::propagate_on_container_move_assignment::value &&
1027 is_nothrow_move_assignable<value_compare>::value &&
1028 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001029#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001030
1031 ~__tree();
1032
Howard Hinnant333f50d2010-09-21 20:16:37 +00001033 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001034 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001035 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001036 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001037 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001038 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001040 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001041
Howard Hinnant333f50d2010-09-21 20:16:37 +00001042 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001043 size_type max_size() const _NOEXCEPT
1044 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001045
Howard Hinnant7686add2011-06-04 14:31:57 +00001046 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001047
Howard Hinnant7686add2011-06-04 14:31:57 +00001048 void swap(__tree& __t)
1049 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001050 __is_nothrow_swappable<value_compare>::value
1051#if _LIBCPP_STD_VER <= 11
1052 && (!__node_traits::propagate_on_container_swap::value ||
1053 __is_nothrow_swappable<__node_allocator>::value)
1054#endif
1055 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001056
Eric Fiselierdb215062016-03-31 02:15:15 +00001057
1058#ifndef _LIBCPP_CXX03_LANG
1059 template <class _Key, class ..._Args>
1060 pair<iterator, bool>
1061 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1062 template <class _Key, class ..._Args>
1063 iterator
1064 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065
1066 template <class... _Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001067 pair<iterator, bool> __emplace_unique(_Args&&... __args);
1068
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 template <class... _Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001070 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001071
Eric Fiselierdb215062016-03-31 02:15:15 +00001072 template <class... _Args>
1073 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074
Eric Fiselierdb215062016-03-31 02:15:15 +00001075 template <class... _Args>
1076 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1077#else
1078 template <class _Key, class _Args>
1079 _LIBCPP_INLINE_VISIBILITY
1080 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1081 template <class _Key, class _Args>
1082 _LIBCPP_INLINE_VISIBILITY
1083 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001084#endif
1085
Eric Fiselierdb215062016-03-31 02:15:15 +00001086 _LIBCPP_INLINE_VISIBILITY
1087 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1088 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1089 }
1090
1091 _LIBCPP_INLINE_VISIBILITY
1092 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1093 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1094 }
1095
1096#ifdef _LIBCPP_CXX03_LANG
1097 _LIBCPP_INLINE_VISIBILITY
1098 iterator __insert_multi(const __container_value_type& __v);
1099 _LIBCPP_INLINE_VISIBILITY
1100 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1101#else
1102 _LIBCPP_INLINE_VISIBILITY
1103 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1104 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1105 }
1106
1107 _LIBCPP_INLINE_VISIBILITY
1108 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1109 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1110 }
1111
1112 template <class _Vp, class = typename enable_if<
1113 !is_same<typename __unconstref<_Vp>::type,
1114 __container_value_type
1115 >::value
1116 >::type>
1117 _LIBCPP_INLINE_VISIBILITY
1118 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1119 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1120 }
1121
1122 template <class _Vp, class = typename enable_if<
1123 !is_same<typename __unconstref<_Vp>::type,
1124 __container_value_type
1125 >::value
1126 >::type>
1127 _LIBCPP_INLINE_VISIBILITY
1128 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1129 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1130 }
1131
1132 _LIBCPP_INLINE_VISIBILITY
1133 iterator __insert_multi(__container_value_type&& __v) {
1134 return __emplace_multi(_VSTD::move(__v));
1135 }
1136
1137 _LIBCPP_INLINE_VISIBILITY
1138 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1139 return __emplace_hint_multi(__p, _VSTD::move(__v));
1140 }
1141
1142 template <class _Vp>
1143 _LIBCPP_INLINE_VISIBILITY
1144 iterator __insert_multi(_Vp&& __v) {
1145 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1146 }
1147
1148 template <class _Vp>
1149 _LIBCPP_INLINE_VISIBILITY
1150 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1151 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1152 }
1153
1154#endif // !_LIBCPP_CXX03_LANG
1155
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001156 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1157 iterator __node_insert_unique(const_iterator __p,
1158 __node_pointer __nd);
1159
1160 iterator __node_insert_multi(__node_pointer __nd);
1161 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1162
1163 iterator erase(const_iterator __p);
1164 iterator erase(const_iterator __f, const_iterator __l);
1165 template <class _Key>
1166 size_type __erase_unique(const _Key& __k);
1167 template <class _Key>
1168 size_type __erase_multi(const _Key& __k);
1169
1170 void __insert_node_at(__node_base_pointer __parent,
1171 __node_base_pointer& __child,
1172 __node_base_pointer __new_node);
1173
1174 template <class _Key>
1175 iterator find(const _Key& __v);
1176 template <class _Key>
1177 const_iterator find(const _Key& __v) const;
1178
1179 template <class _Key>
1180 size_type __count_unique(const _Key& __k) const;
1181 template <class _Key>
1182 size_type __count_multi(const _Key& __k) const;
1183
1184 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001185 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001186 iterator lower_bound(const _Key& __v)
1187 {return __lower_bound(__v, __root(), __end_node());}
1188 template <class _Key>
1189 iterator __lower_bound(const _Key& __v,
1190 __node_pointer __root,
1191 __node_pointer __result);
1192 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001193 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001194 const_iterator lower_bound(const _Key& __v) const
1195 {return __lower_bound(__v, __root(), __end_node());}
1196 template <class _Key>
1197 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001198 __node_pointer __root,
1199 __node_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001200 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001201 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001202 iterator upper_bound(const _Key& __v)
1203 {return __upper_bound(__v, __root(), __end_node());}
1204 template <class _Key>
1205 iterator __upper_bound(const _Key& __v,
1206 __node_pointer __root,
1207 __node_pointer __result);
1208 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001209 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001210 const_iterator upper_bound(const _Key& __v) const
1211 {return __upper_bound(__v, __root(), __end_node());}
1212 template <class _Key>
1213 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001214 __node_pointer __root,
1215 __node_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001216 template <class _Key>
1217 pair<iterator, iterator>
1218 __equal_range_unique(const _Key& __k);
1219 template <class _Key>
1220 pair<const_iterator, const_iterator>
1221 __equal_range_unique(const _Key& __k) const;
1222
1223 template <class _Key>
1224 pair<iterator, iterator>
1225 __equal_range_multi(const _Key& __k);
1226 template <class _Key>
1227 pair<const_iterator, const_iterator>
1228 __equal_range_multi(const _Key& __k) const;
1229
Howard Hinnant99968442011-11-29 18:15:50 +00001230 typedef __tree_node_destructor<__node_allocator> _Dp;
1231 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001232
Howard Hinnant8b537682011-06-04 17:10:24 +00001233 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001234private:
Howard Hinnantd615e472011-04-03 20:05:29 +00001235 typename __node_base::pointer&
Eric Fiselierdb215062016-03-31 02:15:15 +00001236 __find_leaf_low(typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantd615e472011-04-03 20:05:29 +00001237 typename __node_base::pointer&
Eric Fiselierdb215062016-03-31 02:15:15 +00001238 __find_leaf_high(typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantd615e472011-04-03 20:05:29 +00001239 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001240 __find_leaf(const_iterator __hint,
Eric Fiselierdb215062016-03-31 02:15:15 +00001241 typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001242 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001243 typename __node_base::pointer&
1244 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001246 typename __node_base::pointer&
1247 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001248 const _Key& __v);
1249
Eric Fiselierdb215062016-03-31 02:15:15 +00001250#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001251 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001252 __node_holder __construct_node(_Args&& ...__args);
1253#else
1254 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255#endif
1256
Howard Hinnant7686add2011-06-04 14:31:57 +00001257 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001258
Howard Hinnant333f50d2010-09-21 20:16:37 +00001259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001260 void __copy_assign_alloc(const __tree& __t)
1261 {__copy_assign_alloc(__t, integral_constant<bool,
1262 __node_traits::propagate_on_container_copy_assignment::value>());}
1263
Howard Hinnant333f50d2010-09-21 20:16:37 +00001264 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001265 void __copy_assign_alloc(const __tree& __t, true_type)
1266 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001268 void __copy_assign_alloc(const __tree& __t, false_type) {}
1269
1270 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001271 void __move_assign(__tree& __t, true_type)
1272 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1273 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001274
Howard Hinnant333f50d2010-09-21 20:16:37 +00001275 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001276 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001277 _NOEXCEPT_(
1278 !__node_traits::propagate_on_container_move_assignment::value ||
1279 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001280 {__move_assign_alloc(__t, integral_constant<bool,
1281 __node_traits::propagate_on_container_move_assignment::value>());}
1282
Howard Hinnant333f50d2010-09-21 20:16:37 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001285 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001286 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001287 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001288 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001289
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001290 __node_pointer __detach();
1291 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001292
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001293 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1294 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001295};
1296
1297template <class _Tp, class _Compare, class _Allocator>
1298__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001299 _NOEXCEPT_(
1300 is_nothrow_default_constructible<__node_allocator>::value &&
1301 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001302 : __pair3_(0, __comp)
1303{
1304 __begin_node() = __end_node();
1305}
1306
1307template <class _Tp, class _Compare, class _Allocator>
1308__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001309 : __begin_node_(__node_pointer()),
1310 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311 __pair3_(0)
1312{
1313 __begin_node() = __end_node();
1314}
1315
1316template <class _Tp, class _Compare, class _Allocator>
1317__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1318 const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001319 : __begin_node_(__node_pointer()),
1320 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001321 __pair3_(0, __comp)
1322{
1323 __begin_node() = __end_node();
1324}
1325
1326// Precondition: size() != 0
1327template <class _Tp, class _Compare, class _Allocator>
1328typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1329__tree<_Tp, _Compare, _Allocator>::__detach()
1330{
1331 __node_pointer __cache = __begin_node();
1332 __begin_node() = __end_node();
1333 __end_node()->__left_->__parent_ = nullptr;
1334 __end_node()->__left_ = nullptr;
1335 size() = 0;
1336 // __cache->__left_ == nullptr
1337 if (__cache->__right_ != nullptr)
1338 __cache = static_cast<__node_pointer>(__cache->__right_);
1339 // __cache->__left_ == nullptr
1340 // __cache->__right_ == nullptr
1341 return __cache;
1342}
1343
1344// Precondition: __cache != nullptr
1345// __cache->left_ == nullptr
1346// __cache->right_ == nullptr
1347// This is no longer a red-black tree
1348template <class _Tp, class _Compare, class _Allocator>
1349typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1350__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1351{
1352 if (__cache->__parent_ == nullptr)
1353 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001354 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001355 {
1356 __cache->__parent_->__left_ = nullptr;
1357 __cache = static_cast<__node_pointer>(__cache->__parent_);
1358 if (__cache->__right_ == nullptr)
1359 return __cache;
1360 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1361 }
1362 // __cache is right child
1363 __cache->__parent_->__right_ = nullptr;
1364 __cache = static_cast<__node_pointer>(__cache->__parent_);
1365 if (__cache->__left_ == nullptr)
1366 return __cache;
1367 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1368}
1369
1370template <class _Tp, class _Compare, class _Allocator>
1371__tree<_Tp, _Compare, _Allocator>&
1372__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1373{
1374 if (this != &__t)
1375 {
1376 value_comp() = __t.value_comp();
1377 __copy_assign_alloc(__t);
1378 __assign_multi(__t.begin(), __t.end());
1379 }
1380 return *this;
1381}
1382
1383template <class _Tp, class _Compare, class _Allocator>
1384template <class _InputIterator>
1385void
1386__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1387{
Eric Fiselierdb215062016-03-31 02:15:15 +00001388 typedef iterator_traits<_InputIterator> _ITraits;
1389 typedef typename _ITraits::value_type _ItValueType;
1390 static_assert((is_same<_ItValueType, __container_value_type>::value),
1391 "__assign_unique may only be called with the containers value type");
1392
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 if (size() != 0)
1394 {
1395 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001396#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001397 try
1398 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001399#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400 for (; __cache != nullptr && __first != __last; ++__first)
1401 {
1402 __cache->__value_ = *__first;
1403 __node_pointer __next = __detach(__cache);
1404 __node_insert_unique(__cache);
1405 __cache = __next;
1406 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001407#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001408 }
1409 catch (...)
1410 {
1411 while (__cache->__parent_ != nullptr)
1412 __cache = static_cast<__node_pointer>(__cache->__parent_);
1413 destroy(__cache);
1414 throw;
1415 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001416#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417 if (__cache != nullptr)
1418 {
1419 while (__cache->__parent_ != nullptr)
1420 __cache = static_cast<__node_pointer>(__cache->__parent_);
1421 destroy(__cache);
1422 }
1423 }
1424 for (; __first != __last; ++__first)
1425 __insert_unique(*__first);
1426}
1427
1428template <class _Tp, class _Compare, class _Allocator>
1429template <class _InputIterator>
1430void
1431__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1432{
Eric Fiselierdb215062016-03-31 02:15:15 +00001433 typedef iterator_traits<_InputIterator> _ITraits;
1434 typedef typename _ITraits::value_type _ItValueType;
1435 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1436 is_same<_ItValueType, __node_value_type>::value),
1437 "__assign_multi may only be called with the containers value type"
1438 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001439 if (size() != 0)
1440 {
1441 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001442#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001443 try
1444 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001445#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001446 for (; __cache != nullptr && __first != __last; ++__first)
1447 {
1448 __cache->__value_ = *__first;
1449 __node_pointer __next = __detach(__cache);
1450 __node_insert_multi(__cache);
1451 __cache = __next;
1452 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001453#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001454 }
1455 catch (...)
1456 {
1457 while (__cache->__parent_ != nullptr)
1458 __cache = static_cast<__node_pointer>(__cache->__parent_);
1459 destroy(__cache);
1460 throw;
1461 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001462#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001463 if (__cache != nullptr)
1464 {
1465 while (__cache->__parent_ != nullptr)
1466 __cache = static_cast<__node_pointer>(__cache->__parent_);
1467 destroy(__cache);
1468 }
1469 }
1470 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001471 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001472}
1473
1474template <class _Tp, class _Compare, class _Allocator>
1475__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1476 : __begin_node_(__node_pointer()),
1477 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1478 __pair3_(0, __t.value_comp())
1479{
1480 __begin_node() = __end_node();
1481}
1482
Howard Hinnant73d21a42010-09-04 23:28:19 +00001483#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001484
1485template <class _Tp, class _Compare, class _Allocator>
1486__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001487 _NOEXCEPT_(
1488 is_nothrow_move_constructible<__node_allocator>::value &&
1489 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001490 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1491 __pair1_(_VSTD::move(__t.__pair1_)),
1492 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001493{
1494 if (size() == 0)
1495 __begin_node() = __end_node();
1496 else
1497 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001498 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499 __t.__begin_node() = __t.__end_node();
1500 __t.__end_node()->__left_ = nullptr;
1501 __t.size() = 0;
1502 }
1503}
1504
1505template <class _Tp, class _Compare, class _Allocator>
1506__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1507 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001508 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001509{
1510 if (__a == __t.__alloc())
1511 {
1512 if (__t.size() == 0)
1513 __begin_node() = __end_node();
1514 else
1515 {
1516 __begin_node() = __t.__begin_node();
1517 __end_node()->__left_ = __t.__end_node()->__left_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001518 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001519 size() = __t.size();
1520 __t.__begin_node() = __t.__end_node();
1521 __t.__end_node()->__left_ = nullptr;
1522 __t.size() = 0;
1523 }
1524 }
1525 else
1526 {
1527 __begin_node() = __end_node();
1528 }
1529}
1530
1531template <class _Tp, class _Compare, class _Allocator>
1532void
1533__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001534 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1535 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001536{
1537 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1538 __begin_node_ = __t.__begin_node_;
1539 __pair1_.first() = __t.__pair1_.first();
1540 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001541 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001542 if (size() == 0)
1543 __begin_node() = __end_node();
1544 else
1545 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001546 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001547 __t.__begin_node() = __t.__end_node();
1548 __t.__end_node()->__left_ = nullptr;
1549 __t.size() = 0;
1550 }
1551}
1552
1553template <class _Tp, class _Compare, class _Allocator>
1554void
1555__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1556{
1557 if (__node_alloc() == __t.__node_alloc())
1558 __move_assign(__t, true_type());
1559 else
1560 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001561 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562 const_iterator __e = end();
1563 if (size() != 0)
1564 {
1565 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001566#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001567 try
1568 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001569#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570 while (__cache != nullptr && __t.size() != 0)
1571 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001572 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001573 __node_pointer __next = __detach(__cache);
1574 __node_insert_multi(__cache);
1575 __cache = __next;
1576 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001577#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578 }
1579 catch (...)
1580 {
1581 while (__cache->__parent_ != nullptr)
1582 __cache = static_cast<__node_pointer>(__cache->__parent_);
1583 destroy(__cache);
1584 throw;
1585 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001586#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001587 if (__cache != nullptr)
1588 {
1589 while (__cache->__parent_ != nullptr)
1590 __cache = static_cast<__node_pointer>(__cache->__parent_);
1591 destroy(__cache);
1592 }
1593 }
1594 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001595 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001596 }
1597}
1598
1599template <class _Tp, class _Compare, class _Allocator>
1600__tree<_Tp, _Compare, _Allocator>&
1601__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001602 _NOEXCEPT_(
1603 __node_traits::propagate_on_container_move_assignment::value &&
1604 is_nothrow_move_assignable<value_compare>::value &&
1605 is_nothrow_move_assignable<__node_allocator>::value)
1606
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607{
1608 __move_assign(__t, integral_constant<bool,
1609 __node_traits::propagate_on_container_move_assignment::value>());
1610 return *this;
1611}
1612
Howard Hinnant73d21a42010-09-04 23:28:19 +00001613#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614
1615template <class _Tp, class _Compare, class _Allocator>
1616__tree<_Tp, _Compare, _Allocator>::~__tree()
1617{
1618 destroy(__root());
1619}
1620
1621template <class _Tp, class _Compare, class _Allocator>
1622void
Howard Hinnant7686add2011-06-04 14:31:57 +00001623__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001624{
1625 if (__nd != nullptr)
1626 {
1627 destroy(static_cast<__node_pointer>(__nd->__left_));
1628 destroy(static_cast<__node_pointer>(__nd->__right_));
1629 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001630 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 __node_traits::deallocate(__na, __nd, 1);
1632 }
1633}
1634
1635template <class _Tp, class _Compare, class _Allocator>
1636void
1637__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Marshall Clow7d914d12015-07-13 20:04:56 +00001638 _NOEXCEPT_(
1639 __is_nothrow_swappable<value_compare>::value
1640#if _LIBCPP_STD_VER <= 11
1641 && (!__node_traits::propagate_on_container_swap::value ||
1642 __is_nothrow_swappable<__node_allocator>::value)
1643#endif
1644 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001646 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 swap(__begin_node_, __t.__begin_node_);
1648 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001649 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001650 __pair3_.swap(__t.__pair3_);
1651 if (size() == 0)
1652 __begin_node() = __end_node();
1653 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001654 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001655 if (__t.size() == 0)
1656 __t.__begin_node() = __t.__end_node();
1657 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001658 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001659}
1660
1661template <class _Tp, class _Compare, class _Allocator>
1662void
Howard Hinnant7686add2011-06-04 14:31:57 +00001663__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001664{
1665 destroy(__root());
1666 size() = 0;
1667 __begin_node() = __end_node();
1668 __end_node()->__left_ = nullptr;
1669}
1670
1671// Find lower_bound place to insert
1672// Set __parent to parent of null leaf
1673// Return reference to null leaf
1674template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001675typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1676__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001677 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001678{
1679 __node_pointer __nd = __root();
1680 if (__nd != nullptr)
1681 {
1682 while (true)
1683 {
1684 if (value_comp()(__nd->__value_, __v))
1685 {
1686 if (__nd->__right_ != nullptr)
1687 __nd = static_cast<__node_pointer>(__nd->__right_);
1688 else
1689 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001690 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001691 return __parent->__right_;
1692 }
1693 }
1694 else
1695 {
1696 if (__nd->__left_ != nullptr)
1697 __nd = static_cast<__node_pointer>(__nd->__left_);
1698 else
1699 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001700 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001701 return __parent->__left_;
1702 }
1703 }
1704 }
1705 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001706 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001707 return __parent->__left_;
1708}
1709
1710// Find upper_bound place to insert
1711// Set __parent to parent of null leaf
1712// Return reference to null leaf
1713template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001714typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1715__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001716 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001717{
1718 __node_pointer __nd = __root();
1719 if (__nd != nullptr)
1720 {
1721 while (true)
1722 {
1723 if (value_comp()(__v, __nd->__value_))
1724 {
1725 if (__nd->__left_ != nullptr)
1726 __nd = static_cast<__node_pointer>(__nd->__left_);
1727 else
1728 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001729 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001730 return __parent->__left_;
1731 }
1732 }
1733 else
1734 {
1735 if (__nd->__right_ != nullptr)
1736 __nd = static_cast<__node_pointer>(__nd->__right_);
1737 else
1738 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001739 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001740 return __parent->__right_;
1741 }
1742 }
1743 }
1744 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001745 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001746 return __parent->__left_;
1747}
1748
1749// Find leaf place to insert closest to __hint
1750// First check prior to __hint.
1751// Next check after __hint.
1752// Next do O(log N) search.
1753// Set __parent to parent of null leaf
1754// Return reference to null leaf
1755template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001756typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001758 typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001759 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760{
1761 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1762 {
1763 // __v <= *__hint
1764 const_iterator __prior = __hint;
1765 if (__prior == begin() || !value_comp()(__v, *--__prior))
1766 {
1767 // *prev(__hint) <= __v <= *__hint
1768 if (__hint.__ptr_->__left_ == nullptr)
1769 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001770 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001771 return __parent->__left_;
1772 }
1773 else
1774 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001775 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 return __parent->__right_;
1777 }
1778 }
1779 // __v < *prev(__hint)
1780 return __find_leaf_high(__parent, __v);
1781 }
1782 // else __v > *__hint
1783 return __find_leaf_low(__parent, __v);
1784}
1785
1786// Find place to insert if __v doesn't exist
1787// Set __parent to parent of null leaf
1788// Return reference to null leaf
1789// If __v exists, set parent to node of __v and return reference to node of __v
1790template <class _Tp, class _Compare, class _Allocator>
1791template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001792typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1793__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001794 const _Key& __v)
1795{
1796 __node_pointer __nd = __root();
1797 if (__nd != nullptr)
1798 {
1799 while (true)
1800 {
1801 if (value_comp()(__v, __nd->__value_))
1802 {
1803 if (__nd->__left_ != nullptr)
1804 __nd = static_cast<__node_pointer>(__nd->__left_);
1805 else
1806 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001807 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001808 return __parent->__left_;
1809 }
1810 }
1811 else if (value_comp()(__nd->__value_, __v))
1812 {
1813 if (__nd->__right_ != nullptr)
1814 __nd = static_cast<__node_pointer>(__nd->__right_);
1815 else
1816 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001817 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001818 return __parent->__right_;
1819 }
1820 }
1821 else
1822 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001823 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001824 return __parent;
1825 }
1826 }
1827 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001828 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829 return __parent->__left_;
1830}
1831
1832// Find place to insert if __v doesn't exist
1833// First check prior to __hint.
1834// Next check after __hint.
1835// Next do O(log N) search.
1836// Set __parent to parent of null leaf
1837// Return reference to null leaf
1838// If __v exists, set parent to node of __v and return reference to node of __v
1839template <class _Tp, class _Compare, class _Allocator>
1840template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001841typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001842__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001843 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 const _Key& __v)
1845{
1846 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1847 {
1848 // __v < *__hint
1849 const_iterator __prior = __hint;
1850 if (__prior == begin() || value_comp()(*--__prior, __v))
1851 {
1852 // *prev(__hint) < __v < *__hint
1853 if (__hint.__ptr_->__left_ == nullptr)
1854 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001855 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001856 return __parent->__left_;
1857 }
1858 else
1859 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001860 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001861 return __parent->__right_;
1862 }
1863 }
1864 // __v <= *prev(__hint)
1865 return __find_equal(__parent, __v);
1866 }
1867 else if (value_comp()(*__hint, __v)) // check after
1868 {
1869 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00001870 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001871 if (__next == end() || value_comp()(__v, *__next))
1872 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001873 // *__hint < __v < *_VSTD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 if (__hint.__ptr_->__right_ == nullptr)
1875 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001876 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 return __parent->__right_;
1878 }
1879 else
1880 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001881 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001882 return __parent->__left_;
1883 }
1884 }
1885 // *next(__hint) <= __v
1886 return __find_equal(__parent, __v);
1887 }
1888 // else __v == *__hint
Howard Hinnant70342b92013-06-19 21:29:40 +00001889 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001890 return __parent;
1891}
1892
1893template <class _Tp, class _Compare, class _Allocator>
1894void
1895__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1896 __node_base_pointer& __child,
1897 __node_base_pointer __new_node)
1898{
1899 __new_node->__left_ = nullptr;
1900 __new_node->__right_ = nullptr;
1901 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00001902 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001903 __child = __new_node;
1904 if (__begin_node()->__left_ != nullptr)
1905 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1906 __tree_balance_after_insert(__end_node()->__left_, __child);
1907 ++size();
1908}
1909
Eric Fiselierdb215062016-03-31 02:15:15 +00001910#ifndef _LIBCPP_CXX03_LANG
1911template <class _Tp, class _Compare, class _Allocator>
1912template <class _Key, class... _Args>
1913pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1914__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
1915#else
1916template <class _Tp, class _Compare, class _Allocator>
1917template <class _Key, class _Args>
1918pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1919__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
1920#endif
1921{
1922 __node_base_pointer __parent;
1923 __node_base_pointer& __child = __find_equal(__parent, __k);
1924 __node_pointer __r = static_cast<__node_pointer>(__child);
1925 bool __inserted = false;
1926 if (__child == nullptr)
1927 {
1928#ifndef _LIBCPP_CXX03_LANG
1929 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1930#else
1931 __node_holder __h = __construct_node(__args);
1932#endif
1933 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1934 __r = __h.release();
1935 __inserted = true;
1936 }
1937 return pair<iterator, bool>(iterator(__r), __inserted);
1938}
1939
1940
1941#ifndef _LIBCPP_CXX03_LANG
1942template <class _Tp, class _Compare, class _Allocator>
1943template <class _Key, class... _Args>
1944typename __tree<_Tp, _Compare, _Allocator>::iterator
1945__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
1946 const_iterator __p, _Key const& __k, _Args&&... __args)
1947#else
1948template <class _Tp, class _Compare, class _Allocator>
1949template <class _Key, class _Args>
1950typename __tree<_Tp, _Compare, _Allocator>::iterator
1951__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
1952 const_iterator __p, _Key const& __k, _Args& __args)
1953#endif
1954{
1955 __node_base_pointer __parent;
1956 __node_base_pointer& __child = __find_equal(__p, __parent, __k);
1957 __node_pointer __r = static_cast<__node_pointer>(__child);
1958 if (__child == nullptr)
1959 {
1960#ifndef _LIBCPP_CXX03_LANG
1961 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1962#else
1963 __node_holder __h = __construct_node(__args);
1964#endif
1965 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
1966 __r = __h.release();
1967 }
1968 return iterator(__r);
1969}
1970
1971
1972#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001973
1974template <class _Tp, class _Compare, class _Allocator>
1975template <class ..._Args>
1976typename __tree<_Tp, _Compare, _Allocator>::__node_holder
1977__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
1978{
Eric Fiselierdb215062016-03-31 02:15:15 +00001979 static_assert(!__is_tree_value_type<_Args...>::value,
1980 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001981 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00001982 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00001983 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001984 __h.get_deleter().__value_constructed = true;
1985 return __h;
1986}
1987
Eric Fiselierdb215062016-03-31 02:15:15 +00001988
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001989template <class _Tp, class _Compare, class _Allocator>
1990template <class... _Args>
1991pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1992__tree<_Tp, _Compare, _Allocator>::__emplace_unique(_Args&&... __args)
1993{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001994 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001995 __node_base_pointer __parent;
1996 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
1997 __node_pointer __r = static_cast<__node_pointer>(__child);
1998 bool __inserted = false;
1999 if (__child == nullptr)
2000 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002001 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002002 __r = __h.release();
2003 __inserted = true;
2004 }
2005 return pair<iterator, bool>(iterator(__r), __inserted);
2006}
2007
2008template <class _Tp, class _Compare, class _Allocator>
2009template <class... _Args>
2010typename __tree<_Tp, _Compare, _Allocator>::iterator
2011__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique(const_iterator __p, _Args&&... __args)
2012{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002013 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014 __node_base_pointer __parent;
2015 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
2016 __node_pointer __r = static_cast<__node_pointer>(__child);
2017 if (__child == nullptr)
2018 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002019 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002020 __r = __h.release();
2021 }
2022 return iterator(__r);
2023}
2024
2025template <class _Tp, class _Compare, class _Allocator>
2026template <class... _Args>
2027typename __tree<_Tp, _Compare, _Allocator>::iterator
2028__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2029{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002030 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002032 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002033 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002034 return iterator(static_cast<__node_pointer>(__h.release()));
2035}
2036
2037template <class _Tp, class _Compare, class _Allocator>
2038template <class... _Args>
2039typename __tree<_Tp, _Compare, _Allocator>::iterator
2040__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2041 _Args&&... __args)
2042{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002043 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002044 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002045 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002046 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002047 return iterator(static_cast<__node_pointer>(__h.release()));
2048}
2049
Howard Hinnant73d21a42010-09-04 23:28:19 +00002050
Eric Fiselierdb215062016-03-31 02:15:15 +00002051#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002052
2053template <class _Tp, class _Compare, class _Allocator>
2054typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002055__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002056{
2057 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002058 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002059 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002060 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002061 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062}
2063
Eric Fiselierdb215062016-03-31 02:15:15 +00002064#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002065
Eric Fiselierdb215062016-03-31 02:15:15 +00002066#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002067template <class _Tp, class _Compare, class _Allocator>
2068typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002069__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002070{
2071 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002072 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002073 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002074 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002075 return iterator(__h.release());
2076}
2077
2078template <class _Tp, class _Compare, class _Allocator>
2079typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002080__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081{
2082 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002083 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002084 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002085 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002086 return iterator(__h.release());
2087}
Eric Fiselierdb215062016-03-31 02:15:15 +00002088#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002090template <class _Tp, class _Compare, class _Allocator>
2091pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2092__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2093{
2094 __node_base_pointer __parent;
2095 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2096 __node_pointer __r = static_cast<__node_pointer>(__child);
2097 bool __inserted = false;
2098 if (__child == nullptr)
2099 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002100 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002101 __r = __nd;
2102 __inserted = true;
2103 }
2104 return pair<iterator, bool>(iterator(__r), __inserted);
2105}
2106
2107template <class _Tp, class _Compare, class _Allocator>
2108typename __tree<_Tp, _Compare, _Allocator>::iterator
2109__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2110 __node_pointer __nd)
2111{
2112 __node_base_pointer __parent;
2113 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2114 __node_pointer __r = static_cast<__node_pointer>(__child);
2115 if (__child == nullptr)
2116 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002117 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 __r = __nd;
2119 }
2120 return iterator(__r);
2121}
2122
2123template <class _Tp, class _Compare, class _Allocator>
2124typename __tree<_Tp, _Compare, _Allocator>::iterator
2125__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2126{
2127 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002128 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002129 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002130 return iterator(__nd);
2131}
2132
2133template <class _Tp, class _Compare, class _Allocator>
2134typename __tree<_Tp, _Compare, _Allocator>::iterator
2135__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2136 __node_pointer __nd)
2137{
2138 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002139 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002140 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002141 return iterator(__nd);
2142}
2143
2144template <class _Tp, class _Compare, class _Allocator>
2145typename __tree<_Tp, _Compare, _Allocator>::iterator
2146__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2147{
Howard Hinnant70342b92013-06-19 21:29:40 +00002148 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002149 iterator __r(__np);
2150 ++__r;
2151 if (__begin_node() == __np)
2152 __begin_node() = __r.__ptr_;
2153 --size();
2154 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 __tree_remove(__end_node()->__left_,
2156 static_cast<__node_base_pointer>(__np));
Eric Fiselierdb215062016-03-31 02:15:15 +00002157 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2158 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159 __node_traits::deallocate(__na, __np, 1);
2160 return __r;
2161}
2162
2163template <class _Tp, class _Compare, class _Allocator>
2164typename __tree<_Tp, _Compare, _Allocator>::iterator
2165__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2166{
2167 while (__f != __l)
2168 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002169 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170}
2171
2172template <class _Tp, class _Compare, class _Allocator>
2173template <class _Key>
2174typename __tree<_Tp, _Compare, _Allocator>::size_type
2175__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2176{
2177 iterator __i = find(__k);
2178 if (__i == end())
2179 return 0;
2180 erase(__i);
2181 return 1;
2182}
2183
2184template <class _Tp, class _Compare, class _Allocator>
2185template <class _Key>
2186typename __tree<_Tp, _Compare, _Allocator>::size_type
2187__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2188{
2189 pair<iterator, iterator> __p = __equal_range_multi(__k);
2190 size_type __r = 0;
2191 for (; __p.first != __p.second; ++__r)
2192 __p.first = erase(__p.first);
2193 return __r;
2194}
2195
2196template <class _Tp, class _Compare, class _Allocator>
2197template <class _Key>
2198typename __tree<_Tp, _Compare, _Allocator>::iterator
2199__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2200{
2201 iterator __p = __lower_bound(__v, __root(), __end_node());
2202 if (__p != end() && !value_comp()(__v, *__p))
2203 return __p;
2204 return end();
2205}
2206
2207template <class _Tp, class _Compare, class _Allocator>
2208template <class _Key>
2209typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2210__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2211{
2212 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2213 if (__p != end() && !value_comp()(__v, *__p))
2214 return __p;
2215 return end();
2216}
2217
2218template <class _Tp, class _Compare, class _Allocator>
2219template <class _Key>
2220typename __tree<_Tp, _Compare, _Allocator>::size_type
2221__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2222{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002223 __node_pointer __result = __end_node();
2224 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002225 while (__rt != nullptr)
2226 {
2227 if (value_comp()(__k, __rt->__value_))
2228 {
2229 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002230 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002231 }
2232 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002233 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002234 else
2235 return 1;
2236 }
2237 return 0;
2238}
2239
2240template <class _Tp, class _Compare, class _Allocator>
2241template <class _Key>
2242typename __tree<_Tp, _Compare, _Allocator>::size_type
2243__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2244{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002245 __node_pointer __result = __end_node();
2246 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247 while (__rt != nullptr)
2248 {
2249 if (value_comp()(__k, __rt->__value_))
2250 {
2251 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002252 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 }
2254 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002255 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002256 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002257 return _VSTD::distance(
Eric Fiselier227b47c2016-02-20 07:12:17 +00002258 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2259 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002260 );
2261 }
2262 return 0;
2263}
2264
2265template <class _Tp, class _Compare, class _Allocator>
2266template <class _Key>
2267typename __tree<_Tp, _Compare, _Allocator>::iterator
2268__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2269 __node_pointer __root,
2270 __node_pointer __result)
2271{
2272 while (__root != nullptr)
2273 {
2274 if (!value_comp()(__root->__value_, __v))
2275 {
2276 __result = __root;
2277 __root = static_cast<__node_pointer>(__root->__left_);
2278 }
2279 else
2280 __root = static_cast<__node_pointer>(__root->__right_);
2281 }
2282 return iterator(__result);
2283}
2284
2285template <class _Tp, class _Compare, class _Allocator>
2286template <class _Key>
2287typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2288__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002289 __node_pointer __root,
2290 __node_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291{
2292 while (__root != nullptr)
2293 {
2294 if (!value_comp()(__root->__value_, __v))
2295 {
2296 __result = __root;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002297 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002298 }
2299 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002300 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002301 }
2302 return const_iterator(__result);
2303}
2304
2305template <class _Tp, class _Compare, class _Allocator>
2306template <class _Key>
2307typename __tree<_Tp, _Compare, _Allocator>::iterator
2308__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2309 __node_pointer __root,
2310 __node_pointer __result)
2311{
2312 while (__root != nullptr)
2313 {
2314 if (value_comp()(__v, __root->__value_))
2315 {
2316 __result = __root;
2317 __root = static_cast<__node_pointer>(__root->__left_);
2318 }
2319 else
2320 __root = static_cast<__node_pointer>(__root->__right_);
2321 }
2322 return iterator(__result);
2323}
2324
2325template <class _Tp, class _Compare, class _Allocator>
2326template <class _Key>
2327typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2328__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002329 __node_pointer __root,
2330 __node_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002331{
2332 while (__root != nullptr)
2333 {
2334 if (value_comp()(__v, __root->__value_))
2335 {
2336 __result = __root;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002337 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002338 }
2339 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002340 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 }
2342 return const_iterator(__result);
2343}
2344
2345template <class _Tp, class _Compare, class _Allocator>
2346template <class _Key>
2347pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2348 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2349__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2350{
Howard Hinnant99968442011-11-29 18:15:50 +00002351 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 __node_pointer __result = __end_node();
2353 __node_pointer __rt = __root();
2354 while (__rt != nullptr)
2355 {
2356 if (value_comp()(__k, __rt->__value_))
2357 {
2358 __result = __rt;
2359 __rt = static_cast<__node_pointer>(__rt->__left_);
2360 }
2361 else if (value_comp()(__rt->__value_, __k))
2362 __rt = static_cast<__node_pointer>(__rt->__right_);
2363 else
Howard Hinnant99968442011-11-29 18:15:50 +00002364 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002365 iterator(
2366 __rt->__right_ != nullptr ?
2367 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2368 : __result));
2369 }
Howard Hinnant99968442011-11-29 18:15:50 +00002370 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002371}
2372
2373template <class _Tp, class _Compare, class _Allocator>
2374template <class _Key>
2375pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2376 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2377__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2378{
Howard Hinnant99968442011-11-29 18:15:50 +00002379 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002380 __node_pointer __result = __end_node();
2381 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002382 while (__rt != nullptr)
2383 {
2384 if (value_comp()(__k, __rt->__value_))
2385 {
2386 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002387 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002388 }
2389 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002390 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002391 else
Howard Hinnant99968442011-11-29 18:15:50 +00002392 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002393 const_iterator(
2394 __rt->__right_ != nullptr ?
Eric Fiselier227b47c2016-02-20 07:12:17 +00002395 static_cast<__node_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 : __result));
2397 }
Howard Hinnant99968442011-11-29 18:15:50 +00002398 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399}
2400
2401template <class _Tp, class _Compare, class _Allocator>
2402template <class _Key>
2403pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2404 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2405__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2406{
Howard Hinnant99968442011-11-29 18:15:50 +00002407 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002408 __node_pointer __result = __end_node();
2409 __node_pointer __rt = __root();
2410 while (__rt != nullptr)
2411 {
2412 if (value_comp()(__k, __rt->__value_))
2413 {
2414 __result = __rt;
2415 __rt = static_cast<__node_pointer>(__rt->__left_);
2416 }
2417 else if (value_comp()(__rt->__value_, __k))
2418 __rt = static_cast<__node_pointer>(__rt->__right_);
2419 else
Howard Hinnant99968442011-11-29 18:15:50 +00002420 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2422 }
Howard Hinnant99968442011-11-29 18:15:50 +00002423 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002424}
2425
2426template <class _Tp, class _Compare, class _Allocator>
2427template <class _Key>
2428pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2429 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2430__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2431{
Howard Hinnant99968442011-11-29 18:15:50 +00002432 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002433 __node_pointer __result = __end_node();
2434 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 while (__rt != nullptr)
2436 {
2437 if (value_comp()(__k, __rt->__value_))
2438 {
2439 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002440 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002441 }
2442 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002443 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002444 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002445 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2446 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002447 }
Howard Hinnant99968442011-11-29 18:15:50 +00002448 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002449}
2450
2451template <class _Tp, class _Compare, class _Allocator>
2452typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002453__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002454{
Howard Hinnant70342b92013-06-19 21:29:40 +00002455 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456 if (__begin_node() == __np)
2457 {
2458 if (__np->__right_ != nullptr)
2459 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2460 else
2461 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2462 }
2463 --size();
2464 __tree_remove(__end_node()->__left_,
2465 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002466 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002467}
2468
Howard Hinnant7686add2011-06-04 14:31:57 +00002469template <class _Tp, class _Compare, class _Allocator>
2470inline _LIBCPP_INLINE_VISIBILITY
2471void
2472swap(__tree<_Tp, _Compare, _Allocator>& __x,
2473 __tree<_Tp, _Compare, _Allocator>& __y)
2474 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2475{
2476 __x.swap(__y);
2477}
2478
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002479_LIBCPP_END_NAMESPACE_STD
2480
2481#endif // _LIBCPP___TREE