blob: eb6ff05c492bcf9b8cffe58543e70a53d83a761d [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 Fiselier83c9dc12016-04-15 23:27:27 +00001067 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierdb215062016-03-31 02:15:15 +00001068
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001069 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001070 iterator __emplace_hint_unique_impl(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);
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001077
1078 template <class _Pp>
1079 _LIBCPP_INLINE_VISIBILITY
1080 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1081 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1082 __can_extract_key<_Pp, key_type>());
1083 }
1084
1085 template <class... _Args>
1086 _LIBCPP_INLINE_VISIBILITY
1087 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1088 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1089 }
1090
1091 template <class _Pp>
1092 _LIBCPP_INLINE_VISIBILITY
1093 pair<iterator, bool>
1094 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1095 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1096 }
1097
1098 template <class _Pp>
1099 _LIBCPP_INLINE_VISIBILITY
1100 pair<iterator, bool>
1101 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1102 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1103 }
1104
1105 template <class _Pp>
1106 _LIBCPP_INLINE_VISIBILITY
1107 pair<iterator, bool>
1108 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1109 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1110 }
1111
1112 template <class _Pp>
1113 _LIBCPP_INLINE_VISIBILITY
1114 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1115 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1116 __can_extract_key<_Pp, key_type>());
1117 }
1118
1119 template <class... _Args>
1120 _LIBCPP_INLINE_VISIBILITY
1121 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1122 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1123 }
1124
1125 template <class _Pp>
1126 _LIBCPP_INLINE_VISIBILITY
1127 iterator
1128 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1129 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1130 }
1131
1132 template <class _Pp>
1133 _LIBCPP_INLINE_VISIBILITY
1134 iterator
1135 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1136 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1137 }
1138
1139 template <class _Pp>
1140 _LIBCPP_INLINE_VISIBILITY
1141 iterator
1142 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1143 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1144 }
1145
Eric Fiselierdb215062016-03-31 02:15:15 +00001146#else
1147 template <class _Key, class _Args>
1148 _LIBCPP_INLINE_VISIBILITY
1149 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1150 template <class _Key, class _Args>
1151 _LIBCPP_INLINE_VISIBILITY
1152 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001153#endif
1154
Eric Fiselierdb215062016-03-31 02:15:15 +00001155 _LIBCPP_INLINE_VISIBILITY
1156 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1157 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1158 }
1159
1160 _LIBCPP_INLINE_VISIBILITY
1161 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1162 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1163 }
1164
1165#ifdef _LIBCPP_CXX03_LANG
1166 _LIBCPP_INLINE_VISIBILITY
1167 iterator __insert_multi(const __container_value_type& __v);
1168 _LIBCPP_INLINE_VISIBILITY
1169 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1170#else
1171 _LIBCPP_INLINE_VISIBILITY
1172 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1173 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1174 }
1175
1176 _LIBCPP_INLINE_VISIBILITY
1177 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1178 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1179 }
1180
1181 template <class _Vp, class = typename enable_if<
1182 !is_same<typename __unconstref<_Vp>::type,
1183 __container_value_type
1184 >::value
1185 >::type>
1186 _LIBCPP_INLINE_VISIBILITY
1187 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1188 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1189 }
1190
1191 template <class _Vp, class = typename enable_if<
1192 !is_same<typename __unconstref<_Vp>::type,
1193 __container_value_type
1194 >::value
1195 >::type>
1196 _LIBCPP_INLINE_VISIBILITY
1197 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1198 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1199 }
1200
1201 _LIBCPP_INLINE_VISIBILITY
1202 iterator __insert_multi(__container_value_type&& __v) {
1203 return __emplace_multi(_VSTD::move(__v));
1204 }
1205
1206 _LIBCPP_INLINE_VISIBILITY
1207 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1208 return __emplace_hint_multi(__p, _VSTD::move(__v));
1209 }
1210
1211 template <class _Vp>
1212 _LIBCPP_INLINE_VISIBILITY
1213 iterator __insert_multi(_Vp&& __v) {
1214 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1215 }
1216
1217 template <class _Vp>
1218 _LIBCPP_INLINE_VISIBILITY
1219 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1220 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1221 }
1222
1223#endif // !_LIBCPP_CXX03_LANG
1224
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001225 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1226 iterator __node_insert_unique(const_iterator __p,
1227 __node_pointer __nd);
1228
1229 iterator __node_insert_multi(__node_pointer __nd);
1230 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1231
1232 iterator erase(const_iterator __p);
1233 iterator erase(const_iterator __f, const_iterator __l);
1234 template <class _Key>
1235 size_type __erase_unique(const _Key& __k);
1236 template <class _Key>
1237 size_type __erase_multi(const _Key& __k);
1238
1239 void __insert_node_at(__node_base_pointer __parent,
1240 __node_base_pointer& __child,
1241 __node_base_pointer __new_node);
1242
1243 template <class _Key>
1244 iterator find(const _Key& __v);
1245 template <class _Key>
1246 const_iterator find(const _Key& __v) const;
1247
1248 template <class _Key>
1249 size_type __count_unique(const _Key& __k) const;
1250 template <class _Key>
1251 size_type __count_multi(const _Key& __k) const;
1252
1253 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001254 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001255 iterator lower_bound(const _Key& __v)
1256 {return __lower_bound(__v, __root(), __end_node());}
1257 template <class _Key>
1258 iterator __lower_bound(const _Key& __v,
1259 __node_pointer __root,
1260 __node_pointer __result);
1261 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001262 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001263 const_iterator lower_bound(const _Key& __v) const
1264 {return __lower_bound(__v, __root(), __end_node());}
1265 template <class _Key>
1266 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001267 __node_pointer __root,
1268 __node_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001269 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001270 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001271 iterator upper_bound(const _Key& __v)
1272 {return __upper_bound(__v, __root(), __end_node());}
1273 template <class _Key>
1274 iterator __upper_bound(const _Key& __v,
1275 __node_pointer __root,
1276 __node_pointer __result);
1277 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001279 const_iterator upper_bound(const _Key& __v) const
1280 {return __upper_bound(__v, __root(), __end_node());}
1281 template <class _Key>
1282 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001283 __node_pointer __root,
1284 __node_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001285 template <class _Key>
1286 pair<iterator, iterator>
1287 __equal_range_unique(const _Key& __k);
1288 template <class _Key>
1289 pair<const_iterator, const_iterator>
1290 __equal_range_unique(const _Key& __k) const;
1291
1292 template <class _Key>
1293 pair<iterator, iterator>
1294 __equal_range_multi(const _Key& __k);
1295 template <class _Key>
1296 pair<const_iterator, const_iterator>
1297 __equal_range_multi(const _Key& __k) const;
1298
Howard Hinnant99968442011-11-29 18:15:50 +00001299 typedef __tree_node_destructor<__node_allocator> _Dp;
1300 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001301
Howard Hinnant8b537682011-06-04 17:10:24 +00001302 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001303private:
Howard Hinnantd615e472011-04-03 20:05:29 +00001304 typename __node_base::pointer&
Eric Fiselierdb215062016-03-31 02:15:15 +00001305 __find_leaf_low(typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantd615e472011-04-03 20:05:29 +00001306 typename __node_base::pointer&
Eric Fiselierdb215062016-03-31 02:15:15 +00001307 __find_leaf_high(typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantd615e472011-04-03 20:05:29 +00001308 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001309 __find_leaf(const_iterator __hint,
Eric Fiselierdb215062016-03-31 02:15:15 +00001310 typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001311 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001312 typename __node_base::pointer&
1313 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001315 typename __node_base::pointer&
1316 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001317 const _Key& __v);
1318
Eric Fiselierdb215062016-03-31 02:15:15 +00001319#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001320 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001321 __node_holder __construct_node(_Args&& ...__args);
1322#else
1323 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001324#endif
1325
Howard Hinnant7686add2011-06-04 14:31:57 +00001326 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001327
Howard Hinnant333f50d2010-09-21 20:16:37 +00001328 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001329 void __copy_assign_alloc(const __tree& __t)
1330 {__copy_assign_alloc(__t, integral_constant<bool,
1331 __node_traits::propagate_on_container_copy_assignment::value>());}
1332
Howard Hinnant333f50d2010-09-21 20:16:37 +00001333 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001334 void __copy_assign_alloc(const __tree& __t, true_type)
1335 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001336 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001337 void __copy_assign_alloc(const __tree& __t, false_type) {}
1338
1339 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001340 void __move_assign(__tree& __t, true_type)
1341 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1342 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343
Howard Hinnant333f50d2010-09-21 20:16:37 +00001344 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001345 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001346 _NOEXCEPT_(
1347 !__node_traits::propagate_on_container_move_assignment::value ||
1348 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 {__move_assign_alloc(__t, integral_constant<bool,
1350 __node_traits::propagate_on_container_move_assignment::value>());}
1351
Howard Hinnant333f50d2010-09-21 20:16:37 +00001352 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001354 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001355 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001356 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001357 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001359 __node_pointer __detach();
1360 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001361
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001362 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1363 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001364};
1365
1366template <class _Tp, class _Compare, class _Allocator>
1367__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001368 _NOEXCEPT_(
1369 is_nothrow_default_constructible<__node_allocator>::value &&
1370 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 : __pair3_(0, __comp)
1372{
1373 __begin_node() = __end_node();
1374}
1375
1376template <class _Tp, class _Compare, class _Allocator>
1377__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001378 : __begin_node_(__node_pointer()),
1379 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001380 __pair3_(0)
1381{
1382 __begin_node() = __end_node();
1383}
1384
1385template <class _Tp, class _Compare, class _Allocator>
1386__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1387 const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001388 : __begin_node_(__node_pointer()),
1389 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001390 __pair3_(0, __comp)
1391{
1392 __begin_node() = __end_node();
1393}
1394
1395// Precondition: size() != 0
1396template <class _Tp, class _Compare, class _Allocator>
1397typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1398__tree<_Tp, _Compare, _Allocator>::__detach()
1399{
1400 __node_pointer __cache = __begin_node();
1401 __begin_node() = __end_node();
1402 __end_node()->__left_->__parent_ = nullptr;
1403 __end_node()->__left_ = nullptr;
1404 size() = 0;
1405 // __cache->__left_ == nullptr
1406 if (__cache->__right_ != nullptr)
1407 __cache = static_cast<__node_pointer>(__cache->__right_);
1408 // __cache->__left_ == nullptr
1409 // __cache->__right_ == nullptr
1410 return __cache;
1411}
1412
1413// Precondition: __cache != nullptr
1414// __cache->left_ == nullptr
1415// __cache->right_ == nullptr
1416// This is no longer a red-black tree
1417template <class _Tp, class _Compare, class _Allocator>
1418typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1419__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1420{
1421 if (__cache->__parent_ == nullptr)
1422 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001423 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001424 {
1425 __cache->__parent_->__left_ = nullptr;
1426 __cache = static_cast<__node_pointer>(__cache->__parent_);
1427 if (__cache->__right_ == nullptr)
1428 return __cache;
1429 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1430 }
1431 // __cache is right child
1432 __cache->__parent_->__right_ = nullptr;
1433 __cache = static_cast<__node_pointer>(__cache->__parent_);
1434 if (__cache->__left_ == nullptr)
1435 return __cache;
1436 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1437}
1438
1439template <class _Tp, class _Compare, class _Allocator>
1440__tree<_Tp, _Compare, _Allocator>&
1441__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1442{
1443 if (this != &__t)
1444 {
1445 value_comp() = __t.value_comp();
1446 __copy_assign_alloc(__t);
1447 __assign_multi(__t.begin(), __t.end());
1448 }
1449 return *this;
1450}
1451
1452template <class _Tp, class _Compare, class _Allocator>
1453template <class _InputIterator>
1454void
1455__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1456{
Eric Fiselierdb215062016-03-31 02:15:15 +00001457 typedef iterator_traits<_InputIterator> _ITraits;
1458 typedef typename _ITraits::value_type _ItValueType;
1459 static_assert((is_same<_ItValueType, __container_value_type>::value),
1460 "__assign_unique may only be called with the containers value type");
1461
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001462 if (size() != 0)
1463 {
1464 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001465#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466 try
1467 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001468#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001469 for (; __cache != nullptr && __first != __last; ++__first)
1470 {
1471 __cache->__value_ = *__first;
1472 __node_pointer __next = __detach(__cache);
1473 __node_insert_unique(__cache);
1474 __cache = __next;
1475 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001476#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 }
1478 catch (...)
1479 {
1480 while (__cache->__parent_ != nullptr)
1481 __cache = static_cast<__node_pointer>(__cache->__parent_);
1482 destroy(__cache);
1483 throw;
1484 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001485#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486 if (__cache != nullptr)
1487 {
1488 while (__cache->__parent_ != nullptr)
1489 __cache = static_cast<__node_pointer>(__cache->__parent_);
1490 destroy(__cache);
1491 }
1492 }
1493 for (; __first != __last; ++__first)
1494 __insert_unique(*__first);
1495}
1496
1497template <class _Tp, class _Compare, class _Allocator>
1498template <class _InputIterator>
1499void
1500__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1501{
Eric Fiselierdb215062016-03-31 02:15:15 +00001502 typedef iterator_traits<_InputIterator> _ITraits;
1503 typedef typename _ITraits::value_type _ItValueType;
1504 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1505 is_same<_ItValueType, __node_value_type>::value),
1506 "__assign_multi may only be called with the containers value type"
1507 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 if (size() != 0)
1509 {
1510 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001511#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001512 try
1513 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001514#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 for (; __cache != nullptr && __first != __last; ++__first)
1516 {
1517 __cache->__value_ = *__first;
1518 __node_pointer __next = __detach(__cache);
1519 __node_insert_multi(__cache);
1520 __cache = __next;
1521 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001522#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001523 }
1524 catch (...)
1525 {
1526 while (__cache->__parent_ != nullptr)
1527 __cache = static_cast<__node_pointer>(__cache->__parent_);
1528 destroy(__cache);
1529 throw;
1530 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001531#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001532 if (__cache != nullptr)
1533 {
1534 while (__cache->__parent_ != nullptr)
1535 __cache = static_cast<__node_pointer>(__cache->__parent_);
1536 destroy(__cache);
1537 }
1538 }
1539 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001540 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541}
1542
1543template <class _Tp, class _Compare, class _Allocator>
1544__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1545 : __begin_node_(__node_pointer()),
1546 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1547 __pair3_(0, __t.value_comp())
1548{
1549 __begin_node() = __end_node();
1550}
1551
Howard Hinnant73d21a42010-09-04 23:28:19 +00001552#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001553
1554template <class _Tp, class _Compare, class _Allocator>
1555__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001556 _NOEXCEPT_(
1557 is_nothrow_move_constructible<__node_allocator>::value &&
1558 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001559 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1560 __pair1_(_VSTD::move(__t.__pair1_)),
1561 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001562{
1563 if (size() == 0)
1564 __begin_node() = __end_node();
1565 else
1566 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001567 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001568 __t.__begin_node() = __t.__end_node();
1569 __t.__end_node()->__left_ = nullptr;
1570 __t.size() = 0;
1571 }
1572}
1573
1574template <class _Tp, class _Compare, class _Allocator>
1575__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1576 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001577 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001578{
1579 if (__a == __t.__alloc())
1580 {
1581 if (__t.size() == 0)
1582 __begin_node() = __end_node();
1583 else
1584 {
1585 __begin_node() = __t.__begin_node();
1586 __end_node()->__left_ = __t.__end_node()->__left_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001587 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001588 size() = __t.size();
1589 __t.__begin_node() = __t.__end_node();
1590 __t.__end_node()->__left_ = nullptr;
1591 __t.size() = 0;
1592 }
1593 }
1594 else
1595 {
1596 __begin_node() = __end_node();
1597 }
1598}
1599
1600template <class _Tp, class _Compare, class _Allocator>
1601void
1602__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001603 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1604 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605{
1606 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1607 __begin_node_ = __t.__begin_node_;
1608 __pair1_.first() = __t.__pair1_.first();
1609 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001610 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001611 if (size() == 0)
1612 __begin_node() = __end_node();
1613 else
1614 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001615 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001616 __t.__begin_node() = __t.__end_node();
1617 __t.__end_node()->__left_ = nullptr;
1618 __t.size() = 0;
1619 }
1620}
1621
1622template <class _Tp, class _Compare, class _Allocator>
1623void
1624__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1625{
1626 if (__node_alloc() == __t.__node_alloc())
1627 __move_assign(__t, true_type());
1628 else
1629 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001630 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001631 const_iterator __e = end();
1632 if (size() != 0)
1633 {
1634 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001635#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 try
1637 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001638#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001639 while (__cache != nullptr && __t.size() != 0)
1640 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001641 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001642 __node_pointer __next = __detach(__cache);
1643 __node_insert_multi(__cache);
1644 __cache = __next;
1645 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001646#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001647 }
1648 catch (...)
1649 {
1650 while (__cache->__parent_ != nullptr)
1651 __cache = static_cast<__node_pointer>(__cache->__parent_);
1652 destroy(__cache);
1653 throw;
1654 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001655#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001656 if (__cache != nullptr)
1657 {
1658 while (__cache->__parent_ != nullptr)
1659 __cache = static_cast<__node_pointer>(__cache->__parent_);
1660 destroy(__cache);
1661 }
1662 }
1663 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001664 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 }
1666}
1667
1668template <class _Tp, class _Compare, class _Allocator>
1669__tree<_Tp, _Compare, _Allocator>&
1670__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001671 _NOEXCEPT_(
1672 __node_traits::propagate_on_container_move_assignment::value &&
1673 is_nothrow_move_assignable<value_compare>::value &&
1674 is_nothrow_move_assignable<__node_allocator>::value)
1675
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676{
1677 __move_assign(__t, integral_constant<bool,
1678 __node_traits::propagate_on_container_move_assignment::value>());
1679 return *this;
1680}
1681
Howard Hinnant73d21a42010-09-04 23:28:19 +00001682#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001683
1684template <class _Tp, class _Compare, class _Allocator>
1685__tree<_Tp, _Compare, _Allocator>::~__tree()
1686{
1687 destroy(__root());
1688}
1689
1690template <class _Tp, class _Compare, class _Allocator>
1691void
Howard Hinnant7686add2011-06-04 14:31:57 +00001692__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001693{
1694 if (__nd != nullptr)
1695 {
1696 destroy(static_cast<__node_pointer>(__nd->__left_));
1697 destroy(static_cast<__node_pointer>(__nd->__right_));
1698 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001699 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001700 __node_traits::deallocate(__na, __nd, 1);
1701 }
1702}
1703
1704template <class _Tp, class _Compare, class _Allocator>
1705void
1706__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Marshall Clow7d914d12015-07-13 20:04:56 +00001707 _NOEXCEPT_(
1708 __is_nothrow_swappable<value_compare>::value
1709#if _LIBCPP_STD_VER <= 11
1710 && (!__node_traits::propagate_on_container_swap::value ||
1711 __is_nothrow_swappable<__node_allocator>::value)
1712#endif
1713 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001714{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001715 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 swap(__begin_node_, __t.__begin_node_);
1717 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001718 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001719 __pair3_.swap(__t.__pair3_);
1720 if (size() == 0)
1721 __begin_node() = __end_node();
1722 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001723 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001724 if (__t.size() == 0)
1725 __t.__begin_node() = __t.__end_node();
1726 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001727 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001728}
1729
1730template <class _Tp, class _Compare, class _Allocator>
1731void
Howard Hinnant7686add2011-06-04 14:31:57 +00001732__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733{
1734 destroy(__root());
1735 size() = 0;
1736 __begin_node() = __end_node();
1737 __end_node()->__left_ = nullptr;
1738}
1739
1740// Find lower_bound place to insert
1741// Set __parent to parent of null leaf
1742// Return reference to null leaf
1743template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001744typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1745__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001746 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001747{
1748 __node_pointer __nd = __root();
1749 if (__nd != nullptr)
1750 {
1751 while (true)
1752 {
1753 if (value_comp()(__nd->__value_, __v))
1754 {
1755 if (__nd->__right_ != nullptr)
1756 __nd = static_cast<__node_pointer>(__nd->__right_);
1757 else
1758 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001759 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001760 return __parent->__right_;
1761 }
1762 }
1763 else
1764 {
1765 if (__nd->__left_ != nullptr)
1766 __nd = static_cast<__node_pointer>(__nd->__left_);
1767 else
1768 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001769 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 return __parent->__left_;
1771 }
1772 }
1773 }
1774 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001775 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776 return __parent->__left_;
1777}
1778
1779// Find upper_bound place to insert
1780// Set __parent to parent of null leaf
1781// Return reference to null leaf
1782template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001783typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1784__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001785 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001786{
1787 __node_pointer __nd = __root();
1788 if (__nd != nullptr)
1789 {
1790 while (true)
1791 {
1792 if (value_comp()(__v, __nd->__value_))
1793 {
1794 if (__nd->__left_ != nullptr)
1795 __nd = static_cast<__node_pointer>(__nd->__left_);
1796 else
1797 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001798 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001799 return __parent->__left_;
1800 }
1801 }
1802 else
1803 {
1804 if (__nd->__right_ != nullptr)
1805 __nd = static_cast<__node_pointer>(__nd->__right_);
1806 else
1807 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001808 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001809 return __parent->__right_;
1810 }
1811 }
1812 }
1813 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001814 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815 return __parent->__left_;
1816}
1817
1818// Find leaf place to insert closest to __hint
1819// First check prior to __hint.
1820// Next check after __hint.
1821// Next do O(log N) search.
1822// Set __parent to parent of null leaf
1823// Return reference to null leaf
1824template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001825typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001826__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001827 typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001828 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001829{
1830 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1831 {
1832 // __v <= *__hint
1833 const_iterator __prior = __hint;
1834 if (__prior == begin() || !value_comp()(__v, *--__prior))
1835 {
1836 // *prev(__hint) <= __v <= *__hint
1837 if (__hint.__ptr_->__left_ == nullptr)
1838 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001839 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001840 return __parent->__left_;
1841 }
1842 else
1843 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001844 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001845 return __parent->__right_;
1846 }
1847 }
1848 // __v < *prev(__hint)
1849 return __find_leaf_high(__parent, __v);
1850 }
1851 // else __v > *__hint
1852 return __find_leaf_low(__parent, __v);
1853}
1854
1855// Find place to insert if __v doesn't exist
1856// Set __parent to parent of null leaf
1857// Return reference to null leaf
1858// If __v exists, set parent to node of __v and return reference to node of __v
1859template <class _Tp, class _Compare, class _Allocator>
1860template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001861typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1862__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001863 const _Key& __v)
1864{
1865 __node_pointer __nd = __root();
1866 if (__nd != nullptr)
1867 {
1868 while (true)
1869 {
1870 if (value_comp()(__v, __nd->__value_))
1871 {
1872 if (__nd->__left_ != nullptr)
1873 __nd = static_cast<__node_pointer>(__nd->__left_);
1874 else
1875 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001876 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001877 return __parent->__left_;
1878 }
1879 }
1880 else if (value_comp()(__nd->__value_, __v))
1881 {
1882 if (__nd->__right_ != nullptr)
1883 __nd = static_cast<__node_pointer>(__nd->__right_);
1884 else
1885 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001886 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001887 return __parent->__right_;
1888 }
1889 }
1890 else
1891 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001892 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001893 return __parent;
1894 }
1895 }
1896 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001897 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001898 return __parent->__left_;
1899}
1900
1901// Find place to insert if __v doesn't exist
1902// First check prior to __hint.
1903// Next check after __hint.
1904// Next do O(log N) search.
1905// Set __parent to parent of null leaf
1906// Return reference to null leaf
1907// If __v exists, set parent to node of __v and return reference to node of __v
1908template <class _Tp, class _Compare, class _Allocator>
1909template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001910typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001911__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001912 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001913 const _Key& __v)
1914{
1915 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1916 {
1917 // __v < *__hint
1918 const_iterator __prior = __hint;
1919 if (__prior == begin() || value_comp()(*--__prior, __v))
1920 {
1921 // *prev(__hint) < __v < *__hint
1922 if (__hint.__ptr_->__left_ == nullptr)
1923 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001924 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001925 return __parent->__left_;
1926 }
1927 else
1928 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001929 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001930 return __parent->__right_;
1931 }
1932 }
1933 // __v <= *prev(__hint)
1934 return __find_equal(__parent, __v);
1935 }
1936 else if (value_comp()(*__hint, __v)) // check after
1937 {
1938 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00001939 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940 if (__next == end() || value_comp()(__v, *__next))
1941 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001942 // *__hint < __v < *_VSTD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001943 if (__hint.__ptr_->__right_ == nullptr)
1944 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001945 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 return __parent->__right_;
1947 }
1948 else
1949 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001950 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 return __parent->__left_;
1952 }
1953 }
1954 // *next(__hint) <= __v
1955 return __find_equal(__parent, __v);
1956 }
1957 // else __v == *__hint
Howard Hinnant70342b92013-06-19 21:29:40 +00001958 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 return __parent;
1960}
1961
1962template <class _Tp, class _Compare, class _Allocator>
1963void
1964__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1965 __node_base_pointer& __child,
1966 __node_base_pointer __new_node)
1967{
1968 __new_node->__left_ = nullptr;
1969 __new_node->__right_ = nullptr;
1970 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00001971 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 __child = __new_node;
1973 if (__begin_node()->__left_ != nullptr)
1974 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
1975 __tree_balance_after_insert(__end_node()->__left_, __child);
1976 ++size();
1977}
1978
Eric Fiselierdb215062016-03-31 02:15:15 +00001979#ifndef _LIBCPP_CXX03_LANG
1980template <class _Tp, class _Compare, class _Allocator>
1981template <class _Key, class... _Args>
1982pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1983__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
1984#else
1985template <class _Tp, class _Compare, class _Allocator>
1986template <class _Key, class _Args>
1987pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
1988__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
1989#endif
1990{
1991 __node_base_pointer __parent;
1992 __node_base_pointer& __child = __find_equal(__parent, __k);
1993 __node_pointer __r = static_cast<__node_pointer>(__child);
1994 bool __inserted = false;
1995 if (__child == nullptr)
1996 {
1997#ifndef _LIBCPP_CXX03_LANG
1998 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
1999#else
2000 __node_holder __h = __construct_node(__args);
2001#endif
2002 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2003 __r = __h.release();
2004 __inserted = true;
2005 }
2006 return pair<iterator, bool>(iterator(__r), __inserted);
2007}
2008
2009
2010#ifndef _LIBCPP_CXX03_LANG
2011template <class _Tp, class _Compare, class _Allocator>
2012template <class _Key, class... _Args>
2013typename __tree<_Tp, _Compare, _Allocator>::iterator
2014__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2015 const_iterator __p, _Key const& __k, _Args&&... __args)
2016#else
2017template <class _Tp, class _Compare, class _Allocator>
2018template <class _Key, class _Args>
2019typename __tree<_Tp, _Compare, _Allocator>::iterator
2020__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2021 const_iterator __p, _Key const& __k, _Args& __args)
2022#endif
2023{
2024 __node_base_pointer __parent;
2025 __node_base_pointer& __child = __find_equal(__p, __parent, __k);
2026 __node_pointer __r = static_cast<__node_pointer>(__child);
2027 if (__child == nullptr)
2028 {
2029#ifndef _LIBCPP_CXX03_LANG
2030 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2031#else
2032 __node_holder __h = __construct_node(__args);
2033#endif
2034 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2035 __r = __h.release();
2036 }
2037 return iterator(__r);
2038}
2039
2040
2041#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002042
2043template <class _Tp, class _Compare, class _Allocator>
2044template <class ..._Args>
2045typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2046__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2047{
Eric Fiselierdb215062016-03-31 02:15:15 +00002048 static_assert(!__is_tree_value_type<_Args...>::value,
2049 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002050 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002051 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002052 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002053 __h.get_deleter().__value_constructed = true;
2054 return __h;
2055}
2056
Eric Fiselierdb215062016-03-31 02:15:15 +00002057
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002058template <class _Tp, class _Compare, class _Allocator>
2059template <class... _Args>
2060pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002061__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002062{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002063 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002064 __node_base_pointer __parent;
2065 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2066 __node_pointer __r = static_cast<__node_pointer>(__child);
2067 bool __inserted = false;
2068 if (__child == nullptr)
2069 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002070 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071 __r = __h.release();
2072 __inserted = true;
2073 }
2074 return pair<iterator, bool>(iterator(__r), __inserted);
2075}
2076
2077template <class _Tp, class _Compare, class _Allocator>
2078template <class... _Args>
2079typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002080__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002081{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002082 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002083 __node_base_pointer __parent;
2084 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
2085 __node_pointer __r = static_cast<__node_pointer>(__child);
2086 if (__child == nullptr)
2087 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002088 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 __r = __h.release();
2090 }
2091 return iterator(__r);
2092}
2093
2094template <class _Tp, class _Compare, class _Allocator>
2095template <class... _Args>
2096typename __tree<_Tp, _Compare, _Allocator>::iterator
2097__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2098{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002099 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002101 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002102 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002103 return iterator(static_cast<__node_pointer>(__h.release()));
2104}
2105
2106template <class _Tp, class _Compare, class _Allocator>
2107template <class... _Args>
2108typename __tree<_Tp, _Compare, _Allocator>::iterator
2109__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2110 _Args&&... __args)
2111{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002112 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002113 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002114 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002115 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002116 return iterator(static_cast<__node_pointer>(__h.release()));
2117}
2118
Howard Hinnant73d21a42010-09-04 23:28:19 +00002119
Eric Fiselierdb215062016-03-31 02:15:15 +00002120#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002121
2122template <class _Tp, class _Compare, class _Allocator>
2123typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002124__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002125{
2126 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002127 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002128 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002130 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002131}
2132
Eric Fiselierdb215062016-03-31 02:15:15 +00002133#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002134
Eric Fiselierdb215062016-03-31 02:15:15 +00002135#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002136template <class _Tp, class _Compare, class _Allocator>
2137typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002138__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002139{
2140 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002141 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002143 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002144 return iterator(__h.release());
2145}
2146
2147template <class _Tp, class _Compare, class _Allocator>
2148typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002149__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002150{
2151 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002152 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002153 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002154 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002155 return iterator(__h.release());
2156}
Eric Fiselierdb215062016-03-31 02:15:15 +00002157#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002159template <class _Tp, class _Compare, class _Allocator>
2160pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2161__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2162{
2163 __node_base_pointer __parent;
2164 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2165 __node_pointer __r = static_cast<__node_pointer>(__child);
2166 bool __inserted = false;
2167 if (__child == nullptr)
2168 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002169 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002170 __r = __nd;
2171 __inserted = true;
2172 }
2173 return pair<iterator, bool>(iterator(__r), __inserted);
2174}
2175
2176template <class _Tp, class _Compare, class _Allocator>
2177typename __tree<_Tp, _Compare, _Allocator>::iterator
2178__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2179 __node_pointer __nd)
2180{
2181 __node_base_pointer __parent;
2182 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2183 __node_pointer __r = static_cast<__node_pointer>(__child);
2184 if (__child == nullptr)
2185 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002186 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187 __r = __nd;
2188 }
2189 return iterator(__r);
2190}
2191
2192template <class _Tp, class _Compare, class _Allocator>
2193typename __tree<_Tp, _Compare, _Allocator>::iterator
2194__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2195{
2196 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002197 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002198 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002199 return iterator(__nd);
2200}
2201
2202template <class _Tp, class _Compare, class _Allocator>
2203typename __tree<_Tp, _Compare, _Allocator>::iterator
2204__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2205 __node_pointer __nd)
2206{
2207 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002208 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002209 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002210 return iterator(__nd);
2211}
2212
2213template <class _Tp, class _Compare, class _Allocator>
2214typename __tree<_Tp, _Compare, _Allocator>::iterator
2215__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2216{
Howard Hinnant70342b92013-06-19 21:29:40 +00002217 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002218 iterator __r(__np);
2219 ++__r;
2220 if (__begin_node() == __np)
2221 __begin_node() = __r.__ptr_;
2222 --size();
2223 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002224 __tree_remove(__end_node()->__left_,
2225 static_cast<__node_base_pointer>(__np));
Eric Fiselierdb215062016-03-31 02:15:15 +00002226 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2227 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 __node_traits::deallocate(__na, __np, 1);
2229 return __r;
2230}
2231
2232template <class _Tp, class _Compare, class _Allocator>
2233typename __tree<_Tp, _Compare, _Allocator>::iterator
2234__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2235{
2236 while (__f != __l)
2237 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002238 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239}
2240
2241template <class _Tp, class _Compare, class _Allocator>
2242template <class _Key>
2243typename __tree<_Tp, _Compare, _Allocator>::size_type
2244__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2245{
2246 iterator __i = find(__k);
2247 if (__i == end())
2248 return 0;
2249 erase(__i);
2250 return 1;
2251}
2252
2253template <class _Tp, class _Compare, class _Allocator>
2254template <class _Key>
2255typename __tree<_Tp, _Compare, _Allocator>::size_type
2256__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2257{
2258 pair<iterator, iterator> __p = __equal_range_multi(__k);
2259 size_type __r = 0;
2260 for (; __p.first != __p.second; ++__r)
2261 __p.first = erase(__p.first);
2262 return __r;
2263}
2264
2265template <class _Tp, class _Compare, class _Allocator>
2266template <class _Key>
2267typename __tree<_Tp, _Compare, _Allocator>::iterator
2268__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2269{
2270 iterator __p = __lower_bound(__v, __root(), __end_node());
2271 if (__p != end() && !value_comp()(__v, *__p))
2272 return __p;
2273 return end();
2274}
2275
2276template <class _Tp, class _Compare, class _Allocator>
2277template <class _Key>
2278typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2279__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2280{
2281 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2282 if (__p != end() && !value_comp()(__v, *__p))
2283 return __p;
2284 return end();
2285}
2286
2287template <class _Tp, class _Compare, class _Allocator>
2288template <class _Key>
2289typename __tree<_Tp, _Compare, _Allocator>::size_type
2290__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2291{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002292 __node_pointer __result = __end_node();
2293 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 while (__rt != nullptr)
2295 {
2296 if (value_comp()(__k, __rt->__value_))
2297 {
2298 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002299 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300 }
2301 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002302 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002303 else
2304 return 1;
2305 }
2306 return 0;
2307}
2308
2309template <class _Tp, class _Compare, class _Allocator>
2310template <class _Key>
2311typename __tree<_Tp, _Compare, _Allocator>::size_type
2312__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2313{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002314 __node_pointer __result = __end_node();
2315 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002316 while (__rt != nullptr)
2317 {
2318 if (value_comp()(__k, __rt->__value_))
2319 {
2320 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002321 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002322 }
2323 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002324 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002325 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002326 return _VSTD::distance(
Eric Fiselier227b47c2016-02-20 07:12:17 +00002327 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2328 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 );
2330 }
2331 return 0;
2332}
2333
2334template <class _Tp, class _Compare, class _Allocator>
2335template <class _Key>
2336typename __tree<_Tp, _Compare, _Allocator>::iterator
2337__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2338 __node_pointer __root,
2339 __node_pointer __result)
2340{
2341 while (__root != nullptr)
2342 {
2343 if (!value_comp()(__root->__value_, __v))
2344 {
2345 __result = __root;
2346 __root = static_cast<__node_pointer>(__root->__left_);
2347 }
2348 else
2349 __root = static_cast<__node_pointer>(__root->__right_);
2350 }
2351 return iterator(__result);
2352}
2353
2354template <class _Tp, class _Compare, class _Allocator>
2355template <class _Key>
2356typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2357__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002358 __node_pointer __root,
2359 __node_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002360{
2361 while (__root != nullptr)
2362 {
2363 if (!value_comp()(__root->__value_, __v))
2364 {
2365 __result = __root;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002366 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002367 }
2368 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002369 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 }
2371 return const_iterator(__result);
2372}
2373
2374template <class _Tp, class _Compare, class _Allocator>
2375template <class _Key>
2376typename __tree<_Tp, _Compare, _Allocator>::iterator
2377__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2378 __node_pointer __root,
2379 __node_pointer __result)
2380{
2381 while (__root != nullptr)
2382 {
2383 if (value_comp()(__v, __root->__value_))
2384 {
2385 __result = __root;
2386 __root = static_cast<__node_pointer>(__root->__left_);
2387 }
2388 else
2389 __root = static_cast<__node_pointer>(__root->__right_);
2390 }
2391 return iterator(__result);
2392}
2393
2394template <class _Tp, class _Compare, class _Allocator>
2395template <class _Key>
2396typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2397__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002398 __node_pointer __root,
2399 __node_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002400{
2401 while (__root != nullptr)
2402 {
2403 if (value_comp()(__v, __root->__value_))
2404 {
2405 __result = __root;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002406 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002407 }
2408 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002409 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002410 }
2411 return const_iterator(__result);
2412}
2413
2414template <class _Tp, class _Compare, class _Allocator>
2415template <class _Key>
2416pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2417 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2418__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2419{
Howard Hinnant99968442011-11-29 18:15:50 +00002420 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002421 __node_pointer __result = __end_node();
2422 __node_pointer __rt = __root();
2423 while (__rt != nullptr)
2424 {
2425 if (value_comp()(__k, __rt->__value_))
2426 {
2427 __result = __rt;
2428 __rt = static_cast<__node_pointer>(__rt->__left_);
2429 }
2430 else if (value_comp()(__rt->__value_, __k))
2431 __rt = static_cast<__node_pointer>(__rt->__right_);
2432 else
Howard Hinnant99968442011-11-29 18:15:50 +00002433 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002434 iterator(
2435 __rt->__right_ != nullptr ?
2436 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2437 : __result));
2438 }
Howard Hinnant99968442011-11-29 18:15:50 +00002439 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440}
2441
2442template <class _Tp, class _Compare, class _Allocator>
2443template <class _Key>
2444pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2445 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2446__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2447{
Howard Hinnant99968442011-11-29 18:15:50 +00002448 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002449 __node_pointer __result = __end_node();
2450 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002451 while (__rt != nullptr)
2452 {
2453 if (value_comp()(__k, __rt->__value_))
2454 {
2455 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002456 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002457 }
2458 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002459 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002460 else
Howard Hinnant99968442011-11-29 18:15:50 +00002461 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462 const_iterator(
2463 __rt->__right_ != nullptr ?
Eric Fiselier227b47c2016-02-20 07:12:17 +00002464 static_cast<__node_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 : __result));
2466 }
Howard Hinnant99968442011-11-29 18:15:50 +00002467 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002468}
2469
2470template <class _Tp, class _Compare, class _Allocator>
2471template <class _Key>
2472pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2473 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2474__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2475{
Howard Hinnant99968442011-11-29 18:15:50 +00002476 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002477 __node_pointer __result = __end_node();
2478 __node_pointer __rt = __root();
2479 while (__rt != nullptr)
2480 {
2481 if (value_comp()(__k, __rt->__value_))
2482 {
2483 __result = __rt;
2484 __rt = static_cast<__node_pointer>(__rt->__left_);
2485 }
2486 else if (value_comp()(__rt->__value_, __k))
2487 __rt = static_cast<__node_pointer>(__rt->__right_);
2488 else
Howard Hinnant99968442011-11-29 18:15:50 +00002489 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002490 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2491 }
Howard Hinnant99968442011-11-29 18:15:50 +00002492 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002493}
2494
2495template <class _Tp, class _Compare, class _Allocator>
2496template <class _Key>
2497pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2498 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2499__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2500{
Howard Hinnant99968442011-11-29 18:15:50 +00002501 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002502 __node_pointer __result = __end_node();
2503 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002504 while (__rt != nullptr)
2505 {
2506 if (value_comp()(__k, __rt->__value_))
2507 {
2508 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002509 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 }
2511 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002512 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002513 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002514 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2515 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002516 }
Howard Hinnant99968442011-11-29 18:15:50 +00002517 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002518}
2519
2520template <class _Tp, class _Compare, class _Allocator>
2521typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002522__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002523{
Howard Hinnant70342b92013-06-19 21:29:40 +00002524 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002525 if (__begin_node() == __np)
2526 {
2527 if (__np->__right_ != nullptr)
2528 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2529 else
2530 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2531 }
2532 --size();
2533 __tree_remove(__end_node()->__left_,
2534 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002535 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002536}
2537
Howard Hinnant7686add2011-06-04 14:31:57 +00002538template <class _Tp, class _Compare, class _Allocator>
2539inline _LIBCPP_INLINE_VISIBILITY
2540void
2541swap(__tree<_Tp, _Compare, _Allocator>& __x,
2542 __tree<_Tp, _Compare, _Allocator>& __y)
2543 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2544{
2545 __x.swap(__y);
2546}
2547
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002548_LIBCPP_END_NAMESPACE_STD
2549
2550#endif // _LIBCPP___TREE