blob: 792870aa6ebcaa8091e46cfbf118a84bed416a60 [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
Eric Fiselier018a3d52017-05-31 22:07:49 +000024_LIBCPP_PUSH_MACROS
25#include <__undef_macros>
26
27
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028_LIBCPP_BEGIN_NAMESPACE_STD
29
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000030template <class _Tp, class _Compare, class _Allocator> class __tree;
31template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000032 class _LIBCPP_TEMPLATE_VIS __tree_iterator;
Howard Hinnant2b1b2d42011-06-14 19:58:17 +000033template <class _Tp, class _ConstNodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +000034 class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035
Eric Fiselier55263482016-02-20 05:28:30 +000036template <class _Pointer> class __tree_end_node;
37template <class _VoidPtr> class __tree_node_base;
38template <class _Tp, class _VoidPtr> class __tree_node;
39
40#ifndef _LIBCPP_CXX03_LANG
41template <class _Key, class _Value>
42union __value_type;
43#else
44template <class _Key, class _Value>
45struct __value_type;
46#endif
47
Eric Fiselierebaf7da2017-01-13 22:02:08 +000048template <class _Key, class _CP, class _Compare,
49 bool = is_empty<_Compare>::value && !__libcpp_is_final<_Compare>::value>
50class __map_value_compare;
51
Eric Fiselier55263482016-02-20 05:28:30 +000052template <class _Allocator> class __map_node_destructor;
Eric Fiselierc3589a82017-01-04 23:56:00 +000053template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_iterator;
54template <class _TreeIterator> class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier55263482016-02-20 05:28:30 +000055
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))
Eric Fiselier7310ec82016-07-19 17:56:20 +0000176 __x = __x->__parent_unsafe();
177 return __x->__parent_unsafe();
178}
179
180template <class _EndNodePtr, class _NodePtr>
181inline _LIBCPP_INLINE_VISIBILITY
182_EndNodePtr
183__tree_next_iter(_NodePtr __x) _NOEXCEPT
184{
185 if (__x->__right_ != nullptr)
186 return static_cast<_EndNodePtr>(__tree_min(__x->__right_));
187 while (!__tree_is_left_child(__x))
188 __x = __x->__parent_unsafe();
189 return static_cast<_EndNodePtr>(__x->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000190}
191
192// Returns: pointer to the previous in-order node before __x.
193// Precondition: __x != nullptr.
Eric Fiselier7310ec82016-07-19 17:56:20 +0000194// Note: __x may be the end node.
195template <class _NodePtr, class _EndNodePtr>
196inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000197_NodePtr
Eric Fiselier7310ec82016-07-19 17:56:20 +0000198__tree_prev_iter(_EndNodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199{
200 if (__x->__left_ != nullptr)
201 return __tree_max(__x->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +0000202 _NodePtr __xx = static_cast<_NodePtr>(__x);
203 while (__tree_is_left_child(__xx))
204 __xx = __xx->__parent_unsafe();
205 return __xx->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206}
207
208// Returns: pointer to a node which has no children
209// Precondition: __x != nullptr.
210template <class _NodePtr>
211_NodePtr
Howard Hinnant8b537682011-06-04 17:10:24 +0000212__tree_leaf(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213{
214 while (true)
215 {
216 if (__x->__left_ != nullptr)
217 {
218 __x = __x->__left_;
219 continue;
220 }
221 if (__x->__right_ != nullptr)
222 {
223 __x = __x->__right_;
224 continue;
225 }
226 break;
227 }
228 return __x;
229}
230
231// Effects: Makes __x->__right_ the subtree root with __x as its left child
232// while preserving in-order order.
233// Precondition: __x->__right_ != nullptr
234template <class _NodePtr>
235void
Howard Hinnant8b537682011-06-04 17:10:24 +0000236__tree_left_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000237{
238 _NodePtr __y = __x->__right_;
239 __x->__right_ = __y->__left_;
240 if (__x->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000241 __x->__right_->__set_parent(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242 __y->__parent_ = __x->__parent_;
243 if (__tree_is_left_child(__x))
244 __x->__parent_->__left_ = __y;
245 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000246 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000247 __y->__left_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000248 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000249}
250
251// Effects: Makes __x->__left_ the subtree root with __x as its right child
252// while preserving in-order order.
253// Precondition: __x->__left_ != nullptr
254template <class _NodePtr>
255void
Howard Hinnant8b537682011-06-04 17:10:24 +0000256__tree_right_rotate(_NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257{
258 _NodePtr __y = __x->__left_;
259 __x->__left_ = __y->__right_;
260 if (__x->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000261 __x->__left_->__set_parent(__x);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000262 __y->__parent_ = __x->__parent_;
263 if (__tree_is_left_child(__x))
264 __x->__parent_->__left_ = __y;
265 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000266 __x->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267 __y->__right_ = __x;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000268 __x->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000269}
270
271// Effects: Rebalances __root after attaching __x to a leaf.
272// Precondition: __root != nulptr && __x != nullptr.
273// __x has no children.
274// __x == __root or == a direct or indirect child of __root.
275// If __x were to be unlinked from __root (setting __root to
276// nullptr if __root == __x), __tree_invariant(__root) == true.
277// Postcondition: __tree_invariant(end_node->__left_) == true. end_node->__left_
278// may be different than the value passed in as __root.
279template <class _NodePtr>
280void
Howard Hinnant8b537682011-06-04 17:10:24 +0000281__tree_balance_after_insert(_NodePtr __root, _NodePtr __x) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282{
283 __x->__is_black_ = __x == __root;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000284 while (__x != __root && !__x->__parent_unsafe()->__is_black_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000285 {
286 // __x->__parent_ != __root because __x->__parent_->__is_black == false
Eric Fiselier7310ec82016-07-19 17:56:20 +0000287 if (__tree_is_left_child(__x->__parent_unsafe()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000288 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000289 _NodePtr __y = __x->__parent_unsafe()->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290 if (__y != nullptr && !__y->__is_black_)
291 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000292 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000294 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295 __x->__is_black_ = __x == __root;
296 __y->__is_black_ = true;
297 }
298 else
299 {
300 if (!__tree_is_left_child(__x))
301 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000302 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303 __tree_left_rotate(__x);
304 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000305 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000307 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 __x->__is_black_ = false;
309 __tree_right_rotate(__x);
310 break;
311 }
312 }
313 else
314 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000315 _NodePtr __y = __x->__parent_unsafe()->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316 if (__y != nullptr && !__y->__is_black_)
317 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000318 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000319 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000320 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000321 __x->__is_black_ = __x == __root;
322 __y->__is_black_ = true;
323 }
324 else
325 {
326 if (__tree_is_left_child(__x))
327 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000328 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000329 __tree_right_rotate(__x);
330 }
Eric Fiselier7310ec82016-07-19 17:56:20 +0000331 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000332 __x->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000333 __x = __x->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000334 __x->__is_black_ = false;
335 __tree_left_rotate(__x);
336 break;
337 }
338 }
339 }
340}
341
342// Precondition: __root != nullptr && __z != nullptr.
343// __tree_invariant(__root) == true.
344// __z == __root or == a direct or indirect child of __root.
345// Effects: unlinks __z from the tree rooted at __root, rebalancing as needed.
346// Postcondition: __tree_invariant(end_node->__left_) == true && end_node->__left_
347// nor any of its children refer to __z. end_node->__left_
348// may be different than the value passed in as __root.
349template <class _NodePtr>
350void
Howard Hinnant8b537682011-06-04 17:10:24 +0000351__tree_remove(_NodePtr __root, _NodePtr __z) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000352{
353 // __z will be removed from the tree. Client still needs to destruct/deallocate it
354 // __y is either __z, or if __z has two children, __tree_next(__z).
355 // __y will have at most one child.
356 // __y will be the initial hole in the tree (make the hole at a leaf)
357 _NodePtr __y = (__z->__left_ == nullptr || __z->__right_ == nullptr) ?
358 __z : __tree_next(__z);
359 // __x is __y's possibly null single child
360 _NodePtr __x = __y->__left_ != nullptr ? __y->__left_ : __y->__right_;
361 // __w is __x's possibly null uncle (will become __x's sibling)
362 _NodePtr __w = nullptr;
363 // link __x to __y's parent, and find __w
364 if (__x != nullptr)
365 __x->__parent_ = __y->__parent_;
366 if (__tree_is_left_child(__y))
367 {
368 __y->__parent_->__left_ = __x;
369 if (__y != __root)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000370 __w = __y->__parent_unsafe()->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000371 else
372 __root = __x; // __w == nullptr
373 }
374 else
375 {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000376 __y->__parent_unsafe()->__right_ = __x;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000377 // __y can't be root if it is a right child
378 __w = __y->__parent_->__left_;
379 }
380 bool __removed_black = __y->__is_black_;
381 // If we didn't remove __z, do so now by splicing in __y for __z,
382 // but copy __z's color. This does not impact __x or __w.
383 if (__y != __z)
384 {
Howard Hinnant324bb032010-08-22 00:02:43 +0000385 // __z->__left_ != nulptr but __z->__right_ might == __x == nullptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000386 __y->__parent_ = __z->__parent_;
387 if (__tree_is_left_child(__z))
388 __y->__parent_->__left_ = __y;
389 else
Eric Fiselier7310ec82016-07-19 17:56:20 +0000390 __y->__parent_unsafe()->__right_ = __y;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 __y->__left_ = __z->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000392 __y->__left_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393 __y->__right_ = __z->__right_;
394 if (__y->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +0000395 __y->__right_->__set_parent(__y);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 __y->__is_black_ = __z->__is_black_;
397 if (__root == __z)
398 __root = __y;
399 }
400 // There is no need to rebalance if we removed a red, or if we removed
401 // the last node.
402 if (__removed_black && __root != nullptr)
403 {
404 // Rebalance:
405 // __x has an implicit black color (transferred from the removed __y)
406 // associated with it, no matter what its color is.
407 // If __x is __root (in which case it can't be null), it is supposed
408 // to be black anyway, and if it is doubly black, then the double
409 // can just be ignored.
410 // If __x is red (in which case it can't be null), then it can absorb
411 // the implicit black just by setting its color to black.
412 // Since __y was black and only had one child (which __x points to), __x
413 // is either red with no children, else null, otherwise __y would have
414 // different black heights under left and right pointers.
415 // if (__x == __root || __x != nullptr && !__x->__is_black_)
416 if (__x != nullptr)
417 __x->__is_black_ = true;
418 else
419 {
420 // Else __x isn't root, and is "doubly black", even though it may
421 // be null. __w can not be null here, else the parent would
422 // see a black height >= 2 on the __x side and a black height
423 // of 1 on the __w side (__w must be a non-null black or a red
424 // with a non-null black child).
425 while (true)
426 {
427 if (!__tree_is_left_child(__w)) // if x is left child
428 {
429 if (!__w->__is_black_)
430 {
431 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000432 __w->__parent_unsafe()->__is_black_ = false;
433 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000434 // __x is still valid
435 // reset __root only if necessary
436 if (__root == __w->__left_)
437 __root = __w;
438 // reset sibling, and it still can't be null
439 __w = __w->__left_->__right_;
440 }
441 // __w->__is_black_ is now true, __w may have null children
442 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
443 (__w->__right_ == nullptr || __w->__right_->__is_black_))
444 {
445 __w->__is_black_ = false;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000446 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447 // __x can no longer be null
448 if (__x == __root || !__x->__is_black_)
449 {
450 __x->__is_black_ = true;
451 break;
452 }
453 // reset sibling, and it still can't be null
454 __w = __tree_is_left_child(__x) ?
Eric Fiselier7310ec82016-07-19 17:56:20 +0000455 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000456 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000457 // continue;
458 }
459 else // __w has a red child
460 {
461 if (__w->__right_ == nullptr || __w->__right_->__is_black_)
462 {
463 // __w left child is non-null and red
464 __w->__left_->__is_black_ = true;
465 __w->__is_black_ = false;
466 __tree_right_rotate(__w);
467 // __w is known not to be root, so root hasn't changed
468 // reset sibling, and it still can't be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000469 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470 }
471 // __w has a right red child, left child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000472 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
473 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474 __w->__right_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000475 __tree_left_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476 break;
477 }
478 }
479 else
480 {
481 if (!__w->__is_black_)
482 {
483 __w->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000484 __w->__parent_unsafe()->__is_black_ = false;
485 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486 // __x is still valid
487 // reset __root only if necessary
488 if (__root == __w->__right_)
489 __root = __w;
490 // reset sibling, and it still can't be null
491 __w = __w->__right_->__left_;
492 }
493 // __w->__is_black_ is now true, __w may have null children
494 if ((__w->__left_ == nullptr || __w->__left_->__is_black_) &&
495 (__w->__right_ == nullptr || __w->__right_->__is_black_))
496 {
497 __w->__is_black_ = false;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000498 __x = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000499 // __x can no longer be null
500 if (!__x->__is_black_ || __x == __root)
501 {
502 __x->__is_black_ = true;
503 break;
504 }
505 // reset sibling, and it still can't be null
506 __w = __tree_is_left_child(__x) ?
Eric Fiselier7310ec82016-07-19 17:56:20 +0000507 __x->__parent_unsafe()->__right_ :
Howard Hinnant324bb032010-08-22 00:02:43 +0000508 __x->__parent_->__left_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000509 // continue;
510 }
511 else // __w has a red child
512 {
513 if (__w->__left_ == nullptr || __w->__left_->__is_black_)
514 {
515 // __w right child is non-null and red
516 __w->__right_->__is_black_ = true;
517 __w->__is_black_ = false;
518 __tree_left_rotate(__w);
519 // __w is known not to be root, so root hasn't changed
520 // reset sibling, and it still can't be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000521 __w = __w->__parent_unsafe();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000522 }
523 // __w has a left red child, right child may be null
Eric Fiselier7310ec82016-07-19 17:56:20 +0000524 __w->__is_black_ = __w->__parent_unsafe()->__is_black_;
525 __w->__parent_unsafe()->__is_black_ = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526 __w->__left_->__is_black_ = true;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000527 __tree_right_rotate(__w->__parent_unsafe());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000528 break;
529 }
530 }
531 }
532 }
533 }
534}
535
Eric Fiselier55263482016-02-20 05:28:30 +0000536// node traits
537
Eric Fiselierdb215062016-03-31 02:15:15 +0000538
539#ifndef _LIBCPP_CXX03_LANG
540template <class _Tp>
541struct __is_tree_value_type_imp : false_type {};
542
543template <class _Key, class _Value>
544struct __is_tree_value_type_imp<__value_type<_Key, _Value>> : true_type {};
545
546template <class ..._Args>
547struct __is_tree_value_type : false_type {};
548
549template <class _One>
550struct __is_tree_value_type<_One> : __is_tree_value_type_imp<typename __uncvref<_One>::type> {};
551#endif
552
Eric Fiselier55263482016-02-20 05:28:30 +0000553template <class _Tp>
554struct __tree_key_value_types {
555 typedef _Tp key_type;
556 typedef _Tp __node_value_type;
557 typedef _Tp __container_value_type;
558 static const bool __is_map = false;
Eric Fiselierdb215062016-03-31 02:15:15 +0000559
560 _LIBCPP_INLINE_VISIBILITY
561 static key_type const& __get_key(_Tp const& __v) {
562 return __v;
563 }
564 _LIBCPP_INLINE_VISIBILITY
565 static __container_value_type const& __get_value(__node_value_type const& __v) {
566 return __v;
567 }
568 _LIBCPP_INLINE_VISIBILITY
569 static __container_value_type* __get_ptr(__node_value_type& __n) {
570 return _VSTD::addressof(__n);
571 }
572
573#ifndef _LIBCPP_CXX03_LANG
574 _LIBCPP_INLINE_VISIBILITY
575 static __container_value_type&& __move(__node_value_type& __v) {
576 return _VSTD::move(__v);
577 }
578#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000579};
580
581template <class _Key, class _Tp>
582struct __tree_key_value_types<__value_type<_Key, _Tp> > {
583 typedef _Key key_type;
584 typedef _Tp mapped_type;
585 typedef __value_type<_Key, _Tp> __node_value_type;
586 typedef pair<const _Key, _Tp> __container_value_type;
587 typedef pair<_Key, _Tp> __nc_value_type;
588 typedef __container_value_type __map_value_type;
589 static const bool __is_map = true;
Eric Fiselierdb215062016-03-31 02:15:15 +0000590
591 _LIBCPP_INLINE_VISIBILITY
592 static key_type const&
593 __get_key(__node_value_type const& __t) {
594 return __t.__cc.first;
595 }
596
597 template <class _Up>
598 _LIBCPP_INLINE_VISIBILITY
599 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
600 key_type const&>::type
601 __get_key(_Up& __t) {
602 return __t.first;
603 }
604
605 _LIBCPP_INLINE_VISIBILITY
606 static __container_value_type const&
607 __get_value(__node_value_type const& __t) {
608 return __t.__cc;
609 }
610
611 template <class _Up>
612 _LIBCPP_INLINE_VISIBILITY
613 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
614 __container_value_type const&>::type
615 __get_value(_Up& __t) {
616 return __t;
617 }
618
619 _LIBCPP_INLINE_VISIBILITY
620 static __container_value_type* __get_ptr(__node_value_type& __n) {
621 return _VSTD::addressof(__n.__cc);
622 }
623
624#ifndef _LIBCPP_CXX03_LANG
625 _LIBCPP_INLINE_VISIBILITY
626 static __nc_value_type&& __move(__node_value_type& __v) {
627 return _VSTD::move(__v.__nc);
628 }
629#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000630};
631
632template <class _VoidPtr>
633struct __tree_node_base_types {
634 typedef _VoidPtr __void_pointer;
635
636 typedef __tree_node_base<__void_pointer> __node_base_type;
637 typedef typename __rebind_pointer<_VoidPtr, __node_base_type>::type
638 __node_base_pointer;
639
640 typedef __tree_end_node<__node_base_pointer> __end_node_type;
641 typedef typename __rebind_pointer<_VoidPtr, __end_node_type>::type
642 __end_node_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000643#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
644 typedef __end_node_pointer __parent_pointer;
645#else
646 typedef typename conditional<
647 is_pointer<__end_node_pointer>::value,
648 __end_node_pointer,
649 __node_base_pointer>::type __parent_pointer;
650#endif
651
Eric Fiselier55263482016-02-20 05:28:30 +0000652private:
653 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
654 "_VoidPtr does not point to unqualified void type");
655};
656
657template <class _Tp, class _AllocPtr, class _KVTypes = __tree_key_value_types<_Tp>,
658 bool = _KVTypes::__is_map>
659struct __tree_map_pointer_types {};
660
661template <class _Tp, class _AllocPtr, class _KVTypes>
662struct __tree_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
663 typedef typename _KVTypes::__map_value_type _Mv;
664 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
665 __map_value_type_pointer;
666 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
667 __const_map_value_type_pointer;
668};
669
670template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
671struct __tree_node_types;
672
673template <class _NodePtr, class _Tp, class _VoidPtr>
674struct __tree_node_types<_NodePtr, __tree_node<_Tp, _VoidPtr> >
675 : public __tree_node_base_types<_VoidPtr>,
676 __tree_key_value_types<_Tp>,
677 __tree_map_pointer_types<_Tp, _VoidPtr>
678{
679 typedef __tree_node_base_types<_VoidPtr> __base;
680 typedef __tree_key_value_types<_Tp> __key_base;
681 typedef __tree_map_pointer_types<_Tp, _VoidPtr> __map_pointer_base;
682public:
683
684 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
685 typedef _NodePtr __node_pointer;
686
687 typedef _Tp __node_value_type;
688 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
689 __node_value_type_pointer;
690 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
691 __const_node_value_type_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000692#if defined(_LIBCPP_ABI_TREE_REMOVE_NODE_POINTER_UB)
693 typedef typename __base::__end_node_pointer __iter_pointer;
694#else
695 typedef typename conditional<
696 is_pointer<__node_pointer>::value,
697 typename __base::__end_node_pointer,
698 __node_pointer>::type __iter_pointer;
699#endif
Eric Fiselier55263482016-02-20 05:28:30 +0000700private:
701 static_assert(!is_const<__node_type>::value,
702 "_NodePtr should never be a pointer to const");
703 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
704 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
705};
706
707template <class _ValueTp, class _VoidPtr>
708struct __make_tree_node_types {
709 typedef typename __rebind_pointer<_VoidPtr, __tree_node<_ValueTp, _VoidPtr> >::type
710 _NodePtr;
711 typedef __tree_node_types<_NodePtr> type;
712};
713
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000714// node
715
716template <class _Pointer>
717class __tree_end_node
718{
719public:
720 typedef _Pointer pointer;
721 pointer __left_;
722
Howard Hinnant333f50d2010-09-21 20:16:37 +0000723 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant8b537682011-06-04 17:10:24 +0000724 __tree_end_node() _NOEXCEPT : __left_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725};
726
727template <class _VoidPtr>
728class __tree_node_base
Eric Fiselier55263482016-02-20 05:28:30 +0000729 : public __tree_node_base_types<_VoidPtr>::__end_node_type
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000730{
Eric Fiselier55263482016-02-20 05:28:30 +0000731 typedef __tree_node_base_types<_VoidPtr> _NodeBaseTypes;
732
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000733public:
Eric Fiselier55263482016-02-20 05:28:30 +0000734 typedef typename _NodeBaseTypes::__node_base_pointer pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000735 typedef typename _NodeBaseTypes::__parent_pointer __parent_pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000736
Eric Fiselier7310ec82016-07-19 17:56:20 +0000737 pointer __right_;
738 __parent_pointer __parent_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000739 bool __is_black_;
740
Eric Fiselier7310ec82016-07-19 17:56:20 +0000741 _LIBCPP_INLINE_VISIBILITY
742 pointer __parent_unsafe() const { return static_cast<pointer>(__parent_);}
743
744 _LIBCPP_INLINE_VISIBILITY
745 void __set_parent(pointer __p) {
746 __parent_ = static_cast<__parent_pointer>(__p);
747 }
748
Eric Fiselierdb215062016-03-31 02:15:15 +0000749private:
750 ~__tree_node_base() _LIBCPP_EQUAL_DELETE;
751 __tree_node_base(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
752 __tree_node_base& operator=(__tree_node_base const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000753};
754
755template <class _Tp, class _VoidPtr>
756class __tree_node
757 : public __tree_node_base<_VoidPtr>
758{
759public:
Eric Fiselier55263482016-02-20 05:28:30 +0000760 typedef _Tp __node_value_type;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000761
Eric Fiselier55263482016-02-20 05:28:30 +0000762 __node_value_type __value_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000763
Eric Fiselierdb215062016-03-31 02:15:15 +0000764private:
765 ~__tree_node() _LIBCPP_EQUAL_DELETE;
766 __tree_node(__tree_node const&) _LIBCPP_EQUAL_DELETE;
767 __tree_node& operator=(__tree_node const&) _LIBCPP_EQUAL_DELETE;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000768};
769
Eric Fiselierdb215062016-03-31 02:15:15 +0000770
771template <class _Allocator>
772class __tree_node_destructor
773{
774 typedef _Allocator allocator_type;
775 typedef allocator_traits<allocator_type> __alloc_traits;
776
777public:
778 typedef typename __alloc_traits::pointer pointer;
779private:
780 typedef __tree_node_types<pointer> _NodeTypes;
781 allocator_type& __na_;
782
783 __tree_node_destructor& operator=(const __tree_node_destructor&);
784
785public:
786 bool __value_constructed;
787
788 _LIBCPP_INLINE_VISIBILITY
789 explicit __tree_node_destructor(allocator_type& __na, bool __val = false) _NOEXCEPT
790 : __na_(__na),
791 __value_constructed(__val)
792 {}
793
794 _LIBCPP_INLINE_VISIBILITY
795 void operator()(pointer __p) _NOEXCEPT
796 {
797 if (__value_constructed)
798 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
799 if (__p)
800 __alloc_traits::deallocate(__na_, __p, 1);
801 }
802
803 template <class> friend class __map_node_destructor;
804};
805
806
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000807template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000808class _LIBCPP_TEMPLATE_VIS __tree_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809{
Eric Fiselier55263482016-02-20 05:28:30 +0000810 typedef __tree_node_types<_NodePtr> _NodeTypes;
811 typedef _NodePtr __node_pointer;
812 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000813 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
814 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000815 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000816
Eric Fiselier7310ec82016-07-19 17:56:20 +0000817 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000818
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000819public:
Eric Fiselier55263482016-02-20 05:28:30 +0000820 typedef bidirectional_iterator_tag iterator_category;
821 typedef _Tp value_type;
822 typedef _DiffType difference_type;
823 typedef value_type& reference;
824 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000825
Marshall Clow051c8482013-08-08 21:52:50 +0000826 _LIBCPP_INLINE_VISIBILITY __tree_iterator() _NOEXCEPT
827#if _LIBCPP_STD_VER > 11
828 : __ptr_(nullptr)
829#endif
830 {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000831
Eric Fiselier7310ec82016-07-19 17:56:20 +0000832 _LIBCPP_INLINE_VISIBILITY reference operator*() const
833 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000834 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000835 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000836
Howard Hinnant333f50d2010-09-21 20:16:37 +0000837 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000838 __tree_iterator& operator++() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000839 __ptr_ = static_cast<__iter_pointer>(
840 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000841 return *this;
842 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000844 __tree_iterator operator++(int)
845 {__tree_iterator __t(*this); ++(*this); return __t;}
846
Howard Hinnant333f50d2010-09-21 20:16:37 +0000847 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000848 __tree_iterator& operator--() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000849 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
850 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000851 return *this;
852 }
Howard Hinnant333f50d2010-09-21 20:16:37 +0000853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000854 __tree_iterator operator--(int)
855 {__tree_iterator __t(*this); --(*this); return __t;}
856
Howard Hinnant333f50d2010-09-21 20:16:37 +0000857 friend _LIBCPP_INLINE_VISIBILITY
858 bool operator==(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000859 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000860 friend _LIBCPP_INLINE_VISIBILITY
861 bool operator!=(const __tree_iterator& __x, const __tree_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862 {return !(__x == __y);}
863
864private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000865 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000866 explicit __tree_iterator(__node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
Eric Fiselier7310ec82016-07-19 17:56:20 +0000867 _LIBCPP_INLINE_VISIBILITY
868 explicit __tree_iterator(__end_node_pointer __p) _NOEXCEPT : __ptr_(__p) {}
869 _LIBCPP_INLINE_VISIBILITY
870 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000871 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000872 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
873 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_iterator;
874 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
875 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
876 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
877 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000878};
879
Eric Fiselier55263482016-02-20 05:28:30 +0000880template <class _Tp, class _NodePtr, class _DiffType>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000881class _LIBCPP_TEMPLATE_VIS __tree_const_iterator
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000882{
Eric Fiselier55263482016-02-20 05:28:30 +0000883 typedef __tree_node_types<_NodePtr> _NodeTypes;
884 typedef typename _NodeTypes::__node_pointer __node_pointer;
885 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000886 typedef typename _NodeTypes::__end_node_pointer __end_node_pointer;
887 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +0000888 typedef pointer_traits<__node_pointer> __pointer_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000889
Eric Fiselier7310ec82016-07-19 17:56:20 +0000890 __iter_pointer __ptr_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000892public:
Eric Fiselier55263482016-02-20 05:28:30 +0000893 typedef bidirectional_iterator_tag iterator_category;
894 typedef _Tp value_type;
895 typedef _DiffType difference_type;
896 typedef const value_type& reference;
897 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000898
Marshall Clow051c8482013-08-08 21:52:50 +0000899 _LIBCPP_INLINE_VISIBILITY __tree_const_iterator() _NOEXCEPT
900#if _LIBCPP_STD_VER > 11
901 : __ptr_(nullptr)
902#endif
903 {}
904
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000905private:
Eric Fiselier55263482016-02-20 05:28:30 +0000906 typedef __tree_iterator<value_type, __node_pointer, difference_type>
907 __non_const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000908public:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000909 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000910 __tree_const_iterator(__non_const_iterator __p) _NOEXCEPT
911 : __ptr_(__p.__ptr_) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000912
Eric Fiselier7310ec82016-07-19 17:56:20 +0000913 _LIBCPP_INLINE_VISIBILITY reference operator*() const
914 {return __get_np()->__value_;}
Howard Hinnant70342b92013-06-19 21:29:40 +0000915 _LIBCPP_INLINE_VISIBILITY pointer operator->() const
Eric Fiselier7310ec82016-07-19 17:56:20 +0000916 {return pointer_traits<pointer>::pointer_to(__get_np()->__value_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000917
Howard Hinnant333f50d2010-09-21 20:16:37 +0000918 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000919 __tree_const_iterator& operator++() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000920 __ptr_ = static_cast<__iter_pointer>(
921 __tree_next_iter<__end_node_pointer>(static_cast<__node_base_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000922 return *this;
923 }
924
Howard Hinnant333f50d2010-09-21 20:16:37 +0000925 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000926 __tree_const_iterator operator++(int)
927 {__tree_const_iterator __t(*this); ++(*this); return __t;}
928
Howard Hinnant333f50d2010-09-21 20:16:37 +0000929 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000930 __tree_const_iterator& operator--() {
Eric Fiselier7310ec82016-07-19 17:56:20 +0000931 __ptr_ = static_cast<__iter_pointer>(__tree_prev_iter<__node_base_pointer>(
932 static_cast<__end_node_pointer>(__ptr_)));
Eric Fiselier9c8e6632015-03-03 20:10:01 +0000933 return *this;
934 }
935
Howard Hinnant333f50d2010-09-21 20:16:37 +0000936 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000937 __tree_const_iterator operator--(int)
938 {__tree_const_iterator __t(*this); --(*this); return __t;}
939
Howard Hinnant333f50d2010-09-21 20:16:37 +0000940 friend _LIBCPP_INLINE_VISIBILITY
941 bool operator==(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000942 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +0000943 friend _LIBCPP_INLINE_VISIBILITY
944 bool operator!=(const __tree_const_iterator& __x, const __tree_const_iterator& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000945 {return !(__x == __y);}
946
947private:
Howard Hinnant333f50d2010-09-21 20:16:37 +0000948 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +0000949 explicit __tree_const_iterator(__node_pointer __p) _NOEXCEPT
950 : __ptr_(__p) {}
Eric Fiselier7310ec82016-07-19 17:56:20 +0000951 _LIBCPP_INLINE_VISIBILITY
952 explicit __tree_const_iterator(__end_node_pointer __p) _NOEXCEPT
953 : __ptr_(__p) {}
954 _LIBCPP_INLINE_VISIBILITY
955 __node_pointer __get_np() const { return static_cast<__node_pointer>(__ptr_); }
956
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957 template <class, class, class> friend class __tree;
Eric Fiselierc3589a82017-01-04 23:56:00 +0000958 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
959 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
960 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS set;
961 template <class, class, class> friend class _LIBCPP_TEMPLATE_VIS multiset;
962 template <class> friend class _LIBCPP_TEMPLATE_VIS __map_const_iterator;
Eric Fiselier7310ec82016-07-19 17:56:20 +0000963
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000964};
965
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000966#ifndef _LIBCPP_CXX03_LANG
967template <class _Tp, class _Compare, class _Allocator>
968struct __diagnose_tree_helper {
969 static constexpr bool __trigger_diagnostics()
Eric Fiseliereaf29202017-01-13 22:42:53 +0000970 _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000971 "the specified comparator type does not provide a const call operator")
972 { return true; }
973};
974
975template <class _Key, class _Value, class _KeyComp, class _Alloc>
976struct __diagnose_tree_helper<
977 __value_type<_Key, _Value>,
978 __map_value_compare<_Key, __value_type<_Key, _Value>, _KeyComp>,
979 _Alloc
Eric Fiseliereaf29202017-01-13 22:42:53 +0000980> : __diagnose_tree_helper<_Key, _KeyComp, _Alloc>
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000981{
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000982};
Eric Fiseliereaf29202017-01-13 22:42:53 +0000983#endif // !_LIBCPP_CXX03_LANG
Eric Fiselierebaf7da2017-01-13 22:02:08 +0000984
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000985template <class _Tp, class _Compare, class _Allocator>
986class __tree
987{
988public:
989 typedef _Tp value_type;
990 typedef _Compare value_compare;
991 typedef _Allocator allocator_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000992
993private:
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000994 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier55263482016-02-20 05:28:30 +0000995 typedef typename __make_tree_node_types<value_type,
996 typename __alloc_traits::void_pointer>::type
997 _NodeTypes;
Eric Fiselierdb215062016-03-31 02:15:15 +0000998 typedef typename _NodeTypes::key_type key_type;
Eric Fiselier55263482016-02-20 05:28:30 +0000999public:
1000 typedef typename _NodeTypes::__node_value_type __node_value_type;
1001 typedef typename _NodeTypes::__container_value_type __container_value_type;
1002
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001003 typedef typename __alloc_traits::pointer pointer;
1004 typedef typename __alloc_traits::const_pointer const_pointer;
1005 typedef typename __alloc_traits::size_type size_type;
1006 typedef typename __alloc_traits::difference_type difference_type;
1007
Eric Fiselier55263482016-02-20 05:28:30 +00001008public:
1009 typedef typename _NodeTypes::__void_pointer __void_pointer;
Howard Hinnant70342b92013-06-19 21:29:40 +00001010
Eric Fiselier55263482016-02-20 05:28:30 +00001011 typedef typename _NodeTypes::__node_type __node;
1012 typedef typename _NodeTypes::__node_pointer __node_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +00001013
1014 typedef typename _NodeTypes::__node_base_type __node_base;
1015 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier55263482016-02-20 05:28:30 +00001016
1017 typedef typename _NodeTypes::__end_node_type __end_node_t;
1018 typedef typename _NodeTypes::__end_node_pointer __end_node_ptr;
Eric Fiselier55263482016-02-20 05:28:30 +00001019
Eric Fiselier7310ec82016-07-19 17:56:20 +00001020 typedef typename _NodeTypes::__parent_pointer __parent_pointer;
1021 typedef typename _NodeTypes::__iter_pointer __iter_pointer;
1022
Marshall Clow66302c62015-04-07 05:21:38 +00001023 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Eric Fiselier55263482016-02-20 05:28:30 +00001024 typedef allocator_traits<__node_allocator> __node_traits;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001025
Eric Fiselier55263482016-02-20 05:28:30 +00001026private:
1027 // check for sane allocator pointer rebinding semantics. Rebinding the
1028 // allocator for a new pointer type should be exactly the same as rebinding
1029 // the pointer using 'pointer_traits'.
1030 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
1031 "Allocator does not rebind pointers in a sane manner.");
1032 typedef typename __rebind_alloc_helper<__node_traits, __node_base>::type
1033 __node_base_allocator;
1034 typedef allocator_traits<__node_base_allocator> __node_base_traits;
1035 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
1036 "Allocator does not rebind pointers in a sane manner.");
1037
1038private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001039 __iter_pointer __begin_node_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001040 __compressed_pair<__end_node_t, __node_allocator> __pair1_;
1041 __compressed_pair<size_type, value_compare> __pair3_;
1042
1043public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001044 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001045 __iter_pointer __end_node() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001046 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001047 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001048 pointer_traits<__end_node_ptr>::pointer_to(__pair1_.first())
1049 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001051 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001052 __iter_pointer __end_node() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001053 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001054 return static_cast<__iter_pointer>(
Eric Fiselier227b47c2016-02-20 07:12:17 +00001055 pointer_traits<__end_node_ptr>::pointer_to(
1056 const_cast<__end_node_t&>(__pair1_.first())
1057 )
1058 );
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001059 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001060 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001061 __node_allocator& __node_alloc() _NOEXCEPT {return __pair1_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001062private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001063 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001064 const __node_allocator& __node_alloc() const _NOEXCEPT
1065 {return __pair1_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001066 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001067 __iter_pointer& __begin_node() _NOEXCEPT {return __begin_node_;}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001068 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier7310ec82016-07-19 17:56:20 +00001069 const __iter_pointer& __begin_node() const _NOEXCEPT {return __begin_node_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001070public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001071 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001072 allocator_type __alloc() const _NOEXCEPT
1073 {return allocator_type(__node_alloc());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074private:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001075 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001076 size_type& size() _NOEXCEPT {return __pair3_.first();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001077public:
Howard Hinnant333f50d2010-09-21 20:16:37 +00001078 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001079 const size_type& size() const _NOEXCEPT {return __pair3_.first();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001080 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001081 value_compare& value_comp() _NOEXCEPT {return __pair3_.second();}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001082 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001083 const value_compare& value_comp() const _NOEXCEPT
1084 {return __pair3_.second();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001085public:
Eric Fiselier227b47c2016-02-20 07:12:17 +00001086
Howard Hinnant333f50d2010-09-21 20:16:37 +00001087 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier227b47c2016-02-20 07:12:17 +00001088 __node_pointer __root() const _NOEXCEPT
1089 {return static_cast<__node_pointer>(__end_node()->__left_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001090
Eric Fiselier7310ec82016-07-19 17:56:20 +00001091 __node_base_pointer* __root_ptr() const _NOEXCEPT {
1092 return _VSTD::addressof(__end_node()->__left_);
1093 }
1094
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001095 typedef __tree_iterator<value_type, __node_pointer, difference_type> iterator;
Howard Hinnant70342b92013-06-19 21:29:40 +00001096 typedef __tree_const_iterator<value_type, __node_pointer, difference_type> const_iterator;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001097
Howard Hinnant7686add2011-06-04 14:31:57 +00001098 explicit __tree(const value_compare& __comp)
1099 _NOEXCEPT_(
1100 is_nothrow_default_constructible<__node_allocator>::value &&
1101 is_nothrow_copy_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001102 explicit __tree(const allocator_type& __a);
1103 __tree(const value_compare& __comp, const allocator_type& __a);
1104 __tree(const __tree& __t);
1105 __tree& operator=(const __tree& __t);
1106 template <class _InputIterator>
1107 void __assign_unique(_InputIterator __first, _InputIterator __last);
1108 template <class _InputIterator>
1109 void __assign_multi(_InputIterator __first, _InputIterator __last);
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001110#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7686add2011-06-04 14:31:57 +00001111 __tree(__tree&& __t)
1112 _NOEXCEPT_(
1113 is_nothrow_move_constructible<__node_allocator>::value &&
1114 is_nothrow_move_constructible<value_compare>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001115 __tree(__tree&& __t, const allocator_type& __a);
Howard Hinnant7686add2011-06-04 14:31:57 +00001116 __tree& operator=(__tree&& __t)
1117 _NOEXCEPT_(
1118 __node_traits::propagate_on_container_move_assignment::value &&
1119 is_nothrow_move_assignable<value_compare>::value &&
1120 is_nothrow_move_assignable<__node_allocator>::value);
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001121#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001122
1123 ~__tree();
1124
Howard Hinnant333f50d2010-09-21 20:16:37 +00001125 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001126 iterator begin() _NOEXCEPT {return iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001127 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001128 const_iterator begin() const _NOEXCEPT {return const_iterator(__begin_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001129 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001130 iterator end() _NOEXCEPT {return iterator(__end_node());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001131 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001132 const_iterator end() const _NOEXCEPT {return const_iterator(__end_node());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001133
Howard Hinnant333f50d2010-09-21 20:16:37 +00001134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant7686add2011-06-04 14:31:57 +00001135 size_type max_size() const _NOEXCEPT
Eric Fiselieref3060e2016-11-23 01:18:56 +00001136 {return std::min<size_type>(
1137 __node_traits::max_size(__node_alloc()),
1138 numeric_limits<difference_type >::max());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001139
Howard Hinnant7686add2011-06-04 14:31:57 +00001140 void clear() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001141
Howard Hinnant7686add2011-06-04 14:31:57 +00001142 void swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001143#if _LIBCPP_STD_VER <= 11
Howard Hinnant7686add2011-06-04 14:31:57 +00001144 _NOEXCEPT_(
Marshall Clow7d914d12015-07-13 20:04:56 +00001145 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001146 && (!__node_traits::propagate_on_container_swap::value ||
1147 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001148 );
Dimitry Andric1421cf02016-08-27 19:32:03 +00001149#else
1150 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value);
1151#endif
Eric Fiselierdb215062016-03-31 02:15:15 +00001152
1153#ifndef _LIBCPP_CXX03_LANG
1154 template <class _Key, class ..._Args>
1155 pair<iterator, bool>
1156 __emplace_unique_key_args(_Key const&, _Args&&... __args);
1157 template <class _Key, class ..._Args>
1158 iterator
1159 __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001160
1161 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001162 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
Eric Fiselierdb215062016-03-31 02:15:15 +00001163
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001164 template <class... _Args>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001165 iterator __emplace_hint_unique_impl(const_iterator __p, _Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001166
Eric Fiselierdb215062016-03-31 02:15:15 +00001167 template <class... _Args>
1168 iterator __emplace_multi(_Args&&... __args);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001169
Eric Fiselierdb215062016-03-31 02:15:15 +00001170 template <class... _Args>
1171 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001172
1173 template <class _Pp>
1174 _LIBCPP_INLINE_VISIBILITY
1175 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1176 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1177 __can_extract_key<_Pp, key_type>());
1178 }
1179
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001180 template <class _First, class _Second>
1181 _LIBCPP_INLINE_VISIBILITY
1182 typename enable_if<
1183 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1184 pair<iterator, bool>
1185 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1186 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1187 _VSTD::forward<_Second>(__s));
1188 }
1189
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001190 template <class... _Args>
1191 _LIBCPP_INLINE_VISIBILITY
1192 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1193 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1194 }
1195
1196 template <class _Pp>
1197 _LIBCPP_INLINE_VISIBILITY
1198 pair<iterator, bool>
1199 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1200 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1201 }
1202
1203 template <class _Pp>
1204 _LIBCPP_INLINE_VISIBILITY
1205 pair<iterator, bool>
1206 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1207 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1208 }
1209
1210 template <class _Pp>
1211 _LIBCPP_INLINE_VISIBILITY
1212 pair<iterator, bool>
1213 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1214 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1215 }
1216
1217 template <class _Pp>
1218 _LIBCPP_INLINE_VISIBILITY
1219 iterator __emplace_hint_unique(const_iterator __p, _Pp&& __x) {
1220 return __emplace_hint_unique_extract_key(__p, _VSTD::forward<_Pp>(__x),
1221 __can_extract_key<_Pp, key_type>());
1222 }
1223
Eric Fiselierdc414cd2016-04-16 00:23:12 +00001224 template <class _First, class _Second>
1225 _LIBCPP_INLINE_VISIBILITY
1226 typename enable_if<
1227 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1228 iterator
1229 >::type __emplace_hint_unique(const_iterator __p, _First&& __f, _Second&& __s) {
1230 return __emplace_hint_unique_key_args(__p, __f,
1231 _VSTD::forward<_First>(__f),
1232 _VSTD::forward<_Second>(__s));
1233 }
1234
Eric Fiselier83c9dc12016-04-15 23:27:27 +00001235 template <class... _Args>
1236 _LIBCPP_INLINE_VISIBILITY
1237 iterator __emplace_hint_unique(const_iterator __p, _Args&&... __args) {
1238 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Args>(__args)...);
1239 }
1240
1241 template <class _Pp>
1242 _LIBCPP_INLINE_VISIBILITY
1243 iterator
1244 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_fail_tag) {
1245 return __emplace_hint_unique_impl(__p, _VSTD::forward<_Pp>(__x));
1246 }
1247
1248 template <class _Pp>
1249 _LIBCPP_INLINE_VISIBILITY
1250 iterator
1251 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_self_tag) {
1252 return __emplace_hint_unique_key_args(__p, __x, _VSTD::forward<_Pp>(__x));
1253 }
1254
1255 template <class _Pp>
1256 _LIBCPP_INLINE_VISIBILITY
1257 iterator
1258 __emplace_hint_unique_extract_key(const_iterator __p, _Pp&& __x, __extract_key_first_tag) {
1259 return __emplace_hint_unique_key_args(__p, __x.first, _VSTD::forward<_Pp>(__x));
1260 }
1261
Eric Fiselierdb215062016-03-31 02:15:15 +00001262#else
1263 template <class _Key, class _Args>
1264 _LIBCPP_INLINE_VISIBILITY
1265 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1266 template <class _Key, class _Args>
1267 _LIBCPP_INLINE_VISIBILITY
1268 iterator __emplace_hint_unique_key_args(const_iterator, _Key const&, _Args&);
Marshall Clow3426a862016-01-05 19:32:41 +00001269#endif
1270
Eric Fiselierdb215062016-03-31 02:15:15 +00001271 _LIBCPP_INLINE_VISIBILITY
1272 pair<iterator, bool> __insert_unique(const __container_value_type& __v) {
1273 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), __v);
1274 }
1275
1276 _LIBCPP_INLINE_VISIBILITY
1277 iterator __insert_unique(const_iterator __p, const __container_value_type& __v) {
1278 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), __v);
1279 }
1280
1281#ifdef _LIBCPP_CXX03_LANG
1282 _LIBCPP_INLINE_VISIBILITY
1283 iterator __insert_multi(const __container_value_type& __v);
1284 _LIBCPP_INLINE_VISIBILITY
1285 iterator __insert_multi(const_iterator __p, const __container_value_type& __v);
1286#else
1287 _LIBCPP_INLINE_VISIBILITY
1288 pair<iterator, bool> __insert_unique(__container_value_type&& __v) {
1289 return __emplace_unique_key_args(_NodeTypes::__get_key(__v), _VSTD::move(__v));
1290 }
1291
1292 _LIBCPP_INLINE_VISIBILITY
1293 iterator __insert_unique(const_iterator __p, __container_value_type&& __v) {
1294 return __emplace_hint_unique_key_args(__p, _NodeTypes::__get_key(__v), _VSTD::move(__v));
1295 }
1296
1297 template <class _Vp, class = typename enable_if<
1298 !is_same<typename __unconstref<_Vp>::type,
1299 __container_value_type
1300 >::value
1301 >::type>
1302 _LIBCPP_INLINE_VISIBILITY
1303 pair<iterator, bool> __insert_unique(_Vp&& __v) {
1304 return __emplace_unique(_VSTD::forward<_Vp>(__v));
1305 }
1306
1307 template <class _Vp, class = typename enable_if<
1308 !is_same<typename __unconstref<_Vp>::type,
1309 __container_value_type
1310 >::value
1311 >::type>
1312 _LIBCPP_INLINE_VISIBILITY
1313 iterator __insert_unique(const_iterator __p, _Vp&& __v) {
1314 return __emplace_hint_unique(__p, _VSTD::forward<_Vp>(__v));
1315 }
1316
1317 _LIBCPP_INLINE_VISIBILITY
1318 iterator __insert_multi(__container_value_type&& __v) {
1319 return __emplace_multi(_VSTD::move(__v));
1320 }
1321
1322 _LIBCPP_INLINE_VISIBILITY
1323 iterator __insert_multi(const_iterator __p, __container_value_type&& __v) {
1324 return __emplace_hint_multi(__p, _VSTD::move(__v));
1325 }
1326
1327 template <class _Vp>
1328 _LIBCPP_INLINE_VISIBILITY
1329 iterator __insert_multi(_Vp&& __v) {
1330 return __emplace_multi(_VSTD::forward<_Vp>(__v));
1331 }
1332
1333 template <class _Vp>
1334 _LIBCPP_INLINE_VISIBILITY
1335 iterator __insert_multi(const_iterator __p, _Vp&& __v) {
1336 return __emplace_hint_multi(__p, _VSTD::forward<_Vp>(__v));
1337 }
1338
1339#endif // !_LIBCPP_CXX03_LANG
1340
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001341 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1342 iterator __node_insert_unique(const_iterator __p,
1343 __node_pointer __nd);
1344
1345 iterator __node_insert_multi(__node_pointer __nd);
1346 iterator __node_insert_multi(const_iterator __p, __node_pointer __nd);
1347
1348 iterator erase(const_iterator __p);
1349 iterator erase(const_iterator __f, const_iterator __l);
1350 template <class _Key>
1351 size_type __erase_unique(const _Key& __k);
1352 template <class _Key>
1353 size_type __erase_multi(const _Key& __k);
1354
Eric Fiselier7310ec82016-07-19 17:56:20 +00001355 void __insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001356 __node_base_pointer& __child,
1357 __node_base_pointer __new_node);
1358
1359 template <class _Key>
1360 iterator find(const _Key& __v);
1361 template <class _Key>
1362 const_iterator find(const _Key& __v) const;
1363
1364 template <class _Key>
1365 size_type __count_unique(const _Key& __k) const;
1366 template <class _Key>
1367 size_type __count_multi(const _Key& __k) const;
1368
1369 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001370 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001371 iterator lower_bound(const _Key& __v)
1372 {return __lower_bound(__v, __root(), __end_node());}
1373 template <class _Key>
1374 iterator __lower_bound(const _Key& __v,
1375 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001376 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001377 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001378 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001379 const_iterator lower_bound(const _Key& __v) const
1380 {return __lower_bound(__v, __root(), __end_node());}
1381 template <class _Key>
1382 const_iterator __lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001383 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001384 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001385 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001386 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001387 iterator upper_bound(const _Key& __v)
1388 {return __upper_bound(__v, __root(), __end_node());}
1389 template <class _Key>
1390 iterator __upper_bound(const _Key& __v,
1391 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001392 __iter_pointer __result);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001393 template <class _Key>
Howard Hinnant333f50d2010-09-21 20:16:37 +00001394 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001395 const_iterator upper_bound(const _Key& __v) const
1396 {return __upper_bound(__v, __root(), __end_node());}
1397 template <class _Key>
1398 const_iterator __upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00001399 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001400 __iter_pointer __result) const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001401 template <class _Key>
1402 pair<iterator, iterator>
1403 __equal_range_unique(const _Key& __k);
1404 template <class _Key>
1405 pair<const_iterator, const_iterator>
1406 __equal_range_unique(const _Key& __k) const;
1407
1408 template <class _Key>
1409 pair<iterator, iterator>
1410 __equal_range_multi(const _Key& __k);
1411 template <class _Key>
1412 pair<const_iterator, const_iterator>
1413 __equal_range_multi(const _Key& __k) const;
1414
Howard Hinnant99968442011-11-29 18:15:50 +00001415 typedef __tree_node_destructor<__node_allocator> _Dp;
1416 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001417
Howard Hinnant8b537682011-06-04 17:10:24 +00001418 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001419private:
Eric Fiselier7310ec82016-07-19 17:56:20 +00001420 __node_base_pointer&
1421 __find_leaf_low(__parent_pointer& __parent, const key_type& __v);
1422 __node_base_pointer&
1423 __find_leaf_high(__parent_pointer& __parent, const key_type& __v);
1424 __node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001425 __find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001426 __parent_pointer& __parent, const key_type& __v);
Eric Fiselier856712b2017-01-05 06:06:18 +00001427 // FIXME: Make this function const qualified. Unfortunetly doing so
1428 // breaks existing code which uses non-const callable comparators.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001429 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001430 __node_base_pointer&
1431 __find_equal(__parent_pointer& __parent, const _Key& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001432 template <class _Key>
Eric Fiselier856712b2017-01-05 06:06:18 +00001433 _LIBCPP_INLINE_VISIBILITY __node_base_pointer&
1434 __find_equal(__parent_pointer& __parent, const _Key& __v) const {
1435 return const_cast<__tree*>(this)->__find_equal(__parent, __v);
1436 }
1437 template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001438 __node_base_pointer&
1439 __find_equal(const_iterator __hint, __parent_pointer& __parent,
1440 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001441 const _Key& __v);
1442
Eric Fiselierdb215062016-03-31 02:15:15 +00001443#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001444 template <class ..._Args>
Eric Fiselierdb215062016-03-31 02:15:15 +00001445 __node_holder __construct_node(_Args&& ...__args);
1446#else
1447 __node_holder __construct_node(const __container_value_type& __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001448#endif
1449
Howard Hinnant7686add2011-06-04 14:31:57 +00001450 void destroy(__node_pointer __nd) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001451
Howard Hinnant333f50d2010-09-21 20:16:37 +00001452 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001453 void __copy_assign_alloc(const __tree& __t)
1454 {__copy_assign_alloc(__t, integral_constant<bool,
1455 __node_traits::propagate_on_container_copy_assignment::value>());}
1456
Howard Hinnant333f50d2010-09-21 20:16:37 +00001457 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001458 void __copy_assign_alloc(const __tree& __t, true_type)
Marshall Clow546498c2016-08-17 23:24:02 +00001459 {
1460 if (__node_alloc() != __t.__node_alloc())
1461 clear();
1462 __node_alloc() = __t.__node_alloc();
1463 }
Howard Hinnant333f50d2010-09-21 20:16:37 +00001464 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001465 void __copy_assign_alloc(const __tree&, false_type) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001466
1467 void __move_assign(__tree& __t, false_type);
Howard Hinnant7686add2011-06-04 14:31:57 +00001468 void __move_assign(__tree& __t, true_type)
1469 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1470 is_nothrow_move_assignable<__node_allocator>::value);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001471
Howard Hinnant333f50d2010-09-21 20:16:37 +00001472 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001473 void __move_assign_alloc(__tree& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001474 _NOEXCEPT_(
1475 !__node_traits::propagate_on_container_move_assignment::value ||
1476 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001477 {__move_assign_alloc(__t, integral_constant<bool,
1478 __node_traits::propagate_on_container_move_assignment::value>());}
1479
Howard Hinnant333f50d2010-09-21 20:16:37 +00001480 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001481 void __move_assign_alloc(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001482 _NOEXCEPT_(is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001483 {__node_alloc() = _VSTD::move(__t.__node_alloc());}
Howard Hinnant333f50d2010-09-21 20:16:37 +00001484 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier0e5ebbc2016-12-23 23:37:52 +00001485 void __move_assign_alloc(__tree&, false_type) _NOEXCEPT {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001486
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001487 __node_pointer __detach();
1488 static __node_pointer __detach(__node_pointer);
Howard Hinnant70342b92013-06-19 21:29:40 +00001489
Eric Fiselierc3589a82017-01-04 23:56:00 +00001490 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS map;
1491 template <class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS multimap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001492};
1493
1494template <class _Tp, class _Compare, class _Allocator>
1495__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp)
Howard Hinnant7686add2011-06-04 14:31:57 +00001496 _NOEXCEPT_(
1497 is_nothrow_default_constructible<__node_allocator>::value &&
1498 is_nothrow_copy_constructible<value_compare>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001499 : __pair3_(0, __comp)
1500{
1501 __begin_node() = __end_node();
1502}
1503
1504template <class _Tp, class _Compare, class _Allocator>
1505__tree<_Tp, _Compare, _Allocator>::__tree(const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001506 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001507 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001508 __pair3_(0)
1509{
1510 __begin_node() = __end_node();
1511}
1512
1513template <class _Tp, class _Compare, class _Allocator>
1514__tree<_Tp, _Compare, _Allocator>::__tree(const value_compare& __comp,
1515 const allocator_type& __a)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001516 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001517 __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001518 __pair3_(0, __comp)
1519{
1520 __begin_node() = __end_node();
1521}
1522
1523// Precondition: size() != 0
1524template <class _Tp, class _Compare, class _Allocator>
1525typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1526__tree<_Tp, _Compare, _Allocator>::__detach()
1527{
Eric Fiselier7310ec82016-07-19 17:56:20 +00001528 __node_pointer __cache = static_cast<__node_pointer>(__begin_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001529 __begin_node() = __end_node();
1530 __end_node()->__left_->__parent_ = nullptr;
1531 __end_node()->__left_ = nullptr;
1532 size() = 0;
1533 // __cache->__left_ == nullptr
1534 if (__cache->__right_ != nullptr)
1535 __cache = static_cast<__node_pointer>(__cache->__right_);
1536 // __cache->__left_ == nullptr
1537 // __cache->__right_ == nullptr
1538 return __cache;
1539}
1540
1541// Precondition: __cache != nullptr
1542// __cache->left_ == nullptr
1543// __cache->right_ == nullptr
1544// This is no longer a red-black tree
1545template <class _Tp, class _Compare, class _Allocator>
1546typename __tree<_Tp, _Compare, _Allocator>::__node_pointer
1547__tree<_Tp, _Compare, _Allocator>::__detach(__node_pointer __cache)
1548{
1549 if (__cache->__parent_ == nullptr)
1550 return nullptr;
Howard Hinnant70342b92013-06-19 21:29:40 +00001551 if (__tree_is_left_child(static_cast<__node_base_pointer>(__cache)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001552 {
1553 __cache->__parent_->__left_ = nullptr;
1554 __cache = static_cast<__node_pointer>(__cache->__parent_);
1555 if (__cache->__right_ == nullptr)
1556 return __cache;
1557 return static_cast<__node_pointer>(__tree_leaf(__cache->__right_));
1558 }
1559 // __cache is right child
Eric Fiselier7310ec82016-07-19 17:56:20 +00001560 __cache->__parent_unsafe()->__right_ = nullptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001561 __cache = static_cast<__node_pointer>(__cache->__parent_);
1562 if (__cache->__left_ == nullptr)
1563 return __cache;
1564 return static_cast<__node_pointer>(__tree_leaf(__cache->__left_));
1565}
1566
1567template <class _Tp, class _Compare, class _Allocator>
1568__tree<_Tp, _Compare, _Allocator>&
1569__tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t)
1570{
1571 if (this != &__t)
1572 {
1573 value_comp() = __t.value_comp();
1574 __copy_assign_alloc(__t);
1575 __assign_multi(__t.begin(), __t.end());
1576 }
1577 return *this;
1578}
1579
1580template <class _Tp, class _Compare, class _Allocator>
1581template <class _InputIterator>
1582void
1583__tree<_Tp, _Compare, _Allocator>::__assign_unique(_InputIterator __first, _InputIterator __last)
1584{
Eric Fiselierdb215062016-03-31 02:15:15 +00001585 typedef iterator_traits<_InputIterator> _ITraits;
1586 typedef typename _ITraits::value_type _ItValueType;
1587 static_assert((is_same<_ItValueType, __container_value_type>::value),
1588 "__assign_unique may only be called with the containers value type");
1589
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001590 if (size() != 0)
1591 {
1592 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001593#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001594 try
1595 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001596#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001597 for (; __cache != nullptr && __first != __last; ++__first)
1598 {
1599 __cache->__value_ = *__first;
1600 __node_pointer __next = __detach(__cache);
1601 __node_insert_unique(__cache);
1602 __cache = __next;
1603 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001604#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001605 }
1606 catch (...)
1607 {
1608 while (__cache->__parent_ != nullptr)
1609 __cache = static_cast<__node_pointer>(__cache->__parent_);
1610 destroy(__cache);
1611 throw;
1612 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001613#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001614 if (__cache != nullptr)
1615 {
1616 while (__cache->__parent_ != nullptr)
1617 __cache = static_cast<__node_pointer>(__cache->__parent_);
1618 destroy(__cache);
1619 }
1620 }
1621 for (; __first != __last; ++__first)
1622 __insert_unique(*__first);
1623}
1624
1625template <class _Tp, class _Compare, class _Allocator>
1626template <class _InputIterator>
1627void
1628__tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _InputIterator __last)
1629{
Eric Fiselierdb215062016-03-31 02:15:15 +00001630 typedef iterator_traits<_InputIterator> _ITraits;
1631 typedef typename _ITraits::value_type _ItValueType;
1632 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1633 is_same<_ItValueType, __node_value_type>::value),
1634 "__assign_multi may only be called with the containers value type"
1635 " or the nodes value type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001636 if (size() != 0)
1637 {
1638 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001639#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001640 try
1641 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001642#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001643 for (; __cache != nullptr && __first != __last; ++__first)
1644 {
1645 __cache->__value_ = *__first;
1646 __node_pointer __next = __detach(__cache);
1647 __node_insert_multi(__cache);
1648 __cache = __next;
1649 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001650#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001651 }
1652 catch (...)
1653 {
1654 while (__cache->__parent_ != nullptr)
1655 __cache = static_cast<__node_pointer>(__cache->__parent_);
1656 destroy(__cache);
1657 throw;
1658 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001660 if (__cache != nullptr)
1661 {
1662 while (__cache->__parent_ != nullptr)
1663 __cache = static_cast<__node_pointer>(__cache->__parent_);
1664 destroy(__cache);
1665 }
1666 }
1667 for (; __first != __last; ++__first)
Eric Fiselierdb215062016-03-31 02:15:15 +00001668 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001669}
1670
1671template <class _Tp, class _Compare, class _Allocator>
1672__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t)
Eric Fiselier7310ec82016-07-19 17:56:20 +00001673 : __begin_node_(__iter_pointer()),
Eric Fiselier161ccc12017-04-13 00:34:24 +00001674 __pair1_(__second_tag(), __node_traits::select_on_container_copy_construction(__t.__node_alloc())),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001675 __pair3_(0, __t.value_comp())
1676{
1677 __begin_node() = __end_node();
1678}
1679
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001680#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001681
1682template <class _Tp, class _Compare, class _Allocator>
1683__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001684 _NOEXCEPT_(
1685 is_nothrow_move_constructible<__node_allocator>::value &&
1686 is_nothrow_move_constructible<value_compare>::value)
Howard Hinnant0949eed2011-06-30 21:18:19 +00001687 : __begin_node_(_VSTD::move(__t.__begin_node_)),
1688 __pair1_(_VSTD::move(__t.__pair1_)),
1689 __pair3_(_VSTD::move(__t.__pair3_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001690{
1691 if (size() == 0)
1692 __begin_node() = __end_node();
1693 else
1694 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001695 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001696 __t.__begin_node() = __t.__end_node();
1697 __t.__end_node()->__left_ = nullptr;
1698 __t.size() = 0;
1699 }
1700}
1701
1702template <class _Tp, class _Compare, class _Allocator>
1703__tree<_Tp, _Compare, _Allocator>::__tree(__tree&& __t, const allocator_type& __a)
Eric Fiselier161ccc12017-04-13 00:34:24 +00001704 : __pair1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant0949eed2011-06-30 21:18:19 +00001705 __pair3_(0, _VSTD::move(__t.value_comp()))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001706{
1707 if (__a == __t.__alloc())
1708 {
1709 if (__t.size() == 0)
1710 __begin_node() = __end_node();
1711 else
1712 {
1713 __begin_node() = __t.__begin_node();
1714 __end_node()->__left_ = __t.__end_node()->__left_;
Eric Fiselier7310ec82016-07-19 17:56:20 +00001715 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001716 size() = __t.size();
1717 __t.__begin_node() = __t.__end_node();
1718 __t.__end_node()->__left_ = nullptr;
1719 __t.size() = 0;
1720 }
1721 }
1722 else
1723 {
1724 __begin_node() = __end_node();
1725 }
1726}
1727
1728template <class _Tp, class _Compare, class _Allocator>
1729void
1730__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, true_type)
Howard Hinnant7686add2011-06-04 14:31:57 +00001731 _NOEXCEPT_(is_nothrow_move_assignable<value_compare>::value &&
1732 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001733{
1734 destroy(static_cast<__node_pointer>(__end_node()->__left_));
1735 __begin_node_ = __t.__begin_node_;
1736 __pair1_.first() = __t.__pair1_.first();
1737 __move_assign_alloc(__t);
Howard Hinnant0949eed2011-06-30 21:18:19 +00001738 __pair3_ = _VSTD::move(__t.__pair3_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001739 if (size() == 0)
1740 __begin_node() = __end_node();
1741 else
1742 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001743 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001744 __t.__begin_node() = __t.__end_node();
1745 __t.__end_node()->__left_ = nullptr;
1746 __t.size() = 0;
1747 }
1748}
1749
1750template <class _Tp, class _Compare, class _Allocator>
1751void
1752__tree<_Tp, _Compare, _Allocator>::__move_assign(__tree& __t, false_type)
1753{
1754 if (__node_alloc() == __t.__node_alloc())
1755 __move_assign(__t, true_type());
1756 else
1757 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001758 value_comp() = _VSTD::move(__t.value_comp());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001759 const_iterator __e = end();
1760 if (size() != 0)
1761 {
1762 __node_pointer __cache = __detach();
Howard Hinnantd4444702010-08-11 17:04:31 +00001763#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001764 try
1765 {
Howard Hinnant324bb032010-08-22 00:02:43 +00001766#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001767 while (__cache != nullptr && __t.size() != 0)
1768 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00001769 __cache->__value_ = _VSTD::move(__t.remove(__t.begin())->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001770 __node_pointer __next = __detach(__cache);
1771 __node_insert_multi(__cache);
1772 __cache = __next;
1773 }
Howard Hinnantd4444702010-08-11 17:04:31 +00001774#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001775 }
1776 catch (...)
1777 {
1778 while (__cache->__parent_ != nullptr)
1779 __cache = static_cast<__node_pointer>(__cache->__parent_);
1780 destroy(__cache);
1781 throw;
1782 }
Howard Hinnant324bb032010-08-22 00:02:43 +00001783#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001784 if (__cache != nullptr)
1785 {
1786 while (__cache->__parent_ != nullptr)
1787 __cache = static_cast<__node_pointer>(__cache->__parent_);
1788 destroy(__cache);
1789 }
1790 }
1791 while (__t.size() != 0)
Eric Fiselierdb215062016-03-31 02:15:15 +00001792 __insert_multi(__e, _NodeTypes::__move(__t.remove(__t.begin())->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001793 }
1794}
1795
1796template <class _Tp, class _Compare, class _Allocator>
1797__tree<_Tp, _Compare, _Allocator>&
1798__tree<_Tp, _Compare, _Allocator>::operator=(__tree&& __t)
Howard Hinnant7686add2011-06-04 14:31:57 +00001799 _NOEXCEPT_(
1800 __node_traits::propagate_on_container_move_assignment::value &&
1801 is_nothrow_move_assignable<value_compare>::value &&
1802 is_nothrow_move_assignable<__node_allocator>::value)
1803
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001804{
1805 __move_assign(__t, integral_constant<bool,
1806 __node_traits::propagate_on_container_move_assignment::value>());
1807 return *this;
1808}
1809
Eric Fiselier5875c1d2017-04-19 01:23:04 +00001810#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001811
1812template <class _Tp, class _Compare, class _Allocator>
1813__tree<_Tp, _Compare, _Allocator>::~__tree()
1814{
Marshall Clow1a933122016-06-30 22:05:45 +00001815 static_assert((is_copy_constructible<value_compare>::value),
1816 "Comparator must be copy-constructible.");
Eric Fiselierebaf7da2017-01-13 22:02:08 +00001817#ifndef _LIBCPP_CXX03_LANG
1818 static_assert((__diagnose_tree_helper<_Tp, _Compare, _Allocator>::
1819 __trigger_diagnostics()), "");
1820#endif
1821 destroy(__root());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001822}
1823
1824template <class _Tp, class _Compare, class _Allocator>
1825void
Howard Hinnant7686add2011-06-04 14:31:57 +00001826__tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001827{
1828 if (__nd != nullptr)
1829 {
1830 destroy(static_cast<__node_pointer>(__nd->__left_));
1831 destroy(static_cast<__node_pointer>(__nd->__right_));
1832 __node_allocator& __na = __node_alloc();
Eric Fiselierdb215062016-03-31 02:15:15 +00001833 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__nd->__value_));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001834 __node_traits::deallocate(__na, __nd, 1);
1835 }
1836}
1837
1838template <class _Tp, class _Compare, class _Allocator>
1839void
1840__tree<_Tp, _Compare, _Allocator>::swap(__tree& __t)
Dimitry Andric1421cf02016-08-27 19:32:03 +00001841#if _LIBCPP_STD_VER <= 11
Marshall Clow7d914d12015-07-13 20:04:56 +00001842 _NOEXCEPT_(
1843 __is_nothrow_swappable<value_compare>::value
Marshall Clow7d914d12015-07-13 20:04:56 +00001844 && (!__node_traits::propagate_on_container_swap::value ||
1845 __is_nothrow_swappable<__node_allocator>::value)
Marshall Clow7d914d12015-07-13 20:04:56 +00001846 )
Dimitry Andric1421cf02016-08-27 19:32:03 +00001847#else
1848 _NOEXCEPT_(__is_nothrow_swappable<value_compare>::value)
1849#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001850{
Howard Hinnant0949eed2011-06-30 21:18:19 +00001851 using _VSTD::swap;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001852 swap(__begin_node_, __t.__begin_node_);
1853 swap(__pair1_.first(), __t.__pair1_.first());
Marshall Clow7d914d12015-07-13 20:04:56 +00001854 __swap_allocator(__node_alloc(), __t.__node_alloc());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001855 __pair3_.swap(__t.__pair3_);
1856 if (size() == 0)
1857 __begin_node() = __end_node();
1858 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001859 __end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001860 if (__t.size() == 0)
1861 __t.__begin_node() = __t.__end_node();
1862 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00001863 __t.__end_node()->__left_->__parent_ = static_cast<__parent_pointer>(__t.__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001864}
1865
1866template <class _Tp, class _Compare, class _Allocator>
1867void
Howard Hinnant7686add2011-06-04 14:31:57 +00001868__tree<_Tp, _Compare, _Allocator>::clear() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001869{
1870 destroy(__root());
1871 size() = 0;
1872 __begin_node() = __end_node();
1873 __end_node()->__left_ = nullptr;
1874}
1875
1876// Find lower_bound place to insert
1877// Set __parent to parent of null leaf
1878// Return reference to null leaf
1879template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001880typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1881__tree<_Tp, _Compare, _Allocator>::__find_leaf_low(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001882 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001883{
1884 __node_pointer __nd = __root();
1885 if (__nd != nullptr)
1886 {
1887 while (true)
1888 {
1889 if (value_comp()(__nd->__value_, __v))
1890 {
1891 if (__nd->__right_ != nullptr)
1892 __nd = static_cast<__node_pointer>(__nd->__right_);
1893 else
1894 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001895 __parent = static_cast<__parent_pointer>(__nd);
1896 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001897 }
1898 }
1899 else
1900 {
1901 if (__nd->__left_ != nullptr)
1902 __nd = static_cast<__node_pointer>(__nd->__left_);
1903 else
1904 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001905 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001906 return __parent->__left_;
1907 }
1908 }
1909 }
1910 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001911 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001912 return __parent->__left_;
1913}
1914
1915// Find upper_bound place to insert
1916// Set __parent to parent of null leaf
1917// Return reference to null leaf
1918template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001919typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1920__tree<_Tp, _Compare, _Allocator>::__find_leaf_high(__parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001921 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001922{
1923 __node_pointer __nd = __root();
1924 if (__nd != nullptr)
1925 {
1926 while (true)
1927 {
1928 if (value_comp()(__v, __nd->__value_))
1929 {
1930 if (__nd->__left_ != nullptr)
1931 __nd = static_cast<__node_pointer>(__nd->__left_);
1932 else
1933 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001934 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001935 return __parent->__left_;
1936 }
1937 }
1938 else
1939 {
1940 if (__nd->__right_ != nullptr)
1941 __nd = static_cast<__node_pointer>(__nd->__right_);
1942 else
1943 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001944 __parent = static_cast<__parent_pointer>(__nd);
1945 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001946 }
1947 }
1948 }
1949 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00001950 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001951 return __parent->__left_;
1952}
1953
1954// Find leaf place to insert closest to __hint
1955// First check prior to __hint.
1956// Next check after __hint.
1957// Next do O(log N) search.
1958// Set __parent to parent of null leaf
1959// Return reference to null leaf
1960template <class _Tp, class _Compare, class _Allocator>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001961typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001962__tree<_Tp, _Compare, _Allocator>::__find_leaf(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00001963 __parent_pointer& __parent,
Eric Fiselierdb215062016-03-31 02:15:15 +00001964 const key_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001965{
1966 if (__hint == end() || !value_comp()(*__hint, __v)) // check before
1967 {
1968 // __v <= *__hint
1969 const_iterator __prior = __hint;
1970 if (__prior == begin() || !value_comp()(__v, *--__prior))
1971 {
1972 // *prev(__hint) <= __v <= *__hint
1973 if (__hint.__ptr_->__left_ == nullptr)
1974 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001975 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001976 return __parent->__left_;
1977 }
1978 else
1979 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00001980 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
1981 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001982 }
1983 }
1984 // __v < *prev(__hint)
1985 return __find_leaf_high(__parent, __v);
1986 }
1987 // else __v > *__hint
1988 return __find_leaf_low(__parent, __v);
1989}
1990
1991// Find place to insert if __v doesn't exist
1992// Set __parent to parent of null leaf
1993// Return reference to null leaf
1994// If __v exists, set parent to node of __v and return reference to node of __v
1995template <class _Tp, class _Compare, class _Allocator>
1996template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00001997typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
1998__tree<_Tp, _Compare, _Allocator>::__find_equal(__parent_pointer& __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001999 const _Key& __v)
2000{
2001 __node_pointer __nd = __root();
Eric Fiselier7310ec82016-07-19 17:56:20 +00002002 __node_base_pointer* __nd_ptr = __root_ptr();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002003 if (__nd != nullptr)
2004 {
2005 while (true)
2006 {
2007 if (value_comp()(__v, __nd->__value_))
2008 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002009 if (__nd->__left_ != nullptr) {
2010 __nd_ptr = _VSTD::addressof(__nd->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002011 __nd = static_cast<__node_pointer>(__nd->__left_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002012 } else {
2013 __parent = static_cast<__parent_pointer>(__nd);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002014 return __parent->__left_;
2015 }
2016 }
2017 else if (value_comp()(__nd->__value_, __v))
2018 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002019 if (__nd->__right_ != nullptr) {
2020 __nd_ptr = _VSTD::addressof(__nd->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002021 __nd = static_cast<__node_pointer>(__nd->__right_);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002022 } else {
2023 __parent = static_cast<__parent_pointer>(__nd);
2024 return __nd->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002025 }
2026 }
2027 else
2028 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002029 __parent = static_cast<__parent_pointer>(__nd);
2030 return *__nd_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002031 }
2032 }
2033 }
Eric Fiselier7310ec82016-07-19 17:56:20 +00002034 __parent = static_cast<__parent_pointer>(__end_node());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002035 return __parent->__left_;
2036}
2037
2038// Find place to insert if __v doesn't exist
2039// First check prior to __hint.
2040// Next check after __hint.
2041// Next do O(log N) search.
2042// Set __parent to parent of null leaf
2043// Return reference to null leaf
2044// If __v exists, set parent to node of __v and return reference to node of __v
2045template <class _Tp, class _Compare, class _Allocator>
2046template <class _Key>
Eric Fiselier7310ec82016-07-19 17:56:20 +00002047typename __tree<_Tp, _Compare, _Allocator>::__node_base_pointer&
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002048__tree<_Tp, _Compare, _Allocator>::__find_equal(const_iterator __hint,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002049 __parent_pointer& __parent,
2050 __node_base_pointer& __dummy,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002051 const _Key& __v)
2052{
2053 if (__hint == end() || value_comp()(__v, *__hint)) // check before
2054 {
2055 // __v < *__hint
2056 const_iterator __prior = __hint;
2057 if (__prior == begin() || value_comp()(*--__prior, __v))
2058 {
2059 // *prev(__hint) < __v < *__hint
2060 if (__hint.__ptr_->__left_ == nullptr)
2061 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002062 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002063 return __parent->__left_;
2064 }
2065 else
2066 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002067 __parent = static_cast<__parent_pointer>(__prior.__ptr_);
2068 return static_cast<__node_base_pointer>(__prior.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002069 }
2070 }
2071 // __v <= *prev(__hint)
2072 return __find_equal(__parent, __v);
2073 }
2074 else if (value_comp()(*__hint, __v)) // check after
2075 {
2076 // *__hint < __v
Howard Hinnant0949eed2011-06-30 21:18:19 +00002077 const_iterator __next = _VSTD::next(__hint);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002078 if (__next == end() || value_comp()(__v, *__next))
2079 {
Howard Hinnant0949eed2011-06-30 21:18:19 +00002080 // *__hint < __v < *_VSTD::next(__hint)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002081 if (__hint.__get_np()->__right_ == nullptr)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002082 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002083 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2084 return static_cast<__node_base_pointer>(__hint.__ptr_)->__right_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002085 }
2086 else
2087 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002088 __parent = static_cast<__parent_pointer>(__next.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002089 return __parent->__left_;
2090 }
2091 }
2092 // *next(__hint) <= __v
2093 return __find_equal(__parent, __v);
2094 }
2095 // else __v == *__hint
Eric Fiselier7310ec82016-07-19 17:56:20 +00002096 __parent = static_cast<__parent_pointer>(__hint.__ptr_);
2097 __dummy = static_cast<__node_base_pointer>(__hint.__ptr_);
2098 return __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002099}
2100
2101template <class _Tp, class _Compare, class _Allocator>
2102void
Eric Fiselier7310ec82016-07-19 17:56:20 +00002103__tree<_Tp, _Compare, _Allocator>::__insert_node_at(__parent_pointer __parent,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002104 __node_base_pointer& __child,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002105 __node_base_pointer __new_node)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002106{
2107 __new_node->__left_ = nullptr;
2108 __new_node->__right_ = nullptr;
2109 __new_node->__parent_ = __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002110 // __new_node->__is_black_ is initialized in __tree_balance_after_insert
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002111 __child = __new_node;
2112 if (__begin_node()->__left_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002113 __begin_node() = static_cast<__iter_pointer>(__begin_node()->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002114 __tree_balance_after_insert(__end_node()->__left_, __child);
2115 ++size();
2116}
2117
Eric Fiselierdb215062016-03-31 02:15:15 +00002118#ifndef _LIBCPP_CXX03_LANG
2119template <class _Tp, class _Compare, class _Allocator>
2120template <class _Key, class... _Args>
2121pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2122__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
2123#else
2124template <class _Tp, class _Compare, class _Allocator>
2125template <class _Key, class _Args>
2126pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2127__tree<_Tp, _Compare, _Allocator>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
2128#endif
2129{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002130 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002131 __node_base_pointer& __child = __find_equal(__parent, __k);
2132 __node_pointer __r = static_cast<__node_pointer>(__child);
2133 bool __inserted = false;
2134 if (__child == nullptr)
2135 {
2136#ifndef _LIBCPP_CXX03_LANG
2137 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2138#else
2139 __node_holder __h = __construct_node(__args);
2140#endif
2141 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2142 __r = __h.release();
2143 __inserted = true;
2144 }
2145 return pair<iterator, bool>(iterator(__r), __inserted);
2146}
2147
2148
2149#ifndef _LIBCPP_CXX03_LANG
2150template <class _Tp, class _Compare, class _Allocator>
2151template <class _Key, class... _Args>
2152typename __tree<_Tp, _Compare, _Allocator>::iterator
2153__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2154 const_iterator __p, _Key const& __k, _Args&&... __args)
2155#else
2156template <class _Tp, class _Compare, class _Allocator>
2157template <class _Key, class _Args>
2158typename __tree<_Tp, _Compare, _Allocator>::iterator
2159__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_key_args(
2160 const_iterator __p, _Key const& __k, _Args& __args)
2161#endif
2162{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002163 __parent_pointer __parent;
2164 __node_base_pointer __dummy;
2165 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __k);
Eric Fiselierdb215062016-03-31 02:15:15 +00002166 __node_pointer __r = static_cast<__node_pointer>(__child);
2167 if (__child == nullptr)
2168 {
2169#ifndef _LIBCPP_CXX03_LANG
2170 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
2171#else
2172 __node_holder __h = __construct_node(__args);
2173#endif
2174 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
2175 __r = __h.release();
2176 }
2177 return iterator(__r);
2178}
2179
2180
2181#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002182
2183template <class _Tp, class _Compare, class _Allocator>
2184template <class ..._Args>
2185typename __tree<_Tp, _Compare, _Allocator>::__node_holder
2186__tree<_Tp, _Compare, _Allocator>::__construct_node(_Args&& ...__args)
2187{
Eric Fiselierdb215062016-03-31 02:15:15 +00002188 static_assert(!__is_tree_value_type<_Args...>::value,
2189 "Cannot construct from __value_type");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002190 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002191 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002192 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002193 __h.get_deleter().__value_constructed = true;
2194 return __h;
2195}
2196
Eric Fiselierdb215062016-03-31 02:15:15 +00002197
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002198template <class _Tp, class _Compare, class _Allocator>
2199template <class... _Args>
2200pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002201__tree<_Tp, _Compare, _Allocator>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002202{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002203 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002204 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002205 __node_base_pointer& __child = __find_equal(__parent, __h->__value_);
2206 __node_pointer __r = static_cast<__node_pointer>(__child);
2207 bool __inserted = false;
2208 if (__child == nullptr)
2209 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002210 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002211 __r = __h.release();
2212 __inserted = true;
2213 }
2214 return pair<iterator, bool>(iterator(__r), __inserted);
2215}
2216
2217template <class _Tp, class _Compare, class _Allocator>
2218template <class... _Args>
2219typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselier83c9dc12016-04-15 23:27:27 +00002220__tree<_Tp, _Compare, _Allocator>::__emplace_hint_unique_impl(const_iterator __p, _Args&&... __args)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002221{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002222 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002223 __parent_pointer __parent;
2224 __node_base_pointer __dummy;
2225 __node_base_pointer& __child = __find_equal(__p, __parent, __dummy, __h->__value_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002226 __node_pointer __r = static_cast<__node_pointer>(__child);
2227 if (__child == nullptr)
2228 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002229 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002230 __r = __h.release();
2231 }
2232 return iterator(__r);
2233}
2234
2235template <class _Tp, class _Compare, class _Allocator>
2236template <class... _Args>
2237typename __tree<_Tp, _Compare, _Allocator>::iterator
2238__tree<_Tp, _Compare, _Allocator>::__emplace_multi(_Args&&... __args)
2239{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002240 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002241 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002242 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002243 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002244 return iterator(static_cast<__node_pointer>(__h.release()));
2245}
2246
2247template <class _Tp, class _Compare, class _Allocator>
2248template <class... _Args>
2249typename __tree<_Tp, _Compare, _Allocator>::iterator
2250__tree<_Tp, _Compare, _Allocator>::__emplace_hint_multi(const_iterator __p,
2251 _Args&&... __args)
2252{
Howard Hinnant0949eed2011-06-30 21:18:19 +00002253 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Eric Fiselier7310ec82016-07-19 17:56:20 +00002254 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002255 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__h->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002256 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002257 return iterator(static_cast<__node_pointer>(__h.release()));
2258}
2259
Howard Hinnant73d21a42010-09-04 23:28:19 +00002260
Eric Fiselierdb215062016-03-31 02:15:15 +00002261#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002262
2263template <class _Tp, class _Compare, class _Allocator>
2264typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Eric Fiselierdb215062016-03-31 02:15:15 +00002265__tree<_Tp, _Compare, _Allocator>::__construct_node(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002266{
2267 __node_allocator& __na = __node_alloc();
Howard Hinnant99968442011-11-29 18:15:50 +00002268 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierdb215062016-03-31 02:15:15 +00002269 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002270 __h.get_deleter().__value_constructed = true;
Dimitry Andric89663502015-08-19 06:43:33 +00002271 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002272}
2273
Eric Fiselierdb215062016-03-31 02:15:15 +00002274#endif // _LIBCPP_CXX03_LANG
Howard Hinnantd0a2fbf2011-03-10 17:27:57 +00002275
Eric Fiselierdb215062016-03-31 02:15:15 +00002276#ifdef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002277template <class _Tp, class _Compare, class _Allocator>
2278typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002279__tree<_Tp, _Compare, _Allocator>::__insert_multi(const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002280{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002281 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002282 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002283 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002284 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002285 return iterator(__h.release());
2286}
2287
2288template <class _Tp, class _Compare, class _Allocator>
2289typename __tree<_Tp, _Compare, _Allocator>::iterator
Eric Fiselierdb215062016-03-31 02:15:15 +00002290__tree<_Tp, _Compare, _Allocator>::__insert_multi(const_iterator __p, const __container_value_type& __v)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002291{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002292 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002293 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__v));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002294 __node_holder __h = __construct_node(__v);
Howard Hinnant70342b92013-06-19 21:29:40 +00002295 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__h.get()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002296 return iterator(__h.release());
2297}
Eric Fiselierdb215062016-03-31 02:15:15 +00002298#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002299
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002300template <class _Tp, class _Compare, class _Allocator>
2301pair<typename __tree<_Tp, _Compare, _Allocator>::iterator, bool>
2302__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(__node_pointer __nd)
2303{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002304 __parent_pointer __parent;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002305 __node_base_pointer& __child = __find_equal(__parent, __nd->__value_);
2306 __node_pointer __r = static_cast<__node_pointer>(__child);
2307 bool __inserted = false;
2308 if (__child == nullptr)
2309 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002310 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002311 __r = __nd;
2312 __inserted = true;
2313 }
2314 return pair<iterator, bool>(iterator(__r), __inserted);
2315}
2316
2317template <class _Tp, class _Compare, class _Allocator>
2318typename __tree<_Tp, _Compare, _Allocator>::iterator
2319__tree<_Tp, _Compare, _Allocator>::__node_insert_unique(const_iterator __p,
2320 __node_pointer __nd)
2321{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002322 __parent_pointer __parent;
2323 __node_base_pointer __dummy;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002324 __node_base_pointer& __child = __find_equal(__p, __parent, __nd->__value_);
2325 __node_pointer __r = static_cast<__node_pointer>(__child);
2326 if (__child == nullptr)
2327 {
Howard Hinnant70342b92013-06-19 21:29:40 +00002328 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002329 __r = __nd;
2330 }
2331 return iterator(__r);
2332}
2333
2334template <class _Tp, class _Compare, class _Allocator>
2335typename __tree<_Tp, _Compare, _Allocator>::iterator
2336__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(__node_pointer __nd)
2337{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002338 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002339 __node_base_pointer& __child = __find_leaf_high(__parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002340 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002341 return iterator(__nd);
2342}
2343
2344template <class _Tp, class _Compare, class _Allocator>
2345typename __tree<_Tp, _Compare, _Allocator>::iterator
2346__tree<_Tp, _Compare, _Allocator>::__node_insert_multi(const_iterator __p,
2347 __node_pointer __nd)
2348{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002349 __parent_pointer __parent;
Eric Fiselierdb215062016-03-31 02:15:15 +00002350 __node_base_pointer& __child = __find_leaf(__p, __parent, _NodeTypes::__get_key(__nd->__value_));
Howard Hinnant70342b92013-06-19 21:29:40 +00002351 __insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__nd));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002352 return iterator(__nd);
2353}
2354
2355template <class _Tp, class _Compare, class _Allocator>
2356typename __tree<_Tp, _Compare, _Allocator>::iterator
2357__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p)
2358{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002359 __node_pointer __np = __p.__get_np();
2360 iterator __r(__p.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002361 ++__r;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002362 if (__begin_node() == __p.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002363 __begin_node() = __r.__ptr_;
2364 --size();
2365 __node_allocator& __na = __node_alloc();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002366 __tree_remove(__end_node()->__left_,
2367 static_cast<__node_base_pointer>(__np));
Eric Fiselierdb215062016-03-31 02:15:15 +00002368 __node_traits::destroy(__na, _NodeTypes::__get_ptr(
2369 const_cast<__node_value_type&>(*__p)));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002370 __node_traits::deallocate(__na, __np, 1);
2371 return __r;
2372}
2373
2374template <class _Tp, class _Compare, class _Allocator>
2375typename __tree<_Tp, _Compare, _Allocator>::iterator
2376__tree<_Tp, _Compare, _Allocator>::erase(const_iterator __f, const_iterator __l)
2377{
2378 while (__f != __l)
2379 __f = erase(__f);
Howard Hinnant70342b92013-06-19 21:29:40 +00002380 return iterator(__l.__ptr_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002381}
2382
2383template <class _Tp, class _Compare, class _Allocator>
2384template <class _Key>
2385typename __tree<_Tp, _Compare, _Allocator>::size_type
2386__tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k)
2387{
2388 iterator __i = find(__k);
2389 if (__i == end())
2390 return 0;
2391 erase(__i);
2392 return 1;
2393}
2394
2395template <class _Tp, class _Compare, class _Allocator>
2396template <class _Key>
2397typename __tree<_Tp, _Compare, _Allocator>::size_type
2398__tree<_Tp, _Compare, _Allocator>::__erase_multi(const _Key& __k)
2399{
2400 pair<iterator, iterator> __p = __equal_range_multi(__k);
2401 size_type __r = 0;
2402 for (; __p.first != __p.second; ++__r)
2403 __p.first = erase(__p.first);
2404 return __r;
2405}
2406
2407template <class _Tp, class _Compare, class _Allocator>
2408template <class _Key>
2409typename __tree<_Tp, _Compare, _Allocator>::iterator
2410__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v)
2411{
2412 iterator __p = __lower_bound(__v, __root(), __end_node());
2413 if (__p != end() && !value_comp()(__v, *__p))
2414 return __p;
2415 return end();
2416}
2417
2418template <class _Tp, class _Compare, class _Allocator>
2419template <class _Key>
2420typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2421__tree<_Tp, _Compare, _Allocator>::find(const _Key& __v) const
2422{
2423 const_iterator __p = __lower_bound(__v, __root(), __end_node());
2424 if (__p != end() && !value_comp()(__v, *__p))
2425 return __p;
2426 return end();
2427}
2428
2429template <class _Tp, class _Compare, class _Allocator>
2430template <class _Key>
2431typename __tree<_Tp, _Compare, _Allocator>::size_type
2432__tree<_Tp, _Compare, _Allocator>::__count_unique(const _Key& __k) const
2433{
Eric Fiselier227b47c2016-02-20 07:12:17 +00002434 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002435 while (__rt != nullptr)
2436 {
2437 if (value_comp()(__k, __rt->__value_))
2438 {
Eric Fiselier227b47c2016-02-20 07:12:17 +00002439 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002440 }
2441 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002442 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002443 else
2444 return 1;
2445 }
2446 return 0;
2447}
2448
2449template <class _Tp, class _Compare, class _Allocator>
2450template <class _Key>
2451typename __tree<_Tp, _Compare, _Allocator>::size_type
2452__tree<_Tp, _Compare, _Allocator>::__count_multi(const _Key& __k) const
2453{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002454 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002455 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002456 while (__rt != nullptr)
2457 {
2458 if (value_comp()(__k, __rt->__value_))
2459 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002460 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002461 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002462 }
2463 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002464 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002465 else
Howard Hinnant0949eed2011-06-30 21:18:19 +00002466 return _VSTD::distance(
Eric Fiselier7310ec82016-07-19 17:56:20 +00002467 __lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002468 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002469 );
2470 }
2471 return 0;
2472}
2473
2474template <class _Tp, class _Compare, class _Allocator>
2475template <class _Key>
2476typename __tree<_Tp, _Compare, _Allocator>::iterator
2477__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
2478 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002479 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002480{
2481 while (__root != nullptr)
2482 {
2483 if (!value_comp()(__root->__value_, __v))
2484 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002485 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002486 __root = static_cast<__node_pointer>(__root->__left_);
2487 }
2488 else
2489 __root = static_cast<__node_pointer>(__root->__right_);
2490 }
2491 return iterator(__result);
2492}
2493
2494template <class _Tp, class _Compare, class _Allocator>
2495template <class _Key>
2496typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2497__tree<_Tp, _Compare, _Allocator>::__lower_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002498 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002499 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002500{
2501 while (__root != nullptr)
2502 {
2503 if (!value_comp()(__root->__value_, __v))
2504 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002505 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002506 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002507 }
2508 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002509 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002510 }
2511 return const_iterator(__result);
2512}
2513
2514template <class _Tp, class _Compare, class _Allocator>
2515template <class _Key>
2516typename __tree<_Tp, _Compare, _Allocator>::iterator
2517__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
2518 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002519 __iter_pointer __result)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002520{
2521 while (__root != nullptr)
2522 {
2523 if (value_comp()(__v, __root->__value_))
2524 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002525 __result = static_cast<__iter_pointer>(__root);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002526 __root = static_cast<__node_pointer>(__root->__left_);
2527 }
2528 else
2529 __root = static_cast<__node_pointer>(__root->__right_);
2530 }
2531 return iterator(__result);
2532}
2533
2534template <class _Tp, class _Compare, class _Allocator>
2535template <class _Key>
2536typename __tree<_Tp, _Compare, _Allocator>::const_iterator
2537__tree<_Tp, _Compare, _Allocator>::__upper_bound(const _Key& __v,
Eric Fiselier227b47c2016-02-20 07:12:17 +00002538 __node_pointer __root,
Eric Fiselier7310ec82016-07-19 17:56:20 +00002539 __iter_pointer __result) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002540{
2541 while (__root != nullptr)
2542 {
2543 if (value_comp()(__v, __root->__value_))
2544 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002545 __result = static_cast<__iter_pointer>(__root);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002546 __root = static_cast<__node_pointer>(__root->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002547 }
2548 else
Eric Fiselier227b47c2016-02-20 07:12:17 +00002549 __root = static_cast<__node_pointer>(__root->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002550 }
2551 return const_iterator(__result);
2552}
2553
2554template <class _Tp, class _Compare, class _Allocator>
2555template <class _Key>
2556pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2557 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2558__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k)
2559{
Howard Hinnant99968442011-11-29 18:15:50 +00002560 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002561 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002562 __node_pointer __rt = __root();
2563 while (__rt != nullptr)
2564 {
2565 if (value_comp()(__k, __rt->__value_))
2566 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002567 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002568 __rt = static_cast<__node_pointer>(__rt->__left_);
2569 }
2570 else if (value_comp()(__rt->__value_, __k))
2571 __rt = static_cast<__node_pointer>(__rt->__right_);
2572 else
Howard Hinnant99968442011-11-29 18:15:50 +00002573 return _Pp(iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002574 iterator(
2575 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002576 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002577 : __result));
2578 }
Howard Hinnant99968442011-11-29 18:15:50 +00002579 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002580}
2581
2582template <class _Tp, class _Compare, class _Allocator>
2583template <class _Key>
2584pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2585 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2586__tree<_Tp, _Compare, _Allocator>::__equal_range_unique(const _Key& __k) const
2587{
Howard Hinnant99968442011-11-29 18:15:50 +00002588 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002589 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002590 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002591 while (__rt != nullptr)
2592 {
2593 if (value_comp()(__k, __rt->__value_))
2594 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002595 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002596 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002597 }
2598 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002599 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002600 else
Howard Hinnant99968442011-11-29 18:15:50 +00002601 return _Pp(const_iterator(__rt),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002602 const_iterator(
2603 __rt->__right_ != nullptr ?
Eric Fiselier7310ec82016-07-19 17:56:20 +00002604 static_cast<__iter_pointer>(__tree_min(__rt->__right_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002605 : __result));
2606 }
Howard Hinnant99968442011-11-29 18:15:50 +00002607 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002608}
2609
2610template <class _Tp, class _Compare, class _Allocator>
2611template <class _Key>
2612pair<typename __tree<_Tp, _Compare, _Allocator>::iterator,
2613 typename __tree<_Tp, _Compare, _Allocator>::iterator>
2614__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k)
2615{
Howard Hinnant99968442011-11-29 18:15:50 +00002616 typedef pair<iterator, iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002617 __iter_pointer __result = __end_node();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002618 __node_pointer __rt = __root();
2619 while (__rt != nullptr)
2620 {
2621 if (value_comp()(__k, __rt->__value_))
2622 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002623 __result = static_cast<__iter_pointer>(__rt);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002624 __rt = static_cast<__node_pointer>(__rt->__left_);
2625 }
2626 else if (value_comp()(__rt->__value_, __k))
2627 __rt = static_cast<__node_pointer>(__rt->__right_);
2628 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002629 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002630 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
2631 }
Howard Hinnant99968442011-11-29 18:15:50 +00002632 return _Pp(iterator(__result), iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002633}
2634
2635template <class _Tp, class _Compare, class _Allocator>
2636template <class _Key>
2637pair<typename __tree<_Tp, _Compare, _Allocator>::const_iterator,
2638 typename __tree<_Tp, _Compare, _Allocator>::const_iterator>
2639__tree<_Tp, _Compare, _Allocator>::__equal_range_multi(const _Key& __k) const
2640{
Howard Hinnant99968442011-11-29 18:15:50 +00002641 typedef pair<const_iterator, const_iterator> _Pp;
Eric Fiselier7310ec82016-07-19 17:56:20 +00002642 __iter_pointer __result = __end_node();
Eric Fiselier227b47c2016-02-20 07:12:17 +00002643 __node_pointer __rt = __root();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002644 while (__rt != nullptr)
2645 {
2646 if (value_comp()(__k, __rt->__value_))
2647 {
Eric Fiselier7310ec82016-07-19 17:56:20 +00002648 __result = static_cast<__iter_pointer>(__rt);
Eric Fiselier227b47c2016-02-20 07:12:17 +00002649 __rt = static_cast<__node_pointer>(__rt->__left_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002650 }
2651 else if (value_comp()(__rt->__value_, __k))
Eric Fiselier227b47c2016-02-20 07:12:17 +00002652 __rt = static_cast<__node_pointer>(__rt->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002653 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002654 return _Pp(__lower_bound(__k, static_cast<__node_pointer>(__rt->__left_), static_cast<__iter_pointer>(__rt)),
Eric Fiselier227b47c2016-02-20 07:12:17 +00002655 __upper_bound(__k, static_cast<__node_pointer>(__rt->__right_), __result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002656 }
Howard Hinnant99968442011-11-29 18:15:50 +00002657 return _Pp(const_iterator(__result), const_iterator(__result));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002658}
2659
2660template <class _Tp, class _Compare, class _Allocator>
2661typename __tree<_Tp, _Compare, _Allocator>::__node_holder
Howard Hinnant8b537682011-06-04 17:10:24 +00002662__tree<_Tp, _Compare, _Allocator>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002663{
Eric Fiselier7310ec82016-07-19 17:56:20 +00002664 __node_pointer __np = __p.__get_np();
2665 if (__begin_node() == __p.__ptr_)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002666 {
2667 if (__np->__right_ != nullptr)
Eric Fiselier7310ec82016-07-19 17:56:20 +00002668 __begin_node() = static_cast<__iter_pointer>(__np->__right_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002669 else
Eric Fiselier7310ec82016-07-19 17:56:20 +00002670 __begin_node() = static_cast<__iter_pointer>(__np->__parent_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002671 }
2672 --size();
2673 __tree_remove(__end_node()->__left_,
2674 static_cast<__node_base_pointer>(__np));
Marshall Clow01c1c6f2015-01-28 19:54:25 +00002675 return __node_holder(__np, _Dp(__node_alloc(), true));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002676}
2677
Howard Hinnant7686add2011-06-04 14:31:57 +00002678template <class _Tp, class _Compare, class _Allocator>
2679inline _LIBCPP_INLINE_VISIBILITY
2680void
2681swap(__tree<_Tp, _Compare, _Allocator>& __x,
2682 __tree<_Tp, _Compare, _Allocator>& __y)
2683 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2684{
2685 __x.swap(__y);
2686}
2687
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002688_LIBCPP_END_NAMESPACE_STD
2689
Eric Fiselier018a3d52017-05-31 22:07:49 +00002690_LIBCPP_POP_MACROS
2691
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00002692#endif // _LIBCPP___TREE