blob: 62178e3dc67e348371d318699ab53a1952f18c74 [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;
Dan Albert99ca8202015-03-04 14:08:24 -080031template <class _Key, class _Tp, class _Compare, class _Allocator>
32 class _LIBCPP_TYPE_VIS_ONLY map;
33template <class _Key, class _Tp, class _Compare, class _Allocator>
34 class _LIBCPP_TYPE_VIS_ONLY multimap;
35template <class _Key, class _Compare, class _Allocator>
36 class _LIBCPP_TYPE_VIS_ONLY set;
37template <class _Key, class _Compare, class _Allocator>
38 class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039
Eric Fiselier55263482016-02-20 05:28:30 +000040template <class _Pointer> class __tree_end_node;
41template <class _VoidPtr> class __tree_node_base;
42template <class _Tp, class _VoidPtr> class __tree_node;
43
44#ifndef _LIBCPP_CXX03_LANG
45template <class _Key, class _Value>
46union __value_type;
47#else
48template <class _Key, class _Value>
49struct __value_type;
50#endif
51
52template <class _Allocator> class __map_node_destructor;
53template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
54template <class _TreeIterator> class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
55
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056/*
57
58_NodePtr algorithms
59
60The algorithms taking _NodePtr are red black tree algorithms. Those
61algorithms taking a parameter named __root should assume that __root
62points to a proper red black tree (unless otherwise specified).
63
64Each algorithm herein assumes that __root->__parent_ points to a non-null
65structure which has a member __left_ which points back to __root. No other
66member is read or written to at __root->__parent_.
67
68__root->__parent_ will be referred to below (in comments only) as end_node.
69end_node->__left_ is an externably accessible lvalue for __root, and can be
70changed by node insertion and removal (without explicit reference to end_node).
71
72All nodes (with the exception of end_node), even the node referred to as
73__root, have a non-null __parent_ field.
74
75*/
76
77// Returns: true if __x is a left child of its parent, else false
78// Precondition: __x != nullptr.
79template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +000080inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081bool
Howard Hinnant8b537682011-06-04 17:10:24 +000082__tree_is_left_child(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083{
84 return __x == __x->__parent_->__left_;
85}
86
87// Determintes if the subtree rooted at __x is a proper red black subtree. If
88// __x is a proper subtree, returns the black height (null counts as 1). If
89// __x is an improper subtree, returns 0.
90template <class _NodePtr>
91unsigned
92__tree_sub_invariant(_NodePtr __x)
93{
94 if (__x == nullptr)
95 return 1;
96 // parent consistency checked by caller
97 // check __x->__left_ consistency
98 if (__x->__left_ != nullptr && __x->__left_->__parent_ != __x)
99 return 0;
100 // check __x->__right_ consistency
101 if (__x->__right_ != nullptr && __x->__right_->__parent_ != __x)
102 return 0;
103 // check __x->__left_ != __x->__right_ unless both are nullptr
104 if (__x->__left_ == __x->__right_ && __x->__left_ != nullptr)
105 return 0;
106 // If this is red, neither child can be red
107 if (!__x->__is_black_)
108 {
109 if (__x->__left_ && !__x->__left_->__is_black_)
110 return 0;
111 if (__x->__right_ && !__x->__right_->__is_black_)
112 return 0;
113 }
114 unsigned __h = __tree_sub_invariant(__x->__left_);
115 if (__h == 0)
116 return 0; // invalid left subtree
117 if (__h != __tree_sub_invariant(__x->__right_))
118 return 0; // invalid or different height right subtree
119 return __h + __x->__is_black_; // return black height of this node
120}
121
122// Determintes if the red black tree rooted at __root is a proper red black tree.
123// __root == nullptr is a proper tree. Returns true is __root is a proper
124// red black tree, else returns false.
125template <class _NodePtr>
126bool
127__tree_invariant(_NodePtr __root)
128{
129 if (__root == nullptr)
130 return true;
131 // check __x->__parent_ consistency
132 if (__root->__parent_ == nullptr)
133 return false;
134 if (!__tree_is_left_child(__root))
135 return false;
136 // root must be black
137 if (!__root->__is_black_)
138 return false;
139 // do normal node checks
140 return __tree_sub_invariant(__root) != 0;
141}
142
143// Returns: pointer to the left-most node under __x.
144// Precondition: __x != nullptr.
145template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000146inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000148__tree_min(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000149{
150 while (__x->__left_ != nullptr)
151 __x = __x->__left_;
152 return __x;
153}
154
155// Returns: pointer to the right-most node under __x.
156// Precondition: __x != nullptr.
157template <class _NodePtr>
Howard Hinnant333f50d2010-09-21 20:16:37 +0000158inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000160__tree_max(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161{
162 while (__x->__right_ != nullptr)
163 __x = __x->__right_;
164 return __x;
165}
166
167// Returns: pointer to the next in-order node after __x.
168// Precondition: __x != nullptr.
169template <class _NodePtr>
170_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000171__tree_next(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000172{
173 if (__x->__right_ != nullptr)
174 return __tree_min(__x->__right_);
175 while (!__tree_is_left_child(__x))
176 __x = __x->__parent_;
177 return __x->__parent_;
178}
179
180// Returns: pointer to the previous in-order node before __x.
181// Precondition: __x != nullptr.
182template <class _NodePtr>
183_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000184__tree_prev(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185{
186 if (__x->__left_ != nullptr)
187 return __tree_max(__x->__left_);
188 while (__tree_is_left_child(__x))
189 __x = __x->__parent_;
190 return __x->__parent_;
191}
192
193// Returns: pointer to a node which has no children
194// Precondition: __x != nullptr.
195template <class _NodePtr>
196_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000197__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198{
199 while (true)
200 {
201 if (__x->__left_ != nullptr)
202 {
203 __x = __x->__left_;
204 continue;
205 }
206 if (__x->__right_ != nullptr)
207 {
208 __x = __x->__right_;
209 continue;
210 }
211 break;
212 }
213 return __x;
214}
215
216// Effects: Makes __x->__right_ the subtree root with __x as its left child
217// while preserving in-order order.
218// Precondition: __x->__right_ != nullptr
219template <class _NodePtr>
220void
Howard Hinnant8b537682011-06-04 17:10:24 +0000221__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222{
223 _NodePtr __y = __x->__right_;
224 __x->__right_ = __y->__left_;
225 if (__x->__right_ != nullptr)
226 __x->__right_->__parent_ = __x;
227 __y->__parent_ = __x->__parent_;
228 if (__tree_is_left_child(__x))
229 __x->__parent_->__left_ = __y;
230 else
231 __x->__parent_->__right_ = __y;
232 __y->__left_ = __x;
233 __x->__parent_ = __y;
234}
235
236// Effects: Makes __x->__left_ the subtree root with __x as its right child
237// while preserving in-order order.
238// Precondition: __x->__left_ != nullptr
239template <class _NodePtr>
240void
Howard Hinnant8b537682011-06-04 17:10:24 +0000241__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242{
243 _NodePtr __y = __x->__left_;
244 __x->__left_ = __y->__right_;
245 if (__x->__left_ != nullptr)
246 __x->__left_->__parent_ = __x;
247 __y->__parent_ = __x->__parent_;
248 if (__tree_is_left_child(__x))
249 __x->__parent_->__left_ = __y;
250 else
251 __x->__parent_->__right_ = __y;
252 __y->__right_ = __x;
253 __x->__parent_ = __y;
254}
255
256// Effects: Rebalances __root after attaching __x to a leaf.
257// Precondition: __root != nulptr && __x != nullptr.
258// __x has no children.
259// __x == __root or == a direct or indirect child of __root.
260// If __x were to be unlinked from __root (setting __root to
261// nullptr if __root == __x), __tree_invariant(__root) == true.
262// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
263// may be different than the value passed in as __root.
264template <class _NodePtr>
265void
Howard Hinnant8b537682011-06-04 17:10:24 +0000266__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267{
268 __x->__is_black_ = __x == __root;
269 while (__x != __root && !__x->__parent_->__is_black_)
270 {
271 // __x->__parent_ != __root because __x->__parent_->__is_black == false
272 if (__tree_is_left_child(__x->__parent_))
273 {
274 _NodePtr __y = __x->__parent_->__parent_->__right_;
275 if (__y != nullptr && !__y->__is_black_)
276 {
277 __x = __x->__parent_;
278 __x->__is_black_ = true;
279 __x = __x->__parent_;
280 __x->__is_black_ = __x == __root;
281 __y->__is_black_ = true;
282 }
283 else
284 {
285 if (!__tree_is_left_child(__x))
286 {
287 __x = __x->__parent_;
288 __tree_left_rotate(__x);
289 }
290 __x = __x->__parent_;
291 __x->__is_black_ = true;
292 __x = __x->__parent_;
293 __x->__is_black_ = false;
294 __tree_right_rotate(__x);
295 break;
296 }
297 }
298 else
299 {
300 _NodePtr __y = __x->__parent_->__parent_->__left_;
301 if (__y != nullptr && !__y->__is_black_)
302 {
303 __x = __x->__parent_;
304 __x->__is_black_ = true;
305 __x = __x->__parent_;
306 __x->__is_black_ = __x == __root;
307 __y->__is_black_ = true;
308 }
309 else
310 {
311 if (__tree_is_left_child(__x))
312 {
313 __x = __x->__parent_;
314 __tree_right_rotate(__x);
315 }
316 __x = __x->__parent_;
317 __x->__is_black_ = true;
318 __x = __x->__parent_;
319 __x->__is_black_ = false;
320 __tree_left_rotate(__x);
321 break;
322 }
323 }
324 }
325}
326
327// Precondition: __root != nullptr && __z != nullptr.
328// __tree_invariant(__root) == true.
329// __z == __root or == a direct or indirect child of __root.
330// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
331// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
332// nor any of its children refer to __z. end_node->__left_
333// may be different than the value passed in as __root.
334template <class _NodePtr>
335void
Howard Hinnant8b537682011-06-04 17:10:24 +0000336__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000337{
338 // __z will be removed from the tree. Client still needs to destruct/deallocate it
339 // __y is either __z, or if __z has two children, __tree_next(__z).
340 // __y will have at most one child.
341 // __y will be the initial hole in the tree (make the hole at a leaf)
342 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
343 __z : __tree_next(__z);
344 // __x is __y's possibly null single child
345 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
346 // __w is __x's possibly null uncle (will become __x's sibling)
347 _NodePtr __w = nullptr;
348 // link __x to __y's parent, and find __w
349 if (__x != nullptr)
350 __x->__parent_ = __y->__parent_;
351 if (__tree_is_left_child(__y))
352 {
353 __y->__parent_->__left_ = __x;
354 if (__y != __root)
355 __w = __y->__parent_->__right_;
356 else
357 __root = __x; // __w == nullptr
358 }
359 else
360 {
361 __y->__parent_->__right_ = __x;
362 // __y can't be root if it is a right child
363 __w = __y->__parent_->__left_;
364 }
365 bool __removed_black = __y->__is_black_;
366 // If we didn't remove __z, do so now by splicing in __y for __z,
367 // but copy __z's color. This does not impact __x or __w.
368 if (__y != __z)
369 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000370 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371 __y->__parent_ = __z->__parent_;
372 if (__tree_is_left_child(__z))
373 __y->__parent_->__left_ = __y;
374 else
375 __y->__parent_->__right_ = __y;
376 __y->__left_ = __z->__left_;
377 __y->__left_->__parent_ = __y;
378 __y->__right_ = __z->__right_;
379 if (__y->__right_ != nullptr)
380 __y->__right_->__parent_ = __y;
381 __y->__is_black_ = __z->__is_black_;
382 if (__root == __z)
383 __root = __y;
384 }
385 // There is no need to rebalance if we removed a red, or if we removed
386 // the last node.
387 if (__removed_black && __root != nullptr)
388 {
389 // Rebalance:
390 // __x has an implicit black color (transferred from the removed __y)
391 // associated with it, no matter what its color is.
392 // If __x is __root (in which case it can't be null), it is supposed
393 // to be black anyway, and if it is doubly black, then the double
394 // can just be ignored.
395 // If __x is red (in which case it can't be null), then it can absorb
396 // the implicit black just by setting its color to black.
397 // Since __y was black and only had one child (which __x points to), __x
398 // is either red with no children, else null, otherwise __y would have
399 // different black heights under left and right pointers.
400 // if (__x == __root || __x != nullptr && !__x->__is_black_)
401 if (__x != nullptr)
402 __x->__is_black_ = true;
403 else
404 {
405 // Else __x isn't root, and is "doubly black", even though it may
406 // be null. __w can not be null here, else the parent would
407 // see a black height >= 2 on the __x side and a black height
408 // of 1 on the __w side (__w must be a non-null black or a red
409 // with a non-null black child).
410 while (true)
411 {
412 if (!__tree_is_left_child(__w)) // if x is left child
413 {
414 if (!__w->__is_black_)
415 {
416 __w->__is_black_ = true;
417 __w->__parent_->__is_black_ = false;
418 __tree_left_rotate(__w->__parent_);
419 // __x is still valid
420 // reset __root only if necessary
421 if (__root == __w->__left_)
422 __root = __w;
423 // reset sibling, and it still can't be null
424 __w = __w->__left_->__right_;
425 }
426 // __w->__is_black_ is now true, __w may have null children
427 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
428 (__w->__right_ == nullptr || __w->__right_->__is_black_))
429 {
430 __w->__is_black_ = false;
431 __x = __w->__parent_;
432 // __x can no longer be null
433 if (__x == __root || !__x->__is_black_)
434 {
435 __x->__is_black_ = true;
436 break;
437 }
438 // reset sibling, and it still can't be null
439 __w = __tree_is_left_child(__x) ?
Howard Hinnant324bb032010-08-22 00:02:43 +0000440 __x->__parent_->__right_ :
441 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442 // continue;
443 }
444 else // __w has a red child
445 {
446 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
447 {
448 // __w left child is non-null and red
449 __w->__left_->__is_black_ = true;
450 __w->__is_black_ = false;
451 __tree_right_rotate(__w);
452 // __w is known not to be root, so root hasn't changed
453 // reset sibling, and it still can't be null
454 __w = __w->__parent_;
455 }
456 // __w has a right red child, left child may be null
457 __w->__is_black_ = __w->__parent_->__is_black_;
458 __w->__parent_->__is_black_ = true;
459 __w->__right_->__is_black_ = true;
460 __tree_left_rotate(__w->__parent_);
461 break;
462 }
463 }
464 else
465 {
466 if (!__w->__is_black_)
467 {
468 __w->__is_black_ = true;
469 __w->__parent_->__is_black_ = false;
470 __tree_right_rotate(__w->__parent_);
471 // __x is still valid
472 // reset __root only if necessary
473 if (__root == __w->__right_)
474 __root = __w;
475 // reset sibling, and it still can't be null
476 __w = __w->__right_->__left_;
477 }
478 // __w->__is_black_ is now true, __w may have null children
479 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
480 (__w->__right_ == nullptr || __w->__right_->__is_black_))
481 {
482 __w->__is_black_ = false;
483 __x = __w->__parent_;
484 // __x can no longer be null
485 if (!__x->__is_black_ || __x == __root)
486 {
487 __x->__is_black_ = true;
488 break;
489 }
490 // reset sibling, and it still can't be null
491 __w = __tree_is_left_child(__x) ?
Howard Hinnant324bb032010-08-22 00:02:43 +0000492 __x->__parent_->__right_ :
493 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494 // continue;
495 }
496 else // __w has a red child
497 {
498 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
499 {
500 // __w right child is non-null and red
501 __w->__right_->__is_black_ = true;
502 __w->__is_black_ = false;
503 __tree_left_rotate(__w);
504 // __w is known not to be root, so root hasn't changed
505 // reset sibling, and it still can't be null
506 __w = __w->__parent_;
507 }
508 // __w has a left red child, right child may be null
509 __w->__is_black_ = __w->__parent_->__is_black_;
510 __w->__parent_->__is_black_ = true;
511 __w->__left_->__is_black_ = true;
512 __tree_right_rotate(__w->__parent_);
513 break;
514 }
515 }
516 }
517 }
518 }
519}
520
Eric Fiselier55263482016-02-20 05:28:30 +0000521// node traits
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522
Eric Fiselierdb215062016-03-31 02:15:15 +0000523
524#ifndef _LIBCPP_CXX03_LANG
525template <class _Tp>
526struct __is_tree_value_type_imp : false_type {};
527
528template <class _Key, class _Value>
529struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
530
531template <class ..._Args>
532struct __is_tree_value_type : false_type {};
533
534template <class _One>
535struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
536#endif
537
Eric Fiselier55263482016-02-20 05:28:30 +0000538template <class _Tp>
539struct __tree_key_value_types {
540 typedef _Tp key_type;
541 typedef _Tp __node_value_type;
542 typedef _Tp __container_value_type;
543 static const bool __is_map = false;
Eric Fiselierdb215062016-03-31 02:15:15 +0000544
545 _LIBCPP_INLINE_VISIBILITY
546 static key_type const& __get_key(_Tp const& __v) {
547 return __v;
548 }
549 _LIBCPP_INLINE_VISIBILITY
550 static __container_value_type const& __get_value(__node_value_type const& __v) {
551 return __v;
552 }
553 _LIBCPP_INLINE_VISIBILITY
554 static __container_value_type* __get_ptr(__node_value_type& __n) {
555 return _VSTD::addressof(__n);
556 }
557
558#ifndef _LIBCPP_CXX03_LANG
559 _LIBCPP_INLINE_VISIBILITY
560 static __container_value_type&& __move(__node_value_type& __v) {
561 return _VSTD::move(__v);
562 }
563#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000564};
565
566template <class _Key, class _Tp>
567struct __tree_key_value_types<__value_type<_Key, _Tp> > {
568 typedef _Key key_type;
569 typedef _Tp mapped_type;
570 typedef __value_type<_Key, _Tp> __node_value_type;
571 typedef pair<const _Key, _Tp> __container_value_type;
572 typedef pair<_Key, _Tp> __nc_value_type;
573 typedef __container_value_type __map_value_type;
574 static const bool __is_map = true;
Eric Fiselierdb215062016-03-31 02:15:15 +0000575
576 _LIBCPP_INLINE_VISIBILITY
577 static key_type const&
578 __get_key(__node_value_type const& __t) {
579 return __t.__cc.first;
580 }
581
582 template <class _Up>
583 _LIBCPP_INLINE_VISIBILITY
584 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
585 key_type const&>::type
586 __get_key(_Up& __t) {
587 return __t.first;
588 }
589
590 _LIBCPP_INLINE_VISIBILITY
591 static __container_value_type const&
592 __get_value(__node_value_type const& __t) {
593 return __t.__cc;
594 }
595
596 template <class _Up>
597 _LIBCPP_INLINE_VISIBILITY
598 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
599 __container_value_type const&>::type
600 __get_value(_Up& __t) {
601 return __t;
602 }
603
604 _LIBCPP_INLINE_VISIBILITY
605 static __container_value_type* __get_ptr(__node_value_type& __n) {
606 return _VSTD::addressof(__n.__cc);
607 }
608
609#ifndef _LIBCPP_CXX03_LANG
610 _LIBCPP_INLINE_VISIBILITY
611 static __nc_value_type&& __move(__node_value_type& __v) {
612 return _VSTD::move(__v.__nc);
613 }
614#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000615};
616
617template <class _VoidPtr>
618struct __tree_node_base_types {
619 typedef _VoidPtr __void_pointer;
620
621 typedef __tree_node_base<__void_pointer> __node_base_type;
622 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
623 __node_base_pointer;
624
625 typedef __tree_end_node<__node_base_pointer> __end_node_type;
626 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
627 __end_node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000628private:
Eric Fiselier55263482016-02-20 05:28:30 +0000629 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
630 "_VoidPtr does not point to unqualified void type");
631};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632
Eric Fiselier55263482016-02-20 05:28:30 +0000633template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
634 bool = _KVTypes::__is_map>
635struct __tree_map_pointer_types {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000636
Eric Fiselier55263482016-02-20 05:28:30 +0000637template <class _Tp, class _AllocPtr, class _KVTypes>
638struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
639 typedef typename _KVTypes::__map_value_type _Mv;
640 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
641 __map_value_type_pointer;
642 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
643 __const_map_value_type_pointer;
644};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000645
Eric Fiselier55263482016-02-20 05:28:30 +0000646template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
647struct __tree_node_types;
648
649template <class _NodePtr, class _Tp, class _VoidPtr>
650struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
651 : public __tree_node_base_types<_VoidPtr>,
652 __tree_key_value_types<_Tp>,
653 __tree_map_pointer_types<_Tp, _VoidPtr>
654{
655 typedef __tree_node_base_types<_VoidPtr> __base;
656 typedef __tree_key_value_types<_Tp> __key_base;
657 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658public:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000659
Eric Fiselier55263482016-02-20 05:28:30 +0000660 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
661 typedef _NodePtr __node_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000662
Eric Fiselier55263482016-02-20 05:28:30 +0000663 typedef _Tp __node_value_type;
664 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
665 __node_value_type_pointer;
666 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
667 __const_node_value_type_pointer;
668private:
669 static_assert(!is_const<__node_type>::value,
670 "_NodePtr should never be a pointer to const");
671 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
672 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
673};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674
Eric Fiselier55263482016-02-20 05:28:30 +0000675template <class _ValueTp, class _VoidPtr>
676struct __make_tree_node_types {
677 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
678 _NodePtr;
679 typedef __tree_node_types<_NodePtr> type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000680};
681
682// node
683
684template <class _Pointer>
685class __tree_end_node
686{
687public:
688 typedef _Pointer pointer;
689 pointer __left_;
690
Howard Hinnant333f50d2010-09-21 20:16:37 +0000691 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000692 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000693};
694
695template <class _VoidPtr>
696class __tree_node_base
Eric Fiselier55263482016-02-20 05:28:30 +0000697 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698{
Eric Fiselier55263482016-02-20 05:28:30 +0000699 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
700
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000701public:
Eric Fiselier55263482016-02-20 05:28:30 +0000702 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703
704 pointer __right_;
705 pointer __parent_;
706 bool __is_black_;
707
Eric Fiselierdb215062016-03-31 02:15:15 +0000708private:
709 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
710 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
711 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000712};
713
714template <class _Tp, class _VoidPtr>
715class __tree_node
716 : public __tree_node_base<_VoidPtr>
717{
718public:
Eric Fiselier55263482016-02-20 05:28:30 +0000719 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720
Eric Fiselier55263482016-02-20 05:28:30 +0000721 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000722
Eric Fiselierdb215062016-03-31 02:15:15 +0000723private:
724 ~__tree_node() _LIBCPP_EQUAL_DELETE;
725 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
726 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000727};
728
Eric Fiselierdb215062016-03-31 02:15:15 +0000729
730template <class _Allocator>
731class __tree_node_destructor
732{
733 typedef _Allocator allocator_type;
734 typedef allocator_traits<allocator_type> __alloc_traits;
735
736public:
737 typedef typename __alloc_traits::pointer pointer;
738private:
739 typedef __tree_node_types<pointer> _NodeTypes;
740 allocator_type& __na_;
741
742 __tree_node_destructor& operator=(const __tree_node_destructor&);
743
744public:
745 bool __value_constructed;
746
747 _LIBCPP_INLINE_VISIBILITY
748 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
749 : __na_(__na),
750 __value_constructed(__val)
751 {}
752
753 _LIBCPP_INLINE_VISIBILITY
754 void operator()(pointer __p) _NOEXCEPT
755 {
756 if (__value_constructed)
757 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
758 if (__p)
759 __alloc_traits::deallocate(__na_, __p, 1);
760 }
761
762 template <class> friend class __map_node_destructor;
763};
764
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000765
766template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000767class _LIBCPP_TYPE_VIS_ONLY __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768{
Eric Fiselier55263482016-02-20 05:28:30 +0000769 typedef __tree_node_types<_NodePtr> _NodeTypes;
770 typedef _NodePtr __node_pointer;
771 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
772 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000773
774 __node_pointer __ptr_;
775
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000776public:
Eric Fiselier55263482016-02-20 05:28:30 +0000777 typedef bidirectional_iterator_tag iterator_category;
778 typedef _Tp value_type;
779 typedef _DiffType difference_type;
780 typedef value_type& reference;
781 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000782
Marshall Clow051c8482013-08-08 21:52:50 +0000783 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
784#if _LIBCPP_STD_VER > 11
785 : __ptr_(nullptr)
786#endif
787 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000788
Howard Hinnant333f50d2010-09-21 20:16:37 +0000789 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000790 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
791 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000792
Howard Hinnant333f50d2010-09-21 20:16:37 +0000793 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000794 __tree_iterator& operator++() {
795 __ptr_ = static_cast<__node_pointer>(
Eric Fiselier55263482016-02-20 05:28:30 +0000796 __tree_next(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000797 return *this;
798 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000799 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000800 __tree_iterator operator++(int)
801 {__tree_iterator __t(*this); ++(*this); return __t;}
802
Howard Hinnant333f50d2010-09-21 20:16:37 +0000803 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000804 __tree_iterator& operator--() {
805 __ptr_ = static_cast<__node_pointer>(
Eric Fiselier55263482016-02-20 05:28:30 +0000806 __tree_prev(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000807 return *this;
808 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000809 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000810 __tree_iterator operator--(int)
811 {__tree_iterator __t(*this); --(*this); return __t;}
812
Howard Hinnant333f50d2010-09-21 20:16:37 +0000813 friend _LIBCPP_INLINE_VISIBILITY
814 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000816 friend _LIBCPP_INLINE_VISIBILITY
817 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818 {return !(__x == __y);}
819
820private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000822 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000823 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000824 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator;
825 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_iterator;
826 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
827 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
828 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
829 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000830};
831
Eric Fiselier55263482016-02-20 05:28:30 +0000832template <class _Tp, class _NodePtr, class _DiffType>
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000833class _LIBCPP_TYPE_VIS_ONLY __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834{
Eric Fiselier55263482016-02-20 05:28:30 +0000835 typedef __tree_node_types<_NodePtr> _NodeTypes;
836 typedef typename _NodeTypes::__node_pointer __node_pointer;
837 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
838 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000839
840 __node_pointer __ptr_;
841
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000842public:
Eric Fiselier55263482016-02-20 05:28:30 +0000843 typedef bidirectional_iterator_tag iterator_category;
844 typedef _Tp value_type;
845 typedef _DiffType difference_type;
846 typedef const value_type& reference;
847 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848
Marshall Clow051c8482013-08-08 21:52:50 +0000849 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
850#if _LIBCPP_STD_VER > 11
851 : __ptr_(nullptr)
852#endif
853 {}
854
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000855private:
Eric Fiselier55263482016-02-20 05:28:30 +0000856 typedef __tree_iterator<value_type, __node_pointer, difference_type>
857 __non_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000858public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000859 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000860 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
861 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862
Howard Hinnant333f50d2010-09-21 20:16:37 +0000863 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return __ptr_->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000864 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
865 {return pointer_traits<pointer>::pointer_to(__ptr_->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000866
Howard Hinnant333f50d2010-09-21 20:16:37 +0000867 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000868 __tree_const_iterator& operator++() {
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000869 __ptr_ = static_cast<__node_pointer>(
870 __tree_next(static_cast<__node_base_pointer>(__ptr_)));
871 return *this;
872 }
873
Howard Hinnant333f50d2010-09-21 20:16:37 +0000874 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000875 __tree_const_iterator operator++(int)
876 {__tree_const_iterator __t(*this); ++(*this); return __t;}
877
Howard Hinnant333f50d2010-09-21 20:16:37 +0000878 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000879 __tree_const_iterator& operator--() {
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000880 __ptr_ = static_cast<__node_pointer>(
881 __tree_prev(static_cast<__node_base_pointer>(__ptr_)));
882 return *this;
883 }
884
Howard Hinnant333f50d2010-09-21 20:16:37 +0000885 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000886 __tree_const_iterator operator--(int)
887 {__tree_const_iterator __t(*this); --(*this); return __t;}
888
Howard Hinnant333f50d2010-09-21 20:16:37 +0000889 friend _LIBCPP_INLINE_VISIBILITY
890 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000892 friend _LIBCPP_INLINE_VISIBILITY
893 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000894 {return !(__x == __y);}
895
896private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000897 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000898 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
899 : __ptr_(__p) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000900 template <class, class, class> friend class __tree;
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000901 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
902 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
903 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY set;
904 template <class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multiset;
905 template <class> friend class _LIBCPP_TYPE_VIS_ONLY __map_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000906};
907
908template <class _Tp, class _Compare, class _Allocator>
909class __tree
910{
911public:
912 typedef _Tp value_type;
913 typedef _Compare value_compare;
914 typedef _Allocator allocator_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000915
916private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier55263482016-02-20 05:28:30 +0000918 typedef typename __make_tree_node_types<value_type,
919 typename __alloc_traits::void_pointer>::type
920 _NodeTypes;
Eric Fiselierdb215062016-03-31 02:15:15 +0000921 typedef typename _NodeTypes::key_type key_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000922public:
923 typedef typename _NodeTypes::__node_value_type __node_value_type;
924 typedef typename _NodeTypes::__container_value_type __container_value_type;
925
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926 typedef typename __alloc_traits::pointer pointer;
927 typedef typename __alloc_traits::const_pointer const_pointer;
928 typedef typename __alloc_traits::size_type size_type;
929 typedef typename __alloc_traits::difference_type difference_type;
930
Eric Fiselier55263482016-02-20 05:28:30 +0000931public:
932 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +0000933
Eric Fiselier55263482016-02-20 05:28:30 +0000934 typedef typename _NodeTypes::__node_type __node;
935 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000936
937 typedef typename _NodeTypes::__node_base_type __node_base;
938 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000939
940 typedef typename _NodeTypes::__end_node_type __end_node_t;
941 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiselier55263482016-02-20 05:28:30 +0000942
Marshall Clow66302c62015-04-07 05:21:38 +0000943 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiselier55263482016-02-20 05:28:30 +0000944 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945
Eric Fiselier55263482016-02-20 05:28:30 +0000946private:
947 // check for sane allocator pointer rebinding semantics. Rebinding the
948 // allocator for a new pointer type should be exactly the same as rebinding
949 // the pointer using 'pointer_traits'.
950 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
951 "Allocator does not rebind pointers in a sane manner.");
952 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
953 __node_base_allocator;
954 typedef allocator_traits<__node_base_allocator> __node_base_traits;
955 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
956 "Allocator does not rebind pointers in a sane manner.");
957
958private:
Eric Fiselier227b47c2016-02-20 07:12:17 +0000959 __node_pointer __begin_node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000960 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
961 __compressed_pair<size_type, value_compare> __pair3_;
962
963public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000964 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000965 __node_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000966 {
Eric Fiselier227b47c2016-02-20 07:12:17 +0000967 return static_cast<__node_pointer>(
968 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
969 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000970 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000971 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +0000972 __node_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000973 {
Eric Fiselier227b47c2016-02-20 07:12:17 +0000974 return static_cast<__node_pointer>(
975 pointer_traits<__end_node_ptr>::pointer_to(
976 const_cast<__end_node_t&>(__pair1_.first())
977 )
978 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000979 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000980 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000981 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000982private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000983 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000984 const __node_allocator& __node_alloc() const _NOEXCEPT
985 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000986 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000987 __node_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000988 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000989 const __node_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000990public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000991 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000992 allocator_type __alloc() const _NOEXCEPT
993 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000995 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000996 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000998 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000999 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001000 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001001 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001002 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001003 const value_compare& value_comp() const _NOEXCEPT
1004 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001005public:
Eric Fiselier227b47c2016-02-20 07:12:17 +00001006
Howard Hinnant333f50d2010-09-21 20:16:37 +00001007 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +00001008 __node_pointer __root() const _NOEXCEPT
1009 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001010
1011 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +00001012 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001013
Howard Hinnant7686add2011-06-04 14:31:57 +00001014 explicit __tree(const value_compare& __comp)
1015 _NOEXCEPT_(
1016 is_nothrow_default_constructible<__node_allocator>::value &&
1017 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018 explicit __tree(const allocator_type& __a);
1019 __tree(const value_compare& __comp, const allocator_type& __a);
1020 __tree(const __tree& __t);
1021 __tree& operator=(const __tree& __t);
1022 template <class _InputIterator>
1023 void __assign_unique(_InputIterator __first, _InputIterator __last);
1024 template <class _InputIterator>
1025 void __assign_multi(_InputIterator __first, _InputIterator __last);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001026#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant7686add2011-06-04 14:31:57 +00001027 __tree(__tree&& __t)
1028 _NOEXCEPT_(
1029 is_nothrow_move_constructible<__node_allocator>::value &&
1030 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001031 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +00001032 __tree& operator=(__tree&& __t)
1033 _NOEXCEPT_(
1034 __node_traits::propagate_on_container_move_assignment::value &&
1035 is_nothrow_move_assignable<value_compare>::value &&
1036 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnant73d21a42010-09-04 23:28:19 +00001037#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001038
1039 ~__tree();
1040
Howard Hinnant333f50d2010-09-21 20:16:37 +00001041 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001042 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001043 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001044 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001045 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001046 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001047 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001048 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001049
Howard Hinnant333f50d2010-09-21 20:16:37 +00001050 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001051 size_type max_size() const _NOEXCEPT
1052 {return __node_traits::max_size(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053
Howard Hinnant7686add2011-06-04 14:31:57 +00001054 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001055
Howard Hinnant7686add2011-06-04 14:31:57 +00001056 void swap(__tree& __t)
1057 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001058 __is_nothrow_swappable<value_compare>::value
1059#if _LIBCPP_STD_VER <= 11
1060 && (!__node_traits::propagate_on_container_swap::value ||
1061 __is_nothrow_swappable<__node_allocator>::value)
1062#endif
1063 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001064
Eric Fiselierdb215062016-03-31 02:15:15 +00001065
1066#ifndef _LIBCPP_CXX03_LANG
1067 template <class _Key, class ..._Args>
1068 pair<iterator, bool>
1069 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1070 template <class _Key, class ..._Args>
1071 iterator
1072 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001073
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001075 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierdb215062016-03-31 02:15:15 +00001076
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001078 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001079
Eric Fiselierdb215062016-03-31 02:15:15 +00001080 template <class... _Args>
1081 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001082
Eric Fiselierdb215062016-03-31 02:15:15 +00001083 template <class... _Args>
1084 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001085
1086 template <class _Pp>
1087 _LIBCPP_INLINE_VISIBILITY
1088 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1089 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1090 __can_extract_key<_Pp, key_type>());
1091 }
1092
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001093 template <class _First, class _Second>
1094 _LIBCPP_INLINE_VISIBILITY
1095 typename enable_if<
1096 __can_extract_map_key<_First, key_type, __container_value_type>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097 pair<iterator, bool>
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001098 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1099 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1100 _VSTD::forward<_Second>(__s));
1101 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102
1103 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001104 _LIBCPP_INLINE_VISIBILITY
1105 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1106 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1107 }
1108
1109 template <class _Pp>
1110 _LIBCPP_INLINE_VISIBILITY
1111 pair<iterator, bool>
1112 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1113 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1114 }
1115
1116 template <class _Pp>
1117 _LIBCPP_INLINE_VISIBILITY
1118 pair<iterator, bool>
1119 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1120 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1121 }
1122
1123 template <class _Pp>
1124 _LIBCPP_INLINE_VISIBILITY
1125 pair<iterator, bool>
1126 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1127 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1128 }
1129
1130 template <class _Pp>
1131 _LIBCPP_INLINE_VISIBILITY
1132 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1133 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1134 __can_extract_key<_Pp, key_type>());
1135 }
1136
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001137 template <class _First, class _Second>
1138 _LIBCPP_INLINE_VISIBILITY
1139 typename enable_if<
1140 __can_extract_map_key<_First, key_type, __container_value_type>::value,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001141 iterator
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001142 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1143 return __emplace_hint_unique_key_args(__p, __f,
1144 _VSTD::forward<_First>(__f),
1145 _VSTD::forward<_Second>(__s));
1146 }
1147
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001148 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001149 _LIBCPP_INLINE_VISIBILITY
1150 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1151 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1152 }
1153
1154 template <class _Pp>
1155 _LIBCPP_INLINE_VISIBILITY
1156 iterator
1157 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1158 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1159 }
1160
1161 template <class _Pp>
1162 _LIBCPP_INLINE_VISIBILITY
1163 iterator
1164 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1165 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1166 }
1167
1168 template <class _Pp>
1169 _LIBCPP_INLINE_VISIBILITY
1170 iterator
1171 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1172 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1173 }
1174
Eric Fiselierdb215062016-03-31 02:15:15 +00001175#else
1176 template <class _Key, class _Args>
1177 _LIBCPP_INLINE_VISIBILITY
1178 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1179 template <class _Key, class _Args>
1180 _LIBCPP_INLINE_VISIBILITY
1181 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001182#endif
1183
Eric Fiselierdb215062016-03-31 02:15:15 +00001184 _LIBCPP_INLINE_VISIBILITY
1185 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1186 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1187 }
1188
1189 _LIBCPP_INLINE_VISIBILITY
1190 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1191 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1192 }
1193
1194#ifdef _LIBCPP_CXX03_LANG
1195 _LIBCPP_INLINE_VISIBILITY
1196 iterator __insert_multi(const __container_value_type& __v);
1197 _LIBCPP_INLINE_VISIBILITY
1198 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1199#else
1200 _LIBCPP_INLINE_VISIBILITY
1201 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1202 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1203 }
1204
1205 _LIBCPP_INLINE_VISIBILITY
1206 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1207 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1208 }
1209
1210 template <class _Vp, class = typename enable_if<
1211 !is_same<typename __unconstref<_Vp>::type,
1212 __container_value_type
1213 >::value
1214 >::type>
1215 _LIBCPP_INLINE_VISIBILITY
1216 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1217 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1218 }
1219
1220 template <class _Vp, class = typename enable_if<
1221 !is_same<typename __unconstref<_Vp>::type,
1222 __container_value_type
1223 >::value
1224 >::type>
1225 _LIBCPP_INLINE_VISIBILITY
1226 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1227 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1228 }
1229
1230 _LIBCPP_INLINE_VISIBILITY
1231 iterator __insert_multi(__container_value_type&& __v) {
1232 return __emplace_multi(_VSTD::move(__v));
1233 }
1234
1235 _LIBCPP_INLINE_VISIBILITY
1236 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1237 return __emplace_hint_multi(__p, _VSTD::move(__v));
1238 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001239
1240 template <class _Vp>
Eric Fiselierdb215062016-03-31 02:15:15 +00001241 _LIBCPP_INLINE_VISIBILITY
1242 iterator __insert_multi(_Vp&& __v) {
1243 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1244 }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001245
Eric Fiselierdb215062016-03-31 02:15:15 +00001246 template <class _Vp>
1247 _LIBCPP_INLINE_VISIBILITY
1248 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1249 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1250 }
1251
1252#endif // !_LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001253
1254 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1255 iterator __node_insert_unique(const_iterator __p,
1256 __node_pointer __nd);
1257
1258 iterator __node_insert_multi(__node_pointer __nd);
1259 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1260
1261 iterator erase(const_iterator __p);
1262 iterator erase(const_iterator __f, const_iterator __l);
1263 template <class _Key>
1264 size_type __erase_unique(const _Key& __k);
1265 template <class _Key>
1266 size_type __erase_multi(const _Key& __k);
1267
1268 void __insert_node_at(__node_base_pointer __parent,
1269 __node_base_pointer& __child,
1270 __node_base_pointer __new_node);
1271
1272 template <class _Key>
1273 iterator find(const _Key& __v);
1274 template <class _Key>
1275 const_iterator find(const _Key& __v) const;
1276
1277 template <class _Key>
1278 size_type __count_unique(const _Key& __k) const;
1279 template <class _Key>
1280 size_type __count_multi(const _Key& __k) const;
1281
1282 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001284 iterator lower_bound(const _Key& __v)
1285 {return __lower_bound(__v, __root(), __end_node());}
1286 template <class _Key>
1287 iterator __lower_bound(const _Key& __v,
1288 __node_pointer __root,
1289 __node_pointer __result);
1290 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001292 const_iterator lower_bound(const _Key& __v) const
1293 {return __lower_bound(__v, __root(), __end_node());}
1294 template <class _Key>
1295 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001296 __node_pointer __root,
1297 __node_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001298 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001299 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001300 iterator upper_bound(const _Key& __v)
1301 {return __upper_bound(__v, __root(), __end_node());}
1302 template <class _Key>
1303 iterator __upper_bound(const _Key& __v,
1304 __node_pointer __root,
1305 __node_pointer __result);
1306 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001307 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001308 const_iterator upper_bound(const _Key& __v) const
1309 {return __upper_bound(__v, __root(), __end_node());}
1310 template <class _Key>
1311 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001312 __node_pointer __root,
1313 __node_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001314 template <class _Key>
1315 pair<iterator, iterator>
1316 __equal_range_unique(const _Key& __k);
1317 template <class _Key>
1318 pair<const_iterator, const_iterator>
1319 __equal_range_unique(const _Key& __k) const;
1320
1321 template <class _Key>
1322 pair<iterator, iterator>
1323 __equal_range_multi(const _Key& __k);
1324 template <class _Key>
1325 pair<const_iterator, const_iterator>
1326 __equal_range_multi(const _Key& __k) const;
1327
Howard Hinnant99968442011-11-29 18:15:50 +00001328 typedef __tree_node_destructor<__node_allocator> _Dp;
1329 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001330
Howard Hinnant8b537682011-06-04 17:10:24 +00001331 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001332private:
Howard Hinnantd615e472011-04-03 20:05:29 +00001333 typename __node_base::pointer&
Eric Fiselierdb215062016-03-31 02:15:15 +00001334 __find_leaf_low(typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantd615e472011-04-03 20:05:29 +00001335 typename __node_base::pointer&
Eric Fiselierdb215062016-03-31 02:15:15 +00001336 __find_leaf_high(typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantd615e472011-04-03 20:05:29 +00001337 typename __node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001338 __find_leaf(const_iterator __hint,
Eric Fiselierdb215062016-03-31 02:15:15 +00001339 typename __node_base::pointer& __parent, const key_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001340 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001341 typename __node_base::pointer&
1342 __find_equal(typename __node_base::pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001343 template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001344 typename __node_base::pointer&
1345 __find_equal(const_iterator __hint, typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001346 const _Key& __v);
1347
Eric Fiselierdb215062016-03-31 02:15:15 +00001348#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001349 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001350 __node_holder __construct_node(_Args&& ...__args);
1351#else
1352 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001353#endif
1354
Howard Hinnant7686add2011-06-04 14:31:57 +00001355 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356
Howard Hinnant333f50d2010-09-21 20:16:37 +00001357 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001358 void __copy_assign_alloc(const __tree& __t)
1359 {__copy_assign_alloc(__t, integral_constant<bool,
1360 __node_traits::propagate_on_container_copy_assignment::value>());}
1361
Howard Hinnant333f50d2010-09-21 20:16:37 +00001362 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001363 void __copy_assign_alloc(const __tree& __t, true_type)
1364 {__node_alloc() = __t.__node_alloc();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001365 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001366 void __copy_assign_alloc(const __tree& __t, false_type) {}
1367
1368 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001369 void __move_assign(__tree& __t, true_type)
1370 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1371 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001372
Howard Hinnant333f50d2010-09-21 20:16:37 +00001373 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001374 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001375 _NOEXCEPT_(
1376 !__node_traits::propagate_on_container_move_assignment::value ||
1377 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001378 {__move_assign_alloc(__t, integral_constant<bool,
1379 __node_traits::propagate_on_container_move_assignment::value>());}
1380
Howard Hinnant333f50d2010-09-21 20:16:37 +00001381 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001382 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001383 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001384 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001385 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001386 void __move_assign_alloc(__tree& __t, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001388 __node_pointer __detach();
1389 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001390
Howard Hinnant0f678bd2013-08-12 18:38:34 +00001391 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY map;
1392 template <class, class, class, class> friend class _LIBCPP_TYPE_VIS_ONLY multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393};
1394
1395template <class _Tp, class _Compare, class _Allocator>
1396__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001397 _NOEXCEPT_(
1398 is_nothrow_default_constructible<__node_allocator>::value &&
1399 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001400 : __pair3_(0, __comp)
1401{
1402 __begin_node() = __end_node();
1403}
1404
1405template <class _Tp, class _Compare, class _Allocator>
1406__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001407 : __begin_node_(__node_pointer()),
1408 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001409 __pair3_(0)
1410{
1411 __begin_node() = __end_node();
1412}
1413
1414template <class _Tp, class _Compare, class _Allocator>
1415__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1416 const allocator_type& __a)
Eric Fiselier02bb4bd2015-07-18 23:56:04 +00001417 : __begin_node_(__node_pointer()),
1418 __pair1_(__node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419 __pair3_(0, __comp)
1420{
1421 __begin_node() = __end_node();
1422}
1423
1424// Precondition: size() != 0
1425template <class _Tp, class _Compare, class _Allocator>
1426typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1427__tree<_Tp, _Compare, _Allocator>::__detach()
1428{
1429 __node_pointer __cache = __begin_node();
1430 __begin_node() = __end_node();
1431 __end_node()->__left_->__parent_ = nullptr;
1432 __end_node()->__left_ = nullptr;
1433 size() = 0;
1434 // __cache->__left_ == nullptr
1435 if (__cache->__right_ != nullptr)
1436 __cache = static_cast<__node_pointer>(__cache->__right_);
1437 // __cache->__left_ == nullptr
1438 // __cache->__right_ == nullptr
1439 return __cache;
1440}
1441
1442// Precondition: __cache != nullptr
1443// __cache->left_ == nullptr
1444// __cache->right_ == nullptr
1445// This is no longer a red-black tree
1446template <class _Tp, class _Compare, class _Allocator>
1447typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1448__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1449{
1450 if (__cache->__parent_ == nullptr)
1451 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001452 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 {
1454 __cache->__parent_->__left_ = nullptr;
1455 __cache = static_cast<__node_pointer>(__cache->__parent_);
1456 if (__cache->__right_ == nullptr)
1457 return __cache;
1458 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1459 }
1460 // __cache is right child
1461 __cache->__parent_->__right_ = nullptr;
1462 __cache = static_cast<__node_pointer>(__cache->__parent_);
1463 if (__cache->__left_ == nullptr)
1464 return __cache;
1465 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1466}
1467
1468template <class _Tp, class _Compare, class _Allocator>
1469__tree<_Tp, _Compare, _Allocator>&
1470__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1471{
1472 if (this != &__t)
1473 {
1474 value_comp() = __t.value_comp();
1475 __copy_assign_alloc(__t);
1476 __assign_multi(__t.begin(), __t.end());
1477 }
1478 return *this;
1479}
1480
1481template <class _Tp, class _Compare, class _Allocator>
1482template <class _InputIterator>
1483void
1484__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1485{
Eric Fiselierdb215062016-03-31 02:15:15 +00001486 typedef iterator_traits<_InputIterator> _ITraits;
1487 typedef typename _ITraits::value_type _ItValueType;
1488 static_assert((is_same<_ItValueType, __container_value_type>::value),
1489 "__assign_unique may only be called with the containers value type");
1490
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001491 if (size() != 0)
1492 {
1493 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001494#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001495 try
1496 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001497#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001498 for (; __cache != nullptr && __first != __last; ++__first)
1499 {
1500 __cache->__value_ = *__first;
1501 __node_pointer __next = __detach(__cache);
1502 __node_insert_unique(__cache);
1503 __cache = __next;
1504 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001505#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001506 }
1507 catch (...)
1508 {
1509 while (__cache->__parent_ != nullptr)
1510 __cache = static_cast<__node_pointer>(__cache->__parent_);
1511 destroy(__cache);
1512 throw;
1513 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001514#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001515 if (__cache != nullptr)
1516 {
1517 while (__cache->__parent_ != nullptr)
1518 __cache = static_cast<__node_pointer>(__cache->__parent_);
1519 destroy(__cache);
1520 }
1521 }
1522 for (; __first != __last; ++__first)
1523 __insert_unique(*__first);
1524}
1525
1526template <class _Tp, class _Compare, class _Allocator>
1527template <class _InputIterator>
1528void
1529__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1530{
Eric Fiselierdb215062016-03-31 02:15:15 +00001531 typedef iterator_traits<_InputIterator> _ITraits;
1532 typedef typename _ITraits::value_type _ItValueType;
1533 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1534 is_same<_ItValueType, __node_value_type>::value),
1535 "__assign_multi may only be called with the containers value type"
1536 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001537 if (size() != 0)
1538 {
1539 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001540#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001541 try
1542 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001543#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001544 for (; __cache != nullptr && __first != __last; ++__first)
1545 {
1546 __cache->__value_ = *__first;
1547 __node_pointer __next = __detach(__cache);
1548 __node_insert_multi(__cache);
1549 __cache = __next;
1550 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001551#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 }
1553 catch (...)
1554 {
1555 while (__cache->__parent_ != nullptr)
1556 __cache = static_cast<__node_pointer>(__cache->__parent_);
1557 destroy(__cache);
1558 throw;
1559 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001560#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 if (__cache != nullptr)
1562 {
1563 while (__cache->__parent_ != nullptr)
1564 __cache = static_cast<__node_pointer>(__cache->__parent_);
1565 destroy(__cache);
1566 }
1567 }
1568 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001569 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001570}
1571
1572template <class _Tp, class _Compare, class _Allocator>
1573__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
1574 : __begin_node_(__node_pointer()),
1575 __pair1_(__node_traits::select_on_container_copy_construction(__t.__node_alloc())),
1576 __pair3_(0, __t.value_comp())
1577{
1578 __begin_node() = __end_node();
1579}
1580
Howard Hinnant73d21a42010-09-04 23:28:19 +00001581#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001582
1583template <class _Tp, class _Compare, class _Allocator>
1584__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001585 _NOEXCEPT_(
1586 is_nothrow_move_constructible<__node_allocator>::value &&
1587 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001588 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1589 __pair1_(_VSTD::move(__t.__pair1_)),
1590 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001591{
1592 if (size() == 0)
1593 __begin_node() = __end_node();
1594 else
1595 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001596 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 __t.__begin_node() = __t.__end_node();
1598 __t.__end_node()->__left_ = nullptr;
1599 __t.size() = 0;
1600 }
1601}
1602
1603template <class _Tp, class _Compare, class _Allocator>
1604__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
1605 : __pair1_(__node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001606 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001607{
1608 if (__a == __t.__alloc())
1609 {
1610 if (__t.size() == 0)
1611 __begin_node() = __end_node();
1612 else
1613 {
1614 __begin_node() = __t.__begin_node();
1615 __end_node()->__left_ = __t.__end_node()->__left_;
Howard Hinnant70342b92013-06-19 21:29:40 +00001616 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001617 size() = __t.size();
1618 __t.__begin_node() = __t.__end_node();
1619 __t.__end_node()->__left_ = nullptr;
1620 __t.size() = 0;
1621 }
1622 }
1623 else
1624 {
1625 __begin_node() = __end_node();
1626 }
1627}
1628
1629template <class _Tp, class _Compare, class _Allocator>
1630void
1631__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001632 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1633 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001634{
1635 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1636 __begin_node_ = __t.__begin_node_;
1637 __pair1_.first() = __t.__pair1_.first();
1638 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001639 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 if (size() == 0)
1641 __begin_node() = __end_node();
1642 else
1643 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001644 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001645 __t.__begin_node() = __t.__end_node();
1646 __t.__end_node()->__left_ = nullptr;
1647 __t.size() = 0;
1648 }
1649}
1650
1651template <class _Tp, class _Compare, class _Allocator>
1652void
1653__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1654{
1655 if (__node_alloc() == __t.__node_alloc())
1656 __move_assign(__t, true_type());
1657 else
1658 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001659 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 const_iterator __e = end();
1661 if (size() != 0)
1662 {
1663 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001664#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001665 try
1666 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001667#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001668 while (__cache != nullptr && __t.size() != 0)
1669 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001670 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001671 __node_pointer __next = __detach(__cache);
1672 __node_insert_multi(__cache);
1673 __cache = __next;
1674 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001675#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001676 }
1677 catch (...)
1678 {
1679 while (__cache->__parent_ != nullptr)
1680 __cache = static_cast<__node_pointer>(__cache->__parent_);
1681 destroy(__cache);
1682 throw;
1683 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001684#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001685 if (__cache != nullptr)
1686 {
1687 while (__cache->__parent_ != nullptr)
1688 __cache = static_cast<__node_pointer>(__cache->__parent_);
1689 destroy(__cache);
1690 }
1691 }
1692 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001693 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001694 }
1695}
1696
1697template <class _Tp, class _Compare, class _Allocator>
1698__tree<_Tp, _Compare, _Allocator>&
1699__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001700 _NOEXCEPT_(
1701 __node_traits::propagate_on_container_move_assignment::value &&
1702 is_nothrow_move_assignable<value_compare>::value &&
1703 is_nothrow_move_assignable<__node_allocator>::value)
1704
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001705{
1706 __move_assign(__t, integral_constant<bool,
1707 __node_traits::propagate_on_container_move_assignment::value>());
1708 return *this;
1709}
1710
Howard Hinnant73d21a42010-09-04 23:28:19 +00001711#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001712
1713template <class _Tp, class _Compare, class _Allocator>
1714__tree<_Tp, _Compare, _Allocator>::~__tree()
1715{
1716 destroy(__root());
1717}
1718
1719template <class _Tp, class _Compare, class _Allocator>
1720void
Howard Hinnant7686add2011-06-04 14:31:57 +00001721__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001722{
1723 if (__nd != nullptr)
1724 {
1725 destroy(static_cast<__node_pointer>(__nd->__left_));
1726 destroy(static_cast<__node_pointer>(__nd->__right_));
1727 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001728 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001729 __node_traits::deallocate(__na, __nd, 1);
1730 }
1731}
1732
1733template <class _Tp, class _Compare, class _Allocator>
1734void
1735__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Marshall Clow7d914d12015-07-13 20:04:56 +00001736 _NOEXCEPT_(
1737 __is_nothrow_swappable<value_compare>::value
1738#if _LIBCPP_STD_VER <= 11
1739 && (!__node_traits::propagate_on_container_swap::value ||
1740 __is_nothrow_swappable<__node_allocator>::value)
1741#endif
1742 )
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001743{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001744 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001745 swap(__begin_node_, __t.__begin_node_);
1746 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001747 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001748 __pair3_.swap(__t.__pair3_);
1749 if (size() == 0)
1750 __begin_node() = __end_node();
1751 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001752 __end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001753 if (__t.size() == 0)
1754 __t.__begin_node() = __t.__end_node();
1755 else
Howard Hinnant70342b92013-06-19 21:29:40 +00001756 __t.__end_node()->__left_->__parent_ = static_cast<__node_base_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001757}
1758
1759template <class _Tp, class _Compare, class _Allocator>
1760void
Howard Hinnant7686add2011-06-04 14:31:57 +00001761__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001762{
1763 destroy(__root());
1764 size() = 0;
1765 __begin_node() = __end_node();
1766 __end_node()->__left_ = nullptr;
1767}
1768
1769// Find lower_bound place to insert
1770// Set __parent to parent of null leaf
1771// Return reference to null leaf
1772template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001773typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1774__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001775 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001776{
1777 __node_pointer __nd = __root();
1778 if (__nd != nullptr)
1779 {
1780 while (true)
1781 {
1782 if (value_comp()(__nd->__value_, __v))
1783 {
1784 if (__nd->__right_ != nullptr)
1785 __nd = static_cast<__node_pointer>(__nd->__right_);
1786 else
1787 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001788 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001789 return __parent->__right_;
1790 }
1791 }
1792 else
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 }
1803 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001804 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001805 return __parent->__left_;
1806}
1807
1808// Find upper_bound place to insert
1809// Set __parent to parent of null leaf
1810// Return reference to null leaf
1811template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001812typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1813__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001814 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001815{
1816 __node_pointer __nd = __root();
1817 if (__nd != nullptr)
1818 {
1819 while (true)
1820 {
1821 if (value_comp()(__v, __nd->__value_))
1822 {
1823 if (__nd->__left_ != nullptr)
1824 __nd = static_cast<__node_pointer>(__nd->__left_);
1825 else
1826 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001827 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001828 return __parent->__left_;
1829 }
1830 }
1831 else
1832 {
1833 if (__nd->__right_ != nullptr)
1834 __nd = static_cast<__node_pointer>(__nd->__right_);
1835 else
1836 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001837 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001838 return __parent->__right_;
1839 }
1840 }
1841 }
1842 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001843 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001844 return __parent->__left_;
1845}
1846
1847// Find leaf place to insert closest to __hint
1848// First check prior to __hint.
1849// Next check after __hint.
1850// Next do O(log N) search.
1851// Set __parent to parent of null leaf
1852// Return reference to null leaf
1853template <class _Tp, class _Compare, class _Allocator>
Howard Hinnantd615e472011-04-03 20:05:29 +00001854typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001856 typename __node_base::pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001857 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001858{
1859 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1860 {
1861 // __v <= *__hint
1862 const_iterator __prior = __hint;
1863 if (__prior == begin() || !value_comp()(__v, *--__prior))
1864 {
1865 // *prev(__hint) <= __v <= *__hint
1866 if (__hint.__ptr_->__left_ == nullptr)
1867 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001868 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869 return __parent->__left_;
1870 }
1871 else
1872 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001873 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001874 return __parent->__right_;
1875 }
1876 }
1877 // __v < *prev(__hint)
1878 return __find_leaf_high(__parent, __v);
1879 }
1880 // else __v > *__hint
1881 return __find_leaf_low(__parent, __v);
1882}
1883
1884// Find place to insert if __v doesn't exist
1885// Set __parent to parent of null leaf
1886// Return reference to null leaf
1887// If __v exists, set parent to node of __v and return reference to node of __v
1888template <class _Tp, class _Compare, class _Allocator>
1889template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001890typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
1891__tree<_Tp, _Compare, _Allocator>::__find_equal(typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001892 const _Key& __v)
1893{
1894 __node_pointer __nd = __root();
1895 if (__nd != nullptr)
1896 {
1897 while (true)
1898 {
1899 if (value_comp()(__v, __nd->__value_))
1900 {
1901 if (__nd->__left_ != nullptr)
1902 __nd = static_cast<__node_pointer>(__nd->__left_);
1903 else
1904 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001905 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 return __parent->__left_;
1907 }
1908 }
1909 else if (value_comp()(__nd->__value_, __v))
1910 {
1911 if (__nd->__right_ != nullptr)
1912 __nd = static_cast<__node_pointer>(__nd->__right_);
1913 else
1914 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001915 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001916 return __parent->__right_;
1917 }
1918 }
1919 else
1920 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001921 __parent = static_cast<__node_base_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922 return __parent;
1923 }
1924 }
1925 }
Howard Hinnant70342b92013-06-19 21:29:40 +00001926 __parent = static_cast<__node_base_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001927 return __parent->__left_;
1928}
1929
1930// Find place to insert if __v doesn't exist
1931// First check prior to __hint.
1932// Next check after __hint.
1933// Next do O(log N) search.
1934// Set __parent to parent of null leaf
1935// Return reference to null leaf
1936// If __v exists, set parent to node of __v and return reference to node of __v
1937template <class _Tp, class _Compare, class _Allocator>
1938template <class _Key>
Howard Hinnantd615e472011-04-03 20:05:29 +00001939typename __tree<_Tp, _Compare, _Allocator>::__node_base::pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001940__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Howard Hinnantd615e472011-04-03 20:05:29 +00001941 typename __node_base::pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001942 const _Key& __v)
1943{
1944 if (__hint == end() || value_comp()(__v, *__hint)) // check before
1945 {
1946 // __v < *__hint
1947 const_iterator __prior = __hint;
1948 if (__prior == begin() || value_comp()(*--__prior, __v))
1949 {
1950 // *prev(__hint) < __v < *__hint
1951 if (__hint.__ptr_->__left_ == nullptr)
1952 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001953 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001954 return __parent->__left_;
1955 }
1956 else
1957 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001958 __parent = static_cast<__node_base_pointer>(__prior.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001959 return __parent->__right_;
1960 }
1961 }
1962 // __v <= *prev(__hint)
1963 return __find_equal(__parent, __v);
1964 }
1965 else if (value_comp()(*__hint, __v)) // check after
1966 {
1967 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00001968 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001969 if (__next == end() || value_comp()(__v, *__next))
1970 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001971 // *__hint < __v < *_VSTD::next(__hint)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001972 if (__hint.__ptr_->__right_ == nullptr)
1973 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001974 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001975 return __parent->__right_;
1976 }
1977 else
1978 {
Howard Hinnant70342b92013-06-19 21:29:40 +00001979 __parent = static_cast<__node_base_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001980 return __parent->__left_;
1981 }
1982 }
1983 // *next(__hint) <= __v
1984 return __find_equal(__parent, __v);
1985 }
1986 // else __v == *__hint
Howard Hinnant70342b92013-06-19 21:29:40 +00001987 __parent = static_cast<__node_base_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001988 return __parent;
1989}
1990
1991template <class _Tp, class _Compare, class _Allocator>
1992void
1993__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__node_base_pointer __parent,
1994 __node_base_pointer& __child,
1995 __node_base_pointer __new_node)
1996{
1997 __new_node->__left_ = nullptr;
1998 __new_node->__right_ = nullptr;
1999 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002000 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002001 __child = __new_node;
2002 if (__begin_node()->__left_ != nullptr)
2003 __begin_node() = static_cast<__node_pointer>(__begin_node()->__left_);
2004 __tree_balance_after_insert(__end_node()->__left_, __child);
2005 ++size();
2006}
2007
Eric Fiselierdb215062016-03-31 02:15:15 +00002008#ifndef _LIBCPP_CXX03_LANG
2009template <class _Tp, class _Compare, class _Allocator>
2010template <class _Key, class... _Args>
2011pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2012__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2013#else
2014template <class _Tp, class _Compare, class _Allocator>
2015template <class _Key, class _Args>
2016pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2017__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2018#endif
2019{
2020 __node_base_pointer __parent;
2021 __node_base_pointer& __child = __find_equal(__parent, __k);
2022 __node_pointer __r = static_cast<__node_pointer>(__child);
2023 bool __inserted = false;
2024 if (__child == nullptr)
2025 {
2026#ifndef _LIBCPP_CXX03_LANG
2027 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2028#else
2029 __node_holder __h = __construct_node(__args);
2030#endif
2031 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2032 __r = __h.release();
2033 __inserted = true;
2034 }
2035 return pair<iterator, bool>(iterator(__r), __inserted);
2036}
2037
2038
2039#ifndef _LIBCPP_CXX03_LANG
2040template <class _Tp, class _Compare, class _Allocator>
2041template <class _Key, class... _Args>
2042typename __tree<_Tp, _Compare, _Allocator>::iterator
2043__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2044 const_iterator __p, _Key const& __k, _Args&&... __args)
2045#else
2046template <class _Tp, class _Compare, class _Allocator>
2047template <class _Key, class _Args>
2048typename __tree<_Tp, _Compare, _Allocator>::iterator
2049__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2050 const_iterator __p, _Key const& __k, _Args& __args)
2051#endif
2052{
2053 __node_base_pointer __parent;
2054 __node_base_pointer& __child = __find_equal(__p, __parent, __k);
2055 __node_pointer __r = static_cast<__node_pointer>(__child);
2056 if (__child == nullptr)
2057 {
2058#ifndef _LIBCPP_CXX03_LANG
2059 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2060#else
2061 __node_holder __h = __construct_node(__args);
2062#endif
2063 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2064 __r = __h.release();
2065 }
2066 return iterator(__r);
2067}
2068
2069
2070#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002071
2072template <class _Tp, class _Compare, class _Allocator>
2073template <class ..._Args>
2074typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2075__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2076{
Eric Fiselierdb215062016-03-31 02:15:15 +00002077 static_assert(!__is_tree_value_type<_Args...>::value,
2078 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002079 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002080 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002081 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082 __h.get_deleter().__value_constructed = true;
2083 return __h;
2084}
2085
Eric Fiselierdb215062016-03-31 02:15:15 +00002086
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002087template <class _Tp, class _Compare, class _Allocator>
2088template <class... _Args>
2089pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002090__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002091{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002092 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002093 __node_base_pointer __parent;
2094 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2095 __node_pointer __r = static_cast<__node_pointer>(__child);
2096 bool __inserted = false;
2097 if (__child == nullptr)
2098 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002099 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002100 __r = __h.release();
2101 __inserted = true;
2102 }
2103 return pair<iterator, bool>(iterator(__r), __inserted);
2104}
2105
2106template <class _Tp, class _Compare, class _Allocator>
2107template <class... _Args>
2108typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002109__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002110{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002111 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002112 __node_base_pointer __parent;
2113 __node_base_pointer& __child = __find_equal(__p, __parent, __h->__value_);
2114 __node_pointer __r = static_cast<__node_pointer>(__child);
2115 if (__child == nullptr)
2116 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002117 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002118 __r = __h.release();
2119 }
2120 return iterator(__r);
2121}
2122
2123template <class _Tp, class _Compare, class _Allocator>
2124template <class... _Args>
2125typename __tree<_Tp, _Compare, _Allocator>::iterator
2126__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2127{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002128 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002129 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002130 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002131 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002132 return iterator(static_cast<__node_pointer>(__h.release()));
2133}
2134
2135template <class _Tp, class _Compare, class _Allocator>
2136template <class... _Args>
2137typename __tree<_Tp, _Compare, _Allocator>::iterator
2138__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2139 _Args&&... __args)
2140{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002141 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002142 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002143 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002144 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002145 return iterator(static_cast<__node_pointer>(__h.release()));
2146}
2147
Howard Hinnant73d21a42010-09-04 23:28:19 +00002148
Eric Fiselierdb215062016-03-31 02:15:15 +00002149#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002150
2151template <class _Tp, class _Compare, class _Allocator>
2152typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002153__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002154{
2155 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002156 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002157 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002158 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002159 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002160}
2161
Eric Fiselierdb215062016-03-31 02:15:15 +00002162#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002163
Eric Fiselierdb215062016-03-31 02:15:15 +00002164#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002165template <class _Tp, class _Compare, class _Allocator>
2166typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002167__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002168{
2169 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002170 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002171 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002172 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002173 return iterator(__h.release());
2174}
2175
2176template <class _Tp, class _Compare, class _Allocator>
2177typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002178__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002179{
2180 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002181 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002183 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002184 return iterator(__h.release());
2185}
Eric Fiselierdb215062016-03-31 02:15:15 +00002186#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002187
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002188template <class _Tp, class _Compare, class _Allocator>
2189pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2190__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2191{
2192 __node_base_pointer __parent;
2193 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2194 __node_pointer __r = static_cast<__node_pointer>(__child);
2195 bool __inserted = false;
2196 if (__child == nullptr)
2197 {
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 __r = __nd;
2200 __inserted = true;
2201 }
2202 return pair<iterator, bool>(iterator(__r), __inserted);
2203}
2204
2205template <class _Tp, class _Compare, class _Allocator>
2206typename __tree<_Tp, _Compare, _Allocator>::iterator
2207__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2208 __node_pointer __nd)
2209{
2210 __node_base_pointer __parent;
2211 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2212 __node_pointer __r = static_cast<__node_pointer>(__child);
2213 if (__child == nullptr)
2214 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002215 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002216 __r = __nd;
2217 }
2218 return iterator(__r);
2219}
2220
2221template <class _Tp, class _Compare, class _Allocator>
2222typename __tree<_Tp, _Compare, _Allocator>::iterator
2223__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2224{
2225 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002226 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002227 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002228 return iterator(__nd);
2229}
2230
2231template <class _Tp, class _Compare, class _Allocator>
2232typename __tree<_Tp, _Compare, _Allocator>::iterator
2233__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2234 __node_pointer __nd)
2235{
2236 __node_base_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002237 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002238 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002239 return iterator(__nd);
2240}
2241
2242template <class _Tp, class _Compare, class _Allocator>
2243typename __tree<_Tp, _Compare, _Allocator>::iterator
2244__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2245{
Howard Hinnant70342b92013-06-19 21:29:40 +00002246 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002247 iterator __r(__np);
2248 ++__r;
2249 if (__begin_node() == __np)
2250 __begin_node() = __r.__ptr_;
2251 --size();
2252 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002253 __tree_remove(__end_node()->__left_,
2254 static_cast<__node_base_pointer>(__np));
Eric Fiselierdb215062016-03-31 02:15:15 +00002255 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2256 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257 __node_traits::deallocate(__na, __np, 1);
2258 return __r;
2259}
2260
2261template <class _Tp, class _Compare, class _Allocator>
2262typename __tree<_Tp, _Compare, _Allocator>::iterator
2263__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2264{
2265 while (__f != __l)
2266 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002267 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002268}
2269
2270template <class _Tp, class _Compare, class _Allocator>
2271template <class _Key>
2272typename __tree<_Tp, _Compare, _Allocator>::size_type
2273__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2274{
2275 iterator __i = find(__k);
2276 if (__i == end())
2277 return 0;
2278 erase(__i);
2279 return 1;
2280}
2281
2282template <class _Tp, class _Compare, class _Allocator>
2283template <class _Key>
2284typename __tree<_Tp, _Compare, _Allocator>::size_type
2285__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2286{
2287 pair<iterator, iterator> __p = __equal_range_multi(__k);
2288 size_type __r = 0;
2289 for (; __p.first != __p.second; ++__r)
2290 __p.first = erase(__p.first);
2291 return __r;
2292}
2293
2294template <class _Tp, class _Compare, class _Allocator>
2295template <class _Key>
2296typename __tree<_Tp, _Compare, _Allocator>::iterator
2297__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2298{
2299 iterator __p = __lower_bound(__v, __root(), __end_node());
2300 if (__p != end() && !value_comp()(__v, *__p))
2301 return __p;
2302 return end();
2303}
2304
2305template <class _Tp, class _Compare, class _Allocator>
2306template <class _Key>
2307typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2308__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2309{
2310 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2311 if (__p != end() && !value_comp()(__v, *__p))
2312 return __p;
2313 return end();
2314}
2315
2316template <class _Tp, class _Compare, class _Allocator>
2317template <class _Key>
2318typename __tree<_Tp, _Compare, _Allocator>::size_type
2319__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2320{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002321 __node_pointer __result = __end_node();
2322 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002323 while (__rt != nullptr)
2324 {
2325 if (value_comp()(__k, __rt->__value_))
2326 {
2327 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002328 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 }
2330 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002331 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002332 else
2333 return 1;
2334 }
2335 return 0;
2336}
2337
2338template <class _Tp, class _Compare, class _Allocator>
2339template <class _Key>
2340typename __tree<_Tp, _Compare, _Allocator>::size_type
2341__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2342{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002343 __node_pointer __result = __end_node();
2344 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002345 while (__rt != nullptr)
2346 {
2347 if (value_comp()(__k, __rt->__value_))
2348 {
2349 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002350 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002351 }
2352 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002353 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002354 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002355 return _VSTD::distance(
Eric Fiselier227b47c2016-02-20 07:12:17 +00002356 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2357 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002358 );
2359 }
2360 return 0;
2361}
2362
2363template <class _Tp, class _Compare, class _Allocator>
2364template <class _Key>
2365typename __tree<_Tp, _Compare, _Allocator>::iterator
2366__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2367 __node_pointer __root,
2368 __node_pointer __result)
2369{
2370 while (__root != nullptr)
2371 {
2372 if (!value_comp()(__root->__value_, __v))
2373 {
2374 __result = __root;
2375 __root = static_cast<__node_pointer>(__root->__left_);
2376 }
2377 else
2378 __root = static_cast<__node_pointer>(__root->__right_);
2379 }
2380 return iterator(__result);
2381}
2382
2383template <class _Tp, class _Compare, class _Allocator>
2384template <class _Key>
2385typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2386__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002387 __node_pointer __root,
2388 __node_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002389{
2390 while (__root != nullptr)
2391 {
2392 if (!value_comp()(__root->__value_, __v))
2393 {
2394 __result = __root;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002395 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002396 }
2397 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002398 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002399 }
2400 return const_iterator(__result);
2401}
2402
2403template <class _Tp, class _Compare, class _Allocator>
2404template <class _Key>
2405typename __tree<_Tp, _Compare, _Allocator>::iterator
2406__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2407 __node_pointer __root,
2408 __node_pointer __result)
2409{
2410 while (__root != nullptr)
2411 {
2412 if (value_comp()(__v, __root->__value_))
2413 {
2414 __result = __root;
2415 __root = static_cast<__node_pointer>(__root->__left_);
2416 }
2417 else
2418 __root = static_cast<__node_pointer>(__root->__right_);
2419 }
2420 return iterator(__result);
2421}
2422
2423template <class _Tp, class _Compare, class _Allocator>
2424template <class _Key>
2425typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2426__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002427 __node_pointer __root,
2428 __node_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002429{
2430 while (__root != nullptr)
2431 {
2432 if (value_comp()(__v, __root->__value_))
2433 {
2434 __result = __root;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002435 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002436 }
2437 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002438 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002439 }
2440 return const_iterator(__result);
2441}
2442
2443template <class _Tp, class _Compare, class _Allocator>
2444template <class _Key>
2445pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2446 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2447__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2448{
Howard Hinnant99968442011-11-29 18:15:50 +00002449 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002450 __node_pointer __result = __end_node();
2451 __node_pointer __rt = __root();
2452 while (__rt != nullptr)
2453 {
2454 if (value_comp()(__k, __rt->__value_))
2455 {
2456 __result = __rt;
2457 __rt = static_cast<__node_pointer>(__rt->__left_);
2458 }
2459 else if (value_comp()(__rt->__value_, __k))
2460 __rt = static_cast<__node_pointer>(__rt->__right_);
2461 else
Howard Hinnant99968442011-11-29 18:15:50 +00002462 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002463 iterator(
2464 __rt->__right_ != nullptr ?
2465 static_cast<__node_pointer>(__tree_min(__rt->__right_))
2466 : __result));
2467 }
Howard Hinnant99968442011-11-29 18:15:50 +00002468 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469}
2470
2471template <class _Tp, class _Compare, class _Allocator>
2472template <class _Key>
2473pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2474 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2475__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2476{
Howard Hinnant99968442011-11-29 18:15:50 +00002477 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002478 __node_pointer __result = __end_node();
2479 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480 while (__rt != nullptr)
2481 {
2482 if (value_comp()(__k, __rt->__value_))
2483 {
2484 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002485 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 }
2487 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002488 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002489 else
Howard Hinnant99968442011-11-29 18:15:50 +00002490 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002491 const_iterator(
2492 __rt->__right_ != nullptr ?
Eric Fiselier227b47c2016-02-20 07:12:17 +00002493 static_cast<__node_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002494 : __result));
2495 }
Howard Hinnant99968442011-11-29 18:15:50 +00002496 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002497}
2498
2499template <class _Tp, class _Compare, class _Allocator>
2500template <class _Key>
2501pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2502 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2503__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2504{
Howard Hinnant99968442011-11-29 18:15:50 +00002505 typedef pair<iterator, iterator> _Pp;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002506 __node_pointer __result = __end_node();
2507 __node_pointer __rt = __root();
2508 while (__rt != nullptr)
2509 {
2510 if (value_comp()(__k, __rt->__value_))
2511 {
2512 __result = __rt;
2513 __rt = static_cast<__node_pointer>(__rt->__left_);
2514 }
2515 else if (value_comp()(__rt->__value_, __k))
2516 __rt = static_cast<__node_pointer>(__rt->__right_);
2517 else
Howard Hinnant99968442011-11-29 18:15:50 +00002518 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002519 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2520 }
Howard Hinnant99968442011-11-29 18:15:50 +00002521 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002522}
2523
2524template <class _Tp, class _Compare, class _Allocator>
2525template <class _Key>
2526pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2527 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2528__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2529{
Howard Hinnant99968442011-11-29 18:15:50 +00002530 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002531 __node_pointer __result = __end_node();
2532 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002533 while (__rt != nullptr)
2534 {
2535 if (value_comp()(__k, __rt->__value_))
2536 {
2537 __result = __rt;
Eric Fiselier227b47c2016-02-20 07:12:17 +00002538 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002539 }
2540 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002541 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002542 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002543 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), __rt),
2544 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002545 }
Howard Hinnant99968442011-11-29 18:15:50 +00002546 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547}
2548
2549template <class _Tp, class _Compare, class _Allocator>
2550typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002551__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002552{
Howard Hinnant70342b92013-06-19 21:29:40 +00002553 __node_pointer __np = __p.__ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002554 if (__begin_node() == __np)
2555 {
2556 if (__np->__right_ != nullptr)
2557 __begin_node() = static_cast<__node_pointer>(__np->__right_);
2558 else
2559 __begin_node() = static_cast<__node_pointer>(__np->__parent_);
2560 }
2561 --size();
2562 __tree_remove(__end_node()->__left_,
2563 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002564 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002565}
2566
Howard Hinnant7686add2011-06-04 14:31:57 +00002567template <class _Tp, class _Compare, class _Allocator>
2568inline _LIBCPP_INLINE_VISIBILITY
2569void
2570swap(__tree<_Tp, _Compare, _Allocator>& __x,
2571 __tree<_Tp, _Compare, _Allocator>& __y)
2572 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2573{
2574 __x.swap(__y);
2575}
2576
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577_LIBCPP_END_NAMESPACE_STD
2578
2579#endif // _LIBCPP___TREE